Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/i2c.h>

#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0
/* Note: DT_REG_ADDR needs an expanded argument or it will not work properly */
#define DIGITAL_PIN_MATCHES(dev_pha, pin, dev, num) \
(((dev == DT_REG_ADDR(dev_pha)) && (num == pin)) ? 1 : 0)
#define DIGITAL_PIN_EXISTS(n, p, i, dev, num) \
(((dev == DT_REG_ADDR(DT_PHANDLE_BY_IDX(n, p, i))) && (num == DT_PHA_BY_IDX(n, p, i, pin))) ? \
1 : \
0)
DIGITAL_PIN_MATCHES(DT_PHANDLE_BY_IDX(n, p, i), DT_PHA_BY_IDX(n, p, i, pin), dev, num)
Comment thread
soburi marked this conversation as resolved.

/* Check all pins are defined only once */
#define DIGITAL_PIN_CHECK_UNIQUE(i, _) \
Expand All @@ -31,6 +33,12 @@
#endif

#undef DIGITAL_PIN_CHECK_UNIQUE
#endif

// Helper macro to get Arduino pin number from device tree alias
#define DIGITAL_PIN_GPIOS_FIND_NODE(node) \
DIGITAL_PIN_GPIOS_FIND_PIN(DT_REG_ADDR(DT_PHANDLE_BY_IDX(node, gpios, 0)), \
DT_PHA_BY_IDX(node, gpios, 0, pin))

/* Return the index of it if matched, oterwise return 0 */
Comment thread
soburi marked this conversation as resolved.
#define LED_BUILTIN_INDEX_BY_REG_AND_PINNUM(n, p, i, dev, num) \
Expand Down Expand Up @@ -79,7 +87,9 @@
* enum digitalPins { D0, D1, ... LED... NUM_OF_DIGITAL_PINS };
*/
enum digitalPins {
#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0
DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, DN_ENUMS, (, )),
#endif
NUM_OF_DIGITAL_PINS
};

Expand Down
27 changes: 18 additions & 9 deletions cores/arduino/zephyrCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@ static const struct gpio_dt_spec arduino_pins[] = {

namespace {

#if DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios) > 0

/*
* Calculate GPIO ports/pins number statically from devicetree configuration
Comment thread
soburi marked this conversation as resolved.
*/

template <class N, class Head> constexpr const N sum_of_list(const N sum, const Head &head) {
template <class N, class Head> constexpr N sum_of_list(const N sum, const Head &head) {
return sum + head;
}

template <class N, class Head, class... Tail>
constexpr const N sum_of_list(const N sum, const Head &head, const Tail &...tail) {
constexpr N sum_of_list(const N sum, const Head &head, const Tail &...tail) {
return sum_of_list(sum + head, tail...);
}

template <class N, class Head> constexpr const N max_in_list(const N max, const Head &head) {
template <class N, class Head> constexpr N max_in_list(const N max, const Head &head) {
return (max >= head) ? max : head;
}

template <class N, class Head, class... Tail>
constexpr const N max_in_list(const N max, const Head &head, const Tail &...tail) {
constexpr N max_in_list(const N max, const Head &head, const Tail &...tail) {
return max_in_list((max >= head) ? max : head, tail...);
}

template <class Query, class Head>
constexpr const size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
const Query &query, const Head &head) {
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
const Query &query, const Head &head) {
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ? 1 : 0;
}

template <class Query, class Head, class... Tail>
constexpr const size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
const Query &query, const Head &head,
const Tail &...tail) {
constexpr size_t is_first_appearance(const size_t &idx, const size_t &at, const size_t &found,
const Query &query, const Head &head, const Tail &...tail) {
return ((found == ((size_t)-1)) && (query == head) && (idx == at)) ?
1 :
is_first_appearance(idx + 1, at, (query == head ? idx : found), query, tail...);
Expand All @@ -64,6 +65,13 @@ const int port_num = sum_of_list(
const int max_ngpios = max_in_list(
0, DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), digital_pin_gpios, GPIO_NGPIOS, (, )));

#else

const int port_num = 1;
const int max_ngpios = 0;
Comment thread
soburi marked this conversation as resolved.

#endif

/*
* GPIO callback implementation
*/
Expand Down Expand Up @@ -103,6 +111,7 @@ void setInterruptHandler(pin_size_t pinNumber, voidFuncPtr func) {
}

void handleGpioCallback(const struct device *port, struct gpio_callback *cb, uint32_t pins) {
(void)port; // unused
struct gpio_port_callback *pcb = (struct gpio_port_callback *)cb;

for (uint32_t i = 0; i < max_ngpios; i++) {
Expand Down
Loading