freertos.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. FreeRTOS (Overview)
  2. ===================
  3. Overview
  4. --------
  5. FreeRTOS is an open source real-time operating system kernel that acts as the operating system for ESP-IDF applications and is integrated into ESP-IDF as a component. The FreeRTOS component in ESP-IDF contains ports of the FreeRTOS kernel for all the CPU architectures used by ESP targets (i.e., Xtensa and RISC-V). Furthermore, ESP-IDF provides different implementations of FreeRTOS in order to support SMP (Symmetric Multiprocessing) on multi-core ESP targets. This document provides an overview of the FreeRTOS component, the FreeRTOS implementations offered by ESP-IDF, and the common aspects across all implementations.
  6. Implementations
  7. ---------------
  8. The `official FreeRTOS <https://www.freertos.org/index.html>`_ (henceforth referred to as Vanilla FreeRTOS) is a single-core RTOS. In order to support the various multi-core ESP targets, ESP-IDF supports different FreeRTOS implementations, namely **ESP-IDF FreeRTOS** and **Amazon SMP FreeRTOS**.
  9. ESP-IDF FreeRTOS
  10. ^^^^^^^^^^^^^^^^
  11. ESP-IDF FreeRTOS is a FreeRTOS implementation based on Vanilla FreeRTOS v10.4.3, but contains significant modifications to support SMP. ESP-IDF FreeRTOS only supports two cores at most (i.e., dual core SMP), but is more optimized for this scenario by design. For more details regarding ESP-IDF FreeRTOS and its modifications, please refer to the :doc:`freertos_idf` document.
  12. .. note::
  13. ESP-IDF FreeRTOS is currently the default FreeRTOS implementation for ESP-IDF.
  14. Amazon SMP FreeRTOS
  15. ^^^^^^^^^^^^^^^^^^^
  16. Amazon SMP FreeRTOS is an SMP implementation of FreeRTOS that is officially supported by Amazon. Amazon SMP FreeRTOS is able to support N-cores (i.e., more than two cores). Amazon SMP FreeRTOS can be enabled via the :ref:`CONFIG_FREERTOS_SMP` option. For more details regarding Amazon SMP FreeRTOS, please refer to the `official Amazon SMP FreeRTOS documentation <https://freertos.org/symmetric-multiprocessing-introduction.html>`_.
  17. .. warning::
  18. The Amazon SMP FreeRTOS implementation (and its port in ESP-IDF) are currently in experimental/beta state. Therefore, significant behavioral changes and breaking API changes can occur.
  19. Configuration
  20. -------------
  21. Kernel Configuration
  22. ^^^^^^^^^^^^^^^^^^^^
  23. Vanilla FreeRTOS requires that ports and applications configure the kernel by adding various ``#define config...`` macros to ``FreeRTOSConfig.h``. Vanilla FreeRTOS supports a list of kernel configuration options which allow various kernel behaviors and features to be enabled or disabled.
  24. **However, for all FreeRTOS ports in ESP-IDF, the ``FreeRTOSConfig.h`` file is considered private and must not be modified by users**. A large number of kernel configuration options in ``FreeRTOSConfig.h`` are hard coded as they are either required or not supported in ESP-IDF. All kernel configuration options that are configurable by the user will be exposed via menuconfig under ``Component Config/FreeRTOS/Kernel``.
  25. For the full list of user configurable kernel options, see :doc:`/api-reference/kconfig`. The list below highlights some commonly used kernel configuration options:
  26. - :ref:`CONFIG_FREERTOS_UNICORE` will run FreeRTOS only on CPU0. Note that this is **not equivalent to running Vanilla FreeRTOS**. Furthermore, this option may affect behavior of components other than :component:`freertos`. For more details regarding the effects of running FreeRTOS on a single core, refer to :ref:`freertos-smp-single-core` (if using ESP-IDF FreeRTOS) or the official Amazon SMP FreeRTOS documentation. Alternatively, users can also search for occurrences of ``CONFIG_FREERTOS_UNICORE`` in the ESP-IDF components.
  27. .. only:: CONFIG_FREERTOS_UNICORE
  28. .. note::
  29. As {IDF_TARGET_NAME} is a single core SoC, the :ref:`CONFIG_FREERTOS_UNICORE` configuration is always set.
  30. - :ref:`CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY` enables backward compatibility with some FreeRTOS macros/types/functions that were deprecated from v8.0 onwards.
  31. Port Configuration
  32. ^^^^^^^^^^^^^^^^^^
  33. All other FreeRTOS related configuration options that are not part of the kernel configuration are exposed via menuconfig under ``Component Config/FreeRTOS/Port``. These options configure aspects such as:
  34. - The FreeRTOS ports themselves (e.g., tick timer selection, ISR stack size)
  35. - Additional features added to the FreeRTOS implementation or ports
  36. Using FreeRTOS
  37. --------------
  38. Application Entry Point
  39. ^^^^^^^^^^^^^^^^^^^^^^^
  40. Unlike Vanilla FreeRTOS, users of FreeRTOS in ESP-IDF **must never call** :cpp:func:`vTaskStartScheduler` and :cpp:func:`vTaskEndScheduler`. Instead, ESP-IDF will start FreeRTOS automatically. Users must define a ``void app_main(void)`` function which acts as the entry point for user's application and is automatically called on ESP-IDF startup.
  41. - Typically, users would spawn the rest of their application's task from ``app_main``.
  42. - The ``app_main`` function is allowed to return at any point (i.e., before the application terminates).
  43. - The ``app_main`` function is called from the ``main`` task.
  44. .. _freertos_system_tasks:
  45. Background Tasks
  46. ^^^^^^^^^^^^^^^^
  47. During startup, ESP-IDF and FreeRTOS will automatically create multiple tasks that run in the background (listed in the the table below).
  48. .. list-table:: List of Tasks Created During Startup
  49. :widths: 10 75 5 5 5
  50. :header-rows: 1
  51. * - Task Name
  52. - Description
  53. - Stack Size
  54. - Affinity
  55. - Priority
  56. * - Idle Tasks (``IDLEx``)
  57. - An idle task (``IDLEx``) is created for (and pinned to) each CPU, where ``x`` is the CPU's number.
  58. - :ref:`CONFIG_FREERTOS_IDLE_TASK_STACKSIZE`
  59. - CPUx
  60. - ``0``
  61. * - FreeRTOS Timer Task (``Tmr Svc``)
  62. - FreeRTOS will create the Timer Service/Daemon Task if any FreeRTOS Timer APIs are called by the application.
  63. - :ref:`CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH`
  64. - CPU0
  65. - :ref:`CONFIG_FREERTOS_TIMER_TASK_PRIORITY`
  66. * - Main Task (``main``)
  67. - Task that simply calls ``app_main``. This task will self delete when ``app_main`` returns
  68. - :ref:`CONFIG_ESP_MAIN_TASK_STACK_SIZE`
  69. - :ref:`CONFIG_ESP_MAIN_TASK_AFFINITY`
  70. - ``1``
  71. * - IPC Tasks (``ipcx``)
  72. - When :ref:`CONFIG_FREERTOS_UNICORE` is false, an IPC task (``ipcx``) is created for (and pinned to) each CPU. IPC tasks are used to implement the Inter-processor Call (IPC) feature.
  73. - :ref:`CONFIG_ESP_IPC_TASK_STACK_SIZE`
  74. - CPUx
  75. - ``24``
  76. * - ESP Timer Task (``esp_timer``)
  77. - ESP-IDF will create the ESP Timer Task used to process ESP Timer callbacks.
  78. - :ref:`CONFIG_ESP_TIMER_TASK_STACK_SIZE`
  79. - CPU0
  80. - ``22``
  81. .. note::
  82. Note that if an application uses other ESP-IDF features (e.g., WiFi or Bluetooth), those features may create their own background tasks in addition to the tasks listed in the table above.
  83. FreeRTOS Additions
  84. ------------------
  85. ESP-IDF provides some supplemental features to FreeRTOS such as Ring Buffers, ESP-IDF style Tick and Idle Hooks, and TLSP deletion callbacks. See :doc:`freertos_additions` for more details.
  86. FreeRTOS Heap
  87. -------------
  88. Vanilla FreeRTOS provides its own `selection of heap implementations <https://www.freertos.org/a00111.html>`_. However, ESP-IDF already implements its own heap (see :doc:`/api-reference/system/mem_alloc`), thus ESP-IDF does not make use of the heap implementations provided by Vanilla FreeRTOS. All FreeRTOS ports in ESP-IDF map FreeRTOS memory allocation/free calls (e.g., ``pvPortMalloc()`` and ``pvPortFree()``) to ESP-IDF heap API (i.e., :cpp:func:`heap_caps_malloc` and :cpp:func:`heap_caps_free`). However, the FreeRTOS ports ensure that all dynamic memory allocated by FreeRTOS is placed in internal memory.
  89. .. note::
  90. If users wish to place FreeRTOS objects in external memory, users should allocate those objects manually using :cpp:func:`heap_caps_malloc`, then create the object using the object's ``...CreateStatic()`` function.