cancel
Showing results for 
Search instead for 
Did you mean: 

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

JLoli
Associate II

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

13 REPLIES 13
T J
Lead

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
Associate II

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

T J
Lead

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...

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
T J
Lead

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.

with the help of the transceiver also i am not getting data .

and no there are no examples for the STM32L452RE-P if you have any working code kindly attached

thank you

JLoli
Associate II

ok i will try thank you

JLoli
Associate II

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 */

}

hello Clive

here is my initialization code for the CAN can you let me know what is the problem exactly and ya i connected the tx pin of controller to tx pin of transceiver and rx pin to rx same for the other node but not getting data could you please help.

thank you

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 */

}