Skip to content

Commit b0eb0d6

Browse files
committed
extmod/machine_spi: Add optional support for fast software SPI.
If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY then it can use a faster software SPI loop that does not make calls to the delay_us function.
1 parent b932b2d commit b0eb0d6

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

extmod/machine_spi.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint
3838

3939
// only MSB transfer is implemented
4040

41+
// If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
42+
// delay_half is equal to this value, then the software SPI implementation
43+
// will run as fast as possible, limited only by CPU speed and GPIO time.
44+
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
45+
if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
46+
for (size_t i = 0; i < len; ++i) {
47+
uint8_t data_out = src[i];
48+
uint8_t data_in = 0;
49+
for (int j = 0; j < 8; ++j, data_out <<= 1) {
50+
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
51+
mp_hal_pin_write(self->sck, 1 - self->polarity);
52+
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
53+
mp_hal_pin_write(self->sck, self->polarity);
54+
}
55+
if (dest != NULL) {
56+
dest[i] = data_in;
57+
}
58+
}
59+
return;
60+
}
61+
#endif
62+
4163
for (size_t i = 0; i < len; ++i) {
4264
uint8_t data_out = src[i];
4365
uint8_t data_in = 0;

0 commit comments

Comments
 (0)