Skip to content

Commit e87e054

Browse files
morsiskonashif
authored andcommitted
drivers: spi_bitbang: Increase supported word size to 32 bits
This change introduces support for words up to 32 bits size to the spi_bitbang driver Signed-off-by: Michal Morsisko <morsisko@gmail.com>
1 parent c8c0c29 commit e87e054

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

drivers/spi/spi_bitbang.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ static int spi_bitbang_configure(const struct spi_bitbang_config *info,
4444

4545
const int bits = SPI_WORD_SIZE_GET(config->operation);
4646

47-
if (bits > 16) {
48-
LOG_ERR("Word sizes > 16 bits not supported");
47+
if (bits > 32) {
48+
LOG_ERR("Word sizes > 32 bits not supported");
4949
return -ENOTSUP;
5050
}
5151

5252
data->bits = bits;
5353
data->dfs = ((data->bits - 1) / 8) + 1;
54+
55+
/* As there is no uint24_t, it is assumed uint32_t will be used as the buffer base type. */
56+
if (data->dfs == 3) {
57+
data->dfs = 4;
58+
}
59+
5460
if (config->frequency > 0) {
5561
/* convert freq to period, the extra /2 is due to waiting
5662
* twice in each clock cycle. The '2000' is an upscale factor.
@@ -147,10 +153,14 @@ static int spi_bitbang_transceive(const struct device *dev,
147153
const uint32_t wait_us = data->wait_us;
148154

149155
while (spi_context_tx_buf_on(ctx) || spi_context_rx_buf_on(ctx)) {
150-
uint16_t w = 0;
156+
uint32_t w = 0;
151157

152158
if (ctx->tx_len) {
153159
switch (data->dfs) {
160+
case 4:
161+
case 3:
162+
w = *(uint32_t *)(ctx->tx_buf);
163+
break;
154164
case 2:
155165
w = *(uint16_t *)(ctx->tx_buf);
156166
break;
@@ -160,7 +170,7 @@ static int spi_bitbang_transceive(const struct device *dev,
160170
}
161171
}
162172

163-
uint16_t r = 0;
173+
uint32_t r = 0;
164174
uint8_t i = 0;
165175
int b = 0;
166176
bool do_read = false;
@@ -209,6 +219,10 @@ static int spi_bitbang_transceive(const struct device *dev,
209219

210220
if (spi_context_rx_buf_on(ctx)) {
211221
switch (data->dfs) {
222+
case 4:
223+
case 3:
224+
*(uint32_t *)(ctx->rx_buf) = r;
225+
break;
212226
case 2:
213227
*(uint16_t *)(ctx->rx_buf) = r;
214228
break;

0 commit comments

Comments
 (0)