Skip to content

Commit

Permalink
drivers: spi_bitbang: Add support for SPI_TRANSFER_LSB flag
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
morsisko committed Sep 1, 2024
1 parent 7cf124b commit 6f62099
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions drivers/spi/spi_bitbang.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -156,16 +159,17 @@ 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;

if (miso && spi_context_rx_buf_on(ctx)) {
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;
Expand Down Expand Up @@ -197,9 +201,9 @@ static int spi_bitbang_transceive(const struct device *dev,
b = d;
}

r = (r << 1) | (b ? 0x1 : 0x0);

--shift;
r |= (b ? 0x1 : 0x0) << shift;
++i;
}

if (spi_context_rx_buf_on(ctx)) {
Expand Down

0 comments on commit 6f62099

Please sign in to comment.