Skip to content

Commit 79d8e9d

Browse files
committed
libraries: Wire: Hanlde multiple Wire instances
Create multiple Wire instance if i2cs array contains plural elements. Declare these as 'Wire1', 'Wire2', ..., from second element of the array. (First element is already declare as 'Wire'.) Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@fujitsu.com>
1 parent 83c6c0a commit 79d8e9d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

libraries/Wire/Wire.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
#include <Wire.h>
8+
#include <zephyr/sys/util_macro.h>
9+
810
arduino::ZephyrI2C::ZephyrI2C(const struct device *i2c) : i2c_dev(i2c)
911
{
1012
}
@@ -104,8 +106,25 @@ void arduino::ZephyrI2C::flush() {}
104106
void arduino::ZephyrI2C::onReceive(voidFuncPtrParamInt cb) {}
105107
void arduino::ZephyrI2C::onRequest(voidFuncPtr cb) {}
106108

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. */
108126
arduino::ZephyrI2C Wire(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), i2cs, 0)));
127+
#endif // HAS_PORP(i2cs)
109128
/* If i2cs node is not defined, tries to use arduino_i2c */
110129
#elif DT_NODE_EXISTS(DT_NODELABEL(arduino_i2c))
111130
arduino::ZephyrI2C Wire(DEVICE_DT_GET(DT_NODELABEL(arduino_i2c)));

libraries/Wire/Wire.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,21 @@ class ZephyrI2C : public HardwareI2C {
5757

5858
} // namespace arduino
5959

60-
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs) && \
61-
(DT_PROP_LEN(DT_PATH(zephyr_user), i2cs) > 0) || \
62-
DT_NODE_EXISTS(DT_NODELABEL(arduino_i2c))
60+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), i2cs) && (DT_PROP_LEN(DT_PATH(zephyr_user), i2cs) > 1)
61+
#define ARDUINO_WIRE_DEFINED_0 1
62+
#define DECL_EXTERN_WIRE_0(i) extern arduino::ZephyrI2C Wire;
63+
#define DECL_EXTERN_WIRE_N(i) extern arduino::ZephyrI2C Wire##i;
64+
#define DECLARE_EXTERN_WIRE_N(n, p, i) \
65+
COND_CODE_1(ARDUINO_WIRE_DEFINED_##i, (DECL_EXTERN_WIRE_0(i)), (DECL_EXTERN_WIRE_N(i)))
66+
67+
/* Declare Wire, Wire1, Wire2, ... */
68+
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), i2cs, DECLARE_EXTERN_WIRE_N)
69+
70+
#undef DECLARE_EXTERN_WIRE_N
71+
#undef DECL_EXTERN_WIRE_N
72+
#undef DECL_EXTERN_WIRE_0
73+
#undef ARDUINO_WIRE_DEFINED_0
74+
#else
6375
extern arduino::ZephyrI2C Wire;
6476
#endif
6577

0 commit comments

Comments
 (0)