FreeRTOS, short for Free Real-Time Operating System, is an open-source, real-time operating system kernel designed for embedded systems. Developed by Real Time Engineers Ltd., FreeRTOS is known for its simplicity and flexibility, making it a popular choice for a wide range of microcontroller-based applications.
Key features and characteristics of FreeRTOS include:
Real-Time Capabilities: FreeRTOS is specifically designed for real-time systems, providing predictable and deterministic behavior. It includes a real-time scheduler that allows developers to prioritize tasks and manage their execution in a time-sensitive manner.
Task Management: FreeRTOS uses a cooperative multitasking model, allowing developers to create and manage tasks with different priorities. Each task can be assigned a priority level, and the scheduler ensures that tasks with higher priority are executed before those with lower priority.
Resource Management: FreeRTOS includes features for managing resources efficiently, such as lightweight semaphores, queues, and mutexes. These mechanisms enable communication and synchronization between tasks.
Portability: FreeRTOS is designed to be highly portable, and it supports a wide range of microcontrollers and architectures. This portability makes it easier to adapt FreeRTOS to various embedded systems with different hardware configurations.
Memory Management: FreeRTOS includes a memory management scheme that allows dynamic allocation and deallocation of memory for tasks. However, developers can configure the kernel to use a static memory allocation model if needed.
Tickless Operation: FreeRTOS supports tickless operation, where the system enters a low-power state between ticks to conserve energy. This feature is particularly useful in battery-powered and energy-efficient embedded systems.
Community Support: FreeRTOS has an active and supportive user community. Developers can access a wealth of documentation, forums, and resources contributed by the community, making it easier to troubleshoot issues and share knowledge.
FreeRTOS is often used in various embedded applications, including IoT devices, robotics, medical devices, automotive systems, and more. Its open-source nature and permissive license make it a popular choice for both hobbyist projects and commercial products, providing a reliable foundation for building real-time embedded systems.
Default tasks
...
Tasks
...
Folders or files
...
Descriptions
...
Idle
...
powerdown
...
power-down control
...
powerup
...
power-up control
...
tmr Svc
...
Timer
...
viorw
...
virtual IO
...
wakeupkey
...
Detect wake-up key
Drivers locations and features
...
Drivers
...
Folders or files
...
Features
...
ADC (SAR12B)
...
Support read external voltage.
...
Audio (I2S)
...
Support S16_LE format, 48kHz, stereo recording.
Support S16_LE format, 48kHz, stereo playback.
...
GPIO driver
...
Support digital output.
Support digital input.
...
AHB DMA
...
Support SPI DMA mode transferring.
Support memory-to-memory transferring.
...
I2C driver
...
Support RX and TX transferring.
Support 100kHz and 400kHz speeds.
...
Mailbox
...
Support communication between CM4 and CA55.
Support normal mode (software).
Support direction mode (hardware).
...
PWM
...
Support set frequency and duty.
...
RTC
...
Support set and get time.
Support set alarm.
Support alarm interrupt.
Support periodic interrupt.
...
SPI
...
Support master mode TX and RX transferring.
Support set clock frequency.
Support mode 0, 1, 2 and 3.
Support DMA mode.
...
Timer
...
Support generate timer interrupt.
...
UART
...
Support TX and RX transferring.
...
Watchdog
...
Support pretimeout function.
Files
...
Folders
...
Descriptions
...
firmware/arduino_core_sunplus/
...
Root folder of OP-TEE
...
application/
...
Code of app_main, freertos_main and arduino_main
...
application/power_manager/
...
Code for power manager
...
application/VirtIOSerial/
...
Code for virtual IO (UART)
...
bin/
...
Firmware (binary) file, resultant files of compilation.
...
system/drivers
...
Code for drivers
...
system/freertos
...
Code for FreeRTOS
...
system/Middlewares
...
Code for OpenAMP
...
system/sp7350
...
variants/sp7350_evb/
...
For further information, refer to official web site: FreeRTOS - Real-time operating system for microcontrollers
CM4 startup is controlled by the kernel through the use of remoteproc driver. CM4 software implements control of each power domain when the system is suspended or resume. It also includes polling wake-up key to enter or exit deep-sleep mode, power down or up main-domain, CA55, NPU, video codec powers, and data communication between CA55 and CM4 via virtIO as well.
Table of Contents
Table of Contents | ||
---|---|---|
|
Features
Output log at UART6 (set at 115,200 bps by x-boot).
Support SP7350 power manager
Detect wake-up key
Power down and up control for main-domain, CA55, NPU, video codec
Support DDR retention
Support PMIC control
Support communication with CA55 via virtual l/O serial port
Support CmBacktrace to tracks and debug ARM Cortex-M4
Support Arduino hardware interface, such as SPI, I2C, UART, timer, GPIO.
Support CUnit test of SPI, I2C, UART, timer, GPIO driver
Default Tasks
By default, FreeRTOS of SP7350 platform is configured to run 6 tasks. In table below, we outline each task along with its location of source files and functionality.
Tasks | Files (under FreeRTOS project directory) | Descriptions |
powerdown | application/power_manager/power_down.c | power-down control |
powerup | application/power_manager/power_up.c | power-up control |
wakeupkey | application/power_manager/wakeup_key.c | Detect wake-up key |
virtIOrw | application/VirtIOSerial/vios_rw.cpp | virtual IO read/write |
tmr Svc | system/freertos/timers.c | FreeRTOS system Timer task |
Idle | system/freertos/tasks.c | FreeRTOS system Idle task |
The source files of FreeRTOS are located within the "firmware/arduino_core_sunplus/" directory under the project's top directory.
Linux Remote Processor Framework
The Linux Remote Processor Framework is a framework that allows for managing (power on, load firmware, power off) and communicating with remote processors on Linux systems. This framework is typically used in embedded or multiprocessor systems where one processor (usually the main processor) is responsible for managing and controlling the other remote processors. In addition, this framework also adds rpmsg, virtio devices for remote processors that supports this kind of communication. This way, platform-specific remoteproc driver only needs to provide a few low-level handlers, and then all rpmsg drivers will then just work. For example:
Load firmware of CM4 via Linux shell command:
Code Block |
---|
echo firmware > /sys/class/remoteproc/remoteproc/firmware |
where ‘firmware’ is the firmware file (elf format) of CM4 stored at “/lib/firmware/".
Start CM4 via Linux shell command:
Code Block |
---|
echo start > /sys/class/remoteproc/remoteproc0/state |
Stop CM4 via Linux shell command:
Code Block |
---|
echo stop > /sys/class/remoteproc/remoteproc0/state |
Linux Remoteproc Driver
The remoteproc driver bases on Remote Processor Framework to implement firmware loading and booting, resource allocation and management, communication support, and other functions.
Enabling remoteproc driver in Linux kernel needs to enable the kernel feature:
CONFIG_REMOTEPROC=y and
CONFIG_SUNPLUS_REMOTEPROC=y
in default configuration file.
Device-tree Nodes for Remoteproc Driver
Code Block |
---|
remoteproc0: remoteproc@f800817c {
compatible = "sunplus,sp-rproc";
firmware = "firmware";
reg = <0 0xf800817c 0 4>, /* mbox G258.31, cpu0 to cpu2 direct reg07 */
<0 0xf8800250 0 4>,
<0 0xf80081fc 0 4>; /* mbox G259.31, cpu2 to cpu0 direct reg07 */
interrupt-parent = <&gic>;
interrupts = <GIC_SPI 125 IRQ_TYPE_LEVEL_HIGH>; /* CPU2_TO_0_DIRECT_INT7 */
resets = <&rstc RST_CM4>;
memory-region = <&rproc_0_reserved>, <&rproc0runaddr>,<&vdev0buffer>, <&vdev0vring0>, <&vdev0vring1>;
}; |
In the RemoteProc framework, vdev0vring0, vdev0vring1, and vdev0buffer are typically used in conjunction with Virtio devices for efficient data transfers between the host processor and the remote processor.
vring0 and vring1 represent the two virtual rings of a Virtio device, typically one for sending data and the other for receiving data. These two rings pass data and control information through a set of descriptors. vbuffer represents the Virtio device's buffer, which is used to store data.
The remoteproc-related dts notes are as follows:
Code Block |
---|
vdev0buffer: vdev0buffer@5be00000 {
compatible = "shared-dma-pool";
reg = <0x0 0x5be00000 0x0 0x100000>;
no-map;
};
vdev0vring0: vdev0vring0@5bf00000 {
compatible = "shared-dma-pool";
reg = <0x0 0x5bf00000 0x0 0x10000>;
no-map;
};
vdev0vring1: vdev0vring1@5bf10000 {
compatible = "shared-dma-pool";
reg = <0x0 0x5bf10000 0x0 0x10000>;
no-map;
}; |
RPMSG and Virtual IO
The RPMsg framework (Remote Processor Messaging Framework) is an implementation of remote processor messaging. It allows communication between different processors, including communication between the main processor and remote processors, and it is a VirtIO-based message bus that allows Linux kernel drivers to communicate with remote processors in the system.
Enabling RPMSG and VirtIO in kernel needs to enable the kernel feature:
CONFIG_RPMSG=y and
CONFIG_RPMSG_VIRTIO=y
in default configuration file.
RPMSG: After CM4 is running, a device node /dev/ttyRPMSGx is generated on the kernel side, and the kernel side communicates with CM4 via /dev/ttyRPMSGx.
VirtIO: Virtio is a standard for high-performance virtual appliances in virtualized environments, CM4 write/read VirtioSerial to transfer data to /dev/ttyRPMSGX
Build Image
Use “make firmware
" in the root directory to compile the FreeRTOS code, The output file after compilation is put in bin/firmware. You need to copy it to linux/rootfs/initramfs/disk/lib/firmware in project directory.
This project includes several code environments, including FreeRTOS+Arduino (default), Arduino, and CUnit, and the different code environments are determined by the configuration of the Makefile.
FreeRTOS+Arduino: The default compiled image is based on the FreeRTOS environment, It implements power management function for suspend and resume processes.
Arduino: Set
FREERTOS ?=0
in Makefile will compile the code environment with only Arduino, not FreeRTOS. The entry function of the program is in ‘application/arduino_main.cpp’.CUnit: Set
CUNIT ?=1
in Makefile. Compiled code contains only CUnit test code, including SPI, I2C, UART, timer, and GPIO test units.
Supported Drivers for SP7350 Platform
The source files of FreeRTOS are located within the "firmware/arduino_core_sunplus/" directory under the project's top directory. For the SP7350 platform, a wide array of device drivers is supported. These drivers are within the "system/drivers/sp7350_hal_driver/" directory. Below is a reference table detailing the drivers along with their corresponding feature descriptions:
Drivers | Files | Features |
ADC (SAR12B) | sp7350_hal_adc.c | read analog voltage |
Audio (I2S) | sp7350_hal_i2s.c | I2S read/write interface |
GPIO driver | sp7350_hal_gpio.c | write/read gpio level |
AHB DMA | sp7350_hal_dma.c | AHB DMA driver |
I2C driver | sp7350_hal_i2c.c | I2C read/write interface |
Mailbox | sp7350_hal_ipcc.c | trigger between CM4 and CA55 |
PWM | /sp7350_hal_pwm.c | output pwm |
RTC | sp7350_hal_rtc.c | get rtc time and generate rtc irq |
SPI | sp7350_hal_spi.c | SPI read/write interface |
Timer | sp7350_hal_tim.c | get/set timer. generate timer irq |
UART | sp7350_hal_uart.c | UART read/write interface |
Watchdog | sp7350_hal_wdg.c | watchdog interface |
Source Files
The source files of FreeRTOS are organized within the "firmware/arduino_core_sunplus/" directory at the project's top directory. Below is a breakdown of the main subdirectories:
Folders | Descriptions |
cores/arduino | Contains the code for Arduino only |
libraries/ | Contains the third party source code and ‘syscall.c’ |
application/power_manager/ | Contains power manager codes |
application/VirtIOSerial/ | Contains VirtIO Serial read/write codes |
bin/ | Output folder |
system/drivers | Contains CMIS files for CM4 and SP7350 drivers |
system/freertos | Contains FreeRTOS kernel files |
system/Middlewares | Contains Open-amp driver (VirtIO/Remoteproc) |
system/sp7350 | Contains many constant definitions |
variants/sp7350_evb/ | Contains ldscript and config file |
Example Files
CUnit is a C unit testing framework for unit testing C programs. It provides a set of libraries and macros for writing and executing test cases and reporting test results. CUnit tests include examples using the SP7350 peripheral interface driver. Includes examples for other drivers such as SPI, I2C, UART, GPIO and PWM. The path is:
/application/Cunit_test
Set CUNIT ?=1
in Makefile to build image. The code will run from Cuint_main.cpp
FreeRTOS Log and Explanation
Line 1: The banner of CM4 firmware.
Line 2-4: Initialize remoteproc.
Line 5-6: Initialize rpmsg.
Line 7-12: Create FreeRTOS tasks.
Code Block |
---|
CM4 build @ Mar 7 2024 02:01:06
Successfully intialize remoteproc.
Initialize remoteproc successfully.
creating remoteproc virtio: 0x5be00000
initializing rpmsg vdev in mode 1
initializing rpmsg vdev done
CreateTask-viorw-result:1
CreateTask-wakeupkey-result:1
CreateTask-powerdown-result:1
CreateTask-powerup-result:1
CreateTask-IDLE-result:1
CreateTask-Tmr Svc-result:1
power down wait Semaphore ! |