Giter VIP home page Giter VIP logo

introduction-to-rtos's Introduction

Introduction to Real Time Operating Systems (RTOS)

Welcome to the demo code and solutions section of my Introduction to RTOS course! This repository houses all of the example code and solutions that you may use as references when working through the RTOS examples for FreeRTOS.

Intro to RTOS course logo

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 PowerPoint slides are also made available (under the CC BY 4.0 license) if you wish to use or modify them for review or teaching your own RTOS class. If you use the slides, please give credit to Shawn Hymel and Digi-Key Electronics in your slides (as per the CC BY 4.0 requirements).

Chapter Title Video Solution Slides
01 What is an RTOS [video] [solution] [slides]
02 Getting Started with freeRTOS [video] [solution] [slides]
03 Task Scheduling and Management [video] [solution] [slides]
04 Memory Allocation [video] [solution] [slides]
05 Queue [video] [solution] [slides]
06 Mutex [video] [solution] [slides]
07 Semaphore [video] [solution] [slides]
08 Software Timer [video] [solution] [slides]
09 Hardware Interrupts [video] [solution] [slides]
10 Deadlock and Starvation [video] [solution] [slides]
11 Priority Inversion [video] [solution] [slides]
12 Multicore Systems [video] [solution] [slides]

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>/
|- rtos-part-xx.pptx

PowerPoint slides used in each video can be found within the respective xx- directory.

The only exception to this is the images directory, which is where I keep images for this repository.

License

PowerPoint slides are licensed under the Creative Commons CC BY 4.0 license. You are welcome to use and modify them for your own review and teaching. If you use them, please give credit to Shawn Hymel and Digi-Key Electronics.

All code in this repository, unless otherwise noted, is licensed under the Zero-Clause BSD / Free Public License 1.0.0 (0BSD).

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

introduction-to-rtos's People

Contributors

shawnhymel avatar volodink avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

introduction-to-rtos's Issues

Part 10 deadlock demo timeout solution: tasks attempt to give/return mutexes that have never been taken

I noticed while going through this (excellent BTW!) tutorial series that the ESP32 was resetting repeatedly during the execution of the deadlock demo timeout solution.

Serial output:
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180
�
---FreeRTOS Deadlock Demo---
Task A took mutex 1
Task B took mutex 2
Task A timed out waiting for mutex 2
Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception).
Debug exception reason: Stack canary watchpoint triggered (Task A)
Core  0 register dump:
PC      : 0x00001004  PS      : 0x3ffc75b9  A0      : 0x80028fb7  A1      : 0x8002d7c4
A2      : 0x3ffc7330  A3      : 0x00000054  A4      : 0x00000000  A5      : 0x00000080
A6      : 0x3ffc7500  A7      : 0x00000003  A8      : 0x80028d3d  A9      : 0x00000000  
A10     : 0x00000000  A11     : 0x00001800  A12     : 0x00000000  A13     : 0x80028d3d  
A14     : 0x3ffc7350  A15     : 0x00000054  SAR     : 0x00000002  EXCCAUSE: 0x00000001
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x3f005c0c  LCOUNT  : 0x400275ac  


Backtrace:0x00001001:0x8002d7c4 |<-CORRUPTED




ELF file SHA256: 0000000000000000

Rebooting...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x400261c0
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180
Guru Meditation Error: Core  0 panic'ed (Cache error). 

Core  0 register dump:
PC      : 0x40099949  PS      : 0x00060d34  A0      : 0x800883ae  A1      : 0x3fffe2a0  
A2      : 0x02000000  A3      : 0x00000000  A4      : 0x02000000  A5      : 0xfffffffc
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x6000e000  
A10     : 0x007dff2d  A11     : 0x02000000  A12     : 0x00000030  A13     : 0x00000030
A14     : 0x00231d69  A15     : 0x00000000  SAR     : 0x00000007  EXCCAUSE: 0x00000007  
EXCVADDR: 0x00000000  LBEG    : 0x00000030  LEND    : 0x00000030  LCOUNT  : 0x40026008


Backtrace:0x40099946:0x3fffe2a00x400883ab:0x3fffe2b0 0x40088d4b:0x3fffe2d0 0x40088f49:0x3fffe2f0 0x40025fd4:0x3fffe320 0x40050bbe:0x3fffe340 0x40050e53:0x3fffe370 0x4004c1c1:0x3fffe4a0 0x40011371:0x3fffe560 0x4000f788:0x3fffe6f0 0x400072a5:0x3fffe710 




ELF file SHA256: 0000000000000000

Rebooting...

As opposed to what's in this repo, I realized that this version of the function is required to avoid the core reset: (same change for Task B as well)

