freertos参考资料源码、官方文档等mastering_the_freertos_real_time_kernel-a_hands-on_tutorial_guide_第1页
freertos参考资料源码、官方文档等mastering_the_freertos_real_time_kernel-a_hands-on_tutorial_guide_第2页
freertos参考资料源码、官方文档等mastering_the_freertos_real_time_kernel-a_hands-on_tutorial_guide_第3页
freertos参考资料源码、官方文档等mastering_the_freertos_real_time_kernel-a_hands-on_tutorial_guide_第4页
freertos参考资料源码、官方文档等mastering_the_freertos_real_time_kernel-a_hands-on_tutorial_guide_第5页
已阅读5页,还剩392页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Mastering the FreeRTOS Real Time KernelThis is the 161204 copy which does not yet cover FreeRTOS V9.0.0 or low power tick-lessoperation.Check http:/www.FreeRTOS.org regularly for additional documentation andupdates to this book, and see http:/www.FreeRTOS.org/FreeRTOS-V9.html for information onFreeR

2、TOS V9.x.x. Applications created using FreeRTOS V9.x.x can allocate all kernel objects statically at compile time, removing the need to include a heap memory manager.This text is being provided for free. In return we ask that you use the business contactemail link on http:/www.FreeRTOS.org/contact t

3、o provide feedback, comments and corrections. Thank you.Mastering the FreeRTOS Real Time KernelA Hands-On Tutorial GuideRichard BarryPre-release 161204 Edition.All text, source code, and diagrams are the exclusive property of Real Time Engineers Ltd.unless otherwise noted inline. Real Time Engineers

4、 Ltd. 2016.All rights reserved.http:/www.FreeRTOS.org http:/www.FreeRTOS.org/plus http:/www.FreeRTOS.org/labsFreeRTOS, FreeRTOS.org and the FreeRTOS logo are trademarks of Real Time Engineers Ltd. OPENRTOS and SAFERTOS are trademarks of WITTENSTEIN Aerospace and Simulation Ltd. All other brands or p

5、roduct names are theproperty of their respective holders.To Caroline, India and Max.ContentsContentsixList of FiguresxviList of Code ListingsxixList of TablesxxiiiList of NotationxxviPreface1Multitasking in Small Embedded Systems2About FreeRTOS2Value Proposition3A Note About Terminology3Why Use a Re

6、al-time Kernel?3FreeRTOS Features5Licensing, and The FreeRTOS, OpenRTOS, and SafeRTOS Family6Included Source Files and Projects7Obtaining the Examples that Accompany this Book7Chapter 1The FreeRTOS Distribution91.1Chapter Introduction and Scope10Scope101.2Understanding the FreeRTOS Distribution11Def

7、inition: FreeRTOS Port11Building FreeRTOS11FreeRTOSConfig.h11The Official FreeRTOS Distribution12The Top Directories in the FreeRTOS Distribution12FreeRTOS Source Files Common to All Ports12FreeRTOS Source Files Specific to a Port14Header Files151.31.4Demo Applications16Creating a FreeRTOS Project18

8、Adapting One of the Supplied Demo Projects18Creating a New Project from Scratch191.5Data Types and Coding Style Guide21Data Types21Variable Names22Function Names22Formatting23ixMacro Names23Rationale for Excessive Type Casting24Chapter 2Heap Memory Management252.1Chapter Introduction and Scope26Prer

9、equisites26Dynamic Memory Allocation and its Relevance to FreeRTOS26Options for Dynamic Memory Allocation27Scope282.2Example Memory Allocation Schemes29From FreeRTOS V9.0.0 FreeRTOS applications can be completely staticallyallocated, removing the need to include a heap memory manager29Heap_129Heap_2

10、30Heap_332Heap_432Setting a Start Address for the Array Used By Heap_434Heap_535The vPortDefineHeapRegions() API Function362.3Heap Related Utility Functions41The xPortGetFreeHeapSize() API Function41The xPortGetMinimumEverFreeHeapSize() API Function41Malloc Failed Hook Functions42Chapter 3Task Manag

11、ement443.1Chapter Introduction and Scope45Scope4Task Functions46Top Level Task States47Creating Tasks48The xTaskCreate() API Function48Example 1. Creating tasks51Example 2. Using the task parameter553.53.6Task Priorities58Time Measurement and the Tick Interrupt60Example 3. Experimenting wi

