Skip to content

Commit 42b6419

Browse files
committed
Merge branch 'dhylands-fix-sdcard-read'
2 parents 5fa5ca4 + 594699b commit 42b6419

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

stmhal/sdcard.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ bool sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks)
144144
return false;
145145
}
146146

147-
if (HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks) != SD_OK) {
147+
// We must disable IRQs because the SDIO peripheral has a small FIFO
148+
// buffer and we can't let it fill up in the middle of a read.
149+
// This will not be needed when SD uses DMA for transfer.
150+
__disable_irq();
151+
HAL_SD_ErrorTypedef err = HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks);
152+
__enable_irq();
153+
154+
if (err != SD_OK) {
148155
return false;
149156
}
150157

@@ -162,7 +169,14 @@ bool sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl
162169
return false;
163170
}
164171

165-
if (HAL_SD_WriteBlocks(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks) != SD_OK) {
172+
// We must disable IRQs because the SDIO peripheral has a small FIFO
173+
// buffer and we can't let it drain to empty in the middle of a write.
174+
// This will not be needed when SD uses DMA for transfer.
175+
__disable_irq();
176+
HAL_SD_ErrorTypedef err = HAL_SD_WriteBlocks(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks);
177+
__enable_irq();
178+
179+
if (err != SD_OK) {
166180
return false;
167181
}
168182

0 commit comments

Comments
 (0)