FreeRTOS Course - Semaphore/Mutex Management

19,312 views

Published on

1 Comment
14 Likes
Statistics
Notes
No Downloads
Views
Total views
19,312
On SlideShare
0
From Embeds
0
Number of Embeds
3,215
Actions
Shares
0
Downloads
0
Comments
1
Likes
14
Embeds 0
No embeds

No notes for slide
  • * 07/16/96 * ##
  • Queues macros are in semphr.h while queues implementation is in queue.c. * 07/16/96 * ##
  • Note that there is no deletion API * 07/16/96 * ##
  • Note that xSemaphoreCreateMutex and xSemaphoreCreateRecursiveMutex are implemented as the same!! * 07/16/96 * ##
  • FreeRTOS Course - Semaphore/Mutex Management

    1. 1. Introduction to FreeRTOS V6.0.5
    2. 2. About SwiftACT <ul><li>A Technology services startup company </li></ul><ul><ul><li>Under establishment </li></ul></ul><ul><li>Areas of specialties: </li></ul><ul><ul><li>Mobile telecommunication services development </li></ul></ul><ul><ul><li>Embedded systems development </li></ul></ul><ul><li>Types of services: </li></ul><ul><ul><li>Consultation </li></ul></ul><ul><ul><li>Managed services </li></ul></ul><ul><ul><li>Sourcing </li></ul></ul><ul><ul><li>Training </li></ul></ul>Introduction to FreeRTOS V6.0.5 Amr Ali Abdel-Naby@2010
    3. 3. About Me <ul><li>Graduated 2004 </li></ul><ul><ul><li>ECE, ASU: 5 yrs distinction </li></ul></ul><ul><li>6+ years in embedded systems development </li></ul><ul><ul><li>SDLC, Apps, MW, DD, Porting, ... </li></ul></ul><ul><li>4+ years in SW engineering </li></ul><ul><ul><li>PSP, CMMI, Systematic reuse, ... </li></ul></ul><ul><li>4+ years in SW testing </li></ul><ul><ul><li>IBM certified, ISTQB certified, ... </li></ul></ul>Introduction to FreeRTOS V6.0.5 Amr Ali Abdel-Naby@2010
    4. 4. Copyright <ul><li>Materials in this course is the property of Amr Ali Abdel-Naby. </li></ul><ul><li>Reproduction or transmission of the materials outside [Customer Name] in any manner without the copyright owner permission is a law violation. </li></ul><ul><li>[Customer Name] is free to use this material as is within its head quarters for noncommercial uses. </li></ul><ul><li>Snapshots or trademarks used are retained to their original owners. </li></ul>Introduction to FreeRTOS V6.0.5 Amr Ali Abdel-Naby@2010
    5. 5. Course Objectives <ul><li>After completing this course, you will: </li></ul><ul><ul><li>Understand what is FreeRTOS </li></ul></ul><ul><ul><li>Experience different FreeRTOS services and APIs </li></ul></ul><ul><ul><li>Understand the porting process of FreeRTOS </li></ul></ul>Introduction to FreeRTOS V6.0.5 Amr Ali Abdel-Naby@2010
    6. 6. Course Notes <ul><li>I assume you are familiar with RTOS concepts. </li></ul><ul><li>I assume you know C. </li></ul><ul><li>I assume you know 80x86 architecture and assembly. </li></ul><ul><li>Ask any time. </li></ul>Introduction to FreeRTOS V6.0.5 Amr Ali Abdel-Naby@2010
    7. 7. Course References <ul><li>http://www.freertos.org </li></ul>Introduction to FreeRTOS V6.0.5 Amr Ali Abdel-Naby@2010
    8. 8. Outline <ul><li>Introduction to FreeRTOS </li></ul><ul><li>Lab 0: Getting Started </li></ul><ul><li>Kernel Structure </li></ul><ul><li>Lab 1: Kernel Structure </li></ul><ul><li>Task Management </li></ul><ul><li>Lab 2: Task Management </li></ul><ul><li>Queue Management </li></ul><ul><li>Lab 3: Queue Management </li></ul><ul><li>Semaphore/Mutex Management </li></ul><ul><li>Lab 4: Semaphore/Mutex Management </li></ul><ul><li>Co-routine Management </li></ul><ul><li>Lab 5: Co-routine Management </li></ul><ul><li>Advanced Features </li></ul><ul><li>Lab 6: Advanced Features </li></ul><ul><li>Porting FreeRTOS </li></ul><ul><li>Lab 7: Porting FreeRTOS </li></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    9. 9. Outline <ul><li>Introduction to FreeRTOS </li></ul><ul><li>Lab 0: Getting Started </li></ul><ul><li>Kernel Structure </li></ul><ul><li>Lab 1: Kernel Structure </li></ul><ul><li>Task Management </li></ul><ul><li>Lab 2: Task Management </li></ul><ul><li>Queue Management </li></ul><ul><li>Lab 3: Queue Management </li></ul><ul><li>Semaphore/Mutex Management </li></ul><ul><li>Lab 4: Semaphore/Mutex Management </li></ul><ul><li>Co-routine Management </li></ul><ul><li>Lab 5: Co-routine Management </li></ul><ul><li>Advanced Features </li></ul><ul><li>Lab 6: Advanced Features </li></ul><ul><li>Porting FreeRTOS </li></ul><ul><li>Lab 7: Porting FreeRTOS </li></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    10. 10. Binary Semaphore <ul><li>Used for mutual exclusion and synchronization </li></ul><ul><ul><li>Critical Section </li></ul></ul><ul><ul><ul><li>Initial count = 1 </li></ul></ul></ul><ul><ul><ul><li>wait_sem to enter critical section </li></ul></ul></ul><ul><ul><ul><li>signal_sem to leave critical section </li></ul></ul></ul><ul><ul><li>Synchronization </li></ul></ul><ul><ul><ul><li>Initial count = 0 </li></ul></ul></ul><ul><ul><ul><li>wait_sem to wait </li></ul></ul></ul><ul><ul><ul><li>signal_sem to signal </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5 HW Device 1 ISR 1 SW Task
    11. 11. Counting Semaphore <ul><li>Used for resource limit and credit tracking </li></ul><ul><ul><li>Resource limit </li></ul></ul><ul><ul><ul><li>Initial count = # of equivalent resources </li></ul></ul></ul><ul><ul><ul><li>wait_sem to gain access to a resource </li></ul></ul></ul><ul><ul><ul><li>signal_sem to release a resource </li></ul></ul></ul><ul><ul><li>Credit tracking </li></ul></ul><ul><ul><ul><li>Initial count = 0 </li></ul></ul></ul><ul><ul><ul><li>signal_sem to increment (Credit) </li></ul></ul></ul><ul><ul><ul><li>wait_sem to consume (Debit) </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    12. 12. Mutex <ul><li>A special type of binary semaphore </li></ul><ul><ul><li>Preferred over binary semaphore for mutual exclusion </li></ul></ul><ul><li>Mutex vs. Semaphore </li></ul><ul><ul><li>Mutex ownership </li></ul></ul><ul><ul><li>Recursive locking </li></ul></ul><ul><ul><li>Minimize priority inversion using inheritance protocol </li></ul></ul><ul><ul><li>Can’t be used from ISRs </li></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    13. 13. More About Semaphore/Mutex <ul><li>Same queues’ implementation APIs are used to implement semaphores/mutexes. </li></ul><ul><ul><li>Their APIs are macros that point to queues’ implementation APIs. </li></ul></ul><ul><ul><li>Queues and semaphores/mutexes APIs are inter-operable. </li></ul></ul><ul><ul><ul><li>You should be careful if your are going to do this. </li></ul></ul></ul><ul><ul><li>Binary semaphore/Mutex = A queue that can hold 1 item of data size 0 </li></ul></ul><ul><ul><ul><li>Either full or empty  Binary </li></ul></ul></ul><ul><ul><li>Counting semaphore = A queue that can hold n items each of data size 0 </li></ul></ul><ul><li>You can extend their APIs by defining new macros to the queue’s implementation APIs. </li></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    14. 14. Semaphore/Mutex Management APIs <ul><li>They fall under 4 categories: </li></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5 Creation vSemaphoreCreateBinary xSemaphoreCreateCounting xSemaphoreCreateMutex xSemaphoreCreateRecursiveMutex Fully Featured xSemaphoreTake xSemaphoreTakeRecursive xSemaphoreGive xSemaphoreGiveRecursive Light Weight xSemaphoreGiveFromISR Alternative xSemaphoreAltTake xSemaphoreAltGive
    15. 15. Creating a Binary Semaphore, vSemaphoreCreateBinary <ul><li>vSemaphoreCreateBinary(xSemaphoreHandle xSemaphore) </li></ul><ul><ul><li>xSemaphore: Handle to the created semaphore </li></ul></ul><ul><ul><ul><li>!NULL if created </li></ul></ul></ul><ul><ul><ul><li>NULL if failed </li></ul></ul></ul><ul><ul><li>Note the initial binary semaphore value is 1 </li></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    16. 16. Creating a Counting Semaphore, xSemaphoreCreateCounting <ul><li>xSemaphoreHandle xSemaphoreCreateCounting(unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount ) </li></ul><ul><ul><li>uxMaxCount: The maximum count value that can be reached </li></ul></ul><ul><ul><li>uxInitialCount: The count value assigned to the semaphore when it is created </li></ul></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>!NULL if created </li></ul></ul></ul><ul><ul><ul><li>NULL if failed </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    17. 17. Creating a Mutex, xSemaphoreCreateMutex <ul><li>xSemaphoreHandle xSemaphoreCreateMutex(void) </li></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>!NULL if created </li></ul></ul></ul><ul><ul><ul><li>NULL if failed </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    18. 18. Creating a Recursive Mutex, xSemaphoreCreateRecursiveMutex <ul><li>xSemaphoreHandle xSemaphoreCreateRecursiveMutex(void) </li></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>!NULL if created </li></ul></ul></ul><ul><ul><ul><li>NULL if failed </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    19. 19. Obtaining a Semaphore/Mutex, xSemaphoreTake <ul><li>xSemaphoreTake(xSemaphoreHandle xSemaphore, portTickType xBlockTime) </li></ul><ul><ul><li>xSemaphore: A handle to the semaphore/mutex to be obtained </li></ul></ul><ul><ul><ul><li>Created with vSemaphoreCreateBinary, xSemaphoreCreateMutex or xSemaphoreCreateCounting </li></ul></ul></ul><ul><ul><li>xBlockTime: The time in ticks to wait for the semaphore/mutex to become available </li></ul></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>pdTRUE if the semaphore/mutex was obtained </li></ul></ul></ul><ul><ul><ul><li>Otherwise pdFALSE </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    20. 20. Obtaining a Recursive Mutex, xSemaphoreTakeRecursive <ul><li>xSemaphoreTake(RecursivexSemaphoreHandle xSemaphore, portTickType xBlockTime) </li></ul><ul><ul><li>xSemaphore: A handle to the mutex to be obtained </li></ul></ul><ul><ul><ul><li>Created with xSemaphoreCreaterecursiveMutex </li></ul></ul></ul><ul><ul><li>xBlockTime: The time in ticks to wait for the mutex to become available </li></ul></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>pdTRUE if the mutex was obtained </li></ul></ul></ul><ul><ul><ul><li>Otherwise pdFALSE </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    21. 21. Releasing a Semaphore/Mutex, xSemaphoreGive <ul><li>xSemaphoreGive(xSemaphoreHandle xSemaphore) </li></ul><ul><ul><li>xSemaphore: A handle to the semaphore/mutex to be released </li></ul></ul><ul><ul><ul><li>Created with vSemaphoreCreateBinary, xSemaphoreCreateMutex or xSemaphoreCreateCounting </li></ul></ul></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>pdTRUE if the semaphore/mutex was released </li></ul></ul></ul><ul><ul><ul><li>Otherwise pdFALSE </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    22. 22. Releasing a Recursive Mutex, xSemaphoreGiveRecursive <ul><li>xSemaphoreGiveRecursive(xSemaphoreHandle xMutex) </li></ul><ul><ul><li>xSemaphore: A handle to the mutex to be released </li></ul></ul><ul><ul><ul><li>Created with xSemaphoreCreaterecursiveMutex </li></ul></ul></ul><ul><ul><li>Return value: </li></ul></ul><ul><ul><ul><li>pdTRUE if the mutex was released </li></ul></ul></ul><ul><ul><ul><li>Otherwise pdFALSE </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    23. 23. Releasing a Semaphore From ISR, xSemaphoreGiveFromISR <ul><li>xSemaphoreGiveFromISR(xSemaphoreHandle xSemaphore, signed portBASE_TYPE * pxHigherPriorityTaskWoken) </li></ul><ul><ul><li>= xSemaphoreGive but used within ISRs </li></ul></ul><ul><ul><li>pxTaskWoken : Is set to pdTRUE if releasing caused a higher priority task to unblock </li></ul></ul><ul><ul><ul><li>A context switch is needed after ISR exits. </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    24. 24. Alternative Obtaining a Semaphore/Mutex, xSemaphoreAltTake <ul><li>xSemaphoreAltTake(xSemaphoreHandle xSemaphore, portTickType xBlockTime) </li></ul><ul><ul><li>= xSemaphoreTake but with alternative implementation </li></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    25. 25. Alternative Releasing a Semaphore/Mutex, xSemaphoreAltGive <ul><li>xSemaphoreAltGive(xSemaphoreHandle xSemaphore) </li></ul><ul><ul><li>= xSemaphoreGive but with alternative implementation </li></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    26. 26. More About Semaphore/Mutex Revisited <ul><li>Always remember that you can extend semaphores/mutexes with queues’ implementation APIs. </li></ul><ul><li>There is no deletion API!!! </li></ul><ul><ul><li>Possible candidates that can be used are vQueueDelete and vPortFree. </li></ul></ul><ul><ul><li>Before using any, you have to check: </li></ul></ul><ul><ul><ul><li>Configuration for the queue registry functions </li></ul></ul></ul><ul><ul><ul><li>Port memory management functions </li></ul></ul></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    27. 27. Outline <ul><li>Introduction to FreeRTOS </li></ul><ul><li>Lab 0: Getting Started </li></ul><ul><li>Kernel Structure </li></ul><ul><li>Lab 1: Kernel Structure </li></ul><ul><li>Task Management </li></ul><ul><li>Lab 2: Task Management </li></ul><ul><li>Queue Management </li></ul><ul><li>Lab 3: Queue Management </li></ul><ul><li>Semaphore/Mutex Management </li></ul><ul><li>Lab 4: Semaphore/Mutex Management </li></ul><ul><li>Co-routine Management </li></ul><ul><li>Lab 5: Co-routine Management </li></ul><ul><li>Advanced Features </li></ul><ul><li>Lab 6: Advanced Features </li></ul><ul><li>Porting FreeRTOS </li></ul><ul><li>Lab 7: Porting FreeRTOS </li></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5
    28. 28. Lab4: Semaphore/Mutex Management <ul><li>Please follow the instructions in the lab handout </li></ul>Amr Ali Abdel-Naby@2010 Introduction to FreeRTOS V6.0.5

    ×