2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_flash.c
\r
4 * @author MCD Application Team
\r
6 * @date 24-March-2015
\r
7 * @brief FLASH HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the internal FLASH memory:
\r
10 * + Program operations functions
\r
11 * + Memory Control functions
\r
12 * + Peripheral Errors functions
\r
15 ==============================================================================
\r
16 ##### FLASH peripheral features #####
\r
17 ==============================================================================
\r
19 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
\r
20 to the Flash memory. It implements the erase and program Flash memory operations
\r
21 and the read and write protection mechanisms.
\r
23 [..] The Flash memory interface accelerates code execution with a system of instruction
\r
24 prefetch and cache lines.
\r
26 [..] The FLASH main features are:
\r
27 (+) Flash memory read operations
\r
28 (+) Flash memory program/erase operations
\r
29 (+) Read / write protections
\r
30 (+) Prefetch on I-Code
\r
31 (+) 64 cache lines of 128 bits on I-Code
\r
32 (+) 8 cache lines of 128 bits on D-Code
\r
34 ##### How to use this driver #####
\r
35 ==============================================================================
\r
37 This driver provides functions and macros to configure and program the FLASH
\r
38 memory of all STM32F7xx devices.
\r
40 (#) FLASH Memory IO Programming functions:
\r
41 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
\r
42 HAL_FLASH_Lock() functions
\r
43 (++) Program functions: byte, half word, word and double word
\r
44 (++) There Two modes of programming :
\r
45 (+++) Polling mode using HAL_FLASH_Program() function
\r
46 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
\r
48 (#) Interrupts and flags management functions :
\r
49 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
\r
50 (++) Wait for last FLASH operation according to its status
\r
51 (++) Get error flag status by calling HAL_SetErrorCode()
\r
53 In addition to these functions, this driver includes a set of macros allowing
\r
54 to handle the following operations:
\r
56 (+) Enable/Disable the prefetch buffer
\r
57 (+) Enable/Disable the Instruction cache and the Data cache
\r
58 (+) Reset the Instruction cache and the Data cache
\r
59 (+) Enable/Disable the FLASH interrupts
\r
60 (+) Monitor the FLASH flags status
\r
62 (@) For any Flash memory program operation (erase or program), the CPU clock frequency
\r
63 (HCLK) must be at least 1MHz.
\r
64 (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
\r
65 a Flash memory operation.
\r
66 (@) Any attempt to read the Flash memory while it is being written or erased, causes the
\r
67 bus to stall. Read operations are processed correctly once the program operation has
\r
68 completed. This means that code or data fetches cannot be performed while a write/erase
\r
69 operation is ongoing.
\r
72 ******************************************************************************
\r
75 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
77 * Redistribution and use in source and binary forms, with or without modification,
\r
78 * are permitted provided that the following conditions are met:
\r
79 * 1. Redistributions of source code must retain the above copyright notice,
\r
80 * this list of conditions and the following disclaimer.
\r
81 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
82 * this list of conditions and the following disclaimer in the documentation
\r
83 * and/or other materials provided with the distribution.
\r
84 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
85 * may be used to endorse or promote products derived from this software
\r
86 * without specific prior written permission.
\r
88 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
89 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
91 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
94 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
95 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
96 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
97 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
99 ******************************************************************************
\r
102 /* Includes ------------------------------------------------------------------*/
\r
103 #include "stm32f7xx_hal.h"
\r
105 /** @addtogroup STM32F7xx_HAL_Driver
\r
109 /** @defgroup FLASH FLASH
\r
110 * @brief FLASH HAL module driver
\r
114 #ifdef HAL_FLASH_MODULE_ENABLED
\r
116 /* Private typedef -----------------------------------------------------------*/
\r
117 /* Private define ------------------------------------------------------------*/
\r
118 /** @addtogroup FLASH_Private_Constants
\r
121 #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
\r
122 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
\r
126 /* Private macro -------------------------------------------------------------*/
\r
127 /* Private variables ---------------------------------------------------------*/
\r
128 /** @addtogroup FLASH_Private_Variables
\r
131 /* Variable used for Erase sectors under interruption */
\r
132 FLASH_ProcessTypeDef pFlash;
\r
137 /* Private function prototypes -----------------------------------------------*/
\r
138 /** @addtogroup FLASH_Private_Functions
\r
141 /* Program operations */
\r
142 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
\r
143 static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
\r
144 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
\r
145 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
\r
146 static void FLASH_SetErrorCode(void);
\r
148 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
\r
153 /* Exported functions --------------------------------------------------------*/
\r
154 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
\r
158 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
\r
159 * @brief Programming operation functions
\r
162 ===============================================================================
\r
163 ##### Programming operation functions #####
\r
164 ===============================================================================
\r
166 This subsection provides a set of functions allowing to manage the FLASH
\r
167 program operations.
\r
174 * @brief Program byte, halfword, word or double word at a specified address
\r
175 * @param TypeProgram: Indicate the way to program at a specified address.
\r
176 * This parameter can be a value of @ref FLASH_Type_Program
\r
177 * @param Address: specifies the address to be programmed.
\r
178 * @param Data: specifies the data to be programmed
\r
180 * @retval HAL_StatusTypeDef HAL Status
\r
182 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
\r
184 HAL_StatusTypeDef status = HAL_ERROR;
\r
186 /* Process Locked */
\r
187 __HAL_LOCK(&pFlash);
\r
189 /* Check the parameters */
\r
190 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
\r
192 /* Wait for last operation to be completed */
\r
193 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
\r
195 if(status == HAL_OK)
\r
197 switch(TypeProgram)
\r
199 case FLASH_TYPEPROGRAM_BYTE :
\r
201 /*Program byte (8-bit) at a specified address.*/
\r
202 FLASH_Program_Byte(Address, (uint8_t) Data);
\r
206 case FLASH_TYPEPROGRAM_HALFWORD :
\r
208 /*Program halfword (16-bit) at a specified address.*/
\r
209 FLASH_Program_HalfWord(Address, (uint16_t) Data);
\r
213 case FLASH_TYPEPROGRAM_WORD :
\r
215 /*Program word (32-bit) at a specified address.*/
\r
216 FLASH_Program_Word(Address, (uint32_t) Data);
\r
220 case FLASH_TYPEPROGRAM_DOUBLEWORD :
\r
222 /*Program double word (64-bit) at a specified address.*/
\r
223 FLASH_Program_DoubleWord(Address, Data);
\r
229 /* Wait for last operation to be completed */
\r
230 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
\r
232 /* If the program operation is completed, disable the PG Bit */
\r
233 FLASH->CR &= (~FLASH_CR_PG);
\r
236 /* Process Unlocked */
\r
237 __HAL_UNLOCK(&pFlash);
\r
243 * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
\r
244 * @param TypeProgram: Indicate the way to program at a specified address.
\r
245 * This parameter can be a value of @ref FLASH_Type_Program
\r
246 * @param Address: specifies the address to be programmed.
\r
247 * @param Data: specifies the data to be programmed
\r
249 * @retval HAL Status
\r
251 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
\r
253 HAL_StatusTypeDef status = HAL_OK;
\r
255 /* Process Locked */
\r
256 __HAL_LOCK(&pFlash);
\r
258 /* Check the parameters */
\r
259 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
\r
261 /* Enable End of FLASH Operation interrupt */
\r
262 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
\r
264 /* Enable Error source interrupt */
\r
265 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
\r
267 /* Clear pending flags (if any) */
\r
268 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
\r
269 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_ERSERR);
\r
271 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
\r
272 pFlash.Address = Address;
\r
274 switch(TypeProgram)
\r
276 case FLASH_TYPEPROGRAM_BYTE :
\r
278 /*Program byte (8-bit) at a specified address.*/
\r
279 FLASH_Program_Byte(Address, (uint8_t) Data);
\r
283 case FLASH_TYPEPROGRAM_HALFWORD :
\r
285 /*Program halfword (16-bit) at a specified address.*/
\r
286 FLASH_Program_HalfWord(Address, (uint16_t) Data);
\r
290 case FLASH_TYPEPROGRAM_WORD :
\r
292 /*Program word (32-bit) at a specified address.*/
\r
293 FLASH_Program_Word(Address, (uint32_t) Data);
\r
297 case FLASH_TYPEPROGRAM_DOUBLEWORD :
\r
299 /*Program double word (64-bit) at a specified address.*/
\r
300 FLASH_Program_DoubleWord(Address, Data);
\r
310 * @brief This function handles FLASH interrupt request.
\r
314 void HAL_FLASH_IRQHandler(void)
\r
318 /* If the program operation is completed, disable the PG Bit */
\r
319 FLASH->CR &= (~FLASH_CR_PG);
\r
321 /* If the erase operation is completed, disable the SER Bit */
\r
322 FLASH->CR &= (~FLASH_CR_SER);
\r
323 FLASH->CR &= SECTOR_MASK;
\r
325 /* if the erase operation is completed, disable the MER Bit */
\r
326 FLASH->CR &= (~FLASH_MER_BIT);
\r
328 /* Check FLASH End of Operation flag */
\r
329 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
\r
331 switch (pFlash.ProcedureOnGoing)
\r
333 case FLASH_PROC_SECTERASE :
\r
335 /* Nb of sector to erased can be decreased */
\r
336 pFlash.NbSectorsToErase--;
\r
338 /* Check if there are still sectors to erase */
\r
339 if(pFlash.NbSectorsToErase != 0)
\r
341 temp = pFlash.Sector;
\r
342 /* Indicate user which sector has been erased */
\r
343 HAL_FLASH_EndOfOperationCallback(temp);
\r
345 /* Clear pending flags (if any) */
\r
346 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
\r
348 /* Increment sector number */
\r
349 temp = ++pFlash.Sector;
\r
350 FLASH_Erase_Sector(temp, pFlash.VoltageForErase);
\r
354 /* No more sectors to Erase, user callback can be called.*/
\r
355 /* Reset Sector and stop Erase sectors procedure */
\r
356 pFlash.Sector = temp = 0xFFFFFFFF;
\r
357 /* FLASH EOP interrupt user callback */
\r
358 HAL_FLASH_EndOfOperationCallback(temp);
\r
359 /* Sector Erase procedure is completed */
\r
360 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
361 /* Clear FLASH End of Operation pending bit */
\r
362 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
\r
367 case FLASH_PROC_MASSERASE :
\r
369 /* MassErase ended. Return the selected bank : in this product we don't have Banks */
\r
370 /* FLASH EOP interrupt user callback */
\r
371 HAL_FLASH_EndOfOperationCallback(0);
\r
372 /* MAss Erase procedure is completed */
\r
373 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
374 /* Clear FLASH End of Operation pending bit */
\r
375 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
\r
379 case FLASH_PROC_PROGRAM :
\r
381 /*Program ended. Return the selected address*/
\r
382 /* FLASH EOP interrupt user callback */
\r
383 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
\r
384 /* Programming procedure is completed */
\r
385 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
386 /* Clear FLASH End of Operation pending bit */
\r
387 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
\r
395 /* Check FLASH operation error flags */
\r
396 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR )) != RESET)
\r
398 switch (pFlash.ProcedureOnGoing)
\r
400 case FLASH_PROC_SECTERASE :
\r
402 /* return the faulty sector */
\r
403 temp = pFlash.Sector;
\r
404 pFlash.Sector = 0xFFFFFFFF;
\r
407 case FLASH_PROC_MASSERASE :
\r
409 /* No return in case of Mass Erase */
\r
413 case FLASH_PROC_PROGRAM :
\r
415 /*return the faulty address*/
\r
416 temp = pFlash.Address;
\r
422 /*Save the Error code*/
\r
423 FLASH_SetErrorCode();
\r
425 /* FLASH error interrupt user callback */
\r
426 HAL_FLASH_OperationErrorCallback(temp);
\r
427 /* Clear FLASH error pending bits */
\r
428 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR );
\r
430 /*Stop the procedure ongoing */
\r
431 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
434 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
\r
436 /* Disable End of FLASH Operation interrupt */
\r
437 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
\r
439 /* Disable Error source interrupt */
\r
440 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
\r
442 /* Process Unlocked */
\r
443 __HAL_UNLOCK(&pFlash);
\r
449 * @brief FLASH end of operation interrupt callback
\r
450 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
\r
451 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
\r
452 * all the selected sectors have been erased)
\r
453 * - Program : Address which was selected for data program
\r
454 * - Mass Erase : No return value expected
\r
457 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
\r
459 /* NOTE : This function Should not be modified, when the callback is needed,
\r
460 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
\r
465 * @brief FLASH operation error interrupt callback
\r
466 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
\r
467 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
\r
468 * all the selected sectors have been erased)
\r
469 * - Program : Address which was selected for data program
\r
470 * - Mass Erase : No return value expected
\r
473 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
\r
475 /* NOTE : This function Should not be modified, when the callback is needed,
\r
476 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
\r
484 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
\r
485 * @brief management functions
\r
488 ===============================================================================
\r
489 ##### Peripheral Control functions #####
\r
490 ===============================================================================
\r
492 This subsection provides a set of functions allowing to control the FLASH
\r
500 * @brief Unlock the FLASH control register access
\r
501 * @retval HAL Status
\r
503 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
\r
505 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
\r
507 /* Authorize the FLASH Registers access */
\r
508 FLASH->KEYR = FLASH_KEY1;
\r
509 FLASH->KEYR = FLASH_KEY2;
\r
520 * @brief Locks the FLASH control register access
\r
521 * @retval HAL Status
\r
523 HAL_StatusTypeDef HAL_FLASH_Lock(void)
\r
525 /* Set the LOCK Bit to lock the FLASH Registers access */
\r
526 FLASH->CR |= FLASH_CR_LOCK;
\r
532 * @brief Unlock the FLASH Option Control Registers access.
\r
533 * @retval HAL Status
\r
535 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
\r
537 if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
\r
539 /* Authorizes the Option Byte register programming */
\r
540 FLASH->OPTKEYR = FLASH_OPT_KEY1;
\r
541 FLASH->OPTKEYR = FLASH_OPT_KEY2;
\r
552 * @brief Lock the FLASH Option Control Registers access.
\r
553 * @retval HAL Status
\r
555 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
\r
557 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
\r
558 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
\r
564 * @brief Launch the option byte loading.
\r
565 * @retval HAL Status
\r
567 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
\r
569 /* Set the OPTSTRT bit in OPTCR register */
\r
570 FLASH->OPTCR |= FLASH_OPTCR_OPTSTRT;
\r
572 /* Wait for last operation to be completed */
\r
573 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
\r
580 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
\r
581 * @brief Peripheral Errors functions
\r
584 ===============================================================================
\r
585 ##### Peripheral Errors functions #####
\r
586 ===============================================================================
\r
588 This subsection permits to get in run-time Errors of the FLASH peripheral.
\r
595 * @brief Get the specific FLASH error flag.
\r
597 * @retval FLASH_ErrorCode: The returned value can be:
\r
598 * @arg FLASH_ERROR_ERS: FLASH Erasing Sequence error flag
\r
599 * @arg FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
\r
600 * @arg FLASH_ERROR_PGA: FLASH Programming Alignment error flag
\r
601 * @arg FLASH_ERROR_WRP: FLASH Write protected error flag
\r
602 * @arg FLASH_ERROR_OPERATION: FLASH operation Error flag
\r
604 uint32_t HAL_FLASH_GetError(void)
\r
606 return pFlash.ErrorCode;
\r
614 * @brief Wait for a FLASH operation to complete.
\r
615 * @param Timeout: maximum flash operationtimeout
\r
616 * @retval HAL Status
\r
618 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
\r
620 uint32_t tickstart = 0;
\r
622 /* Clear Error Code */
\r
623 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
\r
625 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
\r
626 Even if the FLASH operation fails, the BUSY flag will be reset and an error
\r
627 flag will be set */
\r
629 tickstart = HAL_GetTick();
\r
631 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
\r
633 if(Timeout != HAL_MAX_DELAY)
\r
635 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
\r
637 return HAL_TIMEOUT;
\r
642 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
\r
643 FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR )) != RESET)
\r
645 /*Save the error code*/
\r
646 FLASH_SetErrorCode();
\r
650 /* If there is an error flag set */
\r
656 * @brief Program a double word (64-bit) at a specified address.
\r
657 * @note This function must be used when the device voltage range is from
\r
658 * 2.7V to 3.6V and an External Vpp is present.
\r
660 * @note If an erase and a program operations are requested simultaneously,
\r
661 * the erase operation is performed before the program one.
\r
663 * @param Address: specifies the address to be programmed.
\r
664 * @param Data: specifies the data to be programmed.
\r
667 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
\r
669 /* Check the parameters */
\r
670 assert_param(IS_FLASH_ADDRESS(Address));
\r
672 /* If the previous operation is completed, proceed to program the new data */
\r
673 FLASH->CR &= CR_PSIZE_MASK;
\r
674 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
\r
675 FLASH->CR |= FLASH_CR_PG;
\r
677 *(__IO uint64_t*)Address = Data;
\r
679 /* Data synchronous Barrier (DSB) Just after the write operation
\r
680 This will force the CPU to respect the sequence of instruction (no optimization).*/
\r
686 * @brief Program word (32-bit) at a specified address.
\r
687 * @note This function must be used when the device voltage range is from
\r
690 * @note If an erase and a program operations are requested simultaneously,
\r
691 * the erase operation is performed before the program one.
\r
693 * @param Address: specifies the address to be programmed.
\r
694 * @param Data: specifies the data to be programmed.
\r
697 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
\r
699 /* Check the parameters */
\r
700 assert_param(IS_FLASH_ADDRESS(Address));
\r
702 /* If the previous operation is completed, proceed to program the new data */
\r
703 FLASH->CR &= CR_PSIZE_MASK;
\r
704 FLASH->CR |= FLASH_PSIZE_WORD;
\r
705 FLASH->CR |= FLASH_CR_PG;
\r
707 *(__IO uint32_t*)Address = Data;
\r
709 /* Data synchronous Barrier (DSB) Just after the write operation
\r
710 This will force the CPU to respect the sequence of instruction (no optimization).*/
\r
715 * @brief Program a half-word (16-bit) at a specified address.
\r
716 * @note This function must be used when the device voltage range is from
\r
719 * @note If an erase and a program operations are requested simultaneously,
\r
720 * the erase operation is performed before the program one.
\r
722 * @param Address: specifies the address to be programmed.
\r
723 * @param Data: specifies the data to be programmed.
\r
726 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
\r
728 /* Check the parameters */
\r
729 assert_param(IS_FLASH_ADDRESS(Address));
\r
731 /* If the previous operation is completed, proceed to program the new data */
\r
732 FLASH->CR &= CR_PSIZE_MASK;
\r
733 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
\r
734 FLASH->CR |= FLASH_CR_PG;
\r
736 *(__IO uint16_t*)Address = Data;
\r
738 /* Data synchronous Barrier (DSB) Just after the write operation
\r
739 This will force the CPU to respect the sequence of instruction (no optimization).*/
\r
745 * @brief Program byte (8-bit) at a specified address.
\r
746 * @note This function must be used when the device voltage range is from
\r
749 * @note If an erase and a program operations are requested simultaneously,
\r
750 * the erase operation is performed before the program one.
\r
752 * @param Address: specifies the address to be programmed.
\r
753 * @param Data: specifies the data to be programmed.
\r
756 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
\r
758 /* Check the parameters */
\r
759 assert_param(IS_FLASH_ADDRESS(Address));
\r
761 /* If the previous operation is completed, proceed to program the new data */
\r
762 FLASH->CR &= CR_PSIZE_MASK;
\r
763 FLASH->CR |= FLASH_PSIZE_BYTE;
\r
764 FLASH->CR |= FLASH_CR_PG;
\r
766 *(__IO uint8_t*)Address = Data;
\r
768 /* Data synchronous Barrier (DSB) Just after the write operation
\r
769 This will force the CPU to respect the sequence of instruction (no optimization).*/
\r
774 * @brief Set the specific FLASH error flag.
\r
778 static void FLASH_SetErrorCode(void)
\r
780 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
\r
782 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
\r
785 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
\r
787 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
\r
790 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
\r
792 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
\r
795 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ERSERR) != RESET)
\r
797 pFlash.ErrorCode |= HAL_FLASH_ERROR_ERS;
\r
800 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
\r
802 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
\r
810 #endif /* HAL_FLASH_MODULE_ENABLED */
\r
820 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r