first commit
This commit is contained in:
commit
e6a42e28e9
4355 changed files with 4626605 additions and 0 deletions
81
libraries/FreeRTOS/examples/Mutex/Mutex.ino
Normal file
81
libraries/FreeRTOS/examples/Mutex/Mutex.ino
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
Example of a FreeRTOS mutex
|
||||
https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
|
||||
*/
|
||||
|
||||
// Include Arduino FreeRTOS library
|
||||
#include <Arduino_FreeRTOS.h>
|
||||
|
||||
|
||||
// Include mutex support
|
||||
#include <semphr.h>
|
||||
|
||||
/*
|
||||
Declaring a global variable of type SemaphoreHandle_t
|
||||
|
||||
*/
|
||||
SemaphoreHandle_t mutex;
|
||||
|
||||
int globalCount = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
/**
|
||||
Create a mutex.
|
||||
https://www.freertos.org/CreateMutex.html
|
||||
*/
|
||||
mutex = xSemaphoreCreateMutex();
|
||||
if (mutex != NULL) {
|
||||
Serial.println("Mutex created");
|
||||
}
|
||||
|
||||
/**
|
||||
Create tasks
|
||||
*/
|
||||
xTaskCreate(TaskMutex, // Task function
|
||||
"Task1", // Task name for humans
|
||||
128,
|
||||
1000, // Task parameter
|
||||
1, // Task priority
|
||||
NULL);
|
||||
|
||||
xTaskCreate(TaskMutex, "Task2", 128, 1000, 1, NULL);
|
||||
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
|
||||
void TaskMutex(void *pvParameters)
|
||||
{
|
||||
TickType_t delayTime = *((TickType_t*)pvParameters); // Use task parameters to define delay
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/**
|
||||
Take mutex
|
||||
https://www.freertos.org/a00122.html
|
||||
*/
|
||||
if (xSemaphoreTake(mutex, 10) == pdTRUE)
|
||||
{
|
||||
Serial.print(pcTaskGetName(NULL)); // Get task name
|
||||
Serial.print(", Count read value: ");
|
||||
Serial.print(globalCount);
|
||||
|
||||
globalCount++;
|
||||
|
||||
Serial.print(", Updated value: ");
|
||||
Serial.print(globalCount);
|
||||
|
||||
Serial.println();
|
||||
/**
|
||||
Give mutex
|
||||
https://www.freertos.org/a00123.html
|
||||
*/
|
||||
xSemaphoreGive(mutex);
|
||||
}
|
||||
|
||||
vTaskDelay(delayTime / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue