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