Documentation guidelines

The nRF Connect SDK documentation is written in two formats:

  • reStructuredText (RST) for conceptual documentation

  • doxygen for API documentation

RST guidelines

See Zephyr’s Documentation Guidelines for a short introduction to RST and Zephyr documentation conventions. More information about RST is available in the reStructuredText Primer.

The nRF Connect SDK documentation follows the Zephyr style guide, with the addition of the following rules.

Title and headings

  • Keep titles and headings short and to the point.

  • Add a reference label above the title.

    For example, this page has the reference label .. _doc_styleguide:. You can see this by clicking the View page source in the top-right corner of the page.

  • Do not repeat the section name in the titles of subpages, such as sample when adding a sample.

Table of contents

If your page uses sections, add the .. contents:: directive just under the page title. This will add a linked table of contents at the top of the page.

For easy navigation, do not include a table of contents if the page has a list of subpages.

Subpages

Use the .. toctree:: directive at the bottom of a page to list pages that are located further down in the hierarchy. For example, the About this documentation page has a list of subpages, which includes this page you are currently reading.

For a clean structure, pages with the subpages section must not contain heading-based sections or a table of contents.

Linking

You can use different linking and inclusion methods, depending on the content you want to link to.

Replacements

If you need to repeat some information, do not duplicate the text. Use the .. |tag| replace:: replacement command to reuse the text. Whenever you use the tag in an RST document, it will be replaced with the text specified for the tag.

You can reuse the content with the tag either on one page or on multiple pages:

  • To reuse the text on one page, define the |tag| and the replacement text before the reference label and the page title.

  • To reuse the text on multiple pages, define the |tag| and the replacement text in nrf/doc/nrf/shortcuts.txt.

For example, on this page, the |gl| tag is defined for local usage and will be replaced with guidelines. This tag is not available on other pages. The page is also using the |NCS| tag that is defined in shortcuts.txt and can be used on all documentation pages in the nRF Connect SDK project.

Breathe

The Breathe Sphinx plugin provides a bridge between RST and doxygen.

The doxygen documentation is not automatically included in RST. Therefore, every group must be explicitly added to an RST file. For example, the code below adds the bluetooth_throughput group to the RST document, and includes the public members of any classes in the group. The Breathe documentation contains information about what you can link to and how to do it.

.. doxygengroup:: bluetooth_throughput
   :project: nrf
   :members:

Note

Including a group on a page does not include all its subgroups automatically. To include subgroups, add the :inner: option.

However, if subgroups are defined in separate files, you should rather list them manually on the page of the group they belong to, so that you can include information on where they are defined.

To link directly to a doxygen reference from RST, use the following Breathe domains:

  • Function: :c:func:

  • Structure: :c:struct:

  • Type: :c:type:

  • Enum (the list): :c:enum:

  • Enumerator (an item): :c:enumerator:

  • Macro or define: :c:macro:

  • Structure member: :c:member:

Kconfig

Kconfig options can be linked to from RST by using the :kconfig:option: domain:

:kconfig:option:`CONFIG_DEBUG`

Doxygen guidelines

These are the guidelines for the doxygen-based API documentation.

General documentation guidelines

  1. Always use full sentences, except for descriptions for variables, structs, and enums, where sentence fragments with no verb are accepted, and always end everything with period.

  2. Everything that is documented must belong to a group (see below).

  3. Use capitalization sparingly. When in doubt, use lowercase.

  4. Line breaks: In doxygen, break after 80 characters (following the dev guidelines). In RST, break after each sentence.

  5. @note and @warning should only be used in the details section, and only when really needed for emphasis. Use notes for emphasis, and warnings only if things will really really go wrong if you ignore the warning.

File headers and groups

  1. @file element is always required at the start of a file.

  2. There is no need to use @brief for @file.

  3. @defgroup or @addgroup usually follows @file. You can divide a file into several groups as well.

  4. @{ must open the group, @} must close it.

  5. @brief must be added for every defgroup.

  6. @details is optional to be used within the defgroup.

/**
 * @file
 * @defgroup bt_gatt_pool BLE GATT attribute pool API
 * @{
 * @brief BLE GATT attribute pools.
 */

