2 ******************************************************************************
3 * @file stm32f4xx_flash.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the FLASH peripheral:
9 * - FLASH Interface configuration
10 * - FLASH Memory Programming
11 * - Option Bytes Programming
12 * - Interrupts and flags management
16 * ===================================================================
17 * How to use this driver
18 * ===================================================================
20 * This driver provides functions to configure and program the FLASH
21 * memory of all STM32F4xx devices.
22 * These functions are split in 4 groups:
24 * 1. FLASH Interface configuration functions: this group includes the
25 * management of the following features:
27 * - Enable/Disable the prefetch buffer
28 * - Enable/Disable the Instruction cache and the Data cache
29 * - Reset the Instruction cache and the Data cache
31 * 2. FLASH Memory Programming functions: this group includes all needed
32 * functions to erase and program the main memory:
33 * - Lock and Unlock the FLASH interface
34 * - Erase function: Erase sector, erase all sectors
35 * - Program functions: byte, half word, word and double word
37 * 3. Option Bytes Programming functions: this group includes all needed
38 * functions to manage the Option Bytes:
39 * - Set/Reset the write protection
40 * - Set the Read protection Level
42 * - Program the user Option Bytes
43 * - Launch the Option Bytes loader
45 * 4. Interrupts and flags management functions: this group
46 * includes all needed functions to:
47 * - Enable/Disable the FLASH interrupt sources
50 * - Get FLASH operation status
51 * - Wait for last FLASH operation
55 ******************************************************************************
58 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
59 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
60 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
61 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
62 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
63 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
65 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
66 ******************************************************************************
69 /* Includes ------------------------------------------------------------------*/
70 #include "stm32f4xx_flash.h"
72 /** @addtogroup STM32F4xx_StdPeriph_Driver
77 * @brief FLASH driver modules
81 /* Private typedef -----------------------------------------------------------*/
82 /* Private define ------------------------------------------------------------*/
83 #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
85 /* Private macro -------------------------------------------------------------*/
86 /* Private variables ---------------------------------------------------------*/
87 /* Private function prototypes -----------------------------------------------*/
88 /* Private functions ---------------------------------------------------------*/
90 /** @defgroup FLASH_Private_Functions
94 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
95 * @brief FLASH Interface configuration functions
99 ===============================================================================
100 FLASH Interface configuration functions
101 ===============================================================================
103 This group includes the following functions:
104 - void FLASH_SetLatency(uint32_t FLASH_Latency)
105 To correctly read data from FLASH memory, the number of wait states (LATENCY)
106 must be correctly programmed according to the frequency of the CPU clock
107 (HCLK) and the supply voltage of the device.
108 +-------------------------------------------------------------------------------------+
109 | Latency | HCLK clock frequency (MHz) |
110 | |---------------------------------------------------------------------|
111 | | voltage range | voltage range | voltage range | voltage range |
112 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
113 |---------------|----------------|----------------|-----------------|-----------------|
114 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 18 |0 < HCLK <= 16 |
115 |---------------|----------------|----------------|-----------------|-----------------|
116 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |18 < HCLK <= 36 |16 < HCLK <= 32 |
117 |---------------|----------------|----------------|-----------------|-----------------|
118 |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |36 < HCLK <= 54 |32 < HCLK <= 48 |
119 |---------------|----------------|----------------|-----------------|-----------------|
120 |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |54 < HCLK <= 72 |48 < HCLK <= 64 |
121 |---------------|----------------|----------------|-----------------|-----------------|
122 |4WS(5CPU cycle)| NA |96 < HCLK <= 120|72 < HCLK <= 90 |64 < HCLK <= 80 |
123 |---------------|----------------|----------------|-----------------|-----------------|
124 |5WS(6CPU cycle)| NA | NA |90 < HCLK <= 108 |80 < HCLK <= 96 |
125 |---------------|----------------|----------------|-----------------|-----------------|
126 |6WS(7CPU cycle)| NA | NA |108 < HCLK <= 120|96 < HCLK <= 112 |
127 |---------------|----------------|----------------|-----------------|-----------------|
128 |7WS(8CPU cycle)| NA | NA | NA |112 < HCLK <= 120|
129 |***************|****************|****************|*****************|*****************|*****************************+
130 | | voltage range | voltage range | voltage range | voltage range | voltage range 2.7 V - 3.6 V |
131 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V | with External Vpp = 9V |
132 |---------------|----------------|----------------|-----------------|-----------------|-----------------------------|
133 |Max Parallelism| x32 | x16 | x8 | x64 |
134 |---------------|----------------|----------------|-----------------|-----------------|-----------------------------|
135 |PSIZE[1:0] | 10 | 01 | 00 | 11 |
136 +-------------------------------------------------------------------------------------------------------------------+
138 - void FLASH_PrefetchBufferCmd(FunctionalState NewState)
139 - void FLASH_InstructionCacheCmd(FunctionalState NewState)
140 - void FLASH_DataCacheCmd(FunctionalState NewState)
141 - void FLASH_InstructionCacheReset(void)
142 - void FLASH_DataCacheReset(void)
144 The unlock sequence is not needed for these functions.
151 * @brief Sets the code latency value.
152 * @param FLASH_Latency: specifies the FLASH Latency value.
153 * This parameter can be one of the following values:
154 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
155 * @arg FLASH_Latency_1: FLASH One Latency cycle
156 * @arg FLASH_Latency_2: FLASH Two Latency cycles
157 * @arg FLASH_Latency_3: FLASH Three Latency cycles
158 * @arg FLASH_Latency_4: FLASH Four Latency cycles
159 * @arg FLASH_Latency_5: FLASH Five Latency cycles
160 * @arg FLASH_Latency_6: FLASH Six Latency cycles
161 * @arg FLASH_Latency_7: FLASH Seven Latency cycles
164 void FLASH_SetLatency(uint32_t FLASH_Latency)
166 /* Check the parameters */
167 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
169 /* Perform Byte access to FLASH_ACR[8:0] to set the Latency value */
170 *(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)FLASH_Latency;
174 * @brief Enables or disables the Prefetch Buffer.
175 * @param NewState: new state of the Prefetch Buffer.
176 * This parameter can be: ENABLE or DISABLE.
179 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
181 /* Check the parameters */
182 assert_param(IS_FUNCTIONAL_STATE(NewState));
184 /* Enable or disable the Prefetch Buffer */
185 if(NewState != DISABLE)
187 FLASH->ACR |= FLASH_ACR_PRFTEN;
191 FLASH->ACR &= (~FLASH_ACR_PRFTEN);
196 * @brief Enables or disables the Instruction Cache feature.
197 * @param NewState: new state of the Instruction Cache.
198 * This parameter can be: ENABLE or DISABLE.
201 void FLASH_InstructionCacheCmd(FunctionalState NewState)
203 /* Check the parameters */
204 assert_param(IS_FUNCTIONAL_STATE(NewState));
206 if(NewState != DISABLE)
208 FLASH->ACR |= FLASH_ACR_ICEN;
212 FLASH->ACR &= (~FLASH_ACR_ICEN);
217 * @brief Enables or disables the Data Cache feature.
218 * @param NewState: new state of the Data Cache.
219 * This parameter can be: ENABLE or DISABLE.
222 void FLASH_DataCacheCmd(FunctionalState NewState)
224 /* Check the parameters */
225 assert_param(IS_FUNCTIONAL_STATE(NewState));
227 if(NewState != DISABLE)
229 FLASH->ACR |= FLASH_ACR_DCEN;
233 FLASH->ACR &= (~FLASH_ACR_DCEN);
238 * @brief Resets the Instruction Cache.
239 * @note This function must be used only when the Instruction Cache is disabled.
243 void FLASH_InstructionCacheReset(void)
245 FLASH->ACR |= FLASH_ACR_ICRST;
249 * @brief Resets the Data Cache.
250 * @note This function must be used only when the Data Cache is disabled.
254 void FLASH_DataCacheReset(void)
256 FLASH->ACR |= FLASH_ACR_DCRST;
263 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
264 * @brief FLASH Memory Programming functions
267 ===============================================================================
268 FLASH Memory Programming functions
269 ===============================================================================
271 This group includes the following functions:
272 - void FLASH_Unlock(void)
273 - void FLASH_Lock(void)
274 - FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
275 - FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange)
276 - FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
277 - FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
278 - FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
279 - FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
281 Any operation of erase or program should follow these steps:
282 1. Call the FLASH_Unlock() function to enable the FLASH control register access
284 2. Call the desired function to erase sector(s) or program data
286 3. Call the FLASH_Lock() function to disable the FLASH control register access
287 (recommended to protect the FLASH memory against possible unwanted operation)
294 * @brief Unlocks the FLASH control register access
298 void FLASH_Unlock(void)
300 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
302 /* Authorize the FLASH Registers access */
303 FLASH->KEYR = FLASH_KEY1;
304 FLASH->KEYR = FLASH_KEY2;
309 * @brief Locks the FLASH control register access
313 void FLASH_Lock(void)
315 /* Set the LOCK Bit to lock the FLASH Registers access */
316 FLASH->CR |= FLASH_CR_LOCK;
320 * @brief Erases a specified FLASH Sector.
322 * @param FLASH_Sector: The Sector number to be erased.
323 * This parameter can be a value between FLASH_Sector_0 and FLASH_Sector_11
325 * @param VoltageRange: The device voltage range which defines the erase parallelism.
326 * This parameter can be one of the following values:
327 * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
328 * the operation will be done by byte (8-bit)
329 * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
330 * the operation will be done by half word (16-bit)
331 * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
332 * the operation will be done by word (32-bit)
333 * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
334 * the operation will be done by double word (64-bit)
336 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
337 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
339 FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
341 uint32_t tmp_psize = 0x0;
342 FLASH_Status status = FLASH_COMPLETE;
344 /* Check the parameters */
345 assert_param(IS_FLASH_SECTOR(FLASH_Sector));
346 assert_param(IS_VOLTAGERANGE(VoltageRange));
348 if(VoltageRange == VoltageRange_1)
350 tmp_psize = FLASH_PSIZE_BYTE;
352 else if(VoltageRange == VoltageRange_2)
354 tmp_psize = FLASH_PSIZE_HALF_WORD;
356 else if(VoltageRange == VoltageRange_3)
358 tmp_psize = FLASH_PSIZE_WORD;
362 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
364 /* Wait for last operation to be completed */
365 status = FLASH_WaitForLastOperation();
367 if(status == FLASH_COMPLETE)
369 /* if the previous operation is completed, proceed to erase the sector */
370 FLASH->CR &= CR_PSIZE_MASK;
371 FLASH->CR |= tmp_psize;
372 FLASH->CR &= SECTOR_MASK;
373 FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
374 FLASH->CR |= FLASH_CR_STRT;
376 /* Wait for last operation to be completed */
377 status = FLASH_WaitForLastOperation();
379 /* if the erase operation is completed, disable the SER Bit */
380 FLASH->CR &= (~FLASH_CR_SER);
381 FLASH->CR &= SECTOR_MASK;
383 /* Return the Erase Status */
388 * @brief Erases all FLASH Sectors.
390 * @param VoltageRange: The device voltage range which defines the erase parallelism.
391 * This parameter can be one of the following values:
392 * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
393 * the operation will be done by byte (8-bit)
394 * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
395 * the operation will be done by half word (16-bit)
396 * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
397 * the operation will be done by word (32-bit)
398 * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
399 * the operation will be done by double word (64-bit)
401 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
402 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
404 FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange)
406 uint32_t tmp_psize = 0x0;
407 FLASH_Status status = FLASH_COMPLETE;
409 /* Wait for last operation to be completed */
410 status = FLASH_WaitForLastOperation();
411 assert_param(IS_VOLTAGERANGE(VoltageRange));
413 if(VoltageRange == VoltageRange_1)
415 tmp_psize = FLASH_PSIZE_BYTE;
417 else if(VoltageRange == VoltageRange_2)
419 tmp_psize = FLASH_PSIZE_HALF_WORD;
421 else if(VoltageRange == VoltageRange_3)
423 tmp_psize = FLASH_PSIZE_WORD;
427 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
429 if(status == FLASH_COMPLETE)
431 /* if the previous operation is completed, proceed to erase all sectors */
432 FLASH->CR &= CR_PSIZE_MASK;
433 FLASH->CR |= tmp_psize;
434 FLASH->CR |= FLASH_CR_MER;
435 FLASH->CR |= FLASH_CR_STRT;
437 /* Wait for last operation to be completed */
438 status = FLASH_WaitForLastOperation();
440 /* if the erase operation is completed, disable the MER Bit */
441 FLASH->CR &= (~FLASH_CR_MER);
444 /* Return the Erase Status */
449 * @brief Programs a double word (64-bit) at a specified address.
450 * @note This function must be used when the device voltage range is from
451 * 2.7V to 3.6V and an External Vpp is present.
452 * @param Address: specifies the address to be programmed.
453 * @param Data: specifies the data to be programmed.
454 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
455 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
457 FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
459 FLASH_Status status = FLASH_COMPLETE;
461 /* Check the parameters */
462 assert_param(IS_FLASH_ADDRESS(Address));
464 /* Wait for last operation to be completed */
465 status = FLASH_WaitForLastOperation();
467 if(status == FLASH_COMPLETE)
469 /* if the previous operation is completed, proceed to program the new data */
470 FLASH->CR &= CR_PSIZE_MASK;
471 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
472 FLASH->CR |= FLASH_CR_PG;
474 *(__IO uint64_t*)Address = Data;
476 /* Wait for last operation to be completed */
477 status = FLASH_WaitForLastOperation();
479 /* if the program operation is completed, disable the PG Bit */
480 FLASH->CR &= (~FLASH_CR_PG);
482 /* Return the Program Status */
487 * @brief Programs a word (32-bit) at a specified address.
488 * @param Address: specifies the address to be programmed.
489 * This parameter can be any address in Program memory zone or in OTP zone.
490 * @note This function must be used when the device voltage range is from 2.7V to 3.6V.
491 * @param Data: specifies the data to be programmed.
492 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
493 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
495 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
497 FLASH_Status status = FLASH_COMPLETE;
499 /* Check the parameters */
500 assert_param(IS_FLASH_ADDRESS(Address));
502 /* Wait for last operation to be completed */
503 status = FLASH_WaitForLastOperation();
505 if(status == FLASH_COMPLETE)
507 /* if the previous operation is completed, proceed to program the new data */
508 FLASH->CR &= CR_PSIZE_MASK;
509 FLASH->CR |= FLASH_PSIZE_WORD;
510 FLASH->CR |= FLASH_CR_PG;
512 *(__IO uint32_t*)Address = Data;
514 /* Wait for last operation to be completed */
515 status = FLASH_WaitForLastOperation();
517 /* if the program operation is completed, disable the PG Bit */
518 FLASH->CR &= (~FLASH_CR_PG);
520 /* Return the Program Status */
525 * @brief Programs a half word (16-bit) at a specified address.
526 * @note This function must be used when the device voltage range is from 2.1V to 3.6V.
527 * @param Address: specifies the address to be programmed.
528 * This parameter can be any address in Program memory zone or in OTP zone.
529 * @param Data: specifies the data to be programmed.
530 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
531 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
533 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
535 FLASH_Status status = FLASH_COMPLETE;
537 /* Check the parameters */
538 assert_param(IS_FLASH_ADDRESS(Address));
540 /* Wait for last operation to be completed */
541 status = FLASH_WaitForLastOperation();
543 if(status == FLASH_COMPLETE)
545 /* if the previous operation is completed, proceed to program the new data */
546 FLASH->CR &= CR_PSIZE_MASK;
547 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
548 FLASH->CR |= FLASH_CR_PG;
550 *(__IO uint16_t*)Address = Data;
552 /* Wait for last operation to be completed */
553 status = FLASH_WaitForLastOperation();
555 /* if the program operation is completed, disable the PG Bit */
556 FLASH->CR &= (~FLASH_CR_PG);
558 /* Return the Program Status */
563 * @brief Programs a byte (8-bit) at a specified address.
564 * @note This function can be used within all the device supply voltage ranges.
565 * @param Address: specifies the address to be programmed.
566 * This parameter can be any address in Program memory zone or in OTP zone.
567 * @param Data: specifies the data to be programmed.
568 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
569 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
571 FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
573 FLASH_Status status = FLASH_COMPLETE;
575 /* Check the parameters */
576 assert_param(IS_FLASH_ADDRESS(Address));
578 /* Wait for last operation to be completed */
579 status = FLASH_WaitForLastOperation();
581 if(status == FLASH_COMPLETE)
583 /* if the previous operation is completed, proceed to program the new data */
584 FLASH->CR &= CR_PSIZE_MASK;
585 FLASH->CR |= FLASH_PSIZE_BYTE;
586 FLASH->CR |= FLASH_CR_PG;
588 *(__IO uint8_t*)Address = Data;
590 /* Wait for last operation to be completed */
591 status = FLASH_WaitForLastOperation();
593 /* if the program operation is completed, disable the PG Bit */
594 FLASH->CR &= (~FLASH_CR_PG);
597 /* Return the Program Status */
605 /** @defgroup FLASH_Group3 Option Bytes Programming functions
606 * @brief Option Bytes Programming functions
609 ===============================================================================
610 Option Bytes Programming functions
611 ===============================================================================
613 This group includes the following functions:
614 - void FLASH_OB_Unlock(void)
615 - void FLASH_OB_Lock(void)
616 - void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
617 - void FLASH_OB_RDPConfig(uint8_t OB_RDP)
618 - void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
619 - void FLASH_OB_BORConfig(uint8_t OB_BOR)
620 - FLASH_Status FLASH_ProgramOTP(uint32_t Address, uint32_t Data)
621 - FLASH_Status FLASH_OB_Launch(void)
622 - uint32_t FLASH_OB_GetUser(void)
623 - uint8_t FLASH_OB_GetWRP(void)
624 - uint8_t FLASH_OB_GetRDP(void)
625 - uint8_t FLASH_OB_GetBOR(void)
627 Any operation of erase or program should follow these steps:
628 1. Call the FLASH_OB_Unlock() function to enable the FLASH option control register access
630 2. Call one or several functions to program the desired Option Bytes:
631 - void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState) => to Enable/Disable
632 the desired sector write protection
633 - void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
634 - void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY) => to configure
635 the user Option Bytes.
636 - void FLASH_OB_BORConfig(uint8_t OB_BOR) => to set the BOR Level
638 3. Once all needed Option Bytes to be programmed are correctly written, call the
639 FLASH_OB_Launch() function to launch the Option Bytes programming process.
641 @note When changing the IWDG mode from HW to SW or from SW to HW, a system
642 reset is needed to make the change effective.
644 4. Call the FLASH_OB_Lock() function to disable the FLASH option control register
645 access (recommended to protect the Option Bytes against possible unwanted operations)
652 * @brief Unlocks the FLASH Option Control Registers access.
656 void FLASH_OB_Unlock(void)
658 if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
660 /* Authorizes the Option Byte register programming */
661 FLASH->OPTKEYR = FLASH_OPT_KEY1;
662 FLASH->OPTKEYR = FLASH_OPT_KEY2;
667 * @brief Locks the FLASH Option Control Registers access.
671 void FLASH_OB_Lock(void)
673 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
674 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
678 * @brief Enables or disables the write protection of the desired sectors
679 * @param OB_WRP: specifies the sector(s) to be write protected or unprotected.
680 * This parameter can be one of the following values:
681 * @arg OB_WRP: A value between OB_WRP_Sector0 and OB_WRP_Sector11
682 * @arg OB_WRP_Sector_All
683 * @param Newstate: new state of the Write Protection.
684 * This parameter can be: ENABLE or DISABLE.
687 void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
689 FLASH_Status status = FLASH_COMPLETE;
691 /* Check the parameters */
692 assert_param(IS_OB_WRP(OB_WRP));
693 assert_param(IS_FUNCTIONAL_STATE(NewState));
695 status = FLASH_WaitForLastOperation();
697 if(status == FLASH_COMPLETE)
699 if(NewState != DISABLE)
701 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~OB_WRP);
705 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)OB_WRP;
711 * @brief Sets the read protection level.
712 * @param OB_RDP: specifies the read protection level.
713 * This parameter can be one of the following values:
714 * @arg OB_RDP_Level_0: No protection
715 * @arg OB_RDP_Level_1: Read protection of the memory
716 * @arg OB_RDP_Level_2: Full chip protection
718 * !!!Warning!!! When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
722 void FLASH_OB_RDPConfig(uint8_t OB_RDP)
724 FLASH_Status status = FLASH_COMPLETE;
726 /* Check the parameters */
727 assert_param(IS_OB_RDP(OB_RDP));
729 status = FLASH_WaitForLastOperation();
731 if(status == FLASH_COMPLETE)
733 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = OB_RDP;
739 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
740 * @param OB_IWDG: Selects the IWDG mode
741 * This parameter can be one of the following values:
742 * @arg OB_IWDG_SW: Software IWDG selected
743 * @arg OB_IWDG_HW: Hardware IWDG selected
744 * @param OB_STOP: Reset event when entering STOP mode.
745 * This parameter can be one of the following values:
746 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
747 * @arg OB_STOP_RST: Reset generated when entering in STOP
748 * @param OB_STDBY: Reset event when entering Standby mode.
749 * This parameter can be one of the following values:
750 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
751 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
754 void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
756 uint8_t optiontmp = 0xFF;
757 FLASH_Status status = FLASH_COMPLETE;
759 /* Check the parameters */
760 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
761 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
762 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
764 /* Wait for last operation to be completed */
765 status = FLASH_WaitForLastOperation();
767 if(status == FLASH_COMPLETE)
769 /* Mask OPTLOCK, OPTSTRT and BOR_LEV bits */
770 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0F);
772 /* Update User Option Byte */
773 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = OB_IWDG | (uint8_t)(OB_STDBY | (uint8_t)(OB_STOP | ((uint8_t)optiontmp)));
778 * @brief Sets the BOR Level.
779 * @param OB_BOR: specifies the Option Bytes BOR Reset Level.
780 * This parameter can be one of the following values:
781 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
782 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
783 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
784 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
787 void FLASH_OB_BORConfig(uint8_t OB_BOR)
789 /* Check the parameters */
790 assert_param(IS_OB_BOR(OB_BOR));
792 /* Set the BOR Level */
793 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
794 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= OB_BOR;
799 * @brief Launch the option byte loading.
801 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
802 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
804 FLASH_Status FLASH_OB_Launch(void)
806 FLASH_Status status = FLASH_COMPLETE;
808 /* Set the OPTSTRT bit in OPTCR register */
809 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
811 /* Wait for last operation to be completed */
812 status = FLASH_WaitForLastOperation();
818 * @brief Returns the FLASH User Option Bytes values.
820 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
821 * and RST_STDBY(Bit2).
823 uint8_t FLASH_OB_GetUser(void)
825 /* Return the User Option Byte */
826 return (uint8_t)(FLASH->OPTCR >> 5);
830 * @brief Returns the FLASH Write Protection Option Bytes value.
832 * @retval The FLASH Write Protection Option Bytes value
834 uint16_t FLASH_OB_GetWRP(void)
836 /* Return the FLASH write protection Register value */
837 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
841 * @brief Returns the FLASH Read Protection level.
843 * @retval FLASH ReadOut Protection Status:
844 * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
845 * - RESET, when OB_RDP_Level_0 is set
847 FlagStatus FLASH_OB_GetRDP(void)
849 FlagStatus readstatus = RESET;
851 if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) != (uint8_t)OB_RDP_Level_0))
863 * @brief Returns the FLASH BOR level.
865 * @retval The FLASH BOR level:
866 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
867 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
868 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
869 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
871 uint8_t FLASH_OB_GetBOR(void)
873 /* Return the FLASH BOR level */
874 return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
881 /** @defgroup FLASH_Group4 Interrupts and flags management functions
882 * @brief Interrupts and flags management functions
885 ===============================================================================
886 Interrupts and flags management functions
887 ===============================================================================
894 * @brief Enables or disables the specified FLASH interrupts.
895 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
896 * This parameter can be any combination of the following values:
897 * @arg FLASH_IT_ERR: FLASH Error Interrupt
898 * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
901 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
903 /* Check the parameters */
904 assert_param(IS_FLASH_IT(FLASH_IT));
905 assert_param(IS_FUNCTIONAL_STATE(NewState));
907 if(NewState != DISABLE)
909 /* Enable the interrupt sources */
910 FLASH->CR |= FLASH_IT;
914 /* Disable the interrupt sources */
915 FLASH->CR &= ~(uint32_t)FLASH_IT;
920 * @brief Checks whether the specified FLASH flag is set or not.
921 * @param FLASH_FLAG: specifies the FLASH flag to check.
922 * This parameter can be one of the following values:
923 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
924 * @arg FLASH_FLAG_OPERR: FLASH operation Error flag
925 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
926 * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
927 * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
928 * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
929 * @arg FLASH_FLAG_BSY: FLASH Busy flag
930 * @retval The new state of FLASH_FLAG (SET or RESET).
932 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
934 FlagStatus bitstatus = RESET;
935 /* Check the parameters */
936 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
938 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
946 /* Return the new state of FLASH_FLAG (SET or RESET) */
951 * @brief Clears the FLASH's pending flags.
952 * @param FLASH_FLAG: specifies the FLASH flags to clear.
953 * This parameter can be any combination of the following values:
954 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
955 * @arg FLASH_FLAG_OPERR: FLASH operation Error flag
956 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
957 * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
958 * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
959 * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
962 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
964 /* Check the parameters */
965 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
967 /* Clear the flags */
968 FLASH->SR = FLASH_FLAG;
972 * @brief Returns the FLASH Status.
974 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
975 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
977 FLASH_Status FLASH_GetStatus(void)
979 FLASH_Status flashstatus = FLASH_COMPLETE;
981 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
983 flashstatus = FLASH_BUSY;
987 if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)
989 flashstatus = FLASH_ERROR_WRP;
993 if((FLASH->SR & (uint32_t)0xEF) != (uint32_t)0x00)
995 flashstatus = FLASH_ERROR_PROGRAM;
999 if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)
1001 flashstatus = FLASH_ERROR_OPERATION;
1005 flashstatus = FLASH_COMPLETE;
1010 /* Return the FLASH Status */
1015 * @brief Waits for a FLASH operation to complete.
1017 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PROGRAM,
1018 * FLASH_ERROR_WRP, FLASH_ERROR_OPERATION or FLASH_COMPLETE.
1020 FLASH_Status FLASH_WaitForLastOperation(void)
1022 __IO FLASH_Status status = FLASH_COMPLETE;
1024 /* Check for the FLASH Status */
1025 status = FLASH_GetStatus();
1027 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
1028 Even if the FLASH operation fails, the BUSY flag will be reset and an error
1030 while(status == FLASH_BUSY)
1032 status = FLASH_GetStatus();
1034 /* Return the operation status */
1054 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/