Skip to content

Commit 0501c8d

Browse files
committed
Merge remote-tracking branch 'origin/main' into main
2 parents 8b448eb + 999f713 commit 0501c8d

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

ports/mimxrt10xx/boards/imxrt1010_evk/mpconfigboard.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ USB_MANUFACTURER = "NXP"
66
CHIP_VARIANT = MIMXRT1011DAE5A
77
CHIP_FAMILY = MIMXRT1011
88
FLASH = AT25SF128A
9+
10+
# Include these Python libraries in the firmware
11+
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_ESP32SPI
12+
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Requests

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,14 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
234234
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
235235
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
236236

237-
LPSPI_Enable(self->spi, false);
238-
uint32_t tcrPrescaleValue;
239-
self->baudrate = LPSPI_MasterSetBaudRate(self->spi, baudrate, LPSPI_MASTER_CLK_FREQ, &tcrPrescaleValue);
240-
self->spi->TCR = (self->spi->TCR & ~LPSPI_TCR_PRESCALE_MASK) | LPSPI_TCR_PRESCALE(tcrPrescaleValue);
241-
LPSPI_Enable(self->spi, true);
237+
if (baudrate > 30000000) {
238+
baudrate = 30000000; // "Absolute maximum frequency of operation (fop) is 30 MHz" -- IMXRT1010CEC.pdf
239+
}
242240

243241
if ((polarity == common_hal_busio_spi_get_polarity(self)) &&
244242
(phase == common_hal_busio_spi_get_phase(self)) &&
245-
(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))) {
246245
return true;
247246
}
248247

@@ -253,10 +252,22 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
253252
config.cpol = polarity;
254253
config.cpha = phase;
255254
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;
256259

257260
LPSPI_Deinit(self->spi);
258261
LPSPI_MasterInit(self->spi, &config, LPSPI_MASTER_CLK_FREQ);
259262

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+
260271
return true;
261272
}
262273

@@ -279,6 +290,21 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
279290
self->has_lock = false;
280291
}
281292

293+
static status_t transfer_common(busio_spi_obj_t *self, lpspi_transfer_t *xfer) {
294+
xfer->configFlags = kLPSPI_MasterPcsContinuous;
295+
296+
status_t status;
297+
int retries = MAX_SPI_BUSY_RETRIES;
298+
do {
299+
status = LPSPI_MasterTransferBlocking(self->spi, xfer);
300+
} while (status == kStatus_LPSPI_Busy && --retries > 0);
301+
302+
if (status != kStatus_Success) {
303+
printf("%s: status %ld\r\n", __func__, status);
304+
}
305+
return status;
306+
}
307+
282308
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
283309
const uint8_t *data, size_t len) {
284310
if (len == 0) {
@@ -291,17 +317,8 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self,
291317
lpspi_transfer_t xfer = { 0 };
292318
xfer.txData = (uint8_t *)data;
293319
xfer.dataSize = len;
294-
xfer.configFlags = kLPSPI_MasterPcs0;
295-
296-
status_t status;
297-
int retries = MAX_SPI_BUSY_RETRIES;
298-
do {
299-
status = LPSPI_MasterTransferBlocking(self->spi, &xfer);
300-
} while (status == kStatus_LPSPI_Busy && --retries > 0);
301320

302-
if (status != kStatus_Success) {
303-
printf("%s: status %ld\r\n", __func__, status);
304-
}
321+
status_t status = transfer_common(self, &xfer);
305322

306323
return status == kStatus_Success;
307324
}
@@ -321,15 +338,7 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self,
321338
xfer.rxData = data;
322339
xfer.dataSize = len;
323340

324-
status_t status;
325-
int retries = MAX_SPI_BUSY_RETRIES;
326-
do {
327-
status = LPSPI_MasterTransferBlocking(self->spi, &xfer);
328-
} while (status == kStatus_LPSPI_Busy && --retries > 0);
329-
330-
if (status != kStatus_Success) {
331-
printf("%s: status %ld\r\n", __func__, status);
332-
}
341+
status_t status = transfer_common(self, &xfer);
333342

334343
return status == kStatus_Success;
335344
}
@@ -349,15 +358,7 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou
349358
xfer.rxData = data_in;
350359
xfer.dataSize = len;
351360

352-
status_t status;
353-
int retries = MAX_SPI_BUSY_RETRIES;
354-
do {
355-
status = LPSPI_MasterTransferBlocking(self->spi, &xfer);
356-
} while (status == kStatus_LPSPI_Busy && --retries > 0);
357-
358-
if (status != kStatus_Success) {
359-
printf("%s: status %ld\r\n", __func__, status);
360-
}
361+
status_t status = transfer_common(self, &xfer);
361362

362363
return status == kStatus_Success;
363364
}

0 commit comments

Comments
 (0)