cancel
Showing results for 
Search instead for 
Did you mean: 

CAN application using HAL Library

Posted on January 26, 2015 at 16:01

Hello there,

I am new to STM32 but I have some expirience with AVR8 and 32. I am trying to make a simple demo program using stm32f4discovery board that would allow me to send and receive data on CAN1 channel. For the start I would take the interrupt aproach and if get to know it change it to DMA for receiving and transmitting data. I downloaded the UM1725 document and tried to take it as refference, but it is way to ''general'' in the example section for me.

I would really aprichiate if someone could recomend me a step by step tutorial for configuring CAN peripheral using HAL library, and even better if there is one showing how to config it with the use of DMA.

#hal #can #dma
7 REPLIES 7
jpeacock
Associate II
Posted on January 26, 2015 at 19:46

CAN on the STM32 uses a dedicated buffer and hardware FIFO for the mailboxes so there's no DMA.  Given the short length of the message and the time to set up DMA there isn't much benefit in any case.  And if you prioritize messages (used by protocols like CANopen) the order in which messages are sent doesn't match the order received so using DMA could get complicated.

Pay attention to the two RX streams, there's a reason for them.  Use one for high priority and one for low so you can process urgent messages as soon as possible (for instance, the EMCY and SRDO messages in the CANopen protocol).  Use the filters to direct incoming messages to the low or high priority FIFO.

Same for TX, let the hardware prioritize outgoing messages so the bus gets the urgent ones first.  This is especially important for safety related message where the window between initial and complemented message is critical.

I haven't used the HAL routines but the older Std Peripheral Library calls worked well for a CAN network.  The CAN examples in the ST library are poor but do cover the basics.  Be sure to implement all the interrupts, especially the SCE as it is the best way to detect bus errors.

And remember you need two nodes to test, CAN does not work with just one node.

  Jack Peacock
Posted on January 26, 2015 at 19:55

I do have 2 nodes- one node would be my stm32f4discovery board and the other one would be my PCAN usb adapter, so i can send the messages between the board and PC. Would you mind sharing the source you got the ''poor'' examples from?

Posted on January 26, 2015 at 21:25

They are in the SPL (Standard Peripheral Library) in your case the F4 DSP one. And I've posted several working CAN examples for both end of the connections on F2 and F4 platforms.

No one here of any merit is using HAL, so don't expect any examples.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 26, 2015 at 23:05

Is there something wrong with HAL library? I Have to say I faced a simmilar reaction when asked about Atmel Software Framework in AVR world. People claimed it was bloated, is this the case here as well? Could you show a link to your tutorials? I tried to find them but without succes? I would really aprichiate it.

Posted on January 27, 2015 at 00:27

In all honesty I don't do ''tutorials'', I post code that works, on hardware that works. If you walk in my footprints your system will also work, but I do tire of people who walk off the trail and fall off the cliff, and then cry for help.... I'm not falling off the cliff to save anyone. I might throw you a rope after I tie it to a boulder or tree.

I've published a lot on the forum, I find Google or Bing to be easier than the forum's search, which is next to useless.

Here are a couple to start with

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/CAN%20not%20working%20on%20STM32F205%20%28proto%20board%29&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=117]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FCAN%20not%20working%20on%20STM32F205%20%28proto%20board%29&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=117

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Can-Bus%20Error&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=122]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Can-Bus%20Error&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=122

I don't like the thickness of the HAL abstraction, and I don't like the paradigm it forces, I am not alone.

Atmel's Studio is a bit of a sloth, the Visual Studio based system runs painfully slowly on a lot of systems which are otherwise quite productive.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 27, 2015 at 08:41

Thank you a lot :), ill give it a go.

Amel NASRI
ST Employee
Posted on January 28, 2015 at 08:50

Hi Bremen,

In the

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1897/PF259243

, there are CAN examples related to the eval boards that can be easily updated to be tested with discovery board.

You can look in the following folders for examples:

- STM32Cube_FW_F4_V1.4.0\Projects\STM324xG_EVAL\Examples\CAN\CAN_LoopBack

- STM32Cube_FW_F4_V1.4.0\Projects\STM324xG_EVAL\Examples\CAN\CAN_Networking

- STM32Cube_FW_F4_V1.4.0\Projects\STM324xG_EVAL\Examples\CAN\CAN_Networking

- STM32Cube_FW_F4_V1.4.0\Projects\STM324x9I_EVAL\Examples\CAN\CAN_Networking

-Mayla-

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.