FreeRTOS for ESP32-Arduino
Practical Multitasking Fundamentals
By Warren Gay
Chapter 1: Introduction
This chapter introduces the need for a Real-Time Operating System (RTOS) like FreeRTOS and its application within the ESP32-Arduino environment. It covers hardware considerations, development boards such as the ESP8266, and FreeRTOS conventions including naming practices for variables, functions, and macros. The setup process for Arduino on the ESP32 is detailed, along with ESP-related Arduino resources and the integration of C and C++ with FreeRTOS. Specific ESP32 notes, GPIO references, and details about used graphics/drivers for boards like the TTGO ESP32 T-Display and M5Stack are also discussed. Assumptions about the reader's prior knowledge and a summary of web resources are provided.
Chapter 2: Tasks
This chapter delves into the fundamental concept of tasks in FreeRTOS, explaining preemptive scheduling and the Arduino startup process. It covers main tasks, task demonstration, program design, and stack size considerations. Memory management within FreeRTOS, static tasks, task deletion, and task suspend/resume functionalities are explained. The chapter also discusses task time slicing, yielding the CPU, and the use of the assert macro. Exercises and web resources are included.
Chapter 3: Queues
Queues are a crucial inter-task communication mechanism in FreeRTOS. This chapter explores queue characteristics such as arrival patterns, capacity, and service discipline. It details the basic queue API, including creating static and dynamic queues, queuing items, and receiving from queues. Task scheduling behavior when adding or receiving from queues, including blocking scenarios, is explained. Demonstrations and program setup for queue usage are provided, along with notes on ISR routines and mutexes.
Chapter 4: Timers
This chapter focuses on FreeRTOS timers, covering timer categories, software timers, and the timer callback mechanism. Timer limitations, timer ID values, and the potential for abusing timer IDs are discussed. Timer types and states are explained, along with methods for creating static and dynamic timers and activating them. A demonstration of timer usage, including the AlertLED constructor and its methods, is presented. Notes on setup, loop functions, and priority considerations are also included.
Chapter 5: Semaphores
Semaphores are synchronization primitives used in FreeRTOS. This chapter covers semaphore types, including binary and counting semaphores, and provides a demonstration of binary semaphore usage. Program operation, locks, and the concept of deadlocks are explained, with a focus on the Dining Philosophers problem and deadlock prevention. Lockups and insidious deadlocks are also discussed. Exercises and web resources are provided.
Chapter 6: Mailboxes
Mailboxes are another inter-task communication mechanism in FreeRTOS. This chapter outlines the problem that mailboxes solve, describes the mailbox structure, and details how to create and read from a mailbox. A mailbox demonstration and program dissection are included. Exercises and web resources are provided.
Chapter 7: Task Priorities
This chapter explores task priorities in FreeRTOS and their impact on scheduling. It explains the `vTaskStartScheduler()` function, the configured scheduling algorithm, task pre-emption, and time slicing. ESP32 task priorities and task states are discussed, along with I/O and CPU sharing. Methods for preventing immediate task starts and a simple demonstration are provided. Blocking mechanisms, creating ready-to-go tasks, and ESP32 dual-core considerations are covered. Priority demonstrations and configuration, along with a scheduler review, are included.
Chapter 8: Mutexes
Mutexes are used for mutual exclusion in FreeRTOS. This chapter explains the exclusion principle, the problems mutexes solve, and the mutex solution. Priority inversion and its implications are discussed, along with methods for creating mutexes and the give-and-take operations. Demonstrations, including the PCF8574 chip and LED drive, are provided. Code breakdown, troubleshooting, running demonstrations, recursive mutexes, and deadlock avoidance are also covered.
Chapter 9: Interrupts
This chapter focuses on interrupt handling in FreeRTOS. It covers the characteristics of an Interrupt Service Routine (ISR), asynchronous ISRs, and the ISR stack. Non-reentrant routine calls and ISR priorities are discussed. Short ISR routines, the concept of an ISR not being a task, and special ISR code are explained. ESP32 Arduino GPIO interrupts, frequency counter projects, and challenges related to interrupt handling are presented. Approaches to handling interrupts, including specific cases and project code, are detailed.
Chapter 10: Queue Sets
Queue sets are an advanced FreeRTOS feature for managing multiple queues efficiently. This chapter addresses the problem of managing multiple queues, introduces the queue set concept, and explains queue set configuration and selection. Traps to avoid when using queue sets, such as `xQueueAddToSet` traps, are highlighted. Demonstrations and program breakdowns for queue set usage are provided, along with notes on ISR routines, event monitoring tasks, and mutexes.
Chapter 11: Task Events
This chapter covers task events and notifications in FreeRTOS. It explains restrictions, waiting mechanisms, and the `ulTaskNotifyTake()` function. Binary and counting notifications, along with giving notifications, are discussed. Multiple demonstrations illustrate various aspects of task notifications, including going beyond simple notifications and using arguments for clearing bits on exit and entry. Smart notifications and the `eAction` argument are also covered.
Chapter 12: Event Groups
Event groups are used for synchronizing multiple tasks in FreeRTOS. This chapter explains the `EventBits_t` type, creating event group objects, and notifying event groups. Waiting for event groups and synchronization mechanisms are detailed. Demonstrations of event group usage are provided, along with auxiliary functions such as `vEventGroupDelete()`, `xEventGroupClearBits()`, and `xEventGroupGetBits()`.
Chapter 13: Advanced Topics
This chapter explores advanced FreeRTOS topics relevant to the ESP32. It covers watchdog timers, enabling task watchdogs, and watchdogs for multiple tasks. Non-Arduino watchdog usage and the idle task are discussed. Critical sections, including ESP32-specific critical sections and critical sections for ISRs, are explained. Task local storage and functions for retrieving task information like `uxTaskGetNumberOfTasks()`, `xTaskGetSchedulerState()`, `eTaskGetState()`, `xTaskGetTickCount()`, and `vTaskSuspendAll()` are detailed. ESP32 Arduino limitations are also mentioned.
Chapter 14: Gatekeeper Tasks
Gatekeeper tasks are a design pattern in FreeRTOS for managing shared resources. This chapter introduces gatekeepers, provides demonstrations of their usage, and explains extension GPIO designations. The gatekeeper API and demonstrations of its application, such as with the XGPIO, are covered. Gatekeeper code, initialization, and input/output to peripherals like the PCF8574P are detailed, along with PCF8574P state management and troubleshooting.
Chapter 15: Tips and Hints
This chapter offers practical tips and hints for working with FreeRTOS and the ESP32. It covers various aspects of software development, including engaging with forums, starting small, adopting different approaches to problem-solving, and understanding block diagrams. Faults, storage lifetimes, and the importance of avoiding external names and leveraging scope are discussed. The chapter also provides advice on memory management, note-taking, asking for help, and efficient programming practices, including source code beauty and the use of Fritzing for schematics.
Appendices
The document includes appendices covering additional information and resources, such as Appendix A and Appendix B (Parts), followed by an index for easy reference.
Related Documents
![]() |
ESP32 Robot Car Control: A Comprehensive Guide This guide explores building and controlling a robot car using the ESP32 microcontroller. It covers open-source code with Arduino IDE and PlatformIO, autonomous driving features (GPS, accelerometer, gyroscope), and PS3 controller integration, authored by Udo Brandes and published by Elektor. |
![]() |
Practical Audio DSP Projects with the ESP32: Easy and Affordable Digital Signal Processing Explore practical audio digital signal processing (DSP) projects using the ESP32 microcontroller. This guide covers essential concepts like FIR and IIR filters, FFTs, and audio file formats, with hands-on projects for beginners and experienced users. |
![]() |
RISC-V Assembly Language Programming: Using the ESP32-C3 and QEMU A comprehensive guide to RISC-V assembly language programming, focusing on the ESP32-C3 microcontroller and the QEMU emulator. Covers architecture, instructions, calling conventions, flow control, memory addressing, floating-point operations, portability, and debugging. |
![]() |
Arduino for Radio Amateur Applications: Build Ham Station Utilities A comprehensive guide to programming and building Arduino-based utilities, tools, and instruments for radio amateurs, covering various Arduino boards, shields, modules, and practical projects. |
![]() |
ESP32 by Example: A Project-Based Introduction to Microcontrollers and Drone Control A project-based introduction to embedded systems using the ESP32 microcontroller and drone control. This guide is suitable for beginners and covers programming, the Arduino IDE, cyber-physical systems, and practical projects. |
![]() |
Welcher Mikrocontroller passt zu meinem Projekt? – Ein Leitfaden von Elektor Entdecken Sie die Welt der Mikrocontroller und Entwicklungskits. Dieser Leitfaden vergleicht Arduino, Raspberry Pi, ESP8266, ESP32 und mehr, um Ihnen bei der Auswahl des perfekten Boards für Ihr nächstes Elektronikprojekt zu helfen. |
![]() |
The Ultimate Compendium of Sensor Projects: Arduino, Raspberry Pi, and ESP32 A comprehensive guide featuring over 40 projects using Arduino, Raspberry Pi, and ESP32, covering various sensor applications from light and infrared to vibration, temperature, and Wi-Fi/Bluetooth connectivity. |
![]() |
Develop Your Own Bluetooth Low Energy Applications A comprehensive guide to developing Bluetooth Low Energy (BLE) applications using platforms like Raspberry Pi, ESP32, and nRF52 with Python, Arduino, and Zephyr. Covers BLE fundamentals, development environments, data broadcasting, secure connections, profiles, and power optimization. |