RTOS基础1(FreeRTOS+树莓派PICO环境)

1 前置阅读

最近遇到很多需要RTOS的项目,正好也有点时间,自己也有点兴趣,开发板环境啥的都有现成的,所以也就学习一下。

参考文档:

读完FreeRTOS内核源码,需要多久?

有哪些情况下是RTOS满足不了需求,必须得上嵌入式Linux系统的?

如何进行 RTOS内核的裁剪与优化?

============================

2 环境搭建和编译

这次的硬件是树莓派PICO,还有FreeRTOS,本来是打算直接下载PICO SDK还有FreeRTOS,然后自己写一个例程进行单独编译的,但是在腾讯云上实在有太多的被墙和下载的问题:

没办法,继续看一下别人是怎么解决的,后面看到树莓派热心大佬的一个帖子:

https://forums.raspberrypi.com/viewtopic.php?t=334622

还有一个懒人包:

https://github.com/LearnEmbeddedSystems/rp2040-freertos-project

里面的例程也是经典的LED眨眼,直接下下来,在ProjectFiles中创建make。配置好PICO的SDK,就可以直接用了。

ubuntu@VM-8-10-ubuntu:~/test/freertos/rp2040-freertos-project/build$ export PICO_SDK_PATH=/home/ubuntu/test/freertos/pico-sdk/
ubuntu@VM-8-10-ubuntu:~/test/freertos/rp2040-freertos-project/build$ cmake ..
Using PICO_SDK_PATH from environment ('/home/ubuntu/test/freertos/pico-sdk/')
PICO_SDK_PATH is /home/ubuntu/test/freertos/pico-sdk
Defaulting platform (PICO_PLATFORM) to 'rp2040' since not specified.
Defaulting target board (PICO_BOARD) to 'pico' since not specified.
Using board configuration from /home/ubuntu/test/freertos/pico-sdk/src/boards/include/boards/pico.h
Pico Platform (PICO_PLATFORM) is 'rp2040'.
-- Defaulting build type to 'Release' since not specified.
Defaulting compiler (PICO_COMPILER) to 'pico_arm_cortex_m0plus_gcc' since not specified.
Configuring toolchain based on PICO_COMPILER 'pico_arm_cortex_m0plus_gcc'
Defaulting PICO_GCC_TRIPLE to 'arm-none-eabi'
-- The C compiler identification is GNU 13.2.1
-- The CXX compiler identification is GNU 13.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/arm-none-eabi-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/arm-none-eabi-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/arm-none-eabi-gcc
Build type is Release
-- Found Python3: /usr/bin/python3 (found version "3.12.3") found components: Interpreter
TinyUSB available at /home/ubuntu/test/freertos/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.
BTstack available at /home/ubuntu/test/freertos/pico-sdk/lib/btstack
cyw43-driver available at /home/ubuntu/test/freertos/pico-sdk/lib/cyw43-driver
lwIP available at /home/ubuntu/test/freertos/pico-sdk/lib/lwip
mbedtls available at /home/ubuntu/test/freertos/pico-sdk/lib/mbedtls
CMake Warning at /home/ubuntu/test/freertos/pico-sdk/tools/Findpicotool.cmake:28 (message):
  No installed picotool with version 2.1.0 found - building from source

  It is recommended to build and install picotool separately, or to set
  PICOTOOL_FETCH_FROM_GIT_PATH to a common directory for all your SDK
  projects
Call Stack (most recent call first):
  /home/ubuntu/test/freertos/pico-sdk/tools/CMakeLists.txt:138 (find_package)
  /home/ubuntu/test/freertos/pico-sdk/tools/CMakeLists.txt:485 (pico_init_picotool)
  /home/ubuntu/test/freertos/pico-sdk/src/cmake/on_device.cmake:57 (picotool_postprocess_binary)
  ProjectFiles/CMakeLists.txt:6 (pico_add_extra_outputs)


