Introduction to RTOS Part 2 - Getting Started with FreeRTOS | Digi-Key Electronics

Introduction to RTOS Part 2 - Getting Started with FreeRTOS | Digi-Key Electronics

Introduction to FreeRTOS on ESP32

Overview of the Series

  • The series will cover general RTOS concepts and demonstrate their implementation using FreeRTOS on an ESP32.
  • The Arduino IDE will be used for simplicity, starting with downloading the FreeRTOS source code.

Getting Started with FreeRTOS

  • Visit freeRTOS.org to download the source code; documentation is available under Kernel > Getting Started for building demo applications.
  • Including FreeRTOS in a project requires specific C and header files, along with selecting a heap management file. A microcontroller timer must be assigned as the RTOS tick timer.

Resources for Learning FreeRTOS

Recommended Reading

  • For assistance, download "Mastering the FreeRTOS Real-Time Kernel" and the reference manual from the books link on freeRTOS.org.

Understanding Library Structure

  • The FreeRTOS library functions as a scheduler; additional drivers are included in the FreeRTOS Plus library primarily for networking (TCP/UDP).
  • Example projects can be found in the demo folder, which includes config files defining important variables necessary for your project setup.

ESP32 Specific Considerations

Modified Version of FreeRTOS

  • The ESP32 uses a modified version of FreeRTOS within its ESP-IDF framework to support its dual-core processor architecture (SMP).

Limiting Core Usage

  • This series will limit tasks to one core for simplicity; understanding multi-core support can be explored later through ESP32 documentation.

Setting Up Arduino IDE for ESP32

Installation Steps

  • Install Arduino IDE and add Espressif's board manager URL: https://dl.espressif.com/dl/package_esp32_index.json.

Configuring Board Settings

  • Search and install the latest version of Espressif Systems' ESP32 package via Tools > Board > Board Manager.

FreeRTOS Configuration on ESP32

Accessing Configuration Files

  • Locate FreeRTOS config.h file in your Arduino board packages directory to understand how FreeRTOS is configured specifically for ESP32.

Key Configuration Insights

  • The configuration allows up to 25 priority levels and sets a minimum task stack size of 768 bytes. Use this file as a reference when adjusting settings.

Creating Tasks in FreeRTOS

Defining Tasks

  • Define constants that restrict task creation to one core; typically not recommended but useful for learning purposes.

Implementing Blinky Program

Understanding RTOS and Task Management in FreeRTOS

Tick Timer and Task Scheduling

  • Most Real-Time Operating Systems (RTOS) utilize a tick timer, which is a hardware timer that interrupts the processor at regular intervals.
  • The interval of these interrupts is referred to as a "tick," allowing the scheduler to determine which task should run during each tick.
  • In FreeRTOS, the default tick period is set to one millisecond, with portTICK_PERIOD_MS defined as one.

Creating Tasks in FreeRTOS

  • The function vTaskDelay requires ticks for delay rather than milliseconds; thus, desired milliseconds must be divided by the tick period.
  • To create tasks, xTaskCreatePinnedToCore can be used to specify which core will execute the task; this function is not available in Vanilla FreeRTOS.
  • While xTaskCreate works in ESP-IDF, it allows the scheduler to choose any core for task execution.

Task Parameters and Configuration

  • When creating a task, parameters include specifying the function to call, naming the task, setting stack size (minimum 768 bytes), and defining priority levels (0 being lowest).
  • A pointer or handle can be assigned for managing tasks from other tasks or loops; this helps monitor status and memory usage.

Execution Flow in ESP32 Environment

  • In Arduino framework for ESP32, setup and loop functions operate within their own tasks separate from main program entry points.
  • New tasks begin executing automatically without needing to call vTaskStartScheduler, as it has already been invoked prior to setup.

Practical Application: LED Blinking Challenge

  • After configuring everything correctly, an LED should blink once per second as a basic outcome of the setup process.
Video description

FreeRTOS is a free and open source real-time operating system (RTOS) owned and maintained by Amazon. The solution to the challenge in the video can be found here: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-2-freertos/b3f84c9c9455439ca2dcb8ccfce9dec5 ESP-IDF FreeRTOS SMP Changes: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/freertos-smp.html Add this URL to the "Additional Boards Manager URLs" window in Arduino for ESP32 support: https://dl.espressif.com/dl/package_esp32_index.json In this video, we talk about how a task is created in FreeRTOS using the ESP32 and Arduino. A task in FreeRTOS is similar to a thread in other multi-threaded environments (e.g. POSIX). It is a unit of CPU utilization designed to accomplish some goal. For our purposes, we just want to create a new thread and toggle an LED. The ESP32 that comes with many development boards (including the Adafruit Feather HUZZAH32 shown in the video) runs a modified version of FreeRTOS (if you are using the ESP32 package for Arduino or the Espressif SDK). Most importantly, the ESP32 version (named ESP-IDF) supports the dual-core processor on the ESP32. Tasks created with the regular xTaskCreate() can run on either core as chosen by the scheduler. For demo purposes, we want to run all tasks on a single core. This will allow us to experiment with prioritization and shared resources later in this series. To do this on the ESP32, we use the xTaskCreatePinnedToCore() function instead and specify which core to use. If you are using vanilla FreeRTOS in your own build system, you will want to use xTaskCreate() instead. Product Links: https://www.digikey.com/en/products/detail/adafruit-industries-llc/3405/7244967 Related Videos: Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? - https://youtu.be/F321087yYy4​ Introduction to RTOS Part 2 - Getting Started with FreeRTOS - https://youtu.be/JIr7Xm_riRs​ Introduction to RTOS Part 3 - Task Scheduling - https://youtu.be/95yUbClyf3E​ Introduction to RTOS Part 4 - Memory Management - https://youtu.be/Qske3yZRW5I​ Introduction to RTOS Part 5 - Queue - https://youtu.be/pHJ3lxOoWeI​ Introduction to RTOS Part 6 - Mutex - https://youtu.be/I55auRpbiTs​ Introduction to RTOS Part 7 - https://youtu.be/5JcMtbA9QEE​ Introduction to RTOS Part 8 - https://youtu.be/b1f1Iex0Tso Introduction to RTOS Part 9 - https://youtu.be/qsflCf6ahXU Introduction to RTOS Part 10 - https://youtu.be/hRsWi4HIENc Introduction to RTOS Part 11 - https://youtu.be/C2xKhxROmhA Introduction to RTOS Part 12 - https://youtu.be/LPSHUcH5aQc Related Project Links: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-2-freertos/b3f84c9c9455439ca2dcb8ccfce9dec5 Related Articles: https://www.digikey.com/en/maker/projects/what-is-an-rtos-real-time-operating-system/28d8087f53844decafa5000d89608016 Learn more: Maker.io - https://www.digikey.com/en/maker Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/ And follow us on Twitter https://twitter.com/digikey