Skip to main content
Dick Lin
Senior
May 30, 2018
Question

Nucleo-144 CAN bus example code?

  • May 30, 2018
  • 12 replies
  • 3007 views
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.
    This topic has been closed for replies.

    12 replies

    Tesla DeLorean
    Guru
    May 30, 2018
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Dick Lin
    Dick LinAuthor
    Senior
    May 30, 2018
    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__);

    }

    }

    Tesla DeLorean
    Guru
    May 31, 2018
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..