SD Card Remove and plug again Problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SD Card Remove and plug again Problem

827 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kenji_hung on Wed Feb 24 04:42:05 MST 2016
Hello:
I using LPCOpen sdmmc Demo code(\keil_mcb_1857\periph\periph_sdmmc)+Keil MCB1800 Board to test SD Card Function.
Run the original code,it can success,UART can get message as below:


Initializing RTC (might take few seconds)...Done                                               
Hello NXP Semiconductors                       
***SDCARD demo***                
Opening MESSAGE.TXT from SD Card...Done.                                       
Printing contents of MESSAGE.TXT...                                  
kenji112233445566Close the file.                               
Create a new file (hello.txt).                             
Write text data "Hello world!" to HELLO.TXT                                          
14 bytes written.                
Close the file.
Open root directory.
Directory listing...
         17  MESSAGE.TXT
         14  HELLO.TXT
Test completed.



Then I modify  this demo code,add a function Key to control  Run the code.
First time,press control Key:It can all success,no problem.
Then remove SD Card and plug again.
Press control Key:it show error message as below:



Initializing RTC (might take few seconds)...Done
Hello NXP Semiconductors
***SDCARD demo***
Opening MESSAGE.TXT from SD Card...Failed.
Failed with rc=1.
                 Done.
Printing contents of MESSAGE.TXT...
Failed with rc=9.
                 Close the file.
Failed with rc=9.
                 Create a new file (hello.txt).
Failed with rc=1.
                 Write text data "Hello world!" to HELLO.TXT
Failed with rc=9.
                 0 bytes written.
Close the file.
Failed with rc=9.
                 Open root directory.
Failed with rc=1.
                 Directory listing...
Failed with rc=9.
                 Test completed.







Why?What do I do wrong?
The Key Function code as below:


//---------------------Key function-------------------------------------
App_SDMMC_Init();
/* There is no need to initialize RTC here it is called by fs init
  but it takes long time hence doing it before calling f_open */
debugstr("Initializing RTC (might take few seconds)...");
rtc_initialize();
debugstr("Done\r\n");
debugstr("Hello NXP Semiconductors\r\n\r\n***SDCARD demo***\r\n");
NVIC_DisableIRQ(SDIO_IRQn);
/* Enable SD/MMC Interrupt */
NVIC_EnableIRQ(SDIO_IRQn);
f_mount(0, &Fatfs);/* Register volume work area (never fails) */
debugstr("Opening MESSAGE.TXT from SD Card...");
rc = f_open(&Fil, "MESSAGE.TXT", FA_READ);
if (rc) {
debugstr("Failed.\r\n");
die(rc);
}
debugstr("Done.\r\n");
debugstr("Printing contents of MESSAGE.TXT...\r\n");
for (;; ) {
/* Read a chunk of file */
rc = f_read(&Fil, (void *)cbuf, BUFFER_SIZE, &br);
if (rc || !br) {
break;/* Error or end of file */
}
for (i = 0; i < br; i++)/* Type the data */
#if (defined(BOARD_HITEX_EVA_1850) || defined(BOARD_HITEX_EVA_4350))
{board_uart_out_ch((char) cbuf); }
#else
{DEBUGOUT("%c", cbuf); }
#endif
}
if (rc) {
die(rc);
}
debugstr("Close the file.\r\n");
rc = f_close(&Fil);
if (rc) {
die(rc);
}
debugstr("Create a new file (hello.txt).\r\n");
rc = f_open(&Fil, "HELLO.TXT", FA_WRITE | FA_CREATE_ALWAYS);
if (rc) {
die(rc);
}
debugstr("Write text data \"Hello world!\" to HELLO.TXT\r\n");
rc = f_write(&Fil, "Hello world!\r\n", 14, &bw);
if (rc) {
die(rc);
}
sprintf(debugBuf, "%u bytes written.\r\n", bw);
debugstr(debugBuf);
debugstr("Close the file.\r\n");
rc = f_close(&Fil);
if (rc) {
die(rc);
}
debugstr("Open root directory.\r\n");
rc = f_opendir(&dir, "");
if (rc) {
die(rc);
}
debugstr("Directory listing...\r\n");
for (;; ) {
/* Read a directory item */
rc = f_readdir(&dir, &fno);
if (rc || !fno.fname[0]) {
break;/* Error or end of dir */
}
if (fno.fattrib & AM_DIR) {
sprintf(debugBuf, "   <dir>  %s\r\n", fno.fname);
}
else {
sprintf(debugBuf, "   %8lu  %s\r\n", fno.fsize, fno.fname);
}
debugstr(debugBuf);
}
if (rc) {
die(rc);
}
debugstr("Test completed.\r\n");



0 Kudos
Reply
2 Replies

747 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rocketdawg on Thu Feb 25 09:58:42 MST 2016
I cannot help. But first one needs hardware support.  There is a pin on the SD Card Connector that may notify the application that the SD Card is being removed.
You probably have to connect that pin to an interrupt in.
When you get a surprise removal, all bets are off, if you have open files.  The SD card might be corrupt by removing it during writes.
It is best to have the user hit a button or something, so that all the files can be closed, then blink a light that tells the user they can remove the card.
The interrupt in will also detect a card insertion, so you can re init the SD Card stack.

Generally in embedded space, the device is turned off before removing/inserting hardware.
Think about it, would you remove your PC Hard Drive without shutting the PC off?
Even Windows wants you to click on "safe to remove device" so it can close files and gracefully respond to the removal.
0 Kudos
Reply

747 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kenji_hung on Thu Feb 25 04:27:40 MST 2016
Does anyone can help?

I think that LPCOpen  "periph_sdmmc" demo code not support SD Card "Repeat Hot plug".

It only works in the first time plug SD Card.
When remove and plug again,SD Card Read/Write will fail.

Does anyone have other demo code can support "Repeat Hot plug" SD Card?
0 Kudos
Reply