From 62992863489a42a21a5af965a7b9923ac7ef0941 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 1 Aug 2011 01:12:58 +0200 Subject: [PATCH] Two utility functions to read/write small files --- firmware/filesystem/ff.h | 2 ++ firmware/filesystem/util.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/firmware/filesystem/ff.h b/firmware/filesystem/ff.h index 352ed5c..611a703 100644 --- a/firmware/filesystem/ff.h +++ b/firmware/filesystem/ff.h @@ -332,6 +332,8 @@ int ff_del_syncobj (_SYNC_t); /* Delete a sync object */ #define FILENAMELEN 13 // 8+1+3+1 const char* f_get_rc_string (FRESULT rc); void fsInit(); +int readFile(char * filename, char * data, int len); +int writeFile(char * filename, char * data, int len); #ifdef __cplusplus } diff --git a/firmware/filesystem/util.c b/firmware/filesystem/util.c index 7e635ad..d5cc5d0 100644 --- a/firmware/filesystem/util.c +++ b/firmware/filesystem/util.c @@ -22,3 +22,41 @@ const char* f_get_rc_string (FRESULT rc) { void fsInit(){ f_mount(0, &FatFs); }; + +int readFile(char * filename, char * data, int len){ + FIL file; + UINT readbytes; + int res; + + res=f_open(&file, filename, FA_OPEN_EXISTING|FA_READ); + if(res){ + return -1; + }; + + res = f_read(&file, data, len-1, &readbytes); + if(res){ + return -1; + }; + + data[readbytes]=0; + return readbytes; +}; + +int writeFile(char * filename, char * data, int len){ + FIL file; + UINT writebytes; + int res; + + res=f_open(&file, filename, FA_OPEN_ALWAYS|FA_WRITE); + if(res){ + return -1; + }; + + res = f_write(&file, data, len, &writebytes); + if(res){ + return -1; + }; + + return writebytes; +}; +