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

If you set RXBuffer = 20, do you send at least 20 bytes from the sender?

Did you try RXBuffer = 1?

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

Yes, I do. I send 20 and all kinds of data, just to check if the interrupt gets triggered. It doesn't.

Saket_Om
ST Employee

Hello @Dobrev 

Is the UART global interrupt enabled? If not try to add the line bellow in HAL_UART_MspInit.

 

HAL_NVIC_EnableIRQ(UART4_IRQn);

 

Best regards

Omar

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar
Karl Yamashita
Lead II

You're starting off using HAL_UARTEx_ReceiveToIdle_IT before main while loop, then start using HAL_UARTEx_ReceiveToIdle_DMA in the callback. Not sure why you're mixing it up?

 

Not sure the size of your buffer and the amount of data you're expecting but more than likely you're going to get a half complete callback so you maybe saving only half of your expected string. Another thing is that you're doing too much copying in the interrupt. You should just set a flag to indicate 

 

 

You haven't shown a screen shot of the DMA settings. If you didn't assign a DMA channel then you're not going to get an interrupt.

If you find my answers useful, click the accept button so that way others can see the solution.