Task Synchronization in Embedded Systems Lab

cs4101 introduction to embedded systems n.w
1 / 11
Embed
Share

Learn about implementing interrupt-driven controls using binary semaphores, utilizing mutex for exclusive resource access, and communicating data between tasks using counting semaphores in an embedded systems lab at National Tsing Hua University, Taiwan.

  • Embedded Systems
  • Synchronization
  • Semaphores
  • FreeRTOS
  • Lab

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS4101 Introduction to Embedded Systems Lab 12: Task Synchronization Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan National Tsing Hua University

  2. Introduction In this lab , we will learn How to implement interrupt-driven controls in FreeRTOSusing binary semaphores How to use Mutex to ensure exclusive accesses to system resources How to use counting semaphoresto communicate data between tasks 1 National Tsing Hua University

  3. Binary Semaphore Example vSemaphoreHandle binary_sem; //Global handler void one_sec_isr(void){ // an ISR xSemaphoreGiveFromISR(binary_sem, NULL); } void sem_task(void *p){ while(1) if(xSemaphoreTake(binary_sem,999999)) puts("Tick!"); } int main(void){ vSemaphoreCreateBinary(binary_sem); xTaskCreate(sem_task, (signed char*)) "t1", 2048, NULL, 1, NULL); vTaskStartScheduler(); return 0; } 2 National Tsing Hua University

  4. Mutex Example (1/3) Two tasks attempt to gain exclusive accesses to a precious resource xSemaphoreHandle gatekeeper = 0; //Global handler int main(void){ gatekeeper = xSemaphoreCreateMutex(); /*Create tasks with priority 1 for both users*/ xTaskCreate(user_1, (signed char*)) "t1", 1024, NULL, 1, NULL); xTaskCreate(user_2, (signed char*)) "t2", 1024, NULL, 1, NULL); vTaskStartScheduler(); return 0; } 3 National Tsing Hua University

  5. Mutex Example (2/3) void user_1(void *p){ while(1){ if(xSemaphoreTake(gatekeeper, 1000)){ puts("User 1 got access"); access_precious_resource(); // critical section xSemaphoreGive(gatekeeper); } else{ puts("User 1 failed to get access within 1000ms"); } vTaskDelay(1000); // or do other works /*Without delay, user 1 will get key immediately after releasing the key */ } } 4 National Tsing Hua University

  6. Mutex Example (3/3) void user_2(void *p){ while(1){ if(xSemaphoreTake(gatekeeper, 1000)){ puts("User 2 got access"); access_precious_resource(); // critical section xSemaphoreGive(gatekeeper); } else{ puts("User 2 failed to get access within 1000ms"); } vTaskDelay(1000); /*Without delay, user 2 will get key immediately after releasing the key */ } } 5 National Tsing Hua University

  7. Counting Semaphore Example (1/2) xSemaphoreHandle count_sem; //Global Handler int main(void){ /**parameter for uxMaxCount, uxInitialCount/ count_sem = xSemaphoreCreateCounting(2, 2); /*Create tasks with priority 1 for both users*/ xTaskCreate(task1, (signed char*)) t1", 1024, NULL, 1, NULL); xTaskCreate(task2, (signed char*)) t2", 1024, NULL, 1, NULL); vTaskStartScheduler(); return 0; } 6 National Tsing Hua University

  8. Counting Semaphore Example (2/2) void task1(void *p){ while(1){ if(xSemaphoreTake(count_sem, portMAX_DELAY)){ xSemaphoreGive(count_sem); } vTaskDelay(3000); } } void task2(void *p){ while(1){ if(xSemaphoreTake(count_sem), portMAX_DELAY){ xSemaphoreGive(count_sem); } vTaskDelay(3000); } } 7 National Tsing Hua University

  9. Lab: Basic 1 Modify Basic 2 of Lab 11 so that Task 3 is interrupt- driven Whenever light changes (from bright to dark and vice versa) on NuMaker TRIO, an interrupt is triggered If the light changes from bright to dark, then a FreeRTOS task will print Dark on the display If the light changes from dark to bright, then a FreeRTOS task will print Bright on the display Remember to clear the display before you print 8 National Tsing Hua University

  10. Lab: Basic 2 Extend Basic 1 to allow exclusive accesses of the tasks to the LCD display Create a Mutex to guard the LCD so that only one task can print texts to the LCD display at any time Note: Texts should be displayed at the same position of the LCD You may like to delay the tasks for a while after they printed to the LCD so that the race condition is obvious What happen if the light is blocked very swiftly? Will your program miss the light change events? 9 National Tsing Hua University

  11. Lab: Basic 3 Create 3 sender tasks, 1 receiver task and 2 buffers Each buffer is an array that can hold 10 characters Sender task 1 will fill an empty buffer with aaaaa and bbbbb alternatively; sender task 2 with texts ccccc and ddddd alternatively; and sender task 3 with eeeee and fffff alternatively The receiver task will read the text from the filled buffer and print it on the LCD display Use counting semaphores to allow data communicated between sender and receiver tasks 10 National Tsing Hua University

More Related Content