2021-04-18 09:41 PM
static void FS_FileOperations(void) {
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs uSD + FreeRTOS"; /* File write buffer */
/* Register the file system object to the FatFs module */
if (f_mount(&SDFatFs, (TCHAR const*) SDPath, 0) == FR_OK) {
/* check whether the FS has been already created */
if (isFsCreated == 0) {
if (f_mkfs(SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer))
!= FR_OK) {
printf("f_mkfs Failed!\r\n");
return;
}
isFsCreated = 1;
}
/* Create and Open a new text file object with write access */
if (f_open(&MyFile, "STM32H7.TXT", FA_CREATE_ALWAYS | FA_WRITE)
== FR_OK) {
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void*) &byteswritten);
if ((byteswritten > 0) && (res == FR_OK)) {
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
if (f_open(&MyFile, "STM32H7.TXT", FA_READ) == FR_OK) {
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext),
(void*) &bytesread);
if ((bytesread > 0) && (res == FR_OK)) {
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if ((bytesread == byteswritten)) {
/* Success of the demo: no error occurrence */
printf(
"Success of the demo: no error occurrence\r\n");
return;
}
}
}
}
}
}
/* Error */
printf("FS_FileOperations\r\n");
}