Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: spi_bitbang: Add support for SPI_TRANSFER_LSB flag #77858

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 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 | SPI_LINES_OCTAL)) {
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);
r |= (b ? 0x1 : 0x0) << shift;

--shift;
++i;
}

if (spi_context_rx_buf_on(ctx)) {
Expand Down
Loading