zephyr/samples/basic/custom_dts_binding
Keith Packard 0b90fd5adf samples, tests, boards: Switch main return type from void to int
As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.

Most of these changes were automated using coccinelle with the following
script:

@@
@@
- void
+ int
main(...) {
	...
-	return;
+	return 0;
	...
}

Approximately 40 files had to be edited by hand as coccinelle was unable to
fix them.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-14 07:49:41 +09:00
..
boards
dts/bindings
src
CMakeLists.txt
README.rst
prj.conf
sample.yaml

README.rst

.. _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 :dtcompatible:`gpio-leds` or
:dtcompatible:`gpio-keys` compatibles can be used.

This sample demonstrates how to use a GPIO pin for other purposes with a custom devicetree binding.

We assume that a load with high current demands should be switched on or off via a MOSFET. The
custom devicetree binding for the power output controlled via a GPIO pin is specified in the file
:zephyr_file:`samples/basic/custom_dts_binding/dts/bindings/power-switch.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.

Afterwards, the sample can be built and executed for the ``<board>`` as follows:

.. zephyr-app-commands::
   :zephyr-app: samples/basic/custom_dts_binding
   :board: <board>
   :goals: build flash
   :compact:

For demonstration purposes, some boards use the GPIO pin of the built-in LED.

Sample output
=============

The GPIO pin should be switched to active level after one second.

The following output is printed:

.. code-block:: console

   Initializing pin with inactive level.
   Waiting one second.
   Setting pin to active level.