Skip to content

Commit c03489c

Browse files
committed
fiixed timeout checking
tickstart was inside checking loop removed wait_ms() calls added timeout checks for all while loops
1 parent 016abcc commit c03489c

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

SDIOBlockDevice.cpp

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,17 @@ int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
186186
addr = addr / _block_size;
187187

188188
// make sure card is ready
189-
while (SD_GetCardState() != SD_TRANSFER_OK)
190189
{
191-
// wait until SD ready
192-
wait_ms(1);
190+
uint32_t tickstart = HAL_GetTick();
191+
while (SD_GetCardState() != SD_TRANSFER_OK)
192+
{
193+
// wait until SD ready
194+
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
195+
{
196+
unlock();
197+
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
198+
}
199+
}
193200
}
194201

195202
// receive the data : one block/ multiple blocks is handled in ReadBlocks()
@@ -199,27 +206,32 @@ int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
199206
if (status == MSD_OK)
200207
{
201208
// wait until DMA finished
209+
uint32_t tickstart = HAL_GetTick();
202210
while (SD_DMA_ReadPending() != SD_TRANSFER_OK)
203211
{
204-
uint32_t tickstart = HAL_GetTick();
205212
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
206213
{
207214
unlock();
208215
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
209216
}
210217
}
211218
// make sure card is ready
219+
tickstart = HAL_GetTick();
212220
while (SD_GetCardState() != SD_TRANSFER_OK)
213221
{
214222
// wait until SD ready
215-
wait_ms(10);
223+
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
224+
{
225+
unlock();
226+
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
227+
}
216228
}
217229
}
218230
else
219231
{
220232
debug_if(SD_DBG, "ReadBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
221-
debug_if(SD_DBG, " hsd.errorcode: %lu 0x%lx\n", hsd.ErrorCode, hsd.ErrorCode);
222-
status = SD_BLOCK_DEVICE_ERROR_READBLOCKS;
233+
unlock();
234+
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
223235
}
224236

225237
unlock();
@@ -255,10 +267,17 @@ int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
255267
addr = addr / _block_size;
256268

257269
// make sure card is ready
258-
while (SD_GetCardState() != SD_TRANSFER_OK)
259270
{
260-
// wait until SD ready
261-
wait_ms(1);
271+
uint32_t tickstart = HAL_GetTick();
272+
while (SD_GetCardState() != SD_TRANSFER_OK)
273+
{
274+
// wait until SD ready
275+
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
276+
{
277+
unlock();
278+
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
279+
}
280+
}
262281
}
263282

264283
int status = SD_WriteBlocks_DMA(_buffer, addr, blockCnt);
@@ -267,27 +286,32 @@ int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
267286
if (status == MSD_OK)
268287
{
269288
// wait until DMA finished
289+
uint32_t tickstart = HAL_GetTick();
270290
while (SD_DMA_WritePending() != SD_TRANSFER_OK)
271291
{
272-
uint32_t tickstart = HAL_GetTick();
273292
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
274293
{
275294
unlock();
276-
status = SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
295+
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
277296
}
278297
}
279298
// make sure card is ready
299+
tickstart = HAL_GetTick();
280300
while (SD_GetCardState() != SD_TRANSFER_OK)
281301
{
282302
// wait until SD ready
283-
wait_ms(1);
303+
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
304+
{
305+
unlock();
306+
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
307+
}
284308
}
285309
}
286310
else
287311
{
288312
debug_if(SD_DBG, "WriteBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
289-
debug_if(SD_DBG, " hsd.errorcode: %lu 0x%lx\n", hsd.ErrorCode, hsd.ErrorCode);
290-
status = SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
313+
unlock();
314+
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
291315
}
292316

293317
unlock();
@@ -322,14 +346,20 @@ int SDIOBlockDevice::trim(bd_addr_t addr, bd_size_t size)
322346
if (status != 0)
323347
{
324348
debug_if(SD_DBG, "Erase blocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
325-
status = SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
349+
unlock();
350+
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
326351
}
327352
else
328353
{
354+
uint32_t tickstart = HAL_GetTick();
329355
while (SD_GetCardState() != SD_TRANSFER_OK)
330356
{
331357
// wait until SD ready
332-
wait_ms(10);
358+
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
359+
{
360+
unlock();
361+
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
362+
}
333363
}
334364
}
335365

0 commit comments

Comments
 (0)