FreeRTOS Tutorial Book PIC32 Edition TOC
FreeRTOS Tutorial Book PIC32 Edition TOC
Richard Barry
Contents
List of Figures ........................................................................................................................... v
List of Code Listings ................................................................................................................vii
List of Tables ............................................................................................................................ x
List of Notation......................................................................................................................... xi
Preface
FreeRTOS and the PIC32 ....................................................................................... 1
Multitasking on a PIC32 Microcontroller ............................................................................... 2
An Introduction to Multitasking in Small Embedded Systems ........................................... 2
A Note About Terminology ............................................................................................... 2
Why Use a Real-time Kernel? .......................................................................................... 3
The PIC32 Port of FreeRTOS .......................................................................................... 5
The FreeRTOS, OpenRTOS, and SafeRTOS Family....................................................... 5
Using the Examples that Accompany this Book.................................................................... 8
Required Tools and Hardware ......................................................................................... 8
Opening the Example Workspaces ................................................................................ 10
Connecting the PIC32 Starter Kit ................................................................................... 10
Starting a Debug Session .............................................................................................. 10
Chapter 1 Task Management ............................................................................................... 13
1.1 Chapter Introduction and Scope ................................................................................ 14
Scope ............................................................................................................................ 14
1.2 Task Functions.......................................................................................................... 15
1.3 Top Level Task States .............................................................................................. 16
1.4 Creating Tasks .......................................................................................................... 17
The xTaskCreate() API Function .................................................................................... 17
Example 1. Creating tasks ............................................................................................. 20
Example 2. Using the task parameter ............................................................................ 23
1.5 Task Priorities ........................................................................................................... 26
Example 3. Experimenting with priorities ........................................................................ 27
1.6 Expanding the Not Running State............................................................................ 30
The Blocked State.......................................................................................................... 30
The Suspended State .................................................................................................... 31
The Ready State ............................................................................................................ 31
Completing the State Transition Diagram ....................................................................... 31
Example 4. Using the Blocked state to create a delay .................................................... 32
The vTaskDelayUntil() API Function .............................................................................. 35
Example 5. Converting the example tasks to use vTaskDelayUntil() .............................. 37
Example 6. Combining blocking and non-blocking tasks ................................................ 38
1.7 The Idle Task and the Idle Task Hook ....................................................................... 41
Idle Task Hook Functions............................................................................................... 41
i
Symptom: Adding a simple task to a demo causes the demo to crash ......................... 158
Symptom: Using an API function within an interrupt causes the application to crash.... 158
Symptom: Sometimes the application crashes within an interrupt service routine ........ 159
Symptom: Critical sections do not nest correctly .......................................................... 159
Symptom: The application crashes even before the scheduler is started...................... 159
Symptom: Calling API functions while the scheduler is suspended causes the
application to crash ............................................................................................... 159
Symptom: The prototype for pxPortInitialiseStack() causes compilation to fail............. 160
6.4 Execution VisualizationTrace Hook Macros ......................................................... 161
Available Trace Hook Macros....................................................................................... 161
Defining Trace Hook Macros ........................................................................................ 170
Example 17. Using Trace Hook Macros ....................................................................... 171
6.5 Execution VisualizationRun Time Statistics.......................................................... 176
Run Time Statistics Time Base .................................................................................... 176
Configuring an Application to Collect Run Time Statistics ............................................ 177
The vTaskGetRunTimeStats() API Function................................................................. 178
Example 18. Generating Run Time Statistics ............................................................... 179
Chapter 7 The FreeRTOS Download ................................................................................. 185
7.1 Chapter Introduction and Scope .............................................................................. 186
Scope........................................................................................................................... 186
7.2 Files and Directories................................................................................................ 187
Removing Unused Source Files ................................................................................... 188
7.3 Demo Applications .................................................................................................. 189
Removing Unused Demo Files ..................................................................................... 190
7.4 Creating a FreeRTOS Project.................................................................................. 191
Adapting One of the Supplied Demo Projects .............................................................. 191
Creating a New Project from Scratch ........................................................................... 192
Header Files................................................................................................................. 193
7.5 Data Types and Coding Style Guide........................................................................ 194
Data Types................................................................................................................... 194
Variable Names............................................................................................................ 195
Function Names ........................................................................................................... 195
Formatting .................................................................................................................... 195
Macro Names ............................................................................................................... 195
Rationale for Excessive Type Casting .......................................................................... 196
Appendix 1: Licensing Information ..................................................................................... 199
Open Source License Details ....................................................................................... 200
GPL Exception Text ..................................................................................................... 201
INDEX ................................................................................................................................... 203
iv
List of Figures
Figure 1. PIC32 USB Starter Kit II (Microchip part number DM320003-2) ................................ 8
Figure 2. Block diagram of the PIC32MX795F512L-80I/PT...................................................... 9
Figure 3. Selecting 'PIC32 Starter Kit' as the debug interface ................................................ 11
Figure 4. The 'Run' speed button (highlighted by the box with round corners) ....................... 11
Figure 5. Top level task states and transitions ....................................................................... 16
Figure 6. The output produced when Example 1 is executed ................................................. 21
Figure 7. The execution pattern of the two Example 1 tasks .................................................. 22
Figure 8. The execution sequence expanded to show the tick interrupt executing ................. 27
Figure 9. Running both test tasks at different priorities .......................................................... 28
Figure 10. The execution pattern when one task has a higher priority than the other ............. 29
Figure 11. Full task state machine ......................................................................................... 32
Figure 12. The output produced when Example 4 is executed ............................................... 34
Figure 13. The execution sequence when the tasks use vTaskDelay() in place of the
NULL loop........................................................................................................ 34
Figure 14. Bold lines indicate the state transitions performed by the tasks in Example 4 ...... 35
Figure 15. The output produced when Example 6 is executed ............................................... 39
Figure 16. The execution pattern of Example 6...................................................................... 40
Figure 17. The output produced when Example 7 is executed ............................................... 43
Figure 18. The sequence of task execution when running Example 8 .................................... 48
Figure 19. The output produced when Example 8 is executed ............................................... 49
Figure 20. The output produced when Example 9 is executed ............................................... 52
Figure 21. The execution sequence for Example 9 ................................................................ 53
Figure 22. Execution pattern with pre-emption points highlighted .......................................... 55
Figure 23. An example sequence of writes and reads to and from a queue ........................... 63
Figure 24. The output produced when Example 10 is executed ............................................. 75
Figure 25. The sequence of execution produced by Example 10 ........................................... 75
Figure 26. An example scenario where structures are sent on a queue ................................. 76
Figure 27. The output produced by Example 11 .................................................................... 80
Figure 28. The sequence of execution produced by Example 11 ........................................... 81
Figure 29. The interrupt interrupts one task but returns to another ......................................... 88
Figure 30. Using a binary semaphore to synchronize a task with an interrupt ........................ 93
Figure 31. The output produced when Example 12 is executed ........................................... 100
Figure 32. The sequence of execution when Example 12 is executed ................................. 101
Figure 33. A binary semaphore can latch at most one event ................................................ 103
Figure 34. Using a counting semaphore to count events .................................................... 104
Figure 35. The output produced when Example 13 is executed ........................................... 108
Figure 36. The output produced when Example 14 is executed ........................................... 115
Figure 37. The sequence of execution produced by Example 14 ......................................... 115
Figure 38. Constants affecting interrupt nesting behavior .................................................... 117
Figure 39. Mutual exclusion implemented using a mutex ..................................................... 129
v
Figure 40.
Figure 41.
Figure 42.
Figure 43.
Figure 44.
Figure 45.
Figure 46.
Figure 47.
Figure 48.
Figure 49.
Figure 50.
Figure 51.
Figure 52.
vi
Listing 40.
Listing 41.
Listing 42.
Listing 43.
Listing 44.
Listing 45.
Listing 46.
Listing 47.
Listing 48.
Listing 49.
Listing 50.
Listing 51.
Listing 52.
Listing 53.
Listing 54.
Listing 55.
Listing 56.
Listing 57.
Listing 58.
Listing 59.
Listing 60.
Listing 61.
Listing 62.
Listing 63.
Listing 64.
Listing 65.
Listing 66.
Listing 67.
Listing 68.
Listing 69.
Listing 70.
Listing 71.
Listing 72.
Listing 73.
Listing 74.
Listing 75.
Listing 76.
Listing 77.
Listing 78.
Listing 79.
Listing 80.
viii
Listing 81.
Listing 82.
Listing 83.
Listing 84.
Listing 85.
Listing 86.
Listing 87.
Listing 88.
ix
List of Tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Table 7.
Table 8,
Table 9.