flash_api.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /******************************************************************************
  2. * Flash api for NodeMCU
  3. * NodeMCU Team
  4. * 2014-12-31
  5. *******************************************************************************/
  6. #include "user_config.h"
  7. #include "flash_api.h"
  8. #include "spi_flash.h"
  9. #include "c_stdio.h"
  10. static volatile const uint8_t flash_init_data[128] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR =
  11. {
  12. 0x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05,
  13. 0x04, 0xFE, 0xFD, 0xFF, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xE0, 0xE1, 0x0A, 0xFF, 0xFF, 0xF8, 0x00,
  14. 0xF8, 0xF8, 0x52, 0x4E, 0x4A, 0x44, 0x40, 0x38, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05,
  15. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16. 0xE1, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  20. };
  21. uint32_t flash_detect_size_byte(void)
  22. {
  23. #define FLASH_BUFFER_SIZE_DETECT 32
  24. uint32_t dummy_size = FLASH_SIZE_256KBYTE;
  25. uint8_t data_orig[FLASH_BUFFER_SIZE_DETECT] ICACHE_STORE_ATTR = {0};
  26. uint8_t data_new[FLASH_BUFFER_SIZE_DETECT] ICACHE_STORE_ATTR = {0};
  27. if (SPI_FLASH_RESULT_OK == flash_safe_read(0, (uint32 *)data_orig, FLASH_BUFFER_SIZE_DETECT))
  28. {
  29. dummy_size = FLASH_SIZE_256KBYTE;
  30. while ((dummy_size < FLASH_SIZE_16MBYTE) &&
  31. (SPI_FLASH_RESULT_OK == flash_safe_read(dummy_size, (uint32 *)data_new, FLASH_BUFFER_SIZE_DETECT)) &&
  32. (0 != os_memcmp(data_orig, data_new, FLASH_BUFFER_SIZE_DETECT))
  33. )
  34. {
  35. dummy_size *= 2;
  36. }
  37. };
  38. return dummy_size;
  39. #undef FLASH_BUFFER_SIZE_DETECT
  40. }
  41. uint32_t flash_safe_get_size_byte(void)
  42. {
  43. static uint32_t flash_size = 0;
  44. if (flash_size == 0)
  45. {
  46. flash_size = flash_detect_size_byte();
  47. }
  48. return flash_size;
  49. }
  50. uint16_t flash_safe_get_sec_num(void)
  51. {
  52. return (flash_safe_get_size_byte() / (SPI_FLASH_SEC_SIZE));
  53. }
  54. SpiFlashOpResult flash_safe_read(uint32 src_addr, uint32 *des_addr, uint32 size)
  55. {
  56. SpiFlashOpResult result = SPI_FLASH_RESULT_ERR;
  57. FLASH_SAFEMODE_ENTER();
  58. result = spi_flash_read(src_addr, (uint32 *) des_addr, size);
  59. FLASH_SAFEMODE_LEAVE();
  60. return result;
  61. }
  62. SpiFlashOpResult flash_safe_write(uint32 des_addr, uint32 *src_addr, uint32 size)
  63. {
  64. SpiFlashOpResult result = SPI_FLASH_RESULT_ERR;
  65. FLASH_SAFEMODE_ENTER();
  66. result = spi_flash_write(des_addr, src_addr, size);
  67. FLASH_SAFEMODE_LEAVE();
  68. return result;
  69. }
  70. SpiFlashOpResult flash_safe_erase_sector(uint16 sec)
  71. {
  72. SpiFlashOpResult result = SPI_FLASH_RESULT_ERR;
  73. FLASH_SAFEMODE_ENTER();
  74. result = spi_flash_erase_sector(sec);
  75. FLASH_SAFEMODE_LEAVE();
  76. return result;
  77. }
  78. SPIFlashInfo flash_rom_getinfo(void)
  79. {
  80. volatile SPIFlashInfo spi_flash_info ICACHE_STORE_ATTR;
  81. // Don't use it before cache read disabled
  82. // FLASH_DISABLE_CACHE();
  83. // spi_flash_info = *((SPIFlashInfo *)(FLASH_ADDRESS_START_MAP));
  84. // FLASH_ENABLE_CACHE();
  85. // Needn't safe mode.
  86. spi_flash_read(0, (uint32 *)(& spi_flash_info), sizeof(spi_flash_info));
  87. return spi_flash_info;
  88. }
  89. uint8_t flash_rom_get_size_type(void)
  90. {
  91. return flash_rom_getinfo().size;
  92. }
  93. uint32_t flash_rom_get_size_byte(void)
  94. {
  95. static uint32_t flash_size = 0;
  96. if (flash_size == 0)
  97. {
  98. switch (flash_rom_getinfo().size)
  99. {
  100. case SIZE_2MBIT:
  101. // 2Mbit, 256kByte
  102. flash_size = 256 * 1024;
  103. break;
  104. case SIZE_4MBIT:
  105. // 4Mbit, 512kByte
  106. flash_size = 512 * 1024;
  107. break;
  108. case SIZE_8MBIT:
  109. // 8Mbit, 1MByte
  110. flash_size = 1 * 1024 * 1024;
  111. break;
  112. case SIZE_16MBIT:
  113. // 16Mbit, 2MByte
  114. flash_size = 2 * 1024 * 1024;
  115. break;
  116. case SIZE_32MBIT:
  117. // 32Mbit, 4MByte
  118. flash_size = 4 * 1024 * 1024;
  119. break;
  120. case SIZE_64MBIT:
  121. // 64Mbit, 8MByte
  122. flash_size = 8 * 1024 * 1024;
  123. break;
  124. case SIZE_128MBIT:
  125. // 128Mbit, 16MByte
  126. flash_size = 16 * 1024 * 1024;
  127. break;
  128. default:
  129. // Unknown flash size, fall back mode.
  130. flash_size = 512 * 1024;
  131. break;
  132. }
  133. }
  134. return flash_size;
  135. }
  136. bool flash_rom_set_size_type(uint8_t size)
  137. {
  138. // Dangerous, here are dinosaur infested!!!!!
  139. // Reboot required!!!
  140. // If you don't know what you're doing, your nodemcu may turn into stone ...
  141. NODE_DBG("\nBEGIN SET FLASH HEADER\n");
  142. uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR;
  143. if (SPI_FLASH_RESULT_OK == spi_flash_read(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
  144. {
  145. ((SPIFlashInfo *)(&data[0]))->size = size;
  146. if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(0 * SPI_FLASH_SEC_SIZE))
  147. {
  148. NODE_DBG("\nERASE SUCCESS\n");
  149. }
  150. if (SPI_FLASH_RESULT_OK == spi_flash_write(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
  151. {
  152. NODE_DBG("\nWRITE SUCCESS, %u\n", size);
  153. }
  154. }
  155. NODE_DBG("\nEND SET FLASH HEADER\n");
  156. return true;
  157. }
  158. bool flash_rom_set_size_byte(uint32_t size)
  159. {
  160. // Dangerous, here are dinosaur infested!!!!!
  161. // Reboot required!!!
  162. // If you don't know what you're doing, your nodemcu may turn into stone ...
  163. bool result = true;
  164. uint32_t flash_size = 0;
  165. switch (size)
  166. {
  167. case 256 * 1024:
  168. // 2Mbit, 256kByte
  169. flash_size = SIZE_2MBIT;
  170. flash_rom_set_size_type(flash_size);
  171. break;
  172. case 512 * 1024:
  173. // 4Mbit, 512kByte
  174. flash_size = SIZE_4MBIT;
  175. flash_rom_set_size_type(flash_size);
  176. break;
  177. case 1 * 1024 * 1024:
  178. // 8Mbit, 1MByte
  179. flash_size = SIZE_8MBIT;
  180. flash_rom_set_size_type(flash_size);
  181. break;
  182. case 2 * 1024 * 1024:
  183. // 16Mbit, 2MByte
  184. flash_size = SIZE_16MBIT;
  185. flash_rom_set_size_type(flash_size);
  186. break;
  187. case 4 * 1024 * 1024:
  188. // 32Mbit, 4MByte
  189. flash_size = SIZE_32MBIT;
  190. flash_rom_set_size_type(flash_size);
  191. break;
  192. case 8 * 1024 * 1024:
  193. // 64Mbit, 8MByte
  194. flash_size = SIZE_64MBIT;
  195. flash_rom_set_size_type(flash_size);
  196. break;
  197. case 16 * 1024 * 1024:
  198. // 128Mbit, 16MByte
  199. flash_size = SIZE_128MBIT;
  200. flash_rom_set_size_type(flash_size);
  201. break;
  202. default:
  203. // Unknown flash size.
  204. result = false;
  205. break;
  206. }
  207. return result;
  208. }
  209. uint16_t flash_rom_get_sec_num(void)
  210. {
  211. //static uint16_t sec_num = 0;
  212. // return flash_rom_get_size_byte() / (SPI_FLASH_SEC_SIZE);
  213. // c_printf("\nflash_rom_get_size_byte()=%d\n", ( flash_rom_get_size_byte() / (SPI_FLASH_SEC_SIZE) ));
  214. // if( sec_num == 0 )
  215. //{
  216. // sec_num = 4 * 1024 * 1024 / (SPI_FLASH_SEC_SIZE);
  217. //}
  218. //return sec_num;
  219. return ( flash_rom_get_size_byte() / (SPI_FLASH_SEC_SIZE) );
  220. }
  221. uint8_t flash_rom_get_mode(void)
  222. {
  223. SPIFlashInfo spi_flash_info = flash_rom_getinfo();
  224. switch (spi_flash_info.mode)
  225. {
  226. // Reserved for future use
  227. case MODE_QIO:
  228. break;
  229. case MODE_QOUT:
  230. break;
  231. case MODE_DIO:
  232. break;
  233. case MODE_DOUT:
  234. break;
  235. }
  236. return spi_flash_info.mode;
  237. }
  238. uint32_t flash_rom_get_speed(void)
  239. {
  240. uint32_t speed = 0;
  241. SPIFlashInfo spi_flash_info = flash_rom_getinfo();
  242. switch (spi_flash_info.speed)
  243. {
  244. case SPEED_40MHZ:
  245. // 40MHz
  246. speed = 40000000;
  247. break;
  248. case SPEED_26MHZ:
  249. //26.7MHz
  250. speed = 26700000;
  251. break;
  252. case SPEED_20MHZ:
  253. // 20MHz
  254. speed = 20000000;
  255. break;
  256. case SPEED_80MHZ:
  257. //80MHz
  258. speed = 80000000;
  259. break;
  260. }
  261. return speed;
  262. }
  263. bool flash_rom_set_speed(uint32_t speed)
  264. {
  265. // Dangerous, here are dinosaur infested!!!!!
  266. // Reboot required!!!
  267. // If you don't know what you're doing, your nodemcu may turn into stone ...
  268. NODE_DBG("\nBEGIN SET FLASH HEADER\n");
  269. uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR;
  270. uint8_t speed_type = SPEED_40MHZ;
  271. if (speed < 26700000)
  272. {
  273. speed_type = SPEED_20MHZ;
  274. }
  275. else if (speed < 40000000)
  276. {
  277. speed_type = SPEED_26MHZ;
  278. }
  279. else if (speed < 80000000)
  280. {
  281. speed_type = SPEED_40MHZ;
  282. }
  283. else if (speed >= 80000000)
  284. {
  285. speed_type = SPEED_80MHZ;
  286. }
  287. if (SPI_FLASH_RESULT_OK == spi_flash_read(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
  288. {
  289. ((SPIFlashInfo *)(&data[0]))->speed = speed_type;
  290. if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(0 * SPI_FLASH_SEC_SIZE))
  291. {
  292. NODE_DBG("\nERASE SUCCESS\n");
  293. }
  294. if (SPI_FLASH_RESULT_OK == spi_flash_write(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
  295. {
  296. NODE_DBG("\nWRITE SUCCESS, %u\n", speed_type);
  297. }
  298. }
  299. NODE_DBG("\nEND SET FLASH HEADER\n");
  300. return true;
  301. }
  302. bool flash_init_data_written(void)
  303. {
  304. // FLASH SEC - 4
  305. uint32_t data[2] ICACHE_STORE_ATTR;
  306. #if defined(FLASH_SAFE_API)
  307. if (SPI_FLASH_RESULT_OK == flash_safe_read((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
  308. #else
  309. if (SPI_FLASH_RESULT_OK == spi_flash_read((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
  310. #endif // defined(FLASH_SAFE_API)
  311. {
  312. if (data[0] == 0xFFFFFFFF && data[1] == 0xFFFFFFFF)
  313. {
  314. return false;
  315. }
  316. }
  317. return true;
  318. }
  319. bool flash_init_data_default(void)
  320. {
  321. // FLASH SEC - 4
  322. // Dangerous, here are dinosaur infested!!!!!
  323. // Reboot required!!!
  324. // It will init system data to default!
  325. bool result = false;
  326. #if defined(FLASH_SAFE_API)
  327. if (SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_safe_get_sec_num() - 4)))
  328. {
  329. if (SPI_FLASH_RESULT_OK == flash_safe_write((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)flash_init_data, 128))
  330. {
  331. result = true;
  332. }
  333. }
  334. #else
  335. if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_rom_get_sec_num() - 4)))
  336. {
  337. if (SPI_FLASH_RESULT_OK == spi_flash_write((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)flash_init_data, 128))
  338. {
  339. result = true;
  340. }
  341. }
  342. #endif // defined(FLASH_SAFE_API)
  343. return result;
  344. }
  345. bool flash_init_data_blank(void)
  346. {
  347. // FLASH SEC - 2
  348. // Dangerous, here are dinosaur infested!!!!!
  349. // Reboot required!!!
  350. // It will init system config to blank!
  351. bool result = false;
  352. #if defined(FLASH_SAFE_API)
  353. if ((SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_safe_get_sec_num() - 2))) &&
  354. (SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_safe_get_sec_num() - 1))))
  355. #else
  356. if ((SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_rom_get_sec_num() - 2))) &&
  357. (SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_rom_get_sec_num() - 1))))
  358. #endif // defined(FLASH_SAFE_API)
  359. {
  360. result = true;
  361. }
  362. return result ;
  363. }
  364. bool flash_self_destruct(void)
  365. {
  366. // Dangerous, Erase your flash. Good bye!
  367. SPIEraseChip();
  368. return true;
  369. }
  370. uint8_t byte_of_aligned_array(const uint8_t *aligned_array, uint32_t index)
  371. {
  372. if ( (((uint32_t)aligned_array) % 4) != 0 )
  373. {
  374. NODE_DBG("aligned_array is not 4-byte aligned.\n");
  375. return 0;
  376. }
  377. volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ];
  378. uint8_t *p = (uint8_t *) (&v);
  379. return p[ (index % 4) ];
  380. }
  381. uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index)
  382. {
  383. if ( (((uint32_t)aligned_array) % 4) != 0 )
  384. {
  385. NODE_DBG("aligned_array is not 4-byte aligned.\n");
  386. return 0;
  387. }
  388. volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 2 ];
  389. uint16_t *p = (uint16_t *) (&v);
  390. return (index % 2 == 0) ? p[ 0 ] : p[ 1 ];
  391. // return p[ (index % 2) ]; // -- why error???
  392. // (byte_of_aligned_array((uint8_t *)aligned_array, index * 2 + 1) << 8) | byte_of_aligned_array((uint8_t *)aligned_array, index * 2);
  393. }
  394. // uint8_t flash_rom_get_checksum(void)
  395. // {
  396. // // SPIFlashInfo spi_flash_info ICACHE_STORE_ATTR = flash_rom_getinfo();
  397. // // uint32_t address = sizeof(spi_flash_info) + spi_flash_info.segment_size;
  398. // // uint32_t address_aligned_4bytes = (address + 3) & 0xFFFFFFFC;
  399. // // uint8_t buffer[64] = {0};
  400. // // spi_flash_read(address, (uint32 *) buffer, 64);
  401. // // uint8_t i = 0;
  402. // // c_printf("\nBEGIN DUMP\n");
  403. // // for (i = 0; i < 64; i++)
  404. // // {
  405. // // c_printf("%02x," , buffer[i]);
  406. // // }
  407. // // i = (address + 0x10) & 0x10 - 1;
  408. // // c_printf("\nSIZE:%d CHECK SUM:%02x\n", spi_flash_info.segment_size, buffer[i]);
  409. // // c_printf("\nEND DUMP\n");
  410. // // return buffer[0];
  411. // return 0;
  412. // }
  413. // uint8_t flash_rom_calc_checksum(void)
  414. // {
  415. // return 0;
  416. // }