Skip to content

Commit 24f9d79

Browse files
committed
applications: serial_lte_monitor: Power/indicate pins to devicetree
Move power and indicate pins from Kconfig to devicetree. Jira: LRCS-114 Signed-off-by: Tommi Rantanen <[email protected]>
1 parent 39729aa commit 24f9d79

File tree

6 files changed

+107
-50
lines changed

6 files changed

+107
-50
lines changed

applications/serial_lte_modem/boards/nrf9151dk_nrf9151_ns.overlay

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
pinctrl-names = "default", "sleep";
2727
};
2828

29+
/ {
30+
slm_gpio_pins: slm_gpio_pins {
31+
compatible = "slm-gpio-pins";
32+
power-gpios = <&gpio0 8 GPIO_ACTIVE_LOW>;
33+
power-gpios-active-time-ms = <100>;
34+
power-gpios-debounce-time-ms = <50>;
35+
indicate-gpios = <&gpio0 0 GPIO_ACTIVE_LOW>;
36+
indicate-gpios-active-time-ms = <100>;
37+
//power-gpios = <&gpio0 8 (GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
38+
};
39+
};
40+
2941
&i2c2 {
3042
status = "disabled";
3143
};

applications/serial_lte_modem/overlay-external-mcu.overlay

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@
1818
&uart0 {
1919
status = "disabled";
2020
};
21+
22+
/ {
23+
slm_gpio_pins: slm_gpio_pins {
24+
compatible = "slm-gpio-pins";
25+
power-gpios = <&gpio0 31 GPIO_ACTIVE_LOW>;
26+
power-gpios-active-time-ms = <100>;
27+
power-gpios-debounce-time-ms = <50>;
28+
indicate-gpios = <&gpio0 30 GPIO_ACTIVE_LOW>;
29+
indicate-gpios-active-time-ms = <100>;
30+
};
31+
};

applications/serial_lte_modem/src/slm_at_host.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ static int slm_at_send_indicate(const uint8_t *data, size_t len,
505505
return -EFAULT;
506506
}
507507

508+
#if (INDICATE_PIN_IS_ENABLED)
508509
if (indicate) {
509510
enum pm_device_state state = PM_DEVICE_STATE_OFF;
510511

@@ -513,6 +514,7 @@ static int slm_at_send_indicate(const uint8_t *data, size_t len,
513514
slm_ctrl_pin_indicate();
514515
}
515516
}
517+
#endif
516518

517519
ret = at_backend.send(data, len);
518520
if (!ret) {

applications/serial_lte_modem/src/slm_ctrl_pin.c

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@
1818

1919
LOG_MODULE_REGISTER(slm_ctrl_pin, CONFIG_SLM_LOG_LEVEL);
2020

21-
#define POWER_PIN_DEBOUNCE_MS 50
21+
#if POWER_PIN_IS_ENABLED
22+
23+
static const struct gpio_dt_spec power_pin_member =
24+
GPIO_DT_SPEC_GET_OR(DT_NODELABEL(slm_gpio_pins), power_gpios, {0});
25+
static const struct gpio_dt_spec indicate_pin_member =
26+
GPIO_DT_SPEC_GET_OR(DT_NODELABEL(slm_gpio_pins), indicate_gpios, {0});
27+
static const int power_pin_debounce_ms =
28+
DT_PROP(DT_NODELABEL(slm_gpio_pins), power_gpios_debounce_time_ms);
29+
static const int indicate_pin_time_ms =
30+
DT_PROP(DT_NODELABEL(slm_gpio_pins), indicate_gpios_active_time_ms);
2231

23-
static const struct device *gpio_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));
32+
#endif
2433

2534
#if POWER_PIN_IS_ENABLED
2635
static struct gpio_callback gpio_cb;
36+
static struct k_work_delayable indicate_work;
37+
static atomic_t callback_wakeup_running;
2738
#else
2839
BUILD_ASSERT(!IS_ENABLED(CONFIG_SLM_START_SLEEP),
29-
"CONFIG_SLM_START_SLEEP requires CONFIG_SLM_POWER_PIN to be defined.");
40+
"CONFIG_SLM_START_SLEEP requires slm_gpio_pins to be defined in devicetree.");
3041
#endif
3142

