1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_flash_ramfunc.c
4   * @author  MCD Application Team
5   * @brief   FLASH RAMFUNC driver.
6   *          This file provides a Flash firmware functions which should be
7   *          executed from internal SRAM
8   *
9   *  @verbatim
10 
11     *** ARM Compiler ***
12     --------------------
13     [..] RAM functions are defined using the toolchain options.
14          Functions that are be executed in RAM should reside in a separate
15          source module. Using the 'Options for File' dialog you can simply change
16          the 'Code / Const' area of a module to a memory space in physical RAM.
17          Available memory areas are declared in the 'Target' tab of the
18          Options for Target' dialog.
19 
20     *** ICCARM Compiler ***
21     -----------------------
22     [..] RAM functions are defined using a specific toolchain keyword "__ramfunc".
23 
24     *** GNU Compiler ***
25     --------------------
26     [..] RAM functions are defined using a specific toolchain attribute
27          "__attribute__((section(".RamFunc")))".
28 
29 @endverbatim
30   ******************************************************************************
31   * @attention
32   *
33   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
34   * All rights reserved.</center></h2>
35   *
36   * This software component is licensed by ST under BSD 3-Clause license,
37   * the "License"; You may not use this file except in compliance with the
38   * License. You may obtain a copy of the License at:
39   *                        opensource.org/licenses/BSD-3-Clause
40   *
41   ******************************************************************************
42   */
43 
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32l1xx_hal.h"
46 
47 /** @addtogroup STM32L1xx_HAL_Driver
48   * @{
49   */
50 
51 #ifdef HAL_FLASH_MODULE_ENABLED
52 
53 /** @addtogroup FLASH
54   * @{
55   */
56 /** @addtogroup FLASH_Private_Variables
57  * @{
58  */
59 extern FLASH_ProcessTypeDef pFlash;
60 /**
61   * @}
62   */
63 
64 /**
65   * @}
66   */
67 
68 /** @defgroup FLASH_RAMFUNC FLASH_RAMFUNC
69   * @brief FLASH functions executed from RAM
70   * @{
71   */
72 
73 
74 /* Private typedef -----------------------------------------------------------*/
75 /* Private define ------------------------------------------------------------*/
76 /* Private macro -------------------------------------------------------------*/
77 /* Private variables ---------------------------------------------------------*/
78 /* Private function prototypes -----------------------------------------------*/
79 /** @defgroup FLASH_RAMFUNC_Private_Functions FLASH RAM Private Functions
80  * @{
81  */
82 
83 static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout);
84 static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void);
85 
86 /**
87   * @}
88   */
89 
90 /* Private functions ---------------------------------------------------------*/
91 
92 /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH RAM Exported Functions
93  *
94 @verbatim
95  ===============================================================================
96                       ##### ramfunc functions #####
97  ===============================================================================
98     [..]
99     This subsection provides a set of functions that should be executed from RAM
100     transfers.
101 
102 @endverbatim
103   * @{
104   */
105 
106 /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions
107   * @{
108   */
109 
110 /**
111   * @brief  Enable  the power down mode during RUN mode.
112   * @note  This function can be used only when the user code is running from Internal SRAM.
113   * @retval HAL status
114   */
HAL_FLASHEx_EnableRunPowerDown(void)115 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EnableRunPowerDown(void)
116 {
117   /* Enable the Power Down in Run mode*/
118   __HAL_FLASH_POWER_DOWN_ENABLE();
119 
120   return HAL_OK;
121 }
122 
123 /**
124   * @brief  Disable the power down mode during RUN mode.
125   * @note  This function can be used only when the user code is running from Internal SRAM.
126   * @retval HAL status
127   */
HAL_FLASHEx_DisableRunPowerDown(void)128 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DisableRunPowerDown(void)
129 {
130   /* Disable the Power Down in Run mode*/
131   __HAL_FLASH_POWER_DOWN_DISABLE();
132 
133   return HAL_OK;
134 }
135 
136 /**
137   * @}
138   */
139 
140 /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group2 Programming and erasing operation functions
141  *
142 @verbatim
143 @endverbatim
144   * @{
145   */
146 
147 #if defined(FLASH_PECR_PARALLBANK)
148 /**
149   * @brief  Erases a specified 2 pages in program memory in parallel.
150   * @note   This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5  devices.
151   *         To correctly run this function, the @ref HAL_FLASH_Unlock() function
152   *         must be called before.
153   *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
154   *        (recommended to protect the FLASH memory against possible unwanted operation).
155   * @param  Page_Address1: The page address in program memory to be erased in
156   *         the first Bank (BANK1). This parameter should be between FLASH_BASE
157   *         and FLASH_BANK1_END.
158   * @param  Page_Address2: The page address in program memory to be erased in
159   *         the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
160   *         and FLASH_BANK2_END.
161   * @note   A Page is erased in the Program memory only if the address to load
162   *         is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
163   * @retval HAL status
164   */
HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1,uint32_t Page_Address2)165 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
166 {
167   HAL_StatusTypeDef status = HAL_OK;
168 
169   /* Wait for last operation to be completed */
170   status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
171 
172   if(status == HAL_OK)
173   {
174     /* Proceed to erase the page */
175     SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
176     SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
177     SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
178 
179     /* Write 00000000h to the first word of the first program page to erase */
180     *(__IO uint32_t *)Page_Address1 = 0x00000000U;
181     /* Write 00000000h to the first word of the second program page to erase */
182     *(__IO uint32_t *)Page_Address2 = 0x00000000U;
183 
184     /* Wait for last operation to be completed */
185     status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
186 
187     /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
188     CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
189     CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
190     CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
191   }
192   /* Return the Erase Status */
193   return status;
194 }
195 
196 /**
197   * @brief  Program 2 half pages in program memory in parallel (half page size is 32 Words).
198   * @note   This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5  devices.
199   * @param  Address1: specifies the first address to be written in the first bank
200   *        (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
201   * @param  pBuffer1: pointer to the buffer  containing the data to be  written
202   *         to the first half page in the first bank.
203   * @param  Address2: specifies the second address to be written in the second bank
204   *        (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
205   * @param  pBuffer2: pointer to the buffer containing the data to be  written
206   *         to the second half page in the second bank.
207   * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
208   *         must be called before.
209   *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
210   *         (recommended to protect the FLASH memory against possible unwanted operation).
211   * @note   Half page write is possible only from SRAM.
212   * @note   If there are more than 32 words to write, after 32 words another
213   *         Half Page programming operation starts and has to be finished.
214   * @note   A half page is written to the program memory only if the first
215   *         address to load is the start address of a half page (multiple of 128
216   *         bytes) and the 31 remaining words to load are in the same half page.
217   * @note   During the Program memory half page write all read operations are
218   *         forbidden (this includes DMA read operations and debugger read
219   *         operations such as breakpoints, periodic updates, etc.).
220   * @note   If a PGAERR is set during a Program memory half page write, the
221   *         complete write operation is aborted. Software should then reset the
222   *         FPRG and PROG/DATA bits and restart the write operation from the
223   *         beginning.
224   * @retval HAL status
225   */
HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1,uint32_t * pBuffer1,uint32_t Address2,uint32_t * pBuffer2)226 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
227 {
228   uint32_t primask_bit;
229   uint32_t count = 0U;
230   HAL_StatusTypeDef status = HAL_OK;
231 
232   /* Wait for last operation to be completed */
233   status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
234 
235   if(status == HAL_OK)
236   {
237     /* Disable all IRQs */
238     primask_bit = __get_PRIMASK();
239     __disable_irq();
240 
241     /* Proceed to program the new half page */
242     SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
243     SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
244     SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
245 
246     /* Write the first half page directly with 32 different words */
247     while(count < 32U)
248     {
249       *(__IO uint32_t*) ((uint32_t)(Address1 + (4 * count))) = *pBuffer1;
250       pBuffer1++;
251       count ++;
252     }
253 
254     /* Write the second half page directly with 32 different words */
255     count = 0U;
256     while(count < 32U)
257     {
258       *(__IO uint32_t*) ((uint32_t)(Address2 + (4 * count))) = *pBuffer2;
259       pBuffer2++;
260       count ++;
261     }
262 
263     /* Wait for last operation to be completed */
264     status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
265 
266     /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
267     CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
268     CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
269     CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
270 
271     /* Enable IRQs */
272     __set_PRIMASK(primask_bit);
273   }
274 
275   /* Return the Write Status */
276   return status;
277 }
278 #endif /* FLASH_PECR_PARALLBANK */
279 
280 /**
281   * @brief  Program a half page in program memory.
282   * @param  Address specifies the address to be written.
283   * @param  pBuffer pointer to the buffer  containing the data to be  written to
284   *         the half page.
285   * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
286   *         must be called before.
287   *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
288   *         (recommended to protect the FLASH memory against possible unwanted operation)
289   * @note   Half page write is possible only from SRAM.
290   * @note   If there are more than 32 words to write, after 32 words another
291   *         Half Page programming operation starts and has to be finished.
292   * @note   A half page is written to the program memory only if the first
293   *         address to load is the start address of a half page (multiple of 128
294   *         bytes) and the 31 remaining words to load are in the same half page.
295   * @note   During the Program memory half page write all read operations are
296   *         forbidden (this includes DMA read operations and debugger read
297   *         operations such as breakpoints, periodic updates, etc.).
298   * @note   If a PGAERR is set during a Program memory half page write, the
299   *         complete write operation is aborted. Software should then reset the
300   *         FPRG and PROG/DATA bits and restart the write operation from the
301   *         beginning.
302   * @retval HAL status
303   */
HAL_FLASHEx_HalfPageProgram(uint32_t Address,uint32_t * pBuffer)304 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t* pBuffer)
305 {
306   uint32_t primask_bit;
307   uint32_t count = 0U;
308   HAL_StatusTypeDef status = HAL_OK;
309 
310   /* Wait for last operation to be completed */
311   status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
312 
313   if(status == HAL_OK)
314   {
315     /* Disable all IRQs */
316     primask_bit = __get_PRIMASK();
317     __disable_irq();
318 
319     /* Proceed to program the new half page */
320     SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
321     SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
322 
323     /* Write one half page directly with 32 different words */
324     while(count < 32U)
325     {
326       *(__IO uint32_t*) ((uint32_t)(Address + (4 * count))) = *pBuffer;
327       pBuffer++;
328       count ++;
329     }
330 
331     /* Wait for last operation to be completed */
332     status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
333 
334     /* If the write operation is completed, disable the PROG and FPRG bits */
335     CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
336     CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
337 
338     /* Enable IRQs */
339     __set_PRIMASK(primask_bit);
340   }
341 
342   /* Return the Write Status */
343   return status;
344 }
345 
346 /**
347   * @}
348   */
349 
350 /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group3 Peripheral errors functions
351  *  @brief    Peripheral errors functions
352  *
353 @verbatim
354  ===============================================================================
355                       ##### Peripheral errors functions #####
356  ===============================================================================
357     [..]
358     This subsection permit to get in run-time errors of  the FLASH peripheral.
359 
360 @endverbatim
361   * @{
362   */
363 
364 /**
365   * @brief  Get the specific FLASH errors flag.
366   * @param  Error pointer is the error value. It can be a mixed of:
367 @if STM32L100xB
368 @elif STM32L100xBA
369   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
370 @elif STM32L151xB
371 @elif STM32L151xBA
372   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
373 @elif STM32L152xB
374 @elif STM32L152xBA
375   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
376 @elif STM32L100xC
377   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
378   *            @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
379 @elif STM32L151xC
380   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
381   *            @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
382 @elif STM32L152xC
383   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
384   *            @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
385 @elif STM32L162xC
386   *            @arg @ref HAL_FLASH_ERROR_RD      FLASH Read Protection error flag (PCROP)
387   *            @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
388 @else
389   *            @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
390 @endif
391   *            @arg @ref HAL_FLASH_ERROR_PGA     FLASH Programming Alignment error flag
392   *            @arg @ref HAL_FLASH_ERROR_WRP     FLASH Write protected error flag
393   *            @arg @ref HAL_FLASH_ERROR_OPTV    FLASH Option valid error flag
394   * @retval HAL Status
395   */
HAL_FLASHEx_GetError(uint32_t * Error)396 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_GetError(uint32_t * Error)
397 {
398   *Error = pFlash.ErrorCode;
399   return HAL_OK;
400 }
401 
402 /**
403   * @}
404   */
405 
406 /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group4 DATA EEPROM functions
407   *
408   * @{
409   */
410 
411 /**
412   * @brief  Erase a double word in data memory.
413   * @param  Address specifies the address to be erased.
414   * @note   To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
415   *         must be called before.
416   *         Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
417   *         and Flash program erase control register access(recommended to protect
418   *         the DATA_EEPROM against possible unwanted operation).
419   * @note   Data memory double word erase is possible only from SRAM.
420   * @note   A double word is erased to the data memory only if the first address
421   *         to load is the start address of a double word (multiple of 8 bytes).
422   * @note   During the Data memory double word erase, all read operations are
423   *         forbidden (this includes DMA read operations and debugger read
424   *         operations such as breakpoints, periodic updates, etc.).
425   * @retval HAL status
426   */
427 
HAL_FLASHEx_DATAEEPROM_EraseDoubleWord(uint32_t Address)428 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_EraseDoubleWord(uint32_t Address)
429 {
430   uint32_t primask_bit;
431   HAL_StatusTypeDef status = HAL_OK;
432 
433   /* Wait for last operation to be completed */
434   status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
435 
436   if(status == HAL_OK)
437   {
438     /* Disable all IRQs */
439     primask_bit = __get_PRIMASK();
440     __disable_irq();
441 
442     /* If the previous operation is completed, proceed to erase the next double word */
443     /* Set the ERASE bit */
444     SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
445 
446     /* Set DATA bit */
447     SET_BIT(FLASH->PECR, FLASH_PECR_DATA);
448 
449     /* Write 00000000h to the 2 words to erase */
450     *(__IO uint32_t *)Address = 0x00000000U;
451     Address += 4U;
452     *(__IO uint32_t *)Address = 0x00000000U;
453 
454     /* Wait for last operation to be completed */
455     status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
456 
457     /* If the erase operation is completed, disable the ERASE and DATA bits */
458     CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
459     CLEAR_BIT(FLASH->PECR, FLASH_PECR_DATA);
460 
461     /* Enable IRQs */
462     __set_PRIMASK(primask_bit);
463 
464   }
465 
466   /* Return the erase status */
467   return status;
468 }
469 
470 /**
471   * @brief  Write a double word in data memory without erase.
472   * @param  Address specifies the address to be written.
473   * @param  Data specifies the data to be written.
474   * @note   To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
475   *         must be called before.
476   *         Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
477   *         and Flash program erase control register access(recommended to protect
478   *         the DATA_EEPROM against possible unwanted operation).
479   * @note   Data memory double word write is possible only from SRAM.
480   * @note   A data memory double word is written to the data memory only if the
481   *         first address to load is the start address of a double word (multiple
482   *         of double word).
483   * @note   During the Data memory double word write, all read operations are
484   *         forbidden (this includes DMA read operations and debugger read
485   *         operations such as breakpoints, periodic updates, etc.).
486   * @retval HAL status
487   */
HAL_FLASHEx_DATAEEPROM_ProgramDoubleWord(uint32_t Address,uint64_t Data)488 __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)
489 {
490   uint32_t primask_bit;
491   HAL_StatusTypeDef status = HAL_OK;
492 
493   /* Wait for last operation to be completed */
494   status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
495 
496   if(status == HAL_OK)
497   {
498     /* Disable all IRQs */
499     primask_bit = __get_PRIMASK();
500     __disable_irq();
501 
502     /* If the previous operation is completed, proceed to program the new data*/
503     SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
504     SET_BIT(FLASH->PECR, FLASH_PECR_DATA);
505 
506     /* Write the 2 words */
507      *(__IO uint32_t *)Address = (uint32_t) Data;
508      Address += 4U;
509      *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);
510 
511     /* Wait for last operation to be completed */
512     status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
513 
514     /* If the write operation is completed, disable the FPRG and DATA bits */
515     CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
516     CLEAR_BIT(FLASH->PECR, FLASH_PECR_DATA);
517 
518     /* Enable IRQs */
519     __set_PRIMASK(primask_bit);
520   }
521 
522   /* Return the Write Status */
523   return status;
524 }
525 
526 /**
527   * @}
528   */
529 
530 /**
531   * @}
532   */
533 
534 /** @addtogroup FLASH_RAMFUNC_Private_Functions
535   * @{
536   */
537 
538 /**
539   * @brief  Set the specific FLASH error flag.
540   * @retval HAL Status
541   */
FLASHRAM_SetErrorCode(void)542 static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void)
543 {
544   uint32_t flags = 0U;
545 
546   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
547   {
548     pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
549     flags |= FLASH_FLAG_WRPERR;
550   }
551   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
552   {
553     pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
554     flags |= FLASH_FLAG_PGAERR;
555   }
556   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
557   {
558     pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
559     flags |= FLASH_FLAG_OPTVERR;
560   }
561 
562 #if defined(FLASH_SR_RDERR)
563   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
564   {
565     pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
566     flags |= FLASH_FLAG_RDERR;
567   }
568 #endif /* FLASH_SR_RDERR */
569 #if defined(FLASH_SR_OPTVERRUSR)
570   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR))
571   {
572     pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTVUSR;
573     flags |= FLASH_FLAG_OPTVERRUSR;
574   }
575 #endif /* FLASH_SR_OPTVERRUSR */
576 
577   /* Clear FLASH error pending bits */
578   __HAL_FLASH_CLEAR_FLAG(flags);
579 
580   return HAL_OK;
581 }
582 
583 /**
584   * @brief  Wait for a FLASH operation to complete.
585   * @param  Timeout maximum flash operationtimeout
586   * @retval HAL status
587   */
FLASHRAM_WaitForLastOperation(uint32_t Timeout)588 static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout)
589 {
590     /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
591        Even if the FLASH operation fails, the BUSY flag will be reset and an error
592        flag will be set */
593 
594     while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00U))
595     {
596       Timeout--;
597     }
598 
599     if(Timeout == 0x00U)
600     {
601       return HAL_TIMEOUT;
602     }
603 
604   /* Check FLASH End of Operation flag  */
605   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
606   {
607     /* Clear FLASH End of Operation pending bit */
608     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
609   }
610 
611   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)  ||
612      __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
613 #if defined(FLASH_SR_RDERR)
614       __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
615 #endif /* FLASH_SR_RDERR */
616 #if defined(FLASH_SR_OPTVERRUSR)
617       __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
618 #endif /* FLASH_SR_OPTVERRUSR */
619      __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
620   {
621     /*Save the error code*/
622     FLASHRAM_SetErrorCode();
623     return HAL_ERROR;
624   }
625 
626   /* There is no error flag set */
627   return HAL_OK;
628 }
629 
630 /**
631   * @}
632   */
633 
634 /**
635   * @}
636   */
637 
638 #endif /* HAL_FLASH_MODULE_ENABLED */
639 /**
640   * @}
641   */
642 
643 
644 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
645