How to Port FreeRTOS to Your Project

1. Download

How to Port FreeRTOS to Your Project

FreeRTOS Official Website

https://www.freertos.org/

How to Port FreeRTOS to Your Project

2. Source Code Composition

Directory Composition

FreeRTOS-LTS
├── aws
│   ├── aws-iot-core-mqtt-file-streams-embedded-c
│   ├── device-defender-for-aws-iot-embedded-sdk
│   ├── device-shadow-for-aws-iot-embedded-sdk
│   ├── fleet-provisioning-for-aws-iot-embedded-sdk
│   ├── jobs-for-aws-iot-embedded-sdk
│   └── sigv4-for-aws-iot-embedded-sdk
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── FreeRTOS
│   ├── backoffAlgorithm
│   ├── coreHTTP
│   ├── coreJSON
│   ├── coreMQTT
│   ├── corePKCS11
│   ├── coreSNTP
│   ├── FreeRTOS-Cellular-Interface
│   ├── FreeRTOS-Kernel
│   └── FreeRTOS-Plus-TCP
├── LICENSE.md
├── manifest.yml
└── README.md
aws

This directory contains SDK modules for AWS IoT, used for connecting and interacting with AWS cloud services.

  • <span>aws-iot-core-mqtt-file-streams-embedded-c</span>: File transfer via MQTT protocol.
  • <span>device-defender-for-aws-iot-embedded-sdk</span>: Support for AWS IoT Device Defender service for security monitoring.
  • <span>device-shadow-for-aws-iot-embedded-sdk</span>: State management for synchronizing device shadows.
  • <span>fleet-provisioning-for-aws-iot-embedded-sdk</span>: For bulk registration of devices (automatic onboarding).
  • <span>jobs-for-aws-iot-embedded-sdk</span>: Handles AWS IoT job management.
  • <span>sigv4-for-aws-iot-embedded-sdk</span>: Support for AWS Signature V4 algorithm for authentication and request signing.
FreeRTOS
  • <span>FreeRTOS-Kernel</span>: The FreeRTOS kernel, where the core scheduler and task management logic reside.
  • <span>FreeRTOS-Plus-TCP</span>: Lightweight TCP/IP stack for network connectivity.
  • <span>FreeRTOS-Cellular-Interface</span>: Abstraction layer for cellular communication interfaces.
  • <span>coreMQTT</span>: MQTT protocol client implementation, platform-independent, supporting QoS, keep-alive, etc.
  • <span>coreHTTP</span>: Lightweight HTTP protocol client.
  • <span>coreJSON</span>: Library for parsing/building JSON, suitable for embedded systems.
  • <span>corePKCS11</span>: Implements PKCS #11 interface, providing cryptography and key management support.
  • <span>coreSNTP</span>: SNTP (Simple Network Time Protocol) client for synchronizing system time.
  • <span>backoffAlgorithm</span>: Implementation of retry algorithms (e.g., exponential backoff), commonly used for network reconnections.

When porting FreeRTOS to an MCU, you only need to focus on<span>FreeRTOS-Kernel</span>.

How to Port FreeRTOS to Your Project

3. FreeRTOS Kernel

Source Code Structure

FreeRTOS-Kernel/
├── CMakeLists.txt
├── croutine.c
├── cspell.config.yaml
├── event_groups.c
├── examples
│   ├── cmake_example
│   ├── coverity
│   ├── README.md
│   └── template_configuration
├── GitHub-FreeRTOS-Kernel-Home.url
├── History.txt
├── include
│   ├── atomic.h
│   ├── CMakeLists.txt
│   ├── croutine.h
│   ├── deprecated_definitions.h
│   ├── event_groups.h
│   ├── FreeRTOS.h
│   ├── list.h
│   ├── message_buffer.h
│   ├── mpu_prototypes.h
│   ├── mpu_syscall_numbers.h
│   ├── mpu_wrappers.h
│   ├── newlib-freertos.h
│   ├── picolibc-freertos.h
│   ├── portable.h
│   ├── projdefs.h
│   ├── queue.h
│   ├── semphr.h
│   ├── stack_macros.h
│   ├── StackMacros.h
│   ├── stdint.readme
│   ├── stream_buffer.h
│   ├── task.h
│   └── timers.h
├── LICENSE.md
├── list.c
├── manifest.yml
├── MISRA.md
├── portable
│   ├── ARMClang
│   ├── ARMv8M
│   ├── BCC
│   ├── CCS
│   ├── CMakeLists.txt
│   ├── CodeWarrior
│   ├── Common
│   ├── GCC
│   ├── IAR
│   ├── Keil
│   ├── MemMang
│   ├── MikroC
│   ├── MPLAB
│   ├── MSVC-MingW
│   ├── oWatcom
│   ├── Paradigm
│   ├── readme.txt
│   ├── Renesas
│   ├── Rowley
│   ├── RVDS
│   ├── SDCC
│   ├── Softune
│   ├── Tasking
│   ├── template
│   ├── ThirdParty
│   └── WizC
├── queue.c
├── Quick_Start_Guide.url
├── README.md
├── sbom.spdx
├── stream_buffer.c
├── tasks.c
└── timers.c

