@@ -234,15 +234,14 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
234
234
bool common_hal_busio_spi_configure (busio_spi_obj_t * self ,
235
235
uint32_t baudrate , uint8_t polarity , uint8_t phase , uint8_t bits ) {
236
236
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
+ }
242
240
243
241
if ((polarity == common_hal_busio_spi_get_polarity (self )) &&
244
242
(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 ))) {
246
245
return true;
247
246
}
248
247
@@ -253,10 +252,22 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
253
252
config .cpol = polarity ;
254
253
config .cpha = phase ;
255
254
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 ;
256
259
257
260
LPSPI_Deinit (self -> spi );
258
261
LPSPI_MasterInit (self -> spi , & config , LPSPI_MASTER_CLK_FREQ );
259
262
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
+
260
271
return true;
261
272
}
262
273
@@ -279,6 +290,21 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
279
290
self -> has_lock = false;
280
291
}
281
292
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
+
282
308
bool common_hal_busio_spi_write (busio_spi_obj_t * self ,
283
309
const uint8_t * data , size_t len ) {
284
310
if (len == 0 ) {
@@ -291,17 +317,8 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self,
291
317
lpspi_transfer_t xfer = { 0 };
292
318
xfer .txData = (uint8_t * )data ;
293
319
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 );
301
320
302
- if (status != kStatus_Success ) {
303
- printf ("%s: status %ld\r\n" , __func__ , status );
304
- }
321
+ status_t status = transfer_common (self , & xfer );
305
322
306
323
return status == kStatus_Success ;
307
324
}
@@ -321,15 +338,7 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self,
321
338
xfer .rxData = data ;
322
339
xfer .dataSize = len ;
323
340
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 );
333
342
334
343
return status == kStatus_Success ;
335
344
}
@@ -349,15 +358,7 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou
349
358
xfer .rxData = data_in ;
350
359
xfer .dataSize = len ;
351
360
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 );
361
362
362
363
return status == kStatus_Success ;
363
364
}
0 commit comments