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