stm32l0xx_hal_flash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_flash.h
  4. * @author MCD Application Team
  5. * @brief Header file of Flash HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file in
  13. * the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. ******************************************************************************
  16. */
  17. /* Define to prevent recursive inclusion -------------------------------------*/
  18. #ifndef __STM32L0xx_HAL_FLASH_H
  19. #define __STM32L0xx_HAL_FLASH_H
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32l0xx_hal_def.h"
  25. /** @addtogroup STM32L0xx_HAL_Driver
  26. * @{
  27. */
  28. /** @addtogroup FLASH
  29. * @{
  30. */
  31. /** @addtogroup FLASH_Private_Constants
  32. * @{
  33. */
  34. #define FLASH_TIMEOUT_VALUE (50000U) /* 50 s */
  35. #define FLASH_SIZE_DATA_REGISTER FLASHSIZE_BASE
  36. /**
  37. * @}
  38. */
  39. /** @addtogroup FLASH_Private_Macros
  40. * @{
  41. */
  42. #define IS_FLASH_TYPEPROGRAM(_VALUE_) ((_VALUE_) == FLASH_TYPEPROGRAM_WORD)
  43. #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \
  44. ((__LATENCY__) == FLASH_LATENCY_1))
  45. /**
  46. * @}
  47. */
  48. /* Exported types ------------------------------------------------------------*/
  49. /** @defgroup FLASH_Exported_Types FLASH Exported Types
  50. * @{
  51. */
  52. /**
  53. * @brief FLASH Procedure structure definition
  54. */
  55. typedef enum
  56. {
  57. FLASH_PROC_NONE = 0,
  58. FLASH_PROC_PAGEERASE = 1,
  59. FLASH_PROC_PROGRAM = 2,
  60. } FLASH_ProcedureTypeDef;
  61. /**
  62. * @brief FLASH handle Structure definition
  63. */
  64. typedef struct
  65. {
  66. __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*!< Internal variable to indicate which procedure is ongoing or not in IT context */
  67. __IO uint32_t NbPagesToErase; /*!< Internal variable to save the remaining sectors to erase in IT context*/
  68. __IO uint32_t Address; /*!< Internal variable to save address selected for program or erase */
  69. __IO uint32_t Page; /*!< Internal variable to define the current page which is erasing */
  70. HAL_LockTypeDef Lock; /*!< FLASH locking object */
  71. __IO uint32_t ErrorCode; /*!< FLASH error code
  72. This parameter can be a value of @ref FLASH_Error_Codes */
  73. } FLASH_ProcessTypeDef;
  74. /**
  75. * @}
  76. */
  77. /* Exported constants --------------------------------------------------------*/
  78. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  79. * @{
  80. */
  81. /** @defgroup FLASH_Error_Codes FLASH Error Codes
  82. * @{
  83. */
  84. #define HAL_FLASH_ERROR_NONE 0x00U /*!< No error */
  85. #define HAL_FLASH_ERROR_PGA 0x01U /*!< Programming alignment error */
  86. #define HAL_FLASH_ERROR_WRP 0x02U /*!< Write protection error */
  87. #define HAL_FLASH_ERROR_OPTV 0x04U /*!< Option validity error */
  88. #define HAL_FLASH_ERROR_SIZE 0x08U /*!< */
  89. #define HAL_FLASH_ERROR_RD 0x10U /*!< Read protected error */
  90. #define HAL_FLASH_ERROR_FWWERR 0x20U /*!< FLASH Write or Erase operation aborted */
  91. #define HAL_FLASH_ERROR_NOTZERO 0x40U /*!< FLASH Write operation is done in a not-erased region */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup FLASH_Page_Size FLASH size information
  96. * @{
  97. */
  98. #define FLASH_SIZE (uint32_t)((*((uint32_t *)FLASHSIZE_BASE)&0xFFFF) * 1024U)
  99. #define FLASH_PAGE_SIZE (128U) /*!< FLASH Page Size in bytes */
  100. #define FLASH_END (FLASH_BASE + FLASH_SIZE - 1) /*!< FLASH end address in the alias region */
  101. #if defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
  102. #define FLASH_BANK2_BASE (FLASH_BASE + (FLASH_SIZE >> 1)) /*!< FLASH BANK2 base address in the alias region */
  103. #define FLASH_BANK1_END (FLASH_BANK2_BASE - 1) /*!< Program end FLASH BANK1 address */
  104. #define FLASH_BANK2_END (FLASH_END) /*!< Program end FLASH BANK2 address */
  105. #endif
  106. /**
  107. * @}
  108. */
  109. /** @defgroup FLASH_Type_Program FLASH Type Program
  110. * @{
  111. */
  112. #define FLASH_TYPEPROGRAM_WORD (0x02U) /*!<Program a word (32-bit) at a specified address.*/
  113. /**
  114. * @}
  115. */
  116. /** @defgroup FLASH_Latency FLASH Latency
  117. * @{
  118. */
  119. #define FLASH_LATENCY_0 (0x00000000U) /*!< FLASH Zero Latency cycle */
  120. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY /*!< FLASH One Latency cycle */
  121. /**
  122. * @}
  123. */
  124. /** @defgroup FLASH_Interrupts FLASH Interrupts
  125. * @{
  126. */
  127. #define FLASH_IT_EOP FLASH_PECR_EOPIE /*!< End of programming interrupt source */
  128. #define FLASH_IT_ERR FLASH_PECR_ERRIE /*!< Error interrupt source */
  129. /**
  130. * @}
  131. */
  132. /** @defgroup FLASH_Flags FLASH Flags
  133. * @{
  134. */
  135. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  136. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Programming flag */
  137. #define FLASH_FLAG_ENDHV FLASH_SR_HVOFF /*!< FLASH End of High Voltage flag */
  138. #define FLASH_FLAG_READY FLASH_SR_READY /*!< FLASH Ready flag after low power mode */
  139. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  140. #define FLASH_FLAG_PGAERR FLASH_SR_PGAERR /*!< FLASH Programming Alignment error flag */
  141. #define FLASH_FLAG_SIZERR FLASH_SR_SIZERR /*!< FLASH Size error flag */
  142. #define FLASH_FLAG_OPTVERR FLASH_SR_OPTVERR /*!< FLASH Option Validity error flag */
  143. #define FLASH_FLAG_RDERR FLASH_SR_RDERR /*!< FLASH Read protected error flag */
  144. #define FLASH_FLAG_FWWERR FLASH_SR_FWWERR /*!< FLASH Write or Errase operation aborted */
  145. #define FLASH_FLAG_NOTZEROERR FLASH_SR_NOTZEROERR /*!< FLASH Read protected error flag */
  146. /**
  147. * @}
  148. */
  149. /** @defgroup FLASH_Keys FLASH Keys
  150. * @{
  151. */
  152. #define FLASH_PDKEY1 (0x04152637U) /*!< Flash power down key1 */
  153. #define FLASH_PDKEY2 (0xFAFBFCFDU) /*!< Flash power down key2: used with FLASH_PDKEY1
  154. to unlock the RUN_PD bit in FLASH_ACR */
  155. #define FLASH_PEKEY1 (0x89ABCDEFU) /*!< Flash program erase key1 */
  156. #define FLASH_PEKEY2 (0x02030405U) /*!< Flash program erase key: used with FLASH_PEKEY2
  157. to unlock the write access to the FLASH_PECR register and
  158. data EEPROM */
  159. #define FLASH_PRGKEY1 (0x8C9DAEBFU) /*!< Flash program memory key1 */
  160. #define FLASH_PRGKEY2 (0x13141516U) /*!< Flash program memory key2: used with FLASH_PRGKEY2
  161. to unlock the program memory */
  162. #define FLASH_OPTKEY1 (0xFBEAD9C8U) /*!< Flash option key1 */
  163. #define FLASH_OPTKEY2 (0x24252627U) /*!< Flash option key2: used with FLASH_OPTKEY1 to
  164. unlock the write access to the option byte block */
  165. /**
  166. * @}
  167. */
  168. /* CMSIS_Legacy */
  169. #if defined ( __ICCARM__ )
  170. #define InterruptType_ACTLR_DISMCYCINT_Msk IntType_ACTLR_DISMCYCINT_Msk
  171. #endif
  172. /**
  173. * @}
  174. */
  175. /* Exported macro ------------------------------------------------------------*/
  176. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  177. * @brief macros to control FLASH features
  178. * @{
  179. */
  180. /** @defgroup FLASH_Interrupt FLASH Interrupts
  181. * @brief macros to handle FLASH interrupts
  182. * @{
  183. */
  184. /**
  185. * @brief Enable the specified FLASH interrupt.
  186. * @param __INTERRUPT__ FLASH interrupt
  187. * This parameter can be any combination of the following values:
  188. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  189. * @arg @ref FLASH_IT_ERR Error Interrupt
  190. * @retval none
  191. */
  192. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) SET_BIT((FLASH->PECR), (__INTERRUPT__))
  193. /**
  194. * @brief Disable the specified FLASH interrupt.
  195. * @param __INTERRUPT__ FLASH interrupt
  196. * This parameter can be any combination of the following values:
  197. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  198. * @arg @ref FLASH_IT_ERR Error Interrupt
  199. * @retval none
  200. */
  201. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) CLEAR_BIT((FLASH->PECR), (uint32_t)(__INTERRUPT__))
  202. /**
  203. * @brief Get the specified FLASH flag status.
  204. * @param __FLAG__ specifies the FLASH flag to check.
  205. * This parameter can be one of the following values:
  206. * @arg @ref FLASH_FLAG_BSY FLASH Busy flag
  207. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  208. * @arg @ref FLASH_FLAG_ENDHV FLASH End of High Voltage flag
  209. * @arg @ref FLASH_FLAG_READY FLASH Ready flag after low power mode
  210. * @arg @ref FLASH_FLAG_PGAERR FLASH Programming Alignment error flag
  211. * @arg @ref FLASH_FLAG_SIZERR FLASH Size error flag
  212. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag (not valid with STM32L031xx/STM32L041xx)
  213. * @arg @ref FLASH_FLAG_RDERR FLASH Read protected error flag
  214. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag
  215. * @arg @ref FLASH_FLAG_FWWERR FLASH Fetch While Write Error flag
  216. * @arg @ref FLASH_FLAG_NOTZEROERR Not Zero area error flag
  217. * @retval The new state of __FLAG__ (SET or RESET).
  218. */
  219. #define __HAL_FLASH_GET_FLAG(__FLAG__) (((FLASH->SR) & (__FLAG__)) == (__FLAG__))
  220. /**
  221. * @brief Clear the specified FLASH flag.
  222. * @param __FLAG__ specifies the FLASH flags to clear.
  223. * This parameter can be any combination of the following values:
  224. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  225. * @arg @ref FLASH_FLAG_PGAERR FLASH Programming Alignment error flag
  226. * @arg @ref FLASH_FLAG_SIZERR FLASH Size error flag
  227. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag (not valid with STM32L031xx/STM32L041xx)
  228. * @arg @ref FLASH_FLAG_RDERR FLASH Read protected error flag
  229. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag
  230. * @arg @ref FLASH_FLAG_FWWERR FLASH Fetch While Write Error flag
  231. * @arg @ref FLASH_FLAG_NOTZEROERR Not Zero area error flag
  232. * @retval none
  233. */
  234. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) ((FLASH->SR) = (__FLAG__))
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @}
  240. */
  241. /* Include FLASH HAL Extended module */
  242. #include "stm32l0xx_hal_flash_ex.h"
  243. #include "stm32l0xx_hal_flash_ramfunc.h"
  244. /* Exported functions --------------------------------------------------------*/
  245. /** @addtogroup FLASH_Exported_Functions
  246. * @{
  247. */
  248. /** @addtogroup FLASH_Exported_Functions_Group1
  249. * @{
  250. */
  251. /* IO operation functions *****************************************************/
  252. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
  253. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
  254. /* FLASH IRQ handler function */
  255. void HAL_FLASH_IRQHandler(void);
  256. /* Callbacks in non blocking modes */
  257. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  258. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  259. /**
  260. * @}
  261. */
  262. /** @addtogroup FLASH_Exported_Functions_Group2
  263. * @{
  264. */
  265. /* Peripheral Control functions ***********************************************/
  266. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  267. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  268. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  269. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  270. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  271. /**
  272. * @}
  273. */
  274. /** @addtogroup FLASH_Exported_Functions_Group3
  275. * @{
  276. */
  277. /* Peripheral State and Error functions ***************************************/
  278. uint32_t HAL_FLASH_GetError(void);
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. /** @addtogroup FLASH_Private_Variables
  286. * @{
  287. */
  288. /* Variables used for Erase pages under interruption*/
  289. extern FLASH_ProcessTypeDef pFlash;
  290. /**
  291. * @}
  292. */
  293. /* Private function -------------------------------------------------*/
  294. /** @addtogroup FLASH_Private_Functions
  295. * @{
  296. */
  297. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  298. /**
  299. * @}
  300. */
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @}
  306. */
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310. #endif /* __STM32L0xx_HAL_FLASH_H */