diff --git a/board/drivers/bxcan.h b/board/drivers/bxcan.h index 27ffcee525..89fdd96057 100644 --- a/board/drivers/bxcan.h +++ b/board/drivers/bxcan.h @@ -23,6 +23,11 @@ bool can_set_speed(uint8_t can_number) { return ret; } +void can_clear_send(CAN_TypeDef *CANx, uint8_t can_number) { + can_health[can_number].can_core_reset_cnt += 1U; + llcan_clear_send(CANx); +} + void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) { CAN_TypeDef *CANx = CANIF_FROM_CAN_NUM(can_number); uint32_t esr_reg = CANx->ESR; @@ -52,8 +57,7 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) { can_health[can_number].total_rx_lost_cnt += 1U; CANx->RF0R &= ~(CAN_RF0R_FOVR0); } - can_health[can_number].can_core_reset_cnt += 1U; - llcan_clear_send(CANx); + can_clear_send(CANx, can_number); } } diff --git a/board/drivers/fdcan.h b/board/drivers/fdcan.h index 0ba228a9e6..00bc1a3489 100644 --- a/board/drivers/fdcan.h +++ b/board/drivers/fdcan.h @@ -35,6 +35,12 @@ bool can_set_speed(uint8_t can_number) { return ret; } +void can_clear_send(FDCAN_GlobalTypeDef *FDCANx, uint8_t can_number) { + can_health[can_number].can_core_reset_cnt += 1U; + can_health[can_number].total_tx_lost_cnt += (FDCAN_TX_FIFO_EL_CNT - (FDCANx->TXFQS & FDCAN_TXFQS_TFFL)); // TX FIFO msgs will be lost after reset + llcan_clear_send(FDCANx); +} + void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) { FDCAN_GlobalTypeDef *FDCANx = CANIF_FROM_CAN_NUM(can_number); uint32_t psr_reg = FDCANx->PSR; @@ -75,9 +81,7 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) { // 2. H7 gets stuck in bus off recovery state indefinitely if ((((can_health[can_number].last_error == CAN_ACK_ERROR) || (can_health[can_number].last_data_error == CAN_ACK_ERROR)) && (can_health[can_number].transmit_error_cnt > 127U)) || ((ir_reg & FDCAN_IR_BO) != 0U)) { - can_health[can_number].can_core_reset_cnt += 1U; - can_health[can_number].total_tx_lost_cnt += (FDCAN_TX_FIFO_EL_CNT - (FDCANx->TXFQS & FDCAN_TXFQS_TFFL)); // TX FIFO msgs will be lost after reset - llcan_clear_send(FDCANx); + can_clear_send(FDCANx, can_number); } } } diff --git a/board/main.c b/board/main.c index 3db278eb50..7f95abadef 100644 --- a/board/main.c +++ b/board/main.c @@ -95,7 +95,7 @@ void set_safety_mode(uint16_t mode, uint16_t param) { if (current_board->has_obd) { // Clear any pending messages in the can core (i.e. sending while comma power is unplugged) // TODO: rewrite using hardware queues rather than fifo to cancel specific messages - llcan_clear_send(CANIF_FROM_CAN_NUM(1)); + can_clear_send(CANIF_FROM_CAN_NUM(1), 1); if (param == 0U) { current_board->set_can_mode(CAN_MODE_OBD_CAN2); } else {