Skip to content

Commit 8db434f

Browse files
committed
Arduino.h: Change the pin number setting rules
Arduino pins are assigned numbers in the order of the GPIO pin numbers. D0 and D1 on the board are defined as aliases for these. In the current Arduino-Zephyr implementation, D0 = 0, D1=1, so pins that do not appear in the header cannot be used. We will change this to match the original Android method. Accordingly, we will specify the `gpios` node in the overlay. Sequential numbers will be assigned to the pins of the GPIO ports listed here in the order they are listed. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 211d5dc commit 8db434f

File tree

12 files changed

+161
-161
lines changed

12 files changed

+161
-161
lines changed

cores/arduino/Arduino.h

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2022 Dhruva Gole
3+
* Copyright (c) 2025 TOKITA Hiroshi
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -14,87 +15,38 @@
1415
#include <zephyr/drivers/adc.h>
1516
#include <zephyr/drivers/i2c.h>
1617

17-
#define DIGITAL_PIN_EXISTS(n, p, i, dev, num) \
18-
(((dev == DT_REG_ADDR(DT_PHANDLE_BY_IDX(n, p, i))) && \
19-
(num == DT_PHA_BY_IDX(n, p, i, pin))) \
20-
? 1 \
21-
: 0)
22-
23-
/* Check all pins are defined only once */
24-
#define DIGITAL_PIN_CHECK_UNIQUE(i, _) \
25-
((DT_FOREACH_PROP_ELEM_SEP_VARGS( \
26-
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
27-
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i)), \
28-
DT_PHA_BY_IDX(DT_PATH(zephyr_user), digital_pin_gpios, i, pin))) == 1)
29-
30-
#if !LISTIFY(DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios), DIGITAL_PIN_CHECK_UNIQUE, (&&))
31-
#error "digital_pin_gpios has duplicate definition"
32-
#endif
33-
34-
#undef DIGITAL_PIN_CHECK_UNIQUE
18+
#define ZARD_ADD_NGPIOS(i, n, p) DT_PROP(DT_PROP_BY_IDX(n, p, i), ngpios) +
19+
#define ZARD_ACCUM_NGPIOS(n, p, i, nd) \
20+
COND_CODE_1(DT_SAME_NODE(DT_PROP_BY_IDX(n, p, i), nd), \
21+
(LISTIFY(i, ZARD_ADD_NGPIOS, (), n, p)), ())
22+
#define ZARD_GLOBAL_GPIO_NUM(n, p, i) \
23+
DT_FOREACH_PROP_ELEM_VARGS(DT_PATH(zephyr_user), gpios, ZARD_ACCUM_NGPIOS, \
24+
DT_PHANDLE_BY_IDX(n, p, i)) \
25+
DT_PHA_BY_IDX(n, p, i, pin)
3526

3627
#ifndef LED_BUILTIN
37-
38-
/* Return the index of it if matched, oterwise return 0 */
39-
#define LED_BUILTIN_INDEX_BY_REG_AND_PINNUM(n, p, i, dev, num) \
40-
(DIGITAL_PIN_EXISTS(n, p, i, dev, num) ? i : 0)
41-
42-
/* Only matched pin returns non-zero value, so the sum is matched pin's index */
43-
#define DIGITAL_PIN_GPIOS_FIND_PIN(dev, pin) \
44-
DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, \
45-
LED_BUILTIN_INDEX_BY_REG_AND_PINNUM, (+), dev, pin)
46-
4728
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), builtin_led_gpios) && \
4829
(DT_PROP_LEN(DT_PATH(zephyr_user), builtin_led_gpios) > 0)
49-
50-
#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS( \
51-
DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, (+), \
52-
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
53-
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin)) > 0)
54-
#warning "pin not found in digital_pin_gpios"
55-
#else
56-
#define LED_BUILTIN \
57-
DIGITAL_PIN_GPIOS_FIND_PIN( \
58-
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0)), \
59-
DT_PHA_BY_IDX(DT_PATH(zephyr_user), builtin_led_gpios, 0, pin))
60-
#endif
61-
62-
/* If digital-pin-gpios is not defined, tries to use the led0 alias */
30+
#define LED_BUILTIN ZARD_GLOBAL_GPIO_NUM(DT_PATH(zephyr_user), builtin_led_gpios, 0)
6331
#elif DT_NODE_EXISTS(DT_ALIAS(led0))
64-
65-
#if !(DT_FOREACH_PROP_ELEM_SEP_VARGS(DT_PATH(zephyr_user), digital_pin_gpios, DIGITAL_PIN_EXISTS, \
66-
(+), DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
67-
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin)) > 0)
68-
#warning "pin not found in digital_pin_gpios"
69-
#else
70-
#define LED_BUILTIN \
71-
DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_ALIAS(led0), gpios, 0)), \
72-
DT_PHA_BY_IDX(DT_ALIAS(led0), gpios, 0, pin))
32+
#define LED_BUILTIN ZARD_GLOBAL_GPIO_NUM(DT_ALIAS(led0), gpios, 0)
7333
#endif
74-
75-
#endif // builtin_led_gpios
76-
7734
#endif // LED_BUILTIN
7835

79-
#define DN_ENUMS(n, p, i) D##i = i
80-
8136
/*
8237
* expand as
83-
* enum digitalPins { D0, D1, ... LED... NUM_OF_DIGITAL_PINS };
38+
* enum digitalPins { D0, D1, ... };
8439
*/
40+
#define ZARD_DN_ENUMS(n, p, i) D##i = ZARD_GLOBAL_GPIO_NUM(n, p, i)
8541
enum digitalPins {
86-
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, DN_ENUMS, (, )),
87-
NUM_OF_DIGITAL_PINS
42+
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, ZARD_DN_ENUMS, (,))
8843
};
8944

9045
#ifdef CONFIG_ADC
91-
92-
#define AN_ENUMS(n, p, i) A ## i = DIGITAL_PIN_GPIOS_FIND_PIN( \
93-
DT_REG_ADDR(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), p, i)), \
94-
DT_PHA_BY_IDX(DT_PATH(zephyr_user), p, i, pin)),
95-
enum analogPins { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user),
96-
adc_pin_gpios, AN_ENUMS) };
97-
46+
#define ZARD_AN_ENUMS(n, p, i) A##i = ZARD_GLOBAL_GPIO_NUM(n, p, i)
47+
enum analogPins {
48+
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), adc_pin_gpios, ZARD_AN_ENUMS, (,))
49+
};
9850
#endif
9951

10052
void interrupts(void);

0 commit comments

Comments
 (0)