stm32f4xx_hal_flash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 __STM32F4xx_HAL_FLASH_H
  21. #define __STM32F4xx_HAL_FLASH_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f4xx_hal_def.h"
  27. /** @addtogroup STM32F4xx_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_RD 0x00000001U /*!< Read Protection error */
  74. #define HAL_FLASH_ERROR_PGS 0x00000002U /*!< Programming Sequence error */
  75. #define HAL_FLASH_ERROR_PGP 0x00000004U /*!< Programming Parallelism error */
  76. #define HAL_FLASH_ERROR_PGA 0x00000008U /*!< Programming Alignment error */
  77. #define HAL_FLASH_ERROR_WRP 0x00000010U /*!< Write protection error */
  78. #define HAL_FLASH_ERROR_OPERATION 0x00000020U /*!< Operation Error */
  79. /**
  80. * @}
  81. */
  82. /** @defgroup FLASH_Type_Program FLASH Type Program
  83. * @{
  84. */
  85. #define FLASH_TYPEPROGRAM_BYTE 0x00000000U /*!< Program byte (8-bit) at a specified address */
  86. #define FLASH_TYPEPROGRAM_HALFWORD 0x00000001U /*!< Program a half-word (16-bit) at a specified address */
  87. #define FLASH_TYPEPROGRAM_WORD 0x00000002U /*!< Program a word (32-bit) at a specified address */
  88. #define FLASH_TYPEPROGRAM_DOUBLEWORD 0x00000003U /*!< Program a double word (64-bit) at a specified address */
  89. /**
  90. * @}
  91. */
  92. /** @defgroup FLASH_Flag_definition FLASH Flag definition
  93. * @brief Flag definition
  94. * @{
  95. */
  96. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Operation flag */
  97. #define FLASH_FLAG_OPERR FLASH_SR_SOP /*!< FLASH operation Error flag */
  98. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  99. #define FLASH_FLAG_PGAERR FLASH_SR_PGAERR /*!< FLASH Programming Alignment error flag */
  100. #define FLASH_FLAG_PGPERR FLASH_SR_PGPERR /*!< FLASH Programming Parallelism error flag */
  101. #define FLASH_FLAG_PGSERR FLASH_SR_PGSERR /*!< FLASH Programming Sequence error flag */
  102. #if defined(FLASH_SR_RDERR)
  103. #define FLASH_FLAG_RDERR FLASH_SR_RDERR /*!< Read Protection error flag (PCROP) */
  104. #endif /* FLASH_SR_RDERR */
  105. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  106. /**
  107. * @}
  108. */
  109. /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
  110. * @brief FLASH Interrupt definition
  111. * @{
  112. */
  113. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  114. #define FLASH_IT_ERR 0x02000000U /*!< Error Interrupt source */
  115. /**
  116. * @}
  117. */
  118. /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
  119. * @{
  120. */
  121. #define FLASH_PSIZE_BYTE 0x00000000U
  122. #define FLASH_PSIZE_HALF_WORD 0x00000100U
  123. #define FLASH_PSIZE_WORD 0x00000200U
  124. #define FLASH_PSIZE_DOUBLE_WORD 0x00000300U
  125. #define CR_PSIZE_MASK 0xFFFFFCFFU
  126. /**
  127. * @}
  128. */
  129. /** @defgroup FLASH_Keys FLASH Keys
  130. * @{
  131. */
  132. #define RDP_KEY ((uint16_t)0x00A5)
  133. #define FLASH_KEY1 0x45670123U
  134. #define FLASH_KEY2 0xCDEF89ABU
  135. #define FLASH_OPT_KEY1 0x08192A3BU
  136. #define FLASH_OPT_KEY2 0x4C5D6E7FU
  137. /**
  138. * @}
  139. */
  140. /**
  141. * @}
  142. */
  143. /* Exported macro ------------------------------------------------------------*/
  144. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  145. * @{
  146. */
  147. /**
  148. * @brief Set the FLASH Latency.
  149. * @param __LATENCY__ FLASH Latency
  150. * The value of this parameter depend on device used within the same series
  151. * @retval none
  152. */
  153. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (*(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)(__LATENCY__))
  154. /**
  155. * @brief Get the FLASH Latency.
  156. * @retval FLASH Latency
  157. * The value of this parameter depend on device used within the same series
  158. */
  159. #define __HAL_FLASH_GET_LATENCY() (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
  160. /**
  161. * @brief Enable the FLASH prefetch buffer.
  162. * @retval none
  163. */
  164. #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() (FLASH->ACR |= FLASH_ACR_PRFTEN)
  165. /**
  166. * @brief Disable the FLASH prefetch buffer.
  167. * @retval none
  168. */
  169. #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
  170. /**
  171. * @brief Enable the FLASH instruction cache.
  172. * @retval none
  173. */
  174. #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE() (FLASH->ACR |= FLASH_ACR_ICEN)
  175. /**
  176. * @brief Disable the FLASH instruction cache.
  177. * @retval none
  178. */
  179. #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE() (FLASH->ACR &= (~FLASH_ACR_ICEN))
  180. /**
  181. * @brief Enable the FLASH data cache.
  182. * @retval none
  183. */
  184. #define __HAL_FLASH_DATA_CACHE_ENABLE() (FLASH->ACR |= FLASH_ACR_DCEN)
  185. /**
  186. * @brief Disable the FLASH data cache.
  187. * @retval none
  188. */
  189. #define __HAL_FLASH_DATA_CACHE_DISABLE() (FLASH->ACR &= (~FLASH_ACR_DCEN))
  190. /**
  191. * @brief Resets the FLASH instruction Cache.
  192. * @note This function must be used only when the Instruction Cache is disabled.
  193. * @retval None
  194. */
  195. #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_ICRST; \
  196. FLASH->ACR &= ~FLASH_ACR_ICRST; \
  197. }while(0U)
  198. /**
  199. * @brief Resets the FLASH data Cache.
  200. * @note This function must be used only when the data Cache is disabled.
  201. * @retval None
  202. */
  203. #define __HAL_FLASH_DATA_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_DCRST; \
  204. FLASH->ACR &= ~FLASH_ACR_DCRST; \
  205. }while(0U)
  206. /**
  207. * @brief Enable the specified FLASH interrupt.
  208. * @param __INTERRUPT__ FLASH interrupt
  209. * This parameter can be any combination of the following values:
  210. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  211. * @arg FLASH_IT_ERR: Error Interrupt
  212. * @retval none
  213. */
  214. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) (FLASH->CR |= (__INTERRUPT__))
  215. /**
  216. * @brief Disable the specified FLASH interrupt.
  217. * @param __INTERRUPT__ FLASH interrupt
  218. * This parameter can be any combination of the following values:
  219. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  220. * @arg FLASH_IT_ERR: Error Interrupt
  221. * @retval none
  222. */
  223. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
  224. /**
  225. * @brief Get the specified FLASH flag status.
  226. * @param __FLAG__ specifies the FLASH flags to check.
  227. * This parameter can be any combination of the following values:
  228. * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
  229. * @arg FLASH_FLAG_OPERR : FLASH operation Error flag
  230. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  231. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  232. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  233. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  234. * @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
  235. * @arg FLASH_FLAG_BSY : FLASH Busy flag
  236. * (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices
  237. * @retval The new state of __FLAG__ (SET or RESET).
  238. */
  239. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((FLASH->SR & (__FLAG__)))
  240. /**
  241. * @brief Clear the specified FLASH flags.
  242. * @param __FLAG__ specifies the FLASH flags to clear.
  243. * This parameter can be any combination of the following values:
  244. * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
  245. * @arg FLASH_FLAG_OPERR : FLASH operation Error flag
  246. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  247. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  248. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  249. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  250. * @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
  251. * (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices
  252. * @retval none
  253. */
  254. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) (FLASH->SR = (__FLAG__))
  255. /**
  256. * @}
  257. */
  258. /* Include FLASH HAL Extension module */
  259. #include "stm32f4xx_hal_flash_ex.h"
  260. #include "stm32f4xx_hal_flash_ramfunc.h"
  261. /* Exported functions --------------------------------------------------------*/
  262. /** @addtogroup FLASH_Exported_Functions
  263. * @{
  264. */
  265. /** @addtogroup FLASH_Exported_Functions_Group1
  266. * @{
  267. */
  268. /* Program operation functions ***********************************************/
  269. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  270. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  271. /* FLASH IRQ handler method */
  272. void HAL_FLASH_IRQHandler(void);
  273. /* Callbacks in non blocking modes */
  274. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  275. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  276. /**
  277. * @}
  278. */
  279. /** @addtogroup FLASH_Exported_Functions_Group2
  280. * @{
  281. */
  282. /* Peripheral Control functions **********************************************/
  283. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  284. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  285. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  286. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  287. /* Option bytes control */
  288. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  289. /**
  290. * @}
  291. */
  292. /** @addtogroup FLASH_Exported_Functions_Group3
  293. * @{
  294. */
  295. /* Peripheral State functions ************************************************/
  296. uint32_t HAL_FLASH_GetError(void);
  297. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  298. /**
  299. * @}
  300. */
  301. /**
  302. * @}
  303. */
  304. /* Private types -------------------------------------------------------------*/
  305. /* Private variables ---------------------------------------------------------*/
  306. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  307. * @{
  308. */
  309. /**
  310. * @}
  311. */
  312. /* Private constants ---------------------------------------------------------*/
  313. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  314. * @{
  315. */
  316. /**
  317. * @brief ACR register byte 0 (Bits[7:0]) base address
  318. */
  319. #define ACR_BYTE0_ADDRESS 0x40023C00U
  320. /**
  321. * @brief OPTCR register byte 0 (Bits[7:0]) base address
  322. */
  323. #define OPTCR_BYTE0_ADDRESS 0x40023C14U
  324. /**
  325. * @brief OPTCR register byte 1 (Bits[15:8]) base address
  326. */
  327. #define OPTCR_BYTE1_ADDRESS 0x40023C15U
  328. /**
  329. * @brief OPTCR register byte 2 (Bits[23:16]) base address
  330. */
  331. #define OPTCR_BYTE2_ADDRESS 0x40023C16U
  332. /**
  333. * @brief OPTCR register byte 3 (Bits[31:24]) base address
  334. */
  335. #define OPTCR_BYTE3_ADDRESS 0x40023C17U
  336. /**
  337. * @}
  338. */
  339. /* Private macros ------------------------------------------------------------*/
  340. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  341. * @{
  342. */
  343. /** @defgroup FLASH_IS_FLASH_Definitions FLASH Private macros to check input parameters
  344. * @{
  345. */
  346. #define IS_FLASH_TYPEPROGRAM(VALUE)(((VALUE) == FLASH_TYPEPROGRAM_BYTE) || \
  347. ((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \
  348. ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \
  349. ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /* Private functions ---------------------------------------------------------*/
  357. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  358. * @{
  359. */
  360. /**
  361. * @}
  362. */
  363. /**
  364. * @}
  365. */
  366. /**
  367. * @}
  368. */
  369. #ifdef __cplusplus
  370. }
  371. #endif
  372. #endif /* __STM32F4xx_HAL_FLASH_H */
  373. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/