Skip to content

Commit 4d1c90e

Browse files
Jordan Yatesbbilas
authored andcommitted
spi: helper macro for constructing CS gpio_dt_spec
Adds a helper macro, `DT_SPI_DEV_CS_GPIOS_DT_SPEC_GET`, that constructs a `struct gpio_dt_spec` corresponding with the CS gpio of an spi device. Signed-off-by: Jordan Yates <[email protected]> Co-authored-by: Jordan Yates <[email protected]> Signed-off-by: Bartosz Bilas <[email protected]> Co-authored-by: Bartosz Bilas <[email protected]>
1 parent e706967 commit 4d1c90e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

include/devicetree/spi.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,46 @@ extern "C" {
116116
*/
117117
#define DT_SPI_DEV_HAS_CS_GPIOS(spi_dev) DT_SPI_HAS_CS_GPIOS(DT_BUS(spi_dev))
118118

119+
/**
120+
* @brief Get a SPI device's chip select devicetree specification
121+
*
122+
* Example devicetree fragment:
123+
*
124+
* @code{.devicetree}
125+
* gpio1: gpio@... { ... };
126+
*
127+
* gpio2: gpio@... { ... };
128+
*
129+
* spi@... {
130+
* compatible = "vnd,spi";
131+
* cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
132+
* <&gpio2 20 GPIO_ACTIVE_LOW>;
133+
*
134+
* a: spi-dev-a@0 {
135+
* reg = <0>;
136+
* };
137+
*
138+
* b: spi-dev-b@1 {
139+
* reg = <1>;
140+
* };
141+
* };
142+
* @endcode
143+
*
144+
* Example usage:
145+
*
146+
* @code{.c}
147+
* DT_SPI_DEV_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(a)) \
148+
* // { DEVICE_DT_GET(DT_NODELABEL(gpio1)), 10, GPIO_ACTIVE_LOW }
149+
* DT_SPI_DEV_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(b)) \
150+
* // { DEVICE_DT_GET(DT_NODELABEL(gpio2)), 20, GPIO_ACTIVE_LOW }
151+
* @endcode
152+
*
153+
* @param spi_dev a SPI device node identifier
154+
* @return #gpio_dt_spec struct corresponding with spi_dev's chip select
155+
*/
156+
#define DT_SPI_DEV_CS_GPIOS_DT_SPEC_GET(spi_dev) \
157+
GPIO_DT_SPEC_GET_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR(spi_dev))
158+
119159
/**
120160
* @brief Get a SPI device's chip select GPIO controller's node identifier
121161
*

0 commit comments

Comments
 (0)