SlideShare a Scribd company logo
1 of 42
Download to read offline
FreeRTOS
A Brief Overview
Christopher Kenna
Avionics
October 1, 2010
1 / 34
FreeRTOS
N
Introduction
Outline
1 Introduction
About FreeRTOS
Kernel Overview
2 Tasks
Tasks versus Co-Routines
Task Details
3 IPC and Synchronization
Queues
Semaphores and Mutexes
4 Scheduler
Scheduler Background
Scheduler Code
2 / 34
FreeRTOS
N
Introduction – About FreeRTOS
Background Information
The FreeRTOS Project supports 25 official architecture ports, with many
more community developed ports.
The FreeRTOS RT kernel is portable, open source, royalty free, and very
small.
OpenRTOS is a commercialized version by the sister company High
Integrity Systems.
Richard Barry: I know FreeRTOS has been used in some rockets and other
aircraft, but nothing too commercial.
We have customers that use it on ship systems, and WITTENSTEIN sell
SafeRTOS which has been certified to various standards, but not
DO-178B.
3 / 34
FreeRTOS
N
Introduction – About FreeRTOS
Licensing Information (0/2)
The license is a modified GNU GPL according to the table below.
FreeRTOS
Modified GPL
OpenRTOS
Commercial
Free? ! #
Use it in a commercial application? ! !
Royalty free? ! !
Must open source code that makes
use of FreeRTOS services?
#1
#
Must open source kernel changes? ! #
1As long as code provides functionality that is distinct from that provided by FreeRTOS.
4 / 34
FreeRTOS
N
Introduction – About FreeRTOS
Licensing Information (1/2)
The license is a modified GNU GPL according to the table below.
FreeRTOS
Modified GPL
OpenRTOS
Commercial
Must document that the product uses
FreeRTOS?
!2
#
Required to offer FreeRTOS code to
application users?
! #
Technical support for OS? #3
!
Warranty? # !
2Web link to FreeRTOS.org is sufficient.
3There is an on-line community, though.
5 / 34
FreeRTOS
N
Introduction – Kernel Overview
FreeRTOS Configuration
The operation of FreeRTOS is governed by FreeRTOS.h, with application
specific configuration appearing in FreeRTOSConfg.h.
Obviously, these are static configuration options.
Some examples:
configUSE_PREEMPTION
configCPU_CLOCK_HZ – CPU clock frequency, not necessarily the
bus frequency.
configTICK_RATE_HZ – RTOS tick frequency that dictates interrupt
frequency.
configMAX_PRIORITIES – Total number of priority levels. Each level
creates a new list, so memory sensitive machines should keep this to
a minimum.
And more. . .
6 / 34
FreeRTOS
N
Tasks
Outline
1 Introduction
About FreeRTOS
Kernel Overview
2 Tasks
Tasks versus Co-Routines
Task Details
3 IPC and Synchronization
Queues
Semaphores and Mutexes
4 Scheduler
Scheduler Background
Scheduler Code
7 / 34
FreeRTOS
N
Tasks – Tasks versus Co-Routines
Tasks in FreeRTOS
Tasks have their own context. No dependency on other tasks unless
defined.
One task executes at a time.
Tasks have no knowledge of scheduler activity. The scheduler handles
context switching.
Thus, tasks each have their own stack upon which execution context can be
saved.
Prioritized and preemptable.
8 / 34
FreeRTOS
N
Tasks – Tasks versus Co-Routines
Co-Routines
Co-routines are intended for use on small processors that have severe
RAM constraints. Co-Routines share a single stack.
Co-routines are implemented through macros.
Prioritized relative to other co-routines, but preempted by tasks.
The structure of co-routines is rigid due to the unconventional
implementation.
Lack of stack requires special consideration.
Restrictions on where API calls can be made.
Cannot communicate with tasks.
Will examine tasks in more detail, but not co-routines.
9 / 34
FreeRTOS
N
Tasks – Task Details
Task States
Running – Actively executing and
using the processor.
Ready – Able to execute, but not
because a task of equal or higher
priority is in the Running state.
Blocked – Waiting for a temporal
or external event. E.g., queue and
semaphore events, or calling
vTaskDelay() to block until delay
period has expired. Always have a
β€œtimeout” period, after which the
task is unblocked.
Suspended – Only enter via
vTaskSuspend(), depart via
xTaskResume() API calls.
10 / 34
FreeRTOS
N
Tasks – Task Details
Task Priorities
Each task gets a priority from 0 to configMAX_PRIORITIES βˆ’ 1
Priority can be set on a per application basis.
Tasks can change their own priority, as well as the priority of other tasks.
tskIDLE_PRIORITY = 0
11 / 34
FreeRTOS
N
Tasks – Task Details
The Idle Task
The idle task is created automatically when the scheduler is started.
12 / 34
FreeRTOS
N
Tasks – Task Details
The Idle Task
The idle task is created automatically when the scheduler is started.
It frees memory allocated by the RTOS to tasks that have since been
deleted.
Thus, applications that use vTaskDelete() to remove tasks should ensure
the idle task is not starved.
12 / 34
FreeRTOS
N
Tasks – Task Details
The Idle Task
The idle task is created automatically when the scheduler is started.
It frees memory allocated by the RTOS to tasks that have since been
deleted.
Thus, applications that use vTaskDelete() to remove tasks should ensure
the idle task is not starved.
The idle task has no other function, so cases when the idle task need never
run exist.
12 / 34
FreeRTOS
N
Tasks – Task Details
The Idle Task
The idle task is created automatically when the scheduler is started.
It frees memory allocated by the RTOS to tasks that have since been
deleted.
Thus, applications that use vTaskDelete() to remove tasks should ensure
the idle task is not starved.
The idle task has no other function, so cases when the idle task need never
run exist.
There is an idle task hook, which can do some work at each idle interval
without the RAM usage overhead associated with running a task at the
idle priority.
12 / 34
FreeRTOS
N
Tasks – Task Details
Task Control Block (TCB) (0/2)
A TCB is allocated to each task. It stores the task’s context.
Source/tasks.c
typedef struct tskTaskControlBlock
{
: : Top of task ’ s stack . Must be f i r s t member b/c of context switch
volatile portSTACK_TYPE * pxTopOfStack ; : : code ( l a t e r s l i d e s ) .
#if ( portUSING_MPU_WRAPPERS == 1 )
: : The MPU s e t t i n g s are defined as part of the port l a y e r .
xMPU_SETTINGS xMPUSettings ; : : Must be 2nd member of s t r u c t .
#endif
: : L i s t item used to place the TCB in ready and blocked queues .
xListItem xGenericListItem ;
: : Used to place the TCB in event l i s t s .
xListItem xEventListItem ;
: : The p r i o r i t y of the task where 0 i s the lowest p r i o r i t y .
unsigned portBASE_TYPE uxPriority;
: : Points to the s t a r t of the stack .
portSTACK_TYPE *pxStack;
: : D e s c r i p t i v e name given to the task when created .
: : F a c i l i t a t e s debugging only .
signed char pcTaskName[ configMAX_TASK_NAME_LEN ];
13 / 34
FreeRTOS
N
Tasks – Task Details
Task Control Block (TCB) (1/2)
Source/tasks.c
#if ( portSTACK_GROWTH > 0 )
: : Used f o r stack overflow checking on a r c h i t e c t u r e s where the
: : stack grows up from low memory .
portSTACK_TYPE * pxEndOfStack ;
#endif
...
#if ( configUSE_TRACE_FACILITY == 1 )
: : Used only f o r t r a c i n g the scheduler , making debugging e a s i e r .
unsigned portBASE_TYPE uxTCBNumber ;
#endif
#if ( configUSE_MUTEXES == 1 )
: : The p r i o r i t y l a s t assigned to the task , used by the p r i o r i t y
: : i n h e r i t a n c e mechanism .
unsigned portBASE_TYPE uxBasePriority ;
#endif
...
#if ( configGENERATE_RUN_TIME_STATS == 1 )
: : Used f o r c a l c u l a t i n g how much CPU time each task i s u t i l i z i n g .
unsigned long ulRunTimeCounter ;
#endif
} tskTCB;
14 / 34
FreeRTOS
N
Tasks – Task Details
Implementing a Task
Source/include/projdefs.h
/* Defines the prototype to which task functions must conform. */
typedef void (* pdTASK_CODE )( void * );
void vATaskFunction ( void * pvParameters ){
for( ;; ){
: : Task a p p l i c a t i o n code here .
}
}
Tasks are always a function that returns void and takes a void pointer.
Tasks should never return (loop forever).
Not much to it, really.
15 / 34
FreeRTOS
N
Tasks – Task Details
Creating a Task (0/3)
The kernel creates a task by instantiating and populating a TCB. New
tasks are placed in the Ready state and added to the Ready list.
If the task is the highest priority task, then it is set as the currently
running task.
Created by calling xTaskCreate() and deleted by calling
vTaskDelete().
xTaskCreate() takes the following parameters.
A pointer to the function that implements the task (type pdTASK_CODE from
earlier).
A name for the task.
The depth of the task’s stack.
The task’s priority.
A pointer to any parameters needed by the task’s function.
16 / 34
FreeRTOS
N
Tasks – Task Details
Creating a Task (1/3)
An interesting step in task creation is preparing the task for its first
context switch.
The TCB stack is initialized to look as if the task was already running, but
had been interrupted by the scheduler. The return address is set to the
start of the task function with pxPortInitialiseStack.
See code on next slide.
17 / 34
FreeRTOS
N
Tasks – Task Details
Creating a Task (2/3)
Source/portable/GCC/ATMega323.c (edited)
portSTACK_TYPE * pxPortInitialiseStack ( portSTACK_TYPE *pxTopOfStack ,
pdTASK_CODE pxCode , void * pvParameters )
{
: : Place some known values on bottom of stack , f o r debugging .
* pxTopOfStack = 0x11;
pxTopOfStack --;
* pxTopOfStack = 0x22;
pxTopOfStack --;
...
: : Start of task code w i l l be popped o f f l a s t , so push i t on f i r s t .
unsigned short usAddress = ( unsigned short ) pxCode;
* pxTopOfStack =
( portSTACK_TYPE ) (usAddress & (unsigned short) 0x00ff );
pxTopOfStack --;
usAddress >>= 8;
* pxTopOfStack =
( portSTACK_TYPE ) (usAddress & (unsigned short) 0x00ff );
pxTopOfStack --;
: : And then push values f o r CPU r e g i s t e r s , taking i n t o account
: : what the compiler expects f o r t h i s a r c h i t e c t u r e .
...
18 / 34
FreeRTOS
N
IPC and Synchronization
Outline
1 Introduction
About FreeRTOS
Kernel Overview
2 Tasks
Tasks versus Co-Routines
Task Details
3 IPC and Synchronization
Queues
Semaphores and Mutexes
4 Scheduler
Scheduler Background
Scheduler Code
19 / 34
FreeRTOS
N
IPC and Synchronization – Queues
Queue Overview
Queues are the primary form of inter-task communications.
They can send messages between tasks as well as between interrupts and
tasks.
Supports appending data to the back of a queue, or sending data to the
head of a queue.
Queues can hold arbitrary items of a fixed size.
The size of each item and the capacity of the queue are defined when the
queue is created.
Items are enqueued by copy, not reference.
20 / 34
FreeRTOS
N
IPC and Synchronization – Queues
Queues and Blocking
Access to queues is either blocking or non-blocking.
The scheduler blocks tasks when they attempt to read from or write to a
queue that is either empty of full, respectively.
If the xTicksToWait variable is zero and the queue is empty (full), the
task does not block. Otherwise, the task will block for xTicksToWait
scheduler ticks or until an event on the queue frees up the resource.
This includes attempts to obtain semaphores, since they are special cases
of queues.
21 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Binary Semaphores
Used for both mutual exclusion and synchronization.
Commonly used by Interrupt Service Routines (ISRs) to wake tasks and
avoid polling. Consider the following example.
Example: waking a task from an ISR
22 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Binary Semaphores
Used for both mutual exclusion and synchronization.
Commonly used by Interrupt Service Routines (ISRs) to wake tasks and
avoid polling. Consider the following example.
Example: waking a task from an ISR
1 A binary semaphore is created along with a task that blocks on this
semaphore.
22 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Binary Semaphores
Used for both mutual exclusion and synchronization.
Commonly used by Interrupt Service Routines (ISRs) to wake tasks and
avoid polling. Consider the following example.
Example: waking a task from an ISR
1 A binary semaphore is created along with a task that blocks on this
semaphore.
2 An ISR is written for a peripheral that sets the semaphore when the
peripheral requires servicing using xSemaphoreGiveFromISR().
22 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Binary Semaphores
Used for both mutual exclusion and synchronization.
Commonly used by Interrupt Service Routines (ISRs) to wake tasks and
avoid polling. Consider the following example.
Example: waking a task from an ISR
1 A binary semaphore is created along with a task that blocks on this
semaphore.
2 An ISR is written for a peripheral that sets the semaphore when the
peripheral requires servicing using xSemaphoreGiveFromISR().
3 This awakens the task waiting on this semaphore. The task resets the
semaphore, does some work, and then blocks again.
22 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Counting Semaphores and Mutexes
Counting Semaphores
FreeRTOS has support for counting semaphores, the standard down
and up (wait and signal) operations.
Semaphores are macros defined over the queue API. Therefore,
semaphores use the same API calls as queues do.
23 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Counting Semaphores and Mutexes
Counting Semaphores
FreeRTOS has support for counting semaphores, the standard down
and up (wait and signal) operations.
Semaphores are macros defined over the queue API. Therefore,
semaphores use the same API calls as queues do.
Mutexes
FreeRTOS supports mutexes (binary semaphores with priority
inheritance).
As mutexes use the semaphore API, they also support the blocking
timeout mechanism. Moreover, they are implemented using the queue
API calls.
23 / 34
FreeRTOS
N
IPC and Synchronization – Semaphores and Mutexes
Counting Semaphores and Mutexes
Counting Semaphores
FreeRTOS has support for counting semaphores, the standard down
and up (wait and signal) operations.
Semaphores are macros defined over the queue API. Therefore,
semaphores use the same API calls as queues do.
Mutexes
FreeRTOS supports mutexes (binary semaphores with priority
inheritance).
As mutexes use the semaphore API, they also support the blocking
timeout mechanism. Moreover, they are implemented using the queue
API calls.
The queue data strictures are protected from corruption by disabling
interrupts in some places and disabling the scheduler in others where
it would be okay for an interrupt to occur, but not for another task to
preempt the queuing task.
23 / 34
FreeRTOS
N
Scheduler
Outline
1 Introduction
About FreeRTOS
Kernel Overview
2 Tasks
Tasks versus Co-Routines
Task Details
3 IPC and Synchronization
Queues
Semaphores and Mutexes
4 Scheduler
Scheduler Background
Scheduler Code
24 / 34
FreeRTOS
N
Scheduler – Scheduler Background
General Scheduler Operation
As mentioned, the scheduler can be co-operative or preemptive.
The scheduler is an interrupt handler activated on each tick of hardware
clock. Therefore, it contains hardware specific code.
The ready list is arranged in order of priority with tasks of equal priority
being served on a round-robin basis.
The scheduler starts with the highest priority list and works its way
downward.
There is no explicit Running list or state. The kernel maintains
pxCurrentTCB to identify the process in the Ready list that is currently
running.
25 / 34
FreeRTOS
N
Scheduler – Scheduler Background
Performing a Context Switch (0/2)
The scheduler first resets the
counter timer.
Without preemption enabled, the
timer interrupt simply increments
the tick count and returns.
vPortTickInterupt()
Stack GCC Soft Registers
Reset Timer
Preemption or Co-op?
Increment Tick Count
vTaskIncrementTick()
Save Context
Check for Context
Switch
Increment Tick Count
vTaskIncrementTick()
Restore Context
Un-Stack GCC Soft Registers
Return
Preemption
Co-op
26 / 34
FreeRTOS
N
Scheduler – Scheduler Background
Performing a Context Switch (1/2)
If the scheduler is preemptive, the
scheduler pushes the current task
context on the stack.
Then it increments the tick count
and checks to see if this action has
caused a blocked task to unblock.
If a task of higher priority
unblocked, then a context switch is
executed.
Context is restored.
The scheduler returns, potentially
starting the execution of a different
task than the one that was
interrupted.
vPortTickInterupt()
Stack GCC Soft Registers
Reset Timer
Preemption or Co-op?
Increment Tick Count
vTaskIncrementTick()
Save Context
Check for Context
Switch
Increment Tick Count
vTaskIncrementTick()
Restore Context
Un-Stack GCC Soft Registers
Return
Preemption
Co-op
27 / 34
FreeRTOS
N
Scheduler – Scheduler Code
ISR for Ticks (0/2)
Source/portable/GCC/ATMega323/port.c (edited)
#if configUSE_PREEMPTION == 1
void SIG_OUTPUT_COMPARE1A ( void ) __attribute__ (( signal , naked ));
void SIG_OUTPUT_COMPARE1A ( void ){
vPortYieldFromTick ();
asm volatile ( "reti" );
}
#else
void SIG_OUTPUT_COMPARE1A ( void ) __attribute__ ( ( signal ) );
void SIG_OUTPUT_COMPARE1A ( void ) {
vTaskIncrementTick ();
}
Explanation on next slide.
28 / 34
FreeRTOS
N
Scheduler – Scheduler Code
ISR for Ticks (1/2)
Source/portable/GCC/ATMega323/port.c (edited)
#if configUSE_PREEMPTION == 1
void SIG_OUTPUT_COMPARE1A ( void ) __attribute__ (( signal , naked ));
...
The signal attribute informs GCC the function is an ISR, which means
GCC saves and restores every register the ISR modifies and the return
uses reti instead of ret. The AVR microcontroller disables interrupts
upon entering an ISR, and reti is required to re-enable them.
But FreeRTOS saves registers, so naked is used so that GCC won’t
generate any function entry or exit code. Macros portSAVE_CONTEXT()
and portRESTORE_CONTEXT() save and restore the execution context.
29 / 34
FreeRTOS
N
Scheduler – Scheduler Code
The Context Saving Macro (0/2)
Source/portable/GCC/ATMega323/port.c (edited)
#define portSAVE_CONTEXT () 
asm volatile ( "push r0 nt"  : : Save r e g i s t e r r0 .
"in r0 , __SREG__ nt"  : : See (1) below .
"cli nt"  : : Disable i n t e r r u p t s .
"push r0 nt"  : : Save processor s t a t u s .
"push r1 nt"  : : Save o r i g i n a l r1 value .
"clr r1 nt"  : : See (2) below .
"push r2 nt"  : : Save r3 through r30 .
...
"push r31 nt" 
...
1 Before the microcontroller jumps to an ISR, it pushes the PC onto the
stack, which is why this is not done here.
2 Move processor status register to r0.
3 The instruction clr r1 sets r1 to zero, because the GCC generated ISR
expects it to be zero.
4 Continued on next slide.
30 / 34
FreeRTOS
N
Scheduler – Scheduler Code
The Context Saving Macro (1/2)
Source/portable/GCC/ATMega323/port.c (edited)
...
"push r31 nt" 
"lds r26 , pxCurrentTCB nt"  : : See ( 1 ) .
"lds r27 , pxCurrentTCB + 1 nt" 
"in r0 , 0x3d nt"  : : (2)
"st x+, r0 nt" 
"in r0 , 0x3e nt"  : : (3)
"st x+, r0 nt" 
);
1 The X register allows data indirect addressing with pre-decrement. r26 is
X low byte, 27 the high byte. The X processor register is loaded with the
address to which the stack pointer is to be saved.
2 Save stack pointer low byte.
3 Save stack pointer high nibble.
4 This is the reason struct tskTCB’s first member must be
portSTACK_TYPE *pxTopOfStack.
31 / 34
FreeRTOS
N
Scheduler – Scheduler Code
Incrementing the Tick (0/2)
Source/task.c (edited)
void vTaskIncrementTick ( void ){
if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE ){
: : In t h i s case , the scheduler not suspended .
++ xTickCount; : : Increment t i c k s .
if( xTickCount == ( portTickType ) 0 ) {
xList *pxTemp;
/*
Tick count has overflowed so we need to swap the delay lists.
If there are any items in pxDelayedTaskList here then there is
an error!
*/
pxTemp = pxDelayedTaskList ;
pxDelayedTaskList = pxOverflowDelayedTaskList ;
pxOverflowDelayedTaskList = pxTemp;
xNumOfOverflows ++;
}
prvCheckDelayedTasks (); : : Has t i c k made a timeout e x p i r e ?
} else {
...
32 / 34
FreeRTOS
N
Scheduler – Scheduler Code
Incrementing the Tick (1/2)
Source/task.c (edited)
...
} else {
++ uxMissedTicks ; : : Scheduler i s suspended , so we missed a t i c k .
: : The t i c k hook gets c a l l e d at r e g u l a r i n t e r v a l s , even i f the
: : scheduler i s locked .
#if ( configUSE_TICK_HOOK == 1 ){
extern void vApplicationTickHook ( void );
vApplicationTickHook ();
}
#endif
}
#if ( configUSE_TICK_HOOK == 1 ){
extern void vApplicationTickHook ( void );
/* Guard against the tick hook being called when the missed tick
count is being unwound (scheduler is being unlocked ). */
if( uxMissedTicks == 0 )
vApplicationTickHook ();
}
#endif
...
33 / 34
FreeRTOS
N
Scheduler – Scheduler Code
References
FreeRTOS website: www.FreeRTOS.org
Atmel AVR Instruction Set Guide
www.Atmel.com/ateml/acrobat/doc0856.pdf
34 / 34
FreeRTOS
N