12、th priorities623.7Expanding the Not Running State64The Blocked State64The Suspended State65The Ready State65Completing the State Transition Diagram65Example 4. Using the Blocked state to create a delay66The vTaskDelayUntil() API Function70Example 5. Converting the example tasks to use vTaskDelayUnti

13、l()71xExample 6. Combining blocking and non-blocking tasks723.8The Idle Task and the Idle Task Hook75Idle Task Hook Functions75Limitations on the Implementation of Idle Task Hook Functions76Example 7. Defining an idle task hook function763.9Changing the Priority of a Task79The vTaskPrioritySet() API

14、 Function79The uxTaskPriorityGet() API Function79Example 8. Changing task priorities803.10 Deleting a Task85The vTaskDelete() API Function85Example 9. Deleting tasks863.11 Thread Local Storage893.12 Scheduling Algorithms90A Recap of Task States and Events90Configuring the Scheduling Algorithm90Prior

15、itized Pre-emptive Scheduling with Time Slicing91Prioritized Pre-emptive Scheduling (without Time Slicing)95Co-operative Scheduling97Chapter 4Queue Management1014.1Chapter Introduction and Scope102Scope1024.2Characteristics of a Queue103Data Storage103Access by Multiple Tasks106Blocking on Queue Rea

16、ds106Blocking on Queue Writes106Blocking on Multiple Queues1074.3Using a Queue108The xQueueCreate() API Function108The xQueueSendToBack() and xQueueSendToFront() API Functions109The xQueueReceive() API Function111The uxQueueMessagesWaiting() API Function113Example 10. Blocking when receiving from a

17、queue1144.4Receiving Data From Multiple Sources119Example 11. Blocking when sending to a queue, and sending structures on a queue. 1204.5Working with Large or Variable Sized Data126Queuing Pointers126Using a Queue to Send Different Types and Lengths of Data1284.6Receiving From Multiple Queues131Queu

18、e Sets131The xQueueCreateSet() API Function132The xQueueAddToSet() API Function134xiThe xQueueSelectFromSet() API Function135Example 12. Using a Queue Set137More Realistic Queue Set Use Cases1414.7Using a Queue to Create a Mailbox143The xQueueOverwrite() API Function144The xQueuePeek() API Function1

19、45Chapter 5Software Timer Management1475.1Chapter Introduction and Scope148Scope1485.25.3Software Timer Callback Functions149Attributes and States of a Software Timer150Period of a Software Timer150One-shot and Auto-reload Timers150Software Timer States1515.4The Context of a Software Timer153The RTO

20、S Daemon (Timer Service) Task153The Timer Command Queue153Daemon Task Scheduling1545.5Creating and Starting a Software Timer158The xTimerCreate() API Function158The xTimerStart() API Function159Example 13. Creating one-shot and auto-reload timers1635.6The Timer ID166The vTimerSetTimerID() API Functi

21、on166The pvTimerGetTimerID() API Function166Example 14. Using the callback function parameter and the software timer ID1675.7Changing the Period of a Timer170The xTimerChangePeriod() API Function1705.8Resetting a Software Timer174The xTimerReset() API Function174Example 15. Resetting a software time

22、r176Chapter 6Interrupt Management1816.1Chapter Introduction and Scope182Events182Scope1836.2Using the FreeRTOS API from an ISR184The Interrupt Safe API184The Benefits of Using a Separate Interrupt Safe API184The Disadvantages of Using a Separate Interrupt Safe API185The xHigherPriorityTaskWoken Para

23、meter185The portYIELD_FROM_ISR() and portEND_SWITCHING_ISR() Macros1876.3Deferred Interrupt Processing189xii6.4Binary Semaphores Used for Synchronization191The xSemaphoreCreateBinary() API Function194The xSemaphoreTake() API Function194The xSemaphoreGiveFromISR() API Function196Example 16. Using a b

24、inary semaphore to synchronize a task with an interrupt198Improving the Implementation of the Task Used in Example 162026.5Counting Semaphores208The xSemaphoreCreateCounting() API Function210Example 17. Using a counting semaphore to synchronize a task with an interrupt2116.6Deferring Work to the RTO

25、S Daemon Task213The xTimerPendFunctionCallFromISR() API Function214Example 18. Centralized deferred interrupt processing2166.7Using Queues within an Interrupt Service Routine220The xQueueSendToFrontFromISR() and xQueueSendToBackFromISR() APIFunctions220Considerations When Using a Queue From an ISR22

