]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - embedded/app/usbcan/usb_vend.c
embedded: reintroduce missing emulated bus initialization for UL_USB1 board.
[lincan.git] / embedded / app / usbcan / usb_vend.c
index 3f0afc9e8f278df17afdb45fbf75fbd5dca3e41d..460c85d7573fac82bc5dfa5fcff80e3067cc063e 100644 (file)
@@ -14,9 +14,7 @@
   #include <byteswap.h>
 #endif
 
-// lpc17xx debug
-#include "./can/lpc17xx_can.h"
-// lpc17xx debug - end
+#include "./can/can_bittiming.h"
 
 extern struct canuser_t *canuser;
 extern uint8_t vendor_ret;
@@ -101,30 +99,30 @@ nodata:
        return -1;
 }
 
-int set_canbtr_complete_fnc(struct usb_ep_t *ep, int status){
-  int dest_chip;
 
-//     int32_t rate,sjw,sampl_pt,flags;
-       uint32_t brp, sjw, tseg1, tseg2;
 
-       struct can_lmc1_chip_data *chip_data=NULL;
+int set_bittiming_complete_fnc(struct usb_ep_t *ep, int status){
+  int dest_chip;
+
+       int32_t brp, sjw, tseg1, tseg2;
 
        usb_device_t *udev=ep->udev;
        unsigned char *data=ep->ptr - ep->actual;
 
-       if (udev->request.bRequest==USBCAN_VENDOR_SET_CANBTR){
+       if(ep->actual != USBCAN_BITTIMING_SIZE)
+               goto error;
+
+       if (udev->request.bRequest==USBCAN_VENDOR_SET_BITTIMING){
                dest_chip=(udev->request.wIndex);
                if ((dest_chip>=MAX_TOT_CHIPS)||(dest_chip<0))
                        goto error;
                if (!chips_p[dest_chip])
                        goto error;
-               if ((chip_data=((struct can_lmc1_chip_data*)(chips_p[dest_chip]->chip_data)))==NULL)
-                       goto nodata;
 
-               brp=*(uint32_t *)(data);
-               sjw=*(uint32_t *)(data+4);
-               tseg1=*(uint32_t *)(data+8);
-               tseg2=*(uint32_t *)(data+12);
+               brp=*(int32_t *)(data);
+               sjw=*(int32_t *)(data+4);
+               tseg1=*(int32_t *)(data+8);
+               tseg2=*(int32_t *)(data+12);
                #if __BYTE_ORDER == __BIG_ENDIAN
                brp  = bswap_32(brp);
                sjw  = bswap_32(sjw);
@@ -132,18 +130,69 @@ int set_canbtr_complete_fnc(struct usb_ep_t *ep, int status){
                tseg2  = bswap_32(tseg2);
                #endif
 
+
+               if (chips_p[dest_chip]->chipspecops->set_bittiming(chips_p[dest_chip], brp, sjw, tseg1, tseg2)<0)
+                       goto error;
+       }
+       return 0;
+
+error:
+       return -1;
+}
+
+int usbcan_get_bittiming_const_reply(usb_device_t *udev){
+       int dest_chip;
+       struct canchip_t *chip;
+
+       struct can_bittiming_const btc_buff;
+       struct can_bittiming_const *btc = &btc_buff;
+
+       uint8_t buffer[USBCAN_BITTIMING_CONST_SIZE];
+       uint32_t *ptr;
+
+       if (udev->request.bRequest==USBCAN_VENDOR_GET_BITTIMING_CONST){
+
+               dest_chip=(udev->request.wIndex);
+               if ((dest_chip>=MAX_TOT_CHIPS)||(dest_chip<0))
+                       goto error;
+               if ((chip = chips_p[dest_chip]) == NULL)
+                       goto error;
+               if (chip->chipspecops->get_bittiming_const == NULL){
+                       goto error;
+               }
+
+               chip->chipspecops->get_bittiming_const(chip, btc);              
+               ptr = (uint32_t*) buffer;
+
+               #if __BYTE_ORDER == __BIG_ENDIAN
+               *(ptr++) = bswap_32(chips_p[dest_chip]->clock);
+               *(ptr++) = bswap_32(btc->tseg1_min);
+               *(ptr++) = bswap_32(btc->tseg1_max);
+               *(ptr++) = bswap_32(btc->tseg2_min);
+               *(ptr++) = bswap_32(btc->tseg2_max);
+               *(ptr++) = bswap_32(btc->sjw_max);
+               *(ptr++) = bswap_32(btc->brp_min);
+               *(ptr++) = bswap_32(btc->brp_max);
+               *(ptr) = bswap_32(btc->brp_inc);
+               #else
+               *(ptr++) = chips_p[dest_chip]->clock;
+               *(ptr++) = btc->tseg1_min;
+               *(ptr++) = btc->tseg1_max;
+               *(ptr++) = btc->tseg2_min;
+               *(ptr++) = btc->tseg2_max;
+               *(ptr++) = btc->sjw_max;
+               *(ptr++) = btc->brp_min;
+               *(ptr++) = btc->brp_max;
+               *(ptr) = btc->brp_inc;
+               #endif
+
+               usb_send_control_data(udev, buffer, USBCAN_BITTIMING_CONST_SIZE);
                                
-               printf("BRP: %d, SJW: %d, TSEG1: %d, TSEG2: %d \n", brp, sjw, tseg1, tseg2);
-               CAN_set_bittiming(chips_p[dest_chip], brp, sjw, tseg1, tseg2);  
 
-//             if (chips_p[dest_chip]->chipspecops->baud_rate(chips_p[dest_chip], rate, chips_p[dest_chip]->clock, sjw, sampl_pt, flags)<0)
-//                     goto error;
-               chip_data->flags |= CAN_LMC1_CHIP_CANBTR_SET;
        }
        return 0;
+
 error:
-       chip_data->flags &= ~CAN_LMC1_CHIP_CANBTR_SET;
-nodata:
        return -1;
 }
 
@@ -161,11 +210,15 @@ int usbcan_vendor(usb_device_t *udev)
 
   switch ( udev->request.bRequest) {
                
-               //lpc17xx debug 
-               case USBCAN_VENDOR_SET_CANBTR:
-                       udev->ep0.complete_fnc=set_canbtr_complete_fnc;
+               //lpc17xx
+               case USBCAN_VENDOR_SET_BITTIMING:
+                       udev->ep0.complete_fnc=set_bittiming_complete_fnc;
+                       return 1;
+
+               case USBCAN_VENDOR_GET_BITTIMING_CONST:
+                       usbcan_get_bittiming_const_reply(udev);
                        return 1;
-               //lpc17xx debug - end
+               //lpc17xx - end
 
                case USBCAN_VENDOR_EXT_MASK_SET:
                        udev->ep0.complete_fnc=set_ext_mask_complete_fnc;