Skip to main content
PatTheTrickster
Associate II
June 22, 2023
Question

CANBUS Between two STM controllers

  • June 22, 2023
  • 19 replies
  • 13699 views

Hello I am trying to attempt a CANBUS communication between two different STM boards, I've been following the videos on the STM website as a reference and I got the LOOPBACK Mode to work with one STM board but I am having trouble getting the NORMAL mode to work with two Stm boards.

Video I am referencing:  https://youtu.be/KHNRftBa1Vc

My Master board is a Nucleo-L4R5ZI-P

Slave board is a Nucleo-L4A6ZG

Attached is an image of my current setup on trying to get the CAN transmit working

I kept the code mostly the same as the video, the problem I have right now is when I run it the video doesn't clarify if I should run the slave first then the master or run them in debug mode, doing either method I am getting all sorts of errors or just nothing happening, any clarification on what I can do to fix this would be massive help, Thank you!

 

This topic has been closed for replies.

19 replies

Karl Yamashita
Principal
June 22, 2023

"I am getting all sorts of errors or just nothing happening" is pretty vague. Be more specific.

Your picture doesn't show what are connected to the USB so don't know if you have one or two computers? My first thought is there is no ground reference between either CAN transceivers but goes back to what are connected to the USB connectors?

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
PatTheTrickster
Associate II
June 22, 2023

Yes Sorry, The two USBS are connected to one computer, The kind of errors I get is if I run the master first then the slave I will get a
'Error in final Launch sequence
Failed to start GDB server'

if I run the slave first then the master I get a failure to bind to port 61234 on the command line

when I try running them in debug mode they will throw an error that the target is not responding,retrying and it will constantly just loop that till I get a problem occured message saying
'Error in final launch sequence:
Failed to execute MI command:

target remote localhost:61234

KnarfB
Super User
June 22, 2023

Haven't watched the video. What you describe looks more like a debugger issue, not CAN related.

If you use both ST-LINKs on the same computer, use two instances of STZM32CubeIDE. Connect only one board first, and in the Debugger Setup window enable ST-LINK serial number (S/N) and scan for it. Then, in the other instance, do the same for the other board. This should avoid using the same ST-LINK twice.

hth

KnarfB

Tesla DeLorean
Guru
June 22, 2023

Not sure I've got the patience to watch a 20 min controllers tech video narrated by a robot.

Keil has a simple bus method that might be applicable, bit simpler than the transceivers, hopefully you have those wired up correctly.

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

I would honestly try to avoid single step debugging this type of stuff, and rather use the serial port afforded by the NUCLEO boards to provide information and instrumentation as to what's going on.

You need a device that's reliably listening and responding/acknowledging packets when not using the loop-back mode. Make one the listener.

Most STM32 have TWO CAN, it might be simpler to debug that configuration where CAN1 and CAN2 communicate and you have the simple diode bus illustrated by Keil

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
KnarfB
Super User
June 23, 2023

> narrated by a robot.

At least no strange human accent...

mƎALLEm
ST Technical Moderator
June 23, 2023

Hello,

You didn't provide your CAN bitrate config. Watching the video, I noticed that prescaler = 18, BS1=2 and BS2=2 which are not suitable values. You have to decrease CAN clock prescaler as much as possible and increase BS1 and BS2 to fit your bitrate where BS1 > BS2.

You have also to use an external crystal and not HSI as source clock for CAN communication.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
PatTheTrickster
Associate II
June 23, 2023

So my CAN bitrate config is this
On The Nucleo-L4R5ZI-P on the clock cofiguration I changed the parameters similar to the video (HSE and PLLCLK) but the HCLK I used was 120 MHz as that was the max
On the Nucleo L4A6ZG The HCLCK was set to 80 MHZ

For the L4R5ZI-P I had my Prescalar be 48 and Time quanta for 1 & 2 be 2 Times which resulted in Time Quantum being 400.0ns and the Baud rate being 500000 bit/s

For the L4A6ZG The Prescalar I had was 20 and the Time Quanta for 1 & 2 was 2 Times and 1 Times respectively, This resulted in a Time quantum of 500.0 NS and 500000 bit/s, Let me know if there's a config I should be using but I will take your advice and start playing with those values.

PatTheTrickster
Associate II
June 23, 2023

So looking at the debugger mode I've discovered that the error for both boards happens when it Initializes the RCC Oscillators, don't know if that gives any insight to anyone?

mƎALLEm
ST Technical Moderator
June 27, 2023

Hi,

I told you to change the CAN prescaler value as well as BS1 and BS2 values as they are not safe? did you do that?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
mƎALLEm
ST Technical Moderator
June 27, 2023

Hi again,

Just noticed that the video you're watching doesn't seem to use the same boards as you! Moreover, there are some "bubull**bleep**":

- Nucleo board doesn't have a Crystal mounted on it while he selected Crystal / HSE as system source clock.

- The CAN parameters are not safe: prescaler, BSA, and BS2.

I don't know how this worked for him!!

According to Nucleo-L4A6ZG and Nucleo-L4R5ZI-P schematics, there is no crystal external mounted on the boards. The only solution you have is to use the clock generated by STLink (MCO).

So, according to the schematic, you have to solder SB109 as well as SB148 to connect MCO output from STLink to OSC-IN of the STM32L4xx.

Also you have to configure HSE in bypass mode.

I've attached CubeMx project for STM32L4A6ZG: clock at 80Mhz, CAN bitrate at 500kb/s with reasonable parameters:

SofLit_0-1687858668297.png

You can do the same thing for STM32L4R5ZI.

Hope it helps you.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
PatTheTrickster
Associate II
June 27, 2023

Yeah so I did eventually figure out that my board did not have a HSE option being valid so I've been playing around with HSI as the clock rate, Regarding the CubeMX project that you have just attached is this the configuration for soldering the SB109 as well as the SB148 to the MCO? If so I'll try and see if that works but I think I will have to purchase the components for that. Right now with the changes I've made to clock and CAN rate I don't go into any error handlers but I have been unable to receive a message on either micro controller. Will Let you know if I get it to work with your suggestions!