Increment eeprom version
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_flash.c
blob9899b0be18911613334db7e0452d1bc157021675
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_flash.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-2017
7 * @brief FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the internal FLASH memory:
10 * + Program operations functions
11 * + Memory Control functions
12 * + Peripheral State functions
14 @verbatim
15 ==============================================================================
16 ##### FLASH peripheral features #####
17 ==============================================================================
18 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
19 to the Flash memory. It implements the erase and program Flash memory operations
20 and the read and write protection mechanisms.
22 [..] The Flash memory interface accelerates code execution with a system of instruction
23 prefetch.
25 [..] The FLASH main features are:
26 (+) Flash memory read operations
27 (+) Flash memory program/erase operations
28 (+) Read / write protections
29 (+) Prefetch on I-Code
30 (+) Option Bytes programming
33 ##### How to use this driver #####
34 ==============================================================================
35 [..]
36 This driver provides functions and macros to configure and program the FLASH
37 memory of all STM32F1xx devices.
39 (#) FLASH Memory I/O Programming functions: this group includes all needed
40 functions to erase and program the main memory:
41 (++) Lock and Unlock the FLASH interface
42 (++) Erase function: Erase page, erase all pages
43 (++) Program functions: half word, word and doubleword
44 (#) FLASH Option Bytes Programming functions: this group includes all needed
45 functions to manage the Option Bytes:
46 (++) Lock and Unlock the Option Bytes
47 (++) Set/Reset the write protection
48 (++) Set the Read protection Level
49 (++) Program the user Option Bytes
50 (++) Launch the Option Bytes loader
51 (++) Erase Option Bytes
52 (++) Program the data Option Bytes
53 (++) Get the Write protection.
54 (++) Get the user option bytes.
56 (#) Interrupts and flags management functions : this group
57 includes all needed functions to:
58 (++) Handle FLASH interrupts
59 (++) Wait for last FLASH operation according to its status
60 (++) Get error flag status
62 [..] In addition to these function, this driver includes a set of macros allowing
63 to handle the following operations:
65 (+) Set/Get the latency
66 (+) Enable/Disable the prefetch buffer
67 (+) Enable/Disable the half cycle access
68 (+) Enable/Disable the FLASH interrupts
69 (+) Monitor the FLASH flags status
71 @endverbatim
72 ******************************************************************************
73 * @attention
75 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
77 * Redistribution and use in source and binary forms, with or without modification,
78 * are permitted provided that the following conditions are met:
79 * 1. Redistributions of source code must retain the above copyright notice,
80 * this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright notice,
82 * this list of conditions and the following disclaimer in the documentation
83 * and/or other materials provided with the distribution.
84 * 3. Neither the name of STMicroelectronics nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
88 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
89 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
94 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
95 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
96 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
97 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99 ******************************************************************************
102 /* Includes ------------------------------------------------------------------*/
103 #include "stm32f1xx_hal.h"
105 /** @addtogroup STM32F1xx_HAL_Driver
106 * @{
109 #ifdef HAL_FLASH_MODULE_ENABLED
111 /** @defgroup FLASH FLASH
112 * @brief FLASH HAL module driver
113 * @{
116 /* Private typedef -----------------------------------------------------------*/
117 /* Private define ------------------------------------------------------------*/
118 /** @defgroup FLASH_Private_Constants FLASH Private Constants
119 * @{
122 * @}
125 /* Private macro ---------------------------- ---------------------------------*/
126 /** @defgroup FLASH_Private_Macros FLASH Private Macros
127 * @{
131 * @}
134 /* Private variables ---------------------------------------------------------*/
135 /** @defgroup FLASH_Private_Variables FLASH Private Variables
136 * @{
138 /* Variables used for Erase pages under interruption*/
139 FLASH_ProcessTypeDef pFlash;
141 * @}
144 /* Private function prototypes -----------------------------------------------*/
145 /** @defgroup FLASH_Private_Functions FLASH Private Functions
146 * @{
148 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
149 static void FLASH_SetErrorCode(void);
150 extern void FLASH_PageErase(uint32_t PageAddress);
152 * @}
155 /* Exported functions ---------------------------------------------------------*/
156 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
157 * @{
160 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
161 * @brief Programming operation functions
163 @verbatim
164 @endverbatim
165 * @{
169 * @brief Program halfword, word or double word at a specified address
170 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
171 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
173 * @note If an erase and a program operations are requested simultaneously,
174 * the erase operation is performed before the program one.
176 * @note FLASH should be previously erased before new programmation (only exception to this
177 * is when 0x0000 is programmed)
179 * @param TypeProgram: Indicate the way to program at a specified address.
180 * This parameter can be a value of @ref FLASH_Type_Program
181 * @param Address: Specifies the address to be programmed.
182 * @param Data: Specifies the data to be programmed
184 * @retval HAL_StatusTypeDef HAL Status
186 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
188 HAL_StatusTypeDef status = HAL_ERROR;
189 uint8_t index = 0;
190 uint8_t nbiterations = 0;
192 /* Process Locked */
193 __HAL_LOCK(&pFlash);
195 /* Check the parameters */
196 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
197 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
199 #if defined(FLASH_BANK2_END)
200 if(Address <= FLASH_BANK1_END)
202 #endif /* FLASH_BANK2_END */
203 /* Wait for last operation to be completed */
204 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
205 #if defined(FLASH_BANK2_END)
207 else
209 /* Wait for last operation to be completed */
210 status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE);
212 #endif /* FLASH_BANK2_END */
214 if(status == HAL_OK)
216 if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
218 /* Program halfword (16-bit) at a specified address. */
219 nbiterations = 1U;
221 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
223 /* Program word (32-bit = 2*16-bit) at a specified address. */
224 nbiterations = 2U;
226 else
228 /* Program double word (64-bit = 4*16-bit) at a specified address. */
229 nbiterations = 4U;
232 for (index = 0U; index < nbiterations; index++)
234 FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index)));
236 #if defined(FLASH_BANK2_END)
237 if(Address <= FLASH_BANK1_END)
239 #endif /* FLASH_BANK2_END */
240 /* Wait for last operation to be completed */
241 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
243 /* If the program operation is completed, disable the PG Bit */
244 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
245 #if defined(FLASH_BANK2_END)
247 else
249 /* Wait for last operation to be completed */
250 status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE);
252 /* If the program operation is completed, disable the PG Bit */
253 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG);
255 #endif /* FLASH_BANK2_END */
256 /* In case of error, stop programation procedure */
257 if (status != HAL_OK)
259 break;
264 /* Process Unlocked */
265 __HAL_UNLOCK(&pFlash);
267 return status;
271 * @brief Program halfword, word or double word at a specified address with interrupt enabled.
272 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
273 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
275 * @note If an erase and a program operations are requested simultaneously,
276 * the erase operation is performed before the program one.
278 * @param TypeProgram: Indicate the way to program at a specified address.
279 * This parameter can be a value of @ref FLASH_Type_Program
280 * @param Address: Specifies the address to be programmed.
281 * @param Data: Specifies the data to be programmed
283 * @retval HAL_StatusTypeDef HAL Status
285 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
287 HAL_StatusTypeDef status = HAL_OK;
289 /* Process Locked */
290 __HAL_LOCK(&pFlash);
292 /* Check the parameters */
293 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
294 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
296 #if defined(FLASH_BANK2_END)
297 /* If procedure already ongoing, reject the next one */
298 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
300 return HAL_ERROR;
303 if(Address <= FLASH_BANK1_END)
305 /* Enable End of FLASH Operation and Error source interrupts */
306 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1);
308 }else
310 /* Enable End of FLASH Operation and Error source interrupts */
311 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
313 #else
314 /* Enable End of FLASH Operation and Error source interrupts */
315 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
316 #endif /* FLASH_BANK2_END */
318 pFlash.Address = Address;
319 pFlash.Data = Data;
321 if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
323 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
324 /* Program halfword (16-bit) at a specified address. */
325 pFlash.DataRemaining = 1U;
327 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
329 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
330 /* Program word (32-bit : 2*16-bit) at a specified address. */
331 pFlash.DataRemaining = 2U;
333 else
335 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
336 /* Program double word (64-bit : 4*16-bit) at a specified address. */
337 pFlash.DataRemaining = 4U;
340 /* Program halfword (16-bit) at a specified address. */
341 FLASH_Program_HalfWord(Address, (uint16_t)Data);
343 return status;
347 * @brief This function handles FLASH interrupt request.
348 * @retval None
350 void HAL_FLASH_IRQHandler(void)
352 uint32_t addresstmp = 0U;
354 /* Check FLASH operation error flags */
355 #if defined(FLASH_BANK2_END)
356 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK1) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK1) || \
357 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)))
358 #else
359 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
360 #endif /* FLASH_BANK2_END */
362 /* Return the faulty address */
363 addresstmp = pFlash.Address;
364 /* Reset address */
365 pFlash.Address = 0xFFFFFFFFU;
367 /* Save the Error code */
368 FLASH_SetErrorCode();
370 /* FLASH error interrupt user callback */
371 HAL_FLASH_OperationErrorCallback(addresstmp);
373 /* Stop the procedure ongoing */
374 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
377 /* Check FLASH End of Operation flag */
378 #if defined(FLASH_BANK2_END)
379 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK1))
381 /* Clear FLASH End of Operation pending bit */
382 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK1);
383 #else
384 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
386 /* Clear FLASH End of Operation pending bit */
387 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
388 #endif /* FLASH_BANK2_END */
390 /* Process can continue only if no error detected */
391 if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
393 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
395 /* Nb of pages to erased can be decreased */
396 pFlash.DataRemaining--;
398 /* Check if there are still pages to erase */
399 if(pFlash.DataRemaining != 0U)
401 addresstmp = pFlash.Address;
402 /*Indicate user which sector has been erased */
403 HAL_FLASH_EndOfOperationCallback(addresstmp);
405 /*Increment sector number*/
406 addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
407 pFlash.Address = addresstmp;
409 /* If the erase operation is completed, disable the PER Bit */
410 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
412 FLASH_PageErase(addresstmp);
414 else
416 /* No more pages to Erase, user callback can be called. */
417 /* Reset Sector and stop Erase pages procedure */
418 pFlash.Address = addresstmp = 0xFFFFFFFFU;
419 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
420 /* FLASH EOP interrupt user callback */
421 HAL_FLASH_EndOfOperationCallback(addresstmp);
424 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
426 /* Operation is completed, disable the MER Bit */
427 CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
429 #if defined(FLASH_BANK2_END)
430 /* Stop Mass Erase procedure if no pending mass erase on other bank */
431 if (HAL_IS_BIT_CLR(FLASH->CR2, FLASH_CR2_MER))
433 #endif /* FLASH_BANK2_END */
434 /* MassErase ended. Return the selected bank */
435 /* FLASH EOP interrupt user callback */
436 HAL_FLASH_EndOfOperationCallback(0U);
438 /* Stop Mass Erase procedure*/
439 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
441 #if defined(FLASH_BANK2_END)
443 #endif /* FLASH_BANK2_END */
444 else
446 /* Nb of 16-bit data to program can be decreased */
447 pFlash.DataRemaining--;
449 /* Check if there are still 16-bit data to program */
450 if(pFlash.DataRemaining != 0U)
452 /* Increment address to 16-bit */
453 pFlash.Address += 2U;
454 addresstmp = pFlash.Address;
456 /* Shift to have next 16-bit data */
457 pFlash.Data = (pFlash.Data >> 16U);
459 /* Operation is completed, disable the PG Bit */
460 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
462 /*Program halfword (16-bit) at a specified address.*/
463 FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
465 else
467 /* Program ended. Return the selected address */
468 /* FLASH EOP interrupt user callback */
469 if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
471 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
473 else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
475 HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U);
477 else
479 HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U);
482 /* Reset Address and stop Program procedure */
483 pFlash.Address = 0xFFFFFFFFU;
484 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
490 #if defined(FLASH_BANK2_END)
491 /* Check FLASH End of Operation flag */
492 if(__HAL_FLASH_GET_FLAG( FLASH_FLAG_EOP_BANK2))
494 /* Clear FLASH End of Operation pending bit */
495 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2);
497 /* Process can continue only if no error detected */
498 if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
500 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
502 /* Nb of pages to erased can be decreased */
503 pFlash.DataRemaining--;
505 /* Check if there are still pages to erase*/
506 if(pFlash.DataRemaining != 0U)
508 /* Indicate user which page address has been erased*/
509 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
511 /* Increment page address to next page */
512 pFlash.Address += FLASH_PAGE_SIZE;
513 addresstmp = pFlash.Address;
515 /* Operation is completed, disable the PER Bit */
516 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER);
518 FLASH_PageErase(addresstmp);
520 else
522 /*No more pages to Erase*/
524 /*Reset Address and stop Erase pages procedure*/
525 pFlash.Address = 0xFFFFFFFFU;
526 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
528 /* FLASH EOP interrupt user callback */
529 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
532 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
534 /* Operation is completed, disable the MER Bit */
535 CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
537 if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_MER))
539 /* MassErase ended. Return the selected bank*/
540 /* FLASH EOP interrupt user callback */
541 HAL_FLASH_EndOfOperationCallback(0U);
543 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
546 else
548 /* Nb of 16-bit data to program can be decreased */
549 pFlash.DataRemaining--;
551 /* Check if there are still 16-bit data to program */
552 if(pFlash.DataRemaining != 0U)
554 /* Increment address to 16-bit */
555 pFlash.Address += 2U;
556 addresstmp = pFlash.Address;
558 /* Shift to have next 16-bit data */
559 pFlash.Data = (pFlash.Data >> 16U);
561 /* Operation is completed, disable the PG Bit */
562 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG);
564 /*Program halfword (16-bit) at a specified address.*/
565 FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
567 else
569 /*Program ended. Return the selected address*/
570 /* FLASH EOP interrupt user callback */
571 if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
573 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
575 else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
577 HAL_FLASH_EndOfOperationCallback(pFlash.Address-2U);
579 else
581 HAL_FLASH_EndOfOperationCallback(pFlash.Address-6U);
584 /* Reset Address and stop Program procedure*/
585 pFlash.Address = 0xFFFFFFFFU;
586 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
591 #endif
593 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
595 #if defined(FLASH_BANK2_END)
596 /* Operation is completed, disable the PG, PER and MER Bits for both bank */
597 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
598 CLEAR_BIT(FLASH->CR2, (FLASH_CR2_PG | FLASH_CR2_PER | FLASH_CR2_MER));
600 /* Disable End of FLASH Operation and Error source interrupts for both banks */
601 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1 | FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
602 #else
603 /* Operation is completed, disable the PG, PER and MER Bits */
604 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
606 /* Disable End of FLASH Operation and Error source interrupts */
607 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
608 #endif /* FLASH_BANK2_END */
610 /* Process Unlocked */
611 __HAL_UNLOCK(&pFlash);
616 * @brief FLASH end of operation interrupt callback
617 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
618 * - Mass Erase: No return value expected
619 * - Pages Erase: Address of the page which has been erased
620 * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
621 * - Program: Address which was selected for data program
622 * @retval none
624 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
626 /* Prevent unused argument(s) compilation warning */
627 UNUSED(ReturnValue);
629 /* NOTE : This function Should not be modified, when the callback is needed,
630 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
635 * @brief FLASH operation error interrupt callback
636 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
637 * - Mass Erase: No return value expected
638 * - Pages Erase: Address of the page which returned an error
639 * - Program: Address which was selected for data program
640 * @retval none
642 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
644 /* Prevent unused argument(s) compilation warning */
645 UNUSED(ReturnValue);
647 /* NOTE : This function Should not be modified, when the callback is needed,
648 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
653 * @}
656 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
657 * @brief management functions
659 @verbatim
660 ===============================================================================
661 ##### Peripheral Control functions #####
662 ===============================================================================
663 [..]
664 This subsection provides a set of functions allowing to control the FLASH
665 memory operations.
667 @endverbatim
668 * @{
672 * @brief Unlock the FLASH control register access
673 * @retval HAL Status
675 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
677 if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
679 /* Authorize the FLASH Registers access */
680 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
681 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
683 else
685 return HAL_ERROR;
688 #if defined(FLASH_BANK2_END)
689 if (HAL_IS_BIT_SET(FLASH->CR2, FLASH_CR2_LOCK))
691 /* Authorize the FLASH BANK2 Registers access */
692 WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
693 WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
695 else
697 return HAL_ERROR;
700 #endif /* FLASH_BANK2_END */
701 return HAL_OK;
705 * @brief Locks the FLASH control register access
706 * @retval HAL Status
708 HAL_StatusTypeDef HAL_FLASH_Lock(void)
710 /* Set the LOCK Bit to lock the FLASH Registers access */
711 SET_BIT(FLASH->CR, FLASH_CR_LOCK);
713 #if defined(FLASH_BANK2_END)
714 /* Set the LOCK Bit to lock the FLASH BANK2 Registers access */
715 SET_BIT(FLASH->CR2, FLASH_CR2_LOCK);
717 #endif /* FLASH_BANK2_END */
718 return HAL_OK;
722 * @brief Unlock the FLASH Option Control Registers access.
723 * @retval HAL Status
725 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
727 if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
729 /* Authorizes the Option Byte register programming */
730 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
731 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
733 else
735 return HAL_ERROR;
738 return HAL_OK;
742 * @brief Lock the FLASH Option Control Registers access.
743 * @retval HAL Status
745 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
747 /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
748 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
750 return HAL_OK;
754 * @brief Launch the option byte loading.
755 * @note This function will reset automatically the MCU.
756 * @retval None
758 void HAL_FLASH_OB_Launch(void)
760 /* Initiates a system reset request to launch the option byte loading */
761 HAL_NVIC_SystemReset();
765 * @}
768 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
769 * @brief Peripheral errors functions
771 @verbatim
772 ===============================================================================
773 ##### Peripheral Errors functions #####
774 ===============================================================================
775 [..]
776 This subsection permit to get in run-time errors of the FLASH peripheral.
778 @endverbatim
779 * @{
783 * @brief Get the specific FLASH error flag.
784 * @retval FLASH_ErrorCode The returned value can be:
785 * @ref FLASH_Error_Codes
787 uint32_t HAL_FLASH_GetError(void)
789 return pFlash.ErrorCode;
793 * @}
797 * @}
800 /** @addtogroup FLASH_Private_Functions
801 * @{
805 * @brief Program a half-word (16-bit) at a specified address.
806 * @param Address specify the address to be programmed.
807 * @param Data specify the data to be programmed.
808 * @retval None
810 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
812 /* Clean the error context */
813 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
815 #if defined(FLASH_BANK2_END)
816 if(Address <= FLASH_BANK1_END)
818 #endif /* FLASH_BANK2_END */
819 /* Proceed to program the new data */
820 SET_BIT(FLASH->CR, FLASH_CR_PG);
821 #if defined(FLASH_BANK2_END)
823 else
825 /* Proceed to program the new data */
826 SET_BIT(FLASH->CR2, FLASH_CR2_PG);
828 #endif /* FLASH_BANK2_END */
830 /* Write data in the address */
831 *(__IO uint16_t*)Address = Data;
835 * @brief Wait for a FLASH operation to complete.
836 * @param Timeout maximum flash operation timeout
837 * @retval HAL Status
839 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
841 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
842 Even if the FLASH operation fails, the BUSY flag will be reset and an error
843 flag will be set */
845 uint32_t tickstart = HAL_GetTick();
847 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
849 if (Timeout != HAL_MAX_DELAY)
851 if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
853 return HAL_TIMEOUT;
858 /* Check FLASH End of Operation flag */
859 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
861 /* Clear FLASH End of Operation pending bit */
862 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
865 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
866 __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
867 __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
869 /*Save the error code*/
870 FLASH_SetErrorCode();
871 return HAL_ERROR;
874 /* There is no error flag set */
875 return HAL_OK;
878 #if defined(FLASH_BANK2_END)
880 * @brief Wait for a FLASH BANK2 operation to complete.
881 * @param Timeout maximum flash operation timeout
882 * @retval HAL_StatusTypeDef HAL Status
884 HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout)
886 /* Wait for the FLASH BANK2 operation to complete by polling on BUSY flag to be reset.
887 Even if the FLASH BANK2 operation fails, the BUSY flag will be reset and an error
888 flag will be set */
890 uint32_t tickstart = HAL_GetTick();
892 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY_BANK2))
894 if (Timeout != HAL_MAX_DELAY)
896 if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
898 return HAL_TIMEOUT;
903 /* Check FLASH End of Operation flag */
904 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK2))
906 /* Clear FLASH End of Operation pending bit */
907 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2);
910 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))
912 /*Save the error code*/
913 FLASH_SetErrorCode();
914 return HAL_ERROR;
917 /* If there is an error flag set */
918 return HAL_OK;
921 #endif /* FLASH_BANK2_END */
924 * @brief Set the specific FLASH error flag.
925 * @retval None
927 static void FLASH_SetErrorCode(void)
929 uint32_t flags = 0U;
931 #if defined(FLASH_BANK2_END)
932 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2))
933 #else
934 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
935 #endif /* FLASH_BANK2_END */
937 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
938 #if defined(FLASH_BANK2_END)
939 flags |= FLASH_FLAG_WRPERR | FLASH_FLAG_WRPERR_BANK2;
940 #else
941 flags |= FLASH_FLAG_WRPERR;
942 #endif /* FLASH_BANK2_END */
944 #if defined(FLASH_BANK2_END)
945 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))
946 #else
947 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
948 #endif /* FLASH_BANK2_END */
950 pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
951 #if defined(FLASH_BANK2_END)
952 flags |= FLASH_FLAG_PGERR | FLASH_FLAG_PGERR_BANK2;
953 #else
954 flags |= FLASH_FLAG_PGERR;
955 #endif /* FLASH_BANK2_END */
957 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
959 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
960 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
963 /* Clear FLASH error pending bits */
964 __HAL_FLASH_CLEAR_FLAG(flags);
967 * @}
971 * @}
974 #endif /* HAL_FLASH_MODULE_ENABLED */
977 * @}
980 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/