You are on page 1of 23

FreeRTOS Overview

Much of the content of this set of slides is


taken from the documentation existing on
the FreeRTOS website
www.freertos.org

1
FreeRTOS Overview

Features
● There is a choice of scheduling policy
– Pre-emptive: Always runs the highest priority
available task. Tasks of identical priority
share the CPU. (Fully pre-emptive with round
robin time slicing)
– Cooperative: Context switches only occur if a
task blocks, or explicitly calls taskYIELD()
● Message queue
● Semaphores
2
Tasks and Priorities

● Task priorities are denoted by numbers,


where low numbers denote low priority
tasks. The default idle priority is defined
by tskIDLE_PRIORITY as zero.
● The number of available priorities is
defined within FreeRTOSConfig.h. This
can be changed as needed.
● Any number of tasks can share the same
priority, including that of the idle task.

3
Tasks and Priorities

● Priority numbers should be chosen to be


close to each other and as low as possible.
If your application has three tasks that must
be at different priorities then use priorities
3(highest), 2, 1(lowest).
● A implementation independent way to do
that is to use tskIDLE_PRIORITY, i.e. for the
highest priority use:
tskIDLE_PRIORITY + (unsigned portCHAR)3

4
Implementing a task

A task has the following structure, notice it


never returns.
void mytask(void * parameters)
{
while (1) {
// body of task goes here
}
}

5
TASKS

● Tasks are created by calling xTaskCreate()


and deleted by calling vTaskDelete().
● In the PIC, we don‘t delete tasks as there is
no support to reclaim the memory that was
used by the deleted task.

6
The idle task

● The idle task is created automatically


when the very first task is created. It is
responsible for freeing memory for tasks
that have since been deleted. Therefore if
tasks are deleted, it is essential that the
idle task be given some time on the CPU.
● The idle task has no other essential
functions.

7
Starting the kernel

● The real-time kernel is started by calling


vTaskStartScheduler(). This call does
not return unless an application calls
vTaskEndScheduler() or the call cannot
be completed for some reason.

8
FreeRTOS API

The configuration file is FreeRTOSConfig.h


It is different for each port. The one for
the PIC18 with the MPLAB compiler is
shown on the next slide.

9
Configuration parameters

#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configTICK_RATE_HZ 1000
#define configCPU_CLOCK_HZ 20000000
#define configMAX_PRIORITIES 4
#define configMINIMAL_STACK_SIZE ( 105 )
#define configTOTAL_HEAP_SIZE ( 1024 )
#define configMAX_TASK_NAME_LEN ( 4 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 1
#define configIDLE_SHOULD_YIELD 1
The casts were omitted on some of the above to save space
10
Configuration parameters

● configUSE_PREEMPTION: set to 1 to use


the pre-emptive kernel or 0 to use the
cooperative one
● configUSE_IDLE_HOOK: set to 1 if you
wish to use an idle hook
● configCPU_CLOCK_HZ: the frequency in
HZ at which the internal processor core
will be executing. This is needed to
configure the timers

11
Configuration parameters

● configTICK_RATE_HZ: The frequency of


the RTOS tick interrupt
The tick interrupt is used to measure time.
Therefore a higher tick frequency means
time can be measured to a higher
resolution. However, a high tick
frequency also means that the kernel will
use more CPU time so be less efficient.

12
Configuration parameters

More than one task can share the same


priority. The kernel will share processor
time between tasks of the same priority
by switching between the tasks during
each RTOS tick. A high tick rate
frequency will therefore also have the
effect of reducing the 'time slice' given to
each task.

13
Configuration parameters

● configMAX_PRIORITIES: The number of


priorities available to the application.
Each available priority consumes RAM
within the kernel so this value should not
be set any higher than actually required
by your application.

14
Configuration parameters

● configMINIMAL_STACK_SIZE: The size of


the stack used by the idle task. Generally
this should not be reduced from the value
set in the FreeRTOSConfig.h file.
● configTOTAL_HEAP_SIZE: The total
amount of RAM available to the kernel.
This value will only be used if your
application makes use of one of the
sample memory allocation schemes
provided in the FreeRTOS source code
download.
15
Configuration parameters
● configMAX_TASK_NAME_LEN: The
maximum permissible length of the
descriptive name given to a task when the
task is created. The length is specified is
the number of characters including the
NULL termination byte.
● configUSE_TRACE_FACILITY: Set to 1 if
you wish the trace visualisation
functionality to be available. Tracing is
not supported under the PIC port.
16
Configuration parameters

● configUSE_16_BIT_TICKS: Time is
measured in 'ticks' - which is the number
of times the tick interrupt has executed
since the kernel was started. Defining it
as 1 causes portTickType to be defined
(typedef'ed) as an unsigned 16bit type.
Defining it as 0 causes portTickType to
be defined (typedef'ed) as an unsigned
32bit type.

17
Configuration parameters

Using a 16bit tick type will greatly improve


performance on 8 and 16bit architectures,
but limits the maximum specifiable time
period to 65535 “ticks”. Therefore,
assuming a tick frequency of 250Hz, the
maximum time a task can delay or block
when a 16bit counter is used is 262
seconds, compared to 17179869 seconds
when using a 32bit counter.

18
Configuration parameters

● config_IDLE_SHOULD_YIELD: This
parameter controls the behaviour of tasks
at the idle priority. It only has an effect if
– The pre-emptive scheduler is being used.
– The users application creates tasks that run
at the idle priority.
If tasks share the same priority and none of
the tasks get pre-empted, it might be
assumed that each task at a given priority
will be allocated an equal amount of
processing time 19
Configuration parameters
and if the shared priority is above the idle
priority then this is indeed the case.
However with the idle priority, the idle
task runs before it yields thus using a part
of the time slice for the following task.
This can be avoided by
– Using an idle hook in place of separate
tasks at the idle priority.
– Creating all application tasks at a
priority greater than the idle priority.
– Setting the parameter to 0.
20
Configuration parameters

The next set of configuration items are used


to select only those components of
FreeRTOS necessary for your application.
In this way you can fine-tune the memory
requirements.

21
Configuration parameters

#define INCLUDE_vTaskPrioritySet 0
#define INCLUDE_uxTaskPriorityGet 0
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 0
#define INCLUDE_vTaskSuspend 0
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1

22
Memory management

The RTOS kernel has to allocate RAM each


time a task, queue or semaphore is
created. On the PIC there is not enough
space to implement a full memory
management scheme, so a basic one used
where memory can be allocated but can
never be freed.
In this scheme, all tasks and queues must
be created before the kernel is started.

23

You might also like