11/*
22 * Copyright (c) 2022 Dhruva Gole
3+ * Copyright (c) 2025 TOKITA Hiroshi
34 *
45 * SPDX-License-Identifier: Apache-2.0
56 */
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
35-
36- #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-
47- #if DT_NODE_HAS_PROP (DT_PATH (zephyr_user ), builtin_led_gpios ) && \
48- (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 */
63- #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))
73- #endif
74-
75- #endif // builtin_led_gpios
76-
77- #endif // LED_BUILTIN
78-
79- #define DN_ENUMS (n , p , i ) D##i = i
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), gpio_ports, ZARD_ACCUM_NGPIOS, \
24+ DT_PHANDLE_BY_IDX(n, p, i)) \
25+ DT_PHA_BY_IDX(n, p, i, pin)
8026
8127/*
8228 * expand as
83- * enum digitalPins { D0, D1, ... LED... NUM_OF_DIGITAL_PINS };
29+ * enum digitalPins { D0, D1, ... };
8430 */
31+ #define ZARD_DN_ENUMS (n , p , i ) D##i = ZARD_GLOBAL_GPIO_NUM(n, p, i)
8532enum digitalPins {
86- DT_FOREACH_PROP_ELEM_SEP (DT_PATH (zephyr_user ), digital_pin_gpios , DN_ENUMS , (, )),
87- NUM_OF_DIGITAL_PINS
33+ DT_FOREACH_PROP_ELEM_SEP (DT_PATH (zephyr_user ), digital_pin_gpios , ZARD_DN_ENUMS , (, )),
34+ NUM_OF_DIGITAL_PINS
8835};
8936
9037#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-
38+ #define ZARD_AN_ENUMS (n , p , i ) A##i = ZARD_GLOBAL_GPIO_NUM(n, p, i)
39+ enum analogPins {
40+ DT_FOREACH_PROP_ELEM_SEP (DT_PATH (zephyr_user ), adc_pin_gpios , ZARD_AN_ENUMS , (, )),
41+ NUM_OF_ANALOG_PINS
42+ };
9843#endif
9944
10045void interrupts (void );
@@ -103,6 +48,16 @@ void noInterrupts(void);
10348int digitalPinToInterrupt (pin_size_t pin );
10449
10550#include <variant.h>
51+
52+ #ifndef LED_BUILTIN
53+ #if DT_NODE_HAS_PROP (DT_PATH (zephyr_user ), builtin_led_gpios ) \
54+ && (DT_PROP_LEN (DT_PATH (zephyr_user ), builtin_led_gpios ) > 0 )
55+ #define LED_BUILTIN ZARD_GLOBAL_GPIO_NUM(DT_PATH(zephyr_user), builtin_led_gpios, 0)
56+ #elif DT_NODE_EXISTS (DT_ALIAS (led0 ))
57+ #define LED_BUILTIN ZARD_GLOBAL_GPIO_NUM(DT_ALIAS(led0), gpios, 0)
58+ #endif
59+ #endif // LED_BUILTIN
60+
10661#ifdef __cplusplus
10762#include <zephyrPrint.h>
10863#include <zephyrSerial.h>
0 commit comments