Skip to content

Commit 0d39853

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 e9b9a3b commit 0d39853

2 files changed

Lines changed: 82 additions & 61 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) 2026 TOKITA Hiroshi
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <Arduino.h>
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 & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,9 @@
77
#include <Arduino.h>
88
#include "zephyrInternal.h"
99

10-
static const struct gpio_dt_spec arduino_pins[] = {
11-
DT_FOREACH_PROP_ELEM_SEP(
12-
DT_PATH(zephyr_user), digital_pin_gpios, GPIO_DT_SPEC_GET_BY_IDX, (, ))};
10+
using namespace zephyr::arduino;
1311

1412
namespace {
15-
16-
#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0
17-
18-
/*
19-
* Calculate GPIO ports/pins number statically from devicetree configuration
20-
*/
21-
22-
template <class N, class Head> constexpr N sum_of_list(const N sum, const Head &head) {
23-
return sum + head;
24-
}
25-
26-
template <class N, class Head, class... Tail>
27-
constexpr N sum_of_list(const N sum, const Head &head, const Tail &...tail) {
28-
return sum_of_list(sum + head, tail...);
29-
}
30-
31-
template <class N, class Head> constexpr N max_in_list(const N max, const Head &head) {
32-
return (max >= head) ? max : head;
33-
}
34-
35-
template <class N, class Head, class... Tail>
36-
constexpr N max_in_list(const N max, const Head &head, const Tail &...tail) {
37-
return max_in_list((max >= head) ? max : head, tail...);
38-
}
39-
40-
template <class Query, class Head>
41-
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
42-
const Query &query, const Head &head) {
43-
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? 1 : 0;
44-
}
45-
46-
template <class Query, class Head, class... Tail>
47-
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
48-
const Query &query, const Head &head, const Tail &...tail) {
49-
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ?
50-
1 :
51-
is_first_appearance(idx + 1, at, (query == head ? idx : found), query, tail...);
52-
}
53-
54-
#define GET_DEVICE_VARGS(n, p, i, _) DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i))
55-
#define FIRST_APPEARANCE(n, p, i) \
56-
is_first_appearance(0, i, ((size_t)-1), DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(n, p, i)), \
57-
DT_FOREACH_PROP_ELEM_SEP_VARGS(n, p, GET_DEVICE_VARGS, (, ), 0))
58-
const int port_num = sum_of_list(
59-
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios,
60-
FIRST_APPEARANCE, (, )));
61-
62-
#define GPIO_NGPIOS(n, p, i) DT_PROP(DT_GPIO_CTLR_BY_IDX(n, p, i), ngpios)
63-
const int max_ngpios = max_in_list(
64-
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));
65-
66-
#else
67-
68-
const int port_num = 1;
69-
const int max_ngpios = 0;
70-
71-
#endif
72-
7313
/*
7414
* GPIO callback implementation
7515
*/

0 commit comments

Comments
 (0)