#ifdef __cplusplus
extern "C" {
#endif

#include <bluetooth/gatt.h>
#include <bluetooth/uuid.h>

/**
 *  @brief Register a primary service descriptor.
 *
 *  @param _svc GATT service descriptor.
 *  @param _svc_uuid_init Service UUID.
 */
#define BT_GATT_POOL_SVC_GET(_svc, _svc_uuid_init)   \
{                                                    \
   struct bt_uuid *_svc_uuid = _svc_uuid_init;       \
   bt_gatt_pool_svc_get(_svc, _svc_uuid);            \
}

[...]
/** @brief Return a CCC descriptor to the pool.
 *
 *  @param attr Attribute describing the CCC descriptor to be returned.
 */
void bt_gatt_pool_ccc_put(struct bt_gatt_attr const *attr);

#if CONFIG_BT_GATT_POOL_STATS != 0
/** @brief Print basic module statistics (containing pool size usage).
*/
void bt_gatt_pool_stats_print(void);
#endif

#ifdef __cplusplus
}
#endif

/**
 * @}
 */

Functions

  1. Do not use @fn. Instead, document each function where it is defined.

  2. @brief is mandatory.

    • Start the brief with the “do sth” form.

      /** @brief Request a read operation to be executed from Secure Firmware.
      
      /** @brief Send Boot Keyboard Input Report.
      
  3. @details is optional. It can be introduced either by using @details or by leaving a blank line after @brief.

  4. @param should be used for every parameter.

    • Always add a parameter description. Use a sentence fragment (no verb) with period at the end.

    • Make sure the parameter documentation within the function is consistently using the parameter type: [in], [out], or [in,out].

      * @param[out] destination Pointer to destination array where the content is
      *                         to be copied.
      * @param[in]  addr        Address to be copied from.
      * @param[in]  len         Number of bytes to copy.
      
  5. If you include more than one @sa (“see also”, optional), add them this way:

    @sa first_function
    @sa second_function
    
  6. Do not user @returns, use return or retval instead.

    • `` @return`` should be used to describe a generic return value without a specific value (for example, “@return The length of …”, “@return The handle”). There is usually only one return value.

      *  @return  Initializer that sets up the pipe, length, and byte array for
      *           content of the TX data.
      
    • @retval should be used for specific return values (for example, “@retval true”, “@retval CONN_ERROR”). Describe the condition for each of the return values (for example, “If the function completes successfully”, “If the connection cannot be established”).

      *  @retval 0 If the operation was successful.
      *            Otherwise, a (negative) error code is returned.
      *  @retval (-ENOTSUP) Special error code used when the UUID
      *            of the service does not match the expected UUID.
      

Here is an example of a fully defined function:

/** @brief Request a random number from the Secure Firmware.
 *
 * This function provides a True Random Number from the on-board random number generator.
 *
 * @note Currently, the RNG hardware runs each time this function is called. This
 *       consumes significant time and power.
 *
 * @param[out] output  The random number. Must be at least @p len long.
 * @param[in]  len     The length of the output array. Currently, @p len must be
 *                     144.
 * @param[out] olen    The length of the random number provided.
 *
 * @retval 0        If the operation was successful.
 * @retval -EINVAL  If @p len is invalid. Currently, @p len must be 144.
 */
 int spm_request_random_number(uint8_t *output, size_t len, size_t *olen);

Enums

The documentation block should precede the documented element. This is in accordance with the Zephyr coding style.

/** HID Service Protocol Mode events. */
enum hids_pm_evt {

   /** Boot mode entered. */
   HIDS_PM_EVT_BOOT_MODE_ENTERED,

   /** Report mode entered. */
   HIDS_PM_EVT_REPORT_MODE_ENTERED,
 };

Structs

The documentation block should precede the documented element. This is in accordance with the Zephyr coding style. Make sure to add :members: when you include the API documentation in RST; otherwise, the member documentation will not show up.

/** @brief Event header structure.
 *
 * @warning When event structure is defined application event header must be placed
 *          as the first field.
 */
struct app_event_header {

        /** Linked list node used to chain events. */
   sys_dlist_t node;

        /** Pointer to the event type object. */
   const struct event_type *type_id;
};

Note

Always add a name for the struct. Avoid using unnamed structs due to Sphinx parser issue.

References

To link to functions, enums, or structs from within doxygen itself, use the @ref keyword.

/** @brief Event header structure.
 *  Use this structure with the function @ref function_name and
 *  this structure is related to another structure, @ref structure_name.
 */

Note

Linking to functions does not currently work due to Breathe issue #438.

Typedefs

The documentation block should precede the documented element. This is in accordance with the Zephyr coding style.

/**
 * @brief Download client asynchronous event handler.
 *
 * Through this callback, the application receives events, such as
 * download of a fragment, download completion, or errors.
 *
 * If the callback returns a non-zero value, the download stops.
 * To resume the download, use @ref download_client_start().
 *
 * @param[in] event   The event.
 *
 * @retval 0 The download continues.
 * @retval non-zero The download stops.
 */
 typedef int (*download_client_callback_t)(const struct download_client_evt *event);