Skip to content

Commit 594699b

Browse files
committed
stmhal: Protect SD_WriteBlocks by IRQ disable/enable pair.
1 parent 90ba80d commit 594699b

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

stmhal/sdcard.c

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

147-
HAL_SD_ErrorTypedef err;
148-
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.
149150
__disable_irq();
150-
err = HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks);
151+
HAL_SD_ErrorTypedef err = HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks);
151152
__enable_irq();
152153

153154
if (err != SD_OK) {
@@ -168,7 +169,14 @@ bool sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl
168169
return false;
169170
}
170171

171-
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) {
172180
return false;
173181
}
174182

0 commit comments

Comments
 (0)