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