stm32f2xx_hal_flash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_hal_flash.h
  4. * @author MCD Application Team
  5. * @brief Header file of FLASH HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef __STM32F2xx_HAL_FLASH_H
  21. #define __STM32F2xx_HAL_FLASH_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f2xx_hal_def.h"
  27. /** @addtogroup STM32F2xx_HAL_Driver
  28. * @{
  29. */
  30. /** @addtogroup FLASH
  31. * @{
  32. */
  33. /* Exported types ------------------------------------------------------------*/
  34. /** @defgroup FLASH_Exported_Types FLASH Exported Types
  35. * @{
  36. */
  37. /**
  38. * @brief FLASH Procedure structure definition
  39. */
  40. typedef enum
  41. {
  42. FLASH_PROC_NONE = 0U,
  43. FLASH_PROC_SECTERASE,
  44. FLASH_PROC_MASSERASE,
  45. FLASH_PROC_PROGRAM
  46. } FLASH_ProcedureTypeDef;
  47. /**
  48. * @brief FLASH handle Structure definition
  49. */
  50. typedef struct
  51. {
  52. __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*Internal variable to indicate which procedure is ongoing or not in IT context*/
  53. __IO uint32_t NbSectorsToErase; /*Internal variable to save the remaining sectors to erase in IT context*/
  54. __IO uint8_t VoltageForErase; /*Internal variable to provide voltage range selected by user in IT context*/
  55. __IO uint32_t Sector; /*Internal variable to define the current sector which is erasing*/
  56. __IO uint32_t Bank; /*Internal variable to save current bank selected during mass erase*/
  57. __IO uint32_t Address; /*Internal variable to save address selected for program*/
  58. HAL_LockTypeDef Lock; /* FLASH locking object */
  59. __IO uint32_t ErrorCode; /* FLASH error code */
  60. }FLASH_ProcessTypeDef;
  61. /**
  62. * @}
  63. */
  64. /* Exported constants --------------------------------------------------------*/
  65. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  66. * @{
  67. */
  68. /** @defgroup FLASH_Error_Code FLASH Error Code
  69. * @brief FLASH Error Code
  70. * @{
  71. */
  72. #define HAL_FLASH_ERROR_NONE 0x00000000U /*!< No error */
  73. #define HAL_FLASH_ERROR_PGS 0x00000001U /*!< Programming Sequence error */
  74. #define HAL_FLASH_ERROR_PGP 0x00000002U /*!< Programming Parallelism error */
  75. #define HAL_FLASH_ERROR_PGA 0x00000004U /*!< Programming Alignment error */
  76. #define HAL_FLASH_ERROR_WRP 0x00000008U /*!< Write protection error */
  77. #define HAL_FLASH_ERROR_OPERATION 0x00000010U /*!< Operation Error */
  78. /**
  79. * @}
  80. */
  81. /** @defgroup FLASH_Type_Program FLASH Type Program
  82. * @{
  83. */
  84. #define FLASH_TYPEPROGRAM_BYTE 0x00U /*!< Program byte (8-bit) at a specified address */
  85. #define FLASH_TYPEPROGRAM_HALFWORD 0x01U /*!< Program a half-word (16-bit) at a specified address */
  86. #define FLASH_TYPEPROGRAM_WORD 0x02U /*!< Program a word (32-bit) at a specified address */
  87. #define FLASH_TYPEPROGRAM_DOUBLEWORD 0x03U /*!< Program a double word (64-bit) at a specified address */
  88. /**
  89. * @}
  90. */
  91. /** @defgroup FLASH_Flag_definition FLASH Flag definition
  92. * @brief Flag definition
  93. * @{
  94. */
  95. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Operation flag */
  96. #define FLASH_FLAG_OPERR FLASH_SR_SOP /*!< FLASH operation Error flag */
  97. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  98. #define FLASH_FLAG_PGAERR FLASH_SR_PGAERR /*!< FLASH Programming Alignment error flag */
  99. #define FLASH_FLAG_PGPERR FLASH_SR_PGPERR /*!< FLASH Programming Parallelism error flag */
  100. #define FLASH_FLAG_PGSERR FLASH_SR_PGSERR /*!< FLASH Programming Sequence error flag */
  101. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  102. /**
  103. * @}
  104. */
  105. /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
  106. * @brief FLASH Interrupt definition
  107. * @{
  108. */
  109. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  110. #define FLASH_IT_ERR 0x02000000U /*!< Error Interrupt source */
  111. /**
  112. * @}
  113. */
  114. /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
  115. * @{
  116. */
  117. #define FLASH_PSIZE_BYTE 0x00000000U
  118. #define FLASH_PSIZE_HALF_WORD 0x00000100U
  119. #define FLASH_PSIZE_WORD 0x00000200U
  120. #define FLASH_PSIZE_DOUBLE_WORD 0x00000300U
  121. #define CR_PSIZE_MASK 0xFFFFFCFFU
  122. /**
  123. * @}
  124. */
  125. /** @defgroup FLASH_Keys FLASH Keys
  126. * @{
  127. */
  128. #define RDP_KEY ((uint16_t)0x00A5)
  129. #define FLASH_KEY1 0x45670123U
  130. #define FLASH_KEY2 0xCDEF89ABU
  131. #define FLASH_OPT_KEY1 0x08192A3BU
  132. #define FLASH_OPT_KEY2 0x4C5D6E7FU
  133. /**
  134. * @}
  135. */
  136. /**
  137. * @}
  138. */
  139. /* Exported macro ------------------------------------------------------------*/
  140. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  141. * @{
  142. */
  143. /**
  144. * @brief Set the FLASH Latency.
  145. * @param __LATENCY__ FLASH Latency
  146. * The value of this parameter depend on device used within the same series
  147. * @retval none
  148. */
  149. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (*(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)(__LATENCY__))
  150. /**
  151. * @brief Get the FLASH Latency.
  152. * @retval FLASH Latency
  153. * The value of this parameter depend on device used within the same series
  154. */
  155. #define __HAL_FLASH_GET_LATENCY() (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
  156. /**
  157. * @brief Enable the FLASH prefetch buffer.
  158. * @retval none
  159. */
  160. #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() (FLASH->ACR |= FLASH_ACR_PRFTEN)
  161. /**
  162. * @brief Disable the FLASH prefetch buffer.
  163. * @retval none
  164. */
  165. #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
  166. /**
  167. * @brief Enable the FLASH instruction cache.
  168. * @retval none
  169. */
  170. #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE() (FLASH->ACR |= FLASH_ACR_ICEN)
  171. /**
  172. * @brief Disable the FLASH instruction cache.
  173. * @retval none
  174. */
  175. #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE() (FLASH->ACR &= (~FLASH_ACR_ICEN))
  176. /**
  177. * @brief Enable the FLASH data cache.
  178. * @retval none
  179. */
  180. #define __HAL_FLASH_DATA_CACHE_ENABLE() (FLASH->ACR |= FLASH_ACR_DCEN)
  181. /**
  182. * @brief Disable the FLASH data cache.
  183. * @retval none
  184. */
  185. #define __HAL_FLASH_DATA_CACHE_DISABLE() (FLASH->ACR &= (~FLASH_ACR_DCEN))
  186. /**
  187. * @brief Resets the FLASH instruction Cache.
  188. * @note This function must be used only when the Instruction Cache is disabled.
  189. * @retval None
  190. */
  191. #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_ICRST; \
  192. FLASH->ACR &= ~FLASH_ACR_ICRST; \
  193. }while(0U)
  194. /**
  195. * @brief Resets the FLASH data Cache.
  196. * @note This function must be used only when the data Cache is disabled.
  197. * @retval None
  198. */
  199. #define __HAL_FLASH_DATA_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_DCRST; \
  200. FLASH->ACR &= ~FLASH_ACR_DCRST; \
  201. }while(0U)
  202. /**
  203. * @brief Enable the specified FLASH interrupt.
  204. * @param __INTERRUPT__ FLASH interrupt
  205. * This parameter can be any combination of the following values:
  206. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  207. * @arg FLASH_IT_ERR: Error Interrupt
  208. * @retval none
  209. */
  210. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) (FLASH->CR |= (__INTERRUPT__))
  211. /**
  212. * @brief Disable the specified FLASH interrupt.
  213. * @param __INTERRUPT__ FLASH interrupt
  214. * This parameter can be any combination of the following values:
  215. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  216. * @arg FLASH_IT_ERR: Error Interrupt
  217. * @retval none
  218. */
  219. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
  220. /**
  221. * @brief Get the specified FLASH flag status.
  222. * @param __FLAG__ specifies the FLASH flags to check.
  223. * This parameter can be any combination of the following values:
  224. * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
  225. * @arg FLASH_FLAG_OPERR : FLASH operation Error flag
  226. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  227. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  228. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  229. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  230. * @arg FLASH_FLAG_BSY : FLASH Busy flag
  231. * @retval The new state of __FLAG__ (SET or RESET).
  232. */
  233. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((FLASH->SR & (__FLAG__)))
  234. /**
  235. * @brief Clear the specified FLASH flags.
  236. * @param __FLAG__ specifies the FLASH flags to clear.
  237. * This parameter can be any combination of the following values:
  238. * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
  239. * @arg FLASH_FLAG_OPERR : FLASH operation Error flag
  240. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  241. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  242. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  243. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  244. * @retval none
  245. */
  246. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) (FLASH->SR = (__FLAG__))
  247. /**
  248. * @}
  249. */
  250. /* Include FLASH HAL Extension module */
  251. #include "stm32f2xx_hal_flash_ex.h"
  252. /* Exported functions --------------------------------------------------------*/
  253. /** @addtogroup FLASH_Exported_Functions
  254. * @{
  255. */
  256. /** @addtogroup FLASH_Exported_Functions_Group1
  257. * @{
  258. */
  259. /* Program operation functions ***********************************************/
  260. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  261. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  262. /* FLASH IRQ handler method */
  263. void HAL_FLASH_IRQHandler(void);
  264. /* Callbacks in non blocking modes */
  265. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  266. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  267. /**
  268. * @}
  269. */
  270. /** @addtogroup FLASH_Exported_Functions_Group2
  271. * @{
  272. */
  273. /* Peripheral Control functions **********************************************/
  274. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  275. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  276. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  277. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  278. /* Option bytes control */
  279. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  280. /**
  281. * @}
  282. */
  283. /** @addtogroup FLASH_Exported_Functions_Group3
  284. * @{
  285. */
  286. /* Peripheral State functions ************************************************/
  287. uint32_t HAL_FLASH_GetError(void);
  288. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  289. /**
  290. * @}
  291. */
  292. /**
  293. * @}
  294. */
  295. /* Private types -------------------------------------------------------------*/
  296. /* Private variables ---------------------------------------------------------*/
  297. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  298. * @{
  299. */
  300. /**
  301. * @}
  302. */
  303. /* Private constants ---------------------------------------------------------*/
  304. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  305. * @{
  306. */
  307. /**
  308. * @brief ACR register byte 0 (Bits[7:0]) base address
  309. */
  310. #define ACR_BYTE0_ADDRESS 0x40023C00U
  311. /**
  312. * @brief OPTCR register byte 0 (Bits[7:0]) base address
  313. */
  314. #define OPTCR_BYTE0_ADDRESS 0x40023C14U
  315. /**
  316. * @brief OPTCR register byte 1 (Bits[15:8]) base address
  317. */
  318. #define OPTCR_BYTE1_ADDRESS 0x40023C15U
  319. /**
  320. * @brief OPTCR register byte 2 (Bits[23:16]) base address
  321. */
  322. #define OPTCR_BYTE2_ADDRESS 0x40023C16U
  323. /**
  324. * @brief OPTCR register byte 3 (Bits[31:24]) base address
  325. */
  326. #define OPTCR_BYTE3_ADDRESS 0x40023C17U
  327. /**
  328. * @}
  329. */
  330. /* Private macros ------------------------------------------------------------*/
  331. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  332. * @{
  333. */
  334. /** @defgroup FLASH_IS_FLASH_Definitions FLASH Private macros to check input parameters
  335. * @{
  336. */
  337. #define IS_FLASH_TYPEPROGRAM(VALUE)(((VALUE) == FLASH_TYPEPROGRAM_BYTE) || \
  338. ((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \
  339. ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \
  340. ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))
  341. /**
  342. * @}
  343. */
  344. /**
  345. * @}
  346. */
  347. /* Private functions ---------------------------------------------------------*/
  348. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  349. * @{
  350. */
  351. /**
  352. * @}
  353. */
  354. /**
  355. * @}
  356. */
  357. /**
  358. * @}
  359. */
  360. #ifdef __cplusplus
  361. }
  362. #endif
  363. #endif /* __STM32F2xx_HAL_FLASH_H */
  364. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/