These are the files contained in the <code>arch/</code><i><arch-name></i><code>/</code> directory and are discussed in a paragraph <ahref="#archdirectorystructure">below</a>.
As an example, all ARM processor architectures are provided under the <code>arch/arm/</code> directory which is selected with the <code>CONFIG_ARCH="arm"</code> configuration option.
Extending this example, the ARMv7-M ARM family is supported by logic in <code>arch/arm/include/armv7-m</code> and <code>arch/arm/src/armv7-m</code> directories which are selected by the <code>CONFIG_ARCH_CORTEXM3=y</code>, <code>CONFIG_ARCH_CORTEXM4=y</code>, or <code>CONFIG_ARCH_CORTEXM7=y</code> configuration options
Each processor architecture is embedded in a <i>System-on-a-Chip</i> (SoC) architecture.
The full SoC architecture includes the processor architecture plus chip-specific interrupt logic, clocking logic, general purpose I/O (GPIO) logic, and specialized, internal peripherals (such as UARTs, USB, etc.).
</p>
<p>
These chip-specific files are contained within chip-specific sub-directories also under the
As an example, the STMicro STM32 SoC architecture is based on the ARMv7-M processor and is supported by logic in the <code>arch/arm/include/stm32</code> and <code>arch/arm/src/stm32</code> directories which are selected with the <code>CONFIG_ARCH_CHIP="stm32"</code> configuration setting.
In order to be usable, the chip must be contained in a board environment.
The board configuration defines additional properties of the board including such things as peripheral LEDs, external peripherals (such as networks, USB, etc.).
The directory <code>configs/stm32f4disovery/</code>, as an example, holds board-specific logic for the STM32F4 Discovery board and is selected via the <code>CONFIG_ARCH_BOARD="stm32f4discovery"</code> configuration setting.
All of the specific boards supported by NuttX are identified in the <ahref="https://bitbucket.org/nuttx/nuttx/configs/src/master/README.txt"target="_blank">README.txt</a> file.
Okay, so you have created a new board configuration directory.
Now, how do you hook this board into the configuration system so that you can select with <code>make menuconfig</code>?
</p>
<p>
You will need modify the file <code>configs/Kconfig</code>.
Let's look at the STM32F4-Discovery configuration in the <code>Kconfig</code> file and see how we would add a new board directory to the configuration.
For this configuration let's say that you new board resides in the directory <code>configs/myboard</code>;
It uses an MCU selected with <code>CONFIG_ARCH_CHIP_MYMCU</code>; and you want the board to be selected with <code>CONFIG_ARCH_BOARD_MYBOARD</code>.
Then here is how you can clone the STM32F4-Discovery configuration in <code>configs/Kconfig</code> to support your new board configuration.
</p>
<p>
In <code>configs/Kconfig</code> for the stm32f4-discovery, you will see a configuration definition like this:
<p>
<ul><pre>
config ARCH_BOARD_STM32F4_DISCOVERY
bool "STMicro STM32F4-Discovery board"
depends on ARCH_CHIP_STM32F407VG
select ARCH_HAVE_LEDS
select ARCH_HAVE_BUTTONS
select ARCH_HAVE_IRQBUTTONS
---help---
STMicro STM32F4-Discovery board based on the STMicro STM32F407VGT6 MCU.
</pre></ul>
<p>
The above selects the STM32F4-Discovery board.
The <code>select</code> lines say that the board has both LEDs and buttons and that the board can generate interrupts from the button presses.
You can just copy the above configuration definition to a new location (notice that they the configurations are in alphabetical order).
Then you should edit the configuration to support your board.
The final configuration definition might look something like:
</p>
<ul><pre>
config ARCH_BOARD_MYBOARD
bool "My very own board configuration"
depends on ARCH_CHIP_MYMCU
select ARCH_HAVE_LEDS
select ARCH_HAVE_BUTTONS
select ARCH_HAVE_IRQBUTTONS
---help---
This options selects the board configuration for my very own board
based on the MYMCU processor.
</pre></ul>
<p>
Later in the <code>configs/Kconfig</code> file, you will see a long, long string configuration with lots of defaults like this:
</p>
<ul><pre>
config ARCH_BOARD
string
default "amber" if ARCH_BOARD_AMBER
default "avr32dev1" if ARCH_BOARD_AVR32DEV1
default "c5471evm" if ARCH_BOARD_C5471EVM
...
default "stm32f4discovery" if ARCH_BOARD_STM32F4_DISCOVERY
...
</pre></ul>
<p>
This logic will assign string value to a configuration variable called <code>CONFIG_ARCH_BOARD</code> that will name the directory where the board-specific files reside.
In our case, these files reside in <code>configs/myboard</code> and we add the following to the long list of defaults (again in alphabetical order):
However, if NuttX is built as a separately compiled kernel (with <code>CONFIG_BUILD_PROTECTED=y</code> or <code>CONFIG_BUILD_KERNEL=y</code>), then the contents of this directory are built as two libraries:
These user/kernel space libraries (along with the sycalls of <ahref="#DirStructSyscall"><code>nuttx/syscall</code></a>) are needed to support the two differing protection domains.
<ahref="#boardconfigsubdirs">board-specific configuration files</a> into the top level directory which appears in the make files as the make variable, <code>${TOPDIR}</code>.
<li>Copy <code>configs/</code><i><board-name></i><code>/[</code><i><config-dir></i><code>/]Make.defs</code> to <code>${TOPDIR}/Make.defs</code>,<li>
<li>Copy <code>configs/</code><i><board-name></i><code>/[</code><i><config-dir></i><code>/]defconfig</code> to <code>${TOPDIR}/.config</code></li>
There is an alternative Windows batch file, <code>configure.bat</code>, that can be used instead of <code>configure.sh</code> in the windows native environment like:
The <code>.version</code> file is also used during the build process to create a C header file at <code>include/nuttx/version.h</code> that contains the same version information.
That version file may be used by your C applications for, as an example, reporting version information.
The specific environmental definitions are unique for each board but should include, as a minimum, updates to the <code>PATH</code> variable to include the full path to the architecture-specific toolchain identified in <ahref="#boardconfigsubdirs"><code>Make.defs</code></a>.
<li>Auto-generating the file <code>include/nuttx/config.h</code> using the <code>${TOPDIR}/.config</code> file.</li>
<li>Auto-generating the file <code>${TOPDIR}/.version</code> with version 0.0 if one does not exist.</li>
<li>Auto-generating the file <code>include/nuttx/version.h</code> using the <code>${TOPDIR}/.version</code> file.</li>
<li>Creating a link to <code>${TOPDIR}/arch/</code><i><arch-name></i><code>/include</code> at <code>${TOPDIR}/include/arch</code>.</li>
<li>Creating a link to <code>${TOPDIR}/configs/</code><i><board-name></i><code>/include</code> at <code>${TOPDIR}/include/arch/board</code>.</li>
<li>Creating a link to <code>${TOPDIR}/configs/</code><i><board-name></i><code>/src</code> at <code>${TOPDIR}/arch/</code><i><arch-name></i><code>/src/board</code></li>
<li>Creating a link to <code>${APPDIR}/include</code> at <code>${TOPDIR}/include/apps</code></li>
<code>up_</code> is supposed to stand for microprocessor; the <code>u</code> is like the Greek letter micron: <i><EFBFBD></i>. So it would be <code><EFBFBD>P</code> which is a common shortening of the word microprocessor. I don't like that name very much. I wish I would have used a more obvious prefix like <code>arch_</code> instead -- then I would not have to answer this question so often.
An interface which is unique to a certain microprocessor should be prefixed with the name of the microprocessor, for example <code>stm32_</code>, and be prototyped in some header file in the <code>arch/</code> directories.
There is also a <code>arch/<architecture>/include/<chip>/chip.h</code> header file that can be used to communicate other microprocessor-specific information between the board logic and even application logic.
Any interface that is common to all boards should be prefixed with <code>board_</code> and should also be prototyped in <code>include/nuttx/arch.h</code>.
These <code>board_</code> definitions provide the interface between the board-level logic and the architecture-specific logic.
There is also a <code>configs/<board>/include/board.h</code> header file that can be used to communicate other board-specific information between the architecture logic and even application logic.
These should be prototyped in <code>configs/<board>/src/<board>.h</code> and should not be used outside of that directory since board-specific definitions have no meaning outside of the board directory.
Header files are made accessible to internal OS logic and to applications through symbolic links and through <i>include paths</i> that are provided to the C/C++ compiler.
In this case I am referring to the OS as layered into application interface, common internal OS logic, and lower level platform-specific layers.
The platform-specific layers all reside in the either <code>arch/</code> sub-directories on the <code>config/</code> subdirectories: The former sub-directories are reserved for microcontroller-specific logic and the latter for board-specific logic.
In the strict, layered NuttX architecture, the upper level OS services are always available to platform-specific logic. However, the opposite is <i>not</i> true: Common OS logic must never have any dependency on the lower level platform-specific code. The OS logic must be totally agnostic about its hardware environment. Similarly, microcontroller-specific logic was be completely ignorant of board-specific logic.
</p>
<p>
This strict layering is enforced in the NuttX build system by controlling the compiler include paths: Higher level code can never include header files from either; of the platform-specific source directories; microcontroller-specific code can never include header files from the board-specific source directories. The board-specific directories are, then, at the bottom of the layered hierarchy.
</p>
<p>
An exception to these inclusion restrictions is the platform-specific <i>include/</i>. These are made available to higher level OS logic. The microcontroller-specific include directory will be linked at <code>include/arch/chip</code> and, hence, can be included like <code>#include <arch/chip/chip.h</code>.
Similarly, the board-specific include directory will be linked at <code>include/arch/board</code> and, hence, can be included like <code>#include <arch/board/board.h</code>.
Keeping in the spirit of the layered architecture, these publicly visible header files must <i>not</i> export platform-specific definitions; Only platform-specific realizations of standardized declarations should be visible.
Those <i>standardized declarations</i> should appear in common header files such as those provided by <code>include/nuttx/arch.h</code> and <code>include/nuttx/board.h</code>.
Similarly, these publicly visible header file must <i>not</i> include files that reside in the inaccessible platform-specific source directories.
For example, the board-specific <code>configs/<board>/include/board.h</code> header file must never include microcontroller-specific header files that reside in <code>arch/<arch>/src/<mcu></code>.
That practice will cause inclusion failures when the publicly visible file is included in common logic outside of the platform-specific source directories.
If <code>CONFIG_BUILD_PROTECTED</code> or <code>CONFIG_BUILD_KERNEL</code> have been selected in the NuttX configuration, then special initialization may need to be performed depending on the task type specified in the TCB's flags field:
If neither <code>CONFIG_BUILD_PROTECTED</code> nor <code>CONFIG_BUILD_KERNEL</code> have been selected, then all threads should have kernel-mode privileges.
This thread type is normally available in the flags field of the TCB, however, there are certain contexts where the TCB may not be fully initialized when up_create_stack is called.
If <code>CONFIG_BUILD_PROTECTED</code> or <code>CONFIG_BUILD_KERNEL</code> are defined, then this thread type may affect how the stack is allocated. For example, kernel thread stacks should be allocated from protected kernel memory. Stacks for user tasks and threads must come from memory that is accessible to user code.
NOTE: Unlike <code>up_stack_create()</code> and <code>up_stack_release</code>, this function does not require the task type (<code>ttype</code>) parameter.
The TCB flags will always be set to provide the task type to <code>up_use_stack()</code> if the information needs that information.
This function may be called any time after <code>up_create_stack()</code> or <code>up_use_stack()</code> have been called but before the task has been started.
This thread type is normally available in the flags field of the TCB, however, there are certain error recovery contexts where the TCB may not be fully initialized when up_release_stack is called.
For the kernel build (<code>CONFIG_BUILD_PROTECTED=y</code> or <code>CONFIG_BUILD_KERNEL=y</code>) with both kernel- and user-space heaps (<code>CONFIG_MM_KERNEL_HEAP=y</code>), this function provides the size of the unprotected, user-space heap.
That timer interrupt runs at rate determined by <code>CONFIG_USEC_PER_TICK</code> (default 10000 microseconds or 100Hz. If <code>CONFIG_SCHED_TICKLESS</code> is selected, the default is 100 microseconds).
The timer generates an interrupt each <code>CONFIG_USEC_PER_TICK</code> microseconds and increments a counter called <code>g_system_timer</code>.
<code>g_system_timer</code> then provides a time-base for calculating <i>up-time</i> and elapsed time intervals in units of <code>CONFIG_USEC_PER_TICK</code>.
On many system, the exact timer interval specified by <code>CONFIG_USEC_PER_TICK</code> cannot be achieved due to limitations in frequencies or in dividers.
As a result, the time interval specified by <code>CONFIG_USEC_PER_TICK</code> may only be approximate and there may be small errors in the apparent <i>up-time</i> time.
These small errors, however, will accumulate over time and after a long period of time may have an unacceptably large error in the apparent <i>up-time</i> of the MCU.
If the timer tick period generated by the hardware is not exactly <code>CONFIG_USEC_PER_TICK</code><i>and</i> if there you require accurate up-time for the MCU, then there are measures that you can take:
If <code>CONFIG_RTC_HIRES</code> is enabled in the NuttX configuration, then the RTC provides higher resolution time and completely replaces the system timer for purpose of date and time.
<dt><code>CONFIG_RTC_FREQUENCY</code>
<dd>If <code>CONFIG_RTC_HIRES</code> is defined, then the frequency of the high resolution RTC must be provided.
If <code>CONFIG_RTC_HIRES</code> is not defined, <code>CONFIG_RTC_FREQUENCY</code> is assumed to be one.
By default, a NuttX configuration uses a periodic timer interrupt that drives all system timing. The timer is provided by architecture-specific code that calls into NuttX at a rate controlled by <code>CONFIG_USEC_PER_TICK</code>. The default value of <code>CONFIG_USEC_PER_TICK</code> is 10000 microseconds which corresponds to a timer interrupt rate of 100 Hz.
</p>
<p>
On each timer interrupt, NuttX does these things:
<p>
<ul>
<li>Increments a counter. This counter is the system time and has a resolution of <code>CONFIG_USEC_PER_TICK</code> microseconds.
<li>Checks if it is time to perform time-slice operations on tasks that have select round-robin scheduling.
<li>Checks for expiration of timed events.
</ul>
<p>
What is wrong with this default system timer? Nothing really. It is reliable and uses only a small fraction of the CPU band width. But we can do better. Some limitations of default system timer are, in increasing order of importance:
</p>
<ul>
<li>
<b>Overhead</b>:
Although the CPU usage of the system timer interrupt at 100Hz is really very low, it is still mostly wasted processing time. One most timer interrupts, there is really nothing that needs be done other than incrementing the counter.
</li>
<li>
<b>Resolution</b>:
Resolution of all system timing is also determined by <code>CONFIG_USEC_PER_TICK</code>. So nothing that be time with resolution finer than 10 milliseconds be default. To increase this resolution, <code>CONFIG_USEC_PER_TICK</code> an be reduced. However, then the system timer interrupts use more of the CPU bandwidth processing useless interrupts.
</li>
<li>
<b>Power Usage</b>:
But the biggest issue is power usage. When the system is IDLE, it enters a light, low-power mode (for ARMs, this mode is entered with the <code>wfi</code> or <code>wfe</code> instructions for example). But each interrupt awakens the system from this low power mode. Therefore, higher rates of interrupts cause greater power consumption.
</li>
</ul>
<p>
<b><i>Tickless OS</i></b>.
The so-called <i>Tickless OS</i> provides one solution to issue. The basic concept here is that the periodic, timer interrupt is eliminated and replaced with a one-shot, interval timer. It becomes event driven instead of polled: The default system timer is a polled design. On each interrupt, the NuttX logic checks if it needs to do anything and, if so, it does it.
</p>
<p>
Using an interval timer, one can anticipate when the next interesting OS event will occur, program the interval time and wait for it to fire. When the interval time fires, then the scheduled activity is performed.
In order to use the Tickless OS, one must provide special support from the platform-specific code. Just as with the default system timer, the platform-specific code must provide the timer resources to support the OS behavior.
Currently these timer resources are only provided on a few platforms. An example implementation is for the simulation is at <code>nuttx/arch/sim/src/up_tickless.c</code>. There is another example for the Atmel SAMA5 at <code>nuttx/arch/arm/src/sama5/sam_tickless.c</code>. These paragraphs will explain how to provide the Tickless OS support to any platform.
If the platform provides support for the <i>Tickless OS</i>, then this setting should be selected in the <code>Kconfig</code> file for the board. Here is what the selection looks in the <code>arch/Kconfig</code> file for the simulated platform:
</p>
<ul><pre>
config ARCH_SIM
bool "Simulation"
select ARCH_HAVE_TICKLESS
---help---
Linux/Cywgin user-mode simulation.
</pre></ul>
<p>
When the simulation platform is selected, <code>ARCH_HAVE_TICKLESS</code> is automatically selected, informing the configuration system that <i>Tickless OS</i> options can be selected.
If <code>CONFIG_ARCH_HAVE_TICKLESS</code> is selected, then it will enable the Tickless OS features in NuttX.
</p>
</li>
<li>
<p>
<b><code>CONFIG_SCHED_TICKLESS_ALARM</code></b>:
The tickless option can be supported either via a simple interval timer (plus elapsed time) or via an alarm. The interval timer allows programming events to occur after an interval. With the alarm, you can set a time in the future and get an event when that alarm goes off. This option selects the use of an alarm.
</p>
<p>
The advantage of an alarm is that it avoids some small timing errors; the advantage of the use of the interval timer is that the hardware requirement may be less.
This option is not unique to <i>Tickless OS</i> operation, but changes its relevance when the <i>Tickless OS</i> is selected.
In the default configuration where system time is provided by a periodic timer interrupt, the default system timer is configure the timer for 100Hz or <code>CONFIG_USEC_PER_TICK=10000</code>. If <code>CONFIG_SCHED_TICKLESS</code> is selected, then there are no system timer interrupt. In this case, <code>CONFIG_USEC_PER_TICK</code> does not control any timer rates. Rather, it only determines the resolution of time reported by <code>clock_systimer()</code> and the resolution of times that can be set for certain delays including watchdog timers and delayed work.
In this case there is still a trade-off: It is better to have the <code>CONFIG_USEC_PER_TICK</code> as low as possible for higher timing resolution. However, the time is currently held in <code>unsigned int</code>. On some systems, this may be 16-bits in width but on most contemporary systems it will be 32-bits. In either case, smaller values of <code>CONFIG_USEC_PER_TICK</code> will reduce the range of values that delays that can be represented. So the trade-off is between range and resolution (you could also modify the code to use a 64-bit value if you really want both).
The interfaces that must be provided by the platform specified code are defined in <code>include/nuttx/arch.h</code>, listed below, and summarized in the following paragraphs:
The tickless option can be supported either via a simple interval timer (plus elapsed time) or via an alarm. The interval timer allows programming events to occur after an interval. With the alarm, you can set a time in* the future and get an event when that alarm goes off.
</p>
<p>
If <code>CONFIG_SCHED_TICKLESS_ALARM</code> is defined, then the platform code must provide the following:
Note that a platform-specific implementation would probably require two hardware timers: (1) A interval timer to satisfy the requirements of <ahref="#uptimerstart"><code>up_timer_start()</code></a> and <ahref="#uptimercancel"><code>up_timer_cancel()</code></a>, and a (2) a counter to handle the requirement of <ahref="#uptimergettime"><code>up_timer_gettime()</code></a>. Ideally, both timers would run at the rate determined by <code>CONFIG_USEC_PER_TICK</code> (and certainly never slower than that rate).
The job could be done with a single timer if, for example, the single timer were kept in a free-running at all times. Some timer/counters have the capability to generate a compare interrupt when the timer matches a comparison value but also to continue counting without stopping. If your hardware supports such counters, one might used the <code>CONFIG_SCHED_TICKLESS_ALARM</code> option and be able to simply set the comparison count at the value of the free running timer <i>PLUS</i> the desired delay. Then you could have both with a single timer: An alarm and a free-running counter with the same timer!
In addition to these imported interfaces, the RTOS will export the following interfaces for use by the platform-specific interval timer implementation:
Initializes all platform-specific timer facilities. This function is called early in the initialization sequence by <code>up_initialize()</code>. On return, the current up-time should be available from <code>up_timer_gettime()</code> and the interval timer is ready for use (but not actively timing).
Return the elapsed time since power-up (or, more correctly, since <i><arch></i><code>_timer_initialize()</code> was called). This function is functionally equivalent to <code>clock_gettime()</code> for the clock ID <code>CLOCK_MONOTONIC</code>. This function provides the basis for reporting the current time and also is used to eliminate error build-up from small errors in interval time calculations.
Called from the normal tasking context. The implementation must provide whatever mutual exclusion is necessary for correct operation. This can include disabling interrupts in order to assure atomic register operations.
Cancel the alarm and return the time of cancellation of the alarm. These two steps need to be as nearly atomic as possible. <code>sched_timer_expiration()</code> will not be called unless the alarm is restarted with <code>up_alarm_start()</code>. If, as a race condition, the alarm has already expired when this function is called, then time returned is the current time.
<ul>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
<li><code>ts</code>: Location to return the expiration time. The current time should be returned if the timer is not active. <code>ts</code> may be <code>NULL</code> in which case the time is not returned</li>
</ul>
<p><b>Returned Value</b>:</p>
<ul>
Zero (<code>OK</code>) on success; a negated <code>errno</code> value on failure.
Start the alarm. <code>sched_timer_expiration()</code> will be called when the alarm occurs (unless <code>up_alarm_cancel</code> is called to stop it).
<li><code>ts</code>: The time in the future at the alarm is expected to occur. When the alarm occurs the timer logic will call <code>sched_timer_expiration()</code>.</li>
</ul>
<p><b>Returned Value</b>:</p>
<ul>
Zero (<code>OK</code>) on success; a negated <code>errno</code> value on failure.
</ul>
<p><b>Assumptions</b>:</p>
<ul>
May be called from interrupt level handling or from the normal tasking level. Interrupts may need to be disabled internally to assure non-reentrancy.
Cancel the interval timer and return the time remaining on the timer. These two steps need to be as nearly atomic as possible. <code>sched_timer_expiration()</code> will not be called unless the timer is restarted with <code>up_timer_start()</code>. If, as a race condition, the timer has already expired when this function is called, then that pending interrupt must be cleared so that <code>sched_timer_expiration()</code> is not called spuriously and the remaining time of zero should be returned.
Start the interval timer. <code>sched_timer_expiration()</code> will be called at the completion of the timeout (unless <code>up_timer_cancel()</code> is called to stop the timing).
<b>Description:</b> The <code>wd_create()</code> function will create a timer by allocating the appropriate resources for the watchdog. The <code>wd_create()</code> function returns a pointer to a fully initialized, dynamically allocated <code>struct wdog_s</code> instance (which is <code>typedef</code>'ed as <code>WDOG_ID</code>);
</p>
<p>
<code>wd_static()</code> performs the equivalent initialization of a statically allocated <code>struct wdog_s</code> instance. No allocation is performed in this case. The initializer definition, <code>WDOG_INITIALIZER</code> is also available for initialization of static instances of <code>struct wdog_s</code>. NOTE: <code>wd_static()</code> is also implemented as a macro definition.
<li>Pointer to watchdog that may be used as a handle in subsequent NuttX calls (i.e., the watchdog ID), or NULL if insufficient resources are available to create the watchdogs.
<b>Description:</b> The wd_delete function will deallocate a watchdog timer previously allocated via <code>wd_create()</code>. The watchdog timer will be removed from the timer queue if has been started.
When a watchdog expires, the callback function with this type is called:
</p>
<pre>
typedef void (*wdentry_t)(int argc, ...);
</pre>
<p>
Where <code>argc</code> is the number of <code>wdparm_t</code> type arguments that follow.
</p>
<p>
The arguments are passed as scalar <code>wdparm_t</code> values. For systems where the <code>sizeof(pointer) < sizeof(uint32_t)</code>, the following union defines the alignment of the pointer within the <code>uint32_t</code>. For example, the SDCC MCS51 general pointer is 24-bits, but <code>uint32_t</code> is 32-bits (of course).
</p>
We always have <code>sizeof(pointer) <= sizeof(uintptr_t)</code> by definition.
</p>
<ul><pre>
union wdparm_u
{
FAR void *pvarg; /* The size one generic point */
uint32_t dwarg; /* Big enough for a 32-bit value in any case */
NuttX provides <i>work queues</i>. Work queues are threads that service a queue of work items to be performed. They are useful for off-loading work to a different threading context, for delayed processing, or for serializing activities.
There are three different classes of work queues, each with different properties and intended usage. These class of work queues along with the common work queue interface are described in the following paragraphs.
<h4><aname="hpwork">4.4.1.1 High Priority Kernel Work queue</a></h4>
<p><b>High Priority Kernel Work queue</b>.
The dedicated high-priority work queue is intended to handle delayed processing from interrupt handlers. This work queue is required for some drivers but, if there are no complaints, can be safely disabled. The high priority worker thread also performs garbage collection -- completing any delayed memory deallocations from interrupt handlers. If the high-priority worker thread is disabled, then that clean up will be performed either by (1) the low-priority worker thread, if enabled, and if not (2) the IDLE thread instead (which runs at the lowest of priority and may not be appropriate if memory reclamation is of high priority)
The high-priority worker thread is intended to serve as the <i>bottom half</i> for device drivers. As a consequence it must run at a very high, fixed priority rivalling the priority of the interrupt handler itself. Typically, the high priority work queue should be the highest priority thread in your system (the default priority is 224).
</p>
<p>
<b>Thread Pool</b>.
The work queues can be configured to support multiple, low-priority threads. This is essentially a <i>thread pool</i> that provides multi-threaded servicing of the queue work. This breaks the strict serialization of the "queue" (and hence, the work queue is no longer a queue at all).
</p>
<p>
Multiple worker threads are required to support, for example, I/O operations that stall waiting for input. If there is only a single thread, then the entire work queue processing would stall in such cases.
Such behavior is necessary to support asynchronous I/O, AIO, for example.
For less critical, lower priority, application oriented worker thread support, consider enabling the lower priority work queue. The lower priority work queue runs at a lower priority, of course, but has the added advantage that it supports <i>priority inheritance</i> (if <code>CONFIG_PRIORITY_INHERITANCE=y</code> is also selected): The priority of the lower priority worker thread can then be adjusted to match the highest priority client.
The execution priority of the high-priority worker thread. Default: 224
</li>
<li><code>CONFIG_SCHED_HPWORKSTACKSIZE</code>.
The stack size allocated for the worker thread in bytes. Default: 2048.
</li>
</ul>
<p>
<b>Common Configuration Options</b>.
These options apply to all work queues:
</p>
<ul>
<li><code>CONFIG_SIG_SIGWORK</code>
The signal number that will be used to wake-up the worker thread. This same signal is used with the Default: 17
</li>
</ul>
<h4><aname="lpwork">4.4.1.2 Low Priority Kernel Work Queue</a></h4>
<p>
<b>Low Priority Kernel Work Queue</b>.
This lower priority work queue is better suited for more extended, application oriented processing such as file system clean-up, memory garbage collection and asynchronous I/O operations.
The lower priority work queue runs at a lower priority than the high priority work queue, of course, and so is inappropriate to serve as a driver <i>bottom half</i>.
It is, otherwise, very similar to the high priority work queue and most of the discussion above for the high priority work queue applies equally here.
The lower priority work queue does have one important, however, that make it better suited for some tasks:
</p>
<p><b>Priority Inheritance</b>.
The lower priority worker thread(s) support <i>priority inheritance</i> (if <config> CONFIG_PRIORITY_INHERITANCE</code> is also selected): The priority of the lower priority worker thread can then be adjusted to match the highest priority client.
</p>
<blockquote>
<b>NOTE:</b> This priority inheritance feature is not automatic. The lower priority worker thread will always a fixed priority unless additional logic implements that calls <code>lpwork_boostpriority()</code> to raise the priority of the lower priority worker thread (typically called before scheduling the work) and then calls the matching <code>lpwork_restorepriority()</code> when the work is completed (typically called within the work handler at the completion of the work). Currently, only the NuttX asynchronous I/O logic uses this dynamic prioritization feature.
</blockquote>
<p>
The higher priority worker thread, on the other hand, is intended to serve as the <i>bottom half</i> for device drivers. As a consequence must run at a very high, fixed priority. Typically, it should be the highest priority thread in your system.
The minimum execution priority of the lower priority worker thread. The priority of the all worker threads start at this priority. If priority inheritance is in effect, the priority may be boosted from this level. Default: 50.
The maximum execution priority of the lower priority worker thread. Lower priority worker threads will be started at <code>CONFIG_SCHED_LPWORKPRIORITY</code> but their priority may be boosted due to priority inheritance. The boosted priority of the low priority worker thread will not, however, ever exceed <code>CONFIG_SCHED_LPWORKPRIOMAX</code>. This limit would be necessary, for example, if the higher priority worker thread were to defer work to the lower priority thread. Clearly, in such a case, you would want to limit the maximum priority of the lower priority work thread. Default: 176.
The stack size allocated for the lower priority worker thread. Default: 2048.
</li>
</ul>
<h4><aname="usrwork">4.4.1.3 User-Mode Work Queue</a></h4>
<p>
<b>Work Queue Accessibility</b>.
The high- and low-priority worker threads are kernel-mode threads. In the normal, <i>flat</i> NuttX build, these work queues are are useful to application code and may be shared. However, in the NuttX protected and kernel build modes, kernel mode code is isolated and cannot be accessed from user-mode code.
</p>
<p>
<b>User-Mode Work Queue</b>.
if either <code>CONFIG_BUILD_PROTECTED</code> or <code>CONFIG_BUILD_KERNEL</code> are selected, then the option to enable a special user-mode work queue is enable. The interface to the user-mode work queue is identical to the interface to the kernel-mode work queues and the user-mode work queue is functionally equivalent to the high priority work queue. It differs in that its implementation does not depend on internal, kernel-space facilities.
</p>
<p>
<b>Configuration Options</b>.
</p>
<ul>
<li><code>CONFIG_LIB_USRWORK</code>.
If CONFIG_LIB_USRWORK is also defined then the user-mode work queue will be enabled.
<li><code>CONFIG_LIB_USRWORKPRIORITY</code>.
The execution priority of the user-mode priority worker thread. Default: 100
<li><code>CONFIG_LIB_USRWORKSTACKSIZE</code>.
The stack size allocated for the lower priority worker thread. Default: 2048.
</ul>
<h3><aname="cmnwqifs">4.4.2 Common Work Queue Interfaces</a></h3>
<h4><aname="wqids">4.4.2.1 Work Queue IDs</a></h4>
<p>
<b>Work queue IDs</b>.
All work queues use the identical interface functions (at least identical in terms of the function <i>signature</i>). The first parameter passed to the work queue interface function identifies the work queue:
</p>
<p>
<b>Kernel-Mode Work Queue IDs:</b>
<p>
<ul>
<li>
<code>HPWORK</code>.
This ID of the high priority work queue that should only be used for hi-priority, time-critical, driver bottom-half functions.
</li>
<li>
<code>LPWORK</code>.
This is the ID of the low priority work queue that can be used for any purpose. if <code>CONFIG_SCHED_LPWORK</code> is not defined, then there is only one kernel work queue and <code>LPWORK</code> is equal to <code>HPWORK</code>.
</li>
</ul>
<p>
<b>User-Mode Work Queue IDs:</b>
<p>
<ul>
<li>
<code>USRWORK</code>.
This is the ID of the user-mode work queue that can be used for any purpose by applications. In a flat build, <code>LPWORK</code> is equal to <code>LPWORK</code> so that user applications will use the lower priority work queue (if there is one).
</li>
</ul>
<h4><aname="wqiftypes">4.4.2.2 Work Queue Interface Types</a></h4>
Defines one entry in the work queue. This is a client-allocated structure. Work queue clients should not reference any field in this structure since they are subject to change. The user only needs this structure in order to declare instances of the work structure. Handling of all fields is performed by the work queue interfaces described below.
The work structure is allocated and must be initialized to all zero by the caller.
Otherwise, the work structure is completely managed by the work queue logic.
The caller should never modify the contents of the work queue structure directly.
If <code>work_queue()</code> is called before the previous work as been performed and removed from the queue, then any pending work will be canceled and lost.
int work_cancel(int qid, FAR struct work_s *work);
<ul><pre>
</pre></ul>
</p>
<p>
<b>Description</b>.
Cancel previously queued work. This removes work from the work queue. After work has been cancelled, it may be re-queue by calling <code>work_queue()</code> again.
</p>
<p>
<b>Input Parameters</b>:
</p>
<ul>
<li>
<p>
<code>qid</code>:
The work queue ID.
</p>
</li>
<li>
<p>
<code>work</code>:
The previously queue work structure to cancel.
</p>
</li>
</ul>
<p>
<b>Returned Value</b>:
</p>
<ul>
<p>
Zero is returned on success; a negated <code>errno</code> is returned on failure.
</p>
<ul>
<li><code>ENOENT</code>: There is no such work queued.</li>
<li><code>EINVAL</code>: An invalid work queue was specified.</li>
Signal the worker thread to process the work queue now. This function is used internally by the work logic but could also be used by the user to force an immediate re-assessment of pending work.
</p>
<p>
<b>Input Parameters</b>:
</p>
<ul>
<li>
<p>
<code>qid</code>:
The work queue ID.
</p>
</ul>
<p>
<b>Returned Value</b>:
</p>
<ul>
<p>
Zero is returned on success; a negated <code>errno</code> is returned on failure.
The function is only available as a user interface in the kernel-mode build. In the flat build, there is no user-mode work queue; in the protected mode, the user-mode work queue will automatically be started by the OS start-up code. But in the kernel mode, each user process will be required to start is own, private instance of the user-mode work thread using this interface.
</p>
<p>
<b>Input Parameters</b>: None
</p>
<p>
<b>Returned Value</b>:
</p>
<ul>
<p>
The task ID of the worker thread is returned on success. A negated <code>errno</code> value is returned on failure.
Called by the work queue client to assure that the priority of the low-priority worker thread is at least at the requested level, <code>reqprio</code>. This function would normally be called just before calling <code>work_queue()</code>.
This function is called to restore the priority after it was previously boosted. This is often done by client logic on the worker thread when the scheduled work completes. It will check if we need to drop the priority of the worker thread.
</p>
<p>
<b>Input Parameters</b>:
</p>
<ul>
<li>
<p>
<code>reqprio</code>:
Previously requested minimum worker thread priority to be "unboosted".
The configuration indicates the CPUs ability to support address environments by setting the configuration variable <code>CONFIG_ARCH_HAVE_ADDRENV=y</code>.
That will enable the selection of the actual address environment support which is indicated by the selection of the configuration variable <code>CONFIG_ARCH_ADDRENV=y</code>.
These address environments are created only when tasks are created via <code>exec()</code> or <code>exec_module()</code> (see <code>include/nuttx/binfmt/binfmt.h</code>).
When <code>CONFIG_ARCH_ADDRENV=y</code> is set in the board configuration, the CPU-specific logic must provide a set of interfaces as defined in the header file <code>include/nuttx/arch.h</code>.
These interfaces all operate on type <code>group_addrenv_t</code> which is an abstract representation of a task group's address environment and the type must be defined in<code>arch/arch.h</code> if <code>CONFIG_ARCH_ADDRENV</code> is defined. These low-level interfaces include:
These interfaces are used by the functions in <code>sched/</code> and all operate on the task group which as been assigned an address environment by <code>up_addrenv_clone()</code>.
Because the caller's stack must be preserved in its own address space when we instantiate the environment of the new process in order to initialize it.
</p>
<p>
<b>NOTE:</b> The naming of the <code>CONFIG_ARCH_STACK_DYNAMIC</code> selection implies that dynamic stack allocation is supported.
Certainly this option must be set if dynamic stack allocation is supported by a platform.
But the more general meaning of this configuration environment is simply that the stack has its own address space.
</p>
<p>
If <code>CONFIG_ARCH_STACK_DYNAMIC=y</code> is selected then the platform specific code must export these additional interfaces:
If <code>CONFIG_ARCH_KERNEL_STACK</code> is selected, then each user process will have two stacks: (1) a large (and possibly dynamic) user stack and (2) a smaller kernel stack. However, this option is <i>required</i> if both <code>CONFIG_BUILD_KERNEL</code> and <code>CONFIG_LIBC_EXECFUNCS</code> are selected. Why? Because when we instantiate and initialize the address environment of the new user process, we will temporarily lose the address environment of the old user process, including its stack contents. The kernel C logic will crash immediately with no valid stack in place.
</p>
<p>
If <code>CONFIG_ARCH_KERNEL_STACK=y</code> is selected then the platform specific code must export these additional interfaces:
<li><code>textsize</code>: The size (in bytes) of the <code>.text</code> address environment needed by the task. This region may be read/execute only.</li>
<li><code>datasize</code>: The size (in bytes) of the <code>.bss/.data</code> address environment needed by the task. This region may be read/write only.</li>
This function is called when a final thread leaves the task group and the task group is destroyed. This function then destroys the defunct address environment, releasing the underlying physical memory allocated by <code>up_addrenv_create()</code>.
<li><code>textsize</code>: For some implementations, the text and data will be saved in the same memory region (read/write/execute) and, in this case, the virtual address of the data just lies at this offset into the common region.</li>
<li><code>vdata</code>: The location to return the virtual address.</li>
After an address environment has been established for a task (via <code>up_addrenv_create())</code>, this function may be called to to instantiate that address environment in the virtual address space.
This might be necessary, for example, to load the code for the task from a file or to access address environment private data.
The address environment that was in place before <code>up_addrenv_select()</code> was called.
This may be used with <code>up_addrenv_restore()</code> to restore the original address environment that was in place before <code>up_addrenv_select()</code> was called.
Duplicate an address environment. This does not copy the underlying memory, only the representation that can be used to instantiate that memory as an address environment.
This function is called when a task or thread exits in order to release its reference to an address environment. The address environment, however, should persist until up_addrenv_destroy() is called when the task group is itself destroyed. Any resources unique to this thread may be destroyed now.
Description: if <code>CONFIG_SCHED_TICKLESS</code> is defined, then this function is provided by the RTOS base code and called from platform-specific code when the interval timer used to implemented the tick-less OS expires.
<ul>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
None
</ul>
<p><b>Returned Value</b>:</p>
<ul>
None
</ul>
<p><b>Assumptions</b>:</p>
<ul>
Base code implementation assumes that this function is called from interrupt handling logic with interrupts disabled.
Description: if <code>CONFIG_SCHED_TICKLESS</code> is defined, then this function is provided by the RTOS base code and called from platform-specific code when the interval timer used to implemented the tick-less OS expires.
<ul>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
None
</ul>
<p><b>Returned Value</b>:</p>
<ul>
None
</ul>
<p><b>Assumptions</b>:</p>
<ul>
Base code implementation assumes that this function is called from interrupt handling logic with interrupts disabled.
<p><b>Function Prototype</b>: <code>void irq_dispatch(int irq, FAR void *context);</code></p>
<p><b>Description</b>.
This function must be called from the architecture-
specific logic in order to display an interrupt to
the appropriate, registered handling logic.
</p>
<h2><aname="internalOS">4.7 Application OS vs. Internal OS Interfaces</a></h2>
<p>
NuttX provides a standard, portable OS interface for use by applications.
This standard interface is controlled by the specifications proved at <ahref="http://opengroup.org">OpenGroup.org</a>.
These application interfaces, in general, should not be used directly by logic executing within the OS. The reason for this is that there are certain properties of the standard application interfaces that make them unsuitable for use within the OS
These properties include:
</p>
<ol>
<li>
<p>
<b>Use of the <i>per-thread</i><code>errno</code> variable</b>:
Handling of return values, particularly, in the case of returned error indications.
Most legacy POSIX OS interface return information via a <i>per-thread</i><code>errno</code>.
There must be no alteration of the <code>errno</code> value that must be stable from the point of view of the application.
So, as a general rule, internal OS logic must never modify the <code>errno</code> and particularly not by the inappropriate use of application OS interfaces within OS itself.
</p>
<p>
Within the OS, functions do not return error information via the <code>errno</code> variable. Instead, the majority of internal OS function return error information as an integer value: Returned values greater than or equal to zero are success values; returned values less than zero indicate failures.
Failures are reported by returning a negated <code>errno</code> value from <code>include/errno.h</code>,
</p>
<li>
<p><b>Cancellation Points</b>:
Many of the application OS interfaces are <i>cancellation points</i>, i.e., when the task is operating in <i>defferred cancellation</i> state, it cannot be deleted or cancelled until it calls an application OS interface that is a cancellation point.
</p>
<p>
The POSIX specification is very specific about this, specific both in identifying which application OS interfaces are cancellation points and specific in the fact that it is prohibited for any OS operation other than those listed in the specification to generate cancellation points.
If internal OS logic were to re-use application OS interfaces directly then it could very easily violate this POSIX requirement by incorrectly generating cancellation points on inappropriate OS operations and could result in very difficult to analyze application failures.
</p>
</li>
<li>
<p><b>Use of <i>per-task</i> Resources</b>:
Many resources are only valid in the task group context in which a thread operates. Above we mentioned one: <code>errno</code> is only valid for the thread that is currently executing. So, for example, the <code>errno</code> at the time of a call is a completely different variable than, say, the <code>errno</code> while running in a work queue task.
</p>
<p>
File descriptors are an even better example: An open file on file descriptor 5 on task A is <i>not</i> the same open file as might be used on file descriptor 5 on task B.
</p>
<p>
As a result, internal OS logic may not use application OS interfaces that use file descriptors or any other <i>per-task</i> resource.
</p>
</li>
</ol>
<p>
Within NuttX, this is handled by supporting equivalent internal OS interfaces that do not break the above rules.
These internal interfaces are intended for use <i>only</i> within the OS and should not be used by application logic.
Some examples include:
</p>
<ul>
<li>
<p>
<b><code>nxsem_wait()</code></b>:
<code>nxsem_wait()</code> is functionally equivalent to the standard application interface <code>sem_wait()</code>. However, <code>nxsem_wait()</code> will not modify the errno value and will not cause a cancellation point.
(see <code>include/nuttx/semaphore.h</code> for other internal OS interfaces for semaphores).
</p>
</li>
<li>
<p>
<b><code>nxsig_waitinfo()</code></b>:
<code>nxsig_waitinfo()</code> is functionally equivalent to the standard application interface <code>sigwaitinfo()</code>. However, <code>nxsig_waitinfo()</code> will not modify the errno value and will not cause a cancellation point (see <code>include/nuttx/signal.h</code> for other internal OS interfaces for signals).
</p>
</li>
<li>
<p>
<b><code>nxmq_send()</code></b>:
<code>nxmq_send()</code> is functionally equivalent to the standard application interface <code>mq_send()</code>. However, <code>nxmq_send()</code> will not modify the errno value and will not cause a cancellation point (see <code>include/nuttx/mqueue.h</code> for other internal OS interfaces for POSIX message queues).
</p>
</li>
<li>
<p>
<b><code>file_read()</code></b>:
<code>file_read()</code> is functionally equivalent to the standard application interface <code>read()</code>. However, <code>file_read()</code> will not modify the errno value, will not cause a cancellation point, and uses a special internal data structure in place of the file descriptor (see <code>include/nuttx/fs/fs.h</code> for other internal OS interfaces for VFS functions).
<code>psock_recvfrom()</code> is functionally equivalent to the standard application interface <code>recvfrom()</code>. However, <code>psock_recvfrom()</code> will not modify the errno value, will not cause a cancellation point, and uses a special internal data structure in place of the socket descriptor (see <code>include/nuttx/net/net.h</code> for other internal OS interfaces for sockets).
In a small embedded system, there will typically be a much greater interaction between application and low-level board features.
The canonically correct to implement such interactions is by implementing a character driver and performing the interactions via low level <code>ioctl()</code> calls.
This, however, may not be practical in many cases and will lead to "correct" but awkward implementations.
</p>
<p>
<code>boardctl()</code> is <i>non-standard</i> OS interface to alleviate the problem.
It basically circumvents the normal device driver <code>ioctl()</code> interlace and allows the application to perform direct IOCTL-like calls to the board-specific logic.
According to Wikipedia: "Symmetric multiprocessing (SMP) involves a symmetric multiprocessor system hardware and software architecture where two or more identical processors connect to a single, shared main memory, have full access to all I/O devices, and are controlled by a single operating system instance that treats all processors equally, reserving none for special purposes. Most multiprocessor systems today use an SMP architecture. In the case of multi-core processors, the SMP architecture applies to the cores, treating them as separate processors.
"SMP systems are tightly coupled multiprocessor systems with a pool of homogeneous processors running independently, each processor executing different programs and working on different data and with capability of sharing common resources (memory, I/O device, interrupt system and so on) and connected using a system bus or a crossbar."
</p>
<p>
For a technical description of the NuttX implementation of SMP, see the NuttX <ahref="http://www.nuttx.org/doku.php?id=wiki:nxinternal:smp">SMP Wiki Page</a>.
The value of previous value of the spinlock variable is returned, either <code>SP_LOCKED</code> if the spinlock was previously locked (meaning that the test-and-set operation failed to obtain the lock) or <code>SP_UNLOCKED</code> if the spinlock was previously unlocked (meaning that we successfully obtained the lock)
System initialization occurs on that single thread.
At the completion of the initialization of the OS, just before beginning normal multitasking, the additional CPUs would be started by calling this function.
</p>
<p>
Each CPU is provided the entry point to is IDLE task when started.
A TCB for each CPU's IDLE task has been initialized and placed in the CPU's <code>g_assignedtasks[cpu]</code> list.
A stack has also been allocated and initialized.
</p>
<p>
The OS initialization logic calls this function repeatedly until each CPU has been started, 1 through <code>(CONFIG_SMP_NCPUS-1)</code>.
</p>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
<li>
<code>cpu</code>: The index of the CPU being started.
This will be a numeric value in the range of from one to <code>(CONFIG_SMP_NCPUS-1)</code>).
(CPU 0 is already active).
</li>
</ul>
<p><b>Returned Value</b>:</p>
<ul>
<p>
Zero (<code>OK</code>) is returned on success; a negated <code>errno</code> value on failure.
Save the state of the current task at the head of the <code>g_assignedtasks[cpu]</code> task list and then pause task execution on the CPU.
</p>
<p>
This function is called by the OS when the logic executing on one CPU needs to modify the state of the <code>g_assignedtasks[cpu]</code> list for another CPU.
</p>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
<li>
<code>cpu</code>: The index of the CPU to be paused.
This will not be the index of the currently executing CPU.
</li>
</ul>
<p><b>Returned Value</b>:</p>
<ul>
<p>
Zero (<code>OK</code>) is returned on success; a negated <code>errno</code> value on failure.
Restart the cpu after it was paused via <code>up_cpu_pause()</code>, restoring the state of the task at the head of the <code>g_assignedtasks[cpu]</code> list, and resume normal tasking.
</p>
<p>
This function is called after <code>up_cpu_pause()</code> in order resume operation of the CPU after modifying its <code>g_assignedtasks[cpu]</code> list.
</p>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
<li>
<code>cpu</code>: The index of the CPU being resumed.
This will not be the index of the currently executing CPU.
</li>
</ul>
<p><b>Returned Value</b>:</p>
<ul>
<p>
Zero (<code>OK</code>) is returned on success; a negated <code>errno</code> value on failure.
Shared memory interfaces are only available with the NuttX kernel build (<code>CONFIG_BUILD_KERNEL=y</code>).
These interfaces support user memory regions that can be shared between multiple user processes.
The user interfaces are provided in the standard header file <code>include/sys/shm.h></code>.
All logic to support shared memory is implemented within the NuttX kernel with the exception of two low-level functions that are require to configure the platform-specific MMU resources.
The NuttX logic does not refer to specific LEDS, rather, it refers to an event to be shown on the LEDS
in whatever manner is appropriate for the board;
the way that this event is presented depends upon the hardware available on the board.
</p>
<p>
The model used by NuttX is that the board can show 8 events defined as follows in <code><i><board-name></i>/include/board.h</code>:
</p>
<ul><pre>
#define LED_STARTED ??
#define LED_HEAPALLOCATE ??
#define LED_IRQSENABLED ??
#define LED_STACKCREATED ??
#define LED_INIRQ ??
#define LED_SIGNAL ??
#define LED_ASSERTION ??
#define LED_PANIC ??
</pre></ul>
<p>
The specific value assigned to each pre-processor variable can be whatever makes the implementation easiest for the board logic.
The <i>meaning</i> associated with each definition is as follows:
</p>
<ul>
<li>
<code>LED_STARTED</code> is the value that describes the setting of the LEDs when the LED logic is first initialized.
This LED value is set but never cleared.
</li>
<li>
<code>LED_HEAPALLOCATE</code> indicates that the NuttX heap has been configured.
This is an important place in the boot sequence because if the memory is configured wrong, it will probably crash leaving this LED setting.
This LED value is set but never cleared.
</li>
<li>
<code>LED_IRQSENABLED</code> indicates that interrupts have been enabled.
Again, during bring-up (or if there are hardware problems), it is very likely that the system may crash just when interrupts are enabled, leaving this setting on the LEDs.
This LED value is set but never cleared.
</li>
<li>
<code>LED_STACKCREATED</code> is set each time a new stack is created.
If set, it means that the system attempted to start at least one new thread.
This LED value is set but never cleared.
</li>
<li>
<code>LED_INIRQ</code> is set and cleared on entry and exit from each interrupt.
If interrupts are working okay, this LED will have a dull glow.
</li>
<li>
<code>LED_SIGNAL</code> is set and cleared on entry and exit from a signal handler.
Signal handlers are tricky so this is especially useful during bring-up or a new architecture.
</li>
<li>
<code>LED_ASSERTION</code> is set if an assertion occurs.
</li>
<li>
<code>LED_PANIC</code> will blink at around 1Hz if the system panics and hangs.
New implementations should not use the naming convention for common board interfaces, but should instead use the naming conventions for microprocessor-specific interfaces or the board-specific interfaces (such as <code>stm32_led_initialize()</code>).
This logic was originally added to support network I/O buffering, but has been generalized to meet buffering requirements by all device drivers.
At the time of this writing, IOBs are currently used not only be networking but also by logic in <code>drivers/syslog</code> and <code>drivers/wireless</code>.
Provide common I/O buffer management logic for all drivers,
</li>
<li>
Support I/O buffer allocation from both the tasking and interrupt level contexts.
</li>
<li>
Use a fixed amount of pre-allocated memory.
</li>
<li>
No costly, non-deterministic dynamic memory allocation.
</li>
<li>
When the fixed number of pre-allocated I/O buffers is exhausted, further attempts to allocate memory from tasking logic will cause the task to block and wait until a an I/O buffer to be freed.
</li>
<li>
Each I/O buffer should be small, but can be chained together to support buffering of larger thinks such as full size network packets.
</li>
<li>
Support <i>throttling</i> logic to prevent lower priority tasks from hogging all available I/O buffering.
<dd>Enables generic I/O buffer support. This setting will build the common I/O buffer (IOB) support library.
<dt><code>CONFIG_IOB_NBUFFERS</code>
<dd>Number of pre-allocated I/O buffers. Each packet is represented by a series of small I/O buffers in a chain. This setting determines the number of preallocated I/O buffers available for packet data.
The default value is setup for network support. The default is 8 buffers if neither TCP read-ahead or TCP write buffering is enabled (neither <code>CONFIG_NET_TCP_WRITE_BUFFERS</code> nor <code>CONFIG_NET_TCP_READAHEAD</code>), 24 if only write buffering is enabled, and 36 if both read-ahead and write buffering are enabled.
<dt><code>CONFIG_IOB_BUFSIZE</code>
<dd>Payload size of one I/O buffer. Each packet is represented by a series of small I/O buffers in a chain. This setting determines the data payload each preallocated I/O buffer. The default value is 196 bytes.
<dt><code>CONFIG_IOB_NCHAINS</code>
<dd>Number of pre-allocated I/O buffer chain heads. These tiny nodes are used as <i>containers</i> to support queueing of I/O buffer chains. This will limit the number of I/O transactions that can be <i>in-flight</i> at any give time. The default value of zero disables this features.
<dd>These generic I/O buffer chain containers are not currently used by any logic in NuttX. That is because their other other specialized I/O buffer chain containers that also carry a payload of usage specific information.
The default value is zero if nether TCP nor UDP read-ahead buffering is enabled (i.e., neither <code>CONFIG_NET_TCP_READAHEAD</code>&& !<code>CONFIG_NET_UDP_READAHEAD</code> or eight if either is enabled.
<dt><code>CONFIG_IOB_THROTTLE</code>
<dd>I/O buffer throttle value. TCP write buffering and read-ahead buffer use the same pool of free I/O buffers. In order to prevent uncontrolled incoming TCP packets from hogging all of the available, pre-allocated I/O buffers, a throttling value is required. This throttle value assures that I/O buffers will be denied to the read-ahead logic before TCP writes are halted.
<dd>Force I/O buffer debug. This option will force debug output from I/O buffer logic. This is not normally something that would want to do but is convenient if you are debugging the I/O buffer logic and do not want to get overloaded with other un-related debug output.
NOTE that this selection is not available if DEBUG features are not enabled (<code>CONFIG_DEBUG_FEATURES</code>) with IOBs are being used to syslog buffering logic (<code>CONFIG_SYSLOG_BUFFER</code>).
An allocation throttle was added. I/O buffer allocation logic supports a throttle value originally for read-ahead buffering to prevent the read-ahead logic from consuming all available I/O buffers and blocking the write buffering logic. This throttle logic is only needed for networking only if both write buffering and read-ahead buffering are used. Of use of I/O buffering might have other motivations for throttling.
This structure represents one I/O buffer. A packet is contained by one or more I/O buffers in a chain. The <code>io_pktlen</code> is only valid for the I/O buffer at the head of the chain.
FAR struct iob_s *iob_peek_queue(FAR struct iob_queue_s *iobq);
#endif
</pre>
<p><b>Description</b>.
Return a reference to the I/O buffer chain at the head of a queue. This is similar to iob_remove_queue except that the I/O buffer chain is in place at the head of the queue. The I/O buffer chain may safely be modified by the caller but must be removed from the queue before it can be freed.
</p>
<p><b>Returned Value</b>.
Returns a reference to the I/O buffer chain at the head of the queue.
int iob_trycopyin(FAR struct iob_s *iob, FAR const uint8_t *src,
unsigned int len, unsigned int offset, bool throttled);
</pre>
<p><b>Description</b>.
Copy data <code>len</code> bytes from a user buffer into the I/O buffer chain, starting at <code>offset</code>, extending the chain as necessary BUT without waiting if buffers are not available.
int iob_copyout(FAR uint8_t *dest, FAR const struct iob_s *iob,
unsigned int len, unsigned int offset);
</pre>
<p><b>Description</b>.
Copy data <code>len</code> bytes of data into the user buffer starting at <code>offset</code> in the I/O buffer, returning that actual number of bytes copied out.
FAR struct iob_s *iob_trimtail(FAR struct iob_s *iob, unsigned int trimlen);
</pre>
<p><b>Description</b>.
Remove bytes from the end of an I/O chain. Emptied I/O buffers are freed NULL will be returned in the special case where the entry I/O buffer chain is freed.
FAR struct iob_s *iob_pack(FAR struct iob_s *iob);
</pre>
<p><b>Description</b>.
Pack all data in the I/O buffer chain so that the data offset is zero and all but the final buffer in the chain are filled. Any emptied buffers at the end of the chain are freed.
Device-specific <code>ioctl()</code> commands used to performed specialized operations on the device.
These <code>ioctl()</code> will be documented in header files under <code>include/nuttx</code> that detail the specific device interface.
</li>
<li>
Specialized I/O formats.
Some devices will require that <code>read()</code> and/or <code>write()</code> operations use data conforming to a specific format, rather than a plain stream of bytes.
These specialized I/O formats will be documented in header files under <code>include/nuttx</code> that detail the specific device interface.
The typical representation of the I/O format will be a C structure definition.
The header files for NuttX touchscreen drivers reside in the <code>include/nuttx/include/input</code> directory.
The interface between the touchscreen controller "upper half" and "lower half" drivers are <i>not</i> common, but vary from controller-to-controller.
Because of this, each touchscreen driver has its own unique header file that describes the "upper half"/"lower half" interface in that directory.
The application level interface to each touchscreen driver, on the other hand, <i>is</i> the same for each touchscreen driver and is described <code>include/nuttx/include/input/touchscreen.h</code>.
The touchscreen driver uses a standard character driver framework but read operations return specially formatted data.
</li>
<li><b>"Upper Half" Driver</b>.
The controller-specific, "upper half" touchscreen drivers reside in the directory <code>drivers/input</code>.
</li>
<li><b>"Lower Half" Drivers</b>.
Platform-specific touchscreen drivers reside in either: (1) The <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the processor architectures that have build in touchscreen controllers or (2) the <code>configs/</code><i><board></i><code>/src/</code> directory for boards that use an external touchscreen controller chip.
</li>
</ul>
<h3><aname="analogdrivers">6.1.2 Analog (ADC/DAC) Drivers</a></h3>
<p>
The NuttX analog drivers are split into two parts:
General header files for the NuttX analog drivers reside in <code>include/nuttx/analog/</code>.
These header files includes both the application level interface to the analog driver as well as the interface between the "upper half" and "lower half" drivers.
Common analog logic and share-able analog drivers reside in the <code>drivers/analog/</code>.
</li>
<li>
Platform-specific drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> analog peripheral devices.
This header file includes both the application level interface to the PWM driver as well as the interface between the "upper half" and "lower half" drivers.
The PWM module uses a standard character driver framework.
However, since the PWM driver is a devices control interface and not a data transfer interface,
the majority of the functionality available to the application is implemented in driver ioctl calls.
Platform-specific PWM drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> PWM peripheral devices.
This header file includes both the application level interface to the CAN driver as well as the interface between the "upper half" and "lower half" drivers.
The CAN module uses a standard character driver framework.
</li>
<li><b>"Upper Half" Driver</b>.
The generic, "upper half" CAN driver resides at <code>drivers/can.c</code>.
</li>
<li><b>"Lower Half" Drivers</b>.
Platform-specific CAN drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> CAN peripheral devices.
When reading from the CAN driver multiple messages may be returned, depending on (1) the size the returned can messages, and (2) the size of the buffer provided to receive CAN messages.
<i>Never assume that a single message will be returned</i>... if you do this, <i>you will lose CAN data</i> under conditions where your read buffer can hold more than one small message.
Below is an example about how you should think of the CAN read operation:
The header file for the NuttX Quadrature Encoder driver reside at <code>include/nuttx/sensors/qencoder.h</code>.
This header file includes both the application level interface to the Quadrature Encoder driver as well as the interface between the "upper half" and "lower half" drivers.
The Quadrature Encoder module uses a standard character driver framework.
</li>
<li><b>"Upper Half" Driver</b>.
The generic, "upper half" Quadrature Encoder driver resides at <code>drivers/sensors/qencoder.c</code>.
</li>
<li><b>"Lower Half" Drivers</b>.
Platform-specific Quadrature Encoder drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> Quadrature Encoder peripheral devices.
This header file includes both the application level interface to the timer driver as well as the interface between the "upper half" and "lower half" drivers.
The timer driver uses a standard character driver framework.
The generic, "upper half" timer driver resides at <code>drivers/timers/timer.c</code>.
</li>
<li><b>"Lower Half" Drivers</b>.
Platform-specific timer drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> timer peripheral devices.
This header file includes both the application level interface to the RTC driver as well as the interface between the "upper half" and "lower half" drivers.
The RTC driver uses a standard character driver framework.
The generic, "upper half" RTC driver resides at <code>drivers/timers/rtc.c</code>.
</li>
<li><b>"Lower Half" Drivers</b>.
Platform-specific RTC drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> RTC peripheral devices.
This header file includes both the application level interface to the watchdog timer driver as well as the interface between the "upper half" and "lower half" drivers.
The watchdog timer driver uses a standard character driver framework.
</li>
<li><b>"Upper Half" Driver</b>.
The generic, "upper half" watchdog timer driver resides at <code>drivers/timers/watchdog.c</code>.
Platform-specific watchdog timer drivers reside in <code>arch/</code><i><architecture></i><code>/src/</code><i><chip></i> directory for the specific processor <i><architecture></i> and for the specific <i><chip></i> watchdog timer peripheral devices.
Keyboards and keypads are really the same devices for NuttX.
A keypad is thought of as simply a keyboard with fewer keys.
</p>
<p>
<b>Special Commands</b>.
In NuttX, a keyboard/keypad driver is simply a character driver that may have an (optional) encoding/decoding layer on the data returned by the character driver.
A keyboard may return simple text data (alphabetic, numeric, and punctuation) or control characters (enter, control-C, etc.) when a key is pressed.
We can think about this the "normal" keyboard data stream.
However, in addition, most keyboards support actions that cannot be represented as text or control data.
Such actions include things like cursor controls (home, up arrow, page down, etc.), editing functions (insert, delete, etc.), volume controls, (mute, volume up, etc.) and other special functions.
In this case, some special encoding may be required to multiplex the normal text data and special command key press data streams.
</p>
<p>
<b>Key Press and Release Events</b>
Sometimes the time that a key is released is needed by applications as well.
Thus, in addition to normal and special key press events, it may also be necessary to encode normal and special key release events.
</p>
<p>
<b>Encoding/Decoding</b> Layer</b>.
An optional encoding/decoding layer can be used with the basic character driver to encode the keyboard events into the text data stream.
The function interfaces that comprise that encoding/decoding layer are defined in the header file <code>include/nuttx/input/kbd_code.h</code>.
These functions provide an matched set of (a) driver encoding interfaces, and (b) application decoding interfaces.
All device drivers that are accessible to application logic are either: (1) Character device drivers that can be accessed via the standard driver operations (<code>open()</code>, <code>close()</code>, <code>read()</code>, <code>write()</code>, etc.), or (2) block drivers that can be accessing only as part of mounting a file system or other special use cases as described in the preceding paragraph.
</p>
<p>
In addition to this, there are also specialized "drivers" that can be used only within the OS logic itself and are not accessible to application logic. These specialized drivers are discussed in the following paragraphs.
<b><code>struct usbhost_driver_s</code> and <code>struct usbhost_connection_s</code></b>.
Each USB host controller driver must implement an instance of <code>struct usbhost_driver_s</code> and <code>struct usbhost_connection_s</code>:
<code>struct usbhost_driver_s</code> provides the interface between the USB host driver and the USB class driver;
<code>struct usbhost_connection_s</code> provides the interface between the USB host driver and platform-specific connection management and device enumeration logic.
These structures are defined in <code>include/nuttx/usb/usbhost.h</code>.
The NuttX USB host infrastructure includes a <i>registry</i>.
During its initialization, each USB host class driver must call the interface, <code>usbhost_registerclass()</code>
in order add its interface to the registry.
Later, when a USB device is connected, the USB host controller will look up the USB host class driver that is needed to support the connected device in this registry.
</p>
<p>
<b>Examples</b>:
<code>drivers/usbhost/usbhost_registry.c</code>, <code>drivers/usbhost/usbhost_registerclass.c</code>, and <code>drivers/usbhost/usbhost_findclass.c</code>,
USB host-side controller drivers are not normally directly accessed by user code,
but are usually bound to another, higher level USB host class driver.
The class driver exports the standard NuttX device interface so that the connected USB device can be accessed just as with other, similar, on-board devices.
For example, the USB host mass storage class driver (<code>drivers/usbhost/usbhost_storage.c</code>) will register a standard, NuttX block driver interface (like <code>/dev/sda</code>)
that can be used to mount a file system just as with any other other block driver instance.
In general, the binding sequence is:
</p>
<p>
<ol>
<li>
<p>
Each USB host class driver includes an initialization entry point that is called from the
application at initialization time.
This driver calls <code>usbhost_registerclass()</code> during this initialization in order to makes itself available in the event the device that it supports is connected.
Each application must include a <i>waiter</i> thread thread that (1) calls the USB host controller driver's <code>wait()</code> to detect the connection of a device, and then
(2) call the USB host controller driver's <code>enumerate</code> method to bind the registered USB host class driver to the USB host controller driver.
As part of its operation during the binding operation, the USB host class driver will register an instances of a standard NuttX driver under the <code>/dev</code> directory.
To repeat the above example, the USB host mass storage class driver (<code>drivers/usbhost/usbhost_storage.c</code>) will register a standard, NuttX block driver interface (like <code>/dev/sda</code>)
that can be used to mount a file system just as with any other other block driver instance.
</p>
<p>
<b>Examples</b>:
See the call to <code>register_blockdriver()</code> in the function <code>usbhost_initvolume()</code> in the file <code>drivers/usbhost/usbhost_storage.c</code>.
int syslog(int priority, FAR const IPTR char *format, ...);
int vsyslog(int priority, FAR const IPTR char *src, va_list ap);
</pre></ul>
<p><b>Description:</b>
<code>syslog()</code> generates a log message. The priority argument is formed by ORing the facility and the level values (see <code>include/syslog.h</code>). The remaining arguments are a format, as in <code>printf()</code> and any arguments to the format.
</p>
<p>
The NuttX implementation does not support any special formatting characters beyond those supported by <code>printf()</code>.
</p>
<p>
The function <code>vsyslog()</code> performs the same task as <code>syslog()</code> with the difference that it takes a set of arguments which have been obtained using the <code>stdarg</code> variable argument list macros.
The <code>setlogmask()</code> function sets the logmask and returns the previous mask. If the mask argument is zerio, the current logmask is not modified.
</p>
<p>
The SYSLOG priorities are: <code>LOG_EMERG</code>, <code>LOG_ALERT</code>, <code>LOG_CRIT</code>, <code>LOG_ERR</code>, <code>LOG_WARNING</code>, <code>LOG_NOTICE</code>, <code>LOG_INFO</code>, and <code>LOG_DEBUG</code>. The bit corresponding to a priority <code>p</code> is <code>LOG_MASK(p)</code>; <code>LOG_UPTO(p)</code> provides the mask of all priorities in the above list up to and including <code>p</code>.
</p>
<p>
Per <i>OpenGroup.org</i>"If the maskpri argument is 0, the current log mask is not modified." In this implementation, the value zero is permitted in order to disable all SYSLOG levels.
</p>
<p>
REVISIT: Per POSIX the SYSLOG mask should be a per-process value but in NuttX, the scope of the mask is dependent on the nature of the build:
</p>
<ul>
</li>
<li>
<p>
<i>Flat Build</i>: There is one, global SYSLOG mask that controls all output.
</li>
<li>
<p>
<i>Protected Build</i>: There are two SYSLOG masks. One within the kernel that controls only kernel output. And one in user-space that controls only user SYSLOG output.
<i>Kernel Build</i>: The kernel build is compliant with the POSIX requirement: There will be one mask for for each user process, controlling the SYSLOG output only form that process. There will be a separate mask accessible only in the kernel code to control kernel SYSLOG output.
The above are all standard interfaces as defined at <ahref="http://pubs.opengroup.org/onlinepubs/009695399/functions/closelog.html">OpenGroup.org</a>. Those interfaces are available for use by application software. The remaining interfaces discussed in this section are non-standard, OS-internal interfaces.
<code>include/debug.h</code> are also syslogging interfaces. Those macros are simply wrappers around <code>syslog()</code>. The debugging interfaces differ from the syslog interfaces in that:
</p>
<ul>
</li>
<li>
<p>
They do not take a priority parameter; the priority is inherent in the
debug macro name.
</li>
<li>
<p>
They decorate the output stream with information such as the file name
</li>
<li>
<p>
They can each be disabled via configuration options.
</li>
</ul>
<p>
Each debug macro has a base name that represents the priority and a prefix that represents the sub-system. Each macro is individually initialized by both priority and sub-system. For example, <code>uerr()</code> is the macro used for error level messages from the USB subsystem and is enabled with <code>CONFIG_DEBUG_USB_ERROR</code>.
</p>
<p>
The base debug macro names, their priority, and configuration variable are summarized below:
</p>
<ul>
</li>
<li>
<p>
<code>info()</code>. The <code>info()</code> macro is the lowest priority (<code>LOG_INFO</code>) and is intended to provide general information about the flow of program execution so that you can get an overview of the behavior of the program. <code>info()</code> is often very chatty and voluminous and usually more information than you may want to see. The <code>info()</code> macro is controlled via CONFIG_DEBUG_subsystem_INFO
</li>
<li>
<p>
<code>warn()</code>. The <code>warn()</code> macro has medium priority (<code>LOG_WARN</code>) and is controlled by <code>CONFIG_DEBUG_<i>subsystem</i>_WARN</code>. The <code>warn()</code> is intended to
errors or, perhaps, minor errors that easily recovered.
</li>
<li>
<p>
<code>err()</code>. This is a high priority debug macro (<code>LOG_ERROR</code>) and controlled by <code>CONFIG_DEBUG_<i>subsystem</i>_ERROR</code>. The <code>err()</code> is reserved to indicate important error conditions.
<code>alert()</code>. The highest priority debug macro (<code>LOG_EMERG</code>) and is controlled by <code>CONFIG_DEBUG_ALERT</code>. The <code>alert()</code> macro is reserved for use solely by assertion and crash handling logic. It also differs from the other macros in that it cannot be enabled or disabled per subsystem.
In the NuttX SYSLOG implementation, the underlying device logic the supports the SYSLOG output is referred to as a SYSLOG <i>channel</i>. Each SYSLOG channel is represented by an interface defined in <code>include/nuttx/syslog/syslog.h</code>:
</p>
<ul><pre>
/* This structure provides the interface to a SYSLOG device */
typedef CODE int (*syslog_putc_t)(int ch);
typedef CODE int (*syslog_flush_t)(void);
struct syslog_channel_s
{
/* I/O redirection methods */
syslog_putc_t sc_putc; /* Normal buffered output */
syslog_putc_t sc_force; /* Low-level output for interrupt handlers */
syslog_flush_t sc_flush; /* Flush buffered output (on crash) */
/* Implementation specific logic may follow */
};
</pre></ul>
<p>
The channel interface is instantiated by calling <code>syslog_channel()</code>:
int syslog_channel(FAR const struct syslog_channel_s *channel);
</pre></ul>
<p><b>Description:</b>
Configure the SYSLOG function to use the provided channel to generate SYSLOG output.
</p>
<p>
<code>syslog_channel()</code> is a non-standard, internal OS interface and is not available to applications. It may be called numerous times as necessary to change channel interfaces.
By default, all system log output goes to console (<code>/dev/console</code>).
The initial, default SYSLOG channel is established with statically initialized global variables so that some level of SYSLOG output may be available immediately upon reset. This initialized data is in the file <code>drivers/syslog/syslog_channel.c</code>. The initial SYSLOG capability is determined by the selected SYSLOG channel:
</p>
<ul>
<li>
<p>
<i>In-Memory Buffer (RAMLOG)</i>. Full SYSLOG capability as available at reset.
</li>
<li>
<p>
<i>Serial Console</i>. If the serial implementation provides the low-level character output function <code>up_putc()</code>, then that low level serial output is available as soon as the serial device has been configured.
</li>
<li>
<p>
For all other SYSLOG channels, all SYSLOG output goes to the bit-
bucket until the SYSLOG channel device has been initialized.
</li>
</ul>
<p>
The syslog channel device is initialized when the bring-up logic calls <code>syslog_initialize()</code>:
One power up, the SYSLOG facility is non-existent or limited to very low-level output. This function is called later in the initialization sequence after full driver support has been initialized. It installs the configured SYSLOG drivers and enables full SYSLOG capability.
</p>
<p>
This function performs these basic operations:
</p>
<ul>
<li>
<p>
Initialize the SYSLOG device
</li>
<li>
<p>
Call <code>syslog_channel()</code> to begin using that device.
</li>
<li>
<p>
If <code>CONFIG_ARCH_SYSLOG</code> is selected, then the architecture-specific logic will provide its own SYSLOG device initialize which must include as a minimum a call to <code>syslog_channel()</code> to use the device.
</li>
</ul>
<p><b>Input Parameters:</b>
<ul>
<li>
<p>
<code>phase</code>: One of {<code>SYSLOG_INIT_EARLY</code>, <code>SYSLOG_INIT_LATE</code>}
</li>
</ul>
<p><b>Returned Value:</b>
Zero (<code>OK</code>) is returned on success; a negated <code>errno</code> value is returned on any failure.
</p>
<p>
Different types of SYSLOG devices have different OS initialization
requirements. Some are available immediately at reset, some are available
after some basic OS initialization, and some only after OS is fully
initialized. In order to satisfy these different initialization
requirements, <code>syslog_initialize()</code> is called twice from the boot-up logic:
</p>
<ul>
<li>
<p>
<code>syslog_initialize()</code> is called from the architecture-specific <code>up_initialize()</code> function as some as basic hardware resources have been initialized: Timers, interrupts, etc. In this case, <code>syslog_initialize()</code> is called with the argument <code>SYSLOG_INIT_EARLY</code>.
<code>syslog_initialize()</code> is called again from <code>nx_start()</code> when the full OS initialization has completed, just before the application main entry point is spawned. In this case, <code>syslog_initialize()</code> is called with the argument <code>SYSLOG_INIT_LATE</code>.
There are other types of SYSLOG channel devices that may require even further initialization. For example, the file SYSLOG channel (described below) cannot be initialized until the necessary file systems have been mounted.
As a general statement, SYSLOG output only supports //normal// output from NuttX tasks. However, for debugging purposes, it is also useful to get SYSLOG output from interrupt level logic. In an embedded system, that is often where the most critical operations are performed.
</p>
<p>
There are three conditions under which SYSLOG output generated from interrupt level processing can a included the SYSLOG output stream:
</p>
<ol>
<li>
<p><b>Low-Level Serial Output</b>.
If you are using a SYSLOG console channel (<code>CONFIG_SYSLOG_CONSOLE</code>) with a serial console (<code>CONFIG_SYSLOG_SERIAL_CONSOLE</code>) and if the underlying architecture supports the low-level <code>up_putc()</code> interface(<code>CONFIG_ARCH_LOWPUTC</code>), then the SYLOG logic will direct the output to <code>up_putc()</code> which is capable of generating the serial output within the context of an interrupt handler.
</p>
<p>
There are a few issues in doing this however:
</p>
<ul>
<li>
<p>
<code>up_putc()</code> is able to generate debug output in any context because it disables serial interrupts and polls the hardware directly. These polls may take many milliseconds and during that time, all interrupts are disable within the interrupt handler. This, of course, interferes with the real-time behavior of the RTOS.
The output generated by <code>up_putc()</code> is immediate and in real-time. The normal SYSLOG output, on the other hand, is buffered in the serial driver and may be delayed with respect to the immediate output by many lines. Therefore, the interrupt level SYSLOG output provided through <code>up_putc()</code> is grossly out of synchronization with other debug output
If the RAMLOG SYSLOG channel is supported, then all SYSLOG output is buffered in memory. Interrupt level SYSLOG output is no different than normal SYSLOG output in this case.
</p>
</li>
<li>
<p><b>Serialization Buffer</b>.
A final option is the use the an <i>interrupt buffer</i> to buffer the interrupt level SYSLOG output. In this case:
</p>
<ul>
<li>
<p>
SYSLOG output generated from interrupt level process in not sent to the SYSLOG channel immediately. Rather, it is buffered in the interrupt serialization buffer.
</p>
</li>
<li>
<p>
Later, when the next normal syslog output is generated, it will first empty the content of the interrupt buffer to the SYSLOG device in the proper context. It will then be followed by the normal syslog output. In this case, the interrupt level SYSLOG output will interrupt the normal output stream and the interrupt level SYSLOG output will be inserted into the correct position in the SYSLOG output when the next normal SYLOG output is generated.
</p>
</li>
</ul>
<p>
The SYSLOG interrupt buffer is enabled with <code>CONFIG_SYSLOG_INTBUFFER</code>. When the interrupt buffer is enabled, you must also provide the size of the interrupt buffer with <code>CONFIG_SYSLOG_INTBUFSIZE</code>.
The typical SYSLOG device is the system console. If you are using a serial console, for example, then the SYSLOG output will appear on that serial port.
</p>
<p>
This SYSLOG channel is automatically selected by <code>syslog_initialize()</code> in the LATE initialization phase based on configuration options. The configuration options that affect this channel selection include:
</p>
<ul>
<li>
<p>
<code>CONFIG_DEV_CONSOLE</code>. This setting indicates that the system supports a console device, i.e., that the character device <code>/dev/console</code> exists.
</li>
<li>
<p>
<code>CONFIG_SERIAL_CONSOLE</code>. This configuration option is automatically selected when a UART or USART is configured as the system console. There is no user selection.
<code>CONFIG_SYSLOG_CONSOLE</code>. This configuration option is manually selected from the SYSLOG menu. This is the option that actually enables the SYSLOG console device. It depends on <code>CONFIG_DEV_CONSOLE</code> and it will automatically select <code>CONFIG_SYSLOG_SERIAL_CONSOLE</code> if <code>CONFIG_SERIAL_CONSOLE</code> is selected.
<code>CONFIG_ARCH_LOWPUTC</code>. This is an indication from the architecture configuration that the platform supports the <code>up_putc()</code> interface. <code>up_putc()</code> is a very low level UART interface that can even be used from interrupt handling.
</li>
<li>
<p>
<code>CONFIG_SYSLOG_SERIAL_CONSOLE</code>. This enables certain features of the SYSLOG operation that depend on a serial console. If <code>CONFIG_ARCH_LOWPUTC</code> is also selected, for example, then <code>up_putc()</code> will be used for the forced SYSLOG output.
</li>
</ul>
<p>
Interrupt level SYSLOG output will be lost unless: (1) the interrupt buffer
is enabled to support serialization, or (2) a serial console is used and
<code>up_putc()</code> is supported.
</p>
<p>
NOTE: The console channel uses the fixed character device at <code>/dev/console</code>. The console channel is not synonymous with <code>stdout</code> (or file descriptor 1). <code>stdout</code> is the current output from a task when, say, <code>printf()</code> if used. Initially, <code>stdout</code> does, indeed, use the <code>/dev/console</code> device. However, <code>stdout</code> may subsequently be redirected to some other device or file. This is always the case, for example, when a transient device is used for a console -- such as a USB console or a Telnet console. The SYSLOG channel is not redirected as <code>stdout</code> is; the SYSLOG channel will stayed fixed (unless it is explicitly changed via <code>syslog_channel()</code>).
</p>
<p>
References: <code>drivers/syslog/syslog_consolechannel.c</code> and
<code>drivers/syslog/syslog_device.c</code>
</p>
<h4><aname="syslogchardev">6.4.3.2 SYSLOG Character Device</a></h4>
<p>
The system console device, <code>/dev/console</code>, is a character driver with some special properties. However, any character driver may be used as the SYSLOG output channel. For example, suppose you have a serial console on <code>/dev/ttyS0</code> and you want SYSLOG output on <code>/dev/ttyS1</code>. Or suppose you support only a Telnet console but want to capture debug output <code>/dev/ttyS0</code>.
This SYSLOG device channel is selected with <code>CONFIG_SYSLOG_CHAR</code> and has no other dependencies. Differences from the SYSLOG console channel include:
<code>CONFIG_SYSLOG_DEVPATH</code>. This configuration option string must be set provide the full path to the character device to be used.
</li>
<li>
<p>
The forced SYSLOG output always goes to the bit-bucket. This means that interrupt level SYSLOG output will be lost unless the interrupt buffer is enabled to support serialization.
</li>
<li>
<p>
<code>CONFIG_SYSLOG_CHAR_CRLF</code>. If <code>CONFIG_SYSLOG_CHAR_CRLF</code> is selected, then linefeeds in the SYSLOG output will be expanded to Carriage Return plus Linefeed. Since the character device is not a console device, the addition of carriage returns to line feeds would not be performed otherwise. You would probably want this expansion if you use a serial terminal program with the character device output.
</li>
</ul>
<p>
References: <code>drivers/syslog/syslog_devchannel.c</code> and <code>drivers/syslog/syslog_device.c</code>
Files can also be used as the sink for SYSLOG output. There is, however, a very fundamental difference in using a file as opposed the system console, a RAM buffer, or character device: You must first mount the file system that supports the SYSLOG file. That difference means that the file SYSLOG channel cannot be supported during the boot-up phase but can be instantiated later when board level logic configures the application environment, including mounting of the file systems.
</p>
<p>
The interface <code>syslog_file_channel()</code> is used to configure the SYSLOG file channel:
Configure to use a file in a mounted file system at <code>devpath</code> as the SYSLOG channel.
</p>
<p>
This tiny function is simply a wrapper around <code>syslog_dev_initialize()</code> and <code>syslog_channel()</code>. It calls <code>syslog_dev_initialize()</code> to configure the character file at <code>devpath</code> then calls <code>syslog_channel()</code> to use that device as the SYSLOG output channel.
</p>
<p>
File SYSLOG channels differ from other SYSLOG channels in that they cannot be established until after fully booting and mounting the target file system. This function would need to be called from board-specific bring-up logic AFTER mounting the file system containing <code>devpath</code>.
</p>
<p>
SYSLOG data generated prior to calling <code>syslog_file_channel()</code> will, of course, not be included in the file.
</p>
<p>
NOTE interrupt level SYSLOG output will be lost in this case unless the interrupt buffer is used.
</p>
<p><b>Input Parameters:</b>
<ul>
<li>
<p>
<code>devpath</code>: The full path to the file to be used for SYSLOG output. This may be an existing file or not. If the file exists,
<code>syslog_file_channel()</code> will append new SYSLOG data to the end of the file. If it does not, then <code>syslog_file_channel()</code> will create the file.
</li>
</ul>
<p><b>Returned Value:</b>
Zero (<code>OK</code>) is returned on success; a negated <code>errno</code> value is returned on any failure.
</p>
<p>
References: <code>drivers/syslog/syslog_filechannel.c</code>, <code>drivers/syslog/syslog_device.c</code>, and <code>include/nuttx/syslog/syslog.h</code>.
The RAM logging driver is a driver that was intended to support debugging output (SYSLOG) when the normal serial output is not available. For example, if you are using a Telnet or USB serial console, the debug output will get lost -- or worse. For example, what if you want to debug the network over Telnet?
</p>
<p>
The RAM logging driver can also accept debug output data from interrupt handler with no special serialization buffering. As an added benefit, the RAM logging driver is much less invasive. Since no actual I/O is performed with the debug output is generated, the RAM logger tends to be much faster and will interfere much less when used with time critical drivers.
</p>
<p>
The RAM logging driver is similar to a pipe in that it saves the debugging output in a circular buffer in RAM. It differs from a pipe in numerous details as needed to support logging.
</p>
<p>
This driver is built when <code>CONFIG_RAMLOG</code> is defined in the Nuttx configuration.
When the RAMLOG (with SYSLOG) is enabled, a new NuttShell (NSH) command will appear: <code>dmesg</code>. The <code>dmesg</code> command will dump the contents of the circular buffer to the console (and also clear the circular buffer).
<code>CONFIG_RAMLOG</code>: Enables the RAM logging feature
</li>
<li>
<p>
<code>CONFIG_RAMLOG_CONSOLE</code>: Use the RAM logging device as a system console. If this feature is enabled (along with <code>CONFIG_DEV_CONSOLE</code>), then all console output will be re-directed to a circular buffer in RAM. This might be useful, for example, if the only console is a Telnet console. Then in that case, console output from non-Telnet threads will go to the circular buffer and can be viewed using the NSH <code>dmesg</code> command. This optional is not useful in other scenarios.
</li>
<li>
<p>
<code>CONFIG_RAMLOG_SYSLOG</code>: Use the RAM logging device for the SYSLOG interface. If this feature is enabled, then all debug output will be re-directed to the circular buffer in RAM. This RAM log can be viewed from NSH using the <code>dmesg</code> command. NOTE: Unlike the limited, generic character driver SYSLOG device, the RAMLOG <i>can</i> be used to capture debug output from
interrupt level handlers.
</li>
<li>
<p>
<code>CONFIG_RAMLOG_NPOLLWAITERS</code>: The number of threads than can be waiting for this driver on <code>poll()</code>. Default: 4
</li>
</ul>
<p>
If <code>CONFIG_RAMLOG_CONSOLE</code> or <code>CONFIG_RAMLOG_SYSLOG</code> is selected, then the following must also be provided:
</p>
<ul>
<li>
<p>
<code>CONFIG_RAMLOG_BUFSIZE</code>: The size of the circular buffer to use. Default: 1024 bytes.
</li>
</ul>
<p>
Other miscellaneous settings
</p>
<ul>
<li>
<p>
<code>CONFIG_RAMLOG_CRLF</code>: Pre-pend a carriage return before every linefeed that goes into the RAM log.
</li>
<li>
<p>
<code>CONFIG_RAMLOG_NONBLOCKING</code>: Reading from the RAMLOG will never block if the RAMLOG is empty. If the RAMLOG is empty, then zero is returned (usually interpreted as end-of-file). If you do not define this, the NSH <code>dmesg</code> command will lock up when called! So you probably do want this!
</li>
<li>
<p>
<code>CONFIG_RAMLOG_NPOLLWAITERS</code>: The maximum number of threads that may be waiting on the poll method.
</li>
</ul>
<h2><aname="pwrmgmt">6.5 Power Management</a></h2>
Each PM interfaces includes a integer <i>domain</i> number.
By default, only a single power domain is supported (<code>CONFIG_PM_NDOMAINS=1</code>).
But that is configurable; any number of PM domains can be supported.
Multiple PM domains might be useful, for example, if you would want to control power states associated with a network separately from power states associated with a user interface.
This function must be called <i>very</i> early in the initialization sequence <i>before</i> any other device drivers are initialized (since they may attempt to register with the power management subsystem).
This function returns the "recommended" power management state based on the PM configuration and activity reported in the last sampling periods.
The power management state is not automatically changed, however.
The IDLE loop must call <code>pm_changestate()</code> in order to make the state change.
These two steps are separated because the platform-specific IDLE loop may have additional situational information that is not available to the PM sub-system.
For example, the IDLE loop may know that the battery charge level is very low and may force lower power states even if there is activity.
</p>
<p>
NOTE: That these two steps are separated in time and, hence, the IDLE loop could be suspended for a long period of time between calling <code>pm_checkstate()</code> and <code>pm_changestate()</code>.
The IDLE loop may need to make these calls atomic by either disabling interrupts until the state change is completed.
Zero (<code>OK</code>) means the event was successfully processed and that the driver is prepared for the PM state change.
Non-zero means that the driver is not prepared to perform the tasks needed achieve this power setting and will cause the state change to be aborted.
NOTE: The <code>prepare()</code> method will also be called when reverting from lower back to higher power consumption modes (say because another driver refused a lower power state change).
The current NuttX configuration variables are also documented in separate, auto-generated configuration variable document.
That configuration variable document is generated using the <code>kconfig2html</code> tool that can be found in the <code>nuttx/tools</code> directory.
That tool analyzes the NuttX Kconfig files and generates excruciatingly boring HTML document.
The latest boring configuration variable documentation can be regenerated at any time using that tool or, more appropriately, the wrapper script at <code>nuttx/tools/mkconfigvars.sh</code>.
That script will generate the file <code>nuttx/Documentation/NuttXConfigVariables.html</code>.
The version of <code>NuttXConfigVariables.html</code> for the last released version of NuttX can also be found <ahref="http://nuttx.org/Documentation/NuttXConfigVariables.html">online</a>.