IJFW - IchigoJamのBASICプログラムをメモリカード(MMCまたは互換カード)に保存したり読み出したりできるプログラム。メモリカードにファームウェアのファイルを置くだけで、電源ON時に自動的に書き換える機能も搭載(一応こちらがメイン)。LPC1114FN28専用。

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FatfsIjfwConfigurable.cpp Source File

FatfsIjfwConfigurable.cpp

00001 #include "mbed.h"
00002 #include "ff.h"
00003 #include "FatfsIjfwConfigurable.h"
00004 
00005 FatfsIjfwConfigurable::FatfsIjfwConfigurable(SPI* _spi, DigitalOut* _cs) : FatfsIJFW(_spi, _cs) {
00006     initConfigItem();
00007 }
00008 
00009 
00010 void FatfsIjfwConfigurable::initConfigItem() {
00011     strncpy(configitem[0].itemName, "FirmUpdate", ITEM_NAME_LENGTH);
00012     strncpy(configitem[0].itemValue, "1", ITEM_VAL_LENGTH);
00013     strncpy(configitem[1].itemName, "FirmFileDelete", ITEM_NAME_LENGTH);
00014     strncpy(configitem[1].itemValue, "1", ITEM_VAL_LENGTH);
00015     strncpy(configitem[2].itemName, "BasDirectory", ITEM_NAME_LENGTH);
00016     strncpy(configitem[2].itemValue, "", ITEM_VAL_LENGTH);
00017 }
00018 
00019 
00020 int FatfsIjfwConfigurable::setConfigValue(const char* setName, const char* setValue) {
00021     for (int i = 0; i < CONFIG_ITEM_NUM; i++) {
00022         if (strncmp(configitem[i].itemName, setName, ITEM_NAME_LENGTH) == 0) {
00023             strncpy(configitem[i].itemValue, setValue, ITEM_VAL_LENGTH);
00024             configitem[i].itemValue[ITEM_VAL_LENGTH] = '\0';    // if setValue length is equal to ITEM_VAL_LENGTH
00025             return 1;
00026         }
00027     }
00028     
00029     return 0;
00030 }
00031 
00032 
00033 int FatfsIjfwConfigurable::getConfigValue(const char* setName, char* buf) {
00034     for (int i = 0; i < CONFIG_ITEM_NUM; i++) {
00035         if (strncmp(configitem[i].itemName, setName, ITEM_NAME_LENGTH) == 0) {
00036             strncpy(buf, configitem[i].itemValue, ITEM_VAL_LENGTH);
00037             buf[ITEM_VAL_LENGTH] = '\0';    // if itemValue length is equal to ITEM_VAL_LENGTH
00038             return 1;
00039         }
00040     }
00041 
00042     return 0;
00043 }
00044 
00045 
00046 int FatfsIjfwConfigurable::readLine(char* buf, int length) {
00047     if (f_gets(buf, (UINT)length, &file) != NULL) {
00048         return 1;
00049     }
00050     return 0;
00051 }
00052 
00053 
00054 int FatfsIjfwConfigurable::readConfigFile(const char* configFiie) {
00055     mount();
00056     if (open(configFiie, MODE_RO) != FR_OK) {
00057         return -1;
00058     }
00059 
00060     int retval = 0;
00061     char buf[42], tempbuf[64];
00062 
00063     while(readLine(tempbuf, sizeof(tempbuf))) {
00064         // delete space and tab
00065         for (int i = 0, j = 0; i < sizeof(tempbuf); i++) {
00066             if (tempbuf[i] != ' ' && tempbuf[i] != '\t') {
00067                 if (j < sizeof(buf)) {
00068                     buf[j] = tempbuf[i];
00069                     j++;
00070                 }
00071             }
00072             if (tempbuf[i] == 0x0d || tempbuf[i] == 0x0a) {
00073                 buf[j] = '\0';
00074                 break;
00075             }
00076         }
00077 
00078         // comment
00079         if (buf[0] == '#') {
00080             continue;
00081         }
00082 
00083         // position of separator
00084         int i, pos_sep;
00085         for (i = 0; i < sizeof(buf); i++) {
00086             if (buf[i] == '=') {
00087                 pos_sep = i;
00088                 buf[i] = '\0';
00089             } else if (buf[i] == '\0') {
00090                 break;
00091             }
00092         }
00093 
00094         // check length
00095         if (pos_sep > ITEM_NAME_LENGTH - 1) {
00096             retval = -2;
00097         }
00098         if ( (i-1) - (pos_sep+1) > ITEM_VAL_LENGTH - 1) {
00099             retval = -3;
00100         }
00101 
00102         // set value
00103         setConfigValue(buf, &buf[pos_sep+1]);
00104     }
00105 
00106     close();
00107     return retval;
00108 }
00109 
00110 
00111 int FatfsIjfwConfigurable::openBas(const char* name, const FileMode mode) {
00112     char buf[ITEM_VAL_LENGTH+1];
00113     getConfigValue("BasDirectory", buf);
00114 
00115     if (strlen(buf) > 0) {
00116         mkdir(buf);
00117         chdir("/");
00118         int res = chdir(buf);
00119         if (res != FR_OK) {
00120             return res;
00121         }
00122     }
00123 
00124     return open(name, mode);
00125 }
00126 
00127 
00128 int FatfsIjfwConfigurable::chdir(const char* path) {
00129     FRESULT res = f_chdir(path);
00130     return (int)res;
00131 }
00132 
00133 
00134 int FatfsIjfwConfigurable::checkFirmFile(const char* binfile) {
00135     char buf[ITEM_VAL_LENGTH+1];
00136     getConfigValue("FirmUpdate", buf);
00137 
00138     if (strncmp(buf, "0", 1) == 0) {
00139         return 0;
00140     }
00141 
00142     mount();
00143     if (open(binfile, MODE_RO) == FR_OK) {
00144         close();
00145         return 1;
00146     }
00147 
00148     return 0;
00149 }
00150 
00151 
00152 int FatfsIjfwConfigurable::deleteFirmFile(const char* binfile) {
00153     char buf[ITEM_VAL_LENGTH+1];
00154     getConfigValue("FirmFileDelete", buf);
00155 
00156     if (strncmp(buf, "1", 1) == 0) {
00157         remove(binfile);
00158         return 1;
00159     }
00160 
00161     return 0;
00162 }