2 ******************************************************************************
3 * @file stm32f7xx_hal_flash.c
4 * @author MCD Application Team
5 * @brief FLASH HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the internal FLASH memory:
8 * + Program operations functions
9 * + Memory Control functions
10 * + Peripheral Errors functions
13 ==============================================================================
14 ##### FLASH peripheral features #####
15 ==============================================================================
17 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
18 to the Flash memory. It implements the erase and program Flash memory operations
19 and the read and write protection mechanisms.
21 [..] The Flash memory interface accelerates code execution with a system of instruction
22 prefetch and cache lines.
24 [..] The FLASH main features are:
25 (+) Flash memory read operations
26 (+) Flash memory program/erase operations
27 (+) Read / write protections
28 (+) Prefetch on I-Code
29 (+) 64 cache lines of 128 bits on I-Code
30 (+) 8 cache lines of 128 bits on D-Code
32 ##### How to use this driver #####
33 ==============================================================================
35 This driver provides functions and macros to configure and program the FLASH
36 memory of all STM32F7xx devices.
38 (#) FLASH Memory IO Programming functions:
39 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
40 HAL_FLASH_Lock() functions
41 (++) Program functions: byte, half word, word and double word
42 (++) There Two modes of programming :
43 (+++) Polling mode using HAL_FLASH_Program() function
44 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
46 (#) Interrupts and flags management functions :
47 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
48 (++) Wait for last FLASH operation according to its status
49 (++) Get error flag status by calling HAL_SetErrorCode()
51 In addition to these functions, this driver includes a set of macros allowing
52 to handle the following operations:
54 (+) Enable/Disable the prefetch buffer
55 (+) Enable/Disable the Instruction cache and the Data cache
56 (+) Reset the Instruction cache and the Data cache
57 (+) Enable/Disable the FLASH interrupts
58 (+) Monitor the FLASH flags status
60 (@) For any Flash memory program operation (erase or program), the CPU clock frequency
61 (HCLK) must be at least 1MHz.
62 (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
63 a Flash memory operation.
64 (@) Any attempt to read the Flash memory while it is being written or erased, causes the
65 bus to stall. Read operations are processed correctly once the program operation has
66 completed. This means that code or data fetches cannot be performed while a write/erase
70 ******************************************************************************
73 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
74 * All rights reserved.</center></h2>
76 * This software component is licensed by ST under BSD 3-Clause license,
77 * the "License"; You may not use this file except in compliance with the
78 * License. You may obtain a copy of the License at:
79 * opensource.org/licenses/BSD-3-Clause
81 ******************************************************************************
84 /* Includes ------------------------------------------------------------------*/
85 #include "stm32f7xx_hal.h"
87 /** @addtogroup STM32F7xx_HAL_Driver
91 /** @defgroup FLASH FLASH
92 * @brief FLASH HAL module driver
96 #ifdef HAL_FLASH_MODULE_ENABLED
98 /* Private typedef -----------------------------------------------------------*/
99 /* Private define ------------------------------------------------------------*/
100 /** @addtogroup FLASH_Private_Constants
103 #define SECTOR_MASK ((uint32_t)0xFFFFFF07U)
104 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000U)/* 50 s */
108 /* Private macro -------------------------------------------------------------*/
109 /* Private variables ---------------------------------------------------------*/
110 /** @addtogroup FLASH_Private_Variables
113 /* Variable used for Erase sectors under interruption */
114 FLASH_ProcessTypeDef pFlash
;
119 /* Private function prototypes -----------------------------------------------*/
120 /** @addtogroup FLASH_Private_Functions
123 /* Program operations */
124 static void FLASH_Program_DoubleWord(uint32_t Address
, uint64_t Data
);
125 static void FLASH_Program_Word(uint32_t Address
, uint32_t Data
);
126 static void FLASH_Program_HalfWord(uint32_t Address
, uint16_t Data
);
127 static void FLASH_Program_Byte(uint32_t Address
, uint8_t Data
);
128 static void FLASH_SetErrorCode(void);
130 HAL_StatusTypeDef
FLASH_WaitForLastOperation(uint32_t Timeout
);
135 /* Exported functions --------------------------------------------------------*/
136 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
140 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
141 * @brief Programming operation functions
144 ===============================================================================
145 ##### Programming operation functions #####
146 ===============================================================================
148 This subsection provides a set of functions allowing to manage the FLASH
156 * @brief Program byte, halfword, word or double word at a specified address
157 * @param TypeProgram Indicate the way to program at a specified address.
158 * This parameter can be a value of @ref FLASH_Type_Program
159 * @param Address specifies the address to be programmed.
160 * @param Data specifies the data to be programmed
162 * @retval HAL_StatusTypeDef HAL Status
164 HAL_StatusTypeDef
HAL_FLASH_Program(uint32_t TypeProgram
, uint32_t Address
, uint64_t Data
)
166 HAL_StatusTypeDef status
= HAL_ERROR
;
171 /* Check the parameters */
172 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram
));
174 /* Wait for last operation to be completed */
175 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
181 case FLASH_TYPEPROGRAM_BYTE
:
183 /*Program byte (8-bit) at a specified address.*/
184 FLASH_Program_Byte(Address
, (uint8_t) Data
);
188 case FLASH_TYPEPROGRAM_HALFWORD
:
190 /*Program halfword (16-bit) at a specified address.*/
191 FLASH_Program_HalfWord(Address
, (uint16_t) Data
);
195 case FLASH_TYPEPROGRAM_WORD
:
197 /*Program word (32-bit) at a specified address.*/
198 FLASH_Program_Word(Address
, (uint32_t) Data
);
202 case FLASH_TYPEPROGRAM_DOUBLEWORD
:
204 /*Program double word (64-bit) at a specified address.*/
205 FLASH_Program_DoubleWord(Address
, Data
);
211 /* Wait for last operation to be completed */
212 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
214 /* If the program operation is completed, disable the PG Bit */
215 FLASH
->CR
&= (~FLASH_CR_PG
);
218 /* Process Unlocked */
219 __HAL_UNLOCK(&pFlash
);
225 * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
226 * @param TypeProgram Indicate the way to program at a specified address.
227 * This parameter can be a value of @ref FLASH_Type_Program
228 * @param Address specifies the address to be programmed.
229 * @param Data specifies the data to be programmed
233 HAL_StatusTypeDef
HAL_FLASH_Program_IT(uint32_t TypeProgram
, uint32_t Address
, uint64_t Data
)
235 HAL_StatusTypeDef status
= HAL_OK
;
240 /* Check the parameters */
241 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram
));
243 /* Enable End of FLASH Operation interrupt */
244 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP
);
246 /* Enable Error source interrupt */
247 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR
);
249 /* Clear pending flags (if any) */
250 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
|\
251 FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_ERSERR
);
253 pFlash
.ProcedureOnGoing
= FLASH_PROC_PROGRAM
;
254 pFlash
.Address
= Address
;
258 case FLASH_TYPEPROGRAM_BYTE
:
260 /*Program byte (8-bit) at a specified address.*/
261 FLASH_Program_Byte(Address
, (uint8_t) Data
);
265 case FLASH_TYPEPROGRAM_HALFWORD
:
267 /*Program halfword (16-bit) at a specified address.*/
268 FLASH_Program_HalfWord(Address
, (uint16_t) Data
);
272 case FLASH_TYPEPROGRAM_WORD
:
274 /*Program word (32-bit) at a specified address.*/
275 FLASH_Program_Word(Address
, (uint32_t) Data
);
279 case FLASH_TYPEPROGRAM_DOUBLEWORD
:
281 /*Program double word (64-bit) at a specified address.*/
282 FLASH_Program_DoubleWord(Address
, Data
);
292 * @brief This function handles FLASH interrupt request.
295 void HAL_FLASH_IRQHandler(void)
299 /* If the program operation is completed, disable the PG Bit */
300 FLASH
->CR
&= (~FLASH_CR_PG
);
302 /* If the erase operation is completed, disable the SER Bit */
303 FLASH
->CR
&= (~FLASH_CR_SER
);
304 FLASH
->CR
&= SECTOR_MASK
;
306 /* if the erase operation is completed, disable the MER Bit */
307 FLASH
->CR
&= (~FLASH_MER_BIT
);
309 /* Check FLASH End of Operation flag */
310 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP
) != RESET
)
312 /* Clear FLASH End of Operation pending bit */
313 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
);
315 switch (pFlash
.ProcedureOnGoing
)
317 case FLASH_PROC_SECTERASE
:
319 /* Nb of sector to erased can be decreased */
320 pFlash
.NbSectorsToErase
--;
322 /* Check if there are still sectors to erase */
323 if(pFlash
.NbSectorsToErase
!= 0)
325 temp
= pFlash
.Sector
;
326 /* Indicate user which sector has been erased */
327 HAL_FLASH_EndOfOperationCallback(temp
);
329 /* Increment sector number */
330 temp
= ++pFlash
.Sector
;
331 FLASH_Erase_Sector(temp
, pFlash
.VoltageForErase
);
335 /* No more sectors to Erase, user callback can be called.*/
336 /* Reset Sector and stop Erase sectors procedure */
337 pFlash
.Sector
= temp
= 0xFFFFFFFFU
;
338 /* FLASH EOP interrupt user callback */
339 HAL_FLASH_EndOfOperationCallback(temp
);
340 /* Sector Erase procedure is completed */
341 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
346 case FLASH_PROC_MASSERASE
:
348 /* MassErase ended. Return the selected bank : in this product we don't have Banks */
349 /* FLASH EOP interrupt user callback */
350 HAL_FLASH_EndOfOperationCallback(0);
351 /* MAss Erase procedure is completed */
352 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
356 case FLASH_PROC_PROGRAM
:
358 /*Program ended. Return the selected address*/
359 /* FLASH EOP interrupt user callback */
360 HAL_FLASH_EndOfOperationCallback(pFlash
.Address
);
361 /* Programming procedure is completed */
362 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
370 /* Check FLASH operation error flags */
371 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ALL_ERRORS
) != RESET
)
373 switch (pFlash
.ProcedureOnGoing
)
375 case FLASH_PROC_SECTERASE
:
377 /* return the faulty sector */
378 temp
= pFlash
.Sector
;
379 pFlash
.Sector
= 0xFFFFFFFFU
;
382 case FLASH_PROC_MASSERASE
:
384 /* No return in case of Mass Erase */
388 case FLASH_PROC_PROGRAM
:
390 /*return the faulty address*/
391 temp
= pFlash
.Address
;
397 /*Save the Error code*/
398 FLASH_SetErrorCode();
400 /* FLASH error interrupt user callback */
401 HAL_FLASH_OperationErrorCallback(temp
);
403 /*Stop the procedure ongoing */
404 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
407 if(pFlash
.ProcedureOnGoing
== FLASH_PROC_NONE
)
409 /* Disable End of FLASH Operation interrupt */
410 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP
);
412 /* Disable Error source interrupt */
413 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR
);
415 /* Process Unlocked */
416 __HAL_UNLOCK(&pFlash
);
422 * @brief FLASH end of operation interrupt callback
423 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
424 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
425 * all the selected sectors have been erased)
426 * - Program : Address which was selected for data program
427 * - Mass Erase : No return value expected
430 __weak
void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue
)
432 /* Prevent unused argument(s) compilation warning */
434 /* NOTE : This function Should not be modified, when the callback is needed,
435 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
440 * @brief FLASH operation error interrupt callback
441 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
442 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
443 * all the selected sectors have been erased)
444 * - Program : Address which was selected for data program
445 * - Mass Erase : No return value expected
448 __weak
void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue
)
450 /* Prevent unused argument(s) compilation warning */
452 /* NOTE : This function Should not be modified, when the callback is needed,
453 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
461 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
462 * @brief management functions
465 ===============================================================================
466 ##### Peripheral Control functions #####
467 ===============================================================================
469 This subsection provides a set of functions allowing to control the FLASH
477 * @brief Unlock the FLASH control register access
480 HAL_StatusTypeDef
HAL_FLASH_Unlock(void)
482 HAL_StatusTypeDef status
= HAL_OK
;
484 if(READ_BIT(FLASH
->CR
, FLASH_CR_LOCK
) != RESET
)
486 /* Authorize the FLASH Registers access */
487 WRITE_REG(FLASH
->KEYR
, FLASH_KEY1
);
488 WRITE_REG(FLASH
->KEYR
, FLASH_KEY2
);
490 /* Verify Flash is unlocked */
491 if(READ_BIT(FLASH
->CR
, FLASH_CR_LOCK
) != RESET
)
501 * @brief Locks the FLASH control register access
504 HAL_StatusTypeDef
HAL_FLASH_Lock(void)
506 /* Set the LOCK Bit to lock the FLASH Registers access */
507 FLASH
->CR
|= FLASH_CR_LOCK
;
513 * @brief Unlock the FLASH Option Control Registers access.
516 HAL_StatusTypeDef
HAL_FLASH_OB_Unlock(void)
518 if((FLASH
->OPTCR
& FLASH_OPTCR_OPTLOCK
) != RESET
)
520 /* Authorizes the Option Byte register programming */
521 FLASH
->OPTKEYR
= FLASH_OPT_KEY1
;
522 FLASH
->OPTKEYR
= FLASH_OPT_KEY2
;
533 * @brief Lock the FLASH Option Control Registers access.
536 HAL_StatusTypeDef
HAL_FLASH_OB_Lock(void)
538 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
539 FLASH
->OPTCR
|= FLASH_OPTCR_OPTLOCK
;
545 * @brief Launch the option byte loading.
548 HAL_StatusTypeDef
HAL_FLASH_OB_Launch(void)
550 /* Set the OPTSTRT bit in OPTCR register */
551 FLASH
->OPTCR
|= FLASH_OPTCR_OPTSTRT
;
553 /* Wait for last operation to be completed */
554 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
));
561 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
562 * @brief Peripheral Errors functions
565 ===============================================================================
566 ##### Peripheral Errors functions #####
567 ===============================================================================
569 This subsection permits to get in run-time Errors of the FLASH peripheral.
576 * @brief Get the specific FLASH error flag.
577 * @retval FLASH_ErrorCode: The returned value can be:
578 * @arg FLASH_ERROR_ERS: FLASH Erasing Sequence error flag
579 * @arg FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
580 * @arg FLASH_ERROR_PGA: FLASH Programming Alignment error flag
581 * @arg FLASH_ERROR_WRP: FLASH Write protected error flag
582 * @arg FLASH_ERROR_OPERATION: FLASH operation Error flag
584 uint32_t HAL_FLASH_GetError(void)
586 return pFlash
.ErrorCode
;
594 * @brief Wait for a FLASH operation to complete.
595 * @param Timeout maximum flash operationtimeout
598 HAL_StatusTypeDef
FLASH_WaitForLastOperation(uint32_t Timeout
)
600 uint32_t tickstart
= 0;
602 /* Clear Error Code */
603 pFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
605 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
606 Even if the FLASH operation fails, the BUSY flag will be reset and an error
609 tickstart
= HAL_GetTick();
611 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY
) != RESET
)
613 if(Timeout
!= HAL_MAX_DELAY
)
615 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
622 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ALL_ERRORS
) != RESET
)
624 /*Save the error code*/
625 FLASH_SetErrorCode();
629 /* Check FLASH End of Operation flag */
630 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP
) != RESET
)
632 /* Clear FLASH End of Operation pending bit */
633 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
);
636 /* If there is an error flag set */
642 * @brief Program a double word (64-bit) at a specified address.
643 * @note This function must be used when the device voltage range is from
644 * 2.7V to 3.6V and an External Vpp is present.
646 * @note If an erase and a program operations are requested simultaneously,
647 * the erase operation is performed before the program one.
649 * @param Address specifies the address to be programmed.
650 * @param Data specifies the data to be programmed.
653 static void FLASH_Program_DoubleWord(uint32_t Address
, uint64_t Data
)
655 /* Check the parameters */
656 assert_param(IS_FLASH_ADDRESS(Address
));
658 /* If the previous operation is completed, proceed to program the new data */
659 FLASH
->CR
&= CR_PSIZE_MASK
;
660 FLASH
->CR
|= FLASH_PSIZE_DOUBLE_WORD
;
661 FLASH
->CR
|= FLASH_CR_PG
;
663 /* Program the double-word */
664 *(__IO
uint32_t*)Address
= (uint32_t)Data
;
665 *(__IO
uint32_t*)(Address
+4) = (uint32_t)(Data
>> 32);
667 /* Data synchronous Barrier (DSB) Just after the write operation
668 This will force the CPU to respect the sequence of instruction (no optimization).*/
674 * @brief Program word (32-bit) at a specified address.
675 * @note This function must be used when the device voltage range is from
678 * @note If an erase and a program operations are requested simultaneously,
679 * the erase operation is performed before the program one.
681 * @param Address specifies the address to be programmed.
682 * @param Data specifies the data to be programmed.
685 static void FLASH_Program_Word(uint32_t Address
, uint32_t Data
)
687 /* Check the parameters */
688 assert_param(IS_FLASH_ADDRESS(Address
));
690 /* If the previous operation is completed, proceed to program the new data */
691 FLASH
->CR
&= CR_PSIZE_MASK
;
692 FLASH
->CR
|= FLASH_PSIZE_WORD
;
693 FLASH
->CR
|= FLASH_CR_PG
;
695 *(__IO
uint32_t*)Address
= Data
;
697 /* Data synchronous Barrier (DSB) Just after the write operation
698 This will force the CPU to respect the sequence of instruction (no optimization).*/
703 * @brief Program a half-word (16-bit) at a specified address.
704 * @note This function must be used when the device voltage range is from
707 * @note If an erase and a program operations are requested simultaneously,
708 * the erase operation is performed before the program one.
710 * @param Address specifies the address to be programmed.
711 * @param Data specifies the data to be programmed.
714 static void FLASH_Program_HalfWord(uint32_t Address
, uint16_t Data
)
716 /* Check the parameters */
717 assert_param(IS_FLASH_ADDRESS(Address
));
719 /* If the previous operation is completed, proceed to program the new data */
720 FLASH
->CR
&= CR_PSIZE_MASK
;
721 FLASH
->CR
|= FLASH_PSIZE_HALF_WORD
;
722 FLASH
->CR
|= FLASH_CR_PG
;
724 *(__IO
uint16_t*)Address
= Data
;
726 /* Data synchronous Barrier (DSB) Just after the write operation
727 This will force the CPU to respect the sequence of instruction (no optimization).*/
733 * @brief Program byte (8-bit) at a specified address.
734 * @note This function must be used when the device voltage range is from
737 * @note If an erase and a program operations are requested simultaneously,
738 * the erase operation is performed before the program one.
740 * @param Address specifies the address to be programmed.
741 * @param Data specifies the data to be programmed.
744 static void FLASH_Program_Byte(uint32_t Address
, uint8_t Data
)
746 /* Check the parameters */
747 assert_param(IS_FLASH_ADDRESS(Address
));
749 /* If the previous operation is completed, proceed to program the new data */
750 FLASH
->CR
&= CR_PSIZE_MASK
;
751 FLASH
->CR
|= FLASH_PSIZE_BYTE
;
752 FLASH
->CR
|= FLASH_CR_PG
;
754 *(__IO
uint8_t*)Address
= Data
;
756 /* Data synchronous Barrier (DSB) Just after the write operation
757 This will force the CPU to respect the sequence of instruction (no optimization).*/
762 * @brief Set the specific FLASH error flag.
765 static void FLASH_SetErrorCode(void)
767 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR
) != RESET
)
769 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_OPERATION
;
772 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR
) != RESET
)
774 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_WRP
;
777 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR
) != RESET
)
779 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_PGA
;
782 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR
) != RESET
)
784 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_PGP
;
787 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ERSERR
) != RESET
)
789 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_ERS
;
792 #if defined (FLASH_OPTCR2_PCROP)
793 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR
) != RESET
)
795 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_RD
;
797 #endif /* FLASH_OPTCR2_PCROP */
799 /* Clear error programming flags */
800 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS
);
807 #endif /* HAL_FLASH_MODULE_ENABLED */
817 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/