SD-Card Insert/Remove Handlers
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14891
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: SD-Card Insert/Remove Handlers
Try a series of sdcConnect() / sdcDisconnect() without removing the card, eventually inserting small delays, does this trigger the problem?
Giovanni
Giovanni
Re: SD-Card Insert/Remove Handlers
So i tried this
static void InsertHandler(eventid_t id) {
(void)id;
FRESULT err;
/*
* On insertion SDC init.
*/
// if (sdcConnect(&SDCD1))
// return;
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
/*
* Try to mount the fsys.
*/
err = f_mount(0, &SDC_FS);
if (err != FR_OK) {
sdcDisconnect(&SDCD1);
return;
}
palSetPad(GPIOG, GPIOG_LED2);
fs_ready = TRUE;
}
And card works ok from the go, non of the connnect / disconnects cause an issue. But if i remove the card and re-insert it it fails an loops in the first while loop.
This is very predicable, i.e. it will always fail on the 2nd, 4th, 6th 8th 10th etc insert.
static void InsertHandler(eventid_t id) {
(void)id;
FRESULT err;
/*
* On insertion SDC init.
*/
// if (sdcConnect(&SDCD1))
// return;
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
sdcDisconnect(&SDCD1);
if (sdcConnect(&SDCD1))
while(1){};
/*
* Try to mount the fsys.
*/
err = f_mount(0, &SDC_FS);
if (err != FR_OK) {
sdcDisconnect(&SDCD1);
return;
}
palSetPad(GPIOG, GPIOG_LED2);
fs_ready = TRUE;
}
And card works ok from the go, non of the connnect / disconnects cause an issue. But if i remove the card and re-insert it it fails an loops in the first while loop.
This is very predicable, i.e. it will always fail on the 2nd, 4th, 6th 8th 10th etc insert.
- Giovanni
- Site Admin
- Posts: 14891
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: SD-Card Insert/Remove Handlers
I fail to see how powering the card on and off can cause this.
Could it be some kind of HW failure?
Giovanni
Could it be some kind of HW failure?
Giovanni
Re: SD-Card Insert/Remove Handlers
As i stated before, i can run my super loop code which uses FatFS and STMLib SD-Card driver on the same hardware and every card works fine, so i dont believe it can be hardware.
- Giovanni
- Site Admin
- Posts: 14891
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: SD-Card Insert/Remove Handlers
You could step into sdcConnect() and see where exactly it fails.
Giovanni
Giovanni
Re: SD-Card Insert/Remove Handlers
it fails here
/* V2.0 cards detection.*/
if (!sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_SEND_IF_COND,
MMCSD_CMD8_PATTERN, resp)) {
sdcp->cardmode = SDC_MODE_CARDTYPE_SDV20;
/* Voltage verification.*/
if (((resp[0] >> 8) & 0xF) != 1)
goto failed;
/* V2.0 cards detection.*/
if (!sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_SEND_IF_COND,
MMCSD_CMD8_PATTERN, resp)) {
sdcp->cardmode = SDC_MODE_CARDTYPE_SDV20;
/* Voltage verification.*/
if (((resp[0] >> 8) & 0xF) != 1)
goto failed;
- Giovanni
- Site Admin
- Posts: 14891
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: SD-Card Insert/Remove Handlers
So the card reports something different each time it is powered up? the card manual could explain the behaviour.
That code just sends one simple command and checks the answer.
Giovanni
That code just sends one simple command and checks the answer.
Giovanni
Re: SD-Card Insert/Remove Handlers
i fail to see how this can be possibly be a card issue, as i've said before i'm running the same card with my own super loop (no OS) with FatFS and stm32f4 sdio libs and it work everytime time no problems.
I see STM32_SDIO_DIV_LS is 120
so i take it this is 48Mhz / 120 = the 400Khz startup clock frequency ?
so i see STM32_SDIO_DIV_HS is 0
the same maths would denote SD-Card clock running at 48Mhz is this correct ?
I see STM32_SDIO_DIV_LS is 120
so i take it this is 48Mhz / 120 = the 400Khz startup clock frequency ?
so i see STM32_SDIO_DIV_HS is 0
the same maths would denote SD-Card clock running at 48Mhz is this correct ?
Re: SD-Card Insert/Remove Handlers
Done abit more work on this and found the following
The alternate insert fails on this call in sdcConnect
/* V2.0 cards detection.*/
if (!sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_SEND_IF_COND,
MMCSD_CMD8_PATTERN, resp)) {
digging deeper into sdc_lld_collect_errors function
bool_t sdc_lld_send_cmd_short_crc(SDCDriver *sdcp, uint8_t cmd, uint32_t arg,
uint32_t *resp) {
uint32_t sta;
(void)sdcp;
SDIO->ARG = arg;
SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
while (((sta = SDIO->STA) & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
SDIO_STA_CCRCFAIL)) == 0)
;
SDIO->ICR = sta;
if ((sta & (SDIO_STA_CTIMEOUT | SDIO_STA_CCRCFAIL)) != 0) {
sdc_lld_collect_errors(sdcp, sta);
return CH_FAILED;
}
*resp = SDIO->RESP1;
return CH_SUCCESS;
}
This reports that SDIO_STA_CTIMEOUT is faulting
The alternate insert fails on this call in sdcConnect
/* V2.0 cards detection.*/
if (!sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_SEND_IF_COND,
MMCSD_CMD8_PATTERN, resp)) {
digging deeper into sdc_lld_collect_errors function
bool_t sdc_lld_send_cmd_short_crc(SDCDriver *sdcp, uint8_t cmd, uint32_t arg,
uint32_t *resp) {
uint32_t sta;
(void)sdcp;
SDIO->ARG = arg;
SDIO->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
while (((sta = SDIO->STA) & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
SDIO_STA_CCRCFAIL)) == 0)
;
SDIO->ICR = sta;
if ((sta & (SDIO_STA_CTIMEOUT | SDIO_STA_CCRCFAIL)) != 0) {
sdc_lld_collect_errors(sdcp, sta);
return CH_FAILED;
}
*resp = SDIO->RESP1;
return CH_SUCCESS;
}
This reports that SDIO_STA_CTIMEOUT is faulting
- Giovanni
- Site Admin
- Posts: 14891
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: SD-Card Insert/Remove Handlers
Try inserting a 100mS delay before sdcConnect(), it is possible that card needs more time for power up.
Giovanni
Giovanni