Skip to main content
Visitor II
June 28, 2022
Question

STMCube simple HAL_UART_Transmit seems to die

  • June 28, 2022
  • 5 replies
  • 1346 views

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

    This topic has been closed for replies.

    5 replies

    Super User
    June 28, 2022

    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​

    Super User
    June 28, 2022

    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

    Graduate II
    June 28, 2022

    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.

    TecDroiDAuthor
    Visitor II
    June 29, 2022

    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..

    TecDroiDAuthor
    Visitor II
    June 29, 2022

    I just tried another board and it worked. Seems it's a hardware issue..

    Anyway, thanks for helping.