Skip to content

Commit 41d292e

Browse files
committed
samples: drivers: gpio: custom DTS binding demo
This sample shows how to define a custom devicetree binding to use GPIO pins for a specific purpose. Signed-off-by: Martin Jäger <[email protected]>
1 parent 150e18d commit 41d292e

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
5+
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
6+
project(gpio_custom_dts_binding)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. _gpio-custom-dts-binding-sample:
2+
3+
GPIO with custom Devicetree binding
4+
###################################
5+
6+
Overview
7+
********
8+
9+
In Zephyr, all hardware-specific configuration is described in the devicetree.
10+
11+
Consequently, also GPIO pins are configured in the devicetree and assigned to
12+
a specific purpose using a compatible.
13+
14+
This is in contrast to other embedded environments like Arduino, where e.g.
15+
the direction (input / output) of a GPIO pin is configured in the application
16+
firmware.
17+
18+
For typical use cases like LEDs or buttons, the existing ``gpio-leds`` or
19+
``gpio-keys`` compatibles can be used.
20+
21+
This sample demonstrates how to use a GPIO pin for other purposes with a
22+
custom dts binding.
23+
24+
We assume that a load with high current demands should be switched on or off
25+
via a MOSFET. The custom DTS binding for the power output controlled via a
26+
GPIO pin is specified in the file ``dts/bindings/power-output.yaml``. The gate
27+
driver for the MOSFET would be connected to the pin as specified in the
28+
``.overlay`` file in the boards folder.
29+
30+
Building and Running
31+
********************
32+
33+
For each board that should be supported, a ``.overlay`` file has to be defined
34+
in the ``boards`` subfolder.
35+
36+
Building and Running for ST Nucleo L073RZ
37+
=========================================
38+
The sample can be built and executed for the
39+
:ref:`nucleo_l073rz_board` as follows:
40+
41+
.. zephyr-app-commands::
42+
:zephyr-app: samples/drivers/gpio/custom_dts_binding
43+
:board: nucleo_l073rz
44+
:goals: build flash
45+
:compact:
46+
47+
For demonstration purposes, we use the GPIO pin of the built-in LED.
48+
49+
To build for another board, change "nucleo_l073rz" above to that board's name.
50+
51+
Sample output
52+
=============
53+
54+
The GPIO pin should be switched to active level after one second.
55+
56+
The following output is printed (pin number and port may differ):
57+
58+
.. code-block:: console
59+
60+
Initializing pin 5 on port GPIOA with inactive level.
61+
Waiting one second.
62+
Setting pin to active level.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2020 Martin Jäger / Libre Solar
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
load_switch: load_switch {
9+
compatible = "power-output";
10+
/* using built-in LED pin for demonstration */
11+
gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
12+
};
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2020 Martin Jäger / Libre Solar
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: GPIO pin to switch a power output on or off
5+
6+
compatible: "power-output"
7+
8+
properties:
9+
gpios:
10+
type: phandle-array
11+
required: true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_GPIO=y
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
name: GPIO driver sample with custom DTS binding
3+
tests:
4+
sample.drivers.gpio.custom_dts_binding:
5+
tags: GPIO
6+
platform_whitelist: nucleo_l073rz
7+
depends_on: gpio
8+
harness: console
9+
harness_config:
10+
type: multi_line
11+
regex:
12+
- "Initializing pin ([0-9]*) on port (.*) with inactive level."
13+
- "Waiting one second."
14+
- "Setting pin to active level."
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2020 Libre Solar Technologies GmbH
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <sys/printk.h>
9+
#include <drivers/gpio.h>
10+
11+
#define LOAD_SWITCH DT_NODELABEL(load_switch)
12+
13+
#if DT_NODE_EXISTS(LOAD_SWITCH)
14+
#define LOAD_SWITCH_PORT DT_GPIO_LABEL(LOAD_SWITCH, gpios)
15+
#define LOAD_SWITCH_PIN DT_GPIO_PIN(LOAD_SWITCH, gpios)
16+
#define LOAD_SWITCH_FLAGS DT_GPIO_FLAGS(LOAD_SWITCH, gpios)
17+
#else
18+
#error "Overlay for power output node not properly defined."
19+
#endif
20+
21+
void main(void)
22+
{
23+
struct device *switch_dev = device_get_binding(LOAD_SWITCH_PORT);
24+
25+
printk("Initializing pin %d on port %s with inactive level.\n",
26+
LOAD_SWITCH_PIN, LOAD_SWITCH_PORT);
27+
28+
gpio_pin_configure(switch_dev, LOAD_SWITCH_PIN,
29+
LOAD_SWITCH_FLAGS | GPIO_OUTPUT_INACTIVE);
30+
31+
printk("Waiting one second.\n");
32+
33+
k_sleep(K_MSEC(1000));
34+
35+
printk("Setting pin to active level.\n");
36+
37+
gpio_pin_set(switch_dev, LOAD_SWITCH_PIN, 1);
38+
}

0 commit comments

Comments
 (0)