PX4实时操作系统(nuttx)自学笔记

时间:2021-01-20 20:18:08

注:

nuttx可参考的资料实在太少,目前只有操作手册和官网以及一些网上其他的博主的文章。
这一篇自学笔记就是结合一位博主的文章和官方手册写的:http://blog.chinaunix.net/uid-29786319-id-4393303.html


写在前面

在初学nuttx时,先不必注重在意代码的细节,先要掌握整体的代码的结构,把握模块之间的关联即可。另外,nuttx只需会使用他的API即可,于是用户手册就必不可少。


第一步:系统启动

因为px4用的是stm32,所以启动文件是stm32_start.c:


(注意:在了解px4启动之前我们需要了解一下bootloader。Bootloader是在操作系统内核运行之前运行,可以初始化硬件设备,建立内存空间映射图等,整个系统的加载启动任务就是完全由Bootloader来完成的。它还是嵌入式系统在加电后执行的第一段代码,在它完成cpu和相关硬件的初始化后,再将操作系统映像或固化的嵌入式应用程序装在内存中然后跳转到操作系统所在的空间,启动操作系统运行)


/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <assert.h>
#include <debug.h>

#include <nuttx/init.h>
#include <arch/board/board.h>

#include "up_arch.h"
#include "up_internal.h"

#include "stm32.h"
#include "stm32_gpio.h"
#include "stm32_userspace.h"

#ifdef CONFIG_ARCH_FPU
# include "nvic.h"
#endif

/****************************************************************************
* Private Function prototypes
****************************************************************************/

#ifdef CONFIG_ARCH_FPU
static inline void stm32_fpuconfig(void);
#endif
#ifdef CONFIG_STACK_COLORATION
static void go_os_start(void *pv, unsigned int nbytes)
__attribute__ ((naked, no_instrument_function, noreturn));
#endif

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: showprogress
*
* Description:
* Print a character on the UART to show boot status.
*
****************************************************************************/

#ifdef CONFIG_DEBUG_FEATURES
# define showprogress(c) up_lowputc(c)
#else
# define showprogress(c)
#endif

/****************************************************************************
* Public Functions
****************************************************************************/

#ifdef CONFIG_ARMV7M_STACKCHECK
/* we need to get r10 set before we can allow instrumentation calls */

void __start(void) __attribute__ ((no_instrument_function));
#endif

/****************************************************************************
* Name: stm32_fpuconfig
*
* Description:
* Configure the FPU. Relative bit settings:
*
* CPACR: Enables access to CP10 and CP11
* CONTROL.FPCA: Determines whether the FP extension is active in the
* current context:
* FPCCR.ASPEN: Enables automatic FP state preservation, then the
* processor sets this bit to 1 on successful completion of any FP
* instruction.
* FPCCR.LSPEN: Enables lazy context save of FP state. When this is
* done, the processor reserves space on the stack for the FP state,
* but does not save that state information to the stack.
*
* Software must not change the value of the ASPEN bit or LSPEN bit while either:
* - the CPACR permits access to CP10 and CP11, that give access to the FP
* extension, or
* - the CONTROL.FPCA bit is set to 1
*
****************************************************************************/

#ifdef CONFIG_ARCH_FPU
#if defined(CONFIG_ARMV7M_CMNVECTOR) && !defined(CONFIG_ARMV7M_LAZYFPU)

static inline void stm32_fpuconfig(void)
{
uint32_t regval;

/* Set CONTROL.FPCA so that we always get the extended context frame
* with the volatile FP registers stacked above the basic context.
*/

regval = getcontrol();
regval |= (1 << 2);
setcontrol(regval);

/* Ensure that FPCCR.LSPEN is disabled, so that we don't have to contend
* with the lazy FP context save behaviour. Clear FPCCR.ASPEN since we
* are going to turn on CONTROL.FPCA for all contexts.
*/

regval = getreg32(NVIC_FPCCR);
regval &= ~((1 << 31) | (1 << 30));
putreg32(regval, NVIC_FPCCR);

/* Enable full access to CP10 and CP11 */

regval = getreg32(NVIC_CPACR);
regval |= ((3 << (2*10)) | (3 << (2*11)));
putreg32(regval, NVIC_CPACR);
}

#else

