diff --git a/include/usbd_gs_can.h b/include/usbd_gs_can.h index d9b66b8..a7653dc 100644 --- a/include/usbd_gs_can.h +++ b/include/usbd_gs_can.h @@ -59,7 +59,7 @@ typedef struct { struct gs_host_frame *from_host_buf; - can_data_t *channels[NUM_CAN_CHANNEL]; + can_data_t channels[NUM_CAN_CHANNEL]; led_data_t *leds; bool dfu_detach_requested; @@ -84,7 +84,6 @@ typedef struct { #endif uint8_t USBD_GS_CAN_Init(USBD_GS_CAN_HandleTypeDef *hcan, USBD_HandleTypeDef *pdev, queue_t *q_frame_pool, queue_t *q_from_host, led_data_t *leds); -void USBD_GS_CAN_SetChannel(USBD_HandleTypeDef *pdev, uint8_t channel, can_data_t* handle); void USBD_GS_CAN_SuspendCallback(USBD_HandleTypeDef *pdev); void USBD_GS_CAN_ResumeCallback(USBD_HandleTypeDef *pdev); bool USBD_GS_CAN_TxReady(USBD_HandleTypeDef *pdev); diff --git a/src/main.c b/src/main.c index de24070..901a960 100644 --- a/src/main.c +++ b/src/main.c @@ -49,7 +49,6 @@ static void SystemClock_Config(void); static bool send_to_host_or_enqueue(struct gs_host_frame *frame); static void send_to_host(void); -static can_data_t hCAN = {0}; static USBD_GS_CAN_HandleTypeDef hGS_CAN; static USBD_HandleTypeDef hUSB = {0}; static led_data_t hLED = {0}; @@ -60,6 +59,7 @@ static queue_t *q_to_host = NULL; int main(void) { + can_data_t *channel = &hGS_CAN.channels[0]; uint32_t last_can_error_status = 0; HAL_Init(); @@ -80,8 +80,8 @@ int main(void) led_set_mode(&hLED, led_mode_off); timer_init(); - can_init(&hCAN, CAN_INTERFACE); - can_disable(&hCAN); + can_init(channel, CAN_INTERFACE); + can_disable(channel); q_frame_pool = queue_create(CAN_QUEUE_SIZE); @@ -99,7 +99,6 @@ int main(void) USBD_Init(&hUSB, (USBD_DescriptorsTypeDef*)&FS_Desc, DEVICE_FS); USBD_RegisterClass(&hUSB, &USBD_GS_CAN); USBD_GS_CAN_Init(&hGS_CAN, &hUSB, q_frame_pool, q_from_host, &hLED); - USBD_GS_CAN_SetChannel(&hUSB, 0, &hCAN); USBD_Start(&hUSB); #ifdef CAN_S_GPIO_Port @@ -109,7 +108,7 @@ int main(void) while (1) { struct gs_host_frame *frame = queue_pop_front(q_from_host); if (frame != 0) { // send can message from host - if (can_send(&hCAN, frame)) { + if (can_send(channel, frame)) { // Echo sent frame back to host frame->flags = 0x0; frame->reserved = 0x0; @@ -126,11 +125,11 @@ int main(void) send_to_host(); } - if (can_is_rx_pending(&hCAN)) { + if (can_is_rx_pending(channel)) { struct gs_host_frame *frame = queue_pop_front(q_frame_pool); if (frame != 0) { - if (can_receive(&hCAN, frame)) { + if (can_receive(channel, frame)) { frame->timestamp_us = timer_get(); frame->echo_id = 0xFFFFFFFF; // not a echo frame @@ -152,11 +151,11 @@ int main(void) // received frame", so wait until we get there. LEC will hold some error // to report even if multiple pass by. } else { - uint32_t can_err = can_get_error_status(&hCAN); + uint32_t can_err = can_get_error_status(channel); struct gs_host_frame *frame = queue_pop_front(q_frame_pool); if (frame != 0) { frame->timestamp_us = timer_get(); - if (can_parse_error_status(can_err, last_can_error_status, &hCAN, frame)) { + if (can_parse_error_status(can_err, last_can_error_status, channel, frame)) { send_to_host_or_enqueue(frame); last_can_error_status = can_err; } else { diff --git a/src/usbd_gs_can.c b/src/usbd_gs_can.c index 30893db..5473db0 100644 --- a/src/usbd_gs_can.c +++ b/src/usbd_gs_can.c @@ -323,15 +323,6 @@ static uint8_t USBD_GS_CAN_SOF(struct _USBD_HandleTypeDef *pdev) return USBD_OK; } -void USBD_GS_CAN_SetChannel(USBD_HandleTypeDef *pdev, uint8_t channel, can_data_t* handle) { - USBD_GS_CAN_HandleTypeDef *hcan = (USBD_GS_CAN_HandleTypeDef*) pdev->pClassData; - - assert_basic(hcan); - assert_basic(channel < NUM_CAN_CHANNEL); - hcan->channels[channel] = handle; - return; -} - static const led_seq_step_t led_identify_seq[] = { { .state = 0x01, .time_in_10ms = 10 }, { .state = 0x02, .time_in_10ms = 10 }, @@ -368,7 +359,7 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) { if (param_u32) { led_run_sequence(hcan->leds, led_identify_seq, -1); } else { - ch = hcan->channels[req->wValue]; // TODO verify wValue input data (implement getChannelData() ?) + ch = &hcan->channels[req->wValue]; // TODO verify wValue input data (implement getChannelData() ?) led_set_mode(hcan->leds, can_is_enabled(ch) ? led_mode_normal : led_mode_off); } break; @@ -386,7 +377,7 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) { if (req->wValue < NUM_CAN_CHANNEL) { mode = (struct gs_device_mode*)hcan->ep0_buf; - ch = hcan->channels[req->wValue]; + ch = &hcan->channels[req->wValue]; if (mode->mode == GS_CAN_MODE_RESET) { @@ -414,7 +405,7 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) { timing = (struct gs_device_bittiming*)hcan->ep0_buf; if (req->wValue < NUM_CAN_CHANNEL) { can_set_bittiming( - hcan->channels[req->wValue], + &hcan->channels[req->wValue], timing->brp, timing->prop_seg + timing->phase_seg1, timing->phase_seg2, @@ -725,8 +716,9 @@ void USBD_GS_CAN_SuspendCallback(USBD_HandleTypeDef *pdev) { for (uint32_t i=0; ichannels[i] != NULL) - can_disable(hcan->channels[i]); + can_data_t *channel = &hcan->channels[i]; + + can_disable(channel); } }