3.1. Core Source Code

Core Source Code Files
File Name Function
tasks.c Core task management: creation, deletion, switching, scheduling, priority handling, etc.
queue.c Implementation of message queues/semaphores/mutexes: the basis for inter-task communication.
timers.c Software timer module, supports callback trigger handling.
event_groups.c Event group implementation: supports task synchronization.
croutine.c Coroutine implementation (lightweight tasks, suitable for very resource-constrained devices).
stream_buffer.c Support for stream buffers and message buffers.
list.c Internally used linked list utilities (FreeRTOS heavily uses linked lists for task scheduling and management).

3.2. Common Header Files

File Name Function
FreeRTOS.h Main header file, includes core RTOS macro definitions, configuration references, error checks, etc., must be included in all FreeRTOS applications.
task.h Task management APIs, such as xTaskCreate(), vTaskDelete(), vTaskDelay(), etc.
queue.h Queue APIs, also includes interfaces for semaphores, mutexes, etc. (all encapsulated in queue.c).
semphr.h Semaphore interface, which is also based on queue implementation.
timers.h Software timer APIs, such as xTimerCreate(), xTimerStart().
event_groups.h Event group APIs for inter-task synchronization, supports bitmap event mechanisms.
stream_buffer.h Stream buffer for data stream communication, suitable for continuous data transmission scenarios.
message_buffer.h Message buffer for variable-length message communication.
croutine.h Coroutine interface, suitable for platforms with very limited resources.

3.3. Kernel Mechanism Header Files

File Name Function
list.h Implements the doubly linked list structure used by FreeRTOS for task ready queues, delay queues, etc.
projdefs.h Some common macro definitions, such as pdTRUE, pdFAIL, task priority definitions, etc.
portable.h Interface declarations for the port layer, will include portmacro.h. Implemented by port folders for different architectures.
stack_macros.h / StackMacros.h Macros related to stack operations, which may vary by platform.
atomic.h Atomic operation interfaces (C11 or compiler built-in function support), for task or interrupt-safe operations.

3.4. Memory Protection and Specific Library Header Files

File Name Function
mpu_prototypes.h Function declarations to be called during task switching if using MPU (Memory Protection Unit) features.
mpu_wrappers.h Macro wrapper layer to wrap APIs into protected interfaces.
mpu_syscall_numbers.h System call number definitions for MPU (related to MPU implementation details).
newlib-freertos.h Supports thread safety for the newlib library (e.g., mutex access to malloc(), printf()).
picolibc-freertos.h Also used to support thread safety for picolibc.

3.5. Memory Protection and Specific Library Header Files

The portable directory is the porting layer of FreeRTOS, supporting various CPU architectures and toolchains.

Path Function
GCC/ Porting implementation under the GCC toolchain (e.g., ARM Cortex-M uses port.c, portmacro.h).
ARMClang/, IAR/, Keil/, MSVC-MingW/ etc. Porting support for different compilers/IDEs.
Common/ Modules that are not related to porting but shared by multiple architectures.
MemMang/ Memory managers (heap_1.c ~ heap_5.c), users can choose one based on application needs.
template/ Porting templates, can be used to create support for new platforms.

4. Extracting Source Code

