CARME-M4 BSP  V1.5
sdcard.c

Explains how to read and write from/to sdcard.

/*
* This example shows how to read and write a file on the sdcard.
*/
#include <stm32f4xx.h>
#include <carme.h>
#include <rtc.h>
#include <ff.h>
FATFS main_fs;
int main(void) {
FIL f;
char buff[20];
UINT totalwrite = 0, totalread = 0, tmpwrite, tmpread;
UINT size;
/* initialize the rtc */
/* mount the sdcard */
if (f_mount(&main_fs, "0:", 1) != FR_OK) {
while (1);
}
/* open the read.txt file to get the size */
if (!f_open(&f, "read.txt", FA_OPEN_ALWAYS | FA_READ)) {
/* get the size of the file */
size = f_size(&f);
f_close(&f);
}
while (1) {
/* open the read.txt file to read the next string */
if (!f_open(&f, "read.txt", FA_OPEN_ALWAYS | FA_READ)) {
/* set the cursor next position to read */
f_lseek(&f, totalread);
f_read(&f, buff, sizeof(buff), &tmpread);
f_close(&f);
totalread += tmpread;
}
/* open the write.txt file to write this string */
if (!f_open(&f, "write.txt", FA_OPEN_ALWAYS | FA_WRITE)) {
/* set the cursor next position to read */
f_lseek(&f, f_size(&f));
f_write(&f, buff, tmpread, &tmpwrite);
f_close(&f);
totalwrite += tmpwrite;
}
if (!tmpwrite) {
/* there are no bytes written, so there is a error */
break;
}
if (totalwrite == size) {
/* there is the full file copied */
break;
}
}
for (;;) {
}
return 0;
}