← Zephyr Documents build/dts/zephyr-user-node.html · build/dts/zephyr-user-node.rst 공식 원문 ↗

Zephyr 3.7.0 · Build · Devicetree

/zephyr,user node

Sample과 user application의 작은 build-time 설정을 담는 특수 node입니다.

Official pathbuild/dts/zephyr-user-node.html
Source filebuild/dts/zephyr-user-node.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

Part 1

요약·해설

단순 property, phandle 기반 device 선택, gpio_dt_spec 변환을 실제 code와 함께 설명합니다.

Binding

불필요

Values

숫자 · 배열 · 문자열

Devices

Phandle

GPIO

gpio_dt_spec

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 CCEFBD3044DE15E7D3EB521739FC4E37EED6A212E1EC660D3BF984D5000F4471
.. _dt-inferred-bindings:
.. _dt-zephyr-user:

The ``/zephyr,user`` node
#########################

Zephyr's devicetree scripts handle the ``/zephyr,user`` node as a special case:
you can put essentially arbitrary properties inside it and retrieve their
values without having to write a binding. It is meant as a convenient container
when only a few simple properties are needed.

.. note::

   This node is meant for sample code and user applications. It should not be
   used in the upstream Zephyr source code for device drivers, subsystems, etc.

Simple values
*************

You can store numeric or array values in ``/zephyr,user`` if you want them to
be configurable at build time via devicetree.

For example, with this devicetree overlay:

.. code-block:: devicetree

   / {
	zephyr,user {
		boolean;
		bytes = [81 82 83];
		number = <23>;
		numbers = <1>, <2>, <3>;
		string = "text";
		strings = "a", "b", "c";
	};
   };

You can get the above property values in C/C++ code like this:

.. code-block:: C

   #define ZEPHYR_USER_NODE DT_PATH(zephyr_user)

   DT_PROP(ZEPHYR_USER_NODE, boolean) // 1
   DT_PROP(ZEPHYR_USER_NODE, bytes)   // {0x81, 0x82, 0x83}
   DT_PROP(ZEPHYR_USER_NODE, number)  // 23
   DT_PROP(ZEPHYR_USER_NODE, numbers) // {1, 2, 3}
   DT_PROP(ZEPHYR_USER_NODE, string)  // "text"
   DT_PROP(ZEPHYR_USER_NODE, strings) // {"a", "b", "c"}

Devices
*******

You can store :ref:`phandles <dt-phandles>` in ``/zephyr,user`` if you want to
be able to reconfigure which devices your application uses in simple cases
using devicetree overlays.

For example, with this devicetree overlay:

.. code-block:: devicetree

   / {
	zephyr,user {
		handle = <&gpio0>;
		handles = <&gpio0>, <&gpio1>;
        };
   };

You can convert the phandles in the ``handle`` and ``handles`` properties to
device pointers like this:

.. code-block:: C

   /*
    * Same thing as:
    *
    * ... my_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));
    */
   const struct device *my_device =
   	DEVICE_DT_GET(DT_PROP(ZEPHYR_USER_NODE, handle));

   #define PHANDLE_TO_DEVICE(node_id, prop, idx) \
        DEVICE_DT_GET(DT_PHANDLE_BY_IDX(node_id, prop, idx)),

   /*
    * Same thing as:
    *
    * ... *my_devices[] = {
    *         DEVICE_DT_GET(DT_NODELABEL(gpio0)),
    *         DEVICE_DT_GET(DT_NODELABEL(gpio1)),
    * };
    */
   const struct device *my_devices[] = {
   	DT_FOREACH_PROP_ELEM(ZEPHYR_USER_NODE, handles, PHANDLE_TO_DEVICE)
   };

GPIOs
*****

The ``/zephyr,user`` node is a convenient place to store application-specific
GPIOs that you want to be able to reconfigure with a devicetree overlay.

For example, with this devicetree overlay:

.. code-block:: devicetree

   #include <zephyr/dt-bindings/gpio/gpio.h>

   / {
	zephyr,user {
		signal-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
        };
   };

You can convert the pin defined in ``signal-gpios`` to a ``struct
gpio_dt_spec`` in your source code, then use it like this:

