@@ -260,53 +260,59 @@ PinStatus digitalRead(pin_size_t pinNumber) {
260260struct pin_timer {
261261 struct k_timer timer;
262262 struct gpio_dt_spec spec;
263+ uint32_t count;
263264};
264265
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 ];
267267
268268void tone_expiry_cb (struct k_timer *timer)
269269{
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);
273271
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+ }
277281}
278282
279283void tone (pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
280284 struct k_timer *timer = &arduino_pin_timers[pinNumber].timer ;
281285 const struct gpio_dt_spec *spec = &arduino_pin_timers[pinNumber].spec ;
282286 k_timeout_t timeout;
287+ uint32_t count;
288+
283289 arduino_pin_timers[pinNumber].spec .port = local_gpio_port (pinNumber);
284290 arduino_pin_timers[pinNumber].spec .pin = local_gpio_pin (pinNumber);
285291 arduino_pin_timers[pinNumber].spec .dt_flags = local_gpio_dt_flags (pinNumber);
286292
287293 pinMode (pinNumber, OUTPUT);
294+ k_timer_stop (&arduino_pin_timers[pinNumber].timer );
288295
289296 if (frequency == 0 ) {
290297 gpio_pin_set_dt (&arduino_pin_timers[pinNumber].spec , 0 );
291298 return ;
292299 }
293300
301+ if (duration == 0 ) {
302+ count = UINT32_MAX;
303+ } else {
304+ count = ((uint64_t )duration * (uint64_t )frequency) / 500ULL ;
305+ }
306+
294307 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;
295312
296313 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 );
299315 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- }
310316}
311317
312318void noTone (pin_size_t pinNumber) {
0 commit comments