More Related Content

Similar to freertos-proj.pdf

Virtual platform
Virtual platformVirtual platform
Virtual platformsean chen
Β 
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...PROIDEA
Β 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Andriy Berestovskyy
Β 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!C4Media
Β 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel DevelopmentRodrigo Almeida
Β 
Berkeley Packet Filters
Berkeley Packet FiltersBerkeley Packet Filters
Berkeley Packet FiltersKernel TLV
Β 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
Β 
Building a QT based solution on a i.MX7 processor running Linux and FreeRTOS
Building a QT based solution on a i.MX7 processor running Linux and FreeRTOSBuilding a QT based solution on a i.MX7 processor running Linux and FreeRTOS
Building a QT based solution on a i.MX7 processor running Linux and FreeRTOSFernando Luiz Cola
Β 
Concurrent programming with RTOS
Concurrent programming with RTOSConcurrent programming with RTOS
Concurrent programming with RTOSSirin Software
Β 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
Β 
IRQs: the Hard, the Soft, the Threaded and the Preemptible
IRQs: the Hard, the Soft, the Threaded and the PreemptibleIRQs: the Hard, the Soft, the Threaded and the Preemptible
IRQs: the Hard, the Soft, the Threaded and the PreemptibleAlison Chaiken
Β 
Ui disk & terminal drivers
Ui disk & terminal driversUi disk & terminal drivers
Ui disk & terminal driversSarang Ananda Rao
Β 
Unix system programming
Unix system programmingUnix system programming
Unix system programmingSyed Mustafa
Β 
Introduction to FreeRTOS
Introduction to FreeRTOSIntroduction to FreeRTOS
Introduction to FreeRTOSICS
Β 
BKK16-103 OpenCSD - Open for Business!
BKK16-103 OpenCSD - Open for Business!BKK16-103 OpenCSD - Open for Business!
BKK16-103 OpenCSD - Open for Business!Linaro
Β 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing LandscapeSasha Goldshtein
Β 
Apache Flink Deep Dive
Apache Flink Deep DiveApache Flink Deep Dive
Apache Flink Deep DiveVasia Kalavri
Β 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingMichelle Holley
Β 

