Skip to content

Commit 19bb8a8

Browse files
committed
cores: arduino: Extract definitions used for compile-time calculation
Move constexpr-specified constants and functions used for compile-time calculations from zephyrCommon.cpp to wiring_private.h Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 175f3f5 commit 19bb8a8

2 files changed

Lines changed: 76 additions & 63 deletions

File tree

cores/arduino/wiring_private.h

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,80 @@
11
/*
22
* Copyright (c) 2026 KurtE
3+
* Copyright (c) 2026 TOKITA Hiroshi
4+
*
35
* SPDX-License-Identifier: Apache-2.0
46
*/
57

6-
/* empty header file, for libraries which try to include like Adafruit_GFX_Library */
8+
#pragma once
9+
10+
#ifdef __cplusplus
11+
12+
namespace zephyr {
13+
namespace arduino {
14+
15+
constexpr struct gpio_dt_spec arduino_pins[] = {
16+
DT_FOREACH_PROP_ELEM_SEP(
17+
DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))};
18+
19+
/*
20+
* Calculate GPIO ports/pins number statically from devicetree configuration
21+
*/
22+
23+
template <class N, class Head> constexpr N sum_of_list(const N sum, const Head &head) {
24+
return sum + head;
25+
}
26+
27+
template <class N, class Head, class... Tail>
28+
constexpr N sum_of_list(const N sum, const Head &head, const Tail &...tail) {
29+
return sum_of_list(sum + head, tail...);
30+
}
31+
32+
template <class N, class Head> constexpr N max_in_list(const N max, const Head &head) {
33+
return (max >= head) ? max : head;
34+
}
35+
36+
template <class N, class Head, class... Tail>
37+
constexpr N max_in_list(const N max, const Head &head, const Tail &...tail) {
38+
return max_in_list((max >= head) ? max : head, tail...);
39+
}
40+
41+
template <class Query, class Head>
42+
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
43+
const Query &query, const Head &head) {
44+
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? 1 : 0;
45+
}
46+
47+
template <class Query, class Head, class... Tail>
48+
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
49+
const Query &query, const Head &head, const Tail &...tail) {
50+
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ?
51+
1 :
52+
is_first_appearance(idx + 1, at, (query == head ? idx : found), query, tail...);
53+
}
54+
55+
#define ZARD_GET_DEVICE_VARGS(n, p, i, _) DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i))
56+
#define ZARD_FIRST_APPEARANCE(n, p, i) \
57+
is_first_appearance(0, i, ((size_t)-1), DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)), \
58+
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, p, ZARD_GET_DEVICE_VARGS, (, ), 0))
59+
60+
#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0
61+
62+
constexpr int port_num = sum_of_list(
63+
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
64+
ZARD_FIRST_APPEARANCE, (, )));
65+
66+
#define ZARD_GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
67+
constexpr int max_ngpios = max_in_list(
68+
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, ZARD_GPIO_NGPIOS, (, )));
69+
70+
#else
71+
72+
constexpr int port_num = 1;
73+
constexpr int max_ngpios = 0;
74+
75+
#endif
76+
77+
} // namespace arduino
78+
} // namespace zephyr
79+
80+
#endif // __cplusplus

cores/arduino/zephyrCommon.cpp

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#include <Arduino.h>
8-
#include "zephyrInternal.h"
8+
#include "wiring_private.h"
99

1010
#include <zephyr/spinlock.h>
1111

@@ -22,10 +22,6 @@ void _reinit_peripheral_if_needed(pin_size_t pin, const struct device *dev) {
2222
}
2323
}
2424

25-
static const struct gpio_dt_spec arduino_pins[] = {
26-
DT_FOREACH_PROP_ELEM_SEP(
27-
DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))};
28-
2925
#define RETURN_ON_INVALID_PIN(pinNumber, ...) \
3026
do { \
3127
if ((pin_size_t)(pinNumber) >= ARRAY_SIZE(arduino_pins)) { \
@@ -35,63 +31,6 @@ static const struct gpio_dt_spec arduino_pins[] = {
3531

3632
namespace {
3733

38-
#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0
39-
40-
/*
41-
* Calculate GPIO ports/pins number statically from devicetree configuration
42-
*/
43-
44-
template <class N, class Head> constexpr N sum_of_list(const N sum, const Head &head) {
45-
return sum + head;
46-
}
47-
48-
template <class N, class Head, class... Tail>
49-
constexpr N sum_of_list(const N sum, const Head &head, const Tail &...tail) {
50-
return sum_of_list(sum + head, tail...);
51-
}
52-
53-
template <class N, class Head> constexpr N max_in_list(const N max, const Head &head) {
54-
return (max >= head) ? max : head;
55-
}
56-
57-
template <class N, class Head, class... Tail>
58-
constexpr N max_in_list(const N max, const Head &head, const Tail &...tail) {
59-
return max_in_list((max >= head) ? max : head, tail...);
60-
}
61-
62-
template <class Query, class Head>
63-
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
64-
const Query &query, const Head &head) {
65-
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? 1 : 0;
66-
}
67-
68-
template <class Query, class Head, class... Tail>
69-
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
70-
const Query &query, const Head &head, const Tail &...tail) {
71-
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ?
72-
1 :
73-
is_first_appearance(idx + 1, at, (query == head ? idx : found), query, tail...);
74-
}
75-
76-
#define GET_DEVICE_VARGS(n, p, i, _) DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i))
77-
#define FIRST_APPEARANCE(n, p, i) \
78-
is_first_appearance(0, i, ((size_t)-1), DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)), \
79-
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, p, GET_DEVICE_VARGS, (, ), 0))
80-
const int port_num = sum_of_list(
81-
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
82-
FIRST_APPEARANCE, (, )));
83-
84-
#define GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
85-
const int max_ngpios = max_in_list(
86-
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
87-
88-
#else
89-
90-
const int port_num = 1;
91-
const int max_ngpios = 0;
92-
93-
#endif
94-
9534
/*
9635
* GPIO callback implementation
9736
*/

0 commit comments

Comments
 (0)