]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - embedded/app/usbcan/main.c
Added vendor functions to embedded application, data transferred by usb channel seria...
[lincan.git] / embedded / app / usbcan / main.c
index 97d9785907fd158223ccb49d4c4fe452367385ee..b9271e3407ffad0ccac4dc106d27596283ac2ef9 100644 (file)
 #include <lpciap.h>
 #include <lpciap_kvpb.h>
 
+#include <endian.h>
+#if __BYTE_ORDER == __BIG_ENDIAN
+  #include <byteswap.h>
+#endif
+
 #include "./can/can.h"
 #include "./can/sja1000p.h"
 #include "./can/main.h"
@@ -26,6 +31,8 @@
 #include "./can/ul_usb1.h"
 //#include "./can/setup.h"
 
+#include "./usb/usb_vend.h"
+
 #define MASK_EP1RX  0x01
 #define MASK_EP1TX  0x02
 
@@ -83,6 +90,8 @@ struct canhardware_t *hardware_p=&canhardware;
 struct canchip_t *chips_p[MAX_TOT_CHIPS];
 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
 
+struct canuser_t *canuser;
+
 
 /***********************************************************************
  * SOMETHING BAD HAPPENED
@@ -134,7 +143,6 @@ int main(void)
        struct candevice_t *candev;
        struct canchip_t *chip=NULL;
        struct msgobj_t *obj;
-       struct canuser_t *canuser;
        struct canque_ends_t *qends;
        struct canque_edge_t *edge,*qedge;
        struct canque_slot_t *slot;
@@ -312,12 +320,26 @@ int main(void)
                        if (canque_get_inslot(qends, &qedge, &slot, 0)>=0){ //Free slot obtained
                                size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,USB_MAX_PACKET);
                                if (size==16){
+                                       uint16_t msgflags;
+                                       uint32_t msgid;
                                        canmsg.cob=0;
-                                       canmsg.id=*((uint32_t*)ep1_rx_buff);
-                                       canmsg.flags=(*((uint32_t*)(ep1_rx_buff+4)))>>8;
-                                       canmsg.length=(*((uint16_t*)(ep1_rx_buff+6)))&0x00FF;
-                                       for (i=0;i<CAN_MSG_LENGTH;i++){
-                                               canmsg.data[i]=*((unsigned char*)(ep1_rx_buff+8+i));
+                                       canmsg.length=*(uint8_t *)(ep1_rx_buff+1);
+                                       if (canmsg.length > CAN_MSG_LENGTH)
+                                               canmsg.length=CAN_MSG_LENGTH;
+                                       msgflags=*(uint16_t *)(ep1_rx_buff+2);
+                                       msgid=*(uint32_t *)(ep1_rx_buff+4);
+                                       #if __BYTE_ORDER == __BIG_ENDIAN
+                                               msgflags  = bswap_16( msgflags);
+                                               msgid  = bswap_32( msgid);
+                                       #endif
+                                       canmsg.flags=msgflags;
+                                       canmsg.id=msgid;
+
+                                       for (i=0;i<canmsg.length;i++){
+                                               canmsg.data[i]=*(unsigned char*)(ep1_rx_buff+8+i);
+                                       }
+                                       for (;i<CAN_MSG_LENGTH;i++){
+                                               canmsg.data[i]=0;
                                        }
                                        /* Automatic selection of extended format if ID>2047 */
                                        if (canmsg.id & ~0x7ffl & MSG_ID_MASK ) canmsg.flags |= MSG_EXT;
@@ -352,10 +374,26 @@ int main(void)
 
                if(usb_device.ep_events & MASK_EP1TX){ //EP1TX - data transmitted
                        if(canque_test_outslot(qends, &qedge, &slot)>=0){
-                               (*((uint32_t*)ep1_tx_buff))=slot->msg.id;
-                               (*((uint32_t*)(ep1_tx_buff+4)))=slot->msg.flags<<8 | (slot->msg.length&0xFF);
-                               for (i=0;i<CAN_MSG_LENGTH;i++){
-                                       (*((uint32_t*)(ep1_tx_buff+8+i)))=slot->msg.data[i];
+                               uint16_t msgflags;
+                               uint32_t msgid;
+
+                               *(uint8_t *)(ep1_tx_buff)=0;
+                               *(uint8_t *)(ep1_tx_buff+1)=slot->msg.length;
+
+                               msgflags=slot->msg.flags;
+                               msgid=slot->msg.id;
+                               #if __BYTE_ORDER == __BIG_ENDIAN
+                                       msgflags  = bswap_16( msgflags);
+                                       msgid  = bswap_32( msgid);
+                               #endif
+
+                               *(uint16_t *)(ep1_tx_buff+2)=msgflags;
+                               *(uint32_t *)(ep1_tx_buff+4)=msgid;
+                               for (i=0;i<slot->msg.length;i++){
+                                       *(uint8_t *)(ep1_tx_buff+8+i)=slot->msg.data[i];
+                               }
+                               for (;i<CAN_MSG_LENGTH;i++){
+                                       *(uint8_t *)(ep1_tx_buff+8+i)=0;
                                }
                                usb_udev_write_endpoint(&eps[1],ep1_tx_buff,USB_MAX_PACKET);