32-
static struct k_work_delayable indicate_work;
33-
static atomic_t callback_wakeup_running;
34-
3543
static int ext_xtal_control(bool xtal_on)
3644
{
3745
int err = 0;
@@ -65,57 +73,41 @@ static int ext_xtal_control(bool xtal_on)
6573
return err;
6674
}
6775

68-
#if POWER_PIN_IS_ENABLED || INDICATE_PIN_IS_ENABLED
69-
70-
static int configure_gpio(gpio_pin_t pin, gpio_flags_t flags)
71-
{
72-
const int err = gpio_pin_configure(gpio_dev, pin, flags);
73-
74-
if (err) {
75-
LOG_ERR("Failed to configure GPIO pin P0.%d. (%d)", pin, err);
76-
return err;
77-
}
78-
79-
return 0;
80-
}
81-
#endif
82-
8376
#if POWER_PIN_IS_ENABLED
8477

8578
static int configure_power_pin_interrupt(gpio_callback_handler_t handler, gpio_flags_t flags)
8679
{
8780
int err;
88-
const gpio_pin_t pin = CONFIG_SLM_POWER_PIN;
8981

9082
/* First disable the previously configured interrupt. Somehow when in idle mode if
9183
* the wake-up interrupt is configured to be on an edge the power consumption
9284
* drastically increases (3x), which is why it is configured to be level-triggered.
9385
* When entering idle for some reason first disabling the previously edge-level
9486
* configured interrupt is also needed to keep the power consumption down.
9587
*/
96-
err = gpio_pin_interrupt_configure(gpio_dev, pin, GPIO_INT_DISABLE);
88+
err = gpio_pin_interrupt_configure_dt(&power_pin_member, GPIO_INT_DISABLE);
9789
if (err) {
9890
LOG_ERR("Failed to configure %s (0x%x) on power pin. (%d)",
9991
"interrupt", GPIO_INT_DISABLE, err);
10092
}
10193

102-
err = gpio_pin_interrupt_configure(gpio_dev, pin, flags);
94+
err = gpio_pin_interrupt_configure_dt(&power_pin_member, flags);
10395
if (err) {
10496
LOG_ERR("Failed to configure %s (0x%x) on power pin. (%d)",
10597
"interrupt", flags, err);
10698
return err;
10799
}
108100

109-
gpio_init_callback(&gpio_cb, handler, BIT(pin));
101+
gpio_init_callback(&gpio_cb, handler, BIT(power_pin_member.pin));
110102

111-
err = gpio_add_callback(gpio_dev, &gpio_cb);
103+
err = gpio_add_callback_dt(&power_pin_member, &gpio_cb);
112104
if (err) {
113105
LOG_ERR("Failed to configure %s (0x%x) on power pin. (%d)", "callback", flags, err);
114106
return err;
115107
}
116108

117109
LOG_DBG("Configured interrupt (0x%x) on power pin (%u) with handler (%p).",
118-
flags, pin, (void *)handler);
110+
flags, power_pin_member.pin, (void *)handler);
119111
return 0;
120112
}
121113

@@ -130,16 +122,18 @@ static void power_pin_callback_poweroff(const struct device *dev,
130122
static K_WORK_DEFINE(work, slm_ctrl_pin_enter_sleep_work_fn);
131123

132124
LOG_INF("Power off triggered.");
133-
gpio_remove_callback(dev, gpio_callback);
125+
gpio_remove_callback_dt(&power_pin_member, gpio_callback);
134126
k_work_submit(&work);
135127
}
136128

137129
#endif /* POWER_PIN_IS_ENABLED */
138130

