This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123G SD Card Write Failure

Other Parts Discussed in Thread: TM4C123GH6PM, TEST2, CSD

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

  • Ryan,

     I can't be any help from a code point of view, but here's an idea as to what may be wrong...

    It's highly unlikely that the physical connections are wrong, if they were the card wouldn't initialise and report as data and clock are common. From the message "SD Card mounted successfully" I would guess the card has got as far as data transfer mode, so it's at the stage of transfering blocks.

    Have you tried another card in it? The reason being is that there's a difference in the way Standard capacity (SC) and high capacity(HC) report their size. I thought this might explain the 2022246978K size of an 8GB card but it doesn't work out. However it's worth trying because if the driver isn't that well written it might be getting as far as block operations but failing for some reason. This doesn't quite explain why you can read the filenames though.

    I'd try another card in it. The 8GB card will be a HC card as it exceeds 4GB. Try a SC card in it. SC only supported 2^32 bytes, whereas HC supports 2^32 blocks but they share bit space in the downloaded CSD register. If you've got an older 128MB or 256MB card knocking about that would be ideal.

  • Thank you for the suggestion. I cannot find any cards smaller than 2GB right now, but the output is very similar. This was from an old camera so there is a DCIM directory, but I tried it anyway:

    The error is the same:

    SD Card mounted successfully

    Initialising disk 0. Status = 0

    D---- 2015/04/27 13:35 0 DCIM
    ----A 2007/06/05 11:38 0 TEST2.TXT

    1 File(s), 0 bytes total
    1 Dir(s), 1787362260K bytes free

    D---- 2015/04/27 13:35 0 DCIM

    0 File(s), 0 bytes total
    1 Dir(s), 1787362260K bytes free

    Opened SD card
    Error writing to file: FR_DISK_ERR
    File size is 0

    I'll see if I can locate a smaller card and will try that too.
  • Also googling "FR_DISK_ERR" throws up some interesting stuff on the matter. Not sure which applies.