I can't seem to get this working on my 129x.
I've written a number of wrapper functions to simplify open/read/write operations on the SD card.
void sdcardConfigure() {
// Configure the device pins
// Enable necessary GPIO peripherals
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ);
// PF0/PF4-5/PH4/PQ0-2 are used for the SPI flash (on-board and SD card).
// PH4 selects the SD card and PQ1 selects the on-board SPI flash.
ROM_GPIOPinConfigure(GPIO_PF0_SSI3XDAT1);
ROM_GPIOPinConfigure(GPIO_PF4_SSI3XDAT2);
ROM_GPIOPinConfigure(GPIO_PF5_SSI3XDAT3);
ROM_GPIOPinConfigure(GPIO_PQ0_SSI3CLK);
ROM_GPIOPinConfigure(GPIO_PQ2_SSI3XDAT0);
ROM_GPIOPinTypeSSI(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 | GPIO_PIN_5);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_4);
ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_4, GPIO_PIN_4);
ROM_GPIOPinTypeSSI(GPIO_PORTQ_BASE, GPIO_PIN_0 | GPIO_PIN_2);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTQ_BASE, GPIO_PIN_1);
ROM_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_1, GPIO_PIN_1);
}
uint8_t sdcardFileOpenWrite(char* fileName) {
FRESULT fr;
fr = f_open(&g_sFileObject, fileName, FA_WRITE | FA_OPEN_ALWAYS);
if(fr != FR_OK) {
sdcardLogError(SDCARD_ERROR_FILE,(char *)StringFromFResult(fr));
return 1; // Error opening file
}
// No errors
return 0;
}
uint8_t sdcardFileAppendLine(char* line) {
FRESULT fr;
// Move to the end of the file
fr = f_lseek(&g_sFileObject, f_size(&g_sFileObject));
// if error here return error number
if (fr != FR_OK) {
sdcardFileClose();
sdcardLogError(SDCARD_ERROR_FILE,(char *)StringFromFResult(fr));
return 1; // Seek failure
}
// Variable to hold the number of bytes written
int16_t bw;
// Write the line to the file
bw = f_puts(line, &g_sFileObject);
// if error here return error number
if (bw == -1) {
sdcardFileClose();
sdcardLogError(SDCARD_ERROR_FILE,(char *)StringFromFResult(fr));
return 2; // Write failed, end of file error
}
// No errors
return 0;
}
uint8_t sdcardFileClose() {
FRESULT fr;
fr = f_close(&g_sFileObject);
if(fr != FR_OK) {
sdcardLogError(SDCARD_ERROR_FILE,(char *)StringFromFResult(fr));
return 1; // Close error occurred
}
// No errors
return 0;
}
uint8_t sdcardMount(void) {
FRESULT fr;
fr = f_mount(0, &g_sFatFs);
if(fr != FR_OK) {
sdcardLogError(SDCARD_ERROR_FILE,(char *)StringFromFResult(fr));
return 1; // Mount error occurred
}
return 0;
}
int main(void) {
uint8_t x
sdcardConfigure();
x = sdcardMount();
UARTprintf("mount: %d\n", x);
x = sdcardFileOpenWrite("/myfile.txt");
UARTprintf("open: %d\n",x);
char line[80] = "writing some text";
x = sdcardAppendLine(line);
UARTprintf("append: %d\n", x);
x = sdcardFileClose();
UARTprintf("close: %d\n", x);
}
For some reason sdcardOpen returns 0 (FR_OK) and sdcardAppendLine returns 2 (EOF error). When I look at the contents of the SD Card, the file was never created. Why would I get an FR_OK from f_open if the file wasn't created?
Note: sdcardLogError() is currently an empty function, shouldn't have any affect