Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SerialFlashDirectory.cpp 10KB

il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #include "util/crc16.h"
  29. /* On-chip SerialFlash file allocation data structures:
  30. uint32_t signature = 0xFA96554C;
  31. uint16_t maxfiles
  32. uint16_t stringssize // div by 4
  33. uint16_t hashes[maxfiles]
  34. struct {
  35. uint32_t file_begin
  36. uint32_t file_length
  37. uint16_t string_index // div4
  38. } fileinfo[maxfiles]
  39. char strings[stringssize]
  40. A 32 bit signature is stored at the beginning of the flash memory.
  41. If 0xFFFFFFFF is seen, the entire chip should be assumed blank.
  42. If any value other than 0xFA96554C is found, a different data format
  43. is stored. This could should refuse to access the flash.
  44. The next 4 bytes store number of files and size of the strings
  45. section, which allow the position of every other item to be found.
  46. The string section size is the 16 bit integer times 4, which allows
  47. up to 262140 bytes for string data.
  48. An array of 16 bit filename hashes allows for quick linear search
  49. for potentially matching filenames. A hash value of 0xFFFF indicates
  50. no file is allocated for the remainder of the array.
  51. Following the hashes, and array of 10 byte structs give the location
  52. and length of the file's actual data, and the offset of its filename
  53. in the strings section.
  54. Strings are null terminated. The remainder of the chip is file data.
  55. */
  56. #define DEFAULT_MAXFILES 600
  57. #define DEFAULT_STRINGS_SIZE 25560
  58. static uint32_t check_signature(void)
  59. {
  60. uint32_t sig[2];
  61. SerialFlash.read(0, sig, 8);
  62. //Serial.printf("sig: %08X %08X\n", sig[0], sig[1]);
  63. if (sig[0] == 0xFA96554C) return sig[1];
  64. if (sig[0] == 0xFFFFFFFF) {
  65. sig[0] = 0xFA96554C;
  66. sig[1] = ((DEFAULT_STRINGS_SIZE/4) << 16) | DEFAULT_MAXFILES;
  67. SerialFlash.write(0, sig, 8);
  68. while (!SerialFlash.ready()) ; // TODO: timeout
  69. SerialFlash.read(0, sig, 8);
  70. if (sig[0] == 0xFA96554C) return sig[1];
  71. }
  72. return 0;
  73. }
  74. static uint16_t filename_hash(const char *filename)
  75. {
  76. // http://isthe.com/chongo/tech/comp/fnv/
  77. uint32_t hash = 2166136261;
  78. const char *p;
  79. for (p=filename; *p; p++) {
  80. hash ^= *p;
  81. hash *= 16777619;
  82. }
  83. hash %= (uint32_t)0xFFFF;
  84. return hash;
  85. }
  86. static bool filename_compare(const char *filename, uint32_t straddr)
  87. {
  88. unsigned int i;
  89. const char *p;
  90. char buf[16];
  91. p = filename;
  92. while (1) {
  93. SerialFlash.read(straddr, buf, sizeof(buf));
  94. straddr += sizeof(buf);
  95. for (i=0; i < sizeof(buf); i++) {
  96. if (*p++ != buf[i]) return false;
  97. if (buf[i] == 0) return true;
  98. }
  99. }
  100. }
  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. SerialFlashFile SerialFlashChip::open(const char *filename)
  110. {
  111. uint32_t maxfiles, straddr;
  112. uint16_t hash, hashtable[8];
  113. uint32_t i, n, index=0;
  114. uint32_t buf[3];
  115. SerialFlashFile file;
  116. maxfiles = check_signature();
  117. //Serial.printf("sig: %08X\n", maxfiles);
  118. if (!maxfiles) return file;
  119. maxfiles &= 0xFFFF;
  120. hash = filename_hash(filename);
  121. while (index < maxfiles) {
  122. n = 8;
  123. if (n > maxfiles - index) n = maxfiles - index;
  124. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  125. //Serial.printf(" read %u: ", 8 + index * 2);
  126. //pbuf(hashtable, n * 2);
  127. for (i=0; i < n; i++) {
  128. if (hashtable[i] == hash) {
  129. //Serial.printf(" hash match at index %u\n", index+i);
  130. buf[2] = 0;
  131. SerialFlash.read(8 + maxfiles * 2 + (index+i) * 10, buf, 10);
  132. //Serial.printf(" maxf=%d, index=%d, i=%d\n", maxfiles, index, i);
  133. //Serial.printf(" read %u: ", 8 + maxfiles * 2 + (index+i) * 10);
  134. //pbuf(buf, 10);
  135. straddr = 8 + maxfiles * 12 + buf[2] * 4;
  136. //Serial.printf(" straddr = %u\n", straddr);
  137. if (filename_compare(filename, straddr)) {
  138. //Serial.printf(" match!\n");
  139. //Serial.printf(" addr = %u\n", buf[0]);
  140. //Serial.printf(" len = %u\n", buf[1]);
  141. file.address = buf[0];
  142. file.length = buf[1];
  143. file.offset = 0;
  144. return file;
  145. }
  146. } else if (hashtable[i] == 0xFFFF) {
  147. return file;
  148. }
  149. }
  150. index += n;
  151. }
  152. return file;
  153. }
  154. static uint32_t find_first_unallocated_file_index(uint32_t maxfiles)
  155. {
  156. uint16_t hashtable[8];
  157. uint32_t i, n, index=0;
  158. do {
  159. n = 8;
  160. if (index + n > maxfiles) n = maxfiles - index;
  161. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  162. for (i=0; i < n; i++) {
  163. if (hashtable[i] == 0xFFFF) return index + i;
  164. }
  165. index += n;
  166. } while (index < maxfiles);
  167. return 0xFFFFFFFF;
  168. }
  169. static uint32_t string_length(uint32_t addr)
  170. {
  171. char buf[16];
  172. const char *p;
  173. uint32_t len=0;
  174. while (1) {
  175. SerialFlash.read(addr, buf, sizeof(buf));
  176. for (p=buf; p < buf + sizeof(buf); p++) {
  177. if (*p == 0) return len;
  178. len++;
  179. }
  180. addr += len;
  181. }
  182. }
  183. // uint32_t signature = 0xFA96554C;
  184. // uint16_t maxfiles
  185. // uint16_t stringssize // div by 4
  186. // uint16_t hashes[maxfiles]
  187. // struct {
  188. // uint32_t file_begin
  189. // uint32_t file_length
  190. // uint16_t string_index // div 4
  191. // } fileinfo[maxfiles]
  192. // char strings[stringssize]
  193. bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t align)
  194. {
  195. uint32_t maxfiles, stringsize;
  196. uint32_t index, buf[3];
  197. uint32_t address, straddr, len;
  198. SerialFlashFile file;
  199. // first, get the filesystem parameters
  200. maxfiles = check_signature();
  201. if (!maxfiles) return false;
  202. stringsize = (maxfiles & 0xFFFF0000) >> 14;
  203. maxfiles &= 0xFFFF;
  204. // TODO: should we check if the file already exists? Then what?
  205. // find the first unused slot for this file
  206. index = find_first_unallocated_file_index(maxfiles);
  207. if (index >= maxfiles) return false;
  208. //Serial.printf("index = %u\n", index);
  209. // compute where to store the filename and actual data
  210. straddr = 8 + maxfiles * 12;
  211. if (index == 0) {
  212. address = straddr + stringsize;
  213. } else {
  214. buf[2] = 0;
  215. SerialFlash.read(8 + maxfiles * 2 + (index-1) * 10, buf, 10);
  216. address = buf[0] + buf[1];
  217. straddr += buf[2] * 4;
  218. straddr += string_length(straddr);
  219. straddr = (straddr + 3) & 0x0003FFFC;
  220. }
  221. //Serial.printf("straddr = %u\n", straddr);
  222. //Serial.printf("address = %u\n", address);
  223. //Serial.printf("length = %u\n", length);
  224. if (align > 0) {
  225. // for files aligned to sectors, adjust addr & len
  226. address += align - 1;
  227. address /= align;
  228. address *= align;
  229. //Serial.printf("align address = %u\n", address);
  230. length += align - 1;
  231. length /= align;
  232. length *= align;
  233. //Serial.printf("align length = %u\n", length);
  234. } else {
  235. // always align every file to a page boundary
  236. // for predictable write latency and to guarantee
  237. // write suspend for reading another file can't
  238. // conflict on the same page (2 files never share
  239. // a write page).
  240. address = (address + 255) & 0xFFFFFF00;
  241. }
  242. //Serial.printf("address = %u\n", address);
  243. // last check, if enough space exists...
  244. len = strlen(filename);
  245. // TODO: check for enough string space for filename
  246. uint8_t id[3];
  247. SerialFlash.readID(id);
  248. if (address + length > SerialFlash.capacity(id)) return false;
  249. SerialFlash.write(straddr, filename, len+1);
  250. buf[0] = address;
  251. buf[1] = length;
  252. buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
  253. SerialFlash.write(8 + maxfiles * 2 + index * 10, buf, 10);
  254. //Serial.printf(" write %u: ", 8 + maxfiles * 2 + index * 10);
  255. //pbuf(buf, 10);
  256. while (!SerialFlash.ready()) ; // TODO: timeout
  257. buf[0] = filename_hash(filename);
  258. //Serial.printf("hash = %04X\n", buf[0]);
  259. SerialFlash.write(8 + index * 2, buf, 2);
  260. while (!SerialFlash.ready()) ; // TODO: timeout
  261. return true;
  262. }
  263. bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesize)
  264. {
  265. uint32_t maxfiles, index, straddr;
  266. uint32_t i, n;
  267. uint32_t buf[2];
  268. char str[16], *p=filename;
  269. filename[0] = 0;
  270. maxfiles = check_signature();
  271. if (!maxfiles) return false;
  272. maxfiles &= 0xFFFF;
  273. index = dirindex;
  274. if (index >= maxfiles) return false;
  275. dirindex = index + 1;
  276. //Serial.printf("readdir, index = %u\n", index);
  277. buf[1] = 0;
  278. SerialFlash.read(8 + 4 + maxfiles * 2 + index * 10, buf, 6);
  279. if (buf[0] == 0xFFFFFFFF) return false;
  280. filesize = buf[0];
  281. straddr = 8 + maxfiles * 12 + buf[1] * 4;
  282. //Serial.printf(" length = %u\n", buf[0]);
  283. //Serial.printf(" straddr = %u\n", straddr);
  284. while (strsize) {
  285. n = strsize;
  286. if (n > sizeof(str)) n = sizeof(str);
  287. SerialFlash.read(straddr, str, n);
  288. for (i=0; i < n; i++) {
  289. *p++ = str[i];
  290. if (str[i] == 0) {
  291. //Serial.printf(" name = %s\n", filename);
  292. return true;
  293. }
  294. }
  295. strsize -= n;
  296. }
  297. *(p - 1) = 0;
  298. //Serial.printf(" name(overflow) = %s\n", filename);
  299. return true;
  300. }
  301. void SerialFlashFile::erase()
  302. {
  303. uint32_t i, blocksize;
  304. blocksize = SerialFlash.blockSize();
  305. if (address & (blocksize - 1)) return; // must begin on a block boundary
  306. if (length & (blocksize - 1)) return; // must be exact number of blocks
  307. for (i=0; i < length; i += blocksize) {
  308. SerialFlash.eraseBlock(address + i);
  309. }
  310. }