Skip to content

Commit 2977339

Browse files
Jordan Yatesbbilas
authored andcommitted
spi: convert CS usage to gpio_dt_spec
Convert all CS control logic to be based on the `gpio_dt_spec` member instead of the standalone `port`, `pin` and `flags` members. 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 89d76e9 commit 2977339

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

drivers/spi/spi_context.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,29 +183,34 @@ gpio_dt_flags_t spi_context_cs_active_level(struct spi_context *ctx)
183183
return GPIO_ACTIVE_LOW;
184184
}
185185

186-
static inline void spi_context_cs_configure(struct spi_context *ctx)
186+
static inline int spi_context_cs_configure(struct spi_context *ctx)
187187
{
188-
if (ctx->config->cs && ctx->config->cs->gpio_dev) {
188+
int ret;
189+
190+
if (ctx->config->cs && ctx->config->cs->gpio.port) {
189191
/* Validate CS active levels are equivalent */
190192
__ASSERT(spi_context_cs_active_level(ctx) ==
191-
(ctx->config->cs->gpio_dt_flags & GPIO_ACTIVE_LOW),
193+
(ctx->config->cs->gpio.dt_flags & GPIO_ACTIVE_LOW),
192194
"Devicetree and spi_context CS levels are not equal");
193-
gpio_pin_configure(ctx->config->cs->gpio_dev,
194-
ctx->config->cs->gpio_pin,
195-
ctx->config->cs->gpio_dt_flags |
196-
GPIO_OUTPUT_INACTIVE);
195+
ret = gpio_pin_configure_dt(&ctx->config->cs->gpio,
196+
GPIO_OUTPUT_INACTIVE);
197+
if (ret < 0) {
198+
LOG_ERR("Failed to configure 'cs' gpio: %d", ret);
199+
return ret;
200+
}
197201
} else {
198202
LOG_INF("CS control inhibited (no GPIO device)");
199203
}
204+
205+
return 0;
200206
}
201207

202208
static inline void _spi_context_cs_control(struct spi_context *ctx,
203209
bool on, bool force_off)
204210
{
205-
if (ctx->config && ctx->config->cs && ctx->config->cs->gpio_dev) {
211+
if (ctx->config && ctx->config->cs && ctx->config->cs->gpio.port) {
206212
if (on) {
207-
gpio_pin_set(ctx->config->cs->gpio_dev,
208-
ctx->config->cs->gpio_pin, 1);
213+
gpio_pin_set_dt(&ctx->config->cs->gpio, 1);
209214
k_busy_wait(ctx->config->cs->delay);
210215
} else {
211216
if (!force_off &&
@@ -214,8 +219,7 @@ static inline void _spi_context_cs_control(struct spi_context *ctx,
214219
}
215220

216221
k_busy_wait(ctx->config->cs->delay);
217-
gpio_pin_set(ctx->config->cs->gpio_dev,
218-
ctx->config->cs->gpio_pin, 0);
222+
gpio_pin_set_dt(&ctx->config->cs->gpio, 0);
219223
}
220224
}
221225
}

include/drivers/spi.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,10 @@ struct spi_cs_control {
193193
* @param delay_ The @p delay field to set in the @p spi_cs_control
194194
* @return a pointer to the @p spi_cs_control structure
195195
*/
196-
#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
197-
(&(struct spi_cs_control) { \
198-
.gpio_dev = DEVICE_DT_GET( \
199-
DT_SPI_DEV_CS_GPIOS_CTLR(node_id)), \
200-
.delay = (delay_), \
201-
.gpio_pin = DT_SPI_DEV_CS_GPIOS_PIN(node_id), \
202-
.gpio_dt_flags = DT_SPI_DEV_CS_GPIOS_FLAGS(node_id), \
196+
#define SPI_CS_CONTROL_PTR_DT(node_id, delay_) \
197+
(&(struct spi_cs_control) { \
198+
.gpio = DT_SPI_DEV_CS_GPIOS_DT_SPEC_GET(node_id), \
199+
.delay = (delay_), \
203200
})
204201

205202
/**
@@ -439,7 +436,7 @@ static inline bool spi_is_ready(const struct spi_dt_spec *spec)
439436
}
440437
/* Validate CS gpio port is ready, if it is used */
441438
if (spec->config.cs &&
442-
!device_is_ready(spec->config.cs->gpio_dev)) {
439+
!device_is_ready(spec->config.cs->gpio.port)) {
443440
return false;
444441
}
445442
return true;

0 commit comments

Comments
 (0)