Skip to content

Commit 8d04e8f

Browse files
committed
zephyrCommon: Allow compilation without digital_pin_gpios definition
Change it to behave as NOP if digital_pin_gpios is not defined. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 4bc1867 commit 8d04e8f

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

cores/arduino/Arduino.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
? 1 \
2121
: 0)
2222

23+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
2324
/* Check all pins are defined only once */
2425
#define DIGITAL_PIN_CHECK_UNIQUE(i, _) \
2526
((DT_FOREACH_PROP_ELEM_SEP_VARGS( \
@@ -30,6 +31,7 @@
3031
#if !LISTIFY(DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios), DIGITAL_PIN_CHECK_UNIQUE, (&&))
3132
#error "digital_pin_gpios has duplicate definition"
3233
#endif
34+
#endif
3335

3436
#undef DIGITAL_PIN_CHECK_UNIQUE
3537

@@ -62,6 +64,7 @@
6264
/* If digital-pin-gpios is not defined, tries to use the led0 alias */
6365
#elif DT_NODE_EXISTS(DT_ALIAS(led0))
6466

67+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
6568
#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, \
6669
(+), DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
6770
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) > 0)
@@ -71,6 +74,7 @@
7174
DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
7275
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin))
7376
#endif
77+
#endif
7478

7579
#endif // builtin_led_gpios
7680

@@ -83,7 +87,9 @@
8387
* enum digitalPins { D0, D1, ... LED... NUM_OF_DIGITAL_PINS };
8488
*/
8589
enum digitalPins {
90+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
8691
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, DN_ENUMS, (, )),
92+
#endif
8793
NUM_OF_DIGITAL_PINS
8894
};
8995

cores/arduino/zephyrCommon.cpp

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

10-
static const struct gpio_dt_spec arduino_pins[] = {DT_FOREACH_PROP_ELEM_SEP(
11-
DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))};
10+
static const struct gpio_dt_spec arduino_pins[] = {
11+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
12+
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, )),
13+
#endif
14+
};
1215

1316
namespace {
1417

@@ -61,12 +64,21 @@ constexpr const size_t is_first_appearance(const size_t &idx, const size_t &at,
6164
is_first_appearance(0, i, ((size_t)-1), DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)), \
6265
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, p, GET_DEVICE_VARGS, (, ), 0))
6366
const int port_num =
64-
sum_of_list(0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
65-
FIRST_APPEARANCE, (, )));
67+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
68+
sum_of_list(0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
69+
FIRST_APPEARANCE, (, )));
70+
#else
71+
0;
72+
#endif
6673

6774
#define GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
68-
const int max_ngpios = max_in_list(
69-
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
75+
const int max_ngpios =
76+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
77+
max_in_list(0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
78+
GPIO_NGPIOS, (, )));
79+
#else
80+
0;
81+
#endif
7082

7183
/*
7284
* GPIO callback implementation
@@ -82,7 +94,7 @@ struct gpio_port_callback {
8294
struct arduino_callback handlers[max_ngpios];
8395
gpio_port_pins_t pins;
8496
const struct device *dev;
85-
} port_callback[port_num] = {0};
97+
} port_callback[port_num];
8698

8799
struct gpio_port_callback *find_gpio_port_callback(const struct device *dev)
88100
{
@@ -108,6 +120,7 @@ void setInterruptHandler(pin_size_t pinNumber, voidFuncPtr func)
108120
}
109121
}
110122

123+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
111124
void handleGpioCallback(const struct device *port, struct gpio_callback *cb, uint32_t pins)
112125
{
113126
struct gpio_port_callback *pcb = (struct gpio_port_callback *)cb;
@@ -118,6 +131,7 @@ void handleGpioCallback(const struct device *port, struct gpio_callback *cb, uin
118131
}
119132
}
120133
}
134+
#endif
121135

122136
#ifdef CONFIG_PWM
123137

@@ -187,6 +201,7 @@ void yield(void) {
187201
* A high physical level will be interpreted as value 1
188202
*/
189203
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
204+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
190205
if (pinMode == INPUT) { // input mode
191206
gpio_pin_configure_dt(&arduino_pins[pinNumber],
192207
GPIO_INPUT | GPIO_ACTIVE_HIGH);
@@ -200,14 +215,21 @@ void pinMode(pin_size_t pinNumber, PinMode pinMode) {
200215
gpio_pin_configure_dt(&arduino_pins[pinNumber],
201216
GPIO_OUTPUT_LOW | GPIO_ACTIVE_HIGH);
202217
}
218+
#endif
203219
}
204220

