STM32Cube UART Transmit Function Bug - Page 1
Forum section: electronics / general

author: eds
author_rank: Newbie
timestamp: March 06, 2024, 01:21:49 pm
content: Hello everyone,I am trying to learn the basics of STM32CubeIDE and Embedded C programming. I have a microcontroller that I am trying to program and see how it works step by step. For the first subject, I am trying to see something I am transmitting via UART to the PC. I am trying to transmit "Hello World!" It is just a two line code so I do not know where I am making a mistake. I believe it is a problem with the timing as I am receiving my message in full if I run the function in debug mode line by line.Code:while (1){/* USER CODE END WHILE */HAL_UART_Transmit(&huart1, startupMessage, 13, 10000);HAL_Delay(1000);/* USER CODE BEGIN 3 */}/* USER CODE END 3 */}I am initializing everything before the while loop and also I am sure my Baud Rate, Parity Bits etc. settings are right. This is the output I am getting.

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 06, 2024, 02:56:10 pm
content: Nothing obvious, post the rest of the code (Specially startupMessage init) or upload the project, never had any issue with this.Also, normally you don't transmit the null termination:Code:HAL_UART_Transmit(&huart1, startupMessage, 12, 10000);Try:Code:HAL_UART_Transmit(&huart1, "Hello World!\r\n", 14, 10000);

author: darkspr1te
author_rank: Frequent Contributor
timestamp: March 06, 2024, 05:54:06 pm
content: you need to check the status of the UART before sending again ,use something likewhile (HAL_uart_check_status(&uartname) ==1) {}this adds a delay that is based on the UART status register.i'am not sure of the ST HAL call for this as i moved away from HAL and use HAL_LL_ or use my own libsdarkspr1te

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 06, 2024, 09:20:51 pm
content: Not required, HAL_UART_Transmit doesthisbefore and after transmitting.But the OP should be checking if the function returns HAL_OK:Code:if(HAL_UART_Transmit(&huart1, startupMessage, 12, 10000) != HAL_OK){// Error}

author: dietert1
author_rank: Super Contributor
timestamp: March 06, 2024, 09:38:39 pm
content: The OP needs to put his code into user code begin .. - user code end .. sections. Otherwise his code that may have been working will disappear when using CubeMX to generate another version of the project. This is very important to learn as fast as possible.Regards, Dieter

author: darkspr1te
author_rank: Frequent Contributor
timestamp: March 07, 2024, 04:51:56 am
content: Quote from: dietert1 onYesterdayat 11:38:39 pmYup, learned that one the hard way too.darkspr1te

author: tellurium
author_rank: Frequent Contributor
timestamp: March 08, 2024, 04:09:45 am
content: If you want to learn how uC works, do not use Cube HAL or any other framework.Learn on bare metal. Seehttps://github.com/cpq/bare-metal-programming-guide

author: dietert1
author_rank: Super Contributor
timestamp: March 08, 2024, 06:50:15 am
content: Don't trust some strange github project.Use the "official" ST support including their free tools like CubeMX. A beginner should learn those tools as they speed up getting results.Regards, Dieter

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 08, 2024, 08:26:36 am
content: OP never came back, stop this no sense lol