fal_flash_stm32f4.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <fal.h>
  7. #include <stm32f4xx_hal.h>
  8. /* Base address of the Flash sectors Bank 1 */
  9. #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
  10. #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
  11. #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
  12. #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
  13. #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
  14. #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
  15. #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
  16. #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
  17. #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
  18. #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
  19. #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
  20. #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
  21. /* Base address of the Flash sectors Bank 2 */
  22. #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base @ of Sector 0, 16 Kbytes */
  23. #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base @ of Sector 1, 16 Kbytes */
  24. #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base @ of Sector 2, 16 Kbytes */
  25. #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base @ of Sector 3, 16 Kbytes */
  26. #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base @ of Sector 4, 64 Kbytes */
  27. #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base @ of Sector 5, 128 Kbytes */
  28. #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base @ of Sector 6, 128 Kbytes */
  29. #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base @ of Sector 7, 128 Kbytes */
  30. #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base @ of Sector 8, 128 Kbytes */
  31. #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base @ of Sector 9, 128 Kbytes */
  32. #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base @ of Sector 10, 128 Kbytes */
  33. #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base @ of Sector 11, 128 Kbytes */
  34. /**
  35. * @brief Gets the sector of a given address
  36. * @param None
  37. * @retval The sector of a given address
  38. */
  39. static uint32_t stm32_get_sector(uint32_t addr)
  40. {
  41. uint32_t sector = 0;
  42. if((addr < ADDR_FLASH_SECTOR_1) && (addr >= ADDR_FLASH_SECTOR_0))
  43. {
  44. sector = FLASH_SECTOR_0;
  45. }
  46. else if((addr < ADDR_FLASH_SECTOR_2) && (addr >= ADDR_FLASH_SECTOR_1))
  47. {
  48. sector = FLASH_SECTOR_1;
  49. }
  50. else if((addr < ADDR_FLASH_SECTOR_3) && (addr >= ADDR_FLASH_SECTOR_2))
  51. {
  52. sector = FLASH_SECTOR_2;
  53. }
  54. else if((addr < ADDR_FLASH_SECTOR_4) && (addr >= ADDR_FLASH_SECTOR_3))
  55. {
  56. sector = FLASH_SECTOR_3;
  57. }
  58. else if((addr < ADDR_FLASH_SECTOR_5) && (addr >= ADDR_FLASH_SECTOR_4))
  59. {
  60. sector = FLASH_SECTOR_4;
  61. }
  62. else if((addr < ADDR_FLASH_SECTOR_6) && (addr >= ADDR_FLASH_SECTOR_5))
  63. {
  64. sector = FLASH_SECTOR_5;
  65. }
  66. else if((addr < ADDR_FLASH_SECTOR_7) && (addr >= ADDR_FLASH_SECTOR_6))
  67. {
  68. sector = FLASH_SECTOR_6;
  69. }
  70. else if((addr < ADDR_FLASH_SECTOR_8) && (addr >= ADDR_FLASH_SECTOR_7))
  71. {
  72. sector = FLASH_SECTOR_7;
  73. }
  74. #if defined(FLASH_SECTOR_8)
  75. else if((addr < ADDR_FLASH_SECTOR_9) && (addr >= ADDR_FLASH_SECTOR_8))
  76. {
  77. sector = FLASH_SECTOR_8;
  78. }
  79. #endif
  80. #if defined(FLASH_SECTOR_9)
  81. else if((addr < ADDR_FLASH_SECTOR_10) && (addr >= ADDR_FLASH_SECTOR_9))
  82. {
  83. sector = FLASH_SECTOR_9;
  84. }
  85. #endif
  86. #if defined(FLASH_SECTOR_10)
  87. else if((addr < ADDR_FLASH_SECTOR_11) && (addr >= ADDR_FLASH_SECTOR_10))
  88. {
  89. sector = FLASH_SECTOR_10;
  90. }
  91. #endif
  92. #if defined(FLASH_SECTOR_11)
  93. else if((addr < ADDR_FLASH_SECTOR_12) && (addr >= ADDR_FLASH_SECTOR_11))
  94. {
  95. sector = FLASH_SECTOR_11;
  96. }
  97. #endif
  98. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  99. else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
  100. {
  101. sector = FLASH_SECTOR_12;
  102. }
  103. else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
  104. {
  105. sector = FLASH_SECTOR_13;
  106. }
  107. else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
  108. {
  109. sector = FLASH_SECTOR_14;
  110. }
  111. else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
  112. {
  113. sector = FLASH_SECTOR_15;
  114. }
  115. else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
  116. {
  117. sector = FLASH_SECTOR_16;
  118. }
  119. else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
  120. {
  121. sector = FLASH_SECTOR_17;
  122. }
  123. else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
  124. {
  125. sector = FLASH_SECTOR_18;
  126. }
  127. else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
  128. {
  129. sector = FLASH_SECTOR_19;
  130. }
  131. else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
  132. {
  133. sector = FLASH_SECTOR_20;
  134. }
  135. else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
  136. {
  137. sector = FLASH_SECTOR_21;
  138. }
  139. else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
  140. {
  141. sector = FLASH_SECTOR_22;
  142. }
  143. else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_23) */
  144. {
  145. sector = FLASH_SECTOR_23;
  146. }
  147. #endif
  148. return sector;
  149. }
  150. static int read(long offset, uint8_t *buf, size_t size)
  151. {
  152. size_t i;
  153. long addr = stm32_onchip_flash.addr + offset;
  154. for (i = 0; i < size; i++, buf++, addr++)
  155. {
  156. *buf = *(uint8_t *) addr;
  157. }
  158. return size;
  159. }
  160. static int write(long offset, const uint8_t *buf, size_t size)
  161. {
  162. long addr = stm32_onchip_flash.addr + offset;
  163. HAL_FLASH_Unlock();
  164. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  165. for (size_t i = 0; i < size; i++, addr++, buf++)
  166. {
  167. /* write data to flash */
  168. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (uint64_t)(*buf)) == HAL_OK)
  169. {
  170. if (*(uint8_t *) addr != *buf)
  171. {
  172. return -1;
  173. }
  174. }
  175. else
  176. {
  177. return -1;
  178. }
  179. }
  180. HAL_FLASH_Lock();
  181. return size;
  182. }
  183. static int erase(long offset, size_t size)
  184. {
  185. uint32_t FirstSector = 0, NbOfSectors = 0;
  186. uint32_t SECTORError = 0;
  187. /*Variable used for Erase procedure*/
  188. FLASH_EraseInitTypeDef EraseInitStruct;
  189. long addr = stm32_onchip_flash.addr + offset;
  190. /* Unlock the Flash to enable the flash control register access */
  191. HAL_FLASH_Unlock();
  192. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  193. /* Get the 1st sector to erase */
  194. FirstSector = stm32_get_sector(addr);
  195. /* Get the number of sector to erase from 1st sector*/
  196. NbOfSectors = stm32_get_sector(addr + size - 1) - FirstSector + 1;
  197. /* Fill EraseInit structure*/
  198. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  199. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  200. EraseInitStruct.Sector = FirstSector;
  201. EraseInitStruct.NbSectors = NbOfSectors;
  202. if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *) &SECTORError) != HAL_OK)
  203. {
  204. return -1;
  205. }
  206. HAL_FLASH_Lock();
  207. return size;
  208. }
  209. const struct fal_flash_dev stm32_onchip_flash =
  210. {
  211. .name = "stm32_onchip",
  212. .addr = 0x08000000,
  213. .len = 1024*1024,
  214. .blk_size = 128*1024,
  215. .ops = {NULL, read, write, erase},
  216. .write_gran = 8
  217. };