Mastering The FreeRTOS Real Time Kernel-A Hands-On Tutorial Guide
Mastering The FreeRTOS Real Time Kernel-A Hands-On Tutorial Guide
vii
viii
Macro Names ................................................................................................................. 23
Rationale for Excessive Type Casting ............................................................................ 24
x
The xQueueSelectFromSet() API Function .................................................................. 135
xii
6.4 Binary Semaphores Used for Synchronization ........................................................ 191
The xSemaphoreCreateBinary() API Function ............................................................. 194
The xSemaphoreTake() API Function .......................................................................... 194
The xSemaphoreGiveFromISR() API Function ............................................................ 196
Example 16. Using a binary semaphore to synchronize a task with an interrupt .......... 198
xiii
Event Groups, Event Flags and Event Bits ................................................................... 268
xiv
xv
Figure 72 An event group in which only bits 1, 4 and 7 are set, and all the other event
.......................................... 268
Figure 73 The output produced when Example 22 is executed with xWaitForAllBits set to
pdFALSE ............................................................................................................ 283
Figure 74 The output produced when Example 22 is executed with xWaitForAllBits set to
pdTRUE .............................................................................................................. 284
Figure 75 The output produced when Example 23 is executed ............................................. 292
Figure 76 A communication object being used to send an event from one task to another .... 294
Figure 77 A task notification used to send an event directly from one task to another
xviii
List of Code Listings
xix
Table 10. vTaskDelayUntil() parameters ................................................................................ 71
Table 11. vTaskPrioritySet() parameters ................................................................................ 79
Table 12. uxTaskPriorityGet() parameters and return value ................................................... 80
Table 13. vTaskDelete() parameters ...................................................................................... 85
Table 14. The FreeRTOSConfig.h settings that configure the kernel to use Prioritized
Pre-emptive Scheduling with Time Slicing ............................................................ 91
xxiv
161204
1 6
7
11
161204 Pre-release
19
161204 Pre-release for FreeRTOS V8.x.x. See
Formatting
One
23
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
161204 Pre-release for FreeRTOS V8.x.x.
-for
The heap contains three blocks of free memory that, in the order in which they appear
in the array, are 5 bytes, 20.45 by45(w)15(hi)6(cETqQ EMC /P /< MCID 0>> BDC BT15545).n9aytes, ne te
33
35
161204
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
43
161204
3.1 Chapter Introduction and Scope
Scope
How FreeRTOS
45
161204 Pre-release for FreeRTOS V8.x.x. See
void ATaskFunction( void *pvParameters )
{
47
161204 Pre-release for FreeRTOS V8.x.x. See for
161204
161204 Pre-release
161204 Pre-release for FreeRTOS V8.x.x. See
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
Figure 12. The execution sequence expanded to show the tick interrupt executing
FreeRTOS API calls always specify time in multiples of tick periods, which are often referred to
simply
62
Section 3.12, Scheduling Algorithm
63
161204 Pre-c[( )] TJETBT1 0 0 4re
-ce
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
void vTaskFunction( void *pvParameters )
73
161204 Pre-release for FreeRTOS V8.x.x. See
Figure 21. The output produced when Example 7 is executed
79
81
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
161204
161204 Pre-release for FreeRTOS V8.x.x. See
1204
161204 Pre-release for FreeRTOS V8.x.x. F/-4(8)4(e)4(R)] TJETBT1 0 0 1 2348.21759.6 Tm[ 0 1 2g1 0 1 2G [(h)19(t-5( t-5( p)19(:-5( /)9 /)5( w6(Tw6(T
161204 Pre-
161204
161204 Pre-release for FreeRTOS V8.x.x. See
161204161204
161204
161204 Pre-release for
static void vReceiverTask( void *pvParameters )
{
/* Declare the variable that will hold the values received from the queue. */
int32_t lReceivedValue;
BaseType_t xStatus;
const TickType_t xTicksToWait = pdMS_TO_TICKS( 100 );
/* This task is
117
161204
119
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS
121
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
Listing 49 shows the implementation of the sending task. The sending task specifies a block
time of 100 milliseconds, ea
so 4(e)13(r)-9E-3(i)5(l)iseconds
it enters the Blocked state to wait for space to become available
each time the queue becomes full. It leaves the 4(e)13(r)-9 Tc[(it)] TJETBTss[(t)-4(] TJET6v58(c[(irl)13(rn58(c
122
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
Figure 36
125
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
Using a Queue to Send Different Types and Lengths of
129
161204 Pre-release
133
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html
xQueuePeek() has the same function parameters and return value as xQueueReceive
147
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freert infor
9
153
161204
Figure 41 The timer command queue being used by a software timer API function to
communicate with the RTOS daemon task
155
161204
161204 Pre-
Note: Never call xTimerStart() from an interrupt service routine. The interrupt-
161
The timers
165
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS
5.6 The Timer ID
Each software timer has an ID, which is a tag value that can be used by the application writer
for any purpose. The ID is stored in a void pointer (void *), so can store an integer value
directly,
167
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
5.8
175
177
161204 Pre-release for FreeRTOS V8.x.x. See
161204 Pre
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
If the priority of a task that is unblocked by a FreeRTOS API function is higher than the priority
of the task in the Running state then, in accordance with
187
161204 P r e - r e l e a s e f o r F r e e R T O S V 8 . x . x . S e e h t t p : / / w w
161204
ion on the same semaphore to unblock the task so that the required event
processing can proceed.
193
161204
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
161204 Pre-
161204 Pre-release for FreeRTOS V8.x.x. See
The xSemaphoreCreateCounting() API Function
FreeRTOS V9.0.0 also includes the xSemaphoreCreateCountingStatic() function, which allocates the memory
211
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
161204 Pre
161204 Pre-release for
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
Figure
220
6.7 Using
221
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9-3(.4te)4t/
161204
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
int main( void )
{
/* Before a queue can be used it must first be created. Create both queues used
by this example. One queue can hold variables of type uint32_t, the other queue
can hold variables of type char*. Both queues can hold a maximum of 10 items. A
real application should check the return values to ensure the queues have been
successfully created. */
227
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
229
161204 Pre-release for
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
V9.x.x. See https://www.freertos.org/FreeRTOS-V10.html for information about FreeRTOS V10.x.x.
236
1 6 1 2 0 4
161204 P r e - r e
for FreeRTOS port
241
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
It is safe for calls to vTaskSuspendAll() and xTaskResumeAll() to become nested, because the
kernel keeps a count of the nesting depth
243
161204 Pre-5dTJeas
251
1 6 1 2 0 4 P r e - r e l e a s e f o r F r e e R T O S V 8 . x . x . S e e h t t p :
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html for information about FreeRTOS
void vApplicationTickHook( void );
Listing 127. The name and prototype for a tick hook function
261
161204 Pre-release for FreeRTOS V8.x.x. S
See http://www.FreeRTOS.org/FreeRTOS-V9.html
oreeRTOS for infor
ttrg0 0 18.164(e2.62 0)-5ref*qQ.6 Tm[(r)5(e)46.10 0
void vApplicationTickHook( void )
{
static int iCount = 0;
263
161204 Pre-release for FreeRTOS V8.x.x. See
161204 Pre-release for FreeRTOS V8.x.x. See
8.2 Characteristics of an Event Group
269
EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,ETqq0 g77.664 737.76 463.9911 9ventG
273
161204
161204 Pre
1612 gs4
static void vEventBitReadingTask( void *pvParameters )
{
283
161204 Pre-
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS
9.2 Task Notifications; Benefits and Limitations
Using a task notification to send an even( )].52 TmTr.5271(dca)47(t)-4(v)13( )-69(t)-4(o )-67(a )-67(t)-4(as)13
297
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS
299
307
161204 Pre
Chapter 10
328
161204 Pre-
11.3 FreeRTOS+Trace
333
161204 Pre-release for
161204 Pre-release for
Table 55, uxTaskGetSystemState() parameters and return value
341
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
12.1 Chapter Introduction and Scope
357
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-
161204 Pre-release for FreeRTOS V8.x.x. See
routine that attempts to send to o
367
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html
Variable Names, 22
vPortDefineHeapRegions(), 36
vPortFree(), 27
371