cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 CAN Bus example using HAL Library

David Pekin
Senior
Posted on December 20, 2017 at 18:49

Hello,

I'm trying to bring up the CAN bus on the STM23F303RE nucleo board and am having some difficulty determining which way to go.  I've been unable to find a HAL library example for this platform.  Can anyone point me to one?  I have found a HAL example for the STM32F4 but that target has different peripherals.  

Also, I see three different possible design approaches for this project.  It's a fairly simple control loop that we're developing.  The 3 options seem to be:

   1. Code system using HAL library.

   2. Use STMCube for development (but why is there no CAN example for STMCube and the STM32F3xx?)

   3. I see the ARM-Mbed program but am not familiar with the benefits or roadblocks of using that RTOS.  (I don't think we need an RTOS)

Thanks for any suggestions here.

- Dave

#can-bus-stm32f303
17 REPLIES 17
Posted on December 20, 2017 at 19:13

None of the NUCLEO boards ship with CAN transceivers, dig into the HAL examples for other boards (ie EVAL series), and port to your target and pin selections.

STM32Cube\Repository\STM32Cube_FW_F3_V1.9.0\Projects\STM32303C_EVAL\Examples\CAN\CAN_Networking\readme.txt

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 20, 2017 at 19:30

I’m looking doing just that from the STM32F4 code. Do you know if the addresses and ports are the same between the F3 and F4? We will be building our own trancievers. Do you know if I’m going STM board to STM board if I need the trancievers?

Thanks,

- Dave

________________

Attachments :

image001.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyEX&d=%2Fa%2F0X0000000b4u%2FlNb1bbGShAZykI2eARa6Ww.QHbPckMesRspkqYcgeAI&asPdf=false
Posted on December 20, 2017 at 19:52

The CAN implementation between F2, F3 and F4 should be very similar, APB bus speed being the main thing impacting choice of settings.

With board-to-board you'd need at minimum a diode arrangement as CAN RX needs to see TX data to confirm signal has been placed on bus, and you'd need a responding device to acknowledge receptions.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 21, 2017 at 01:06

Thanks Clive. 

I'm getting closer.  I've brought the HAL_CAN routines into the program and changed the CAN TX/RX pins from GPIOD 0/1 to GPIOA 11/12.  I've seen a signal on PA 12 when I try to transmit a CAN message so its trying to do something. I only have a scope on the pin outs and don't have a transceiver or 2nd CAN device yet. Also, I don't really understand what the AF pins are used for in the CAN protocol. I left them at GPIO_AF9_CAN.

I do notice a problem in the HAL_CAN_Transmit (..., Timeout) function.  The following line overwrites the Timeout parameter that is passed in: 

hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21U) | \

hcan->pTxMsg->RTR);

Before that line is executed the Timeout parameter is 10 (the value passed in) but after that line executes, it is hammered and has a value of 0x20000008.  The value of  

transmitmailbox is zero so we're not exceeding the array bounds (3).  Maybe the problem is that there isn't a real CAN bus yet.?.

I hope adding the transceivers will get things talking. Thanks for any further comments/advice.

Posted on December 21, 2017 at 02:47

The AF mux maps a specific peripheral to a specific pin. See part Data Sheet to see the available pin mappings.

Page 4 here shows the diode connectivity

http://www.keil.com/appnotes/files/apnt_236.pdf

 

You need a CAN device to sink the data sent otherwise things will timeout because no devices responded. ie just wiring the pins to a non-responsive device won't work.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
David Pekin
Senior
Posted on December 22, 2017 at 00:40

I've got the networking sample code running (sort of) on two stm32f303re Nucleo boards. I'm waiting on transceivers which should arrive tomorrow.  I was wondering about the sample code however.  Since both boards are loaded with the same code, won't they both be at the same CAN address?  I think they're both set to StdId of 0x321 in the CAN_config function.  Is that the CAN ID or is it set on the hardware somehow?

Thanks,

Posted on December 22, 2017 at 01:22

It is not an address, it is a message ID

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead
Posted on December 22, 2017 at 02:36

To set the address, I set my address by dip switch.

I used a slot of 16 ID's for each node.

All over I have 48 nodes all using 16 addresses, I use the 'ID' range of 0x400-0x7ff.

as you can see there is no server address.

The primary sensors are the lower 32 slaves, and the IO boards occupy the top 16

any node can transmit strings on channel 0, receive commands on channel 1 etc.

I send 16 analog values on channel 2. A node can send commands to an IO board on channel 4.

there are many ways to implement an addressing scheme, this one works well for me.

Posted on December 22, 2017 at 02:16

How do you set the CAN address on the Nucleo boards?

Sent from my iPhone