To avoid this problem I suggest you to read ESR register and modify the FLEXCAN_ClearErrIntStatusFlag function to allow another parameter as mask to clear, and the Error_Handler.
Be aware that reading the ESR register can alter the content of the register from previous read by clear the status of some bits. RM : "Read this register to capture all error condition and status bits. This action clears the
respective bits that were set since the last read access."
This is way the driver was implemented by clear all condition and to user to read the status of errors to capture all the events.
void FLEXCAN_Error_IRQHandler(uint8_t instance)
{
DEV_ASSERT(instance < CAN_INSTANCE_COUNT);
CAN_Type * base = g_flexcanBase[instance];
flexcan_state_t * state = g_flexcanStatePtr[instance];
/* Invoke callback */
aux = read ESR;
if (state->error_callback != NULL)
{
state->error_callback(instance, FLEXCAN_EVENT_ERROR, state);
}
/* Clear all other interrupts in ESR1 register (Error, Busoff, Wakeup) */
FLEXCAN_ClearErrIntStatusFlag(base,aux);
return;
}
void FLEXCAN_ClearErrIntStatusFlag(CAN_Type * base, uint32_t aux)
{
if((base->ESR1 & FLEXCAN_ALL_INT) != 0U)
{
(base->ESR1) = FLEXCAN_ALL_INT&aux;
#ifdef ERRATA_E9005
/* Dummy read as a workaround for errata e9005 to ensure the flags are
cleared before continuing. */
(void)(base->ESR1);
#endif
}
}
BR,
Alexandru Nan