diff --git a/include/can.h b/include/can.h index 8376aea..e04676b 100644 --- a/include/can.h +++ b/include/can.h @@ -34,6 +34,7 @@ THE SOFTWARE. typedef struct { CAN_TypeDef *instance; + uint32_t reg_esr_old; uint16_t brp; uint8_t phase_seg1; uint8_t phase_seg2; @@ -58,6 +59,7 @@ uint32_t can_get_error_status(can_data_t *hcan); /** parse status value returned by can_get_error_status(). * @param frame : will hold the generated error frame + * @param err : holds the contents of the ESR register * @return 1 when status changes (if any) need a new error frame sent */ -bool can_parse_error_status(uint32_t err, uint32_t last_err, can_data_t *hcan, struct gs_host_frame *frame); +bool can_parse_error_status(can_data_t *hcan, struct gs_host_frame *frame, uint32_t err); diff --git a/src/can.c b/src/can.c index b55c07c..27c4967 100644 --- a/src/can.c +++ b/src/can.c @@ -260,13 +260,15 @@ static bool status_is_active(uint32_t err) return !(err & (CAN_ESR_BOFF | CAN_ESR_EPVF)); } -bool can_parse_error_status(uint32_t err, uint32_t last_err, can_data_t *hcan, struct gs_host_frame *frame) +bool can_parse_error_status(can_data_t *hcan, struct gs_host_frame *frame, uint32_t err) { + uint32_t last_err = hcan->reg_esr_old; /* We build up the detailed error information at the same time as we decide * whether there's anything worth sending. This variable tracks that final * result. */ bool should_send = false; - (void) hcan; + + hcan->reg_esr_old = err; frame->echo_id = 0xFFFFFFFF; frame->can_id = CAN_ERR_FLAG; diff --git a/src/main.c b/src/main.c index 26a10cb..b11fc9f 100644 --- a/src/main.c +++ b/src/main.c @@ -55,7 +55,6 @@ static led_data_t hLED = {0}; int main(void) { can_data_t *channel = &hGS_CAN.channels[0]; - uint32_t last_can_error_status = 0; HAL_Init(); SystemClock_Config(); @@ -174,10 +173,8 @@ int main(void) restore_irq(was_irq_enabled); frame->timestamp_us = timer_get(); - if (can_parse_error_status(can_err, last_can_error_status, channel, frame)) { + if (can_parse_error_status(channel, frame, can_err)) { list_add_tail_locked(&frame_object->list, &hGS_CAN.list_to_host); - - last_can_error_status = can_err; } else { list_add_tail_locked(&frame_object->list, &hGS_CAN.list_frame_pool); }