cancel
Showing results for 
Search instead for 
Did you mean: 

CAN bus on STM32F103: how to begin

emilio
Associate II
Posted on November 25, 2014 at 23:48

Hi everybody, 

these days I'm starting a project based on STM32F103RE uC for which I have to use the CAN bus pheripheral to transmit and receive messages.

I have no previous experience with CAN pheripheral on STM32 and I'd like some advices to start my work.

In addition at the standard pheripheral library and the reference manual

-which application note/other documents can I study?

-Are there any valid code example ready to download?

-Could someone post me some configuration example?

Thanks in advance! 

#can #can #code #example #stm32
6 REPLIES 6
Posted on November 26, 2014 at 01:34

Review the EVAL board schematics, and the software examples.

STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\CAN\DualCAN\main.c

Get yourself a board with CAN interfaces, preferably two.

You need a receiving device to acknowledge data being sent.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
emilio
Associate II
Posted on November 30, 2014 at 23:36

Thanks for answering me.

I think I'll use two STM32 Nucleo boards in order to implement a CAN transmitter and a CAN receiver.

The example seems very useful, I'll post here later if I have any dubt! 🙂

dteal
Associate II
Posted on February 08, 2015 at 21:40

Clive1,

I too am new to the CAN bus code but have spent the last few days looking over examples and searching the web for tips. I copied your sample code from the RedDragon 407 and will be comparing it closely with my code (which came from the ST examples and use the StdPeriph library.

I'm just trying to get transmit to work, so my question is this: shouldn't I see at least some o-scope activity on the CANH or CANL signals on the bus side of the transceiver? I'm using the STM32303C-EVAL unit and everything else seems fine. I also have a Microchip CAN bus analyzer that can receive as well as transmit - I can see signals from it in transmit mode (and not connected to any peer).

Thanks for your help.

DT

Posted on February 09, 2015 at 03:35

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Basic%20CAN%20bus%20transmit&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=1]Other Thread

I haven't tinkered with this F3 board, the lack of two boards, or two buses, is going to make it difficult to test or evaluate. Generally you need some CAN device to receive the transmissions.

You might want to check the Loop Back modes, and if those do, or do not generate CAN_TX, and CAN_H and CAN_L signals.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dteal
Associate II
Posted on February 09, 2015 at 23:34

Thanks Clive. I got it to work and can see pulses on the CAN transceiver with nothing else attached to the bus. The problem was that I was using GPIO_AF_9 instead of GPIO_AF_7.  I can transmit and receive (using the Microchip bus analyzer in transmit mode) at 1 Mbps!

I do have one more questions that applies to coding style: in order to get the receive to work I had to comment out this function in stm32f30x_it.c:

void USB_LP_CAN1_RX0_IRQHandler(void)

and then add it to my main.c (and my supporting code) to extract the received data. That seems dangerous if ST: is going to update those files in the future, my code will break. Is there another, more elegant approach to this?

Thanks,

DT

Posted on February 10, 2015 at 00:35

Well the stm32f3xx_it.c file is project specific so no one is going to change it on you. I tend to present examples that work as a single monolithic file as this significantly improves the clarity, and reduces the ''why doesn't this compile'' type responses.

Once you are comfortable playing with the project templates provided by ST, you can certainly adapt or adjust to your own style or the one an existing you're porting to the platform. Most of the interrupt vectors have a weakly linked routine in the startup_stm32f3xx.s file, so you can choose where and if you want to provide the body function for them.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..