cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4xx Rework of HAL CAN driver: examples?

Stefan Meyre
Associate III
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

1 ACCEPTED SOLUTION

Accepted Solutions
Szymon PANECKI
Senior III
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

View solution in original post

16 REPLIES 16
Szymon PANECKI
Senior III
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

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

Posted on February 02, 2018 at 19:21

Good day!

Will it be possible in the new version of the implementation of working with SAN to determine which of the filters triggered the interrupt?

Posted on February 20, 2018 at 21:50

I found a CAN_Networking example under the following path:

STM32Cube_FW_F4_V1.0\Projects\STM324xG_EVAL\Examples\CAN\CAN_Networking

Panecki.Szymon.002

‌ - thx for your short nice migration guide!

Rashmy Parimi
Associate
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

Posted on June 25, 2018 at 14:42

Hello Szymon, 

I would like to implement CAN Bus Communication on my STM32F767ZI Nucleo Board. I looked around to find some example codes and the result was always the same example shared in this post by Stefan Meyre. As you mentioned, the problem is that  the new HAL Driver for CAN has different functions from the previous version. So I was wondering if you or some other St guy are working on some example code with the functions from the new HAL CAN driver. It would be very useful to share it as a starting point for CAN code implementation.

Thank you.

PS: I am aware of the presentation you made about STM32F7xxx CAN implementation, but also in this pdf the example is implemented with the old HAL CAN Driver version

Regards

Francesco

Posted on June 25, 2018 at 17:19

Hi

Francesco,

Thanks for contacting me and asking.

Recently I prepared for one of customers presentation, which guides how to develop a simple CAN application with new API and how to migrate from old API to new API. Please find it attached. For purpose of this presentation I used STM32L4, however approach used for STM32F7 should be the same.

Good luck with your STM32 CAN project!

Regards

Szymon

________________

Attachments :

STM32L4 CAN new API and migration.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxTW&d=%2Fa%2F0X0000000ayX%2F88jLLXCT3K5cAKBDLIwfRvrqV8wrr5Rvq0_amyQl1dk&asPdf=false
Posted on June 25, 2018 at 22:26

Dear Szymon, 

this is exactly what I was looking for. It may seem simple for an embedded system designer to migrate from an old driver to a new one, but for people like me (mechanical engineer) that want to use these boards for their projects, tutorials and starting guide are very important. I am appreciating the growing St community, and the availability of the people like you who help 'newbies' like me sharing their knowledge with them. Well done. I will try your example as soon as possible.

Regards 

Francesco

Posted on June 25, 2018 at 22:56

Thanks for your feedback and for kind words. I am very glad to hear that this material will be helpful to you.

Regards

Szymon