Skip to main content
Visitor II
May 2, 2025
Solved

FDCAN Tx callback not triggered on STM32U5

  • May 2, 2025
  • 2 replies
  • 883 views

I’ve written code that performs a CAN Tx transmission when an interrupt occurs while in STOP2 mode.
The transmission request via HAL_FDCAN_AddMessageToTxFifoQ() succeeds,
but the HAL_FDCAN_TxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TxEventFifoITs) callback is never triggered.
The GPIO interrupt callback (HAL_GPIO_EXTI_Rising_Callback) works as expected,
and I can see "HAL_CAN_AddTxMessage start" being printed, which means the transmission request was made.
However, it seems like the transmission is not actually completed.
Why is the Tx event not being triggered?

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fdcan.h"
#include "icache.h"
#include "memorymap.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
__IO 	uint32_t PushButtonState = PB_NOT_PRESSED;
FDCAN_RxHeaderTypeDef RxHeader;
uint8_t RxData[8]={0,1,0,0,0,0,0,0};
FDCAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[8]={0,0,0,0,0,0,0,0};
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
/* USER CODE BEGIN PFP */
void FDCAN_Config(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int fd, char *ptr, int len)
{
	HAL_UART_Transmit(&huart1, (unsigned char*)ptr, len, HAL_MAX_DELAY);
	return len;
}
/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the System Power */
 SystemPower_Config();

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_ICACHE_Init();
 MX_FDCAN1_Init();
 MX_USART1_UART_Init();
 /* USER CODE BEGIN 2 */
 FDCAN_Config();
 uint8_t buffer[256];
 sprintf((char *)buffer, "START!\r\n");
 HAL_UART_Transmit(&huart1, buffer, strlen((char *)buffer), 100);
 HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_TX_EVT_FIFO_NEW_DATA, 0);

 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {	 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SBF);
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);
 HAL_Delay(5000);
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0);
 PushButtonState = PB_NOT_PRESSED;

 uint8_t buffer[256];
 sprintf((char *)buffer, "Enter STOP2 MODE\r\n");
 HAL_UART_Transmit(&huart1, buffer, strlen((char *)buffer), 100);

 __HAL_RCC_PWR_CLK_ENABLE ();
 HAL_PWREx_EnterSTOP2Mode(PWR_SLEEPENTRY_WFI);
 SystemClock_Config();

 MX_FDCAN1_Init();
 FDCAN_Config();

 HAL_NVIC_ClearPendingIRQ(EXTI13_IRQn);
 HAL_NVIC_EnableIRQ(EXTI13_IRQn);
 while (PushButtonState == PB_NOT_PRESSED);
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE3) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
 RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
 RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
 RCC_OscInitStruct.PLL.PLLM = 1;
 RCC_OscInitStruct.PLL.PLLN = 32;
 RCC_OscInitStruct.PLL.PLLP = 2;
 RCC_OscInitStruct.PLL.PLLQ = 32;
 RCC_OscInitStruct.PLL.PLLR = 2;
 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
 RCC_OscInitStruct.PLL.PLLFRACN = 0;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
 |RCC_CLOCKTYPE_PCLK3;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
}

/**
 * @brief Power Configuration
 * @retval None
 */
static void SystemPower_Config(void)
{

 /*
 * Switch to SMPS regulator instead of LDO
 */
 if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
 {
 Error_Handler();
 }
/* USER CODE BEGIN PWR */
/* USER CODE END PWR */
}

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{ //SystemClock_Config();
 /* Prevent unused argument(s) compilation warning */
	if(GPIO_Pin == BTN_Pin)
		{
		PushButtonState = PB_PRESSED;
		printf("▶▶\r\n");
		if (HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData) != HAL_OK)
		{
			printf("HAL_CAN_AddTxMessage error\r\n");
			HAL_Delay(100);
		}
		else
		{
			printf("HAL_CAN_AddTxMessage start\r\n");
			HAL_Delay(100);
		}


		}
}

void HAL_FDCAN_TxEventFifoCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t TxEventFifoITs)
{
	printf("HAL_FDCAN_TxEventFifoCallback\r\n");
	 if (TxEventFifoITs & FDCAN_IT_TX_EVT_FIFO_NEW_DATA)
	 {
	 printf("HAL_CAN_AddTxMessage completed\r\n");
	 }
}

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, RxData) != HAL_OK)
 {
 Error_Handler();
 }
	 for(int i=0; i<TxHeader.DataLength; i++)
	 {
		 printf("RxData[%d] = %d\r\n", i, RxData[i]);
		 HAL_Delay(100);
	 }
 }
 }