Similar to freertos-proj.pdf (20)

Virtual platform
Virtual platformVirtual platform
Virtual platform
Β 
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
Β 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)
Β 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
Β 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
Β 
Berkeley Packet Filters
Berkeley Packet FiltersBerkeley Packet Filters
Berkeley Packet Filters
Β 
Lab6 rtos
Lab6 rtosLab6 rtos
Lab6 rtos
Β 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Β 
Building a QT based solution on a i.MX7 processor running Linux and FreeRTOS
Building a QT based solution on a i.MX7 processor running Linux and FreeRTOSBuilding a QT based solution on a i.MX7 processor running Linux and FreeRTOS
Building a QT based solution on a i.MX7 processor running Linux and FreeRTOS
Β 
Concurrent programming with RTOS
Concurrent programming with RTOSConcurrent programming with RTOS
Concurrent programming with RTOS
Β 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
Β 
Free FreeRTOS Course-Task Management
Free FreeRTOS Course-Task ManagementFree FreeRTOS Course-Task Management
Free FreeRTOS Course-Task Management
Β 
IRQs: the Hard, the Soft, the Threaded and the Preemptible
IRQs: the Hard, the Soft, the Threaded and the PreemptibleIRQs: the Hard, the Soft, the Threaded and the Preemptible
IRQs: the Hard, the Soft, the Threaded and the Preemptible
Β 
Ui disk & terminal drivers
Ui disk & terminal driversUi disk & terminal drivers
Ui disk & terminal drivers
Β 
Unix system programming
Unix system programmingUnix system programming
Unix system programming
Β 
Introduction to FreeRTOS
Introduction to FreeRTOSIntroduction to FreeRTOS
Introduction to FreeRTOS
Β 
BKK16-103 OpenCSD - Open for Business!
BKK16-103 OpenCSD - Open for Business!BKK16-103 OpenCSD - Open for Business!
BKK16-103 OpenCSD - Open for Business!
Β 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
Β 
Apache Flink Deep Dive
Apache Flink Deep DiveApache Flink Deep Dive
Apache Flink Deep Dive
Β 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet Processing
Β 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ9953056974 Low Rate Call Girls In Saket, Delhi NCR
Β 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
Β 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
Β 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
Β 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
Β 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
Β 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
Β 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
Β 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
Β 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
Β 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
Β 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
Β 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
Β 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
Β 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
Β 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
Β 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
Β 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
Β 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
Β 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
Β 
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Β 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
Β 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
Β 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
Β 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
Β 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
Β 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Β 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
Β 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
Β 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
Β 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
Β 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)β€”β€”β€”β€”IMP.OF KSHARA ...
Β 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
Β 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
Β 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
Β 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
Β 

