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

SerialFlashDirectory.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "SerialFlash.h"
  2. #include "util/crc16.h"
  3. /* On-chip SerialFlash file allocation data structures:
  4. uint32_t signature = 0xFA96554C;
  5. uint16_t maxfiles
  6. uint16_t stringssize // div by 4
  7. uint16_t hashes[maxfiles]
  8. struct {
  9. uint32_t file_begin
  10. uint32_t file_length
  11. uint16_t string_index // div4
  12. } fileinfo[maxfiles]
  13. char strings[stringssize]
  14. A 32 bit signature is stored at the beginning of the flash memory.
  15. If 0xFFFFFFFF is seen, the entire chip should be assumed blank.
  16. If any value other than 0xFA96554C is found, a different data format
  17. is stored. This could should refuse to access the flash.
  18. The next 4 bytes store number of files and size of the strings
  19. section, which allow the position of every other item to be found.
  20. The string section size is the 16 bit integer times 4, which allows
  21. up to 262140 bytes for string data.
  22. An array of 16 bit filename hashes allows for quick linear search
  23. for potentially matching filenames. A hash value of 0xFFFF indicates
  24. no file is allocated for the remainder of the array.
  25. Following the hashes, and array of 10 byte structs give the location
  26. and length of the file's actual data, and the offset of its filename
  27. in the strings section.
  28. Strings are null terminated. The remainder of the chip is file data.
  29. */
  30. #define DEFAULT_MAXFILES 600
  31. #define DEFAULT_STRINGS_SIZE 25560
  32. static uint32_t check_signature(void)
  33. {
  34. uint32_t sig[2];
  35. SerialFlash.read(sig, 0, 8);
  36. if (sig[0] == 0xFA96554C) return sig[1];
  37. if (sig[0] == 0xFFFFFFFF) {
  38. sig[0] = 0xFA96554C;
  39. sig[1] = ((DEFAULT_STRINGS_SIZE/4) << 16) | DEFAULT_MAXFILES;
  40. SerialFlash.write(sig, 0, 8);
  41. while (!SerialFlash.ready()) ; // TODO: timeout
  42. SerialFlash.read(sig, 0, 8);
  43. if (sig[0] == 0xFA96554C) return sig[1];
  44. }
  45. return 0;
  46. }
  47. static uint16_t filename_hash(const char *filename)
  48. {
  49. uint16_t crc;
  50. const char *p;
  51. crc = 0xFFFF;
  52. for (p=filename; *p; p++) {
  53. // TODO: replace with fast CRC hardware?
  54. crc = _crc16_update(crc, *p);
  55. }
  56. crc ^= 0xFFFF;
  57. if (crc == 0xFFFF) crc = 0;
  58. return crc;
  59. }
  60. static bool filename_compare(const char *filename, uint32_t straddr)
  61. {
  62. unsigned int i;
  63. const char *p;
  64. char buf[16];
  65. p = filename;
  66. while (1) {
  67. SerialFlash.read(buf, straddr, sizeof(buf));
  68. straddr += sizeof(buf);
  69. for (i=0; i < sizeof(buf); i++) {
  70. if (*p++ != buf[i]) return false;
  71. if (buf[i] == 0) return true;
  72. }
  73. }
  74. }
  75. SerialFlashFile SerialFlashChip::open(const char *filename)
  76. {
  77. uint32_t maxfiles, straddr;
  78. uint16_t hash, hashtable[8];
  79. uint32_t i, n, index=0;
  80. uint32_t buf[3];
  81. SerialFlashFile file;
  82. maxfiles = check_signature();
  83. if (!maxfiles) return file;
  84. maxfiles &= 0xFFFF;
  85. hash = filename_hash(filename);
  86. while (index < maxfiles) {
  87. n = 8;
  88. if (n > maxfiles - index) n = maxfiles - index;
  89. SerialFlash.read(hashtable, 8 + index * 2, n * 2);
  90. for (i=0; i < n; i++) {
  91. if (hashtable[i] == hash) {
  92. buf[2] = 0;
  93. SerialFlash.read(buf, 8 + maxfiles * 2 + (index+i) * 10, 10);
  94. straddr = 8 + maxfiles * 12 + buf[2] * 4;
  95. if (filename_compare(filename, straddr)) {
  96. file.address = buf[0];
  97. file.length = buf[1];
  98. file.offset = 0;
  99. return file;
  100. }
  101. } else if (hashtable[i] == 0xFFFF) {
  102. return file;
  103. }
  104. }
  105. index += n;
  106. }
  107. return file;
  108. }
  109. static uint32_t find_first_unallocated_file_index(uint32_t maxfiles)
  110. {
  111. uint16_t hashtable[8];
  112. uint32_t i, n, index=0;
  113. do {
  114. n = 8;
  115. if (index + n > maxfiles) n = maxfiles - index;
  116. SerialFlash.read(hashtable, 8 + index * 2, n * 2);
  117. for (i=0; i < n; i++) {
  118. if (hashtable[i] == 0xFFFF) return index + i;
  119. }
  120. index += n;
  121. } while (index < maxfiles);
  122. return 0xFFFFFFFF;
  123. }
  124. static uint32_t string_length(uint32_t addr)
  125. {
  126. char buf[16];
  127. const char *p;
  128. uint32_t len=0;
  129. while (1) {
  130. SerialFlash.read(buf, addr, sizeof(buf));
  131. for (p=buf; p < buf + sizeof(buf); p++) {
  132. if (*p == 0) return len;
  133. len++;
  134. }
  135. addr += len;
  136. }
  137. }
  138. // uint32_t signature = 0xFA96554C;
  139. // uint16_t maxfiles
  140. // uint16_t stringssize // div by 4
  141. // uint16_t hashes[maxfiles]
  142. // struct {
  143. // uint32_t file_begin
  144. // uint32_t file_length
  145. // uint16_t string_index // div 4
  146. // } fileinfo[maxfiles]
  147. // char strings[stringssize]
  148. bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t align)
  149. {
  150. uint32_t maxfiles, stringsize;
  151. uint32_t index, buf[3];
  152. uint32_t address, straddr, len;
  153. SerialFlashFile file;
  154. // first, get the filesystem parameters
  155. maxfiles = check_signature();
  156. if (!maxfiles) return false;
  157. stringsize = (maxfiles & 0xFFFF0000) >> 14;
  158. maxfiles &= 0xFFFF;
  159. // find the first unused slot for this file
  160. index = find_first_unallocated_file_index(maxfiles);
  161. if (index >= maxfiles) return false;
  162. // compute where to store the filename and actual data
  163. straddr = 8 + maxfiles * 12;
  164. if (index == 0) {
  165. address = straddr + stringsize;
  166. } else {
  167. buf[2] = 0;
  168. SerialFlash.read(buf, 8 + maxfiles * 2 + (index-1) * 10, 10);
  169. address = buf[0] + buf[1];
  170. straddr += buf[2] * 4;
  171. straddr += string_length(straddr);
  172. straddr = (straddr + 3) & 0x0003FFFC;
  173. }
  174. // for files aligned to pages or sectors, adjust addr & len
  175. if (align > 0) {
  176. address += align - 1;
  177. address /= align;
  178. address *= align;
  179. length += align - 1;
  180. length /= align;
  181. length *= align;
  182. }
  183. // last check, if enough space exists...
  184. len = strlen(filename);
  185. // TODO: check for enough string space for filename
  186. if (address + length > SerialFlash.capacity()) return false;
  187. SerialFlash.write(filename, straddr, len+1);
  188. buf[0] = address;
  189. buf[1] = length;
  190. buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
  191. SerialFlash.write(buf, 8 + maxfiles * 2 + index * 10, 10);
  192. buf[0] = filename_hash(filename);
  193. SerialFlash.write(buf, 8 + index * 2, 2);
  194. while (!SerialFlash.ready()) ; // TODO: timeout
  195. return false;
  196. }
  197. bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesize)
  198. {
  199. uint32_t maxfiles, index, straddr;
  200. uint32_t i, n;
  201. uint32_t buf[2];
  202. char str[16], *p=filename;
  203. filename[0] = 0;
  204. maxfiles = check_signature();
  205. if (!maxfiles) return false;
  206. maxfiles &= 0xFFFF;
  207. index = dirindex;
  208. if (index >= maxfiles) return false;
  209. dirindex = index + 1;
  210. buf[1] = 0;
  211. SerialFlash.read(buf, 8 + 4 + maxfiles * 2 + index * 10, 6);
  212. if (buf[0] == 0xFFFFFFFF) return false;
  213. filesize = buf[0];
  214. straddr = 8 + maxfiles * 12 + buf[1] * 4;
  215. while (strsize) {
  216. n = strsize;
  217. if (n > sizeof(str)) n = sizeof(str);
  218. SerialFlash.read(str, straddr, n);
  219. for (i=0; i < n; i++) {
  220. *p++ = str[i];
  221. if (str[i] == 0) {
  222. return true;
  223. }
  224. }
  225. strsize -= n;
  226. }
  227. *(p - 1) = 0;
  228. return true;
  229. }
  230. void SerialFlashFile::erase()
  231. {
  232. // TODO: erase all the blocks of a file
  233. // if it's been block aligned, of course
  234. }