-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4447b3c
commit 36a0c6f
Showing
5 changed files
with
380 additions
and
8 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
05-queue/esp32-freertos-05-demo-queue/esp32-freertos-05-demo-queue.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* FreeRTOS Queue Demo | ||
* | ||
* Send data to the serial terminal using a queue. | ||
* | ||
* Date: January 19, 2020 | ||
* Author: Shawn Hymel | ||
* License: 0BSD | ||
*/ | ||
|
||
// Use only core 1 for demo purposes | ||
#if CONFIG_FREERTOS_UNICORE | ||
static const BaseType_t app_cpu = 0; | ||
#else | ||
static const BaseType_t app_cpu = 1; | ||
#endif | ||
|
||
// Settings | ||
static const uint8_t msg_queue_len = 5; | ||
|
||
// Globals | ||
static QueueHandle_t msg_queue; | ||
|
||
//***************************************************************************** | ||
// Tasks | ||
|
||
// Task: wait for item on queue and print it | ||
void printMessages(void *parameters) { | ||
|
||
int item; | ||
|
||
// Loop forever | ||
while (1) { | ||
|
||
// See if there's a message in the queue (do not block) | ||
if (xQueueReceive(msg_queue, (void *)&item, 0) == pdTRUE) { | ||
//Serial.println(item); | ||
} | ||
Serial.println(item); | ||
|
||
// Wait before trying again | ||
vTaskDelay(500 / portTICK_PERIOD_MS); | ||
} | ||
} | ||
|
||
//***************************************************************************** | ||
// Main (runs as its own task with priority 1 on core 1) | ||
|
||
void setup() { | ||
|
||
// Configure Serial | ||
Serial.begin(115200); | ||
|
||
// Wait a moment to start (so we don't miss Serial output) | ||
vTaskDelay(1000 / portTICK_PERIOD_MS); | ||
Serial.println(); | ||
Serial.println("---FreeRTOS Queue Demo---"); | ||
|
||
// Create queue | ||
msg_queue = xQueueCreate(msg_queue_len, sizeof(int)); | ||
|
||
// Start print task | ||
xTaskCreatePinnedToCore(printMessages, | ||
"Print Messages", | ||
1024, | ||
NULL, | ||
1, | ||
NULL, | ||
app_cpu); | ||
} | ||
|
||
void loop() { | ||
|
||
static int num = 0; | ||
|
||
// Try to add item to queue for 10 ticks, fail if queue is full | ||
if (xQueueSend(msg_queue, (void *)&num, 10) != pdTRUE) { | ||
Serial.println("Queue full"); | ||
} | ||
num++; | ||
|
||
// Wait before trying again | ||
vTaskDelay(1000 / portTICK_PERIOD_MS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
06-mutex/esp32-freertos-06-demo-mutex/esp32-freertos-06-demo-mutex.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* FreeRTOS Mutex Demo | ||
* | ||
* Increment a shared global variable with mutex protection. | ||
* | ||
* Date: January 20, 2021 | ||
* Author: Shawn Hymel | ||
* License: 0BSD | ||
*/ | ||
|
||
// You'll likely need this on vanilla FreeRTOS | ||
//#include semphr.h | ||
|
||
// Use only core 1 for demo purposes | ||
#if CONFIG_FREERTOS_UNICORE | ||
static const BaseType_t app_cpu = 0; | ||
#else | ||
static const BaseType_t app_cpu = 1; | ||
#endif | ||
|
||
// Globals | ||
static int shared_var = 0; | ||
static SemaphoreHandle_t mutex; | ||
|
||
//***************************************************************************** | ||
// Tasks | ||
|
||
// Increment shared variable (the wrong way) | ||
void incTask(void *parameters) { | ||
|
||
int local_var; | ||
|
||
// Loop forever | ||
while (1) { | ||
|
||
// Take mutex prior to critical section | ||
if (xSemaphoreTake(mutex, 0) == pdTRUE) { | ||
|
||
// Critical section (poor demonstration of "shared_var++") | ||
local_var = shared_var; | ||
local_var++; | ||
vTaskDelay(random(100, 500) / portTICK_PERIOD_MS); | ||
shared_var = local_var; | ||
|
||
// Give mutex after critical section | ||
xSemaphoreGive(mutex); | ||
|
||
// Print out new shared variable | ||
Serial.println(shared_var); | ||
|
||
} else { | ||
// Do something else | ||
} | ||
} | ||
} | ||
|
||
//***************************************************************************** | ||
// Main (runs as its own task with priority 1 on core 1) | ||
|
||
void setup() { | ||
|
||
// Hack to kinda get randomness | ||
randomSeed(analogRead(0)); | ||
|
||
// Configure Serial | ||
Serial.begin(115200); | ||
|
||
// Wait a moment to start (so we don't miss Serial output) | ||
vTaskDelay(1000 / portTICK_PERIOD_MS); | ||
Serial.println(); | ||
Serial.println("---FreeRTOS Race Condition Demo---"); | ||
|
||
// Create mutex before starting tasks | ||
mutex = xSemaphoreCreateMutex(); | ||
|
||
// Start task 1 | ||
xTaskCreatePinnedToCore(incTask, | ||
"Increment Task 1", | ||
1024, | ||
NULL, | ||
1, | ||
NULL, | ||
app_cpu); | ||
|
||
// Start task 2 | ||
xTaskCreatePinnedToCore(incTask, | ||
"Increment Task 2", | ||
1024, | ||
NULL, | ||
1, | ||
NULL, | ||
app_cpu); | ||
|
||
// Delete "setup and loop" task | ||
vTaskDelete(NULL); | ||
} | ||
|
||
void loop() { | ||
// Execution should never get here | ||
} |
83 changes: 83 additions & 0 deletions
83
06-mutex/esp32-freertos-06-demo-race-condition/esp32-freertos-06-demo-race-condition.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* FreeRTOS Race Condition Demo | ||
* | ||
* Increment a shared global variable. | ||
* | ||
* Date: January 20, 2021 | ||
* Author: Shawn Hymel | ||
* License: 0BSD | ||
*/ | ||
|
||
// Use only core 1 for demo purposes | ||
#if CONFIG_FREERTOS_UNICORE | ||
static const BaseType_t app_cpu = 0; | ||
#else | ||
static const BaseType_t app_cpu = 1; | ||
#endif | ||
|
||
// Globals | ||
static int shared_var = 0; | ||
|
||
//***************************************************************************** | ||
// Tasks | ||
|
||
// Increment shared variable (the wrong way) | ||
void incTask(void *parameters) { | ||
|
||
int local_var; | ||
|
||
// Loop forever | ||
while (1) { | ||
|
||
// Roundabout way to do "shared_var++" randomly and poorly | ||
local_var = shared_var; | ||
local_var++; | ||
vTaskDelay(random(100, 500) / portTICK_PERIOD_MS); | ||
shared_var = local_var; | ||
|
||
// Print out new shared variable | ||
Serial.println(shared_var); | ||
} | ||
} | ||
|
||
//***************************************************************************** | ||
// Main (runs as its own task with priority 1 on core 1) | ||
|
||
void setup() { | ||
|
||
// Hack to kinda get randomness | ||
randomSeed(analogRead(0)); | ||
|
||
// Configure Serial | ||
Serial.begin(115200); | ||
|
||
// Wait a moment to start (so we don't miss Serial output) | ||
vTaskDelay(1000 / portTICK_PERIOD_MS); | ||
Serial.println(); | ||
Serial.println("---FreeRTOS Race Condition Demo---"); | ||
|
||
// Start task 1 | ||
xTaskCreatePinnedToCore(incTask, | ||
"Increment Task 1", | ||
1024, | ||
NULL, | ||
1, | ||
NULL, | ||
app_cpu); | ||
|
||
// Start task 2 | ||
xTaskCreatePinnedToCore(incTask, | ||
"Increment Task 2", | ||
1024, | ||
NULL, | ||
1, | ||
NULL, | ||
app_cpu); | ||
|
||
// Delete "setup and loop" task | ||
vTaskDelete(NULL); | ||
} | ||
|
||
void loop() { | ||
// Execution should never get here | ||
} |
Oops, something went wrong.