2017-04-05 03:20:40 +08:00
|
|
|
.. _button-sample:
|
|
|
|
|
2017-01-06 23:17:49 +08:00
|
|
|
Button demo
|
|
|
|
###########
|
|
|
|
|
|
|
|
Overview
|
2017-01-21 07:58:05 +08:00
|
|
|
********
|
2017-01-06 23:17:49 +08:00
|
|
|
|
|
|
|
A simple button demo showcasing the use of GPIO input with interrupts.
|
|
|
|
|
|
|
|
Requirements
|
2017-01-21 07:58:05 +08:00
|
|
|
************
|
2017-01-06 23:17:49 +08:00
|
|
|
|
|
|
|
The demo assumes that a push button is connected to one of GPIO lines. The
|
|
|
|
sample code is configured to work on boards with user defined buttons and that
|
2018-12-21 18:08:06 +08:00
|
|
|
have defined the SW0_* variables.
|
2017-01-06 23:17:49 +08:00
|
|
|
|
|
|
|
To use this sample, you will require a board that defines the user switch in its
|
|
|
|
header file. The :file:`board.h` must define the following variables:
|
|
|
|
|
2019-06-22 23:57:14 +08:00
|
|
|
- SW0_GPIO_NAME (or DT_ALIAS_SW0_GPIOS_CONTROLLER)
|
|
|
|
- DT_ALIAS_SW0_GPIOS_PIN
|
2017-01-06 23:17:49 +08:00
|
|
|
|
2018-12-21 18:08:06 +08:00
|
|
|
Alternatively, this could also be done by defining 'sw0' alias in the board
|
2019-10-08 01:37:09 +08:00
|
|
|
devicetree description file.
|
2017-01-06 23:17:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
Building and Running
|
2017-01-21 07:58:05 +08:00
|
|
|
********************
|
2017-01-06 23:17:49 +08:00
|
|
|
|
|
|
|
This sample can be built for multiple boards, in this example we will build it
|
|
|
|
for the nucleo_f103rb board:
|
|
|
|
|
2017-11-12 17:12:44 +08:00
|
|
|
.. zephyr-app-commands::
|
|
|
|
:zephyr-app: samples/basic/button
|
|
|
|
:board: nucleo_f103rb
|
|
|
|
:goals: build
|
|
|
|
:compact:
|
2017-01-06 23:17:49 +08:00
|
|
|
|
|
|
|
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.
|