|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #include <Wire.h>
|
| 8 | +#include <zephyr/sys/util_macro.h> |
| 9 | + |
8 | 10 | arduino::ZephyrI2C::ZephyrI2C(const struct device *i2c) : i2c_dev(i2c)
|
9 | 11 | {
|
10 | 12 | }
|
@@ -104,8 +106,25 @@ void arduino::ZephyrI2C::flush() {}
|
104 | 106 | void arduino::ZephyrI2C::onReceive(voidFuncPtrParamInt cb) {}
|
105 | 107 | void arduino::ZephyrI2C::onRequest(voidFuncPtr cb) {}
|
106 | 108 |
|
107 |
| -#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs) && (DT_PROP_LEN(DT_PATH(zephyr_user), i2cs) > 0) |
| 109 | +#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs) |
| 110 | +#if (DT_PROP_LEN(DT_PATH(zephyr_user), i2cs) > 1) |
| 111 | +#define ARDUINO_WIRE_DEFINED_0 1 |
| 112 | +#define DECL_WIRE_0(n, p, i) arduino::ZephyrI2C Wire(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i))); |
| 113 | +#define DECL_WIRE_N(n, p, i) arduino::ZephyrI2C Wire##i(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i))); |
| 114 | +#define DECLARE_WIRE_N(n, p, i) \ |
| 115 | + COND_CODE_1(ARDUINO_WIRE_DEFINED_##i, (DECL_WIRE_0(n, p, i)), (DECL_WIRE_N(n, p, i))) |
| 116 | + |
| 117 | +/* Declare Wire, Wire1, Wire2, ... */ |
| 118 | +DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), i2cs, DECLARE_WIRE_N) |
| 119 | + |
| 120 | +#undef DECLARE_WIRE_N |
| 121 | +#undef DECL_WIRE_N |
| 122 | +#undef DECL_WIRE_0 |
| 123 | +#undef ARDUINO_WIRE_DEFINED_0 |
| 124 | +#else // PROP_LEN(i2cs) > 1 |
| 125 | +/* When PROP_LEN(i2cs) == 1, DT_FOREACH_PROP_ELEM work not correctly. */ |
108 | 126 | arduino::ZephyrI2C Wire(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), i2cs, 0)));
|
| 127 | +#endif // HAS_PORP(i2cs) |
109 | 128 | /* If i2cs node is not defined, tries to use arduino_i2c */
|
110 | 129 | #elif DT_NODE_EXISTS(DT_NODELABEL(arduino_i2c))
|
111 | 130 | arduino::ZephyrI2C Wire(DEVICE_DT_GET(DT_NODELABEL(arduino_i2c)));
|
|
0 commit comments