void FDCAN_Config(void)
{
 FDCAN_FilterTypeDef sFilterConfig;
 hfdcan1.Init.AutoRetransmission = ENABLE;

 HAL_FDCAN_ConfigInterruptLines(&hfdcan1, FDCAN_IT_TX_EVT_FIFO_NEW_DATA, FDCAN_INTERRUPT_LINE0);
 HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_TX_EVT_FIFO_NEW_DATA, 0);


 /* Configure Rx filter */
 sFilterConfig.IdType = FDCAN_STANDARD_ID;
 sFilterConfig.FilterIndex = 0;
 sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
 sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
 sFilterConfig.FilterID1 = 0x321;
 sFilterConfig.FilterID2 = 0x7FF;
 if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
 {
 Error_Handler();
 }

 /* Start the FDCAN module */
 if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
 {
 Error_Handler();
 }

 if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
 {
 Error_Handler();
 }

 /* Prepare Tx Header */
 TxHeader.Identifier = 0x321;
 TxHeader.IdType = FDCAN_STANDARD_ID;
 TxHeader.TxFrameType = FDCAN_DATA_FRAME;
 TxHeader.DataLength = FDCAN_DLC_BYTES_2; //보낼 바이트 값 설정
 TxHeader.ErrorStateIndicator = FDCAN_ESI_PASSIVE;
 TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
 TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
 TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;
 TxHeader.MessageMarker = 0;

 HAL_FDCAN_Start(&hfdcan1);
}
/* USER CODE END 4 */

