]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_flash.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_flash.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_flash.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   FLASH HAL module driver.
8   *          This file provides firmware functions to manage the following 
9   *          functionalities of the internal FLASH memory:
10   *            + FLASH Interface configuration
11   *            + FLASH Memory Programming
12   *            + DATA EEPROM Programming
13   *            + Option Bytes Programming
14   *            + Interrupts and flags management
15   *
16   *  @verbatim
17
18   ==============================================================================
19                         ##### FLASH peripheral features #####
20   ==============================================================================
21            
22   [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses 
23        to the Flash memory. It implements the erase and program Flash memory operations 
24        and the read and write protection mechanisms.
25
26   [..] The Flash memory interface accelerates code execution with a system of instruction prefetch. 
27
28   [..] The FLASH main features are:
29       (+) Flash memory read operations
30       (+) Flash memory program/erase operations
31       (+) Read / write protections
32       (+) Prefetch on I-Code
33       (+) Option Bytes programming
34            
35                      ##### How to use this driver #####
36   ==============================================================================
37   [..] This driver provides functions to configure and program the Flash 
38      memory of all STM32L0xx devices.
39     
40      (#) FLASH Memory Programming functions: this group includes all 
41        needed functions to erase and program the main memory:
42        (++) Lock and Unlock the Flash interface.
43        (++) Erase function: Erase Page.
44        (++) Program functions: Fast Word and Half Page(should be 
45         executed from internal SRAM).
46   
47      (#) DATA EEPROM Programming functions: this group includes all 
48        needed functions to erase and program the DATA EEPROM memory:
49        (++) Lock and Unlock the DATA EEPROM interface.
50        (++) Erase function: Erase Byte, erase HalfWord, erase Word, erase 
51        Double Word (should be executed from internal SRAM).
52        (++) Program functions: Fast Program Byte, Fast Program Half-Word, 
53         FastProgramWord, Program Byte, Program Half-Word, 
54         Program Word and Program Double-Word (should be executed 
55         from internal SRAM).
56   
57      (#) FLASH Option Bytes Programming functions: this group includes 
58        all needed functions to:
59        (++) Lock and Unlock the Flash Option bytes.
60        (++) Set/Reset the write protection.
61        (++) Set the Read protection Level.
62        (++) Set the BOR level.
63        (++) Program the user option Bytes.
64        (++) Launch the Option Bytes loader.
65        (++) Get the Write protection.
66        (++) Get the read protection status.
67        (++) Get the BOR level.
68        (++) Get the user option bytes.
69     
70       (#) Interrupts and flags management functions : 
71            (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
72            (++) Wait for last FLASH operation according to its status
73            (++) Get error flag status by calling HAL_GetErrorCode()          
74
75     (#) FLASH Interface configuration functions: this group includes 
76       the management of following features:
77       (++) Enable/Disable the RUN PowerDown mode.
78       (++) Enable/Disable the SLEEP PowerDown mode.  
79   
80     (#) FLASH Peripheral State methods: this group includes 
81       the management of following features:
82       (++) Wait for the FLASH operation
83       (++) Get the specific FLASH error flag
84     
85   [..] In addition to these function, this driver includes a set of macros allowing
86       to handle the following operations:
87       
88     (+) Set/Get the latency
89        (+) Enable/Disable the prefetch buffer
90        (+) Enable/Disable the preread buffer
91        (+) Enable/Disable the Flash power-down
92        (+) Enable/Disable the FLASH interrupts
93        (+) Monitor the FLASH flags status
94
95  ===============================================================================
96                   ##### Programming operation functions #####
97  ===============================================================================  
98     [..]
99     This subsection provides a set of functions allowing to manage the FLASH 
100     program operations.
101
102    [..] The FLASH Memory Programming functions, includes the following functions:
103     (+) HAL_FLASH_Unlock(void);
104     (+) HAL_FLASH_Lock(void);
105     (+) HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
106     (+) HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
107    
108     [..] Any operation of erase or program should follow these steps:
109     (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and 
110         program memory access.
111     (#) Call the desired function to erase page or program data.
112     (#) Call the HAL_FLASH_Lock() to disable the flash program memory access 
113        (recommended to protect the FLASH memory against possible unwanted operation).
114
115   ==============================================================================
116                 ##### Option Bytes Programming functions ##### 
117   ==============================================================================  
118
119     [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
120     (+) HAL_FLASH_OB_Unlock(void);
121     (+) HAL_FLASH_OB_Lock(void);
122     (+) HAL_FLASH_OB_Launch(void);
123     (+) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
124     (+) HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
125    
126     [..] Any operation of erase or program should follow these steps:
127     (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control 
128         register access.
129     (#) Call the following functions to program the desired option bytes.
130         (++) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);      
131     (#) Once all needed option bytes to be programmed are correctly written, call the
132         HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
133     (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
134         to protect the option Bytes against possible unwanted operations).
135
136     [..] Proprietary code Read Out Protection (PcROP):    
137     (#) The PcROP sector is selected by using the same option bytes as the Write
138         protection. As a result, these 2 options are exclusive each other.
139     (#) To activate PCROP mode for Flash sectors(s), you need to follow the sequence below:
140         (++) Use this function HAL_FLASHEx_AdvOBProgram with PCROPState = OB_PCROP_STATE_ENABLE.
141
142   *  @endverbatim
143   *                      
144  ******************************************************************************
145   * @attention
146   *
147   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
148   *
149   * Redistribution and use in source and binary forms, with or without modification,
150   * are permitted provided that the following conditions are met:
151   *   1. Redistributions of source code must retain the above copyright notice,
152   *      this list of conditions and the following disclaimer.
153   *   2. Redistributions in binary form must reproduce the above copyright notice,
154   *      this list of conditions and the following disclaimer in the documentation
155   *      and/or other materials provided with the distribution.
156   *   3. Neither the name of STMicroelectronics nor the names of its contributors
157   *      may be used to endorse or promote products derived from this software
158   *      without specific prior written permission.
159   *
160   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
161   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
163   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
164   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
165   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
166   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
167   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
168   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
169   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
170   *
171   ******************************************************************************
172   */
173
174 /* Includes ------------------------------------------------------------------*/
175 #include "stm32l0xx_hal.h"
176
177 /** @addtogroup STM32L0xx_HAL_Driver
178   * @{
179   */
180
181 #ifdef HAL_FLASH_MODULE_ENABLED
182
183 /** @addtogroup FLASH FLASH
184   * @brief FLASH driver modules
185   * @{
186   */ 
187
188 /* Private typedef -----------------------------------------------------------*/
189 /* Private define ------------------------------------------------------------*/
190 /* Private macro -------------------------------------------------------------*/
191 /* Private variables ---------------------------------------------------------*/
192 /** @addtogroup FLASH_Private_Variables FLASH Internal Variables
193   * @{
194   */
195
196 /**
197   * @brief  Variable used for Program/Erase sectors under interruption 
198   */
199 FLASH_ProcessTypeDef ProcFlash;
200
201 /**
202   * @}
203   */ 
204 /* Private function prototypes -----------------------------------------------*/
205 static void FLASH_SetErrorCode(void);
206 static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
207  
208 /* functions -----------------------------------------------------------------*/
209
210 /** @addtogroup FLASH_Exported_Functions FLASH Exported functions
211   * @{
212   */ 
213
214 /** @addtogroup FLASH_Exported_Functions_Group1 
215  *  @brief   Programming operation functions 
216  *
217 @verbatim   
218 @endverbatim
219   * @{
220   */
221 /**
222   * @brief  Program word at a specified address
223   * @note   To correctly run this function, the HAL_FLASH_Unlock() function
224   *         must be called before.
225   *         Call the HAL_FLASH_Lock() to disable the flash memory access
226   *         (recommended to protect the FLASH memory against possible unwanted operation).
227   * @param  TypeProgram:  Indicate the way to program at a specified address.
228   *                           This parameter can be a value of @ref FLASH_Type_Program
229   * @param  Address:  specifies the address to be programmed.
230   * @param  Data: specifies the data to be programmed
231   * 
232   * @retval HAL_StatusTypeDef HAL Status
233   */
234 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
235 {
236   HAL_StatusTypeDef status = HAL_ERROR;
237   
238   /* Process Locked */
239   __HAL_LOCK(&ProcFlash);
240
241   /* Check the parameters */
242   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
243
244   /* Wait for last operation to be completed */
245   status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
246   
247   if(status == HAL_OK)
248   {
249     /* Program word (32-bit) at a specified address */
250     FLASH_Program_Word(Address, (uint32_t) Data);
251
252     /* Wait for last operation to be completed */
253     status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
254   }
255
256   /* Process Unlocked */
257   __HAL_UNLOCK(&ProcFlash);
258
259   return status;  
260 }
261
262 /**
263   * @brief   Program word at a specified address  with interrupt enabled.
264   * @param  TypeProgram:  Indicate the way to program at a specified address.
265   *                           This parameter can be a value of @ref FLASH_Type_Program
266   * @param  Address:  specifies the address to be programmed.
267   * @param  Data: specifies the data to be programmed
268   * 
269   * @retval HAL_StatusTypeDef HAL Status
270   */
271 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
272 {
273   HAL_StatusTypeDef status = HAL_OK;
274   
275   /* Process Locked */
276   __HAL_LOCK(&ProcFlash);
277
278   /* Check the parameters */
279   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
280
281   /* Enable End of FLASH Operation interrupt */
282   __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
283   
284   /* Enable Error source interrupt */
285   __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
286   
287   ProcFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
288   ProcFlash.Address = Address;
289   
290   if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
291   {
292     /* Program word (32-bit) at a specified address */
293     FLASH_Program_Word(Address, (uint32_t) Data);
294   }
295   
296   return status;
297 }
298
299 /**
300   * @brief This function handles FLASH interrupt request.
301   * @param  None
302   * @retval None
303   */
304 void HAL_FLASH_IRQHandler(void)
305 {
306   uint32_t temp;
307
308   /* Check FLASH operation error flags */
309   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | \
310                            FLASH_FLAG_RDERR  | FLASH_FLAG_FWWERR | FLASH_FLAG_NOTZEROERR) != RESET)
311   {
312     if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
313     {
314       /* Return the faulty sector */
315       temp = ProcFlash.Page;
316       ProcFlash.Page = 0xFFFFFFFF;
317     }
318     else
319     {
320       /* Return the faulty address */
321       temp = ProcFlash.Address;
322     }
323     
324     /* Save the Error code */
325     FLASH_SetErrorCode();
326
327     /* FLASH error interrupt user callback */
328     HAL_FLASH_OperationErrorCallback(temp);
329
330     /* Stop the procedure ongoing */
331     ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
332   }
333   
334   /* Check FLASH End of Operation flag */
335   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
336   {
337     /* Clear FLASH End of Operation pending bit */
338     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
339
340     /* Process can continue only if no error detected */
341     if(ProcFlash.ProcedureOnGoing != FLASH_PROC_NONE)
342     {
343       if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
344       {
345         /* Nb of sector to erased can be decreased */
346         ProcFlash.NbPagesToErase--;
347   
348         /* Check if there are still sectors to erase */
349         if(ProcFlash.NbPagesToErase != 0)
350         {
351           temp = ProcFlash.Page;
352           /* Indicate user which sector has been erased */
353           HAL_FLASH_EndOfOperationCallback(temp);
354   
355           /* Increment sector number */
356           temp = ProcFlash.Page + FLASH_PAGE_SIZE;
357           ProcFlash.Page = ProcFlash.Page + FLASH_PAGE_SIZE;
358           
359           /* If the erase operation is completed, disable the ERASE Bit */
360           CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
361   
362           FLASH_ErasePage(temp);
363         }
364         else
365         {
366           /* No more sectors to Erase, user callback can be called */
367           /* Reset Sector and stop Erase sectors procedure */
368           ProcFlash.Page = temp = 0xFFFFFFFF;
369           ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
370           /* FLASH EOP interrupt user callback */
371           HAL_FLASH_EndOfOperationCallback(temp);
372         }
373       }
374       else 
375       {
376         if(ProcFlash.ProcedureOnGoing  == FLASH_PROC_PROGRAM)
377         {
378           /* If the program operation is completed, disable the PROG Bit */
379           CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
380           /* Program ended. Return the selected address */
381           /* FLASH EOP interrupt user callback */
382           HAL_FLASH_EndOfOperationCallback(ProcFlash.Address);
383         }
384         ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
385       }
386     }
387   }
388
389   if(ProcFlash.ProcedureOnGoing == FLASH_PROC_NONE)
390   {
391     /* Operation is completed, disable the PG and PER Bits */
392     CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE | FLASH_PECR_PROG);
393
394     /* Disable End of FLASH Operation interrupt */
395     __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
396
397     /* Disable Error source interrupt */
398     __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
399
400     /* Process Unlocked */
401     __HAL_UNLOCK(&ProcFlash);
402   }
403     
404
405
406 /**
407   * @brief  FLASH end of operation interrupt callback
408   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
409   *                 - Pages Erase: Sector which has been erased 
410   *                    (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
411   *                 - Program: Address which was selected for data program
412   * @retval none
413   */
414 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
415 {
416   /* NOTE : This function Should not be modified, when the callback is needed,
417             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
418    */ 
419 }
420
421 /**
422   * @brief  FLASH operation error interrupt callback
423   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
424   *                 - Pagess Erase: Sector number which returned an error
425   *                 - Program: Address which was selected for data program
426   * @retval none
427   */
428 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
429 {
430   /* NOTE : This function Should not be modified, when the callback is needed,
431             the HAL_FLASH_OperationErrorCallback could be implemented in the user file
432    */ 
433 }
434
435 /**
436   * @}
437   */
438
439 /** @addtogroup FLASH_Exported_Functions_Group2
440  *  @brief   management functions 
441  *
442 @verbatim   
443  ===============================================================================
444                       ##### Peripheral Control functions #####
445  ===============================================================================  
446     [..]
447     This subsection provides a set of functions allowing to control the FLASH 
448     memory operations.
449
450 @endverbatim
451   * @{
452   */  
453
454 /**
455   * @brief  Unlock the FLASH control register access
456   * @retval HAL_StatusTypeDef HAL Status
457   */
458 HAL_StatusTypeDef HAL_FLASH_Unlock(void)  
459 {
460   if((FLASH->PECR & FLASH_PECR_PRGLOCK) != RESET)
461   {
462     /* Unlocking FLASH_PECR register access */
463     if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
464      {  
465          FLASH->PEKEYR = FLASH_PEKEY1;
466          FLASH->PEKEYR = FLASH_PEKEY2;
467      }
468  
469     /* Unlocking the program memory access */
470     FLASH->PRGKEYR = FLASH_PRGKEY1;
471     FLASH->PRGKEYR = FLASH_PRGKEY2;  
472   }
473   else
474   {
475     return HAL_ERROR;
476   }
477   
478   return HAL_OK; 
479 }
480
481 /**
482   * @brief  Locks the FLASH control register access
483   * @retval HAL_StatusTypeDef HAL Status
484   */
485 HAL_StatusTypeDef HAL_FLASH_Lock(void)
486 {
487   /* Set the PRGLOCK Bit to lock the program memory access */
488   SET_BIT(FLASH->PECR, FLASH_PECR_PRGLOCK);
489   
490   return HAL_OK;
491 }
492
493 /**
494   * @brief  Unlock the FLASH Option Control Registers access.
495   * @retval HAL_StatusTypeDef HAL Status
496   */
497 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
498 {
499   if((FLASH->PECR & FLASH_PECR_OPTLOCK) != RESET)
500   {
501      /* Unlocking FLASH_PECR register access */
502      if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
503      {  
504          FLASH->PEKEYR = FLASH_PEKEY1;
505          FLASH->PEKEYR = FLASH_PEKEY2;
506      }
507   
508     /* Unlocking the option bytes block access */
509     FLASH->OPTKEYR = FLASH_OPTKEY1;
510     FLASH->OPTKEYR = FLASH_OPTKEY2;
511   }
512   else
513   {
514     return HAL_ERROR;
515   }  
516   
517   return HAL_OK;
518 }
519
520 /**
521   * @brief  Lock the FLASH Option Control Registers access.
522   * @retval HAL_StatusTypeDef HAL Status 
523   */
524 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
525 {
526   /* Set the OPTLOCK Bit to lock the option bytes block access */
527   SET_BIT(FLASH->PECR, FLASH_PECR_OPTLOCK);
528
529   return HAL_OK;  
530 }
531
532 /**
533   * @brief  Launch the option byte loading.
534   * @retval HAL_StatusTypeDef HAL Status
535   */
536 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
537 {
538   /* Clean the error context */
539   ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
540
541   /* Set the OBL_Launch bit to lauch the option byte loading */
542   SET_BIT(FLASH->PECR, FLASH_PECR_OBL_LAUNCH);
543   
544   /* Wait for last operation to be completed */
545   return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
546 ));  
547 }
548
549 /**
550   * @}
551   */
552
553 /** @addtogroup FLASH_Exported_Functions_Group3
554  *  @brief   Peripheral Errors functions 
555  *
556 @verbatim   
557  ===============================================================================
558                 ##### Peripheral Errors functions #####
559  ===============================================================================  
560     [..]
561     This subsection permit to get in run-time Errors of the FLASH peripheral.
562
563 @endverbatim
564   * @{
565   */
566
567 /**
568   * @brief  Get the specific FLASH error flag.
569   * @retval uint32_t: The returned value can be a mixed of :
570   *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
571   *            @arg HAL_FLASH_ERROR_SIZE: FLASH Programming Parallelism error flag  
572   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
573   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
574   *            @arg HAL_FLASH_ERROR_OPTV: FLASH Option valid error flag 
575   *            @arg HAL_FLASH_ERROR_FWWERR: FLASH Write or Errase operation aborted
576   *            @arg HAL_FLASH_ERROR_NOTZERO: FLASH Write operation is done in a not-erased region 
577   */
578 uint32_t HAL_FLASH_GetError(void)
579
580   return ProcFlash.ErrorCode;
581 }
582
583 /**
584   * @}
585   */
586
587 /**
588   * @}
589   */
590
591 /** @addtogroup FLASH_Private_Functions
592   * @{
593   */
594
595 /**
596   * @brief  Wait for a FLASH operation to complete.
597   * @param  Timeout: maximum flash operationtimeout
598   * @retval HAL status
599   */
600 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
601 {
602   /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
603      Even if the FLASH operation fails, the BUSY flag will be reset and an error
604      flag will be set */
605     
606   uint32_t tickstart = HAL_GetTick();   
607      
608   while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) 
609   { 
610     if(Timeout != HAL_MAX_DELAY)
611     {
612       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
613       {
614         return HAL_TIMEOUT;
615       }
616     } 
617   }
618   if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)  != RESET) || \
619      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) || \
620      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)  != RESET) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR)  != RESET) || \
621      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR) != RESET))
622   {
623     /* Save the error code */
624     FLASH_SetErrorCode();
625     return HAL_ERROR;
626    }
627
628   /* There is no error flag set */
629   return HAL_OK;  
630 }
631
632 /**
633   * @brief  Set the specific FLASH error flag.
634   * @param  None
635   * @retval None
636   */
637 static void FLASH_SetErrorCode(void)
638 {  
639   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
640   { 
641     ProcFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
642   }
643   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
644   { 
645     ProcFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
646   }
647   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
648   { 
649     ProcFlash.ErrorCode |= HAL_FLASH_ERROR_SIZE;
650   }
651   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
652   { 
653     ProcFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
654   }
655   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
656   { 
657     ProcFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
658   }
659   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR))
660   { 
661    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_FWWERR;
662   }
663   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR))
664   { 
665    ProcFlash.ErrorCode |= HAL_FLASH_ERROR_NOTZERO;
666   }
667   
668   /* Errors are now stored, clear errors flags */
669   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR |
670                          FLASH_FLAG_OPTVERR | FLASH_FLAG_RDERR | FLASH_FLAG_FWWERR | 
671                          FLASH_FLAG_NOTZEROERR);
672
673
674 /**
675   * @brief  Erases a specified page in program memory.
676   * @note   To correctly run this function, the HAL_FLASH_Unlock() function
677   *         must be called before.
678   *         Call the HAL_FLASH_Lock() to disable the flash memory access 
679   *         (recommended to protect the FLASH memory against possible unwanted operation)
680   * @param  Page_Address: The page address in program memory to be erased.
681   * @note   A Page is erased in the Program memory only if the address to load 
682   *         is the start address of a page (multiple of 128 bytes).
683   * @retval HAL_StatusTypeDef HAL Status
684   */
685 void FLASH_ErasePage(uint32_t Page_Address)
686 {
687   /* Clean the error context */
688   ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
689   
690   /* Set the ERASE bit */
691   SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
692   
693   /* Set PROG bit */
694   SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
695   
696   /* Write 00000000h to the first word of the program page to erase */
697   *(__IO uint32_t *)Page_Address = 0x00000000;
698 }
699
700 /**
701   * @brief  Program word (32-bit) at a specified address.
702   * @param  Address: specifies the address to be programmed.
703   * @param  Data: specifies the data to be programmed.
704   * @retval None
705   */
706 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
707 {
708   /* Check the parameters */
709   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
710   
711   /* Clean the error context */
712   ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
713   
714   *(__IO uint32_t*)Address = Data;
715 }
716
717 /**
718   * @}
719   */
720
721 /**
722   * @}
723   */
724
725 #endif /* HAL_FLASH_MODULE_ENABLED */     
726
727 /**
728   * @}
729   */
730    
731 /**
732   * @}
733   */
734
735 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
736