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