26、2Example 19. Sending and receiving on a queue from within an interrupt2226.8Interrupt Nesting228A Note to ARM Cortex-M and ARM GIC Users230Chapter 7Resource Management2337.1Chapter Introduction and Scope234Mutual Exclusion236Scope2377.2Critical Sections and Suspending the Scheduler238Basic Critical

27、Sections238Suspending (or Locking) the Scheduler240The vTaskSuspendAll() API Function241The xTaskResumeAll() API Function2417.3Mutexes (and Binary Semaphores)243The xSemaphoreCreateMutex() API Function245Example 20. Rewriting vPrintString() to use a semaphore245Priority Inversion249Priority Inherita

28、nce250Deadlock (or Deadly Embrace)251Recursive Mutexes252Mutexes and Task Scheduling2557.4Gatekeeper Tasks259Example 21. Re-writing vPrintString() to use a gatekeeper task259Chapter 8Event Groups2658.1Chapter Introduction and Scope266Scope2668.2Characteristics of an Event Group268xiiiEvent Groups, E

29、vent Flags and Event Bits268More About the EventBits_t Data Type269Access by Multiple Tasks269A Practical Example of Using an Event Group2698.3Event Management Using Event Groups271The xEventGroupCreate() API Function271The xEventGroupSetBits() API Function271The xEventGroupSetBitsFromISR() API Func

30、tion272The xEventGroupWaitBits() API Function275Example 22. Experimenting with event groups2798.4Task Synchronization Using an Event Group285The xEventGroupSync() API Function287Example 23. Synchronizing tasks289Chapter 9Task Notifications2939.1Chapter Introduction and Scope294Communicating Through

31、Intermediary Objects294Task NotificationsDirect to Task Communication294Scope2959.2Task Notifications; Benefits and Limitations296Performance Benefits of Task Notifications296RAM Footprint Benefits of Task Notifications296Limitations of Task Notifications2969.3Using Task Notifications298Task Notific

32、ation API Options298The xTaskNotifyGive() API Function298The vTaskNotifyGiveFromISR() API Function299The ulTaskNotifyTake() API Function300Example 24. Using a task notification in place of a semaphore, method 1302Example 25. Using a task notification in place of a semaphore, method 2305The xTaskNoti

33、fy() and xTaskNotifyFromISR() API Functions307The xTaskNotifyWait() API Function310Task Notifications Used in Peripheral Device Drivers: UART Example313Task Notifications Used in Peripheral Device Drivers: ADC Example320Task Notifications Used Directly Within an Application322Chapter 10Chapter 11Low

34、 Power Support327Developer Support32811.1 Chapter Introduction and Scope32911.2 configASSERT()330Example configASSERT() definitions33011.3 FreeRTOS+Trace33211.4 Debug Related Hook (Callback) Functions336xivMalloc failed hook33611.5 Viewing Run-time and Task State Information337Task Run-Time Statisti

35、cs337The Run-Time Statistics Clock337Configuring an Application to Collect Run-Time Statistics338The uxTaskGetSystemState() API Function339The vTaskList() Helper Function342The vTaskGetRunTimeStats() Helper Function344Generating and Displaying Run-Time Statistics, a Worked Example34511.6 Trace Hook

36、Macros348Available Trace Hook Macros348Defining Trace Hook Macros352FreeRTOS Aware Debugger Plug-ins353Chapter 12Trouble Shooting355Chapter Introduction and Scope356Interrupt Priorities357Stack Overflow359The uxTaskGetStackHighWaterMark() API Function359Run Time Stack CheckingOverview360

37、Run Time Stack CheckingMethod 1360Run Time Stack CheckingMethod 236112.4 Inappropriate Use of printf() and sprintf()362Printf-stdarg.c36212.5 Other Common Sources of Error364Symptom: Adding a simple task to a demo causes the demo to crash364Symptom: Using an API function within an interrupt causes t

38、he application to crash364Symptom: Sometimes the application crashes within an interrupt service routine364Symptom: The scheduler crashes when attempting to start the first task365Symptom: Interrupts are unexpectedly left disabled, or critical sections do not nest correctly365Symptom: The applicatio

39、n crashes even before the scheduler is started365Symptom: Calling API functions while the scheduler is suspended, or from inside a critical section, causes the application to crash366INDEX368xvList of FiguresFigure 1.Figure 2.Figure 3.Figure 4.Figure 5.Figure 6.Top level directories within the FreeR

