You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
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`.
Copy file name to clipboardexpand all lines: doc/nrf/app_dev/config_and_build/hardware/use_gpio_pin_directly.rst
+43-18
Original file line number
Diff line number
Diff line change
@@ -7,21 +7,36 @@ Driving a GPIO pin directly
7
7
:local:
8
8
:depth: 2
9
9
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.
12
13
13
-
Declaring the GPIO pin
14
-
**********************
14
+
You can copy the following configuration snippets 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 them.
15
16
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
18
18
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
+
******************************
21
21
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 the :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``:
25
40
26
41
.. code-block:: c
27
42
@@ -41,8 +56,18 @@ Additionally, the ``gpio0`` instance and ``gpiote0`` are enabled, so the GPIO dr
41
56
status = "okay";
42
57
};
43
58
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`` and ``gpiote0`` instances 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
+
***********************************
46
71
47
72
To let your application access the declared pins, use the following devicetree macros:
48
73
@@ -54,15 +79,15 @@ To let your application access the declared pins, use the following devicetree m
.. _`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/
0 commit comments