Skip to content

Commit 7e7bc2c

Browse files
committed
zephyrCommon: add gpio-ports property
To determine which gpio ports to use with the Arduino-API, introduce `gpio-ports` to the overlay. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent de6b176 commit 7e7bc2c

File tree

11 files changed

+92
-0
lines changed

11 files changed

+92
-0
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,69 @@ 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), gpio_ports, 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), 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_get(pin_size_t pinNumber) {
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+
return gpio_pin_configure(local_gpio_port(pinNumber), local_gpio_pin(pinNumber), flags);
75+
}
76+
77+
1578
/*
1679
* Calculate GPIO ports/pins number statically from devicetree configuration
1780
*/
@@ -67,6 +130,8 @@ const int port_num =
67130
#define GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
68131
const int max_ngpios = max_in_list(
69132
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
133+
const int sum_ngpios = sum_of_list(
134+
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
70135

71136
/*
72137
* GPIO callback implementation

documentation/variants.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ uses [the Arduino header definitions](https://github.com/zephyrproject-rtos/zeph
9090
};
9191
```
9292

93+
### Configure GPIO devices
94+
95+
The `gpios` node lists the GPIO ports to use.
96+
Pin numbers will be assigned sequentially starting from 0 to the GPIOs of the ports specified here.
97+
For example, if gpio0 and gpio1, each with 16 outputs, are specified, pin 1 of gpio1 will be assigned 17.
98+
99+
```
100+
/ {
101+
zephyr,user {
102+
gpios = <&gpio0>, <&gpio1>;
103+
};
104+
};
105+
```
106+
93107
### Configure Serial devices
94108

95109
The `serials` node defines the Serial devices to use.

variants/arduino_mkrzero/arduino_mkrzero.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<&adc 6>,
5555
<&adc 7>;
5656

57+
gpio-ports = <&porta>, <&portb>;
5758
serials = <&sercom5>;
5859
i2cs = <&sercom0>;
5960
};

variants/arduino_nano_33_ble/arduino_nano_33_ble.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<&adc 4>,
6060
<&adc 1>;
6161

62+
gpio-ports = <&gpio0>, <&gpio1>;
6263
serials = <&uart0>;
6364
i2cs = <&arduino_nano_i2c>;
6465
};

variants/arduino_nano_33_ble_sense/arduino_nano_33_ble_sense.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<&adc 4>,
6060
<&adc 1>;
6161

62+
gpio-ports = <&gpio0>, <&gpio1>;
6263
serials = <&uart0>;
6364
i2cs = <&arduino_nano_i2c>;
6465
};

variants/arduino_nano_33_iot/arduino_nano_33_iot.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<&adc 6>,
6161
<&adc 7>;
6262

63+
gpio-ports = <&porta>, <&portb>;
6364
serials = <&sercom5>;
6465
i2cs = <&arduino_nano_i2c>;
6566
};

variants/beagleconnect_freedom/beagleconnect_freedom.overlay

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
<&gpio0 28 GPIO_ACTIVE_HIGH>; /* D3 - MB1 CS - A5 */
5959

6060
builtin-led-gpios = <&gpio0 18 GPIO_ACTIVE_HIGH>; /* 2.4GHz TX/RX */
61+
62+
gpio-ports = <&gpio0>;
6163
serials = <&uart0 &uart1>;
6264
i2cs = <&i2c0>;
6365
spis = <&spi0>;

variants/cc3220sf_launchxl/cc3220sf_launchxl.overlay

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
<&boosterpack_header 31 0>, /* GPIO_24 */
3131
<&boosterpack_header 32 0>; /* GPIO_23 */
3232
builtin-led-gpios = <&boosterpack_header 10 0>; /* GREEN_LED */
33+
34+
gpio-ports = <&gpioa0>, <&gpioa1>, <&gpioa2>, <&gpioa3>;
3335
serials = <&boosterpack_serial>;
3436
i2cs = <&boosterpack_i2c>;
3537
};

variants/nrf52840dk_nrf52840/nrf52840dk_nrf52840.overlay

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
<&arduino_adc 3>,
5353
<&arduino_adc 4>,
5454
<&arduino_adc 5>;
55+
56+
gpio-ports = <&gpio0>, <&gpio1>;
5557
};
5658
};
5759

variants/nrf9160dk_nrf9160/nrf9160dk_nrf9160.overlay

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
<&arduino_adc 3>,
5858
<&arduino_adc 4>,
5959
<&arduino_adc 5>;
60+
61+
gpio-ports = <&gpio0>;
6062
};
6163
};
6264

0 commit comments

Comments
 (0)