From 127cc6204a653080e88350f6b2528f6cb97f3379 Mon Sep 17 00:00:00 2001 From: Philip Jander Date: Fri, 22 Jan 2021 21:51:43 +0100 Subject: [PATCH 1/3] adds: idle loop to wait for SPI not busy (mimxrt10xx) --- ports/mimxrt10xx/common-hal/busio/SPI.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ports/mimxrt10xx/common-hal/busio/SPI.c b/ports/mimxrt10xx/common-hal/busio/SPI.c index ce7cbea7ec..518bf83de7 100644 --- a/ports/mimxrt10xx/common-hal/busio/SPI.c +++ b/ports/mimxrt10xx/common-hal/busio/SPI.c @@ -289,7 +289,11 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self, xfer.dataSize = len; xfer.configFlags = kLPSPI_MasterPcs0; - const status_t status = LPSPI_MasterTransferBlocking(self->spi, &xfer); + status_t status; + do { + status = LPSPI_MasterTransferBlocking(self->spi, &xfer); + } while (status == kStatus_LPSPI_Busy); + if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status); @@ -311,7 +315,11 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self, xfer.rxData = data; xfer.dataSize = len; - const status_t status = LPSPI_MasterTransferBlocking(self->spi, &xfer); + status_t status; + do { + status = LPSPI_MasterTransferBlocking(self->spi, &xfer); + } while (status == kStatus_LPSPI_Busy); + if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status); @@ -333,7 +341,11 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou xfer.rxData = data_in; xfer.dataSize = len; - const status_t status = LPSPI_MasterTransferBlocking(self->spi, &xfer); + status_t status; + do { + status = LPSPI_MasterTransferBlocking(self->spi, &xfer); + } while (status == kStatus_LPSPI_Busy); + if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status); From ae91b12aeaf18d1de409a35df3c498c9854e5cb3 Mon Sep 17 00:00:00 2001 From: Philip Jander Date: Fri, 22 Jan 2021 22:18:48 +0100 Subject: [PATCH 2/3] chore: whitespace fixed --- ports/mimxrt10xx/common-hal/busio/SPI.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/mimxrt10xx/common-hal/busio/SPI.c b/ports/mimxrt10xx/common-hal/busio/SPI.c index 518bf83de7..fdba5b2026 100644 --- a/ports/mimxrt10xx/common-hal/busio/SPI.c +++ b/ports/mimxrt10xx/common-hal/busio/SPI.c @@ -345,7 +345,7 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou do { status = LPSPI_MasterTransferBlocking(self->spi, &xfer); } while (status == kStatus_LPSPI_Busy); - + if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status); From 5bf08c503ba50fc24652c1d8720aee8c6e7de627 Mon Sep 17 00:00:00 2001 From: Philip Jander Date: Sun, 7 Feb 2021 17:06:46 +0100 Subject: [PATCH 3/3] adds: maximum retries on SPI busy --- ports/mimxrt10xx/common-hal/busio/SPI.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/mimxrt10xx/common-hal/busio/SPI.c b/ports/mimxrt10xx/common-hal/busio/SPI.c index fdba5b2026..00d8d9867f 100644 --- a/ports/mimxrt10xx/common-hal/busio/SPI.c +++ b/ports/mimxrt10xx/common-hal/busio/SPI.c @@ -38,6 +38,8 @@ #define LPSPI_MASTER_CLK_FREQ (CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk) / (CLOCK_GetDiv(kCLOCK_LpspiDiv) + 1)) +#define MAX_SPI_BUSY_RETRIES 100 + //arrays use 0 based numbering: SPI1 is stored at index 0 #define MAX_SPI 4 STATIC bool reserved_spi[MAX_SPI]; @@ -290,9 +292,10 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self, xfer.configFlags = kLPSPI_MasterPcs0; status_t status; + int retries = MAX_SPI_BUSY_RETRIES; do { status = LPSPI_MasterTransferBlocking(self->spi, &xfer); - } while (status == kStatus_LPSPI_Busy); + } while (status == kStatus_LPSPI_Busy && --retries > 0); if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status); @@ -316,9 +319,10 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self, xfer.dataSize = len; status_t status; + int retries = MAX_SPI_BUSY_RETRIES; do { status = LPSPI_MasterTransferBlocking(self->spi, &xfer); - } while (status == kStatus_LPSPI_Busy); + } while (status == kStatus_LPSPI_Busy && --retries > 0); if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status); @@ -342,9 +346,10 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou xfer.dataSize = len; status_t status; + int retries = MAX_SPI_BUSY_RETRIES; do { status = LPSPI_MasterTransferBlocking(self->spi, &xfer); - } while (status == kStatus_LPSPI_Busy); + } while (status == kStatus_LPSPI_Busy && --retries > 0); if (status != kStatus_Success) printf("%s: status %ld\r\n", __func__, status);