]> 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 8ee992c4efe655f11d5f51024fa9be91f2d93c9c..460c85d7573fac82bc5dfa5fcff80e3067cc063e 100644 (file)
@@ -1,19 +1,23 @@
+// #define CAN_DEBUG
+
 #include <stdio.h>
 #include <system_def.h>
-#include <hal_intr.h>
-#include "./can/ul_usb1.h"
+//#include <hal_intr.h>
 #include "./can/can.h"
 #include "./can/can_sysdep.h"
 #include "./can/main.h"
 #include "./can/devcommon.h"
 #include "./usb/usb_vend.h"
-//#include "./can/ul_usb1.h"
+#include "./can/ul_usb1.h"
 #include <endian.h>
 #if __BYTE_ORDER == __BIG_ENDIAN
   #include <byteswap.h>
 #endif
 
+#include "./can/can_bittiming.h"
+
 extern struct canuser_t *canuser;
+extern uint8_t vendor_ret;
 
 int set_ext_mask_complete_fnc(struct usb_ep_t *ep, int status){
   int dest_chip;
@@ -95,30 +99,137 @@ nodata:
        return -1;
 }
 
+
+
+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(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;
+
+               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);
+               tseg1  = bswap_32(tseg1);
+               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);
+                               
+
+       }
+       return 0;
+
+error:
+       return -1;
+}
+
 int usbcan_vendor(usb_device_t *udev)
 {
   // wIndex, wValue, bRequest, wLength
   int dest_chip;
   struct ul_usb1_chip_data *chip_data;
-  uint8_t ret;
 
   dest_chip=(udev->request.wIndex);
   if ((dest_chip>=MAX_TOT_CHIPS)||(dest_chip<0))
-       return 0; // Should look like ok (0) or stall (-1)?
+       return -1; // Should look like ok (0) or stall (-1)?
   if (!chips_p[dest_chip])
-       return 0; // Should look like ok (0) or stall (-1)?
+       return -1; // Should look like ok (0) or stall (-1)?
 
   switch ( udev->request.bRequest) {
+               
+               //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 - end
+
                case USBCAN_VENDOR_EXT_MASK_SET:
                        udev->ep0.complete_fnc=set_ext_mask_complete_fnc;
                        return 1;
                case USBCAN_VENDOR_EXT_MASK_STATUS:
-                       ret=-1;
+                       vendor_ret=-1;
                        if ((chip_data=((struct ul_usb1_chip_data*)(chips_p[dest_chip]->chip_data)))==NULL)
-                               usb_send_control_data(udev,&ret,1);
+                               usb_send_control_data(udev,&vendor_ret,1);
                        else{
-                               ret=(chip_data->flags & UL_USB1_CHIP_MASK_SET)?1:0;
-                               usb_send_control_data(udev,&ret,1);
+                               vendor_ret=(chip_data->flags & UL_USB1_CHIP_MASK_SET)?1:0;
+                               usb_send_control_data(udev,&vendor_ret,1);
                        }
                        chip_data->flags &= ~UL_USB1_CHIP_MASK_SET;
                        return 1;
@@ -127,12 +238,12 @@ int usbcan_vendor(usb_device_t *udev)
                        udev->ep0.complete_fnc=set_baud_rate_complete_fnc;
                        return 1;
                case USBCAN_VENDOR_BAUD_RATE_STATUS:
-                       ret=-1;
+                       vendor_ret=-1;
                        if ((chip_data=((struct ul_usb1_chip_data*)(chips_p[dest_chip]->chip_data)))==NULL)
-                               usb_send_control_data(udev,&ret,1);
+                               usb_send_control_data(udev,&vendor_ret,1);
                        else{
-                               ret=(chip_data->flags & UL_USB1_CHIP_BAUD_SET)?1:0;
-                               usb_send_control_data(udev,&ret,1);
+                               vendor_ret=(chip_data->flags & UL_USB1_CHIP_BAUD_SET)?1:0;
+                               usb_send_control_data(udev,&vendor_ret,1);
                        }
                        chip_data->flags &= ~UL_USB1_CHIP_BAUD_SET;
                        return 1;
@@ -140,11 +251,10 @@ int usbcan_vendor(usb_device_t *udev)
                case USBCAN_VENDOR_SET_BTREGS:
                        {
                                uint16_t value=udev->request.wValue;
-                               ret=1;
+                               vendor_ret=1;
                                if (chips_p[dest_chip]->chipspecops->set_btregs(chips_p[dest_chip],value&0xFF,(value>>8)&0xFF)<0)
-                                       ret=0;
-                               usb_send_control_data(udev,&ret,1);
-                               return 1;
+                                       vendor_ret=0;
+                               usb_send_control_data(udev,&vendor_ret,1);
                        }
                        return 1;
 
@@ -152,26 +262,28 @@ int usbcan_vendor(usb_device_t *udev)
                        {
                                struct canque_edge_t *qedge;
                                struct canque_slot_t *slot;
-                               ret=0;
+                               vendor_ret=0;
                                if (canque_get_inslot(canuser->qends, &qedge, &slot, 0)>=0){
                                        canque_abort_inslot(canuser->qends, qedge, slot);
-                                       ret=1;
+                                       DEBUGMSG("USBCAN_VENDOR_CHECK_TX_STAT - Free slot found\r\n");
+                                       vendor_ret=1;
                                }
-                               usb_send_control_data(udev,&ret,1);
+                               DEBUGMSG("USBCAN_VENDOR_CHECK_TX_STAT - Sending %d\r\n",vendor_ret);
+                               usb_send_control_data(udev,&vendor_ret,1);
                                return 1;
                        }
 
                case USBCAN_VENDOR_START_CHIP:
-                       ret=1;
+                       vendor_ret=1;
                        if (chips_p[dest_chip]->chipspecops->start_chip(chips_p[dest_chip])<0)
-                               ret=0;
-                       usb_send_control_data(udev,&ret,1);
+                               vendor_ret=0;
+                       usb_send_control_data(udev,&vendor_ret,1);
                        return 1;
                case USBCAN_VENDOR_STOP_CHIP:
-                       ret=1;
+                       vendor_ret=1;
                        if (chips_p[dest_chip]->chipspecops->stop_chip(chips_p[dest_chip])<0)
-                               ret=0;
-                       usb_send_control_data(udev,&ret,1);
+                               vendor_ret=0;
+                       usb_send_control_data(udev,&vendor_ret,1);
                        return 1;
   }