flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mpconfig.h"
  27. #include "py/misc.h"
  28. #include "flash.h"
  29. typedef struct {
  30. uint32_t base_address;
  31. uint32_t sector_size;
  32. uint32_t sector_count;
  33. } flash_layout_t;
  34. #if defined(STM32F0)
  35. static const flash_layout_t flash_layout[] = {
  36. { FLASH_BASE, FLASH_PAGE_SIZE, (FLASH_BANK1_END + 1 - FLASH_BASE) / FLASH_PAGE_SIZE },
  37. };
  38. #elif defined(STM32F4)
  39. static const flash_layout_t flash_layout[] = {
  40. { 0x08000000, 0x04000, 4 },
  41. { 0x08010000, 0x10000, 1 },
  42. { 0x08020000, 0x20000, 3 },
  43. #if defined(FLASH_SECTOR_8)
  44. { 0x08080000, 0x20000, 4 },
  45. #endif
  46. #if defined(FLASH_SECTOR_12)
  47. { 0x08100000, 0x04000, 4 },
  48. { 0x08110000, 0x10000, 1 },
  49. { 0x08120000, 0x20000, 7 },
  50. #endif
  51. };
  52. #elif defined(STM32F7)
  53. // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
  54. // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F7
  55. #define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
  56. static const flash_layout_t flash_layout[] = {
  57. { 0x08000000, 0x08000, 4 },
  58. { 0x08020000, 0x20000, 1 },
  59. { 0x08040000, 0x40000, 3 },
  60. };
  61. #elif defined(STM32L4)
  62. static const flash_layout_t flash_layout[] = {
  63. { (uint32_t)FLASH_BASE, (uint32_t)FLASH_PAGE_SIZE, 512 },
  64. };
  65. #elif defined(STM32H7)
  66. static const flash_layout_t flash_layout[] = {
  67. { 0x08000000, 0x20000, 16 },
  68. };
  69. #else
  70. #error Unsupported processor
  71. #endif
  72. #if defined(STM32L4) || defined(STM32H7)
  73. // get the bank of a given flash address
  74. static uint32_t get_bank(uint32_t addr) {
  75. #if defined(STM32H7)
  76. if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_SWAP_BANK) == 0) {
  77. #else
  78. if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0) {
  79. #endif
  80. // no bank swap
  81. if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
  82. return FLASH_BANK_1;
  83. } else {
  84. return FLASH_BANK_2;
  85. }
  86. } else {
  87. // bank swap
  88. if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
  89. return FLASH_BANK_2;
  90. } else {
  91. return FLASH_BANK_1;
  92. }
  93. }
  94. }
  95. #if defined(STM32L4)
  96. // get the page of a given flash address
  97. static uint32_t get_page(uint32_t addr) {
  98. if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
  99. // bank 1
  100. return (addr - FLASH_BASE) / FLASH_PAGE_SIZE;
  101. } else {
  102. // bank 2
  103. return (addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
  104. }
  105. }
  106. #endif
  107. #endif
  108. uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
  109. if (addr >= flash_layout[0].base_address) {
  110. uint32_t sector_index = 0;
  111. for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) {
  112. for (int j = 0; j < flash_layout[i].sector_count; ++j) {
  113. uint32_t sector_start_next = flash_layout[i].base_address
  114. + (j + 1) * flash_layout[i].sector_size;
  115. if (addr < sector_start_next) {
  116. if (start_addr != NULL) {
  117. *start_addr = flash_layout[i].base_address
  118. + j * flash_layout[i].sector_size;
  119. }
  120. if (size != NULL) {
  121. *size = flash_layout[i].sector_size;
  122. }
  123. return sector_index;
  124. }
  125. ++sector_index;
  126. }
  127. }
  128. }
  129. return 0;
  130. }
  131. void flash_erase(uint32_t flash_dest, uint32_t num_word32) {
  132. // check there is something to write
  133. if (num_word32 == 0) {
  134. return;
  135. }
  136. // unlock
  137. HAL_FLASH_Unlock();
  138. FLASH_EraseInitTypeDef EraseInitStruct;
  139. #if defined(STM32F0)
  140. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
  141. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  142. EraseInitStruct.PageAddress = flash_dest;
  143. EraseInitStruct.NbPages = (4 * num_word32 + FLASH_PAGE_SIZE - 4) / FLASH_PAGE_SIZE;
  144. #elif defined(STM32L4)
  145. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  146. // erase the sector(s)
  147. // The sector returned by flash_get_sector_info can not be used
  148. // as the flash has on each bank 0/1 pages 0..255
  149. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  150. EraseInitStruct.Banks = get_bank(flash_dest);
  151. EraseInitStruct.Page = get_page(flash_dest);
  152. EraseInitStruct.NbPages = get_page(flash_dest + 4 * num_word32 - 1) - EraseInitStruct.Page + 1;;
  153. #else
  154. // Clear pending flags (if any)
  155. #if defined(STM32H7)
  156. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2);
  157. #else
  158. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  159. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  160. #endif
  161. // erase the sector(s)
  162. EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
  163. EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
  164. #if defined(STM32H7)
  165. EraseInitStruct.Banks = get_bank(flash_dest);
  166. #endif
  167. EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
  168. EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
  169. #endif
  170. uint32_t SectorError = 0;
  171. if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
  172. // error occurred during sector erase
  173. HAL_FLASH_Lock(); // lock the flash
  174. return;
  175. }
  176. }
  177. /*
  178. // erase the sector using an interrupt
  179. void flash_erase_it(uint32_t flash_dest, uint32_t num_word32) {
  180. // check there is something to write
  181. if (num_word32 == 0) {
  182. return;
  183. }
  184. // unlock
  185. HAL_FLASH_Unlock();
  186. // Clear pending flags (if any)
  187. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  188. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
  189. // erase the sector(s)
  190. FLASH_EraseInitTypeDef EraseInitStruct;
  191. EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
  192. EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
  193. EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
  194. EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
  195. if (HAL_FLASHEx_Erase_IT(&EraseInitStruct) != HAL_OK) {
  196. // error occurred during sector erase
  197. HAL_FLASH_Lock(); // lock the flash
  198. return;
  199. }
  200. }
  201. */
  202. void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
  203. #if defined(STM32L4)
  204. // program the flash uint64 by uint64
  205. for (int i = 0; i < num_word32 / 2; i++) {
  206. uint64_t val = *(uint64_t*)src;
  207. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val) != HAL_OK) {
  208. // error occurred during flash write
  209. HAL_FLASH_Lock(); // lock the flash
  210. return;
  211. }
  212. flash_dest += 8;
  213. src += 2;
  214. }
  215. if ((num_word32 & 0x01) == 1) {
  216. uint64_t val = *(uint64_t*)flash_dest;
  217. val = (val & 0xffffffff00000000uL) | (*src);
  218. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val) != HAL_OK) {
  219. // error occurred during flash write
  220. HAL_FLASH_Lock(); // lock the flash
  221. return;
  222. }
  223. }
  224. #elif defined(STM32H7)
  225. // program the flash 256 bits at a time
  226. for (int i = 0; i < num_word32 / 8; i++) {
  227. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_dest, (uint64_t)(uint32_t)src) != HAL_OK) {
  228. // error occurred during flash write
  229. HAL_FLASH_Lock(); // lock the flash
  230. return;
  231. }
  232. flash_dest += 32;
  233. src += 8;
  234. }
  235. #else
  236. // program the flash word by word
  237. for (int i = 0; i < num_word32; i++) {
  238. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flash_dest, *src) != HAL_OK) {
  239. // error occurred during flash write
  240. HAL_FLASH_Lock(); // lock the flash
  241. return;
  242. }
  243. flash_dest += 4;
  244. src += 1;
  245. }
  246. #endif
  247. // lock the flash
  248. HAL_FLASH_Lock();
  249. }
  250. /*
  251. use erase, then write
  252. void flash_erase_and_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
  253. // check there is something to write
  254. if (num_word32 == 0) {
  255. return;
  256. }
  257. // unlock
  258. HAL_FLASH_Unlock();
  259. // Clear pending flags (if any)
  260. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  261. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
  262. // erase the sector(s)
  263. FLASH_EraseInitTypeDef EraseInitStruct;
  264. EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
  265. EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
  266. EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
  267. EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
  268. uint32_t SectorError = 0;
  269. if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
  270. // error occurred during sector erase
  271. HAL_FLASH_Lock(); // lock the flash
  272. return;
  273. }
  274. // program the flash word by word
  275. for (int i = 0; i < num_word32; i++) {
  276. if (HAL_FLASH_Program(TYPEPROGRAM_WORD, flash_dest, *src) != HAL_OK) {
  277. // error occurred during flash write
  278. HAL_FLASH_Lock(); // lock the flash
  279. return;
  280. }
  281. flash_dest += 4;
  282. src += 1;
  283. }
  284. // lock the flash
  285. HAL_FLASH_Lock();
  286. }
  287. */