Skip to content

Commit 1ec914c

Browse files
Tim-Wang38Arto Kinnunen
authored andcommitted
targets:lpspi: Update the lpspi driver and api
Change the lpspi default transfer delays to fix the data corruption issue. Add the loop and judgement to retry transfer when spi bus is busy. Add the judgement statement to fix the hang issue. Signed-off-by: TimWang <[email protected]>
1 parent 436f5ca commit 1ec914c

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/spi_api.c

100644100755
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,21 @@ int spi_master_write(spi_t *obj, int value)
124124
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
125125
char *rx_buffer, int rx_length, char write_fill) {
126126
int total = (tx_length > rx_length) ? tx_length : rx_length;
127+
int ret;
127128

128129
// Default write is done in each and every call, in future can create HAL API instead
129130
LPSPI_SetDummyData(spi_address[obj->instance], write_fill);
130131

131-
LPSPI_MasterTransferBlocking(spi_address[obj->instance], &(lpspi_transfer_t){
132-
.txData = (uint8_t *)tx_buffer,
133-
.rxData = (uint8_t *)rx_buffer,
134-
.dataSize = total,
135-
.configFlags = kLPSPI_MasterPcs0 | kLPSPI_MasterPcsContinuous | kLPSPI_SlaveByteSwap,
136-
});
132+
do
133+
{
134+
ret = LPSPI_MasterTransferBlocking(spi_address[obj->instance], &(lpspi_transfer_t){
135+
.txData = (uint8_t *)tx_buffer,
136+
.rxData = (uint8_t *)rx_buffer,
137+
.dataSize = total,
138+
.configFlags = kLPSPI_MasterPcs0 | kLPSPI_MasterPcsContinuous | kLPSPI_SlaveByteSwap,
139+
});
140+
141+
} while((ret == kStatus_LPSPI_Busy));
137142

138143
return total;
139144
}

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_lpspi.c

100644100755
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ void LPSPI_MasterGetDefaultConfig(lpspi_master_config_t *masterConfig)
258258
masterConfig->cpha = kLPSPI_ClockPhaseFirstEdge;
259259
masterConfig->direction = kLPSPI_MsbFirst;
260260

261-
masterConfig->pcsToSckDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
262-
masterConfig->lastSckToPcsDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
263-
masterConfig->betweenTransferDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
261+
masterConfig->pcsToSckDelayInNanoSec = 80;
262+
masterConfig->lastSckToPcsDelayInNanoSec = 60;
263+
masterConfig->betweenTransferDelayInNanoSec = 160;
264264

265265
masterConfig->whichPcs = kLPSPI_Pcs0;
266266
masterConfig->pcsActiveHighOrLow = kLPSPI_PcsActiveLow;
@@ -871,14 +871,18 @@ status_t LPSPI_MasterTransferBlocking(LPSPI_Type *base, lpspi_transfer_t *transf
871871
{
872872
}
873873

874-
if (txData)
874+
/* To prevent rxfifo overflow, ensure transmitting and receiving are executed in parallel */
875+
if(((NULL == rxData) || (rxRemainingByteCount - txRemainingByteCount)/bytesEachRead < fifoSize))
875876
{
876-
wordToSend = LPSPI_CombineWriteData(txData, bytesEachWrite, isByteSwap);
877-
txData += bytesEachWrite;
878-
}
877+
if (txData)
878+
{
879+
wordToSend = LPSPI_CombineWriteData(txData, bytesEachWrite, isByteSwap);
880+
txData += bytesEachWrite;
881+
}
879882

880-
LPSPI_WriteData(base, wordToSend);
881-
txRemainingByteCount -= bytesEachWrite;
883+
LPSPI_WriteData(base, wordToSend);
884+
txRemainingByteCount -= bytesEachWrite;
885+
}
882886

883887
/*Check whether there is RX data in RX FIFO . Read out the RX data so that the RX FIFO would not overrun.*/
884888
if (rxData)

0 commit comments

Comments
 (0)