@@ -16,7 +16,11 @@ constexpr struct device * local_gpio_port(int) {
16
16
return nullptr ;
17
17
}
18
18
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) {
20
24
return pin;
21
25
}
22
26
@@ -150,8 +154,8 @@ struct pin_timer {
150
154
struct gpio_dt_spec spec;
151
155
};
152
156
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 ];
155
159
156
160
void tone_expiry_cb (struct k_timer *timer) {
157
161
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) {
160
164
161
165
void tone_timeout_cb (struct k_timer *timer) {
162
166
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 ));
174
168
}
175
169
176
170
} // namespace
@@ -210,28 +204,32 @@ PinStatus digitalRead(pin_size_t pinNumber) {
210
204
211
205
void tone (pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
212
206
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 ;
214
208
k_timeout_t timeout;
215
209
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
+
216
214
pinMode (pinNumber, OUTPUT);
217
215
218
216
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 );
220
218
return ;
221
219
}
222
220
223
221
timeout = K_NSEC (NSEC_PER_SEC / (2 * frequency));
224
222
225
223
k_timer_init (timer, tone_expiry_cb, NULL );
226
224
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 );
228
226
k_timer_start (timer, timeout, timeout);
229
227
230
228
if (duration > 0 ) {
231
229
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) ;
235
233
k_timer_init (timer, tone_timeout_cb, NULL );
236
234
k_timer_user_data_set (timer, &arduino_pin_timers_timeout[pinNumber]);
237
235
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)
240
238
241
239
void noTone (pin_size_t pinNumber) {
242
240
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 );
244
242
}
245
243
246
244
void delay (unsigned long ms) { k_sleep (K_MSEC (ms)); }
0 commit comments