diff options
author | Damien George <damien.p.george@gmail.com> | 2016-10-04 13:46:40 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-10-04 13:46:40 +1100 |
commit | b0eb0d6153d63cf823065792db35d2858fbb9406 (patch) | |
tree | e4ed50d85b4578fc14cf403c5186a75b7056ae63 /extmod/machine_spi.c | |
parent | b932b2dd1faaf3091b13af7d213041a737e40fac (diff) | |
download | micropython-b0eb0d6153d63cf823065792db35d2858fbb9406.tar.gz micropython-b0eb0d6153d63cf823065792db35d2858fbb9406.zip |
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.
Diffstat (limited to 'extmod/machine_spi.c')
-rw-r--r-- | extmod/machine_spi.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index b0bd76faf9..e3d72ab588 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -38,6 +38,28 @@ void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint // only MSB transfer is implemented + // If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured + // delay_half is equal to this value, then the software SPI implementation + // will run as fast as possible, limited only by CPU speed and GPIO time. + #ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY + if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) { + for (size_t i = 0; i < len; ++i) { + uint8_t data_out = src[i]; + uint8_t data_in = 0; + for (int j = 0; j < 8; ++j, data_out <<= 1) { + mp_hal_pin_write(self->mosi, (data_out >> 7) & 1); + mp_hal_pin_write(self->sck, 1 - self->polarity); + data_in = (data_in << 1) | mp_hal_pin_read(self->miso); + mp_hal_pin_write(self->sck, self->polarity); + } + if (dest != NULL) { + dest[i] = data_in; + } + } + return; + } + #endif + for (size_t i = 0; i < len; ++i) { uint8_t data_out = src[i]; uint8_t data_in = 0; |