1創建項目 (STM32F03ZET6)
RT-Thread項目與RT-Thread Nano?項目區別
-
RT-Thread:
- 完整版:這是RT-Thread的完整形態,適用于資源較豐富的物聯網設備。
- 功能:它提供了全面的中間件組件,如文件系統、網絡協議棧、GUI等,支持復雜的物聯網應用開發。
- 資源占用:相對較大,因為它包含了更多的功能組件。
-
RT-Thread Nano:(個別函數也去掉了)
- 精簡版:這是一個精簡的硬實時內核,設計用于資源受限的微控制器,如ARM Cortex-M0。
- 功能:它去掉了完整版中的許多高級特性,保留了最核心的多線程管理和調度功能。
- 資源占用:非常小,適用于內存和存儲空間有限的小型設備。
2選擇板子(根據自己的板子選擇)
3找到主函數
4編寫代碼
4-1創建必要的變量
// 定義線程控制塊
static struct rt_thread thread1;
// 定義線程棧
static rt_uint8_t thread1_stack[1024];
4-2創建入口函數
// 線程入口函數
static void thread1_entry(void *parameter)
{while (1){// 線程執行的代碼rt_kprintf("Thread 2 is running\n");rt_thread_delay(1000); // 線程延時,單位為毫秒}
}
4-3在主函數中啟動線程
// 初始化線程rt_thread_init(&thread1, // 線程控制塊"thread1", // 線程名字thread1_entry, // 線程入口函數RT_NULL, // 線程入口參數&thread1_stack[0], // 線程棧起始地址sizeof(thread1_stack), // 線程棧大小10, // 線程優先級20); // 線程時間片// 啟動線程rt_thread_startup(&thread1);
4-4全部代碼
/** Copyright (c) 2006-2024, RT-Thread Development Team** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date Author Notes* 2024-06-26 RT-Thread first version*/#include <rtthread.h>#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>// 定義線程控制塊
static struct rt_thread thread1;
// 定義線程棧
static rt_uint8_t thread1_stack[1024];// 線程入口函數
static void thread1_entry(void *parameter)
{while (1){// 線程執行的代碼rt_kprintf("Thread 2 is running\n");rt_thread_delay(1000); // 線程延時,單位為毫秒}
}int main(void)
{int count = 1;// 初始化線程rt_thread_init(&thread1, // 線程控制塊"thread1", // 線程名字thread1_entry, // 線程入口函數RT_NULL, // 線程入口參數&thread1_stack[0], // 線程棧起始地址sizeof(thread1_stack), // 線程棧大小10, // 線程優先級20); // 線程時間片// 啟動線程rt_thread_startup(&thread1);while (count++){LOG_D("Hello RT-Thread!");rt_thread_mdelay(1000);}return RT_EOK;
}
其中rt_kprintf()與LOG_D()作用一樣都是串口輸出文本