stm32l1xx_hal_flash.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Define to prevent recursive inclusion -------------------------------------*/
  36. #ifndef __STM32L1xx_HAL_FLASH_H
  37. #define __STM32L1xx_HAL_FLASH_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32l1xx_hal_def.h"
  43. /** @addtogroup STM32L1xx_HAL_Driver
  44. * @{
  45. */
  46. /** @addtogroup FLASH
  47. * @{
  48. */
  49. /** @addtogroup FLASH_Private_Constants
  50. * @{
  51. */
  52. #define FLASH_TIMEOUT_VALUE (50000U) /* 50 s */
  53. /**
  54. * @}
  55. */
  56. /** @addtogroup FLASH_Private_Macros
  57. * @{
  58. */
  59. #define IS_FLASH_TYPEPROGRAM(_VALUE_) ((_VALUE_) == FLASH_TYPEPROGRAM_WORD)
  60. #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \
  61. ((__LATENCY__) == FLASH_LATENCY_1))
  62. /**
  63. * @}
  64. */
  65. /* Exported types ------------------------------------------------------------*/
  66. /** @defgroup FLASH_Exported_Types FLASH Exported Types
  67. * @{
  68. */
  69. /**
  70. * @brief FLASH Procedure structure definition
  71. */
  72. typedef enum
  73. {
  74. FLASH_PROC_NONE = 0U,
  75. FLASH_PROC_PAGEERASE = 1U,
  76. FLASH_PROC_PROGRAM = 2U,
  77. } FLASH_ProcedureTypeDef;
  78. /**
  79. * @brief FLASH handle Structure definition
  80. */
  81. typedef struct
  82. {
  83. __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*!< Internal variable to indicate which procedure is ongoing or not in IT context */
  84. __IO uint32_t NbPagesToErase; /*!< Internal variable to save the remaining sectors to erase in IT context*/
  85. __IO uint32_t Address; /*!< Internal variable to save address selected for program or erase */
  86. __IO uint32_t Page; /*!< Internal variable to define the current page which is erasing */
  87. HAL_LockTypeDef Lock; /*!< FLASH locking object */
  88. __IO uint32_t ErrorCode; /*!< FLASH error code
  89. This parameter can be a value of @ref FLASH_Error_Codes */
  90. } FLASH_ProcessTypeDef;
  91. /**
  92. * @}
  93. */
  94. /* Exported constants --------------------------------------------------------*/
  95. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  96. * @{
  97. */
  98. /** @defgroup FLASH_Error_Codes FLASH Error Codes
  99. * @{
  100. */
  101. #define HAL_FLASH_ERROR_NONE 0x00U /*!< No error */
  102. #define HAL_FLASH_ERROR_PGA 0x01U /*!< Programming alignment error */
  103. #define HAL_FLASH_ERROR_WRP 0x02U /*!< Write protection error */
  104. #define HAL_FLASH_ERROR_OPTV 0x04U /*!< Option validity error */
  105. #define HAL_FLASH_ERROR_SIZE 0x08U /*!< */
  106. #define HAL_FLASH_ERROR_RD 0x10U /*!< Read protected error */
  107. #define HAL_FLASH_ERROR_OPTVUSR 0x20U /*!< Option UserValidity Error. */
  108. #define HAL_FLASH_ERROR_OPERATION 0x40U /*!< Not used */
  109. /**
  110. * @}
  111. */
  112. /** @defgroup FLASH_Page_Size FLASH size information
  113. * @{
  114. */
  115. #define FLASH_SIZE (uint32_t)((*((uint32_t *)FLASHSIZE_BASE)&0xFFFFU) * 1024U)
  116. #define FLASH_PAGE_SIZE (256U) /*!< FLASH Page Size in bytes */
  117. /**
  118. * @}
  119. */
  120. /** @defgroup FLASH_Type_Program FLASH Type Program
  121. * @{
  122. */
  123. #define FLASH_TYPEPROGRAM_WORD (0x02U) /*!<Program a word (32-bit) at a specified address.*/
  124. /**
  125. * @}
  126. */
  127. /** @defgroup FLASH_Latency FLASH Latency
  128. * @{
  129. */
  130. #define FLASH_LATENCY_0 (0x00000000U) /*!< FLASH Zero Latency cycle */
  131. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY /*!< FLASH One Latency cycle */
  132. /**
  133. * @}
  134. */
  135. /** @defgroup FLASH_Interrupts FLASH Interrupts
  136. * @{
  137. */
  138. #define FLASH_IT_EOP FLASH_PECR_EOPIE /*!< End of programming interrupt source */
  139. #define FLASH_IT_ERR FLASH_PECR_ERRIE /*!< Error interrupt source */
  140. /**
  141. * @}
  142. */
  143. /** @defgroup FLASH_Flags FLASH Flags
  144. * @{
  145. */
  146. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  147. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Programming flag */
  148. #define FLASH_FLAG_ENDHV FLASH_SR_ENDHV /*!< FLASH End of High Voltage flag */
  149. #define FLASH_FLAG_READY FLASH_SR_READY /*!< FLASH Ready flag after low power mode */
  150. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  151. #define FLASH_FLAG_PGAERR FLASH_SR_PGAERR /*!< FLASH Programming Alignment error flag */
  152. #define FLASH_FLAG_SIZERR FLASH_SR_SIZERR /*!< FLASH Size error flag */
  153. #define FLASH_FLAG_OPTVERR FLASH_SR_OPTVERR /*!< FLASH Option Validity error flag */
  154. /* Cat2 & Cat3*/
  155. #if defined(FLASH_SR_RDERR)
  156. #define FLASH_FLAG_RDERR FLASH_SR_RDERR /*!< Read protected error flag */
  157. #endif /* FLASH_SR_RDERR */
  158. /* Cat3, Cat4 & Cat5*/
  159. #if defined(FLASH_SR_OPTVERRUSR)
  160. #define FLASH_FLAG_OPTVERRUSR FLASH_SR_OPTVERRUSR /*!< FLASH Option User Validity error flag */
  161. #endif /* FLASH_SR_OPTVERRUSR */
  162. /**
  163. * @}
  164. */
  165. /** @defgroup FLASH_Keys FLASH Keys
  166. * @{
  167. */
  168. #define FLASH_PDKEY1 (0x04152637U) /*!< Flash power down key1 */
  169. #define FLASH_PDKEY2 (0xFAFBFCFDU) /*!< Flash power down key2: used with FLASH_PDKEY1
  170. to unlock the RUN_PD bit in FLASH_ACR */
  171. #define FLASH_PEKEY1 (0x89ABCDEFU) /*!< Flash program erase key1 */
  172. #define FLASH_PEKEY2 (0x02030405U) /*!< Flash program erase key: used with FLASH_PEKEY2
  173. to unlock the write access to the FLASH_PECR register and
  174. data EEPROM */
  175. #define FLASH_PRGKEY1 (0x8C9DAEBFU) /*!< Flash program memory key1 */
  176. #define FLASH_PRGKEY2 (0x13141516U) /*!< Flash program memory key2: used with FLASH_PRGKEY2
  177. to unlock the program memory */
  178. #define FLASH_OPTKEY1 (0xFBEAD9C8U) /*!< Flash option key1 */
  179. #define FLASH_OPTKEY2 (0x24252627U) /*!< Flash option key2: used with FLASH_OPTKEY1 to
  180. unlock the write access to the option byte block */
  181. /**
  182. * @}
  183. */
  184. /**
  185. * @}
  186. */
  187. /* Exported macro ------------------------------------------------------------*/
  188. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  189. * @brief macros to control FLASH features
  190. * @{
  191. */
  192. /** @defgroup FLASH_Interrupt FLASH Interrupts
  193. * @brief macros to handle FLASH interrupts
  194. * @{
  195. */
  196. /**
  197. * @brief Enable the specified FLASH interrupt.
  198. * @param __INTERRUPT__ FLASH interrupt
  199. * This parameter can be any combination of the following values:
  200. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  201. * @arg @ref FLASH_IT_ERR Error Interrupt
  202. * @retval none
  203. */
  204. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) SET_BIT((FLASH->PECR), (__INTERRUPT__))
  205. /**
  206. * @brief Disable the specified FLASH interrupt.
  207. * @param __INTERRUPT__ FLASH interrupt
  208. * This parameter can be any combination of the following values:
  209. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  210. * @arg @ref FLASH_IT_ERR Error Interrupt
  211. * @retval none
  212. */
  213. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) CLEAR_BIT((FLASH->PECR), (uint32_t)(__INTERRUPT__))
  214. /**
  215. * @brief Get the specified FLASH flag status.
  216. * @param __FLAG__ specifies the FLASH flag to check.
  217. * This parameter can be one of the following values:
  218. * @arg @ref FLASH_FLAG_BSY FLASH Busy flag
  219. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  220. * @arg @ref FLASH_FLAG_ENDHV FLASH End of High Voltage flag
  221. * @arg @ref FLASH_FLAG_READY FLASH Ready flag after low power mode
  222. * @arg @ref FLASH_FLAG_PGAERR FLASH Programming Alignment error flag
  223. * @arg @ref FLASH_FLAG_SIZERR FLASH Size error flag
  224. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error error flag
  225. @if STM32L100xB
  226. @elif STM32L100xBA
  227. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  228. @elif STM32L151xB
  229. @elif STM32L151xBA
  230. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  231. @elif STM32L152xB
  232. @elif STM32L152xBA
  233. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  234. @elif STM32L100xC
  235. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  236. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  237. @elif STM32L151xC
  238. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  239. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  240. @elif STM32L152xC
  241. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  242. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  243. @elif STM32L162xC
  244. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  245. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  246. @else
  247. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  248. @endif
  249. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag
  250. * @retval The new state of __FLAG__ (SET or RESET).
  251. */
  252. #define __HAL_FLASH_GET_FLAG(__FLAG__) (((FLASH->SR) & (__FLAG__)) == (__FLAG__))
  253. /**
  254. * @brief Clear the specified FLASH flag.
  255. * @param __FLAG__ specifies the FLASH flags to clear.
  256. * This parameter can be any combination of the following values:
  257. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  258. * @arg @ref FLASH_FLAG_PGAERR FLASH Programming Alignment error flag
  259. * @arg @ref FLASH_FLAG_SIZERR FLASH Size error flag
  260. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error error flag
  261. @if STM32L100xB
  262. @elif STM32L100xBA
  263. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  264. @elif STM32L151xB
  265. @elif STM32L151xBA
  266. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  267. @elif STM32L152xB
  268. @elif STM32L152xBA
  269. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  270. @elif STM32L100xC
  271. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  272. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  273. @elif STM32L151xC
  274. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  275. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  276. @elif STM32L152xC
  277. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  278. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  279. @elif STM32L162xC
  280. * @arg @ref FLASH_FLAG_RDERR FLASH Read Protection error flag (PCROP)
  281. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  282. @else
  283. * @arg @ref FLASH_FLAG_OPTVERRUSR FLASH Option User validity error
  284. @endif
  285. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protected error flag
  286. * @retval none
  287. */
  288. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) ((FLASH->SR) = (__FLAG__))
  289. /**
  290. * @}
  291. */
  292. /**
  293. * @}
  294. */
  295. /* Include FLASH HAL Extended module */
  296. #include "stm32l1xx_hal_flash_ex.h"
  297. #include "stm32l1xx_hal_flash_ramfunc.h"
  298. /* Exported functions --------------------------------------------------------*/
  299. /** @addtogroup FLASH_Exported_Functions
  300. * @{
  301. */
  302. /** @addtogroup FLASH_Exported_Functions_Group1
  303. * @{
  304. */
  305. /* IO operation functions *****************************************************/
  306. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
  307. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
  308. /* FLASH IRQ handler function */
  309. void HAL_FLASH_IRQHandler(void);
  310. /* Callbacks in non blocking modes */
  311. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  312. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  313. /**
  314. * @}
  315. */
  316. /** @addtogroup FLASH_Exported_Functions_Group2
  317. * @{
  318. */
  319. /* Peripheral Control functions ***********************************************/
  320. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  321. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  322. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  323. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  324. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  325. /**
  326. * @}
  327. */
  328. /** @addtogroup FLASH_Exported_Functions_Group3
  329. * @{
  330. */
  331. /* Peripheral State and Error functions ***************************************/
  332. uint32_t HAL_FLASH_GetError(void);
  333. /**
  334. * @}
  335. */
  336. /**
  337. * @}
  338. */
  339. /* Private function -------------------------------------------------*/
  340. /** @addtogroup FLASH_Private_Functions
  341. * @{
  342. */
  343. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  344. /**
  345. * @}
  346. */
  347. /**
  348. * @}
  349. */
  350. /**
  351. * @}
  352. */
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356. #endif /* __STM32L1xx_HAL_FLASH_H */
  357. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/