2022-06-28 11:27 AM
Hi, I just tried to create a simple Test for USART but it doesn't work.
I activated HSE, Serial Wire and UART2 for aynchronous 115200 baud. 8.1 via stmcube
This is basically the code I wrote into main loop:
...
char text[] = "Hallo Welt!\r\n\0";
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (HAL_UART_Transmit(&huart2, (uint8_t*)text, 32, 10) != HAL_OK) {
Error_Handler();
}
HAL_UART_Transmit(&huart2, (uint8_t*)"TEST ", 6, 10);
HAL_Delay(500);
}
...
it should now transmit "Hallo Welt" over and over again.. followed by a "TEST "..
But it just shows "Hallo Welt!" once and then.. nothing..
any idea why?
I append the generated main.c
2022-06-28 12:52 PM
Maybe it's result is not HAL_OK and goes into Error_Handler();?
I don't use Cube/HAL so don't know how that timeout works.
JW
2022-06-28 12:56 PM
You are transferring 32 bytes with the first HAL_UART_Transmit which produces a buffer overflow because char text[] is not large enough. Although reading beyond the end is mostly harmless, it will send unwanted chars to the receiving terminal which might screw up. Use strlen(text) for the length and don't append \0 to your text, this is already implicitly done for strings in C.
hth
KnarfB
2022-06-28 12:59 PM
CubeMX main.c files tend to have pretty superficial content. The MSP file would initialize the pins.
Suggest using strlen(), or sizeof()-1 for sending strings.
Try longer timeouts.
Instrument Error_Handler() and HardFault_Handler() routines to provide actionable information.
Check the pins with a scope
Not sure what chip, board or pins are involved.
Elucidate on context.
2022-06-28 11:02 PM
Did I really forget to mention the mcu I use? shame on me.. STM32F103C8T6 (bluepill)
Interesting point: When I run the program, it only prints the "Hallo Welt!" once.
using single step, running over the HAL_UART_Transmit it prints my text once but I never get back to gdb control. so it never leaves that routine.
When i step into HAL_UART_Transmit, and run it line by line it dies inside the Delay function. After removing it, and keeping that "step into" mode, I can repeatedly print my text.
So it's running in single step debug mode but doesn't in normal run mode..
2022-06-28 11:18 PM
I just tried another board and it worked. Seems it's a hardware issue..
Anyway, thanks for helping.