205221
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
222+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
206223
gpio_pin_set_dt(&arduino_pins[pinNumber], status);
224+
#endif
207225
}
208226

209227
PinStatus digitalRead(pin_size_t pinNumber) {
228+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
210229
return (gpio_pin_get_dt(&arduino_pins[pinNumber]) == 1) ? HIGH : LOW;
230+
#else
231+
return LOW;
232+
#endif
211233
}
212234

213235
struct k_timer arduino_pin_timers[ARRAY_SIZE(arduino_pins)];
@@ -224,6 +246,7 @@ void tone_timeout_cb(struct k_timer *timer) {
224246
}
225247

226248
void tone(pin_size_t pinNumber, unsigned int frequency, unsigned long duration) {
249+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
227250
struct k_timer *timer = &arduino_pin_timers[pinNumber];
228251
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];
229252
k_timeout_t timeout;
@@ -248,11 +271,14 @@ void tone(pin_size_t pinNumber, unsigned int frequency, unsigned long duration)
248271
k_timer_user_data_set(timer, (void*)(uintptr_t)pinNumber);
249272
k_timer_start(timer, K_MSEC(duration), K_NO_WAIT);
250273
}
274+
#endif
251275
}
252276

253277
void noTone(pin_size_t pinNumber) {
278+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
254279
k_timer_stop(&arduino_pin_timers[pinNumber]);
255280
gpio_pin_set_dt(&arduino_pins[pinNumber], 0);
281+
#endif
256282
}
257283

258284
void delay(unsigned long ms) { k_sleep(K_MSEC(ms)); }
@@ -349,6 +375,7 @@ int analogRead(pin_size_t pinNumber)
349375

350376
void attachInterrupt(pin_size_t pinNumber, voidFuncPtr callback, PinStatus pinStatus)
351377
{
378+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
352379
struct gpio_port_callback *pcb;
353380
gpio_flags_t intmode = 0;
354381

@@ -380,6 +407,7 @@ void attachInterrupt(pin_size_t pinNumber, voidFuncPtr callback, PinStatus pinSt
380407
gpio_pin_interrupt_configure(arduino_pins[pinNumber].port, arduino_pins[pinNumber].pin, intmode);
381408
gpio_init_callback(&pcb->callback, handleGpioCallback, pcb->pins);
382409
gpio_add_callback(arduino_pins[pinNumber].port, &pcb->callback);
410+
#endif
383411
}
384412

385413
void detachInterrupt(pin_size_t pinNumber)
@@ -409,6 +437,7 @@ long random(long max) {
409437
#ifdef CONFIG_GPIO_GET_DIRECTION
410438

411439
unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout) {
440+
#if DT_NODE_EXISTS(DT_PATH(zephyr_user, digital_pin_gpios))
412441
struct k_timer timer;
413442
int64_t start, end, delta = 0;
414443
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];
@@ -446,6 +475,9 @@ unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout
446475
cleanup:
447476
k_timer_stop(&timer);
448477
return (unsigned long)delta;
478+
#else
479+
return 0;
480+
#endif
449481
}
450482

451483
#endif // CONFIG_GPIO_GET_DIRECTION

0 commit comments

Comments
 (0)