X-Git-Url: http://rtime.felk.cvut.cz/gitweb/lincan.git/blobdiff_plain/a73d6ffea8948a9abc9bfb32728bb255cec18317..4b51897df818c0cef960dbfab815c7e69586c654:/embedded/app/usbcan/usb_vend.c diff --git a/embedded/app/usbcan/usb_vend.c b/embedded/app/usbcan/usb_vend.c index 1f5d41f..460c85d 100644 --- a/embedded/app/usbcan/usb_vend.c +++ b/embedded/app/usbcan/usb_vend.c @@ -14,9 +14,7 @@ #include #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,29 +99,30 @@ nodata: return -1; } -int set_canbtr_complete_fnc(struct usb_ep_t *ep, int status){ - int dest_chip; - 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); @@ -131,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; } @@ -160,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;