static inline void stm32_fpuconfig(void)
{
uint32_t regval;

/* Clear CONTROL.FPCA so that we do not get the extended context frame
* with the volatile FP registers stacked in the saved context.
*/

regval = getcontrol();
regval &= ~(1 << 2);
setcontrol(regval);

/* Ensure that FPCCR.LSPEN is disabled, so that we don't have to contend
* with the lazy FP context save behaviour. Clear FPCCR.ASPEN since we
* are going to keep CONTROL.FPCA off for all contexts.
*/

regval = getreg32(NVIC_FPCCR);
regval &= ~((1 << 31) | (1 << 30));
putreg32(regval, NVIC_FPCCR);

/* Enable full access to CP10 and CP11 */

regval = getreg32(NVIC_CPACR);
regval |= ((3 << (2*10)) | (3 << (2*11)));
putreg32(regval, NVIC_CPACR);
}

#endif

#else
# define stm32_fpuconfig()
#endif

/****************************************************************************
* Name: go_os_start
*
* Description:
* Set the IDLE stack to the
*
****************************************************************************/

#ifdef CONFIG_STACK_COLORATION
static void go_os_start(void *pv, unsigned int nbytes)
{
/* Set the IDLE stack to the stack coloration value then jump to
* os_start(). We take extreme care here because were currently
* executing on this stack.
*
* We want to avoid sneak stack access generated by the compiler.
*/

__asm__ __volatile__
(
"\tmovs r1, r1, lsr #2\n" /* R1 = nwords = nbytes >
> 2 */
"\tbeq 2f\n" /* (should not happen) */

"\tbic r0, r0, #3\n" /* R0 = Aligned stackptr */
"\tmovw r2, #0xbeef\n" /* R2 = STACK_COLOR = 0xdeadbeef */
"\tmovt r2, #0xdead\n"

"1:\n" /* Top of the loop */
"\tsub r1, r1, #1\n" /* R1 nwords-- */
"\tcmp r1, #0\n" /* Check (nwords == 0) */
"\tstr r2, [r0], #4\n" /* Save stack color word, increment stackptr */
"\tbne 1b\n" /* Bottom of the loop */

"2:\n"
"\tmov r14, #0\n" /* LR = return address (none) */
"\tb os_start\n" /* Branch to os_start */
);
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: _start
*
* Description:
* This is the reset entry point.
*
****************************************************************************/

void __start(void)//从这里开始!!!!!!!!!!!!!!!!!
{
const uint32_t *src;
uint32_t *dest;

#ifdef CONFIG_ARMV7M_STACKCHECK
/* Set the stack limit before we attempt to call any functions */

__asm__ volatile ("sub r10, sp, %0" : : "r" (CONFIG_IDLETHREAD_STACKSIZE - 64) : );
#endif

/* Configure the UART so that we can get debug output as soon as possible */

stm32_clockconfig();
stm32_fpuconfig();
stm32_lowsetup();
stm32_gpioinit();
showprogress('A');

/* Clear .bss. We'll do this inline (vs. calling memset) just to be
* certain that there are no issues with the state of global variables.
*/

for (dest = _START_BSS; dest < _END_BSS; )
{
*dest++ = 0;
}

showprogress('B');

/* Move the initialized data section from his temporary holding spot in
* FLASH into the correct place in SRAM. The correct place in SRAM is
* give by _sdata and _edata. The temporary location is in FLASH at the
* end of all of the other read-only data (.text, .rodata) at _eronly.
*/

for (src = _DATA_INIT, dest = _START_DATA; dest < _END_DATA; )
{
*dest++ = *src++;
}

showprogress('C');

#ifdef CONFIG_ARMV7M_ITMSYSLOG
/* Perform ARMv7-M ITM SYSLOG initialization */

itm_syslog_initialize();
#endif

/* Perform early serial initialization */

#ifdef USE_EARLYSERIALINIT
up_earlyserialinit();
#endif
showprogress('D');

/* For the case of the separate user-/kernel-space build, perform whatever
* platform specific initialization of the user memory is required.
* Normally this just means initializing the user space .data and .bss
* segments.
*/

#ifdef CONFIG_BUILD_PROTECTED
stm32_userspace();
showprogress('E');
#endif

/* Initialize onboard resources */

stm32_boardinitialize();
showprogress('F');

/* Then start NuttX */

showprogress('\r');
showprogress('\n');

#ifdef CONFIG_STACK_COLORATION
/* Set the IDLE stack to the coloration value and jump into os_start() */

go_os_start((FAR void *)&_ebss, CONFIG_IDLETHREAD_STACKSIZE);
#else
/* Call os_start() */

os_start();

/* Shoulnd't get here */

for (; ; );
#endif
}

现在我们来看一下32是如何启动的:

代码位置:Firmware/build_px4fmu-v2_default/px4fmu-v2/Nuttx/nuttx/arch/arm/src/stm32/stm32_start.c
__start– #处理器执行的第一条指令(px4使用的是stm32,入口在stm32_start.c中)
|
v
stm32_clockconfig()—— #初始化时钟
|
v
rcc_reset() #复位rcc
stm32_stdclockconfig() #初始化标准时钟

rcc_enableperipherals() #使能外设时钟

stm32_fpuconfig() #配置fpu
|
v
stm32_lowsetup() #基本初始化串口,之后可以使用up_lowputc()
stm32_gpioinit() #初始化gpio,只是调用stm32_gpioremap()设置重映射
up_earlyserialinit() #初始化串口,之后可以使用up_putc()
stm32_boardinitialize()– #板级初始化
|
v
stm32_spiinitialize() #初始化spi,只是调用stm32_configgpio()设置gpio
stm32_usbinitialize() #初始化usb,只是调用stm32_configgpio()设置gpio
up_ledinit(); #初始化led,只是调用stm32_configgpio()设置gpio
|


在stm32_start.c文件中我们会看到这么一句话:

os_start()————— #初始化操作系统
|
v
dq_init() #初始化各种状态的任务列表(置为null)
g_pidhash[i]= #初始化唯一可以确定的元素–进程ID
g_pidhash[PIDHASH(0)]= #分配空闲任务的进程ID为0
g_idletcb= #初始化空闲任务的任务控制块
sem_initialize()– #初始化信号量
|
v
dq_init() #将信号量队列置为null
sem_initholders() #初始化持有者结构以支持优先级继承
|
——–
|
v
up_allocate_heap() #分配用户模式的堆(设置堆的起点和大小)
kumm_initialize() #初始化用户模式的堆
up_allocate_kheap() #分配内核模式的堆
kmm_initialize() #初始化内核模式的堆
task_initialize() #初始化任务数据结构
irq_initialize() #将所有中断向量都指向同一个异常中断处理程序
wd_initialize() #初始化看门狗数据结构
clock_initialize() #初始化rtc
timer_initialize() #配置POSIX定时器
sig_initialize() #初始化信号
mq_initialize() #初始化命名消息队列
pthread_initialize() #初始化线程特定的数据,空函数
fs_initialize()— #初始化文件系统
|
v
sem_init() #初始化节点信号量为1
files_initialize() #初始化文件数组,空函数
|
——–
|
v
net_initialize()– #初始化网络
|
v
uip_initialize() #初始化uIP层
net_initroute() #初始化路由表
netdev_seminit() #初始化网络设备信号量
arptimer_init() #初始化ARP定时器
|
——–
|
v
up_initialize()— #处理器特定的初始化
|
v
up_calibratedelay() #校准定时器
up_addregion() #增加额外的内存段
up_irqinitialize() #设置中断优先级,关联硬件异常处理函数
up_pminitialize() #初始化电源管理
up_dmainitialize() #初始化DMA
up_timerinit() #初始化定时器中断
devnull_register() #注册/dev/null
devzero_register() #注册/dev/zero
up_serialinit() #注册串口控制台/dev/console和串口/dev/ttyS0
up_rnginitialize() #初始化并注册随机数生成器
up_netinitialize() #初始化网络,是arch/arm/src/chip/stm32_eth.c中的
up_usbinitialize() #初始化usb驱动
board_led_on() #打开中断使能led,但很快会被其它地方的led操作改变状态
|
——–
|
v
lib_initialize() #初始化c库,空函数
group_allocate() #分配空闲组
group_setupidlefiles() #在空闲任务上创建stdout、stderr、stdin
group_initialize() #完全初始化空闲组
os_bringup()—— #创建初始任务
|
v
KEKERNEL_THREAD() #启动内核工作者线程
board_initialize() #最后一刻的板级初始化
TASK_CREATE() #启动默认应用程序
|
——–
|
v
forup_idle() #空闲任务循环
|
——————–
|
v
for(;;) #不应该到达这里