]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_flash_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_flash_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_flash_ex.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   Extended FLASH HAL module driver.
8   *    
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the FLASH peripheral:
11   *           + Extended Initialization/de-initialization functions
12   *           + Extended I/O operation functions
13   *           + Extended Peripheral Control functions 
14   *           + Extended Peripheral State functions
15   *         
16   @verbatim
17   ==============================================================================
18                ##### Flash peripheral extended features  #####
19   ==============================================================================
20            
21                       ##### How to use this driver #####
22   ==============================================================================
23   [..] This driver provides functions to configure and program the FLASH memory 
24        of all STM32F3xxx devices. It includes
25        
26         (++) Set/Reset the write protection
27         (++) Program the user Option Bytes
28         (++) Get the Read protection Level
29   
30   @endverbatim
31   ******************************************************************************
32   * @attention
33   *
34   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
35   *
36   * Redistribution and use in source and binary forms, with or without modification,
37   * are permitted provided that the following conditions are met:
38   *   1. Redistributions of source code must retain the above copyright notice,
39   *      this list of conditions and the following disclaimer.
40   *   2. Redistributions in binary form must reproduce the above copyright notice,
41   *      this list of conditions and the following disclaimer in the documentation
42   *      and/or other materials provided with the distribution.
43   *   3. Neither the name of STMicroelectronics nor the names of its contributors
44   *      may be used to endorse or promote products derived from this software
45   *      without specific prior written permission.
46   *
47   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
51   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57   *
58   ******************************************************************************  
59   */
60
61 /* Includes ------------------------------------------------------------------*/
62 #include "stm32f3xx_hal.h"
63
64 /** @addtogroup STM32F3xx_HAL_Driver
65   * @{
66   */
67
68 /** @addtogroup FLASHEx FLASH Extended HAL module driver
69   * @brief FLASH Extended HAL module driver
70   * @{
71   */
72
73 #ifdef HAL_FLASH_MODULE_ENABLED
74
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 /** @defgroup FLASHEx_Private_Defines FLASH Extended Private Define
78  * @{
79  */
80 #define HAL_FLASH_TIMEOUT_VALUE   ((uint32_t)50000)/* 50 s */
81 /**
82   * @}
83   */
84
85 /* Private macro -------------------------------------------------------------*/
86 /* Private variables ---------------------------------------------------------*/
87 /** @defgroup FLASHEx_Private_Variables FLASH Extended Private Variables
88  * @{
89  */
90 /* Variables used for Erase pages under interruption*/
91 extern FLASH_ProcessTypeDef pFlash;
92 /**
93   * @}
94   */
95
96 /* Private function prototypes -----------------------------------------------*/
97 /** @defgroup FLASHEx_Private_Functions FLASH Extended Private Functions
98  * @{
99  */
100 /* Erase operations */
101 static void              FLASH_MassErase(void);
102
103 /* Option bytes control */
104 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage);
105 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage);
106 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
107 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig);
108 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
109 static uint32_t          FLASH_OB_GetWRP(void);
110 static FlagStatus        FLASH_OB_GetRDP(void);
111 static uint8_t           FLASH_OB_GetUser(void);
112 /**
113   * @}
114   */
115
116 /* Exported functions ---------------------------------------------------------*/
117 /** @defgroup FLASHEx_Exported_Functions FLASH Extended Exported Functions
118   * @{
119   */
120   
121 /** @defgroup FLASHEx_Exported_Functions_Group1 Extended Input and Output operation functions
122   * @brief      I/O operation functions
123   *
124 @verbatim   
125  ===============================================================================
126                       ##### IO operation functions #####
127  ===============================================================================  
128
129 @endverbatim
130   * @{
131   */
132 /**
133   * @brief  Perform a mass erase or erase the specified FLASH memory pages
134   * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
135   *         The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
136   * @param[in]  pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
137   *         contains the configuration information for the erasing.
138   *
139   * @param[out]  PageError: pointer to variable  that
140   *         contains the configuration information on faulty page in case of error
141   *         (0xFFFFFFFF means that all the pages have been correctly erased)
142   *
143   * @retval HAL_StatusTypeDef HAL Status
144   */
145 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
146 {
147   HAL_StatusTypeDef status = HAL_ERROR;
148   uint32_t address = 0;
149
150   /* Process Locked */
151   __HAL_LOCK(&pFlash);
152
153   /* Check the parameters */
154   assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
155
156   /* Wait for last operation to be completed */
157   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
158
159   if (status == HAL_OK)
160   {
161     if (pEraseInit->TypeErase == TYPEERASE_MASSERASE)
162     {
163       /*Mass erase to be done*/
164       FLASH_MassErase();
165
166       /* Wait for last operation to be completed */
167       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
168
169       /* Check FLASH End of Operation flag  */
170       if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
171       {
172         /* Clear FLASH End of Operation pending bit */
173         __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
174       }
175
176       /* If the erase operation is completed, disable the MER Bit */
177       CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
178     }
179     else
180     {
181       /* Check the parameters */
182       assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
183       assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
184
185       /*Initialization of PageError variable*/
186       *PageError = 0xFFFFFFFF;
187
188       /* Erase by page by page to be done*/
189       for(address = pEraseInit->PageAddress;
190           address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE);
191           address += FLASH_PAGE_SIZE)
192       {
193         FLASH_PageErase(address);
194
195         /* Wait for last operation to be completed */
196         status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
197
198         /* Check FLASH End of Operation flag  */
199         if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
200         {
201           /* Clear FLASH End of Operation pending bit */
202           __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
203         }
204
205         /* If the erase operation is completed, disable the PER Bit */
206         CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
207
208         if (status != HAL_OK)
209         {
210           /* In case of error, stop erase procedure and return the faulty address */
211           *PageError = address;
212           break;
213         }
214       }
215     }
216   }
217
218   /* Process Unlocked */
219   __HAL_UNLOCK(&pFlash);
220
221   return status;
222 }
223
224 /**
225   * @brief  Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
226   * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
227   *         The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
228   * @param  pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
229   *         contains the configuration information for the erasing.
230   *
231   * @retval HAL_StatusTypeDef HAL Status
232   */
233 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
234 {
235   HAL_StatusTypeDef status = HAL_OK;
236
237   /* Process Locked */
238   __HAL_LOCK(&pFlash);
239
240   /* Check the parameters */
241   assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
242
243   /* Enable End of FLASH Operation and Error source interrupts */
244   __HAL_FLASH_ENABLE_IT((FLASH_IT_EOP | FLASH_IT_ERR));
245
246   if (pEraseInit->TypeErase == TYPEERASE_MASSERASE)
247   {
248     /*Mass erase to be done*/
249     pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
250     FLASH_MassErase();
251   }
252   else
253   {
254     /* Erase by page to be done*/
255
256     /* Check the parameters */
257     assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
258     assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
259
260     pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
261     pFlash.DataRemaining = pEraseInit->NbPages;
262     pFlash.Address = pEraseInit->PageAddress;
263
264     /*Erase 1st page and wait for IT*/
265     FLASH_PageErase(pEraseInit->PageAddress);
266   }
267
268   return status;
269 }
270
271 /**
272   * @}
273   */
274     
275 /** @defgroup FLASHEx_Exported_Functions_Group2 Extended Peripheral Control functions
276   * @brief      Peripheral Control functions
277   *
278 @verbatim   
279  ===============================================================================
280                       ##### Peripheral Control functions #####
281  ===============================================================================  
282     [..]
283     This subsection provides a set of functions allowing to control the FLASH 
284     memory operations.
285
286 @endverbatim
287   * @{
288   */
289 /**
290   * @brief  Erases the FLASH option bytes.
291   * @note   This functions erases all option bytes except the Read protection (RDP).
292   *         The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
293   *         The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
294   *         The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
295   *         (system reset will occur)
296   * @retval HAL status
297   */
298
299 HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
300 {
301   uint8_t rdptmp = OB_RDP_LEVEL_0;
302   HAL_StatusTypeDef status = HAL_ERROR;
303   FLASH_OBProgramInitTypeDef optionsbytes;
304
305   /* Get the actual read protection Option Byte value */
306   HAL_FLASHEx_OBGetConfig(&optionsbytes);
307   if(optionsbytes.RDPLevel != RESET)
308   {
309     rdptmp = OB_RDP_LEVEL_1;
310   }
311
312   /* Wait for last operation to be completed */
313   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
314
315   /* Clear pending flags (if any) */
316   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
317
318   if(status == HAL_OK)
319   {
320     /* If the previous operation is completed, proceed to erase the option bytes */
321     SET_BIT(FLASH->CR, FLASH_CR_OPTER);
322     SET_BIT(FLASH->CR, FLASH_CR_STRT);
323
324     /* Wait for last operation to be completed */
325     status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
326
327     /* If the erase operation is completed, disable the OPTER Bit */
328     CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
329
330     if(status == HAL_OK)
331     {
332       /* Restore the last read protection Option Byte value */
333       optionsbytes.OptionType = OPTIONBYTE_RDP;
334       optionsbytes.RDPLevel = rdptmp;
335       status = HAL_FLASHEx_OBProgram(&optionsbytes);
336     }
337   }
338
339   /* Return the erase status */
340   return status;
341 }
342
343 /**
344   * @brief  Program option bytes
345   * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
346   *         The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
347   *         The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
348   *         (system reset will occur)
349   *
350   * @param  pOBInit: pointer to an FLASH_OBInitStruct structure that
351   *         contains the configuration information for the programming.
352   *
353   * @retval HAL_StatusTypeDef HAL Status
354   */
355 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
356 {
357   HAL_StatusTypeDef status = HAL_ERROR;
358
359   /* Check the parameters */
360   assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
361
362   /* Write protection configuration */
363   if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
364   {
365     assert_param(IS_WRPSTATE(pOBInit->WRPState));
366     if (pOBInit->WRPState == WRPSTATE_ENABLE)
367     {
368       /* Enable of Write protection on the selected page */
369       status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
370     }
371     else
372     {
373       /* Disable of Write protection on the selected page */
374       status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
375     }
376   }
377
378   /* Read protection configuration */
379   if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
380   {
381     status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
382   }
383
384   /* USER configuration */
385   if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
386   {
387     status = FLASH_OB_UserConfig(pOBInit->USERConfig);
388   }
389
390   /* DATA configuration*/
391   if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
392   {
393     status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
394   }
395
396   return status;
397 }
398
399 /**
400   * @brief  Get the Option byte configuration
401   * @param  pOBInit: pointer to an FLASH_OBInitStruct structure that
402   *         contains the configuration information for the programming.
403   *
404   * @retval None
405   */
406 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
407 {
408   pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
409
410   /*Get WRP*/
411   pOBInit->WRPPage = FLASH_OB_GetWRP();
412
413   /*Get RDP Level*/
414   pOBInit->RDPLevel = FLASH_OB_GetRDP();
415
416   /*Get USER*/
417   pOBInit->USERConfig = FLASH_OB_GetUser();
418 }
419
420 /**
421   * @}
422   */
423
424 /**
425   * @}
426   */
427
428 /** @addtogroup FLASHEx_Private_Functions FLASH Extended Private Functions
429  * @{
430  */
431
432 /**
433   * @brief  Mass erase of FLASH memory
434   * 
435   * @retval None
436   */
437 static void FLASH_MassErase(void)
438 {
439   /* Clear pending flags (if any) */  
440   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); 
441   
442   /* Proceed to erase all sectors */
443   SET_BIT(FLASH->CR, FLASH_CR_MER);
444   SET_BIT(FLASH->CR, FLASH_CR_STRT);
445 }
446
447 /**
448   * @}
449   */
450
451 /**
452   * @brief  Enable the write protection of the desired pages
453   * @note   When the memory read protection level is selected (RDP level = 1), 
454   *         it is not possible to program or erase the flash page i if CortexM4  
455   *         debug features are connected or boot code is executed in RAM, even if nWRPi = 1 
456   * 
457   * @param  WriteProtectPage: specifies the page(s) to be write protected.
458   *         The value of this parameter depend on device used within the same series 
459   * @retval HAL status 
460   */
461 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
462 {
463   HAL_StatusTypeDef status = HAL_OK;
464   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
465 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
466     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
467     defined(STM32F373xC) || defined(STM32F378xx)
468   uint16_t WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
469 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
470        /* STM32F302xC || STM32F303xC || STM32F358xx || */
471        /* STM32F373xC || STM32F378xx                   */
472   
473   /* Check the parameters */
474   assert_param(IS_OB_WRP(WriteProtectPage));
475     
476   WriteProtectPage = (uint32_t)(~WriteProtectPage);
477   WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
478   WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
479 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
480     defined(STM32F373xC) || defined(STM32F378xx)
481   WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
482   WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24); 
483 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
484        /* STM32F373xC || STM32F378xx                   */
485
486 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
487   WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
488   WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24); 
489 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
490
491   /* Wait for last operation to be completed */
492   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
493
494   /* Clear pending flags (if any) */  
495   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); 
496
497   if(status == HAL_OK)
498   { 
499     SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
500
501     if(WRP0_Data != 0xFF)
502     {
503       OB->WRP0 &= WRP0_Data;
504       
505       /* Wait for last operation to be completed */
506       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
507     }
508
509     if((status == HAL_OK) && (WRP1_Data != 0xFF))
510     {
511       OB->WRP1 &= WRP1_Data;
512       
513       /* Wait for last operation to be completed */
514       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
515     }
516           
517 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
518     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
519     defined(STM32F373xC) || defined(STM32F378xx)
520     if((status == HAL_OK) && (WRP2_Data != 0xFF))
521     {
522       OB->WRP2 &= WRP2_Data;
523       
524       /* Wait for last operation to be completed */
525       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
526     }
527
528     if((status == HAL_OK) && (WRP3_Data != 0xFF))
529     {
530       OB->WRP3 &= WRP3_Data;
531       
532       /* Wait for last operation to be completed */
533       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
534     }
535 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
536        /* STM32F302xC || STM32F303xC || STM32F358xx || */
537        /* STM32F373xC || STM32F378xx                   */
538
539     /* if the program operation is completed, disable the OPTPG Bit */
540     CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
541   }
542   
543   return status;
544
545 }
546
547 /**
548   * @brief  Disable the write protection of the desired pages
549   * @note   When the memory read protection level is selected (RDP level = 1), 
550   *         it is not possible to program or erase the flash page i if CortexM4  
551   *         debug features are connected or boot code is executed in RAM, even if nWRPi = 1 
552   * 
553   * @param  WriteProtectPage: specifies the page(s) to be write unprotected.
554   *         The value of this parameter depend on device used within the same series 
555   * @retval HAL status 
556   */
557 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
558 {
559   HAL_StatusTypeDef status = HAL_OK;
560   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
561 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
562     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
563     defined(STM32F373xC) || defined(STM32F378xx)
564   uint16_t WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
565 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
566        /* STM32F302xC || STM32F303xC || STM32F358xx || */
567        /* STM32F373xC || STM32F378xx                   */
568   
569   /* Check the parameters */
570   assert_param(IS_OB_WRP(WriteProtectPage));
571
572   WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
573   WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
574 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
575     defined(STM32F373xC) || defined(STM32F378xx)
576   WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
577   WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24); 
578 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
579        /* STM32F373xC || STM32F378xx                   */
580
581 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
582   WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
583   WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24); 
584 #endif /* STM32F303xE || STM32F303xE || STM32F398xx */
585     
586   /* Wait for last operation to be completed */
587   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
588
589   /* Clear pending flags (if any) */  
590   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); 
591
592   if(status == HAL_OK)
593   { 
594     SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
595
596     if(WRP0_Data != 0xFF)
597     {
598       OB->WRP0 |= WRP0_Data;
599       
600       /* Wait for last operation to be completed */
601       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
602     }
603
604     if((status == HAL_OK) && (WRP1_Data != 0xFF))
605     {
606       OB->WRP1 |= WRP1_Data;
607       
608       /* Wait for last operation to be completed */
609       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
610     }
611           
612 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
613     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
614     defined(STM32F373xC) || defined(STM32F378xx)
615     if((status == HAL_OK) && (WRP2_Data != 0xFF))
616     {
617       OB->WRP2 |= WRP2_Data;
618       
619       /* Wait for last operation to be completed */
620       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
621     }
622
623     if((status == HAL_OK) && (WRP3_Data != 0xFF))
624     {
625       OB->WRP3 |= WRP3_Data;
626       
627       /* Wait for last operation to be completed */
628       status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
629     }
630 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
631        /* STM32F302xC || STM32F303xC || STM32F358xx || */
632        /* STM32F373xC || STM32F378xx                   */
633
634     /* if the program operation is completed, disable the OPTPG Bit */
635     CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
636   }
637   
638   return status;
639 }
640
641 /**
642   * @brief  Set the read protection level.
643   * @param  ReadProtectLevel: specifies the read protection level.
644   *         This parameter can be one of the following values:
645   *            @arg OB_RDP_LEVEL_0: No protection
646   *            @arg OB_RDP_LEVEL_1: Read protection of the memory
647   *            @arg OB_RDP_LEVEL_2: Full chip protection
648   *   
649   * @note   Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
650   *    
651   * @retval HAL status
652   */
653 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
654 {
655   HAL_StatusTypeDef status = HAL_OK;
656   
657   /* Check the parameters */
658   assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
659     
660   /* Wait for last operation to be completed */
661   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
662
663   /* Clear pending flags (if any) */  
664   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); 
665
666   if(status == HAL_OK)
667   { 
668     /* Enable the Option Bytes Programming operation */
669     SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
670
671     WRITE_REG(OB->RDP, ReadProtectLevel);
672
673     /* Wait for last operation to be completed */
674     status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE); 
675
676       /* if the program operation is completed, disable the OPTPG Bit */
677       CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
678     }
679   
680   return status;
681 }
682
683 /**
684   * @brief  Program the FLASH User Option Byte.    
685   * @note   Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
686   * @param  UserConfig: The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
687   *         VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6). 
688   *         And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 . 
689   * @retval HAL status
690   */
691 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
692 {
693   HAL_StatusTypeDef status = HAL_OK;
694
695   /* Check the parameters */
696   assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
697   assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
698   assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
699   assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
700   assert_param(IS_OB_VDDA_ANALOG((UserConfig&OB_VDDA_ANALOG_ON)));
701   assert_param(IS_OB_SRAM_PARITY((UserConfig&OB_SRAM_PARITY_RESET)));
702 #if defined(STM32F373xC) || defined(STM32F378xx)
703   assert_param(IS_OB_SDACD_VDD_MONITOR((UserConfig&OB_SDACD_VDD_MONITOR_SET)));
704 #endif /* STM32F373xC || STM32F378xx */
705
706   /* Wait for last operation to be completed */
707   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
708   
709   /* Clear pending flags (if any) */  
710   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); 
711   
712   if(status == HAL_OK)
713   {     
714     /* Enable the Option Bytes Programming operation */
715     SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 
716            
717 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
718     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
719     defined(STM32F303x8) || defined(STM32F334x8)                         || \
720     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
721     OB->USER = (UserConfig | 0x88);
722 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
723        /* STM32F302xC || STM32F303xC || STM32F358xx || */
724        /* STM32F303x8 || STM32F334x8                || */
725        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
726
727 #if defined(STM32F373xC) || defined(STM32F378xx)
728     OB->USER = (UserConfig | 0x08);
729 #endif /* STM32F373xC || STM32F378xx */
730   
731     /* Wait for last operation to be completed */
732     status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
733
734     /* if the program operation is completed, disable the OPTPG Bit */
735     CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
736   }
737   
738   return status; 
739 }
740
741 /**
742   * @brief  Programs a half word at a specified Option Byte Data address.
743   * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
744   *         The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
745   *         The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes 
746   *         (system reset will occur)
747   *         Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
748   * @param  Address: specifies the address to be programmed.
749   *         This parameter can be 0x1FFFF804 or 0x1FFFF806. 
750   * @param  Data: specifies the data to be programmed.
751   * @retval HAL status
752   */
753 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
754 {
755   HAL_StatusTypeDef status = HAL_ERROR;
756
757   /* Check the parameters */
758   assert_param(IS_OB_DATA_ADDRESS(Address));
759
760   /* Wait for last operation to be completed */
761   status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
762
763   /* Clear pending flags (if any) */  
764   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); 
765
766   if(status == HAL_OK)
767   {
768     /* Enables the Option Bytes Programming operation */
769     SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 
770     *(__IO uint16_t*)Address = Data;
771     
772     /* Wait for last operation to be completed */
773     status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
774     
775       /* If the program operation is completed, disable the OPTPG Bit */
776       CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
777     }
778   /* Return the Option Byte Data Program Status */
779   return status;
780 }
781
782 /**
783   * @brief  Return the FLASH Write Protection Option Bytes value.
784   * @retval The FLASH Write Protection Option Bytes value
785   */
786 static uint32_t FLASH_OB_GetWRP(void)
787 {
788   /* Return the FLASH write protection Register value */
789   return (uint32_t)(READ_REG(FLASH->WRPR));
790 }
791
792 /**
793   * @brief  Returns the FLASH Read Protection level.
794   * @retval FLASH ReadOut Protection Status:
795   *           - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
796   *           - RESET, when OB_RDP_Level_0 is set
797   */
798 static FlagStatus FLASH_OB_GetRDP(void)
799 {
800   FlagStatus readstatus = RESET;
801
802 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
803     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
804     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
805     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
806   if ((uint8_t)READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT) != RESET)
807 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
808        /* STM32F302xC || STM32F303xC || STM32F358xx || */
809        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
810        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
811     
812 #if defined(STM32F373xC) || defined(STM32F378xx)
813   if ((uint8_t)READ_BIT(FLASH->OBR, (FLASH_OBR_LEVEL1_PROT | FLASH_OBR_LEVEL2_PROT)) != RESET)
814 #endif /* STM32F373xC || STM32F378xx */
815   {
816     readstatus = SET;
817   }
818   else
819   {
820     readstatus = RESET;
821   }
822   return readstatus;
823 }
824
825 /**
826   * @brief  Return the FLASH User Option Byte value.
827   * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
828   *         VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
829   *         And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 . 
830   */
831 static uint8_t FLASH_OB_GetUser(void)
832 {
833   /* Return the User Option Byte */
834   return (uint8_t)(READ_REG(FLASH->OBR) >> 8);
835 }
836
837 /**
838   * @}
839   */
840
841 #endif /* HAL_FLASH_MODULE_ENABLED */
842
843   
844 /**
845   * @}
846   */
847
848 /**
849   * @}
850   */
851
852 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/