Skip to content

Commit 767dd47

Browse files
gmarullcarlescufi
authored andcommitted
samples: shields: npm6001_ek: initial version
Add a sample that demonstrates usage of the nPM6001 PMIC (all of its functionalities). Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 1d4b1e3 commit 767dd47

File tree

7 files changed

+353
-1
lines changed

7 files changed

+353
-1
lines changed

boards/shields/npm6001_ek/doc/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Usage
2828
*****
2929

3030
The shield can be used in any application by setting ``SHIELD`` to
31-
``npm6001_ek``.
31+
``npm6001_ek``. You can check :ref:`npm6001_ek_sample` for a comprehensive
32+
sample.
3233

3334
References
3435
**********
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2022 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
set(SHIELD npm6001_ek)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
10+
project(npm6001_ek)
11+
target_sources(app PRIVATE src/main.c)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
.. _npm6001_ek_sample:
2+
3+
nPM6001 EK sample
4+
#################
5+
6+
Overview
7+
********
8+
9+
This sample is provided as an example to test the :ref:`npm6001_ek`. The sample
10+
provides a shell interface that allows to test multiple functionalities offered
11+
by the nPM6001 PMIC, including:
12+
13+
- GPIO
14+
- Watchdog
15+
16+
Requirements
17+
************
18+
19+
The shield needs to be wired to a host board supporting the Arduino connector.
20+
Below you can find a wiring example for the nRF52840 DK:
21+
22+
.. figure:: nrf52840dk_wiring.jpg
23+
:alt: nRF52840DK + nPM6001-EK wiring example
24+
:align: center
25+
26+
nRF52840DK + nPM6001-EK wiring example
27+
28+
Building and Running
29+
********************
30+
31+
The sample is designed so that it can run on any platform. For example, when
32+
building for the nRF52840 DK, the following command can be used:
33+
34+
.. zephyr-app-commands::
35+
:zephyr-app: samples/shields/npm6001_ek
36+
:board: nrf52840dk_nrf52840
37+
:goals: build
38+
:compact:
39+
40+
Note that this sample automatically sets ``SHIELD`` to ``npm6001_ek``. Once
41+
flashed, you should boot into the shell interface. The ``npm6001`` command is
42+
provided to test the PMIC. Below you can find details for each subcommand.
43+
44+
GPIO
45+
====
46+
47+
The ``npm6001`` shell interface provides the ``gpio`` subcommand to test the
48+
GPIO functionality offered by the PMIC. Below you can find some command
49+
examples.
50+
51+
.. code-block:: bash
52+
53+
# configure GPIO 0 as output
54+
npm6001 gpio configure -p 0 -d out
55+
# configure GPIO 0 as output (init high)
56+
npm6001 gpio configure -p 0 -d outh
57+
# configure GPIO 0 as output (init low)
58+
npm6001 gpio configure -p 0 -d outl
59+
# configure GPIO 0 as output with high-drive mode enabled
60+
npm6001 gpio configure -p 0 -d out --high-drive
61+
# configure GPIO 1 as input
62+
npm6001 gpio configure -p 1 -d input
63+
# configure GPIO 1 as input with pull-down enabled
64+
npm6001 gpio configure -p 1 -d input --pull-down
65+
# configure GPIO 1 as input with CMOS mode enabled
66+
npm6001 gpio configure -p 1 -d input --cmos
67+
68+
.. code-block:: bash
69+
70+
# get GPIO 1 level
71+
npm6001 gpio get 1
72+
73+
.. code-block:: bash
74+
75+
# set GPIO 0 high
76+
npm6001 gpio set 0 1
77+
# set GPIO 0 low
78+
npm6001 gpio set 0 0
79+
80+
.. code-block:: bash
81+
82+
# toggle GPIO 0
83+
npm6001 gpio toggle 0
84+
85+
Watchdog
86+
========
87+
88+
The ``npm6001`` shell interface provides the ``wdt`` subcommand to test the
89+
Watchdog functionality offered by the PMIC. Below you can find some command
90+
examples.
91+
92+
.. code-block:: bash
93+
94+
# enable watchdog, timeout set to 8 seconds. Timeout will be rounded up to
95+
# the resolution of the watchdog, e.g. 10s -> 12s.
96+
npm6001 wdt enable 8000
97+
# disable watchdog
98+
npm6001 wdt disable
99+
# kick/feed watchdog
100+
npm6001 wdt kick
101+
102+
.. note::
103+
When the watchdog reset pin is connected to your board reset, you will see
104+
how Zephyr reboots after the watchdog timeout expires.
68.2 KB
Loading

