2 ******************************************************************************
3 * @file stm32f1xx_hal_flash.c
4 * @author MCD Application Team
6 * @date 15-December-2014
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
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
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 ==============================================================================
36 This driver provides functions and macros to configure and program the FLASH
37 memory of all STM32F1xx devices. These functions are split in 3 groups:
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
45 (#) Option Bytes Programming functions: this group includes all needed
46 functions to manage the Option Bytes:
47 (++) Lock and Unlock the Option Bytes
48 (++) Erase Option Bytes
49 (++) Set/Reset the write protection
50 (++) Set the Read protection Level
51 (++) Program the user Option Bytes
52 (++) Program the data Option Bytes
53 (++) Launch the Option Bytes loader
55 (#) Interrupts and flags management functions : this group
56 includes all needed functions to:
57 (++) Handle FLASH interrupts
58 (++) Wait for last FLASH operation according to its status
59 (++) Get error flag status
61 [..] In addition to these function, this driver includes a set of macros allowing
62 to handle the following operations:
65 (+) Enable/Disable the prefetch buffer
66 (+) Enable/Disable the half cycle access
67 (+) Enable/Disable the FLASH interrupts
68 (+) Monitor the FLASH flags status
71 ******************************************************************************
74 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
76 * Redistribution and use in source and binary forms, with or without modification,
77 * are permitted provided that the following conditions are met:
78 * 1. Redistributions of source code must retain the above copyright notice,
79 * this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright notice,
81 * this list of conditions and the following disclaimer in the documentation
82 * and/or other materials provided with the distribution.
83 * 3. Neither the name of STMicroelectronics nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
87 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
90 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
93 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
94 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
95 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
96 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
98 ******************************************************************************
101 /* Includes ------------------------------------------------------------------*/
102 #include "stm32f1xx_hal.h"
104 /** @addtogroup STM32F1xx_HAL_Driver
108 /** @defgroup FLASH FLASH
109 * @brief FLASH HAL module driver
113 #ifdef HAL_FLASH_MODULE_ENABLED
115 /* Private typedef -----------------------------------------------------------*/
116 /* Private define ------------------------------------------------------------*/
117 /** @defgroup FLASH_Private_Constants FLASH Private Constants
124 /* Private macro -------------------------------------------------------------*/
125 /** @defgroup FLASH_Private_Macros FLASH Private Macros
133 /* Private variables ---------------------------------------------------------*/
134 /** @defgroup FLASH_Private_Variables FLASH Private Variables
137 /* Variables used for Erase pages under interruption*/
138 FLASH_ProcessTypeDef pFlash;
143 /* Private function prototypes -----------------------------------------------*/
144 /** @defgroup FLASH_Private_Functions FLASH Private Functions
151 /* Exported functions ---------------------------------------------------------*/
152 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
156 /** @defgroup FLASH_Exported_Functions_Group1 Input and Output operation functions
157 * @brief Data transfers functions
160 ===============================================================================
161 ##### IO operation functions #####
162 ===============================================================================
164 This subsection provides a set of functions allowing to manage the FLASH
165 program operations (write/erase).
172 * @brief Program halfword, word or double word at a specified address
173 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
174 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
176 * @note If an erase and a program operations are requested simultaneously,
177 * the erase operation is performed before the program one.
179 * @note FLASH should be previously erased before new programmation (only exception to this
180 * is when 0x0000 is programmed)
182 * @param TypeProgram: Indicate the way to program at a specified address.
183 * This parameter can be a value of @ref FLASH_Type_Program
184 * @param Address: Specifies the address to be programmed.
185 * @param Data: Specifies the data to be programmed
187 * @retval HAL_StatusTypeDef HAL Status
189 __weak HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
191 HAL_StatusTypeDef status = HAL_ERROR;
193 uint8_t nbiterations = 0;
198 /* Check the parameters */
199 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
200 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
202 /* Wait for last operation to be completed */
203 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
207 if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
209 /* Program halfword (16-bit) at a specified address. */
212 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
214 /* Program word (32-bit = 2*16-bit) at a specified address. */
219 /* Program double word (64-bit = 4*16-bit) at a specified address. */
223 for (index = 0; index < nbiterations; index++)
225 FLASH_Program_HalfWord((Address + (2*index)), (uint16_t)(Data >> (16*index)));
227 /* Wait for last operation to be completed */
228 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
230 /* If the program operation is completed, disable the PG Bit */
231 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
233 /* In case of error, stop programation procedure */
234 if (status != HAL_OK)
241 /* Process Unlocked */
242 __HAL_UNLOCK(&pFlash);
248 * @brief Program halfword, word or double word at a specified address with interrupt enabled.
249 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
250 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
252 * @note If an erase and a program operations are requested simultaneously,
253 * the erase operation is performed before the program one.
255 * @param TypeProgram: Indicate the way to program at a specified address.
256 * This parameter can be a value of @ref FLASH_Type_Program
257 * @param Address: Specifies the address to be programmed.
258 * @param Data: Specifies the data to be programmed
260 * @retval HAL_StatusTypeDef HAL Status
262 __weak HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
264 HAL_StatusTypeDef status = HAL_OK;
269 /* Check the parameters */
270 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
271 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
273 /* Enable End of FLASH Operation and Error source interrupts */
274 __HAL_FLASH_ENABLE_IT((FLASH_IT_EOP | FLASH_IT_ERR));
276 pFlash.Address = Address;
279 if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
281 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
282 /*Program halfword (16-bit) at a specified address.*/
283 pFlash.DataRemaining = 1;
285 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
287 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
288 /*Program word (32-bit : 2*16-bit) at a specified address.*/
289 pFlash.DataRemaining = 2;
293 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
294 /*Program double word (64-bit : 4*16-bit) at a specified address.*/
295 pFlash.DataRemaining = 4;
298 /*Program halfword (16-bit) at a specified address.*/
299 FLASH_Program_HalfWord(Address, (uint16_t)Data);
305 * @brief This function handles FLASH interrupt request.
308 __weak void HAL_FLASH_IRQHandler(void)
310 uint32_t addresstmp = 0;
312 /* Check FLASH operation error flags */
313 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
315 /*Save the Error code*/
316 FLASH_SetErrorCode();
318 /* FLASH error interrupt user callback */
319 HAL_FLASH_OperationErrorCallback(pFlash.Address);
321 /* Reset address and stop the procedure ongoing*/
322 pFlash.Address = 0xFFFFFFFF;
323 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
326 /* Check FLASH End of Operation flag */
327 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
329 /* Clear FLASH End of Operation pending bit */
330 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
332 /* Process can continue only if no error detected */
333 if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
335 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
337 /* Nb of pages to erased can be decreased */
338 pFlash.DataRemaining--;
340 /* Indicate user which page address has been erased*/
341 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
343 /* Check if there are still pages to erase*/
344 if(pFlash.DataRemaining != 0)
346 /* Increment page address to next page */
347 pFlash.Address += FLASH_PAGE_SIZE;
348 addresstmp = pFlash.Address;
350 /* Operation is completed, disable the PER Bit */
351 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
353 FLASH_PageErase(addresstmp);
357 /*No more pages to Erase*/
359 /*Reset Address and stop Erase pages procedure*/
360 pFlash.Address = 0xFFFFFFFF;
361 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
364 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
366 /* Operation is completed, disable the MER Bit */
367 CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
369 /* MassErase ended. Return the selected bank*/
370 /* FLASH EOP interrupt user callback */
371 HAL_FLASH_EndOfOperationCallback(0);
373 /* Stop Mass Erase procedure*/
374 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
378 /* Nb of 16-bit data to program can be decreased */
379 pFlash.DataRemaining--;
381 /* Check if there are still 16-bit data to program */
382 if(pFlash.DataRemaining != 0)
384 /* Increment address to 16-bit */
386 addresstmp = pFlash.Address;
388 /* Shift to have next 16-bit data */
389 pFlash.Data = (pFlash.Data >> 16);
391 /* Operation is completed, disable the PG Bit */
392 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
394 /* Program halfword (16-bit) at a specified address.*/
395 FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
399 /* Program ended. Return the selected address*/
400 /* FLASH EOP interrupt user callback */
401 if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
403 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
405 else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
407 HAL_FLASH_EndOfOperationCallback(pFlash.Address-2);
411 HAL_FLASH_EndOfOperationCallback(pFlash.Address-6);
414 /* Reset Address and stop Program procedure*/
415 pFlash.Address = 0xFFFFFFFF;
416 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
422 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
424 /* Operation is completed, disable the PG, PER and MER Bits */
425 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
427 /* Disable End of FLASH Operation and Error source interrupts */
428 __HAL_FLASH_DISABLE_IT((FLASH_IT_EOP | FLASH_IT_ERR));
430 /* Process Unlocked */
431 __HAL_UNLOCK(&pFlash);
437 * @brief FLASH end of operation interrupt callback
438 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
439 * - Mass Erase: No return value expected
440 * - Pages Erase: Address of the page which has been erased
441 * - Program: Address which was selected for data program
444 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
446 /* NOTE : This function Should not be modified, when the callback is needed,
447 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
452 * @brief FLASH operation error interrupt callback
453 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
454 * - Mass Erase: No return value expected
455 * - Pages Erase: Address of the page which returned an error
456 * - Program: Address which was selected for data program
459 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
461 /* NOTE : This function Should not be modified, when the callback is needed,
462 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
470 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
471 * @brief management functions
474 ===============================================================================
475 ##### Peripheral Control functions #####
476 ===============================================================================
478 This subsection provides a set of functions allowing to control the FLASH
486 * @brief Unlock the FLASH control register access
489 __weak HAL_StatusTypeDef HAL_FLASH_Unlock(void)
491 if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
493 /* Authorize the FLASH Registers access */
494 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
495 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
506 * @brief Locks the FLASH control register access
509 __weak HAL_StatusTypeDef HAL_FLASH_Lock(void)
511 /* Set the LOCK Bit to lock the FLASH Registers access */
512 SET_BIT(FLASH->CR, FLASH_CR_LOCK);
519 * @brief Unlock the FLASH Option Control Registers access.
522 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
524 if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
526 /* Authorizes the Option Byte register programming */
527 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
528 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
539 * @brief Lock the FLASH Option Control Registers access.
542 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
544 /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
545 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
551 * @brief Launch the option byte loading.
552 * @note This function will reset automatically the MCU.
553 * @retval HAL_StatusTypeDef HAL Status
555 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
557 /* Initiates a system reset request to launch the option byte loading */
558 HAL_NVIC_SystemReset();
567 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State functions
568 * @brief Peripheral State functions
571 ===============================================================================
572 ##### Peripheral State functions #####
573 ===============================================================================
575 This subsection permit to get in run-time the status of the FLASH peripheral.
582 * @brief Get the specific FLASH error flag.
583 * @retval FLASH_ErrorCode: The returned value can be:
584 * @arg HAL_FLASH_ERROR_PROG: FLASH Programming error flag
585 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
586 * @arg HAL_FLASH_ERROR_OPTV: Option byte error
588 uint32_t HAL_FLASH_GetError(void)
590 return pFlash.ErrorCode;
600 /** @addtogroup FLASH_Private_Functions
604 * @brief Wait for a FLASH operation to complete.
605 * @param Timeout: maximum flash operationtimeout
606 * @retval HAL_StatusTypeDef HAL Status
608 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
610 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
611 Even if the FLASH operation fails, the BUSY flag will be reset and an error
614 uint32_t tickstart = HAL_GetTick();
616 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
618 if (Timeout != HAL_MAX_DELAY)
620 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
627 /* Check FLASH End of Operation flag */
628 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
630 /* Clear FLASH End of Operation pending bit */
631 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
634 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || \
635 __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
637 /*Save the error code*/
638 FLASH_SetErrorCode();
642 /* If there is no error flag set */
651 #endif /* HAL_FLASH_MODULE_ENABLED */
661 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/