As the name suggests, it is an embedded real-time multi-threaded operating system, one of the basic properties is to support multitasking, allowing multiple tasks to run at the same time does not mean that the processor is actually executing multiple tasks at the same time.
RT-Thread architecture
Architecture diagram
Architecture overview
- Specifically, it includes the following sections:
- Kernel layer: The RT-Thread kernel is the core part of RT-Thread, including the implementation of objects in the kernel system, such as multithreading and its scheduling, semaphore, mailbox, message queue, memory management, timer, etc.; libcpu/BSP (Chip Porting Related Files/Board Level Support Packages) is closely related to hardware and consists of peripheral drivers and CPU porting.
- Component and service layer: Components are upper-layer software based on the RT-Thread kernel, such as virtual file systems, FinSH command line interfaces, network frameworks, device frameworks, etc. The modular design is adopted to achieve high cohesion within the components and low coupling between components.
- RT-Thread Software Package: A common software component for different application areas that runs on the RT-Thread IoT operating system platform, consisting of description information, source code, or library files.
- IoT-related software packages: Paho MQTT, WebClient, mongoose, WebTerminal, etc.
- Scripting language-related packages: JerryScript and MicroPython are currently supported.
- Multimedia-related software packages: Openmv, mupdf.
- Utility packages: CmBacktrace, EasyFlash, EasyLogger, and SystemView.
- System-related packages: RTGUI, Persimmon UI, lwext4, partition, SQLite, etc.
- Peripheral libraries and driver packages: RealTek RTL8710BN SDK.
RT-Thread Overview
In fact, a processor core can only run one task at a time, and due to the short execution time of one task at a time, and the very fast switching between tasks through the task scheduler (the scheduler determines which task to execute at the moment based on priority), it gives the illusion that multiple tasks are running at the same time. In the RT-Thread system, tasks are implemented through threads, and the thread scheduler in RT-Thread is the task scheduler mentioned above.
Compared with the Linux operating system, RT-Thread is small in size, low in cost, low in power consumption, and fast in startup. While 32-bit MCUs are its primary operating platform, many application processors with MMUs, ARM9, ARM11, and even Cortex-A series CPUs are also suitable for specific applications using RT-Thread.
Features of RT-Thread:
- Extremely low resource usage, ultra-low power consumption design, the smallest core (Nano version) only needs 1.2KB RAM, 3KB Flash.
- A thriving package ecosystem with abundant components.
- Simple and easy to use, elegant code style, easy to read and master.
- Highly scalable, high-quality scalable software architecture, loosely coupled, modular, easy to tailor and expand.
- Powerful to support high-performance applications.
- Cross-platform, wide chip support.
Quick Start – Examples
This example is a zip file, unzip it, and here we decompress it to D:/. The directory structure after the decompression is completed is shown in the following figure:
The following table describes the file types contained in each directory:
The name of the directory |
description |
applications |
RT-Thread application. |
rt-thread |
The source file for RT-Thread. |
– components |
RT-Thread’s individual component catalogs. |
– include |
Header file for the RT-Thread kernel. |
– libcpu |
Porting code for various chips, including STM32 porting files here. |
– src |
The source file for the RT-Thread kernel. |
– tools |
RT-Thread command to build the script file of the tool. |
drivers |
The driver of RT-Thread, the underlying driver of different platforms is implemented. |
Libraries |
ST’s STM32 firmware library file. |
kernel-sample-0.1.0 |
Kernel routines for RT-Thread. |
In the directory, there is a project.uvprojx file, which is an MDK5 project file in the routine referenced in this article, double-click the “project.uvprojx” icon to open this project file:
Now let’s click the button in the toolbar at the top of the window to compile the project, as shown in the image:
System startup code
Generally speaking, most of the code starts with the startup part, and the same way is used here, looking for the source of the startup first. Taking MDK-ARM as an example, the user entry point for MDK-ARM is the main() function, which is located in the main.c file. After the system starts, start from the assembly code startup_stm32f103xe.s, then jump to the C code, initialize the RT-Thread system function, and finally enter the user program entry main().
//components.c definition
/* re-define main function */
int $Sub$$main(void)
{
rt_hw_interrupt_disable();
rtthread_startup();
return 0;
}
Here, the $Sub$$main function only calls the rtthread_startup() function. RT-Thread supports multiple platforms and multiple compilers, and the rtthread_startup() function is the unified entry point specified by RT-Thread, so the $Sub$$main function only needs to call the rtthread_startup() function. For example, RT-Thread, which is compiled using the GNU GCC compiler, jumps directly from the assembly startup code section to the rtthread_startup() function and starts the execution of the first C code. Find the rtthread_startup() function in the code of components.c, and we will see the startup process of RT-Thread:
int rtthread_startup(void)
{
rt_hw_interrupt_disable();
/* board level initalization
* NOTE: please initialize heap inside board initialization.
*/
rt_hw_board_init();
/* show RT-Thread version */
rt_show_version();
/* timer system initialization */
rt_system_timer_init();
/* scheduler system initialization */
rt_system_scheduler_init();
#ifdef RT_USING_SIGNALS
/* signal system initialization */
rt_system_signal_init();
#endif
/* create init_thread */
rt_application_init();
/* timer thread initialization */
rt_system_timer_thread_init();
/* idle thread initialization */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return 0;
}
This part of the startup code can be roughly divided into four parts:
- Initialize system-related hardware;
- Initialize system kernel objects such as timers, schedulers;
- Initialize the system device, which is mainly for the RT-Thread device framework.
- Initialize each application thread and start the scheduler.