diff --git a/src/can.c b/src/can.c --- a/src/can.c +++ b/src/can.c @@ -10,6 +10,8 @@ bool can_silenced; uint32_t can_broadcast_id; uint32_t can_rx_id; +volatile CanBuffer can_rx_buffer; + void can_init(uint32_t rx_id, uint32_t broadcast_id) { can_handle.Instance = CAN; @@ -43,13 +45,15 @@ void can_init(uint32_t rx_id, uint32_t b can_filter.FilterActivation = ENABLE; HAL_CAN_ConfigFilter(&can_handle, &can_filter); - HAL_NVIC_SetPriority(CEC_CAN_IRQn, 1, 0); - HAL_NVIC_EnableIRQ(CEC_CAN_IRQn); - HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0); + //HAL_NVIC_SetPriority(CEC_CAN_IRQn, 1, 0); + //HAL_NVIC_EnableIRQ(CEC_CAN_IRQn); + //HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0); can_silenced = false; can_rx_id = rx_id; can_broadcast_id = broadcast_id; + + can_buffer_init(&can_rx_buffer); } void can_send_test(uint16_t id) @@ -120,14 +124,42 @@ bool can_silence_bus(bool value) return result; } +void can_receive(void) +{ + if ((can_handle.Instance->RF0R & CAN_RF0R_FMP0) != 0) + { + HAL_StatusTypeDef status = HAL_CAN_Receive(&can_handle, 0, 0); + + if ((status == HAL_OK) & ((can_handle.pRxMsg->StdId == can_rx_id) | (can_handle.pRxMsg->StdId == can_broadcast_id))) + { + can_buffer_add(&can_rx_buffer, can_handle.pRxMsg); + } + //can_handle.Instance->RF0R |= CAN_RF0R_RFOM0; + + led_blink_once(LED_CAN, 250); + } +} + +void can_process_receive_buffer(void) +{ + CanRxMsgTypeDef msg; + uint8_t size = can_buffer_size(&can_rx_buffer); + + for (uint8_t i = 0; i < size; i++) + { + msg = *can_buffer_remove(&can_rx_buffer); + protocol_receive_message(&msg); + } +} + void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan) { if ((hcan->pRxMsg->StdId == can_rx_id) | (hcan->pRxMsg->StdId == can_broadcast_id)) { - protocol_receive_message(hcan->pRxMsg); + can_buffer_add(&can_rx_buffer, hcan->pRxMsg); } - led_blink_once(LED_CAN, 500); + led_blink_once(LED_ERROR, 500); HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0); }