FreeRTOS Lecture 38- Exercise: Testing UART prints

 

Exercise: Testing UART prints

 

 

Serial communication, particularly UART (Universal Asynchronous Receiver/Transmitter), is a fundamental aspect of embedded systems development. Testing UART communication is crucial to ensure the accuracy and reliability of data transfer in STM32 microcontroller-based projects. In this comprehensive guide, we will take you through the process of testing UART prints on STM32 microcontrollers using Tera Term serial monitor software.

 

Creating the Buffer:

To get started, let’s create a buffer named usr_msg(Figure 1) in the global space. This buffer will serve as our test bed for transmitting messages through UART.

char usr_msg[250];
Figure 1. Creation of buffer.
Figure 1. Creation of buffer.

 

Next, we’ll populate the usr_msg buffer with a sample message using the sprintf function.  Let’s do that after the prvSetupHardware function.

The prvSetupHardware is the function that does the UART initialization. Before UART initialization, you can’t print anything. Let’s populate usr-msg with some message by using sprintf, as shown in Figure 2.

sprintf(usr_msg,"This is hello-world application starting\n");
Figure 2. Populating usr-msg with some message.
Figure 2. Populating usr-msg with some message.

 

After that, let’s use the printmsg function and just pass usr_msg buffer as a parameter, as shown in Figure 3. The sprintf copies the given message into the usr_msg buffer, and then we just print that message via printmsg function.

printmsg(usr_msg);
Figure 3. Call for printmsg function.
Figure 3. Call for printmsg function.

 

Testing the Project:

Now, let’s move on to testing our UART communication project.

Let’s compile the project (Figure 4). The project builds successfully without any errors.

Figure 4. Building the project.
Figure 4. Building the project.

 

Now open your serial monitor software. If you are using Windows, then you can use tera term software (Figure 5). If you don’t have this software, just download and install it on your computer.

Tera term is a serial monitoring software, which is freely available, and you can download and install it if you haven’t done it already.

Let’s open the tera term software. Then establish the serial connection, i.e., our ST-Microelectronics, and click ok (Figure 6).

Figure 5. Tera term software.
Figure 5. Tera term software.

 

Figure 6. Establishing serial connection.
Figure 6. Establishing serial connection.

 

After that, configure the serial port (Figure 7). Select the baud rate of 115200 because the firmware running on our board uses the same baud rate range. All the settings in Figure 7 should be the same with respect to the code that we have written. Now let’s click ok (Figure 7).

Figure 7. Configuring serial port.
Figure 7. Configuring serial port.

 

Download the code into the board (Figure 8). Tick the reset after the program (Figure 9) and click ok.

Figure 8. Downloading the code.
Figure 8. Downloading the code.

 

Figure 9. Ticking reset after program checkbox.
Figure 9. Ticking reset after program checkbox.

 

Now the board is programmed, and we got the output on the serial monitor software (Figure 10). And you can see that the cursor is at the end of the output. That’s because you can do some settings here.

 

Go to the setup terminal and select receive CR+LF and click ok (Figure 11). Now just reset the board.

Troubleshooting:

  • Baud Rate Mismatch: Ensure that the baud rate in your code matches the one configured in Tera Term.
  • Newline Character: If messages appear on a new line but don’t return to the original position, add a carriage return (\r) in your sprintf statement.
  • Incomplete Messages: If messages are truncated or incomplete, check the buffer size and make sure it can accommodate your messages.

In Figure 12, you can see that it is not working. It actually needs a carriage return. The message has come to the newline since you are sending a newline character at the end of the message (Figure 8). But it did not return to the original position. That’s why you have to send the carriage return, i.e., \r, as shown in Figure 13.

Figure 10. Output on the serial monitor software.
Figure 10. Output on the serial monitor software.

 

Figure 11. Configuring serial port.
Figure 11. Configuring serial port.

 

Figure 12. Output on the serial monitor software.
Figure 12. Output on the serial monitor software.

 

Figure 13. Adding carriage return in sprintf statement.
Figure 13. Adding carriage return in sprintf statement.

 

Try to build the project once again—the program compiled successfully. Now let’s test the project by downloading it into the chip. Reset your board once again. Now you can see that the result will be fine (Figure 14).

Figure 14. Output on the serial monitor software.
Figure 14. Output on the serial monitor software.

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.