Skip to main content
Associate II
October 30, 2024
Solved

how to enable uart interrupt

  • October 30, 2024
  • 3 replies
  • 3489 views

Hello,
I want to activate UART1 interrupt directly through code in STM32CubeIDE, without using the .ioc file. UART1 is already active, so I just need to add the necessary lines to enable the interrupt.

In the stm32wbxx_it.c file, I defined the interrupt handler as follows:

void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */

/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */

/* USER CODE END USART1_IRQn 1 */
}

In the stm32wbxx_it.h library, I declared the prototype as void USART1_IRQHandler(void);. However, I am still not able to receive data using the HAL_UART_Receive_IT function.

Best answer by Andrew Neil

Please see the posting tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 

Use HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()

There should be examples in CubeIDE ?

3 replies

Andrew Neil
Andrew NeilBest answer
Super User
October 30, 2024

Please see the posting tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 

Use HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()

There should be examples in CubeIDE ?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
NdemirAuthor
Associate II
October 30, 2024

Thank you for your information. I will consider when I post.

I add this in "stm32wbxx_it.h".

void USART1_IRQHandler(void);


I add this in "stm32wbxx_it.c".

void USART1_IRQHandler(void)
{
 /* USER CODE BEGIN USART1_IRQn 0 */

 /* USER CODE END USART1_IRQn 0 */
 HAL_UART_IRQHandler(&huart1);
 /* USER CODE BEGIN USART1_IRQn 1 */

 /* USER CODE END USART1_IRQn 1 */
}

Where should I use HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions. I compare my code with the other examples and I didn't find any difference.

Andrew Neil
Super User
October 30, 2024

@Ndemir wrote:

Where should I use HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions.


Wherever it makes sense to enable the interrupt - somewhere before you call the HAL_UART_Receive_IT() function

 


@Ndemir wrote:

I compare my code with the other examples and I didn't find any difference.


Were they examples of using interrupts?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
NdemirAuthor
Associate II
October 30, 2024

Thank you Andrew,

It works.
Best Regards,