1 /**
2   ******************************************************************************
3   * @file    stm32f37x_flash.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    20-September-2012
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                     ##### How to use this driver #####
17  ===============================================================================
18     [..] This driver provides functions to configure and program the Flash
19          memory of all STM32F37x devices. These functions are split in 4 groups
20          (#) FLASH Interface configuration functions: this group includes the
21              management of following features:
22              (++) Set the latency
23              (++) Enable/Disable the Half Cycle Access
24              (++) Enable/Disable the prefetch buffer
25 
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 
32          (#) FLASH Option Bytes Programming functions: this group includes all
33              needed functions to:
34              (++) Lock and Unlock the Flash Option bytes.
35              (++) Launch the Option Bytes loader
36              (++) Erase the Option Bytes
37              (++) Set/Reset the write protection
38              (++) Set the Read protection Level
39              (++) Program the user option Bytes
40              (++) Set/Reset the BOOT1 bit
41              (++) Enable/Disable the VDDA Analog Monitoring
42              (++) Enable/Disable the VDD_SD12 Analog Monitoring
43              (++) Get the user option bytes
44              (++) Get the Write protection
45              (++) Get the read protection status
46 
47          (#) FLASH Interrupts and flag management functions: this group includes
48              all needed functions to:
49              (++) Enable/Disable the flash interrupt sources
50              (++) Get flags status
51              (++) Clear flags
52              (++) Get Flash operation status
53              (++) Wait for last flash operation
54 
55  @endverbatim
56 
57   ******************************************************************************
58   * @attention
59   *
60   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
61   *
62   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
63   * You may not use this file except in compliance with the License.
64   * You may obtain a copy of the License at:
65   *
66   *        http://www.st.com/software_license_agreement_liberty_v2
67   *
68   * Unless required by applicable law or agreed to in writing, software
69   * distributed under the License is distributed on an "AS IS" BASIS,
70   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
71   * See the License for the specific language governing permissions and
72   * limitations under the License.
73   *
74   ******************************************************************************
75   */
76 
77 /* Includes ------------------------------------------------------------------*/
78 #include "stm32f37x_flash.h"
79 
80 /** @addtogroup STM32F37x_StdPeriph_Driver
81   * @{
82   */
83 
84 /** @defgroup FLASH
85   * @brief FLASH driver modules
86   * @{
87   */
88 
89 /* Private typedef -----------------------------------------------------------*/
90 /* Private define ------------------------------------------------------------*/
91 /* Private macro -------------------------------------------------------------*/
92 /* Private variables ---------------------------------------------------------*/
93 /* Private function prototypes -----------------------------------------------*/
94 /* Private functions ---------------------------------------------------------*/
95 
96 /** @defgroup FLASH_Private_Functions
97   * @{
98   */
99 
100 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
101   *  @brief   FLASH Interface configuration functions
102  *
103 @verbatim
104  ===============================================================================
105                ##### FLASH Interface configuration functions #####
106  ===============================================================================
107 
108     [..] FLASH_Interface configuration_Functions, includes the following functions:
109        (+) void FLASH_SetLatency(uint32_t FLASH_Latency):
110     [..] To correctly read data from Flash memory, the number of wait states (LATENCY)
111      must be correctly programmed according to the frequency of the CPU clock (HCLK)
112     [..]
113         +------------------------------------------------+
114         |  Wait states   |   HCLK clock frequency (MHz)  |
115         |----------------|-------------------------------|
116         |0WS(1CPU cycle) |       0 < HCLK <= 24          |
117         |----------------|-------------------------------|
118         |1WS(2CPU cycles)|       24 < HCLK <= 48         |
119         |----------------|-------------------------------|
120         |2WS(3CPU cycles)|       48 < HCLK <= 72         |
121         +------------------------------------------------+
122        (+) void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
123        (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
124     [..]
125      All these functions don't need the unlock sequence.
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   */
FLASH_SetLatency(uint32_t FLASH_Latency)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   */
FLASH_HalfCycleAccessCmd(FunctionalState NewState)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   */
FLASH_PrefetchBufferCmd(FunctionalState NewState)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 
214     [..] The FLASH Memory Programming functions, includes the following functions:
215        (+) void FLASH_Unlock(void);
216        (+) void FLASH_Lock(void);
217        (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
218        (+) FLASH_Status FLASH_EraseAllPages(void);
219        (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
220        (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
221 
222     [..] Any operation of erase or program should follow these steps:
223 
224        (#) Call the FLASH_Unlock() function to enable the flash control register and
225            program memory access
226        (#) Call the desired function to erase page or program data
227        (#) Call the FLASH_Lock() to disable the flash program memory access
228       (recommended to protect the FLASH memory against possible unwanted operation)
229 
230 @endverbatim
231   * @{
232   */
233 /**
234   * @brief  Unlocks the FLASH control register and program memory access.
235   * @param  None
236   * @retval None
237   */
FLASH_Unlock(void)238 void FLASH_Unlock(void)
239 {
240   if((FLASH->CR & FLASH_CR_LOCK) != RESET)
241   {
242     /* Authorize the FLASH Registers access */
243     FLASH->KEYR = FLASH_KEY1;
244     FLASH->KEYR = FLASH_KEY2;
245   }
246 }
247 
248 /**
249   * @brief  Locks the FLASH control register access
250   * @param  None
251   * @retval None
252   */
FLASH_Lock(void)253 void FLASH_Lock(void)
254 {
255   /* Set the LOCK Bit to lock the FLASH Registers access */
256   FLASH->CR |= FLASH_CR_LOCK;
257 }
258 
259 /**
260   * @brief  Erases a specified page in program memory.
261   * @note   To correctly run this function, the FLASH_Unlock() function 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   */
FLASH_ErasePage(uint32_t Page_Address)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   * @note   Call 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   */
FLASH_EraseAllPages(void)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 must be called before.
335   * @note   Call the FLASH_Lock() to disable the flash memory access
336   *          (recommended to protect the FLASH memory against possible unwanted operation)
337   * @param  Address: specifies the address to be programmed.
338   * @param  Data: specifies the data to be programmed.
339   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
340   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
341   */
FLASH_ProgramWord(uint32_t Address,uint32_t Data)342 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
343 {
344   FLASH_Status status = FLASH_COMPLETE;
345   __IO uint32_t tmp = 0;
346 
347   /* Check the parameters */
348   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
349 
350   /* Wait for last operation to be completed */
351   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
352 
353   if(status == FLASH_COMPLETE)
354   {
355     /* If the previous operation is completed, proceed to program the new first
356     half word */
357     FLASH->CR |= FLASH_CR_PG;
358 
359     *(__IO uint16_t*)Address = (uint16_t)Data;
360 
361     /* Wait for last operation to be completed */
362     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
363 
364     if(status == FLASH_COMPLETE)
365     {
366       /* If the previous operation is completed, proceed to program the new second
367       half word */
368       tmp = Address + 2;
369 
370       *(__IO uint16_t*) tmp = Data >> 16;
371 
372       /* Wait for last operation to be completed */
373       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
374 
375       /* Disable the PG Bit */
376       FLASH->CR &= ~FLASH_CR_PG;
377     }
378     else
379     {
380       /* Disable the PG Bit */
381       FLASH->CR &= ~FLASH_CR_PG;
382     }
383   }
384 
385   /* Return the Program Status */
386   return status;
387 }
388 
389 /**
390   * @brief  Programs a half word at a specified address.
391   * @note   To correctly run this function, the FLASH_Unlock() function must be called before.
392   * @note   Call the FLASH_Lock() to disable the flash memory access
393   *         (recommended to protect the FLASH memory against possible unwanted operation)
394   * @param  Address: specifies the address to be programmed.
395   * @param  Data: specifies the data to be programmed.
396   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
397   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
398   */
FLASH_ProgramHalfWord(uint32_t Address,uint16_t Data)399 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
400 {
401   FLASH_Status status = FLASH_COMPLETE;
402 
403   /* Check the parameters */
404   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
405 
406   /* Wait for last operation to be completed */
407   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
408 
409   if(status == FLASH_COMPLETE)
410   {
411     /* If the previous operation is completed, proceed to program the new data */
412     FLASH->CR |= FLASH_CR_PG;
413 
414     *(__IO uint16_t*)Address = Data;
415 
416     /* Wait for last operation to be completed */
417     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
418 
419     /* Disable the PG Bit */
420     FLASH->CR &= ~FLASH_CR_PG;
421   }
422 
423   /* Return the Program Status */
424   return status;
425 }
426 
427 /**
428   * @}
429   */
430 
431 /** @defgroup FLASH_Group3 Option Bytes Programming functions
432  *  @brief   Option Bytes Programming functions
433  *
434 @verbatim
435  ===============================================================================
436                 ##### Option Bytes Programming functions #####
437  ===============================================================================
438 
439     [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
440        (+) void FLASH_OB_Unlock(void);
441        (+) void FLASH_OB_Lock(void);
442        (+) void FLASH_OB_Launch(void);
443        (+) FLASH_Status FLASH_OB_Erase(void);
444        (+) FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
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_VDD_SD12Config(uint8_t OB_VDD_SD12);
450        (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
451        (+) uint8_t FLASH_OB_GetUser(void);
452        (+) uint32_t FLASH_OB_GetWRP(void);
453        (+) FlagStatus FLASH_OB_GetRDP(void);
454 
455     [..] Any operation of erase or program should follow these steps:
456 
457    (#) Call the FLASH_OB_Unlock() function to enable the Option Bytes registers access
458 
459    (#) Call one or several functions to program the desired option bytes
460       (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
461       (++) FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
462            => to Enable/Disable the desired sector write protection
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: IWDG, STOP and the Standby.
465       (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
466            => to set or reset BOOT1
467       (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
468            => to enable or disable the VDDA Analog Monitoring
469       (++) FLASH_Status FLASH_OB_VDD_SD12Config(uint8_t OB_VDD_SD12)
470             => to Enable/Disable the VDD_SD12 monotoring
471       (++) You can write all User Options bytes at once using a single function
472            by calling FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
473 
474    (#) Once all needed option bytes to be programmed are correctly written, call the
475       FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
476 
477    (#) Call the FLASH_OB_Lock() to disable the Option Bytes registers access (recommended
478       to protect the option Bytes against possible unwanted operations)
479 
480 @endverbatim
481   * @{
482   */
483 /**
484   * @brief  Unlocks the option bytes block access.
485   * @param  None
486   * @retval None
487   */
FLASH_OB_Unlock(void)488 void FLASH_OB_Unlock(void)
489 {
490   if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
491   {
492     /* Unlocking the option bytes block access */
493     FLASH->OPTKEYR = FLASH_OPTKEY1;
494     FLASH->OPTKEYR = FLASH_OPTKEY2;
495   }
496 }
497 
498 /**
499   * @brief  Locks the option bytes block access.
500   * @param  None
501   * @retval None
502   */
FLASH_OB_Lock(void)503 void FLASH_OB_Lock(void)
504 {
505   /* Set the OPTWREN Bit to lock the option bytes block access */
506   FLASH->CR &= ~FLASH_CR_OPTWRE;
507 }
508 
509 /**
510   * @brief  Launch the option byte loading.
511   * @param  None
512   * @retval None
513   */
FLASH_OB_Launch(void)514 void FLASH_OB_Launch(void)
515 {
516   /* Set the OBL_Launch bit to launch the option byte loading */
517   FLASH->CR |= FLASH_CR_OBL_LAUNCH;
518 }
519 
520 /**
521   * @brief  Erases the FLASH option bytes.
522   * @note   To correctly run this function, the FLASH_OB_Unlock() function must be called before.
523   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
524   *         (recommended to protect the FLASH memory against possible unwanted operation)
525   * @note   This functions erases all option bytes except the Read protection (RDP).
526   * @param  None
527   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
528   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
529   */
FLASH_OB_Erase(void)530 FLASH_Status FLASH_OB_Erase(void)
531 {
532   uint16_t rdptmp = OB_RDP_Level_0;
533 
534   FLASH_Status status = FLASH_COMPLETE;
535 
536   /* Get the actual read protection Option Byte value */
537   if(FLASH_OB_GetRDP() != RESET)
538   {
539     rdptmp = 0x00;
540   }
541 
542   /* Wait for last operation to be completed */
543   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
544 
545   if(status == FLASH_COMPLETE)
546   {
547     /* If the previous operation is completed, proceed to erase the option bytes */
548     FLASH->CR |= FLASH_CR_OPTER;
549     FLASH->CR |= FLASH_CR_STRT;
550 
551     /* Wait for last operation to be completed */
552     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
553 
554     if(status == FLASH_COMPLETE)
555     {
556       /* If the erase operation is completed, disable the OPTER Bit */
557       FLASH->CR &= ~FLASH_CR_OPTER;
558 
559       /* Enable the Option Bytes Programming operation */
560       FLASH->CR |= FLASH_CR_OPTPG;
561 
562       /* Restore the last read protection Option Byte value */
563       OB->RDP = (uint16_t)rdptmp;
564 
565       /* Wait for last operation to be completed */
566       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
567 
568       if(status != FLASH_TIMEOUT)
569       {
570         /* if the program operation is completed, disable the OPTPG Bit */
571         FLASH->CR &= ~FLASH_CR_OPTPG;
572       }
573     }
574     else
575     {
576       if (status != FLASH_TIMEOUT)
577       {
578         /* Disable the OPTPG Bit */
579         FLASH->CR &= ~FLASH_CR_OPTPG;
580       }
581     }
582   }
583   /* Return the erase status */
584   return status;
585 }
586 
587 /**
588   * @brief  Programs a half word at a specified Option Byte Data address.
589   * @note   To correctly run this function, the FLASH_OB_Unlock() function must be called before.
590   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
591   *         (recommended to protect the FLASH memory against possible unwanted operation)
592   * @param  Address: specifies the address to be programmed.
593   *         This parameter can be 0x1FFFF804 or 0x1FFFF806.
594   * @param  Data: specifies the data to be programmed.
595   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
596   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
597   */
FLASH_OB_ProgramData(uint32_t Address,uint8_t Data)598 FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
599 {
600   FLASH_Status status = FLASH_COMPLETE;
601   /* Check the parameters */
602   assert_param(IS_OB_DATA_ADDRESS(Address));
603   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
604 
605   if(status == FLASH_COMPLETE)
606   {
607     /* Enables the Option Bytes Programming operation */
608     FLASH->CR |= FLASH_CR_OPTPG;
609     *(__IO uint16_t*)Address = Data;
610 
611     /* Wait for last operation to be completed */
612     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
613 
614     if(status != FLASH_TIMEOUT)
615     {
616       /* If the program operation is completed, disable the OPTPG Bit */
617       FLASH->CR &= ~FLASH_CR_OPTPG;
618     }
619   }
620   /* Return the Option Byte Data Program Status */
621   return status;
622 }
623 
624 /**
625   * @brief  Write protects the desired pages
626   * @note   To correctly run this function, the FLASH_OB_Unlock() function must be called before.
627   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
628   *         (recommended to protect the FLASH memory against possible unwanted operation)
629   * @param  OB_WRP: specifies the address of the pages to be write protected.
630   *          This parameter can be:
631   *            @arg OB_WRP_Pages0to1..OB_WRP_Pages62to127
632   *            @arg OB_WRP_AllPages
633   * @retval FLASH Status: The returned value can be:
634   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
635   */
FLASH_OB_EnableWRP(uint32_t OB_WRP)636 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
637 {
638   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
639 
640   FLASH_Status status = FLASH_COMPLETE;
641 
642   /* Check the parameters */
643   assert_param(IS_OB_WRP(OB_WRP));
644 
645   OB_WRP = (uint32_t)(~OB_WRP);
646   WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
647   WRP1_Data = (uint16_t)((OB_WRP & OB_WRP0_nWRP0) >> 8);
648   WRP2_Data = (uint16_t)((OB_WRP & OB_WRP1_WRP1) >> 16);
649   WRP3_Data = (uint16_t)((OB_WRP & OB_WRP1_nWRP1) >> 24);
650 
651   /* Wait for last operation to be completed */
652   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
653 
654   if(status == FLASH_COMPLETE)
655   {
656     FLASH->CR |= FLASH_CR_OPTPG;
657 
658     if(WRP0_Data != 0xFF)
659     {
660       OB->WRP0 = WRP0_Data;
661 
662       /* Wait for last operation to be completed */
663       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
664     }
665     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
666     {
667       OB->WRP1 = WRP1_Data;
668 
669       /* Wait for last operation to be completed */
670       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
671     }
672 	if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
673     {
674       OB->WRP2 = WRP2_Data;
675 
676       /* Wait for last operation to be completed */
677       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
678     }
679 	if((status == FLASH_COMPLETE) && (WRP3_Data != 0xFF))
680     {
681       OB->WRP3 = WRP3_Data;
682 
683       /* Wait for last operation to be completed */
684       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
685     }
686 
687     if(status != FLASH_TIMEOUT)
688     {
689       /* if the program operation is completed, disable the OPTPG Bit */
690       FLASH->CR &= ~FLASH_CR_OPTPG;
691     }
692   }
693   /* Return the write protection operation Status */
694   return status;
695 }
696 
697 /**
698   * @brief  Enables or disables the read out protection.
699   * @note   To correctly run this function, the FLASH_OB_Unlock() function must be called before.
700   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
701   *         (recommended to protect the FLASH memory against possible unwanted operation)
702   * @param  FLASH_ReadProtection_Level: specifies the read protection level.
703   *          This parameter can be:
704   *             @arg OB_RDP_Level_0: No protection
705   *             @arg OB_RDP_Level_1: Read protection of the memory
706   *             @arg OB_RDP_Level_2: Chip protection
707   * @note   When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
708   * @retval FLASH Status: The returned value can be:
709   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
710   */
FLASH_OB_RDPConfig(uint8_t OB_RDP)711 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
712 {
713   FLASH_Status status = FLASH_COMPLETE;
714 
715   /* Check the parameters */
716   assert_param(IS_OB_RDP(OB_RDP));
717   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
718 
719   if(status == FLASH_COMPLETE)
720   {
721     FLASH->CR |= FLASH_CR_OPTER;
722     FLASH->CR |= FLASH_CR_STRT;
723 
724     /* Wait for last operation to be completed */
725     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
726 
727     if(status == FLASH_COMPLETE)
728     {
729       /* If the erase operation is completed, disable the OPTER Bit */
730       FLASH->CR &= ~FLASH_CR_OPTER;
731 
732       /* Enable the Option Bytes Programming operation */
733       FLASH->CR |= FLASH_CR_OPTPG;
734 
735       OB->RDP = OB_RDP;
736 
737       /* Wait for last operation to be completed */
738       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
739 
740       if(status != FLASH_TIMEOUT)
741       {
742         /* if the program operation is completed, disable the OPTPG Bit */
743         FLASH->CR &= ~FLASH_CR_OPTPG;
744       }
745     }
746     else
747     {
748       if(status != FLASH_TIMEOUT)
749       {
750         /* Disable the OPTER Bit */
751         FLASH->CR &= ~FLASH_CR_OPTER;
752       }
753     }
754   }
755   /* Return the protection operation Status */
756   return status;
757 }
758 
759 /**
760   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
761   * @note   To correctly run this function, the FLASH_OB_Unlock() function must be called before.
762   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option
763   *         bytes (recommended to protect the FLASH memory against possible unwanted operation)
764   * @param  OB_IWDG: Selects the WDG mode
765   *          This parameter can be one of the following values:
766   *             @arg OB_IWDG_SW: Software WDG selected
767   *             @arg OB_IWDG_HW: Hardware WDG selected
768   * @param  OB_STOP: Reset event when entering STOP mode.
769   *          This parameter can be one of the following values:
770   *             @arg OB_STOP_NoRST: No reset generated when entering in STOP
771   *             @arg OB_STOP_RST: Reset generated when entering in STOP
772   * @param  OB_STDBY: Reset event when entering Standby mode.
773   *          This parameter can be one of the following values:
774   *             @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
775   *             @arg OB_STDBY_RST: Reset generated when entering in STANDBY
776   * @retval FLASH Status: The returned value can be:
777   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
778   */
FLASH_OB_UserConfig(uint8_t OB_IWDG,uint8_t OB_STOP,uint8_t OB_STDBY)779 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
780 {
781   FLASH_Status status = FLASH_COMPLETE;
782 
783   /* Check the parameters */
784   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
785   assert_param(IS_OB_STOP_SOURCE(OB_STOP));
786   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
787 
788   /* Wait for last operation to be completed */
789   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
790 
791   if(status == FLASH_COMPLETE)
792   {
793     /* Enable the Option Bytes Programming operation */
794     FLASH->CR |= FLASH_CR_OPTPG;
795 
796     OB->USER = (uint16_t)((uint16_t)(OB_IWDG | OB_STOP) | (uint16_t)(OB_STDBY | 0xF8));
797 
798     /* Wait for last operation to be completed */
799     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
800 
801     if(status != FLASH_TIMEOUT)
802     {
803       /* If the program operation is completed, disable the OPTPG Bit */
804       FLASH->CR &= ~FLASH_CR_OPTPG;
805     }
806   }
807   /* Return the Option Byte program Status */
808   return status;
809 }
810 
811 /**
812   * @brief  Sets or resets the BOOT1 option bit.
813   * @param  OB_BOOT1: Set or Reset the BOOT1 option bit.
814   *          This parameter can be one of the following values:
815   *             @arg OB_BOOT1_RESET: BOOT1 option bit reset
816   *             @arg OB_BOOT1_SET: BOOT1 option bit set
817   * @retval None
818   */
FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)819 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
820 {
821   FLASH_Status status = FLASH_COMPLETE;
822 
823   /* Check the parameters */
824   assert_param(IS_OB_BOOT1(OB_BOOT1));
825 
826   /* Wait for last operation to be completed */
827   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
828 
829   if(status == FLASH_COMPLETE)
830   {
831     /* Enable the Option Bytes Programming operation */
832     FLASH->CR |= FLASH_CR_OPTPG;
833 
834 	OB->USER = OB_BOOT1|0xEF;
835 
836     /* Wait for last operation to be completed */
837     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
838 
839     if(status != FLASH_TIMEOUT)
840     {
841       /* If the program operation is completed, disable the OPTPG Bit */
842       FLASH->CR &= ~FLASH_CR_OPTPG;
843     }
844   }
845   /* Return the Option Byte program Status */
846   return status;
847 }
848 
849 /**
850   * @brief  Sets or resets the analogue monitoring on VDDA Power source.
851   * @param  OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
852   *          This parameter can be one of the following values:
853   *             @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
854   *             @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
855   * @retval None
856   */
FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)857 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
858 {
859   FLASH_Status status = FLASH_COMPLETE;
860 
861   /* Check the parameters */
862   assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
863 
864   /* Wait for last operation to be completed */
865   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
866 
867   if(status == FLASH_COMPLETE)
868   {
869     /* Enable the Option Bytes Programming operation */
870     FLASH->CR |= FLASH_CR_OPTPG;
871 
872     OB->USER = OB_VDDA_ANALOG | 0xDF;
873 
874     /* Wait for last operation to be completed */
875     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
876 
877     if(status != FLASH_TIMEOUT)
878     {
879       /* if the program operation is completed, disable the OPTPG Bit */
880       FLASH->CR &= ~FLASH_CR_OPTPG;
881     }
882   }
883   /* Return the Option Byte program Status */
884   return status;
885 }
886 
887 /**
888   * @brief  Sets or resets the analogue monitoring on VDD_SD12 Power source.
889   * @param  OB_VDD_SD12: Selects the analog monitoring on VDD_SD12 Power source.
890   *          This parameter can be one of the following values:
891   *            @arg OB_VDD_SD12_ON: Analog monitoring on VDD_SD12 Power source ON
892   *            @arg OB_VDD_SD12_OFF: Analog monitoring on VDD_SD12 Power source OFF
893   * @retval None
894   */
FLASH_OB_VDD_SD12Config(uint8_t OB_VDD_SD12)895 FLASH_Status FLASH_OB_VDD_SD12Config(uint8_t OB_VDD_SD12)
896 {
897   FLASH_Status status = FLASH_COMPLETE;
898 
899   /* Check the parameters */
900   assert_param(IS_OB_VDD_SD12(OB_VDD_SD12));
901 
902   /* Wait for last operation to be completed */
903   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
904 
905   if(status == FLASH_COMPLETE)
906   {
907     /* Enable the Option Bytes Programming operation */
908     FLASH->CR |= FLASH_CR_OPTPG;
909 
910     OB->USER = OB_VDD_SD12 | 0x7F;
911 
912     /* Wait for last operation to be completed */
913     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
914 
915     if(status != FLASH_TIMEOUT)
916     {
917       /* if the program operation is completed, disable the OPTPG Bit */
918       FLASH->CR &= ~FLASH_CR_OPTPG;
919     }
920   }
921   /* Return the Option Byte program Status */
922   return status;
923 }
924 
925 /**
926   * @brief  Sets or resets the SRAM partiy.
927   * @param  OB_SRAM_Parity: SSet or Reset the SRAM partiy enable bit.
928   *          This parameter can be one of the following values:
929   *             @arg OB_SRAM_PARITY_SET: Set SRAM partiy.
930   *             @arg OB_SRAM_PARITY_RESET: Reset SRAM partiy.
931   * @retval None
932   */
FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)933 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
934 {
935   FLASH_Status status = FLASH_COMPLETE;
936 
937   /* Check the parameters */
938   assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
939 
940   /* Wait for last operation to be completed */
941   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
942 
943   if(status == FLASH_COMPLETE)
944   {
945     /* Enable the Option Bytes Programming operation */
946     FLASH->CR |= FLASH_CR_OPTPG;
947 
948     OB->USER = OB_SRAM_Parity | 0xBF;
949 
950     /* Wait for last operation to be completed */
951     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
952 
953     if(status != FLASH_TIMEOUT)
954     {
955       /* if the program operation is completed, disable the OPTPG Bit */
956       FLASH->CR &= ~FLASH_CR_OPTPG;
957     }
958   }
959   /* Return the Option Byte program Status */
960   return status;
961 }
962 
963 /**
964   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY/ BOOT1 / OB_VDDA_ANALOG and
965   *         OB_VDD_SD12.
966   * @note   To correctly run this function, the FLASH_OB_Unlock() function
967   *           must be called before.
968   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
969   *          (recommended to protect the FLASH memory against possible unwanted operation)
970   * @param  OB_USER: Selects all user option bytes
971   *          This parameter is a combination of the following values:
972   *            @arg OB_IWDG_SW: Software WDG selected
973   *            @arg OB_IWDG_HW: Hardware WDG selected
974   *            @arg OB_STOP_NoRST: No reset generated when entering in STOP
975   *            @arg OB_STOP_RST: Reset generated when entering in STOP
976   *            @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
977   *            @arg OB_STDBY_RST: Reset generated when entering in STANDBY
978   *            @arg OB_BOOT1_RESET: BOOT1 Reset
979   *            @arg OB_BOOT1_SET: BOOT1 Set
980   *            @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
981   *            @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
982   *            @arg OB_VDD_SD12_ON: Analog monitoring on VDD_SD12 Power source ON
983   *            @arg OB_VDD_SD12_OFF: Analog monitoring on VDD_SD12 Power source OFF
984   * @retval FLASH Status: The returned value can be:
985   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
986   */
FLASH_OB_WriteUser(uint8_t OB_USER)987 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
988 {
989   FLASH_Status status = FLASH_COMPLETE;
990 
991   /* Wait for last operation to be completed */
992   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
993 
994   if(status == FLASH_COMPLETE)
995   {
996     /* Enable the Option Bytes Programming operation */
997     FLASH->CR |= FLASH_CR_OPTPG;
998 
999     OB->USER = OB_USER | 0x48;
1000 
1001     /* Wait for last operation to be completed */
1002     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
1003 
1004     if(status != FLASH_TIMEOUT)
1005     {
1006       /* if the program operation is completed, disable the OPTPG Bit */
1007       FLASH->CR &= ~FLASH_CR_OPTPG;
1008     }
1009   }
1010   /* Return the Option Byte program Status */
1011   return status;
1012 
1013 }
1014 
1015 /**
1016   * @brief  Returns the FLASH User Option Bytes values.
1017   * @param  None
1018   * @retval The FLASH User Option Bytes .
1019   */
FLASH_OB_GetUser(void)1020 uint8_t FLASH_OB_GetUser(void)
1021 {
1022   /* Return the User Option Byte */
1023   return (uint8_t)(FLASH->OBR >> 8);
1024 }
1025 
1026 /**
1027   * @brief  Returns the FLASH Write Protection Option Bytes value.
1028   * @param  None
1029   * @retval The FLASH Write Protection Option Bytes value
1030   */
FLASH_OB_GetWRP(void)1031 uint32_t FLASH_OB_GetWRP(void)
1032 {
1033   /* Return the FLASH write protection Register value */
1034   return (uint32_t)(FLASH->WRPR);
1035 }
1036 
1037 /**
1038   * @brief  Checks whether the FLASH Read out Protection Status is set or not.
1039   * @param  None
1040   * @retval FLASH ReadOut Protection Status(SET or RESET)
1041   */
FLASH_OB_GetRDP(void)1042 FlagStatus FLASH_OB_GetRDP(void)
1043 {
1044   FlagStatus readstatus = RESET;
1045 
1046   if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
1047   {
1048     readstatus = SET;
1049   }
1050   else
1051   {
1052     readstatus = RESET;
1053   }
1054   return readstatus;
1055 }
1056 
1057 /**
1058   * @}
1059   */
1060 
1061 /** @defgroup FLASH_Group4 Interrupts and flags management functions
1062  *  @brief   Interrupts and flags management functions
1063  *
1064 @verbatim
1065  ===============================================================================
1066              ##### Interrupts and flags management functions #####
1067  ===============================================================================
1068 
1069 @endverbatim
1070   * @{
1071   */
1072 
1073 /**
1074   * @brief  Enables or disables the specified FLASH interrupts.
1075   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or
1076   *         disabled.
1077   *          This parameter can be any combination of the following values:
1078   *             @arg FLASH_IT_EOP: FLASH end of programming Interrupt
1079   *             @arg FLASH_IT_ERR: FLASH Error Interrupt
1080   * @retval None
1081   */
FLASH_ITConfig(uint32_t FLASH_IT,FunctionalState NewState)1082 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
1083 {
1084   /* Check the parameters */
1085   assert_param(IS_FLASH_IT(FLASH_IT));
1086   assert_param(IS_FUNCTIONAL_STATE(NewState));
1087 
1088   if(NewState != DISABLE)
1089   {
1090     /* Enable the interrupt sources */
1091     FLASH->CR |= FLASH_IT;
1092   }
1093   else
1094   {
1095     /* Disable the interrupt sources */
1096     FLASH->CR &= ~(uint32_t)FLASH_IT;
1097   }
1098 }
1099 
1100 /**
1101   * @brief  Checks whether the specified FLASH flag is set or not.
1102   * @param  FLASH_FLAG: specifies the FLASH flag to check.
1103   *          This parameter can be one of the following values:
1104   *             @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
1105   *             @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
1106   *             @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1107   *             @arg FLASH_FLAG_EOP: FLASH End of Programming flag
1108   * @retval The new state of FLASH_FLAG (SET or RESET).
1109   */
FLASH_GetFlagStatus(uint32_t FLASH_FLAG)1110 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
1111 {
1112   FlagStatus bitstatus = RESET;
1113 
1114   /* Check the parameters */
1115   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
1116 
1117   if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
1118   {
1119     bitstatus = SET;
1120   }
1121   else
1122   {
1123     bitstatus = RESET;
1124   }
1125   /* Return the new state of FLASH_FLAG (SET or RESET) */
1126   return bitstatus;
1127 }
1128 
1129 /**
1130   * @brief  Clears the FLASH's pending flags.
1131   * @param  FLASH_FLAG: specifies the FLASH flags to clear.
1132   *          This parameter can be any combination of the following values:
1133   *             @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
1134   *             @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1135   *             @arg FLASH_FLAG_EOP: FLASH End of Programming flag
1136   * @retval None
1137   */
FLASH_ClearFlag(uint32_t FLASH_FLAG)1138 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
1139 {
1140   /* Check the parameters */
1141   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
1142 
1143   /* Clear the flags */
1144   FLASH->SR = FLASH_FLAG;
1145 }
1146 
1147 /**
1148   * @brief  Returns the FLASH Status.
1149   * @param  None
1150   * @retval FLASH Status: The returned value can be:
1151   *         FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
1152   */
FLASH_GetStatus(void)1153 FLASH_Status FLASH_GetStatus(void)
1154 {
1155   FLASH_Status FLASHstatus = FLASH_COMPLETE;
1156 
1157   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
1158   {
1159     FLASHstatus = FLASH_BUSY;
1160   }
1161   else
1162   {
1163     if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
1164     {
1165       FLASHstatus = FLASH_ERROR_WRP;
1166     }
1167     else
1168     {
1169       if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
1170       {
1171         FLASHstatus = FLASH_ERROR_PROGRAM;
1172       }
1173       else
1174       {
1175         FLASHstatus = FLASH_COMPLETE;
1176       }
1177     }
1178   }
1179   /* Return the FLASH Status */
1180   return FLASHstatus;
1181 }
1182 
1183 /**
1184   * @brief  Waits for a FLASH operation to complete or a TIMEOUT to occur.
1185   * @param  Timeout: FLASH programming Timeout
1186   * @retval FLASH Status: The returned value can be: FLASH_BUSY,
1187   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1188   */
FLASH_WaitForLastOperation(uint32_t Timeout)1189 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
1190 {
1191   FLASH_Status status = FLASH_COMPLETE;
1192 
1193   /* Check for the FLASH Status */
1194   status = FLASH_GetStatus();
1195 
1196   /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
1197   while((status == FLASH_BUSY) && (Timeout != 0x00))
1198   {
1199     status = FLASH_GetStatus();
1200     Timeout--;
1201   }
1202 
1203   if(Timeout == 0x00 )
1204   {
1205     status = FLASH_TIMEOUT;
1206   }
1207   /* Return the operation status */
1208   return status;
1209 }
1210 
1211 /**
1212   * @}
1213   */
1214 
1215 /**
1216   * @}
1217   */
1218 
1219 /**
1220   * @}
1221   */
1222 
1223 /**
1224   * @}
1225   */
1226 
1227 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1228