FIGURE 10. RTOS structure overview.
systems like Windows and Linux for PCs and workstations,
and these are now making an impact in high-performance
microcontrollers like the PIC32. In fact, with the
computational power and memory of the PIC32, an RTOS is
needed to efficiently process lots of simultaneous complex
multi-task software operations at the same time. The best
way to visualize this is to appreciate the types of
applications that these advanced microcontrollers are
handling today. In this demo, you will be introduced to a
free open source RTOS for the PIC32 developed by Richard
Barry (see www.freertos.org).
FreeRTOS allows PIC32 applications to be organized as
a collection of independent tasks operating under control
of a real time scheduler (see Figure 10). The scheduler
decides which task should be executing by examining the
priority assigned to each task by the application designer.
Tasks can move from running to block or to ready or
suspended, depending on the priority and how you design
them. In this demo, we will cover task creation, scheduling,
removal, and reinstatement, as well as interrupt handling.
We will use FreeRTOS to perform our “Hello World”
function. It’s a modest start but highlights the powerful
capabilities of organizing your code around an RTOS. To
make things interesting, we will create the ability to
simultaneously handle independent LED blinking tasks —
each task assigned to blink an individual LED. This demo
requires use of the LED matrix and the hook-up diagram
mentioned earlier. As you push and release SW1, an
individual LED blinking task will be dynamically eliminated
until the max of eight tasks is reached. At this point, as you
continue to depress SW1; tasks that were previously
eliminated are restored in reverse order. The main code is
shown in Figure 11. The functions used are described in
the on-line documentation associated with FreeRTOS. Here’s
a quick overview:
• vSetupEnvironment() — Encapsulates the code to
set up the hardware (in this case, eight LEDs and a
• x TaskCreate( v Task1,”Task
1”,240,(void*)image,1,&x Task1Handle) — Creates
a “task 1” and passes it an image (digital port value
of the row/column for a specific LED in the matrix).
This task can be referenced later by its handle. The
task is written as an LED blink function.
• vSemaphoreCreateBinary (xBinarySemaphore) —
Creates a semaphore or binary flag/control that will
be used uniquely by the switch
interrupt service to invoke its task
• x TaskCreate(vHandler Task,
“Handler”, 240, NULL, 3,
NULL) — Creates a task to
handle the switch once an
interrupt occurs. The interrupt
invokes this task using the
above semaphore; the
handler adds and removes
tasks by their handles.
• v TaskStartScheduler() —
Starts the RTOS scheduler so
that the tasks created so far
FIGURE 11. RTOS ‘Hello World’ code.
It is interesting to watch the
LED activity as the FreeRTOS
scheduler spreads and de-spreads
the PIC32 processing power across
the blinking tasks. This demo is
included in the folder named RTOS
example. Open this folder and
navigate to examples, and then
open RTOSEXAMPLE.MCP. This
demo is also available for
download from the NV website.