没有合适的资源?快使用搜索试试~ 我知道了~
首页深入理解FreeRTOS实时内核:实战指南
深入理解FreeRTOS实时内核:实战指南
需积分: 23 11 下载量 83 浏览量
更新于2024-07-16
收藏 3.88MB PDF 举报
"Mastering the FreeRTOS™ Real Time Kernel.pdf"
FreeRTOS是一个实时操作系统(RTOS)内核,由Real Time Engineers Ltd.拥有、开发和维护,主要用于深度嵌入式实时应用,这些应用通常在微控制器或小型微处理器上运行。FreeRTOS设计的目标是满足软实时和硬实时的需求。软实时要求是指在规定的时间内完成任务,但错过期限不会导致系统完全失效。相反,硬实时要求有严格的时间限制,一旦超时,系统将会彻底失败。FreeRTOS作为一个实时调度器,提供了一个基础,开发者可以在其上构建应用程序,确保它们能够满足严格的硬实时需求。
在FreeRTOS V9.0.0及以后的版本中,应用程序可以在编译时静态分配所有内核对象,这消除了对堆内存管理器的需求,从而简化了内存管理并可能提高系统的确定性。不过,注意,该书的161204副本并未涵盖FreeRTOS V9.0.0、V10.0.0以及低功耗无滴答(tick-less)操作。想要获取关于FreeRTOS V9.x.x和V10.x.x的最新信息,应访问官方网站http://www.FreeRTOS.org及其相关页面。
这本书《Mastering the FreeRTOS™ Real Time Kernel》提供了一手教程,旨在通过实践指导读者掌握FreeRTOS的使用。作者Richard Barry在预发布版161204中强调,所有的文本、源代码和图表都是RealTime Engineers Ltd.的独家财产,除非在文中另有说明。该书的目的是帮助开发者充分利用FreeRTOS的功能,优化实时嵌入式系统的性能。
为了获取反馈、评论和修正,作者鼓励用户利用网站http://www.FreeRTOS.org上的商业联系电子邮件链接进行沟通。同时,此书的提供是免费的,但使用时需尊重版权,所有权利归RealTime Engineers Ltd.所有。
FreeRTOS是一款强大的实时操作系统内核,适用于需要满足严格实时性要求的嵌入式系统,而这本书则为开发者提供了深入理解和有效利用FreeRTOS的宝贵资源。
xvi
List of Figures
Figure 1. Top level directories within the FreeRTOS distribution ............................................ 12
Figure 2. Core FreeRTOS source files within the FreeRTOS directory tree ............................ 13
Figure 3. Port specific source files within the FreeRTOS directory tree .................................. 14
Figure 4. The demo directory hierarchy .................................................................................. 17
Figure 5. RAM being allocated from the heap_1 array each time a task is created ................ 30
Figure 6. RAM being allocated and freed from the heap_2 array as tasks are created
and deleted ........................................................................................................... 31
Figure 7. RAM being allocated and freed from the heap_4 array ........................................... 33
Figure 8 Memory Map ............................................................................................................. 37
Figure 9. Top level task states and transitions........................................................................ 47
Figure 10. The output produced when Example 1 is executed ............................................... 53
Figure 11. The actual execution pattern of the two Example 1 tasks ...................................... 54
Figure 12. The execution sequence expanded to show the tick interrupt executing ............... 61
Figure 13. Running both tasks at different priorities ............................................................... 63
Figure 14. The execution pattern when one task has a higher priority than the other ............. 63
Figure 15. Full task state machine .......................................................................................... 66
Figure 16. The output produced when Example 4 is executed ............................................... 68
Figure 17. The execution sequence when the tasks use vTaskDelay() in place of the
NULL loop ............................................................................................................. 69
Figure 18. Bold lines indicate the state transitions performed by the tasks in Example 4 ...... 70
Figure 19. The output produced when Example 6 is executed ............................................... 74
Figure 20. The execution pattern of Example 6 ...................................................................... 74
Figure 21. The output produced when Example 7 is executed ............................................... 78
Figure 22. The sequence of task execution when running Example 8 .................................... 83
Figure 23. The output produced when Example 8 is executed ............................................... 84
Figure 24. The output produced when Example 9 is executed ............................................... 87
Figure 25. The execution sequence for example 9 ................................................................. 88
Figure 26. Execution pattern highlighting task prioritization and pre-emption in a
hypothetical application in which each task has been assigned a unique
priority ................................................................................................................... 92
Figure 27 Execution pattern highlighting task prioritization and time slicing in a
hypothetical application in which two tasks run at the same priority ...................... 94
Figure 28 The execution pattern for the same scenario as shown in Figure 27, but this
time with configIDLE_SHOULD_YIELD set to 1 .................................................... 95
Figure 29 Execution pattern that demonstrates how tasks of equal priority can receive
hugely different amounts of processing time when time slicing is not used ........... 96
Figure 30 Execution pattern demonstrating the behavior of the co-operative scheduler .......... 98
Figure 31. An example sequence of writes to, and reads from a queue ............................... 104
Figure 32. The output produced when Example 10 is executed ........................................... 118
Figure 33. The sequence of execution produced by Example 10 ......................................... 118
Figure 34. An example scenario where structures are sent on a queue ............................... 119
Figure 35 The output produced by Example 11 ..................................................................... 123
xvii
Figure 36. The sequence of execution produced by Example 11 ......................................... 124
Figure 37 The output produced when Example 12 is executed ............................................. 141
Figure 38 The difference in behavior between one-shot and auto-reload software timers ..... 150
Figure 39 Auto-reload software timer states and transitions .................................................. 152
Figure 40 One-shot software timer states and transitions ..................................................... 152
Figure 41 The timer command queue being used by a software timer API function to
communicate with the RTOS daemon task ......................................................... 154
Figure 42 The execution pattern when the priority of a task calling xTimerStart() is above
the priority of the daemon task ............................................................................ 154
Figure 43 The execution pattern when the priority of a task calling xTimerStart() is below
the priority of the daemon task ............................................................................ 156
Figure 44 The output produced when Example 13 is executed ............................................. 165
Figure 45 The output produced when Example 14 is executed ............................................. 169
Figure 46 Starting and resetting a software timer that has a period of 6 ticks ........................ 174
Figure 47 The output produced when Example 15 is executed ............................................. 179
Figure 48 Completing interrupt processing in a high priority task .......................................... 190
Figure 49. Using a binary semaphore to implement deferred interrupt processing ............... 191
Figure 50. Using a binary semaphore to synchronize a task with an interrupt ...................... 193
Figure 51. The output produced when Example 16 is executed ........................................... 201
Figure 52. The sequence of execution when Example 16 is executed ................................. 202
Figure 53. The scenario when one interrupt occurs before the task has finished
processing the first event .................................................................................... 204
Figure 54 The scenario when two interrupts occur before the task has finished
processing the first event .................................................................................... 205
Figure 55. Using a counting semaphore to ‘count’ events .................................................... 209
Figure 56. The output produced when Example 17 is executed ........................................... 212
Figure 57. The output produced when Example 18 is executed ........................................... 218
Figure 58 The sequence of execution when Example 18 is executed ................................... 219
Figure 59. The output produced when Example 19 is executed ........................................... 226
Figure 60. The sequence of execution produced by Example 19 ......................................... 227
Figure 61. Constants affecting interrupt nesting behavior .................................................... 230
Figure 62 How a priority of binary 101 is stored by a Cortex-M microcontroller that
implements four priority bits ................................................................................ 231
Figure 63. Mutual exclusion implemented using a mutex ..................................................... 244
Figure 64. The output produced when Example 20 is executed ........................................... 248
Figure 65. A possible sequence of execution for Example 20 .............................................. 249
Figure 66. A worst case priority inversion scenario .............................................................. 250
Figure 67. Priority inheritance minimizing the effect of priority inversion .............................. 251
Figure 68 A possible sequence of execution when tasks that have the same priority use
the same mutex .................................................................................................. 255
Figure 69 A sequence of execution that could occur if two instances of the task shown by
Listing 125 are created at the same priority ........................................................ 257
Figure 70. The output produced when Example 21 is executed ........................................... 264
Figure 71 Event flag to bit number mapping in a variable of type EventBits_t ....................... 268
xviii
Figure 72 An event group in which only bits 1, 4 and 7 are set, and all the other event
flags are clear, making the event group’s value 0x92 .......................................... 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 ........... 295
Figure 78. The output produced when Example 16 is executed ........................................... 304
Figure 79. The sequence of execution when Example 24 is executed ................................. 305
Figure 80. The output produced when Example 25 is executed ........................................... 307
Figure 81 The communication paths from the application tasks to the cloud server, and
back again .......................................................................................................... 323
Figure 82 FreeRTOS+Trace includes more than 20 interconnected views ............................ 332
Figure 83 FreeRTOS+Trace main trace view - one of more than 20 interconnected trace
views .................................................................................................................. 333
Figure 84 FreeRTOS+Trace CPU load view - one of more than 20 interconnected trace
views .................................................................................................................. 334
Figure 85 FreeRTOS+Trace response time view - one of more than 20 interconnected
trace views .......................................................................................................... 334
Figure 86 FreeRTOS+Trace user event plot view - one of more than 20 interconnected
trace views .......................................................................................................... 335
Figure 87 FreeRTOS+Trace kernel object history view - one of more than 20
interconnected trace views .................................................................................. 335
Figure 88 Example output generated by vTaskList() ............................................................. 344
Figure 89 Example output generated by vTaskGetRunTimeStats() ....................................... 345
Figure 90 FreeRTOS ThreadSpy Eclipse plug-in from Code Confidence Ltd. ....................... 353
xix
List of Code Listings
Listing 1. The template for a new main() function ................................................................... 18
Listing 2. Using GCC syntax to declare the array that will be used by heap_4, and place
the array in a memory section named .my_heap .................................................. 35
Listing 3. Using IAR syntax to declare the array that will be used by heap_4, and place
the array at the absolute address 0x20000000 ..................................................... 35
Listing 4. The vPortDefineHeapRegions() API function prototype .......................................... 36
Listing 5. The HeapRegion_t structure ................................................................................... 36
Listing 6. An array of HeapRegion_t structures that together describe the 3 regions of
RAM in their entirety ............................................................................................. 38
Listing 7. An array of HeapRegion_t structures that describe all of RAM2, all of RAM3,
but only part of RAM1 ........................................................................................... 39
Listing 8. The xPortGetFreeHeapSize() API function prototype .............................................. 41
Listing 9. The xPortGetMinimumEverFreeHeapSize() API function prototype ........................ 41
Listing 10. The malloc failed hook function name and prototype. ........................................... 42
Listing 11. The task function prototype ................................................................................... 46
Listing 12. The structure of a typical task function .................................................................. 46
Listing 13. The xTaskCreate() API function prototype ............................................................ 48
Listing 14. Implementation of the first task used in Example 1 ............................................... 52
Listing 15. Implementation of the second task used in Example 1 ......................................... 52
Listing 16. Starting the Example 1 tasks ................................................................................ 53
Listing 17. Creating a task from within another task after the scheduler has started .............. 55
Listing 18. The single task function used to create two tasks in Example 2 ............................ 56
Listing 19. The main() function for Example 2. ....................................................................... 57
Listing 20. Using the pdMS_TO_TICKS() macro to convert 200 milliseconds into an
equivalent time in tick periods ............................................................................... 61
Listing 21. Creating two tasks at different priorities ................................................................ 62
Listing 22. The vTaskDelay() API function prototype .............................................................. 67
Listing 23. The source code for the example task after the null loop delay has been
replaced by a call to vTaskDelay() ........................................................................ 68
Listing 24. vTaskDelayUntil() API function prototype .............................................................. 71
Listing 25. The implementation of the example task using vTaskDelayUntil() ........................ 72
Listing 26. The continuous processing task used in Example 6 .............................................. 73
Listing 27. The periodic task used in Example 6 .................................................................... 73
Listing 28. The idle task hook function name and prototype ................................................... 76
Listing 29. A very simple Idle hook function ........................................................................... 77
Listing 30. The source code for the example task now prints out the ulIdleCycleCount
value ..................................................................................................................... 77
Listing 31. The vTaskPrioritySet() API function prototype ...................................................... 79
Listing 32. The uxTaskPriorityGet() API function prototype .................................................... 79
Listing 33. The implementation of Task 1 in Example 8 ......................................................... 81
Listing 34. The implementation of Task 2 in Example 8 ......................................................... 82
Listing 35. The implementation of main() for Example 8 ......................................................... 83
xx
Listing 36. The vTaskDelete() API function prototype ............................................................. 85
Listing 37. The implementation of main() for Example 9 ......................................................... 86
Listing 38. The implementation of Task 1 for Example 9 ........................................................ 87
Listing 39. The implementation of Task 2 for Example 9 ........................................................ 87
Listing 40. The xQueueCreate() API function prototype ....................................................... 108
Listing 41. The xQueueSendToFront() API function prototype ............................................. 109
Listing 42. The xQueueSendToBack() API function prototype .............................................. 109
Listing 43. The xQueueReceive() API function prototype ..................................................... 112
Listing 44. The uxQueueMessagesWaiting() API function prototype .................................... 113
Listing 45. Implementation of the sending task used in Example 10. .................................... 115
Listing 46. Implementation of the receiver task for Example 10 ............................................ 116
Listing 47. The implementation of main() in Example 10 ...................................................... 117
Listing 48. The definition of the structure that is to be passed on a queue, plus the
declaration of two variables for use by the example ............................................ 120
Listing 49. The implementation of the sending task for Example 11 ..................................... 121
Listing 50. The definition of the receiving task for Example 11 ............................................. 122
Listing 51. The implementation of main() for Example 11 ..................................................... 123
Listing 52. Creating a queue that holds pointers ................................................................... 127
Listing 53. Using a queue to send a pointer to a buffer ......................................................... 127
Listing 54. Using a queue to receive a pointer to a buffer ..................................................... 127
Listing 55. The structure used to send events to the TCP/IP stack task in
FreeRTOS+TCP ................................................................................................. 128
Listing 56. Pseudo code showing how an IPStackEvent_t structure is used to send data
received from the network to the TCP/IP task ..................................................... 129
Listing 57. Pseudo code showing how an IPStackEvent_t structure is used to send the
handle of a socket that is accepting a connection to the TCP/IP task .................. 129
Listing 58. Pseudo code showing how an IPStackEvent_t structure is used to send a
network down event to the TCP/IP task .............................................................. 130
Listing 59. Pseudo code showing how an IPStackEvent_t structure is used to send a
network down to the TCP/IP task ........................................................................ 130
Listing 60. The xQueueCreateSet() API function prototype .................................................. 132
Listing 61. The xQueueAddToSet() API function prototype .................................................. 134
Listing 62. The xQueueSelectFromSet() API function prototype ........................................... 135
Listing 63. Implementation of main() for Example 12 ............................................................ 138
Listing 64. The sending tasks used in Example 12 ............................................................... 139
Listing 65. The receive task used in Example 12 .................................................................. 140
Listing 66. Using a queue set that contains queues and semaphores .................................. 142
Listing 67. A queue being created for use as a mailbox ....................................................... 144
Listing 68. The xQueueOverwrite() API function prototype ................................................... 144
Listing 69. Using the xQueueOverwrite() API function .......................................................... 145
Listing 70. The xQueuePeek() API function prototype .......................................................... 146
Listing 71. Using the xQueuePeek() API function ................................................................. 146
Listing 72. The software timer callback function prototype ................................................... 149
Listing 73. The xTimerCreate() API function prototype ......................................................... 158
剩余398页未读,继续阅读
2017-04-09 上传
2019-03-13 上传
2019-08-23 上传
2023-10-19 上传
2023-11-28 上传
2021-04-28 上传
2018-04-10 上传
2022-05-03 上传
MichaelNZ
- 粉丝: 6
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- SSM Java项目:StudentInfo 数据管理与可视化分析
- pyedgar:Python库简化EDGAR数据交互与文档下载
- Node.js环境下wfdb文件解码与实时数据处理
- phpcms v2.2企业级网站管理系统发布
- 美团饿了么优惠券推广工具-uniapp源码
- 基于红外传感器的会议室实时占用率测量系统
- DenseNet-201预训练模型:图像分类的深度学习工具箱
- Java实现和弦移调工具:Transposer-java
- phpMyFAQ 2.5.1 Beta多国语言版:技术项目源码共享平台
- Python自动化源码实现便捷自动下单功能
- Android天气预报应用:查看多城市详细天气信息
- PHPTML类:简化HTML页面创建的PHP开源工具
- Biovec在蛋白质分析中的应用:预测、结构和可视化
- EfficientNet-b0深度学习工具箱模型在MATLAB中的应用
- 2024年河北省技能大赛数字化设计开发样题解析
- 笔记本USB加湿器:便携式设计解决方案
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功