@@ -260,53 +260,59 @@ PinStatus digitalRead(pin_size_t pinNumber) {
260
260
struct pin_timer {
261
261
struct k_timer timer;
262
262
struct gpio_dt_spec spec;
263
+ uint32_t count;
263
264
};
264
265
265
- struct pin_timer arduino_pin_timers[sum_ngpios];
266
- struct pin_timer arduino_pin_timers_timeout[sum_ngpios];
266
+ struct pin_timer arduino_pin_timers[20 ];
267
267
268
268
void tone_expiry_cb (struct k_timer *timer)
269
269
{
270
- struct pin_timer *pt = (struct pin_timer *)k_timer_user_data_get (timer);
271
- gpio_pin_toggle_dt (&pt->spec );
272
- }
270
+ struct pin_timer *pt = CONTAINER_OF (timer, struct pin_timer , timer);
273
271
274
- void tone_timeout_cb (struct k_timer *timer) {
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 ));
272
+ if (pt->count == 0 ) {
273
+ k_timer_stop (timer);
274
+ gpio_pin_set_dt (&pt->spec , 0 );
275
+ } else {
276
+ gpio_pin_toggle_dt (&pt->spec );
277
+ if (pt->count != UINT32_MAX) {
278
+ pt->count --;
279
+ }
280
+ }
277
281
}
278
282
279
283
void tone (pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
280
284
struct k_timer *timer = &arduino_pin_timers[pinNumber].timer ;
281
285
const struct gpio_dt_spec *spec = &arduino_pin_timers[pinNumber].spec ;
282
286
k_timeout_t timeout;
287
+ uint32_t count;
288
+
283
289
arduino_pin_timers[pinNumber].spec .port = local_gpio_port (pinNumber);
284
290
arduino_pin_timers[pinNumber].spec .pin = local_gpio_pin (pinNumber);
285
291
arduino_pin_timers[pinNumber].spec .dt_flags = local_gpio_dt_flags (pinNumber);
286
292
287
293
pinMode (pinNumber, OUTPUT);
294
+ k_timer_stop (&arduino_pin_timers[pinNumber].timer );
288
295
289
296
if (frequency == 0 ) {
290
297
gpio_pin_set_dt (&arduino_pin_timers[pinNumber].spec , 0 );
291
298
return ;
292
299
}
293
300
301
+ if (duration == 0 ) {
302
+ count = UINT32_MAX;
303
+ } else {
304
+ count = ((uint64_t )duration * (uint64_t )frequency) / 500ULL ;
305
+ }
306
+
294
307
timeout = K_NSEC (NSEC_PER_SEC / (2 * frequency));
308
+ if (timeout.ticks == 0 ) {
309
+ timeout.ticks = 1 ;
310
+ }
311
+ arduino_pin_timers[pinNumber].count = count;
295
312
296
313
k_timer_init (timer, tone_expiry_cb, NULL );
297
- k_timer_user_data_set (timer, &arduino_pin_timers[pinNumber]);
298
- gpio_pin_set_dt (&arduino_pin_timers[pinNumber].spec , 1 );
314
+ gpio_pin_set_dt (&arduino_pin_timers[pinNumber].spec , 0 );
299
315
k_timer_start (timer, timeout, timeout);
300
-
301
- if (duration > 0 ) {
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);
306
- k_timer_init (timer, tone_timeout_cb, NULL );
307
- k_timer_user_data_set (timer, &arduino_pin_timers_timeout[pinNumber]);
308
- k_timer_start (timer, K_MSEC (duration), K_NO_WAIT);
309
- }
310
316
}
311
317
312
318
void noTone (pin_size_t pinNumber) {
0 commit comments