Chương 6 FreeRTOS
Chương 6 FreeRTOS
Non-preemptive task
Task runs to completion, even if other tasks
want service
Example: the ISR routines
Preemptive task
Task is suspended if some other task wants
service, then restarted at a later time
Example: main program loop (preempted
by ISRs)
Preemptive vs. Non-Preemptive Tasks
The task has just started, and will need to run again for new
inputs•
All other tasks will need to run•
This same task will need to run again•
Same time for all tasks ... “i” doesn’t appear on right hand side of
equation
j N 1
Ri C
j 0
j
Task Scheduling Policies
Highest Priority
Hardware approach
Hardware determines something of higher
priority needs to execute
Works OK for ISRs preempting ordinary
code
But, tricky for ISRs preempting other ISRs!
How Do You Know When To Preempt?
Software approach
Software sees which task is highest priority,
and starts it running
You need a context swap to make this
work
“Context” = all data required to define the state of
the current task
Simplest example: all register values
Might include other things in more complex
computing enviroments(for example, file I/O status)
Should You Build Your Own Scheduler
Modified GPL
History of FreeRTOS
Newest version: 10.1.1
Keypad LCD
Display
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 0
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
Task management
Tác vụ là 1 hàm với một vòng lặp vô tận.
void ATaskFunction( void *pvParameters )
{
int iVariableExample = 0;
vTaskDelete( NULL );
}
Task state
Tác vụ chuyển từ trạng thái dừng sang chạy gọi là “swapped in”,
“switched in”
Tác vụ chuyển từ trạng thái chạy sang dừng gọi là “swapped out”,
“switched out”
Task management function
Creation
xTaskCreate Utilities
xTaskDelete tskIDLE_PRIORITY
xTaskGetTickCount
uxTaskGetNumberOfTasks
Control vTaskList
vTaskDelay vTaskGetRunTimeStats
vTaskDelayUntil vTaskStartTrace
uxTaskPriorityGet ulTaskEndTrace
vTaskPrioritySet uxTaskGetStackHighWaterMark
vTaskSuspend vTaskSetApplicationTaskTag
vTaskResume xTaskSetApplicationTaskTag
xTaskResumeFromISR xTaskCallApplicationTaskHook
Naming convention
Naming convention
Variables of type char are prefixed c
Variables of type short are prefixed s
Variables of type long are prefixed l
Variables of type float are prefixed f
Variables of type double are prefixed d
Enumerated variables are prefixed e
Other types (e.g. structs) are prefixed x
Pointers have an additional prefixed p , for example a
pointer to a short will have prefix ps
Unsigned variables have an additional prefixed u , for
example an unsigned short will have prefix us
Naming convention
File private functions are prefixed with prv
API functions are prefixed with their return
type, as per the convention defined for
variables
Function names start with the file in which they
are defined. For example vTaskDelete is
defined in Task. c
x is used both for structures and for RTOS
types such as portTickType (in the ARM port
this is equivalent to unsigned int)
Creating a Task, xTaskCreate
portBASE_TYPE xTaskCreate(
pdTASK_CODE pvTaskCode,
const signed portCHAR * const pcName,
unsigned portSHORT usStackDepth,
void *pvParameters,
unsignedportBASE_TYPE uxPriority,
xTaskHandle *pxCreatedTask
);
Creating a Task, xTaskCreate
for( ;; )
{
vPrintString( pcTaskName );
xLastWakeTime = xTaskGetTickCount();
xLastWakeTime = xTaskGetTickCount();
for( ;; );
return 0;
}
Periodic
Continuous 1
Continuous 2
IDLE
t1 t2 t3 t4 t5
IDLE task
for( ;; )
{
/* Print out the name of this task. */
vPrintString( "Task1 is running\r\n" );
for( ;; )
{
vPrintString( "Task2 is running\r\n" );
for( ;; );
}
Delete a task
for( ;; )
{
/* Print out the name of this task. */
vPrintString( "Task1 is running\r\n" );