PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

394 satır
11KB

  1. /* SerialFlash Library - for filesystem-like access to SPI Serial Flash memory
  2. * https://github.com/PaulStoffregen/SerialFlash
  3. * Copyright (C) 2015, Paul Stoffregen, paul@pjrc.com
  4. *
  5. * Development of this library was funded by PJRC.COM, LLC by sales of Teensy.
  6. * Please support PJRC's efforts to develop open source software by purchasing
  7. * Teensy or other genuine PJRC products.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice, development funding notice, and this permission
  17. * notice shall be included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "SerialFlash.h"
  28. /* On-chip SerialFlash file allocation data structures:
  29. uint32_t signature = 0xFA96554C;
  30. uint16_t maxfiles
  31. uint16_t stringssize // div by 4
  32. uint16_t hashes[maxfiles]
  33. struct {
  34. uint32_t file_begin
  35. uint32_t file_length
  36. uint16_t string_index // div4
  37. } fileinfo[maxfiles]
  38. char strings[stringssize]
  39. A 32 bit signature is stored at the beginning of the flash memory.
  40. If 0xFFFFFFFF is seen, the entire chip should be assumed blank.
  41. If any value other than 0xFA96554C is found, a different data format
  42. is stored. This could should refuse to access the flash.
  43. The next 4 bytes store number of files and size of the strings
  44. section, which allow the position of every other item to be found.
  45. The string section size is the 16 bit integer times 4, which allows
  46. up to 262140 bytes for string data.
  47. An array of 16 bit filename hashes allows for quick linear search
  48. for potentially matching filenames. A hash value of 0xFFFF indicates
  49. no file is allocated for the remainder of the array.
  50. Following the hashes, and array of 10 byte structs give the location
  51. and length of the file's actual data, and the offset of its filename
  52. in the strings section.
  53. Strings are null terminated. The remainder of the chip is file data.
  54. */
  55. #define DEFAULT_MAXFILES 600
  56. #define DEFAULT_STRINGS_SIZE 25560
  57. static uint32_t check_signature(void)
  58. {
  59. uint32_t sig[2];
  60. SerialFlash.read(0, sig, 8);
  61. //Serial.printf("sig: %08X %08X\n", sig[0], sig[1]);
  62. if (sig[0] == 0xFA96554C) return sig[1];
  63. if (sig[0] == 0xFFFFFFFF) {
  64. sig[0] = 0xFA96554C;
  65. sig[1] = ((uint32_t)(DEFAULT_STRINGS_SIZE/4) << 16) | DEFAULT_MAXFILES;
  66. SerialFlash.write(0, sig, 8);
  67. while (!SerialFlash.ready()) ; // TODO: timeout
  68. SerialFlash.read(0, sig, 8);
  69. if (sig[0] == 0xFA96554C) return sig[1];
  70. }
  71. return 0;
  72. }
  73. static uint16_t filename_hash(const char *filename)
  74. {
  75. // http://isthe.com/chongo/tech/comp/fnv/
  76. uint32_t hash = 2166136261;
  77. const char *p;
  78. for (p=filename; *p; p++) {
  79. hash ^= *p;
  80. hash *= 16777619;
  81. }
  82. hash = (hash % (uint32_t)0xFFFE) + 1; // all values except 0000 & FFFF
  83. return hash;
  84. }
  85. static bool filename_compare(const char *filename, uint32_t straddr)
  86. {
  87. unsigned int i;
  88. const char *p;
  89. char buf[16];
  90. p = filename;
  91. while (1) {
  92. SerialFlash.read(straddr, buf, sizeof(buf));
  93. straddr += sizeof(buf);
  94. for (i=0; i < sizeof(buf); i++) {
  95. if (*p++ != buf[i]) return false;
  96. if (buf[i] == 0) return true;
  97. }
  98. }
  99. }
  100. #if 0
  101. void pbuf(const void *buf, uint32_t len)
  102. {
  103. const uint8_t *p = (const uint8_t *)buf;
  104. do {
  105. Serial.printf("%02X ", *p++);
  106. } while (--len > 0);
  107. Serial.println();
  108. }
  109. #endif
  110. SerialFlashFile SerialFlashChip::open(const char *filename)
  111. {
  112. uint32_t maxfiles, straddr;
  113. uint16_t hash, hashtable[8];
  114. uint32_t i, n, index=0;
  115. uint32_t buf[3];
  116. SerialFlashFile file;
  117. maxfiles = check_signature();
  118. //Serial.printf("sig: %08X\n", maxfiles);
  119. if (!maxfiles) return file;
  120. maxfiles &= 0xFFFF;
  121. hash = filename_hash(filename);
  122. //Serial.printf("hash %04X for \"%s\"\n", hash, filename);
  123. while (index < maxfiles) {
  124. n = 8;
  125. if (n > maxfiles - index) n = maxfiles - index;
  126. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  127. //Serial.printf(" read %u: ", 8 + index * 2);
  128. //pbuf(hashtable, n * 2);
  129. for (i=0; i < n; i++) {
  130. if (hashtable[i] == hash) {
  131. //Serial.printf(" hash match at index %u\n", index+i);
  132. buf[2] = 0;
  133. SerialFlash.read(8 + maxfiles * 2 + (index+i) * 10, buf, 10);
  134. //Serial.printf(" maxf=%d, index=%d, i=%d\n", maxfiles, index, i);
  135. //Serial.printf(" read %u: ", 8 + maxfiles * 2 + (index+i) * 10);
  136. //pbuf(buf, 10);
  137. straddr = 8 + maxfiles * 12 + buf[2] * 4;
  138. //Serial.printf(" straddr = %u\n", straddr);
  139. if (filename_compare(filename, straddr)) {
  140. //Serial.printf(" match!\n");
  141. //Serial.printf(" addr = %u\n", buf[0]);
  142. //Serial.printf(" len = %u\n", buf[1]);
  143. file.address = buf[0];
  144. file.length = buf[1];
  145. file.offset = 0;
  146. file.dirindex = index + i;
  147. return file;
  148. }
  149. } else if (hashtable[i] == 0xFFFF) {
  150. return file;
  151. }
  152. }
  153. index += n;
  154. }
  155. return file;
  156. }
  157. bool SerialFlashChip::exists(const char *filename)
  158. {
  159. SerialFlashFile file = open(filename);
  160. return (bool)file;
  161. }
  162. bool SerialFlashChip::remove(const char *filename)
  163. {
  164. SerialFlashFile file = open(filename);
  165. return remove(file);
  166. }
  167. bool SerialFlashChip::remove(SerialFlashFile &file)
  168. {
  169. // To "remove" a file, we simply zero its hash in the lookup
  170. // table, so it can't be found by open(). The space on the
  171. // flash memory is not freed.
  172. if (!file) return false;
  173. uint16_t hash;
  174. SerialFlash.read(8 + file.dirindex * 2, &hash, 2);
  175. //Serial.printf("remove hash %04X at %d index\n", hash, file.dirindex);
  176. hash ^= 0xFFFF; // write zeros to all ones
  177. SerialFlash.write(8 + file.dirindex * 2, &hash, 2);
  178. while (!SerialFlash.ready()) ; // wait... TODO: timeout
  179. SerialFlash.read(8 + file.dirindex * 2, &hash, 2);
  180. if (hash != 0) {
  181. //Serial.printf("remove failed, hash %04X\n", hash);
  182. return false;
  183. }
  184. file.address = 0;
  185. file.length = 0;
  186. return true;
  187. }
  188. static uint32_t find_first_unallocated_file_index(uint32_t maxfiles)
  189. {
  190. uint16_t hashtable[8];
  191. uint32_t i, n, index=0;
  192. do {
  193. n = 8;
  194. if (index + n > maxfiles) n = maxfiles - index;
  195. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  196. for (i=0; i < n; i++) {
  197. if (hashtable[i] == 0xFFFF) return index + i;
  198. }
  199. index += n;
  200. } while (index < maxfiles);
  201. return 0xFFFFFFFF;
  202. }
  203. static uint32_t string_length(uint32_t addr)
  204. {
  205. char buf[16];
  206. const char *p;
  207. uint32_t len=0;
  208. while (1) {
  209. SerialFlash.read(addr, buf, sizeof(buf));
  210. for (p=buf; p < buf + sizeof(buf); p++) {
  211. len++;
  212. if (*p == 0) return len;
  213. }
  214. addr += sizeof(buf);
  215. }
  216. }
  217. // uint32_t signature = 0xFA96554C;
  218. // uint16_t maxfiles
  219. // uint16_t stringssize // div by 4
  220. // uint16_t hashes[maxfiles]
  221. // struct {
  222. // uint32_t file_begin
  223. // uint32_t file_length
  224. // uint16_t string_index // div 4
  225. // } fileinfo[maxfiles]
  226. // char strings[stringssize]
  227. bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t align)
  228. {
  229. uint32_t maxfiles, stringsize;
  230. uint32_t index, buf[3];
  231. uint32_t address, straddr, len;
  232. SerialFlashFile file;
  233. // check if the file already exists
  234. if (exists(filename)) return false;
  235. // first, get the filesystem parameters
  236. maxfiles = check_signature();
  237. if (!maxfiles) return false;
  238. stringsize = (maxfiles & 0xFFFF0000) >> 14;
  239. maxfiles &= 0xFFFF;
  240. // find the first unused slot for this file
  241. index = find_first_unallocated_file_index(maxfiles);
  242. if (index >= maxfiles) return false;
  243. //Serial.printf("index = %u\n", index);
  244. // compute where to store the filename and actual data
  245. straddr = 8 + maxfiles * 12;
  246. if (index == 0) {
  247. address = straddr + stringsize;
  248. } else {
  249. buf[2] = 0;
  250. SerialFlash.read(8 + maxfiles * 2 + (index-1) * 10, buf, 10);
  251. address = buf[0] + buf[1];
  252. straddr += buf[2] * 4;
  253. straddr += string_length(straddr);
  254. straddr = (straddr + 3) & 0x0003FFFC;
  255. }
  256. //Serial.printf("straddr = %u\n", straddr);
  257. //Serial.printf("address = %u\n", address);
  258. //Serial.printf("length = %u\n", length);
  259. if (align > 0) {
  260. // for files aligned to sectors, adjust addr & len
  261. address += align - 1;
  262. address /= align;
  263. address *= align;
  264. //Serial.printf("align address = %u\n", address);
  265. length += align - 1;
  266. length /= align;
  267. length *= align;
  268. //Serial.printf("align length = %u\n", length);
  269. } else {
  270. // always align every file to a page boundary
  271. // for predictable write latency and to guarantee
  272. // write suspend for reading another file can't
  273. // conflict on the same page (2 files never share
  274. // a write page).
  275. address = (address + 255) & 0xFFFFFF00;
  276. }
  277. //Serial.printf("address = %u\n", address);
  278. // last check, if enough space exists...
  279. len = strlen(filename);
  280. // TODO: check for enough string space for filename
  281. uint8_t id[5];
  282. SerialFlash.readID(id);
  283. if (address + length > SerialFlash.capacity(id)) return false;
  284. SerialFlash.write(straddr, filename, len+1);
  285. buf[0] = address;
  286. buf[1] = length;
  287. buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
  288. SerialFlash.write(8 + maxfiles * 2 + index * 10, buf, 10);
  289. //Serial.printf(" write %u: ", 8 + maxfiles * 2 + index * 10);
  290. //pbuf(buf, 10);
  291. while (!SerialFlash.ready()) ; // TODO: timeout
  292. buf[0] = filename_hash(filename);
  293. //Serial.printf("hash = %04X\n", buf[0]);
  294. SerialFlash.write(8 + index * 2, buf, 2);
  295. while (!SerialFlash.ready()) ; // TODO: timeout
  296. return true;
  297. }
  298. bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesize)
  299. {
  300. uint32_t maxfiles, index, straddr;
  301. uint32_t i, n;
  302. uint32_t buf[2];
  303. uint16_t hash;
  304. char str[16], *p=filename;
  305. filename[0] = 0;
  306. maxfiles = check_signature();
  307. if (!maxfiles) return false;
  308. maxfiles &= 0xFFFF;
  309. index = dirindex;
  310. while (1) {
  311. if (index >= maxfiles) return false;
  312. //Serial.printf("readdir, index = %u\n", index);
  313. SerialFlash.read(8 + index * 2, &hash, 2);
  314. if (hash != 0) break;
  315. index++; // skip deleted entries
  316. }
  317. dirindex = index + 1;
  318. buf[1] = 0;
  319. SerialFlash.read(8 + 4 + maxfiles * 2 + index * 10, buf, 6);
  320. if (buf[0] == 0xFFFFFFFF) return false;
  321. filesize = buf[0];
  322. straddr = 8 + maxfiles * 12 + buf[1] * 4;
  323. //Serial.printf(" length = %u\n", buf[0]);
  324. //Serial.printf(" straddr = %u\n", straddr);
  325. while (strsize) {
  326. n = strsize;
  327. if (n > sizeof(str)) n = sizeof(str);
  328. SerialFlash.read(straddr, str, n);
  329. for (i=0; i < n; i++) {
  330. *p++ = str[i];
  331. if (str[i] == 0) {
  332. //Serial.printf(" name = %s\n", filename);
  333. return true;
  334. }
  335. }
  336. strsize -= n;
  337. straddr += n;
  338. }
  339. *(p - 1) = 0;
  340. //Serial.printf(" name(overflow) = %s\n", filename);
  341. return true;
  342. }
  343. void SerialFlashFile::erase()
  344. {
  345. uint32_t i, blocksize;
  346. blocksize = SerialFlash.blockSize();
  347. if (address & (blocksize - 1)) return; // must begin on a block boundary
  348. if (length & (blocksize - 1)) return; // must be exact number of blocks
  349. for (i=0; i < length; i += blocksize) {
  350. SerialFlash.eraseBlock(address + i);
  351. }
  352. }