Skip to main content
Stefan Meyre
Associate III
February 1, 2018
Solved

STM32F4xx Rework of HAL CAN driver: examples?

  • February 1, 2018
  • 16 replies
  • 6281 views
Posted on February 01, 2018 at 21:39

Hello!

The HAL CAN driver for the STM32F4 got reworked lately (december 2017). I try to port my code from the old to the new drivers (compatibility break). There is a brief driver description in the update history but it would be also very helpful to get some example code to speed up porting ;-). I did look up the Cube STM32F4 sample codes but it seems all the CAN samples got deleted (not yet developed?).... Also the HAL description is still version 5 and the new API is not yet worked in. Correct, or did I miss anything?

Cheers, Stefan

    This topic has been closed for replies.
    Best answer by Szymon PANECKI
    Posted on February 01, 2018 at 22:03

    Hello Stefan,

    This is correct that HAL CAN driver has been redesigned with new API.

    Please find below a short migration guide:

    • Fields of CAN_InitTypeDef structure are renamed: SJW to SyncJumpWidth, BS1 to TimeSeg1, BS2 to TimeSeg2, ABOM to AutoBusOff, AWUM to AutoWakeUp, NART to AutoRetransmission (inversed), RFLM to ReceiveFifoLocked and TXFP to TransmitFifoPriority
    • HAL_CAN_Init() is split into both HAL_CAN_Init() and HAL_CAN_Start()
    • HAL_CAN_Transmit() is replaced by HAL_CAN_AddTxMessage() to place Tx request, then HAL_CAN_GetTxMailboxesFreeLevel() for polling until completion
    • HAL_CAN_Transmit_IT() is replaced by HAL_CAN_ActivateNotification() to enable transmission with interrupt mode, then HAL_CAN_AddTxMessage() to place Tx request
    • HAL_CAN_Receive() is replaced by HAL_CAN_GetRxFifoFillLevel() for polling until reception, then HAL_CAN_GetRxMessage() to get Rx message
    • HAL_CAN_Receive_IT() is replaced by HAL_CAN_ActivateNotification() to enable reception with interrupt mode, then HAL_CAN_GetRxMessage() in the receive callback to get Rx message
    • HAL_CAN_Sleep() is renamed to HAL_CAN_RequestSleep()
    • HAL_CAN_TxCpltCallback() is split into HAL_CAN_TxMailbox0CompleteCallback(), HAL_CAN_TxMailbox1CompleteCallback() and HAL_CAN_TxMailbox2CompleteCallback()
    • HAL_CAN_RxCpltCallback() is split into HAL_CAN_RxFifo0MsgPendingCallback() and HAL_CAN_RxFifo1MsgPendingCallback()

    More complete 'how to use the new driver' is detailed in the driver header section itself.

    Regards

    Szymon

    16 replies

    Szymon PANECKI
    Szymon PANECKIBest answer
    Senior III
    February 1, 2018
    Posted on February 01, 2018 at 22:03

    Hello Stefan,

    This is correct that HAL CAN driver has been redesigned with new API.

    Please find below a short migration guide:

    • Fields of CAN_InitTypeDef structure are renamed: SJW to SyncJumpWidth, BS1 to TimeSeg1, BS2 to TimeSeg2, ABOM to AutoBusOff, AWUM to AutoWakeUp, NART to AutoRetransmission (inversed), RFLM to ReceiveFifoLocked and TXFP to TransmitFifoPriority
    • HAL_CAN_Init() is split into both HAL_CAN_Init() and HAL_CAN_Start()
    • HAL_CAN_Transmit() is replaced by HAL_CAN_AddTxMessage() to place Tx request, then HAL_CAN_GetTxMailboxesFreeLevel() for polling until completion
    • HAL_CAN_Transmit_IT() is replaced by HAL_CAN_ActivateNotification() to enable transmission with interrupt mode, then HAL_CAN_AddTxMessage() to place Tx request
    • HAL_CAN_Receive() is replaced by HAL_CAN_GetRxFifoFillLevel() for polling until reception, then HAL_CAN_GetRxMessage() to get Rx message
    • HAL_CAN_Receive_IT() is replaced by HAL_CAN_ActivateNotification() to enable reception with interrupt mode, then HAL_CAN_GetRxMessage() in the receive callback to get Rx message
    • HAL_CAN_Sleep() is renamed to HAL_CAN_RequestSleep()
    • HAL_CAN_TxCpltCallback() is split into HAL_CAN_TxMailbox0CompleteCallback(), HAL_CAN_TxMailbox1CompleteCallback() and HAL_CAN_TxMailbox2CompleteCallback()
    • HAL_CAN_RxCpltCallback() is split into HAL_CAN_RxFifo0MsgPendingCallback() and HAL_CAN_RxFifo1MsgPendingCallback()

    More complete 'how to use the new driver' is detailed in the driver header section itself.

    Regards

    Szymon

    Stefan Meyre
    Associate III
    February 2, 2018
    Posted on February 02, 2018 at 07:41

    Thank you Szymon! Yes I saw these explanations in the comments to the driver description. And I use it ;-). However I do not have that much experience with the CAN bus and would like to have a look at some sample code to find my way around...

    Cheers Stefan

    update: examples can be found here: C:\....STM32Cube\Repository\STM32Cube_FW_F4_V1.19.0\Projects\STM32446E_EVAL\Examples\CAN\CAN_Loopback\Src

    Rashmy Parimi
    Visitor II
    March 23, 2018
    Posted on March 23, 2018 at 02:03

    Hi

    I have recently updated to new CAN driver and I found it useful for few things which were not as easy to do with the older version. I however am not entirely clear on the Rx mechanism:

    Does Rx always start with RxFifo 0 by default or is there anyway to change this at init or during runtime?

    Is it possible to ping pong between RxFifo 0 and RxFifo 1 in runtime?

    Thanks

    Rashmy

    Levs Levs
    Visitor II
    July 5, 2018
    Posted on July 05, 2018 at 19:20

    Hello guys,

     will be update this CAN driver also on the STM32F1 firmware package?

    Best regards,

    Levs

    DTerr
    Visitor II
    August 29, 2018

    Not sure about the STM32F1 but I have heard that STM32F0 is due the 17th week of 2019.

    Eich
    Associate III
    September 12, 2018

    Hi !

    One additional topic...

    Filter configuration also changed a bit, what is the replacement for the "FilterNumber" ? And how can I change the interrupt priority. Could that be still done by HAL_NVIC_SetPriority(CAN2_RX0_IRQn...)?

    Regards,

    Dirk

    AErns
    Visitor II
    October 10, 2018

    Hello Szymon.

    I'm struggling to find a very simple and up to date can example, just to see some activity on the Tx pin.

    You are talking about a  customers presentation just above, but what do you mean by "Please find it attached"? - I can't see any attachments anywhere.

    Regards Allan

    JSzuc
    Associate II
    April 10, 2019

    Hi Szymon!

    I try the example on an stm32f103c8t6 and its working fine, but I cant change the filter config to "_IDLIST", Its simply not work anymore in any combination of the mask and id… May be I missed something...

    Regards.

    J.

    RAlex
    Visitor II
    July 1, 2019

    Hi szja681.5541402147734006E12,

    I began recently with Microcontroller and STM32 and I stumbled over the same issue.

    If you are using a standard-ID ( CAN_TxHeaderTypeDef TxHeader; TxHeader.IDE = CAN_ID_STD;)

    and Filterscale 32Bit (sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;)

    you have to shift FilterIDHigh and FilterMaskIdHigh by 5 Bits to the left.

    E.g.

    sFilterConfig.FilterIdHigh = 0x0123<<5; 
    sFilterConfig.FilterMaskIdHigh = 0x0123<<5;

    This is IMO due to the register in the Tx Mailbox.

    Here is picture from the STM32F103 Reference Manual .

    As you can see the Std-Id is from [31:21].

    0690X000009YEi6QAG.png

    If you don't shift the Filter ID the comparison will fail.

    Because the STD-ID is left justified in the comparing Mailbox.

    Here is a pic from the register Organisation.

    Mapping is your Mailbox register.0690X000009YEoEQAW.pngI hope this will help someone.

    Pictures are taken from the reference manual and are not my property.

    Greetings

    Rene

    MarcV
    Associate II
    April 19, 2020

    Not sure if anyone is still looking for examples of the new HAL CAN driver like I was just this week. The in my opinion most useful yet simple example can be found here:

    https://github.com/timsonater/stm32-CAN-bus-example-HAL-API