@@ -16,7 +16,11 @@ constexpr struct device * local_gpio_port(int) {
1616 return nullptr ;
1717}
1818
19- constexpr pin_size_t global_gpio_pin (struct device * port, int pin) {
19+ constexpr uint32_t local_gpio_dt_flags (int ) {
20+ return 0 ;
21+ }
22+
23+ constexpr pin_size_t global_gpio_pin (const struct device * port, int pin) {
2024 return pin;
2125}
2226
@@ -150,8 +154,8 @@ struct pin_timer {
150154 struct gpio_dt_spec spec;
151155};
152156
153- struct pin_timer arduino_pin_timers[ARRAY_SIZE(arduino_pins) ];
154- struct pin_timer arduino_pin_timers_timeout[ARRAY_SIZE(arduino_pins) ];
157+ struct pin_timer arduino_pin_timers[GPIO_PORT_NUMS ];
158+ struct pin_timer arduino_pin_timers_timeout[GPIO_PORT_NUMS ];
155159
156160void tone_expiry_cb (struct k_timer *timer) {
157161 struct pin_timer * pt = (struct pin_timer *)k_timer_user_data_get (timer);
@@ -160,17 +164,7 @@ void tone_expiry_cb(struct k_timer *timer) {
160164
161165void tone_timeout_cb (struct k_timer *timer) {
162166 struct pin_timer * pt = (struct pin_timer *)k_timer_user_data_get (timer);
163- noTone (pt->spec .pin );
164- }
165-
166- void tone_expiry_cb (struct k_timer *timer) {
167- struct pin_timer * pt = (struct pin_timer *)k_timer_user_data_get (timer);
168- gpio_pin_toggle (pt->gpio , pt->pin );
169- }
170-
171- void tone_timeout_cb (struct k_timer *timer) {
172- struct pin_timer * pt = (struct pin_timer *)k_timer_user_data_get (timer);
173- noTone (global_gpio_pin (pt->gpio , pt->pin ));
167+ noTone (global_gpio_pin (pt->spec .port , pt->spec .pin ));
174168}
175169
176170} // namespace
@@ -210,28 +204,32 @@ PinStatus digitalRead(pin_size_t pinNumber) {
210204
211205void tone (pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
212206 struct k_timer *timer = &arduino_pin_timers[pinNumber].timer ;
213- const struct gpio_dt_spec *spec = &arduino_pins [pinNumber].spec ;
207+ const struct gpio_dt_spec *spec = &arduino_pin_timers [pinNumber].spec ;
214208 k_timeout_t timeout;
215209
210+ arduino_pin_timers[pinNumber].spec .port = local_gpio_port (pinNumber);
211+ arduino_pin_timers[pinNumber].spec .pin = local_gpio_pin (pinNumber);
212+ arduino_pin_timers[pinNumber].spec .dt_flags = local_gpio_dt_flags (pinNumber);
213+
216214 pinMode (pinNumber, OUTPUT);
217215
218216 if (frequency == 0 ) {
219- gpio_pin_set ( local_gpio_port ( pinNumber), local_gpio_pin (pinNumber) , 0 );
217+ gpio_pin_set_dt (&arduino_pin_timers[ pinNumber]. spec , 0 );
220218 return ;
221219 }
222220
223221 timeout = K_NSEC (NSEC_PER_SEC / (2 * frequency));
224222
225223 k_timer_init (timer, tone_expiry_cb, NULL );
226224 k_timer_user_data_set (timer, &arduino_pin_timers[pinNumber]);
227- gpio_pin_set_dt (spec, 1 );
225+ gpio_pin_set_dt (&arduino_pin_timers[pinNumber]. spec , 1 );
228226 k_timer_start (timer, timeout, timeout);
229227
230228 if (duration > 0 ) {
231229 timer = &arduino_pin_timers_timeout[pinNumber].timer ;
232- arduino_pin_timers_timeout[pinNumber].spec .port = nullptr ;
233- arduino_pin_timers_timeout[pinNumber].spec .pin = pinNumber;
234- arduino_pin_timers_timeout[pinNumber].spec .dt_flags = 0 ;
230+ arduino_pin_timers_timeout[pinNumber].spec .port = local_gpio_port (pinNumber) ;
231+ arduino_pin_timers_timeout[pinNumber].spec .pin = local_gpio_pin ( pinNumber) ;
232+ arduino_pin_timers_timeout[pinNumber].spec .dt_flags = local_gpio_dt_flags (pinNumber) ;
235233 k_timer_init (timer, tone_timeout_cb, NULL );
236234 k_timer_user_data_set (timer, &arduino_pin_timers_timeout[pinNumber]);
237235 k_timer_start (timer, K_MSEC (duration), K_NO_WAIT);
@@ -240,7 +238,7 @@ void tone(pin_size_t pinNumber, unsigned int frequency, unsigned long duration)
240238
241239void noTone (pin_size_t pinNumber) {
242240 k_timer_stop (&arduino_pin_timers[pinNumber].timer );
243- gpio_pin_set_dt (&arduino_pins [pinNumber], 0 );
241+ gpio_pin_set_dt (&arduino_pin_timers [pinNumber]. spec , 0 );
244242}
245243
246244void delay (unsigned long ms) { k_sleep (K_MSEC (ms)); }
0 commit comments