前言
FreeRTOS是一个开源的实时操作系统,广泛应用于嵌入式领域。相较于裸机系统,采用嵌入式实时操作系统(RTOS)可以更合理、更有效地利用 CPU 的资源,简化应用软件的设计,缩短系统开发时间,更好地保证系统的实时性和可靠性。FreeRTOS 作为一个轻量级的操作系统,功能包括:任务管理、时间管理、信号量、消息队列、内存管理、记录功能、软件定时器、协程等,可基本满足较小系统的需要。FreeRTOS 由美国的Richard Barry于2003年发布,Richard Barry是FreeRTOS的拥有者和维护者,在过去的十多年 中 FreeRTOS 历经了9个版本,与众多半导体厂商合作密切,累计开发者数百万,是目前市场占有率最高的RTOS。 FreeRTOS于2018年被亚马逊收购,改名为AWS FreeRTOS,版本号升级为V10,且开源协议也由原来的GPLv2+修改为 MIT,与GPLv2+相比,MIT更加开放,你完全可以理解为是为所欲为的免费。V9以前的版本还是维持原样,V10版本相 比于V9就是加入了一些物联网相关的组件,内核基本不变。
为什么要使用 FreeRTOS
从技术上讲,并非在所有情况下都需要 RTOS。一些嵌入式应用可能非常简单,例如从传感器读取值并将这些值发送到输出。在这种情况下,不需要 RTOS。然而,随着 MCU 的功能越来越强大,嵌入式应用变得越来越复杂,使用 RTOS 的好处也越来越大。 多任务处理是使用 RTOS 的重要原因之一,也是 FreeRTOS 的一项关键功能。 什么是多任务处理?它是一种在嵌入式应用程序中运行多个任务的方法,以便这些任务可以彼此独立执行,同时仍共享同一处理器。任务执行由所谓的调度程序管理。对于单核微控制器,调度程序的工作是根据指定的优先级和其他因素为任务提供执行时间。这可能会给人一种同时执行多个任务的感觉,即使调度程序实际上是通过在毫秒级任务之间快速连续切换来管理执行。安装配置 FreeRTOS
树莓派 C/C++ SDK 中并未包含 FreeRTOS,所以你需要将 FreeRTOS 先安装到 Mac 上,并且配置环境变量。
cd ~
# 使用 git clone 命令下载 FreeRTOS 内核
git clone -b smp https://github.com/FreeRTOS/FreeRTOS-Kernel --recurse-submodules
# 配置环境变量
vim .zprofile
export FREERTOS_KERNEL_PATH=$HOME/FreeRTOS-Kernel
source .zprofile
在 Pico 上使用 FreeRTOS
下面我们通过一个简单的示例程序来学习如何使用 FreeRTOS。拷贝cmake 文件和配置文件
创建项目文件目录,并且将FreeRTOS_Kernel_import.cmake 和 pico_sdk_import.cmake 文件拷贝到项目根目录下,并且创建项目所需文件。我们还需要一个“FreeRTOSConfig.h”文件来为我们的项目配置 FreeRTOS。FreeRTOSConfig 文件内容如下:
mkdir blink_freertos
cd blink_freertos
touch blink_freertos.c CMakeLists.txt FreeRTOSConfig.h
cp $PICO_SDK_PATH/external/pico_sdk_import.cmake .
cp $FREERTOS_KERNEL_PATH/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake .
/*
* FreeRTOS V202111.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html
*----------------------------------------------------------*/
/* Scheduler Related */
#define configUSE_PREEMPTION 1
#define configUSE_TICKLESS_IDLE 0
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES 32
#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 1024
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
/* Synchronization Related */
#define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configUSE_QUEUE_SETS 1
#define configUSE_TIME_SLICING 1
#define configUSE_NEWLIB_REENTRANT 0
// todo need this for lwip FreeRTOS sys_arch to compile
#define configENABLE_BACKWARD_COMPATIBILITY 1
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
/* System */
#define configSTACK_DEPTH_TYPE uint32_t
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
/* Memory allocation related definitions. */
#define configSUPPORT_STATIC_ALLOCATION 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configTOTAL_HEAP_SIZE (128*1024)
#define configAPPLICATION_ALLOCATED_HEAP 0
/* Hook function related definitions. */
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configUSE_MALLOC_FAILED_HOOK 0
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
/* Co-routine related definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES 1
/* Software timer related definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH 1024
/* Interrupt nesting behaviour configuration. */
/*
#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor]
#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application]
#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application]
*/
#if FREE_RTOS_KERNEL_SMP // set by the RP2040 SMP port of FreeRTOS
/* SMP port only */
#define configNUM_CORES 2
#define configTICK_CORE 0
#define configRUN_MULTIPLE_PRIORITIES 1
#define configUSE_CORE_AFFINITY 1
#endif
/* RP2040 specific */
#define configSUPPORT_PICO_SYNC_INTEROP 1
#define configSUPPORT_PICO_TIME_INTEROP 1
#include <assert.h>
/* Define to trap errors during development. */
#define configASSERT(x) assert(x)
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTaskGetSchedulerState 1
#define INCLUDE_xTaskGetCurrentTaskHandle 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#define INCLUDE_xTaskGetIdleTaskHandle 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xTaskResumeFromISR 1
#define INCLUDE_xQueueGetMutexHolder 1
/* A header file that defines trace macro can be included here. */
#endif /* FREERTOS_CONFIG_H */
配置 CMamkeLists.txt
前面的教程我们已经学习了如何配置 CMakeLists.txt,在这里我们只需要多添加几项配置:
- 额外添加导入 FreeRTOS cmake 文件。
- 在 target_link_libraries 中添加 FreeRTOS-Kernel-Heap4。
- 添加 target_include_directories 导入 FreeRTOSConfig.h。
# 设置Cmake 最小依赖版本
cmake_minimum_required(VERSION 3.17)
# 设置c/c++ 编译版本
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# 导入 pico sdk cmake
include(pico_sdk_import.cmake)
include(FreeRTOS_Kernel_import.cmake)
# ====================================================================================
# 设置开发板为 pico w (如果你的开发板是 pico 可以注释这行)
set(PICO_BOARD pico_w CACHE STRING "Board type")
# 设置项目名
project(blink_freertos C CXX ASM)
# 初始化sdk
pico_sdk_init()
# 添加执行文件
add_executable(blink_freertos
blink_freertos.c
)
# 设置项目名称字符串
pico_set_program_name(blink_freertos "hello_usb")
# 设置项目版本号
pico_set_program_version(blink_freertos "0.1")
# Modify the below lines to enable/disable output over UART/USB
# 是否打开 UART 串口
pico_enable_stdio_uart(blink_freertos 0)
# 是否打开USB 串口
pico_enable_stdio_usb(blink_freertos 1)
# 添加依赖库 (pico w 必须加入 pico_cyw43_arch_none )
target_link_libraries(blink_freertos
pico_stdlib # for core functionality
pico_cyw43_arch_none
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
)
# Add the standard include files to the build
target_include_directories(blink_freertos PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
)
# 设置输出文件
pico_add_extra_outputs(blink_freertos)
blink_freertos.c
现在我们来使用 C 语言编写一个简单 FreeRTOS 应用程序。它使用FreeRTOS 的 xTaskCreate 函数创建了个两个任务,一个任务是使板载的 LED 闪烁,另外一个任务是打印 “Hello, world!”。最后使用 vTaskStartScheduler 函数来启动这些任务。代码如下所示:使用之前学习的编译命令,编译 uf2 文件并烧录到 树莓派 Pico 中。
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "FreeRTOS.h"
#include "task.h"
void led_task(pvParameters)
{
// const uint LED_PIN = PICO_DEFAULT_LED_PIN;
// gpio_init(LED_PIN);
// gpio_set_dir(LED_PIN, GPIO_OUT);
while (true) {
// gpio_put(LED_PIN, 1);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
vTaskDelay(100);
// gpio_put(LED_PIN, 0);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
vTaskDelay(100);
}
}
void sayHelloWorld(pvParameters)
{
while (true) {
printf("Hello, world!\n");
vTaskDelay(1000);
}
}
int main()
{
stdio_init_all();
// Initialise the Wi-Fi chip
if (cyw43_arch_init()) {
printf("Wi-Fi init failed\n");
return -1;
}
xTaskCreate(led_task, "LED_Task", 256, NULL, 1, NULL);
xTaskCreate(sayHelloWorld, "Hello_Task", 256, NULL, 1, NULL);
vTaskStartScheduler();
while(1){};
}
如果你正常烧录成功,你应该可以看到 Raspberry Pi Pico 上的 LED 闪烁,此时,使用 CoolTerm 连接 Pico ,可以同时看到打印的 “Hello, world!” 信息。
mkdir build && cd build
cmake ..
make -j4
总结
在本教程中,我们通过一个简单的 Demo 来讲解如何按照配置 FreeRTOS 以及如何使用 FreeRTOS 来执行多任务。
源代码:https://github.com/xtcel/Raspberry_Pi_Pico_C_Tutorial/tree/master/blink_freertos