Skip to main content
alxrey
Associate II
February 11, 2021
Solved

FDCAN callback function is not called

  • February 11, 2021
  • 1 reply
  • 3741 views

Hello,

I am trying to set up a FDCAN communication between two STM32G431KBT6.

The Tx part seems to work perfectly but I have a problem at the level of the reception.

General informations: I use CAN-FD as a standard CAN 2.0B (Extended Id) at a baud rate of 500 kbit/s. The two boards are exactly the same with the same base configuration.

Here is my Initialisation CAN function generated by STM32CubeMX:

/* FDCAN1 init function */
void MX_FDCAN1_Init(void)
{
 
 hfdcan1.Instance = FDCAN1;
 hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1;
 hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
 hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
 hfdcan1.Init.AutoRetransmission = DISABLE;
 hfdcan1.Init.TransmitPause = DISABLE;
 hfdcan1.Init.ProtocolException = DISABLE;
 hfdcan1.Init.NominalPrescaler = 2;
 hfdcan1.Init.NominalSyncJumpWidth = 1;
 hfdcan1.Init.NominalTimeSeg1 = 13;
 hfdcan1.Init.NominalTimeSeg2 = 2;
 hfdcan1.Init.DataPrescaler = 1;
 hfdcan1.Init.DataSyncJumpWidth = 1;
 hfdcan1.Init.DataTimeSeg1 = 1;
 hfdcan1.Init.DataTimeSeg2 = 1;
 hfdcan1.Init.StdFiltersNbr = 0;
 hfdcan1.Init.ExtFiltersNbr = 1;
 hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_QUEUE_OPERATION;
 if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
 {
 Error_Handler();
 }
 
}

which is exactly the same on the both MCU.

And here is my "home made" configuration function that I call after the Init function:

/**
 * @brief Configures the FDCAN.
 * @param None
 * @retval None
 */
void FDCAN_Config(void) {
 FDCAN_FilterTypeDef sFilterConfig;
 
 /* Configure Rx filter */
 sFilterConfig.IdType = FDCAN_EXTENDED_ID;
 sFilterConfig.FilterIndex = 0;
 sFilterConfig.FilterType = FDCAN_FILTER_MASK;
 sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
 sFilterConfig.FilterID1 = 0x10; // Filter bits
 sFilterConfig.FilterID2 = 0; // Mask bits
 if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
 {
 /* Filter configuration Error */
 Error_Handler();
 }
 
 /* Configure global filter:
 Filter all remote frames with STD and EXT ID
 Reject non matching frames with STD ID and EXT ID */
 if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK)
 {
 Error_Handler();
 }
 
 /* Start the FDCAN module */
 if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
 {
 /* Start Error */
 Error_Handler();
 }
 
 /* Enable interrupts */
 if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
 {
 /* Notification Error */
 Error_Handler();
 }
}

I set the mask (sFilterConfig.FilterID2) to 0 to accept all messages.

And finally here is my callback function:

/**
 * @brief Rx FIFO 0 callback.
 * @param hfdcan: pointer to an FDCAN_HandleTypeDef structure that contains
 * the configuration information for the specified FDCAN.
 * @param RxFifo0ITs: indicates which Rx FIFO 0 interrupts are signalled.
 * This parameter can be any combination of @arg FDCAN_Rx_Fifo0_Interrupts.
 * @retval None
 */
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{
 if((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET)
 {
 /* Retrieve Rx messages from RX FIFO0 */
 if (HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader, fdcan_RxData) != HAL_OK)
 {
 /* Reception Error */
 Error_Handler();
 }
 
 /* Toggle LED */
 if (RxHeader.IdType == FDCAN_EXTENDED_ID)
 {
 HAL_GPIO_TogglePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
 }
 
 if (HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
 {
 /* Notification Error */
 Error_Handler();
 }
 }
}

To begin I try to just toggle a led for each received frame (the Tx MCU send a frame once a second).

With a logic analyser I checked the Rx pin on the Rx MCU and the frames arrive well. Hence my feeling that the problem lies at the level of the callback function where the MCU never go. (I tested to place a breakpoint at the beginning of it).

Novice in the use of CAN on STM32, I based my work on this project.

Thank you in advance for the help you will be able to give me.

This topic has been closed for replies.
Best answer by alxrey

Fixed.

I forgot to Init the Interrupt:

/* FDCAN1 interrupt Init */
HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn);

in HAL_FDCAN_MspInit function.

Done by checking the box FDCAN1 interrupt 0 in the NVIC Settings pane of FDCAN1 Configuration window in STM32CubeMX.

1 reply

alxrey
alxreyAuthorBest answer
Associate II
February 16, 2021

Fixed.

I forgot to Init the Interrupt:

/* FDCAN1 interrupt Init */
HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn);

in HAL_FDCAN_MspInit function.

Done by checking the box FDCAN1 interrupt 0 in the NVIC Settings pane of FDCAN1 Configuration window in STM32CubeMX.