Blog 2 - RTOS Using the PIC Microcontroller

1.0 Introduction 

In this next installment of the binbot blog, the process of RTOS will be examined. RTOS stands for:

Real Time Operating System

A real time operating system is a system in which multiple "tasks" (which will be represented by functions) are called and ran by a central piece of code, which is the operating system. In a real-time operating system, the operating system quickly cycles between tasks, breaking from one task to run the next.

 

Programs to be run in this experiment:

  1. Tasks
  2. Termination
  3. Enable/Disable
  4. Messages
  5. Yield
  6. Semaphores
  7. Await
  8. Statistics
  9. Basic Kernel

 

2.0 Programs

2.1 Tasks

Tasks is the first and simplest piece of code to be run by the PIC. In this program the PIC simply cycles between different tasks of varying different rates.
 
Figure 1: Output of "Tasks" in the terminal window
 
 
Figure 1 shows the output of the program displayed in a terminal window. It can be seen from this that tasks 3 and 4 are the highest priority tasks, as they occur the most frequently.
 
The code for this program was later modified in order to add in an infinite loop. The results of this can be seen in Figure 1, as the program continually prints out the warning for being inside of an infinite loop. Yet despite being inside of this loop the other tasks can still run. This is due to the "yield" function, which will be explored in the fifth piece of code.
 
Figure 2: Code for the infinite loop with the "rtos_yield()"
 
Figure 2 shows this code that puts and infinite loop in the first rtos task.

 2.2 Termination

In this second piece of code the "rtos_terminate( )" function will be examined. Calling this function will cause all of the rtos tasks to cease running.
 

Figure 3: Output of "termination" in the terminal


In Figure 3 the output of this terminate function can be seen. It can be seen also that the function runs again after RTOS has been terminated. How was this achieved?
 
In order for the RTOS function to restart it has to be wrapped in a loop. More than one RTOS run function cannot be placed in the main function. For this reason instead the piece of code that RTOS run is contained within is placed within an infinite while loop that ensures that the RTOS will continually run again after being terminated. The counter is also reset to 0 in the loop, so that the code always counts from 0 to 4, then terminates and resets.
 

Figure 4: The while loop containing the "rtos_run ( )" function

  

Figure 4 shows the code of this infinite while loop.

2.3 Enable/Disable

In this third program the functions of "rtos_enable( )" and "rtos_disable( )" functions were used in order to enable and disable certain tasks within the rtos program.
 
Figure 5: The "enable/disable" program running

 The results of this program are showed in figure five. It can be seen from this that the third task is periodically disabled, before being enabled again. Here the third task is represented through the printed out "3".
 

2.4 Messages

In this program, the functionality of "messages" will be examined. Through the use of messages, tasks can send information such as variables to other tasks. In this example the first rtos task sends its "count" variable to the second task. The second task can now print out this variable to the screen.
Figure 6: The "messages" program displaying the count
 
Figure 6 above shows the output of the program, where the count is printed out to the terminal.

2.5 Yield

In this program the "rtos_yield( )" function will be shown off.. The yield function allows for the user to break out of a task when it is called. In this case there is an infinite loop in the first task. In order to avoid the program getting perpetually stuck in the first task, the yield function is used allowed for the program to pause the first task and move on to running the second task.
 
Figure 7: The "yield" function showing the count printout of the second task.

Figure 7 above shows the output. In this case the second task prints out a count, showing the count value from the infinite loop in task one. Task one is continually incrementing using "count++".
 

2.7 Semaphores

A semaphore is a resource shared between tasks. In this case that is supposed to be the red LED on the PIC board. However the red LED did not seem to be operational, so only the LCD screen output was available, which displayed text stating whether the LED was on or off.
 
Both tasks one and two turn the LED on and off, before calling "rtos_signal ( )" in order to free up the resource for the next task to use.
 

2.8 Statistics

The statistics functions allows for statistics about the RTOS functionality to be recorded. In this experiment three different statistics will be recorded.
  • Task_Total_Ticks
  • Task_Min_Ticks
  • Task_Max_Ticks 
These statistics will then be printed out to the terminal.

Figure 10: The "statistics" program printing out total, min, and max ticks

 
The rtos stats are stored in a struct, from which they can be retrieved in order to print out. The terminal also shows whenever the second task has overrun, taking more time than it should to complete.

2.9 Basic Kernel

In this final program a very simple terminal will be created using rtos. This terminal will be able to take in a number of commands, which will affect the PIC board that it is connected to. These commands are:
  • Enable 1
  • Enable 2
  • Disable 1
  • Disable 2

These commands are intended to turn off and on LED lights that are on the PIC. However due to the LED lights not appearing to work, once again the LCD screen will need to observed in order to see the effects.

Two rtos tasks are acheieved to enable this to function, one which controls LED one, and one which controls LED two. 

Another function is used as the kernel, which runs in an infinite loop. The kernal is able to detect when defined commands are entered, and in turn enable or disable tasks one and two.

Figure 11: The terminal of the basic kernel
 

Figure 11 above shows the basic terminal, with several of the commands inputted into it. The command terminal shows the status of both of the LEDs after a command is inputted.
 


Comments

Popular posts from this blog

Blog 1 - Igor Kapusniak