HAL_flash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. ******************************************************************************
  3. * @file HAL_flash.h
  4. * @author AE Team
  5. * @version V1.1.0
  6. * @date 28/08/2019
  7. * @brief This file contains all the functions prototypes for the FLASH
  8. * firmware library.
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __HAL_FLASH_H
  23. #define __HAL_FLASH_H
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "HAL_device.h"
  26. /** @addtogroup StdPeriph_Driver
  27. * @{
  28. */
  29. /** @addtogroup FLASH
  30. * @{
  31. */
  32. /** @defgroup FLASH_Exported_Types
  33. * @{
  34. */
  35. /**
  36. * @brief FLASH Status
  37. */
  38. typedef enum
  39. {
  40. FLASH_BUSY = 1,
  41. FLASH_ERROR_PG,
  42. FLASH_ERROR_WRP,
  43. FLASH_COMPLETE,
  44. FLASH_TIMEOUT
  45. } FLASH_Status;
  46. /**
  47. * @}
  48. */
  49. /** @defgroup FLASH_Exported_Constants
  50. * @{
  51. */
  52. /** @defgroup Flash_Latency
  53. * @{
  54. */
  55. #define FLASH_Latency_0 ((uint32_t)0x00000000) /* FLASH Zero Latency cycle */
  56. #define FLASH_Latency_1 ((uint32_t)0x00000001) /* FLASH One Latency cycle */
  57. #define FLASH_Latency_2 ((uint32_t)0x00000002) /* FLASH Two Latency cycles */
  58. #define FLASH_Latency_3 ((uint32_t)0x00000003) /* FLASH Three Latency cycles */
  59. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
  60. ((LATENCY) == FLASH_Latency_1) || \
  61. ((LATENCY) == FLASH_Latency_2) || \
  62. ((LATENCY) == FLASH_Latency_3))
  63. /**
  64. * @}
  65. */
  66. /** @defgroup Half_Cycle_Enable_Disable
  67. * @{
  68. */
  69. #define FLASH_HalfCycleAccess_Enable ((uint32_t)0x00000008) /* FLASH Half Cycle Enable */
  70. #define FLASH_HalfCycleAccess_Disable ((uint32_t)0x00000000) /* FLASH Half Cycle Disable */
  71. #define IS_FLASH_HALFCYCLEACCESS_STATE(STATE) (((STATE) == FLASH_HalfCycleAccess_Enable) || \
  72. ((STATE) == FLASH_HalfCycleAccess_Disable))
  73. /**
  74. * @}
  75. */
  76. /** @defgroup Prefetch_Buffer_Enable_Disable
  77. * @{
  78. */
  79. #define FLASH_PrefetchBuffer_Enable ((uint32_t)0x00000010) /* FLASH Prefetch Buffer Enable */
  80. #define FLASH_PrefetchBuffer_Disable ((uint32_t)0x00000000) /* FLASH Prefetch Buffer Disable */
  81. #define IS_FLASH_PREFETCHBUFFER_STATE(STATE) (((STATE) == FLASH_PrefetchBuffer_Enable) || \
  82. ((STATE) == FLASH_PrefetchBuffer_Disable))
  83. /**
  84. * @}
  85. */
  86. /** @defgroup Option_Bytes_Write_Protection
  87. * @{
  88. */
  89. /* Values to be used with microcontroller Medium-density devices: FLASH memory density
  90. ranges between 32 and 128 Kbytes with page size equal to 1 Kbytes */
  91. #define FLASH_WRProt_Pages0to3 ((uint32_t)0x00000001) /* Write protection of page 0 to 3 */
  92. #define FLASH_WRProt_Pages4to7 ((uint32_t)0x00000002) /* Write protection of page 4 to 7 */
  93. #define FLASH_WRProt_Pages8to11 ((uint32_t)0x00000004) /* Write protection of page 8 to 11 */
  94. #define FLASH_WRProt_Pages12to15 ((uint32_t)0x00000008) /* Write protection of page 12 to 15 */
  95. #define FLASH_WRProt_Pages16to19 ((uint32_t)0x00000010) /* Write protection of page 16 to 19 */
  96. #define FLASH_WRProt_Pages20to23 ((uint32_t)0x00000020) /* Write protection of page 20 to 23 */
  97. #define FLASH_WRProt_Pages24to27 ((uint32_t)0x00000040) /* Write protection of page 24 to 27 */
  98. #define FLASH_WRProt_Pages28to31 ((uint32_t)0x00000080) /* Write protection of page 28 to 31 */
  99. #define FLASH_WRProt_Pages32to35 ((uint32_t)0x00000100) /* Write protection of page 32 to 35 */
  100. #define FLASH_WRProt_Pages36to39 ((uint32_t)0x00000200) /* Write protection of page 36 to 39 */
  101. #define FLASH_WRProt_Pages40to43 ((uint32_t)0x00000400) /* Write protection of page 40 to 43 */
  102. #define FLASH_WRProt_Pages44to47 ((uint32_t)0x00000800) /* Write protection of page 44 to 47 */
  103. #define FLASH_WRProt_Pages48to51 ((uint32_t)0x00001000) /* Write protection of page 48 to 51 */
  104. #define FLASH_WRProt_Pages52to55 ((uint32_t)0x00002000) /* Write protection of page 52 to 55 */
  105. #define FLASH_WRProt_Pages56to59 ((uint32_t)0x00004000) /* Write protection of page 56 to 59 */
  106. #define FLASH_WRProt_Pages60to63 ((uint32_t)0x00008000) /* Write protection of page 60 to 63 */
  107. #define FLASH_WRProt_Pages64to67 ((uint32_t)0x00010000) /* Write protection of page 64 to 67 */
  108. #define FLASH_WRProt_Pages68to71 ((uint32_t)0x00020000) /* Write protection of page 68 to 71 */
  109. #define FLASH_WRProt_Pages72to75 ((uint32_t)0x00040000) /* Write protection of page 72 to 75 */
  110. #define FLASH_WRProt_Pages76to79 ((uint32_t)0x00080000) /* Write protection of page 76 to 79 */
  111. #define FLASH_WRProt_Pages80to83 ((uint32_t)0x00100000) /* Write protection of page 80 to 83 */
  112. #define FLASH_WRProt_Pages84to87 ((uint32_t)0x00200000) /* Write protection of page 84 to 87 */
  113. #define FLASH_WRProt_Pages88to91 ((uint32_t)0x00400000) /* Write protection of page 88 to 91 */
  114. #define FLASH_WRProt_Pages92to95 ((uint32_t)0x00800000) /* Write protection of page 92 to 95 */
  115. #define FLASH_WRProt_Pages96to99 ((uint32_t)0x01000000) /* Write protection of page 96 to 99 */
  116. #define FLASH_WRProt_Pages100to103 ((uint32_t)0x02000000) /* Write protection of page 100 to 103 */
  117. #define FLASH_WRProt_Pages104to107 ((uint32_t)0x04000000) /* Write protection of page 104 to 107 */
  118. #define FLASH_WRProt_Pages108to111 ((uint32_t)0x08000000) /* Write protection of page 108 to 111 */
  119. #define FLASH_WRProt_Pages112to115 ((uint32_t)0x10000000) /* Write protection of page 112 to 115 */
  120. #define FLASH_WRProt_Pages116to119 ((uint32_t)0x20000000) /* Write protection of page 115 to 119 */
  121. #define FLASH_WRProt_Pages120to123 ((uint32_t)0x40000000) /* Write protection of page 120 to 123 */
  122. #define FLASH_WRProt_Pages124to127 ((uint32_t)0x80000000) /* Write protection of page 124 to 127 */
  123. /* Values to be used with microcontroller High-density devices: FLASH memory density
  124. ranges between 256 and 512 Kbytes with page size equal to 2 Kbytes */
  125. #define FLASH_WRProt_Pages0to1 ((uint32_t)0x00000001) /* Write protection of page 0 to 1 */
  126. #define FLASH_WRProt_Pages2to3 ((uint32_t)0x00000002) /* Write protection of page 2 to 3 */
  127. #define FLASH_WRProt_Pages4to5 ((uint32_t)0x00000004) /* Write protection of page 4 to 5 */
  128. #define FLASH_WRProt_Pages6to7 ((uint32_t)0x00000008) /* Write protection of page 6 to 7 */
  129. #define FLASH_WRProt_Pages8to9 ((uint32_t)0x00000010) /* Write protection of page 8 to 9 */
  130. #define FLASH_WRProt_Pages10to11 ((uint32_t)0x00000020) /* Write protection of page 10 to 11 */
  131. #define FLASH_WRProt_Pages12to13 ((uint32_t)0x00000040) /* Write protection of page 12 to 13 */
  132. #define FLASH_WRProt_Pages14to15 ((uint32_t)0x00000080) /* Write protection of page 14 to 15 */
  133. #define FLASH_WRProt_Pages16to17 ((uint32_t)0x00000100) /* Write protection of page 16 to 17 */
  134. #define FLASH_WRProt_Pages18to19 ((uint32_t)0x00000200) /* Write protection of page 18 to 19 */
  135. #define FLASH_WRProt_Pages20to21 ((uint32_t)0x00000400) /* Write protection of page 20 to 21 */
  136. #define FLASH_WRProt_Pages22to23 ((uint32_t)0x00000800) /* Write protection of page 22 to 23 */
  137. #define FLASH_WRProt_Pages24to25 ((uint32_t)0x00001000) /* Write protection of page 24 to 25 */
  138. #define FLASH_WRProt_Pages26to27 ((uint32_t)0x00002000) /* Write protection of page 26 to 27 */
  139. #define FLASH_WRProt_Pages28to29 ((uint32_t)0x00004000) /* Write protection of page 28 to 29 */
  140. #define FLASH_WRProt_Pages30to31 ((uint32_t)0x00008000) /* Write protection of page 30 to 31 */
  141. #define FLASH_WRProt_Pages32to33 ((uint32_t)0x00010000) /* Write protection of page 32 to 33 */
  142. #define FLASH_WRProt_Pages34to35 ((uint32_t)0x00020000) /* Write protection of page 34 to 35 */
  143. #define FLASH_WRProt_Pages36to37 ((uint32_t)0x00040000) /* Write protection of page 36 to 37 */
  144. #define FLASH_WRProt_Pages38to39 ((uint32_t)0x00080000) /* Write protection of page 38 to 39 */
  145. #define FLASH_WRProt_Pages40to41 ((uint32_t)0x00100000) /* Write protection of page 40 to 41 */
  146. #define FLASH_WRProt_Pages42to43 ((uint32_t)0x00200000) /* Write protection of page 42 to 43 */
  147. #define FLASH_WRProt_Pages44to45 ((uint32_t)0x00400000) /* Write protection of page 44 to 45 */
  148. #define FLASH_WRProt_Pages46to47 ((uint32_t)0x00800000) /* Write protection of page 46 to 47 */
  149. #define FLASH_WRProt_Pages48to49 ((uint32_t)0x01000000) /* Write protection of page 48 to 49 */
  150. #define FLASH_WRProt_Pages50to51 ((uint32_t)0x02000000) /* Write protection of page 50 to 51 */
  151. #define FLASH_WRProt_Pages52to53 ((uint32_t)0x04000000) /* Write protection of page 52 to 53 */
  152. #define FLASH_WRProt_Pages54to55 ((uint32_t)0x08000000) /* Write protection of page 54 to 55 */
  153. #define FLASH_WRProt_Pages56to57 ((uint32_t)0x10000000) /* Write protection of page 56 to 57 */
  154. #define FLASH_WRProt_Pages58to59 ((uint32_t)0x20000000) /* Write protection of page 58 to 59 */
  155. #define FLASH_WRProt_Pages60to61 ((uint32_t)0x40000000) /* Write protection of page 60 to 61 */
  156. #define FLASH_WRProt_Pages62to255 ((uint32_t)0x80000000) /* Write protection of page 62 to 255 */
  157. #define FLASH_WRProt_AllPages ((uint32_t)0xFFFFFFFF) /* Write protection of all Pages */
  158. #define IS_FLASH_WRPROT_PAGE(PAGE) (((PAGE) != 0x00000000))
  159. #define IS_FLASH_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x0807FFFF))
  160. #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804) || ((ADDRESS) == 0x1FFFF806))
  161. /**
  162. * @}
  163. */
  164. /** @defgroup Option_Bytes_IWatchdog
  165. * @{
  166. */
  167. #define OB_IWDG_SW ((uint16_t)0x0001) /* Software IWDG selected */
  168. #define OB_IWDG_HW ((uint16_t)0x0000) /* Hardware IWDG selected */
  169. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  170. /**
  171. * @}
  172. */
  173. /** @defgroup Option_Bytes_nRST_STOP
  174. * @{
  175. */
  176. #define OB_STOP_NoRST ((uint16_t)0x0002) /* No reset generated when entering in STOP */
  177. #define OB_STOP_RST ((uint16_t)0x0000) /* Reset generated when entering in STOP */
  178. #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
  179. /**
  180. * @}
  181. */
  182. /** @defgroup Option_Bytes_nRST_STDBY
  183. * @{
  184. */
  185. #define OB_STDBY_NoRST ((uint16_t)0x0004) /* No reset generated when entering in STANDBY */
  186. #define OB_STDBY_RST ((uint16_t)0x0000) /* Reset generated when entering in STANDBY */
  187. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
  188. /**
  189. * @}
  190. */
  191. /** @defgroup FLASH_Interrupts
  192. * @{
  193. */
  194. #define FLASH_IT_ERROR ((uint32_t)0x00000400) /* FPEC error interrupt source */
  195. #define FLASH_IT_EOP ((uint32_t)0x00001000) /* End of FLASH Operation Interrupt source */
  196. #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
  197. /**
  198. * @}
  199. */
  200. /** @defgroup FLASH_Flags
  201. * @{
  202. */
  203. #define FLASH_FLAG_BSY ((uint32_t)0x00000001) /* FLASH Busy flag */
  204. #define FLASH_FLAG_EOP ((uint32_t)0x00000020) /* FLASH End of Operation flag */
  205. #define FLASH_FLAG_PGERR ((uint32_t)0x00000004) /* FLASH Program error flag */
  206. #define FLASH_FLAG_WRPRTERR ((uint32_t)0x00000010) /* FLASH Write protected error flag */
  207. #define FLASH_FLAG_OPTERR ((uint32_t)0x00000001) /* FLASH Option Byte error flag */
  208. #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFCA) == 0x00000000) && ((FLAG) != 0x00000000))
  209. #define IS_FLASH_GET_FLAG(FLAG) (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_EOP) || \
  210. ((FLAG) == FLASH_FLAG_PGERR) || ((FLAG) == FLASH_FLAG_WRPRTERR) || \
  211. ((FLAG) == FLASH_FLAG_OPTERR))
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @}
  217. */
  218. /** @defgroup FLASH_Exported_Macros
  219. * @{
  220. */
  221. /**
  222. * @}
  223. */
  224. /** @defgroup FLASH_Exported_Functions
  225. * @{
  226. */
  227. void FLASH_SetLatency(uint32_t FLASH_Latency);
  228. void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);
  229. void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer);
  230. void FLASH_Unlock(void);
  231. void FLASH_Lock(void);
  232. FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
  233. FLASH_Status FLASH_EraseAllPages(void);
  234. FLASH_Status FLASH_EraseOptionBytes(void);
  235. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  236. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  237. FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data);
  238. FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages);
  239. FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState);
  240. FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY);
  241. uint32_t FLASH_GetUserOptionByte(void);
  242. uint32_t FLASH_GetWriteProtectionOptionByte(void);
  243. FlagStatus FLASH_GetReadOutProtectionStatus(void);
  244. FlagStatus FLASH_GetPrefetchBufferStatus(void);
  245. void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState);
  246. FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG);
  247. void FLASH_ClearFlag(uint16_t FLASH_FLAG);
  248. FLASH_Status FLASH_GetStatus(void);
  249. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
  250. #endif /* __HAL_FLASH_H */
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/