/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state */
 __disable_irq();
 while (1)
 {
 }
 /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * file: pointer to the source file name
 * line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
    This topic has been closed for replies.
    Best answer by Karl Yamashita

     

    How are you getting HAL_FDCAN_RxFifo0Callback to work when you didn't enable it?

    HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_TX_EVT_FIFO_NEW_DATA, 0);

     

    I haven't used Event Type callback but instead use empty callback.

    I can queue a bunch of Tx messages and the callback will finish sending all of the messages in the queue. 

    HAL_FDCAN_ActivateNotification(msg->fdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE | FDCAN_IT_TX_FIFO_EMPTY, 0);
    
    
    void HAL_FDCAN_TxFifoEmptyCallback(FDCAN_HandleTypeDef *hfdcan)
    {
    	if(hfdcan == &hfdcan1)
    	{
    		FDCAN_Tx_Send(&fdcan1_msg); // send anymore pending messages in queue
    	}
    }

     

    2 replies

    Technical Moderator
    May 6, 2025

    Hello,

    Did you enable the NVIC FDCAN?

    Also you didn't share your MX_FDCAN1_Init()!

    Please in next time provide the exact part number of the STM32 MCU.

    dlagyalsAuthor
    Visitor II
    May 7, 2025
    /* USER CODE BEGIN Header */
    /**
     ******************************************************************************
     * @file fdcan.c
     * @brief This file provides code for the configuration
     * of the FDCAN instances.
     ******************************************************************************
     * @attention
     *
     * Copyright (c) 2025 STMicroelectronics.
     * All rights reserved.
     *
     * This software is licensed under terms that can be found in the LICENSE file
     * in the root directory of this software component.
     * If no LICENSE file comes with this software, it is provided AS-IS.
     *
     ******************************************************************************
     */
    /* USER CODE END Header */
    /* Includes ------------------------------------------------------------------*/
    #include "fdcan.h"
    
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    FDCAN_HandleTypeDef hfdcan1;
    
    /* FDCAN1 init function */
    void MX_FDCAN1_Init(void)
    {
    
     /* USER CODE BEGIN FDCAN1_Init 0 */
    
     /* USER CODE END FDCAN1_Init 0 */
    
     /* USER CODE BEGIN FDCAN1_Init 1 */
    
     /* USER CODE END FDCAN1_Init 1 */
     hfdcan1.Instance = FDCAN1;
     hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1;
     hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
     hfdcan1.Init.Mode = FDCAN_MODE_INTERNAL_LOOPBACK;
     hfdcan1.Init.AutoRetransmission = DISABLE;
     hfdcan1.Init.TransmitPause = DISABLE;
     hfdcan1.Init.ProtocolException = DISABLE;
     hfdcan1.Init.NominalPrescaler = 16;
     hfdcan1.Init.NominalSyncJumpWidth = 1;
     hfdcan1.Init.NominalTimeSeg1 = 7;
     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 = 0;
     hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
     if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
     {
     Error_Handler();
     }
     /* USER CODE BEGIN FDCAN1_Init 2 */
    
     /* USER CODE END FDCAN1_Init 2 */
    
    }
    
    void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle)
    {
    
     GPIO_InitTypeDef GPIO_InitStruct = {0};
     RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
     if(fdcanHandle->Instance==FDCAN1)
     {
     /* USER CODE BEGIN FDCAN1_MspInit 0 */
    
     /* USER CODE END FDCAN1_MspInit 0 */
    
     /** Initializes the peripherals clock
     */
     PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN1;
     PeriphClkInit.Fdcan1ClockSelection = RCC_FDCAN1CLKSOURCE_PLL1;
     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
     {
     Error_Handler();
     }
    
     /* FDCAN1 clock enable */
     __HAL_RCC_FDCAN1_CLK_ENABLE();
    
     __HAL_RCC_GPIOA_CLK_ENABLE();
     /**FDCAN1 GPIO Configuration
     PA11 ------> FDCAN1_RX
     PA12 ------> FDCAN1_TX
     */
     GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
     /* FDCAN1 interrupt Init */
     HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn);
     /* USER CODE BEGIN FDCAN1_MspInit 1 */
    
     /* USER CODE END FDCAN1_MspInit 1 */
     }
    }
    
    void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle)
    {
    
     if(fdcanHandle->Instance==FDCAN1)
     {
     /* USER CODE BEGIN FDCAN1_MspDeInit 0 */
    
     /* USER CODE END FDCAN1_MspDeInit 0 */
     /* Peripheral clock disable */
     __HAL_RCC_FDCAN1_CLK_DISABLE();
    
     /**FDCAN1 GPIO Configuration
     PA11 ------> FDCAN1_RX
     PA12 ------> FDCAN1_TX
     */
     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
    
     /* FDCAN1 interrupt Deinit */
     HAL_NVIC_DisableIRQ(FDCAN1_IT0_IRQn);
     /* USER CODE BEGIN FDCAN1_MspDeInit 1 */
    
     /* USER CODE END FDCAN1_MspDeInit 1 */
     }
    }
    
    /* USER CODE BEGIN 1 */
    
    /* USER CODE END 1 */

    This is my FDCAN code

    Please in next time provide the exact part number of the STM32 MCU ->STMU545REQ

     

    Graduate II
    May 15, 2025

     

    How are you getting HAL_FDCAN_RxFifo0Callback to work when you didn't enable it?

    HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_TX_EVT_FIFO_NEW_DATA, 0);

     

    I haven't used Event Type callback but instead use empty callback.

    I can queue a bunch of Tx messages and the callback will finish sending all of the messages in the queue. 

    HAL_FDCAN_ActivateNotification(msg->fdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE | FDCAN_IT_TX_FIFO_EMPTY, 0);
    
    
    void HAL_FDCAN_TxFifoEmptyCallback(FDCAN_HandleTypeDef *hfdcan)
    {
    	if(hfdcan == &hfdcan1)
    	{
    		FDCAN_Tx_Send(&fdcan1_msg); // send anymore pending messages in queue
    	}
    }

     

    dlagyalsAuthor
    Visitor II
    May 14, 2025

    I am currently working on a code for the U545REQ board where it exits STOP2 mode due to an interrupt, sends CAN_Tx, and then re-enters STOP2 mode. I have also configured an Rx callback function that exits STOP2 mode upon receiving an Rx interrupt. However, this code is not working. As far as I know, when exiting STOP2 mode, the system clock automatically switches to 4 MHz, so I set the CAN to operate at 4 MHz as well, but it's still not working. What could be the reason for this?

    Technical Moderator
    May 14, 2025

    Please don't duplicate the same question over several threads.

    This will be merged.

    dlagyalsAuthor
    Visitor II
    May 15, 2025

    none of the answers have actually solved the problem.