131+
#if (INDICATE_PIN_IS_ENABLED)
132+
139133
static void indicate_stop(void)
140134
{
141135
#if (INDICATE_PIN_IS_ENABLED)
142-
if (gpio_pin_set(gpio_dev, CONFIG_SLM_INDICATE_PIN, 0) != 0) {
136+
if (gpio_pin_set_dt(&indicate_pin_member, 0) != 0) {
143137
LOG_WRN("GPIO_0 set error");
144138
}
145139
LOG_DBG("Stop indicating");
@@ -153,6 +147,8 @@ static void indicate_wk(struct k_work *work)
153147
indicate_stop();
154148
}
155149

150+
#endif
151+
156152
#if POWER_PIN_IS_ENABLED
157153

158154
static void power_pin_callback_enable_poweroff_fn(struct k_work *)
@@ -166,10 +162,10 @@ static K_WORK_DELAYABLE_DEFINE(work_poweroff, power_pin_callback_enable_poweroff
166162
static void power_pin_callback_enable_poweroff(const struct device *dev,
167163
struct gpio_callback *gpio_callback, uint32_t)
168164
{
169-
LOG_INF("Enabling the poweroff interrupt shortly...");
170-
gpio_remove_callback(dev, gpio_callback);
165+
LOG_INF("Enabling the poweroff interrupt after debounce time %d...", power_pin_debounce_ms);
166+
gpio_remove_callback_dt(&power_pin_member, gpio_callback);
171167

172-
k_work_reschedule(&work_poweroff, K_MSEC(POWER_PIN_DEBOUNCE_MS));
168+
k_work_reschedule(&work_poweroff, K_MSEC(power_pin_debounce_ms));
173169
}
174170

175171
static void power_pin_callback_wakeup_work_fn(struct k_work *)
@@ -189,7 +185,7 @@ static void power_pin_callback_wakeup_work_fn(struct k_work *)
189185
err = slm_at_host_power_on();
190186
if (err) {
191187
LOG_ERR("Failed to power on uart: %d. Resetting SLM.", err);
192-
gpio_remove_callback(gpio_dev, &gpio_cb);
188+
gpio_remove_callback_dt(&power_pin_member, &gpio_cb);
193189
slm_reset();
194190
return;
195191
}
@@ -208,7 +204,7 @@ static void power_pin_callback_wakeup(const struct device *dev,
208204
}
209205

210206
LOG_INF("Resuming from idle shortly...");
211-
gpio_remove_callback(dev, gpio_callback);
207+
gpio_remove_callback_dt(&power_pin_member, gpio_callback);
212208

213209
/* Enable the poweroff interrupt only when the pin will be back to a nonactive state. */
214210
configure_power_pin_interrupt(power_pin_callback_enable_poweroff, GPIO_INT_EDGE_FALLING);
@@ -224,12 +220,12 @@ int slm_ctrl_pin_indicate(void)
224220
if (k_work_delayable_is_pending(&indicate_work)) {
225221
return 0;
226222
}
227-
LOG_DBG("Start indicating");
228-
err = gpio_pin_set(gpio_dev, CONFIG_SLM_INDICATE_PIN, 1);
223+
LOG_DBG("Start indicating for %d ms", indicate_pin_time_ms);
224+
err = gpio_pin_set_dt(&indicate_pin_member, 1);
229225
if (err) {
230226
LOG_ERR("GPIO_0 set error: %d", err);
231227
} else {
232-
k_work_reschedule(&indicate_work, K_MSEC(CONFIG_SLM_INDICATE_TIME));
228+
k_work_reschedule(&indicate_work, K_MSEC(indicate_pin_time_ms));
233229
}
234230
#endif
235231
return err;
@@ -240,7 +236,7 @@ void slm_ctrl_pin_enter_idle(void)
240236
LOG_INF("Entering idle.");
241237
int err;
242238

243-
gpio_remove_callback(gpio_dev, &gpio_cb);
239+
gpio_remove_callback_dt(&power_pin_member, &gpio_cb);
244240

245241
err = configure_power_pin_interrupt(power_pin_callback_wakeup, GPIO_INT_LEVEL_LOW);
246242
if (err) {
@@ -270,7 +266,7 @@ void slm_ctrl_pin_enter_sleep_no_uninit(void)
270266
{
271267
LOG_INF("Entering sleep.");
272268
LOG_PANIC();
273-
nrf_gpio_cfg_sense_set(CONFIG_SLM_POWER_PIN, NRF_GPIO_PIN_SENSE_LOW);
269+
nrf_gpio_cfg_sense_set(power_pin_member.pin, NRF_GPIO_PIN_SENSE_LOW);
274270

275271
k_sleep(K_MSEC(100));
276272

@@ -286,11 +282,11 @@ void slm_ctrl_pin_enter_shutdown(void)
286282

287283
/* De-configure GPIOs */
288284
#if POWER_PIN_IS_ENABLED
289-
gpio_pin_interrupt_configure(gpio_dev, CONFIG_SLM_POWER_PIN, GPIO_INT_DISABLE);
290-
gpio_pin_configure(gpio_dev, CONFIG_SLM_POWER_PIN, GPIO_DISCONNECTED);
285+
gpio_pin_interrupt_configure_dt(&power_pin_member, GPIO_INT_DISABLE);
286+
gpio_pin_configure_dt(&power_pin_member, GPIO_DISCONNECTED);
291287
#endif
292288
#if INDICATE_PIN_IS_ENABLED
293-
gpio_pin_configure(gpio_dev, CONFIG_SLM_INDICATE_PIN, GPIO_DISCONNECTED);
289+
gpio_pin_configure_dt(&indicate_pin_member, GPIO_DISCONNECTED);
294290
#endif
295291

296292
k_sleep(K_MSEC(100));
@@ -301,32 +297,35 @@ void slm_ctrl_pin_enter_shutdown(void)
301297

302298
void slm_ctrl_pin_init_gpios(void)
303299
{
304-
if (!device_is_ready(gpio_dev)) {
300+
#if POWER_PIN_IS_ENABLED
301+
if (!gpio_is_ready_dt(&power_pin_member)) {
305302
LOG_ERR("GPIO controller not ready");
306303
return;
307304
}
308305

309-
#if POWER_PIN_IS_ENABLED
310-
(void)configure_gpio(CONFIG_SLM_POWER_PIN, GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_LOW);
306+
LOG_WRN("power_pin_member %d", power_pin_member.pin);
307+
/* TODO: Flags into DTS */
308+
gpio_pin_configure_dt(&power_pin_member, GPIO_INPUT | GPIO_PULL_UP);
311309
#endif
312310

313311
#if INDICATE_PIN_IS_ENABLED
314-
(void)configure_gpio(CONFIG_SLM_INDICATE_PIN, GPIO_OUTPUT_INACTIVE | GPIO_ACTIVE_LOW);
312+
LOG_WRN("indicate_pin_member %d", indicate_pin_member.pin);
313+
gpio_pin_configure_dt(&indicate_pin_member, GPIO_OUTPUT_INACTIVE);
315314
#endif
316315
}
317316

318317
int slm_ctrl_pin_init(void)
319318
{
320319
int err;
321320

322-
k_work_init_delayable(&indicate_work, indicate_wk);
323-
324321
err = ext_xtal_control(true);
325322
if (err < 0) {
326323
LOG_ERR("Failed to enable ext XTAL: %d", err);
327324
return err;
328325
}
329326
#if POWER_PIN_IS_ENABLED
327+
k_work_init_delayable(&indicate_work, indicate_wk);
328+
330329
/* Do not directly enable the poweroff interrupt so that only a full toggle triggers
331330
* power off. This is because power on is triggered on low level, so if the pin is held
332331
* down until SLM is fully initialized releasing it would directly trigger the power off.

applications/serial_lte_modem/src/slm_defines.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum {
3636
#define SLM_NRF52_BLK_SIZE 4096 /** nRF52 flash block size for write operation */
3737
#define SLM_NRF52_BLK_TIME 2000 /** nRF52 flash block write time in millisecond (1.x second) */
3838

39-
#define POWER_PIN_IS_ENABLED (CONFIG_SLM_POWER_PIN != -1)
40-
#define INDICATE_PIN_IS_ENABLED (CONFIG_SLM_INDICATE_PIN != -1)
39+
#define POWER_PIN_IS_ENABLED DT_NODE_HAS_PROP(DT_NODELABEL(slm_gpio_pins), power_gpios)
40+
#define INDICATE_PIN_IS_ENABLED DT_NODE_HAS_PROP(DT_NODELABEL(slm_gpio_pins), indicate_gpios)
4141

4242
#endif

dts/bindings/gpio/slm-gpio-pins.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
description: GPIO pins to control Serial LTE Modem
2+
3+
compatible: "slm-gpio-pins"
4+
5+
properties:
6+
power-gpios:
7+
type: phandle-array
8+
required: true
9+
description: |
10+
Power pin.
11+
12+
power-gpios-active-time-ms:
13+
type: int
14+
default: 100
15+
description: |
16+
Power pin active time in milliseconds.
17+
18+
power-gpios-debounce-time-ms:
19+
type: int
20+
default: 50
21+
description: |
22+
Power pin debounce time in milliseconds.
23+
24+
indicate-gpios:
25+
type: phandle-array
26+
description: |
27+
Indicate pin.
28+
29+
indicate-gpios-active-time-ms:
30+
type: int
31+
default: 100
32+
description: |
33+
Indicate pin active time in milliseconds.

0 commit comments

Comments
 (0)