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