From 8c0f598115d8c992d9a14ad842ad307fbbfd7868 Mon Sep 17 00:00:00 2001 From: Michal Morsisko Date: Sun, 1 Sep 2024 22:15:29 +0200 Subject: [PATCH 1/2] drivers: spi_bitbang: Add support for SPI_TRANSFER_LSB flag Add support for sending and receiving the least significant bit first for the spi_bitbang driver. This driver can now be used with SPI_TRANFER_LSB flag. Signed-off-by: Michal Morsisko --- drivers/spi/spi_bitbang.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c index bffe9377179430..f073881c9bd100 100644 --- a/drivers/spi/spi_bitbang.c +++ b/drivers/spi/spi_bitbang.c @@ -36,8 +36,7 @@ static int spi_bitbang_configure(const struct spi_bitbang_config *info, return -ENOTSUP; } - if (config->operation & (SPI_TRANSFER_LSB | SPI_LINES_DUAL - | SPI_LINES_QUAD)) { + if (config->operation & (SPI_LINES_DUAL | SPI_LINES_QUAD)) { LOG_ERR("Unsupported configuration"); return -ENOTSUP; } @@ -124,6 +123,7 @@ static int spi_bitbang_transceive(const struct device *dev, int clock_state = 0; int cpha = 0; bool loop = false; + bool lsb = false; if (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_CPOL) { clock_state = 1; @@ -134,6 +134,9 @@ static int spi_bitbang_transceive(const struct device *dev, if (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_LOOP) { loop = true; } + if (spi_cfg->operation & SPI_TRANSFER_LSB) { + lsb = true; + } /* set the initial clock state before CS */ gpio_pin_set_dt(&info->clk_gpio, clock_state); @@ -156,8 +159,8 @@ static int spi_bitbang_transceive(const struct device *dev, } } - int shift = data->bits - 1; uint16_t r = 0; + uint8_t i = 0; int b = 0; bool do_read = false; @@ -165,7 +168,8 @@ static int spi_bitbang_transceive(const struct device *dev, do_read = true; } - while (shift >= 0) { + while (i < data->bits) { + const int shift = lsb ? i : (data->bits - 1 - i); const int d = (w >> shift) & 0x1; b = 0; @@ -197,9 +201,9 @@ static int spi_bitbang_transceive(const struct device *dev, b = d; } - r = (r << 1) | (b ? 0x1 : 0x0); + r |= (b ? 0x1 : 0x0) << shift; - --shift; + ++i; } if (spi_context_rx_buf_on(ctx)) { From 98896760a808489b5791823d2dcbf84c6173d7ff Mon Sep 17 00:00:00 2001 From: Michal Morsisko Date: Sun, 1 Sep 2024 22:24:57 +0200 Subject: [PATCH 2/2] drivers: spi_bitbang: Make SPI_LINES_OCTAL explicitly unsupported flag Prevent the driver from perfroming transfer when SPI_LINES_OCTAL flag is specified, as this driver supports only SPI_LINES_DUAL for now. Signed-off-by: Michal Morsisko --- drivers/spi/spi_bitbang.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c index f073881c9bd100..04b1e0f09a6d0e 100644 --- a/drivers/spi/spi_bitbang.c +++ b/drivers/spi/spi_bitbang.c @@ -36,7 +36,7 @@ static int spi_bitbang_configure(const struct spi_bitbang_config *info, return -ENOTSUP; } - if (config->operation & (SPI_LINES_DUAL | SPI_LINES_QUAD)) { + if (config->operation & (SPI_LINES_DUAL | SPI_LINES_QUAD | SPI_LINES_OCTAL)) { LOG_ERR("Unsupported configuration"); return -ENOTSUP; }