]> git.piffa.net Git - arduino/blob - sheets/gyro/GY-52 Three-axis gyroscope sending data /Three-axis gyroscope sending data/GY-52 Test program/STM32-CODE/serial port output MPU-3050/serial/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c
first commit
[arduino] / sheets / gyro / GY-52 Three-axis gyroscope sending data / Three-axis gyroscope sending data / GY-52 Test program / STM32-CODE / serial port output MPU-3050 / serial / Libraries / STM32F10x_StdPeriph_Driver / src / stm32f10x_flash.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_flash.c\r
4   * @author  MCD Application Team\r
5   * @version V3.1.2\r
6   * @date    09/28/2009\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 2009 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 \r
74 /* FLASH Keys */\r
75 #define RDP_Key                  ((uint16_t)0x00A5)\r
76 #define FLASH_KEY1               ((uint32_t)0x45670123)\r
77 #define FLASH_KEY2               ((uint32_t)0xCDEF89AB)\r
78 \r
79 /* Delay definition */   \r
80 #define EraseTimeout             ((uint32_t)0x00000FFF)\r
81 #define ProgramTimeout           ((uint32_t)0x0000000F)\r
82 \r
83 /**\r
84   * @}\r
85   */ \r
86 \r
87 /** @defgroup FLASH_Private_Macros\r
88   * @{\r
89   */\r
90 \r
91 /**\r
92   * @}\r
93   */ \r
94 \r
95 /** @defgroup FLASH_Private_Variables\r
96   * @{\r
97   */\r
98 \r
99 /**\r
100   * @}\r
101   */ \r
102 \r
103 /** @defgroup FLASH_Private_FunctionPrototypes\r
104   * @{\r
105   */\r
106 \r
107 static void delay(void);\r
108 /**\r
109   * @}\r
110   */\r
111 \r
112 /** @defgroup FLASH_Private_Functions\r
113   * @{\r
114   */\r
115 \r
116 /**\r
117   * @brief  Sets the code latency value.\r
118   * @param  FLASH_Latency: specifies the FLASH Latency value.\r
119   *   This parameter can be one of the following values:\r
120   *     @arg FLASH_Latency_0: FLASH Zero Latency cycle\r
121   *     @arg FLASH_Latency_1: FLASH One Latency cycle\r
122   *     @arg FLASH_Latency_2: FLASH Two Latency cycles\r
123   * @retval None\r
124   */\r
125 void FLASH_SetLatency(uint32_t FLASH_Latency)\r
126 {\r
127   uint32_t tmpreg = 0;\r
128   \r
129   /* Check the parameters */\r
130   assert_param(IS_FLASH_LATENCY(FLASH_Latency));\r
131   \r
132   /* Read the ACR register */\r
133   tmpreg = FLASH->ACR;  \r
134   \r
135   /* Sets the Latency value */\r
136   tmpreg &= ACR_LATENCY_Mask;\r
137   tmpreg |= FLASH_Latency;\r
138   \r
139   /* Write the ACR register */\r
140   FLASH->ACR = tmpreg;\r
141 }\r
142 \r
143 /**\r
144   * @brief  Enables or disables the Half cycle flash access.\r
145   * @param  FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.\r
146   *   This parameter can be one of the following values:\r
147   *     @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable\r
148   *     @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable\r
149   * @retval None\r
150   */\r
151 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)\r
152 {\r
153   /* Check the parameters */\r
154   assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));\r
155   \r
156   /* Enable or disable the Half cycle access */\r
157   FLASH->ACR &= ACR_HLFCYA_Mask;\r
158   FLASH->ACR |= FLASH_HalfCycleAccess;\r
159 }\r
160 \r
161 /**\r
162   * @brief  Enables or disables the Prefetch Buffer.\r
163   * @param  FLASH_PrefetchBuffer: specifies the Prefetch buffer status.\r
164   *   This parameter can be one of the following values:\r
165   *     @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable\r
166   *     @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable\r
167   * @retval None\r
168   */\r
169 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)\r
170 {\r
171   /* Check the parameters */\r
172   assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));\r
173   \r
174   /* Enable or disable the Prefetch Buffer */\r
175   FLASH->ACR &= ACR_PRFTBE_Mask;\r
176   FLASH->ACR |= FLASH_PrefetchBuffer;\r
177 }\r
178 \r
179 /**\r
180   * @brief  Unlocks the FLASH Program Erase Controller.\r
181   * @param  None\r
182   * @retval None\r
183   */\r
184 void FLASH_Unlock(void)\r
185 {\r
186   /* Authorize the FPEC Access */\r
187   FLASH->KEYR = FLASH_KEY1;\r
188   FLASH->KEYR = FLASH_KEY2;\r
189 }\r
190 \r
191 /**\r
192   * @brief  Locks the FLASH Program Erase Controller.\r
193   * @param  None\r
194   * @retval None\r
195   */\r
196 void FLASH_Lock(void)\r
197 {\r
198   /* Set the Lock Bit to lock the FPEC and the FCR */\r
199   FLASH->CR |= CR_LOCK_Set;\r
200 }\r
201 \r
202 /**\r
203   * @brief  Erases a specified FLASH page.\r
204   * @param  Page_Address: The page address to be erased.\r
205   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
206   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
207   */\r
208 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)\r
209 {\r
210   FLASH_Status status = FLASH_COMPLETE;\r
211   /* Check the parameters */\r
212   assert_param(IS_FLASH_ADDRESS(Page_Address));\r
213   /* Wait for last operation to be completed */\r
214   status = FLASH_WaitForLastOperation(EraseTimeout);\r
215   \r
216   if(status == FLASH_COMPLETE)\r
217   { \r
218     /* if the previous operation is completed, proceed to erase the page */\r
219     FLASH->CR|= CR_PER_Set;\r
220     FLASH->AR = Page_Address; \r
221     FLASH->CR|= CR_STRT_Set;\r
222     \r
223     /* Wait for last operation to be completed */\r
224     status = FLASH_WaitForLastOperation(EraseTimeout);\r
225     if(status != FLASH_TIMEOUT)\r
226     {\r
227       /* if the erase operation is completed, disable the PER Bit */\r
228       FLASH->CR &= CR_PER_Reset;\r
229     }\r
230   }\r
231   /* Return the Erase Status */\r
232   return status;\r
233 }\r
234 \r
235 /**\r
236   * @brief  Erases all FLASH pages.\r
237   * @param  None\r
238   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
239   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
240   */\r
241 FLASH_Status FLASH_EraseAllPages(void)\r
242 {\r
243   FLASH_Status status = FLASH_COMPLETE;\r
244   /* Wait for last operation to be completed */\r
245   status = FLASH_WaitForLastOperation(EraseTimeout);\r
246   \r
247   if(status == FLASH_COMPLETE)\r
248   {\r
249     /* if the previous operation is completed, proceed to erase all pages */\r
250      FLASH->CR |= CR_MER_Set;\r
251      FLASH->CR |= CR_STRT_Set;\r
252     \r
253     /* Wait for last operation to be completed */\r
254     status = FLASH_WaitForLastOperation(EraseTimeout);\r
255     if(status != FLASH_TIMEOUT)\r
256     {\r
257       /* if the erase operation is completed, disable the MER Bit */\r
258       FLASH->CR &= CR_MER_Reset;\r
259     }\r
260   }        \r
261   /* Return the Erase Status */\r
262   return status;\r
263 }\r
264 \r
265 /**\r
266   * @brief  Erases the FLASH option bytes.\r
267   * @note   This functions erases all option bytes and then deactivates the Read\r
268   *         protection. If the user needs to keep the Read protection activated,\r
269   *         he has to enable it after this function call (using\r
270   *         FLASH_ReadOutProtection function)\r
271   * @param  None\r
272   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
273   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
274   */\r
275 FLASH_Status FLASH_EraseOptionBytes(void)\r
276 {\r
277   FLASH_Status status = FLASH_COMPLETE;\r
278   \r
279   /* Wait for last operation to be completed */\r
280   status = FLASH_WaitForLastOperation(EraseTimeout);\r
281   if(status == FLASH_COMPLETE)\r
282   {\r
283     /* Authorize the small information block programming */\r
284     FLASH->OPTKEYR = FLASH_KEY1;\r
285     FLASH->OPTKEYR = FLASH_KEY2;\r
286     \r
287     /* if the previous operation is completed, proceed to erase the option bytes */\r
288     FLASH->CR |= CR_OPTER_Set;\r
289     FLASH->CR |= CR_STRT_Set;\r
290     /* Wait for last operation to be completed */\r
291     status = FLASH_WaitForLastOperation(EraseTimeout);\r
292     \r
293     if(status == FLASH_COMPLETE)\r
294     {\r
295       /* if the erase operation is completed, disable the OPTER Bit */\r
296       FLASH->CR &= CR_OPTER_Reset;\r
297        \r
298       /* Enable the Option Bytes Programming operation */\r
299       FLASH->CR |= CR_OPTPG_Set;\r
300       /* Disable the Read protection */\r
301       OB->RDP= RDP_Key; \r
302       /* Wait for last operation to be completed */\r
303       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
304  \r
305       if(status != FLASH_TIMEOUT)\r
306       {\r
307         /* if the program operation is completed, disable the OPTPG Bit */\r
308         FLASH->CR &= CR_OPTPG_Reset;\r
309       }\r
310     }\r
311     else\r
312     {\r
313       if (status != FLASH_TIMEOUT)\r
314       {\r
315         /* Disable the OPTPG Bit */\r
316         FLASH->CR &= CR_OPTPG_Reset;\r
317       }\r
318     }  \r
319   }\r
320   /* Return the erase status */\r
321   return status;\r
322 }\r
323 \r
324 /**\r
325   * @brief  Programs a word at a specified address.\r
326   * @param  Address: specifies the address to be programmed.\r
327   * @param  Data: specifies the data to be programmed.\r
328   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
329   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
330   */\r
331 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)\r
332 {\r
333   FLASH_Status status = FLASH_COMPLETE;\r
334   __IO uint32_t tmp = 0;\r
335 \r
336   /* Check the parameters */\r
337   assert_param(IS_FLASH_ADDRESS(Address));\r
338   /* Wait for last operation to be completed */\r
339   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
340   \r
341   if(status == FLASH_COMPLETE)\r
342   {\r
343     /* if the previous operation is completed, proceed to program the new first \r
344     half word */\r
345     FLASH->CR |= CR_PG_Set;\r
346   \r
347     *(__IO uint16_t*)Address = (uint16_t)Data;\r
348     /* Wait for last operation to be completed */\r
349     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
350  \r
351     if(status == FLASH_COMPLETE)\r
352     {\r
353       /* if the previous operation is completed, proceed to program the new second \r
354       half word */\r
355       tmp = Address + 2;\r
356 \r
357       *(__IO uint16_t*) tmp = Data >> 16;\r
358     \r
359       /* Wait for last operation to be completed */\r
360       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
361         \r
362       if(status != FLASH_TIMEOUT)\r
363       {\r
364         /* Disable the PG Bit */\r
365         FLASH->CR &= CR_PG_Reset;\r
366       }\r
367     }\r
368     else\r
369     {\r
370       if (status != FLASH_TIMEOUT)\r
371       {\r
372         /* Disable the PG Bit */\r
373         FLASH->CR &= CR_PG_Reset;\r
374       }\r
375      }\r
376   }\r
377   /* Return the Program Status */\r
378   return status;\r
379 }\r
380 \r
381 /**\r
382   * @brief  Programs a half word at a specified address.\r
383   * @param  Address: specifies the address to be programmed.\r
384   * @param  Data: specifies the data to be programmed.\r
385   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
386   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
387   */\r
388 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)\r
389 {\r
390   FLASH_Status status = FLASH_COMPLETE;\r
391   /* Check the parameters */\r
392   assert_param(IS_FLASH_ADDRESS(Address));\r
393   /* Wait for last operation to be completed */\r
394   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
395   \r
396   if(status == FLASH_COMPLETE)\r
397   {\r
398     /* if the previous operation is completed, proceed to program the new data */\r
399     FLASH->CR |= CR_PG_Set;\r
400   \r
401     *(__IO uint16_t*)Address = Data;\r
402     /* Wait for last operation to be completed */\r
403     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
404     if(status != FLASH_TIMEOUT)\r
405     {\r
406       /* if the program operation is completed, disable the PG Bit */\r
407       FLASH->CR &= CR_PG_Reset;\r
408     }\r
409   } \r
410   /* Return the Program Status */\r
411   return status;\r
412 }\r
413 \r
414 /**\r
415   * @brief  Programs a half word at a specified Option Byte Data address.\r
416   * @param  Address: specifies the address to be programmed.\r
417   *   This parameter can be 0x1FFFF804 or 0x1FFFF806. \r
418   * @param  Data: specifies the data to be programmed.\r
419   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
420   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
421   */\r
422 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)\r
423 {\r
424   FLASH_Status status = FLASH_COMPLETE;\r
425   /* Check the parameters */\r
426   assert_param(IS_OB_DATA_ADDRESS(Address));\r
427   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
428   if(status == FLASH_COMPLETE)\r
429   {\r
430     /* Authorize the small information block programming */\r
431     FLASH->OPTKEYR = FLASH_KEY1;\r
432     FLASH->OPTKEYR = FLASH_KEY2;\r
433     /* Enables the Option Bytes Programming operation */\r
434     FLASH->CR |= CR_OPTPG_Set; \r
435     *(__IO uint16_t*)Address = Data;\r
436     \r
437     /* Wait for last operation to be completed */\r
438     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
439     if(status != FLASH_TIMEOUT)\r
440     {\r
441       /* if the program operation is completed, disable the OPTPG Bit */\r
442       FLASH->CR &= CR_OPTPG_Reset;\r
443     }\r
444   }    \r
445   /* Return the Option Byte Data Program Status */\r
446   return status;\r
447 }\r
448 \r
449 /**\r
450   * @brief  Write protects the desired pages\r
451   * @param  FLASH_Pages: specifies the address of the pages to be write protected.\r
452   *   This parameter can be:\r
453   *     @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31  \r
454   *     @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3\r
455   *       and FLASH_WRProt_Pages124to127\r
456   *     @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and\r
457   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255\r
458   *     @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and\r
459   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127    \r
460   *     @arg FLASH_WRProt_AllPages\r
461   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
462   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
463   */\r
464 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)\r
465 {\r
466   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;\r
467   \r
468   FLASH_Status status = FLASH_COMPLETE;\r
469   \r
470   /* Check the parameters */\r
471   assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));\r
472   \r
473   FLASH_Pages = (uint32_t)(~FLASH_Pages);\r
474   WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);\r
475   WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);\r
476   WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);\r
477   WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);\r
478   \r
479   /* Wait for last operation to be completed */\r
480   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
481   \r
482   if(status == FLASH_COMPLETE)\r
483   {\r
484     /* Authorizes the small information block programming */\r
485     FLASH->OPTKEYR = FLASH_KEY1;\r
486     FLASH->OPTKEYR = FLASH_KEY2;\r
487     FLASH->CR |= CR_OPTPG_Set;\r
488     if(WRP0_Data != 0xFF)\r
489     {\r
490       OB->WRP0 = WRP0_Data;\r
491       \r
492       /* Wait for last operation to be completed */\r
493       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
494     }\r
495     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))\r
496     {\r
497       OB->WRP1 = WRP1_Data;\r
498       \r
499       /* Wait for last operation to be completed */\r
500       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
501     }\r
502     if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))\r
503     {\r
504       OB->WRP2 = WRP2_Data;\r
505       \r
506       /* Wait for last operation to be completed */\r
507       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
508     }\r
509     \r
510     if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))\r
511     {\r
512       OB->WRP3 = WRP3_Data;\r
513      \r
514       /* Wait for last operation to be completed */\r
515       status = FLASH_WaitForLastOperation(ProgramTimeout);\r
516     }\r
517           \r
518     if(status != FLASH_TIMEOUT)\r
519     {\r
520       /* if the program operation is completed, disable the OPTPG Bit */\r
521       FLASH->CR &= CR_OPTPG_Reset;\r
522     }\r
523   } \r
524   /* Return the write protection operation Status */\r
525   return status;       \r
526 }\r
527 \r
528 /**\r
529   * @brief  Enables or disables the read out protection.\r
530   * @note   If the user has already programmed the other option bytes before calling \r
531   *   this function, he must re-program them since this function erases all option bytes.\r
532   * @param  Newstate: new state of the ReadOut Protection.\r
533   *   This parameter can be: ENABLE or DISABLE.\r
534   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
535   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
536   */\r
537 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)\r
538 {\r
539   FLASH_Status status = FLASH_COMPLETE;\r
540   /* Check the parameters */\r
541   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
542   status = FLASH_WaitForLastOperation(EraseTimeout);\r
543   if(status == FLASH_COMPLETE)\r
544   {\r
545     /* Authorizes the small information block programming */\r
546     FLASH->OPTKEYR = FLASH_KEY1;\r
547     FLASH->OPTKEYR = FLASH_KEY2;\r
548     FLASH->CR |= CR_OPTER_Set;\r
549     FLASH->CR |= CR_STRT_Set;\r
550     /* Wait for last operation to be completed */\r
551     status = FLASH_WaitForLastOperation(EraseTimeout);\r
552     if(status == FLASH_COMPLETE)\r
553     {\r
554       /* if the erase operation is completed, disable the OPTER Bit */\r
555       FLASH->CR &= CR_OPTER_Reset;\r
556       /* Enable the Option Bytes Programming operation */\r
557       FLASH->CR |= CR_OPTPG_Set; \r
558       if(NewState != DISABLE)\r
559       {\r
560         OB->RDP = 0x00;\r
561       }\r
562       else\r
563       {\r
564         OB->RDP = RDP_Key;  \r
565       }\r
566       /* Wait for last operation to be completed */\r
567       status = FLASH_WaitForLastOperation(EraseTimeout); \r
568     \r
569       if(status != FLASH_TIMEOUT)\r
570       {\r
571         /* if the program operation is completed, disable the OPTPG Bit */\r
572         FLASH->CR &= CR_OPTPG_Reset;\r
573       }\r
574     }\r
575     else \r
576     {\r
577       if(status != FLASH_TIMEOUT)\r
578       {\r
579         /* Disable the OPTER Bit */\r
580         FLASH->CR &= CR_OPTER_Reset;\r
581       }\r
582     }\r
583   }\r
584   /* Return the protection operation Status */\r
585   return status;      \r
586 }\r
587 \r
588 /**\r
589   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.\r
590   * @param  OB_IWDG: Selects the IWDG mode\r
591   *   This parameter can be one of the following values:\r
592   *     @arg OB_IWDG_SW: Software IWDG selected\r
593   *     @arg OB_IWDG_HW: Hardware IWDG selected\r
594   * @param  OB_STOP: Reset event when entering STOP mode.\r
595   *   This parameter can be one of the following values:\r
596   *     @arg OB_STOP_NoRST: No reset generated when entering in STOP\r
597   *     @arg OB_STOP_RST: Reset generated when entering in STOP\r
598   * @param  OB_STDBY: Reset event when entering Standby mode.\r
599   *   This parameter can be one of the following values:\r
600   *     @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY\r
601   *     @arg OB_STDBY_RST: Reset generated when entering in STANDBY\r
602   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, \r
603   * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
604   */\r
605 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)\r
606 {\r
607   FLASH_Status status = FLASH_COMPLETE; \r
608 \r
609   /* Check the parameters */\r
610   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));\r
611   assert_param(IS_OB_STOP_SOURCE(OB_STOP));\r
612   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));\r
613 \r
614   /* Authorize the small information block programming */\r
615   FLASH->OPTKEYR = FLASH_KEY1;\r
616   FLASH->OPTKEYR = FLASH_KEY2;\r
617   \r
618   /* Wait for last operation to be completed */\r
619   status = FLASH_WaitForLastOperation(ProgramTimeout);\r
620   \r
621   if(status == FLASH_COMPLETE)\r
622   {  \r
623     /* Enable the Option Bytes Programming operation */\r
624     FLASH->CR |= CR_OPTPG_Set; \r
625            \r
626     OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8))); \r
627   \r
628     /* Wait for last operation to be completed */\r
629     status = FLASH_WaitForLastOperation(ProgramTimeout);\r
630     if(status != FLASH_TIMEOUT)\r
631     {\r
632       /* if the program operation is completed, disable the OPTPG Bit */\r
633       FLASH->CR &= CR_OPTPG_Reset;\r
634     }\r
635   }    \r
636   /* Return the Option Byte program Status */\r
637   return status;\r
638 }\r
639 \r
640 /**\r
641   * @brief  Returns the FLASH User Option Bytes values.\r
642   * @param  None\r
643   * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)\r
644   *   and RST_STDBY(Bit2).\r
645   */\r
646 uint32_t FLASH_GetUserOptionByte(void)\r
647 {\r
648   /* Return the User Option Byte */\r
649   return (uint32_t)(FLASH->OBR >> 2);\r
650 }\r
651 \r
652 /**\r
653   * @brief  Returns the FLASH Write Protection Option Bytes Register value.\r
654   * @param  None\r
655   * @retval The FLASH Write Protection  Option Bytes Register value\r
656   */\r
657 uint32_t FLASH_GetWriteProtectionOptionByte(void)\r
658 {\r
659   /* Return the Falsh write protection Register value */\r
660   return (uint32_t)(FLASH->WRPR);\r
661 }\r
662 \r
663 /**\r
664   * @brief  Checks whether the FLASH Read Out Protection Status is set or not.\r
665   * @param  None\r
666   * @retval FLASH ReadOut Protection Status(SET or RESET)\r
667   */\r
668 FlagStatus FLASH_GetReadOutProtectionStatus(void)\r
669 {\r
670   FlagStatus readoutstatus = RESET;\r
671   if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)\r
672   {\r
673     readoutstatus = SET;\r
674   }\r
675   else\r
676   {\r
677     readoutstatus = RESET;\r
678   }\r
679   return readoutstatus;\r
680 }\r
681 \r
682 /**\r
683   * @brief  Checks whether the FLASH Prefetch Buffer status is set or not.\r
684   * @param  None\r
685   * @retval FLASH Prefetch Buffer Status (SET or RESET).\r
686   */\r
687 FlagStatus FLASH_GetPrefetchBufferStatus(void)\r
688 {\r
689   FlagStatus bitstatus = RESET;\r
690   \r
691   if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)\r
692   {\r
693     bitstatus = SET;\r
694   }\r
695   else\r
696   {\r
697     bitstatus = RESET;\r
698   }\r
699   /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */\r
700   return bitstatus; \r
701 }\r
702 \r
703 /**\r
704   * @brief  Enables or disables the specified FLASH interrupts.\r
705   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.\r
706   *   This parameter can be any combination of the following values:\r
707   *     @arg FLASH_IT_ERROR: FLASH Error Interrupt\r
708   *     @arg FLASH_IT_EOP: FLASH end of operation Interrupt\r
709   * @param  NewState: new state of the specified Flash interrupts.\r
710   *   This parameter can be: ENABLE or DISABLE.      \r
711   * @retval None \r
712   */\r
713 void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState)\r
714 {\r
715   /* Check the parameters */\r
716   assert_param(IS_FLASH_IT(FLASH_IT)); \r
717   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
718   if(NewState != DISABLE)\r
719   {\r
720     /* Enable the interrupt sources */\r
721     FLASH->CR |= FLASH_IT;\r
722   }\r
723   else\r
724   {\r
725     /* Disable the interrupt sources */\r
726     FLASH->CR &= ~(uint32_t)FLASH_IT;\r
727   }\r
728 }\r
729 \r
730 /**\r
731   * @brief  Checks whether the specified FLASH flag is set or not.\r
732   * @param  FLASH_FLAG: specifies the FLASH flag to check.\r
733   *   This parameter can be one of the following values:\r
734   *     @arg FLASH_FLAG_BSY: FLASH Busy flag           \r
735   *     @arg FLASH_FLAG_PGERR: FLASH Program error flag       \r
736   *     @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      \r
737   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag           \r
738   *     @arg FLASH_FLAG_OPTERR:  FLASH Option Byte error flag     \r
739   * @retval The new state of FLASH_FLAG (SET or RESET).\r
740   */\r
741 FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG)\r
742 {\r
743   FlagStatus bitstatus = RESET;\r
744   /* Check the parameters */\r
745   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;\r
746   if(FLASH_FLAG == FLASH_FLAG_OPTERR) \r
747   {\r
748     if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)\r
749     {\r
750       bitstatus = SET;\r
751     }\r
752     else\r
753     {\r
754       bitstatus = RESET;\r
755     }\r
756   }\r
757   else\r
758   {\r
759    if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)\r
760     {\r
761       bitstatus = SET;\r
762     }\r
763     else\r
764     {\r
765       bitstatus = RESET;\r
766     }\r
767   }\r
768   /* Return the new state of FLASH_FLAG (SET or RESET) */\r
769   return bitstatus;\r
770 }\r
771 \r
772 /**\r
773   * @brief  Clears the FLASH\92s pending flags.\r
774   * @param  FLASH_FLAG: specifies the FLASH flags to clear.\r
775   *   This parameter can be any combination of the following values:         \r
776   *     @arg FLASH_FLAG_PGERR: FLASH Program error flag       \r
777   *     @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      \r
778   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag           \r
779   * @retval None\r
780   */\r
781 void FLASH_ClearFlag(uint16_t FLASH_FLAG)\r
782 {\r
783   /* Check the parameters */\r
784   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;\r
785   \r
786   /* Clear the flags */\r
787   FLASH->SR = FLASH_FLAG;\r
788 }\r
789 \r
790 /**\r
791   * @brief  Returns the FLASH Status.\r
792   * @param  None\r
793   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
794   *   FLASH_ERROR_WRP or FLASH_COMPLETE\r
795   */\r
796 FLASH_Status FLASH_GetStatus(void)\r
797 {\r
798   FLASH_Status flashstatus = FLASH_COMPLETE;\r
799   \r
800   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) \r
801   {\r
802     flashstatus = FLASH_BUSY;\r
803   }\r
804   else \r
805   {  \r
806     if((FLASH->SR & FLASH_FLAG_PGERR) != 0)\r
807     { \r
808       flashstatus = FLASH_ERROR_PG;\r
809     }\r
810     else \r
811     {\r
812       if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )\r
813       {\r
814         flashstatus = FLASH_ERROR_WRP;\r
815       }\r
816       else\r
817       {\r
818         flashstatus = FLASH_COMPLETE;\r
819       }\r
820     }\r
821   }\r
822   /* Return the Flash Status */\r
823   return flashstatus;\r
824 }\r
825 \r
826 /**\r
827   * @brief  Waits for a Flash operation to complete or a TIMEOUT to occur.\r
828   * @param  Timeout: FLASH progamming Timeout\r
829   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
830   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
831   */\r
832 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)\r
833\r
834   FLASH_Status status = FLASH_COMPLETE;\r
835    \r
836   /* Check for the Flash Status */\r
837   status = FLASH_GetStatus();\r
838   /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
839   while((status == FLASH_BUSY) && (Timeout != 0x00))\r
840   {\r
841     delay();\r
842     status = FLASH_GetStatus();\r
843     Timeout--;\r
844   }\r
845   if(Timeout == 0x00 )\r
846   {\r
847     status = FLASH_TIMEOUT;\r
848   }\r
849   /* Return the operation status */\r
850   return status;\r
851 }\r
852 \r
853 /**\r
854   * @brief  Inserts a time delay.\r
855   * @param  None\r
856   * @retval None\r
857   */\r
858 static void delay(void)\r
859 {\r
860   __IO uint32_t i = 0;\r
861   for(i = 0xFF; i != 0; i--)\r
862   {\r
863   }\r
864 }\r
865 \r
866 /**\r
867   * @}\r
868   */\r
869 \r
870 /**\r
871   * @}\r
872   */\r
873 \r
874 /**\r
875   * @}\r
876   */\r
877 \r
878 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/\r