2011-08-01 21:51:41 +00:00
|
|
|
#include <sysdefs.h>
|
2011-07-12 22:22:18 +00:00
|
|
|
#include <ff.h>
|
2011-12-23 00:07:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "at45db041d.h"
|
|
|
|
#include "lcd/print.h"
|
|
|
|
#include "usb/usbmsc.h"
|
2011-07-12 22:22:18 +00:00
|
|
|
|
2011-07-26 22:57:36 +00:00
|
|
|
FATFS FatFs; /* File system object for logical drive */
|
|
|
|
|
2011-07-12 22:22:18 +00:00
|
|
|
const TCHAR *rcstrings =
|
|
|
|
_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
|
|
|
|
_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
|
|
|
|
_T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
|
|
|
|
_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");
|
|
|
|
|
|
|
|
const char* f_get_rc_string (FRESULT rc) {
|
|
|
|
FRESULT i;
|
|
|
|
const char *p=rcstrings;
|
|
|
|
|
|
|
|
for (i = 0; i != rc && *p; i++) {
|
|
|
|
while(*p++) ;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-12-18 21:25:10 +00:00
|
|
|
const uint8_t init1[] = {0xeb, 0xfe, 0x90, 0x4d, 0x53, 0x44, 0x4f, 0x53,
|
|
|
|
0x35, 0x2e, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
|
|
|
|
0x01, 0x00, 0x02, 0x00, 0x04, 0xf0, 0x04, 0x00,
|
|
|
|
0x3f, 0x00, 0xff};
|
|
|
|
|
|
|
|
const uint8_t init2[] = {0x80, 0x00, 0x29, 0x37,
|
|
|
|
0x4b, 0x55, 0x33, 0x4e, 0x4f, 0x20, 0x4e, 0x41,
|
|
|
|
0x4d, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41,
|
|
|
|
0x54, 0x20, 0x20, 0x20, 0x20, 0x20};
|
|
|
|
|
2011-12-23 00:07:48 +00:00
|
|
|
inline void format_formatDF(void) {
|
2011-12-18 21:25:10 +00:00
|
|
|
int i;
|
2011-12-23 00:07:48 +00:00
|
|
|
BYTE buf[512];
|
2011-12-18 21:25:10 +00:00
|
|
|
|
|
|
|
memset(buf, 0, 512);
|
2011-12-22 14:17:00 +00:00
|
|
|
for(i=0; i<100; i++) dataflash_write(buf, i, 1);
|
2011-12-19 19:58:08 +00:00
|
|
|
|
2011-12-18 21:25:10 +00:00
|
|
|
memcpy(buf, init1, sizeof(init1));
|
|
|
|
memcpy(buf+0x24, init2, sizeof(init2));
|
|
|
|
buf[510] = 0x55;
|
|
|
|
buf[511] = 0xaa;
|
|
|
|
dataflash_write(buf, 0, 1);
|
|
|
|
|
|
|
|
for(i=0; i< 512; i++) buf[i] = 0x00;
|
|
|
|
buf[0] = 0xf0;
|
|
|
|
buf[1] = 0xff;
|
|
|
|
buf[2] = 0xff;
|
|
|
|
dataflash_write(buf, 1, 1);
|
|
|
|
}
|
|
|
|
|
2011-07-26 22:57:36 +00:00
|
|
|
|
|
|
|
void fsInit(){
|
2011-12-17 21:25:21 +00:00
|
|
|
FRESULT i;
|
|
|
|
DIR dir; /* Directory object */
|
2011-07-26 22:57:36 +00:00
|
|
|
f_mount(0, &FatFs);
|
2011-12-17 21:25:21 +00:00
|
|
|
|
|
|
|
i = f_opendir(&dir, "0:");
|
|
|
|
|
|
|
|
if( i == FR_NO_FILESYSTEM ){
|
2011-12-17 21:39:01 +00:00
|
|
|
lcdPrintln("filesystm b0rk");
|
2011-12-17 21:25:21 +00:00
|
|
|
lcdPrintln("Formating...");
|
|
|
|
lcdRefresh();
|
|
|
|
format_formatDF();
|
2011-12-17 21:39:01 +00:00
|
|
|
lcdPrintln("Done.");
|
|
|
|
lcdPrintln("Now copy files");
|
|
|
|
lcdPrintln("to drive. See");
|
|
|
|
lcdPrintln("");
|
|
|
|
lcdPrintln("r0ket.de/init");
|
2011-12-17 21:25:21 +00:00
|
|
|
lcdRefresh();
|
2011-12-17 21:39:01 +00:00
|
|
|
usbMSCInit();
|
2011-12-17 21:25:21 +00:00
|
|
|
while(1);
|
|
|
|
}
|
2011-07-26 22:57:36 +00:00
|
|
|
};
|
2011-07-31 23:12:58 +00:00
|
|
|
|
2011-08-01 21:51:41 +00:00
|
|
|
void fsReInit(){
|
|
|
|
f_mount(0, NULL);
|
|
|
|
f_mount(0, &FatFs);
|
|
|
|
};
|
|
|
|
|
2011-07-31 23:12:58 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2011-08-21 17:10:08 +00:00
|
|
|
res = f_read(&file, data, len, &readbytes);
|
2011-07-31 23:12:58 +00:00
|
|
|
if(res){
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2011-07-31 23:55:04 +00:00
|
|
|
f_close(&file);
|
|
|
|
|
2011-07-31 23:12:58 +00:00
|
|
|
return readbytes;
|
|
|
|
};
|
|
|
|
|
2011-08-21 17:10:08 +00:00
|
|
|
int readTextFile(char * filename, char * data, int len){
|
2012-04-17 22:43:39 +00:00
|
|
|
int readbytes;
|
2011-08-21 17:10:08 +00:00
|
|
|
|
|
|
|
readbytes=readFile(filename,data,len-1);
|
|
|
|
if(len>=0)
|
|
|
|
data[readbytes]=0;
|
|
|
|
return readbytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-31 23:12:58 +00:00
|
|
|
int writeFile(char * filename, char * data, int len){
|
|
|
|
FIL file;
|
|
|
|
UINT writebytes;
|
|
|
|
int res;
|
|
|
|
|
2011-08-02 21:54:07 +00:00
|
|
|
res=f_open(&file, filename, FA_CREATE_ALWAYS|FA_WRITE);
|
2011-07-31 23:12:58 +00:00
|
|
|
if(res){
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
res = f_write(&file, data, len, &writebytes);
|
|
|
|
if(res){
|
|
|
|
return -1;
|
|
|
|
};
|
2011-07-31 23:55:04 +00:00
|
|
|
f_close(&file);
|
2011-07-31 23:12:58 +00:00
|
|
|
|
|
|
|
return writebytes;
|
|
|
|
};
|
|
|
|
|