Part 1
요약·해설
sw0·선택적 led0 alias, gpio-keys binding, pin/flags cell과 nucleo_f103rb build를 설명합니다.
sw0 · gpio-keys
Falling edge
led0 feedback
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
852815A015EA54AEDAA2AD1CA69193CE16B0044027D5B420B3C4064B24BBAA15
.. zephyr:code-sample:: button
:name: Button
:relevant-api: gpio_interface
Handle GPIO inputs with interrupts.
Overview
********
A simple button demo showcasing the use of GPIO input with interrupts.
The sample prints a message to the console each time a button is pressed.
Requirements
************
The board hardware must have a push button connected via a GPIO pin. These are
called "User buttons" on many of Zephyr's :ref:`boards`.
The button must be configured using the ``sw0`` :ref:`devicetree <dt-guide>`
alias, usually in the :ref:`BOARD.dts file <devicetree-in-out-files>`. You will
see this error if you try to build this sample for an unsupported board:
.. code-block:: none
Unsupported board: sw0 devicetree alias is not defined
You may see additional build errors if the ``sw0`` alias exists, but is not
properly defined.
The sample additionally supports an optional ``led0`` devicetree alias. This is
the same alias used by the :zephyr:code-sample:`blinky` sample. If this is provided, the LED
will be turned on when the button is pressed, and turned off off when it is
released.
Devicetree details
==================
This section provides more details on devicetree configuration.
Here is a minimal devicetree fragment which supports this sample. This only
includes a ``sw0`` alias; the optional ``led0`` alias is left out for
simplicity.
.. code-block:: devicetree
/ {
aliases {
sw0 = &button0;
};
soc {
gpio0: gpio@0 {
status = "okay";
gpio-controller;
#gpio-cells = <2>;
/* ... */
};
};
buttons {
compatible = "gpio-keys";
button0: button_0 {
gpios = < &gpio0 PIN FLAGS >;
label = "User button";
};
/* ... other buttons ... */
};
};
As shown, the ``sw0`` devicetree alias must point to a child node of a node
with a "gpio-keys" :ref:`compatible <dt-important-props>`.
The above situation is for the common case where:
- ``gpio0`` is an example node label referring to a GPIO controller
- ``PIN`` should be a pin number, like ``8`` or ``0``
- ``FLAGS`` should be a logical OR of :ref:`GPIO configuration flags <gpio_api>`
meant to apply to the button, such as ``(GPIO_PULL_UP | GPIO_ACTIVE_LOW)``
This assumes the common case, where ``#gpio-cells = <2>`` in the ``gpio0``
node, and that the GPIO controller's devicetree binding names those two cells
"pin" and "flags" like so:
.. code-block:: yaml
gpio-cells:
- pin
- flags
This sample requires a ``pin`` cell in the ``gpios`` property. The ``flags``
cell is optional, however, and the sample still works if the GPIO cells
do not contain ``flags``.
Building and Running
********************
This sample can be built for multiple boards, in this example we will build it
for the nucleo_f103rb board:
.. zephyr-app-commands::
:zephyr-app: samples/basic/button
:board: nucleo_f103rb
:goals: build
:compact:
After startup, the program looks up a predefined GPIO device, and configures the
pin in input mode, enabling interrupt generation on falling edge. During each
iteration of the main loop, the state of GPIO line is monitored and printed to
the serial console. When the input button gets pressed, the interrupt handler
will print an information about this event along with its timestamp.
Part 3
한국어 전문 번역
개요
GPIO input과 interrupt 사용법을 보여주는 간단한 button demo입니다. Button을 누를 때마다 console에 message를 출력합니다.
요구사항
Board hardware에는 GPIO pin으로 연결된 push button이 있어야 합니다. 많은 Zephyr board에서는 이를 User button이라고 부릅니다.
Button은 일반적으로 BOARD.dts file에서 sw0 Devicetree alias로 configuration해야 합니다. 지원하지 않는 board용으로 build하면 다음 오류가 나타납니다.
Unsupported board: sw0 devicetree alias is not defined
sw0 alias가 있지만 올바르게 정의되지 않은 경우 추가 build error가 발생할 수 있습니다.
Sample은 선택적인 led0 Devicetree alias도 지원합니다. Blinky sample과 같은 alias입니다. 제공하면 button을 누를 때 LED가 켜지고 놓을 때 꺼집니다.
Devicetree 세부사항
다음은 이 sample을 지원하는 최소 Devicetree fragment입니다. 단순화를 위해 sw0 alias만 포함하고 선택적인 led0 alias는 생략했습니다.
/ {
aliases {
sw0 = &button0;
};
soc {
gpio0: gpio@0 {
status = "okay";
gpio-controller;
#gpio-cells = <2>;
/* ... */
};
};
buttons {
compatible = "gpio-keys";
button0: button_0 {
gpios = < &gpio0 PIN FLAGS >;
label = "User button";
};
/* ... other buttons ... */
};
};
sw0 Devicetree alias는 gpio-keys compatible node의 child node를 가리켜야 합니다.
위 예제는 다음과 같은 일반적인 경우를 가정합니다.
gpio0는 GPIO controller를 가리키는 example node label입니다.PIN은8또는0같은 pin 번호입니다.FLAGS는 button에 적용할 GPIO configuration flag를 logical OR한 값입니다. 예:(GPIO_PULL_UP | GPIO_ACTIVE_LOW)
또한 gpio0 node에 #gpio-cells = <2>가 있고 GPIO controller의 Devicetree binding이 두 cell을 다음과 같이 pin과 flags로 이름 붙인 일반적인 경우를 가정합니다.
gpio-cells:
- pin
- flags
이 sample은 gpios property에 pin cell이 있어야 합니다. flags cell은 선택 사항이며 GPIO cell에 flags가 없어도 sample은 동작합니다.
Build와 실행
여러 board용으로 build할 수 있으며, 이 example에서는 nucleo_f103rb board용으로 build합니다.
west build -b nucleo_f103rb samples/basic/button
시작 후 program은 미리 정의된 GPIO device를 찾고 pin을 input mode로 설정하며 falling edge에서 interrupt가 발생하도록 합니다. Main loop가 반복될 때마다 GPIO line 상태를 감시해 serial console에 출력합니다. Input button을 누르면 interrupt handler가 event 정보와 timestamp를 출력합니다.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.