cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-144 CAN bus example code?

Dick Lin
Senior
Posted on May 30, 2018 at 21:08

Hi,

I am using STM32L496 with Nucleo-144/STM32CubeMX.

Is there any CAN BUS example with HAL Drivers? I can't even find how to setup the baudrate.

Thanks,

****

Note: this post was migrated and contained many threaded conversations, some content may be missing.
12 REPLIES 12
Posted on May 30, 2018 at 22:19

Try looking at this (L4x6 family RM0351)

STM32Cube_FW_L4_V1.11.0\Projects\STM32L476G-EVAL\Examples\CAN\CAN_Networking

Baud is derived from APB clock divided by Prescaler, and subdivided by sum of SJW+BS1+BS2 quanta

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 30, 2018 at 22:39

Got it. Thx

How to setup the baudrate? Probably in this MX_CAN1_Init(). I just haven't figured which code is doing the baudrate.

/* CAN1 init function */

void MX_CAN1_Init(void)

{

hcan1.Instance = CAN1;

hcan1.Init.Prescaler = 16;

hcan1.Init.Mode = CAN_MODE_NORMAL;

hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;

hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

hcan1.Init.TimeTriggeredMode = DISABLE;

hcan1.Init.AutoBusOff = DISABLE;

hcan1.Init.AutoWakeUp = DISABLE;

hcan1.Init.AutoRetransmission = DISABLE;

hcan1.Init.ReceiveFifoLocked = DISABLE;

hcan1.Init.TransmitFifoPriority = DISABLE;

if (HAL_CAN_Init(&hcan1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

Posted on May 30, 2018 at 23:27

I still don't see any useful info.

So how do I setup to 500Kbit/s?

Thx

Posted on May 30, 2018 at 23:38

High-school level math I hope..

1Mbit example

  CanHandle.Init.SJW = CAN_SJW_1TQ;

  CanHandle.Init.BS1 = CAN_BS1_4TQ;

  CanHandle.Init.BS2 = CAN_BS2_5TQ;

  CanHandle.Init.Prescaler = 8;

Bus clock 80 MHz

80,000,000 / 8 / (1+4+5)

80,000,000 / 8 / 10

80,000,000 / 80 = 1,000,000

For 500Kbit

  CanHandle.Init.Prescaler = 16;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 31, 2018 at 14:21

These lines impact the wire speed

hcan1.Init.Prescaler = 16;

hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;

hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

APBClock / (16 * 3)

APBClock /48

The specific bit rate on the CAN bus will depend on how fast you're clocking your process/buses, which you don't specify

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 31, 2018 at 17:39

From Cube, both APB1/2 are 47.333333. Let say 48 MHz.

hcan1.Init.Prescaler = 16;

hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;

hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

APBClock / (16 * 3)

APBClock /48

From your equation above will be

48 M/48 = 1 mbps.

In this case, if I want 500Kbps, What should I change? SJW, TimeSeg1 or 2?

Thanks,

Dick

On Thu, May 31, 2018 at 5:21 AM, Clive One <st-microelectronics@jiveon.com>

Posted on May 31, 2018 at 18:03

I think I've explained the math sufficiently, for the bit timings you might want to consult with some CAN documentation/specifications.

This should get you there, you should probably ponder/explain why your clock source isn't 48 MHz exactly, CAN has tight requirements.

  CanHandle.Init.SJW = CAN_SJW_1TQ;

  CanHandle.Init.BS1 = CAN_BS1_2TQ;

  CanHandle.Init.BS2 = CAN_BS2_3TQ;
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 31, 2018 at 18:25

The clock value came from SMT32CubeMX. I did nothing to change it.

On Thu, May 31, 2018 at 9:05 AM, Clive One <st-microelectronics@jiveon.com>

________________

Attachments :

image.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxQJ&d=%2Fa%2F0X0000000azm%2FHuBA1BgBHXrrgu9ACid57SqeBhWoRwvc469n8vIPLH4&asPdf=false
Posted on May 31, 2018 at 18:32

>>The clock value came from SMT32CubeMX. I did nothing to change it.

Then you should probably review the PLL settings, and pick something than blends better.

I would strongly recommend reading the documentation to understand how the clocks and the part work, having a robot create code, or copy-n-pasting it from other sources really doesn't constitute a clear understanding of the mechanics you are relying on. At some point knowledge of facts is important/critical.

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