Skip to content

Commit

Permalink
libraries: SPI: hanlde multiple SPI instances
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
soburi committed Aug 18, 2024
1 parent 4bc1867 commit bc7cf72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,28 @@ void arduino::ZephyrSPI::begin() {}

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

#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis)
#if (DT_PROP_LEN(DT_PATH(zephyr_user), spis) > 1)
#define ARDUINO_SPI_DEFINED_0 1
#define DECL_SPI_0(n, p, i) arduino::ZephyrSPI SPI(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i)));
#define DECL_SPI_N(n, p, i) arduino::ZephyrSPI SPI##i(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(n, p, i)));
#define DECLARE_SPI_N(n, p, i) \
COND_CODE_1(ARDUINO_SPI_DEFINED_##i, (DECL_SPI_0(n, p, i)), (DECL_SPI_N(n, p, i)))

/* Declare SPI, SPI1, SPI2, ... */
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), spis, DECLARE_SPI_N)

#undef DECLARE_SPI_N
#undef DECL_SPI_N
#undef DECL_SPI_0
#undef ARDUINO_SPI_DEFINED_0
#else // PROP_LEN(spis) > 1
/* When PROP_LEN(spis) == 1, DT_FOREACH_PROP_ELEM work not correctly. */
arduino::ZephyrSPI
SPI(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(DT_PATH(zephyr_user), spis, 0)));
#endif // HAS_PORP(spis)
/* If spis node is not defined, tries to use arduino_spi */
#elif DT_NODE_EXISTS(DT_NODELABEL(arduino_spi))
arduino::ZephyrSPI
SPI(DEVICE_DT_GET(DT_NODELABEL(arduino_spi)));
#endif
17 changes: 17 additions & 0 deletions libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,24 @@ class ZephyrSPI : public HardwareSPI {

} // namespace arduino

#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis) && (DT_PROP_LEN(DT_PATH(zephyr_user), spis) > 1)
#define ARDUINO_SPI_DEFINED_0 1
#define DECL_EXTERN_SPI_0(i) extern arduino::ZephyrSPI SPI;

Check failure on line 61 in libraries/SPI/SPI.h

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: Macros with multiple statements should be enclosed in a do - while loop

Check failure on line 61 in libraries/SPI/SPI.h

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: macros should not use a trailing semicolon
#define DECL_EXTERN_SPI_N(i) extern arduino::ZephyrSPI SPI##i;

Check failure on line 62 in libraries/SPI/SPI.h

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: Macros with multiple statements should be enclosed in a do - while loop

Check failure on line 62 in libraries/SPI/SPI.h

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: macros should not use a trailing semicolon
#define DECLARE_EXTERN_SPI_N(n, p, i) \
COND_CODE_1(ARDUINO_SPI_DEFINED_##i, (DECL_EXTERN_SPI_0(i)), (DECL_EXTERN_SPI_N(i)))

Check failure on line 64 in libraries/SPI/SPI.h

View workflow job for this annotation

GitHub Actions / checkpatch review

ERROR: code indent should use tabs where possible

Check failure on line 64 in libraries/SPI/SPI.h

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: please, no spaces at the start of a line

/* Declare SPI, SPI1, SPI2, ... */
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), spis, DECLARE_EXTERN_SPI_N)

#undef DECLARE_EXTERN_SPI_N
#undef DECL_EXTERN_SPI_N
#undef DECL_EXTERN_SPI_0
#undef ARDUINO_SPI_DEFINED_0
#else
extern arduino::ZephyrSPI SPI;
#endif

/* Serial Peripheral Control Register */
extern uint8_t SPCR;

Expand Down

0 comments on commit bc7cf72

Please sign in to comment.