Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

274 lines
6.8KB

  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. if (align > 0) {
  175. // for files aligned to sectors, adjust addr & len
  176. address += align - 1;
  177. address /= align;
  178. address *= align;
  179. length += align - 1;
  180. length /= align;
  181. length *= align;
  182. } else {
  183. // always align every file to a page boundary
  184. // for predictable write latency and to guarantee
  185. // write suspend for reading another file can't
  186. // conflict on the same page (2 files never share
  187. // a write page).
  188. address = (address + 255) & 0xFFFFFF00;
  189. }
  190. // last check, if enough space exists...
  191. len = strlen(filename);
  192. // TODO: check for enough string space for filename
  193. if (address + length > SerialFlash.capacity()) return false;
  194. SerialFlash.write(filename, straddr, len+1);
  195. buf[0] = address;
  196. buf[1] = length;
  197. buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
  198. SerialFlash.write(buf, 8 + maxfiles * 2 + index * 10, 10);
  199. buf[0] = filename_hash(filename);
  200. SerialFlash.write(buf, 8 + index * 2, 2);
  201. while (!SerialFlash.ready()) ; // TODO: timeout
  202. return false;
  203. }
  204. bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesize)
  205. {
  206. uint32_t maxfiles, index, straddr;
  207. uint32_t i, n;
  208. uint32_t buf[2];
  209. char str[16], *p=filename;
  210. filename[0] = 0;
  211. maxfiles = check_signature();
  212. if (!maxfiles) return false;
  213. maxfiles &= 0xFFFF;
  214. index = dirindex;
  215. if (index >= maxfiles) return false;
  216. dirindex = index + 1;
  217. buf[1] = 0;
  218. SerialFlash.read(buf, 8 + 4 + maxfiles * 2 + index * 10, 6);
  219. if (buf[0] == 0xFFFFFFFF) return false;
  220. filesize = buf[0];
  221. straddr = 8 + maxfiles * 12 + buf[1] * 4;
  222. while (strsize) {
  223. n = strsize;
  224. if (n > sizeof(str)) n = sizeof(str);
  225. SerialFlash.read(str, straddr, n);
  226. for (i=0; i < n; i++) {
  227. *p++ = str[i];
  228. if (str[i] == 0) {
  229. return true;
  230. }
  231. }
  232. strsize -= n;
  233. }
  234. *(p - 1) = 0;
  235. return true;
  236. }
  237. void SerialFlashFile::erase()
  238. {
  239. // TODO: erase all the blocks of a file
  240. // if it's been block aligned, of course
  241. }