// Task A (high priority)
void doTaskA(void *parameters) {
  // Loop forever
  while (1) {
    // Take mutex 1
    if (xSemaphoreTake(mutex_1, mutex_timeout) == pdTRUE) {
      // Say we took mutex 1 and wait (to force deadlock)
      Serial.println("Task A took mutex 1");
      vTaskDelay(1 / portTICK_PERIOD_MS);

      // Take mutex 2
      if (xSemaphoreTake(mutex_2, mutex_timeout) == pdTRUE) {
        // Say we took mutex 2
        Serial.println("Task A took mutex 2");

        // Critical section protected by 2 mutexes
        Serial.println("Task A doing some work");
        vTaskDelay(500 / portTICK_PERIOD_MS);

        // Give back mutex
        xSemaphoreGive(mutex_2);
      } else {
        Serial.println("Task A timed out waiting for mutex 2");
      }

      // Give back mutex
      xSemaphoreGive(mutex_1);
    } else {
      Serial.println("Task A timed out waiting for mutex 1");
    }

    // Wait to let the other task execute
    Serial.println("Task A going to sleep");
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}

I can submit a PR to fix this if you're interested? Love your work here, Shawn!

suspended task deletion program stops and does not switch to another task

Board = ESP32
version = 1.0.6

CODE

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0 ;
#else
static const BaseType_t app_cpu = 1;
#endif

//const char msg[] = "qwerty is aqsws"; //STATEMENT - 1
const char msg[] = "qwqwmqkqkw kkwmdkwdksd ksmdksmdks ksmdksmd"; //STATEMENT - 2
//static const int led_pin = 2;

//TAsk Handlers
static TaskHandle_t task_1 = NULL;
static TaskHandle_t task_2 = NULL;

void startTask1( void *parameters)
{
int msg_len = strlen(msg);

while(1)
{
Serial.println();
for(int i =0 ; i < msg_len ;i++)
{
Serial.print(msg[i]);
}
Serial.println();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void startTask2( void *parameters)
{

while(1)
{
Serial.print('*');
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}

void setup() {
// put your setup code here, to run once:
// pinMode( led_pin ,OUTPUT );
Serial.begin(300);

vTaskDelay(1000 / portTICK_PERIOD_MS);

Serial.println("----FREE RTOS DEMO ----");

Serial.println("setup and loop task running on core ");
Serial.print(xPortGetCoreID());
Serial.print(" with priority ");
Serial.println(uxTaskPriorityGet(NULL));

//task to run forever
xTaskCreatePinnedToCore( //USE xTaskCreate() in vanilla FreeRTOS
startTask1, //Function to be called
"Task 1", //NAME of Task
1024, //Stack Size ( bytes in esp32 ,words in vanilla)
NULL, //Parameters to pass to function,
1, //Task priority ( 0 to configMAX_priorites(25 for esp32 ) -1 i.e 0 to 24)
&task_1, //parameters to pass to functions
app_cpu); //Run on One Core for demo purpose

xTaskCreatePinnedToCore( //USE xTaskCreate() in vanilla FreeRTOS
startTask2, //Function to be called
"Task 2", //NAME of Task
1024, //Stack Size ( bytes in esp32 ,words in vanilla)
NULL, //Parameters to pass to function,
2, //Task priority ( 0 to configMAX_priorites(25 for esp32 ) -1 i.e 0 to 24)
&task_2, //parameters to pass to functions
app_cpu); //Run on One Core for demo purpose
}

void loop() {
// put your main code here, to run repeatedly:

for( int i = 0 ; i < 3 ; i++ )
{
vTaskSuspend(task_2);
vTaskDelay(2000 / portTICK_PERIOD_MS);
vTaskResume(task_2);
vTaskDelay(2000 / portTICK_PERIOD_MS);

}

if( task_1 != NULL ){
vTaskDelete(task_1);
task_1 =NULL;
}

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Problem :
when statement 1 is selected program run smoothly i.e task 2 keeps printing "*" continously after deletion of task 1,
but if statement 2 is selected program gets stuck .
whats happens if suspended task is deleted in free rtos?
STATEMENT1
STATEMENT2

Following the Series with Arduino Uno and FreeRTOS

Hello everyone,

Although this repository is built upon ESP32 in particular, I have been following along the YouTube videos with my Arduino Uno and the Arduino port of FreeRTOS library. It's quite informative and entertaining.

As I was experimenting with task scheduling I stumbled upon an issue which I think is related to how the Arduino library is implemented. I thought I should share this in case somebody else faces the same issue.

Even when adopted for Arduino Uno, the solution code for the LED challenge (source) fails to work whenever there's any calls to vTaskDelay or vTaskDelete in the setup function. I believe this is due to implementation differences between libraries, though I'm not so sure.

I checked the source code for the Arduino library and it looks like the library is starting the scheduler right after calling the setup function (Source Code). Maybe the setup function is defined as an ordinary function (not as a task) and therefore functions like vTaskDelay doesn't work because of that.

Anyway, removing the calls for vTaskDelay or vTaskDelete in the setup function resolved my problem.

Solution example not compileing

The solution of ep 3 does not compile in Arduino IDE 2
it stops at this line: idx%2B%2B; error: unable to find numeric literal operator 'operator""B'

Something wrong in part 6 (Mutex demo)

Hello,
First of all, thank you for this great course.
I'm currently following the course and trying to write and test all the demos and challenges.

In part 6 where you talk about Mutex, I noticed that there is something wrong and only one of the tasks is executed, and the other task is completely blocked and never executed.

I found out this problem by adding these lines before Serial.println(shared_var); line in esp32-freertos-06-demo-mutex.ino.

The function pcTaskGetName(NULL) returns the pcName of the task.

      Serial.print(pcTaskGetName(NULL));
      Serial.print(" : ");
      Serial.println(shared_var);

This is the resulting output:

image

I still don't know why only one task is running. Maybe it has something to do with ‍‍xBlockTime or something else.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.