When porting FreeRTOS to an MCU, you only need to keep<span>source code, include, portable</span> three parts. Among them, the<span>portable</span> part should be selected based on your hardware platform and compiler.

For example, when using Keil to develop common ARM microcontrollers, you only need to keep the following parts:

freertos
├── include
│   ├── CMakeLists.txt
│   ├── FreeRTOS.h
│   ├── StackMacros.h
│   ├── atomic.h
│   ├── croutine.h
│   ├── deprecated_definitions.h
│   ├── event_groups.h
│   ├── list.h
│   ├── message_buffer.h
│   ├── mpu_prototypes.h
│   ├── mpu_syscall_numbers.h
│   ├── mpu_wrappers.h
│   ├── newlib-freertos.h
│   ├── picolibc-freertos.h
│   ├── portable.h
│   ├── projdefs.h
│   ├── queue.h
│   ├── semphr.h
│   ├── stack_macros.h
│   ├── stdint.readme
│   ├── stream_buffer.h
│   ├── task.h
│   └── timers.h
├── portable
│   ├── ARMClang
│   ├── Common
│   ├── GCC
│   ├── Keil
│   ├── MemMang
│   └── RVDS
└── src
    ├── croutine.c
    ├── event_groups.c
    ├── list.c
    ├── queue.c
    ├── stream_buffer.c
    ├── tasks.c
    └── timers.c

5. Adding the Project

Project Directory Structure

├── bsp
│   ├── extio
│   ├── flash
│   ├── lcd
│   ├── rtc
│   ├── sdmc
│   ├── spi
│   ├── sys
│   ├── uart
│   ├── usb
│   └── wdt
├── drv
│   └── extio
├── hal
│   ├── cmsis
│   └── drivers
├── middle
│   ├── cbb_base
│   ├── cbb_esc
│   ├── cbb_log
│   ├── cbb_misc
│   ├── cbb_mvc
│   ├── cbb_ring
│   ├── freertos
│   ├── ring_queue
│   └── son_host
└── project
    ├── learning_wheel
    ├── manifest
    └── shost_rpc
Create Configuration File

In the path related to the project, create a<span>FreeRTOSConfig.h</span> configuration file, or copy an example file from the FreeRTOS official source. The specific content will be introduced in the next chapter.

Add Source Code to the Project

How to Port FreeRTOS to Your Project

Add Header File Paths

How to Port FreeRTOS to Your Project

Important Notes
  • The core architecture of the chip can be checked through the data sheet, such as M3, M4, M4F (with hardware floating-point unit).
  • For those using the Mdk development environment:
    • The new version generally defaults to compiler version 6, and the adaptation files should be selected from the<span>GCC</span> path;
    • If the compiler version is selected as 5, the adaptation files should be chosen from the<span>RVDS</span> path;
  • For devices with M4 cores, if the hardware does not include<span>MFU</span> and there are no corresponding<span>M4</span> adaptation files, the<span>M4F</span> port files cannot be used, and only the M3 core adaptation files can be used; otherwise, the compilation will fail.

6. Initial Compilation

After completing the above content, an initial compilation of the project can be performed. At this point, FreeRTOS is only compiled and has not yet been deployed to the system. The main purpose of this step is to eliminate initial project issues.

How to Port FreeRTOS to Your Project

If the compilation passes, the porting of FreeRTOS is complete. If the compilation fails, you need to troubleshoot the compilation issues.

How to Port FreeRTOS to Your Project

Previous Recommendations

RECOMMEND

How to Port FreeRTOS to Your Project

[1]. Core Mechanisms and Important Concepts of FreeRTOS Development

[2]. OpenWRT Router: An Embedded System Worth Exploring

[3]. Embedded Layered Design: A Streamlined and Efficient Project Structure

[4]. Embedded Driver Development: Bsp-UART Solution Design

I am Aike, an embedded software engineer.

Follow me for more embedded insights.

Remember to like, share, and light up the “look” button,

Your encouragement is my greatest motivation to continue sharing!

See you next time.

Looking forward to your

sharing

likes

looks

NEWS

WeChat IDaike_eureka

Baijiahao|Mastering Embedded Systems

Leave a Comment