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

Dependencies:   mbed

参考URL http://www.cyberchabudai.org/index.php/entry?tag=IJFW

Committer:
oks486
Date:
Sun Aug 21 07:51:01 2016 +0000
Revision:
2:daf6c4719496
Parent:
1:11f73f269fdc
Modified I2c2mem for "FILES" command

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oks486 1:11f73f269fdc 1 #include "mbed.h"
oks486 1:11f73f269fdc 2 #include "ff.h"
oks486 1:11f73f269fdc 3 #include "FatfsIjfwConfigurable.h"
oks486 1:11f73f269fdc 4
oks486 1:11f73f269fdc 5 FatfsIjfwConfigurable::FatfsIjfwConfigurable(SPI* _spi, DigitalOut* _cs) : FatfsIJFW(_spi, _cs) {
oks486 1:11f73f269fdc 6 initConfigItem();
oks486 1:11f73f269fdc 7 }
oks486 1:11f73f269fdc 8
oks486 1:11f73f269fdc 9
oks486 1:11f73f269fdc 10 void FatfsIjfwConfigurable::initConfigItem() {
oks486 1:11f73f269fdc 11 strncpy(configitem[0].itemName, "FirmUpdate", ITEM_NAME_LENGTH);
oks486 1:11f73f269fdc 12 strncpy(configitem[0].itemValue, "1", ITEM_VAL_LENGTH);
oks486 1:11f73f269fdc 13 strncpy(configitem[1].itemName, "FirmFileDelete", ITEM_NAME_LENGTH);
oks486 1:11f73f269fdc 14 strncpy(configitem[1].itemValue, "1", ITEM_VAL_LENGTH);
oks486 1:11f73f269fdc 15 strncpy(configitem[2].itemName, "BasDirectory", ITEM_NAME_LENGTH);
oks486 1:11f73f269fdc 16 strncpy(configitem[2].itemValue, "", ITEM_VAL_LENGTH);
oks486 1:11f73f269fdc 17 }
oks486 1:11f73f269fdc 18
oks486 1:11f73f269fdc 19
oks486 1:11f73f269fdc 20 int FatfsIjfwConfigurable::setConfigValue(const char* setName, const char* setValue) {
oks486 1:11f73f269fdc 21 for (int i = 0; i < CONFIG_ITEM_NUM; i++) {
oks486 1:11f73f269fdc 22 if (strncmp(configitem[i].itemName, setName, ITEM_NAME_LENGTH) == 0) {
oks486 1:11f73f269fdc 23 strncpy(configitem[i].itemValue, setValue, ITEM_VAL_LENGTH);
oks486 1:11f73f269fdc 24 configitem[i].itemValue[ITEM_VAL_LENGTH] = '\0'; // if setValue length is equal to ITEM_VAL_LENGTH
oks486 1:11f73f269fdc 25 return 1;
oks486 1:11f73f269fdc 26 }
oks486 1:11f73f269fdc 27 }
oks486 1:11f73f269fdc 28
oks486 1:11f73f269fdc 29 return 0;
oks486 1:11f73f269fdc 30 }
oks486 1:11f73f269fdc 31
oks486 1:11f73f269fdc 32
oks486 1:11f73f269fdc 33 int FatfsIjfwConfigurable::getConfigValue(const char* setName, char* buf) {
oks486 1:11f73f269fdc 34 for (int i = 0; i < CONFIG_ITEM_NUM; i++) {
oks486 1:11f73f269fdc 35 if (strncmp(configitem[i].itemName, setName, ITEM_NAME_LENGTH) == 0) {
oks486 1:11f73f269fdc 36 strncpy(buf, configitem[i].itemValue, ITEM_VAL_LENGTH);
oks486 1:11f73f269fdc 37 buf[ITEM_VAL_LENGTH] = '\0'; // if itemValue length is equal to ITEM_VAL_LENGTH
oks486 1:11f73f269fdc 38 return 1;
oks486 1:11f73f269fdc 39 }
oks486 1:11f73f269fdc 40 }
oks486 1:11f73f269fdc 41
oks486 1:11f73f269fdc 42 return 0;
oks486 1:11f73f269fdc 43 }
oks486 1:11f73f269fdc 44
oks486 1:11f73f269fdc 45
oks486 1:11f73f269fdc 46 int FatfsIjfwConfigurable::readLine(char* buf, int length) {
oks486 1:11f73f269fdc 47 if (f_gets(buf, (UINT)length, &file) != NULL) {
oks486 1:11f73f269fdc 48 return 1;
oks486 1:11f73f269fdc 49 }
oks486 1:11f73f269fdc 50 return 0;
oks486 1:11f73f269fdc 51 }
oks486 1:11f73f269fdc 52
oks486 1:11f73f269fdc 53
oks486 1:11f73f269fdc 54 int FatfsIjfwConfigurable::readConfigFile(const char* configFiie) {
oks486 1:11f73f269fdc 55 mount();
oks486 1:11f73f269fdc 56 if (open(configFiie, MODE_RO) != FR_OK) {
oks486 1:11f73f269fdc 57 return -1;
oks486 1:11f73f269fdc 58 }
oks486 1:11f73f269fdc 59
oks486 1:11f73f269fdc 60 int retval = 0;
oks486 1:11f73f269fdc 61 char buf[42], tempbuf[64];
oks486 1:11f73f269fdc 62
oks486 1:11f73f269fdc 63 while(readLine(tempbuf, sizeof(tempbuf))) {
oks486 1:11f73f269fdc 64 // delete space and tab
oks486 1:11f73f269fdc 65 for (int i = 0, j = 0; i < sizeof(tempbuf); i++) {
oks486 1:11f73f269fdc 66 if (tempbuf[i] != ' ' && tempbuf[i] != '\t') {
oks486 1:11f73f269fdc 67 if (j < sizeof(buf)) {
oks486 1:11f73f269fdc 68 buf[j] = tempbuf[i];
oks486 1:11f73f269fdc 69 j++;
oks486 1:11f73f269fdc 70 }
oks486 1:11f73f269fdc 71 }
oks486 1:11f73f269fdc 72 if (tempbuf[i] == 0x0d || tempbuf[i] == 0x0a) {
oks486 1:11f73f269fdc 73 buf[j] = '\0';
oks486 1:11f73f269fdc 74 break;
oks486 1:11f73f269fdc 75 }
oks486 1:11f73f269fdc 76 }
oks486 1:11f73f269fdc 77
oks486 1:11f73f269fdc 78 // comment
oks486 1:11f73f269fdc 79 if (buf[0] == '#') {
oks486 1:11f73f269fdc 80 continue;
oks486 1:11f73f269fdc 81 }
oks486 1:11f73f269fdc 82
oks486 1:11f73f269fdc 83 // position of separator
oks486 1:11f73f269fdc 84 int i, pos_sep;
oks486 1:11f73f269fdc 85 for (i = 0; i < sizeof(buf); i++) {
oks486 1:11f73f269fdc 86 if (buf[i] == '=') {
oks486 1:11f73f269fdc 87 pos_sep = i;
oks486 1:11f73f269fdc 88 buf[i] = '\0';
oks486 1:11f73f269fdc 89 } else if (buf[i] == '\0') {
oks486 1:11f73f269fdc 90 break;
oks486 1:11f73f269fdc 91 }
oks486 1:11f73f269fdc 92 }
oks486 1:11f73f269fdc 93
oks486 1:11f73f269fdc 94 // check length
oks486 1:11f73f269fdc 95 if (pos_sep > ITEM_NAME_LENGTH - 1) {
oks486 1:11f73f269fdc 96 retval = -2;
oks486 1:11f73f269fdc 97 }
oks486 1:11f73f269fdc 98 if ( (i-1) - (pos_sep+1) > ITEM_VAL_LENGTH - 1) {
oks486 1:11f73f269fdc 99 retval = -3;
oks486 1:11f73f269fdc 100 }
oks486 1:11f73f269fdc 101
oks486 1:11f73f269fdc 102 // set value
oks486 1:11f73f269fdc 103 setConfigValue(buf, &buf[pos_sep+1]);
oks486 1:11f73f269fdc 104 }
oks486 1:11f73f269fdc 105
oks486 1:11f73f269fdc 106 close();
oks486 1:11f73f269fdc 107 return retval;
oks486 1:11f73f269fdc 108 }
oks486 1:11f73f269fdc 109
oks486 1:11f73f269fdc 110
oks486 1:11f73f269fdc 111 int FatfsIjfwConfigurable::openBas(const char* name, const FileMode mode) {
oks486 1:11f73f269fdc 112 char buf[ITEM_VAL_LENGTH+1];
oks486 1:11f73f269fdc 113 getConfigValue("BasDirectory", buf);
oks486 1:11f73f269fdc 114
oks486 1:11f73f269fdc 115 if (strlen(buf) > 0) {
oks486 1:11f73f269fdc 116 mkdir(buf);
oks486 1:11f73f269fdc 117 chdir("/");
oks486 1:11f73f269fdc 118 int res = chdir(buf);
oks486 1:11f73f269fdc 119 if (res != FR_OK) {
oks486 1:11f73f269fdc 120 return res;
oks486 1:11f73f269fdc 121 }
oks486 1:11f73f269fdc 122 }
oks486 1:11f73f269fdc 123
oks486 1:11f73f269fdc 124 return open(name, mode);
oks486 1:11f73f269fdc 125 }
oks486 1:11f73f269fdc 126
oks486 1:11f73f269fdc 127
oks486 1:11f73f269fdc 128 int FatfsIjfwConfigurable::chdir(const char* path) {
oks486 1:11f73f269fdc 129 FRESULT res = f_chdir(path);
oks486 1:11f73f269fdc 130 return (int)res;
oks486 1:11f73f269fdc 131 }
oks486 1:11f73f269fdc 132
oks486 1:11f73f269fdc 133
oks486 1:11f73f269fdc 134 int FatfsIjfwConfigurable::checkFirmFile(const char* binfile) {
oks486 1:11f73f269fdc 135 char buf[ITEM_VAL_LENGTH+1];
oks486 1:11f73f269fdc 136 getConfigValue("FirmUpdate", buf);
oks486 1:11f73f269fdc 137
oks486 1:11f73f269fdc 138 if (strncmp(buf, "0", 1) == 0) {
oks486 1:11f73f269fdc 139 return 0;
oks486 1:11f73f269fdc 140 }
oks486 1:11f73f269fdc 141
oks486 1:11f73f269fdc 142 mount();
oks486 1:11f73f269fdc 143 if (open(binfile, MODE_RO) == FR_OK) {
oks486 1:11f73f269fdc 144 close();
oks486 1:11f73f269fdc 145 return 1;
oks486 1:11f73f269fdc 146 }
oks486 1:11f73f269fdc 147
oks486 1:11f73f269fdc 148 return 0;
oks486 1:11f73f269fdc 149 }
oks486 1:11f73f269fdc 150
oks486 1:11f73f269fdc 151
oks486 1:11f73f269fdc 152 int FatfsIjfwConfigurable::deleteFirmFile(const char* binfile) {
oks486 1:11f73f269fdc 153 char buf[ITEM_VAL_LENGTH+1];
oks486 1:11f73f269fdc 154 getConfigValue("FirmFileDelete", buf);
oks486 1:11f73f269fdc 155
oks486 1:11f73f269fdc 156 if (strncmp(buf, "1", 1) == 0) {
oks486 1:11f73f269fdc 157 remove(binfile);
oks486 1:11f73f269fdc 158 return 1;
oks486 1:11f73f269fdc 159 }
oks486 1:11f73f269fdc 160
oks486 1:11f73f269fdc 161 return 0;
oks486 1:11f73f269fdc 162 }