cancel
Showing results for 
Search instead for 
Did you mean: 

Having issue in CAN recieve

Srikanth1
Senior

Hello experts,

I am using stm32f779I - Eval kit, I got stuck in receiving the CAN from other node wheres as transmit from my node is working fine. The IDE and packages am using as follows.

stm cube ide 1.13.1

stm cube mx 6.9.1

package: stm cube F7_v1.17.1

Below is the CAN initialization code I am using

/* CAN initialization */

CAN_FilterTypeDef sFilterConfig;

 

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

CanHandle.Instance = CAN1;

 

CanHandle.Init.TimeTriggeredMode = DISABLE;

CanHandle.Init.AutoBusOff = DISABLE; //

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 = 6;

 

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- Activate CAN RX notification #######################################*/

if (HAL_CAN_ActivateNotification(&CanHandle, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)

{

/* Notification Error */

Error_Handler();

}

 

/*##-5- Configure Transmission process #####################################*/

TxHeader.StdId = 0x321;

TxHeader.ExtId = 0x01;

TxHeader.RTR = CAN_RTR_DATA;

TxHeader.IDE = CAN_ID_STD;

TxHeader.DLC = 8;

TxHeader.TransmitGlobalTime = DISABLE;

 

When I tried to debug and check , I understood that am interrupt for once and

when am trying to read the data using below function in interrupt, it is returing with CAN module in reset state.

if(HAL_CAN_GetRxMessage(&hcan1,CAN_RX_FIFO0,&RxHeader,Rx_data) != HAL_OK)

* Interrupt function */

void CAN1_RX0_IRQHandler(void)

{

/* USER CODE BEGIN CAN1_RX0_IRQn 0 */

 

/* USER CODE END CAN1_RX0_IRQn 0 */

HAL_CAN_IRQHandler(&hcan1);

/* USER CODE BEGIN CAN1_RX0_IRQn 1 */

if(HAL_CAN_GetRxMessage(&hcan1,CAN_RX_FIFO0,&RxHeader,Rx_data) != HAL_OK)

{

Error_Handler();

}

if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)

{

/* Notification Error */

Error_Handler();

}

 

/* USER CODE END CAN1_RX0_IRQn 1 */

}

 Can some one please let me know the mistake am doing.

Thanks & Regards,

R. Srikanth, 

Post edited to remove personal information.

 

1 REPLY 1
FBL
ST Employee

Hello @Srikanth1 

First, I would recommend using Callbacks to control the process. You need to avoid heavy processing inside the interrupt handler

When using CAN_IT_RX_FIFOx_MSG_PENDING notifications, you trig callback HAL_CAN_RxFIFOxMsgPendingCallback(). you have two possible options here:

  • Either directly get the Rx message in the callback, using HAL_CAN_GetRxMessage().
  • Or deactivate the notification in the callback without getting the Rx message. The Rx message can then be got later using HAL_CAN_GetRxMessage(). Once the Rx message have been read, the notification can be activated again.

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.