Re: GPIO support on nRF52840 DK
" via Lists.Zephyrproject.Org"
Hello everyone,All GPIOs are supported and accessible, not just the LEDs and
buttons. This includes all GPIOs pinned out to the expansion headers.
I think this is likely to be a common question for beginners, so I'll
provide various details here that hopefully will be google-able for
others later :). There is a TL;DR for this specific question below.
Zephyr's GPIO API works differently than Arduino's.
Instead of a single, linear pin numbering from 0 to N-1 for all the pins
(or the D0, D1, ... "digital" and A0, A1, ... "analog" pin
macros), Zephyr identifies a GPIO with two pieces of information:
1. The GPIO "controller", which is a struct device implementing the GPIO
API. For all the gory details on the device model, see:
2. The pin number, which is specific to each controller. For example,
pin 0 on one controller is different than pin 0 on another
On nRF SoCs, there are two separate GPIO controller devices: one for the
P0.x pins, and another for the P1.x pins.
So, to use pin P1.01 in the button sample, you need to set PORT to use
the P1.x controller, and PIN to 1.
To set PORT, you need the "label" for the P1.x controller, which in this
case is DT_ALIAS_GPIO_1_LABEL.
This define is generated by device tree. See
zephyr/include/generated/generated_dts_board_unfixed.h in your build
directory for all the available defines, and
https://docs.zephyrproject.org/latest/guides/dts/index.html for more
information on DT.
It works the exact same way on other SoCs. Only the label defines from
TL;DR here's a patch that does what you want:
diff --git zephyr/samples/basic/button/src/main.c zephyr/samples/basic/button/src/main.c
index d55147c171..c1e94767a7 100644
@@ -52,6 +52,10 @@
/* Sleep time */
#define SLEEP_TIME 500
+#define PORT DT_ALIAS_GPIO_1_LABEL
+#define PIN 1
void button_pressed(struct device *gpiob, struct gpio_callback *cb,
You did great! Your question was clear.
Hope this helps,