samples/shields/npm6001_ek/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2022 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_SHELL=y
5+
CONFIG_SHELL_GETOPT=y
6+
CONFIG_GETOPT=y
7+
CONFIG_GETOPT_LONG=y
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2022 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
sample:
5+
name: nPM6001 EK
6+
tests:
7+
sample.shields.npm6001_ek:
8+
harness: shield
9+
tags: shield
10+
depends_on: arduino_i2c

samples/shields/npm6001_ek/src/main.c

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <stdlib.h>
7+
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/gpio.h>
10+
#include <zephyr/drivers/watchdog.h>
11+
#include <zephyr/dt-bindings/gpio/nordic-npm6001-gpio.h>
12+
#include <zephyr/shell/shell.h>
13+
#include <zephyr/sys/printk.h>
14+
15+
#include <getopt.h>
16+
17+
static const struct device *const gpio = DEVICE_DT_GET_ONE(nordic_npm6001_gpio);
18+
static const struct device *const wdt = DEVICE_DT_GET_ONE(nordic_npm6001_wdt);
19+
20+
void main(void)
21+
{
22+
if (!device_is_ready(gpio)) {
23+
printk("nPM6001 GPIO device not ready\n");
24+
return;
25+
}
26+
27+
if (!device_is_ready(wdt)) {
28+
printk("nPM6001 Watchdog device not ready\n");
29+
return;
30+
}
31+
}
32+
33+
static int cmd_gpio_configure(const struct shell *sh, size_t argc, char **argv)
34+
{
35+
int ret;
36+
int opt, long_index = 0;
37+
static int high_drive, pull_down, cmos;
38+
gpio_pin_t pin = 0U;
39+
gpio_flags_t flags = 0U;
40+
41+
static const struct option long_options[] = {
42+
{"pin", required_argument, NULL, 'p'},
43+
{"direction", required_argument, NULL, 'd'},
44+
{"high-drive", no_argument, &high_drive, 1},
45+
{"pull-down", no_argument, &pull_down, 1},
46+
{"cmos", no_argument, &cmos, 1},
47+
{NULL, 0, NULL, 0},
48+
};
49+
50+
high_drive = 0;
51+
pull_down = 0;
52+
cmos = 0;
53+
54+
while ((opt = getopt_long(argc, argv, "p:d:", long_options,
55+
&long_index)) != -1) {
56+
switch (opt) {
57+
case 0:
58+
/* options setting a flag, do nothing */
59+
break;
60+
case 'p':
61+
pin = atoi(optarg);
62+
break;
63+
case 'd':
64+
if (strcmp(optarg, "in") == 0) {
65+
flags |= GPIO_INPUT;
66+
} else if (strcmp(optarg, "out") == 0) {
67+
flags |= GPIO_OUTPUT;
68+
} else if (strcmp(optarg, "outh") == 0) {
69+
flags |= GPIO_OUTPUT_HIGH;
70+
} else if (strcmp(optarg, "outl") == 0) {
71+
flags |= GPIO_OUTPUT_LOW;
72+
}
73+
break;
74+
default:
75+
shell_error(sh, "Invalid option: %c", opt);
76+
return -EINVAL;
77+
}
78+
}
79+
80+
if (pull_down == 1) {
81+
flags |= GPIO_PULL_DOWN;
82+
}
83+
84+
if (high_drive == 1) {
85+
flags |= NPM6001_GPIO_DRIVE_HIGH;
86+
}
87+
88+
if (cmos == 1) {
89+
flags |= NPM6001_GPIO_SENSE_CMOS;
90+
}
91+
92+
ret = gpio_pin_configure(gpio, pin, flags);
93+
if (ret < 0) {
94+
shell_error(sh, "Configuration failed (%d)", ret);
95+
return ret;
96+
}
97+
98+
return 0;
99+
}
100+
101+
static int cmd_gpio_get(const struct shell *sh, size_t argc, char **argv)
102+
{
103+
gpio_pin_t pin;
104+
int val;
105+
106+
pin = (gpio_pin_t)atoi(argv[1]);
107+
108+
val = gpio_pin_get(gpio, pin);
109+
if (val < 0) {
110+
shell_error(sh, "Could not get pin level (%d)", val);
111+
return val;
112+
}
113+
114+
shell_print(sh, "Level: %d", val);
115+
116+
return 0;
117+
}
118+
119+
static int cmd_gpio_set(const struct shell *sh, size_t argc, char **argv)
120+
{
121+
gpio_pin_t pin;
122+
int val;
123+
int ret;
124+
125+
pin = (gpio_pin_t)atoi(argv[1]);
126+
val = atoi(argv[2]);
127+
128+
ret = gpio_pin_set(gpio, pin, val);
129+
if (ret < 0) {
130+
shell_error(sh, "Could not set pin level (%d)", ret);
131+
return ret;
132+
}
133+
134+
return 0;
135+
}
136+
137+
static int cmd_gpio_toggle(const struct shell *sh, size_t argc, char **argv)
138+
{
139+
gpio_pin_t pin;
140+
int ret;
141+
142+
pin = (gpio_pin_t)atoi(argv[1]);
143+
144+
ret = gpio_pin_toggle(gpio, pin);
145+
if (ret < 0) {
146+
shell_error(sh, "Could not toggle pin level (%d)", ret);
147+
return ret;
148+
}
149+
150+
return 0;
151+
}
152+
153+
static int cmd_wdt_enable(const struct shell *sh, size_t argc, char **argv)
154+
{
155+
int ret;
156+
struct wdt_timeout_cfg cfg = { 0 };
157+
158+
cfg.window.max = (uint32_t)strtoul(argv[1], NULL, 10) * 1000U;
159+
160+
ret = wdt_install_timeout(wdt, &cfg);
161+
if (ret < 0) {
162+
shell_error(sh, "Could not install watchdog timeout (%d)", ret);
163+
}
164+
165+
return 0;
166+
}
167+
168+
static int cmd_wdt_disable(const struct shell *sh, size_t argc, char **argv)
169+
{
170+
int ret;
171+
172+
ret = wdt_disable(wdt);
173+
if (ret < 0) {
174+
shell_error(sh, "Could not disable watchdog (%d)", ret);
175+
}
176+
177+
return 0;
178+
}
179+
180+
static int cmd_wdt_kick(const struct shell *sh, size_t argc, char **argv)
181+
{
182+
int ret;
183+
184+
ret = wdt_feed(wdt, 0);
185+
if (ret < 0) {
186+
shell_error(sh, "Could not kick watchdog (%d)", ret);
187+
}
188+
189+
return 0;
190+
}
191+
192+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_npm6001_gpio_cmds,
193+
SHELL_CMD_ARG(configure, NULL, "Configure GPIO",
194+
cmd_gpio_configure, 5, 3),
195+
SHELL_CMD_ARG(get, NULL, "Get GPIO level",
196+
cmd_gpio_get, 2, 0),
197+
SHELL_CMD_ARG(set, NULL, "Set GPIO level",
198+
cmd_gpio_set, 3, 0),
199+
SHELL_CMD_ARG(toggle, NULL, "Toggle GPIO level",
200+
cmd_gpio_toggle, 2, 0),
201+
SHELL_SUBCMD_SET_END);
202+
203+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_npm6001_wdt_cmds,
204+
SHELL_CMD_ARG(enable, NULL, "Enable watchdog",
205+
cmd_wdt_enable, 2, 0),
206+
SHELL_CMD(disable, NULL, "Disable watchdog",
207+
cmd_wdt_disable),
208+
SHELL_CMD(kick, NULL, "Kick watchdog",
209+
cmd_wdt_kick),
210+
SHELL_SUBCMD_SET_END);
211+
212+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_npm6001_cmds,
213+
SHELL_CMD(gpio, &sub_npm6001_gpio_cmds, "GPIO",
214+
NULL),
215+
SHELL_CMD(wdt, &sub_npm6001_wdt_cmds, "Watchdog",
216+
NULL),
217+
SHELL_SUBCMD_SET_END);
218+
219+
SHELL_CMD_REGISTER(npm6001, &sub_npm6001_cmds, "nPM6001 commands", NULL);

0 commit comments

Comments
 (0)