RT-Thread内核说明
2019-07-19 本文已影响0人
Mr_Michael
一、系统初始化
1. RT-Thread 的启动流程
(1)初始化与系统相关的硬件;
(2)初始化系统内核对象,例如定时器、调度器、信号;
(3)创建main线程,在main线程中对各类模块依次进行初始化;
(4)初始化定时器线程、空闲线程,并启动调度器。
image2. 组件初始化
-
RT-Thread 提供一种低耦合高内聚的初始化方式,不需要我们手动添加调用初始化函数,它能在系统运行前自动完成各模块的初始化。
-
组件初始化方式要求初始化函数主动通过一些宏接口进行申明,链接器会 自动收集所有被申明的初始化函数,放到特定的数据段中,数据段中的 所有函数在系统初始化时会被调用。
-
组件初始化的所有宏接口及其被初始化的顺序如下表所示
- INIT_BOARD_EXPORT(fn)声明的函数会被rt_components_board_init()调用, 其他宏声明的函数会被rt_components_init()调用。
初始化顺序 | 接口 | 描述 |
---|---|---|
1 | INIT_BOARD_EXPORT(fn) | 硬件的初始化,此时调度器还未启动 |
2 | INIT_PREV_EXPORT(fn) | 主要是用于纯软件的初始化、没有太多依赖的函数 |
3 | INIT_DEVICE_EXPORT(fn) | 外设驱动初始化相关,比如网卡设备 |
4 | INIT_COMPONENT_EXPORT(fn) | 组件初始化,比如文件系统或者LWIP |
5 | INIT_ENV_EXPORT(fn) | 系统环境初始化,比如挂载文件系统 |
6 | INIT_APP_EXPORT(fn) | 应用初始化,比如GUI应用 |
3. 内核对象信息
系统所有的对象的信息都可以通过rt_object_get_information函数获取,RT-Thread所有的对象如下
RT_Object_Class_Thread | 线程对象 |
---|---|
RT_Object_Class_Semaphore | 信号量对象 |
RT_Object_Class_Mutex | 互斥量对象 |
RT_Object_Class_Event | 事件对象 |
RT_Object_Class_MailBox | 邮箱对象 |
RT_Object_Class_MessageQueue | 消息队列对象 |
RT_Object_Class_MemHeap | 内存堆对象 |
RT_Object_Class_MemPool | 内存池对象 |
RT_Object_Class_Device | 设备对象 |
RT_Object_Class_Timer | 定时器对象 |
RT_Object_Class_Module | 动态模块对象 |
RT_Object_Class_Unknown | 未知对象 |
RT_Object_Class_Static | 静态对象 |
-
查找所有在运行的线程
long list_thread(void) { struct rt_object_information *info; info = rt_object_get_information(RT_Object_Class_Thread); return _list_thread(&info->object_list); } FINSH_FUNCTION_EXPORT(list_thread, list thread); MSH_CMD_EXPORT(list_thread, list thread);
-
查找所有运行的动态模块
int list_module(void) { struct rt_dlmodule *module; struct rt_list_node *list, *node; struct rt_object_information *info; info = rt_object_get_information(RT_Object_Class_Module); list = &info->object_list; rt_kprintf("module ref address \n"); rt_kprintf("-------- -------- ------------\n"); for (node = list->next; node != list; node = node->next) { module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list)); rt_kprintf("%-*.*s %-04d 0x%08x\n", RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space); } return 0; } MSH_CMD_EXPORT(list_module, list modules in system);
二、bsp工程资源的选择与配置
在bsp工程(如rt-thread\bsp\stm32\stm32f429-atk-apollo),通过menuconfig看到的配置选项,是通过bsp工程路径下个文件夹的Kconfig文件决定的。
stm32f429-atk-apollo\Kconfig文件
mainmenu "RT-Thread Configuration"
config BSP_DIR
string
option env="BSP_ROOT"
default "."
config RTT_DIR
string
option env="RTT_ROOT"
default "../../.."
config PKGS_DIR
string
option env="PKGS_ROOT"
default "packages"
source "$RTT_DIR/Kconfig"
source "$PKGS_DIR/Kconfig"
source "../libraries/Kconfig"
source "board/Kconfig"
rt-thread\Kconfig文件
source "$RTT_DIR/src/Kconfig"
source "$RTT_DIR/libcpu/Kconfig"
source "$RTT_DIR/components/Kconfig"
- 及其路径所示各级文件
stm32f429-atk-apollo\board\Kconfig文件
menu "Hardware Drivers Config"
config SOC_STM32F429IG
bool
select SOC_SERIES_STM32F4
default y
menu "Onboard Peripheral Drivers"
config BSP_USING_USB_TO_USART
bool "Enable USB TO USART (uart1)"
select BSP_USING_UART
select BSP_USING_UART1
default y
config BSP_USING_COM2
bool "Enable COM2 (uart2 pin conflict with Ethernet and PWM)"
select BSP_USING_UART
select BSP_USING_UART2
default n
config BSP_USING_COM3
bool "Enable COM3 (uart3 pin conflict with Ethernet)"
select BSP_USING_UART3
default n
config BSP_USING_SDRAM
bool "Enable SDRAM"
select BSP_USING_FMC
default n
config BSP_USING_SPI_FLASH
bool "Enable SPI FLASH (W25Q256 spi5)"
select BSP_USING_SPI
select BSP_USING_SPI5
select RT_USING_SFUD
select RT_SFUD_USING_SFDP
default n
config BSP_USING_MPU9250
bool "Enable MPU 9250 (i2c1)"
select BSP_USING_I2C1
select PKG_USING_MPU6XXX
default n
config PHY_USING_LAN8720A
bool
config BSP_USING_ETH
bool "Enable Ethernet"
select BSP_USING_I2C1
select PKG_USING_PCF8574
select RT_USING_LWIP
select PHY_USING_LAN8720A
default n
config BSP_USING_SDCARD
bool "Enable SDCARD (sdio)"
select BSP_USING_SDIO
select RT_USING_DFS
select RT_USING_DFS_ELMFAT
default n
endmenu
menu "On-chip Peripheral Drivers"
config BSP_USING_GPIO
bool "Enable GPIO"
select RT_USING_PIN
default y
menuconfig BSP_USING_UART
bool "Enable UART"
default y
select RT_USING_SERIAL
if BSP_USING_UART
config BSP_USING_UART1
bool "Enable UART1"
default y
config BSP_UART1_RX_USING_DMA
bool "Enable UART1 RX DMA"
depends on BSP_USING_UART1 && RT_SERIAL_USING_DMA
default n
config BSP_USING_UART2
bool "Enable UART2"
default n
config BSP_UART2_RX_USING_DMA
bool "Enable UART2 RX DMA"
depends on BSP_USING_UART2 && RT_SERIAL_USING_DMA
default n
config BSP_USING_UART3
bool "Enable UART3"
default n
config BSP_UART3_RX_USING_DMA
bool "Enable UART3 RX DMA"
depends on BSP_USING_UART3 && RT_SERIAL_USING_DMA
default n
endif
config BSP_USING_ON_CHIP_FLASH
bool "Enable on-chip FLASH"
default n
menuconfig BSP_USING_SPI
bool "Enable SPI BUS"
default n
select RT_USING_SPI
if BSP_USING_SPI
config BSP_USING_SPI1
bool "Enable SPI1 BUS"
default n
config BSP_SPI1_TX_USING_DMA
bool "Enable SPI1 TX DMA"
depends on BSP_USING_SPI1
default n
config BSP_SPI1_RX_USING_DMA
bool "Enable SPI1 RX DMA"
depends on BSP_USING_SPI1
select BSP_SPI1_TX_USING_DMA
default n
config BSP_USING_SPI2
bool "Enable SPI2 BUS"
default n
config BSP_SPI2_TX_USING_DMA
bool "Enable SPI2 TX DMA"
depends on BSP_USING_SPI2
default n
config BSP_SPI2_RX_USING_DMA
bool "Enable SPI2 RX DMA"
depends on BSP_USING_SPI2
select BSP_SPI2_TX_USING_DMA
default n
config BSP_USING_SPI5
bool "Enable SPI5 BUS"
default n
config BSP_SPI5_TX_USING_DMA
bool "Enable SPI5 TX DMA"
depends on BSP_USING_SPI5
default n
config BSP_SPI5_RX_USING_DMA
bool "Enable SPI5 RX DMA"
depends on BSP_USING_SPI5
select BSP_SPI5_TX_USING_DMA
default n
endif
menuconfig BSP_USING_I2C1
bool "Enable I2C1 BUS (software simulation)"
default n
select RT_USING_I2C
select RT_USING_I2C_BITOPS
select RT_USING_PIN
if BSP_USING_I2C1
comment "Notice: PH4 --> 116; PH5 --> 117"
config BSP_I2C1_SCL_PIN
int "I2C1 scl pin number"
range 1 176
default 116
config BSP_I2C1_SDA_PIN
int "I2C1 sda pin number"
range 1 176
default 117
endif
menuconfig BSP_USING_TIM
bool "Enable timer"
default n
select RT_USING_HWTIMER
if BSP_USING_TIM
config BSP_USING_TIM11
bool "Enable TIM11"
default n
config BSP_USING_TIM13
bool "Enable TIM13"
default n
config BSP_USING_TIM14
bool "Enable TIM14"
default n
endif
menuconfig BSP_USING_PWM
bool "Enable pwm"
default n
select RT_USING_PWM
if BSP_USING_PWM
menuconfig BSP_USING_PWM2
bool "Enable timer2 output pwm"
default n
if BSP_USING_PWM2
config BSP_USING_PWM2_CH4
bool "Enable PWM2 channel4"
default n
endif
endif
menuconfig BSP_USING_ADC
bool "Enable ADC"
default n
select RT_USING_ADC
if BSP_USING_ADC
config BSP_USING_ADC1
bool "Enable ADC1"
default n
endif
menuconfig BSP_USING_ONCHIP_RTC
bool "Enable RTC"
select RT_USING_RTC
select RT_USING_LIBC
default n
if BSP_USING_ONCHIP_RTC
choice
prompt "Select clock source"
default BSP_RTC_USING_LSE
config BSP_RTC_USING_LSE
bool "RTC USING LSE"
config BSP_RTC_USING_LSI
bool "RTC USING LSI"
endchoice
endif
config BSP_USING_WDT
bool "Enable Watchdog Timer"
select RT_USING_WDT
default n
config BSP_USING_SDIO
bool "Enable SDIO"
select RT_USING_SDIO
select RT_USING_DFS
default n
config BSP_USING_FMC
bool
default n
endmenu
menu "Board extended module Drivers"
endmenu
endmenu
- Kconfig是类似lua语言的配置方式,menuconfig保存配置时会将所有config为ture的选项添加到rtconfig.h的文件的宏定义。
- 工程根据宏定义进行选择编译的内容。