tae32f53xx_ll_flash.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. ******************************************************************************
  3. * @file tae32f53xx_ll_flash.h
  4. * @author MCD Application Team
  5. * @brief Head file for FLASH module LL driver.
  6. *
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software is licensed by Tai-Action under BSD 3-Clause license,
  14. * the "License"; You may not use this file except in compliance with the
  15. * License. You may obtain a copy of the License at:
  16. * opensource.org/licenses/BSD-3-Clause
  17. *
  18. ******************************************************************************
  19. */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef _TAE32F53XX_LL_FLASH_H_
  22. #define _TAE32F53XX_LL_FLASH_H_
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif /* __cplusplus */
  26. /* Includes ------------------------------------------------------------------*/
  27. #include "tae32f53xx_ll_def.h"
  28. /** @addtogroup TAE32F53xx_LL_Driver
  29. * @{
  30. */
  31. /** @addtogroup FLASH_LL
  32. * @{
  33. */
  34. /* Exported types ------------------------------------------------------------*/
  35. /** @defgroup FLASH_LL_Exported_Types FLASH LL Exported Types
  36. * @brief Set the read protection level
  37. * @{
  38. */
  39. /**
  40. @brief Set the read protection level
  41. */
  42. typedef enum {
  43. FLASH_RDP_LEVEL_0 = 0xAAU, /*!< FLASH Read Protection Level 0 */
  44. FLASH_RDP_LEVEL_1 = 0xFFU, /*!< FLASH Read Protection Level 1 */
  45. FLASH_RDP_LEVEL_2 = 0xCCU, /*!< FLASH Read Protection Level 2, Warning: When enabling read protection
  46. level 2,it's no more possible to go back to level 1 or 0 */
  47. } FLASH_RDPLVETypeDef;
  48. /**
  49. @brief Write Protection State
  50. */
  51. typedef enum {
  52. FLASH_WRPSTATE_DISABLE = 0x00U, /*!< Disable the write protection of the desired sections */
  53. FLASH_WRPSTATE_ENABLE = 0x01U, /*!< Enable the write protection of the desired sections */
  54. } FLASH_WRPSTETypeDef;
  55. /**
  56. * @brief Write Permission Area Control. Each AREA control 8 sections (refer to one area) in Main Memory Array
  57. */
  58. typedef enum {
  59. FLASH_WRP_AREA_1 = 0x00000001, /*!< FLASH Write Protection Area 1 */
  60. FLASH_WRP_AREA_2 = 0x00000002, /*!< FLASH Write Protection Area 2 */
  61. FLASH_WRP_AREA_3 = 0x00000004, /*!< FLASH Write Protection Area 3 */
  62. FLASH_WRP_AREA_4 = 0x00000008, /*!< FLASH Write Protection Area 4 */
  63. FLASH_WRP_AREA_5 = 0x00000010, /*!< FLASH Write Protection Area 5 */
  64. FLASH_WRP_AREA_6 = 0x00000020, /*!< FLASH Write Protection Area 6 */
  65. FLASH_WRP_AREA_7 = 0x00000040, /*!< FLASH Write Protection Area 7 */
  66. FLASH_WRP_AREA_8 = 0x00000080, /*!< FLASH Write Protection Area 8 */
  67. FLASH_WRP_AREA_9 = 0x00000100, /*!< FLASH Write Protection Area 9 */
  68. FLASH_WRP_AREA_10 = 0x00000200, /*!< FLASH Write Protection Area 10 */
  69. FLASH_WRP_AREA_11 = 0x00000400, /*!< FLASH Write Protection Area 11 */
  70. FLASH_WRP_AREA_12 = 0x00000800, /*!< FLASH Write Protection Area 12 */
  71. FLASH_WRP_AREA_13 = 0x00001000, /*!< FLASH Write Protection Area 13 */
  72. FLASH_WRP_AREA_14 = 0x00002000, /*!< FLASH Write Protection Area 14 */
  73. FLASH_WRP_AREA_15 = 0x00004000, /*!< FLASH Write Protection Area 15 */
  74. FLASH_WRP_AREA_16 = 0x00008000, /*!< FLASH Write Protection Area 16 */
  75. FLASH_WRP_AREA_17 = 0x00010000, /*!< FLASH Write Protection Area 17 */
  76. FLASH_WRP_AREA_18 = 0x00020000, /*!< FLASH Write Protection Area 18 */
  77. FLASH_WRP_AREA_19 = 0x00040000, /*!< FLASH Write Protection Area 19 */
  78. FLASH_WRP_AREA_20 = 0x00080000, /*!< FLASH Write Protection Area 20 */
  79. FLASH_WRP_AREA_ALL = 0x000FFFFF, /*!< FLASH Write Protection Area ALL */
  80. FLASH_WRP_AREA_Msk = FLASH_WRP_AREA_ALL, /*!< FLASH Write Protection Area Mask */
  81. } FLASH_WRPAREAETypeDef;
  82. /**
  83. * @}
  84. */
  85. /* Exported constants --------------------------------------------------------*/
  86. /** @defgroup FLASH_LL_Exported_Constants FLASH LL Exported Constants
  87. * @brief FLASH LL Exported Constants
  88. * @{
  89. */
  90. /** @defgroup FLASH_Error_Codes FLASH Error Codes
  91. * @{
  92. */
  93. #define FLASH_ERROR_NONE 0x00U /*!< No error */
  94. #define FLASH_ERROR_OPT 0x01U /*!< Operation error */
  95. #define FLASH_ERROR_WRP 0x02U /*!< Write protection error */
  96. #define FLASH_ERROR_RDP 0x04U /*!< Read protection error */
  97. #define FLASH_ERROR_ECC 0x08U /*!< ECC validity error */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup FLASH_Program_Width FLASH Program Width
  102. * @{
  103. */
  104. #define FLASH_PROG_DATA_WIDTH 16U /*!< Data width in a single programming operation.Number in bytes */
  105. /**
  106. * @}
  107. */
  108. /** @defgroup FLASH_Keys FLASH Keys
  109. * @{
  110. */
  111. #define FLASH_KEY1 0x32107654U /*!< FLASH key1 */
  112. #define FLASH_KEY2 0xFEDCBA98U /*!< FLASH key2: used with FLASH_KEY1
  113. to unlock the FLASH Program/Erase features */
  114. #define FLASH_OP_KEY 0x50035003U /*!< FLASH Operation Protection Key :
  115. Unlock the operation of FLASH Read/Write Protection Register */
  116. #define FLASH_PWR_KEY 0x50030000U /*!< FLASH pwr key: unlock the FLASH Standby/Wakeup features */
  117. /**
  118. * @}
  119. */
  120. /** @defgroup FLASH_Flag_definition FLASH Flag Definition
  121. * @{
  122. */
  123. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Flag BSY */
  124. #define FLASH_FLAG_DIF FLASH_ISR_DIF /*!< FLASH Flag DIF */
  125. #define FLASH_FLAG_ECCEIF FLASH_ISR_ECCEIF /*!< FLASH Flag ECCEIF */
  126. #define FLASH_FLAG_RPEIF FLASH_ISR_RPEIF /*!< FLASH Flag RPEIF */
  127. #define FLASH_FLAG_WPEIF FLASH_ISR_WPEIF /*!< FLASH Flag WPEIF */
  128. #define FLASH_FLAG_OPTEIF FLASH_ISR_OPTEIF /*!< FLASH Flag OPEIF */
  129. /**
  130. * @}
  131. */
  132. /**
  133. * @}
  134. */
  135. /* Exported macro ------------------------------------------------------------*/
  136. /** @defgroup FLASH_LL_Exported_Macros FLASH LL Exported Macros
  137. * @brief FLASH LL Exported Macros
  138. * @{
  139. */
  140. /**
  141. * @brief Enable FLASH I BUS Prefetch feature.
  142. * @note Prefetch feature <b>should not be enabled</b> when CPU frequency is lower than <b>40MHz</b>.
  143. * @param None
  144. * @return None
  145. */
  146. #define __LL_FLASH_I_BUS_PREFETCH_ENABLE() SET_BIT(FLASH->CR, FLASH_CR_IBPE)
  147. /**
  148. * @brief Disable FLASH I BUS Prefetch feature.
  149. * @param None
  150. * @return None
  151. */
  152. #define __LL_FLASH_I_BUS_PREFETCH_DISABLE() CLEAR_BIT(FLASH->CR, FLASH_CR_IBPE)
  153. /**
  154. * @brief Enable FLASH D BUS Prefetch feature.
  155. * @note Prefetch feature <b>should not be enabled</b> when CPU frequency is lower than <b>40MHz</b>.
  156. * @param None
  157. * @return None
  158. */
  159. #define __LL_FLASH_D_BUS_PREFETCH_ENABLE() SET_BIT(FLASH->CR, FLASH_CR_DBPE)
  160. /**
  161. * @brief Disable FLASH D BUS Prefetch feature.
  162. * @param None
  163. * @return None
  164. */
  165. #define __LL_FLASH_D_BUS_PREFETCH_DISABLE() CLEAR_BIT(FLASH->CR, FLASH_CR_DBPE)
  166. /**
  167. * @brief FLASH Standby.
  168. * Flash will enter standby mode aim for low power mode.
  169. * @param None
  170. * @return None
  171. */
  172. #define __LL_FLASH_STANDBY_ENABLE() do{ WRITE_REG(FLASH->LPR, FLASH_PWR_KEY); \
  173. SET_BIT(FLASH->LPR, FLASH_LPR_STDBY); \
  174. } while (0);
  175. /**
  176. * @brief FLASH Wakeup.
  177. * Flash will exit from standby mode.
  178. * @param None
  179. * @return None
  180. */
  181. #define __LL_FLASH_WAKEUP_ENABLE() do{ WRITE_REG(FLASH->LPR, FLASH_PWR_KEY); \
  182. SET_BIT(FLASH->LPR, FLASH_LPR_WKUP); \
  183. } while (0);
  184. /**
  185. * @brief Check whether the specified status flag in FLASH_SR Register is SET or not.
  186. * @param __FLAG__ specifies the FLASH status flag to check.
  187. * This parameter can be ONE of the following values:
  188. * @arg FLASH_FLAG_BSY : FLASH BUSY Status
  189. * @return The new state of __FLAG__ (SET or RESET).
  190. */
  191. #define __LL_FLASH_GET_STATUS_FLAG(__FLAG__) ((READ_BIT(FLASH->SR, (__FLAG__)) == (__FLAG__)) ? SET : RESET)
  192. /**
  193. * @brief Check whether the specified interrupt pending flag in FLASH_ISR Register is SET or not.
  194. * @param __FLAG__ specifies the FLASH interrupt pending flag to check.
  195. * This parameter can be ONE of the following values:
  196. * @arg FLASH_FLAG_DIF : Done
  197. * @arg FLASH_FLAG_ECCEIF : ECC Error
  198. * @arg FLASH_FLAG_RPEIF : Read Protection Error
  199. * @arg FLASH_FLAG_WPEIF : Write Protection Error
  200. * @arg FLASH_FLAG_OPTEIF : Operation Error
  201. * @return The new state of __FLAG__ (SET or RESET).
  202. */
  203. #define __LL_FLASH_GET_PENDING_FLAG(__FLAG__) ((READ_BIT(FLASH->ISR, (__FLAG__)) == (__FLAG__)) ? SET : RESET)
  204. /**
  205. * @brief Clear the FLASH's Pending Register flag.
  206. * @param __FLAG__ specifies the FLASH pending flags to clear.
  207. * This parameter can be any combination of the following values:
  208. * @arg FLASH_FLAG_DIF : Done
  209. * @arg FLASH_FLAG_ECCEIF : ECC Error
  210. * @arg FLASH_FLAG_RPEIF : Read Protection Error
  211. * @arg FLASH_FLAG_WPEIF : Write Protection Error
  212. * @arg FLASH_FLAG_OPTEIF : Operation Error
  213. * @return The new state of __FLAG__ (SET or RESET).
  214. */
  215. #define __LL_FLASH_CLEAR_PENDING_FLAG(__FLAG__) WRITE_REG(FLASH->ISR, (__FLAG__))
  216. /**
  217. * @}
  218. */
  219. /* Exported functions --------------------------------------------------------*/
  220. /** @addtogroup FLASH_LL_Exported_Functions
  221. * @{
  222. */
  223. /** @addtogroup FLASH_LL_Exported_Functions_Group1
  224. * @{
  225. */
  226. LL_StatusETypeDef LL_FLASH_ReadProtectLevelConfig(FLASH_RDPLVETypeDef RDPLevel);
  227. LL_StatusETypeDef LL_FLASH_WriteProtectConfig(FLASH_WRPAREAETypeDef WRPAreas, FLASH_WRPSTETypeDef WRPState);
  228. /**
  229. * @}
  230. */
  231. /** @addtogroup FLASH_LL_Exported_Functions_Group2
  232. * @{
  233. */
  234. LL_StatusETypeDef LL_FLASH_WaitForLastOperation(uint32_t Timeout);
  235. uint32_t LL_FLASH_GetError(void);
  236. /**
  237. * @}
  238. */
  239. /** @addtogroup FLASH_LL_Exported_Functions_Group3
  240. * @{
  241. */
  242. /**
  243. * @brief Unlock the FLASH Program/Erase access.
  244. * @param None
  245. * @return LL Status
  246. */
  247. __STATIC_INLINE LL_StatusETypeDef LL_FLASH_Unlock(void)
  248. {
  249. if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) {
  250. /* Authorize the FLASH Program/Erase access */
  251. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  252. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  253. /* Verify FLASH is unlocked */
  254. if (READ_BIT(FLASH->CR, FLASH_CR_LOCK_Msk) != RESET) {
  255. return LL_ERROR;
  256. }
  257. }
  258. /* Return function status */
  259. return LL_OK;
  260. }
  261. /**
  262. * @brief Lock the FLASH Program/Erase access.
  263. * @param None
  264. * @return LL Status
  265. */
  266. __STATIC_INLINE LL_StatusETypeDef LL_FLASH_Lock(void)
  267. {
  268. /* Set the LOCK Bit to lock the FLASH Program/Erase access */
  269. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  270. /* Return function status */
  271. return LL_OK;
  272. }
  273. /**
  274. * @brief Unlock the FLASH write/read Protection Feature access.
  275. * @param None
  276. * @return LL Status
  277. */
  278. __STATIC_INLINE LL_StatusETypeDef LL_FLASH_PF_Unlock(void)
  279. {
  280. /* Unlock the FLASH Read/Write Operation Protection access */
  281. WRITE_REG(FLASH->KEYR, FLASH_OP_KEY);
  282. /* Return function status */
  283. return LL_OK;
  284. }
  285. /**
  286. * @brief Lock the FLASH write/read Protection Feature access.
  287. * @param None
  288. * @return LL Status
  289. */
  290. __STATIC_INLINE LL_StatusETypeDef LL_FLASH_PF_Lock(void)
  291. {
  292. /* Lock the Read/Write Operation Protection access */
  293. WRITE_REG(FLASH->KEYR, 0x00000000UL);
  294. /* Return function status */
  295. return LL_OK;
  296. }
  297. /**
  298. * @brief Launch the write/read Protection Feature reloading.
  299. * @param None
  300. * @retval None
  301. */
  302. __STATIC_INLINE LL_StatusETypeDef LL_FLASH_PF_Launch(void)
  303. {
  304. /* Set the LAU bit in FLASH_CR register to reload */
  305. SET_BIT(FLASH->CR, FLASH_CR_LAU);
  306. /* Return function status */
  307. return LL_OK;
  308. }
  309. LL_StatusETypeDef LL_FLASH_Program(uint32_t Address, uint8_t Data[FLASH_PROG_DATA_WIDTH]);
  310. LL_StatusETypeDef LL_FLASH_MultiSectorsErase(uint16_t Sector, uint16_t Num, uint16_t *SectorError);
  311. LL_StatusETypeDef LL_FLASH_SectorErase(uint16_t Sector);
  312. LL_StatusETypeDef LL_FLASH_ChipErase(void);
  313. /**
  314. * @}
  315. */
  316. /**
  317. * @}
  318. */
  319. /* Private types -------------------------------------------------------------*/
  320. /* Private variables ---------------------------------------------------------*/
  321. /* Private constants ---------------------------------------------------------*/
  322. /** @defgroup FLASH_LL_Private_Constants FLASH LL Private Constants
  323. * @brief FLASH LL Private Constants
  324. * @{
  325. */
  326. #define FLASH_PROGRAM_ADDRESS_MASK 0x00FFFFFFU /*!< Program address mask */
  327. #define FLASH_TIMEOUT_MAX_VALUE 300U /*!< Max timeout for flash operations. Default 300 ticks */
  328. /**
  329. * @}
  330. */
  331. /* Private macros ------------------------------------------------------------*/
  332. /** @defgroup FLASH_LL_Private_Macros FLASH LL Private Macros
  333. * @brief FLASH LL Private Macros
  334. * @{
  335. */
  336. /**
  337. * @brief Judge is write protect state or not
  338. * @param STATE state to judge
  339. * @retval 0 isn't write protect state
  340. * @retval 1 is write protect state
  341. */
  342. #define IS_WRITE_PROTECT_STATE(STATE) (((STATE) == FLASH_WRPSTATE_DISABLE) || \
  343. /**
  344. * @brief Judge is flash program address align 128bit or not
  345. * @param ADDRESS address to judge
  346. * @retval 0 isn't flash program address align 128bit
  347. * @retval 1 is flash program address align 128bit
  348. */
  349. #define IS_FLASH_PROGRAM_ADDRESS_ALIGN_128BIT(ADDRESS) (((ADDRESS) & 0xF) == 0x00U)
  350. /**
  351. * @brief Judge is flash sector or not
  352. * @param SECTOR sector to judge
  353. * @retval 0 isn't flash sector
  354. * @retval 1 is flash sector
  355. */
  356. #define IS_FLASH_NB_SECTORS(SECTOR) ((SECTOR) < 160U)
  357. /**
  358. * @}
  359. */
  360. /* Private functions ---------------------------------------------------------*/
  361. /**
  362. * @}
  363. */
  364. /**
  365. * @}
  366. */
  367. #ifdef __cplusplus
  368. }
  369. #endif /* __cplusplus */
  370. #endif /* _TAE32F53XX_LL_FLASH_H_ */
  371. /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/