FreeRTOS是一个面向嵌入式设备的实时操作系统内核,目前已移植超过 40 个微控制器平台,AWS正在开发SMP版本。它采用MIT 许可证发行,商用免费。
最初由英国人 Richard Barry 于 2003 年左右开发。2018年美国亚马逊收购FreeRTOS,并把协议从GPLv2为MIT,AWS于2020年发布首个LTS版本,以下是LTS列表:
202012.00-LTS:发布于2020年12月 (首个)。202104.00-LTS:发布于2021年4月。202107.00-LTS:发布于2021年7月。202206.00-LTS:发布于2022年6月。202406.00-LTS:发布于2024年6月。
下面会在Ubuntu上,通过树莓派Pico和esp32-c6使用FreeRTOS。
1. 树莓派Pico
1.1 安装Pico开发环境
步骤1.1是公共部分,步骤1.2和步骤1.3是依赖于步骤1.1的两个独立的步骤,任选其一即可。
$ sudo apt install build-essential flex bc gawk texinfo file liblz4-dev ssh git libssl-dev libncurses-dev cmake
$ sudo apt install gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib pkg-config libusb-1.0-0-dev
下载pico-sdk和picotool:
$ mkdir -p ~/pico
$ cd ~/pico
$ git clone --recurse-submodules https://github.com/raspberrypi/pico-sdk.git
$ git clone https://github.com/raspberrypi/picotool.git
设置sdk环境变量:
$ export PICO_SDK_PATH=$HOME/pico/pico-sdk
这个是临时变量,如果需要永久变量,就把这条命令同时写入'~/.bashrc'。
编译安装picotool:
$ cd ~/pico/picotool/
$ mkdir build
$ cd build/
$ cmake ..
$ make -j4
$ sudo make install
1.2 FreeRTOS-SMP-Demos (已废弃)
设置pico-sdk环境变量:
$ export PICO_SDK_PATH=$HOME/pico/pico-sdk
下载编译FreeRTOS-SMP-Demos:
$ mkdir ~/rtos
$ cd ~/rtos
$ git clone --recurse-submodules https://github.com/FreeRTOS/FreeRTOS-SMP-Demos.git
$ cd FreeRTOS/Demo/CORTEX_M0+_RP2040
$ mkdir build
$ cd build
$ cmake ..
$ make -j4
生成文件如下:
$ cd ~/rtos/FreeRTOS-SMP-Demos/FreeRTOS/Demo/CORTEX_M0+_RP2040/build
$ tree -L 2 .
.
├── ...
├── OnEitherCore
│ ├── ...
│ ├── on_core_one.uf2 # 多核实验.
│ └── on_core_zero.uf2 # 多核实验.
└── Standard
├── ...
├── main_blinky.uf2 # 板载LED闪灯实验.
└── main_full.uf2 # 综合实验.
按住Pico开发板的bootsel键,插入电脑,直接把'build'中的'.uf2'拖进Pico存储即可看到效果,可以都试一下。
1.3 FreeRTOS-Community-Supported-Demos
下载FreeRTOS和FreeRTOS-Community-Supported-Demos,前者是核心代码,后者是示例代码:
$ mkdir ~/rtos
$ cd ~/rtos
$ git clone --recurse-submodules https://github.com/FreeRTOS/FreeRTOS.git
$ git clone --recurse-submodules https://github.com/FreeRTOS/FreeRTOS-Community-Supported-Demos.git
声明变量:
$ export PICO_SDK_PATH=$HOME/pico/pico-sdk
$ export FREERTOS_KERNEL_PATH=$HOME/rtos/FreeRTOS/FreeRTOS/Source
$ export FREERTOS_DEMO_PATH=$HOME/rtos/FreeRTOS/FreeRTOS/Demo
编译:
$ cd ~/rtos/FreeRTOS-Community-Supported-Demos/CORTEX_M0+_RP2040
$ mkdir build
$ cd build
$ cmake ..
$ make -j4
如果不想声明变量,则可以添加编译参数:
$ cd ~/rtos/FreeRTOS-Community-Supported-Demos/CORTEX_M0+_RP2040
$ mkdir build
$ cd build
$ cmake -DPICO_SDK_PATH=$HOME/pico/pico-sdk \
-DFREERTOS_KERNEL_PATH=$HOME/rtos/FreeRTOS/FreeRTOS/Source \
-DFREERTOS_DEMO_PATH=$HOME/rtos/FreeRTOS/FreeRTOS/Demo \
-DCMAKE_BUILD_TYPE=Release \
-DPICO_PLATFORM=rp2040 \
..
$ make -j4
Note: 上述cmake命令中的'..'不要漏掉。
生成文件如下:
$ cd ~/rtos/FreeRTOS-Community-Supported-Demos/CORTEX_M0+_RP2040/build
$ tree -L 2 .
.
├── ...
├── OnEitherCore
│ ├── ...
│ ├── on_core_one.uf2
│ └── on_core_zero.uf2
├── Standard
│ ├── ...
│ ├── main_blinky.uf2
│ └── main_full.uf2
├── Standard_smp
│ ├── ...
│ ├── main_blinky_smp.uf2
│ └── main_full_smp.uf2
└── UsingCMSIS
├── ...
└── using_cmsis.uf2
按住Pico开发板的bootsel键,插入电脑,直接把其中的'.uf2'拖进Pico存储即可看到效果,可以都试一下。
2. ESP32-C6
esp-idf里有FreeRTOS的示例,我们使用其中的示例测试。
2.1 安装环境
首先,需要安装esp-idf (这里使用的版本号是esp-idf-v5.4.1),可以参考以下链接之一:
1. 搭建ESP-IDF环境: https://www.cnblogs.com/phoebus-ma/p/18831476
2. 官方: https://docs.espressif.com/projects/esp-idf/zh_CN/v5.4.1/esp32c6/get-started/linux-macos-setup.html
2.2 编译刷机
$ get_idf
$ cp -rf esp-idf/examples/system/freertos/basic_freertos_smp_usage/ ~/
$ cd ~/basic_freertos_smp_usage
$ idf.py set-target esp32c6
$ idf.py build
$ idf.py -p /dev/ttyACM0 flash
其中的USB端口,以实际为主,这里是'/dev/ttyACM0'。
2.3 测试
使用串口调试工具,如minicom打开esp32-c6开发板的串口,会打印内容如下:
Type 'help' to get the list of commands.
Use UP/DOWN arrows to navigate through command history.
Press TAB when typing command name to auto-complete.
Please type the component you would like to run.
I (318) main_task: Returned from app_main()
esp32c6>
根据提示,输入'help',查看怎么使用:
esp32c6> help
help [
Print the summary of all registered commands if no arguments are given,
otherwise print summary of given command.
-v, --verbose=<0|1> If specified, list console commands with given verbose level
create_task
Run the example that demonstrates how to create and run pinned and unpinned
tasks
queue
Run the example that demonstrates how to use queue to communicate between
tasks
lock
Run the example that demonstrates how to use mutex and spinlock to protect a
shared resource
task_notification
Run the example that demonstrates how to use task notifications to
synchronize tasks
batch_processing
Run the example that combines queue, mutex, task notification together
可知,有5个示例,分别是任务创建、队列、竞争锁、通知和批处理,输入对应的命令,即可看到效果,如输入'create_task',现象是创建了多个task交替运行在core 0。
Note: esp32-c6是非对称双核,官方的示例只是运行在core 0,如果是对称双核如esp32-s3,可能是双核运行)。