Skip to content

Commit bc7cf72

Browse files
committed
libraries: SPI: hanlde multiple SPI instances
Create multiple SPI instance if spis array contains plural elements. Declare these as 'SPI1', 'SPI2', ..., from second element of the array. (First element is already declare as 'SPI'.) This works if `spis` property defined. The property parse as SPI array declaration, to define SPI instance with the name of SPI, SPI1, SPI2... If `spis` is not defined but the DTS already defines `arduino-spi` and, use it to define first SPI instance. The `arduion-spi` is usually define with arduino_header in boards definitions. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 4bc1867 commit bc7cf72

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

libraries/SPI/SPI.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,28 @@ void arduino::ZephyrSPI::begin() {}
121121

122122
void arduino::ZephyrSPI::end() {}
123123

124+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis)
125+
#if (DT_PROP_LEN(DT_PATH(zephyr_user), spis) > 1)
126+
#define ARDUINO_SPI_DEFINED_0 1
127+
#define DECL_SPI_0(n, p, i) arduino::ZephyrSPI SPI(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i)));
128+
#define DECL_SPI_N(n, p, i) arduino::ZephyrSPI SPI##i(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i)));
129+
#define DECLARE_SPI_N(n, p, i) \
130+
COND_CODE_1(ARDUINO_SPI_DEFINED_##i, (DECL_SPI_0(n, p, i)), (DECL_SPI_N(n, p, i)))
131+
132+
/* Declare SPI, SPI1, SPI2, ... */
133+
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), spis, DECLARE_SPI_N)
134+
135+
#undef DECLARE_SPI_N
136+
#undef DECL_SPI_N
137+
#undef DECL_SPI_0
138+
#undef ARDUINO_SPI_DEFINED_0
139+
#else // PROP_LEN(spis) > 1
140+
/* When PROP_LEN(spis) == 1, DT_FOREACH_PROP_ELEM work not correctly. */
124141
arduino::ZephyrSPI
125142
SPI(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), spis, 0)));
143+
#endif // HAS_PORP(spis)
144+
/* If spis node is not defined, tries to use arduino_spi */
145+
#elif DT_NODE_EXISTS(DT_NODELABEL(arduino_spi))
146+
arduino::ZephyrSPI
147+
SPI(DEVICE_DT_GET(DT_NODELABEL(arduino_spi)));
148+
#endif

libraries/SPI/SPI.h

+17
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,24 @@ class ZephyrSPI : public HardwareSPI {
5656

5757
} // namespace arduino
5858

59+
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis) && (DT_PROP_LEN(DT_PATH(zephyr_user), spis) > 1)
60+
#define ARDUINO_SPI_DEFINED_0 1
61+
#define DECL_EXTERN_SPI_0(i) extern arduino::ZephyrSPI SPI;
62+
#define DECL_EXTERN_SPI_N(i) extern arduino::ZephyrSPI SPI##i;
63+
#define DECLARE_EXTERN_SPI_N(n, p, i) \
64+
COND_CODE_1(ARDUINO_SPI_DEFINED_##i, (DECL_EXTERN_SPI_0(i)), (DECL_EXTERN_SPI_N(i)))
65+
66+
/* Declare SPI, SPI1, SPI2, ... */
67+
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), spis, DECLARE_EXTERN_SPI_N)
68+
69+
#undef DECLARE_EXTERN_SPI_N
70+
#undef DECL_EXTERN_SPI_N
71+
#undef DECL_EXTERN_SPI_0
72+
#undef ARDUINO_SPI_DEFINED_0
73+
#else
5974
extern arduino::ZephyrSPI SPI;
75+
#endif
76+
6077
/* Serial Peripheral Control Register */
6178
extern uint8_t SPCR;
6279

0 commit comments

Comments
 (0)