Skip to content

Commit 695d659

Browse files
committed
doc: devicetree: edits to driving gpio guide
Edited the Driving the GPIO pins directly guide for style. Fixed some style issues in the nordic,gpio-pins.yaml. Added a generic page about adding drivers. NCSDK-24813. Signed-off-by: Grzegorz Ferenc <Grzegorz.Ferenc@nordicsemi.no>
1 parent 852345e commit 695d659

File tree

5 files changed

+148
-24
lines changed

5 files changed

+148
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
.. _add_new_driver:
2+
3+
Adding new drivers
4+
##################
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
Adding drivers to your embedded project allows you to add support for additional hardware components and extend functionalities of your application.
11+
12+
To add a new driver to an application in the |NCS|, complete the following steps:
13+
14+
.. rst-class:: numbered-step
15+
16+
Create a devicetree binding
17+
***************************
18+
19+
In most of the cases you want to make it possible to configure some properties of your driver instance.
20+
For example, a sensor may need to allow configuring used GPIO pins or communication interface.
21+
22+
A driver instance can be made configurable through a devicetree node that is compatible with a specific binding.
23+
The devicetree bindings provide structure for the devicetree by declaring requirements for the content of devicetree nodes.
24+
The :ref:`compatible <zephyr:dt-bindings-compatible>` property defines compatibility of a devicetree node with a devicetree binding.
25+
26+
You can create a devicetree binding YAML file for your driver in the :file:`dts/bindings` directory of your project.
27+
If applicable, you can also use one of the existing DTS bindings available in the |NCS| or Zephyr.
28+
For implementation examples, see :file:`nrf/dts/bindings` and :file:`zephyr/dts/bindings` directories.
29+
See also :ref:`zephyr:devicetree` documentation for more information about devicetree.
30+
31+
For more information about devicetree bindings, read the :ref:`documentation about them in Zephyr <zephyr:dt-binding-compat>`.
32+
33+
.. rst-class:: numbered-step
34+
35+
Create the driver files
36+
***********************
37+
38+
First, create the files that will implement driver and expose driver APIs.
39+
As a reference, you can check the |NCS| :ref:`drivers`, whose code is located at :file:`nrf/drivers`.
40+
41+
You will need the following files:
42+
43+
* :file:`CMakeLists.txt` - This file adds your driver sources to the build as a Zephyr library.
44+
See :ref:`app_build_system` for more information about the build system.
45+
* :file:`Kconfig` - This file will include all custom Kconfig options for your driver, including the Kconfig option to enable and disable the driver.
46+
See Zephyr's :ref:`zephyr:kconfig` for more information.
47+
* Driver's source files - These files will include the code for your driver, such as :ref:`zephyr:device_struct`, device definition through the :c:macro:`DEVICE_DT_DEFINE` macro, and other elements.
48+
If possible, your driver should expose a generic API to simplify integrating the driver in user application.
49+
For example, the driver could expose sensor driver API (:c:struct:`sensor_driver_api`).
50+
See :file:`nrf/drivers/sensor/paw3212/paw3212.c` for an example.
51+
See also Zephyr's :ref:`zephyr:device_model_api` for more information, in particular the "Subsystems and API structures" section.
52+
53+
.. rst-class:: numbered-step
54+
55+
Include the driver in your application
56+
**************************************
57+
58+
To enable the driver in your application's configuration, complete the following steps:
59+
60+
1. Enable the necessary Kconfig options in the application's :file:`prj.conf` file.
61+
This includes the Kconfig option you defined for enabling and disabling the driver.
62+
See :ref:`configuring_kconfig` for information about how to enable Kconfig options.
63+
#. Create or modify a devicetree overlay file for your board to add the necessary devicetree node for your custom driver.
64+
This step is crucial for connecting the driver to the specific hardware on your board.
65+
For information about how to create or modify devicetree files, see :ref:`configuring_devicetree`.
66+
#. Include the appropriate header file for your custom driver in your application code and use the driver APIs in the application.
67+
If your driver exposes a generic API (for example, :ref:`sensor driver API <zephyr:sensor>`), you can use generic headers defined for the API.
68+
69+
DevAcademy courses
70+
******************
71+
72+
`Nordic Developer Academy`_ contains introductory courses to the |NCS| and Zephyr.
73+
See the following course lessons to get started with driver development:
74+
75+
* `Lesson 6 – Serial communication (I2C)`_ in `nRF Connect SDK Fundamentals course`_ describes how to communicate with a sensor connected over I2C using I2C APIs.
76+
* `Lesson 5 – Serial Peripheral Interface (SPI)`_ in `nRF Connect SDK Intermediate course`_ describes how to communicate with sensors over SPI in Zephyr.
77+
* `Lesson 7 - Device driver model`_ in `nRF Connect SDK Intermediate course`_ describes how to start with adding your own sensor driver in the Exercise 1.
78+
79+
Related documentation
80+
*********************
81+
82+
The :ref:`nrf_desktop` application describes how to :ref:`add a new motion sensor to the project <porting_guide_adding_sensor>`.
83+
84+
Implementation examples
85+
***********************
86+
87+
Check the driver implementation examples at the following paths:
88+
89+
* |NCS|: :ref:`drivers`, with code located at :file:`nrf/drivers`.
90+
* Zephyr: :ref:`zephyr:driver-samples`, with code located at :file:`zephyr/samples/drivers`.

