@@ -103,9 +103,23 @@ constexpr const N max_in_list(const N max, const Head &head, const Tail &...tail
103
103
return max_in_list ((max >= head) ? max : head, tail...);
104
104
}
105
105
106
+ template <class N , class Head >
107
+ constexpr const N sum_in_list (const N acc, const Head& head)
108
+ {
109
+ return acc + head;
110
+ }
111
+
112
+ template <class N , class Head , class ... Tail>
113
+ constexpr const N sum_in_list (const N acc, const Head& head, const Tail&... tail)
114
+ {
115
+ return sum_in_list (acc + head, tail...);
116
+ }
117
+
106
118
#define GPIO_NGPIOS (n, p, i ) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
107
119
const int max_ngpios = max_in_list(
108
120
0 , DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
121
+ const int sum_ngpios = sum_in_list(
122
+ 0 , DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
109
123
110
124
/*
111
125
* GPIO callback implementation
@@ -248,49 +262,56 @@ struct pin_timer {
248
262
struct gpio_dt_spec spec;
249
263
};
250
264
251
- struct k_timer arduino_pin_timers[ARRAY_SIZE(arduino_pins) ];
252
- struct k_timer arduino_pin_timers_timeout[ARRAY_SIZE(arduino_pins) ];
265
+ struct pin_timer arduino_pin_timers[sum_ngpios ];
266
+ struct pin_timer arduino_pin_timers_timeout[sum_ngpios ];
253
267
254
- void tone_expiry_cb (struct k_timer *timer) {
255
- const struct gpio_dt_spec *spec = (gpio_dt_spec*)k_timer_user_data_get (timer);
256
- gpio_pin_toggle_dt (spec);
268
+ void tone_expiry_cb (struct k_timer *timer)
269
+ {
270
+ struct pin_timer *pt = (struct pin_timer *)k_timer_user_data_get (timer);
271
+ gpio_pin_toggle_dt (&pt->spec );
257
272
}
258
273
259
274
void tone_timeout_cb (struct k_timer *timer) {
260
- pin_size_t pinNumber = (pin_size_t )( uintptr_t )k_timer_user_data_get (timer);
261
- noTone (pinNumber );
275
+ struct pin_timer *pt = (struct pin_timer * )k_timer_user_data_get (timer);
276
+ noTone (global_gpio_pin (pt-> spec . port , pt-> spec . pin ) );
262
277
}
263
278
264
279
void tone (pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
265
- struct k_timer *timer = &arduino_pin_timers[pinNumber];
266
- const struct gpio_dt_spec *spec = &arduino_pins [pinNumber];
280
+ struct k_timer *timer = &arduino_pin_timers[pinNumber]. timer ;
281
+ const struct gpio_dt_spec *spec = &arduino_pin_timers [pinNumber]. spec ;
267
282
k_timeout_t timeout;
283
+ arduino_pin_timers[pinNumber].spec .port = local_gpio_port (pinNumber);
284
+ arduino_pin_timers[pinNumber].spec .pin = local_gpio_pin (pinNumber);
285
+ arduino_pin_timers[pinNumber].spec .dt_flags = local_gpio_dt_flags (pinNumber);
268
286
269
287
pinMode (pinNumber, OUTPUT);
270
288
271
289
if (frequency == 0 ) {
272
- gpio_pin_set_dt (spec, 0 );
290
+ gpio_pin_set_dt (&arduino_pin_timers[pinNumber]. spec , 0 );
273
291
return ;
274
292
}
275
293
276
294
timeout = K_NSEC (NSEC_PER_SEC / (2 * frequency));
277
295
278
296
k_timer_init (timer, tone_expiry_cb, NULL );
279
- k_timer_user_data_set (timer, ( void *)spec );
280
- gpio_pin_set_dt (spec, 1 );
297
+ k_timer_user_data_set (timer, &arduino_pin_timers[pinNumber] );
298
+ gpio_pin_set_dt (&arduino_pin_timers[pinNumber]. spec , 1 );
281
299
k_timer_start (timer, timeout, timeout);
282
300
283
301
if (duration > 0 ) {
284
- timer = &arduino_pin_timers_timeout[pinNumber];
302
+ timer = &arduino_pin_timers_timeout[pinNumber].timer ;
303
+ arduino_pin_timers_timeout[pinNumber].spec .port = local_gpio_port (pinNumber);
304
+ arduino_pin_timers_timeout[pinNumber].spec .pin = local_gpio_pin (pinNumber);
305
+ arduino_pin_timers_timeout[pinNumber].spec .dt_flags = local_gpio_dt_flags (pinNumber);
285
306
k_timer_init (timer, tone_timeout_cb, NULL );
286
- k_timer_user_data_set (timer, ( void *)( uintptr_t ) pinNumber);
307
+ k_timer_user_data_set (timer, &arduino_pin_timers_timeout[ pinNumber] );
287
308
k_timer_start (timer, K_MSEC (duration), K_NO_WAIT);
288
309
}
289
310
}
290
311
291
312
void noTone (pin_size_t pinNumber) {
292
- k_timer_stop (&arduino_pin_timers[pinNumber]);
293
- gpio_pin_set_dt (&arduino_pins [pinNumber], 0 );
313
+ k_timer_stop (&arduino_pin_timers[pinNumber]. timer );
314
+ gpio_pin_set_dt (&arduino_pin_timers [pinNumber]. spec , 0 );
294
315
}
295
316
296
317
void delay (unsigned long ms) { k_sleep (K_MSEC (ms)); }
0 commit comments