Skip to content

Commit 98cafc0

Browse files
author
Michael Polia
committed
Modifications to demonstrate FreeRTOS task creation and mutex operations running on an AdaFruit ItsyBitsy nRF52840 Express board.
1 parent 5bcf511 commit 98cafc0

File tree

1 file changed

+69
-50
lines changed

1 file changed

+69
-50
lines changed

examples/Mutex/Mutex.ino

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
44
*/
55

6-
// Include Arduino FreeRTOS library
7-
#include <Arduino_FreeRTOS.h>
6+
#include <Arduino.h>
7+
#include <Adafruit_TinyUSB.h> // for Serial
88

99

1010
// Include mutex support
@@ -20,62 +20,81 @@ int globalCount = 0;
2020

2121
void setup() {
2222

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);
4524

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
4728

48-
void loop() {}
29+
Serial.println("STARTING THE APPLICATION.");
4930

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+
}
5342

54-
for (;;)
55-
{
5643
/**
57-
Take mutex
58-
https://www.freertos.org/a00122.html
44+
Create tasks.
45+
Relative task periodicity is offset by 200 mSec.
5946
*/
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+
}
6556

66-
globalCount++;
57+
void loop() {
58+
Serial.println("...looping...");
59+
delay(1000);
60+
}
6761

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
7065

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);
7871

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+
}
81100
}

0 commit comments

Comments
 (0)