|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 TOKITA Hiroshi |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <Arduino.h> |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | + |
| 13 | +namespace zephyr { |
| 14 | +namespace arduino { |
| 15 | + |
| 16 | +constexpr struct gpio_dt_spec arduino_pins[] = { |
| 17 | + DT_FOREACH_PROP_ELEM_SEP( |
| 18 | + DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))}; |
| 19 | + |
| 20 | +/* |
| 21 | + * Calculate GPIO ports/pins number statically from devicetree configuration |
| 22 | + */ |
| 23 | + |
| 24 | +template <class N, class Head> constexpr N sum_of_list(const N sum, const Head &head) { |
| 25 | + return sum + head; |
| 26 | +} |
| 27 | + |
| 28 | +template <class N, class Head, class... Tail> |
| 29 | +constexpr N sum_of_list(const N sum, const Head &head, const Tail &...tail) { |
| 30 | + return sum_of_list(sum + head, tail...); |
| 31 | +} |
| 32 | + |
| 33 | +template <class N, class Head> constexpr N max_in_list(const N max, const Head &head) { |
| 34 | + return (max >= head) ? max : head; |
| 35 | +} |
| 36 | + |
| 37 | +template <class N, class Head, class... Tail> |
| 38 | +constexpr N max_in_list(const N max, const Head &head, const Tail &...tail) { |
| 39 | + return max_in_list((max >= head) ? max : head, tail...); |
| 40 | +} |
| 41 | + |
| 42 | +template <class Query, class Head> |
| 43 | +constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found, |
| 44 | + const Query &query, const Head &head) { |
| 45 | + return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? 1 : 0; |
| 46 | +} |
| 47 | + |
| 48 | +template <class Query, class Head, class... Tail> |
| 49 | +constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found, |
| 50 | + const Query &query, const Head &head, const Tail &...tail) { |
| 51 | + return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? |
| 52 | + 1 : |
| 53 | + is_first_appearance(idx + 1, at, (query == head ? idx : found), query, tail...); |
| 54 | +} |
| 55 | + |
| 56 | +#define ZARD_GET_DEVICE_VARGS(n, p, i, _) DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)) |
| 57 | +#define ZARD_FIRST_APPEARANCE(n, p, i) \ |
| 58 | + is_first_appearance(0, i, ((size_t)-1), DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)), \ |
| 59 | + DT_FOREACH_PROP_ELEM_SEP_VARGS(n, p, ZARD_GET_DEVICE_VARGS, (, ), 0)) |
| 60 | + |
| 61 | +#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0 |
| 62 | + |
| 63 | +constexpr int port_num = sum_of_list( |
| 64 | + 0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, |
| 65 | + ZARD_FIRST_APPEARANCE, (, ))); |
| 66 | + |
| 67 | +#define ZARD_GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios) |
| 68 | +constexpr int max_ngpios = max_in_list( |
| 69 | + 0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, ZARD_GPIO_NGPIOS, (, ))); |
| 70 | + |
| 71 | +#else |
| 72 | + |
| 73 | +constexpr int port_num = 1; |
| 74 | +constexpr int max_ngpios = 0; |
| 75 | + |
| 76 | +#endif |
| 77 | + |
| 78 | +} // namespace arduino |
| 79 | +} // namespace zephyr |
| 80 | + |
| 81 | +#endif // __cplusplus |
0 commit comments