Skip to content

Commit 231cb1f

Browse files
committed
mimxrt10xx: Use the proper "betweenTransferDelay" (et al) values
Set the betweenTransferDelay to the SCK low-time, to avoid long pauses between bytes (transfers) while preventing the last SCK cycle in a byte from being a runt pulse. Compared to an earlier revision of this change, which just set the delays all to zero, this doesn't break using an AirLift, which was sensitive to the runt pulses (the simple loopback-wire test didn't detect the problem)
1 parent 1d48054 commit 231cb1f

File tree

1 file changed

+14
-7
lines changed
  • ports/mimxrt10xx/common-hal/busio

1 file changed

+14
-7
lines changed

ports/mimxrt10xx/common-hal/busio/SPI.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,10 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
238238
baudrate = 30000000; // "Absolute maximum frequency of operation (fop) is 30 MHz" -- IMXRT1010CEC.pdf
239239
}
240240

241-
LPSPI_Enable(self->spi, false);
242-
uint32_t tcrPrescaleValue;
243-
self->baudrate = LPSPI_MasterSetBaudRate(self->spi, baudrate, LPSPI_MASTER_CLK_FREQ, &tcrPrescaleValue);
244-
self->spi->TCR = (self->spi->TCR & ~LPSPI_TCR_PRESCALE_MASK) | LPSPI_TCR_PRESCALE(tcrPrescaleValue);
245-
LPSPI_Enable(self->spi, true);
246-
247241
if ((polarity == common_hal_busio_spi_get_polarity(self)) &&
248242
(phase == common_hal_busio_spi_get_phase(self)) &&
249-
(bits == ((self->spi->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT)) + 1) {
243+
(bits == ((self->spi->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT)) + 1 &&
244+
(baudrate == common_hal_busio_spi_get_frequency(self))) {
250245
return true;
251246
}
252247

@@ -257,10 +252,22 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
257252
config.cpol = polarity;
258253
config.cpha = phase;
259254
config.bitsPerFrame = bits;
255+
// The between-transfer-delay must be equal to the SCK low-time.
256+
// Setting it lower introduces runt pulses, while setting it higher
257+
// wastes time.
258+
config.betweenTransferDelayInNanoSec = 1000000000 / config.baudRate / 2;
260259

261260
LPSPI_Deinit(self->spi);
262261
LPSPI_MasterInit(self->spi, &config, LPSPI_MASTER_CLK_FREQ);
263262

263+
// Recompute the actual baudrate so that we can set the baudrate
264+
// (frequency) property. We don't need to set TCR because it was
265+
// established by LPSPI_MasterInit, above
266+
uint32_t tcrPrescaleValue;
267+
LPSPI_Enable(self->spi, false);
268+
self->baudrate = LPSPI_MasterSetBaudRate(self->spi, baudrate, LPSPI_MASTER_CLK_FREQ, &tcrPrescaleValue);
269+
LPSPI_Enable(self->spi, true);
270+
264271
return true;
265272
}
266273

0 commit comments

Comments
 (0)