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");
printmsg("Hello-world: From Task-2\n");
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.
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.
Let’s give \r and \n, as shown in Figure 7.
Now in Figure 8, you can observe the output that we cannot be able to visualize.
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.
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