Skip to content

Commit c39547d

Browse files
committed
Fix SD card wait that was using 0xFFFFFFFF and so could pretty much lock up forever
Also allow us to include this from elsewhere with SD_OVERRIDE and to force it to use polling
1 parent f7b41f3 commit c39547d

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

libs/filesystem/fat_sd/ffconf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
/ Drive/Volume Configurations
151151
/---------------------------------------------------------------------------*/
152152

153-
#define _VOLUMES 8
153+
#define _VOLUMES 1
154154
/* Number of volumes (logical drives) to be used. */
155155

156156

targets/stm32/sdio_sdcard_stm32f4.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,12 @@ SD_Error SD_WaitReadOperation(void);
234234
SD_Error SD_WaitWriteOperation(void);
235235

236236
// undo the STM32F1-specific defines
237+
#ifndef SD_OVERRIDE
237238
#undef SD_DMA_MODE
238239
#undef SD_INTERRUPT_MODE
239240
#undef SD_POLLING_MODE
240241
#define SD_DMA_MODE 1
242+
#endif
241243

242244
SD_Error SD_SetDeviceMode(uint32_t Mode) {
243245
NOT_USED(Mode); // ignored, it's now build-time
@@ -417,6 +419,8 @@ SDCardState SD_GetState(void);
417419
#define SD_HALFFIFO ((uint32_t)0x00000008)
418420
#define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
419421

422+
#define SD_WAIT_CYCLES ((uint32_t)1000000) // how long to wait at most if polling
423+
420424
/**
421425
* @brief Command Class Supported
422426
*/
@@ -1363,7 +1367,7 @@ SD_Error SD_ReadBlock(uint32_t ReadAddr, uint32_t *readbuff, uint16_t BlockSize)
13631367
errorstatus = SD_START_BIT_ERR;
13641368
return(errorstatus);
13651369
}
1366-
count = SD_DATATIMEOUT;
1370+
count = SD_WAIT_CYCLES;
13671371
while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
13681372
{
13691373
*tempbuff = SDIO_ReadData();
@@ -1539,16 +1543,17 @@ SD_Error SD_WaitReadOperation(void)
15391543
SD_Error errorstatus = SD_OK;
15401544
uint32_t timeout;
15411545

1542-
timeout = SD_DATATIMEOUT;
1543-
1546+
#ifndef SD_POLLING_MODE
1547+
timeout = SD_WAIT_CYCLES;
15441548
while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
15451549
{
15461550
timeout--;
15471551
}
1552+
#endif
15481553

15491554
DMAEndOfTransfer = 0x00;
15501555

1551-
timeout = SD_DATATIMEOUT;
1556+
timeout = SD_WAIT_CYCLES;
15521557

15531558
while(((SDIO->STA & SDIO_FLAG_RXACT)) && (timeout > 0))
15541559
{
@@ -1929,7 +1934,7 @@ SD_Error SD_WaitWriteOperation(void)
19291934
SD_Error errorstatus = SD_OK;
19301935
uint32_t timeout;
19311936

1932-
timeout = SD_DATATIMEOUT;
1937+
timeout = SD_WAIT_CYCLES;
19331938

19341939
while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
19351940
{
@@ -1938,7 +1943,7 @@ SD_Error SD_WaitWriteOperation(void)
19381943

19391944
DMAEndOfTransfer = 0x00;
19401945

1941-
timeout = SD_DATATIMEOUT;
1946+
timeout = SD_WAIT_CYCLES;
19421947

19431948
while(((SDIO->STA & SDIO_FLAG_TXACT)) && (timeout > 0))
19441949
{
@@ -2096,7 +2101,7 @@ SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
20962101

20972102
/*!< Wait till the card is in programming state */
20982103
errorstatus = IsCardProgramming(&cardstate);
2099-
delay = SD_DATATIMEOUT;
2104+
delay = SD_WAIT_CYCLES;
21002105
while ((delay > 0) && (errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate)))
21012106
{
21022107
errorstatus = IsCardProgramming(&cardstate);
@@ -2246,7 +2251,7 @@ SD_Error SD_SendSDStatus(uint32_t *psdstatus)
22462251
return(errorstatus);
22472252
}
22482253

2249-
count = SD_DATATIMEOUT;
2254+
count = SD_WAIT_CYCLES;
22502255
while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
22512256
{
22522257
*psdstatus = SDIO_ReadData();

0 commit comments

Comments
 (0)