cancel
Showing results for 
Search instead for 
Did you mean: 

Handling FDCAN HAL ErrorCode

CTapp.1
Senior III

I am using the HAL FDCAN driver in queue mode (STM32G431, FW_G4 V1.6.1).

The application is (intentionally) working with a high bus load (~90%), so it is expected that there will occasionally be times when there are no transmit message buffers available - so HAL_FDCAN_AddMessageToTxFifoQ() is expected to return HAL_Error, as that is what it does if this is the case.

The application code can then use HAL_FDCAN_GetError() to get the ErrorCode (though this seems a bit pointless, as instance->ErrorCode does the same, so not a lot of abstraction here).

However, it only becomes clear from looking at the HAL code that ErrorCode uses bits to represent the set of possible errors - so it is not as simple as comparing the value with HAL_FDCAN_ERROR_FIFO_FULL (the test needs to use a mask operation). The documentation does not explain that this is the case.

How should the error be cleared once it has been handled? The HAL does not provide an API for this, so it appears as if the user is expected to directly alter the device's state through its handle, using something like:

hfdcan1.ErrorCode &= ~HAL_FDCAN_ERROR_FIFO_FULL;

This seems to defeat the point of having an API, which should be abstracting hardware manipulation from the application code (though this could be argued for the whole of the HAL, as I would also have expected all device handles to be opaque pointers to prevent the application from manipulating a driver's state).

Am I missing something?

All posts are made in a personal capacity
MISRA C++ Chair
MISRA C WG Member
Director The MISRA Consortium Limited (TMCL)
1 ACCEPTED SOLUTION

Accepted Solutions
Saket_Om
ST Employee

Hello @CTapp.1 

The HAL does not provide a dedicated function to clear specific error bits after you have handled them. The only way to clear an error bit (such as HAL_FDCAN_ERROR_FIFO_FULL) is to manually clear it in your application code, as you described.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

View solution in original post

3 REPLIES 3
Saket_Om
ST Employee

Hello @CTapp.1 

The HAL does not provide a dedicated function to clear specific error bits after you have handled them. The only way to clear an error bit (such as HAL_FDCAN_ERROR_FIFO_FULL) is to manually clear it in your application code, as you described.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
Karl Yamashita
Principal

The interrupt error flags are cleared at register level in the HAL_FDCAN_IRQHandler.

The hfdcan1.ErrorCode just holds the last error bits which you can view in the HAL_FDCAN_ErrorCallback and act upon the errors. You don't have to necessarily clear any bits in hfdcan1.ErrorCode as it has no impact to the flags/bits in the Interrupt register.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.
CAN Jammer an open source CAN bus hacking tool
CANableV3 Open Source

@Karl Yamashita 

Thanks, but I am not using interrupts and these are API errors, not hardware errors.

All posts are made in a personal capacity
MISRA C++ Chair
MISRA C WG Member
Director The MISRA Consortium Limited (TMCL)