@@ -12,6 +12,69 @@ static const struct gpio_dt_spec arduino_pins[] = {DT_FOREACH_PROP_ELEM_SEP(
1212
1313namespace {
1414
15+ #define DEVICE_GPIO (n, p, i ) DEVICE_DT_GET(DT_PROP_BY_IDX(n, p, i))
16+ constexpr const struct device *gpios[] = {
17+ DT_FOREACH_PROP_ELEM_SEP (DT_PATH (zephyr_user), gpio_ports, DEVICE_GPIO, (,))
18+ };
19+
20+ #define PROP_NGPIOS (n, p, i ) DT_PROP(DT_PROP_BY_IDX(n, p, i), ngpios)
21+ constexpr uint32_t pins[] = {
22+ DT_FOREACH_PROP_ELEM_SEP (DT_PATH (zephyr_user), gpio_ports, PROP_NGPIOS, (,))
23+ };
24+
25+ constexpr inline const struct device *local_gpio_port_r (pin_size_t pin,
26+ const struct device *const *ctrl,
27+ const uint32_t accum,
28+ const uint32_t *end, size_t n) {
29+ return (n == 0 ) ? nullptr
30+ : (pin < accum + end[0 ]) ? ctrl[0 ]
31+ : local_gpio_port_r (pin, ctrl + 1 , accum + end[0 ], end + 1 , n - 1 );
32+ }
33+
34+ constexpr inline size_t port_index_r (const struct device *target,
35+ const struct device *const *table, pin_size_t idx, size_t n) {
36+ return (n == 0 ) ? size_t (-1 )
37+ : (target == table[0 ]) ? idx
38+ : port_index_r (target, table + 1 , idx + 1 , n - 1 );
39+ }
40+
41+ constexpr inline const struct device *local_gpio_port (pin_size_t gpin) {
42+ return local_gpio_port_r (gpin, gpios, 0 , pins, ARRAY_SIZE (gpios));
43+ }
44+
45+ constexpr inline pin_size_t port_idx (pin_size_t gpin) {
46+ return port_index_r (local_gpio_port (gpin), gpios, 0 , ARRAY_SIZE (gpios));
47+ }
48+
49+ constexpr inline pin_size_t end_accum_r (const uint32_t accum, const uint32_t *end, size_t n) {
50+ return (n == 0 ) ? accum : end_accum_r (accum + end[0 ], end + 1 , n - 1 );
51+ }
52+
53+ constexpr inline pin_size_t end_accum (size_t n) {
54+ return end_accum_r (0 , pins, n);
55+ }
56+
57+ constexpr inline pin_size_t local_gpio_pin (pin_size_t gpin) {
58+ return port_idx (gpin) == pin_size_t (-1 ) ? pin_size_t (-1 ) : gpin - end_accum (port_idx (gpin));
59+ }
60+
61+ constexpr inline pin_size_t global_gpio_pin_ (size_t port_idx, pin_size_t lpin) {
62+ return port_idx == size_t (-1 ) ? size_t (-1 ) : end_accum (port_idx) + lpin;
63+ }
64+
65+ constexpr inline pin_size_t global_gpio_pin (const struct device *lport, pin_size_t lpin) {
66+ return global_gpio_pin_ (port_index_r (lport, gpios, 0 , ARRAY_SIZE (gpios)), lpin);
67+ }
68+
69+ inline int global_gpio_pin_get (pin_size_t pinNumber) {
70+ return gpio_pin_get (local_gpio_port (pinNumber), local_gpio_pin (pinNumber));
71+ }
72+
73+ inline int global_gpio_pin_configure (pin_size_t pinNumber, int flags) {
74+ return gpio_pin_configure (local_gpio_port (pinNumber), local_gpio_pin (pinNumber), flags);
75+ }
76+
77+
1578/*
1679 * Calculate GPIO ports/pins number statically from devicetree configuration
1780 */
@@ -67,6 +130,8 @@ const int port_num =
67130#define GPIO_NGPIOS (n, p, i ) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
68131const int max_ngpios = max_in_list(
69132 0 , DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
133+ const int sum_ngpios = sum_of_list(
134+ 0 , DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
70135
71136/*
72137 * GPIO callback implementation
0 commit comments