Skip to content

Commit a719251

Browse files
committed
x
1 parent 4763faa commit a719251

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

cores/arduino/Arduino.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#define GPIO_PIN_NUMS \
2727
(DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), gpios, PROP_NGPIOS, (+)))
2828

29+
#define GPIO_PORT_NUMS DT_PROP_LEN(DT_PATH(zephyr_user), gpios)
30+
2931
#ifndef LED_BUILTIN
3032
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), builtin_led_gpios) && \
3133
(DT_PROP_LEN(DT_PATH(zephyr_user), builtin_led_gpios) > 0)

cores/arduino/zephyrCommon.cpp

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <Arduino.h>
88
#include "zephyrInternal.h"
99

10-
1110
namespace {
1211
struct gpio_dt_spec local_gpio_dt(int) {
1312
struct gpio_dt_spec sp = {0};
@@ -26,10 +25,6 @@ constexpr pin_size_t global_gpio_pin(struct device* port, int pin) {
2625
return pin;
2726
}
2827

29-
static const uint32_t xpins[] = {16u, 32u}; // exclusive-upper
30-
static const int port_num = sizeof(xpins) / sizeof(xpins[0]);
31-
static const int pin_num = 32u;
32-
3328
template <class N, class Head> constexpr const N max_in_list(const N max, const Head &head)
3429
{
3530
return (max >= head) ? max : head;
@@ -59,7 +54,7 @@ struct gpio_port_callback {
5954
struct arduino_callback handlers[max_ngpios];
6055
gpio_port_pins_t pins;
6156
const struct device *dev;
62-
} port_callback[port_num] = {0};
57+
} port_callback[GPIO_PORT_NUMS] = {0};
6358

6459
struct gpio_port_callback *find_gpio_port_callback(const struct device *dev)
6560
{
@@ -185,39 +180,37 @@ void yield(void) {
185180
* A high physical level will be interpreted as value 1
186181
*/
187182
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
188-
const gpio_dt_spec spec = local_gpio_dt(pinNumber);
189183
if (pinMode == INPUT) { // input mode
190-
gpio_pin_configure_dt(&spec, GPIO_INPUT | GPIO_ACTIVE_HIGH);
184+
gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber),
185+
GPIO_INPUT | GPIO_ACTIVE_HIGH);
191186
} else if (pinMode == INPUT_PULLUP) { // input with internal pull-up
192-
gpio_pin_configure_dt(&spec, GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_HIGH);
187+
gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber),
188+
GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_HIGH);
193189
} else if (pinMode == INPUT_PULLDOWN) { // input with internal pull-down
194-
gpio_pin_configure_dt(&spec, GPIO_INPUT | GPIO_PULL_DOWN | GPIO_ACTIVE_HIGH);
190+
gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber),
191+
GPIO_INPUT | GPIO_PULL_DOWN | GPIO_ACTIVE_HIGH);
195192
} else if (pinMode == OUTPUT) { // output mode
196-
gpio_pin_configure_dt(&spec, GPIO_OUTPUT_LOW | GPIO_ACTIVE_HIGH);
193+
gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber),
194+
GPIO_OUTPUT_LOW | GPIO_ACTIVE_HIGH);
197195
}
198196
}
199197

200198
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
201-
const gpio_dt_spec spec = local_gpio_dt(pinNumber);
202-
gpio_pin_set_dt(&spec, status);
199+
gpio_pin_set(local_gpio_port(pinNumber), local_gpio_pin(pinNumber), status);
203200
}
204201

205202
PinStatus digitalRead(pin_size_t pinNumber) {
206-
const gpio_dt_spec spec = local_gpio_dt(pinNumber);
207-
return (gpio_pin_get_dt(&spec) == 1) ? HIGH : LOW;
203+
return (gpio_pin_get(local_gpio_port(pinNumber), local_gpio_pin(pinNumber)) == 1) ? HIGH : LOW;
208204
}
209205

210-
struct k_timer arduino_pin_timers[GPIO_PIN_NUMS];
211-
struct k_timer arduino_pin_timers_timeout[GPIO_PIN_NUMS];
212-
213206
void tone_expiry_cb(struct k_timer *timer) {
214-
const struct gpio_dt_spec *spec = (gpio_dt_spec*)k_timer_user_data_get(timer);
215-
gpio_pin_toggle_dt(spec);
207+
struct pin_timer* pt = (struct pin_timer*)k_timer_user_data_get(timer);
208+
gpio_pin_toggle(pt->gpio, pt->pin);
216209
}
217210

218211
void tone_timeout_cb(struct k_timer *timer) {
219-
pin_size_t pinNumber = (pin_size_t)(uintptr_t)k_timer_user_data_get(timer);
220-
noTone(pinNumber);
212+
struct pin_timer* pt = (struct pin_timer*)k_timer_user_data_get(timer);
213+
noTone(global_gpio_pin(pt->gpio, pt->pin));
221214
}
222215

223216
void tone(pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
@@ -228,7 +221,7 @@ void tone(pin_size_t pinNumber, unsigned int frequency, unsigned long duration)
228221
pinMode(pinNumber, OUTPUT);
229222

230223
if (frequency == 0) {
231-
gpio_pin_set_dt(&spec, 0);
224+
gpio_pin_set(local_gpio_port(pinNumber), local_gpio_pin(pinNumber), 0);
232225
return;
233226
}
234227

@@ -410,31 +403,30 @@ long random(long max) {
410403
unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout) {
411404
struct k_timer timer;
412405
int64_t start, end, delta = 0;
413-
const struct gpio_dt_spec spec = local_gpio_dt(pinNumber);
414406

415407
k_timer_init(&timer, NULL, NULL);
416408
k_timer_start(&timer, K_MSEC(timeout), K_NO_WAIT);
417409

418-
if (!gpio_is_ready_dt(&spec)) {
410+
if (!device_is_ready(local_gpio_port(pinNumber))) {
419411
goto cleanup;
420412
}
421413

422-
if (!gpio_pin_is_input_dt(&spec)) {
414+
if (!device_is_ready(local_gpio_port(pinNumber))) {
423415
goto cleanup;
424416
}
425417

426-
while(gpio_pin_get_dt(&spec) == state && k_timer_status_get(&timer) == 0);
418+
while(gpio_pin_get(local_gpio_port(pinNumber), local_gpio_pin(pinNumber)) == state && k_timer_status_get(&timer) == 0);
427419
if (k_timer_status_get(&timer) > 0) {
428420
goto cleanup;
429421
}
430422

431-
while(gpio_pin_get_dt(&spec) != state && k_timer_status_get(&timer) == 0);
423+
while(gpio_pin_get(local_gpio_port(pinNumber), local_gpio_pin(pinNumber)) != state && k_timer_status_get(&timer) == 0);
432424
if (k_timer_status_get(&timer) > 0) {
433425
goto cleanup;
434426
}
435427

436428
start = k_uptime_ticks();
437-
while(gpio_pin_get_dt(&spec) == state && k_timer_status_get(&timer) == 0);
429+
while(gpio_pin_get(local_gpio_port(pinNumber), local_gpio_pin(pinNumber)) == state && k_timer_status_get(&timer) == 0);
438430
if (k_timer_status_get(&timer) > 0) {
439431
goto cleanup;
440432
}

0 commit comments

Comments
 (0)