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 18KB

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