1 Something more about multi-threading
One of most important feature of ChibiOS is multi-threading. Oversimplifying, a thread could be imagined like a sequence of instructions (with associated a priority and a working area) and multi-threading means kernel can manage more than a thread independently, executing them in a parallel fashion even if there is a single core.
Achieving this requires Kernel must plan operation sequence: this task is called scheduling. We could act indirectly on this operation though priority levels. Priority follows a simple rule:
among all the threads ready for execution, the one with the highest priority is the one being executed, no exceptions to this rule.
There is a nice article on chibios.org about how to create threads.Threads must always have a chThdSleep() or some suspending function inside their loop. In this way they will yield the CPU ownership and the Kernel will perform a context switch resuming the thread in ready state having the highest priority. Typically a thread is resumed on a certain event, performs its operations and than is suspended.
The best way to maximize usefulness of multi-threading is to divide our application in more independent parts and make them cooperate to achieve our purpose. Every part will represent one of ours threads.
Consider a scenario in which a LED changes its state on button pressed: we can consider this like a kind of action and reaction. So we have two task: checking the button state and changing the LED state. We can assign these tasks to two different threads, in this way code related to every task is independent from other tasks and if a task changes we don’t have to edit the whole software.
In this tutorial we are going to use the green LED and the User Button of our STM32 Nucleo-64 F401RE to explain how to use multi-threading and to introduce the PAL driver. In the following step, we will modify the LED related task without editing the one button related.
2 STM32 General Purpose I/Os
The simplest way to interact with a MCU is sampling voltages from its pins or force a signal on them. Every MCU manages its pins in different way and typically pins or pads (inspired by contact pads) are grouped by ports. continue reading…