FreeRTOS Lecture 39 – Exercise: Testing our hello world application over UART

  • Post author:
  • Post category:Blog

 

Exercise: Testing our hello world application over UART

 

 

Now let’s call the printmsg function from the task handlers, as shown in Figure 1 and Figure 2.

printmsg("Hello-world: From Task-1\n");
Figure 1. Call for printmsg function from task handler 1.
Figure 1. Call for printmsg function from task handler 1.

 

printmsg("Hello-world: From Task-2\n");
Figure 2. Call for printmsg function from task handler 2.
Figure 2. Call for printmsg function from task handler 2.

 

Now let’s see how these prints are going to display. For that, first, compile the project (Figure 3). It builds successfully without any errors.

Remember that now we are printing via UART. Download the code into the board (Figure 4). Tick the reset after the program (Figure 5) and click ok.

Testing our hello world application over UART
Figure 3. Building the project.

 

Testing our hello world application over UART
Figure 4. Downloading the code.

 

Testing our hello world application over UART
Figure 5. Ticking reset after program checkbox.

 

In Figure 6, you can see the output of this project. We actually forgot to give \r and \n. That’s why the result is printing continuously on the terminal of the serial monitor software.

Testing our hello world application over UART
Figure 6. Output on Serial monitor software.

 

Let’s give \r and \n, as shown in Figure 7.

Testing our hello world application over UART
Figure 7. Adding \r and \n in printmsg.

 

Now in Figure 8, you can observe the output that we cannot be able to visualize.

Testing our hello world application over UART
Figure 8. Output on Serial monitor software.

 

Now let’s analyze the output. Let’s stop this program execution.

Go to the file and click disconnect (Figure 9). Now it is disconnected, and let’s analyze the output. This output (Figure 8) looks like gibberish. The output is completely messed up, and this is happening because of context switching between the tasks.

Figure 9. Disconnecting the tera term.
Figure 9. Disconnecting the tera term.

 

For example, when task 1 was scheduled to run on the processor. It is supposed to print the complete line “Hello-world: From Task-1”.

Remember that when it is printing those characters over UART, since task 2 also has got equal priority, task 2 actually preempted task 1 in the middle of the transmission itself. But the task 1 is not yet completed sending all the characters via UART. Now task 2 has already taken over the CPU, and it tries to send its message, i.e., “Hello-world: From Task-2”. That’s the reason you don’t see any proper order in this output. Both the tasks are racing around the UART peripheral to dump their characters.

This is an example of non-synchronized tasks racing around a shared entity, i.e., the UART peripheral. That means they are continuously corrupting the data register of the UART. Task 1 is preempted by task 2 in the middle, and task 2 is again preempted by task1. They are actually corrupting the data register of the UART, and there is no proper order. That’s why the output is very messed up.

Now how to make this non-synchronized task execution synchronized. In RTOS, there are various methods as follows:

  • Usage of semaphores.
  • Mutexes
  • Disabling interrupts and re-enabling the interrupts.
  • Using a simple flag variable.
  • Also, there are various techniques called task notifications, queues, etc.

All the above things you can use for synchronization purposes.

 

Fastbit Embedded Brain Academy Courses

https://fastbitlab.com/course1

 

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.

Leave a Reply