Skip to main content
Visitor II
May 21, 2021
Question

[STM32F411] I want UART waiting for RXNE interrupt,but it can't stop to keep entering UART_IRQ_Handler and alway readed the last data even I'm not trans any data to MCU, Where am I doing wrong?

  • May 21, 2021
  • 1 reply
  • 749 views

UART_HandleTypeDef huart2;

void MX_USART2_UART_Init(void){

 huart2.Instance = USART2;

 huart2.Init.BaudRate = 9600;

 huart2.Init.WordLength = UART_WORDLENGTH_8B;

 huart2.Init.StopBits = UART_STOPBITS_1;

 huart2.Init.Parity = UART_PARITY_NONE;

 huart2.Init.Mode = UART_MODE_TX_RX;

 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart2.Init.OverSampling = UART_OVERSAMPLING_16;

 if (HAL_UART_Init(&huart2) != HAL_OK)

 {

  Error_Handler();

 }

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

}

int main(void){

 HAL_Init();

 MX_USART2_UART_Init();

 while (1)

 { }

}

void USART2_IRQHandler(void){

static uint8_t temp;

HAL_UART_IRQHandler(&huart2);

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

}

    This topic has been closed for replies.

    1 reply

    Super User
    May 21, 2021

    If you want to use HAL, you'll have to use its way of doing things. In this case, that means using the HAL_UART_Receive_IT function to start the transfer.

    See example here:

    https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Projects/STM32F411E-Discovery/Examples/UART/UART_TwoBoards_ComIT/Src/main.c