TaskScheduler: A Lightweight Cooperative Multitasking Scheduling Library for Arduino, ESPx, STM32, and Other Microcontrollers

TaskScheduler is a lightweight cooperative multitasking scheduling library designed for Arduino, ESPx, STM32, and other microcontrollers. It provides a simpler and more user-friendly alternative to preemptive programming and frameworks like FreeRTOS, allowing you to easily implement multitasking without getting bogged down in the pitfalls of concurrent programming.

1. Advantages of Cooperative Multitasking

TaskScheduler adopts a cooperative multitasking model. This means that tasks voluntarily relinquish CPU control while running, thus avoiding the complex issues of race conditions, deadlocks, livelocks, and resource sharing that can arise with preemptive multitasking. Cooperative multitasking simplifies the development process and reduces the likelihood of errors, making it particularly suitable for resource-constrained microcontroller environments.

2. Key Features and Characteristics

TaskScheduler boasts a rich set of features designed to meet the needs of multitasking programming for various microcontrollers:

  • Flexible Task Scheduling: Supports periodic task execution with dynamically adjustable execution periods (in milliseconds or microseconds). You can set the number of iterations for task execution (finite or infinite).

  • Custom Task Sequences: Allows tasks to be executed in a predefined order.

  • Dynamic Parameter Adjustment: Task execution parameters such as frequency, iteration count, and callback methods can be dynamically modified at runtime.

  • Energy Saving Mechanism: The system enters IDLE sleep mode when there are no tasks to run, saving power.

  • Event-Driven: Supports event-driven task invocation through state request objects.

  • Error Handling and Monitoring: Supports task IDs and control points for easy error handling and watchdog timer integration.

  • Local Task Storage: Supports local task storage pointers, allowing the same callback code to serve multiple tasks.

  • Hierarchical Priorities: Supports hierarchical task priorities for priority scheduling between tasks.

  • Standard Function Support: Supports <span>std::function</span> (tested on ESPx and STM32).

  • Task Timeout Mechanism: Sets a global task timeout period.

  • Static and Dynamic Callback Binding: Supports static and dynamic callback method binding, allowing flexible configuration of task callback functions.

  • CPU Load Statistics: Provides CPU load/idle state statistics for performance analysis of time-critical applications.

  • Multiple Scheduling Strategies: Offers various scheduling strategies, including priority scheduling (with and without catch-up mechanisms) and interval scheduling.

  • Pause/Resume and Enable/Disable: Allows pausing/resuming and enabling/disabling task scheduling.

  • Thread Safety: Guarantees thread safety when running under preemptive schedulers (e.g., FreeRTOS).

  • Dynamic Task Self-Destruction: Supports automatic destruction of dynamically created tasks when disabled.

  • Tickless Execution under FreeRTOS: Supports tickless execution under FreeRTOS, allowing the system to remain asleep until the next scheduled task call.

  • Low Scheduling Overhead: On Arduino UNO rev 3 (16MHz), the scheduling overhead is between 15 to 18 microseconds per invocation (single scheduler, no priorities).

3. Supported Platforms

TaskScheduler has been extensively tested and is compatible with various microcontroller platforms:

  • • Arduino Uno R3

  • • Arduino Nano

  • • Arduino Micro

  • • ATtiny85

  • • ESP8266

  • • ESP32

  • • Teensy (Teensy 3.5 beta)

  • • nRF52 (nRF52832 beta)

  • • nRF52 Adafruit Core (nRF52840 beta, v3.6.2 compatibility handling)

  • • STM32 (Mini USB STM32F103RCBT6 ARM Cortex-M3 leaflabs Leaf maple mini module F beta)

  • • MSP430 and MSP432 chips

  • • Raspberry Pi (requires external Arduino.h and millis() implementation)

4. Conclusion

TaskScheduler provides powerful and easy-to-use cooperative multitasking scheduling capabilities for microcontrollers like Arduino. Its lightweight nature and rich features make it an ideal choice for various embedded applications. Whether for simple timed tasks or complex event-driven systems, TaskScheduler effectively simplifies the development process, improves code quality, and unleashes the potential of microcontrollers.

Project URL: https://github.com/arkhipenko/TaskScheduler

Leave a Comment