Skip to content

Commit

Permalink
fiixed timeout checking
Browse files Browse the repository at this point in the history
tickstart was inside checking loop
removed wait_ms() calls
added timeout checks for all while loops
  • Loading branch information
JojoS62 committed Dec 16, 2018
1 parent 016abcc commit c03489c
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions SDIOBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,17 @@ int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
addr = addr / _block_size;

// make sure card is ready
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
wait_ms(1);
uint32_t tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
}
}

// receive the data : one block/ multiple blocks is handled in ReadBlocks()
Expand All @@ -199,27 +206,32 @@ int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
if (status == MSD_OK)
{
// wait until DMA finished
uint32_t tickstart = HAL_GetTick();
while (SD_DMA_ReadPending() != SD_TRANSFER_OK)
{
uint32_t tickstart = HAL_GetTick();
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
}
// make sure card is ready
tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
wait_ms(10);
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
}
}
else
{
debug_if(SD_DBG, "ReadBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
debug_if(SD_DBG, " hsd.errorcode: %lu 0x%lx\n", hsd.ErrorCode, hsd.ErrorCode);
status = SD_BLOCK_DEVICE_ERROR_READBLOCKS;
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}

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

// make sure card is ready
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
wait_ms(1);
uint32_t tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
}

int status = SD_WriteBlocks_DMA(_buffer, addr, blockCnt);
Expand All @@ -267,27 +286,32 @@ int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
if (status == MSD_OK)
{
// wait until DMA finished
uint32_t tickstart = HAL_GetTick();
while (SD_DMA_WritePending() != SD_TRANSFER_OK)
{
uint32_t tickstart = HAL_GetTick();
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
status = SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
// make sure card is ready
tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
wait_ms(1);
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
}
else
{
debug_if(SD_DBG, "WriteBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
debug_if(SD_DBG, " hsd.errorcode: %lu 0x%lx\n", hsd.ErrorCode, hsd.ErrorCode);
status = SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}

unlock();
Expand Down Expand Up @@ -322,14 +346,20 @@ int SDIOBlockDevice::trim(bd_addr_t addr, bd_size_t size)
if (status != 0)
{
debug_if(SD_DBG, "Erase blocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
status = SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
unlock();
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
}
else
{
uint32_t tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
wait_ms(10);
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
}
}
}

Expand Down

0 comments on commit c03489c

Please sign in to comment.