I have a board with a TM4C123GH6PM and, amongst other things, an SD card attached to SSI0. I have run through the FatFS (sd-card) example code, spent many hours reading through this forum and elsewhere, and can only claim partial success. First, the card will mount successfully. I can use the sd-card.c example code (modified to remove the display portions) and through the UART I can list and cat files that were placed on the card by my Windows machine or my the TM4C. However, when I attempt to write a new file I get a FR_DISK_ERR error. Stepping though the code, the issue appears to get thrown within the xmit_datablock section of the mmc-dk-tm4c123g.c
When this occurs, a file of the correct name is created but is of 0 size. In the output below, the 28 byte file was created on my Windows machine. The 0 byte files were all "created" by the TM4C. I'm not trusting the example code's listing of available space. It's an 8GB Samsung microSD card, but I'm not needing all of that space for my application.
Since I can read files on the card (and apparently can create a file) I am making the assumption that the physical connections are correct. Has anyone seen this particular issue or have suggestions of where to go next?
My pared-down main related to the SD card is as follows. Ideally this example code would list the files on the card, delete certain files, and then write a new file. This is what I need to accomplish in the end, so that is what I was testing.
Any suggestions would be appreciated! Thank you.
int main(void) { // SD Card Operations Variables int nStatus; FRESULT iFResult; uint32_t bw; // Set the clock internal to the Tiva to 40 MHz ROM_SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //************************************************************* // Proceed through each startup and configuration routine * //************************************************************* ConfigurePeripherals(); ConfigureSSI(); //************************************************************* // Configure SysTick for a 100Hz interrupt. The FatFs driver * // wants a 10 ms tick. * //************************************************************* ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100); ROM_SysTickEnable(); ROM_SysTickIntEnable(); //************************************************************* // Enable interrupts. * //************************************************************* ROM_IntMasterEnable(); ROM_SSIDisable(SSI0_BASE); ROM_SSIConfigSetExpClk(SSI0_BASE, ROM_SysCtlClockGet(), SSI_FRF_MOTO_MODE_0, SSI_MODE_MASTER, 400000, 8); ROM_SSIEnable(SSI0_BASE); //************************************************************* // Mount the file system, using logical disk 0. * //************************************************************* iFResult = f_mount(0, &g_sFatFs); if(iFResult != FR_OK) { UARTprintf(UART_DEBUG, "f_mount error: %s\n", StringFromFResult(iFResult)); UARTprintf(UART_DEBUG, "f_mount error: \n"); return(1); } else { UARTprintf(UART_DEBUG, "SD Card mounted successfully\n"); } BOOL a = wait_ready(); DSTATUS errd; if(a) { send_initial_clock_train(); errd = disk_initialize(0); UARTprintf(UART_DEBUG, "\nInitialising disk 0. Status = %i\n", errd); } Cmd_ls(1); iFResult = f_unlink("TEST2.TXT"); Cmd_ls(1); FIL fil; SysCtlDelay(SysCtlClockGet()/3); uint32_t count = 8*512; iFResult = f_open(&fil, "TEST2.TXT", FA_CREATE_ALWAYS|FA_WRITE); SysCtlDelay(SysCtlClockGet()/3); if(iFResult != FR_OK) { UARTprintf(UART_DEBUG, "fresult: %s\n", StringFromFResult(iFResult)); } else { UARTprintf(UART_DEBUG, "\n Opened SD card\n"); } iFResult = f_write(&fil, "Hello world\n", 12, &count); if(iFResult != FR_OK) { UARTprintf(UART_DEBUG, "Error writing to file: %s\n", StringFromFResult(iFResult)); } SysCtlDelay(SysCtlClockGet()/3); iFResult = f_close(&fil); SysCtlDelay(SysCtlClockGet()/3); UARTprintf(UART_DEBUG, "File size is %u\n",fil.fsize); while(1){}; }
My output on the UART shows:
SD Card mounted successfully
Initialising disk 0. Status = 0
----- 1980/00/00 00:00 0 NEWFILE.TXT
----A 2007/06/05 11:38 0 TESTFILE.TXT
----A 2007/06/05 11:38 0 TEST.TXT
----A 2007/06/05 11:38 0 TEST2.TXT
----A 2015/08/03 15:33 28 BLAHTX~1.TXT
5 File(s), 28 bytes total
0 Dir(s), 2022246978K bytes free
----- 1980/00/00 00:00 0 NEWFILE.TXT
----A 2007/06/05 11:38 0 TESTFILE.TXT
----A 2007/06/05 11:38 0 TEST.TXT
----A 2015/08/03 15:33 28 BLAHTX~1.TXT
4 File(s), 28 bytes total
0 Dir(s), 2022246978K bytes free
Opened SD card
Error writing to file: FR_DISK_ERR
File size is 0