您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

309 行
8.6KB

  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. if (sig[0] == 0xFA96554C) return sig[1];
  63. if (sig[0] == 0xFFFFFFFF) {
  64. sig[0] = 0xFA96554C;
  65. sig[1] = ((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 %= (uint32_t)0xFFFF;
  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. SerialFlashFile SerialFlashChip::open(const char *filename)
  101. {
  102. uint32_t maxfiles, straddr;
  103. uint16_t hash, hashtable[8];
  104. uint32_t i, n, index=0;
  105. uint32_t buf[3];
  106. SerialFlashFile file;
  107. maxfiles = check_signature();
  108. if (!maxfiles) return file;
  109. maxfiles &= 0xFFFF;
  110. hash = filename_hash(filename);
  111. while (index < maxfiles) {
  112. n = 8;
  113. if (n > maxfiles - index) n = maxfiles - index;
  114. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  115. for (i=0; i < n; i++) {
  116. if (hashtable[i] == hash) {
  117. buf[2] = 0;
  118. SerialFlash.read(8 + maxfiles * 2 + (index+i) * 10, buf, 10);
  119. straddr = 8 + maxfiles * 12 + buf[2] * 4;
  120. if (filename_compare(filename, straddr)) {
  121. file.address = buf[0];
  122. file.length = buf[1];
  123. file.offset = 0;
  124. return file;
  125. }
  126. } else if (hashtable[i] == 0xFFFF) {
  127. return file;
  128. }
  129. }
  130. index += n;
  131. }
  132. return file;
  133. }
  134. static uint32_t find_first_unallocated_file_index(uint32_t maxfiles)
  135. {
  136. uint16_t hashtable[8];
  137. uint32_t i, n, index=0;
  138. do {
  139. n = 8;
  140. if (index + n > maxfiles) n = maxfiles - index;
  141. SerialFlash.read(8 + index * 2, hashtable, n * 2);
  142. for (i=0; i < n; i++) {
  143. if (hashtable[i] == 0xFFFF) return index + i;
  144. }
  145. index += n;
  146. } while (index < maxfiles);
  147. return 0xFFFFFFFF;
  148. }
  149. static uint32_t string_length(uint32_t addr)
  150. {
  151. char buf[16];
  152. const char *p;
  153. uint32_t len=0;
  154. while (1) {
  155. SerialFlash.read(addr, buf, sizeof(buf));
  156. for (p=buf; p < buf + sizeof(buf); p++) {
  157. if (*p == 0) return len;
  158. len++;
  159. }
  160. addr += len;
  161. }
  162. }
  163. // uint32_t signature = 0xFA96554C;
  164. // uint16_t maxfiles
  165. // uint16_t stringssize // div by 4
  166. // uint16_t hashes[maxfiles]
  167. // struct {
  168. // uint32_t file_begin
  169. // uint32_t file_length
  170. // uint16_t string_index // div 4
  171. // } fileinfo[maxfiles]
  172. // char strings[stringssize]
  173. bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t align)
  174. {
  175. uint32_t maxfiles, stringsize;
  176. uint32_t index, buf[3];
  177. uint32_t address, straddr, len;
  178. SerialFlashFile file;
  179. // first, get the filesystem parameters
  180. maxfiles = check_signature();
  181. if (!maxfiles) return false;
  182. stringsize = (maxfiles & 0xFFFF0000) >> 14;
  183. maxfiles &= 0xFFFF;
  184. // TODO: should we check if the file already exists? Then what?
  185. // find the first unused slot for this file
  186. index = find_first_unallocated_file_index(maxfiles);
  187. if (index >= maxfiles) return false;
  188. // compute where to store the filename and actual data
  189. straddr = 8 + maxfiles * 12;
  190. if (index == 0) {
  191. address = straddr + stringsize;
  192. } else {
  193. buf[2] = 0;
  194. SerialFlash.read(8 + maxfiles * 2 + (index-1) * 10, buf, 10);
  195. address = buf[0] + buf[1];
  196. straddr += buf[2] * 4;
  197. straddr += string_length(straddr);
  198. straddr = (straddr + 3) & 0x0003FFFC;
  199. }
  200. if (align > 0) {
  201. // for files aligned to sectors, adjust addr & len
  202. address += align - 1;
  203. address /= align;
  204. address *= align;
  205. length += align - 1;
  206. length /= align;
  207. length *= align;
  208. } else {
  209. // always align every file to a page boundary
  210. // for predictable write latency and to guarantee
  211. // write suspend for reading another file can't
  212. // conflict on the same page (2 files never share
  213. // a write page).
  214. address = (address + 255) & 0xFFFFFF00;
  215. }
  216. // last check, if enough space exists...
  217. len = strlen(filename);
  218. // TODO: check for enough string space for filename
  219. uint8_t id[3];
  220. SerialFlash.readID(id);
  221. if (address + length > SerialFlash.capacity(id)) return false;
  222. SerialFlash.write(straddr, filename, len+1);
  223. buf[0] = address;
  224. buf[1] = length;
  225. buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
  226. SerialFlash.write(8 + maxfiles * 2 + index * 10, buf, 10);
  227. buf[0] = filename_hash(filename);
  228. SerialFlash.write(8 + index * 2, buf, 2);
  229. while (!SerialFlash.ready()) ; // TODO: timeout
  230. return true;
  231. }
  232. bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesize)
  233. {
  234. uint32_t maxfiles, index, straddr;
  235. uint32_t i, n;
  236. uint32_t buf[2];
  237. char str[16], *p=filename;
  238. filename[0] = 0;
  239. maxfiles = check_signature();
  240. if (!maxfiles) return false;
  241. maxfiles &= 0xFFFF;
  242. index = dirindex;
  243. if (index >= maxfiles) return false;
  244. dirindex = index + 1;
  245. buf[1] = 0;
  246. SerialFlash.read(8 + 4 + maxfiles * 2 + index * 10, buf, 6);
  247. if (buf[0] == 0xFFFFFFFF) return false;
  248. filesize = buf[0];
  249. straddr = 8 + maxfiles * 12 + buf[1] * 4;
  250. while (strsize) {
  251. n = strsize;
  252. if (n > sizeof(str)) n = sizeof(str);
  253. SerialFlash.read(straddr, str, n);
  254. for (i=0; i < n; i++) {
  255. *p++ = str[i];
  256. if (str[i] == 0) {
  257. return true;
  258. }
  259. }
  260. strsize -= n;
  261. }
  262. *(p - 1) = 0;
  263. return true;
  264. }
  265. void SerialFlashFile::erase()
  266. {
  267. uint32_t i, blocksize;
  268. blocksize = SerialFlash.blockSize();
  269. if (address & (blocksize - 1)) return; // must begin on a block boundary
  270. if (length & (blocksize - 1)) return; // must be exact number of blocks
  271. for (i=0; i < length; i += blocksize) {
  272. SerialFlash.eraseBlock(address + i);
  273. }
  274. }