Skip to content

Commit ae6de9a

Browse files
committed
localglobal
1 parent 0ac2dca commit ae6de9a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,70 @@ static const struct gpio_dt_spec arduino_pins[] = {DT_FOREACH_PROP_ELEM_SEP(
1212

1313
namespace {
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), gpios, 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), gpios, PROP_NGPIOS, (,))
23+
};
24+
25+
constexpr inline const struct device *local_gpio_port_recursive(pin_size_t pin,
26+
const struct device *const *ctrl,
27+
const uint32_t *end, size_t n)
28+
{
29+
return (n == 0) ? nullptr
30+
: (pin < end[0]) ? ctrl[0]
31+
: local_gpio_port_recursive(pin, ctrl + 1, end + 1, n - 1);
32+
}
33+
34+
constexpr inline size_t port_index_recursive(const struct device *target,
35+
const struct device *const *table, pin_size_t idx,
36+
size_t n)
37+
{
38+
return (n == 0) ? size_t(-1)
39+
: (target == table[0]) ? idx
40+
: port_index_recursive(target, table + 1, idx + 1, n - 1);
41+
}
42+
43+
constexpr inline const struct device *local_gpio_port(pin_size_t gpin)
44+
{
45+
return local_gpio_port_recursive(gpin, gpios, pins, ARRAY_SIZE(gpios));
46+
}
47+
48+
constexpr inline pin_size_t local_gpio_pin(pin_size_t gpin)
49+
{
50+
return gpin - pins[port_index_recursive(local_gpio_port(gpin), gpios, 0, ARRAY_SIZE(gpios))];
51+
}
52+
53+
constexpr inline pin_size_t global_gpio_pin_(size_t port_idx, pin_size_t lpin)
54+
{
55+
return port_idx == size_t(-1) ? size_t(-1) : pins[port_idx] + lpin;
56+
}
57+
58+
constexpr inline pin_size_t global_gpio_pin(const struct device *lport, pin_size_t lpin)
59+
{
60+
return global_gpio_pin_(port_index_recursive(lport, gpios, 0, ARRAY_SIZE(gpios)), lpin);
61+
}
62+
63+
constexpr inline uint32_t local_gpio_dt_flags(int)
64+
{
65+
return 0;
66+
}
67+
68+
inline int global_gpio_pin_get(pin_size_t pinNumber)
69+
{
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+
{
75+
return gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber), flags);
76+
}
77+
78+
1579
/*
1680
* Calculate GPIO ports/pins number statically from devicetree configuration
1781
*/

0 commit comments

Comments
 (0)