Skip to content

Commit

Permalink
Added 05-07 demos, challenges, and solutions. Updated README.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnHymel committed Jan 28, 2021
1 parent 4281e22 commit baf86d2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void testTask(void *parameter) {
// One way to prevent heap overflow is to check the malloc output
if (ptr == NULL) {
Serial.println("Not enough heap.");
vPortFree(NULL);
} else {

// Do something with the memory so it's not optimized out by the compiler
Expand All @@ -52,7 +53,7 @@ void testTask(void *parameter) {
Serial.println(xPortGetFreeHeapSize());

// Free up our allocated memory
vPortFree(ptr);
//vPortFree(ptr);

// Wait for a while
vTaskDelay(100 / portTICK_PERIOD_MS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* FreeRTOS Mutex Challenge
*
* Pass a parameter to a task using a mutex.
*
* 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

// Pins (change this if your Arduino board does not have LED_BUILTIN defined)
static const int led_pin = LED_BUILTIN;

//*****************************************************************************
// Tasks

// Blink LED based on rate passed by parameter
void blinkLED(void *parameters) {

// Copy the parameter into a local variable
int num = *(int *)parameters;

// Print the parameter
Serial.print("Received: ");
Serial.println(num);

// Configure the LED pin
pinMode(led_pin, OUTPUT);

// Blink forever and ever
while (1) {
digitalWrite(led_pin, HIGH);
vTaskDelay(num / portTICK_PERIOD_MS);
digitalWrite(led_pin, LOW);
vTaskDelay(num / portTICK_PERIOD_MS);
}
}

//*****************************************************************************
// Main (runs as its own task with priority 1 on core 1)

void setup() {

long int delay_arg;

// 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 Mutex Challenge---");
Serial.println("Enter a number for delay (milliseconds)");

// Wait for input from Serial
while (Serial.available() <= 0);

// Read integer value
delay_arg = Serial.parseInt();
Serial.print("Sending: ");
Serial.println(delay_arg);

// Start task 1
xTaskCreatePinnedToCore(blinkLED,
"Blink LED",
1024,
(void *)&delay_arg,
1,
NULL,
app_cpu);

// Show that we accomplished our task of passing the stack-based argument
Serial.println("Done!");
}

void loop() {

// Do nothing but allow yielding to lower-priority tasks
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ static int buf[BUF_SIZE]; // Shared buffer
static int head = 0; // Writing index to buffer
static int tail = 0; // Reading index to buffer
static SemaphoreHandle_t bin_sem; // Waits for parameter to be read
static SemaphoreHandle_t mutex; // Lock access to buffer and Serial

//*****************************************************************************
// Tasks
Expand Down Expand Up @@ -93,7 +92,6 @@ void setup() {

// Create mutexes and semaphores before starting tasks
bin_sem = xSemaphoreCreateBinary();
mutex = xSemaphoreCreateMutex();

// Start producer tasks (wait for each to read argument)
for (int i = 0; i < num_prod_tasks; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void setup() {
app_cpu);

// Do nothing until binary semaphore has been returned
while (xSemaphoreTake(bin_sem, 0) != pdTRUE);
xSemaphoreTake(bin_sem, portMAX_DELAY);

// Show that we accomplished our task of passing the stack-based argument
Serial.println("Done!");
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ Welcome to the demo code and solutions section of my Introduction to RTOS course

This course is hosted on YouTube that you may take for free. All you need to do is watch the videos and complete the challenge issued at the end of each video. I highly recommend you try each challenge before peeking at the solutions here.

The first video in the series is found here: %%%TODO: LINK%%%
The first video in the series is found here: [Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)?](https://www.youtube.com/watch?v=F321087yYy4)

Written guides that explain the solutions can be found [among my maker.io projects](https://www.digikey.com/en/maker/profiles/72825bdd887a427eaf8d960b6505adac).

## Directory Structure

Examples and solutions are housed in dirctories that correspond to each chapter or video number. For example, if you watch "Intro to RTOS Part 3 - Task Scheduling," you should refer to the directory *03-task-scheduling-and-management.* In it, you will find 2 directories, one marked "demo" that gives the finished demo code used during the video (so you may run it and examine it at your own pace) and another marked "solution" that provides one possible solution to the challenge issued at the end of the video.

If a challenge is issued in a video that starts with some code, it will be listed as a *challenge* Arduino sketch in the naming scheme shown below.

Directories are in the following structure (where xx is the part or chapter number):

```
xx-<name of chapter>
|- esp32-freertos-xx-challenge-<name>
|- esp32-freertos-xx-demo-<name>
|- esp32-freertos-xx-solution-<name>
```
Expand Down

0 comments on commit baf86d2

Please sign in to comment.