Re: GPIO support on nRF52840 DK

Bolivar, Marti


" via Lists.Zephyrproject.Org"
<> writes:

Hello everyone,

Can I access (read/write) a GPIO that is not an LED or button listed in the
"Connections and IOs" section of link below?

I read in that page that "Other hardware features are not supported by the
Zephyr kernel." but (since I am a newbie in embedded devices) I am not sure
if that means that "only buttons 1 to 4 are supported and no other GPIO" or
else refers to more abstract features (e.g. Enhanced ShockBurst).
All GPIOs are supported and accessible, not just the LEDs and
buttons. This includes all GPIOs pinned out to the expansion headers.

Here's the context of my question:
I am editing the example code samples/basic/button to use a GPIO pin that
is different from BUTTON 1, 2, 3 or 4 to trigger the button_pressed

I changed
... to ...
#define PIN 33
and the callback is never called when I toggle the logical state of that
pin in the board (from ground to 3.3V nor VDD).

I am not sure if this makes sense if I am using Zephyr, but in the nRF's
SDK, GPIO P1.01 of the development board is mapped to 33:
#define ARDUINO_0_PIN
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

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 for more
information on DT.

It works the exact same way on other SoCs. Only the label defines from
DT vary.

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
--- zephyr/samples/basic/button/src/main.c
+++ zephyr/samples/basic/button/src/main.c
@@ -52,6 +52,10 @@
/* Sleep time */
#define SLEEP_TIME 500

+#undef PORT
+#undef PIN
+#define PIN 1

void button_pressed(struct device *gpiob, struct gpio_callback *cb,
u32_t pins)

This is my very first question on a public list/forum ever ( far as I
recall) so please forgive any mistakes in terms of posting rules, etc.
You did great! Your question was clear.

Thanks for your help!
Hope this helps,

Join to automatically receive all group messages.