@@ -206,6 +206,10 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
206
206
spi_clock_enable (1 << (self -> sck -> periph_index - 1 ));
207
207
reserved_spi [self -> sck -> periph_index - 1 ] = true;
208
208
209
+ // Always start at 250khz which is what SD cards need. They are sensitive to
210
+ // SPI bus noise before they are put into SPI mode.
211
+ const uint32_t default_baudrate = 250000UL ;
212
+
209
213
self -> handle .Instance = SPIx ;
210
214
self -> handle .Init .Mode = SPI_MODE_MASTER ;
211
215
// Direction change only required for RX-only, see RefMan RM0090:884
@@ -218,16 +222,16 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
218
222
self -> handle .Init .CLKPolarity = SPI_POLARITY_LOW ;
219
223
self -> handle .Init .CLKPhase = SPI_PHASE_1EDGE ;
220
224
self -> handle .Init .NSS = SPI_NSS_SOFT ;
221
- self -> handle .Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256 ;
225
+ self -> handle .Init .BaudRatePrescaler = stm32_baud_to_spi_div ( default_baudrate , & self -> prescaler , get_busclock ( self -> handle . Instance )) ;
222
226
self -> handle .Init .FirstBit = SPI_FIRSTBIT_MSB ;
223
227
self -> handle .Init .TIMode = SPI_TIMODE_DISABLE ;
224
228
self -> handle .Init .CRCCalculation = SPI_CRCCALCULATION_DISABLE ;
225
229
self -> handle .Init .CRCPolynomial = 10 ;
226
230
if (HAL_SPI_Init (& self -> handle ) != HAL_OK ) {
227
231
mp_raise_ValueError (translate ("SPI init error" ));
228
232
}
229
- self -> baudrate = ( get_busclock ( SPIx ) / 16 ) ;
230
- self -> prescaler = 16 ;
233
+ self -> baudrate = default_baudrate ;
234
+ // self->prescaler = 16; // Initialised above by stm32_baud_to_spi_div
231
235
self -> half_duplex = half_duplex ;
232
236
self -> polarity = 0 ;
233
237
self -> phase = 0 ;
@@ -381,7 +385,7 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
381
385
382
386
uint32_t common_hal_busio_spi_get_frequency (busio_spi_obj_t * self ) {
383
387
// returns actual frequency
384
- uint32_t result = HAL_RCC_GetPCLK2Freq ( ) / self -> prescaler ;
388
+ uint32_t result = get_busclock ( self -> handle . Instance ) / self -> prescaler ;
385
389
return result ;
386
390
}
387
391
0 commit comments