From 6728e464dbd9929725b8394ca423690873677d9c Mon Sep 17 00:00:00 2001 From: Jiri Vanek Date: Sun, 8 Apr 2012 14:03:20 +0200 Subject: [PATCH] Added functionality for bittiming setting by the OS driver --- embedded/app/usbcan/can/lpc17xx_can.h | 3 ++ embedded/app/usbcan/lpc17xx_can.c | 23 +++++++++++ embedded/app/usbcan/usb/usb_vend.h | 3 ++ embedded/app/usbcan/usb_vend.c | 57 +++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) diff --git a/embedded/app/usbcan/can/lpc17xx_can.h b/embedded/app/usbcan/can/lpc17xx_can.h index 0e137a9..d920ffd 100644 --- a/embedded/app/usbcan/can/lpc17xx_can.h +++ b/embedded/app/usbcan/can/lpc17xx_can.h @@ -150,6 +150,7 @@ void CAN_init(struct canchip_t *chip); void CAN_send(struct canchip_t *chip, canmsg_t* msg); void CAN_recv(struct canchip_t *chip, canmsg_t* msg); void CAN_IRQHandler(void); +void CAN_set_bittiming(struct canchip_t *chip, uint32_t brp, uint32_t sjw, uint32_t tseg1, uint32_t tseg2); //---------------------------------- @@ -158,6 +159,8 @@ struct can_lmc1_chip_data int flags; }; +#define CAN_LMC1_CHIP_CANBTR_SET (1<<1) + // board can-lmc1 specific functions: int can_lmc1_register(struct hwspecops_t *hwspecops); int can_lmc1_init_hw_data(struct candevice_t *candev); diff --git a/embedded/app/usbcan/lpc17xx_can.c b/embedded/app/usbcan/lpc17xx_can.c index 8b3a773..fd09804 100644 --- a/embedded/app/usbcan/lpc17xx_can.c +++ b/embedded/app/usbcan/lpc17xx_can.c @@ -406,6 +406,29 @@ void CAN_send(struct canchip_t *chip, canmsg_t* msg){ } +void CAN_set_bittiming(struct canchip_t *chip, uint32_t brp, uint32_t sjw, uint32_t tseg1, uint32_t tseg2){ + + uint8_t SAM = 0; // 0 = the bus is sampled once + + brp--; + sjw--; + tseg1--; + tseg2--; + + can_disable_irq(chip->chip_irq); + // enter reset mode + can_write_reg(chip, 1, CAN_MOD_o); + + + can_write_reg(chip, ((SAM<<23)|(tseg2<<20)|(tseg1<<16)|(sjw<<14)|(brp<<0)), CAN_BTR_o); + + + // return to normal operating + can_write_reg(chip, 0, CAN_MOD_o); + + can_enable_irq(chip->chip_irq); +} + void CAN_setBusTiming(struct canchip_t *chip){ uint32_t PCLK_CAN; diff --git a/embedded/app/usbcan/usb/usb_vend.h b/embedded/app/usbcan/usb/usb_vend.h index 30835d0..8be5754 100644 --- a/embedded/app/usbcan/usb/usb_vend.h +++ b/embedded/app/usbcan/usb/usb_vend.h @@ -12,6 +12,9 @@ #define USBCAN_VENDOR_EXT_MASK_SET (7) #define USBCAN_VENDOR_EXT_MASK_STATUS (8) +//lpc17xx debug +#define USBCAN_VENDOR_SET_CANBTR (9) + int usbcan_vendor(usb_device_t *udev); int set_ext_mask_complete_fnc(struct usb_ep_t *ep, int status); diff --git a/embedded/app/usbcan/usb_vend.c b/embedded/app/usbcan/usb_vend.c index 29cea43..3f0afc9 100644 --- a/embedded/app/usbcan/usb_vend.c +++ b/embedded/app/usbcan/usb_vend.c @@ -14,6 +14,10 @@ #include #endif +// lpc17xx debug +#include "./can/lpc17xx_can.h" +// lpc17xx debug - end + extern struct canuser_t *canuser; extern uint8_t vendor_ret; @@ -97,6 +101,52 @@ 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; + + usb_device_t *udev=ep->udev; + unsigned char *data=ep->ptr - ep->actual; + + if (udev->request.bRequest==USBCAN_VENDOR_SET_CANBTR){ + 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); + #if __BYTE_ORDER == __BIG_ENDIAN + brp = bswap_32(brp); + sjw = bswap_32(sjw); + tseg1 = bswap_32(tseg1); + tseg2 = bswap_32(tseg2); + #endif + + + 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; +} + int usbcan_vendor(usb_device_t *udev) { // wIndex, wValue, bRequest, wLength @@ -110,6 +160,13 @@ int usbcan_vendor(usb_device_t *udev) return -1; // Should look like ok (0) or stall (-1)? switch ( udev->request.bRequest) { + + //lpc17xx debug + case USBCAN_VENDOR_SET_CANBTR: + udev->ep0.complete_fnc=set_canbtr_complete_fnc; + return 1; + //lpc17xx debug - end + case USBCAN_VENDOR_EXT_MASK_SET: udev->ep0.complete_fnc=set_ext_mask_complete_fnc; return 1; -- 2.39.2