doc/nrf/app_dev/config_and_build/hardware/index.rst

+5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ The following guides provide information about configuring specific aspects of h
1919
Read them together with Zephyr's :ref:`zephyr:hardware_support` and :ref:`zephyr:dt-guide` guides, and the official `Devicetree Specification`_.
2020
In particular, :ref:`zephyr:set-devicetree-overlays` explains how the base devicetree files are selected.
2121

22+
.. note::
23+
If you want to go through dedicated training related to some of the topics covered here, enroll in the courses in the `Nordic Developer Academy`_.
24+
For example, `nRF Connect SDK Fundamentals course`_ describes devicetree in `Lesson 2 <Lesson 2 - Reading buttons and controlling LEDs_>`_.
25+
2226
.. toctree::
2327
:maxdepth: 1
2428
:caption: Subpages:
2529

30+
add_new_driver
2631
pin_control
2732
use_gpio_pin_directly

doc/nrf/app_dev/config_and_build/hardware/use_gpio_pin_directly.rst

+43-18
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,36 @@ Driving a GPIO pin directly
77
:local:
88
:depth: 2
99

10-
This user guide shows how to drive a GPIO pin directly.
11-
In the development phase of an embedded project, it can be used to collect execution timings or as a debugging tool.
10+
This user guide shows how to control a GPIO pin directly using GPIO driver APIs.
11+
In the development phase of an embedded project, you can use this configuration to set the pin state using software, without any intermediary circuitry or additional hardware components.
12+
You can use this solution to collect execution timings, or as a debugging tool.
1213

13-
Declaring the GPIO pin
14-
**********************
14+
The following configuration can be copied manually into the devicetree overlay files.
15+
You can also use the `Devicetree Visual Editor <How to work with Devicetree Visual Editor_>`_ (either in GUI or text mode) in the |nRFVSC| to apply it.
1516

16-
To declare a GPIO pin node, use a DTS node with ``nordic,gpio-pins`` compatible set in the :term:`devicetree <Devicetree>`.
17-
If your application does not use the same GPIO instance as used by the node, enable the GPIO port instance required by the GPIO driver.
17+
.. rst-class:: numbered-step
1818

19-
This DTS content can be introduced in the DTS of your board, application, or overlay file.
20-
Pins defined this way can be accessed using devicetree macros.
19+
Define the compatible DTS node
20+
******************************
2121

22-
The following snippet shows the declaration of a GPIO pin node for **Pin 2** of **GPIO0**.
23-
The node is labeled as ``user-dbg-pin``.
24-
Additionally, the ``gpio0`` instance and ``gpiote0`` are enabled, so the GPIO driver can be built and executed.
22+
You need to define a new GPIO pin devicetree node for the pin.
23+
The devicetree node needs to be compatible with the GPIO devicetree binding.
24+
GPIO APIs use the devicetree node to refer to the specified GPIO pin.
25+
26+
If you work with a Nordic Semiconductor device, you can use the binding definition in :file:`dts/bindings/gpio`.
27+
28+
See Zephyr's documentation about :ref:`zephyr:devicetree` and :ref:`zephyr:gpio_api` for more information.
29+
30+
.. rst-class:: numbered-step
31+
32+
Configure the GPIO pin through DTS
33+
**********************************
34+
35+
To declare a GPIO pin node, create a DTS node in the DTS of your board or application's DTS overlay.
36+
Pins defined this way can be accessed using :ref:`GPIO devicetree APIs<zephyr:gpio_api>`.
37+
38+
Make sure that GPIO ports used by the GPIO pins are enabled.
39+
The following snippet shows the declaration of a GPIO pin node called ``user_dbg_pin``:
2540

