3
3
https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
4
4
*/
5
5
6
- // Include Arduino FreeRTOS library
7
- #include < Arduino_FreeRTOS .h>
6
+ # include < Arduino.h >
7
+ #include < Adafruit_TinyUSB .h> // for Serial
8
8
9
9
10
10
// Include mutex support
@@ -20,62 +20,81 @@ int globalCount = 0;
20
20
21
21
void setup () {
22
22
23
- Serial.begin (9600 );
24
-
25
- /* *
26
- Create a mutex.
27
- https://www.freertos.org/CreateMutex.html
28
- */
29
- mutex = xSemaphoreCreateMutex ();
30
- if (mutex != NULL ) {
31
- Serial.println (" Mutex created" );
32
- }
33
-
34
- /* *
35
- Create tasks
36
- */
37
- xTaskCreate (TaskMutex, // Task function
38
- " Task1" , // Task name for humans
39
- 128 ,
40
- 1000 , // Task parameter
41
- 1 , // Task priority
42
- NULL );
43
-
44
- xTaskCreate (TaskMutex, " Task2" , 128 , 1000 , 1 , NULL );
23
+ Serial.begin (115200 );
45
24
46
- }
25
+ // Wait for a serial port connection to be established before continuing.
26
+ // Don't want to miss any debug messages.
27
+ while ( !Serial ) delay (10 ); // for nrf52840 with native usb
47
28
48
- void loop () {}
29
+ Serial. println ( " STARTING THE APPLICATION. " );
49
30
50
- void TaskMutex (void *pvParameters)
51
- {
52
- TickType_t delayTime = *((TickType_t*)pvParameters); // Use task parameters to define delay
31
+ /* *
32
+ Create a mutex.
33
+ https://www.freertos.org/CreateMutex.html
34
+ */
35
+ mutex = xSemaphoreCreateMutex ();
36
+ if (mutex != NULL ) {
37
+ Serial.println (" Mutex created" );
38
+ }
39
+ else {
40
+ Serial.println (" ERROR! Mutex creation failed!" );
41
+ }
53
42
54
- for (;;)
55
- {
56
43
/* *
57
- Take mutex
58
- https://www.freertos.org/a00122.html
44
+ Create tasks.
45
+ Relative task periodicity is offset by 200 mSec.
59
46
*/
60
- if (xSemaphoreTake (mutex, 10 ) == pdTRUE)
61
- {
62
- Serial.print (pcTaskGetName (NULL )); // Get task name
63
- Serial.print (" , Count read value: " );
64
- Serial.print (globalCount);
47
+ xTaskCreate (TaskMutex, // Task function
48
+ " Task1" , // Task name for humans
49
+ 128 ,
50
+ (void *)900 , // Task parameter
51
+ 1 , // Task priority
52
+ NULL );
53
+
54
+ xTaskCreate (TaskMutex, " Task2" , 128 , (void *)1100 , 1 , NULL );
55
+ }
65
56
66
- globalCount++;
57
+ void loop () {
58
+ Serial.println (" ...looping..." );
59
+ delay (1000 );
60
+ }
67
61
68
- Serial.print (" , Updated value: " );
69
- Serial.print (globalCount);
62
+ void TaskMutex (void *pvParameters)
63
+ {
64
+ TickType_t delayTime = ((TickType_t)pvParameters); // Use task parameters to define delay
70
65
71
- Serial.println ();
72
- /* *
73
- Give mutex
74
- https://www.freertos.org/a00123.html
75
- */
76
- xSemaphoreGive (mutex);
77
- }
66
+ Serial.print (" Starting task " );
67
+ Serial.print (pcTaskGetName (NULL )); // Get task name
68
+ Serial.print (" with delay " );
69
+ Serial.println (delayTime);
70
+ delay (1000 );
78
71
79
- vTaskDelay (delayTime / portTICK_PERIOD_MS);
80
- }
72
+ for (;;)
73
+ {
74
+ /* *
75
+ Take mutex
76
+ https://www.freertos.org/a00122.html
77
+ */
78
+ if (xSemaphoreTake (mutex, 10 ) == pdTRUE)
79
+ {
80
+ Serial.print (pcTaskGetName (NULL )); // Get task name
81
+ Serial.print (" , Count read value: " );
82
+ Serial.print (globalCount);
83
+
84
+ globalCount++;
85
+
86
+ Serial.print (" , Updated value: " );
87
+ Serial.print (globalCount);
88
+
89
+ Serial.println ();
90
+ /* *
91
+ Give mutex
92
+ https://www.freertos.org/a00123.html
93
+ */
94
+ xSemaphoreGive (mutex);
95
+ }
96
+
97
+ // Add '1' to prevent divide by zero compilation error.
98
+ vTaskDelay (delayTime / (1 + portTICK_PERIOD_MS));
99
+ }
81
100
}
0 commit comments