2021-08-24 05:55 AM
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);
}
2021-08-24 06:33 AM
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
2021-08-24 11:07 AM
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.
2021-08-24 11:10 AM
Several issues:
2021-08-25 10:31 PM
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.