cancel
Showing results for 
Search instead for 
Did you mean: 

UART communication works only in debug mode

CHech.1
Associate III

Hi,

I hope this is the right topic to ask this question.

I'm new to embedded, and I'm experimenting with stm32l496ag discovery and sending/receiving uart form the connected pc using interrupts.

I configured USART2 in CubeMX, enabling "global interrupt" in NVIC Settings tab, and setting the baud rate to 9600.

I'm using GPIO "PA2" and "PD6".

My problem is that while in debugging, HAL_UART_RxCpltCallback is called and acts as expected, sending back the "Test123" message seen below.

On the other hand, when running without debugging I get no message back.

I tried setting a LED on when receiving, which as well works in debug but not outside of it.

What could cause this? I'm at a loss.

Any help would be appreciated!

These are the relevant code snippets (to my limited knowledge, I'll happily supply more):

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CRC_Init();
  MX_I2C2_Init();
  MX_DMA2D_Init();
  MX_QUADSPI_Init();
  MX_FMC_Init();
  MX_UART4_Init();
  MX_USART2_UART_Init();
  MX_TouchGFX_Init();
  /* USER CODE BEGIN 2 */
 HAL_UART_Receive_IT(&huart2, (uint8_t*) rx_buff , 10);
 HAL_UART_Transmit_IT(&huart2, (uint8_t*) tx_buff , 10);
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CRC_Init();
  MX_I2C2_Init();
  MX_DMA2D_Init();
  MX_QUADSPI_Init();
  MX_FMC_Init();
  MX_UART4_Init();
  MX_USART2_UART_Init();
  MX_TouchGFX_Init();
  /* USER CODE BEGIN 2 */
 HAL_UART_Receive_IT(&huart2, (uint8_t*) rx_buff , 10);
 HAL_UART_Transmit_IT(&huart2, (uint8_t*) tx_buff , 10);

/* USER CODE BEGIN 4 */
 
 
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	__HAL_UART_CLEAR_OREFLAG(&huart2);
	HAL_UART_Transmit(&huart2, (uint8_t *) "TEST123", sizeof("TEST123"), 1000);
	HAL_UART_Receive_IT(&huart2, (uint8_t*) rx_buff , 10);
}

4 REPLIES 4
CHech.1
Associate III

Also, i forgot to ask - is using

__HAL_UART_CLEAR_OREFLAG(&huart2);

the right function to use? if i don't use it, than the second receive doesn't work

TDK
Guru

If ORE is getting set, you are receiving characters without reading them out, probably because the mcu is stuck in a blocking HAL_UART_Transmit call.

Monitor return values of HAL functions to verify they worked. Generally, only call blocking functions outside of interrupts. Don't mix HAL_UART_Transmit_IT and HAL_UART_Transmit.

That it works okay in Debug doesn't mean there aren't bugs in the program. Optimization can change the outcome of a race condition, or change the effect of using the stack outside of its scope, or change the value in uninitialized variables.

There are many examples of UART communication in the CubeMX examples relevant for your undisclosed chip.

If you feel a post has answered your question, please click "Accept as Solution".
Bob S
Principal

Several issues:

  1. in your init code at "user code begin 2" you start transmitting before there is anything in the buffer (well, probably all NULL bytes if tx_buff is a global variable).
  2. in your RxCpltCallback() you are calling the blocking transmit function. That will not return until the buffer has been sent (at 9600 baud this is about 8ms). The callback function is called from the UART interrupt - you really don't want to call a function that takes a long time to return from an interrupt. If you really want to start transmitting something, use the _IT() variant.
  3. What do you think the value of sizeof("123TEST") will be? And what do you think is the last byte that will be sent?
  4. Why is the overrun flag getting set? Maybe fixing the above issues will fix this as well.
CHech.1
Associate III

Hi,

Thank you for the answers!

Your questions helped me "search the right questions", and now it works, and works as intended.

Besides the problem you listed, I also had an incompatible baud rate with the uart port.