Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_flash.c
blobfd65a45cc72bfb6ed359768259ec69a7754fc451
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_flash.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-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 Errors functions
14 @verbatim
15 ==============================================================================
16 ##### FLASH peripheral features #####
17 ==============================================================================
19 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
20 to the Flash memory. It implements the erase and program Flash memory operations
21 and the read and write protection mechanisms.
23 [..] The Flash memory interface accelerates code execution with a system of instruction
24 prefetch and cache lines.
26 [..] The FLASH main features are:
27 (+) Flash memory read operations
28 (+) Flash memory program/erase operations
29 (+) Read / write protections
30 (+) Prefetch on I-Code
31 (+) 64 cache lines of 128 bits on I-Code
32 (+) 8 cache lines of 128 bits on D-Code
34 ##### How to use this driver #####
35 ==============================================================================
36 [..]
37 This driver provides functions and macros to configure and program the FLASH
38 memory of all STM32F7xx devices.
40 (#) FLASH Memory IO Programming functions:
41 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
42 HAL_FLASH_Lock() functions
43 (++) Program functions: byte, half word, word and double word
44 (++) There Two modes of programming :
45 (+++) Polling mode using HAL_FLASH_Program() function
46 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
48 (#) Interrupts and flags management functions :
49 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
50 (++) Wait for last FLASH operation according to its status
51 (++) Get error flag status by calling HAL_SetErrorCode()
52 [..]
53 In addition to these functions, this driver includes a set of macros allowing
54 to handle the following operations:
55 (+) Set the latency
56 (+) Enable/Disable the prefetch buffer
57 (+) Enable/Disable the Instruction cache and the Data cache
58 (+) Reset the Instruction cache and the Data cache
59 (+) Enable/Disable the FLASH interrupts
60 (+) Monitor the FLASH flags status
61 [..]
62 (@) For any Flash memory program operation (erase or program), the CPU clock frequency
63 (HCLK) must be at least 1MHz.
64 (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
65 a Flash memory operation.
66 (@) Any attempt to read the Flash memory while it is being written or erased, causes the
67 bus to stall. Read operations are processed correctly once the program operation has
68 completed. This means that code or data fetches cannot be performed while a write/erase
69 operation is ongoing.
71 @endverbatim
72 ******************************************************************************
73 * @attention
75 * <h2><center>&copy; COPYRIGHT(c) 2017 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 "stm32f7xx_hal.h"
105 /** @addtogroup STM32F7xx_HAL_Driver
106 * @{
109 /** @defgroup FLASH FLASH
110 * @brief FLASH HAL module driver
111 * @{
114 #ifdef HAL_FLASH_MODULE_ENABLED
116 /* Private typedef -----------------------------------------------------------*/
117 /* Private define ------------------------------------------------------------*/
118 /** @addtogroup FLASH_Private_Constants
119 * @{
121 #define SECTOR_MASK ((uint32_t)0xFFFFFF07U)
122 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000U)/* 50 s */
124 * @}
126 /* Private macro -------------------------------------------------------------*/
127 /* Private variables ---------------------------------------------------------*/
128 /** @addtogroup FLASH_Private_Variables
129 * @{
131 /* Variable used for Erase sectors under interruption */
132 FLASH_ProcessTypeDef pFlash;
134 * @}
137 /* Private function prototypes -----------------------------------------------*/
138 /** @addtogroup FLASH_Private_Functions
139 * @{
141 /* Program operations */
142 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
143 static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
144 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
145 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
146 static void FLASH_SetErrorCode(void);
148 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
150 * @}
153 /* Exported functions --------------------------------------------------------*/
154 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
155 * @{
158 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
159 * @brief Programming operation functions
161 @verbatim
162 ===============================================================================
163 ##### Programming operation functions #####
164 ===============================================================================
165 [..]
166 This subsection provides a set of functions allowing to manage the FLASH
167 program operations.
169 @endverbatim
170 * @{
174 * @brief Program byte, halfword, word or double word at a specified address
175 * @param TypeProgram: Indicate the way to program at a specified address.
176 * This parameter can be a value of @ref FLASH_Type_Program
177 * @param Address: specifies the address to be programmed.
178 * @param Data: specifies the data to be programmed
180 * @retval HAL_StatusTypeDef HAL Status
182 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
184 HAL_StatusTypeDef status = HAL_ERROR;
186 /* Process Locked */
187 __HAL_LOCK(&pFlash);
189 /* Check the parameters */
190 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
192 /* Wait for last operation to be completed */
193 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
195 if(status == HAL_OK)
197 switch(TypeProgram)
199 case FLASH_TYPEPROGRAM_BYTE :
201 /*Program byte (8-bit) at a specified address.*/
202 FLASH_Program_Byte(Address, (uint8_t) Data);
203 break;
206 case FLASH_TYPEPROGRAM_HALFWORD :
208 /*Program halfword (16-bit) at a specified address.*/
209 FLASH_Program_HalfWord(Address, (uint16_t) Data);
210 break;
213 case FLASH_TYPEPROGRAM_WORD :
215 /*Program word (32-bit) at a specified address.*/
216 FLASH_Program_Word(Address, (uint32_t) Data);
217 break;
220 case FLASH_TYPEPROGRAM_DOUBLEWORD :
222 /*Program double word (64-bit) at a specified address.*/
223 FLASH_Program_DoubleWord(Address, Data);
224 break;
226 default :
227 break;
229 /* Wait for last operation to be completed */
230 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
232 /* If the program operation is completed, disable the PG Bit */
233 FLASH->CR &= (~FLASH_CR_PG);
236 /* Process Unlocked */
237 __HAL_UNLOCK(&pFlash);
239 return status;
243 * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
244 * @param TypeProgram: Indicate the way to program at a specified address.
245 * This parameter can be a value of @ref FLASH_Type_Program
246 * @param Address: specifies the address to be programmed.
247 * @param Data: specifies the data to be programmed
249 * @retval HAL Status
251 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
253 HAL_StatusTypeDef status = HAL_OK;
255 /* Process Locked */
256 __HAL_LOCK(&pFlash);
258 /* Check the parameters */
259 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
261 /* Enable End of FLASH Operation interrupt */
262 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
264 /* Enable Error source interrupt */
265 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
267 /* Clear pending flags (if any) */
268 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
269 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_ERSERR);
271 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
272 pFlash.Address = Address;
274 switch(TypeProgram)
276 case FLASH_TYPEPROGRAM_BYTE :
278 /*Program byte (8-bit) at a specified address.*/
279 FLASH_Program_Byte(Address, (uint8_t) Data);
280 break;
283 case FLASH_TYPEPROGRAM_HALFWORD :
285 /*Program halfword (16-bit) at a specified address.*/
286 FLASH_Program_HalfWord(Address, (uint16_t) Data);
287 break;
290 case FLASH_TYPEPROGRAM_WORD :
292 /*Program word (32-bit) at a specified address.*/
293 FLASH_Program_Word(Address, (uint32_t) Data);
294 break;
297 case FLASH_TYPEPROGRAM_DOUBLEWORD :
299 /*Program double word (64-bit) at a specified address.*/
300 FLASH_Program_DoubleWord(Address, Data);
301 break;
303 default :
304 break;
306 return status;
310 * @brief This function handles FLASH interrupt request.
311 * @retval None
313 void HAL_FLASH_IRQHandler(void)
315 uint32_t temp = 0;
317 /* If the program operation is completed, disable the PG Bit */
318 FLASH->CR &= (~FLASH_CR_PG);
320 /* If the erase operation is completed, disable the SER Bit */
321 FLASH->CR &= (~FLASH_CR_SER);
322 FLASH->CR &= SECTOR_MASK;
324 /* if the erase operation is completed, disable the MER Bit */
325 FLASH->CR &= (~FLASH_MER_BIT);
327 /* Check FLASH End of Operation flag */
328 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
330 /* Clear FLASH End of Operation pending bit */
331 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
333 switch (pFlash.ProcedureOnGoing)
335 case FLASH_PROC_SECTERASE :
337 /* Nb of sector to erased can be decreased */
338 pFlash.NbSectorsToErase--;
340 /* Check if there are still sectors to erase */
341 if(pFlash.NbSectorsToErase != 0)
343 temp = pFlash.Sector;
344 /* Indicate user which sector has been erased */
345 HAL_FLASH_EndOfOperationCallback(temp);
347 /* Increment sector number */
348 temp = ++pFlash.Sector;
349 FLASH_Erase_Sector(temp, pFlash.VoltageForErase);
351 else
353 /* No more sectors to Erase, user callback can be called.*/
354 /* Reset Sector and stop Erase sectors procedure */
355 pFlash.Sector = temp = 0xFFFFFFFFU;
356 /* FLASH EOP interrupt user callback */
357 HAL_FLASH_EndOfOperationCallback(temp);
358 /* Sector Erase procedure is completed */
359 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
361 break;
364 case FLASH_PROC_MASSERASE :
366 /* MassErase ended. Return the selected bank : in this product we don't have Banks */
367 /* FLASH EOP interrupt user callback */
368 HAL_FLASH_EndOfOperationCallback(0);
369 /* MAss Erase procedure is completed */
370 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
371 break;
374 case FLASH_PROC_PROGRAM :
376 /*Program ended. Return the selected address*/
377 /* FLASH EOP interrupt user callback */
378 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
379 /* Programming procedure is completed */
380 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
381 break;
383 default :
384 break;
388 /* Check FLASH operation error flags */
389 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ALL_ERRORS) != RESET)
391 switch (pFlash.ProcedureOnGoing)
393 case FLASH_PROC_SECTERASE :
395 /* return the faulty sector */
396 temp = pFlash.Sector;
397 pFlash.Sector = 0xFFFFFFFFU;
398 break;
400 case FLASH_PROC_MASSERASE :
402 /* No return in case of Mass Erase */
403 temp = 0;
404 break;
406 case FLASH_PROC_PROGRAM :
408 /*return the faulty address*/
409 temp = pFlash.Address;
410 break;
412 default :
413 break;
415 /*Save the Error code*/
416 FLASH_SetErrorCode();
418 /* FLASH error interrupt user callback */
419 HAL_FLASH_OperationErrorCallback(temp);
421 /*Stop the procedure ongoing */
422 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
425 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
427 /* Disable End of FLASH Operation interrupt */
428 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
430 /* Disable Error source interrupt */
431 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
433 /* Process Unlocked */
434 __HAL_UNLOCK(&pFlash);
440 * @brief FLASH end of operation 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
446 * @retval None
448 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
450 /* Prevent unused argument(s) compilation warning */
451 UNUSED(ReturnValue);
452 /* NOTE : This function Should not be modified, when the callback is needed,
453 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
458 * @brief FLASH operation error interrupt callback
459 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
460 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
461 * all the selected sectors have been erased)
462 * - Program : Address which was selected for data program
463 * - Mass Erase : No return value expected
464 * @retval None
466 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
468 /* Prevent unused argument(s) compilation warning */
469 UNUSED(ReturnValue);
470 /* NOTE : This function Should not be modified, when the callback is needed,
471 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
476 * @}
479 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
480 * @brief management functions
482 @verbatim
483 ===============================================================================
484 ##### Peripheral Control functions #####
485 ===============================================================================
486 [..]
487 This subsection provides a set of functions allowing to control the FLASH
488 memory operations.
490 @endverbatim
491 * @{
495 * @brief Unlock the FLASH control register access
496 * @retval HAL Status
498 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
500 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
502 /* Authorize the FLASH Registers access */
503 FLASH->KEYR = FLASH_KEY1;
504 FLASH->KEYR = FLASH_KEY2;
506 else
508 return HAL_ERROR;
511 return HAL_OK;
515 * @brief Locks the FLASH control register access
516 * @retval HAL Status
518 HAL_StatusTypeDef HAL_FLASH_Lock(void)
520 /* Set the LOCK Bit to lock the FLASH Registers access */
521 FLASH->CR |= FLASH_CR_LOCK;
523 return HAL_OK;
527 * @brief Unlock the FLASH Option Control Registers access.
528 * @retval HAL Status
530 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
532 if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
534 /* Authorizes the Option Byte register programming */
535 FLASH->OPTKEYR = FLASH_OPT_KEY1;
536 FLASH->OPTKEYR = FLASH_OPT_KEY2;
538 else
540 return HAL_ERROR;
543 return HAL_OK;
547 * @brief Lock the FLASH Option Control Registers access.
548 * @retval HAL Status
550 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
552 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
553 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
555 return HAL_OK;
559 * @brief Launch the option byte loading.
560 * @retval HAL Status
562 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
564 /* Set the OPTSTRT bit in OPTCR register */
565 FLASH->OPTCR |= FLASH_OPTCR_OPTSTRT;
567 /* Wait for last operation to be completed */
568 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
572 * @}
575 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
576 * @brief Peripheral Errors functions
578 @verbatim
579 ===============================================================================
580 ##### Peripheral Errors functions #####
581 ===============================================================================
582 [..]
583 This subsection permits to get in run-time Errors of the FLASH peripheral.
585 @endverbatim
586 * @{
590 * @brief Get the specific FLASH error flag.
591 * @retval FLASH_ErrorCode: The returned value can be:
592 * @arg FLASH_ERROR_ERS: FLASH Erasing Sequence error flag
593 * @arg FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
594 * @arg FLASH_ERROR_PGA: FLASH Programming Alignment error flag
595 * @arg FLASH_ERROR_WRP: FLASH Write protected error flag
596 * @arg FLASH_ERROR_OPERATION: FLASH operation Error flag
598 uint32_t HAL_FLASH_GetError(void)
600 return pFlash.ErrorCode;
604 * @}
608 * @brief Wait for a FLASH operation to complete.
609 * @param Timeout: maximum flash operationtimeout
610 * @retval HAL Status
612 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
614 uint32_t tickstart = 0;
616 /* Clear Error Code */
617 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
619 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
620 Even if the FLASH operation fails, the BUSY flag will be reset and an error
621 flag will be set */
622 /* Get tick */
623 tickstart = HAL_GetTick();
625 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
627 if(Timeout != HAL_MAX_DELAY)
629 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
631 return HAL_TIMEOUT;
636 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ALL_ERRORS) != RESET)
638 /*Save the error code*/
639 FLASH_SetErrorCode();
640 return HAL_ERROR;
643 /* Check FLASH End of Operation flag */
644 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
646 /* Clear FLASH End of Operation pending bit */
647 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
650 /* If there is an error flag set */
651 return HAL_OK;
656 * @brief Program a double word (64-bit) at a specified address.
657 * @note This function must be used when the device voltage range is from
658 * 2.7V to 3.6V and an External Vpp is present.
660 * @note If an erase and a program operations are requested simultaneously,
661 * the erase operation is performed before the program one.
663 * @param Address: specifies the address to be programmed.
664 * @param Data: specifies the data to be programmed.
665 * @retval None
667 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
669 /* Check the parameters */
670 assert_param(IS_FLASH_ADDRESS(Address));
672 /* If the previous operation is completed, proceed to program the new data */
673 FLASH->CR &= CR_PSIZE_MASK;
674 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
675 FLASH->CR |= FLASH_CR_PG;
677 *(__IO uint64_t*)Address = Data;
679 /* Data synchronous Barrier (DSB) Just after the write operation
680 This will force the CPU to respect the sequence of instruction (no optimization).*/
681 __DSB();
686 * @brief Program word (32-bit) at a specified address.
687 * @note This function must be used when the device voltage range is from
688 * 2.7V to 3.6V.
690 * @note If an erase and a program operations are requested simultaneously,
691 * the erase operation is performed before the program one.
693 * @param Address: specifies the address to be programmed.
694 * @param Data: specifies the data to be programmed.
695 * @retval None
697 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
699 /* Check the parameters */
700 assert_param(IS_FLASH_ADDRESS(Address));
702 /* If the previous operation is completed, proceed to program the new data */
703 FLASH->CR &= CR_PSIZE_MASK;
704 FLASH->CR |= FLASH_PSIZE_WORD;
705 FLASH->CR |= FLASH_CR_PG;
707 *(__IO uint32_t*)Address = Data;
709 /* Data synchronous Barrier (DSB) Just after the write operation
710 This will force the CPU to respect the sequence of instruction (no optimization).*/
711 __DSB();
715 * @brief Program a half-word (16-bit) at a specified address.
716 * @note This function must be used when the device voltage range is from
717 * 2.7V to 3.6V.
719 * @note If an erase and a program operations are requested simultaneously,
720 * the erase operation is performed before the program one.
722 * @param Address: specifies the address to be programmed.
723 * @param Data: specifies the data to be programmed.
724 * @retval None
726 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
728 /* Check the parameters */
729 assert_param(IS_FLASH_ADDRESS(Address));
731 /* If the previous operation is completed, proceed to program the new data */
732 FLASH->CR &= CR_PSIZE_MASK;
733 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
734 FLASH->CR |= FLASH_CR_PG;
736 *(__IO uint16_t*)Address = Data;
738 /* Data synchronous Barrier (DSB) Just after the write operation
739 This will force the CPU to respect the sequence of instruction (no optimization).*/
740 __DSB();
745 * @brief Program byte (8-bit) at a specified address.
746 * @note This function must be used when the device voltage range is from
747 * 2.7V to 3.6V.
749 * @note If an erase and a program operations are requested simultaneously,
750 * the erase operation is performed before the program one.
752 * @param Address: specifies the address to be programmed.
753 * @param Data: specifies the data to be programmed.
754 * @retval None
756 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
758 /* Check the parameters */
759 assert_param(IS_FLASH_ADDRESS(Address));
761 /* If the previous operation is completed, proceed to program the new data */
762 FLASH->CR &= CR_PSIZE_MASK;
763 FLASH->CR |= FLASH_PSIZE_BYTE;
764 FLASH->CR |= FLASH_CR_PG;
766 *(__IO uint8_t*)Address = Data;
768 /* Data synchronous Barrier (DSB) Just after the write operation
769 This will force the CPU to respect the sequence of instruction (no optimization).*/
770 __DSB();
774 * @brief Set the specific FLASH error flag.
775 * @retval None
777 static void FLASH_SetErrorCode(void)
779 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
781 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
784 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
786 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
789 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
791 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
794 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
796 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
799 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ERSERR) != RESET)
801 pFlash.ErrorCode |= HAL_FLASH_ERROR_ERS;
804 #if defined (FLASH_OPTCR2_PCROP)
805 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
807 pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
809 #endif /* FLASH_OPTCR2_PCROP */
811 /* Clear error programming flags */
812 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
816 * @}
819 #endif /* HAL_FLASH_MODULE_ENABLED */
822 * @}
826 * @}
829 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/