Skip to content

smittytone/RP2040-FreeRTOS

Repository files navigation

RP2040-FreeRTOS Template 1.5.0

This repo contains my base project for FreeRTOS on the Raspberry Pi RP2040 microcontroller. It can be run as a demo and then used as the basis of a new project.

More details in this blog post.

Project Structure

/RP2040-FreeRTOS
|
|___/App-Template           // Application 1 (FreeRTOS template) source code (C)
|   |___CMakeLists.txt      // Application-level CMake config file
|
|___/App-Scheduling         // Application 2 (scheduling demo) source code (C++)
|   |___CMakeLists.txt      // Application-level CMake config file
|
|___/App-IRQs               // Application 3 (IRQs demo) source code (C++)
|   |___CMakeLists.txt      // Application-level CMake config file
|
|___/App-Timers             // Application 4 (timers demo) source code (C++)
|   |___CMakeLists.txt      // Application-level CMake config file
|
|___/Common                 // Source code common to applications 2-4 (C++)
|
|___/Config
|   |___FreeRTOSConfig.h    // FreeRTOS project config file
|
|___/FreeRTOS-Kernel        // FreeRTOS kernel files, included as a submodule
|___/pico-sdk               // Raspberry Pi Pico SDK, included as a submodule
|
|___CMakeLists.txt          // Top-level project CMake config file
|___pico_sdk_import.cmake   // Raspberry Pi Pico SDK CMake import script
|___deploy.sh               // Build-and-deploy shell script
|
|___rp2040.code-workspace   // Visual Studio Code workspace
|___rp2040.xcworkspace      // Xcode workspace
|
|___README.md
|___LICENSE.md

Prerequisites

To use the code in this repo, your system must be set up for RP2040 C/C++ development. See this blog post of mine for setup details.

Usage

  1. Clone the repo: git clone https://github.com/smittytone/RP2040-FreeRTOS.
  2. Enter the repo: cd RP2040-FreeRTOS.
  3. Prepare the submodules: git submodule update --init.
  4. Install Pico SDK submodules: cd pico-sdk && git submodule update --init.
  5. Optionally, edit CMakeLists.txt and /<Application>/CMakeLists.txt to rename the project.
  6. Optionally, manually configure the build process: cmake -S . -B build/.
  7. Optionally, manually build the app: cmake --build build.
  8. Connect your device so it’s ready for file transfer.
  9. Install the app: ./deploy.sh.
    • Pass the app you wish to deplopy:
      • ./deploy.sh build/App-Template/TEMPLATE.uf2.
      • ./deploy.sh build/App-Scheduling/SCHEDULING_DEMO.uf2.
    • To trigger a build, include the --build or -b flag: ./deploy.sh -b.

Debug vs Release

You can switch between build types when you make the cmake call in step 5, above. A debug build is made explicit with:

cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug

For a release build, which among various optimisations omits UART debugging code, call:

cmake -S . -B build -D CMAKE_BUILD_TYPE=Release

Follow both of these commands with the usual

cmake --build build

The Apps

This repo includes a number of deployable apps. The project builds them all, sequentially. Exclude apps from the build process by commenting out their add_subdirectory() lines in the top-level CMakeLists.txt.

App One: Template

This C app provides a simple flip-flop using an on-board LED and an LED wired between GPIO 20 and GND. The board LED flashes every 500ms under one task. When its state changes, a message containing its state is added to a FreeRTOS inter-task xQueue. A second task checks for an enqueued message: if one is present, it reads the message and sets the LED it controls — the GPIO LED — accordingly to the inverse of the board LED’s state.

Circuit layout

The code demonstrates a basic FreeRTOS setup, but you can replace it entirely with your own code if you’re using this repo’s contents as a template for your own projects.

App Two: Scheduling

This C++ app builds on the first by adding an MCP9808 temperature sensor and an HT16K33-based LED display. It is used in this blog post.

Circuit layout

App Three: IRQs

This C++ app builds on the second by using the MCP9808 temperature sensor to trigger an interrupt. It is used in this blog post.

Circuit layout

App Four: Timers

This C++ app provides an introduction to FreeRTOS’ software timers. No extra hardware is required. It is used in this blog post.

IDEs

Workspace files are included for the Visual Studio Code and Xcode IDEs.

Credits

This work was inspired by work done on KORE Wireless Microvisor FreeRTOS Demo code, but the version of the FreeRTOSConfig.h file included here was derived from work by @yunka2.

Copyright and Licences

Application source © 2024, Tony Smith and licensed under the terms of the MIT Licence.

FreeRTOS © 2021, Amazon Web Services, Inc. It is also licensed under the terms of the MIT Licence.

The Raspberry Pi Pico SDK is © 2020, Raspberry Pi (Trading) Ltd. It is licensed under the terms of the BSD 3-Clause "New" or "Revised" Licence.