Compare commits
2 commits
f9e3b65d3a
...
072498d59d
Author | SHA1 | Date | |
---|---|---|---|
072498d59d | |||
37efde62fc |
1 changed files with 35 additions and 25 deletions
|
@ -14,17 +14,25 @@ LOG_MODULE_REGISTER(spi_bitbang);
|
||||||
#include <zephyr/drivers/spi.h>
|
#include <zephyr/drivers/spi.h>
|
||||||
#include "spi_context.h"
|
#include "spi_context.h"
|
||||||
|
|
||||||
|
/* Expands to 1 if the node has no MISO GPIOs */
|
||||||
|
#define _MISO_PRESENT(n) (DT_INST_PROP_LEN_OR(n, miso_gpios, 0) != 0) |
|
||||||
|
#define SPI_BITBANG_MISO_REQUIRED DT_INST_FOREACH_STATUS_OKAY(_MISO_PRESENT) 0
|
||||||
|
|
||||||
struct spi_bitbang_data {
|
struct spi_bitbang_data {
|
||||||
struct spi_context ctx;
|
struct spi_context ctx;
|
||||||
int bits;
|
int16_t wait_us;
|
||||||
int wait_us;
|
/* Number of bits per word. Maximum is 16. */
|
||||||
int dfs;
|
int8_t bits;
|
||||||
|
/* Number of bytes per word. Maximum is 2. */
|
||||||
|
int8_t dfs;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct spi_bitbang_config {
|
struct spi_bitbang_config {
|
||||||
struct gpio_dt_spec clk_gpio;
|
struct gpio_dt_spec clk_gpio;
|
||||||
struct gpio_dt_spec mosi_gpio;
|
struct gpio_dt_spec mosi_gpio;
|
||||||
|
#if SPI_BITBANG_MISO_REQUIRED
|
||||||
struct gpio_dt_spec miso_gpio;
|
struct gpio_dt_spec miso_gpio;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
static int spi_bitbang_configure(const struct spi_bitbang_config *info,
|
static int spi_bitbang_configure(const struct spi_bitbang_config *info,
|
||||||
|
@ -105,10 +113,11 @@ static int spi_bitbang_transceive(const struct device *dev,
|
||||||
if (info->mosi_gpio.port) {
|
if (info->mosi_gpio.port) {
|
||||||
mosi = &info->mosi_gpio;
|
mosi = &info->mosi_gpio;
|
||||||
}
|
}
|
||||||
|
#if SPI_BITBANG_MISO_REQUIRED
|
||||||
if (info->miso_gpio.port) {
|
if (info->miso_gpio.port) {
|
||||||
miso = &info->miso_gpio;
|
miso = &info->miso_gpio;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info->mosi_gpio.port) {
|
if (info->mosi_gpio.port) {
|
||||||
|
@ -284,6 +293,7 @@ int spi_bitbang_init(const struct device *dev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SPI_BITBANG_MISO_REQUIRED
|
||||||
if (config->miso_gpio.port != NULL) {
|
if (config->miso_gpio.port != NULL) {
|
||||||
if (!gpio_is_ready_dt(&config->miso_gpio)) {
|
if (!gpio_is_ready_dt(&config->miso_gpio)) {
|
||||||
LOG_ERR("GPIO port for miso pin is not ready");
|
LOG_ERR("GPIO port for miso pin is not ready");
|
||||||
|
@ -297,6 +307,7 @@ int spi_bitbang_init(const struct device *dev)
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
rc = spi_context_cs_configure_all(&data->ctx);
|
rc = spi_context_cs_configure_all(&data->ctx);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
|
@ -307,26 +318,25 @@ int spi_bitbang_init(const struct device *dev)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SPI_BITBANG_INIT(inst) \
|
#if SPI_BITBANG_MISO_REQUIRED
|
||||||
static struct spi_bitbang_config spi_bitbang_config_##inst = { \
|
#define SPI_BITBANG_INIT_MISO(inst) .miso_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, miso_gpios, {0}),
|
||||||
.clk_gpio = GPIO_DT_SPEC_INST_GET(inst, clk_gpios), \
|
#else
|
||||||
.mosi_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mosi_gpios, {0}), \
|
#define SPI_BITBANG_INIT_MISO(inst)
|
||||||
.miso_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, miso_gpios, {0}), \
|
#endif
|
||||||
}; \
|
|
||||||
\
|
#define SPI_BITBANG_INIT(inst) \
|
||||||
static struct spi_bitbang_data spi_bitbang_data_##inst = { \
|
static const struct spi_bitbang_config spi_bitbang_config_##inst = { \
|
||||||
SPI_CONTEXT_INIT_LOCK(spi_bitbang_data_##inst, ctx), \
|
.clk_gpio = GPIO_DT_SPEC_INST_GET(inst, clk_gpios), \
|
||||||
SPI_CONTEXT_INIT_SYNC(spi_bitbang_data_##inst, ctx), \
|
.mosi_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mosi_gpios, {0}), \
|
||||||
SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(inst), ctx) \
|
SPI_BITBANG_INIT_MISO(inst)}; \
|
||||||
}; \
|
\
|
||||||
\
|
static struct spi_bitbang_data spi_bitbang_data_##inst = { \
|
||||||
DEVICE_DT_INST_DEFINE(inst, \
|
SPI_CONTEXT_INIT_LOCK(spi_bitbang_data_##inst, ctx), \
|
||||||
spi_bitbang_init, \
|
SPI_CONTEXT_INIT_SYNC(spi_bitbang_data_##inst, ctx), \
|
||||||
NULL, \
|
SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(inst), ctx)}; \
|
||||||
&spi_bitbang_data_##inst, \
|
\
|
||||||
&spi_bitbang_config_##inst, \
|
DEVICE_DT_INST_DEFINE(inst, spi_bitbang_init, NULL, &spi_bitbang_data_##inst, \
|
||||||
POST_KERNEL, \
|
&spi_bitbang_config_##inst, POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
|
||||||
CONFIG_SPI_INIT_PRIORITY, \
|
&spi_bitbang_api);
|
||||||
&spi_bitbang_api);
|
|
||||||
|
|
||||||
DT_INST_FOREACH_STATUS_OKAY(SPI_BITBANG_INIT)
|
DT_INST_FOREACH_STATUS_OKAY(SPI_BITBANG_INIT)
|
||||||
|
|
Loading…
Reference in a new issue