Build libraries for stm32l1xx and stm32f10x
[fw/stlink] / example / libs_stm / src / stm32f10x / stm32f10x_flash.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_flash.c\r
4   * @author  MCD Application Team\r
5   * @version V3.3.0\r
6   * @date    04/16/2010\r
7   * @brief   This file provides all the FLASH firmware functions.\r
8   ******************************************************************************\r
9   * @copy\r
10   *\r
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32f10x_flash.h"\r
23 \r
24 /** @addtogroup STM32F10x_StdPeriph_Driver\r
25   * @{\r
26   */\r
27 \r
28 /** @defgroup FLASH \r
29   * @brief FLASH driver modules\r
30   * @{\r
31   */ \r
32 \r
33 /** @defgroup FLASH_Private_TypesDefinitions\r
34   * @{\r
35   */\r
36 \r
37 /**\r
38   * @}\r
39   */ \r
40 \r
41 /** @defgroup FLASH_Private_Defines\r
42   * @{\r
43   */ \r
44 \r
45 /* Flash Access Control Register bits */\r
46 #define ACR_LATENCY_Mask         ((uint32_t)0x00000038)\r
47 #define ACR_HLFCYA_Mask          ((uint32_t)0xFFFFFFF7)\r
48 #define ACR_PRFTBE_Mask          ((uint32_t)0xFFFFFFEF)\r
49 \r
50 /* Flash Access Control Register bits */\r
51 #define ACR_PRFTBS_Mask          ((uint32_t)0x00000020) \r
52 \r
53 /* Flash Control Register bits */\r
54 #define CR_PG_Set                ((uint32_t)0x00000001)\r
55 #define CR_PG_Reset              ((uint32_t)0x00001FFE) \r
56 #define CR_PER_Set               ((uint32_t)0x00000002)\r
57 #define CR_PER_Reset             ((uint32_t)0x00001FFD)\r
58 #define CR_MER_Set               ((uint32_t)0x00000004)\r
59 #define CR_MER_Reset             ((uint32_t)0x00001FFB)\r
60 #define CR_OPTPG_Set             ((uint32_t)0x00000010)\r
61 #define CR_OPTPG_Reset           ((uint32_t)0x00001FEF)\r
62 #define CR_OPTER_Set             ((uint32_t)0x00000020)\r
63 #define CR_OPTER_Reset           ((uint32_t)0x00001FDF)\r
64 #define CR_STRT_Set              ((uint32_t)0x00000040)\r
65 #define CR_LOCK_Set              ((uint32_t)0x00000080)\r
66 \r
67 /* FLASH Mask */\r
68 #define RDPRT_Mask               ((uint32_t)0x00000002)\r
69 #define WRP0_Mask                ((uint32_t)0x000000FF)\r
70 #define WRP1_Mask                ((uint32_t)0x0000FF00)\r
71 #define WRP2_Mask                ((uint32_t)0x00FF0000)\r
72 #define WRP3_Mask                ((uint32_t)0xFF000000)\r
73 #define OB_USER_BFB2             ((uint16_t)0x0008)\r
74 \r
75 /* FLASH Keys */\r
76 #define RDP_Key                  ((uint16_t)0x00A5)\r
77 #define FLASH_KEY1               ((uint32_t)0x45670123)\r
78 #define FLASH_KEY2               ((uint32_t)0xCDEF89AB)\r
79 \r
80 /* FLASH BANK address */\r
81 #define FLASH_BANK1_END_ADDRESS   ((uint32_t)0x807FFFF)\r
82 \r
83 /* Delay definition */   \r
84 #define EraseTimeout          ((uint32_t)0x000B0000)\r
85 #define ProgramTimeout        ((uint32_t)0x00002000)\r
86 /**\r
87   * @}\r
88   */ \r
89 \r
90 /** @defgroup FLASH_Private_Macros\r
91   * @{\r
92   */\r
93 \r
94 /**\r
95   * @}\r
96   */ \r
97 \r
98 /** @defgroup FLASH_Private_Variables\r
99   * @{\r
100   */\r
101 \r
102 /**\r
103   * @}\r
104   */ \r
105 \r
106 /** @defgroup FLASH_Private_FunctionPrototypes\r
107   * @{\r
108   */\r
109   \r
110 /**\r
111   * @}\r
112   */\r
113 \r
114 /** @defgroup FLASH_Private_Functions\r
115   * @{\r
116   */\r
117 \r
118 /**\r
119 @code  \r
120  \r
121  This driver provides functions to configure and program the Flash memory of all STM32F10x devices,\r
122  including the latest STM32F10x_XL density devices. \r
123 \r
124  STM32F10x_XL devices feature up to 1 Mbyte with dual bank architecture for read-while-write (RWW) capability:\r
125     - bank1: fixed size of 512 Kbytes (256 pages of 2Kbytes each)\r
126     - bank2: up to 512 Kbytes (up to 256 pages of 2Kbytes each)\r
127  While other STM32F10x devices features only one bank with memory up to 512 Kbytes.\r
128 \r
129  In version V3.3.0, some functions were updated and new ones were added to support\r
130  STM32F10x_XL devices. Thus some functions manages all devices, while other are \r
131  dedicated for XL devices only.\r
132  \r
133  The table below presents the list of available functions depending on the used STM32F10x devices.  \r
134       \r
135    ***************************************************\r
136    * Legacy functions used for all STM32F10x devices *\r
137    ***************************************************\r
138    +----------------------------------------------------------------------------------------------------------------------------------+\r
139    |       Functions prototypes         |STM32F10x_XL|Other STM32F10x|    Comments                                                    |\r
140    |                                    |   devices  |  devices      |                                                                |\r
141    |----------------------------------------------------------------------------------------------------------------------------------|\r
142    |FLASH_SetLatency                    |    Yes     |      Yes      | No change                                                      |\r
143    |----------------------------------------------------------------------------------------------------------------------------------|\r
144    |FLASH_HalfCycleAccessCmd            |    Yes     |      Yes      | No change                                                      |\r
145    |----------------------------------------------------------------------------------------------------------------------------------|\r
146    |FLASH_PrefetchBufferCmd             |    Yes     |      Yes      | No change                                                      |\r
147    |----------------------------------------------------------------------------------------------------------------------------------|\r
148    |FLASH_Unlock                        |    Yes     |      Yes      | - For STM32F10X_XL devices: unlock Bank1 and Bank2.            |\r
149    |                                    |            |               | - For other devices: unlock Bank1 and it is equivalent         |\r
150    |                                    |            |               |   to FLASH_UnlockBank1 function.                               |\r
151    |----------------------------------------------------------------------------------------------------------------------------------|\r
152    |FLASH_Lock                          |    Yes     |      Yes      | - For STM32F10X_XL devices: lock Bank1 and Bank2.              |\r
153    |                                    |            |               | - For other devices: lock Bank1 and it is equivalent           |\r
154    |                                    |            |               |   to FLASH_LockBank1 function.                                 |\r
155    |----------------------------------------------------------------------------------------------------------------------------------|\r
156    |FLASH_ErasePage                     |    Yes     |      Yes      | - For STM32F10x_XL devices: erase a page in Bank1 and Bank2    |\r
157    |                                    |            |               | - For other devices: erase a page in Bank1                     |\r
158    |----------------------------------------------------------------------------------------------------------------------------------|\r
159    |FLASH_EraseAllPages                 |    Yes     |      Yes      | - For STM32F10x_XL devices: erase all pages in Bank1 and Bank2 |\r
160    |                                    |            |               | - For other devices: erase all pages in Bank1                  |\r
161    |----------------------------------------------------------------------------------------------------------------------------------|\r
162    |FLASH_EraseOptionBytes              |    Yes     |      Yes      | No change                                                      |\r
163    |----------------------------------------------------------------------------------------------------------------------------------|\r
164    |FLASH_ProgramWord                   |    Yes     |      Yes      | Updated to program up to 1MByte (depending on the used device) |\r
165    |----------------------------------------------------------------------------------------------------------------------------------|\r
166    |FLASH_ProgramHalfWord               |    Yes     |      Yes      | Updated to program up to 1MByte (depending on the used device) |\r
167    |----------------------------------------------------------------------------------------------------------------------------------|\r
168    |FLASH_ProgramOptionByteData         |    Yes     |      Yes      | No change                                                      |\r
169    |----------------------------------------------------------------------------------------------------------------------------------|\r
170    |FLASH_EnableWriteProtection         |    Yes     |      Yes      | No change                                                      |\r
171    |----------------------------------------------------------------------------------------------------------------------------------|\r
172    |FLASH_ReadOutProtection             |    Yes     |      Yes      | No change                                                      |\r
173    |----------------------------------------------------------------------------------------------------------------------------------|\r
174    |FLASH_UserOptionByteConfig          |    Yes     |      Yes      | No change                                                      |\r
175    |----------------------------------------------------------------------------------------------------------------------------------|\r
176    |FLASH_GetUserOptionByte             |    Yes     |      Yes      | No change                                                      |\r
177    |----------------------------------------------------------------------------------------------------------------------------------|\r
178    |FLASH_GetWriteProtectionOptionByte  |    Yes     |      Yes      | No change                                                      |\r
179    |----------------------------------------------------------------------------------------------------------------------------------|\r
180    |FLASH_GetReadOutProtectionStatus    |    Yes     |      Yes      | No change                                                      |\r
181    |----------------------------------------------------------------------------------------------------------------------------------|\r
182    |FLASH_GetPrefetchBufferStatus       |    Yes     |      Yes      | No change                                                      |\r
183    |----------------------------------------------------------------------------------------------------------------------------------|\r
184    |FLASH_ITConfig                      |    Yes     |      Yes      | - For STM32F10x_XL devices: enable Bank1 and Bank2's interrupts|\r
185    |                                    |            |               | - For other devices: enable Bank1's interrupts                 |\r
186    |----------------------------------------------------------------------------------------------------------------------------------|\r
187    |FLASH_GetFlagStatus                 |    Yes     |      Yes      | - For STM32F10x_XL devices: return Bank1 and Bank2's flag status|\r
188    |                                    |            |               | - For other devices: return Bank1's flag status                |\r
189    |----------------------------------------------------------------------------------------------------------------------------------|\r
190    |FLASH_ClearFlag                     |    Yes     |      Yes      | - For STM32F10x_XL devices: clear Bank1 and Bank2's flag       |\r
191    |                                    |            |               | - For other devices: clear Bank1's flag                        |\r
192    |----------------------------------------------------------------------------------------------------------------------------------|\r
193    |FLASH_GetStatus                     |    Yes     |      Yes      | - Return the status of Bank1 (for all devices)                 |\r
194    |                                    |            |               |   equivalent to FLASH_GetBank1Status function                  |\r
195    |----------------------------------------------------------------------------------------------------------------------------------|\r
196    |FLASH_WaitForLastOperation          |    Yes     |      Yes      | - Wait for Bank1 last operation (for all devices)              |\r
197    |                                    |            |               |   equivalent to: FLASH_WaitForLastBank1Operation function      |\r
198    +----------------------------------------------------------------------------------------------------------------------------------+\r
199 \r
200    ************************************************************************************************************************\r
201    * New functions used for all STM32F10x devices to manage Bank1:                                                        *\r
202    *   - These functions are mainly useful for STM32F10x_XL density devices, to have separate control for Bank1 and bank2 *\r
203    *   - For other devices, these functions are optional (covered by functions listed above)                              *\r
204    ************************************************************************************************************************\r
205    +----------------------------------------------------------------------------------------------------------------------------------+\r
206    |       Functions prototypes         |STM32F10x_XL|Other STM32F10x|    Comments                                                    |\r
207    |                                    |   devices  |  devices      |                                                                |\r
208    |----------------------------------------------------------------------------------------------------------------------------------|\r
209    | FLASH_UnlockBank1                  |    Yes     |      Yes      | - Unlock Bank1                                                 |\r
210    |----------------------------------------------------------------------------------------------------------------------------------|\r
211    |FLASH_LockBank1                     |    Yes     |      Yes      | - Lock Bank1                                                   |\r
212    |----------------------------------------------------------------------------------------------------------------------------------|\r
213    | FLASH_EraseAllBank1Pages           |    Yes     |      Yes      | - Erase all pages in Bank1                                     |\r
214    |----------------------------------------------------------------------------------------------------------------------------------|\r
215    | FLASH_GetBank1Status               |    Yes     |      Yes      | - Return the status of Bank1                                   |\r
216    |----------------------------------------------------------------------------------------------------------------------------------|\r
217    | FLASH_WaitForLastBank1Operation    |    Yes     |      Yes      | - Wait for Bank1 last operation                                |\r
218    +----------------------------------------------------------------------------------------------------------------------------------+\r
219 \r
220    *****************************************************************************\r
221    * New Functions used only with STM32F10x_XL density devices to manage Bank2 *\r
222    *****************************************************************************\r
223    +----------------------------------------------------------------------------------------------------------------------------------+\r
224    |       Functions prototypes         |STM32F10x_XL|Other STM32F10x|    Comments                                                    |\r
225    |                                    |   devices  |  devices      |                                                                |\r
226    |----------------------------------------------------------------------------------------------------------------------------------|\r
227    | FLASH_UnlockBank2                  |    Yes     |      No       | - Unlock Bank2                                                 |\r
228    |----------------------------------------------------------------------------------------------------------------------------------|\r
229    |FLASH_LockBank2                     |    Yes     |      No       | - Lock Bank2                                                   |\r
230    |----------------------------------------------------------------------------------------------------------------------------------|\r
231    | FLASH_EraseAllBank2Pages           |    Yes     |      No       | - Erase all pages in Bank2                                     |\r
232    |----------------------------------------------------------------------------------------------------------------------------------|\r
233    | FLASH_GetBank2Status               |    Yes     |      No       | - Return the status of Bank2                                   |\r
234    |----------------------------------------------------------------------------------------------------------------------------------|\r
235    | FLASH_WaitForLastBank2Operation    |    Yes     |      No       | - Wait for Bank2 last operation                                |\r
236    |----------------------------------------------------------------------------------------------------------------------------------|\r
237    | FLASH_BootConfig                   |    Yes     |      No       | - Configure to boot from Bank1 or Bank2                        |\r
238    +----------------------------------------------------------------------------------------------------------------------------------+\r
239 @endcode\r
240 */\r
241 \r
242 \r
243 /**\r
244   * @brief  Sets the code latency value.\r
245   * @note   This function can be used for all STM32F10x devices.\r
246   * @param  FLASH_Latency: specifies the FLASH Latency value.\r
247   *   This parameter can be one of the following values:\r
248   *     @arg FLASH_Latency_0: FLASH Zero Latency cycle\r
249   *     @arg FLASH_Latency_1: FLASH One Latency cycle\r
250   *     @arg FLASH_Latency_2: FLASH Two Latency cycles\r
251   * @retval None\r
252   */\r
253 void FLASH_SetLatency(uint32_t FLASH_Latency)\r
254 {\r
255   uint32_t tmpreg = 0;\r
256   \r
257   /* Check the parameters */\r
258   assert_param(IS_FLASH_LATENCY(FLASH_Latency));\r
259   \r
260   /* Read the ACR register */\r
261   tmpreg = FLASH->ACR;  \r
262   \r
263   /* Sets the Latency value */\r
264   tmpreg &= ACR_LATENCY_Mask;\r
265   tmpreg |= FLASH_Latency;\r
266   \r
267   /* Write the ACR register */\r
268   FLASH->ACR = tmpreg;\r
269 }\r
270 \r
271 /**\r
272   * @brief  Enables or disables the Half cycle flash access.\r
273   * @note   This function can be used for all STM32F10x devices.\r
274   * @param  FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.\r
275   *   This parameter can be one of the following values:\r
276   *     @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable\r
277   *     @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable\r
278   * @retval None\r
279   */\r
280 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)\r
281 {\r
282   /* Check the parameters */\r
283   assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));\r
284   \r
285   /* Enable or disable the Half cycle access */\r
286   FLASH->ACR &= ACR_HLFCYA_Mask;\r
287   FLASH->ACR |= FLASH_HalfCycleAccess;\r
288 }\r
289 \r
290 /**\r
291   * @brief  Enables or disables the Prefetch Buffer.\r
292   * @note   This function can be used for all STM32F10x devices.\r
293   * @param  FLASH_PrefetchBuffer: specifies the Prefetch buffer status.\r
294   *   This parameter can be one of the following values:\r
295   *     @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable\r
296   *     @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable\r
297   * @retval None\r
298   */\r
299 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)\r
300 {\r
301   /* Check the parameters */\r
302   assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));\r
303   \r
304   /* Enable or disable the Prefetch Buffer */\r
305   FLASH->ACR &= ACR_PRFTBE_Mask;\r
306   FLASH->ACR |= FLASH_PrefetchBuffer;\r
307 }\r
308 \r
309 /**\r
310   * @brief  Unlocks the FLASH Program Erase Controller.\r
311   * @note   This function can be used for all STM32F10x devices.\r
312   *         - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.\r
313   *         - For all other devices it unlocks Bank1 and it is equivalent \r
314   *           to FLASH_UnlockBank1 function.. \r
315   * @param  None\r
316   * @retval None\r
317   */\r
318 void FLASH_Unlock(void)\r
319 {\r
320   /* Authorize the FPEC of Bank1 Access */\r
321   FLASH->KEYR = FLASH_KEY1;\r
322   FLASH->KEYR = FLASH_KEY2;\r
323 \r
324 #ifdef STM32F10X_XL\r
325   /* Authorize the FPEC of Bank2 Access */\r
326   FLASH->KEYR2 = FLASH_KEY1;\r
327   FLASH->KEYR2 = FLASH_KEY2;\r
328 #endif /* STM32F10X_XL */\r
329 }\r
330 /**\r
331   * @brief  Unlocks the FLASH Bank1 Program Erase Controller.\r
332   * @note   This function can be used for all STM32F10x devices.\r
333   *         - For STM32F10X_XL devices this function unlocks Bank1.\r
334   *         - For all other devices it unlocks Bank1 and it is \r
335   *           equivalent to FLASH_Unlock function.\r
336   * @param  None\r
337   * @retval None\r
338   */\r
339 void FLASH_UnlockBank1(void)\r
340 {\r
341   /* Authorize the FPEC of Bank1 Access */\r
342   FLASH->KEYR = FLASH_KEY1;\r
343   FLASH->KEYR = FLASH_KEY2;\r
344 }\r
345 \r
346 #ifdef STM32F10X_XL\r
347 /**\r
348   * @brief  Unlocks the FLASH Bank2 Program Erase Controller.\r
349   * @note   This function can be used only for STM32F10X_XL density devices.\r
350   * @param  None\r
351   * @retval None\r
352   */\r
353 void FLASH_UnlockBank2(void)\r
354 {\r
355   /* Authorize the FPEC of Bank2 Access */\r
356   FLASH->KEYR2 = FLASH_KEY1;\r
357   FLASH->KEYR2 = FLASH_KEY2;\r
358 \r
359 }\r
360 #endif /* STM32F10X_XL */\r
361 \r
362 /**\r
363   * @brief  Locks the FLASH Program Erase Controller.\r
364   * @note   This function can be used for all STM32F10x devices.\r
365   *         - For STM32F10X_XL devices this function Locks Bank1 and Bank2.\r
366   *         - For all other devices it Locks Bank1 and it is equivalent \r
367   *           to FLASH_LockBank1 function.\r
368   * @param  None\r
369   * @retval None\r
370   */\r
371 void FLASH_Lock(void)\r
372 {\r
373   /* Set the Lock Bit to lock the FPEC and the CR of  Bank1 */\r
374   FLASH->CR |= CR_LOCK_Set;\r
375 \r
376 #ifdef STM32F10X_XL\r
377   /* Set the Lock Bit to lock the FPEC and the CR of  Bank2 */\r
378   FLASH->CR2 |= CR_LOCK_Set;\r
379 #endif /* STM32F10X_XL */\r
380 }\r
381 \r
382 /**\r
383   * @brief  Locks the FLASH Bank1 Program Erase Controller.\r
384   * @note   this function can be used for all STM32F10x devices.\r
385   *         - For STM32F10X_XL devices this function Locks Bank1.\r
386   *         - For all other devices it Locks Bank1 and it is equivalent \r
387   *           to FLASH_Lock function.\r
388   * @param  None\r
389   * @retval None\r
390   */\r
391 void FLASH_LockBank1(void)\r
392 {\r
393   /* Set the Lock Bit to lock the FPEC and the CR of  Bank1 */\r
394   FLASH->CR |= CR_LOCK_Set;\r
395 }\r
396 \r
397 #ifdef STM32F10X_XL\r
398 /**\r
399   * @brief  Locks the FLASH Bank2 Program Erase Controller.\r
400   * @note   This function can be used only for STM32F10X_XL density devices.\r
401   * @param  None\r
402   * @retval None\r
403   */\r
404 void FLASH_LockBank2(void)\r
405 {\r
406   /* Set the Lock Bit to lock the FPEC and the CR of  Bank2 */\r
407   FLASH->CR2 |= CR_LOCK_Set;\r
408 }\r
409 #endif /* STM32F10X_XL */\r
410 \r
411 /**\r
412   * @brief  Erases a specified FLASH page.\r
413   * @note   This function can be used for all STM32F10x devices.\r
414   * @param  Page_Address: The page address to be erased.\r
415   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
416   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
417   */\r
418 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)\r
419 {\r
420   FLASH_Status status = FLASH_COMPLETE;\r
421   /* Check the parameters */\r
422   assert_param(IS_FLASH_ADDRESS(Page_Address));\r
423 \r
424 #ifdef STM32F10X_XL\r
425   if(Page_Address < FLASH_BANK1_END_ADDRESS)  \r
426   {\r
427     /* Wait for last operation to be completed */\r
428     status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
429     if(status == FLASH_COMPLETE)\r
430     { \r
431       /* if the previous operation is completed, proceed to erase the page */\r
432       FLASH->CR|= CR_PER_Set;\r
433       FLASH->AR = Page_Address; \r
434       FLASH->CR|= CR_STRT_Set;\r
435     \r
436       /* Wait for last operation to be completed */\r
437       status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
438       if(status != FLASH_TIMEOUT)\r
439       {\r
440         /* if the erase operation is completed, disable the PER Bit */\r
441         FLASH->CR &= CR_PER_Reset;\r
442       }\r
443     }\r
444   }\r
445   else\r
446   {\r
447     /* Wait for last operation to be completed */\r
448     status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
449     if(status == FLASH_COMPLETE)\r
450     { \r
451       /* if the previous operation is completed, proceed to erase the page */\r
452       FLASH->CR2|= CR_PER_Set;\r
453       FLASH->AR2 = Page_Address; \r
454       FLASH->CR2|= CR_STRT_Set;\r
455     \r
456       /* Wait for last operation to be completed */\r
457       status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
458       if(status != FLASH_TIMEOUT)\r
459       {\r
460         /* if the erase operation is completed, disable the PER Bit */\r
461         FLASH->CR2 &= CR_PER_Reset;\r
462       }\r
463     }\r
464   }\r
465 #else\r
466   /* Wait for last operation to be completed */\r
467   status = FLASH_WaitForLastOperation(EraseTimeout);\r
468   \r
469   if(status == FLASH_COMPLETE)\r
470   { \r
471     /* if the previous operation is completed, proceed to erase the page */\r
472     FLASH->CR|= CR_PER_Set;\r
473     FLASH->AR = Page_Address; \r
474     FLASH->CR|= CR_STRT_Set;\r
475     \r
476     /* Wait for last operation to be completed */\r
477     status = FLASH_WaitForLastOperation(EraseTimeout);\r
478     if(status != FLASH_TIMEOUT)\r
479     {\r
480       /* if the erase operation is completed, disable the PER Bit */\r
481       FLASH->CR &= CR_PER_Reset;\r
482     }\r
483   }\r
484 #endif /* STM32F10X_XL */\r
485 \r
486   /* Return the Erase Status */\r
487   return status;\r
488 }\r
489 \r
490 /**\r
491   * @brief  Erases all FLASH pages.\r
492   * @note   This function can be used for all STM32F10x devices.\r
493   * @param  None\r
494   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
495   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
496   */\r
497 FLASH_Status FLASH_EraseAllPages(void)\r
498 {\r
499   FLASH_Status status = FLASH_COMPLETE;\r
500 \r
501 #ifdef STM32F10X_XL\r
502   /* Wait for last operation to be completed */\r
503   status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
504   \r
505   if(status == FLASH_COMPLETE)\r
506   {\r
507     /* if the previous operation is completed, proceed to erase all pages */\r
508      FLASH->CR |= CR_MER_Set;\r
509      FLASH->CR |= CR_STRT_Set;\r
510     \r
511     /* Wait for last operation to be completed */\r
512     status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
513     if(status != FLASH_TIMEOUT)\r
514     {\r
515       /* if the erase operation is completed, disable the MER Bit */\r
516       FLASH->CR &= CR_MER_Reset;\r
517     }\r
518   }    \r
519   if(status == FLASH_COMPLETE)\r
520   {\r
521     /* if the previous operation is completed, proceed to erase all pages */\r
522      FLASH->CR2 |= CR_MER_Set;\r
523      FLASH->CR2 |= CR_STRT_Set;\r
524     \r
525     /* Wait for last operation to be completed */\r
526     status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
527     if(status != FLASH_TIMEOUT)\r
528     {\r
529       /* if the erase operation is completed, disable the MER Bit */\r
530       FLASH->CR2 &= CR_MER_Reset;\r
531     }\r
532   }\r
533 #else\r
534   /* Wait for last operation to be completed */\r
535   status = FLASH_WaitForLastOperation(EraseTimeout);\r
536   if(status == FLASH_COMPLETE)\r
537   {\r
538     /* if the previous operation is completed, proceed to erase all pages */\r
539      FLASH->CR |= CR_MER_Set;\r
540      FLASH->CR |= CR_STRT_Set;\r
541     \r
542     /* Wait for last operation to be completed */\r
543     status = FLASH_WaitForLastOperation(EraseTimeout);\r
544     if(status != FLASH_TIMEOUT)\r
545     {\r
546       /* if the erase operation is completed, disable the MER Bit */\r
547       FLASH->CR &= CR_MER_Reset;\r
548     }\r
549   }\r
550 #endif /* STM32F10X_XL */\r
551 \r
552   /* Return the Erase Status */\r
553   return status;\r
554 }\r
555 \r
556 /**\r
557   * @brief  Erases all Bank1 FLASH pages.\r
558   * @note   This function can be used for all STM32F10x devices.\r
559   *         - For STM32F10X_XL devices this function erases all Bank1 pages.\r
560   *         - For all other devices it erases all Bank1 pages and it is equivalent \r
561   *           to FLASH_EraseAllPages function.\r
562   * @param  None\r
563   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
564   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
565   */\r
566 FLASH_Status FLASH_EraseAllBank1Pages(void)\r
567 {\r
568   FLASH_Status status = FLASH_COMPLETE;\r
569   /* Wait for last operation to be completed */\r
570   status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
571   \r
572   if(status == FLASH_COMPLETE)\r
573   {\r
574     /* if the previous operation is completed, proceed to erase all pages */\r
575      FLASH->CR |= CR_MER_Set;\r
576      FLASH->CR |= CR_STRT_Set;\r
577     \r
578     /* Wait for last operation to be completed */\r
579     status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
580     if(status != FLASH_TIMEOUT)\r
581     {\r
582       /* if the erase operation is completed, disable the MER Bit */\r
583       FLASH->CR &= CR_MER_Reset;\r
584     }\r
585   }    \r
586   /* Return the Erase Status */\r
587   return status;\r
588 }\r
589 \r
590 #ifdef STM32F10X_XL\r
591 /**\r
592   * @brief  Erases all Bank2 FLASH pages.\r
593   * @note   This function can be used only for STM32F10x_XL density devices.\r
594   * @param  None\r
595   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
596   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
597   */\r
598 FLASH_Status FLASH_EraseAllBank2Pages(void)\r
599 {\r
600   FLASH_Status status = FLASH_COMPLETE;\r
601   /* Wait for last operation to be completed */\r
602   status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
603   \r
604   if(status == FLASH_COMPLETE)\r
605   {\r
606     /* if the previous operation is completed, proceed to erase all pages */\r
607      FLASH->CR2 |= CR_MER_Set;\r
608      FLASH->CR2 |= CR_STRT_Set;\r
609     \r
610     /* Wait for last operation to be completed */\r
611     status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
612     if(status != FLASH_TIMEOUT)\r
613     {\r
614       /* if the erase operation is completed, disable the MER Bit */\r
615       FLASH->CR2 &= CR_MER_Reset;\r
616     }\r
617   }    \r
618   /* Return the Erase Status */\r
619   return status;\r
620 }\r
621 #endif /* STM32F10X_XL */\r
622 \r
623 /**\r
624   * @brief  Erases the FLASH option bytes.\r
625   * @note   This functions erases all option bytes except the Read protection (RDP). \r
626   * @note   This function can be used for all STM32F10x devices.\r
627   * @param  None\r
628   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
629   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
630   */\r
631 FLASH_Status FLASH_EraseOptionBytes(void)\r
632 {\r
633   uint16_t rdptmp = RDP_Key;\r
634 \r
635   FLASH_Status status = FLASH_COMPLETE;\r
636 \r
637   /* Get the actual read protection Option Byte value */ \r
638   if(FLASH_GetReadOutProtectionStatus() != RESET)\r
639   {\r
640     rdptmp = 0x00;  \r
641   }\r
642 \r
643   /* Wait for last operation to be completed */\r
644   status = FLASH_WaitForLastOperation(EraseTimeout);\r
645   if(status == FLASH_COMPLETE)\r
646   {\r
647     /* Authorize the small information block programming */\r
648     FLASH->OPTKEYR = FLASH_KEY1;\r
649     FLASH->OPTKEYR = FLASH_KEY2;\r
650     \r
651     /* if the previous operation is completed, proceed to erase the option bytes */\r
652     FLASH->CR |= CR_OPTER_Set;\r
653     FLASH->CR |= CR_STRT_Set;\r
654     /* Wait for last operation to be completed */\r
655     status = FLASH_WaitForLastOperation(EraseTimeout);\r
656     \r
657     if(status == FLASH_COMPLETE)\r
658     {\r
659       /* if the erase operation is completed, disable the OPTER Bit */\r
660       FLASH->CR &= CR_OPTER_Reset;\r
661        \r
662       /* Enable the Option Bytes Programming operation */\r
663       FLASH->CR |= CR_OPTPG_Set;\r
664       /* Restore the last read protection Option Byte value */\r
665       OB->RDP = (uint16_t)rdptmp; \r
666       /* Wait for last operation to be completed */\r
667       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
668  \r
669       if(status != FLASH_TIMEOUT)\r
670       {\r
671         /* if the program operation is completed, disable the OPTPG Bit */\r
672         FLASH->CR &= CR_OPTPG_Reset;\r
673       }\r
674     }\r
675     else\r
676     {\r
677       if (status != FLASH_TIMEOUT)\r
678       {\r
679         /* Disable the OPTPG Bit */\r
680         FLASH->CR &= CR_OPTPG_Reset;\r
681       }\r
682     }  \r
683   }\r
684   /* Return the erase status */\r
685   return status;\r
686 }\r
687 \r
688 /**\r
689   * @brief  Programs a word at a specified address.\r
690   * @note   This function can be used for all STM32F10x devices.\r
691   * @param  Address: specifies the address to be programmed.\r
692   * @param  Data: specifies the data to be programmed.\r
693   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
694   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
695   */\r
696 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)\r
697 {\r
698   FLASH_Status status = FLASH_COMPLETE;\r
699   __IO uint32_t tmp = 0;\r
700 \r
701   /* Check the parameters */\r
702   assert_param(IS_FLASH_ADDRESS(Address));\r
703 \r
704 #ifdef STM32F10X_XL\r
705   if(Address < FLASH_BANK1_END_ADDRESS - 2)\r
706   { \r
707     /* Wait for last operation to be completed */\r
708     status = FLASH_WaitForLastBank1Operation(ProgramTimeout); \r
709     if(status == FLASH_COMPLETE)\r
710     {\r
711       /* if the previous operation is completed, proceed to program the new first \r
712         half word */\r
713       FLASH->CR |= CR_PG_Set;\r
714   \r
715       *(__IO uint16_t*)Address = (uint16_t)Data;\r
716       /* Wait for last operation to be completed */\r
717       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
718  \r
719       if(status == FLASH_COMPLETE)\r
720       {\r
721         /* if the previous operation is completed, proceed to program the new second \r
722         half word */\r
723         tmp = Address + 2;\r
724 \r
725         *(__IO uint16_t*) tmp = Data >> 16;\r
726     \r
727         /* Wait for last operation to be completed */\r
728         status = FLASH_WaitForLastOperation(ProgramTimeout);\r
729         \r
730         if(status != FLASH_TIMEOUT)\r
731         {\r
732           /* Disable the PG Bit */\r
733           FLASH->CR &= CR_PG_Reset;\r
734         }\r
735       }\r
736       else\r
737       {\r
738         if (status != FLASH_TIMEOUT)\r
739         {\r
740           /* Disable the PG Bit */\r
741           FLASH->CR &= CR_PG_Reset;\r
742         }\r
743        }\r
744     }\r
745   }\r
746   else if(Address == (FLASH_BANK1_END_ADDRESS - 1))\r
747   {\r
748     /* Wait for last operation to be completed */\r
749     status = FLASH_WaitForLastBank1Operation(ProgramTimeout);\r
750 \r
751     if(status == FLASH_COMPLETE)\r
752     {\r
753       /* if the previous operation is completed, proceed to program the new first \r
754         half word */\r
755       FLASH->CR |= CR_PG_Set;\r
756   \r
757       *(__IO uint16_t*)Address = (uint16_t)Data;\r
758 \r
759       /* Wait for last operation to be completed */\r
760       status = FLASH_WaitForLastBank1Operation(ProgramTimeout);\r
761 \r
762       if(status != FLASH_TIMEOUT)\r
763       {\r
764         /* Disable the PG Bit */\r
765         FLASH->CR &= CR_PG_Reset;\r
766       }\r
767     }\r
768     else\r
769     {\r
770       if (status != FLASH_TIMEOUT)\r
771       {\r
772         /* Disable the PG Bit */\r
773         FLASH->CR &= CR_PG_Reset;\r
774       }\r
775     }\r
776 \r
777     /* Wait for last operation to be completed */\r
778     status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
779 \r
780     if(status == FLASH_COMPLETE)\r
781     {\r
782       /* if the previous operation is completed, proceed to program the new second \r
783       half word */\r
784       FLASH->CR2 |= CR_PG_Set;\r
785       tmp = Address + 2;\r
786 \r
787       *(__IO uint16_t*) tmp = Data >> 16;\r
788     \r
789       /* Wait for last operation to be completed */\r
790       status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
791         \r
792       if(status != FLASH_TIMEOUT)\r
793       {\r
794         /* Disable the PG Bit */\r
795         FLASH->CR2 &= CR_PG_Reset;\r
796       }\r
797     }\r
798     else\r
799     {\r
800       if (status != FLASH_TIMEOUT)\r
801       {\r
802         /* Disable the PG Bit */\r
803         FLASH->CR2 &= CR_PG_Reset;\r
804       }\r
805     }\r
806   }\r
807   else\r
808   {\r
809     /* Wait for last operation to be completed */\r
810     status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
811 \r
812     if(status == FLASH_COMPLETE)\r
813     {\r
814       /* if the previous operation is completed, proceed to program the new first \r
815         half word */\r
816       FLASH->CR2 |= CR_PG_Set;\r
817   \r
818       *(__IO uint16_t*)Address = (uint16_t)Data;\r
819       /* Wait for last operation to be completed */\r
820       status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
821  \r
822       if(status == FLASH_COMPLETE)\r
823       {\r
824         /* if the previous operation is completed, proceed to program the new second \r
825         half word */\r
826         tmp = Address + 2;\r
827 \r
828         *(__IO uint16_t*) tmp = Data >> 16;\r
829     \r
830         /* Wait for last operation to be completed */\r
831         status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
832         \r
833         if(status != FLASH_TIMEOUT)\r
834         {\r
835           /* Disable the PG Bit */\r
836           FLASH->CR2 &= CR_PG_Reset;\r
837         }\r
838       }\r
839       else\r
840       {\r
841         if (status != FLASH_TIMEOUT)\r
842         {\r
843           /* Disable the PG Bit */\r
844           FLASH->CR2 &= CR_PG_Reset;\r
845         }\r
846       }\r
847     }\r
848   }\r
849 #else\r
850   /* Wait for last operation to be completed */\r
851   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
852   \r
853   if(status == FLASH_COMPLETE)\r
854   {\r
855     /* if the previous operation is completed, proceed to program the new first \r
856     half word */\r
857     FLASH->CR |= CR_PG_Set;\r
858   \r
859     *(__IO uint16_t*)Address = (uint16_t)Data;\r
860     /* Wait for last operation to be completed */\r
861     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
862  \r
863     if(status == FLASH_COMPLETE)\r
864     {\r
865       /* if the previous operation is completed, proceed to program the new second \r
866       half word */\r
867       tmp = Address + 2;\r
868 \r
869       *(__IO uint16_t*) tmp = Data >> 16;\r
870     \r
871       /* Wait for last operation to be completed */\r
872       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
873         \r
874       if(status != FLASH_TIMEOUT)\r
875       {\r
876         /* Disable the PG Bit */\r
877         FLASH->CR &= CR_PG_Reset;\r
878       }\r
879     }\r
880     else\r
881     {\r
882       if (status != FLASH_TIMEOUT)\r
883       {\r
884         /* Disable the PG Bit */\r
885         FLASH->CR &= CR_PG_Reset;\r
886       }\r
887     }\r
888   }         \r
889 #endif /* STM32F10X_XL */\r
890    \r
891   /* Return the Program Status */\r
892   return status;\r
893 }\r
894 \r
895 /**\r
896   * @brief  Programs a half word at a specified address.\r
897   * @note   This function can be used for all STM32F10x devices.\r
898   * @param  Address: specifies the address to be programmed.\r
899   * @param  Data: specifies the data to be programmed.\r
900   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
901   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
902   */\r
903 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)\r
904 {\r
905   FLASH_Status status = FLASH_COMPLETE;\r
906   /* Check the parameters */\r
907   assert_param(IS_FLASH_ADDRESS(Address));\r
908 \r
909 #ifdef STM32F10X_XL\r
910   /* Wait for last operation to be completed */\r
911   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
912   \r
913   if(Address < FLASH_BANK1_END_ADDRESS)\r
914   {\r
915     if(status == FLASH_COMPLETE)\r
916     {\r
917       /* if the previous operation is completed, proceed to program the new data */\r
918       FLASH->CR |= CR_PG_Set;\r
919   \r
920       *(__IO uint16_t*)Address = Data;\r
921       /* Wait for last operation to be completed */\r
922       status = FLASH_WaitForLastBank1Operation(ProgramTimeout);\r
923       if(status != FLASH_TIMEOUT)\r
924       {\r
925         /* if the program operation is completed, disable the PG Bit */\r
926         FLASH->CR &= CR_PG_Reset;\r
927       }\r
928     }\r
929   }\r
930   else\r
931   {\r
932     if(status == FLASH_COMPLETE)\r
933     {\r
934       /* if the previous operation is completed, proceed to program the new data */\r
935       FLASH->CR2 |= CR_PG_Set;\r
936   \r
937       *(__IO uint16_t*)Address = Data;\r
938       /* Wait for last operation to be completed */\r
939       status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
940       if(status != FLASH_TIMEOUT)\r
941       {\r
942         /* if the program operation is completed, disable the PG Bit */\r
943         FLASH->CR2 &= CR_PG_Reset;\r
944       }\r
945     }\r
946   }\r
947 #else\r
948   /* Wait for last operation to be completed */\r
949   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
950   \r
951   if(status == FLASH_COMPLETE)\r
952   {\r
953     /* if the previous operation is completed, proceed to program the new data */\r
954     FLASH->CR |= CR_PG_Set;\r
955   \r
956     *(__IO uint16_t*)Address = Data;\r
957     /* Wait for last operation to be completed */\r
958     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
959     if(status != FLASH_TIMEOUT)\r
960     {\r
961       /* if the program operation is completed, disable the PG Bit */\r
962       FLASH->CR &= CR_PG_Reset;\r
963     }\r
964   } \r
965 #endif  /* STM32F10X_XL */\r
966   \r
967   /* Return the Program Status */\r
968   return status;\r
969 }\r
970 \r
971 /**\r
972   * @brief  Programs a half word at a specified Option Byte Data address.\r
973   * @note   This function can be used for all STM32F10x devices.\r
974   * @param  Address: specifies the address to be programmed.\r
975   *   This parameter can be 0x1FFFF804 or 0x1FFFF806. \r
976   * @param  Data: specifies the data to be programmed.\r
977   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
978   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
979   */\r
980 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)\r
981 {\r
982   FLASH_Status status = FLASH_COMPLETE;\r
983   /* Check the parameters */\r
984   assert_param(IS_OB_DATA_ADDRESS(Address));\r
985   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
986 \r
987   if(status == FLASH_COMPLETE)\r
988   {\r
989     /* Authorize the small information block programming */\r
990     FLASH->OPTKEYR = FLASH_KEY1;\r
991     FLASH->OPTKEYR = FLASH_KEY2;\r
992     /* Enables the Option Bytes Programming operation */\r
993     FLASH->CR |= CR_OPTPG_Set; \r
994     *(__IO uint16_t*)Address = Data;\r
995     \r
996     /* Wait for last operation to be completed */\r
997     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
998     if(status != FLASH_TIMEOUT)\r
999     {\r
1000       /* if the program operation is completed, disable the OPTPG Bit */\r
1001       FLASH->CR &= CR_OPTPG_Reset;\r
1002     }\r
1003   }\r
1004   /* Return the Option Byte Data Program Status */\r
1005   return status;\r
1006 }\r
1007 \r
1008 /**\r
1009   * @brief  Write protects the desired pages\r
1010   * @note   This function can be used for all STM32F10x devices.\r
1011   * @param  FLASH_Pages: specifies the address of the pages to be write protected.\r
1012   *   This parameter can be:\r
1013   *     @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31  \r
1014   *     @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3\r
1015   *       and FLASH_WRProt_Pages124to127\r
1016   *     @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and\r
1017   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255\r
1018   *     @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and\r
1019   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127    \r
1020   *     @arg For @b STM32_XL-density_devices: value between FLASH_WRProt_Pages0to1 and\r
1021   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to511\r
1022   *     @arg FLASH_WRProt_AllPages\r
1023   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
1024   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1025   */\r
1026 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)\r
1027 {\r
1028   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;\r
1029   \r
1030   FLASH_Status status = FLASH_COMPLETE;\r
1031   \r
1032   /* Check the parameters */\r
1033   assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));\r
1034   \r
1035   FLASH_Pages = (uint32_t)(~FLASH_Pages);\r
1036   WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);\r
1037   WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);\r
1038   WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);\r
1039   WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);\r
1040   \r
1041   /* Wait for last operation to be completed */\r
1042   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1043   \r
1044   if(status == FLASH_COMPLETE)\r
1045   {\r
1046     /* Authorizes the small information block programming */\r
1047     FLASH->OPTKEYR = FLASH_KEY1;\r
1048     FLASH->OPTKEYR = FLASH_KEY2;\r
1049     FLASH->CR |= CR_OPTPG_Set;\r
1050     if(WRP0_Data != 0xFF)\r
1051     {\r
1052       OB->WRP0 = WRP0_Data;\r
1053       \r
1054       /* Wait for last operation to be completed */\r
1055       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1056     }\r
1057     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))\r
1058     {\r
1059       OB->WRP1 = WRP1_Data;\r
1060       \r
1061       /* Wait for last operation to be completed */\r
1062       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1063     }\r
1064     if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))\r
1065     {\r
1066       OB->WRP2 = WRP2_Data;\r
1067       \r
1068       /* Wait for last operation to be completed */\r
1069       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1070     }\r
1071     \r
1072     if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))\r
1073     {\r
1074       OB->WRP3 = WRP3_Data;\r
1075      \r
1076       /* Wait for last operation to be completed */\r
1077       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1078     }\r
1079           \r
1080     if(status != FLASH_TIMEOUT)\r
1081     {\r
1082       /* if the program operation is completed, disable the OPTPG Bit */\r
1083       FLASH->CR &= CR_OPTPG_Reset;\r
1084     }\r
1085   } \r
1086   /* Return the write protection operation Status */\r
1087   return status;       \r
1088 }\r
1089 \r
1090 /**\r
1091   * @brief  Enables or disables the read out protection.\r
1092   * @note   If the user has already programmed the other option bytes before calling \r
1093   *   this function, he must re-program them since this function erases all option bytes.\r
1094   * @note   This function can be used for all STM32F10x devices.\r
1095   * @param  Newstate: new state of the ReadOut Protection.\r
1096   *   This parameter can be: ENABLE or DISABLE.\r
1097   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
1098   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1099   */\r
1100 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)\r
1101 {\r
1102   FLASH_Status status = FLASH_COMPLETE;\r
1103   /* Check the parameters */\r
1104   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1105   status = FLASH_WaitForLastOperation(EraseTimeout);\r
1106   if(status == FLASH_COMPLETE)\r
1107   {\r
1108     /* Authorizes the small information block programming */\r
1109     FLASH->OPTKEYR = FLASH_KEY1;\r
1110     FLASH->OPTKEYR = FLASH_KEY2;\r
1111     FLASH->CR |= CR_OPTER_Set;\r
1112     FLASH->CR |= CR_STRT_Set;\r
1113     /* Wait for last operation to be completed */\r
1114     status = FLASH_WaitForLastOperation(EraseTimeout);\r
1115     if(status == FLASH_COMPLETE)\r
1116     {\r
1117       /* if the erase operation is completed, disable the OPTER Bit */\r
1118       FLASH->CR &= CR_OPTER_Reset;\r
1119       /* Enable the Option Bytes Programming operation */\r
1120       FLASH->CR |= CR_OPTPG_Set; \r
1121       if(NewState != DISABLE)\r
1122       {\r
1123         OB->RDP = 0x00;\r
1124       }\r
1125       else\r
1126       {\r
1127         OB->RDP = RDP_Key;  \r
1128       }\r
1129       /* Wait for last operation to be completed */\r
1130       status = FLASH_WaitForLastOperation(EraseTimeout); \r
1131     \r
1132       if(status != FLASH_TIMEOUT)\r
1133       {\r
1134         /* if the program operation is completed, disable the OPTPG Bit */\r
1135         FLASH->CR &= CR_OPTPG_Reset;\r
1136       }\r
1137     }\r
1138     else \r
1139     {\r
1140       if(status != FLASH_TIMEOUT)\r
1141       {\r
1142         /* Disable the OPTER Bit */\r
1143         FLASH->CR &= CR_OPTER_Reset;\r
1144       }\r
1145     }\r
1146   }\r
1147   /* Return the protection operation Status */\r
1148   return status;       \r
1149 }\r
1150 \r
1151 /**\r
1152   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.\r
1153   * @note   This function can be used for all STM32F10x devices.\r
1154   * @param  OB_IWDG: Selects the IWDG mode\r
1155   *   This parameter can be one of the following values:\r
1156   *     @arg OB_IWDG_SW: Software IWDG selected\r
1157   *     @arg OB_IWDG_HW: Hardware IWDG selected\r
1158   * @param  OB_STOP: Reset event when entering STOP mode.\r
1159   *   This parameter can be one of the following values:\r
1160   *     @arg OB_STOP_NoRST: No reset generated when entering in STOP\r
1161   *     @arg OB_STOP_RST: Reset generated when entering in STOP\r
1162   * @param  OB_STDBY: Reset event when entering Standby mode.\r
1163   *   This parameter can be one of the following values:\r
1164   *     @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY\r
1165   *     @arg OB_STDBY_RST: Reset generated when entering in STANDBY\r
1166   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, \r
1167   * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1168   */\r
1169 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)\r
1170 {\r
1171   FLASH_Status status = FLASH_COMPLETE; \r
1172 \r
1173   /* Check the parameters */\r
1174   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));\r
1175   assert_param(IS_OB_STOP_SOURCE(OB_STOP));\r
1176   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));\r
1177 \r
1178   /* Authorize the small information block programming */\r
1179   FLASH->OPTKEYR = FLASH_KEY1;\r
1180   FLASH->OPTKEYR = FLASH_KEY2;\r
1181   \r
1182   /* Wait for last operation to be completed */\r
1183   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1184   \r
1185   if(status == FLASH_COMPLETE)\r
1186   {  \r
1187     /* Enable the Option Bytes Programming operation */\r
1188     FLASH->CR |= CR_OPTPG_Set; \r
1189            \r
1190     OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8))); \r
1191   \r
1192     /* Wait for last operation to be completed */\r
1193     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1194     if(status != FLASH_TIMEOUT)\r
1195     {\r
1196       /* if the program operation is completed, disable the OPTPG Bit */\r
1197       FLASH->CR &= CR_OPTPG_Reset;\r
1198     }\r
1199   }    \r
1200   /* Return the Option Byte program Status */\r
1201   return status;\r
1202 }\r
1203 \r
1204 #ifdef STM32F10X_XL\r
1205 /**\r
1206   * @brief  Configures to boot from Bank1 or Bank2.  \r
1207   * @note   This function can be used only for STM32F10x_XL density devices.\r
1208   * @param  FLASH_BOOT: select the FLASH Bank to boot from.\r
1209   *   This parameter can be one of the following values:\r
1210   *     @arg FLASH_BOOT_Bank1: At startup, if boot pins are set in boot from user Flash\r
1211   *        position and this parameter is selected the device will boot from Bank1(Default).\r
1212   *     @arg FLASH_BOOT_Bank2: At startup, if boot pins are set in boot from user Flash\r
1213   *        position and this parameter is selected the device will boot from Bank2 or Bank1,\r
1214   *        depending on the activation of the bank. The active banks are checked in\r
1215   *        the following order: Bank2, followed by Bank1.\r
1216   *        The active bank is recognized by the value programmed at the base address\r
1217   *        of the respective bank (corresponding to the initial stack pointer value\r
1218   *        in the interrupt vector table).\r
1219   *        For more information, please refer to AN2606 from www.st.com.    \r
1220   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, \r
1221   * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1222   */\r
1223 FLASH_Status FLASH_BootConfig(uint16_t FLASH_BOOT)\r
1224\r
1225   FLASH_Status status = FLASH_COMPLETE; \r
1226   assert_param(IS_FLASH_BOOT(FLASH_BOOT));\r
1227   /* Authorize the small information block programming */\r
1228   FLASH->OPTKEYR = FLASH_KEY1;\r
1229   FLASH->OPTKEYR = FLASH_KEY2;\r
1230   \r
1231   /* Wait for last operation to be completed */\r
1232   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1233   \r
1234   if(status == FLASH_COMPLETE)\r
1235   {  \r
1236     /* Enable the Option Bytes Programming operation */\r
1237     FLASH->CR |= CR_OPTPG_Set; \r
1238 \r
1239     if(FLASH_BOOT == FLASH_BOOT_Bank1)\r
1240     {\r
1241       OB->USER |= OB_USER_BFB2;\r
1242     }\r
1243     else\r
1244     {\r
1245       OB->USER &= (uint16_t)(~(uint16_t)(OB_USER_BFB2));\r
1246     }\r
1247     /* Wait for last operation to be completed */\r
1248     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
1249     if(status != FLASH_TIMEOUT)\r
1250     {\r
1251       /* if the program operation is completed, disable the OPTPG Bit */\r
1252       FLASH->CR &= CR_OPTPG_Reset;\r
1253     }\r
1254   }    \r
1255   /* Return the Option Byte program Status */\r
1256   return status;\r
1257 }\r
1258 #endif /* STM32F10X_XL */\r
1259 \r
1260 /**\r
1261   * @brief  Returns the FLASH User Option Bytes values.\r
1262   * @note   This function can be used for all STM32F10x devices.\r
1263   * @param  None\r
1264   * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)\r
1265   *   and RST_STDBY(Bit2).\r
1266   */\r
1267 uint32_t FLASH_GetUserOptionByte(void)\r
1268 {\r
1269   /* Return the User Option Byte */\r
1270   return (uint32_t)(FLASH->OBR >> 2);\r
1271 }\r
1272 \r
1273 /**\r
1274   * @brief  Returns the FLASH Write Protection Option Bytes Register value.\r
1275   * @note   This function can be used for all STM32F10x devices.\r
1276   * @param  None\r
1277   * @retval The FLASH Write Protection  Option Bytes Register value\r
1278   */\r
1279 uint32_t FLASH_GetWriteProtectionOptionByte(void)\r
1280 {\r
1281   /* Return the Falsh write protection Register value */\r
1282   return (uint32_t)(FLASH->WRPR);\r
1283 }\r
1284 \r
1285 /**\r
1286   * @brief  Checks whether the FLASH Read Out Protection Status is set or not.\r
1287   * @note   This function can be used for all STM32F10x devices.\r
1288   * @param  None\r
1289   * @retval FLASH ReadOut Protection Status(SET or RESET)\r
1290   */\r
1291 FlagStatus FLASH_GetReadOutProtectionStatus(void)\r
1292 {\r
1293   FlagStatus readoutstatus = RESET;\r
1294   if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)\r
1295   {\r
1296     readoutstatus = SET;\r
1297   }\r
1298   else\r
1299   {\r
1300     readoutstatus = RESET;\r
1301   }\r
1302   return readoutstatus;\r
1303 }\r
1304 \r
1305 /**\r
1306   * @brief  Checks whether the FLASH Prefetch Buffer status is set or not.\r
1307   * @note   This function can be used for all STM32F10x devices.\r
1308   * @param  None\r
1309   * @retval FLASH Prefetch Buffer Status (SET or RESET).\r
1310   */\r
1311 FlagStatus FLASH_GetPrefetchBufferStatus(void)\r
1312 {\r
1313   FlagStatus bitstatus = RESET;\r
1314   \r
1315   if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)\r
1316   {\r
1317     bitstatus = SET;\r
1318   }\r
1319   else\r
1320   {\r
1321     bitstatus = RESET;\r
1322   }\r
1323   /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */\r
1324   return bitstatus; \r
1325 }\r
1326 \r
1327 /**\r
1328   * @brief  Enables or disables the specified FLASH interrupts.\r
1329   * @note   This function can be used for all STM32F10x devices.\r
1330   *         - For STM32F10X_XL devices, enables or disables the specified FLASH interrupts\r
1331               for Bank1 and Bank2.\r
1332   *         - For other devices it enables or disables the specified FLASH interrupts for Bank1.\r
1333   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.\r
1334   *   This parameter can be any combination of the following values:\r
1335   *     @arg FLASH_IT_ERROR: FLASH Error Interrupt\r
1336   *     @arg FLASH_IT_EOP: FLASH end of operation Interrupt\r
1337   * @param  NewState: new state of the specified Flash interrupts.\r
1338   *   This parameter can be: ENABLE or DISABLE.      \r
1339   * @retval None \r
1340   */\r
1341 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)\r
1342 {\r
1343 #ifdef STM32F10X_XL\r
1344   /* Check the parameters */\r
1345   assert_param(IS_FLASH_IT(FLASH_IT)); \r
1346   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1347 \r
1348   if((FLASH_IT & 0x80000000) != 0x0)\r
1349   {\r
1350     if(NewState != DISABLE)\r
1351     {\r
1352       /* Enable the interrupt sources */\r
1353       FLASH->CR2 |= (FLASH_IT & 0x7FFFFFFF);\r
1354     }\r
1355     else\r
1356     {\r
1357       /* Disable the interrupt sources */\r
1358       FLASH->CR2 &= ~(uint32_t)(FLASH_IT & 0x7FFFFFFF);\r
1359     }\r
1360   }\r
1361   else\r
1362   {\r
1363     if(NewState != DISABLE)\r
1364     {\r
1365       /* Enable the interrupt sources */\r
1366       FLASH->CR |= FLASH_IT;\r
1367     }\r
1368     else\r
1369     {\r
1370       /* Disable the interrupt sources */\r
1371       FLASH->CR &= ~(uint32_t)FLASH_IT;\r
1372     }\r
1373   }\r
1374 #else\r
1375   /* Check the parameters */\r
1376   assert_param(IS_FLASH_IT(FLASH_IT)); \r
1377   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1378 \r
1379   if(NewState != DISABLE)\r
1380   {\r
1381     /* Enable the interrupt sources */\r
1382     FLASH->CR |= FLASH_IT;\r
1383   }\r
1384   else\r
1385   {\r
1386     /* Disable the interrupt sources */\r
1387     FLASH->CR &= ~(uint32_t)FLASH_IT;\r
1388   }\r
1389 #endif /* STM32F10X_XL */\r
1390 }\r
1391 \r
1392 /**\r
1393   * @brief  Checks whether the specified FLASH flag is set or not.\r
1394   * @note   This function can be used for all STM32F10x devices.\r
1395   *         - For STM32F10X_XL devices, this function checks whether the specified \r
1396   *           Bank1 or Bank2 flag is set or not.\r
1397   *         - For other devices, it checks whether the specified Bank1 flag is \r
1398   *           set or not.\r
1399   * @param  FLASH_FLAG: specifies the FLASH flag to check.\r
1400   *   This parameter can be one of the following values:\r
1401   *     @arg FLASH_FLAG_BSY: FLASH Busy flag           \r
1402   *     @arg FLASH_FLAG_PGERR: FLASH Program error flag       \r
1403   *     @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      \r
1404   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag           \r
1405   *     @arg FLASH_FLAG_OPTERR:  FLASH Option Byte error flag     \r
1406   * @retval The new state of FLASH_FLAG (SET or RESET).\r
1407   */\r
1408 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)\r
1409 {\r
1410   FlagStatus bitstatus = RESET;\r
1411 \r
1412 #ifdef STM32F10X_XL\r
1413   /* Check the parameters */\r
1414   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;\r
1415   if(FLASH_FLAG == FLASH_FLAG_OPTERR) \r
1416   {\r
1417     if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)\r
1418     {\r
1419       bitstatus = SET;\r
1420     }\r
1421     else\r
1422     {\r
1423       bitstatus = RESET;\r
1424     }\r
1425   }\r
1426   else\r
1427   {\r
1428     if((FLASH_FLAG & 0x80000000) != 0x0)\r
1429     {\r
1430       if((FLASH->SR2 & FLASH_FLAG) != (uint32_t)RESET)\r
1431       {\r
1432         bitstatus = SET;\r
1433       }\r
1434       else\r
1435       {\r
1436         bitstatus = RESET;\r
1437       }\r
1438     }\r
1439     else\r
1440     {\r
1441       if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)\r
1442       {\r
1443         bitstatus = SET;\r
1444       }\r
1445       else\r
1446       {\r
1447         bitstatus = RESET;\r
1448       }\r
1449     }\r
1450   }\r
1451 #else\r
1452   /* Check the parameters */\r
1453   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;\r
1454   if(FLASH_FLAG == FLASH_FLAG_OPTERR) \r
1455   {\r
1456     if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)\r
1457     {\r
1458       bitstatus = SET;\r
1459     }\r
1460     else\r
1461     {\r
1462       bitstatus = RESET;\r
1463     }\r
1464   }\r
1465   else\r
1466   {\r
1467    if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)\r
1468     {\r
1469       bitstatus = SET;\r
1470     }\r
1471     else\r
1472     {\r
1473       bitstatus = RESET;\r
1474     }\r
1475   }\r
1476 #endif /* STM32F10X_XL */\r
1477 \r
1478   /* Return the new state of FLASH_FLAG (SET or RESET) */\r
1479   return bitstatus;\r
1480 }\r
1481 \r
1482 /**\r
1483   * @brief  Clears the FLASH\92s pending flags.\r
1484   * @note   This function can be used for all STM32F10x devices.\r
1485   *         - For STM32F10X_XL devices, this function clears Bank1 or Bank2\92s pending flags\r
1486   *         - For other devices, it clears Bank1\92s pending flags.\r
1487   * @param  FLASH_FLAG: specifies the FLASH flags to clear.\r
1488   *   This parameter can be any combination of the following values:         \r
1489   *     @arg FLASH_FLAG_PGERR: FLASH Program error flag       \r
1490   *     @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      \r
1491   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag           \r
1492   * @retval None\r
1493   */\r
1494 void FLASH_ClearFlag(uint32_t FLASH_FLAG)\r
1495 {\r
1496 #ifdef STM32F10X_XL\r
1497   /* Check the parameters */\r
1498   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;\r
1499 \r
1500   if((FLASH_FLAG & 0x80000000) != 0x0)\r
1501   {\r
1502     /* Clear the flags */\r
1503     FLASH->SR2 = FLASH_FLAG;\r
1504   }\r
1505   else\r
1506   {\r
1507     /* Clear the flags */\r
1508     FLASH->SR = FLASH_FLAG;\r
1509   }  \r
1510 \r
1511 #else\r
1512   /* Check the parameters */\r
1513   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;\r
1514   \r
1515   /* Clear the flags */\r
1516   FLASH->SR = FLASH_FLAG;\r
1517 #endif /* STM32F10X_XL */\r
1518 }\r
1519 \r
1520 /**\r
1521   * @brief  Returns the FLASH Status.\r
1522   * @note   This function can be used for all STM32F10x devices, it is equivalent\r
1523   *    to FLASH_GetBank1Status function.\r
1524   * @param  None\r
1525   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
1526   *   FLASH_ERROR_WRP or FLASH_COMPLETE\r
1527   */\r
1528 FLASH_Status FLASH_GetStatus(void)\r
1529 {\r
1530   FLASH_Status flashstatus = FLASH_COMPLETE;\r
1531   \r
1532   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) \r
1533   {\r
1534     flashstatus = FLASH_BUSY;\r
1535   }\r
1536   else \r
1537   {  \r
1538     if((FLASH->SR & FLASH_FLAG_PGERR) != 0)\r
1539     { \r
1540       flashstatus = FLASH_ERROR_PG;\r
1541     }\r
1542     else \r
1543     {\r
1544       if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )\r
1545       {\r
1546         flashstatus = FLASH_ERROR_WRP;\r
1547       }\r
1548       else\r
1549       {\r
1550         flashstatus = FLASH_COMPLETE;\r
1551       }\r
1552     }\r
1553   }\r
1554   /* Return the Flash Status */\r
1555   return flashstatus;\r
1556 }\r
1557 \r
1558 /**\r
1559   * @brief  Returns the FLASH Bank1 Status.\r
1560   * @note   This function can be used for all STM32F10x devices, it is equivalent\r
1561   *   to FLASH_GetStatus function.\r
1562   * @param  None\r
1563   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
1564   *   FLASH_ERROR_WRP or FLASH_COMPLETE\r
1565   */\r
1566 FLASH_Status FLASH_GetBank1Status(void)\r
1567 {\r
1568   FLASH_Status flashstatus = FLASH_COMPLETE;\r
1569   \r
1570   if((FLASH->SR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY) \r
1571   {\r
1572     flashstatus = FLASH_BUSY;\r
1573   }\r
1574   else \r
1575   {  \r
1576     if((FLASH->SR & FLASH_FLAG_BANK1_PGERR) != 0)\r
1577     { \r
1578       flashstatus = FLASH_ERROR_PG;\r
1579     }\r
1580     else \r
1581     {\r
1582       if((FLASH->SR & FLASH_FLAG_BANK1_WRPRTERR) != 0 )\r
1583       {\r
1584         flashstatus = FLASH_ERROR_WRP;\r
1585       }\r
1586       else\r
1587       {\r
1588         flashstatus = FLASH_COMPLETE;\r
1589       }\r
1590     }\r
1591   }\r
1592   /* Return the Flash Status */\r
1593   return flashstatus;\r
1594 }\r
1595 \r
1596 #ifdef STM32F10X_XL\r
1597 /**\r
1598   * @brief  Returns the FLASH Bank2 Status.\r
1599   * @note   This function can be used for STM32F10x_XL density devices.\r
1600   * @param  None\r
1601   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
1602   *   FLASH_ERROR_WRP or FLASH_COMPLETE\r
1603   */\r
1604 FLASH_Status FLASH_GetBank2Status(void)\r
1605 {\r
1606   FLASH_Status flashstatus = FLASH_COMPLETE;\r
1607   \r
1608   if((FLASH->SR2 & (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) \r
1609   {\r
1610     flashstatus = FLASH_BUSY;\r
1611   }\r
1612   else \r
1613   {  \r
1614     if((FLASH->SR2 & (FLASH_FLAG_BANK2_PGERR & 0x7FFFFFFF)) != 0)\r
1615     { \r
1616       flashstatus = FLASH_ERROR_PG;\r
1617     }\r
1618     else \r
1619     {\r
1620       if((FLASH->SR2 & (FLASH_FLAG_BANK2_WRPRTERR & 0x7FFFFFFF)) != 0 )\r
1621       {\r
1622         flashstatus = FLASH_ERROR_WRP;\r
1623       }\r
1624       else\r
1625       {\r
1626         flashstatus = FLASH_COMPLETE;\r
1627       }\r
1628     }\r
1629   }\r
1630   /* Return the Flash Status */\r
1631   return flashstatus;\r
1632 }\r
1633 #endif /* STM32F10X_XL */\r
1634 /**\r
1635   * @brief  Waits for a Flash operation to complete or a TIMEOUT to occur.\r
1636   * @note   This function can be used for all STM32F10x devices, \r
1637   *         it is equivalent to FLASH_WaitForLastBank1Operation.\r
1638   *         - For STM32F10X_XL devices this function waits for a Bank1 Flash operation\r
1639   *           to complete or a TIMEOUT to occur.\r
1640   *         - For all other devices it waits for a Flash operation to complete \r
1641   *           or a TIMEOUT to occur.\r
1642   * @param  Timeout: FLASH progamming Timeout\r
1643   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
1644   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1645   */\r
1646 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)\r
1647\r
1648   FLASH_Status status = FLASH_COMPLETE;\r
1649    \r
1650   /* Check for the Flash Status */\r
1651   status = FLASH_GetBank1Status();\r
1652   /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
1653   while((status == FLASH_BUSY) && (Timeout != 0x00))\r
1654   {\r
1655     status = FLASH_GetBank1Status();\r
1656     Timeout--;\r
1657   }\r
1658   if(Timeout == 0x00 )\r
1659   {\r
1660     status = FLASH_TIMEOUT;\r
1661   }\r
1662   /* Return the operation status */\r
1663   return status;\r
1664 }\r
1665 \r
1666 /**\r
1667   * @brief  Waits for a Flash operation on Bank1 to complete or a TIMEOUT to occur.\r
1668   * @note   This function can be used for all STM32F10x devices, \r
1669   *         it is equivalent to FLASH_WaitForLastOperation.\r
1670   * @param  Timeout: FLASH progamming Timeout\r
1671   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
1672   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1673   */\r
1674 FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout)\r
1675\r
1676   FLASH_Status status = FLASH_COMPLETE;\r
1677    \r
1678   /* Check for the Flash Status */\r
1679   status = FLASH_GetBank1Status();\r
1680   /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
1681   while((status == FLASH_FLAG_BANK1_BSY) && (Timeout != 0x00))\r
1682   {\r
1683     status = FLASH_GetBank1Status();\r
1684     Timeout--;\r
1685   }\r
1686   if(Timeout == 0x00 )\r
1687   {\r
1688     status = FLASH_TIMEOUT;\r
1689   }\r
1690   /* Return the operation status */\r
1691   return status;\r
1692 }\r
1693 \r
1694 #ifdef STM32F10X_XL\r
1695 /**\r
1696   * @brief  Waits for a Flash operation on Bank2 to complete or a TIMEOUT to occur.\r
1697   * @note   This function can be used only for STM32F10x_XL density devices.\r
1698   * @param  Timeout: FLASH progamming Timeout\r
1699   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
1700   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1701   */\r
1702 FLASH_Status FLASH_WaitForLastBank2Operation(uint32_t Timeout)\r
1703\r
1704   FLASH_Status status = FLASH_COMPLETE;\r
1705    \r
1706   /* Check for the Flash Status */\r
1707   status = FLASH_GetBank2Status();\r
1708   /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
1709   while((status == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) && (Timeout != 0x00))\r
1710   {\r
1711     status = FLASH_GetBank2Status();\r
1712     Timeout--;\r
1713   }\r
1714   if(Timeout == 0x00 )\r
1715   {\r
1716     status = FLASH_TIMEOUT;\r
1717   }\r
1718   /* Return the operation status */\r
1719   return status;\r
1720 }\r
1721 #endif /* STM32F10X_XL */\r
1722 \r
1723 /**\r
1724   * @}\r
1725   */\r
1726 \r
1727 /**\r
1728   * @}\r
1729   */\r
1730 \r
1731 /**\r
1732   * @}\r
1733   */\r
1734 \r
1735 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r