freertos-proj.pdf

  • 1. FreeRTOS A Brief Overview Christopher Kenna Avionics October 1, 2010 1 / 34 FreeRTOS N
  • 2. Introduction Outline 1 Introduction About FreeRTOS Kernel Overview 2 Tasks Tasks versus Co-Routines Task Details 3 IPC and Synchronization Queues Semaphores and Mutexes 4 Scheduler Scheduler Background Scheduler Code 2 / 34 FreeRTOS N
  • 3. Introduction – About FreeRTOS Background Information The FreeRTOS Project supports 25 official architecture ports, with many more community developed ports. The FreeRTOS RT kernel is portable, open source, royalty free, and very small. OpenRTOS is a commercialized version by the sister company High Integrity Systems. Richard Barry: I know FreeRTOS has been used in some rockets and other aircraft, but nothing too commercial. We have customers that use it on ship systems, and WITTENSTEIN sell SafeRTOS which has been certified to various standards, but not DO-178B. 3 / 34 FreeRTOS N
  • 4. Introduction – About FreeRTOS Licensing Information (0/2) The license is a modified GNU GPL according to the table below. FreeRTOS Modified GPL OpenRTOS Commercial Free? ! # Use it in a commercial application? ! ! Royalty free? ! ! Must open source code that makes use of FreeRTOS services? #1 # Must open source kernel changes? ! # 1As long as code provides functionality that is distinct from that provided by FreeRTOS. 4 / 34 FreeRTOS N
  • 5. Introduction – About FreeRTOS Licensing Information (1/2) The license is a modified GNU GPL according to the table below. FreeRTOS Modified GPL OpenRTOS Commercial Must document that the product uses FreeRTOS? !2 # Required to offer FreeRTOS code to application users? ! # Technical support for OS? #3 ! Warranty? # ! 2Web link to FreeRTOS.org is sufficient. 3There is an on-line community, though. 5 / 34 FreeRTOS N
  • 6. Introduction – Kernel Overview FreeRTOS Configuration The operation of FreeRTOS is governed by FreeRTOS.h, with application specific configuration appearing in FreeRTOSConfg.h. Obviously, these are static configuration options. Some examples: configUSE_PREEMPTION configCPU_CLOCK_HZ – CPU clock frequency, not necessarily the bus frequency. configTICK_RATE_HZ – RTOS tick frequency that dictates interrupt frequency. configMAX_PRIORITIES – Total number of priority levels. Each level creates a new list, so memory sensitive machines should keep this to a minimum. And more. . . 6 / 34 FreeRTOS N
  • 7. Tasks Outline 1 Introduction About FreeRTOS Kernel Overview 2 Tasks Tasks versus Co-Routines Task Details 3 IPC and Synchronization Queues Semaphores and Mutexes 4 Scheduler Scheduler Background Scheduler Code 7 / 34 FreeRTOS N
  • 8. Tasks – Tasks versus Co-Routines Tasks in FreeRTOS Tasks have their own context. No dependency on other tasks unless defined. One task executes at a time. Tasks have no knowledge of scheduler activity. The scheduler handles context switching. Thus, tasks each have their own stack upon which execution context can be saved. Prioritized and preemptable. 8 / 34 FreeRTOS N
  • 9. Tasks – Tasks versus Co-Routines Co-Routines Co-routines are intended for use on small processors that have severe RAM constraints. Co-Routines share a single stack. Co-routines are implemented through macros. Prioritized relative to other co-routines, but preempted by tasks. The structure of co-routines is rigid due to the unconventional implementation. Lack of stack requires special consideration. Restrictions on where API calls can be made. Cannot communicate with tasks. Will examine tasks in more detail, but not co-routines. 9 / 34 FreeRTOS N
  • 10. Tasks – Task Details Task States Running – Actively executing and using the processor. Ready – Able to execute, but not because a task of equal or higher priority is in the Running state. Blocked – Waiting for a temporal or external event. E.g., queue and semaphore events, or calling vTaskDelay() to block until delay period has expired. Always have a β€œtimeout” period, after which the task is unblocked. Suspended – Only enter via vTaskSuspend(), depart via xTaskResume() API calls. 10 / 34 FreeRTOS N
  • 11. Tasks – Task Details Task Priorities Each task gets a priority from 0 to configMAX_PRIORITIES βˆ’ 1 Priority can be set on a per application basis. Tasks can change their own priority, as well as the priority of other tasks. tskIDLE_PRIORITY = 0 11 / 34 FreeRTOS N
  • 12. Tasks – Task Details The Idle Task The idle task is created automatically when the scheduler is started. 12 / 34 FreeRTOS N
  • 13. Tasks – Task Details The Idle Task The idle task is created automatically when the scheduler is started. It frees memory allocated by the RTOS to tasks that have since been deleted. Thus, applications that use vTaskDelete() to remove tasks should ensure the idle task is not starved. 12 / 34 FreeRTOS N
  • 14. Tasks – Task Details The Idle Task The idle task is created automatically when the scheduler is started. It frees memory allocated by the RTOS to tasks that have since been deleted. Thus, applications that use vTaskDelete() to remove tasks should ensure the idle task is not starved. The idle task has no other function, so cases when the idle task need never run exist. 12 / 34 FreeRTOS N
  • 15. Tasks – Task Details The Idle Task The idle task is created automatically when the scheduler is started. It frees memory allocated by the RTOS to tasks that have since been deleted. Thus, applications that use vTaskDelete() to remove tasks should ensure the idle task is not starved. The idle task has no other function, so cases when the idle task need never run exist. There is an idle task hook, which can do some work at each idle interval without the RAM usage overhead associated with running a task at the idle priority. 12 / 34 FreeRTOS N
  • 16. Tasks – Task Details Task Control Block (TCB) (0/2) A TCB is allocated to each task. It stores the task’s context. Source/tasks.c typedef struct tskTaskControlBlock { : : Top of task ’ s stack . Must be f i r s t member b/c of context switch volatile portSTACK_TYPE * pxTopOfStack ; : : code ( l a t e r s l i d e s ) . #if ( portUSING_MPU_WRAPPERS == 1 ) : : The MPU s e t t i n g s are defined as part of the port l a y e r . xMPU_SETTINGS xMPUSettings ; : : Must be 2nd member of s t r u c t . #endif : : L i s t item used to place the TCB in ready and blocked queues . xListItem xGenericListItem ; : : Used to place the TCB in event l i s t s . xListItem xEventListItem ; : : The p r i o r i t y of the task where 0 i s the lowest p r i o r i t y . unsigned portBASE_TYPE uxPriority; : : Points to the s t a r t of the stack . portSTACK_TYPE *pxStack; : : D e s c r i p t i v e name given to the task when created . : : F a c i l i t a t e s debugging only . signed char pcTaskName[ configMAX_TASK_NAME_LEN ]; 13 / 34 FreeRTOS N
  • 17. Tasks – Task Details Task Control Block (TCB) (1/2) Source/tasks.c #if ( portSTACK_GROWTH > 0 ) : : Used f o r stack overflow checking on a r c h i t e c t u r e s where the : : stack grows up from low memory . portSTACK_TYPE * pxEndOfStack ; #endif ... #if ( configUSE_TRACE_FACILITY == 1 ) : : Used only f o r t r a c i n g the scheduler , making debugging e a s i e r . unsigned portBASE_TYPE uxTCBNumber ; #endif #if ( configUSE_MUTEXES == 1 ) : : The p r i o r i t y l a s t assigned to the task , used by the p r i o r i t y : : i n h e r i t a n c e mechanism . unsigned portBASE_TYPE uxBasePriority ; #endif ... #if ( configGENERATE_RUN_TIME_STATS == 1 ) : : Used f o r c a l c u l a t i n g how much CPU time each task i s u t i l i z i n g . unsigned long ulRunTimeCounter ; #endif } tskTCB; 14 / 34 FreeRTOS N
  • 18. Tasks – Task Details Implementing a Task Source/include/projdefs.h /* Defines the prototype to which task functions must conform. */ typedef void (* pdTASK_CODE )( void * ); void vATaskFunction ( void * pvParameters ){ for( ;; ){ : : Task a p p l i c a t i o n code here . } } Tasks are always a function that returns void and takes a void pointer. Tasks should never return (loop forever). Not much to it, really. 15 / 34 FreeRTOS N
  • 19. Tasks – Task Details Creating a Task (0/3) The kernel creates a task by instantiating and populating a TCB. New tasks are placed in the Ready state and added to the Ready list. If the task is the highest priority task, then it is set as the currently running task. Created by calling xTaskCreate() and deleted by calling vTaskDelete(). xTaskCreate() takes the following parameters. A pointer to the function that implements the task (type pdTASK_CODE from earlier). A name for the task. The depth of the task’s stack. The task’s priority. A pointer to any parameters needed by the task’s function. 16 / 34 FreeRTOS N
  • 20. Tasks – Task Details Creating a Task (1/3) An interesting step in task creation is preparing the task for its first context switch. The TCB stack is initialized to look as if the task was already running, but had been interrupted by the scheduler. The return address is set to the start of the task function with pxPortInitialiseStack. See code on next slide. 17 / 34 FreeRTOS N
  • 21. Tasks – Task Details Creating a Task (2/3) Source/portable/GCC/ATMega323.c (edited) portSTACK_TYPE * pxPortInitialiseStack ( portSTACK_TYPE *pxTopOfStack , pdTASK_CODE pxCode , void * pvParameters ) { : : Place some known values on bottom of stack , f o r debugging . * pxTopOfStack = 0x11; pxTopOfStack --; * pxTopOfStack = 0x22; pxTopOfStack --; ... : : Start of task code w i l l be popped o f f l a s t , so push i t on f i r s t . unsigned short usAddress = ( unsigned short ) pxCode; * pxTopOfStack = ( portSTACK_TYPE ) (usAddress & (unsigned short) 0x00ff ); pxTopOfStack --; usAddress >>= 8; * pxTopOfStack = ( portSTACK_TYPE ) (usAddress & (unsigned short) 0x00ff ); pxTopOfStack --; : : And then push values f o r CPU r e g i s t e r s , taking i n t o account : : what the compiler expects f o r t h i s a r c h i t e c t u r e . ... 18 / 34 FreeRTOS N
  • 22. IPC and Synchronization Outline 1 Introduction About FreeRTOS Kernel Overview 2 Tasks Tasks versus Co-Routines Task Details 3 IPC and Synchronization Queues Semaphores and Mutexes 4 Scheduler Scheduler Background Scheduler Code 19 / 34 FreeRTOS N
  • 23. IPC and Synchronization – Queues Queue Overview Queues are the primary form of inter-task communications. They can send messages between tasks as well as between interrupts and tasks. Supports appending data to the back of a queue, or sending data to the head of a queue. Queues can hold arbitrary items of a fixed size. The size of each item and the capacity of the queue are defined when the queue is created. Items are enqueued by copy, not reference. 20 / 34 FreeRTOS N
  • 24. IPC and Synchronization – Queues Queues and Blocking Access to queues is either blocking or non-blocking. The scheduler blocks tasks when they attempt to read from or write to a queue that is either empty of full, respectively. If the xTicksToWait variable is zero and the queue is empty (full), the task does not block. Otherwise, the task will block for xTicksToWait scheduler ticks or until an event on the queue frees up the resource. This includes attempts to obtain semaphores, since they are special cases of queues. 21 / 34 FreeRTOS N
  • 25. IPC and Synchronization – Semaphores and Mutexes Binary Semaphores Used for both mutual exclusion and synchronization. Commonly used by Interrupt Service Routines (ISRs) to wake tasks and avoid polling. Consider the following example. Example: waking a task from an ISR 22 / 34 FreeRTOS N
  • 26. IPC and Synchronization – Semaphores and Mutexes Binary Semaphores Used for both mutual exclusion and synchronization. Commonly used by Interrupt Service Routines (ISRs) to wake tasks and avoid polling. Consider the following example. Example: waking a task from an ISR 1 A binary semaphore is created along with a task that blocks on this semaphore. 22 / 34 FreeRTOS N
  • 27. IPC and Synchronization – Semaphores and Mutexes Binary Semaphores Used for both mutual exclusion and synchronization. Commonly used by Interrupt Service Routines (ISRs) to wake tasks and avoid polling. Consider the following example. Example: waking a task from an ISR 1 A binary semaphore is created along with a task that blocks on this semaphore. 2 An ISR is written for a peripheral that sets the semaphore when the peripheral requires servicing using xSemaphoreGiveFromISR(). 22 / 34 FreeRTOS N
  • 28. IPC and Synchronization – Semaphores and Mutexes Binary Semaphores Used for both mutual exclusion and synchronization. Commonly used by Interrupt Service Routines (ISRs) to wake tasks and avoid polling. Consider the following example. Example: waking a task from an ISR 1 A binary semaphore is created along with a task that blocks on this semaphore. 2 An ISR is written for a peripheral that sets the semaphore when the peripheral requires servicing using xSemaphoreGiveFromISR(). 3 This awakens the task waiting on this semaphore. The task resets the semaphore, does some work, and then blocks again. 22 / 34 FreeRTOS N
  • 29. IPC and Synchronization – Semaphores and Mutexes Counting Semaphores and Mutexes Counting Semaphores FreeRTOS has support for counting semaphores, the standard down and up (wait and signal) operations. Semaphores are macros defined over the queue API. Therefore, semaphores use the same API calls as queues do. 23 / 34 FreeRTOS N
  • 30. IPC and Synchronization – Semaphores and Mutexes Counting Semaphores and Mutexes Counting Semaphores FreeRTOS has support for counting semaphores, the standard down and up (wait and signal) operations. Semaphores are macros defined over the queue API. Therefore, semaphores use the same API calls as queues do. Mutexes FreeRTOS supports mutexes (binary semaphores with priority inheritance). As mutexes use the semaphore API, they also support the blocking timeout mechanism. Moreover, they are implemented using the queue API calls. 23 / 34 FreeRTOS N
  • 31. IPC and Synchronization – Semaphores and Mutexes Counting Semaphores and Mutexes Counting Semaphores FreeRTOS has support for counting semaphores, the standard down and up (wait and signal) operations. Semaphores are macros defined over the queue API. Therefore, semaphores use the same API calls as queues do. Mutexes FreeRTOS supports mutexes (binary semaphores with priority inheritance). As mutexes use the semaphore API, they also support the blocking timeout mechanism. Moreover, they are implemented using the queue API calls. The queue data strictures are protected from corruption by disabling interrupts in some places and disabling the scheduler in others where it would be okay for an interrupt to occur, but not for another task to preempt the queuing task. 23 / 34 FreeRTOS N
  • 32. Scheduler Outline 1 Introduction About FreeRTOS Kernel Overview 2 Tasks Tasks versus Co-Routines Task Details 3 IPC and Synchronization Queues Semaphores and Mutexes 4 Scheduler Scheduler Background Scheduler Code 24 / 34 FreeRTOS N
  • 33. Scheduler – Scheduler Background General Scheduler Operation As mentioned, the scheduler can be co-operative or preemptive. The scheduler is an interrupt handler activated on each tick of hardware clock. Therefore, it contains hardware specific code. The ready list is arranged in order of priority with tasks of equal priority being served on a round-robin basis. The scheduler starts with the highest priority list and works its way downward. There is no explicit Running list or state. The kernel maintains pxCurrentTCB to identify the process in the Ready list that is currently running. 25 / 34 FreeRTOS N
  • 34. Scheduler – Scheduler Background Performing a Context Switch (0/2) The scheduler first resets the counter timer. Without preemption enabled, the timer interrupt simply increments the tick count and returns. vPortTickInterupt() Stack GCC Soft Registers Reset Timer Preemption or Co-op? Increment Tick Count vTaskIncrementTick() Save Context Check for Context Switch Increment Tick Count vTaskIncrementTick() Restore Context Un-Stack GCC Soft Registers Return Preemption Co-op 26 / 34 FreeRTOS N
  • 35. Scheduler – Scheduler Background Performing a Context Switch (1/2) If the scheduler is preemptive, the scheduler pushes the current task context on the stack. Then it increments the tick count and checks to see if this action has caused a blocked task to unblock. If a task of higher priority unblocked, then a context switch is executed. Context is restored. The scheduler returns, potentially starting the execution of a different task than the one that was interrupted. vPortTickInterupt() Stack GCC Soft Registers Reset Timer Preemption or Co-op? Increment Tick Count vTaskIncrementTick() Save Context Check for Context Switch Increment Tick Count vTaskIncrementTick() Restore Context Un-Stack GCC Soft Registers Return Preemption Co-op 27 / 34 FreeRTOS N
  • 36. Scheduler – Scheduler Code ISR for Ticks (0/2) Source/portable/GCC/ATMega323/port.c (edited) #if configUSE_PREEMPTION == 1 void SIG_OUTPUT_COMPARE1A ( void ) __attribute__ (( signal , naked )); void SIG_OUTPUT_COMPARE1A ( void ){ vPortYieldFromTick (); asm volatile ( "reti" ); } #else void SIG_OUTPUT_COMPARE1A ( void ) __attribute__ ( ( signal ) ); void SIG_OUTPUT_COMPARE1A ( void ) { vTaskIncrementTick (); } Explanation on next slide. 28 / 34 FreeRTOS N
  • 37. Scheduler – Scheduler Code ISR for Ticks (1/2) Source/portable/GCC/ATMega323/port.c (edited) #if configUSE_PREEMPTION == 1 void SIG_OUTPUT_COMPARE1A ( void ) __attribute__ (( signal , naked )); ... The signal attribute informs GCC the function is an ISR, which means GCC saves and restores every register the ISR modifies and the return uses reti instead of ret. The AVR microcontroller disables interrupts upon entering an ISR, and reti is required to re-enable them. But FreeRTOS saves registers, so naked is used so that GCC won’t generate any function entry or exit code. Macros portSAVE_CONTEXT() and portRESTORE_CONTEXT() save and restore the execution context. 29 / 34 FreeRTOS N
  • 38. Scheduler – Scheduler Code The Context Saving Macro (0/2) Source/portable/GCC/ATMega323/port.c (edited) #define portSAVE_CONTEXT () asm volatile ( "push r0 nt" : : Save r e g i s t e r r0 . "in r0 , __SREG__ nt" : : See (1) below . "cli nt" : : Disable i n t e r r u p t s . "push r0 nt" : : Save processor s t a t u s . "push r1 nt" : : Save o r i g i n a l r1 value . "clr r1 nt" : : See (2) below . "push r2 nt" : : Save r3 through r30 . ... "push r31 nt" ... 1 Before the microcontroller jumps to an ISR, it pushes the PC onto the stack, which is why this is not done here. 2 Move processor status register to r0. 3 The instruction clr r1 sets r1 to zero, because the GCC generated ISR expects it to be zero. 4 Continued on next slide. 30 / 34 FreeRTOS N
  • 39. Scheduler – Scheduler Code The Context Saving Macro (1/2) Source/portable/GCC/ATMega323/port.c (edited) ... "push r31 nt" "lds r26 , pxCurrentTCB nt" : : See ( 1 ) . "lds r27 , pxCurrentTCB + 1 nt" "in r0 , 0x3d nt" : : (2) "st x+, r0 nt" "in r0 , 0x3e nt" : : (3) "st x+, r0 nt" ); 1 The X register allows data indirect addressing with pre-decrement. r26 is X low byte, 27 the high byte. The X processor register is loaded with the address to which the stack pointer is to be saved. 2 Save stack pointer low byte. 3 Save stack pointer high nibble. 4 This is the reason struct tskTCB’s first member must be portSTACK_TYPE *pxTopOfStack. 31 / 34 FreeRTOS N
  • 40. Scheduler – Scheduler Code Incrementing the Tick (0/2) Source/task.c (edited) void vTaskIncrementTick ( void ){ if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE ){ : : In t h i s case , the scheduler not suspended . ++ xTickCount; : : Increment t i c k s . if( xTickCount == ( portTickType ) 0 ) { xList *pxTemp; /* Tick count has overflowed so we need to swap the delay lists. If there are any items in pxDelayedTaskList here then there is an error! */ pxTemp = pxDelayedTaskList ; pxDelayedTaskList = pxOverflowDelayedTaskList ; pxOverflowDelayedTaskList = pxTemp; xNumOfOverflows ++; } prvCheckDelayedTasks (); : : Has t i c k made a timeout e x p i r e ? } else { ... 32 / 34 FreeRTOS N
  • 41. Scheduler – Scheduler Code Incrementing the Tick (1/2) Source/task.c (edited) ... } else { ++ uxMissedTicks ; : : Scheduler i s suspended , so we missed a t i c k . : : The t i c k hook gets c a l l e d at r e g u l a r i n t e r v a l s , even i f the : : scheduler i s locked . #if ( configUSE_TICK_HOOK == 1 ){ extern void vApplicationTickHook ( void ); vApplicationTickHook (); } #endif } #if ( configUSE_TICK_HOOK == 1 ){ extern void vApplicationTickHook ( void ); /* Guard against the tick hook being called when the missed tick count is being unwound (scheduler is being unlocked ). */ if( uxMissedTicks == 0 ) vApplicationTickHook (); } #endif ... 33 / 34 FreeRTOS N
  • 42. Scheduler – Scheduler Code References FreeRTOS website: www.FreeRTOS.org Atmel AVR Instruction Set Guide www.Atmel.com/ateml/acrobat/doc0856.pdf 34 / 34 FreeRTOS N