18
18
19
19
LOG_MODULE_REGISTER (slm_ctrl_pin , CONFIG_SLM_LOG_LEVEL );
20
20
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 );
22
31
23
- static const struct device * gpio_dev = DEVICE_DT_GET ( DT_NODELABEL ( gpio0 ));
32
+ #endif
24
33
25
34
#if POWER_PIN_IS_ENABLED
26
35
static struct gpio_callback gpio_cb ;
36
+ static struct k_work_delayable indicate_work ;
37
+ static atomic_t callback_wakeup_running ;
27
38
#else
28
39
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 ." );
30
41
#endif
31
42
32
- static struct k_work_delayable indicate_work ;
33
- static atomic_t callback_wakeup_running ;
34
-
35
43
static int ext_xtal_control (bool xtal_on )
36
44
{
37
45
int err = 0 ;
@@ -65,57 +73,41 @@ static int ext_xtal_control(bool xtal_on)
65
73
return err ;
66
74
}
67
75
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
-
83
76
#if POWER_PIN_IS_ENABLED
84
77
85
78
static int configure_power_pin_interrupt (gpio_callback_handler_t handler , gpio_flags_t flags )
86
79
{
87
80
int err ;
88
- const gpio_pin_t pin = CONFIG_SLM_POWER_PIN ;
89
81
90
82
/* First disable the previously configured interrupt. Somehow when in idle mode if
91
83
* the wake-up interrupt is configured to be on an edge the power consumption
92
84
* drastically increases (3x), which is why it is configured to be level-triggered.
93
85
* When entering idle for some reason first disabling the previously edge-level
94
86
* configured interrupt is also needed to keep the power consumption down.
95
87
*/
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 );
97
89
if (err ) {
98
90
LOG_ERR ("Failed to configure %s (0x%x) on power pin. (%d)" ,
99
91
"interrupt" , GPIO_INT_DISABLE , err );
100
92
}
101
93
102
- err = gpio_pin_interrupt_configure ( gpio_dev , pin , flags );
94
+ err = gpio_pin_interrupt_configure_dt ( & power_pin_member , flags );
103
95
if (err ) {
104
96
LOG_ERR ("Failed to configure %s (0x%x) on power pin. (%d)" ,
105
97
"interrupt" , flags , err );
106
98
return err ;
107
99
}
108
100
109
- gpio_init_callback (& gpio_cb , handler , BIT (pin ));
101
+ gpio_init_callback (& gpio_cb , handler , BIT (power_pin_member . pin ));
110
102
111
- err = gpio_add_callback ( gpio_dev , & gpio_cb );
103
+ err = gpio_add_callback_dt ( & power_pin_member , & gpio_cb );
112
104
if (err ) {
113
105
LOG_ERR ("Failed to configure %s (0x%x) on power pin. (%d)" , "callback" , flags , err );
114
106
return err ;
115
107
}
116
108
117
109
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 );
119
111
return 0 ;
120
112
}
121
113
@@ -130,16 +122,18 @@ static void power_pin_callback_poweroff(const struct device *dev,
130
122
static K_WORK_DEFINE (work , slm_ctrl_pin_enter_sleep_work_fn ) ;
131
123
132
124
LOG_INF ("Power off triggered." );
133
- gpio_remove_callback ( dev , gpio_callback );
125
+ gpio_remove_callback_dt ( & power_pin_member , gpio_callback );
134
126
k_work_submit (& work );
135
127
}
136
128
137
129
#endif /* POWER_PIN_IS_ENABLED */
138
130
131
+ #if (INDICATE_PIN_IS_ENABLED )
132
+
139
133
static void indicate_stop (void )
140
134
{
141
135
#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 ) {
143
137
LOG_WRN ("GPIO_0 set error" );
144
138
}
145
139
LOG_DBG ("Stop indicating" );
@@ -153,6 +147,8 @@ static void indicate_wk(struct k_work *work)
153
147
indicate_stop ();
154
148
}
155
149
150
+ #endif
151
+
156
152
#if POWER_PIN_IS_ENABLED
157
153
158
154
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
166
162
static void power_pin_callback_enable_poweroff (const struct device * dev ,
167
163
struct gpio_callback * gpio_callback , uint32_t )
168
164
{
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 );
171
167
172
- k_work_reschedule (& work_poweroff , K_MSEC (POWER_PIN_DEBOUNCE_MS ));
168
+ k_work_reschedule (& work_poweroff , K_MSEC (power_pin_debounce_ms ));
173
169
}
174
170
175
171
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 *)
189
185
err = slm_at_host_power_on ();
190
186
if (err ) {
191
187
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 );
193
189
slm_reset ();
194
190
return ;
195
191
}
@@ -208,7 +204,7 @@ static void power_pin_callback_wakeup(const struct device *dev,
208
204
}
209
205
210
206
LOG_INF ("Resuming from idle shortly..." );
211
- gpio_remove_callback ( dev , gpio_callback );
207
+ gpio_remove_callback_dt ( & power_pin_member , gpio_callback );
212
208
213
209
/* Enable the poweroff interrupt only when the pin will be back to a nonactive state. */
214
210
configure_power_pin_interrupt (power_pin_callback_enable_poweroff , GPIO_INT_EDGE_FALLING );
@@ -224,12 +220,12 @@ int slm_ctrl_pin_indicate(void)
224
220
if (k_work_delayable_is_pending (& indicate_work )) {
225
221
return 0 ;
226
222
}
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 );
229
225
if (err ) {
230
226
LOG_ERR ("GPIO_0 set error: %d" , err );
231
227
} 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 ));
233
229
}
234
230
#endif
235
231
return err ;
@@ -240,7 +236,7 @@ void slm_ctrl_pin_enter_idle(void)
240
236
LOG_INF ("Entering idle." );
241
237
int err ;
242
238
243
- gpio_remove_callback ( gpio_dev , & gpio_cb );
239
+ gpio_remove_callback_dt ( & power_pin_member , & gpio_cb );
244
240
245
241
err = configure_power_pin_interrupt (power_pin_callback_wakeup , GPIO_INT_LEVEL_LOW );
246
242
if (err ) {
@@ -270,7 +266,7 @@ void slm_ctrl_pin_enter_sleep_no_uninit(void)
270
266
{
271
267
LOG_INF ("Entering sleep." );
272
268
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 );
274
270
275
271
k_sleep (K_MSEC (100 ));
276
272
@@ -286,11 +282,11 @@ void slm_ctrl_pin_enter_shutdown(void)
286
282
287
283
/* De-configure GPIOs */
288
284
#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 );
291
287
#endif
292
288
#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 );
294
290
#endif
295
291
296
292
k_sleep (K_MSEC (100 ));
@@ -301,32 +297,35 @@ void slm_ctrl_pin_enter_shutdown(void)
301
297
302
298
void slm_ctrl_pin_init_gpios (void )
303
299
{
304
- if (!device_is_ready (gpio_dev )) {
300
+ #if POWER_PIN_IS_ENABLED
301
+ if (!gpio_is_ready_dt (& power_pin_member )) {
305
302
LOG_ERR ("GPIO controller not ready" );
306
303
return ;
307
304
}
308
305
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 );
311
309
#endif
312
310
313
311
#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 );
315
314
#endif
316
315
}
317
316
318
317
int slm_ctrl_pin_init (void )
319
318
{
320
319
int err ;
321
320
322
- k_work_init_delayable (& indicate_work , indicate_wk );
323
-
324
321
err = ext_xtal_control (true);
325
322
if (err < 0 ) {
326
323
LOG_ERR ("Failed to enable ext XTAL: %d" , err );
327
324
return err ;
328
325
}
329
326
#if POWER_PIN_IS_ENABLED
327
+ k_work_init_delayable (& indicate_work , indicate_wk );
328
+
330
329
/* Do not directly enable the poweroff interrupt so that only a full toggle triggers
331
330
* power off. This is because power on is triggered on low level, so if the pin is held
332
331
* down until SLM is fully initialized releasing it would directly trigger the power off.
0 commit comments