My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

flash_storage.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*********************
  2. * flash_storage.cpp *
  3. *********************/
  4. /****************************************************************************
  5. * Written By Mark Pelletier 2017 - Aleph Objects, Inc. *
  6. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  7. * *
  8. * This program is free software: you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation, either version 3 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * To view a copy of the GNU General Public License, go to the following *
  19. * location: <https://www.gnu.org/licenses/>. *
  20. ****************************************************************************/
  21. #include "../compat.h"
  22. #if ENABLED(TOUCH_UI_FTDI_EVE)
  23. #include "../ftdi_eve_lib/ftdi_eve_lib.h"
  24. #include "media_file_reader.h"
  25. #include "flash_storage.h"
  26. // The following must be changed whenever the layout of the flash
  27. // data is changed in a manner that would render the data invalid.
  28. constexpr uint32_t flash_eeprom_version = 1;
  29. /* SPI Flash Memory Map:
  30. *
  31. * The following offsets and sizes are specified in 4k erase units:
  32. *
  33. * Page Size Description
  34. * 0 16 DATA STORAGE AREA
  35. * 16 1 VERSIONING DATA
  36. * 17 inf MEDIA STORAGE AREA
  37. */
  38. #define DATA_STORAGE_SIZE_64K
  39. using namespace FTDI::SPI;
  40. using namespace FTDI::SPI::most_significant_byte_first;
  41. bool UIFlashStorage::is_present = false;
  42. #ifdef SPI_FLASH_SS
  43. /************************** SPI Flash Chip Interface **************************/
  44. void SPIFlash::wait_while_busy() {
  45. uint8_t status;
  46. safe_delay(1);
  47. do {
  48. spi_flash_select();
  49. spi_write_8(READ_STATUS_1);
  50. status = spi_read_8();
  51. spi_flash_deselect();
  52. safe_delay(1);
  53. } while (status & 1);
  54. }
  55. void SPIFlash::erase_sector_4k(uint32_t addr) {
  56. spi_flash_select();
  57. spi_write_8(WRITE_ENABLE);
  58. spi_flash_deselect();
  59. spi_flash_select();
  60. spi_write_8(ERASE_4K);
  61. spi_write_24(addr);
  62. spi_flash_deselect();
  63. wait_while_busy();
  64. }
  65. void SPIFlash::erase_sector_64k(uint32_t addr) {
  66. spi_flash_select();
  67. spi_write_8(WRITE_ENABLE);
  68. spi_flash_deselect();
  69. spi_flash_select();
  70. spi_write_8(ERASE_64K);
  71. spi_write_24(addr);
  72. spi_flash_deselect();
  73. wait_while_busy();
  74. }
  75. void SPIFlash::spi_write_begin(uint32_t addr) {
  76. spi_flash_select();
  77. spi_write_8(WRITE_ENABLE);
  78. spi_flash_deselect();
  79. spi_flash_select();
  80. spi_write_8(PAGE_PROGRAM);
  81. spi_write_24(addr);
  82. }
  83. void SPIFlash::spi_write_end() {
  84. spi_flash_deselect();
  85. wait_while_busy();
  86. }
  87. void SPIFlash::spi_read_begin(uint32_t addr) {
  88. spi_flash_select();
  89. spi_write_8(READ_DATA);
  90. spi_write_24(addr);
  91. }
  92. void SPIFlash::spi_read_end() {
  93. spi_flash_deselect();
  94. }
  95. void SPIFlash::erase_chip() {
  96. spi_flash_select();
  97. spi_write_8(WRITE_ENABLE);
  98. spi_flash_deselect();
  99. spi_flash_select();
  100. spi_write_8(ERASE_CHIP);
  101. spi_flash_deselect();
  102. wait_while_busy();
  103. }
  104. void SPIFlash::read_jedec_id(uint8_t &manufacturer_id, uint8_t &device_type, uint8_t &capacity) {
  105. spi_flash_select();
  106. spi_write_8(READ_JEDEC_ID);
  107. manufacturer_id = spi_recv();
  108. device_type = spi_recv();
  109. capacity = spi_recv();
  110. spi_flash_deselect ();
  111. }
  112. /* This function writes "size" bytes from "data" starting at addr, while properly
  113. * taking into account the special case of writing across a 256 byte page boundary.
  114. * Returns the addr directly after the write.
  115. */
  116. uint32_t SPIFlash::write(uint32_t addr, const void *_data, size_t size) {
  117. const uint8_t *data = (const uint8_t*) _data;
  118. while (size) {
  119. const uint32_t page_start = addr & 0xFFFF00ul;
  120. const uint32_t page_end = page_start + 256;
  121. const uint32_t write_size = min(page_end - addr, size);
  122. spi_write_begin(addr);
  123. spi_write_bulk<ram_write>(data, write_size);
  124. spi_write_end();
  125. addr += write_size;
  126. size -= write_size;
  127. data += write_size;
  128. }
  129. return addr;
  130. }
  131. uint32_t SPIFlash::read(uint32_t addr, void *data, size_t size) {
  132. spi_read_begin(addr);
  133. spi_read_bulk(data, size);
  134. spi_read_end();
  135. return addr + size;
  136. }
  137. /********************************** UTILITY ROUTINES *********************************/
  138. bool UIFlashStorage::check_known_device() {
  139. uint8_t manufacturer_id, device_type, capacity;
  140. read_jedec_id(manufacturer_id, device_type, capacity);
  141. const bool is_known =
  142. ((manufacturer_id == 0xEF) && (device_type == 0x40) && (capacity == 0x15)) || // unknown
  143. ((manufacturer_id == 0x01) && (device_type == 0x40) && (capacity == 0x15)) || // Cypress S25FL116K
  144. ((manufacturer_id == 0xEF) && (device_type == 0x14) && (capacity == 0x15)) || // Winbond W25Q16JV
  145. ((manufacturer_id == 0x1F) && (device_type == 0x86) && (capacity == 0x01)) ; // Adesto AT255F161
  146. if (!is_known) {
  147. SERIAL_ECHO_MSG("Unable to locate supported SPI Flash Memory.");
  148. SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" Manufacturer ID, got: ", manufacturer_id);
  149. SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" Device Type , got: ", device_type);
  150. SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" Capacity , got: ", capacity);
  151. }
  152. return is_known;
  153. }
  154. void UIFlashStorage::initialize() {
  155. for (uint8_t i = 0; i < 10; i++) {
  156. if (check_known_device()) {
  157. is_present = true;
  158. break;
  159. }
  160. safe_delay(1000);
  161. }
  162. }
  163. /**************************** DATA STORAGE AREA (first 4K or 64k) ********************/
  164. #ifdef DATA_STORAGE_SIZE_64K
  165. constexpr uint32_t data_storage_area_size = 64 * 1024; // Large erase unit
  166. #else
  167. constexpr uint32_t data_storage_area_size = 4 * 1024; // Small erase unit
  168. #endif
  169. /* In order to provide some degree of wear leveling, each data write to the
  170. * SPI Flash chip is appended to data that was already written before, until
  171. * the data storage area is completely filled. New data is written preceeded
  172. * with a 32-bit delimiter 'LULZ', so that we can distinguish written and
  173. * unwritten data:
  174. *
  175. * 'LULZ' <--- 1st record delimiter
  176. * <data_byte>
  177. * <data_byte>
  178. * <data_byte>
  179. * 'LULZ' <--- 2nd record delimiter
  180. * <data_byte>
  181. * <data_byte>
  182. * <data_byte>
  183. * ...
  184. * 'LULZ' <--- Last record delimiter
  185. * <data_byte>
  186. * <data_byte>
  187. * <data_byte>
  188. * 0xFF <--- Start of free space
  189. * 0xFF
  190. * ...
  191. *
  192. * This function walks down the data storage area, verifying that the
  193. * delimiters are either 'LULZ' or 0xFFFFFFFF. In the case that an invalid
  194. * delimiter is found, this function returns -1, indicating that the Flash
  195. * data is invalid (this will happen if the block_size changed with respect
  196. * to earlier firmware). Otherwise, it returns the offset of the last
  197. * valid delimiter 'LULZ', indicating the most recently written data.
  198. */
  199. int32_t UIFlashStorage::get_config_read_offset(uint32_t block_size) {
  200. uint16_t stride = 4 + block_size;
  201. int32_t read_offset = -1;
  202. for (uint32_t offset = 0; offset < (data_storage_area_size - stride); offset += stride) {
  203. uint32_t delim;
  204. spi_read_begin(offset);
  205. spi_read_bulk (&delim, sizeof(delim));
  206. spi_read_end();
  207. switch (delim) {
  208. case 0xFFFFFFFFul: return read_offset;
  209. case delimiter: read_offset = offset; break;
  210. default:
  211. SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR("Invalid delimiter in Flash: ", delim);
  212. return -1;
  213. }
  214. }
  215. SERIAL_ECHO_MSG("No LULZ delimiter found.");
  216. return -1;
  217. }
  218. /* This function returns the offset at which new data should be
  219. * appended, or -1 if the Flash needs to be erased */
  220. int32_t UIFlashStorage::get_config_write_offset(uint32_t block_size) {
  221. int32_t read_offset = get_config_read_offset(block_size);
  222. if (read_offset == -1) return -1; // The SPI flash is invalid
  223. int32_t write_offset = read_offset + 4 + block_size;
  224. if ((write_offset + 4 + block_size) > data_storage_area_size) {
  225. SERIAL_ECHO_MSG("Not enough free space in Flash.");
  226. return -1; // Not enough free space
  227. }
  228. return write_offset;
  229. }
  230. bool UIFlashStorage::verify_config_data(const void *data, size_t size) {
  231. if (!is_present) return false;
  232. int32_t read_addr = get_config_read_offset(size);
  233. if (read_addr == -1) return false;
  234. uint32_t delim;
  235. spi_read_begin(read_addr);
  236. spi_read_bulk (&delim, sizeof(delim));
  237. bool ok = spi_verify_bulk(data,size);
  238. spi_read_end();
  239. return ok && delim == delimiter;
  240. }
  241. bool UIFlashStorage::read_config_data(void *data, size_t size) {
  242. if (!is_present) return false;
  243. int32_t read_addr = get_config_read_offset(size);
  244. if (read_addr == -1) return false;
  245. uint32_t delim;
  246. spi_read_begin(read_addr);
  247. spi_read_bulk (&delim, sizeof(delim));
  248. spi_read_bulk (data, size);
  249. spi_read_end();
  250. return delim == delimiter;
  251. }
  252. void UIFlashStorage::write_config_data(const void *data, size_t size) {
  253. if (!is_present) {
  254. SERIAL_ECHO_MSG("SPI Flash chip not present. Not saving UI settings.");
  255. return;
  256. }
  257. // Since Flash storage has a limited number of write cycles,
  258. // make sure that the data is different before rewriting.
  259. if (verify_config_data(data, size)) {
  260. SERIAL_ECHO_MSG("UI settings already written, skipping write.");
  261. return;
  262. }
  263. int16_t write_addr = get_config_write_offset(size);
  264. if (write_addr == -1) {
  265. SERIAL_ECHO_START();
  266. SERIAL_ECHOPGM("Erasing UI settings from SPI Flash... ");
  267. #ifdef DATA_STORAGE_SIZE_64K
  268. erase_sector_64k(0);
  269. #else
  270. erase_sector_4k(0);
  271. #endif
  272. write_addr = 0;
  273. SERIAL_ECHOLNPGM("DONE");
  274. }
  275. SERIAL_ECHO_START();
  276. SERIAL_ECHOPAIR("Writing UI settings to SPI Flash (offset ", write_addr);
  277. SERIAL_ECHOPGM(")...");
  278. const uint32_t delim = delimiter;
  279. write_addr = write(write_addr, &delim, sizeof(delim));
  280. write_addr = write(write_addr, data, size);
  281. SERIAL_ECHOLNPGM("DONE");
  282. }
  283. /************************** VERSIONING INFO AREA ************************/
  284. /* The version info area follows the data storage area. If the version
  285. * is incorrect, the data on the chip is invalid and format_flash should
  286. * be called.
  287. */
  288. typedef struct {
  289. uint32_t magic;
  290. uint32_t version;
  291. } flash_version_info;
  292. constexpr uint32_t version_info_addr = data_storage_area_size;
  293. constexpr uint32_t version_info_size = 4 * 1024; // Small erase unit
  294. bool UIFlashStorage::is_valid() {
  295. flash_version_info info;
  296. spi_read_begin(version_info_addr);
  297. spi_read_bulk (&info, sizeof(flash_version_info));
  298. spi_read_end();
  299. return info.magic == delimiter && info.version == flash_eeprom_version;
  300. }
  301. void UIFlashStorage::write_version_info() {
  302. flash_version_info info;
  303. info.magic = delimiter;
  304. info.version = flash_eeprom_version;
  305. spi_write_begin(version_info_addr);
  306. spi_write_bulk<ram_write>(&info, sizeof(flash_version_info));
  307. spi_write_end();
  308. }
  309. /**************************** MEDIA STORAGE AREA *****************************/
  310. /* The media storage area follows the versioning info area. It consists
  311. * of a file index followed by the data for one or more media files.
  312. *
  313. * The file index consists of an array of 32-bit file sizes. If a file
  314. * is not present, the file's size will be set to 0xFFFFFFFF
  315. */
  316. constexpr uint32_t media_storage_addr = version_info_addr + version_info_size;
  317. constexpr uint8_t media_storage_slots = 4;
  318. void UIFlashStorage::format_flash() {
  319. SERIAL_ECHO_START(); SERIAL_ECHOPGM("Erasing SPI Flash...");
  320. SPIFlash::erase_chip();
  321. SERIAL_ECHOLNPGM("DONE");
  322. write_version_info();
  323. }
  324. uint32_t UIFlashStorage::get_media_file_start(uint8_t slot) {
  325. uint32_t addr = media_storage_addr + sizeof(uint32_t) * media_storage_slots;
  326. spi_read_begin(media_storage_addr);
  327. for (uint8_t i = 0; i < slot; i++)
  328. addr += spi_read_32();
  329. spi_read_end();
  330. return addr;
  331. }
  332. void UIFlashStorage::set_media_file_size(uint8_t slot, uint32_t size) {
  333. spi_write_begin(media_storage_addr + sizeof(uint32_t) * slot);
  334. spi_write_32(size);
  335. spi_write_end();
  336. }
  337. uint32_t UIFlashStorage::get_media_file_size(uint8_t slot) {
  338. spi_read_begin(media_storage_addr + sizeof(uint32_t) * slot);
  339. uint32_t size = spi_read_32();
  340. spi_read_end();
  341. return size;
  342. }
  343. /* Writes a media file from the SD card/USB flash drive into a slot on the SPI Flash. Media
  344. * files must be written sequentially following by a chip erase and it is not possible to
  345. * overwrite files. */
  346. UIFlashStorage::error_t UIFlashStorage::write_media_file(progmem_str filename, uint8_t slot) {
  347. #if ENABLED(SDSUPPORT)
  348. uint32_t addr;
  349. uint8_t buff[write_page_size];
  350. strcpy_P( (char*) buff, (const char*) filename);
  351. MediaFileReader reader;
  352. if (!reader.open((char*) buff)) {
  353. SERIAL_ECHO_MSG("Unable to find media file");
  354. return FILE_NOT_FOUND;
  355. }
  356. if (get_media_file_size(slot) != 0xFFFFFFFFUL) {
  357. SERIAL_ECHO_MSG("Media file already exists");
  358. return WOULD_OVERWRITE;
  359. }
  360. SERIAL_ECHO_START(); SERIAL_ECHOPGM("Writing SPI Flash...");
  361. set_media_file_size(slot, reader.size());
  362. addr = get_media_file_start(slot);
  363. // Write out the file itself
  364. for (;;) {
  365. const int16_t nBytes = reader.read(buff, write_page_size);
  366. if (nBytes == -1) {
  367. SERIAL_ECHOLNPGM("Failed to read from file");
  368. return READ_ERROR;
  369. }
  370. addr = write(addr, buff, nBytes);
  371. if (nBytes != write_page_size) break;
  372. TERN_(EXTENSIBLE_UI, ExtUI::yield());
  373. }
  374. SERIAL_ECHOLNPGM("DONE");
  375. SERIAL_ECHO_START(); SERIAL_ECHOPGM("Verifying SPI Flash...");
  376. bool verifyOk = true;
  377. // Verify the file index
  378. if (get_media_file_start(slot+1) != (get_media_file_start(slot) + reader.size())) {
  379. SERIAL_ECHOLNPGM("File index verification failed. ");
  380. verifyOk = false;
  381. }
  382. // Verify the file itself
  383. addr = get_media_file_start(slot);
  384. reader.rewind();
  385. while (verifyOk) {
  386. const int16_t nBytes = reader.read(buff, write_page_size);
  387. if (nBytes == -1) {
  388. SERIAL_ECHOPGM("Failed to read from file");
  389. verifyOk = false;
  390. break;
  391. }
  392. spi_read_begin(addr);
  393. if (!spi_verify_bulk(buff, nBytes)) {
  394. verifyOk = false;
  395. spi_read_end();
  396. break;
  397. }
  398. spi_read_end();
  399. addr += nBytes;
  400. if (nBytes != write_page_size) break;
  401. TERN_(EXTENSIBLE_UI, ExtUI::yield());
  402. };
  403. if (verifyOk) {
  404. SERIAL_ECHOLNPGM("DONE");
  405. return SUCCESS;
  406. }
  407. else {
  408. SERIAL_ECHOLNPGM("FAIL");
  409. return VERIFY_ERROR;
  410. }
  411. #else
  412. return VERIFY_ERROR;
  413. #endif // SDSUPPORT
  414. }
  415. bool UIFlashStorage::BootMediaReader::isAvailable(uint32_t slot) {
  416. if (!is_present) return false;
  417. bytes_remaining = get_media_file_size(slot);
  418. if (bytes_remaining != 0xFFFFFFFFUL) {
  419. SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR("Boot media file size:", bytes_remaining);
  420. addr = get_media_file_start(slot);
  421. return true;
  422. }
  423. return false;
  424. }
  425. int16_t UIFlashStorage::BootMediaReader::read(void *data, const size_t size) {
  426. if (bytes_remaining == 0xFFFFFFFFUL) return -1;
  427. if (size > bytes_remaining)
  428. return read(data, bytes_remaining);
  429. if (size > 0) {
  430. spi_read_begin(addr);
  431. spi_read_bulk(data, size);
  432. spi_read_end();
  433. addr += size;
  434. bytes_remaining -= size;
  435. }
  436. return size;
  437. }
  438. int16_t UIFlashStorage::BootMediaReader::read(void *obj, void *data, const size_t size) {
  439. return reinterpret_cast<UIFlashStorage::BootMediaReader*>(obj)->read(data, size);
  440. }
  441. #else
  442. void UIFlashStorage::initialize() {}
  443. bool UIFlashStorage::is_valid() {return true;}
  444. void UIFlashStorage::write_config_data(const void *, size_t) {}
  445. bool UIFlashStorage::verify_config_data(const void *, size_t) {return false;}
  446. bool UIFlashStorage::read_config_data(void *, size_t ) {return false;}
  447. UIFlashStorage::error_t UIFlashStorage::write_media_file(progmem_str, uint8_t) {return FILE_NOT_FOUND;}
  448. void UIFlashStorage::format_flash() {}
  449. bool UIFlashStorage::BootMediaReader::isAvailable(uint32_t) {return false;}
  450. int16_t UIFlashStorage::BootMediaReader::read(void *, const size_t) {return -1;}
  451. int16_t UIFlashStorage::BootMediaReader::read(void *, void *, const size_t) {return -1;}
  452. #endif // SPI_FLASH_SS
  453. #endif // TOUCH_UI_FTDI_EVE