|
1 |
| -/* |
2 |
| - This example demonstrates basic usage of FreeRTOS Semaphores and queue sets for coordination between tasks for multi threading. |
3 |
| - Please refer to other examples in this folder to better understand usage of tasks. |
4 |
| - It is also advised to read documentation on FreeRTOS web pages: |
5 |
| - https://www.freertos.org/a00106.html |
| 1 | +// Please read file README.md in the folder containing this example. |
6 | 2 |
|
7 |
| - Theory: |
8 |
| - Semaphore is in essence a variable. Tasks can set the value, wait until one or more |
9 |
| - semaphores are set and thus communicate between each other their state. |
10 |
| - A binary semaphore is a semaphore that has a maximum count of 1, hence the 'binary' name. |
11 |
| - A task can only 'take' the semaphore if it is available, and the semaphore is only available if its count is 1. |
12 |
| -
|
13 |
| - Semaphore can be controlled from any number of tasks. If you use semaphore as a one-way |
14 |
| - signalization with only one task giving and only one task taking there is much faster option |
15 |
| - called Task Notifications - please see FreeRTOS documentation read more about them: https://www.freertos.org/RTOS-task-notifications.html |
16 |
| -
|
17 |
| - The example: |
18 |
| - We'll use a semaphore to signal when a package is delivered to a warehouse by multiple |
19 |
| - delivery trucks, and multiple workers are waiting to receive the package. |
20 |
| -*/ |
21 | 3 | #include <Arduino.h>
|
22 | 4 |
|
23 |
| - |
24 | 5 | SemaphoreHandle_t package_delivered_semaphore;
|
25 | 6 |
|
26 | 7 | void delivery_truck_task(void *pvParameters) {
|
@@ -51,21 +32,19 @@ void warehouse_worker_task(void *pvParameters) {
|
51 | 32 |
|
52 | 33 | void setup() {
|
53 | 34 | Serial.begin(115200);
|
| 35 | + while(!Serial){ delay(100); } |
54 | 36 | // Create the semaphore
|
55 | 37 | package_delivered_semaphore = xSemaphoreCreateCounting(10, 0);
|
56 | 38 |
|
57 | 39 | // Create multiple delivery truck tasks
|
58 | 40 | for (int i = 0; i < 5; i++) {
|
59 |
| - xTaskCreate(delivery_truck_task, "Delivery Truck", 1024, (void *)i, tskIDLE_PRIORITY, NULL); |
| 41 | + xTaskCreate(delivery_truck_task, "Delivery Truck", 512, (void *)i, tskIDLE_PRIORITY, NULL); |
60 | 42 | }
|
61 | 43 |
|
62 | 44 | // Create multiple warehouse worker tasks
|
63 | 45 | for (int i = 0; i < 3; i++) {
|
64 |
| - xTaskCreate(warehouse_worker_task, "Warehouse Worker", 1024, (void *)i, tskIDLE_PRIORITY, NULL); |
| 46 | + xTaskCreate(warehouse_worker_task, "Warehouse Worker", 512, (void *)i, tskIDLE_PRIORITY, NULL); |
65 | 47 | }
|
66 |
| - |
67 |
| - // Start the RTOS scheduler |
68 |
| - vTaskStartScheduler(); |
69 | 48 | }
|
70 | 49 |
|
71 | 50 | void loop() {
|
|
0 commit comments