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):
</p>
<ul><pre>
default "myboar" if ARCH_BOARD_MYBOARD
</pre></ul>
<p>
Now the build system knows where to find your board configuration!
</p>
<p>
And finally, add something like this near the bottom of <code>configs/myboard</code>:
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>/]setenv.sh</code> to <code>${TOPDIR}/setenv.sh</code>, and</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 <ahref="#boardconfigsubdirs"><code>setenv.sh</code></a> contains Linux/Cygwin environmental settings that are needed for the build.
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>.
The <ahref="#boardconfigsubdirs"><code>setenv.sh</code></a> only needs to be source'ed at the beginning of a session.
<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>
<h2><aname="naming">4.1 Naming and Header File Conventions</a></h2>
<ul>
<li>
<p>
<b>Common Microprocessor Interfaces</b>.
Any interface that is common to all microprocessors should be prefixed with <code>up_</code> and prototyped in <code>include/nuttx/arch.h</code>.
The definitions in that header file provide the common interface between NuttX and the architecture-specific implementation in <code>arch/</code>.
</p>
<blockquote><small>
<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.
</small></blockquote>
</li>
<li>
<p>
<b>Microprocessor-Specific Interfaces</b>.
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.
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.
</p>
<p>
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 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).
</p>
<p>
The default, 100 microseconds, will provide for a range of delays up to 120 hours.
</p>
<p>
This value should never be less than the underlying resolution of the timer. Errors may ensue.
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_intialize()</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).
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
None
</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>
Called early in the initialization sequence before any special concurrency protections are required.
Return the elapsed time since power-up (or, more correctly, since <code>up_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.
Zero (<code>OK</code>) on success; a negated <code>errno</code> value on failure.
</ul>
<p><b>Assumptions</b>:</p>
<ul>
Called from the 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.
</ul>
<p><b>Assumptions</b>:</p>
<ul>
May be called from interrupt level handling or from the normal tasking level. iterrupts may need to be disabled internally to assure non-reentrancy.
Start the alarm. <code>sched_timer_expiration()</code> will be called when the alarm occurs (unless <code>up_alaram_cancel</code> is called to stop it).
<ul>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
<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.
<h3><aname="wqclasses">4.4.1 Classes of Work Queues</a></h3>
<p><b>Classes of Work Queues</b>.
There are three different classes of work queues, each with different properties and intended usage. These class of work queues along with the the common work queue interface are described in the following paragraphs.
</p>
<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)
</p>
<p><b>Device Driver Bottom Half</b>.
The higher 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>Compared to the Low Priority Kernel Work Queue</b>.
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 <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.
The execution priority of the high-priority worker thread. Default: 224
</li>
<li><code>CONFIG_SCHED_HPWORKPERIOD</code>.
How often the worker thread re-checks for work in units of microseconds. This work period is really only necessary if the the high priority thread is performing periodic garbage collection. The worker thread will be awakened immediately with it is queued work to be done. If the high priority worker thread is performing garbage collection, then the default is 50*1000 (50 MS). Otherwise, if the lower priority worker thread is performing garbage collection, the default is 100*1000.
<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.
</p>
<p>
<b>Compared to the High Priority Work Queue</b>.
The lower priority work queue runs at a lower priority than the high priority work queue, of course, and so is inapproperiate to serve as a driver <i>bottom half</i>. The lower priority work queue has the other advantages, however, that make it better suited for some tasks:
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.
<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.
</p>
</li>
<li>
<p>
<b>Thread Pool</b>.
The low-priority work queue can be configured to support multiple, low-priority threads. This is essentially a <i>thread pool</i> that provides multi-threaded servicing of the low-priority work thread. This breaks the strict serialization of the "queue" (and hence, the low-priority 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 low-priority queue processing would stall in such cases. Such behavior is necessary to support asynchronous I/O, AIO, for example.
</p>
</li>
</ul>
<p>
<b>Configuration Options</b>.
</p>
<ul>
<li><code>CONFIG_SCHED_LPWORK</code>.
If CONFIG_SCHED_LPWORK is selected then a lower-priority work queue will be enabled.
</li>
<li><code>CONFIG_SCHED_LPNTHREADS</code>.
The number of thread in the low-priority queue's thread pool. Default: 1
</li>
<li><code>CONFIG_SCHED_LPWORKPRIORITY</code>.
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.
</li>
<li><code>CONFIG_SCHED_LPWORKPRIOMAX</code>.
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.
</li>
<li><code>CONFIG_SCHED_LPWORKPERIOD</code>.
How often the lower priority worker thread checks for garbage collection in units of microseconds. Default: 50*1000 (50 MS).
</li>
<li><code>CONFIG_SCHED_LPWORKSTACKSIZE</code>.
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_USRWORKPERIOD</code>
How often the lower priority worker thread is awakened in units of microseconds. Default: 100*1000 (100 MS).
<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 subjec 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.
</li>
</ul>
<h4><aname="wqintfs">4.4.2.3 Work Queue Interfaces</a></h4>
int work_queue(int qid, FAR struct work_s *work, worker_t worker,
FAR void *arg, uint32_t delay);
</pre></ul>
</p>
<p>
<b>Description</b>.
Queue work to be performed at a later time. All queued work will be performed on the worker thread of execution (not the caller's).
</p>
<p>
The work structure is allocated by caller, but completely managed by the work queue logic. The caller should never modify the contents of the work queue structure; the caller should not call <code>work_queue()</code> again until either (1) the previous work has been performed and removed from the queue, or (2) <code>work_cancel()</code> has been called to cancel the work and remove it from the work queue.
</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 work structure to queue
</p>
</li>
<li>
<p>
<code>worker</code>:
The worker callback to be invoked. The callback will invoked on the worker thread of execution.
</p>
</li>
<li>
<p>
<code>arg</code>:
The argument that will be passed to the worker callback function when it is invoked.
</p>
</li>
<li>
<p>
<code>delay</code>:
Delay (in system clock ticks) from the time queue until the worker is invoked. Zero means to perform the work immediately.
</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.
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 evironment 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.
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.
</p>
<p>
"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>.
spinlock_t up_testset(volatile FAR spinlock_t *lock);
#endif
</pre></ul>
<p><b>Description</b>:</p>
<ul>
<p>
Perform and atomic test and set operation on the provided spinlock.
</p>
</ul>
<p><b>Input Parameters</b>:</p>
<ul>
<li>
<code>lock</code>: The address of spinlock object.
</li>
</ul>
<p><b>Returned Value</b>:</p>
<ul>
<p>
The spinlock is always locked upon return.
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)
In an SMP configution, only one CPU is initially active (CPU 0).
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.
</p>
</ul>
<h2><aname="exports">4.8 APIs Exported by NuttX to Architecture-Specific Logic</a></h2>
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.
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.
But there are a few architectures where this initialization function is still called from common chip architecture logic.
This interface is nott, however, a common board interface in any event.
</blockquote>
<blockquote>
<b>WARNING:</b> This interface name will eventually be removed; do not use it in new board ports.
New implementations should not use the naming convention for common board interfaces, but should instted use the naming conventions for microprocessor-specific interfaces or the board-specific interfaces (such as <code>stm32_led_initialize()</code>).
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.
The header file for the NuttX PWM driver reside at <code>include/nuttx/pwm.h</code>.
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.
<h3><aname="candrivers">6.1.5 CAN Drivers</a></h3>
<p>
NuttX supports only a very low-level CAN driver.
This driver supports only the data exchange and does not include any high-level CAN protocol.
The NuttX CAN driver is split into two parts:
</p>
<ol>
<li>
An "upper half", generic driver that provides the common CAN interface to application level code, and
</li>
<li>
A "lower half", platform-specific driver that implements the low-level timer controls to implement the CAN functionality.
</li>
</ol>
<p>
Files supporting CAN can be found in the following locations:
</p>
<ul>
<li><b>Interface Definition</b>.
The header file for the NuttX CAN driver reside at <code>include/nuttx/can.h</code>.
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.
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>.
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 initialize (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>.