cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G4 FDCAN HAL Driver Issue (v1.5.0)

DAshb.11
Associate

The HAL_FDCAN_ErrorCallback is erroneously being called during every interrupt after a Protocol Error (PEA) occurs, even when no error exists during the interrupt. I believe this condition will occur for some of the other interrupts as well (ELO, WDI, PED, ARA).

The HAL_FDCAN_IRQHandler does not appear to correctly clear the Error bits of the FDCAN_HandleTypeDef between interrupt calls. Once a Protocol Error (PEA) occurs the HAL_FDCAN_ERROR_PROTOCOL_ARBT bit is set and never cleared.

  Errors = hfdcan->Instance->IR & FDCAN_ERROR_MASK;
  Errors &= hfdcan->Instance->IE;
 
  if (Errors != 0U)
  {
    /* Clear the Error flags */
    __HAL_FDCAN_CLEAR_FLAG(hfdcan, Errors);
 
    /* Update error code */
    hfdcan->ErrorCode |= Errors;
  }
 
  if (hfdcan->ErrorCode != HAL_FDCAN_ERROR_NONE)
  {
#if USE_HAL_FDCAN_REGISTER_CALLBACKS == 1
    /* Call registered callback*/
    hfdcan->ErrorCallback(hfdcan);
#else
    /* Error Callback */
    HAL_FDCAN_ErrorCallback(hfdcan);
#endif /* USE_HAL_FDCAN_REGISTER_CALLBACKS */
  }

I have added clearing the Protocol Error flags to the HAL_FDCAN_ErrorCallback interrupt and that appears to resolve the issue (at least for the Protocol Error issue).

void HAL_FDCAN_ErrorCallback(FDCAN_HandleTypeDef *hfdcan) {
    hfdcan->ErrorCode &= ~(FDCAN_IR_ELO | FDCAN_IR_WDI | FDCAN_IR_PEA | FDCAN_IR_PED | FDCAN_IR_ARA);
}

Steps to reproduce:

  1. Setup the STM32G4 with a working CAN bus (any bit rate, properly terminated, at least one other device to acknowledge frames).
  2. Use the HAL to Configure and Activate the interrupt for the ARB_PROTOCOL_ERROR.
  3. Remove termination from the CAN bus.
  4. Attempt to transmit a CAN frame onto the bus using the HAL, this should cause a Protocol Error to occur.
  5. Observe that the HAL_FDCAN_ErrorCallback is correctly called for each Protocol Error.
  6. Replace the termination on the CAN bus.
  7. Transmit another frame on the CAN bus, this should result in a successful transmission.
  8. Observe that the HAL_FDCAN_ErrorCallback is erroneously called during every other CAN interrupt even when there are no Protocol Errors.

0 REPLIES 0