2641
.. code-block:: c
2742
@@ -41,8 +56,18 @@ Additionally, the ``gpio0`` instance and ``gpiote0`` are enabled, so the GPIO dr
4156
status = "okay";
4257
};
4358
44-
Using the pin in your application
45-
*********************************
59+
In this snippet:
60+
61+
* The node is labelled as ``user-dbg-pin``.
62+
* ``compatible`` specifies the binding definition to be used (``nordic,gpio-pins`` from :file:`dts/bindings/gpio`).
63+
* ``gpios`` specifies which pin node is being used (**Pin 2** of **GPIO0**).
64+
* ``status`` enables the pin node when it is set to ``"okay"``.
65+
* Additionally, the ``gpio0`` instance and ``gpiote0`` are enabled, so the GPIO driver can be built and executed.
66+
67+
.. rst-class:: numbered-step
68+
69+
Include the pin in your application
70+
***********************************
4671

4772
To let your application access the declared pins, use the following devicetree macros:
4873

@@ -54,15 +79,15 @@ To let your application access the declared pins, use the following devicetree m
5479
static const struct gpio_dt_spec pin_dbg =
5580
GPIO_DT_SPEC_GET_OR(DT_NODELABEL(user_dbg_pin), gpios, {0});
5681
57-
#. Make sure your application initializes the pin:
82+
#. Make sure your application initializes the pin using the :c:func:`gpio_pin_configure_dt` function:
5883

5984
.. code-block:: c
6085
6186
if (pin_dbg.port) {
6287
gpio_pin_configure_dt(&pin_dbg, GPIO_OUTPUT_INACTIVE);
6388
}
6489
65-
#. To let your application drive the pin, add the following code where needed:
90+
#. Add the following code where needed to let your application drive the pin using the :c:func:`gpio_pin_set_dt` function:
6691

6792
.. code-block:: c
6893
@@ -83,7 +108,8 @@ This is an easy way to disable this debugging infrastructure.
83108
Declaring multiple pins
84109
***********************
85110

86-
Multiple pins can be declared in one GPIO property as well.
111+
You can also declare multiple pins in one GPIO property.
112+
See the following code snippet:
87113

88114
.. code-block:: c
89115
@@ -96,8 +122,7 @@ Multiple pins can be declared in one GPIO property as well.
96122
};
97123
};
98124
99-
To initialize the defined GPIO pin structures, use the ``GPIO_DT_SPEC_INST_GET_BY_IDX_OR()`` macro.
100-
125+
To initialize the defined GPIO pin structures, use the :c:macro:`GPIO_DT_SPEC_INST_GET_BY_IDX_OR` macro:
101126

102127
.. code-block:: c
103128

doc/nrf/links.txt

+4
Original file line numberDiff line numberDiff line change
@@ -908,9 +908,13 @@
908908

909909
.. _`nRF Connect SDK Fundamentals course`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/
910910
.. _`Installing nRF Connect SDK and VS Code`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-1-nrf-connect-sdk-introduction/topic/exercise-1-1/
911+
.. _`Lesson 2 - Reading buttons and controlling LEDs`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-2-reading-buttons-and-controlling-leds/
912+
.. _`Lesson 6 – Serial communication (I2C)`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-6-serial-com-i2c/topic/i2c-driver/
911913

912914
.. _`nRF Connect SDK Intermediate course`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/
913915
.. _`Lesson 2 - Debugging and troubleshooting`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-2-debugging/
916+
.. _`Lesson 5 – Serial Peripheral Interface (SPI)`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-5-serial-peripheral-interface-spi/
917+
.. _`Lesson 7 - Device driver model`: https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate/lessons/lesson-7-device-driver-model/
914918

915919
.. _`Cellular IoT Fundamentals course`: https://academy.nordicsemi.com/courses/cellular-iot-fundamentals/
916920
.. _`Power saving techniques`: https://academy.nordicsemi.com/courses/cellular-iot-fundamentals/lessons/lesson-1-cellular-fundamentals/topic/lesson-1-power-saving-techniques/

dts/bindings/gpio/nordic,gpio-pins.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
33

44
description: |
5-
This allows to define the pin of the GPIO to be owned by the domain.
6-
It does not provide any specyfic driver.
7-
Its function is to allow us to use two functionalities:
5+
This binding allows to assign a GPIO to the domain.
6+
It does not provide any specific driver.
7+
The binding enables the following functionalities:
88
9-
1. Generate the right UICR marking the domain to own the selected pin.
10-
2. Access to pins by the macros from zephyr/devicetree/gpio.h file
11-
increasing the hardware abstraction this way.
9+
- Add an entry in UICR that marks the domain to own the selected pin.
10+
- Allow access to pins using the macros from the `zephyr/devicetree/gpio.h` file,
11+
which increases the hardware abstraction.
1212
1313
compatible: "nordic,gpio-pins"
1414

0 commit comments

Comments
 (0)