SD-Card Insert/Remove Handlers

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
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

Post by Giovanni »

Try a series of sdcConnect() / sdcDisconnect() without removing the card, eventually inserting small delays, does this trigger the problem?

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: SD-Card Insert/Remove Handlers

Post by Tabulous »

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.
User avatar
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

Post by Giovanni »

I fail to see how powering the card on and off can cause this.

Could it be some kind of HW failure?

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: SD-Card Insert/Remove Handlers

Post by Tabulous »

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.
User avatar
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

Post by Giovanni »

You could step into sdcConnect() and see where exactly it fails.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: SD-Card Insert/Remove Handlers

Post by Tabulous »

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;
User avatar
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

Post by Giovanni »

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
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: SD-Card Insert/Remove Handlers

Post by Tabulous »

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 ?
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: SD-Card Insert/Remove Handlers

Post by Tabulous »

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
User avatar
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

Post by Giovanni »

Try inserting a 100mS delay before sdcConnect(), it is possible that card needs more time for power up.

Giovanni
Post Reply