1 /**
2   ******************************************************************************
3   * @file               ft32f0xx_flash.h
4   * @author             FMD AE
5   * @brief              This file contains all the functions prototypes for the FLASH
6   *                     firmware library.
7   * @version            V1.0.0
8   * @data                   2021-07-01
9   ******************************************************************************
10   */
11 
12 /* Define to prevent recursive inclusion -------------------------------------*/
13 #ifndef __FT32F0XX_FLASH_H
14 #define __FT32F0XX_FLASH_H
15 
16 #ifdef __cplusplus
17  extern "C" {
18 #endif
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "ft32f0xx.h"
22 
23 
24 /** @addtogroup FLASH
25   * @{
26   */
27 
28 /* Exported types ------------------------------------------------------------*/
29 
30 /**
31   * @brief  FLASH Status
32   */
33 typedef enum
34 {
35   FLASH_BUSY = 1,
36   FLASH_ERROR_WRP,
37   FLASH_ERROR_PROGRAM,
38   FLASH_COMPLETE,
39   FLASH_TIMEOUT
40 }FLASH_Status;
41 
42 /* Exported constants --------------------------------------------------------*/
43 
44 /** @defgroup FLASH_Exported_Constants
45   * @{
46   */
47 
48 /** @defgroup FLASH_Latency
49   * @{
50   */
51 #define FLASH_Latency_0                 ((uint32_t)0x00000000)  /*!< FLASH Zero Latency cycle */
52 #define FLASH_Latency_1                 ((uint32_t)0x00000001)  /*!< FLASH One Latency cycle */
53 #define FLASH_Latency_2                 ((uint32_t)0x00000002)
54 #define FLASH_Latency_3                 ((uint32_t)0x00000003)
55 #define FLASH_Latency_4                 ((uint32_t)0x00000004)
56 #define FLASH_Latency_5                 ((uint32_t)0x00000005)
57 #define FLASH_Latency_6                 ((uint32_t)0x00000006)
58 #define FLASH_Latency_7                 ((uint32_t)0x00000007)
59 #define FLASH_Latency_8                 ((uint32_t)0x00000008)
60 #define FLASH_Latency_9                 ((uint32_t)0x00000009)
61 #define FLASH_Latency_10                ((uint32_t)0x0000000a)
62 #define FLASH_Latency_11                ((uint32_t)0x0000000b)
63 #define FLASH_Latency_12                ((uint32_t)0x0000000c)
64 #define FLASH_Latency_13                ((uint32_t)0x0000000d)
65 #define FLASH_Latency_14                ((uint32_t)0x0000000e)
66 #define FLASH_Latency_15                ((uint32_t)0x0000000f)
67 
68 #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
69                                    ((LATENCY) == FLASH_Latency_1) || \
70                                     ((LATENCY) == FLASH_Latency_2 ) || \
71                                     ((LATENCY) == FLASH_Latency_3 ) || \
72                                     ((LATENCY) == FLASH_Latency_4 ) || \
73                                     ((LATENCY) == FLASH_Latency_5 ) || \
74                                     ((LATENCY) == FLASH_Latency_6 ) || \
75                                     ((LATENCY) == FLASH_Latency_7 ) || \
76                                     ((LATENCY) == FLASH_Latency_8 ) || \
77                                     ((LATENCY) == FLASH_Latency_9 ) || \
78                                     ((LATENCY) == FLASH_Latency_10) || \
79                                     ((LATENCY) == FLASH_Latency_11) || \
80                                     ((LATENCY) == FLASH_Latency_12) || \
81                                     ((LATENCY) == FLASH_Latency_13) || \
82                                     ((LATENCY) == FLASH_Latency_14) || \
83                                     ((LATENCY) == FLASH_Latency_15))
84 /**
85   * @}
86   */
87 
88 /** @defgroup FLASH_Interrupts
89   * @{
90   */
91 
92 #define FLASH_IT_EOP                   FLASH_CR_EOPIE  /*!< End of programming interrupt source */
93 #define FLASH_IT_ERR                   FLASH_CR_ERRIE  /*!< Error interrupt source */
94 #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
95 /**
96   * @}
97   */
98 
99 /** @defgroup FLASH_Address
100   * @{
101   */
102 
103 #if defined(FT32F030X8)  /*64K devices */
104  #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0800FFFF))
105 #elif defined (FT32F072xB)  /*128K devices */
106  #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0801FFFF))
107 #endif
108 
109 /**
110   * @}
111   */
112 
113 /** @defgroup FLASH_OB_DATA_ADDRESS
114   * @{
115   */
116 #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804))
117 
118 /**
119   * @}
120   */
121 
122 /** @defgroup FLASH_Option_Bytes_Write_Protection
123   * @{
124   */
125 
126 #define OB_WRP_Pages0to7               ((uint32_t)0x00000001) /* Write protection of page 0     to  7     */
127 #define OB_WRP_Pages8to15              ((uint32_t)0x00000002) /* Write protection of page 8     to  15    */
128 #define OB_WRP_Pages16to23             ((uint32_t)0x00000004) /* Write protection of page 16    to  23    */
129 #define OB_WRP_Pages24to31             ((uint32_t)0x00000008) /* Write protection of page 24    to  31    */
130 #define OB_WRP_Pages32to39             ((uint32_t)0x00000010) /* Write protection of page 32    to  39    */
131 #define OB_WRP_Pages40to47             ((uint32_t)0x00000020) /* Write protection of page 40    to  47    */
132 #define OB_WRP_Pages48to55             ((uint32_t)0x00000040) /* Write protection of page 48    to  55    */
133 #define OB_WRP_Pages56to63             ((uint32_t)0x00000080) /* Write protection of page 56    to  63    */
134 #define OB_WRP_Pages64to71             ((uint32_t)0x00000100) /* Write protection of page 64    to  71    */
135 #define OB_WRP_Pages72to79             ((uint32_t)0x00000200) /* Write protection of page 72    to  79    */
136 #define OB_WRP_Pages80to87             ((uint32_t)0x00000400) /* Write protection of page 80    to  87    */
137 #define OB_WRP_Pages88to95             ((uint32_t)0x00000800) /* Write protection of page 88    to  95    */
138 #define OB_WRP_Pages96to103            ((uint32_t)0x00001000) /* Write protection of page 96    to  103   */
139 #define OB_WRP_Pages104to111           ((uint32_t)0x00002000) /* Write protection of page 104   to  111   */
140 #define OB_WRP_Pages112to119           ((uint32_t)0x00004000) /* Write protection of page 112   to  119   */
141 #define OB_WRP_Pages120to127           ((uint32_t)0x00008000) /* Write protection of page 120   to  127   */
142 
143 #define OB_WRP_AllPages                ((uint32_t)0x0000FFFF) /*!< Write protection of all Sectors */
144 
145 #define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))
146 
147 /**
148   * @}
149   */
150 
151 /** @defgroup FLASH_Option_Bytes_Read_Protection
152   * @{
153   */
154 
155 /**
156   * @brief  FLASH_Read Protection Level
157   */
158 #define OB_RDP_Level_0   ((uint8_t)0xAA)
159 #define OB_RDP_Level_1   ((uint8_t)0xBB)
160 /*#define OB_RDP_Level_2   ((uint8_t)0xCC)*/ /* Warning: When enabling read protection level 2
161                                                 it's no more possible to go back to level 1 or 0 */
162 
163 #define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
164                           ((LEVEL) == OB_RDP_Level_1))/*||\
165                           ((LEVEL) == OB_RDP_Level_2))*/
166 /**
167   * @}
168   */
169 
170 /** @defgroup FLASH_Option_Bytes_IWatchdog
171   * @{
172   */
173 #if defined (FT32F072xB)
174     #define OB_IWDG_SW                     ((uint8_t)0x01)  /*!< Software IWDG selected */
175     #define OB_IWDG_HW                     ((uint8_t)0x00)  /*!< Hardware IWDG selected */
176 #else
177     #define OB_IWDG_SW                     ((uint8_t)0x00)  /*!< Software IWDG selected */
178     #define OB_IWDG_HW                     ((uint8_t)0x01)  /*!< Hardware IWDG selected */
179 #endif
180 #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
181 
182 /**
183   * @}
184   */
185 
186 /** @defgroup FLASH_Option_Bytes_nRST_STOP
187   * @{
188   */
189 
190 #define OB_STOP_NoRST                  ((uint8_t)0x02) /*!< No reset generated when entering in STOP */
191 #define OB_STOP_RST                    ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
192 #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
193 
194 /**
195   * @}
196   */
197 
198 /** @defgroup FLASH_Option_Bytes_nRST_STDBY
199   * @{
200   */
201 
202 #define OB_STDBY_NoRST                 ((uint8_t)0x04) /*!< No reset generated when entering in STANDBY */
203 #define OB_STDBY_RST                   ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
204 #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
205 
206 /**
207   * @}
208   */
209 
210 /** @defgroup FLASH_Option_Bytes_BOOT1
211   * @{
212   */
213 
214 #define OB_BOOT1_RESET                 ((uint8_t)0x00) /*!< BOOT1 Reset */
215 #define OB_BOOT1_SET                   ((uint8_t)0x10) /*!< BOOT1 Set */
216 #define IS_OB_BOOT1(BOOT1) (((BOOT1) == OB_BOOT1_RESET) || ((BOOT1) == OB_BOOT1_SET))
217 
218 /**
219   * @}
220   */
221 
222 /**
223   * @}
224   */
225 
226 /**
227   * @}
228   */
229 
230 /** @defgroup FLASH_Option_Bytes_VDDA_Analog_Monitoring
231   * @{
232   */
233 
234 #define OB_VDDA_ANALOG_ON              ((uint8_t)0x20) /*!< Analog monitoring on VDDA Power source ON */
235 #define OB_VDDA_ANALOG_OFF             ((uint8_t)0x00) /*!< Analog monitoring on VDDA Power source OFF */
236 
237 #define IS_OB_VDDA_ANALOG(ANALOG) (((ANALOG) == OB_VDDA_ANALOG_ON) || ((ANALOG) == OB_VDDA_ANALOG_OFF))
238 
239 /**
240   * @}
241   */
242 
243 /** @defgroup FLASH_Option_Bytes_SRAM_Parity_Enable
244   * @{
245   */
246 
247 #define OB_SRAM_PARITY_SET              ((uint8_t)0x00) /*!< SRAM parity enable Set */
248 #define OB_SRAM_PARITY_RESET            ((uint8_t)0x40) /*!< SRAM parity enable reset */
249 
250 #define IS_OB_SRAM_PARITY(PARITY) (((PARITY) == OB_SRAM_PARITY_SET) || ((PARITY) == OB_SRAM_PARITY_RESET))
251 
252 /**
253   * @}
254   */
255 
256 /** @defgroup FLASH_Flags
257   * @{
258   */
259 
260 #define FLASH_FLAG_BSY                 FLASH_SR_BSY     /*!< FLASH Busy flag */
261 #define FLASH_FLAG_PGERR               FLASH_SR_PGERR   /*!< FLASH Programming error flag */
262 #define FLASH_FLAG_WRPERR              FLASH_SR_WRPERR  /*!< FLASH Write protected error flag */
263 #define FLASH_FLAG_EOP                 FLASH_SR_EOP     /*!< FLASH End of Programming flag */
264 
265 #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFCB) == 0x00000000) && ((FLAG) != 0x00000000))
266 
267 #define IS_FLASH_GET_FLAG(FLAG)  (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_PGERR) || \
268                                   ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP))
269 /**
270   * @}
271   */
272 
273 /** @defgroup FLASH_Timeout_definition
274   * @{
275   */
276 #define FLASH_ER_PRG_TIMEOUT         ((uint32_t)0x000B0000)
277 
278 /**
279   * @}
280   */
281 
282 /** @defgroup FLASH_Legacy
283   * @{
284   */
285 #define FLASH_WRProt_Pages0to7         OB_WRP_Pages0to7
286 #define FLASH_WRProt_Pages8to15        OB_WRP_Pages8to15
287 #define FLASH_WRProt_Pages16to23       OB_WRP_Pages16to23
288 #define FLASH_WRProt_Pages24to31       OB_WRP_Pages24to31
289 #define FLASH_WRProt_Pages32to39       OB_WRP_Pages32to39
290 #define FLASH_WRProt_Pages40to47       OB_WRP_Pages40to47
291 #define FLASH_WRProt_Pages48to55       OB_WRP_Pages48to55
292 #define FLASH_WRProt_Pages56to63       OB_WRP_Pages56to63
293 #define FLASH_WRProt_Pages64to71       OB_WRP_Pages64to71
294 #define FLASH_WRProt_Pages72to79       OB_WRP_Pages72to79
295 #define FLASH_WRProt_Pages80to87       OB_WRP_Pages80to87
296 #define FLASH_WRProt_Pages88to95       OB_WRP_Pages88to95
297 #define FLASH_WRProt_Pages96to103      OB_WRP_Pages96to103
298 #define FLASH_WRProt_Pages104to111     OB_WRP_Pages104to111
299 #define FLASH_WRProt_Pages112to119     OB_WRP_Pages112to119
300 #define FLASH_WRProt_Pages120to127     OB_WRP_Pages120to127
301 
302 
303 #define FLASH_WRProt_AllPages          OB_WRP_AllPages
304 /**
305   * @}
306   */
307 
308 /**
309   * @}
310   */
311 
312 /* Exported macro ------------------------------------------------------------*/
313 /* Exported functions ------------------------------------------------------- */
314 
315 /**
316   * @brief  FLASH memory functions that can be executed from FLASH.
317   */
318 /* FLASH Interface configuration functions ************************************/
319 void FLASH_SetLatency(uint32_t FLASH_Latency);
320 void FLASH_PrefetchBufferCmd(FunctionalState NewState);
321 FlagStatus FLASH_GetPrefetchBufferStatus(void);
322 
323 /* FLASH Memory Programming functions *****************************************/
324 void FLASH_Unlock(void);
325 void FLASH_Lock(void);
326 FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
327 FLASH_Status FLASH_EraseAllPages(void);
328 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
329 #if defined(FT32F072xB)
330 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
331 #endif
332 
333 /* FLASH Option Bytes Programming functions *****************************************/
334 void FLASH_OB_Unlock(void);
335 void FLASH_OB_Lock(void);
336 void FLASH_OB_Launch(void);
337 FLASH_Status FLASH_OB_Erase(void);
338 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
339 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
340 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
341 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
342 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
343 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity);
344 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
345 #if defined(FT32F072xB)
346 FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
347 #else
348 FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint32_t Data);
349 #endif
350 uint8_t FLASH_OB_GetUser(void);
351 uint32_t FLASH_OB_GetWRP(void);
352 FlagStatus FLASH_OB_GetRDP(void);
353 
354 /* FLASH Interrupts and flags management functions **********************************/
355 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
356 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
357 void FLASH_ClearFlag(uint32_t FLASH_FLAG);
358 FLASH_Status FLASH_GetStatus(void);
359 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
360 
361 /** @defgroup FLASH_Legacy
362   * @{
363   */
364 #define FLASH_EraseOptionBytes               FLASH_OB_Erase
365 #define FLASH_EnableWriteProtection          FLASH_OB_EnableWRP
366 #define FLASH_UserOptionByteConfig           FLASH_OB_UserConfig
367 #define FLASH_ProgramOption4ByteData         FLASH_OB_ProgramData
368 #define FLASH_GetUserOptionByte              FLASH_OB_GetUser
369 #define FLASH_GetWriteProtectionOptionByte   FLASH_OB_GetWRP
370 
371 /**
372   * @}
373   */
374 
375 #ifdef __cplusplus
376 }
377 #endif
378 
379 #endif /* __FT32F0XX_FLASH_H */
380 
381 /**
382   * @}
383   */
384 
385 /**
386   * @}
387   */
388 
389 /************************ (C) COPYRIGHT FMD *****END OF FILE****/
390