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