Skip to content

Commit 5c81ccc

Browse files
committed
x
1 parent 0bd954a commit 5c81ccc

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,55 @@
88
#include "zephyrInternal.h"
99

1010
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+
1120
constexpr pin_size_t local_gpio_pin(int) {
1221
return 0;
1322
}
1423

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));
1753
}
1854

1955
constexpr uint32_t local_gpio_dt_flags(int) {
2056
return 0;
2157
}
2258

23-
constexpr pin_size_t global_gpio_pin(const struct device* port, int pin) {
24-
return pin;
25-
}
59+
2660

2761
template <class N, class Head> constexpr const N max_in_list(const N max, const Head &head)
2862
{

0 commit comments

Comments
 (0)