]> git.gag.com Git - fw/stlink/blob - example/libstm32l_discovery/src/stm32l1xx_flash.c
Merge branch 'master' of https://github.com/texane/stlink
[fw/stlink] / example / libstm32l_discovery / src / stm32l1xx_flash.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_flash.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides all the Flash firmware functions. These functions \r
8   *          can be executed from Internal FLASH or Internal SRAM memories. \r
9   *          The functions that should be called from SRAM are defined inside \r
10   *          the "stm32l1xx_flash_ramfunc.c" file.\r
11   *          This file provides firmware functions to manage the following \r
12   *          functionalities of the FLASH peripheral:\r
13   *            - FLASH Interface configuration\r
14   *            - FLASH Memory Programming\r
15   *            - DATA EEPROM Programming\r
16   *            - Option Bytes Programming\r
17   *            - Interrupts and flags management\r
18   *               \r
19   *  @verbatim\r
20   *  \r
21   *          ===================================================================\r
22   *                                 How to use this driver\r
23   *          ===================================================================\r
24   *                           \r
25   *          This driver provides functions to configure and program the Flash \r
26   *          memory of all STM32L1xx devices\r
27   *          These functions are split in 5 groups\r
28   * \r
29   *           1. FLASH Interface configuration functions: this group includes \r
30   *              the management of following features:\r
31   *                    - Set the latency\r
32   *                    - Enable/Disable the prefetch buffer\r
33   *                    - Enable/Disable the 64 bit Read Access \r
34   *                    - Enable/Disable the RUN PowerDown mode\r
35   *                    - Enable/Disable the SLEEP PowerDown mode  \r
36   *  \r
37   *           2. FLASH Memory Programming functions: this group includes all \r
38   *              needed functions to erase and program the main memory:\r
39   *                    - Lock and Unlock the Flash interface.\r
40   *                    - Erase function: Erase Page.\r
41   *                    - Program functions: Fast Word and Half Page(should be \r
42   *                      executed from internal SRAM).\r
43   *   \r
44   *           3. DATA EEPROM Programming functions: this group includes all \r
45   *              needed functions to erase and program the DATA EEPROM memory:\r
46   *                    - Lock and Unlock the DATA EEPROM interface.\r
47   *                    - Erase function: Erase Word, erase Double Word (should be \r
48   *                      executed from internal SRAM).\r
49   *                    - Program functions: Fast Program Byte, Fast Program Half-Word, \r
50   *                      FastProgramWord, Program Byte, Program Half-Word, \r
51   *                      Program Word and Program Double-Word (should be executed \r
52   *                      from internal SRAM).\r
53   *    \r
54   *           4. FLASH Option Bytes Programming functions: this group includes \r
55   *              all needed functions to:\r
56   *                    - Lock and Unlock the Flash Option bytes.  \r
57   *                    - Set/Reset the write protection\r
58   *                    - Set the Read protection Level\r
59   *                    - Set the BOR level\r
60   *                    - Program the user option Bytes \r
61   *                    - Launch the Option Bytes loader\r
62   *                    - Get the Write protection\r
63   *                    - Get the read protection status\r
64   *                    - Get the BOR level\r
65   *                    - Get the user option bytes        \r
66   *  \r
67   *           5. FLASH Interrupts and flag management functions: this group \r
68   *              includes all needed functions to:\r
69   *                    - Enable/Disable the flash interrupt sources\r
70   *                    - Get flags status\r
71   *                    - Clear flags\r
72   *                    - Get Flash operation status\r
73   *                    - Wait for last flash operation   \r
74   * \r
75   *  @endverbatim\r
76   *                      \r
77   ******************************************************************************\r
78   * @attention\r
79   *\r
80   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
81   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
82   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
83   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
84   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
85   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
86   *\r
87   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
88   ******************************************************************************  \r
89   */ \r
90 \r
91 /* Includes ------------------------------------------------------------------*/\r
92 #include "stm32l1xx_flash.h"\r
93 \r
94 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
95   * @{\r
96   */\r
97 \r
98 /** @defgroup FLASH \r
99   * @brief FLASH driver modules\r
100   * @{\r
101   */ \r
102 \r
103 /* Private typedef -----------------------------------------------------------*/\r
104 /* Private define ------------------------------------------------------------*/\r
105   \r
106 /* FLASH Mask */\r
107 #define RDPRT_MASK                 ((uint32_t)0x00000002)\r
108 #define WRP01_MASK                 ((uint32_t)0x0000FFFF)\r
109 #define WRP23_MASK                 ((uint32_t)0xFFFF0000)\r
110 \r
111 /* Private macro -------------------------------------------------------------*/\r
112 /* Private variables ---------------------------------------------------------*/\r
113 /* Private function prototypes -----------------------------------------------*/\r
114 /* Private functions ---------------------------------------------------------*/\r
115  \r
116 /** @defgroup FLASH_Private_Functions\r
117   * @{\r
118   */ \r
119 \r
120 /** @defgroup FLASH_Group1 FLASH Interface configuration functions\r
121   *  @brief   FLASH Interface configuration functions \r
122  *\r
123 @verbatim   \r
124  ===============================================================================\r
125                        FLASH Interface configuration functions\r
126  ===============================================================================  \r
127 \r
128    FLASH_Interface configuration_Functions, includes the following functions:\r
129    - void FLASH_SetLatency(uint32_t FLASH_Latency):\r
130      To correctly read data from Flash memory, the number of wait states (LATENCY) \r
131      must be correctly programmed according to the frequency of the CPU clock \r
132      (HCLK) and the supply voltage of the device.\r
133   ----------------------------------------------------------------     \r
134  |  Wait states  |                HCLK clock frequency (MHz)      |\r
135  |               |------------------------------------------------|     \r
136  |   (Latency)   |            voltage range       | voltage range | \r
137  |               |            1.65 V - 3.6 V      | 2.0 V - 3.6 V |\r
138  |               |----------------|---------------|---------------| \r
139  |               |  VCORE = 1.2 V | VCORE = 1.5 V | VCORE = 1.8 V |\r
140  |-------------- |----------------|---------------|---------------|             \r
141  |0WS(1CPU cycle)|0 < HCLK <= 2   |0 < HCLK <= 8  |0 < HCLK <= 16 |\r
142  |---------------|----------------|---------------|---------------|  \r
143  |1WS(2CPU cycle)|2 < HCLK <= 4   |8 < HCLK <= 16 |16 < HCLK <= 32| \r
144   ----------------------------------------------------------------   \r
145    \r
146    - void FLASH_PrefetchBufferCmd(FunctionalState NewState);\r
147    - void FLASH_ReadAccess64Cmd(FunctionalState NewState);\r
148    - void FLASH_RUNPowerDownCmd(FunctionalState NewState);\r
149    - void FLASH_SLEEPPowerDownCmd(FunctionalState NewState);\r
150    - void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);\r
151    \r
152   Here below the allowed configuration of Latency, 64Bit access and prefetch buffer\r
153   --------------------------------------------------------------------------------              \r
154  |               |              ACC64 = 0         |              ACC64 = 1        |\r
155  |   Latency     |----------------|---------------|---------------|---------------|   \r
156  |               |   PRFTEN = 0   |   PRFTEN = 1  |   PRFTEN = 0  |   PRFTEN = 1  | \r
157  |---------------|----------------|---------------|---------------|---------------|   \r
158  |0WS(1CPU cycle)|     YES        |     NO        |     YES       |     YES       |\r
159  |---------------|----------------|---------------|---------------|---------------| \r
160  |1WS(2CPU cycle)|     NO         |     NO        |     YES       |     YES       | \r
161   --------------------------------------------------------------------------------   \r
162    All these functions don't need the unlock sequence.\r
163 \r
164 @endverbatim\r
165   * @{\r
166   */\r
167 \r
168 /**\r
169   * @brief  Sets the code latency value.\r
170   * @param  FLASH_Latency: specifies the FLASH Latency value.\r
171   *   This parameter can be one of the following values:\r
172   *     @arg FLASH_Latency_0: FLASH Zero Latency cycle\r
173   *     @arg FLASH_Latency_1: FLASH One Latency cycle\r
174   * @retval None\r
175   */\r
176 void FLASH_SetLatency(uint32_t FLASH_Latency)\r
177 {\r
178    uint32_t tmpreg = 0;\r
179   \r
180   /* Check the parameters */\r
181   assert_param(IS_FLASH_LATENCY(FLASH_Latency));\r
182   \r
183   /* Read the ACR register */\r
184   tmpreg = FLASH->ACR;  \r
185   \r
186   /* Sets the Latency value */\r
187   tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));\r
188   tmpreg |= FLASH_Latency;\r
189   \r
190   /* Write the ACR register */\r
191   FLASH->ACR = tmpreg;\r
192 }\r
193 \r
194 /**\r
195   * @brief  Enables or disables the Prefetch Buffer.\r
196   * @param  NewState: new state of the FLASH prefetch buffer.\r
197   *              This parameter can be: ENABLE or DISABLE. \r
198   * @retval None\r
199   */\r
200 void FLASH_PrefetchBufferCmd(FunctionalState NewState)\r
201 {\r
202   /* Check the parameters */\r
203   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
204    \r
205   if(NewState != DISABLE)\r
206   {\r
207     FLASH->ACR |= FLASH_ACR_PRFTEN;\r
208   }\r
209   else\r
210   {\r
211     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTEN));\r
212   }\r
213 }\r
214 \r
215 /**\r
216   * @brief  Enables or disables read access to flash by 64 bits.\r
217   * @param  NewState: new state of the FLASH read access mode.\r
218   *              This parameter can be: ENABLE or DISABLE.  \r
219   * @note   - If this bit is set, the Read access 64 bit is used.\r
220   *         - If this bit is reset, the Read access 32 bit is used.\r
221   * @note   - This bit cannot be written at the same time as the LATENCY and \r
222   *           PRFTEN bits. \r
223   *         - To reset this bit, the LATENCY should be zero wait state and the \r
224   *           prefetch off.\r
225   * @retval None\r
226   */\r
227 void FLASH_ReadAccess64Cmd(FunctionalState NewState)\r
228 {\r
229   /* Check the parameters */\r
230   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
231   \r
232   if(NewState != DISABLE)\r
233   {\r
234     FLASH->ACR |= FLASH_ACR_ACC64;\r
235   }\r
236   else\r
237   {\r
238     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_ACC64));\r
239   }\r
240 }\r
241 \r
242 /**\r
243   * @brief  Enable or disable the power down mode during Sleep mode.\r
244   * @note   This function is used to power down the FLASH when the system is in SLEEP LP mode.  \r
245   * @param  NewState: new state of the power down mode during sleep mode.\r
246   *   This parameter can be: ENABLE or DISABLE.\r
247   * @retval None\r
248   */\r
249 void FLASH_SLEEPPowerDownCmd(FunctionalState NewState)\r
250 {\r
251   /* Check the parameters */\r
252   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
253   \r
254   if (NewState != DISABLE)\r
255   {\r
256     /* Set the SLEEP_PD bit to put Flash in power down mode during sleep mode */\r
257     FLASH->ACR |= FLASH_ACR_SLEEP_PD;\r
258   }\r
259   else\r
260   {\r
261     /* Clear the SLEEP_PD bit in to put Flash in idle mode during sleep mode */\r
262     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_SLEEP_PD));\r
263   }\r
264 }\r
265 \r
266 /**\r
267   * @}\r
268   */\r
269 \r
270 /** @defgroup FLASH_Group2 FLASH Memory Programming functions\r
271  *  @brief   FLASH Memory Programming functions\r
272  *\r
273 @verbatim   \r
274  ===============================================================================\r
275                       FLASH Memory Programming functions\r
276  ===============================================================================  \r
277 \r
278    The FLASH Memory Programming functions, includes the following functions:\r
279     - void FLASH_Unlock(void);\r
280     - void FLASH_Lock(void);\r
281     - FLASH_Status FLASH_ErasePage(uint32_t Page_Address);\r
282     - FLASH_Status FLASH_FastProgramWord(uint32_t Address, uint32_t Data);\r
283    \r
284    Any operation of erase or program should follow these steps:\r
285 \r
286    1. Call the FLASH_Unlock() function to enable the flash control register and \r
287      program memory access\r
288 \r
289    2. Call the desired function to erase page or program data\r
290 \r
291    3. Call the FLASH_Lock() to disable the flash program memory access \r
292      (recommended to protect the FLASH memory against possible unwanted operation)\r
293 \r
294 @endverbatim\r
295   * @{\r
296   */\r
297 \r
298 /**\r
299   * @brief  Unlocks the FLASH control register and program memory access.\r
300   * @param  None\r
301   * @retval None\r
302   */\r
303 void FLASH_Unlock(void)\r
304 {\r
305   if((FLASH->PECR & FLASH_PECR_PRGLOCK) != RESET)\r
306   {\r
307     /* Unlocking the data memory and FLASH_PECR register access */\r
308     DATA_EEPROM_Unlock();\r
309   \r
310     /* Unlocking the program memory access */\r
311     FLASH->PRGKEYR = FLASH_PRGKEY1;\r
312     FLASH->PRGKEYR = FLASH_PRGKEY2;  \r
313   }\r
314 }\r
315 \r
316 /**\r
317   * @brief  Locks the Program memory access.\r
318   * @param  None\r
319   * @retval None\r
320   */\r
321 void FLASH_Lock(void)\r
322 {\r
323   /* Set the PRGLOCK Bit to lock the program memory access */\r
324   FLASH->PECR |= FLASH_PECR_PRGLOCK;\r
325 }\r
326 \r
327 /**\r
328   * @brief  Erases a specified page in program memory.\r
329   * @note   - To correctly run this function, the FLASH_Unlock() function\r
330   *           must be called before.\r
331   *         - Call the FLASH_Lock() to disable the flash memory access \r
332   *          (recommended to protect the FLASH memory against possible unwanted operation)  \r
333   * @param  Page_Address: The page address in program memory to be erased.\r
334   * @note   A Page is erased in the Program memory only if the address to load \r
335   *         is the start address of a page (multiple of 256 bytes).  \r
336   * @retval FLASH Status: The returned value can be: \r
337   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
338   */\r
339 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)\r
340 {\r
341   FLASH_Status status = FLASH_COMPLETE;\r
342 \r
343   /* Check the parameters */\r
344   assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));\r
345  \r
346   /* Wait for last operation to be completed */\r
347   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
348   \r
349   if(status == FLASH_COMPLETE)\r
350   {\r
351     /* If the previous operation is completed, proceed to erase the page */\r
352 \r
353     /* Set the ERASE bit */\r
354     FLASH->PECR |= FLASH_PECR_ERASE;\r
355 \r
356     /* Set PROG bit */\r
357     FLASH->PECR |= FLASH_PECR_PROG;\r
358   \r
359     /* Write 00000000h to the first word of the program page to erase */\r
360     *(__IO uint32_t *)Page_Address = 0x00000000;\r
361  \r
362     /* Wait for last operation to be completed */\r
363     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
364 \r
365     /* If the erase operation is completed, disable the ERASE and PROG bits */\r
366     FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);\r
367     FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);   \r
368   }     \r
369   /* Return the Erase Status */\r
370   return status;\r
371 }\r
372 \r
373 /**\r
374   * @brief  Programs a word at a specified address in program memory.\r
375   * @note   - To correctly run this function, the FLASH_Unlock() function\r
376   *           must be called before.\r
377   *         - Call the FLASH_Lock() to disable the flash memory access  \r
378   *          (recommended to protect the FLASH memory against possible unwanted operation)  \r
379   * @param  Address: specifies the address to be written.\r
380   * @param  Data: specifies the data to be written.\r
381   * @retval FLASH Status: The returned value can be:  \r
382   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
383   */\r
384 FLASH_Status FLASH_FastProgramWord(uint32_t Address, uint32_t Data)\r
385 {\r
386   FLASH_Status status = FLASH_COMPLETE;\r
387 \r
388   /* Check the parameters */\r
389   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));\r
390   \r
391   /* Wait for last operation to be completed */\r
392   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
393   \r
394   if(status == FLASH_COMPLETE)\r
395   {\r
396     /* If the previous operation is completed, proceed to program the new  word */  \r
397     *(__IO uint32_t *)Address = Data;\r
398     \r
399     /* Wait for last operation to be completed */\r
400     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);       \r
401   }\r
402   /* Return the Write Status */\r
403   return status;\r
404 }\r
405 \r
406 /**\r
407   * @}\r
408   */\r
409   \r
410 /** @defgroup FLASH_Group3 DATA EEPROM Programming functions\r
411  *  @brief   DATA EEPROM Programming functions\r
412  *\r
413 @verbatim   \r
414  ===============================================================================\r
415                        DATA EEPROM Programming functions\r
416  ===============================================================================  \r
417 \r
418    The DATA_EEPROM Programming_Functions, includes the following functions:\r
419    - void DATA_EEPROM_Unlock(void);\r
420    - void DATA_EEPROM_Lock(void);\r
421    - FLASH_Status DATA_EEPROM_EraseWord(uint32_t Address);\r
422    - FLASH_Status DATA_EEPROM_FastProgramByte(uint32_t Address, uint8_t Data);\r
423    - FLASH_Status DATA_EEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);\r
424    - FLASH_Status DATA_EEPROM_FastProgramWord(uint32_t Address, uint32_t Data);\r
425    - FLASH_Status DATA_EEPROM_ProgramByte(uint32_t Address, uint8_t Data);\r
426    - FLASH_Status DATA_EEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);\r
427    - FLASH_Status DATA_EEPROM_ProgramWord(uint32_t Address, uint32_t Data);\r
428    \r
429    Any operation of erase or program should follow these steps:\r
430 \r
431    1. Call the DATA_EEPROM_Unlock() function to enable the data EEPROM access\r
432      and Flash program erase control register access.\r
433 \r
434    2. Call the desired function to erase or program data\r
435 \r
436    3. Call the DATA_EEPROM_Lock() to disable the data EEPROM access\r
437      and Flash program erase control register access(recommended\r
438      to protect the DATA_EEPROM against possible unwanted operation)\r
439 \r
440 @endverbatim\r
441   * @{\r
442   */\r
443 \r
444 /**\r
445   * @brief  Unlocks the data memory and FLASH_PECR register access.\r
446   * @param  None\r
447   * @retval None\r
448   */\r
449 void DATA_EEPROM_Unlock(void)\r
450 {\r
451   if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)\r
452   {  \r
453     /* Unlocking the Data memory and FLASH_PECR register access*/\r
454     FLASH->PEKEYR = FLASH_PEKEY1;\r
455     FLASH->PEKEYR = FLASH_PEKEY2;\r
456   }\r
457 }\r
458 \r
459 /**\r
460   * @brief  Locks the Data memory and FLASH_PECR register access.\r
461   * @param  None\r
462   * @retval None\r
463   */\r
464 void DATA_EEPROM_Lock(void)\r
465 {\r
466   /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */\r
467   FLASH->PECR |= FLASH_PECR_PELOCK;\r
468 }\r
469 \r
470 /**\r
471   * @brief  Enables or disables DATA EEPROM fixed Time programming (2*Tprog).\r
472   * @param  NewState: new state of the DATA EEPROM fixed Time programming mode.\r
473   *              This parameter can be: ENABLE or DISABLE.  \r
474   * @retval None\r
475   */\r
476 void DATA_EEPROM_FixedTimeProgramCmd(FunctionalState NewState)\r
477 {\r
478   /* Check the parameters */\r
479   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
480   \r
481   if(NewState != DISABLE)\r
482   {\r
483     FLASH->PECR |= (uint32_t)FLASH_PECR_FTDW;\r
484   }\r
485   else\r
486   {\r
487     FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));\r
488   }\r
489 }\r
490 \r
491 /**\r
492   * @brief  Erase a word in data memory.\r
493   * @param  Address: specifies the address to be erased\r
494   * @note1  - A data memory word is erased in the data memory only if the address \r
495   *         to load is the start address of a word (multiple of a word).\r
496   * @note2   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
497   *           must be called before.\r
498   *          - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
499   *            and Flash program erase control register access(recommended to protect \r
500   *            the DATA_EEPROM against possible unwanted operation)    \r
501   * @retval FLASH Status: The returned value can be: \r
502   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
503   */\r
504 FLASH_Status DATA_EEPROM_EraseWord(uint32_t Address)\r
505 {\r
506   FLASH_Status status = FLASH_COMPLETE;\r
507   \r
508   /* Check the parameters */\r
509   assert_param(IS_FLASH_DATA_ADDRESS(Address));\r
510   \r
511   /* Wait for last operation to be completed */\r
512   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
513   \r
514   if(status == FLASH_COMPLETE)\r
515   {\r
516     /* Write "00000000h" to valid address in the data memory" */\r
517     *(__IO uint32_t *) Address = 0x00000000;\r
518   }\r
519    \r
520   /* Return the erase status */\r
521   return status;\r
522 }\r
523 \r
524 /**\r
525   * @brief  Write a Byte at a specified address in data memory.\r
526   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
527   *           must be called before.\r
528   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
529   *            and Flash program erase control register access(recommended to protect \r
530   *            the DATA_EEPROM against possible unwanted operation)  \r
531   * @param  Address: specifies the address to be written.\r
532   * @param  Data: specifies the data to be written.\r
533   * @note This function assumes that the is data word is already erased.  \r
534   * @retval FLASH Status: The returned value can be:  \r
535   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
536   */\r
537 FLASH_Status DATA_EEPROM_FastProgramByte(uint32_t Address, uint8_t Data)\r
538 {\r
539   FLASH_Status status = FLASH_COMPLETE;\r
540   uint32_t tmp = 0, tmpaddr = 0;\r
541   \r
542   /* Check the parameters */\r
543   assert_param(IS_FLASH_DATA_ADDRESS(Address)); \r
544 \r
545   /* Wait for last operation to be completed */\r
546   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
547     \r
548   if(status == FLASH_COMPLETE)\r
549   {\r
550     /* Clear the FTDW bit */\r
551     FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));\r
552     \r
553     if(Data != (uint8_t)0x00) \r
554     {\r
555       /* If the previous operation is completed, proceed to write the new Data */        \r
556       *(__IO uint8_t *)Address = Data;\r
557             \r
558       /* Wait for last operation to be completed */\r
559       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);           \r
560     }\r
561     else\r
562     {\r
563       tmpaddr = Address & 0xFFFFFFFC;\r
564       tmp = * (__IO uint32_t *) tmpaddr;\r
565       tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));\r
566       tmp &= ~tmpaddr;\r
567       status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);\r
568       status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);         \r
569     }       \r
570   }\r
571   /* Return the Write Status */\r
572   return status;\r
573 }\r
574 \r
575 /**\r
576   * @brief  Writes a half word at a specified address in data memory.\r
577   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
578   *           must be called before.\r
579   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
580   *            and Flash program erase control register access(recommended to protect \r
581   *            the DATA_EEPROM against possible unwanted operation)    \r
582   * @param  Address: specifies the address to be written.\r
583   * @param  Data: specifies the data to be written.\r
584   * @note This function assumes that the is data word is already erased.  \r
585   * @retval FLASH Status: The returned value can be: \r
586   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or  FLASH_TIMEOUT. \r
587   */\r
588 FLASH_Status DATA_EEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)\r
589 {\r
590   FLASH_Status status = FLASH_COMPLETE;\r
591   uint32_t tmp = 0, tmpaddr = 0;\r
592   \r
593   /* Check the parameters */\r
594   assert_param(IS_FLASH_DATA_ADDRESS(Address));\r
595 \r
596   /* Wait for last operation to be completed */\r
597   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
598     \r
599   if(status == FLASH_COMPLETE)\r
600   {\r
601     /* Clear the FTDW bit */\r
602     FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));\r
603   \r
604     if(Data != (uint16_t)0x0000) \r
605     {\r
606       /* If the previous operation is completed, proceed to write the new data */   \r
607       *(__IO uint16_t *)Address = Data;\r
608   \r
609       /* Wait for last operation to be completed */\r
610       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
611     }\r
612     else\r
613     {\r
614       if((Address & 0x3) != 0x3)\r
615       {        \r
616         tmpaddr = Address & 0xFFFFFFFC;\r
617         tmp = * (__IO uint32_t *) tmpaddr;\r
618         tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));\r
619         tmp &= ~tmpaddr;        \r
620         status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);\r
621         status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);      \r
622       }\r
623       else\r
624       {\r
625         DATA_EEPROM_FastProgramByte(Address, 0x00);\r
626         DATA_EEPROM_FastProgramByte(Address + 1, 0x00);          \r
627       }      \r
628     }      \r
629   } \r
630   /* Return the Write Status */\r
631   return status;\r
632 }\r
633 \r
634 /**\r
635   * @brief  Programs a word at a specified address in data memory.\r
636   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
637   *           must be called before.\r
638   *         - Call the DATA_EEPROM_Lock() to the data EEPROM access\r
639   *            and Flash program erase control register access(recommended to protect \r
640   *            the DATA_EEPROM against possible unwanted operation)    \r
641   * @param  Address: specifies the address to be written.\r
642   * @param  Data: specifies the data to be written.\r
643   * @note This function assumes that the is data word is already erased.    \r
644   * @retval FLASH Status: The returned value can be: \r
645   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
646   */\r
647 FLASH_Status DATA_EEPROM_FastProgramWord(uint32_t Address, uint32_t Data)\r
648 {\r
649   FLASH_Status status = FLASH_COMPLETE;\r
650 \r
651   /* Check the parameters */\r
652   assert_param(IS_FLASH_DATA_ADDRESS(Address));\r
653   \r
654   /* Wait for last operation to be completed */\r
655   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
656   \r
657   if(status == FLASH_COMPLETE)\r
658   {\r
659     /* Clear the FTDW bit */\r
660     FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));\r
661   \r
662     /* If the previous operation is completed, proceed to program the new data */    \r
663     *(__IO uint32_t *)Address = Data;\r
664     \r
665     /* Wait for last operation to be completed */\r
666     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);       \r
667   }\r
668   /* Return the Write Status */\r
669   return status;\r
670 }\r
671 \r
672 /**\r
673   * @brief  Write a Byte at a specified address in data memory without erase.\r
674   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
675   *           must be called before.\r
676   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
677   *            and Flash program erase control register access(recommended to protect \r
678   *            the DATA_EEPROM against possible unwanted operation) \r
679   * @note   The function  DATA_EEPROM_FixedTimeProgramCmd() can be called before \r
680   *         this function to configure the Fixed Time Programming.      \r
681   * @param  Address: specifies the address to be written.\r
682   * @param  Data: specifies the data to be written.\r
683   * @retval FLASH Status: The returned value can be: \r
684   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
685   */\r
686 FLASH_Status DATA_EEPROM_ProgramByte(uint32_t Address, uint8_t Data)\r
687 {\r
688   FLASH_Status status = FLASH_COMPLETE;\r
689   uint32_t tmp = 0, tmpaddr = 0;\r
690   \r
691   /* Check the parameters */\r
692   assert_param(IS_FLASH_DATA_ADDRESS(Address)); \r
693 \r
694   /* Wait for last operation to be completed */\r
695   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
696   \r
697   if(status == FLASH_COMPLETE)\r
698   {\r
699     if(Data != (uint8_t) 0x00)\r
700     {  \r
701       *(__IO uint8_t *)Address = Data;\r
702     \r
703       /* Wait for last operation to be completed */\r
704       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
705 \r
706     }\r
707     else\r
708     {\r
709       tmpaddr = Address & 0xFFFFFFFC;\r
710       tmp = * (__IO uint32_t *) tmpaddr;\r
711       tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));\r
712       tmp &= ~tmpaddr;        \r
713       status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);\r
714       status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);        \r
715     }   \r
716   }\r
717   /* Return the Write Status */\r
718   return status;\r
719 }\r
720 \r
721 /**\r
722   * @brief  Writes a half word at a specified address in data memory without erase.\r
723   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
724   *           must be called before.\r
725   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
726   *            and Flash program erase control register access(recommended to protect \r
727   *            the DATA_EEPROM against possible unwanted operation)\r
728   * @note   The function  DATA_EEPROM_FixedTimeProgramCmd() can be called before \r
729   *         this function to configure the Fixed Time Programming                 \r
730   * @param  Address: specifies the address to be written.\r
731   * @param  Data: specifies the data to be written.\r
732   * @retval FLASH Status: The returned value can be:  \r
733   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
734   */\r
735 FLASH_Status DATA_EEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)\r
736 {\r
737   FLASH_Status status = FLASH_COMPLETE;\r
738   uint32_t tmp = 0, tmpaddr = 0;\r
739   \r
740   /* Check the parameters */\r
741   assert_param(IS_FLASH_DATA_ADDRESS(Address));\r
742 \r
743   /* Wait for last operation to be completed */\r
744   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
745   \r
746   if(status == FLASH_COMPLETE)\r
747   {\r
748     if(Data != (uint16_t)0x0000)\r
749     {\r
750       *(__IO uint16_t *)Address = Data;\r
751    \r
752       /* Wait for last operation to be completed */\r
753       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);     \r
754     }\r
755     else\r
756     {\r
757       if((Address & 0x3) != 0x3)\r
758       {\r
759         tmpaddr = Address & 0xFFFFFFFC;\r
760         tmp = * (__IO uint32_t *) tmpaddr;\r
761         tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));\r
762         tmp &= ~tmpaddr;          \r
763         status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);\r
764         status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);  \r
765       }\r
766       else\r
767       {\r
768         DATA_EEPROM_FastProgramByte(Address, 0x00);\r
769         DATA_EEPROM_FastProgramByte(Address + 1, 0x00);          \r
770       }              \r
771     }    \r
772   } \r
773   /* Return the Write Status */\r
774   return status;\r
775 }\r
776 \r
777 /**\r
778   * @brief  Programs a word at a specified address in data memory without erase.\r
779   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
780   *           must be called before.\r
781   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
782   *            and Flash program erase control register access(recommended to protect \r
783   *            the DATA_EEPROM against possible unwanted operation)  \r
784   * @note   The function  DATA_EEPROM_FixedTimeProgramCmd() can be called before \r
785   *         this function to configure the Fixed Time Programming.               \r
786   * @param  Address: specifies the address to be written.\r
787   * @param  Data: specifies the data to be written.\r
788   * @retval FLASH Status: The returned value can be:  \r
789   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or  FLASH_TIMEOUT. \r
790   */\r
791 FLASH_Status DATA_EEPROM_ProgramWord(uint32_t Address, uint32_t Data)\r
792 {\r
793   FLASH_Status status = FLASH_COMPLETE;\r
794   \r
795   /* Check the parameters */\r
796   assert_param(IS_FLASH_DATA_ADDRESS(Address));\r
797   \r
798   /* Wait for last operation to be completed */\r
799   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
800   \r
801   if(status == FLASH_COMPLETE)\r
802   {\r
803     *(__IO uint32_t *)Address = Data;\r
804 \r
805     /* Wait for last operation to be completed */\r
806     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);        \r
807   }\r
808   /* Return the Write Status */\r
809   return status;\r
810 }\r
811 \r
812 /**\r
813   * @}\r
814   */\r
815 \r
816 /** @defgroup FLASH_Group4 Option Bytes Programming functions\r
817  *  @brief   Option Bytes Programming functions \r
818  *\r
819 @verbatim   \r
820  ===============================================================================\r
821                         Option Bytes Programming functions\r
822  ===============================================================================  \r
823 \r
824    The FLASH_Option Bytes Programming_functions, includes the following functions:\r
825    - void FLASH_OB_Unlock(void);\r
826    - void FLASH_OB_Lock(void);\r
827    - void FLASH_OB_Launch(void);\r
828    - FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);\r
829    - FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);\r
830    - FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);\r
831    - FLASH_Status FLASH_OB_BORConfig(uint8_t OB_BOR);\r
832    - uint8_t FLASH_OB_GetUser(void);\r
833    - uint32_t FLASH_OB_GetWRP(void);\r
834    - FlagStatus FLASH_OB_GetRDP(void);\r
835    - uint8_t FLASH_OB_GetBOR(void);\r
836    \r
837    Any operation of erase or program should follow these steps:\r
838    \r
839    1. Call the FLASH_OB_Unlock() function to enable the Flash option control register access\r
840    \r
841    2. Call one or several functions to program the desired option bytes \r
842       - void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState) => to Enable/Disable \r
843         the desired sector write protection\r
844       - void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level\r
845       - void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY) => to configure \r
846         the user option Bytes: IWDG, STOP and the Standby.\r
847       - void FLASH_OB_BORConfig(uint8_t OB_BOR) => to Set the BOR level \r
848       - FLASH_Status FLASH_ProgramOTP(uint32_t Address, uint32_t Data) => to program the OTP bytes                       \r
849    \r
850    3. Once all needed option bytes to be programmed are correctly written, call the\r
851      FLASH_OB_Launch(void) function to launch the Option Bytes programming process.  \r
852    \r
853    4. Call the FLASH_OB_Lock() to disable the Flash option control register access (recommended\r
854       to protect the option Bytes against possible unwanted operations)\r
855 \r
856 @endverbatim\r
857   * @{\r
858   */\r
859 \r
860 /**\r
861   * @brief  Unlocks the option bytes block access.\r
862   * @param  None\r
863   * @retval None\r
864   */\r
865 void FLASH_OB_Unlock(void)\r
866 {\r
867   if((FLASH->PECR & FLASH_PECR_OPTLOCK) != RESET)\r
868   {\r
869     /* Unlocking the data memory and FLASH_PECR register access */\r
870     DATA_EEPROM_Unlock();\r
871   \r
872     /* Unlocking the option bytes block access */\r
873     FLASH->OPTKEYR = FLASH_OPTKEY1;\r
874     FLASH->OPTKEYR = FLASH_OPTKEY2;\r
875   }\r
876 }\r
877 \r
878 /**\r
879   * @brief  Locks the option bytes block access.\r
880   * @param  None\r
881   * @retval None\r
882   */\r
883 void FLASH_OB_Lock(void)\r
884 {\r
885   /* Set the OPTLOCK Bit to lock the option bytes block access */\r
886   FLASH->PECR |= FLASH_PECR_OPTLOCK;\r
887 }\r
888 \r
889 /**\r
890   * @brief  Launch the option byte loading.\r
891   * @param  None\r
892   * @retval None\r
893   */\r
894 void FLASH_OB_Launch(void)\r
895 {\r
896   /* Set the OBL_Launch bit to lauch the option byte loading */\r
897   FLASH->PECR |= FLASH_PECR_OBL_LAUNCH;\r
898 }\r
899 \r
900 /**\r
901   * @brief  Write protects the desired pages\r
902   * @note   - To correctly run this function, the FLASH_OB_Unlock() function\r
903   *           must be called before.\r
904   *         - Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes \r
905   *          (recommended to protect the FLASH memory against possible unwanted operation)    \r
906   * @param  OB_WRP: specifies the address of the pages to be write protected.\r
907   *   This parameter can be:\r
908   *     @arg  value between OB_WRP_Pages0to15 and OB_WRP_Pages496to511\r
909   *     @arg OB_WRP_AllPages\r
910   * @param  NewState: new state of the specified FLASH Pages Wtite protection.\r
911   *   This parameter can be: ENABLE or DISABLE.\r
912   * @retval FLASH Status: The returned value can be: \r
913   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
914   */\r
915 FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)\r
916 {\r
917   uint32_t WRP01_Data = 0, WRP23_Data = 0;\r
918   \r
919   FLASH_Status status = FLASH_COMPLETE;\r
920   uint32_t tmp1 = 0, tmp2 = 0;\r
921   \r
922   /* Check the parameters */\r
923   assert_param(IS_OB_WRP(OB_WRP));\r
924   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
925      \r
926   /* Wait for last operation to be completed */\r
927   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
928  \r
929   if(status == FLASH_COMPLETE)\r
930   {\r
931     if (NewState != DISABLE)\r
932     {\r
933       WRP01_Data = (uint16_t)(((OB_WRP & WRP01_MASK) | OB->WRP01));\r
934       WRP23_Data = (uint16_t)((((OB_WRP & WRP23_MASK)>>16 | OB->WRP23))); \r
935       tmp1 = (uint32_t)(~(WRP01_Data) << 16)|(WRP01_Data);\r
936       OB->WRP01 = tmp1;\r
937       \r
938       tmp2 = (uint32_t)(~(WRP23_Data) << 16)|(WRP23_Data);\r
939       OB->WRP23 = tmp2;      \r
940     }             \r
941     \r
942     else\r
943     {\r
944       WRP01_Data = (uint16_t)(~OB_WRP & (WRP01_MASK & OB->WRP01));\r
945       WRP23_Data = (uint16_t)((((~OB_WRP & WRP23_MASK)>>16 & OB->WRP23))); \r
946 \r
947       tmp1 = (uint32_t)((~WRP01_Data) << 16)|(WRP01_Data);\r
948       OB->WRP01 = tmp1;\r
949       \r
950       tmp2 = (uint32_t)((~WRP23_Data) << 16)|(WRP23_Data);\r
951       OB->WRP23 = tmp2;\r
952     }\r
953     /* Wait for last operation to be completed */\r
954     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
955   }\r
956 \r
957   /* Return the write protection operation Status */\r
958   return status;      \r
959 }\r
960 \r
961 /**\r
962   * @brief  Enables or disables the read out protection.\r
963   * @note   - To correctly run this function, the FLASH_OB_Unlock() function\r
964   *           must be called before.\r
965   *         - Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes \r
966   *          (recommended to protect the FLASH memory against possible unwanted operation)   \r
967   * @param  FLASH_ReadProtection_Level: specifies the read protection level. \r
968   *   This parameter can be:\r
969   *     @arg OB_RDP_Level_0: No protection\r
970   *     @arg OB_RDP_Level_1: Read protection of the memory                     \r
971   *     @arg OB_RDP_Level_2: Chip protection\r
972   *     @retval FLASH Status: The returned value can be: \r
973   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
974   */\r
975 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)\r
976 {\r
977   FLASH_Status status = FLASH_COMPLETE;\r
978   uint8_t tmp1 = 0;\r
979   uint32_t tmp2 = 0;\r
980   \r
981   /* Check the parameters */\r
982   assert_param(IS_OB_RDP(OB_RDP));\r
983   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
984   \r
985   /* calculate the option byte to write */\r
986   tmp1 = (uint8_t)(~(OB_RDP ));\r
987   tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)OB_RDP));\r
988   \r
989   if(status == FLASH_COMPLETE)\r
990   {         \r
991    /* program read protection level */\r
992     OB->RDP = tmp2;\r
993   }\r
994   \r
995   /* Wait for last operation to be completed */\r
996     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
997      \r
998   /* Return the Read protection operation Status */\r
999   return status;            \r
1000 }\r
1001 \r
1002 /**\r
1003   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.\r
1004   * @note   - To correctly run this function, the FLASH_OB_Unlock() function\r
1005   *           must be called before.\r
1006   *         - Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes \r
1007   *          (recommended to protect the FLASH memory against possible unwanted operation)   \r
1008   * @param  OB_IWDG: Selects the WDG mode\r
1009   *   This parameter can be one of the following values:\r
1010   *     @arg OB_IWDG_SW: Software WDG selected\r
1011   *     @arg OB_IWDG_HW: Hardware WDG selected\r
1012   * @param  OB_STOP: Reset event when entering STOP mode.\r
1013   *   This parameter can be one of the following values:\r
1014   *     @arg OB_STOP_NoRST: No reset generated when entering in STOP\r
1015   *     @arg OB_STOP_RST: Reset generated when entering in STOP\r
1016   * @param  OB_STDBY: Reset event when entering Standby mode.\r
1017   *   This parameter can be one of the following values:\r
1018   *     @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY\r
1019   *     @arg OB_STDBY_RST: Reset generated when entering in STANDBY\r
1020   * @retval FLASH Status: The returned value can be: \r
1021   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1022   */\r
1023 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)\r
1024 {\r
1025   FLASH_Status status = FLASH_COMPLETE; \r
1026   uint32_t tmp = 0, tmp1 = 0;\r
1027 \r
1028   /* Check the parameters */\r
1029   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));\r
1030   assert_param(IS_OB_STOP_SOURCE(OB_STOP));\r
1031   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));\r
1032 \r
1033   /* Get the User Option byte register */\r
1034   tmp1 = (FLASH->OBR & 0x000F0000) >> 16;\r
1035     \r
1036   /* Calculate the user option byte to write */ \r
1037   tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << ((uint32_t)0x10));\r
1038   tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);\r
1039   \r
1040   /* Wait for last operation to be completed */\r
1041   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
1042   \r
1043   if(status == FLASH_COMPLETE)\r
1044   {  \r
1045     /* Write the User Option Byte */              \r
1046     OB->USER = tmp; \r
1047   }\r
1048   \r
1049   /* Wait for last operation to be completed */\r
1050     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
1051        \r
1052   /* Return the Option Byte program Status */\r
1053   return status;\r
1054 }\r
1055 \r
1056 /**\r
1057   * @brief  Programs the FLASH brownout reset threshold level Option Byte.\r
1058   * @note   - To correctly run this function, the FLASH_OB_Unlock() function\r
1059   *           must be called before.\r
1060   *         - Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes \r
1061   *          (recommended to protect the FLASH memory against possible unwanted operation)   \r
1062   * @param  OB_BOR: Selects the brownout reset threshold level\r
1063   *   This parameter can be one of the following values:\r
1064   *     @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD \r
1065   *                      power supply reaches the PDR(Power Down Reset) threshold (1.5V)\r
1066   *     @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply\r
1067   *     @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply\r
1068   *     @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply\r
1069   *     @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply\r
1070   *     @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply\r
1071   * @retval FLASH Status: The returned value can be: \r
1072   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1073   */\r
1074 FLASH_Status FLASH_OB_BORConfig(uint8_t OB_BOR)\r
1075 {\r
1076   FLASH_Status status = FLASH_COMPLETE;\r
1077   uint32_t tmp = 0, tmp1 = 0;\r
1078 \r
1079   /* Check the parameters */\r
1080   assert_param(IS_OB_BOR_LEVEL(OB_BOR));\r
1081 \r
1082   /* Get the User Option byte register */\r
1083   tmp1 = (FLASH->OBR & 0x00700000) >> 16;\r
1084      \r
1085   /* Calculate the option byte to write */\r
1086   tmp = (uint32_t)~(OB_BOR | tmp1)<<16;\r
1087   tmp |= (OB_BOR | tmp1);\r
1088     \r
1089   /* Wait for last operation to be completed */\r
1090   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
1091   \r
1092   if(status == FLASH_COMPLETE)\r
1093   {  \r
1094     /* Write the BOR Option Byte */            \r
1095     OB->USER = tmp; \r
1096   }\r
1097   \r
1098   /* Wait for last operation to be completed */\r
1099     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
1100         \r
1101   /* Return the Option Byte program Status */\r
1102   return status;\r
1103 }\r
1104 \r
1105 /**\r
1106   * @brief  Returns the FLASH User Option Bytes values.\r
1107   * @param  None\r
1108   * @retval The FLASH User Option Bytes .\r
1109   */\r
1110 uint8_t FLASH_OB_GetUser(void)\r
1111 {\r
1112   /* Return the User Option Byte */\r
1113   return (uint8_t)(FLASH->OBR >> 20);\r
1114 }\r
1115 \r
1116 /**\r
1117   * @brief  Returns the FLASH Write Protection Option Bytes value.\r
1118   * @param  None\r
1119   * @retval The FLASH Write Protection Option Bytes value\r
1120   */\r
1121 uint32_t FLASH_OB_GetWRP(void)\r
1122 {\r
1123   /* Return the FLASH write protection Register value */\r
1124   return (uint32_t)(FLASH->WRPR);\r
1125 }\r
1126 \r
1127 /**\r
1128   * @brief  Checks whether the FLASH Read out Protection Status is set or not.\r
1129   * @param  None\r
1130   * @retval FLASH ReadOut Protection Status(SET or RESET)\r
1131   */\r
1132 FlagStatus FLASH_OB_GetRDP(void)\r
1133 {\r
1134   FlagStatus readstatus = RESET;\r
1135   \r
1136   if ((uint8_t)(FLASH->OBR) != (uint8_t)OB_RDP_Level_0)\r
1137   {\r
1138     readstatus = SET;\r
1139   }\r
1140   else\r
1141   {\r
1142     readstatus = RESET;\r
1143   }\r
1144   return readstatus;\r
1145 }\r
1146 \r
1147 /**\r
1148   * @brief  Returns the FLASH BOR level.\r
1149   * @param  None\r
1150   * @retval The FLASH User Option Bytes .\r
1151   */\r
1152 uint8_t FLASH_OB_GetBOR(void)\r
1153 {\r
1154   /* Return the BOR level */\r
1155   return (uint8_t)((FLASH->OBR & (uint32_t)0x000F0000) >> 16);\r
1156 }\r
1157 \r
1158 /**\r
1159   * @}\r
1160   */\r
1161 \r
1162 /** @defgroup FLASH_Group5 Interrupts and flags management functions\r
1163  *  @brief   Interrupts and flags management functions\r
1164  *\r
1165 @verbatim   \r
1166  ===============================================================================\r
1167                   Interrupts and flags management functions\r
1168  ===============================================================================  \r
1169 \r
1170 @endverbatim\r
1171   * @{\r
1172   */\r
1173 \r
1174 /**\r
1175   * @brief  Enables or disables the specified FLASH interrupts.\r
1176   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or \r
1177   *         disabled.\r
1178   *   This parameter can be any combination of the following values:     \r
1179   *     @arg FLASH_IT_EOP: FLASH end of programming Interrupt\r
1180   *     @arg FLASH_IT_ERR: FLASH Error Interrupt  \r
1181   * @retval None \r
1182   */\r
1183 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)\r
1184 {\r
1185   /* Check the parameters */\r
1186   assert_param(IS_FLASH_IT(FLASH_IT)); \r
1187   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1188   \r
1189   if(NewState != DISABLE)\r
1190   {\r
1191     /* Enable the interrupt sources */\r
1192     FLASH->PECR |= FLASH_IT;\r
1193   }\r
1194   else\r
1195   {\r
1196     /* Disable the interrupt sources */\r
1197     FLASH->PECR &= ~(uint32_t)FLASH_IT;\r
1198   }\r
1199 }\r
1200 \r
1201 /**\r
1202   * @brief  Checks whether the specified FLASH flag is set or not.\r
1203   * @param  FLASH_FLAG: specifies the FLASH flag to check.\r
1204   *   This parameter can be one of the following values:\r
1205   *     @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag \r
1206   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag\r
1207   *     @arg FLASH_FLAG_READY: FLASH Ready flag after low power mode\r
1208   *     @arg FLASH_FLAG_ENDHV: FLASH End of high voltage flag\r
1209   *     @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag \r
1210   *     @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag\r
1211   *     @arg FLASH_FLAG_SIZERR: FLASH size error flag    \r
1212   *     @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag         \r
1213   * @retval The new state of FLASH_FLAG (SET or RESET).\r
1214   */\r
1215 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)\r
1216 {\r
1217   FlagStatus bitstatus = RESET;\r
1218 \r
1219   /* Check the parameters */\r
1220   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));\r
1221 \r
1222   if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)\r
1223   {\r
1224     bitstatus = SET;\r
1225   }\r
1226   else\r
1227   {\r
1228     bitstatus = RESET;\r
1229   }\r
1230   /* Return the new state of FLASH_FLAG (SET or RESET) */\r
1231   return bitstatus; \r
1232 }\r
1233 \r
1234 /**\r
1235   * @brief  Clears the FLASH\92s pending flags.\r
1236   * @param  FLASH_FLAG: specifies the FLASH flags to clear.\r
1237   *   This parameter can be any combination of the following values:\r
1238   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag\r
1239   *     @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag \r
1240   *     @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag \r
1241   *     @arg FLASH_FLAG_SIZERR: FLASH size error flag    \r
1242   *     @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag              \r
1243   * @retval None\r
1244   */\r
1245 void FLASH_ClearFlag(uint32_t FLASH_FLAG)\r
1246 {\r
1247   /* Check the parameters */\r
1248   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));\r
1249   \r
1250   /* Clear the flags */\r
1251   FLASH->SR = FLASH_FLAG;\r
1252 }\r
1253 \r
1254 /**\r
1255   * @brief  Returns the FLASH Status.\r
1256   * @param  None\r
1257   * @retval FLASH Status: The returned value can be: \r
1258   *   FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.\r
1259   */\r
1260 FLASH_Status FLASH_GetStatus(void)\r
1261 {\r
1262   FLASH_Status FLASHstatus = FLASH_COMPLETE;\r
1263   \r
1264   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) \r
1265   {\r
1266     FLASHstatus = FLASH_BUSY;\r
1267   }\r
1268   else \r
1269   {  \r
1270     if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)\r
1271     { \r
1272       FLASHstatus = FLASH_ERROR_WRP;\r
1273     }\r
1274     else \r
1275     {\r
1276       if((FLASH->SR & (uint32_t)0xFEF0) != (uint32_t)0x00)\r
1277       {\r
1278         FLASHstatus = FLASH_ERROR_PROGRAM; \r
1279       }\r
1280       else\r
1281       {\r
1282         FLASHstatus = FLASH_COMPLETE;\r
1283       }\r
1284     }\r
1285   }\r
1286   /* Return the FLASH Status */\r
1287   return FLASHstatus;\r
1288 }\r
1289 \r
1290 \r
1291 /**\r
1292   * @brief  Waits for a FLASH operation to complete or a TIMEOUT to occur.\r
1293   * @param  Timeout: FLASH programming Timeout\r
1294   * @retval FLASH Status: The returned value can be: FLASH_BUSY, \r
1295   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
1296   */\r
1297 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)\r
1298\r
1299   FLASH_Status status = FLASH_COMPLETE;\r
1300    \r
1301   /* Check for the FLASH Status */\r
1302   status = FLASH_GetStatus();\r
1303   \r
1304   /* Wait for a FLASH operation to complete or a TIMEOUT to occur */\r
1305   while((status == FLASH_BUSY) && (Timeout != 0x00))\r
1306   {\r
1307     status = FLASH_GetStatus();\r
1308     Timeout--;\r
1309   }\r
1310   \r
1311   if(Timeout == 0x00 )\r
1312   {\r
1313     status = FLASH_TIMEOUT;\r
1314   }\r
1315   /* Return the operation status */\r
1316   return status;\r
1317 }\r
1318 \r
1319 /**\r
1320   * @}\r
1321   */\r
1322 \r
1323 /**\r
1324   * @}\r
1325   */\r
1326    \r
1327   /**\r
1328   * @}\r
1329   */ \r
1330 \r
1331 /**\r
1332   * @}\r
1333   */ \r
1334 \r
1335 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r