Skip to main content
JLoli
Associate II
October 28, 2018
Question

i am using STM32L452RE-P in that CAN protocol is not working in NORMAL mode while in LOOPBACK mode it is working.

  • October 28, 2018
  • 3 replies
  • 2085 views

with the help of the HAL code library i am able to see the output while using LOOPBACK mode in CAN but at the same time not able to get the data in the NORMAL mode . can any one help me out with this problem ?

thanks in advanced

This topic has been closed for replies.

3 replies

T J
Senior III
October 28, 2018

You must have a terminator if you are using the line driver.

if you are not using a line driver you may need to pull up the line.

and you need a slave on the wire to respond.

also ABOM should be enabled,

JLoli
JLoliAuthor
Associate II
October 28, 2018

thanks for your answer but firstly i want to check the protocol with two stm boards only that mean i want to do loopback by shorting the two board tx and rx pins in that one function name HAL_CAN_GetRxFifoFillLevel i am stucking it is not coming out with that function .

and ya already enable the ABOM as you said .so guide me what i have to do now ?

thanks you

Tesla DeLorean
Guru
October 28, 2018

You'd need one of the nodes to receive/acknowledge the packets.

Aren't there some Network examples amongst the Cube trees for assorted chips/boards

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
T J
Senior III
October 28, 2018

you can't just join Rx to Tx

You have to join both the Rx pins together and use a pull up like 1k cause you are being dodgy..

Each of the Tx pins require a diode to pull down the joined Rx pins.

That is dodgy... but it works...

JLoli
JLoliAuthor
Associate II
October 29, 2018

ok i will try thank you

T J
Senior III
October 28, 2018

it is not trivial to get this stuff running, do you have a scope ?

I purchased a CanDo unit from the UK,

I set it to transmit 1 packet every second, I worked on the receiver first.

It took a week to get 250khz running. Then I worked on the transmitter, it was done in a day.

JLoli
JLoliAuthor
Associate II
October 29, 2018

sorry but not getting what you are trying to say .

and ya here is my code for the board please check and let me know

i named a function CAN_Polling() for the CAN transmit and receive .

HAL_StatusTypeDef CAN_Polling(void)

{

 CAN_FilterTypeDef sFilterConfig;

  

 /*##-1- Configure the CAN peripheral #######################################*/

 CanHandle.Instance = CAN1;

   

 CanHandle.Init.TimeTriggeredMode = DISABLE;

 CanHandle.Init.AutoBusOff = ENABLE;

 CanHandle.Init.AutoWakeUp = DISABLE;

 CanHandle.Init.AutoRetransmission = ENABLE;

 CanHandle.Init.ReceiveFifoLocked = DISABLE;

 CanHandle.Init.TransmitFifoPriority = DISABLE;

 CanHandle.Init.Mode = CAN_MODE_NORMAL;

 CanHandle.Init.SyncJumpWidth = CAN_SJW_1TQ;

 CanHandle.Init.TimeSeg1 = CAN_BS1_6TQ;

 CanHandle.Init.TimeSeg2 = CAN_BS2_2TQ;

 CanHandle.Init.Prescaler = 5;

  

 if(HAL_CAN_Init(&CanHandle) != HAL_OK)

 {

  /* Initialization Error */

  Error_Handler();

 }

 /*##-2- Configure the CAN Filter ###########################################*/

 sFilterConfig.FilterBank = 0;

 sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

 sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

 sFilterConfig.FilterIdHigh = 0x0000;

 sFilterConfig.FilterIdLow = 0x0000;

 sFilterConfig.FilterMaskIdHigh = 0x0000;

 sFilterConfig.FilterMaskIdLow = 0x0000;

 sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;

 sFilterConfig.FilterActivation = ENABLE;

 sFilterConfig.SlaveStartFilterBank = 14;

  

 if(HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)

 {

  /* Filter configuration Error */

  Error_Handler();

 }

 /*##-3- Start the CAN peripheral ###########################################*/

 if (HAL_CAN_Start(&CanHandle) != HAL_OK)

 {

  /* Start Error */

  Error_Handler();

 }

 /*##-4- Start the Transmission process #####################################*/

 TxHeader.StdId = 0x11;

 TxHeader.RTR = CAN_RTR_DATA;

 TxHeader.IDE = CAN_ID_STD;

 TxHeader.DLC = 2;

 TxHeader.TransmitGlobalTime = DISABLE;

// TxData[8]=(uint8_t *)"afflaffl";

 TxData[0] = '1';

 TxData[1] = '2';

  

 /* Request transmission */

 if(HAL_CAN_AddTxMessage(&CanHandle, &TxHeader, TxData, &TxMailbox) != HAL_OK)

 {

  /* Transmission request Error */

  Error_Handler();

 }

  

 /* Wait transmission complete */

 while(HAL_CAN_GetTxMailboxesFreeLevel(&CanHandle) != 3) {}

 /*##-5- Start the Reception process ########################################*/

 if(HAL_CAN_GetRxFifoFillLevel(&CanHandle, CAN_RX_FIFO0) != 1)

 {

  /* Reception Missing */

  Error_Handler();

 }

 if(HAL_CAN_GetRxMessage(&CanHandle, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)

 {

  /* Reception Error */

  Error_Handler();

 }

 if((RxHeader.StdId != 0x11)           ||

   (RxHeader.RTR != CAN_RTR_DATA)        ||

   (RxHeader.IDE != CAN_ID_STD)         ||

   (RxHeader.DLC != 2)  )            

 //  ((RxData[0]<<8 | RxData[1]) != 0x))

 {

  /* Rx message Error */

  return HAL_ERROR;

 }

 return HAL_OK; /* Test Passed */

}