organization.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. .. include:: /substitutions.rst
  2. .. todo::
  3. This is mostly untouched from the original documentation. It does
  4. not really belong to "quickstart". Also, this needs cleanup.
  5. .. _organization:
  6. ===================
  7. Directory Structure
  8. ===================
  9. This is included for reference, and it's not necessary to know
  10. all the details at first.
  11. The general directory layout for NuttX is
  12. very similar to the directory structure of the Linux kernel -- at
  13. least at the most superficial layers. At the top level is the main
  14. makefile and a series of sub-directories identified below and
  15. discussed in the following paragraphs:
  16. **Configuration Files**. The NuttX configuration consists of logic
  17. in processor architecture directories, *chip/SoC* directories, and
  18. board configuration directories. The complete configuration is
  19. specified by several settings in the NuttX configuration file.
  20. - *Processor architecture specific files*. These are the files
  21. contained in the ``arch/``\ *<arch-name>*\ ``/`` directory and
  22. are discussed in a paragraph
  23. `below <#archdirectorystructure>`__. As an example, all ARM
  24. processor architectures are provided under the ``arch/arm/``
  25. directory which is selected with the ``CONFIG_ARCH="arm"``
  26. configuration option.
  27. Variants of the processor architecture may be provided in
  28. sub-directories of the Extending this example, the ARMv7-M ARM
  29. family is supported by logic in ``arch/arm/include/armv7-m``
  30. and ``arch/arm/src/armv7-m`` directories which are selected by
  31. the ``CONFIG_ARCH_CORTEXM3=y``, ``CONFIG_ARCH_CORTEXM4=y``, or
  32. ``CONFIG_ARCH_CORTEXM7=y`` configuration options
  33. - *Chip/SoC specific files*. Each processor architecture is
  34. embedded in a *System-on-a-Chip* (SoC) architecture. The full
  35. SoC architecture includes the processor architecture plus
  36. chip-specific interrupt logic, clocking logic, general purpose
  37. I/O (GPIO) logic, and specialized, internal peripherals (such
  38. as UARTs, USB, etc.).
  39. These chip-specific files are contained within chip-specific
  40. sub-directories also under the ``arch/``\ *<arch-name>*\ ``/``
  41. directory and are selected via the ``CONFIG_ARCH_CHIP``
  42. selection.
  43. As an example, the STMicro STM32 SoC architecture is based on
  44. the ARMv7-M processor and is supported by logic in the
  45. ``arch/arm/include/stm32`` and ``arch/arm/src/stm32``
  46. directories which are selected with the
  47. ``CONFIG_ARCH_CHIP="stm32"`` configuration setting.
  48. - *Board specific configurations*. In order to be usable, the
  49. chip must be contained in a board environment. The board
  50. configuration defines additional properties of the board
  51. including such things as peripheral LEDs, external peripherals
  52. (such as networks, USB, etc.).
  53. These board-specific configuration files can be found in the
  54. ``boards/``\ *<arch-name>*\ ``/``\ *<chip-name>*\ ``/``\ *<board-name>*\ ``/``
  55. sub-directories and are discussed in a paragraph
  56. `below <#boardsdirectorystructure>`__.
  57. The directory ``boards/arm/stm32/stm32f4disovery/``, as an
  58. example, holds board-specific logic for the STM32F4 Discovery
  59. board and is selected via the
  60. ``CONFIG_ARCH_BOARD="stm32f4discovery"`` configuration setting.
  61. ``nuttx/Documentation``
  62. =======================
  63. This directory holds the NuttX documentation. It's made with
  64. the `Sphinx documentation system <https://www.sphinx-doc.org>`_. See the
  65. README.md file for information on how to build it.
  66. ``nuttx/arch``
  67. ==============
  68. Arch Subdirectory Structure
  69. ---------------------------
  70. This directory contains several sub-directories, each containing
  71. architecture-specific logic. The task of porting NuttX to a new
  72. processor consists of add a new subdirectory under ``arch/``
  73. containing logic specific to the new architecture. The complete
  74. board port in is defined by the architecture-specific code in this
  75. directory (plus the board-specific configurations in the
  76. ``config/`` subdirectory). Each architecture must provide a
  77. subdirectory, *<arch-name>* under ``arch/`` with the following
  78. characteristics:
  79. Arch Summary of Files
  80. ---------------------
  81. - ``include/``\ *<chip-name>*\ ``/`` This sub-directory contains
  82. chip-specific header files.
  83. - ``include/arch.h``: This is a hook for any architecture
  84. specific definitions that may be needed by the system. It is
  85. included by ``include/nuttx/arch.h``.
  86. - ``include/types.h``: This provides
  87. architecture/toolchain-specific definitions for standard types.
  88. This file should ``typedef``:
  89. and if the architecture supports 24- or 64-bit integers
  90. NOTE that these type names have a leading underscore character.
  91. This file will be included(indirectly) by include/stdint.h and
  92. typedef'ed to the final name without the underscore character.
  93. This roundabout way of doings things allows the stdint.h to be
  94. removed from the include/ directory in the event that the user
  95. prefers to use the definitions provided by their toolchain
  96. header files
  97. And finally
  98. Must be defined to the be the size required to hold the
  99. interrupt enable/disable state.
  100. This file will be included by include/sys/types.h and be made
  101. available to all files.
  102. - ``include/irq.h``: This file needs to define some architecture
  103. specific functions (usually inline if the compiler supports
  104. inlining) and some structures. These include:
  105. - ``struct xcptcontext``: This structures represents the saved
  106. context of a thread.
  107. - ``irqstate_t up_irq_save(void)``: Used to disable all
  108. interrupts. In the case of multi-CPU platforms, this
  109. function disables interrupts on CPUs.
  110. - ``void up_irq_restore(irqstate_t flags)``: Used to restore
  111. interrupt enables to the same state as before
  112. ``up_irq_save()`` was called.
  113. This file must also define ``NR_IRQS``, the total number of
  114. IRQs supported by the board.
  115. - ``include/syscall.h``: This file needs to define some
  116. architecture specific functions (usually inline if the compiler
  117. supports inlining) to support software interrupts or
  118. *syscall*\ s that can be used all from user-mode applications
  119. into kernel-mode NuttX functions. This directory must always be
  120. provided to prevent compilation errors. However, it need only
  121. contain valid function declarations if the architecture
  122. supports the ``CONFIG_BUILD_PROTECTED`` or
  123. ``CONFIG_BUILD_KERNEL``\ configurations.
  124. - ``uintptr_t sys_call0(unsigned int nbr)``: ``nbr`` is one of
  125. the system call numbers that can be found in
  126. ``include/sys/syscall.h``. This function will perform a
  127. system call with no (additional) parameters.
  128. - ``uintptr_t sys_call1(unsigned int nbr, uintptr_t parm1)``:
  129. ``nbr`` is one of the system call numbers that can be found
  130. in ``include/sys/syscall.h``. This function will perform a
  131. system call with one (additional) parameter.
  132. - ``uintptr_t sys_call2(unsigned int nbr, uintptr_t parm1, uintptr_t parm2)``:
  133. ``nbr`` is one of the system call numbers that can be found
  134. in ``include/sys/syscall.h``. This function will perform a
  135. system call with two (additional) parameters.
  136. - ``uintptr_t sys_call3(unsigned int nbr, uintptr_t parm1, uintptr_t parm2, uintptr_t parm3)``:
  137. ``nbr`` is one of the system call numbers that can be found
  138. in ``include/sys/syscall.h``. This function will perform a
  139. system call with three (additional) parameters.
  140. - ``uintptr_t sys_call4(unsigned int nbr, uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, uintptr_t parm4)``:
  141. ``nbr`` is one of the system call numbers that can be found
  142. in ``include/sys/syscall.h``. This function will perform a
  143. system call with four (additional) parameters.
  144. - ``uintptr_t sys_call5(unsigned int nbr, uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, uintptr_t parm4, uintptr_t parm5)``:
  145. ``nbr`` is one of the system call numbers that can be found
  146. in ``include/sys/syscall.h``. This function will perform a
  147. system call with five (additional) parameters.
  148. - ``uintptr_t sys_call6(unsigned int nbr, uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, uintptr_t parm4, uintptr_t parm5, uintptr_t parm6)``:
  149. ``nbr`` is one of the system call numbers that can be found
  150. in ``include/sys/syscall.h``. This function will perform a
  151. system call with six (additional) parameters.
  152. This file must also define ``NR_IRQS``, the total number of
  153. IRQs supported by the board.
  154. - ``src/``\ *<chip-name>*\ ``/`` This sub-directory contains
  155. chip-specific source files.
  156. - ``src/Makefile``: This makefile will be executed to build the
  157. targets ``src/libup.a`` and ``src/up_head.o``. The
  158. ``up_head.o`` file holds the entry point into the system
  159. (power-on reset entry point, for example). It will be used in
  160. the final link with ``libup.a`` and other system archives to
  161. generate the final executable.
  162. - *(architecture-specific source files)*. The file
  163. ``include/nuttx/arch.h`` identifies all of the APIs that must
  164. be provided by the architecture specific logic. (It also
  165. includes ``arch/``\ *<arch-name>*\ ``/arch.h`` as described
  166. above).
  167. Supported Architectures
  168. -----------------------
  169. **Architecture- and Chip-Specific Directories**. All processor
  170. architecture-specific directories are maintained in
  171. sub-directories of the ``arch/`` directory. Different chips or
  172. SoC's may implement the same processor core. Chip-specific logic
  173. can be found in sub-directories under the architecture directory.
  174. Current architecture/chip directories are summarized below:
  175. - ``arch/sim``: A user-mode port of NuttX to the x86 Linux or
  176. Cygwin platform is available. The purpose of this port is
  177. primarily to support OS feature development. This port does not
  178. support interrupts or a real timer (and hence no round robin
  179. scheduler) Otherwise, it is complete.
  180. - ``arch/arm``: This directory holds common ARM architectures.
  181. - ``arch/avr``: This directory holds common AVR and AVR32
  182. architectures.
  183. - ``arch/mips``: This directory holds common MIPS architectures.
  184. This include PIC32MX and PIC32MZ.
  185. - ``arch/misoc``: This directory supports the Misoc LM3
  186. architecture.
  187. - ``arch/or1K``: This directory supports the OpenRISC mor1kx
  188. architecture.
  189. - ``arch/renesas``: This directory is the home for various
  190. Renesas architectures, currently only the M16C and vererable
  191. SuperH-1 architectures.
  192. - ``arch/xtensa``: This directory supports the Xtensa LX6
  193. architecture as used by the ESP32.
  194. - ``arch/z16f``: Zilog z16f Microcontroller.
  195. - ``arch/z80``: This directory holds 8-bit ZiLOG architectures.
  196. At present, this includes the Zilog z80, ez80Acclaim! and
  197. z8Encore! Microcontrollers.
  198. ``nuttx/binfmt``
  199. ================
  200. The ``binfmt/`` subdirectory contains logic for loading binaries
  201. in the file system into memory in a form that can be used to
  202. execute them.
  203. ``nuttx/audio``
  204. ===============
  205. The ``audio/`` subdirectory contains the NuttX audio sub-system.
  206. .. _nuttx_boards:
  207. ``nuttx/boards``
  208. ================
  209. The ``boards/`` subdirectory contains custom logic and board
  210. configuration data for each board. These board-specific
  211. configurations plus the architecture-specific configurations in
  212. the ``arch/`` subdirectory complete define a customized port of
  213. NuttX.
  214. Boards Subdirectory Structure
  215. -----------------------------
  216. The ``boards/`` directory contains board specific configuration
  217. files. Each board must provide a sub-directory <board-name> under
  218. ``boards/``\ *<arch-name>*\ ``/``>\ *<chip-name>*\ ``/`` with the
  219. following characteristics:
  220. Boards Summary of Files
  221. -----------------------
  222. **Board Specific Logic**
  223. - ``include/``: This directory contains board specific header
  224. files. This directory will be linked as ``include/arch/board``
  225. at configuration time and can be included via
  226. ``#include <arch/board/header.h>``. These header file can only
  227. be included by files in ``arch/``\ *<arch-name>*\ ``/include/``
  228. and ``arch/``\ *<arch-name>*\ ``/src/``.
  229. - ``src/``: This directory contains board specific drivers. This
  230. directory will be linked as
  231. *<config>*\ ``/arch/``\ *<arch-name>*\ ``/src/board`` at
  232. configuration time and will be integrated into the build
  233. system.
  234. - ``src/Makefile``: This makefile will be invoked to build the
  235. board specific drivers. It must support the following targets:
  236. ``libext$(LIBEXT)``, ``clean``, and ``distclean``.
  237. **Board Specific Configuration Sub-Directories**
  238. The
  239. ``boards/``\ *<arch-name>*\ ``/``\ *<chip-name>*\ ``/``\ *<board-name>*\ ``/configs``
  240. sub-directory holds all of the files that are necessary to
  241. configure NuttX for the particular board. A board may have various
  242. different configurations using the common source files. Each board
  243. configuration is described by two files: ``Make.defs`` and
  244. ``defconfig``. Typically, each set of configuration files is
  245. retained in a separate configuration sub-directory
  246. (*<config1-dir>*, *<config2-dir>*, .. in the above diagram).
  247. NOTE: That the ``Make.defs`` file may reside in one of two
  248. locations: There may be a unique Make.defs file for each
  249. configuration in the configuration directory *OR* if that file is
  250. absent, there may be a common board ``Make.defs`` file in the
  251. ``/scripts`` directory. The ``Make.defs`` file in the
  252. configuration takes precedence if it is present.
  253. The procedure for configuring NuttX is described
  254. `below <#configuringnuttx>`__, This paragraph will describe the
  255. contents of these configuration files.
  256. - ``Make.defs``: This makefile fragment provides architecture and
  257. tool-specific build options. It will be included by all other
  258. makefiles in the build (once it is installed). This make
  259. fragment should define:
  260. - Tools: ``CC``, ``LD``, ``AR``, ``NM``, ``OBJCOPY``,
  261. ``OBJDUMP``
  262. - Tool options: ``CFLAGS``, ``LDFLAGS``
  263. When this makefile fragment runs, it will be passed ``TOPDIR``
  264. which is the path to the root directory of the build. This
  265. makefile fragment should include:
  266. - ``$(TOPDIR)/.config`` : NuttX configuration
  267. - ``$(TOPDIR)/tools/Config.mk`` : Common definitions
  268. Definitions in the ``Make.defs`` file probably depend on some
  269. of the settings in the .\ ``config`` file. For example, the
  270. ``CFLAGS`` will most likely be different if
  271. ``CONFIG_DEBUG_FEATURES=y``.
  272. The included ``tools/Config.mk`` file contains additional
  273. definitions that may be overridden in the architecture-specific
  274. Make.defs file as necessary:
  275. - ``COMPILE``, ``ASSEMBLE``, ``ARCHIVE``, ``CLEAN``, and
  276. ``MKDEP`` macros
  277. - ``defconfig``: This is a configuration file similar to the
  278. Linux configuration file. In contains variable/value pairs
  279. like:
  280. - ``CONFIG_VARIABLE``\ =value
  281. This configuration file will be used at build time:
  282. #. As a makefile fragment included in other makefiles, and
  283. #. to generate ``include/nuttx/config.h`` which is included by
  284. most C files in the system.
  285. Supported Boards
  286. ----------------
  287. All of the specific boards supported by NuttX are identified in
  288. the
  289. `README.txt <https://github.com/apache/incubator-nuttx/blob/master/boards/README.txt>`__
  290. file.
  291. Adding a New Board Configuration
  292. --------------------------------
  293. Okay, so you have created a new board configuration directory.
  294. Now, how do you hook this board into the configuration system so
  295. that you can select with ``make menuconfig``?
  296. You will need modify the file ``boards/Kconfig``. Let's look at
  297. the STM32F4-Discovery configuration in the ``Kconfig`` file and
  298. see how we would add a new board directory to the configuration.
  299. For this configuration let's say that you new board resides in the
  300. directory ``boards/myarch/mychip/myboard``; It uses an MCU
  301. selected with ``CONFIG_ARCH_CHIP_MYMCU``; and you want the board
  302. to be selected with ``CONFIG_ARCH_BOARD_MYBOARD``. Then here is
  303. how you can clone the STM32F4-Discovery configuration in
  304. ``boards/Kconfig`` to support your new board configuration.
  305. In ``boards/Kconfig`` for the stm32f4-discovery, you will see a
  306. configuration definition like this:
  307. The above selects the STM32F4-Discovery board. The ``select``
  308. lines say that the board has both LEDs and buttons and that the
  309. board can generate interrupts from the button presses. You can
  310. just copy the above configuration definition to a new location
  311. (notice that they the configurations are in alphabetical order).
  312. Then you should edit the configuration to support your board. The
  313. final configuration definition might look something like:
  314. Later in the ``boards/Kconfig`` file, you will see a long, long
  315. string configuration with lots of defaults like this:
  316. This logic will assign string value to a configuration variable
  317. called ``CONFIG_ARCH_BOARD`` that will name the directory where
  318. the board-specific files reside. In our case, these files reside
  319. in ``boards/myarch/mychip/myboard`` and we add the following to
  320. the long list of defaults (again in alphabetical order):
  321. Now the build system knows where to find your board configuration!
  322. And finally, add something like this near the bottom of
  323. ``boards/myarch/mychip/myboard``:
  324. This includes additional, board-specific configuration variable
  325. definitions in ``boards/myarch/mychip/myboard/Kconfig``.
  326. ``nuttx/crypto``
  327. ================
  328. This sub-directory holds the NuttX cryptographic sub-system.
  329. ``nuttx/drivers``
  330. =================
  331. This directory holds architecture-independent device drivers.
  332. ``nuttx/fs``
  333. ============
  334. This directory contains the NuttX file system. This file system is
  335. described `below <#NxFileSystem>`__.
  336. ``nuttx/graphics``
  337. ==================
  338. This directory contains files for graphics/video support under
  339. NuttX.
  340. ``nuttx/include``
  341. =================
  342. This directory holds NuttX header files. Standard header files
  343. file retained in can be included in the *normal* fashion:
  344. ``nuttx``
  345. =========
  346. This is a (almost) empty directory that has a holding place for
  347. generated static libraries. The NuttX build system generates a
  348. collection of such static libraries in this directory during the
  349. compile phase. These libraries are then in a known place for the
  350. final link phase where they are accessed to generated the final
  351. binaries.
  352. ``nuttx/libs/libc``
  353. ===================
  354. This directory holds a collection of standard libc-like functions
  355. with custom interfaces into NuttX.
  356. Normally the logic in this file builds to a single library
  357. (``libc.a``). However, if NuttX is built as a separately compiled
  358. kernel (with ``CONFIG_BUILD_PROTECTED=y`` or
  359. ``CONFIG_BUILD_KERNEL=y``), then the contents of this directory
  360. are built as two libraries: One for use by user programs
  361. (``libc.a``) and one for use only within the <kernel> space
  362. (``libkc.a``).
  363. These user/kernel space libraries (along with the sycalls of
  364. ```nuttx/syscall`` <#DirStructSyscall>`__) are needed to support
  365. the two differing protection domains.
  366. Directory structure:
  367. ``nuttx/libs/libxx``
  368. ====================
  369. This directory holds a tiny, minimal standard std C++ that can be
  370. used to build some, simple C++ applications in NuttX.
  371. ``nuttx/mm``
  372. ============
  373. This is the NuttX memory manager.
  374. ``nuttx/net``
  375. =============
  376. This directory contains the implementation of the NuttX networking
  377. layer including internal socket APIs.
  378. ``nuttx/sched``
  379. ===============
  380. The files forming core of the NuttX RTOS reside here.
  381. ``nuttx/syscall``
  382. =================
  383. If NuttX is built as a separately compiled kernel (with
  384. ``CONFIG_BUILD_PROTECTED=y`` or ``CONFIG_BUILD_KERNEL=y``), then
  385. the contents of this directory are built. This directory holds a
  386. syscall interface that can be used for communication between
  387. user-mode applications and the kernel-mode RTOS.
  388. ``nuttx/tools``
  389. ===============
  390. This directory holds a collection of tools and scripts to simplify
  391. configuring, building and maintaining NuttX.
  392. Refer to the README file in the ``tools`` directory for more
  393. information about the individual files. Some of these tools are
  394. discussed below as well in the discussion of `configuring and
  395. building <#configandbuild>`__ NuttX.
  396. ``nuttx/wireless``
  397. ==================
  398. This directory holds support for hardware-independent wireless
  399. support.
  400. ``nuttx/Makefile``
  401. ==================
  402. The top-level ``Makefile`` in the ``$(TOPDIR)`` directory contains
  403. all of the top-level control logic to build NuttX.