-
Notifications
You must be signed in to change notification settings - Fork 8.2k
samples: drivers: gpio: custom DTS binding demo #25596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| cmake_minimum_required(VERSION 3.13.1) | ||
|
|
||
| find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) | ||
| project(gpio_custom_dts_binding) | ||
|
|
||
| target_sources(app PRIVATE src/main.c) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| .. _gpio-custom-dts-binding-sample: | ||
|
|
||
| GPIO with custom Devicetree binding | ||
| ################################### | ||
|
|
||
| Overview | ||
| ******** | ||
|
|
||
| In Zephyr, all hardware-specific configuration is described in the devicetree. | ||
|
|
||
| Consequently, also GPIO pins are configured in the devicetree and assigned to | ||
| a specific purpose using a compatible. | ||
|
|
||
| This is in contrast to other embedded environments like Arduino, where e.g. | ||
| the direction (input / output) of a GPIO pin is configured in the application | ||
| firmware. | ||
|
|
||
| For typical use cases like LEDs or buttons, the existing ``gpio-leds`` or | ||
| ``gpio-keys`` compatibles can be used. | ||
|
|
||
| This sample demonstrates how to use a GPIO pin for other purposes with a | ||
| custom dts binding. | ||
|
|
||
| We assume that a load with high current demands should be switched on or off | ||
| via a MOSFET. The custom DTS binding for the power output controlled via a | ||
| GPIO pin is specified in the file ``dts/bindings/power-output.yaml``. The gate | ||
| driver for the MOSFET would be connected to the pin as specified in the | ||
| ``.overlay`` file in the boards folder. | ||
|
|
||
| Building and Running | ||
| ******************** | ||
|
|
||
| For each board that should be supported, a ``.overlay`` file has to be defined | ||
| in the ``boards`` subfolder. | ||
|
|
||
| Building and Running for ST Nucleo L073RZ | ||
| ========================================= | ||
| The sample can be built and executed for the | ||
| :ref:`nucleo_l073rz_board` as follows: | ||
|
|
||
| .. zephyr-app-commands:: | ||
| :zephyr-app: samples/drivers/gpio/custom_dts_binding | ||
| :board: nucleo_l073rz | ||
| :goals: build flash | ||
| :compact: | ||
|
|
||
| For demonstration purposes, we use the GPIO pin of the built-in LED. | ||
|
|
||
| To build for another board, change "nucleo_l073rz" above to that board's name. | ||
|
|
||
| Sample output | ||
| ============= | ||
|
|
||
| The GPIO pin should be switched to active level after one second. | ||
|
|
||
| The following output is printed (pin number and port may differ): | ||
|
|
||
| .. code-block:: console | ||
|
|
||
| Initializing pin 5 on port GPIOA with inactive level. | ||
| Waiting one second. | ||
| Setting pin to active level. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright (c) 2020 Martin Jäger / Libre Solar | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| / { | ||
| load_switch: load_switch { | ||
| compatible = "power-output"; | ||
| /* using built-in LED pin for demonstration */ | ||
| gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>; | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Copyright (c) 2020 Martin Jäger / Libre Solar | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: GPIO pin to switch a power output on or off | ||
|
||
|
|
||
| compatible: "power-output" | ||
|
|
||
| properties: | ||
| gpios: | ||
| type: phandle-array | ||
| required: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| CONFIG_GPIO=y |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| sample: | ||
| name: GPIO driver sample with custom DTS binding | ||
| tests: | ||
| sample.drivers.gpio.custom_dts_binding: | ||
| tags: GPIO | ||
| platform_whitelist: nucleo_l073rz | ||
| depends_on: gpio | ||
| harness: console | ||
| harness_config: | ||
| type: multi_line | ||
| regex: | ||
| - "Initializing pin ([0-9]*) on port (.*) with inactive level." | ||
| - "Waiting one second." | ||
| - "Setting pin to active level." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright (c) 2020 Libre Solar Technologies GmbH | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <zephyr.h> | ||
| #include <sys/printk.h> | ||
| #include <drivers/gpio.h> | ||
|
|
||
| #define LOAD_SWITCH DT_NODELABEL(load_switch) | ||
|
|
||
| #if DT_NODE_EXISTS(LOAD_SWITCH) | ||
| #define LOAD_SWITCH_PORT DT_GPIO_LABEL(LOAD_SWITCH, gpios) | ||
| #define LOAD_SWITCH_PIN DT_GPIO_PIN(LOAD_SWITCH, gpios) | ||
| #define LOAD_SWITCH_FLAGS DT_GPIO_FLAGS(LOAD_SWITCH, gpios) | ||
| #else | ||
| #error "Overlay for power output node not properly defined." | ||
| #endif | ||
|
|
||
| void main(void) | ||
| { | ||
| struct device *switch_dev = device_get_binding(LOAD_SWITCH_PORT); | ||
|
|
||
| printk("Initializing pin %d on port %s with inactive level.\n", | ||
| LOAD_SWITCH_PIN, LOAD_SWITCH_PORT); | ||
|
|
||
| gpio_pin_configure(switch_dev, LOAD_SWITCH_PIN, | ||
| LOAD_SWITCH_FLAGS | GPIO_OUTPUT_INACTIVE); | ||
|
|
||
| printk("Waiting one second.\n"); | ||
|
|
||
| k_sleep(K_MSEC(1000)); | ||
|
|
||
| printk("Setting pin to active level.\n"); | ||
|
|
||
| gpio_pin_set(switch_dev, LOAD_SWITCH_PIN, 1); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this sample can be improved by not using the LED GPIO pin, as using the LED GPIO pin leaves out one important step: Having to setup the pinmux.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This statement depends on platform. On STM32, this is not needed (only required for PMW LED).
But I agree, ideally this sample should not require additional change.
EDIT: that said, having a LED to see the effect of the code is a benefit (more than the console print)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok, was not aware that the pinmux setup is necessary for other platforms. Can you suggest a board that requires pinmux setup and that one of you has available for testing? I can add an additional overlay for that. (assuming those other platforms handle pinmux via dts and not with the
pinmux.cfile as for STM32)Edit: I suggest to continue using the LED, but still add a (redundant) pinmux configuration to the overlay so that users don't forget it. And I can also mention it in the Readme.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting pinmux is currently only required on NXP platform and hopefully this will soon change. As part of the work on supporting pinmux configuration in DTS we want the driver to be responsible for configuring the pins. The same approach is used by Linux.
I propose to leave this part as is.