40、TOS distribution12Core FreeRTOS source files within the FreeRTOS directory tree13Port specific source files within the FreeRTOS directory tree14The demo directory hierarchy17RAM being allocated from the heap_1 array each time a task is created30RAM being allocated and freed from the heap_2 array as

41、tasks are createdand deleted31RAM being allocated and freed from the heap_4 array33Figure 7.Figure 8 Memory Map37Figure 9. Top level task states and transitions47Figure 10.Figure 11.Figure 12.Figure 13.Figure 14.Figure 15.Figure 16.Figure 17.The output produced when Example 1 is executed53The actual

42、 execution pattern of the two Example 1 tasks54The execution sequence expanded to show the tick interrupt executing61Running both tasks at different priorities63The execution pattern when one task has a higher priority than the other63Full task state machine66The output produced when Example 4 is ex

43、ecuted68The execution sequence when the tasks use vTaskDelay() in place of theNULL loop69Bold lines indicate the state transitions performed by the tasks in Example 470The output produced when Example 6 is executed74The execution pattern of Example 674The output produced when Example 7 is executed78

44、The sequence of task execution when running Example 883The output produced when Example 8 is executed84The output produced when Example 9 is executed87The execution sequence for example 988Execution pattern highlighting task prioritization and pre-emption in a hypothetical application in which each

45、task has been assigned a uniquepriority92Figure 18.Figure 19.Figure 20.Figure 21.Figure 22.Figure 23.Figure 24.Figure 25.Figure 26.Figure 27 Execution pattern highlighting task prioritization and time slicing in ahypothetical application in which two tasks run at the same priority94Figure 28 The exe

46、cution pattern for the same scenario as shown in Figure 27, but thistime with configIDLE_SHOULD_YIELD set to 195Figure 29 Execution pattern that demonstrates how tasks of equal priority can receivehugely different amounts of processing time when time slicing is not used96Figure 30 Execution pattern

47、demonstrating the behavior of the co-operative scheduler98Figure 31.Figure 32.Figure 33.Figure 34.An example sequence of writes to, and reads from a queue104The output produced when Example 10 is executed118The sequence of execution produced by Example 10118An example scenario where structures are s

48、ent on a queue119Figure 35 The output produced by Example 11123xviFigure 36. The sequence of execution produced by Example 11124Figure 37 The output produced when Example 12 is executed141Figure 38 The difference in behavior between one-shot and auto-reload software timers150Figure 39 Auto-reload so

49、ftware timer states and transitions152Figure 40 One-shot software timer states and transitions152Figure 41 The timer command queue being used by a software timer API function to communicate with the RTOS daemon task154Figure 42 The execution pattern when the priority of a task calling xTimerStart()

50、is abovethe priority of the daemon task154Figure 43 The execution pattern when the priority of a task calling xTimerStart() is belowthe priority of the daemon task156Figure 44 The output produced when Example 13 is executed165Figure 45 The output produced when Example 14 is executed169Figure 46 Star

51、ting and resetting a software timer that has a period of 6 ticks174Figure 47 The output produced when Example 15 is executed179Figure 48 Completing interrupt processing in a high priority task190Figure 49.Figure 50.Figure 51.Figure 52.Figure 53.Using a binary semaphore to implement deferred interrup

52、t processing191Using a binary semaphore to synchronize a task with an interrupt193The output produced when Example 16 is executed201The sequence of execution when Example 16 is executed202The scenario when one interrupt occurs before the task has finishedprocessing the first event204Figure 54 The sc

53、enario when two interrupts occur before the task has finishedprocessing the first event205Figure 55.Figure 56.Figure 57.Using a counting semaphore to count events209The output produced when Example 17 is executed212The output produced when Example 18 is executed218Figure 58 The sequence of execution

54、 when Example 18 is executed219Figure 59.Figure 60.Figure 61.The output produced when Example 19 is executed226The sequence of execution produced by Example 19227Constants affecting interrupt nesting behavior230Figure 62 How a priority of binary 101 is stored by a Cortex-M microcontroller thatimplem

55、ents four priority bits231Figure 63.Figure 64.Figure 65.Figure 66.Figure 67.Mutual exclusion implemented using a mutex244The output produced when Example 20 is executed248A possible sequence of execution for Example 20249A worst case priority inversion scenario250Priority inheritance minimizing the effect of priority inversion251Figure 68 A possible sequence of execution when tasks that have the same priority

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论