2018-05-30 12:08 PM
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.2018-05-30 01:19 PM
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
2018-05-30 03:39 PM
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__); }}
2018-05-30 04:27 PM
I still don't see any useful info.
So how do I setup to 500Kbit/s?
Thx
2018-05-30 04:38 PM
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;
2018-05-31 07:21 AM
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
2018-05-31 10:39 AM
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>
2018-05-31 11:03 AM
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;2018-05-31 11:25 AM
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=false2018-05-31 11:32 AM
>>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.