]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/ST_Code/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash.c
Rename STM32Cube to GCC for STM32L4 Discovery projects as GCC is
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil / ST_Code / Drivers / STM32L4xx_HAL_Driver / Src / stm32l4xx_hal_flash.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l4xx_hal_flash.c\r
4   * @author  MCD Application Team\r
5   * @brief   FLASH HAL module driver.\r
6   *          This file provides firmware functions to manage the following\r
7   *          functionalities of the internal FLASH memory:\r
8   *           + Program operations functions\r
9   *           + Memory Control functions\r
10   *           + Peripheral Errors functions\r
11   *\r
12  @verbatim\r
13   ==============================================================================\r
14                         ##### FLASH peripheral features #####\r
15   ==============================================================================\r
16 \r
17   [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses\r
18        to the Flash memory. It implements the erase and program Flash memory operations\r
19        and the read and write protection mechanisms.\r
20 \r
21   [..] The Flash memory interface accelerates code execution with a system of instruction\r
22        prefetch and cache lines.\r
23 \r
24   [..] The FLASH main features are:\r
25       (+) Flash memory read operations\r
26       (+) Flash memory program/erase operations\r
27       (+) Read / write protections\r
28       (+) Option bytes programming\r
29       (+) Prefetch on I-Code\r
30       (+) 32 cache lines of 4*64 bits on I-Code\r
31       (+) 8 cache lines of 4*64 bits on D-Code\r
32       (+) Error code correction (ECC) : Data in flash are 72-bits word\r
33           (8 bits added per double word)\r
34 \r
35 \r
36                         ##### How to use this driver #####\r
37  ==============================================================================\r
38     [..]\r
39       This driver provides functions and macros to configure and program the FLASH\r
40       memory of all STM32L4xx devices.\r
41 \r
42       (#) Flash Memory IO Programming functions:\r
43            (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and\r
44                 HAL_FLASH_Lock() functions\r
45            (++) Program functions: double word and fast program (full row programming)\r
46            (++) There Two modes of programming :\r
47             (+++) Polling mode using HAL_FLASH_Program() function\r
48             (+++) Interrupt mode using HAL_FLASH_Program_IT() function\r
49 \r
50       (#) Interrupts and flags management functions :\r
51            (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()\r
52            (++) Callback functions are called when the flash operations are finished :\r
53                 HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise\r
54                 HAL_FLASH_OperationErrorCallback()\r
55            (++) Get error flag status by calling HAL_GetError()\r
56 \r
57       (#) Option bytes management functions :\r
58            (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and\r
59                 HAL_FLASH_OB_Lock() functions\r
60            (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.\r
61                 In this case, a reset is generated\r
62 \r
63     [..]\r
64       In addition to these functions, this driver includes a set of macros allowing\r
65       to handle the following operations:\r
66        (+) Set the latency\r
67        (+) Enable/Disable the prefetch buffer\r
68        (+) Enable/Disable the Instruction cache and the Data cache\r
69        (+) Reset the Instruction cache and the Data cache\r
70        (+) Enable/Disable the Flash power-down during low-power run and sleep modes\r
71        (+) Enable/Disable the Flash interrupts\r
72        (+) Monitor the Flash flags status\r
73 \r
74  @endverbatim\r
75   ******************************************************************************\r
76   * @attention\r
77   *\r
78   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.\r
79   * All rights reserved.</center></h2>\r
80   *\r
81   * This software component is licensed by ST under BSD 3-Clause license,\r
82   * the "License"; You may not use this file except in compliance with the\r
83   * License. You may obtain a copy of the License at:\r
84   *                       opensource.org/licenses/BSD-3-Clause\r
85   *\r
86   ******************************************************************************\r
87   */\r
88 \r
89 /* Includes ------------------------------------------------------------------*/\r
90 #include "stm32l4xx_hal.h"\r
91 \r
92 /** @addtogroup STM32L4xx_HAL_Driver\r
93   * @{\r
94   */\r
95 \r
96 /** @defgroup FLASH FLASH\r
97   * @brief FLASH HAL module driver\r
98   * @{\r
99   */\r
100 \r
101 #ifdef HAL_FLASH_MODULE_ENABLED\r
102 \r
103 /* Private typedef -----------------------------------------------------------*/\r
104 /* Private defines -----------------------------------------------------------*/\r
105 #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)\r
106 #define FLASH_NB_DOUBLE_WORDS_IN_ROW  64\r
107 #else\r
108 #define FLASH_NB_DOUBLE_WORDS_IN_ROW  32\r
109 #endif\r
110 /* Private macros ------------------------------------------------------------*/\r
111 /* Private variables ---------------------------------------------------------*/\r
112 /** @defgroup FLASH_Private_Variables FLASH Private Variables\r
113  * @{\r
114  */\r
115 /**\r
116   * @brief  Variable used for Program/Erase sectors under interruption\r
117   */\r
118 FLASH_ProcessTypeDef pFlash = {.Lock = HAL_UNLOCKED, \\r
119                                .ErrorCode = HAL_FLASH_ERROR_NONE, \\r
120                                .ProcedureOnGoing = FLASH_PROC_NONE, \\r
121                                .Address = 0U, \\r
122                                .Bank = FLASH_BANK_1, \\r
123                                .Page = 0U, \\r
124                                .NbPagesToErase = 0U, \\r
125                                .CacheToReactivate = FLASH_CACHE_DISABLED};\r
126 /**\r
127   * @}\r
128   */\r
129 \r
130 /* Private function prototypes -----------------------------------------------*/\r
131 /** @defgroup FLASH_Private_Functions FLASH Private Functions\r
132  * @{\r
133  */\r
134 static void          FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);\r
135 static void          FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);\r
136 /**\r
137   * @}\r
138   */\r
139 \r
140 /* Exported functions --------------------------------------------------------*/\r
141 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions\r
142   * @{\r
143   */\r
144 \r
145 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions\r
146  *  @brief   Programming operation functions\r
147  *\r
148 @verbatim\r
149  ===============================================================================\r
150                   ##### Programming operation functions #####\r
151  ===============================================================================\r
152     [..]\r
153     This subsection provides a set of functions allowing to manage the FLASH\r
154     program operations.\r
155 \r
156 @endverbatim\r
157   * @{\r
158   */\r
159 \r
160 /**\r
161   * @brief  Program double word or fast program of a row at a specified address.\r
162   * @param  TypeProgram:  Indicate the way to program at a specified address.\r
163   *                           This parameter can be a value of @ref FLASH_Type_Program\r
164   * @param  Address:  specifies the address to be programmed.\r
165   * @param  Data: specifies the data to be programmed\r
166   *                This parameter is the data for the double word program and the address where\r
167   *                are stored the data for the row fast program\r
168   *\r
169   * @retval HAL_StatusTypeDef HAL Status\r
170   */\r
171 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)\r
172 {\r
173   HAL_StatusTypeDef status;\r
174   uint32_t prog_bit = 0;\r
175 \r
176   /* Process Locked */\r
177   __HAL_LOCK(&pFlash);\r
178 \r
179   /* Check the parameters */\r
180   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));\r
181 \r
182   /* Wait for last operation to be completed */\r
183   status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);\r
184 \r
185   if(status == HAL_OK)\r
186   {\r
187     pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;\r
188 \r
189     /* Deactivate the data cache if they are activated to avoid data misbehavior */\r
190     if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)\r
191     {\r
192       /* Disable data cache  */\r
193       __HAL_FLASH_DATA_CACHE_DISABLE();\r
194       pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;\r
195     }\r
196     else\r
197     {\r
198       pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;\r
199     }\r
200 \r
201     if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)\r
202     {\r
203       /* Program double-word (64-bit) at a specified address */\r
204       FLASH_Program_DoubleWord(Address, Data);\r
205       prog_bit = FLASH_CR_PG;\r
206     }\r
207     else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))\r
208     {\r
209       /* Fast program a 32 row double-word (64-bit) at a specified address */\r
210       FLASH_Program_Fast(Address, (uint32_t)Data);\r
211 \r
212       /* If it is the last row, the bit will be cleared at the end of the operation */\r
213       if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)\r
214       {\r
215         prog_bit = FLASH_CR_FSTPG;\r
216       }\r
217     }\r
218     else\r
219     {\r
220       /* Nothing to do */\r
221     }\r
222 \r
223     /* Wait for last operation to be completed */\r
224     status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);\r
225 \r
226     /* If the program operation is completed, disable the PG or FSTPG Bit */\r
227     if (prog_bit != 0U)\r
228     {\r
229       CLEAR_BIT(FLASH->CR, prog_bit);\r
230     }\r
231 \r
232     /* Flush the caches to be sure of the data consistency */\r
233     FLASH_FlushCaches();\r
234   }\r
235 \r
236   /* Process Unlocked */\r
237   __HAL_UNLOCK(&pFlash);\r
238 \r
239   return status;\r
240 }\r
241 \r
242 /**\r
243   * @brief  Program double word or fast program of a row at a specified address with interrupt enabled.\r
244   * @param  TypeProgram:  Indicate the way to program at a specified address.\r
245   *                           This parameter can be a value of @ref FLASH_Type_Program\r
246   * @param  Address:  specifies the address to be programmed.\r
247   * @param  Data: specifies the data to be programmed\r
248   *                This parameter is the data for the double word program and the address where\r
249   *                are stored the data for the row fast program\r
250   *\r
251   * @retval HAL Status\r
252   */\r
253 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)\r
254 {\r
255   HAL_StatusTypeDef status = HAL_OK;\r
256 \r
257   /* Check the parameters */\r
258   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));\r
259 \r
260   /* Process Locked */\r
261   __HAL_LOCK(&pFlash);\r
262 \r
263   pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;\r
264 \r
265   /* Deactivate the data cache if they are activated to avoid data misbehavior */\r
266   if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)\r
267   {\r
268     /* Disable data cache  */\r
269     __HAL_FLASH_DATA_CACHE_DISABLE();\r
270     pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;\r
271   }\r
272   else\r
273   {\r
274     pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;\r
275   }\r
276 \r
277   /* Set internal variables used by the IRQ handler */\r
278   if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)\r
279   {\r
280     pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;\r
281   }\r
282   else\r
283   {\r
284     pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;\r
285   }\r
286   pFlash.Address = Address;\r
287 \r
288   /* Enable End of Operation and Error interrupts */\r
289   __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);\r
290 \r
291   if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)\r
292   {\r
293     /* Program double-word (64-bit) at a specified address */\r
294     FLASH_Program_DoubleWord(Address, Data);\r
295   }\r
296   else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))\r
297   {\r
298     /* Fast program a 32 row double-word (64-bit) at a specified address */\r
299     FLASH_Program_Fast(Address, (uint32_t)Data);\r
300   }\r
301   else\r
302   {\r
303     /* Nothing to do */\r
304   }\r
305 \r
306   return status;\r
307 }\r
308 \r
309 /**\r
310   * @brief Handle FLASH interrupt request.\r
311   * @retval None\r
312   */\r
313 void HAL_FLASH_IRQHandler(void)\r
314 {\r
315   uint32_t tmp_page;\r
316   uint32_t error;\r
317   FLASH_ProcedureTypeDef procedure;\r
318 \r
319   /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */\r
320   CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));\r
321 #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \\r
322     defined (STM32L496xx) || defined (STM32L4A6xx) || \\r
323     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)\r
324   CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);\r
325 #endif\r
326 \r
327   /* Disable the FSTPG Bit only if it is the last row programmed */\r
328   if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)\r
329   {\r
330     CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);\r
331   }\r
332 \r
333   /* Check FLASH operation error flags */\r
334   error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);\r
335   error |= (FLASH->ECCR & FLASH_FLAG_ECCC);\r
336 \r
337   if (error !=0U)\r
338   {\r
339     /*Save the error code*/\r
340     pFlash.ErrorCode |= error;\r
341 \r
342     /* Clear error programming flags */\r
343     __HAL_FLASH_CLEAR_FLAG(error);\r
344 \r
345     /* Flush the caches to be sure of the data consistency */\r
346     FLASH_FlushCaches() ;\r
347 \r
348     /* FLASH error interrupt user callback */\r
349     procedure = pFlash.ProcedureOnGoing;\r
350     if(procedure == FLASH_PROC_PAGE_ERASE)\r
351     {\r
352        HAL_FLASH_OperationErrorCallback(pFlash.Page);\r
353     }\r
354     else if(procedure == FLASH_PROC_MASS_ERASE)\r
355     {\r
356         HAL_FLASH_OperationErrorCallback(pFlash.Bank);\r
357     }\r
358     else if((procedure == FLASH_PROC_PROGRAM) ||\r
359             (procedure == FLASH_PROC_PROGRAM_LAST))\r
360     {\r
361        HAL_FLASH_OperationErrorCallback(pFlash.Address);\r
362     }\r
363     else\r
364     {\r
365        HAL_FLASH_OperationErrorCallback(0U);\r
366     }\r
367 \r
368     /*Stop the procedure ongoing*/\r
369     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;\r
370   }\r
371 \r
372   /* Check FLASH End of Operation flag  */\r
373   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != 0U)\r
374   {\r
375     /* Clear FLASH End of Operation pending bit */\r
376     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);\r
377 \r
378     if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)\r
379     {\r
380       /* Nb of pages to erased can be decreased */\r
381       pFlash.NbPagesToErase--;\r
382 \r
383       /* Check if there are still pages to erase*/\r
384       if(pFlash.NbPagesToErase != 0U)\r
385       {\r
386         /* Indicate user which page has been erased*/\r
387         HAL_FLASH_EndOfOperationCallback(pFlash.Page);\r
388 \r
389         /* Increment page number */\r
390         pFlash.Page++;\r
391         tmp_page = pFlash.Page;\r
392         FLASH_PageErase(tmp_page, pFlash.Bank);\r
393       }\r
394       else\r
395       {\r
396         /* No more pages to Erase */\r
397         /* Reset Address and stop Erase pages procedure */\r
398         pFlash.Page = 0xFFFFFFFFU;\r
399         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;\r
400 \r
401         /* Flush the caches to be sure of the data consistency */\r
402         FLASH_FlushCaches() ;\r
403 \r
404         /* FLASH EOP interrupt user callback */\r
405         HAL_FLASH_EndOfOperationCallback(pFlash.Page);\r
406       }\r
407     }\r
408     else\r
409     {\r
410       /* Flush the caches to be sure of the data consistency */\r
411       FLASH_FlushCaches() ;\r
412 \r
413       procedure = pFlash.ProcedureOnGoing;\r
414       if(procedure == FLASH_PROC_MASS_ERASE)\r
415       {\r
416         /* MassErase ended. Return the selected bank */\r
417         /* FLASH EOP interrupt user callback */\r
418         HAL_FLASH_EndOfOperationCallback(pFlash.Bank);\r
419       }\r
420       else if((procedure == FLASH_PROC_PROGRAM) ||\r
421               (procedure == FLASH_PROC_PROGRAM_LAST))\r
422       {\r
423         /* Program ended. Return the selected address */\r
424         /* FLASH EOP interrupt user callback */\r
425         HAL_FLASH_EndOfOperationCallback(pFlash.Address);\r
426       }\r
427       else\r
428       {\r
429         /* Nothing to do */\r
430       }\r
431 \r
432       /*Clear the procedure ongoing*/\r
433       pFlash.ProcedureOnGoing = FLASH_PROC_NONE;\r
434     }\r
435   }\r
436 \r
437   if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)\r
438   {\r
439     /* Disable End of Operation and Error interrupts */\r
440     __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);\r
441 \r
442     /* Process Unlocked */\r
443     __HAL_UNLOCK(&pFlash);\r
444   }\r
445 }\r
446 \r
447 /**\r
448   * @brief  FLASH end of operation interrupt callback.\r
449   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure\r
450   *                  Mass Erase: Bank number which has been requested to erase\r
451   *                  Page Erase: Page which has been erased\r
452   *                    (if 0xFFFFFFFF, it means that all the selected pages have been erased)\r
453   *                  Program: Address which was selected for data program\r
454   * @retval None\r
455   */\r
456 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)\r
457 {\r
458   /* Prevent unused argument(s) compilation warning */\r
459   UNUSED(ReturnValue);\r
460 \r
461   /* NOTE : This function should not be modified, when the callback is needed,\r
462             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file\r
463    */\r
464 }\r
465 \r
466 /**\r
467   * @brief  FLASH operation error interrupt callback.\r
468   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure\r
469   *                 Mass Erase: Bank number which has been requested to erase\r
470   *                 Page Erase: Page number which returned an error\r
471   *                 Program: Address which was selected for data program\r
472   * @retval None\r
473   */\r
474 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)\r
475 {\r
476   /* Prevent unused argument(s) compilation warning */\r
477   UNUSED(ReturnValue);\r
478 \r
479   /* NOTE : This function should not be modified, when the callback is needed,\r
480             the HAL_FLASH_OperationErrorCallback could be implemented in the user file\r
481    */\r
482 }\r
483 \r
484 /**\r
485   * @}\r
486   */\r
487 \r
488 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions\r
489  *  @brief   Management functions\r
490  *\r
491 @verbatim\r
492  ===============================================================================\r
493                       ##### Peripheral Control functions #####\r
494  ===============================================================================\r
495     [..]\r
496     This subsection provides a set of functions allowing to control the FLASH\r
497     memory operations.\r
498 \r
499 @endverbatim\r
500   * @{\r
501   */\r
502 \r
503 /**\r
504   * @brief  Unlock the FLASH control register access.\r
505   * @retval HAL Status\r
506   */\r
507 HAL_StatusTypeDef HAL_FLASH_Unlock(void)\r
508 {\r
509   HAL_StatusTypeDef status = HAL_OK;\r
510 \r
511   if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)\r
512   {\r
513     /* Authorize the FLASH Registers access */\r
514     WRITE_REG(FLASH->KEYR, FLASH_KEY1);\r
515     WRITE_REG(FLASH->KEYR, FLASH_KEY2);\r
516 \r
517     /* Verify Flash is unlocked */\r
518     if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)\r
519     {\r
520       status = HAL_ERROR;\r
521     }\r
522   }\r
523 \r
524   return status;\r
525 }\r
526 \r
527 /**\r
528   * @brief  Lock the FLASH control register access.\r
529   * @retval HAL Status\r
530   */\r
531 HAL_StatusTypeDef HAL_FLASH_Lock(void)\r
532 {\r
533   /* Set the LOCK Bit to lock the FLASH Registers access */\r
534   SET_BIT(FLASH->CR, FLASH_CR_LOCK);\r
535 \r
536   return HAL_OK;\r
537 }\r
538 \r
539 /**\r
540   * @brief  Unlock the FLASH Option Bytes Registers access.\r
541   * @retval HAL Status\r
542   */\r
543 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)\r
544 {\r
545   if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)\r
546   {\r
547     /* Authorizes the Option Byte register programming */\r
548     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);\r
549     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);\r
550   }\r
551   else\r
552   {\r
553     return HAL_ERROR;\r
554   }\r
555 \r
556   return HAL_OK;\r
557 }\r
558 \r
559 /**\r
560   * @brief  Lock the FLASH Option Bytes Registers access.\r
561   * @retval HAL Status\r
562   */\r
563 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)\r
564 {\r
565   /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */\r
566   SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);\r
567 \r
568   return HAL_OK;\r
569 }\r
570 \r
571 /**\r
572   * @brief  Launch the option byte loading.\r
573   * @retval HAL Status\r
574   */\r
575 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)\r
576 {\r
577   /* Set the bit to force the option byte reloading */\r
578   SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);\r
579 \r
580   /* Wait for last operation to be completed */\r
581   return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));\r
582 }\r
583 \r
584 /**\r
585   * @}\r
586   */\r
587 \r
588 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions\r
589  *  @brief   Peripheral Errors functions\r
590  *\r
591 @verbatim\r
592  ===============================================================================\r
593                 ##### Peripheral Errors functions #####\r
594  ===============================================================================\r
595     [..]\r
596     This subsection permits to get in run-time Errors of the FLASH peripheral.\r
597 \r
598 @endverbatim\r
599   * @{\r
600   */\r
601 \r
602 /**\r
603   * @brief  Get the specific FLASH error flag.\r
604   * @retval FLASH_ErrorCode: The returned value can be:\r
605   *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)\r
606   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag\r
607   *            @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag\r
608   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag\r
609   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag\r
610   *            @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag\r
611   *            @arg HAL_FLASH_ERROR_NONE: No error set\r
612   *            @arg HAL_FLASH_ERROR_OP: FLASH Operation error\r
613   *            @arg HAL_FLASH_ERROR_PROG: FLASH Programming error\r
614   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error\r
615   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error\r
616   *            @arg HAL_FLASH_ERROR_SIZ: FLASH Size error\r
617   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error\r
618   *            @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error\r
619   *            @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error\r
620   *            @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error\r
621   *            @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error\r
622   *            @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)\r
623   *            @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected\r
624   */\r
625 uint32_t HAL_FLASH_GetError(void)\r
626 {\r
627    return pFlash.ErrorCode;\r
628 }\r
629 \r
630 /**\r
631   * @}\r
632   */\r
633 \r
634 /**\r
635   * @}\r
636   */\r
637 \r
638 /* Private functions ---------------------------------------------------------*/\r
639 \r
640 /** @addtogroup FLASH_Private_Functions\r
641   * @{\r
642   */\r
643 \r
644 /**\r
645   * @brief  Wait for a FLASH operation to complete.\r
646   * @param  Timeout: maximum flash operation timeout\r
647   * @retval HAL_StatusTypeDef HAL Status\r
648   */\r
649 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)\r
650 {\r
651   /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.\r
652      Even if the FLASH operation fails, the BUSY flag will be reset and an error\r
653      flag will be set */\r
654 \r
655   uint32_t tickstart = HAL_GetTick();\r
656   uint32_t error;\r
657 \r
658   while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))\r
659   {\r
660     if(Timeout != HAL_MAX_DELAY)\r
661     {\r
662       if((HAL_GetTick() - tickstart) >= Timeout)\r
663       {\r
664         return HAL_TIMEOUT;\r
665       }\r
666     }\r
667   }\r
668 \r
669   error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);\r
670   error |= (FLASH->ECCR & FLASH_FLAG_ECCD);\r
671 \r
672   if(error != 0u)\r
673   {\r
674     /*Save the error code*/\r
675     pFlash.ErrorCode |= error;\r
676 \r
677     /* Clear error programming flags */\r
678     __HAL_FLASH_CLEAR_FLAG(error);\r
679 \r
680     return HAL_ERROR;\r
681   }\r
682 \r
683   /* Check FLASH End of Operation flag  */\r
684   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))\r
685   {\r
686     /* Clear FLASH End of Operation pending bit */\r
687     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);\r
688   }\r
689 \r
690   /* If there is an error flag set */\r
691   return HAL_OK;\r
692 }\r
693 \r
694 /**\r
695   * @brief  Program double-word (64-bit) at a specified address.\r
696   * @param  Address: specifies the address to be programmed.\r
697   * @param  Data: specifies the data to be programmed.\r
698   * @retval None\r
699   */\r
700 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)\r
701 {\r
702   /* Check the parameters */\r
703   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));\r
704 \r
705   /* Set PG bit */\r
706   SET_BIT(FLASH->CR, FLASH_CR_PG);\r
707 \r
708   /* Program first word */\r
709   *(__IO uint32_t*)Address = (uint32_t)Data;\r
710 \r
711   /* Barrier to ensure programming is performed in 2 steps, in right order\r
712     (independently of compiler optimization behavior) */\r
713   __ISB();\r
714 \r
715   /* Program second word */\r
716   *(__IO uint32_t*)(Address+4U) = (uint32_t)(Data >> 32);\r
717 }\r
718 \r
719 /**\r
720   * @brief  Fast program a row double-word (64-bit) at a specified address.\r
721   * @param  Address: specifies the address to be programmed.\r
722   * @param  DataAddress: specifies the address where the data are stored.\r
723   * @retval None\r
724   */\r
725 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)\r
726 {\r
727   uint32_t primask_bit;\r
728   uint8_t row_index = (2*FLASH_NB_DOUBLE_WORDS_IN_ROW);\r
729   __IO uint32_t *dest_addr = (__IO uint32_t*)Address;\r
730   __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;\r
731 \r
732   /* Check the parameters */\r
733   assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));\r
734 \r
735   /* Set FSTPG bit */\r
736   SET_BIT(FLASH->CR, FLASH_CR_FSTPG);\r
737 \r
738   /* Disable interrupts to avoid any interruption during the loop */\r
739   primask_bit = __get_PRIMASK();\r
740   __disable_irq();\r
741 \r
742   /* Program the double word of the row */\r
743   do\r
744   {\r
745     *dest_addr = *src_addr;\r
746     dest_addr++;\r
747     src_addr++;\r
748     row_index--;\r
749   } while (row_index != 0U);\r
750 \r
751   /* Re-enable the interrupts */\r
752   __set_PRIMASK(primask_bit);\r
753 }\r
754 \r
755 /**\r
756   * @}\r
757   */\r
758 \r
759 #endif /* HAL_FLASH_MODULE_ENABLED */\r
760 \r
761 /**\r
762   * @}\r
763   */\r
764 \r
765 /**\r
766   * @}\r
767   */\r
768 \r
769 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r