.. code-block:: C

   #include <zephyr/drivers/gpio.h>

   #define ZEPHYR_USER_NODE DT_PATH(zephyr_user)

   const struct gpio_dt_spec signal =
           GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, signal_gpios);

   /* Configure the pin */
   gpio_pin_configure_dt(&signal, GPIO_OUTPUT_INACTIVE);

   /* Set the pin to its active level */
   gpio_pin_set_dt(&signal, 1);

(See :c:struct:`gpio_dt_spec`, :c:macro:`GPIO_DT_SPEC_GET`, and
:c:func:`gpio_pin_configure_dt` for details on these APIs.)

Part 3

한국어 전문 번역

/zephyr,user node

Zephyr의 devicetree script는 /zephyr,user를 특별 취급합니다. Binding을 작성하지 않아도 거의 임의의 property를 넣고 값을 가져올 수 있어 간단한 property 몇 개가 필요한 application에 편리합니다.

단순 값

Build 시 devicetree로 바꾸고 싶은 numeric 또는 array 값을 저장할 수 있습니다. 다음 overlay는 boolean, byte array, number, number array, string, string array를 정의합니다.

/ {

C/C++에서는 DT_PATH(zephyr_user)로 node identifier를 만들고 DT_PROP으로 값을 읽습니다.

#define ZEPHYR_USER_NODE DT_PATH(zephyr_user)

DT_PROP(ZEPHYR_USER_NODE, boolean) // 1
DT_PROP(ZEPHYR_USER_NODE, bytes)   // {0x81, 0x82, 0x83}
DT_PROP(ZEPHYR_USER_NODE, number)  // 23
DT_PROP(ZEPHYR_USER_NODE, numbers) // {1, 2, 3}
DT_PROP(ZEPHYR_USER_NODE, string)  // "text"
DT_PROP(ZEPHYR_USER_NODE, strings) // {"a", "b", "c"}

Device 선택

간단한 application에서 어떤 device를 쓸지 overlay로 바꾸고 싶다면 phandle을 저장할 수 있습니다.

/ {

handle의 phandle 하나와 handles의 여러 phandle을 device pointer로 바꾸는 방법은 다음과 같습니다.

/*
 * Same thing as:
 *
 * ... my_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));
 */
const struct device *my_device =
	DEVICE_DT_GET(DT_PROP(ZEPHYR_USER_NODE, handle));

#define PHANDLE_TO_DEVICE(node_id, prop, idx) \
     DEVICE_DT_GET(DT_PHANDLE_BY_IDX(node_id, prop, idx)),

/*
 * Same thing as:
 *
 * ... *my_devices[] = {
 *         DEVICE_DT_GET(DT_NODELABEL(gpio0)),
 *         DEVICE_DT_GET(DT_NODELABEL(gpio1)),
 * };
 */
const struct device *my_devices[] = {
	DT_FOREACH_PROP_ELEM(ZEPHYR_USER_NODE, handles, PHANDLE_TO_DEVICE)
};

DEVICE_DT_GET은 phandle이 가리키는 node의 device pointer를 만들고, DT_FOREACH_PROP_ELEM은 property 배열의 모든 element에 변환 macro를 적용합니다.

Application 전용 GPIO

Overlay로 다시 배치할 application-specific GPIO도 /zephyr,user에 둘 수 있습니다.

#include <zephyr/dt-bindings/gpio/gpio.h>

/ {

Source에서는 signal-gpiosstruct gpio_dt_spec으로 변환한 뒤 GPIO API에 전달합니다.

#include <zephyr/drivers/gpio.h>

#define ZEPHYR_USER_NODE DT_PATH(zephyr_user)

const struct gpio_dt_spec signal =
        GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, signal_gpios);

/* Configure the pin */
gpio_pin_configure_dt(&signal, GPIO_OUTPUT_INACTIVE);

/* Set the pin to its active level */
gpio_pin_set_dt(&signal, 1);

gpio_pin_configure_dt는 pin을 inactive output으로 설정하고, gpio_pin_set_dt(..., 1)은 devicetree flag가 정의한 active level로 출력합니다. 자세한 형식은 gpio_dt_spec, GPIO_DT_SPEC_GET, gpio_pin_configure_dt API를 참고하십시오.

Source

출처

원문 파일의 단락, directive, 표, 코드, symbol, 경로는 영어 원문 영역에 그대로 보존했습니다.