4444#define CMD55 (55) /* APP_CMD */
4545#define CMD58 (58) /* READ_OCR */
4646
47- static volatile
48- DSTATUS Stat = STA_NOINIT ; /* Physical drive status */
47+ #define DELAY_TIMER (delta_mtime < (SystemCoreClock/4000.0 * wt))
4948
50- // static volatile
51- // UINT Timer1, Timer2; /* 1kHz decrement timer stopped at zero (disk_timerproc()) */
5249static volatile
53- UINT delay_timer1 , delay_timer2 ; /* 1kHz decrement timer stopped at zero (disk_timerproc()) */
50+ DSTATUS Stat = STA_NOINIT ; /* Physical drive status */
5451
5552static
5653BYTE CardType ; /* Card type flags */
@@ -135,13 +132,14 @@ int wait_ready ( /* 1:Ready, 0:Timeout */
135132)
136133{
137134 BYTE d ;
135+ uint64_t start_mtime , delta_mtime ;
138136
139- // Timer2 = wt;
140- delay_timer2 = wt ;
137+ start_mtime = get_time ();
141138 do {
142139 d = xchg_spi (0xFF );
143140 /* This loop takes a time. Insert rot_rdq() here for multitask envilonment. */
144- } while (d != 0xFF && delay_timer2 ); /* Wait for card goes ready or timeout */
141+ delta_mtime = get_timer_value () - start_mtime ;
142+ } while (d != 0xFF && DELAY_TIMER ); /* Wait for card goes ready or timeout */
145143
146144 return (d == 0xFF ) ? 1 : 0 ;
147145}
@@ -189,12 +187,15 @@ int rcvr_datablock ( /* 1:OK, 0:Error */
189187)
190188{
191189 BYTE token ;
190+ uint64_t start_mtime , delta_mtime ;
192191
193- delay_timer1 = 200 ;
192+ UINT wt = 200 ;
193+ start_mtime = get_time ();
194194 do { /* Wait for DataStart token in timeout of 200ms */
195195 token = xchg_spi (0xFF );
196196 /* This loop will take a time. Insert rot_rdq() here for multitask envilonment. */
197- } while ((token == 0xFF ) && delay_timer1 );
197+ delta_mtime = get_timer_value () - start_mtime ;
198+ } while ((token == 0xFF ) && DELAY_TIMER );
198199 if (token != 0xFE ) return 0 ; /* Function fails if invalid DataStart token or timeout */
199200
200201 rcvr_spi_multi (buff , btr ); /* Store trailing data to the buffer */
@@ -267,11 +268,12 @@ DSTATUS disk_initialize (
267268)
268269{
269270 BYTE n , cmd , ty , ocr [4 ];
271+ uint64_t start_mtime , delta_mtime ;
270272
271273
272274 if (drv ) return STA_NOINIT ; /* Supports only drive 0 */
273275 init_spi (); /* Initialize SPI */
274- delay_1ms (10 );
276+ delay_1ms (10 );
275277
276278 if (Stat & STA_NODISK ) return Stat ; /* Is card existing in the soket? */
277279
@@ -281,12 +283,16 @@ DSTATUS disk_initialize (
281283
282284 ty = 0 ;
283285 if (send_cmd (CMD0 , 0 ) == 1 ) { /* Put the card SPI/Idle state */
284- delay_timer1 = 1000 ; /* Initialization timeout = 1 sec */
286+ UINT wt = 1000 ; /* Initialization timeout = 1 sec */
287+ start_mtime = get_time ();
288+ delta_mtime = 0 ;
285289 if (send_cmd (CMD8 , 0x1AA ) == 1 ) { /* SDv2? */
286290 for (n = 0 ; n < 4 ; n ++ ) ocr [n ] = xchg_spi (0xFF ); /* Get 32 bit return value of R7 resp */
287291 if (ocr [2 ] == 0x01 && ocr [3 ] == 0xAA ) { /* Is the card supports vcc of 2.7-3.6V? */
288- while (delay_timer1 && send_cmd (ACMD41 , 1UL << 30 )) ; /* Wait for end of initialization with ACMD41(HCS) */
289- if (delay_timer1 && send_cmd (CMD58 , 0 ) == 0 ) { /* Check CCS bit in the OCR */
292+ while (DELAY_TIMER && send_cmd (ACMD41 , 1UL << 30 )) {
293+ delta_mtime = get_timer_value () - start_mtime ;
294+ } /* Wait for end of initialization with ACMD41(HCS) */
295+ if (DELAY_TIMER && send_cmd (CMD58 , 0 ) == 0 ) { /* Check CCS bit in the OCR */
290296 for (n = 0 ; n < 4 ; n ++ ) ocr [n ] = xchg_spi (0xFF );
291297 ty = (ocr [0 ] & 0x40 ) ? CT_SD2 | CT_BLOCK : CT_SD2 ; /* Card id SDv2 */
292298 }
@@ -297,8 +303,10 @@ DSTATUS disk_initialize (
297303 } else {
298304 ty = CT_MMC ; cmd = CMD1 ; /* MMCv3 (CMD1(0)) */
299305 }
300- while (delay_timer1 && send_cmd (cmd , 0 )) ; /* Wait for end of initialization */
301- if (!delay_timer1 || send_cmd (CMD16 , 512 ) != 0 ) /* Set block length: 512 */
306+ while (DELAY_TIMER && send_cmd (cmd , 0 )) {
307+ delta_mtime = get_timer_value () - start_mtime ;
308+ } /* Wait for end of initialization */
309+ if (!DELAY_TIMER || send_cmd (CMD16 , 512 ) != 0 ) /* Set block length: 512 */
302310 ty = 0 ;
303311 }
304312 }
0 commit comments