Wednesday, 8 February 2017

mbed RTOS Basics - Threads - Thomas Murphy

Introduction

RTOS stands for Real Time Operating System. An RTOS is intended to run multiple different applications all in real time i.e. inputs to the applications are being processed as they arrive instead of being buffered and processed later. An RTOS is perceived as being able to run all the applications on the system simultaneously (Multitasking) i.e. each operation runs in "real time" at the same time. In a single processor system it is only possible to run one of the applications at a time however it seems, to the user, that the system is running all applications at the same time. So how can applications appear to operate in real time but not have constant access to the processor?  The truth is very simple, they don't. Each application has access to the processor for a certain amount of time before it is taken off and another application is given access. The applications get such a small amount of time and the switching process is so fast that to the human eye, real time multitasking appears to be occurring. Typically the RTOS is in full control of which application is running and for how long.


RTOS Control and Threads

In an RTOS, the applications stated above are referred to as threads. Each thread has a task/ job that it has to carry out. The RTOS controls when a thread can run on the processor and for how long it can run. Threads use super-loops and would continuously run if given the opportunity. If the RTOS could not remove a thread after its allotted time, then the RTOS has failed. This failure is very obvious because all other processes would stop and in a system like an airplane, you can't have a thread which controls the radar stuck on the processor while the engines shut off and the plane drops out of the sky. This is a rather extreme example but the RTOS really is this important. The RTOS allows threads to run on the processor one after the other based on their priority. A high priority thread should always run before any lower priority thread. Each thread is allowed a certain amount of time on the processor before being taken off by the RTOS. Even if the thread has not completed a cycle it is still removed so the thread is essentially paused until it is put back on the processor where it has a chance to finish its operation. This control of threads is a key practice of the RTOS because if there was a bug in the thread where it may never finish its function and never return control, and the RTOS could not forcibly remove it then all other threads are essentially dead due to a fault in a different thread. The RTOS will remove a thread if it has to but a thread can also request to be taken off by essentially telling the RTOS that it has completed its task(s) and should be taken off the processor for a specified period of time.

mbed RTOS Example

The code below shows an example of an RTOS on mbed. The program has five threads, four for controlling four LEDs and one for reading a joystick and displaying a value on an LCD screen. Note: main() is also a thread! main() is the first thread and toggles the value applied to led1 each time it completes a cycle. When a thread completes a cycle it runs Thread::wait(specified time) where the thread requests to be taken off the processor and be put back on after the specified wait time. For main() the LED labelled led1 will toggle on/off every 500ms. Also in main() are the thread declares and calls. The next three threads are aimed to toggle three other LEDs at different rates, based on their Thread::wait(). The last thread reads a toggle switch and prints a count value on an LCD screen. If the joystick is pushed left, right, up, or down then the count is decremented. If it is clicked down in the center then the count increments. Holding the joystick in a certain position will update the count and the LCD screen every 100ms since the Thread::wait() function is set to 100ms. This makes this thread the highest priority since it is set to run the most often. This is a simple example of using threads but any function will likely be able to run in a thread. There are some precautions that must be taken when using multiple threads where many variable are being used. More details to come in another post. As for now the code below can be used to get familiar with threads but don't forget to include the right libraries. 


No comments:

Post a Comment