cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 - CAN HAL driver transmit error

Anand A
Associate III
Posted on September 06, 2017 at 10:39

Hello,

I am using STM32L476G evaluation board, I have generated CAN HAL driver using default parameters which is available in Cube MX. using this when i try to transmit a message i am getting transmit error(TERR0 = 1).  Please refer the code below.

Can some one tell me why I am getting this error.

static void MX_CAN1_Init(void)

{

  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 16;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SJW = 0;

  hcan1.Init.BS1 = 0;

  hcan1.Init.BS2 = 0;

  hcan1.Init.TTCM = DISABLE;

  hcan1.Init.ABOM = DISABLE;

  hcan1.Init.AWUM = DISABLE;

  hcan1.Init.NART = DISABLE;

  hcan1.Init.RFLM = DISABLE;

  hcan1.Init.TXFP = DISABLE;

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

int main(void)

{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_CAN1_Init();

    hcan1.pTxMsg->Data[0] = 0x11;

    hcan1.pTxMsg->Data[1] = 0x12;

    hcan1.pTxMsg->Data[2] = 0x13;

    hcan1.pTxMsg->Data[3] = 0x14;

    hcan1.pTxMsg->Data[4] = 0x15;

    hcan1.pTxMsg->Data[5] = 0x16;

    hcan1.pTxMsg->Data[6] = 0x17;

    hcan1.pTxMsg->Data[7] = 0x18;

    

    hcan1.pTxMsg->DLC = 8;

    hcan1.pTxMsg->StdId = 0x71F;

    hcan1.pTxMsg->ExtId = 0;

    hcan1.pTxMsg->IDE = CAN_ID_STD;

    hcan1.pTxMsg->RTR = CAN_RTR_DATA;

  

    HAL_CAN_Transmit(&hcan1, 50);

}

Thanks

Anand

4 REPLIES 4
Imen.D
ST Employee
Posted on September 06, 2017 at 17:03

Hello

A.Anandan

,

There are working CAN example under STM32CubeL4 with

STM32L476G_EVAL:

STM32Cube_FW_L4_V1.8.0\Projects\STM32L476G_EVAL\Examples\CAN\CAN_Networking

You can review this example and see howto configure the CAN to tramsmita message in normal mode.

Best Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Anand A
Associate III
Posted on September 08, 2017 at 13:47

Hello Imen,

Thanks for the suggestion, I got the sample code and now I am able to transmit at 1mbps.

I am having transmit error with 125000 bps. I have configured the register values in the same way how it was configured for 1mbps.

The register values which i used for 125kbps are given below, I am using CAN1 and the APB1 clock is 54MHz.

BRP = 24, TS1 = 11, TS2 = 4

Using these values nominal bit time is 0.0000079 i.e. ()

So the baudrate is 1/0.0000079 = 125046bps.

But I am getting stuff error,

Can you please tell me what is wrong  here.

Regards

Anand A

Posted on September 08, 2017 at 14:08

Hello Anand,

For CAN it is important not only to set proper baudrate, but also to set proper asmple point.

For purpose of translating MCU's clock and CAN baudrate into registers' values I often use this website:

https://community.st.com/external-link.jspa?url=http%3A%2F%2Fwww.bittiming.can-wiki.info%2F

Once you fill in the CAN controller name (ST BxCAN), clock rate (54MHz in your case) and sample point (87.5% by default), yu will get a table with popular CAN baudrates and registers configuration related to these baudrates. For 125kbit/s this is a result:

0690X0000060871QAA.png

Please try these values (the one highlighted in yellow is a preferred one) and hopefully you will not get stuff error.

Regards

Szymon
Posted on September 08, 2017 at 17:03

Hello Szymon,

Thanks for the information that I need to consider sample point,

I have tried the values, which are mentioned by you. But still I am getting error.

BTR = 27, TS1 = 12, TS2 = 1.

0690X00000608ArQAI.png

Anything else i need to consider here.

Regards

Anand A