Toan Dang / Mbed 2 deprecated fatfs

Dependencies:   mbed

Revision:
0:f24ba3a89ec1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 30 10:12:16 2015 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+#include "diskio.h"
+#include "ff.h"
+Serial pc(USBTX, USBRX); // tx, rx
+Ticker flipper;
+FATFS FatFs;   /* Work area (file system object) for logical drive */
+void _10ms_task (void){
+    disk_timerproc();
+    } 
+    
+FRESULT FATFS_DriveSize(uint32_t* total, uint32_t* free) {
+    FATFS *fs;
+    DWORD fre_clust;
+    FRESULT res;
+
+    /* Get volume information and free clusters of drive */
+    res = f_getfree("", &fre_clust, &fs);
+    if (res != FR_OK) {
+        return res;
+    }
+
+    /* Get total sectors and free sectors */
+    *total = (fs->n_fatent - 2) * fs->csize / 2;
+    *free = fre_clust * fs->csize / 2;
+    
+    /* Return OK */
+    return FR_OK;
+}
+int main() {
+    FIL fil;       /* File object */
+    FRESULT fr;    /* FatFs return code */
+    char line[82]; /* Line buffer */
+    uint32_t total = 0,free = 0;
+    int res =0;
+    flipper.attach(&_10ms_task, 0.001); // the address of the function to be attached (flip) and the interval (2 seconds)
+    pc.printf("Hello World!\r\n");
+    
+    /* Register work area to the default drive */
+    fr =  f_mount(&FatFs,(TCHAR*) "0:", 1);
+    pc.printf("mount return %d\r\n", fr);
+    /* Open a text file */
+    fr = f_open(&fil, "message1.txt", FA_OPEN_ALWAYS | FA_WRITE);
+    pc.printf("open return %d \r\n", fr);
+    fr = f_sync(&fil);
+    FATFS_DriveSize(&total,&free);
+    pc.printf("total %d free %d \r\n", total, free);
+    f_lseek(&fil, f_size(&fil));
+    res = f_printf(&fil,"hello world\r\n");
+    if(res > 0){
+        pc.printf("write success \r\n");
+    } 
+    else{
+        pc.printf("write failed \r\n");  
+    }
+    fr = f_sync(&fil);
+    f_close(&fil);
+    
+    /* Register work area to the default drive */
+    f_mount(&FatFs,(TCHAR*) "0:", 1);
+
+    /* Open a text file */
+    fr = f_open(&fil, "message1.txt", FA_READ);
+    if (fr) return (int)fr;
+
+    /* Read all lines and display it */
+    while (f_gets(line, sizeof line, &fil))
+        pc.printf(line);
+
+    /* Close the file */
+    f_close(&fil);
+    while(1) {
+    }
+}
\ No newline at end of file