DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
At the highest level, the NuttX initialization sequence can be represented in three phases:
Phase A - The hardware-specific power-on reset initialization,
Phase B - NuttX RTOS initialization, and
Phase C - Application Initialization.
This initialization sequence is really quite simple because the system runs in single-thread mode up until the point the that is starts the application. That means that the initialization sequence is just simple, straight-line function calls.
...
Each of these will be discussed in more detail in the following paragraphssections.
Phase A - Power-On Reset Initialization.
Overview
The software system begins execution when the processor is reset. This usually at power-on, but all resets are basically the same whether they occur because of power-on, pressing the reset button, or on a watchdog timer expiration. The software code that executes when the processor is reset is unique to the particular CPU architecture and is not a common part of NuttX. The kinds kind of things that must be done by the architecture-specific reset handling includes:
- Putting the processor in its operational state. This may include things like setting CPU modes; initializing co-processors, etc.
- Setting up clocking so that the software and peripherals operate as expected,
- Setting up the C stack pointer (and other processor registers).
- Initializing memory, and
- Starting NuttX.
...
- It provides the (initial) values of the initialized variables by copying the values from FLASH into the
.datasection, and - It resets all of the uninitialized variables to zero. It clears the
.bsssection.
Case example: STM32 F4
...
We'll use Lets walk through reset sequence of one particular processor. Let's look at the NuttX initialization for the STM32 F4 MCU . This reset logic can be found in two files:and its popular evaluation board STM32F4Discovery as example but it can be applied to any supported architecture. Here is the map of initialization function calls
Lets walk through reset sequence. This reset logic can be found in two files:
nuttxnuttx/arch/arm/src/stm32_vectors.Snuttx/arch/arm/src/stm32_start.c
nuttx/arch/arm/src/stm32_vectors.S
The roll of stm32_vectors.S in this reset sequence is very small. This file provides all of the STM32 exception vectors and power-on reset is simply another exception vector. Some important things to note about this file:
.section .vectors, "ax". This pseudo operation will place all of the vectors into a special section call .vectors. On of the STM32 F4 linker scripts is located at nuttx/boards/arm/stm32/
...
stm32f4discovery/
...
scripts/ld.script. In that file, you can see that
...
section .vectors is forced to lie at the very beginning of FLASH memory. The STM32 F4 can be configured to boot in different ways via strapping. If it is strapped to boot from FLASH, then the STM32 FLASH memory will be aliased to address 0x0000 0000 when the reset occurs.
...
That is the address of the power-up reset interrupt vector.
The first two 32-bit entries in the vector table represent the power-up exception vector (which we know will be positioned at address 0x0000 0000 when the reset occurs). Those two entries are:
| Code Block |
|---|
.word IDLE_STACK /* Vector 0: Reset stack pointer */
.word __start /* Vector 1: Reset vector */
|
The Cortex-M family is unique in the way that is handles the reset vector. Notice that there are two values: the stack pointer for the start-up thread (the IDLE thread), and the entry point in the IDLE thread. When the reset occurs, the the stack pointer is automatically set to the first value and then the processor jumps to reset entry point
...
| No Format |
|---|
__start |
lies in the file nuttx/arch/arm/src/stm32/stm32_start.c and does the real, low-level architecture-specific initialization. This initialization includes:
stm32_clockconfig() ;-Initialize the PLLs and peripheral clocking needed by the board.stm32_fpuconfig() ;-If the STM32 F4's hardware floating point is initialized, then configure the FPU and enable access to the FPU co-processors.stm32_lowsetup() ;-Enable the low-level UART. This is done very early in initialization so that we can get serial debug output to the console as soon as possible. If you are doing a board bring-up this is very important.stm32_gpioinit() ;-Perform any GPIO remapping that is needed (this is a stub for the F4, but the F1 family requires this step).- ''showprogress('A');'' This simply outputs the character 'A' on the serial console (only if
CONFIG_DEBUGis enabled). If debug is enabled, you will always see the letters ABDE output on the console. That output all comes from this file. - Next the memory is initialized:
- The
.bsssection is set to zero (Letter 'B' is then output ifCONFIG_DEBUGis enabled), then - The
.datasection is set to its initial values (The letter 'C' is output if debug is enabled),
- The
- Then board-specific logic is initialized:stm32
stm32_boardinitialize(); This function resides with the board - Board-specific logic is initialized by calling this function. For the case of the STM3240G-EVAL STM32F4Discovery board, this board initialization logic can be found at nuttx/boards/arm/stm32/stm3240g-evalstm32f4discovery/src/stm32_boot.c.For the case of the STM3240G-EVAL board, thestm32_boardinitialize()and does the following operations:- stm32_spidev_spiinitializeinitialize() ;- Initialize SPI chip selects if SPI is enabled.
- stm32_selectsram(); Configure the STM32 FSMC to support external SRAM if external SRAM support is enabled.usbinitialize() - Initialize hardware USB devices if enabled.
- stm32_netinitialize() - Initialize hardware network devices if enabled.
- boardstm32_autoled_initialize() ; Initialize the - Configure on-board LEDs if they are usedLED support has been selected.
When
stm32_boardinitialize() returns toNo Format __start()
, the low-level, architecture-specific initialization is complete and NuttX is started:.
Phase B - NuttX RTOS Initialization
nx_start()
...
This function resides in the file nuttx/sched/init/nx_start.c and
...
is the NuttX entry point.
...
It is called by __start() and performs the next phase of RTOS-specific initialization
...
before bringing up the application.
The operations performed by nx_start() are discussed in the next paragraph.
NuttX RTOS Initialization
nx_start()
When the low-level, architecture-specific initialization is complete and NuttX is started by calling the function nx_start(). This function resides in the file nuttx/sched/init/nx_start.c. The operations performed by nx_start() are summarized below. Note that many of these features can be disabled from the NuttX configuration file and in that case those operations are not performed:
...
summarized below. Note that many of these features can be disabled from the NuttX configuration file and in that case those operations are not performed:
1- Initializes some NuttX global data structures,
2- Initializes the TCB for the IDLE (i.e, the thread that the initialization is performed on),
3- nxsem_initialize() - Initialize the POSIX semaphore facilities. This needs to be done first
...
because almost all other OS features depend on POSIX counting semaphores.
...
4- Memory organization - This includes heap configuration, memory manager, paging, I/O buffers, etc.
5- task_initialize() - Initialize task data structures.
6- fs_initialize() - Initialize the file system (needed to support device drivers).
7- irq_initialize()
...
- Initialize the interrupt handler subsystem. This initializes only data structures; CPU interrupts are still disabled.
8- wd_initialize()
...
- Initialize the NuttX watchdog timer facility,
9- clock_initialize()
...
- Initialize the system clock,
10- timer_initialize()
...
- Initialize the POSIX timer facilities,
...
11- nxsig_initialize()
...
- Initialize the POSIX signal facilities,
...
12- nxmq_initialize()
...
- Initialize the POSIX message queue facilities,
13- pthread_initialize()
...
- Initialize the POSIX pthread facilities,
...
14- net_initialize()
...
- Initialize networking facilities,
Up to this point, all of the initialization steps have only been software initializations. Nothing has interacted with the hardware. Rather, all of these steps simply prepared the environment so that things like interrupts and threads can function properly. The next phases depend upon that setup.
15- up_initialize()
...
- The processor specific details of running the operating system will be handled here. Such things as setting up interrupt service routines and starting the clock are some of the things that are different for each processor and hardware platform.
...
IDLE Thread Activities
As mention, the IDLE thread is the thread that executes only when there is nothing else to do in the system. It has the lowest priority in the system. It always has the priority 0. It is the only thread that is permitted to have the priority 0. And it can never be blocked (otherwise, what would run then?).
As a result, the IDLE thread is always in the g_readytorun list and, in fact, since that list is prioritized, can guaranteed to always be the final entry at the tail of the g_readytorun list.
The IDLE is an an infinite loop. But this does not make it a “CPU hog.” Since it is the lowest priority, the it can be suspended whenever anything else needs to run.
The IDLE thread does two things in this infinite loop:
...
All ARM-based MCUs share a common up_initialize() implementation provided at nuttx/arch/arm/src/common/up_initialize.c. The operations perform by this common ARM initialization will, however, call into facilities provided by the particular ARM chip. For the STM32 F4, those facilities would be provided by logic in files as nuttx/arch/arm/src/stm32. The common ARM initialization sequence is:
up_color_intstack() - Colorize the interrupt stack.
arm_addregion() - The basic heap was set up during processing by nx_start(). However, if the board supports multiple, discontiguous memory regions, any addition memory regions can be added to the heap by this function. For the STM32 F4, up_addregion() is implemented in nuttx/arch/arm/src/stm32/stm32_allocateheap.c.
arm_pminitialize() - If CONFIG_PM is defined, the function must initialize the power management subsystem. This MCU-specific function must be called very early in the intialization sequence before any other device drivers are initialized (since they may attempt to register with the power management subsystem). There is no implementation of up_pminitialize() for any STM32 platform.
arm_dmainitialize(); Initialize the DMA subsystem. For the STM32 F4, this DMA initialization can be found in nuttx/arch/arm/src/stm32/stm32_dma.c (which includes nuttx/arch/arm/src/stm32f4xxx_dma.c).
devnull_register(); Registers the standard /dev/null.
devrandom_register(); Registers the standard /dev/random.
devurandom_register(); Registers the standard /dev/urandom.
up_irqinitialize(); This function initialize the interrupt subsystem. For the STM32 F4, up_irqinitialize() is implemented in nuttx/arch/arm/src/stm32/stm32_irq.c.
up_timerinit(); Initialize the system timer interrupt. For the STM32 F4, this function initializes the ARM Cortex-M SYSTICK timer and can be found at nuttx/arch/arm/src/stm32/stm32_timerisr.c.
Then this function initializes the console device (if any). This means calling one of (1) up_serialinit(); for the standard serial driver (found at nuttx/arch/arm/src/stm32/stm32_serial.c for the STM32 F4), (2) lowconsole_init(); for the low-level, write-only serial console (found at nuttx/drivers/serial/lowconsole_init.c), or (2) ramlog_sysloginit() for the RAM console (found at nuttx/drivers/ramlog.c).
up_netinitialize(); Initialize the network. For the STM32 F4, this function is in nuttx/arch/arm/src/stm32/stm32_eth.c.
up_usbinitialize(); Initialize USB (host or device). For the STM32 F4, this function is in nuttx/arch/arm/src/stm32/stm32_otgfsdev.c.
up_ledon(LED_IRQSENABLED); Finally, up_initialize() illuminates board-specific LEDs to indicate the IRQs are now enabled.
16- board_early_initialize() - If CONFIG_BOARD_EARLY_INITIALIZE is selected, then an additional initialization call will be performed in the boot-up sequence to a function called board_early_initialize(). It will be called immediately after up_initialize() (and may be thought of as a board-specific, extension of up_initialize()) and well before board_late_initialize() is called and the initial application is started.
17- g_nx_initstate = OSINIT_HARDWARE - This signals that basic hardware setup is complete
18- shm_initialize() - Initialize shared memory support
19- lib_initialize() - Initialize the C libraries. This is done last because the libraries may depend on the above.
20- binfmt_initialize() - Initialize binary loader subsystem.
21- Start SMP support in multi-core MCUs. This is not the case in STM32F4Discovery but relevant because here are created stdin, stdout and stderr for each CPU's (even if there is only one) IDLE task. All tasks subsequently created by the IDLE thread will inherit these file descriptors.
22- syslog_initialize() - Late initialization of the system logging device. Some SYSLOG channel must be initialized late in the initialization sequence because it may depend on having IDLE task file structures setup.
23- nx_bringup() - Create the initial tasks. This will be described more below
...
.
nx_bringup()
This function is called at the very end of the initialization sequence in nx_start(), just before entering the IDLE loop. This function It is located in nuttx/sched/init/nx_bringup.c. This function and it starts all of the required threads and tasks needed to bring up the system. This function performed the following specific operations:
...
nx_pgworker()- Start the page fill worker kernel thread that will resolve page faults. This should always be the first thread started because it may have to resolve page faults in other threads. This is the task that runs in order to satisfy page faults in processors that have an MMU and in configurations where on-demand paging is enabled.
...
nx_workqueues()- Start the worker thread. The worker thread may be used to execute any processing deferred to the worker thread via APIs provided ininclude/nuttx/wqueue.h. The worker thread's primary function is as the “bottom half” for extended device driver processing but can be used for a variety of purposes like misc garbage clean-up.
...
nx_
...
create_initthread()
...
- - Once the operating system has been initialized,this funcions either directly calls nx_start_application() or creates a thread for running it
nx_start_application()- If set in the NuttX configuration, this function calls board_late_initialize().board_late_initialize()is a last-minute, board-specific initialization. Note that there was earlier, board-specific initialization calls (tostm32_board_initialize()and toboard_early_initialize()). The difference here is these first, low-level initialization calls were made before the OS was completely launched.board_late_initialize(), on the other hand, is called at the end after the OS has been initialized but before any application tasks have been started.board_late_initialize()would be an ideal place to do board-specific initialization steps that depend on having a fully initialized OS such as memory allocations, initialization of complex device drivers, mounting of file systems, etc.
- After that,
nx_start_application()launches the application either by creating a task for it or executing a program from a filesystem after mounting it. - In the case of creating a task for the application, its entry has the name
user_start().user_start()is provided by application code and when it runs, it begins the application-specific phase of the initialization sequence as described below.
NOTE: The default user_start() entry point can be changed to use one of the named applications used by NSH. This is a start-up option that is not often used and will not be discussed further here.
And finally enter the IDLE loop. After completing the initialization, the role of the IDLE thread changes. It becomes the thread that executes only when there is nothing else to do in the system (hence, the name IDLE thread).
IDLE Thread Activities
As mention, the IDLE thread is the thread that executes only when there is nothing else to do in the system. It has the lowest priority in the system. It always has the priority 0. It is the only thread that is permitted to have the priority 0. And it can never be blocked (otherwise, what would run then?).
As a result, the IDLE thread is always in the g_readytorun list and, in fact, since that list is prioritized, can guaranteed to always be the final entry at the tail of the g_readytorun list.
The IDLE is an an infinite loop. But this does not make it a “CPU hog.” Since it is the lowest priority, the it can be suspended whenever anything else needs to run.
The IDLE thread does two things in this infinite loop:
- If the worker was not started (see
nx_bringup()below), then the IDLE thread will perform memory clean-up. Memory clean is required to handle deferred memory deallocation. Memory allocations must be deferred when the memory is freed in a context where the software does not have access to the heap and, hence, cannot truly free the memory (such as in an interrupt handler). In this case, the memory is simply put into a list of freed memory and, eventually, cleaned up by the IDLE thread. NOTE: The worker thread's primary function is as the “bottom half” for extended device driver processing. If the worker thread was started, then it will run at a higher priority than the IDLE thread. In this case, the worker thread will take over responsibility for cleaning up these deferred allocations. up_idle();Then the loop callsup_idle(). The operations performed byup_idle()are architecture- and board-specific. In general, this is the location where CPU-specific reduced power operations may be performed.
STM32 F4 up_initialize()
NOTE: The default user_start() entry point can be changed to use one of the named applications used by NSH. This is a start-up option that is not often used and will not be discussed further here.
STM32 F4 up_initialize()
All ARM-based MCUs share a common up_initialize() implementation provided at nuttx/arch/arm/common/up_initialize.c. The operations perform by this common ARM initialization will, however, call into facilities provided by the particular ARM chip. For the STM32 F4, those facilities would be provided by logic in files as nuttx/arch/arm/src/stm32. The common ARM initialization sequence is:
...
STM32 F4 IDLE thread
The default STM32 F4 IDLE thread is located at nuttx/arch/arm/src/stm32_idle.c. This default version does very little:
...