Fundamentals 11 min read

Unlock Embedded Power: Deep Dive into RT-Thread RTOS Core Libraries

RT-Thread, a Chinese open‑source real‑time operating system, offers a highly scalable kernel, rich component ecosystem, and modular design; this article examines its core libraries, module classifications, typical IoT and industrial applications, and provides detailed C code examples for thread management, synchronization, device drivers, file systems, and networking.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Unlock Embedded Power: Deep Dive into RT-Thread RTOS Core Libraries

RT‑Thread Overview

RT‑Thread is an open‑source real‑time operating system (RTOS) written in C and licensed under GPLv2. It is designed for a wide range of embedded platforms, from resource‑constrained Cortex‑M0 cores (kernel size ~3 KB) to high‑performance Cortex‑A multicore systems.

Core Library Features

Scalability : Kernel can be trimmed to a few kilobytes or expanded to support SMP.

Deterministic real‑time performance : Pre‑emptive multitasking with context‑switch latency in the microsecond range.

Rich component ecosystem : Filesystem, network stack, GUI, AI toolkit, POSIX layer, etc.

Portability : Supports ARM, RISC‑V, MIPS and many other architectures.

Module Classification

Kernel Module

Provides task management, scheduling, synchronization and inter‑thread communication.

Thread management – up to 256 priority levels.

Semaphores, mutexes and event objects for resource protection.

Message queues for data exchange.

Software timers and high‑precision hardware timers.

Dynamic heap and small‑block memory allocator.

Device Driver Framework

A unified driver model that abstracts common peripherals such as UART, I2C/SPI, GPIO, ADC/DAC.

File System

Supports multiple file systems:

FAT – for SD cards and USB drives.

LittleFS – lightweight, wear‑leveling file system for constrained devices.

tmpfs – in‑memory temporary storage.

Network Protocol Stack

Integrates the lightweight lwIP TCP/IP stack with MQTT/HTTP support and a standard BSD‑socket API.

Other Components

RT‑Thread Studio – IDE for configuration, code generation and debugging.

RT‑AK – AI inference toolkit for embedded devices.

POSIX interface – improves code portability across POSIX‑compatible platforms.

Code Samples

Thread Management

#include <rtthread.h>
#define THREAD_PRIORITY   25
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE  5
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
static void thread1_entry(void *parameter)
{
    rt_uint32_t count = 0;
    while (1)
    {
        rt_kprintf("Thread1 count: %d
", count++);
        rt_thread_mdelay(500);
    }
}
static void thread2_entry(void *parameter)
{
    rt_uint32_t count = 0;
    while (1)
    {
        rt_kprintf("Thread2 count: %d
", count++);
        rt_thread_mdelay(800);
    }
}
int main(void)
{
    tid1 = rt_thread_create("thread1", thread1_entry, RT_NULL,
                           THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid1 != RT_NULL) rt_thread_startup(tid1);
    tid2 = rt_thread_create("thread2", thread2_entry, RT_NULL,
                           THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
    if (tid2 != RT_NULL) rt_thread_startup(tid2);
    return 0;
}

Semaphore Synchronization (Producer/Consumer)

#include <rtthread.h>
static rt_sem_t sem;
static void producer_thread_entry(void *parameter)
{
    while (1)
    {
        rt_kprintf("Producer: produce an item
");
        rt_sem_release(sem);
        rt_thread_mdelay(1000);
    }
}
static void consumer_thread_entry(void *parameter)
{
    while (1)
    {
        rt_sem_take(sem, RT_WAITING_FOREVER);
        rt_kprintf("Consumer: consume an item
");
    }
}
int main(void)
{
    sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO);
    if (sem == RT_NULL) return -1;
    rt_thread_create("producer", producer_thread_entry, RT_NULL, 512, 20, 10);
    rt_thread_create("consumer", consumer_thread_entry, RT_NULL, 512, 21, 10);
    return 0;
}

UART Device Driver

#include <rtthread.h>
#include <rtdevice.h>
#define UART_NAME "uart1"
int main(void)
{
    rt_device_t uart_dev;
    const char str[] = "Hello, RT-Thread!
";
    uart_dev = rt_device_find(UART_NAME);
    if (uart_dev == RT_NULL) return -1;
    if (rt_device_open(uart_dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK) return -1;
    while (1)
    {
        rt_device_write(uart_dev, 0, str, sizeof(str));
        rt_thread_mdelay(1000);
    }
    rt_device_close(uart_dev);
    return 0;
}

File System Operation (FAT on SD Card)

#include <rtthread.h>
#include <dfs_posix.h>
int main(void)
{
    int fd;
    const char buffer[] = "Hello, RT-Thread File System!
";
    fd = open("/sd/test.txt", O_WRONLY | O_CREAT, 0);
    if (fd >= 0)
    {
        write(fd, buffer, sizeof(buffer));
        close(fd);
        rt_kprintf("File written successfully!
");
    }
    else
    {
        rt_kprintf("Failed to open file!
");
    }
    return 0;
}

TCP Client Using lwIP

#include <rtthread.h>
#include <lwip/sockets.h>
#include <lwip/api.h>
#define SERVER_IP   "192.168.1.100"
#define SERVER_PORT 8080
int main(void)
{
    int sock;
    struct sockaddr_in server_addr;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) return -1;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port   = htons(SERVER_PORT);
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    rt_memset(&server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        rt_kprintf("Connect to server failed!
");
        closesocket(sock);
        return -1;
    }
    const char *msg = "Hello from RT-Thread!
";
    send(sock, msg, strlen(msg), 0);
    rt_kprintf("Message sent to server!
");
    closesocket(sock);
    return 0;
}

Advanced Development Tips

Package management : Use pkgs --update to fetch community packages (e.g., MQTT client, JSON parser).

Logging and debugging : rt_kprintf provides console output; adjust thread priorities and time slices to tune performance.

Memory optimization : Enable the small‑memory manager to reduce fragmentation on low‑resource targets.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

IoTC programmingembedded systemsRTOSRT-ThreadReal-Time Kernel
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.