freertos overview

23
1 FreeRTOS Overview Much of the content of this set of slides is taken from the documentation existing on the FreeRTOS website www.freertos.org

Upload: atul-gupta

Post on 27-Nov-2014

259 views

Category:

Documents


15 download

TRANSCRIPT

Page 1: FreeRTOS Overview

1

FreeRTOS Overview

Much of the content of this set of slides is taken from the documentation existing on the FreeRTOS website

www.freertos.org

Page 2: FreeRTOS Overview

2

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

Page 3: FreeRTOS Overview

3

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.

Page 4: FreeRTOS Overview

4

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

Page 5: FreeRTOS Overview

5

Implementing a task

A task has the following structure, notice it never returns.

void mytask(void * parameters)

{

while (1) {

// body of task goes here

}

}

Page 6: FreeRTOS Overview

6

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.

Page 7: FreeRTOS Overview

7

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.

Page 8: FreeRTOS Overview

8

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.

Page 9: FreeRTOS Overview

9

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.

Page 10: FreeRTOS Overview

10

#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

Configuration parameters

The casts were omitted on some of the above to save space

Page 11: FreeRTOS Overview

11

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

Page 12: FreeRTOS Overview

12

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.

Page 13: FreeRTOS Overview

13

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.

Page 14: FreeRTOS Overview

14

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.

Page 15: FreeRTOS Overview

15

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.

Page 16: FreeRTOS Overview

16

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.

Page 17: FreeRTOS Overview

17

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.

Page 18: FreeRTOS Overview

18

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.

Page 19: FreeRTOS Overview

19

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

Page 20: FreeRTOS Overview

20

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.

Page 21: FreeRTOS Overview

21

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.

Page 22: FreeRTOS Overview

22

#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

Configuration parameters

Page 23: FreeRTOS Overview

23

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.