Skip to content

Commit d6dd3e3

Browse files
committed
zephyrCommon: Changing pin numbering rules
Zephyr's ba48d83bec18e9f2caf61979aadf9f8537086cee changes make it easy to configure pin numbering at compile, so we'll change the pin numbers to be based on the GPIO pin numbers. This is same as the traditional Arduino, using the GPIO numbers as pin numbers. Pin names written on the board, such as D1 and D2, are aliases for these pin numbers. When using multiple GPIO ports, pin numbers are numbered consecutively from the beginning. In other words, if you have two 16-port GPIOs, 17 refers to the second pin (idx=1) of the second port. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 7ca4da5 commit d6dd3e3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,62 @@ namespace {
1414
constexpr const struct device *gpios[] = {
1515
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), gpio_ports, DEVICE_GPIO, (,))
1616
};
17+
<<<<<<< HEAD
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_configure(pin_size_t pinNumber, int flags) {
70+
return gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber), flags);
71+
}
72+
>>>>>>> 484a5ea (zephyrCommon: Changing pin numbering rules)
1773

1874
#define PROP_NGPIOS(n, p, i) DT_PROP(DT_PROP_BY_IDX(n, p, i), ngpios)
1975
constexpr uint32_t pins[] = {

0 commit comments

Comments
 (0)