@@ -191,16 +191,15 @@ bool sdcard_power_on(void) {
191191 sd_handle .Init .ClockDiv = SDIO_TRANSFER_CLK_DIV ;
192192
193193 // init the SD interface, with retry if it's not ready yet
194- HAL_SD_CardInfoTypedef cardinfo ;
195- for (int retry = 10 ; HAL_SD_Init (& sd_handle , & cardinfo ) != SD_OK ; retry -- ) {
194+ for (int retry = 10 ; HAL_SD_Init (& sd_handle ) != HAL_OK ; retry -- ) {
196195 if (retry == 0 ) {
197196 goto error ;
198197 }
199198 mp_hal_delay_ms (50 );
200199 }
201200
202201 // configure the SD bus width for wide operation
203- if (HAL_SD_WideBusOperation_Config (& sd_handle , SDIO_BUS_WIDE_4B ) != SD_OK ) {
202+ if (HAL_SD_ConfigWideBusOperation (& sd_handle , SDIO_BUS_WIDE_4B ) != HAL_OK ) {
204203 HAL_SD_DeInit (& sd_handle );
205204 goto error ;
206205 }
@@ -224,9 +223,9 @@ uint64_t sdcard_get_capacity_in_bytes(void) {
224223 if (sd_handle .Instance == NULL ) {
225224 return 0 ;
226225 }
227- HAL_SD_CardInfoTypedef cardinfo ;
228- HAL_SD_Get_CardInfo (& sd_handle , & cardinfo );
229- return cardinfo .CardCapacity ;
226+ HAL_SD_CardInfoTypeDef cardinfo ;
227+ HAL_SD_GetCardInfo (& sd_handle , & cardinfo );
228+ return ( uint64_t ) cardinfo .LogBlockNbr * ( uint64_t ) cardinfo . LogBlockSize ;
230229}
231230
232231void SDIO_IRQHandler (void ) {
@@ -243,13 +242,37 @@ void SDMMC2_IRQHandler(void) {
243242}
244243#endif
245244
245+ STATIC HAL_StatusTypeDef sdcard_wait_finished (SD_HandleTypeDef * sd , uint32_t timeout ) {
246+ // Wait for HAL driver to be ready (eg for DMA to finish)
247+ uint32_t start = HAL_GetTick ();
248+ while (sd -> State == HAL_SD_STATE_BUSY ) {
249+ if (HAL_GetTick () - start >= timeout ) {
250+ return HAL_TIMEOUT ;
251+ }
252+ }
253+ // Wait for SD card to complete the operation
254+ for (;;) {
255+ HAL_SD_CardStateTypeDef state = HAL_SD_GetCardState (sd );
256+ if (state == HAL_SD_CARD_TRANSFER ) {
257+ return HAL_OK ;
258+ }
259+ if (!(state == HAL_SD_CARD_SENDING || state == HAL_SD_CARD_RECEIVING || state == HAL_SD_CARD_PROGRAMMING )) {
260+ return HAL_ERROR ;
261+ }
262+ if (HAL_GetTick () - start >= timeout ) {
263+ return HAL_TIMEOUT ;
264+ }
265+ }
266+ return HAL_OK ;
267+ }
268+
246269mp_uint_t sdcard_read_blocks (uint8_t * dest , uint32_t block_num , uint32_t num_blocks ) {
247270 // check that SD card is initialised
248271 if (sd_handle .Instance == NULL ) {
249- return SD_ERROR ;
272+ return HAL_ERROR ;
250273 }
251274
252- HAL_SD_ErrorTypedef err = SD_OK ;
275+ HAL_StatusTypeDef err = HAL_OK ;
253276
254277 // check that dest pointer is aligned on a 4-byte boundary
255278 uint8_t * orig_dest = NULL ;
@@ -280,18 +303,20 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
280303 // from reading the peripheral the CPU then reads the new data
281304 MP_HAL_CLEANINVALIDATE_DCACHE (dest , num_blocks * SDCARD_BLOCK_SIZE );
282305
283- err = HAL_SD_ReadBlocks_BlockNumber_DMA (& sd_handle , (uint32_t * )dest , block_num , SDCARD_BLOCK_SIZE , num_blocks );
284- if (err == SD_OK ) {
285- // wait for DMA transfer to finish, with a large timeout
286- err = HAL_SD_CheckReadOperation (& sd_handle , 100000000 );
306+ err = HAL_SD_ReadBlocks_DMA (& sd_handle , dest , block_num , num_blocks );
307+ if (err == HAL_OK ) {
308+ err = sdcard_wait_finished (& sd_handle , 60000 );
287309 }
288310
289311 dma_deinit (& SDMMC_RX_DMA );
290312 sd_handle .hdmarx = NULL ;
291313
292314 restore_irq_pri (basepri );
293315 } else {
294- err = HAL_SD_ReadBlocks_BlockNumber (& sd_handle , (uint32_t * )dest , block_num , SDCARD_BLOCK_SIZE , num_blocks );
316+ err = HAL_SD_ReadBlocks (& sd_handle , dest , block_num , num_blocks , 60000 );
317+ if (err == HAL_OK ) {
318+ err = sdcard_wait_finished (& sd_handle , 60000 );
319+ }
295320 }
296321
297322 if (orig_dest != NULL ) {
@@ -306,22 +331,22 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
306331mp_uint_t sdcard_write_blocks (const uint8_t * src , uint32_t block_num , uint32_t num_blocks ) {
307332 // check that SD card is initialised
308333 if (sd_handle .Instance == NULL ) {
309- return SD_ERROR ;
334+ return HAL_ERROR ;
310335 }
311336
312- HAL_SD_ErrorTypedef err = SD_OK ;
337+ HAL_StatusTypeDef err = HAL_OK ;
313338
314339 // check that src pointer is aligned on a 4-byte boundary
315340 if (((uint32_t )src & 3 ) != 0 ) {
316341 // pointer is not aligned, so allocate a temporary block to do the write
317342 uint8_t * src_aligned = m_new_maybe (uint8_t , SDCARD_BLOCK_SIZE );
318343 if (src_aligned == NULL ) {
319- return SD_ERROR ;
344+ return HAL_ERROR ;
320345 }
321346 for (size_t i = 0 ; i < num_blocks ; ++ i ) {
322347 memcpy (src_aligned , src + i * SDCARD_BLOCK_SIZE , SDCARD_BLOCK_SIZE );
323348 err = sdcard_write_blocks (src_aligned , block_num + i , 1 );
324- if (err != SD_OK ) {
349+ if (err != HAL_OK ) {
325350 break ;
326351 }
327352 }
@@ -339,17 +364,19 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
339364 // make sure cache is flushed to RAM so the DMA can read the correct data
340365 MP_HAL_CLEAN_DCACHE (src , num_blocks * SDCARD_BLOCK_SIZE );
341366
342- err = HAL_SD_WriteBlocks_BlockNumber_DMA (& sd_handle , (uint32_t * )src , block_num , SDCARD_BLOCK_SIZE , num_blocks );
343- if (err == SD_OK ) {
344- // wait for DMA transfer to finish, with a large timeout
345- err = HAL_SD_CheckWriteOperation (& sd_handle , 100000000 );
367+ err = HAL_SD_WriteBlocks_DMA (& sd_handle , (uint8_t * )src , block_num , num_blocks );
368+ if (err == HAL_OK ) {
369+ err = sdcard_wait_finished (& sd_handle , 60000 );
346370 }
347371 dma_deinit (& SDMMC_TX_DMA );
348372 sd_handle .hdmatx = NULL ;
349373
350374 restore_irq_pri (basepri );
351375 } else {
352- err = HAL_SD_WriteBlocks_BlockNumber (& sd_handle , (uint32_t * )src , block_num , SDCARD_BLOCK_SIZE , num_blocks );
376+ err = HAL_SD_WriteBlocks (& sd_handle , (uint8_t * )src , block_num , num_blocks , 60000 );
377+ if (err == HAL_OK ) {
378+ err = sdcard_wait_finished (& sd_handle , 60000 );
379+ }
353380 }
354381
355382 return err ;
@@ -392,12 +419,12 @@ STATIC mp_obj_t sd_info(mp_obj_t self) {
392419 if (sd_handle .Instance == NULL ) {
393420 return mp_const_none ;
394421 }
395- HAL_SD_CardInfoTypedef cardinfo ;
396- HAL_SD_Get_CardInfo (& sd_handle , & cardinfo );
422+ HAL_SD_CardInfoTypeDef cardinfo ;
423+ HAL_SD_GetCardInfo (& sd_handle , & cardinfo );
397424 // cardinfo.SD_csd and cardinfo.SD_cid have lots of info but we don't use them
398425 mp_obj_t tuple [3 ] = {
399- mp_obj_new_int_from_ull (cardinfo .CardCapacity ),
400- mp_obj_new_int_from_uint (cardinfo .CardBlockSize ),
426+ mp_obj_new_int_from_ull (( uint64_t ) cardinfo .LogBlockNbr * ( uint64_t ) cardinfo . LogBlockSize ),
427+ mp_obj_new_int_from_uint (cardinfo .LogBlockSize ),
401428 mp_obj_new_int (cardinfo .CardType ),
402429 };
403430 return mp_obj_new_tuple (3 , tuple );
0 commit comments