]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3XX/stm32f30x_flash.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3XX / stm32f30x_flash.c
1 /**
2   ******************************************************************************
3   * @file    stm32f30x_flash.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    27-February-2014
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of the FLASH peripheral:
9   *            + FLASH Interface configuration
10   *            + FLASH Memory Programming
11   *            + Option Bytes Programming
12   *            + Interrupts and flags management
13   *  
14   @verbatim
15   
16  ===============================================================================
17                       ##### How to use this driver #####
18  ===============================================================================
19     [..] This driver provides functions to configure and program the FLASH 
20          memory of all STM32F30x devices. These functions are split in 4 groups:
21          (#) FLASH Interface configuration functions: this group includes the
22              management of following features:
23              (++) Set the latency.
24              (++) Enable/Disable the Half Cycle Access.
25              (++) Enable/Disable the prefetch buffer.
26          (#) FLASH Memory Programming functions: this group includes all needed
27              functions to erase and program the main memory:
28              (++) Lock and Unlock the FLASH interface.
29              (++) Erase function: Erase page, erase all pages.
30              (++) Program functions: Half Word and Word write.
31          (#) FLASH Option Bytes Programming functions: this group includes all 
32              needed functions to manage the Option Bytes:
33              (++) Lock and Unlock the Flash Option bytes.
34              (++) Launch the Option Bytes loader
35              (++) Erase the Option Bytes
36              (++) Set/Reset the write protection
37              (++) Set the Read protection Level
38              (++) Program the user option Bytes
39              (++) Set/Reset the BOOT1 bit
40              (++) Enable/Disable the VDDA Analog Monitoring
41              (++) Enable/Disable the SRAM parity
42              (++) Get the user option bytes
43              (++) Get the Write protection
44              (++) Get the read protection status
45          (#) FLASH Interrupts and flags management functions: this group includes 
46              all needed functions to:
47              (++) Enable/Disable the FLASH interrupt sources.
48              (++) Get flags status.
49              (++) Clear flags.
50              (++) Get FLASH operation status.
51              (++) Wait for last FLASH operation.
52  
53   @endverbatim
54                       
55   ******************************************************************************
56   * @attention
57   *
58   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
59   *
60   * Redistribution and use in source and binary forms, with or without modification,
61   * are permitted provided that the following conditions are met:
62   *   1. Redistributions of source code must retain the above copyright notice,
63   *      this list of conditions and the following disclaimer.
64   *   2. Redistributions in binary form must reproduce the above copyright notice,
65   *      this list of conditions and the following disclaimer in the documentation
66   *      and/or other materials provided with the distribution.
67   *   3. Neither the name of STMicroelectronics nor the names of its contributors
68   *      may be used to endorse or promote products derived from this software
69   *      without specific prior written permission.
70   *
71   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
72   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
75   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
78   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
79   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81   *
82   ******************************************************************************
83   */
84
85 /* Includes ------------------------------------------------------------------*/
86 #include "stm32f30x_flash.h"
87
88 /** @addtogroup STM32F30x_StdPeriph_Driver
89   * @{
90   */
91
92 /** @defgroup FLASH 
93   * @brief FLASH driver modules
94   * @{
95   */ 
96
97 /* Private typedef -----------------------------------------------------------*/
98 /* Private define ------------------------------------------------------------*/
99
100 /* FLASH Mask */
101 #define RDPRT_MASK                 ((uint32_t)0x00000002)
102 #define WRP01_MASK                 ((uint32_t)0x0000FFFF)
103 #define WRP23_MASK                 ((uint32_t)0xFFFF0000)
104 /* Private macro -------------------------------------------------------------*/
105 /* Private variables ---------------------------------------------------------*/
106 /* Private function prototypes -----------------------------------------------*/
107 /* Private functions ---------------------------------------------------------*/
108
109 /** @defgroup FLASH_Private_Functions
110   * @{
111   */ 
112
113 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
114   *  @brief   FLASH Interface configuration functions 
115  *
116
117 @verbatim   
118  ===============================================================================
119             ##### FLASH Interface configuration functions #####
120  ===============================================================================
121     [..] This group includes the following functions:
122          (+) void FLASH_SetLatency(uint32_t FLASH_Latency); 
123          (+) void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);     
124          (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
125     [..] The unlock sequence is not needed for these functions.
126  
127 @endverbatim
128   * @{
129   */
130  
131 /**
132   * @brief  Sets the code latency value.
133   * @param  FLASH_Latency: specifies the FLASH Latency value.
134   *          This parameter can be one of the following values:
135   *            @arg FLASH_Latency_0: FLASH Zero Latency cycle
136   *            @arg FLASH_Latency_1: FLASH One Latency cycle
137   *            @arg FLASH_Latency_2: FLASH Two Latency cycles      
138   * @retval None
139   */
140 void FLASH_SetLatency(uint32_t FLASH_Latency)
141 {
142    uint32_t tmpreg = 0;
143   
144   /* Check the parameters */
145   assert_param(IS_FLASH_LATENCY(FLASH_Latency));
146   
147   /* Read the ACR register */
148   tmpreg = FLASH->ACR;  
149   
150   /* Sets the Latency value */
151   tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
152   tmpreg |= FLASH_Latency;
153   
154   /* Write the ACR register */
155   FLASH->ACR = tmpreg;
156 }
157
158 /**
159   * @brief  Enables or disables the Half cycle flash access.
160   * @param  FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
161   *          This parameter can be one of the following values:
162   *            @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
163   *            @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
164   * @retval None
165   */
166 void FLASH_HalfCycleAccessCmd(FunctionalState NewState)
167 {
168   /* Check the parameters */
169   assert_param(IS_FUNCTIONAL_STATE(NewState));
170    
171   if(NewState != DISABLE)
172   {
173     FLASH->ACR |= FLASH_ACR_HLFCYA;
174   }
175   else
176   {
177     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_HLFCYA));
178   }
179 }
180
181 /**
182   * @brief  Enables or disables the Prefetch Buffer.
183   * @param  NewState: new state of the Prefetch Buffer.
184   *          This parameter  can be: ENABLE or DISABLE.
185   * @retval None
186   */
187 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
188 {
189   /* Check the parameters */
190   assert_param(IS_FUNCTIONAL_STATE(NewState));
191    
192   if(NewState != DISABLE)
193   {
194     FLASH->ACR |= FLASH_ACR_PRFTBE;
195   }
196   else
197   {
198     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
199   }
200 }
201
202 /**
203   * @}
204   */
205
206 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
207  *  @brief   FLASH Memory Programming functions
208  *
209 @verbatim   
210  ===============================================================================
211               ##### FLASH Memory Programming functions #####
212  ===============================================================================   
213     [..] This group includes the following functions:
214          (+) void FLASH_Unlock(void);
215          (+) void FLASH_Lock(void);
216          (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
217          (+) FLASH_Status FLASH_EraseAllPages(void);
218          (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
219          (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
220     [..] Any operation of erase or program should follow these steps:
221          (#) Call the FLASH_Unlock() function to enable the FLASH control register 
222              program memory access.
223          (#) Call the desired function to erase page or program data.
224          (#) Call the FLASH_Lock() function to disable the FLASH control register 
225              access (recommended to protect the FLASH memory against possible 
226              unwanted operation).
227     
228 @endverbatim
229   * @{
230   */
231
232 /**
233   * @brief  Unlocks the FLASH control register access
234   * @param  None
235   * @retval None
236   */
237 void FLASH_Unlock(void)
238 {
239   if((FLASH->CR & FLASH_CR_LOCK) != RESET)
240   {
241     /* Authorize the FLASH Registers access */
242     FLASH->KEYR = FLASH_KEY1;
243     FLASH->KEYR = FLASH_KEY2;
244   }  
245 }
246
247 /**
248   * @brief  Locks the FLASH control register access
249   * @param  None
250   * @retval None
251   */
252 void FLASH_Lock(void)
253 {
254   /* Set the LOCK Bit to lock the FLASH Registers access */
255   FLASH->CR |= FLASH_CR_LOCK;
256 }
257
258 /**
259   * @brief  Erases a specified page in program memory.
260   * @note   To correctly run this function, the FLASH_Unlock() function
261   *         must be called before.
262   * @note   Call the FLASH_Lock() to disable the flash memory access 
263   *         (recommended to protect the FLASH memory against possible unwanted operation)  
264   * @param  Page_Address: The page address in program memory to be erased.
265   * @note   A Page is erased in the Program memory only if the address to load 
266   *         is the start address of a page (multiple of 1024 bytes).  
267   * @retval FLASH Status: The returned value can be: 
268   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
269   */
270 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
271 {
272   FLASH_Status status = FLASH_COMPLETE;
273
274   /* Check the parameters */
275   assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
276  
277   /* Wait for last operation to be completed */
278   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
279   
280   if(status == FLASH_COMPLETE)
281   { 
282     /* If the previous operation is completed, proceed to erase the page */
283     FLASH->CR |= FLASH_CR_PER;
284     FLASH->AR  = Page_Address; 
285     FLASH->CR |= FLASH_CR_STRT;
286     
287     /* Wait for last operation to be completed */
288     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
289     
290     /* Disable the PER Bit */
291     FLASH->CR &= ~FLASH_CR_PER;
292   }
293     
294   /* Return the Erase Status */
295   return status;
296 }
297
298 /**
299   * @brief  Erases all FLASH pages.
300   * @note   To correctly run this function, the FLASH_Unlock() function
301   *         must be called before.
302   *         all the FLASH_Lock() to disable the flash memory access 
303   *         (recommended to protect the FLASH memory against possible unwanted operation)
304   * @param  None
305   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
306   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
307   */
308 FLASH_Status FLASH_EraseAllPages(void)
309 {
310   FLASH_Status status = FLASH_COMPLETE;
311
312   /* Wait for last operation to be completed */
313   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
314   
315   if(status == FLASH_COMPLETE)
316   {
317     /* if the previous operation is completed, proceed to erase all pages */
318      FLASH->CR |= FLASH_CR_MER;
319      FLASH->CR |= FLASH_CR_STRT;
320     
321     /* Wait for last operation to be completed */
322     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
323
324     /* Disable the MER Bit */
325     FLASH->CR &= ~FLASH_CR_MER;
326   }
327
328   /* Return the Erase Status */
329   return status;
330 }
331
332 /**
333   * @brief  Programs a word at a specified address.
334   * @note   To correctly run this function, the FLASH_Unlock() function
335   *         must be called before.
336   *         Call the FLASH_Lock() to disable the flash memory access 
337   *         (recommended to protect the FLASH memory against possible unwanted operation)  
338   * @param  Address: specifies the address to be programmed.
339   * @param  Data: specifies the data to be programmed.
340   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
341   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
342   */
343 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
344 {
345   FLASH_Status status = FLASH_COMPLETE;
346   __IO uint32_t tmp = 0;
347
348   /* Check the parameters */
349   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
350
351   /* Wait for last operation to be completed */
352   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
353   
354   if(status == FLASH_COMPLETE)
355   {
356     /* If the previous operation is completed, proceed to program the new first 
357     half word */
358     FLASH->CR |= FLASH_CR_PG;
359   
360     *(__IO uint16_t*)Address = (uint16_t)Data;
361     
362     /* Wait for last operation to be completed */
363     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
364  
365     if(status == FLASH_COMPLETE)
366     {
367       /* If the previous operation is completed, proceed to program the new second 
368       half word */
369       tmp = Address + 2;
370
371       *(__IO uint16_t*) tmp = Data >> 16;
372     
373       /* Wait for last operation to be completed */
374       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
375         
376       /* Disable the PG Bit */
377       FLASH->CR &= ~FLASH_CR_PG;
378     }
379     else
380     {
381       /* Disable the PG Bit */
382       FLASH->CR &= ~FLASH_CR_PG;
383     }
384   }
385    
386   /* Return the Program Status */
387   return status;
388 }
389
390 /**
391   * @brief  Programs a half word at a specified address.
392   * @note   To correctly run this function, the FLASH_Unlock() function
393   *         must be called before.
394   *         Call the FLASH_Lock() to disable the flash memory access 
395   *         (recommended to protect the FLASH memory against possible unwanted operation) 
396   * @param  Address: specifies the address to be programmed.
397   * @param  Data: specifies the data to be programmed.
398   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
399   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
400   */
401 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
402 {
403   FLASH_Status status = FLASH_COMPLETE;
404
405   /* Check the parameters */
406   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
407
408   /* Wait for last operation to be completed */
409   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
410   
411   if(status == FLASH_COMPLETE)
412   {
413     /* If the previous operation is completed, proceed to program the new data */
414     FLASH->CR |= FLASH_CR_PG;
415   
416     *(__IO uint16_t*)Address = Data;
417
418     /* Wait for last operation to be completed */
419     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
420     
421     /* Disable the PG Bit */
422     FLASH->CR &= ~FLASH_CR_PG;
423   } 
424   
425   /* Return the Program Status */
426   return status;
427 }
428
429 /**
430   * @}
431   */
432   
433 /** @defgroup FLASH_Group3 Option Bytes Programming functions
434  *  @brief   Option Bytes Programming functions 
435  *
436 @verbatim   
437  ===============================================================================
438                 ##### Option Bytes Programming functions #####
439  ===============================================================================  
440     [..] This group includes the following functions:
441          (+) void FLASH_OB_Unlock(void);
442          (+) void FLASH_OB_Lock(void);
443          (+) void FLASH_OB_Erase(void);
444          (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
445          (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
446          (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
447          (+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
448          (+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
449          (+) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity);
450          (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);                                  
451          (+) FLASH_Status FLASH_OB_Launch(void);
452          (+) uint32_t FLASH_OB_GetUser(void);                                           
453          (+) uint8_t FLASH_OB_GetWRP(void);                                             
454          (+) uint8_t FLASH_OB_GetRDP(void);                                                     
455     [..] Any operation of erase or program should follow these steps:
456          (#) Call the FLASH_OB_Unlock() function to enable the FLASH option control 
457              register access.
458          (#) Call one or several functions to program the desired Option Bytes:
459              (++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState); 
460                   => to Enable/Disable the desired sector write protection.
461              (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the 
462                   desired read Protection Level.
463              (++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY); 
464                   => to configure the user Option Bytes.
465                  (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1); 
466                   => to set the boot1 mode
467              (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG); 
468                   => to Enable/Disable the VDDA monotoring.
469              (++) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity); 
470                   => to Enable/Disable the SRAM Parity check.            
471                  (++) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER); 
472                   => to write all user option bytes: OB_IWDG, OB_STOP, OB_STDBY, 
473                      OB_BOOT1, OB_VDDA_ANALOG and OB_VDD_SD12.  
474          (#) Once all needed Option Bytes to be programmed are correctly written, 
475              call the FLASH_OB_Launch() function to launch the Option Bytes 
476              programming process.
477          (#@) When changing the IWDG mode from HW to SW or from SW to HW, a system 
478               reset is needed to make the change effective.  
479          (#) Call the FLASH_OB_Lock() function to disable the FLASH option control 
480              register access (recommended to protect the Option Bytes against 
481              possible unwanted operations).
482     
483 @endverbatim
484   * @{
485   */
486
487 /**
488   * @brief  Unlocks the option bytes block access.
489   * @param  None
490   * @retval None
491   */
492 void FLASH_OB_Unlock(void)
493 {
494   if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
495   { 
496     /* Unlocking the option bytes block access */
497     FLASH->OPTKEYR = FLASH_OPTKEY1;
498     FLASH->OPTKEYR = FLASH_OPTKEY2;
499   }
500 }
501
502 /**
503   * @brief  Locks the option bytes block access.
504   * @param  None
505   * @retval None
506   */
507 void FLASH_OB_Lock(void)
508 {
509   /* Set the OPTWREN Bit to lock the option bytes block access */
510   FLASH->CR &= ~FLASH_CR_OPTWRE;
511 }
512
513 /**
514   * @brief  Launch the option byte loading.
515   * @param  None
516   * @retval None
517   */
518 void FLASH_OB_Launch(void)
519 {
520   /* Set the OBL_Launch bit to launch the option byte loading */
521   FLASH->CR |= FLASH_CR_OBL_LAUNCH; 
522 }
523
524 /**
525   * @brief  Erases the FLASH option bytes.
526   * @note   This functions erases all option bytes except the Read protection (RDP). 
527   * @param  None
528   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
529   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
530   */
531 FLASH_Status FLASH_OB_Erase(void)
532 {
533   uint16_t rdptmp = OB_RDP_Level_0;
534
535   FLASH_Status status = FLASH_COMPLETE;
536
537   /* Get the actual read protection Option Byte value */ 
538   if(FLASH_OB_GetRDP() != RESET)
539   {
540     rdptmp = 0x00;  
541   }
542
543   /* Wait for last operation to be completed */
544   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
545
546   if(status == FLASH_COMPLETE)
547   {   
548     /* If the previous operation is completed, proceed to erase the option bytes */
549     FLASH->CR |= FLASH_CR_OPTER;
550     FLASH->CR |= FLASH_CR_STRT;
551
552     /* Wait for last operation to be completed */
553     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
554     
555     if(status == FLASH_COMPLETE)
556     {
557       /* If the erase operation is completed, disable the OPTER Bit */
558       FLASH->CR &= ~FLASH_CR_OPTER;
559        
560       /* Enable the Option Bytes Programming operation */
561       FLASH->CR |= FLASH_CR_OPTPG;
562
563       /* Restore the last read protection Option Byte value */
564       OB->RDP = (uint16_t)rdptmp; 
565
566       /* Wait for last operation to be completed */
567       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
568  
569       if(status != FLASH_TIMEOUT)
570       {
571         /* if the program operation is completed, disable the OPTPG Bit */
572         FLASH->CR &= ~FLASH_CR_OPTPG;
573       }
574     }
575     else
576     {
577       if (status != FLASH_TIMEOUT)
578       {
579         /* Disable the OPTPG Bit */
580         FLASH->CR &= ~FLASH_CR_OPTPG;
581       }
582     }  
583   }
584   /* Return the erase status */
585   return status;
586 }
587
588 /**
589   * @brief  Write protects the desired pages
590   * @note   To correctly run this function, the FLASH_OB_Unlock() function
591   *         must be called before.
592   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
593   *         (recommended to protect the FLASH memory against possible unwanted operation)    
594   * @param  OB_WRP: specifies the address of the pages to be write protected.
595   *   This parameter can be:
596   *     @arg  value between OB_WRP_Pages0to35 and OB_WRP_Pages60to63
597   *     @arg OB_WRP_AllPages
598   * @retval FLASH Status: The returned value can be: 
599   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
600   */
601 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
602 {
603   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
604   
605   FLASH_Status status = FLASH_COMPLETE;
606   
607   /* Check the parameters */
608   assert_param(IS_OB_WRP(OB_WRP));
609     
610   OB_WRP = (uint32_t)(~OB_WRP);
611   WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
612   WRP1_Data = (uint16_t)((OB_WRP & OB_WRP0_nWRP0) >> 8);
613   
614   /* Wait for last operation to be completed */
615   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
616   
617   if(status == FLASH_COMPLETE)
618   {
619     FLASH->CR |= FLASH_CR_OPTPG;
620
621     if(WRP0_Data != 0xFF)
622     {
623       OB->WRP0 = WRP0_Data;
624       
625       /* Wait for last operation to be completed */
626       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
627     }
628     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
629     {
630       OB->WRP1 = WRP1_Data;
631       
632       /* Wait for last operation to be completed */
633       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
634     }
635     if(status != FLASH_TIMEOUT)
636     {
637       /* if the program operation is completed, disable the OPTPG Bit */
638       FLASH->CR &= ~FLASH_CR_OPTPG;
639     }
640   } 
641   /* Return the write protection operation Status */
642   return status;      
643 }
644
645 /**
646   * @brief  Enables or disables the read out protection.
647   * @note   To correctly run this function, the FLASH_OB_Unlock() function
648   *         must be called before.
649   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
650   *         (recommended to protect the FLASH memory against possible unwanted operation)   
651   * @param  FLASH_ReadProtection_Level: specifies the read protection level. 
652   *   This parameter can be:
653   *     @arg OB_RDP_Level_0: No protection
654   *     @arg OB_RDP_Level_1: Read protection of the memory                     
655   *     @arg OB_RDP_Level_2: Chip protection
656   *     @retval FLASH Status: The returned value can be: 
657   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
658   */
659 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
660 {
661   FLASH_Status status = FLASH_COMPLETE;
662   
663   /* Check the parameters */
664   assert_param(IS_OB_RDP(OB_RDP));
665   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
666   
667   if(status == FLASH_COMPLETE)
668   {
669     FLASH->CR |= FLASH_CR_OPTER;
670     FLASH->CR |= FLASH_CR_STRT;
671     
672     /* Wait for last operation to be completed */
673     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
674     
675     if(status == FLASH_COMPLETE)
676     {
677       /* If the erase operation is completed, disable the OPTER Bit */
678       FLASH->CR &= ~FLASH_CR_OPTER;
679       
680       /* Enable the Option Bytes Programming operation */
681       FLASH->CR |= FLASH_CR_OPTPG;
682        
683       OB->RDP = OB_RDP;
684
685       /* Wait for last operation to be completed */
686       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT); 
687     
688       if(status != FLASH_TIMEOUT)
689       {
690         /* if the program operation is completed, disable the OPTPG Bit */
691         FLASH->CR &= ~FLASH_CR_OPTPG;
692       }
693     }
694     else 
695     {
696       if(status != FLASH_TIMEOUT)
697       {
698         /* Disable the OPTER Bit */
699         FLASH->CR &= ~FLASH_CR_OPTER;
700       }
701     }
702   }
703   /* Return the protection operation Status */
704   return status;             
705 }
706
707 /**
708   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
709   * @param  OB_IWDG: Selects the IWDG mode
710   *   This parameter can be one of the following values:
711   *     @arg OB_IWDG_SW: Software IWDG selected
712   *     @arg OB_IWDG_HW: Hardware IWDG selected
713   * @param  OB_STOP: Reset event when entering STOP mode.
714   *   This parameter can be one of the following values:
715   *     @arg OB_STOP_NoRST: No reset generated when entering in STOP
716   *     @arg OB_STOP_RST: Reset generated when entering in STOP
717   * @param  OB_STDBY: Reset event when entering Standby mode.
718   *   This parameter can be one of the following values:
719   *     @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
720   *     @arg OB_STDBY_RST: Reset generated when entering in STANDBY
721   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, 
722   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
723   */
724 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
725 {
726   FLASH_Status status = FLASH_COMPLETE; 
727
728   /* Check the parameters */
729   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
730   assert_param(IS_OB_STOP_SOURCE(OB_STOP));
731   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
732
733   /* Authorize the small information block programming */
734   FLASH->OPTKEYR = FLASH_KEY1;
735   FLASH->OPTKEYR = FLASH_KEY2;
736   
737   /* Wait for last operation to be completed */
738   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
739   
740   if(status == FLASH_COMPLETE)
741   {  
742     /* Enable the Option Bytes Programming operation */
743     FLASH->CR |= FLASH_CR_OPTPG; 
744            
745     OB->USER = (uint8_t)((uint8_t)(OB_IWDG | OB_STOP) | (uint8_t)(OB_STDBY |0xF8));
746   
747     /* Wait for last operation to be completed */
748     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
749
750     if(status != FLASH_TIMEOUT)
751     {
752       /* if the program operation is completed, disable the OPTPG Bit */
753       FLASH->CR &= ~FLASH_CR_OPTPG;
754     }
755   }    
756   /* Return the Option Byte program Status */
757   return status;
758 }
759
760 /**
761   * @brief  Sets or resets the BOOT1. 
762   * @param  OB_BOOT1: Set or Reset the BOOT1.
763   *   This parameter can be one of the following values:
764   *     @arg OB_BOOT1_RESET: BOOT1 Reset
765   *     @arg OB_BOOT1_SET: BOOT1 Set
766   * @retval None
767   */
768 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
769 {
770   FLASH_Status status = FLASH_COMPLETE; 
771
772   /* Check the parameters */
773   assert_param(IS_OB_BOOT1(OB_BOOT1));
774
775   /* Authorize the small information block programming */
776   FLASH->OPTKEYR = FLASH_KEY1;
777   FLASH->OPTKEYR = FLASH_KEY2;
778   
779   /* Wait for last operation to be completed */
780   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
781   
782   if(status == FLASH_COMPLETE)
783   {  
784     /* Enable the Option Bytes Programming operation */
785     FLASH->CR |= FLASH_CR_OPTPG; 
786            
787         OB->USER = OB_BOOT1|0xEF;
788   
789     /* Wait for last operation to be completed */
790     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
791
792     if(status != FLASH_TIMEOUT)
793     {
794       /* if the program operation is completed, disable the OPTPG Bit */
795       FLASH->CR &= ~FLASH_CR_OPTPG;
796     }
797   }    
798   /* Return the Option Byte program Status */
799   return status;
800 }
801
802 /**
803   * @brief  Sets or resets the analogue monitoring on VDDA Power source. 
804   * @param  OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
805   *   This parameter can be one of the following values:
806   *     @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
807   *     @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
808   * @retval None
809   */
810 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
811 {
812   FLASH_Status status = FLASH_COMPLETE; 
813
814   /* Check the parameters */
815   assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
816
817   /* Authorize the small information block programming */
818   FLASH->OPTKEYR = FLASH_KEY1;
819   FLASH->OPTKEYR = FLASH_KEY2;
820   
821   /* Wait for last operation to be completed */
822   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
823   
824   if(status == FLASH_COMPLETE)
825   {  
826     /* Enable the Option Bytes Programming operation */
827     FLASH->CR |= FLASH_CR_OPTPG; 
828            
829         OB->USER = OB_VDDA_ANALOG |0xDF;
830   
831     /* Wait for last operation to be completed */
832     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
833
834     if(status != FLASH_TIMEOUT)
835     {
836       /* if the program operation is completed, disable the OPTPG Bit */
837       FLASH->CR &= ~FLASH_CR_OPTPG;
838     }
839   }    
840   /* Return the Option Byte program Status */
841   return status;
842 }
843
844 /**
845   * @brief  Sets or resets the SRAM partiy.
846   * @param  OB_SRAM_Parity: Set or Reset the SRAM partiy enable bit.
847   *         This parameter can be one of the following values:
848   *             @arg OB_SRAM_PARITY_SET: Set SRAM partiy.
849   *             @arg OB_SRAM_PARITY_RESET: Reset SRAM partiy.
850   * @retval None
851   */
852 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
853 {
854   FLASH_Status status = FLASH_COMPLETE; 
855
856   /* Check the parameters */
857   assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
858
859   /* Wait for last operation to be completed */
860   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
861   
862   if(status == FLASH_COMPLETE)
863   {  
864     /* Enable the Option Bytes Programming operation */
865     FLASH->CR |= FLASH_CR_OPTPG; 
866
867     OB->USER = OB_SRAM_Parity | 0xBF;
868   
869     /* Wait for last operation to be completed */
870     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
871
872     if(status != FLASH_TIMEOUT)
873     {
874       /* if the program operation is completed, disable the OPTPG Bit */
875       FLASH->CR &= ~FLASH_CR_OPTPG;
876     }
877   }
878   /* Return the Option Byte program Status */
879   return status;
880 }
881
882 /**
883   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY/ BOOT1 and OB_VDDA_ANALOG.
884   * @note   To correctly run this function, the FLASH_OB_Unlock() function
885   *         must be called before.
886   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
887   *         (recommended to protect the FLASH memory against possible unwanted operation)   
888   * @param  OB_USER: Selects all user option bytes
889   *   This parameter is a combination of the following values:
890   *     @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
891   *     @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
892   *     @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
893   *     @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
894   *     @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
895   * @retval FLASH Status: The returned value can be: 
896   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
897   */
898 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
899 {
900   FLASH_Status status = FLASH_COMPLETE; 
901
902   /* Authorize the small information block programming */
903   FLASH->OPTKEYR = FLASH_KEY1;
904   FLASH->OPTKEYR = FLASH_KEY2;
905   
906   /* Wait for last operation to be completed */
907   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
908   
909   if(status == FLASH_COMPLETE)
910   {  
911     /* Enable the Option Bytes Programming operation */
912     FLASH->CR |= FLASH_CR_OPTPG; 
913            
914           OB->USER = OB_USER | 0x88;
915   
916     /* Wait for last operation to be completed */
917     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
918
919     if(status != FLASH_TIMEOUT)
920     {
921       /* if the program operation is completed, disable the OPTPG Bit */
922       FLASH->CR &= ~FLASH_CR_OPTPG;
923     }
924   }    
925   /* Return the Option Byte program Status */
926   return status;
927
928 }
929
930 /**
931   * @brief  Programs a half word at a specified Option Byte Data address.
932   * @note    To correctly run this function, the FLASH_OB_Unlock() function
933   *           must be called before.
934   *          Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
935   *          (recommended to protect the FLASH memory against possible unwanted operation)
936   * @param  Address: specifies the address to be programmed.
937   *   This parameter can be 0x1FFFF804 or 0x1FFFF806. 
938   * @param  Data: specifies the data to be programmed.
939   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
940   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
941   */
942 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
943 {
944   FLASH_Status status = FLASH_COMPLETE;
945   /* Check the parameters */
946   assert_param(IS_OB_DATA_ADDRESS(Address));
947   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
948
949   if(status == FLASH_COMPLETE)
950   {
951     /* Enables the Option Bytes Programming operation */
952     FLASH->CR |= FLASH_CR_OPTPG; 
953     *(__IO uint16_t*)Address = Data;
954     
955     /* Wait for last operation to be completed */
956     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
957     
958     if(status != FLASH_TIMEOUT)
959     {
960       /* If the program operation is completed, disable the OPTPG Bit */
961       FLASH->CR &= ~FLASH_CR_OPTPG;
962     }
963   }
964   /* Return the Option Byte Data Program Status */
965   return status;
966 }
967
968 /**
969   * @brief  Returns the FLASH User Option Bytes values.
970   * @param  None
971   * @retval The FLASH User Option Bytes .
972   */
973 uint8_t FLASH_OB_GetUser(void)
974 {
975   /* Return the User Option Byte */
976   return (uint8_t)(FLASH->OBR >> 8);
977 }
978
979 /**
980   * @brief  Returns the FLASH Write Protection Option Bytes value.
981   * @param  None
982   * @retval The FLASH Write Protection Option Bytes value
983   */
984 uint32_t FLASH_OB_GetWRP(void)
985 {
986   /* Return the FLASH write protection Register value */
987   return (uint32_t)(FLASH->WRPR);
988 }
989
990 /**
991   * @brief  Checks whether the FLASH Read out Protection Status is set or not.
992   * @param  None
993   * @retval FLASH ReadOut Protection Status(SET or RESET)
994   */
995 FlagStatus FLASH_OB_GetRDP(void)
996 {
997   FlagStatus readstatus = RESET;
998   
999   if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
1000   {
1001     readstatus = SET;
1002   }
1003   else
1004   {
1005     readstatus = RESET;
1006   }
1007   return readstatus;
1008 }
1009
1010 /**
1011   * @}
1012   */
1013
1014 /** @defgroup FLASH_Group4 Interrupts and flags management functions
1015  *  @brief   Interrupts and flags management functions
1016  *
1017 @verbatim   
1018  ===============================================================================
1019              ##### Interrupts and flags management functions #####
1020  ===============================================================================  
1021
1022 @endverbatim
1023   * @{
1024   */
1025
1026 /**
1027   * @brief  Enables or disables the specified FLASH interrupts.
1028   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or 
1029   *         disabled.
1030   *   This parameter can be any combination of the following values:     
1031   *     @arg FLASH_IT_EOP: FLASH end of programming Interrupt
1032   *     @arg FLASH_IT_ERR: FLASH Error Interrupt 
1033   * @retval None 
1034   */
1035 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
1036 {
1037   /* Check the parameters */
1038   assert_param(IS_FLASH_IT(FLASH_IT)); 
1039   assert_param(IS_FUNCTIONAL_STATE(NewState));
1040   
1041   if(NewState != DISABLE)
1042   {
1043     /* Enable the interrupt sources */
1044     FLASH->CR |= FLASH_IT;
1045   }
1046   else
1047   {
1048     /* Disable the interrupt sources */
1049     FLASH->CR &= ~(uint32_t)FLASH_IT;
1050   }
1051 }
1052
1053 /**
1054   * @brief  Checks whether the specified FLASH flag is set or not.
1055   * @param  FLASH_FLAG: specifies the FLASH flag to check.
1056   *   This parameter can be one of the following values:
1057   *     @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag 
1058   *     @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
1059   *     @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1060   *     @arg FLASH_FLAG_EOP: FLASH End of Programming flag        
1061   * @retval The new state of FLASH_FLAG (SET or RESET).
1062   */
1063 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
1064 {
1065   FlagStatus bitstatus = RESET;
1066
1067   /* Check the parameters */
1068   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
1069
1070   if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
1071   {
1072     bitstatus = SET;
1073   }
1074   else
1075   {
1076     bitstatus = RESET;
1077   }
1078   /* Return the new state of FLASH_FLAG (SET or RESET) */
1079   return bitstatus; 
1080 }
1081
1082 /**
1083   * @brief  Clears the FLASH's pending flags.
1084   * @param  FLASH_FLAG: specifies the FLASH flags to clear.
1085   *   This parameter can be any combination of the following values:
1086   *     @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
1087   *     @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1088   *     @arg FLASH_FLAG_EOP: FLASH End of Programming flag                
1089   * @retval None
1090   */
1091 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
1092 {
1093   /* Check the parameters */
1094   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
1095   
1096   /* Clear the flags */
1097   FLASH->SR = FLASH_FLAG;
1098 }
1099
1100 /**
1101   * @brief  Returns the FLASH Status.
1102   * @param  None
1103   * @retval FLASH Status: The returned value can be: 
1104   *         FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
1105   */
1106 FLASH_Status FLASH_GetStatus(void)
1107 {
1108   FLASH_Status FLASHstatus = FLASH_COMPLETE;
1109   
1110   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) 
1111   {
1112     FLASHstatus = FLASH_BUSY;
1113   }
1114   else 
1115   {  
1116     if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
1117     { 
1118       FLASHstatus = FLASH_ERROR_WRP;
1119     }
1120     else 
1121     {
1122       if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
1123       {
1124         FLASHstatus = FLASH_ERROR_PROGRAM; 
1125       }
1126       else
1127       {
1128         FLASHstatus = FLASH_COMPLETE;
1129       }
1130     }
1131   }
1132   /* Return the FLASH Status */
1133   return FLASHstatus;
1134 }
1135
1136 /**
1137   * @brief  Waits for a FLASH operation to complete or a TIMEOUT to occur.
1138   * @param  Timeout: FLASH programming Timeout
1139   * @retval FLASH Status: The returned value can be: FLASH_BUSY, 
1140   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1141   */
1142 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
1143
1144   FLASH_Status status = FLASH_COMPLETE;
1145    
1146   /* Check for the FLASH Status */
1147   status = FLASH_GetStatus();
1148   
1149   /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
1150   while((status == FLASH_BUSY) && (Timeout != 0x00))
1151   {
1152     status = FLASH_GetStatus();
1153     Timeout--;
1154   }
1155   
1156   if(Timeout == 0x00 )
1157   {
1158     status = FLASH_TIMEOUT;
1159   }
1160   /* Return the operation status */
1161   return status;
1162 }
1163
1164 /**
1165   * @}
1166   */ 
1167
1168 /**
1169   * @}
1170   */ 
1171
1172 /**
1173   * @}
1174   */ 
1175
1176 /**
1177   * @}
1178   */
1179
1180 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/