cancel
Showing results for 
Search instead for 
Did you mean: 

UART RX interrupt

Dobrev
Associate II

Hello,

I'm trying trying to setup an interrupt receive on the U5 MCU using the STM32U5GJ-DK2 development kit. The USART1_IRQHandler does not get triggered at all. Here's my CubeMX config for the USART1:

Dobrev_0-1708518829438.png

Dobrev_1-1708518864347.png

I've sniffed the RX line, so I'm sure it's working as expected.

Here's my main() code:

 

 

 

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 clock */
  SystemClock_Config();

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

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_GPDMA1_Init();
  MX_ICACHE_Init();
  MX_DCACHE1_Init();
  MX_DCACHE2_Init();
  MX_CRC_Init();
  MX_LTDC_Init();
  MX_DMA2D_Init();
  MX_GPU2D_Init();
  MX_HSPI1_Init();
  MX_I2C2_Init();
  MX_JPEG_Init();
  MX_USART1_UART_Init();
  // MX_TouchGFX_Init();
  /* Call PreOsInit function */
  // MX_TouchGFX_PreOSInit();
  /* USER CODE BEGIN 2 */

  HAL_UARTEx_ReceiveToIdle_IT(&huart1, RxBuffer, RxBuffer_Size);
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);

  printf("OS kernel initialized\n");
  /* USER CODE END 2 */

  /* Init scheduler */
  // osKernelInitialize();

  /* Call init function for freertos objects (in freertos.c) */
  // MX_FREERTOS_Init();

  /* Start scheduler */
  // osKernelStart();

  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

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

 

 

 

and here's my redefined Callback as per the HAL's instructions:

 

 

 

int len = 0;
#define RxBuffer_Size 20
#define TxBuffer_Size 20

uint8_t RxBuffer[RxBuffer_Size];
uint8_t TxBuffer[TxBuffer_Size];
extern uint8_t ComCommand[RxBuffer_Size];
extern uint8_t polled;

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
  if (huart->Instance == USART1)
  {
    memset(ComCommand, 0, sizeof(ComCommand));
    len = strlen(RxBuffer);
    strncpy(TxBuffer, RxBuffer, len);
    strncpy(ComCommand, RxBuffer, len);
    polled = 0;
    // HAL_UART_Transmit(&huart1,TxBuffer,len,HAL_MAX_DELAY); //transmit the full sentence again
    memset(TxBuffer, 0, sizeof(TxBuffer));
    memset(RxBuffer, 0, sizeof(RxBuffer));
    printf("UART transfer complete\n");
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1, RxBuffer, RxBuffer_Size);
    __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
  }
}

 

 

 

 I've been using the same technique on the f7, where it was working flawlessly, but on the u5 the interrupt never gets triggered, so the callback never gets called. What am I missing?

13 REPLIES 13
Andrew Neil
Evangelist III

Before getting on to interrupts, does work for a simple polled read?

Dobrev
Associate II

Just tried it, yes it does work.

SofLit
ST Employee

Not related to your issue but too many code to execute in the callback (interrupt) including a printf(). Not recommended to do so ..

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
  if (huart->Instance == USART1)
  {
    memset(ComCommand, 0, sizeof(ComCommand));
    len = strlen(RxBuffer);
    strncpy(TxBuffer, RxBuffer, len);
    strncpy(ComCommand, RxBuffer, len);
    polled = 0;
    // HAL_UART_Transmit(&huart1,TxBuffer,len,HAL_MAX_DELAY); //transmit the full sentence again
    memset(TxBuffer, 0, sizeof(TxBuffer));
    memset(RxBuffer, 0, sizeof(RxBuffer));
    printf("UART transfer complete\n");
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1, RxBuffer, RxBuffer_Size);
    __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
  }

 

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

I realize that, most of this code is for debug purposes. However my problem here is that the RXNE interrupt, which I have explicitly enabled, does not get triggered.

And if you activate the UART interrupt with HAL_UART_Receive_IT() instead of HAL_UARTEx_ReceiveToIdle_IT()

and use HAL_UART_RxCpltCallback() instead of HAL_UARTEx_RxEventCallback(), what happens? 

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

Same behavior. Interrupt not triggered.

+1 avoid any blocking function in interrupts / callbacks.

The use of string functions is also a bit problematic. Yes, array fills with NUL's but needs to have a trailing NUL in maximal case.

Variables changing under interrupt context or callbacks should be marked as volatile if you're reliant on them elsewhere.

Random use of sizeof() vs strlen(). Please just make a string subroutine wrapper for HAL_UART_Transmit() you can use consistently. It will improve code readability significantly. Thanks for using Code Formatting tools </>

Try to present more complete code rather than key hole view. Cube hides a lot of the important code in the MSP layers, making main.c highly superficial. Posting complete examples in GitHub might help convey important detail and interrelationships. 

Mix of sprintf() and printf(), also remember that sprintf() implicitly returns the string length

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SofLit
ST Employee

What is the value of RxBuffer_Size? Does your UART is receiving the same amount of data?

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

RXBuffer is 20. I've tried with 10 and a whole bunch of different values. I've also measured the data with a logic analyzer to ensure consistency. The interrupt RXNE, which should get triggered when the FIFO is not empty, simply doesn't get triggered.