Skip to content

Commit b76b2bb

Browse files
morsiskokartben
authored andcommitted
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 <[email protected]>
1 parent f4267ec commit b76b2bb

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

drivers/spi/spi_bitbang.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ static int spi_bitbang_configure(const struct spi_bitbang_config *info,
3737
return -ENOTSUP;
3838
}
3939

40-
if (config->operation & (SPI_TRANSFER_LSB | SPI_LINES_DUAL
41-
| SPI_LINES_QUAD)) {
40+
if (config->operation & (SPI_LINES_DUAL | SPI_LINES_QUAD)) {
4241
LOG_ERR("Unsupported configuration");
4342
return -ENOTSUP;
4443
}
@@ -125,6 +124,7 @@ static int spi_bitbang_transceive(const struct device *dev,
125124
int clock_state = 0;
126125
int cpha = 0;
127126
bool loop = false;
127+
bool lsb = false;
128128

129129
if (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_CPOL) {
130130
clock_state = 1;
@@ -135,6 +135,9 @@ static int spi_bitbang_transceive(const struct device *dev,
135135
if (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_LOOP) {
136136
loop = true;
137137
}
138+
if (spi_cfg->operation & SPI_TRANSFER_LSB) {
139+
lsb = true;
140+
}
138141

139142
/* set the initial clock state before CS */
140143
gpio_pin_set_dt(&info->clk_gpio, clock_state);
@@ -157,16 +160,17 @@ static int spi_bitbang_transceive(const struct device *dev,
157160
}
158161
}
159162

160-
int shift = data->bits - 1;
161163
uint16_t r = 0;
164+
uint8_t i = 0;
162165
int b = 0;
163166
bool do_read = false;
164167

165168
if (miso && spi_context_rx_buf_on(ctx)) {
166169
do_read = true;
167170
}
168171

169-
while (shift >= 0) {
172+
while (i < data->bits) {
173+
const int shift = lsb ? i : (data->bits - 1 - i);
170174
const int d = (w >> shift) & 0x1;
171175

172176
b = 0;
@@ -198,9 +202,9 @@ static int spi_bitbang_transceive(const struct device *dev,
198202
b = d;
199203
}
200204

201-
r = (r << 1) | (b ? 0x1 : 0x0);
205+
r |= (b ? 0x1 : 0x0) << shift;
202206

203-
--shift;
207+
++i;
204208
}
205209

206210
if (spi_context_rx_buf_on(ctx)) {

0 commit comments

Comments
 (0)