|
8 | 8 | #include "zephyrInternal.h"
|
9 | 9 |
|
10 | 10 | namespace {
|
| 11 | + |
| 12 | +struct device gpio0; |
| 13 | +struct device gpio1; |
| 14 | + |
| 15 | +static constexpr struct device *gpios[] = {&gpio0, &gpio1}; |
| 16 | +static constexpr uint32_t pins[] = {16u, 32u}; // exclusive-upper |
| 17 | +static constexpr size_t NUM_PORTS = sizeof(gpios) / sizeof(gpios[0]); |
| 18 | + |
| 19 | + |
11 | 20 | constexpr pin_size_t local_gpio_pin(int) {
|
12 | 21 | return 0;
|
13 | 22 | }
|
14 | 23 |
|
15 |
| -constexpr struct device * local_gpio_port(int) { |
16 |
| - return nullptr; |
| 24 | +/* グローバル → gpio* を探す */ |
| 25 | +constexpr const struct device * |
| 26 | +local_gpio_port_recursive(size_t pin, const struct device *const *ctrl, |
| 27 | + const uint32_t *end, size_t n) { |
| 28 | + return (n == 0) ? nullptr |
| 29 | + : (pin < end[0]) ? ctrl[0] |
| 30 | + : local_gpio_port_recursive(pin, ctrl + 1, end + 1, n - 1); |
| 31 | +} |
| 32 | + |
| 33 | +constexpr const struct device *local_gpio_port(size_t pin) { |
| 34 | + return local_gpio_port_recursive(pin, gpios, pins, NUM_PORTS); |
| 35 | +} |
| 36 | + |
| 37 | +/* ポートインデックスを取得(見つからなければ size_t(-1)) */ |
| 38 | +constexpr size_t port_index_recursive(const struct device *target, |
| 39 | + const struct device *const *table, |
| 40 | + size_t idx, size_t n) { |
| 41 | + return (n == 0) ? size_t(-1) |
| 42 | + : (target == table[0]) |
| 43 | + ? idx |
| 44 | + : port_index_recursive(target, table + 1, idx + 1, n - 1); |
| 45 | +} |
| 46 | + |
| 47 | +pin_size_t global_gpio_pin(const struct device *port, size_t local_pin) noexcept { |
| 48 | + size_t pidx = port_index_recursive(port, gpios, 0, NUM_PORTS); |
| 49 | + size_t base = (pidx == 0) ? 0 : pins[pidx - 1]; |
| 50 | + size_t width = pins[pidx] - base; |
| 51 | + |
| 52 | + return ((local_pin < width) ? base + local_pin : size_t(-1)); |
17 | 53 | }
|
18 | 54 |
|
19 | 55 | constexpr uint32_t local_gpio_dt_flags(int) {
|
20 | 56 | return 0;
|
21 | 57 | }
|
22 | 58 |
|
23 |
| -constexpr pin_size_t global_gpio_pin(const struct device* port, int pin) { |
24 |
| - return pin; |
25 |
| -} |
| 59 | + |
26 | 60 |
|
27 | 61 | template <class N, class Head> constexpr const N max_in_list(const N max, const Head &head)
|
28 | 62 | {
|
|
0 commit comments