Skip to content

Commit 2497736

Browse files
committed
cores: arduino: Extract definitions used for compile-time calculations
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 713e9b2 commit 2497736

2 files changed

Lines changed: 82 additions & 62 deletions

File tree

cores/arduino/wiring_private.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
* Copyright (c) 2026 KurtE
4+
* Copyright (c) 2026 TOKITA Hiroshi
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#pragma once
10+
11+
#ifdef __cplusplus
12+
13+
namespace zephyr {
14+
namespace arduino {
15+
16+
constexpr struct gpio_dt_spec arduino_pins[] = {
17+
DT_FOREACH_PROP_ELEM_SEP(
18+
DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))};
19+
20+
/*
21+
* Calculate GPIO ports/pins number statically from devicetree configuration
22+
*/
23+
24+
template <class N, class Head> constexpr N sum_of_list(const N sum, const Head &head) {
25+
return sum + head;
26+
}
27+
28+
template <class N, class Head, class... Tail>
29+
constexpr N sum_of_list(const N sum, const Head &head, const Tail &...tail) {
30+
return sum_of_list(sum + head, tail...);
31+
}
32+
33+
template <class N, class Head> constexpr N max_in_list(const N max, const Head &head) {
34+
return (max >= head) ? max : head;
35+
}
36+
37+
template <class N, class Head, class... Tail>
38+
constexpr N max_in_list(const N max, const Head &head, const Tail &...tail) {
39+
return max_in_list((max >= head) ? max : head, tail...);
40+
}
41+
42+
template <class Query, class Head>
43+
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
44+
const Query &query, const Head &head) {
45+
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? 1 : 0;
46+
}
47+
48+
template <class Query, class Head, class... Tail>
49+
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
50+
const Query &query, const Head &head, const Tail &...tail) {
51+
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ?
52+
1 :
53+
is_first_appearance(idx + 1, at, (query == head ? idx : found), query, tail...);
54+
}
55+
56+
#define ZARD_GET_DEVICE_VARGS(n, p, i, _) DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i))
57+
#define ZARD_FIRST_APPEARANCE(n, p, i) \
58+
is_first_appearance(0, i, ((size_t)-1), DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)), \
59+
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, p, ZARD_GET_DEVICE_VARGS, (, ), 0))
60+
61+
#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0
62+
63+
constexpr int port_num = sum_of_list(
64+
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
65+
ZARD_FIRST_APPEARANCE, (, )));
66+
67+
#define ZARD_GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
68+
constexpr int max_ngpios = max_in_list(
69+
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, ZARD_GPIO_NGPIOS, (, )));
70+
71+
#else
72+
73+
constexpr int port_num = 1;
74+
constexpr int max_ngpios = 0;
75+
76+
#endif
77+
78+
} // namespace arduino
79+
} // namespace zephyr
80+
81+
#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)