1 /**
2   ******************************************************************************
3   * @file    stm32f10x_flash.h
4   * @author  MCD Application Team
5   * @version V3.1.2
6   * @date    09/28/2009
7   * @brief   This file contains all the functions prototypes for the FLASH
8   *          firmware library.
9   ******************************************************************************
10   * @copy
11   *
12   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
15   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18   *
19   * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
20   */
21 
22 /* Define to prevent recursive inclusion -------------------------------------*/
23 #ifndef __STM32F10x_FLASH_H
24 #define __STM32F10x_FLASH_H
25 
26 #ifdef __cplusplus
27  extern "C" {
28 #endif
29 
30 /* Includes ------------------------------------------------------------------*/
31 #include "stm32f10x.h"
32 
33 /** @addtogroup STM32F10x_StdPeriph_Driver
34   * @{
35   */
36 
37 /** @addtogroup FLASH
38   * @{
39   */
40 
41 /** @defgroup FLASH_Exported_Types
42   * @{
43   */
44 
45 /**
46   * @brief  FLASH Status
47   */
48 
49 typedef enum
50 {
51   FLASH_BUSY = 1,
52   FLASH_ERROR_PG,
53   FLASH_ERROR_WRP,
54   FLASH_COMPLETE,
55   FLASH_TIMEOUT
56 }FLASH_Status;
57 
58 /**
59   * @}
60   */
61 
62 /** @defgroup FLASH_Exported_Constants
63   * @{
64   */
65 
66 /** @defgroup Flash_Latency
67   * @{
68   */
69 
70 #define FLASH_Latency_0                ((uint32_t)0x00000000)  /*!< FLASH Zero Latency cycle */
71 #define FLASH_Latency_1                ((uint32_t)0x00000001)  /*!< FLASH One Latency cycle */
72 #define FLASH_Latency_2                ((uint32_t)0x00000002)  /*!< FLASH Two Latency cycles */
73 #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0) || \
74                                    ((LATENCY) == FLASH_Latency_1) || \
75                                    ((LATENCY) == FLASH_Latency_2))
76 /**
77   * @}
78   */
79 
80 /** @defgroup Half_Cycle_Enable_Disable
81   * @{
82   */
83 
84 #define FLASH_HalfCycleAccess_Enable   ((uint32_t)0x00000008)  /*!< FLASH Half Cycle Enable */
85 #define FLASH_HalfCycleAccess_Disable  ((uint32_t)0x00000000)  /*!< FLASH Half Cycle Disable */
86 #define IS_FLASH_HALFCYCLEACCESS_STATE(STATE) (((STATE) == FLASH_HalfCycleAccess_Enable) || \
87                                                ((STATE) == FLASH_HalfCycleAccess_Disable))
88 /**
89   * @}
90   */
91 
92 /** @defgroup Prefetch_Buffer_Enable_Disable
93   * @{
94   */
95 
96 #define FLASH_PrefetchBuffer_Enable    ((uint32_t)0x00000010)  /*!< FLASH Prefetch Buffer Enable */
97 #define FLASH_PrefetchBuffer_Disable   ((uint32_t)0x00000000)  /*!< FLASH Prefetch Buffer Disable */
98 #define IS_FLASH_PREFETCHBUFFER_STATE(STATE) (((STATE) == FLASH_PrefetchBuffer_Enable) || \
99                                               ((STATE) == FLASH_PrefetchBuffer_Disable))
100 /**
101   * @}
102   */
103 
104 /** @defgroup Option_Bytes_Write_Protection
105   * @{
106   */
107 
108 /* Values to be used with STM32 Low and Medium density devices */
109 #define FLASH_WRProt_Pages0to3         ((uint32_t)0x00000001) /*!< STM32 Low and Medium density devices: Write protection of page 0 to 3 */
110 #define FLASH_WRProt_Pages4to7         ((uint32_t)0x00000002) /*!< STM32 Low and Medium density devices: Write protection of page 4 to 7 */
111 #define FLASH_WRProt_Pages8to11        ((uint32_t)0x00000004) /*!< STM32 Low and Medium density devices: Write protection of page 8 to 11 */
112 #define FLASH_WRProt_Pages12to15       ((uint32_t)0x00000008) /*!< STM32 Low and Medium density devices: Write protection of page 12 to 15 */
113 #define FLASH_WRProt_Pages16to19       ((uint32_t)0x00000010) /*!< STM32 Low and Medium density devices: Write protection of page 16 to 19 */
114 #define FLASH_WRProt_Pages20to23       ((uint32_t)0x00000020) /*!< STM32 Low and Medium density devices: Write protection of page 20 to 23 */
115 #define FLASH_WRProt_Pages24to27       ((uint32_t)0x00000040) /*!< STM32 Low and Medium density devices: Write protection of page 24 to 27 */
116 #define FLASH_WRProt_Pages28to31       ((uint32_t)0x00000080) /*!< STM32 Low and Medium density devices: Write protection of page 28 to 31 */
117 
118 /* Values to be used with STM32 Medium-density devices */
119 #define FLASH_WRProt_Pages32to35       ((uint32_t)0x00000100) /*!< STM32 Medium-density devices: Write protection of page 32 to 35 */
120 #define FLASH_WRProt_Pages36to39       ((uint32_t)0x00000200) /*!< STM32 Medium-density devices: Write protection of page 36 to 39 */
121 #define FLASH_WRProt_Pages40to43       ((uint32_t)0x00000400) /*!< STM32 Medium-density devices: Write protection of page 40 to 43 */
122 #define FLASH_WRProt_Pages44to47       ((uint32_t)0x00000800) /*!< STM32 Medium-density devices: Write protection of page 44 to 47 */
123 #define FLASH_WRProt_Pages48to51       ((uint32_t)0x00001000) /*!< STM32 Medium-density devices: Write protection of page 48 to 51 */
124 #define FLASH_WRProt_Pages52to55       ((uint32_t)0x00002000) /*!< STM32 Medium-density devices: Write protection of page 52 to 55 */
125 #define FLASH_WRProt_Pages56to59       ((uint32_t)0x00004000) /*!< STM32 Medium-density devices: Write protection of page 56 to 59 */
126 #define FLASH_WRProt_Pages60to63       ((uint32_t)0x00008000) /*!< STM32 Medium-density devices: Write protection of page 60 to 63 */
127 #define FLASH_WRProt_Pages64to67       ((uint32_t)0x00010000) /*!< STM32 Medium-density devices: Write protection of page 64 to 67 */
128 #define FLASH_WRProt_Pages68to71       ((uint32_t)0x00020000) /*!< STM32 Medium-density devices: Write protection of page 68 to 71 */
129 #define FLASH_WRProt_Pages72to75       ((uint32_t)0x00040000) /*!< STM32 Medium-density devices: Write protection of page 72 to 75 */
130 #define FLASH_WRProt_Pages76to79       ((uint32_t)0x00080000) /*!< STM32 Medium-density devices: Write protection of page 76 to 79 */
131 #define FLASH_WRProt_Pages80to83       ((uint32_t)0x00100000) /*!< STM32 Medium-density devices: Write protection of page 80 to 83 */
132 #define FLASH_WRProt_Pages84to87       ((uint32_t)0x00200000) /*!< STM32 Medium-density devices: Write protection of page 84 to 87 */
133 #define FLASH_WRProt_Pages88to91       ((uint32_t)0x00400000) /*!< STM32 Medium-density devices: Write protection of page 88 to 91 */
134 #define FLASH_WRProt_Pages92to95       ((uint32_t)0x00800000) /*!< STM32 Medium-density devices: Write protection of page 92 to 95 */
135 #define FLASH_WRProt_Pages96to99       ((uint32_t)0x01000000) /*!< STM32 Medium-density devices: Write protection of page 96 to 99 */
136 #define FLASH_WRProt_Pages100to103     ((uint32_t)0x02000000) /*!< STM32 Medium-density devices: Write protection of page 100 to 103 */
137 #define FLASH_WRProt_Pages104to107     ((uint32_t)0x04000000) /*!< STM32 Medium-density devices: Write protection of page 104 to 107 */
138 #define FLASH_WRProt_Pages108to111     ((uint32_t)0x08000000) /*!< STM32 Medium-density devices: Write protection of page 108 to 111 */
139 #define FLASH_WRProt_Pages112to115     ((uint32_t)0x10000000) /*!< STM32 Medium-density devices: Write protection of page 112 to 115 */
140 #define FLASH_WRProt_Pages116to119     ((uint32_t)0x20000000) /*!< STM32 Medium-density devices: Write protection of page 115 to 119 */
141 #define FLASH_WRProt_Pages120to123     ((uint32_t)0x40000000) /*!< STM32 Medium-density devices: Write protection of page 120 to 123 */
142 #define FLASH_WRProt_Pages124to127     ((uint32_t)0x80000000) /*!< STM32 Medium-density devices: Write protection of page 124 to 127 */
143 
144 /* Values to be used with STM32 High-density and STM32F10X Connectivity line devices */
145 #define FLASH_WRProt_Pages0to1         ((uint32_t)0x00000001) /*!< STM32 Medium-density and Connectivity line devices:
146                                                                    Write protection of page 0 to 1 */
147 #define FLASH_WRProt_Pages2to3         ((uint32_t)0x00000002) /*!< STM32 Medium-density and Connectivity line devices:
148                                                                    Write protection of page 2 to 3 */
149 #define FLASH_WRProt_Pages4to5         ((uint32_t)0x00000004) /*!< STM32 Medium-density and Connectivity line devices:
150                                                                    Write protection of page 4 to 5 */
151 #define FLASH_WRProt_Pages6to7         ((uint32_t)0x00000008) /*!< STM32 Medium-density and Connectivity line devices:
152                                                                    Write protection of page 6 to 7 */
153 #define FLASH_WRProt_Pages8to9         ((uint32_t)0x00000010) /*!< STM32 Medium-density and Connectivity line devices:
154                                                                    Write protection of page 8 to 9 */
155 #define FLASH_WRProt_Pages10to11       ((uint32_t)0x00000020) /*!< STM32 Medium-density and Connectivity line devices:
156                                                                    Write protection of page 10 to 11 */
157 #define FLASH_WRProt_Pages12to13       ((uint32_t)0x00000040) /*!< STM32 Medium-density and Connectivity line devices:
158                                                                    Write protection of page 12 to 13 */
159 #define FLASH_WRProt_Pages14to15       ((uint32_t)0x00000080) /*!< STM32 Medium-density and Connectivity line devices:
160                                                                    Write protection of page 14 to 15 */
161 #define FLASH_WRProt_Pages16to17       ((uint32_t)0x00000100) /*!< STM32 Medium-density and Connectivity line devices:
162                                                                    Write protection of page 16 to 17 */
163 #define FLASH_WRProt_Pages18to19       ((uint32_t)0x00000200) /*!< STM32 Medium-density and Connectivity line devices:
164                                                                    Write protection of page 18 to 19 */
165 #define FLASH_WRProt_Pages20to21       ((uint32_t)0x00000400) /*!< STM32 Medium-density and Connectivity line devices:
166                                                                    Write protection of page 20 to 21 */
167 #define FLASH_WRProt_Pages22to23       ((uint32_t)0x00000800) /*!< STM32 Medium-density and Connectivity line devices:
168                                                                    Write protection of page 22 to 23 */
169 #define FLASH_WRProt_Pages24to25       ((uint32_t)0x00001000) /*!< STM32 Medium-density and Connectivity line devices:
170                                                                    Write protection of page 24 to 25 */
171 #define FLASH_WRProt_Pages26to27       ((uint32_t)0x00002000) /*!< STM32 Medium-density and Connectivity line devices:
172                                                                    Write protection of page 26 to 27 */
173 #define FLASH_WRProt_Pages28to29       ((uint32_t)0x00004000) /*!< STM32 Medium-density and Connectivity line devices:
174                                                                    Write protection of page 28 to 29 */
175 #define FLASH_WRProt_Pages30to31       ((uint32_t)0x00008000) /*!< STM32 Medium-density and Connectivity line devices:
176                                                                    Write protection of page 30 to 31 */
177 #define FLASH_WRProt_Pages32to33       ((uint32_t)0x00010000) /*!< STM32 Medium-density and Connectivity line devices:
178                                                                    Write protection of page 32 to 33 */
179 #define FLASH_WRProt_Pages34to35       ((uint32_t)0x00020000) /*!< STM32 Medium-density and Connectivity line devices:
180                                                                    Write protection of page 34 to 35 */
181 #define FLASH_WRProt_Pages36to37       ((uint32_t)0x00040000) /*!< STM32 Medium-density and Connectivity line devices:
182                                                                    Write protection of page 36 to 37 */
183 #define FLASH_WRProt_Pages38to39       ((uint32_t)0x00080000) /*!< STM32 Medium-density and Connectivity line devices:
184                                                                    Write protection of page 38 to 39 */
185 #define FLASH_WRProt_Pages40to41       ((uint32_t)0x00100000) /*!< STM32 Medium-density and Connectivity line devices:
186                                                                    Write protection of page 40 to 41 */
187 #define FLASH_WRProt_Pages42to43       ((uint32_t)0x00200000) /*!< STM32 Medium-density and Connectivity line devices:
188                                                                    Write protection of page 42 to 43 */
189 #define FLASH_WRProt_Pages44to45       ((uint32_t)0x00400000) /*!< STM32 Medium-density and Connectivity line devices:
190                                                                    Write protection of page 44 to 45 */
191 #define FLASH_WRProt_Pages46to47       ((uint32_t)0x00800000) /*!< STM32 Medium-density and Connectivity line devices:
192                                                                    Write protection of page 46 to 47 */
193 #define FLASH_WRProt_Pages48to49       ((uint32_t)0x01000000) /*!< STM32 Medium-density and Connectivity line devices:
194                                                                    Write protection of page 48 to 49 */
195 #define FLASH_WRProt_Pages50to51       ((uint32_t)0x02000000) /*!< STM32 Medium-density and Connectivity line devices:
196                                                                    Write protection of page 50 to 51 */
197 #define FLASH_WRProt_Pages52to53       ((uint32_t)0x04000000) /*!< STM32 Medium-density and Connectivity line devices:
198                                                                    Write protection of page 52 to 53 */
199 #define FLASH_WRProt_Pages54to55       ((uint32_t)0x08000000) /*!< STM32 Medium-density and Connectivity line devices:
200                                                                    Write protection of page 54 to 55 */
201 #define FLASH_WRProt_Pages56to57       ((uint32_t)0x10000000) /*!< STM32 Medium-density and Connectivity line devices:
202                                                                    Write protection of page 56 to 57 */
203 #define FLASH_WRProt_Pages58to59       ((uint32_t)0x20000000) /*!< STM32 Medium-density and Connectivity line devices:
204                                                                    Write protection of page 58 to 59 */
205 #define FLASH_WRProt_Pages60to61       ((uint32_t)0x40000000) /*!< STM32 Medium-density and Connectivity line devices:
206                                                                    Write protection of page 60 to 61 */
207 #define FLASH_WRProt_Pages62to127      ((uint32_t)0x80000000) /*!< STM32 Connectivity line devices: Write protection of page 62 to 127 */
208 #define FLASH_WRProt_Pages62to255      ((uint32_t)0x80000000) /*!< STM32 Medium-density devices: Write protection of page 62 to 255 */
209 
210 #define FLASH_WRProt_AllPages          ((uint32_t)0xFFFFFFFF) /*!< Write protection of all Pages */
211 
212 #define IS_FLASH_WRPROT_PAGE(PAGE) (((PAGE) != 0x00000000))
213 
214 #define IS_FLASH_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x0807FFFF))
215 
216 #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804) || ((ADDRESS) == 0x1FFFF806))
217 
218 /**
219   * @}
220   */
221 
222 /** @defgroup Option_Bytes_IWatchdog
223   * @{
224   */
225 
226 #define OB_IWDG_SW                     ((uint16_t)0x0001)  /*!< Software IWDG selected */
227 #define OB_IWDG_HW                     ((uint16_t)0x0000)  /*!< Hardware IWDG selected */
228 #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
229 
230 /**
231   * @}
232   */
233 
234 /** @defgroup Option_Bytes_nRST_STOP
235   * @{
236   */
237 
238 #define OB_STOP_NoRST                  ((uint16_t)0x0002) /*!< No reset generated when entering in STOP */
239 #define OB_STOP_RST                    ((uint16_t)0x0000) /*!< Reset generated when entering in STOP */
240 #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NoRST) || ((SOURCE) == OB_STOP_RST))
241 
242 /**
243   * @}
244   */
245 
246 /** @defgroup Option_Bytes_nRST_STDBY
247   * @{
248   */
249 
250 #define OB_STDBY_NoRST                 ((uint16_t)0x0004) /*!< No reset generated when entering in STANDBY */
251 #define OB_STDBY_RST                   ((uint16_t)0x0000) /*!< Reset generated when entering in STANDBY */
252 #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NoRST) || ((SOURCE) == OB_STDBY_RST))
253 
254 /**
255   * @}
256   */
257 
258 /** @defgroup FLASH_Interrupts
259   * @{
260   */
261 
262 #define FLASH_IT_ERROR                 ((uint32_t)0x00000400)  /*!< FPEC error interrupt source */
263 #define FLASH_IT_EOP                   ((uint32_t)0x00001000)  /*!< End of FLASH Operation Interrupt source */
264 #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && (((IT) != 0x00000000)))
265 
266 /**
267   * @}
268   */
269 
270 /** @defgroup FLASH_Flags
271   * @{
272   */
273 
274 #define FLASH_FLAG_BSY                 ((uint32_t)0x00000001)  /*!< FLASH Busy flag */
275 #define FLASH_FLAG_EOP                 ((uint32_t)0x00000020)  /*!< FLASH End of Operation flag */
276 #define FLASH_FLAG_PGERR               ((uint32_t)0x00000004)  /*!< FLASH Program error flag */
277 #define FLASH_FLAG_WRPRTERR            ((uint32_t)0x00000010)  /*!< FLASH Write protected error flag */
278 #define FLASH_FLAG_OPTERR              ((uint32_t)0x00000001)  /*!< FLASH Option Byte error flag */
279 
280 #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFCA) == 0x00000000) && ((FLAG) != 0x00000000))
281 #define IS_FLASH_GET_FLAG(FLAG)  (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_EOP) || \
282                                   ((FLAG) == FLASH_FLAG_PGERR) || ((FLAG) == FLASH_FLAG_WRPRTERR) || \
283                                   ((FLAG) == FLASH_FLAG_OPTERR))
284 
285 /**
286   * @}
287   */
288 
289 /**
290   * @}
291   */
292 
293 /** @defgroup FLASH_Exported_Macros
294   * @{
295   */
296 
297 /**
298   * @}
299   */
300 
301 /** @defgroup FLASH_Exported_Functions
302   * @{
303   */
304 
305 void FLASH_SetLatency(uint32_t FLASH_Latency);
306 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);
307 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer);
308 void FLASH_Unlock(void);
309 void FLASH_Lock(void);
310 FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
311 FLASH_Status FLASH_EraseAllPages(void);
312 FLASH_Status FLASH_EraseOptionBytes(void);
313 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
314 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
315 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data);
316 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages);
317 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState);
318 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY);
319 uint32_t FLASH_GetUserOptionByte(void);
320 uint32_t FLASH_GetWriteProtectionOptionByte(void);
321 FlagStatus FLASH_GetReadOutProtectionStatus(void);
322 FlagStatus FLASH_GetPrefetchBufferStatus(void);
323 void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState);
324 FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG);
325 void FLASH_ClearFlag(uint16_t FLASH_FLAG);
326 FLASH_Status FLASH_GetStatus(void);
327 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
328 
329 #ifdef __cplusplus
330 }
331 #endif
332 
333 #endif /* __STM32F10x_FLASH_H */
334 /**
335   * @}
336   */
337 
338 /**
339   * @}
340   */
341 
342 /**
343   * @}
344   */
345 
346 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
347