Skip to content

Commit 238d310

Browse files
author
Alrik Vidstrom
committed
Fix freeze on open() after close() on SD card
It's necessary to save the assigned IRQs, or they will be lost and replaced with FSP_INVALID_VECTOR if the SDCardBlockDevice is destroyed and created again. If we don't, r_sdhi_card_identify() inside the Renesas FSP will return FSP_ERR_RESPONSE because of a timeout when it tries to put the card in idle mode, and R_SDHI_MediaInit() will fail.
1 parent 2a9b446 commit 238d310

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

libraries/BlockDevices/SDCardBlockDevice.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,35 @@ int SDCardBlockDevice::open() {
243243
// context pointer to r_sdhi_access_irq_process(), causing a hard fault.
244244
// -->
245245
static bool wasOpenedBefore = false;
246+
static IRQn_Type accessAssigned = (IRQn_Type) 0;
247+
static IRQn_Type cardAssigned = (IRQn_Type) 0;
248+
static IRQn_Type sdioAssigned = (IRQn_Type) 0;
246249
if (false == wasOpenedBefore)
247250
{
248251
IRQManager::getInstance().addPeripheral(IRQ_SDCARD,&cfg);
252+
#ifdef USE_DMAC
253+
IRQManager::getInstance().addDMA(g_transfer0_extend);
254+
#endif
249255
wasOpenedBefore = true;
256+
// It's also necessary to save the assigned IRQs, or they will be lost and
257+
// replaced with FSP_INVALID_VECTOR if the SDCardBlockDevice is destroyed
258+
// and created again
259+
accessAssigned = cfg.access_irq;
260+
cardAssigned = cfg.card_irq;
261+
sdioAssigned = cfg.sdio_irq;
262+
}
263+
else
264+
{
265+
// Was destroyed and is now created again, so restore the previously assigned IRQs,
266+
// because if we don't, r_sdhi_card_identify() inside the Renesas FSP will
267+
// return FSP_ERR_RESPONSE because of a timeout when it tries to put the card in
268+
// idle mode, and R_SDHI_MediaInit() will fail a bit further down in this function
269+
cfg.access_irq = accessAssigned;
270+
cfg.card_irq = cardAssigned;
271+
cfg.sdio_irq = sdioAssigned;
250272
}
251273
// <--
252274

253-
#ifdef USE_DMAC
254-
IRQManager::getInstance().addDMA(g_transfer0_extend);
255-
#endif
256-
257275
#ifdef SDHI_DEBUG
258276
Serial.println("[CALL]: R_SDHI_Open");
259277
#endif

0 commit comments

Comments
 (0)