1 /**
2   ******************************************************************************
3   * @file    hk32f0xx_flash.h
4   * @version V1.0.1
5   * @date    2019-08-15
6   ******************************************************************************
7   */
8 
9 /* Define to prevent recursive inclusion -------------------------------------*/
10 #ifndef __HK32F0XX_FLASH_H
11 #define __HK32F0XX_FLASH_H
12 
13 #ifdef __cplusplus
14  extern "C" {
15 #endif
16 
17 /* Includes ------------------------------------------------------------------*/
18 #include "hk32f0xx.h"
19 
20 /** @addtogroup HK32F0xx_StdPeriph_Driver
21   * @{
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                FLASH_ACR_LATENCY       /*!< FLASH One Latency cycle */
53 
54 #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
55                                    ((LATENCY) == FLASH_Latency_1))
56 /**
57   * @}
58   */
59 
60 /** @defgroup FLASH_Interrupts
61   * @{
62   */
63 
64 #define FLASH_IT_EOP                   FLASH_CR_EOPIE  /*!< End of programming interrupt source */
65 #define FLASH_IT_ERR                   FLASH_CR_ERRIE  /*!< Error interrupt source */
66 #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
67 /**
68   * @}
69   */
70 
71 /** @defgroup FLASH_Address
72   * @{
73   */
74 
75 #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) <= 0x0800FFFF))
76 
77 /**
78   * @}
79   */
80 
81 /** @defgroup FLASH_OB_DATA_ADDRESS
82   * @{
83   */
84 #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804) || ((ADDRESS) == 0x1FFFF806))
85 
86 /**
87   * @}
88   */
89 
90 /** @defgroup FLASH_Option_Bytes_Write_Protection
91   * @{
92   */
93 
94 #define OB_WRP_Pages0to3               ((uint32_t)0x00000001) /* Write protection of page 0 to 3 */
95 #define OB_WRP_Pages4to7               ((uint32_t)0x00000002) /* Write protection of page 4 to 7 */
96 #define OB_WRP_Pages8to11              ((uint32_t)0x00000004) /* Write protection of page 8 to 11 */
97 #define OB_WRP_Pages12to15             ((uint32_t)0x00000008) /* Write protection of page 12 to 15 */
98 #define OB_WRP_Pages16to19             ((uint32_t)0x00000010) /* Write protection of page 16 to 19 */
99 #define OB_WRP_Pages20to23             ((uint32_t)0x00000020) /* Write protection of page 20 to 23 */
100 #define OB_WRP_Pages24to27             ((uint32_t)0x00000040) /* Write protection of page 24 to 27 */
101 #define OB_WRP_Pages28to31             ((uint32_t)0x00000080) /* Write protection of page 28 to 31 */
102 #define OB_WRP_Pages32to35             ((uint32_t)0x00000100) /* Write protection of page 32 to 35 */
103 #define OB_WRP_Pages36to39             ((uint32_t)0x00000200) /* Write protection of page 36 to 39 */
104 #define OB_WRP_Pages40to43             ((uint32_t)0x00000400) /* Write protection of page 40 to 43 */
105 #define OB_WRP_Pages44to47             ((uint32_t)0x00000800) /* Write protection of page 44 to 47 */
106 #define OB_WRP_Pages48to51             ((uint32_t)0x00001000) /* Write protection of page 48 to 51 */
107 #define OB_WRP_Pages52to55             ((uint32_t)0x00002000) /* Write protection of page 52 to 55 */
108 #define OB_WRP_Pages56to59             ((uint32_t)0x00004000) /* Write protection of page 56 to 59 */
109 #define OB_WRP_Pages60to63             ((uint32_t)0x00008000) /* Write protection of page 60 to 63 */
110 
111 #define OB_WRP_AllPages                ((uint32_t)0x0000FFFF) /*!< Write protection of all Sectors */
112 
113 #define IS_OB_WRP(PAGE) (((PAGE) != 0x0000000))
114 
115 
116 /**
117   * @}
118   */
119 
120 /** @defgroup FLASH_Option_Bytes_Read_Protection
121   * @{
122   */
123 
124 /**
125   * @brief  FLASH_Read Protection Level
126   */
127 #define OB_RDP_Level_0   ((uint8_t)0xAA)
128 #define OB_RDP_Level_1   ((uint8_t)0xBB)
129 /*#define OB_RDP_Level_2   ((uint8_t)0xCC)*/ /* Warning: When enabling read protection level 2
130                                                 it's no more possible to go back to level 1 or 0 */
131 
132 #define IS_OB_RDP(LEVEL) (((LEVEL) == OB_RDP_Level_0)||\
133                           ((LEVEL) == OB_RDP_Level_1))/*||\
134                           ((LEVEL) == OB_RDP_Level_2))*/
135 /**
136   * @}
137   */
138 
139 /** @defgroup FLASH_Option_Bytes_IWatchdog
140   * @{
141   */
142 
143 #define OB_IWDG_SW                     ((uint8_t)0x01)  /*!< Software IWDG selected */
144 #define OB_IWDG_HW                     ((uint8_t)0x00)  /*!< Hardware IWDG selected */
145 #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
146 
147 /**
148   * @}
149   */
150 
151 /** @defgroup FLASH_Option_Bytes_nRST_STOP
152   * @{
153   */
154 
155 #define OB_STOP_NoRST                  ((uint8_t)0x02) /*!< No reset generated when entering in STOP */
156 #define OB_STOP_RST                    ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
157 #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
158 
159 /**
160   * @}
161   */
162 
163 /** @defgroup FLASH_Option_Bytes_nRST_STDBY
164   * @{
165   */
166 
167 #define OB_STDBY_NoRST                 ((uint8_t)0x04) /*!< No reset generated when entering in STANDBY */
168 #define OB_STDBY_RST                   ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
169 #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
170 
171 /**
172   * @}
173   */
174 
175 /** @defgroup FLASH_Option_Bytes_BOOT1
176   * @{
177   */
178 
179 #define OB_BOOT1_RESET                 ((uint8_t)0x00) /*!< BOOT1 Reset */
180 #define OB_BOOT1_SET                   ((uint8_t)0x10) /*!< BOOT1 Set */
181 #define IS_OB_BOOT1(BOOT1) (((BOOT1) == OB_BOOT1_RESET) || ((BOOT1) == OB_BOOT1_SET))
182 
183 /**
184   * @}
185   */
186 
187 /** @defgroup FLASH_Option_Bytes_BOOT0
188   * @{
189   */
190 
191 #define OB_BOOT0_RESET                 ((uint8_t)0x00) /*!< BOOT0 Reset */
192 #define OB_BOOT0_SET                   ((uint8_t)0x08) /*!< BOOT0 Set */
193 #define IS_OB_BOOT0(BOOT0) (((BOOT0) == OB_BOOT0_RESET) || ((BOOT0) == OB_BOOT0_SET))
194 
195 /**
196   * @}
197   */
198 
199 /** @defgroup FLASH_Option_Bytes_BOOT0SW
200   * @{
201   */
202 
203 #define OB_BOOT0_SW                   ((uint8_t)0x00) /*!< BOOT0 pin disabled */
204 #define OB_BOOT0_HW                   ((uint8_t)0x80) /*!< BOOT0 pin bonded with GPIO */
205 #define IS_OB_BOOT0SW(BOOT0) (((BOOT0) == OB_BOOT0_SW) || ((BOOT0) == OB_BOOT0_HW))
206 
207 /**
208   * @}
209   */
210 
211 /** @defgroup FLASH_Option_Bytes_VDDA_Analog_Monitoring
212   * @{
213   */
214 
215 #define OB_VDDA_ANALOG_ON              ((uint8_t)0x20) /*!< Analog monitoring on VDDA Power source ON */
216 #define OB_VDDA_ANALOG_OFF             ((uint8_t)0x00) /*!< Analog monitoring on VDDA Power source OFF */
217 
218 #define IS_OB_VDDA_ANALOG(ANALOG) (((ANALOG) == OB_VDDA_ANALOG_ON) || ((ANALOG) == OB_VDDA_ANALOG_OFF))
219 
220 /**
221   * @}
222   */
223 
224 /** @defgroup FLASH_Option_Bytes_SRAM_Parity_Enable
225   * @{
226   */
227 
228 #define OB_SRAM_PARITY_SET              ((uint8_t)0x00) /*!< SRAM parity enable Set */
229 #define OB_SRAM_PARITY_RESET            ((uint8_t)0x40) /*!< SRAM parity enable reset */
230 
231 #define IS_OB_SRAM_PARITY(PARITY) (((PARITY) == OB_SRAM_PARITY_SET) || ((PARITY) == OB_SRAM_PARITY_RESET))
232 
233 /**
234   * @}
235   */
236 
237 /** @defgroup FLASH_Flags
238   * @{
239   */
240 
241 #define FLASH_FLAG_BSY                 FLASH_SR_BSY     /*!< FLASH Busy flag */
242 #define FLASH_FLAG_PGERR               FLASH_SR_PGERR   /*!< FLASH Programming error flag */
243 #define FLASH_FLAG_WRPERR              FLASH_SR_WRPERR  /*!< FLASH Write protected error flag */
244 #define FLASH_FLAG_EOP                 FLASH_SR_EOP     /*!< FLASH End of Programming flag */
245 
246 #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFCB) == 0x00000000) && ((FLAG) != 0x00000000))
247 
248 #define IS_FLASH_GET_FLAG(FLAG)  (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_PGERR) || \
249                                   ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP))
250 /**
251   * @}
252   */
253 
254 /** @defgroup FLASH_Timeout_definition
255   * @{
256   */
257 #define FLASH_ER_PRG_TIMEOUT         ((uint32_t)0x000B0000)
258 
259 /**
260   * @}
261   */
262 
263 /** @defgroup FLASH_Legacy
264   * @{
265   */
266 #define FLASH_WRProt_Pages0to3	       OB_WRP_Pages0to3
267 #define FLASH_WRProt_Pages4to7	       OB_WRP_Pages4to7
268 #define FLASH_WRProt_Pages8to11	       OB_WRP_Pages8to11
269 #define FLASH_WRProt_Pages12to15	   OB_WRP_Pages12to15
270 #define FLASH_WRProt_Pages16to19	   OB_WRP_Pages16to19
271 #define FLASH_WRProt_Pages20to23	   OB_WRP_Pages20to23
272 #define FLASH_WRProt_Pages24to27	   OB_WRP_Pages24to27
273 #define FLASH_WRProt_Pages28to31	   OB_WRP_Pages28to31
274 #define FLASH_WRProt_Pages32to35	   OB_WRP_Pages32to35
275 #define FLASH_WRProt_Pages36to39	   OB_WRP_Pages36to39
276 #define FLASH_WRProt_Pages40to43	   OB_WRP_Pages40to21
277 #define FLASH_WRProt_Pages44to47	   OB_WRP_Pages44to23
278 #define FLASH_WRProt_Pages48to51	   OB_WRP_Pages48to51
279 #define FLASH_WRProt_Pages52to55	   OB_WRP_Pages52to55
280 #define FLASH_WRProt_Pages56to59	   OB_WRP_Pages56to59
281 #define FLASH_WRProt_Pages60to63	   OB_WRP_Pages60to63
282 
283 
284 #define FLASH_WRProt_AllPages          OB_WRP_AllPages
285 /**
286   * @}
287   */
288 
289 /**
290   * @}
291   */
292 
293 /* Exported macro ------------------------------------------------------------*/
294 /* Exported functions ------------------------------------------------------- */
295 
296 /**
297   * @brief  FLASH memory functions that can be executed from FLASH.
298   */
299 /* FLASH Interface configuration functions ************************************/
300 void FLASH_SetLatency(uint32_t FLASH_Latency);
301 void FLASH_PrefetchBufferCmd(FunctionalState NewState);
302 FlagStatus FLASH_GetPrefetchBufferStatus(void);
303 
304 /* FLASH Memory Programming functions *****************************************/
305 void FLASH_Unlock(void);
306 void FLASH_Lock(void);
307 FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
308 FLASH_Status FLASH_EraseAllPages(void);
309 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
310 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
311 
312 /* FLASH Option Bytes Programming functions *****************************************/
313 void FLASH_OB_Unlock(void);
314 void FLASH_OB_Lock(void);
315 void FLASH_OB_Launch(void);
316 FLASH_Status FLASH_OB_Erase(void);
317 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
318 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
319 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
320 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
321 FLASH_Status FLASH_OB_BOOT0Config(uint8_t OB_BOOT0);
322 FLASH_Status FLASH_OB_BOOT0SWConfig(uint8_t OB_BOOT0SW);
323 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
324 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity);
325 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
326 FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
327 uint8_t FLASH_OB_GetUser(void);
328 uint32_t FLASH_OB_GetWRP(void);
329 FlagStatus FLASH_OB_GetRDP(void);
330 
331 /* FLASH Interrupts and flags management functions **********************************/
332 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
333 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
334 void FLASH_ClearFlag(uint32_t FLASH_FLAG);
335 FLASH_Status FLASH_GetStatus(void);
336 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
337 
338 /** @defgroup FLASH_Legacy
339   * @{
340   */
341 #define FLASH_EraseOptionBytes               FLASH_OB_Erase
342 #define FLASH_EnableWriteProtection	         FLASH_OB_EnableWRP
343 #define FLASH_UserOptionByteConfig	         FLASH_OB_UserConfig
344 #define FLASH_ProgramOptionByteData          FLASH_OB_ProgramData
345 #define FLASH_GetUserOptionByte	             FLASH_OB_GetUser
346 #define FLASH_GetWriteProtectionOptionByte   FLASH_OB_GetWRP
347 
348 /**
349   * @}
350   */
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 
356 #endif /* __HK32F0XX_FLASH_H */
357 
358 
359