Downloading Picotool
-- Configuring done (6.8s)
-- Generating done (0.1s)
-- Build files have been written to: /home/ubuntu/test/freertos/rp2040-freertos-project/build
ubuntu@VM-8-10-ubuntu:~/test/freertos/rp2040-freertos-project/build$ make
[  1%] Building ASM object pico-sdk/src/rp2040/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.o
[  2%] Linking ASM executable bs2_default.elf
[  2%] Built target bs2_default
[  3%] Generating bs2_default.bin
[  4%] Generating bs2_default_padded_checksummed.S
[  6%] Building ASM object pico-sdk/src/rp2040/boot_stage2/CMakeFiles/bs2_default_library.dir/bs2_default_padded_checksummed.S.o
[  6%] Built target bs2_default_library
[  7%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/event_groups.c.o
[  8%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/list.c.o
[  9%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/queue.c.o
[ 11%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/stream_buffer.c.o
[ 12%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/tasks.c.o
[ 13%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/timers.c.o
[ 14%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/portable/MemMang/heap_3.c.o
[ 16%] Building C object freertos/CMakeFiles/freertos.dir/FreeRTOS-Kernel/portable/GCC/ARM_CM0/port.c.o
[ 17%] Linking C static library libfreertos.a
[ 17%] Built target freertos
[ 17%] Built target picotoolForceReconfigure
[ 18%] Creating directories for 'picotoolBuild'
[ 19%] No download step for 'picotoolBuild'
[ 20%] No update step for 'picotoolBuild'
[ 22%] No patch step for 'picotoolBuild'
[ 23%] Performing configure step for 'picotoolBuild'
Not searching for unused variables given on the command line.
-- The C compiler identification is GNU 13.3.0
-- The CXX compiler identification is GNU 13.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
.//home/ubuntu/test/freertos/rp2040-freertos-project/build/_deps/picotool/
-- Using the single-header code from /home/ubuntu/test/freertos/rp2040-freertos-project/build/_deps/picotool-src/lib/nlohmann_json/single_include/
CMake Deprecation Warning at /home/ubuntu/test/freertos/pico-sdk/lib/mbedtls/CMakeLists.txt:23 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Found Python3: /usr/bin/python3 (found version "3.12.3") found components: Interpreter
-- Performing Test C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS
-- Performing Test C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS - Success
PICOTOOL_NO_LIBUSB is set - no USB support will be built
-- Configuring done (1.0s)
-- Generating done (0.1s)
-- Build files have been written to: /home/ubuntu/test/freertos/rp2040-freertos-project/build/_deps/picotool-build
[ 24%] Performing build step for 'picotoolBuild'
[ 67%] Built target mbedcrypto
[ 76%] Built target mbedx509
[ 86%] Built target mbedtls
[ 88%] Built target errors
[ 89%] Built target elf
[ 92%] Built target bintool
[ 94%] Built target elf2uf2
[100%] Built target picotool
[ 25%] Performing install step for 'picotoolBuild'
[ 67%] Built target mbedcrypto
[ 76%] Built target mbedx509
[ 86%] Built target mbedtls
[ 88%] Built target errors
[ 89%] Built target elf
[ 92%] Built target bintool
[ 94%] Built target elf2uf2
[100%] Built target picotool
Install the project...
-- Install configuration: "Release"
[ 27%] Performing test step for 'picotoolBuild'
picotool v2.1.1-develop (Linux, GNU-13.3.0, Release)
[ 28%] Completed 'picotoolBuild'
[ 28%] Built target picotoolBuild
[ 29%] Building C object ProjectFiles/CMakeFiles/blink.dir/main.c.o
[ 30%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_stdlib/stdlib.c.o
[ 32%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_gpio/gpio.c.o
[ 33%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2040/pico_platform/platform.c.o
[ 34%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_platform_panic/panic.c.o
[ 35%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/hardware_claim/claim.c.o
[ 37%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_sync/sync.c.o
[ 38%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_sync_spin_lock/sync_spin_lock.c.o
[ 39%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_irq/irq.c.o
[ 40%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_irq/irq_handler_chain.S.o
[ 41%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_sync/sem.c.o
[ 43%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_sync/lock_core.c.o
[ 44%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_sync/mutex.c.o
[ 45%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_sync/critical_section.c.o
[ 46%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_time/time.c.o
[ 48%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_time/timeout_helper.c.o
[ 49%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_timer/timer.c.o
[ 50%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_util/datetime.c.o
[ 51%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_util/pheap.c.o
[ 53%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/common/pico_util/queue.c.o
[ 54%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_uart/uart.c.o
[ 55%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_clocks/clocks.c.o
[ 56%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_pll/pll.c.o
[ 58%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_vreg/vreg.c.o
[ 59%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_watchdog/watchdog.c.o
[ 60%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_ticks/ticks.c.o
[ 61%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_bootrom/bootrom.c.o
[ 62%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_bootrom/bootrom_lock.c.o
[ 64%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_boot_lock/boot_lock.c.o
[ 65%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_flash/flash.c.o
[ 66%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_xosc/xosc.c.o
[ 67%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/hardware_divider/divider.S.o
[ 69%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_runtime/runtime.c.o
[ 70%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_runtime_init/runtime_init.c.o
[ 71%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_runtime_init/runtime_init_clocks.c.o
[ 72%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_runtime_init/runtime_init_stack_guard.c.o
[ 74%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_bit_ops/bit_ops_aeabi.S.o
[ 75%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_divider/divider_hardware.S.o
[ 76%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_double/double_aeabi_rp2040.S.o
[ 77%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_double/double_init_rom_rp2040.c.o
[ 79%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_double/double_math.c.o
[ 80%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_double/double_v1_rom_shim_rp2040.S.o
[ 81%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_int64_ops/pico_int64_ops_aeabi.S.o
[ 82%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_float/float_aeabi_rp2040.S.o
[ 83%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_float/float_init_rom_rp2040.c.o
[ 85%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_float/float_math.c.o
[ 86%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_float/float_v1_rom_shim_rp2040.S.o
[ 87%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_malloc/malloc.c.o
[ 88%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_mem_ops/mem_ops_aeabi.S.o
[ 90%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_atomic/atomic.c.o
[ 91%] Building CXX object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_cxx_options/new_delete.cpp.o
[ 92%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_standard_binary_info/standard_binary_info.c.o
[ 93%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_printf/printf.c.o
[ 95%] Building ASM object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_crt0/crt0.S.o
[ 96%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_clib_interface/newlib_interface.c.o
[ 97%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_stdio/stdio.c.o
[ 98%] Building C object ProjectFiles/CMakeFiles/blink.dir/home/ubuntu/test/freertos/pico-sdk/src/rp2_common/pico_stdio_uart/stdio_uart.c.o
[100%] Linking CXX executable blink.elf
[100%] Built target blink

生成物如下:

其中PICO要的就是blink.uf2这个文件。直接按住树莓派的BOOTSEL启动,将blink.uf2文件拷贝进去新出现的文件夹,就可以看到效果了。

3 代码阅读

代码结构如下:

其中build是自己建的。项目文件是在ProjectFiles里面。就是两个,CMakeLists.txt和main.c。这次主要关注项目代码。

BTW:cmake之前很简单写过,可以参见:CMake小结_外层cmakelists和内层cmakelists-优快云博客

CMakeLists.txt

add_executable(blink
        main.c
)

target_link_libraries(blink pico_stdlib freertos)
pico_add_extra_outputs(blink)

这里指定了blink的源代码main.c,然后链接的静态库pico_stdlib和freertos,最后是增加uf2的输出。

pico_stdlib,定义在pico-sdk\src\rp2_common\pico_stdlib\CMakeLists.txt(真感觉现在的新库cmake都用得来飞起。。。)

if (NOT TARGET pico_stdlib)
    pico_add_impl_library(pico_stdlib)
    target_sources(pico_stdlib INTERFACE
            ${CMAKE_CURRENT_LIST_DIR}/stdlib.c
    )

    set(PICO_STDLIB_LIBRARIES
            hardware_gpio
            hardware_uart
            hardware_divider
            pico_time
            pico_util
            pico_platform
            pico_runtime
            pico_stdio
    )
    foreach(LIB IN LISTS PICO_STDLIB_LIBRARIES)
        if (TARGET ${LIB})
            pico_mirrored_target_link_libraries(pico_stdlib INTERFACE ${LIB})
        endif()
    endforeach()
endif()

freerots目标定义在freerots根目录的CMakelists.txt如下:

set(PICO_SDK_FREERTOS_SOURCE FreeRTOS-Kernel)

add_library(freertos
    ${PICO_SDK_FREERTOS_SOURCE}/event_groups.c
    ${PICO_SDK_FREERTOS_SOURCE}/list.c
    ${PICO_SDK_FREERTOS_SOURCE}/queue.c
    ${PICO_SDK_FREERTOS_SOURCE}/stream_buffer.c
    ${PICO_SDK_FREERTOS_SOURCE}/tasks.c
    ${PICO_SDK_FREERTOS_SOURCE}/timers.c
    ${PICO_SDK_FREERTOS_SOURCE}/portable/MemMang/heap_3.c
    ${PICO_SDK_FREERTOS_SOURCE}/portable/GCC/ARM_CM0/port.c
)

看着好像就是几个文件。。。这么少吗?后面抽时间再多读读。。。

pico_add_extra_outputs定义如下:

function(pico_add_extra_outputs TARGET)
    # Disassembly will be nonsense for encrypted binaries,
    # so disassemble before picotool processing
    pico_add_dis_output(${TARGET})

    # Picotool processing (signing/encrypting/etc)
    # PICO_CMAKE_CONFIG: PICO_NO_PICOTOOL, Disable use/requirement for picotool meaning that UF2 output and signing/hashing and coprocoessor disassembly will all be unavailable, type=bool, default=0, group=build
    if (NOT PICO_NO_PICOTOOL)
        picotool_postprocess_binary(${TARGET} IS_ENCRYPTED)
    endif()

    if (PICO_32BIT)
        pico_add_hex_output(${TARGET})
    endif()
    pico_add_bin_output(${TARGET})
    pico_add_map_output(${TARGET})

    # PICO_CMAKE_CONFIG: PICO_NO_TARGET_NAME, Don't define PICO_TARGET_NAME, type=bool, default=0, group=build
    # PICO_BUILD_DEFINE: PICO_TARGET_NAME, Name of the build target being compiled (unless PICO_NO_TARGET_NAME set in build), type=string, default=target name, group=build
    if (NOT PICO_NO_TARGET_NAME)
        target_compile_definitions(${TARGET} PRIVATE
                PICO_TARGET_NAME="${TARGET}"
                )
    endif()

    if (PICO_SYMLINK_ELF_AS_FILENAME)
        add_custom_target(${TARGET}_symlinked)
        add_dependencies(${TARGET}_symlinked ${TARGET})

        add_custom_command(TARGET ${TARGET}_symlinked POST_BUILD
                COMMAND rm -f "${PICO_SYMLINK_ELF_AS_FILENAME}"
                COMMAND ln -s -r $<TARGET_FILE:${TARGET}> "${PICO_SYMLINK_ELF_AS_FILENAME}"
                COMMENT "Symlinking from ${PICO_SYMLINK_ELF_AS_FILENAME} to ${TARGET}"
                )
    endif ()
    # PICO_CMAKE_CONFIG: PICO_NO_UF2, Disable UF2 output, type=bool, default=0, group=build
    if (NOT (PICO_NO_UF2 OR PICO_NO_PICOTOOL))
        pico_add_uf2_output(${TARGET})
    endif()
endfunction()

就是把bin转成uf2格式。感觉像是picotool里面的elf2uf2,不过现在没有证据,以后有时间再看看。。。

main.c如下:

#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
#include "pico/stdlib.h"


void led_task()
{   
    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);
        vTaskDelay(100);
        gpio_put(LED_PIN, 0);
        vTaskDelay(100);
    }
}

int main()
{
    stdio_init_all();

    xTaskCreate(led_task, "LED_Task", 256, NULL, 1, NULL);
    vTaskStartScheduler();

    while(1){};
}

可以看到。Freertos的使用还是很简单的。

stdio_init_all()是pico的初始化。

xTaskCreate的操作很类似增加线程,但是里面的API要使用xtask和vtask这几个。然后使用vTaskStartScheduler打开调度器。

最后一个while(1)死循环。

进一步参考:Build your first FreeRTOS project - FreeRTOS™

4 参考

如何用树莓派 Pico 学习 RTOS?

树莓派 Pico 使用 RP2040 微控制器,基于 ARM Cortex-M0+ 内核,支持各种 RTOS(实时操作系统)。如果你想学习 RTOS,Pico 是一个很好的平台,以下是系统学习路径:

1. 选择合适的 RTOS

常见的 RTOS 选项:

  • FreeRTOS(最流行,学习资料多)
  • RT-Thread(国产 RTOS,适合 IoT)
  • Zephyr(适用于物联网和工业)
  • ChibiOS(轻量级,适合小型 MCU)

推荐FreeRTOS 最适合作为入门 RTOS,官方支持好,教程多,使用广泛。

2. 准备开发环境

硬件

  • Raspberry Pi Pico
  • Micro-USB 线
  • LED、按钮、传感器(扩展实验)

软件

  • CMake + Ninja + ARM GCC(官方推荐)
  • VS Code + Pico SDK(推荐)
  • 或者 Keil / IAR(传统嵌入式开发工具)

3. 安装 FreeRTOS 并配置开发环境

(1) 下载 FreeRTOS

git clone https://github.com/FreeRTOS/FreeRTOS-Kernel.git

或者使用官方仓库:

git clone --recurse-submodules https://github.com/FreeRTOS/FreeRTOS.git

(2) 安装 Raspberry Pi Pico SDK

git clone -b master https://github.com/raspberrypi/pico-sdk.git
cd pico-sdk
git submodule update --init
export PICO_SDK_PATH=$PWD

(3) 配置 FreeRTOS 任务调度器

FreeRTOSConfig.h 中修改:

#define configUSE_PREEMPTION                    1
#define configUSE_TIME_SLICING                  1
#define configCPU_CLOCK_HZ                      (125000000) // RP2040 125MHz
#define configTICK_RATE_HZ                      (1000)
#define configMAX_PRIORITIES                    (5)
#define configMINIMAL_STACK_SIZE                (configSTACK_DEPTH_TYPE)128
#define configTOTAL_HEAP_SIZE                   ((size_t)(16 * 1024))

4. 编写一个 FreeRTOS 任务

创建 main.c,写入以下代码:

#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
#include "pico/stdlib.h"

void Task1(void *pvParameters) {
    while (1) {
        printf("Task 1 running...\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void Task2(void *pvParameters) {
    while (1) {
        printf("Task 2 running...\n");
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

int main() {
    stdio_init_all();
    xTaskCreate(Task1, "Task 1", 256, NULL, 1, NULL);
    xTaskCreate(Task2, "Task 2", 256, NULL, 1, NULL);
    vTaskStartScheduler();
    while (1);
}

说明

  • Task1 每 1000ms 运行一次
  • Task2 每 500ms 运行一次
  • vTaskStartScheduler() 启动任务调度器

5. 编译 & 运行

(1) 编译代码

mkdir build
cd build
cmake ..
make -j4

(2) 烧录到 Pico

  • 进入 BOOTSEL 模式(按住 BOOTSEL 按钮,插入 USB)
  • 拷贝生成的 .uf2 文件到 Pico

(3) 观察输出

使用 minicomscreen 读取串口输出:

screen /dev/ttyUSB0 115200

6. 进阶学习

任务同步 & 互斥

  • 队列(Queue): 任务间通信
  • 信号量(Semaphore):资源管理
  • 互斥量(Mutex):防止数据竞争

中断处理

  • 使用 ISR-safe API,如 xQueueSendFromISR()

低功耗管理

  • Tickless Idle 模式降低功耗

移植 & 设备驱动

  • 在 FreeRTOS 上移植 I2C/SPI/UART 设备驱动

联网 & 远程控制

  • 结合 LWIP(轻量级 TCP/IP) 使 Pico 具备联网功能

7. 推荐学习资源

📚 书籍

  • 《Mastering FreeRTOS》
  • 《嵌入式实时操作系统开发》
  • 《实时操作系统(RTOS)基础》

🎥 视频

📂 官方文档

总结

🔹 用 Raspberry Pi Pico 学习 FreeRTOS 是个很好的选择! 🔹 从简单的任务调度开始,再深入学习任务间通信、中断管理、功耗优化 🔹 Pico 的双核支持,适合研究 FreeRTOS SMP(多核调度)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值