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");