Adding new drivers
Adding drivers to your embedded project allows you to add support for additional hardware components and extend functionalities of your application.
To add a new driver to an application in the nRF Connect SDK, complete the following steps:
Create a devicetree binding
In most of the cases you want to make it possible to configure some properties of your driver instance. For example, a sensor may need to allow configuring used GPIO pins or communication interface.
A driver instance can be made configurable through a devicetree node that is compatible with a specific binding.
The devicetree bindings provide the structure for the content of the devicetree nodes. The compatible property defines the compatibility of a devicetree node with a devicetree binding. For more information, read the documentation about devicetree bindings in Zephyr.
You can create a devicetree binding YAML file for your driver in the dts/bindings directory of your project.
If applicable, you can also use one of the existing DTS bindings available in the nRF Connect SDK or Zephyr.
For implementation examples, see nrf/dts/bindings and zephyr/dts/bindings directories.
See also Devicetree documentation for more information about devicetree.
Create the driver files
First, create the files that will implement driver and expose driver APIs.
As a reference, you can check the nRF Connect SDK Drivers, whose code is located at nrf/drivers.
You will need the following files:
CMakeLists.txt- This file adds your driver sources to the build as a Zephyr library. See Build and configuration system for more information about the build system.Kconfig- This file will include all custom Kconfig options for your driver, including the Kconfig option to enable and disable the driver. See Zephyr’s Configuration System (Kconfig) for more information.Driver’s source files - These files will include the code for your driver, such as Driver Data Structures device definition through the
DEVICE_DT_DEFINEmacro, and other elements. If possible, your driver should expose a generic API to simplify integrating the driver in user application. For example, the driver could expose sensor driver API (sensor_driver_api). Seenrf/drivers/sensor/paw3212/paw3212.cfor an example. See also Zephyr’s Device Driver Model for more information, in particular the “Subsystems and API structures” section.
Enable the driver in your application
To enable the driver in your application’s configuration, complete the following steps:
Enable the necessary Kconfig options in the application’s Kconfig configuration. This includes the Kconfig option you defined for enabling and disabling the driver. See Configuring Kconfig for information about how to enable Kconfig options.
Create or modify a devicetree overlay file for your board to add the necessary devicetree node for your custom driver. This step is crucial for connecting the driver to the specific hardware on your board. For information about how to create or modify devicetree files, see Configuring devicetree.
Include the appropriate header file for your custom driver in your application code and use the driver APIs in the application. If your driver exposes a generic API (for example, sensor driver API), you can use generic headers defined for the API.
DevAcademy courses
Nordic Developer Academy contains introductory courses to the nRF Connect SDK and Zephyr. See the following course lessons to get started with driver development:
Lesson 6 - Serial communication (I2C) in nRF Connect SDK Fundamentals course describes how to communicate with a sensor connected over I2C using I2C APIs.
Lesson 5 - Serial Peripheral Interface (SPI) in nRF Connect SDK Intermediate course describes how to communicate with sensors over SPI in Zephyr.
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.
Implementation examples
Check the driver implementation examples at the following paths: