# Chapter 10: The Same Thing with FreeRTOS ## Sample code - Directory: `docs/os-on-arduino/code/10_freertos` ```bash uv run pio run -d 10_freertos -t upload uv run pio device monitor -b 115200 ``` The real-FreeRTOS examples are in `10_freertos_real`, one environment (the build configuration picked with `-e`) per example. Replace `integrated` with `basic`, `preemptive`, `task_control`, `semaphore` or `queue` for the others. ```bash uv run pio run -d 10_freertos_real -e integrated -t upload ``` ## Source code The sketch for this chapter. Click a file name to expand. The code is mirrored automatically from the manuscript repository, so what you see here is always current. ### `docs/os-on-arduino/code/10_freertos` :::{dropdown} `src/main.cpp` — 229 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos/src/main.cpp) ```{literalinclude} ../../../../code/10_freertos/src/main.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `include/servo_pwm.h` — 61 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos/include/servo_pwm.h) ```{literalinclude} ../../../../code/10_freertos/include/servo_pwm.h :language: cpp :linenos: ``` ::: ### `docs/os-on-arduino/code/10_freertos_real` :::{dropdown} `src/basic.cpp` — 35 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos_real/src/basic.cpp) ```{literalinclude} ../../../../code/10_freertos_real/src/basic.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/integrated.cpp` — 103 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos_real/src/integrated.cpp) ```{literalinclude} ../../../../code/10_freertos_real/src/integrated.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/preemptive.cpp` — 48 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos_real/src/preemptive.cpp) ```{literalinclude} ../../../../code/10_freertos_real/src/preemptive.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/queue.cpp` — 54 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos_real/src/queue.cpp) ```{literalinclude} ../../../../code/10_freertos_real/src/queue.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/semaphore.cpp` — 44 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos_real/src/semaphore.cpp) ```{literalinclude} ../../../../code/10_freertos_real/src/semaphore.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/task_control.cpp` — 62 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/10_freertos_real/src/task_control.cpp) ```{literalinclude} ../../../../code/10_freertos_real/src/task_control.cpp :language: cpp :linenos: ``` ::: ## On the board ```{figure} ../_static/ch10_freertos.gif :name: fig-ch10-freertos :width: 100% `10_freertos` running with the monitor open. The tasks are written against a FreeRTOS-style API (`xTaskCreate` and so on) and run by a simple scheduler inside `loop()`; the `[FreeRTOS Task]` task increments `Count` once a second. ``` ```{figure} ../_static/ch10_freertos_real.gif :name: fig-ch10-freertos-real :width: 100% The real-FreeRTOS `integrated` example, flashed with `uv run pio run -d 10_freertos_real -e integrated -t upload` (`-e` names an environment, i.e. a build configuration, in platformio.ini). Every 2 s it prints the FreeRTOS tick count, the free heap and the A0 reading. With nothing connected to A0, `Pot value` floats around 200; with a potentiometer it follows the knob (0–1023). ``` ## Pitfalls (Collected from reader questions — ask via [GitHub Issues](https://github.com/iory/build-your-own-arduino-rtos/issues).)