Move telemetry displayport init and cms device registering
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F30x_StdPeriph_Driver / src / stm32f30x_flash.c
blob20b9782557e4ade3ad4c4ff38a7968c1e36514b6
1 /**
2 ******************************************************************************
3 * @file stm32f30x_flash.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 04-April-2014
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
14 @verbatim
16 ===============================================================================
17 ##### How to use this driver #####
18 ===============================================================================
19 [..] This driver provides functions to configure and program the FLASH
20 memory of all STM32F30x devices. These functions are split in 4 groups:
21 (#) FLASH Interface configuration functions: this group includes the
22 management of following features:
23 (++) Set the latency.
24 (++) Enable/Disable the Half Cycle Access.
25 (++) Enable/Disable the prefetch buffer.
26 (#) FLASH Memory Programming functions: this group includes all needed
27 functions to erase and program the main memory:
28 (++) Lock and Unlock the FLASH interface.
29 (++) Erase function: Erase page, erase all pages.
30 (++) Program functions: Half Word and Word write.
31 (#) FLASH Option Bytes Programming functions: this group includes all
32 needed functions to manage the Option Bytes:
33 (++) Lock and Unlock the Flash Option bytes.
34 (++) Launch the Option Bytes loader
35 (++) Erase the Option Bytes
36 (++) Set/Reset the write protection
37 (++) Set the Read protection Level
38 (++) Program the user option Bytes
39 (++) Set/Reset the BOOT1 bit
40 (++) Enable/Disable the VDDA Analog Monitoring
41 (++) Enable/Disable the SRAM parity
42 (++) Get the user option bytes
43 (++) Get the Write protection
44 (++) Get the read protection status
45 (#) FLASH Interrupts and flags management functions: this group includes
46 all needed functions to:
47 (++) Enable/Disable the FLASH interrupt sources.
48 (++) Get flags status.
49 (++) Clear flags.
50 (++) Get FLASH operation status.
51 (++) Wait for last FLASH operation.
53 @endverbatim
55 ******************************************************************************
56 * @attention
58 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
60 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
61 * You may not use this file except in compliance with the License.
62 * You may obtain a copy of the License at:
64 * http://www.st.com/software_license_agreement_liberty_v2
66 * Unless required by applicable law or agreed to in writing, software
67 * distributed under the License is distributed on an "AS IS" BASIS,
68 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
69 * See the License for the specific language governing permissions and
70 * limitations under the License.
72 ******************************************************************************
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f30x_flash.h"
78 /** @addtogroup STM32F30x_StdPeriph_Driver
79 * @{
82 /** @defgroup FLASH
83 * @brief FLASH driver modules
84 * @{
85 */
87 /* Private typedef -----------------------------------------------------------*/
88 /* Private define ------------------------------------------------------------*/
90 /* FLASH Mask */
91 #define RDPRT_MASK ((uint32_t)0x00000002)
92 #define WRP01_MASK ((uint32_t)0x0000FFFF)
93 #define WRP23_MASK ((uint32_t)0xFFFF0000)
94 /* Private macro -------------------------------------------------------------*/
95 /* Private variables ---------------------------------------------------------*/
96 /* Private function prototypes -----------------------------------------------*/
97 /* Private functions ---------------------------------------------------------*/
99 /** @defgroup FLASH_Private_Functions
100 * @{
103 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
104 * @brief FLASH Interface configuration functions
107 @verbatim
108 ===============================================================================
109 ##### FLASH Interface configuration functions #####
110 ===============================================================================
111 [..] This group includes the following functions:
112 (+) void FLASH_SetLatency(uint32_t FLASH_Latency);
113 (+) void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);
114 (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
115 [..] The unlock sequence is not needed for these functions.
117 @endverbatim
118 * @{
122 * @brief Sets the code latency value.
123 * @param FLASH_Latency: specifies the FLASH Latency value.
124 * This parameter can be one of the following values:
125 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
126 * @arg FLASH_Latency_1: FLASH One Latency cycle
127 * @arg FLASH_Latency_2: FLASH Two Latency cycles
128 * @retval None
130 void FLASH_SetLatency(uint32_t FLASH_Latency)
132 uint32_t tmpreg = 0;
134 /* Check the parameters */
135 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
137 /* Read the ACR register */
138 tmpreg = FLASH->ACR;
140 /* Sets the Latency value */
141 tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
142 tmpreg |= FLASH_Latency;
144 /* Write the ACR register */
145 FLASH->ACR = tmpreg;
149 * @brief Enables or disables the Half cycle flash access.
150 * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
151 * This parameter can be one of the following values:
152 * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
153 * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
154 * @retval None
156 void FLASH_HalfCycleAccessCmd(FunctionalState NewState)
158 /* Check the parameters */
159 assert_param(IS_FUNCTIONAL_STATE(NewState));
161 if(NewState != DISABLE)
163 FLASH->ACR |= FLASH_ACR_HLFCYA;
165 else
167 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_HLFCYA));
172 * @brief Enables or disables the Prefetch Buffer.
173 * @param NewState: new state of the Prefetch Buffer.
174 * This parameter can be: ENABLE or DISABLE.
175 * @retval None
177 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
179 /* Check the parameters */
180 assert_param(IS_FUNCTIONAL_STATE(NewState));
182 if(NewState != DISABLE)
184 FLASH->ACR |= FLASH_ACR_PRFTBE;
186 else
188 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
193 * @}
196 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
197 * @brief FLASH Memory Programming functions
199 @verbatim
200 ===============================================================================
201 ##### FLASH Memory Programming functions #####
202 ===============================================================================
203 [..] This group includes the following functions:
204 (+) void FLASH_Unlock(void);
205 (+) void FLASH_Lock(void);
206 (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
207 (+) FLASH_Status FLASH_EraseAllPages(void);
208 (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
209 (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
210 [..] Any operation of erase or program should follow these steps:
211 (#) Call the FLASH_Unlock() function to enable the FLASH control register
212 program memory access.
213 (#) Call the desired function to erase page or program data.
214 (#) Call the FLASH_Lock() function to disable the FLASH control register
215 access (recommended to protect the FLASH memory against possible
216 unwanted operation).
218 @endverbatim
219 * @{
223 * @brief Unlocks the FLASH control register access
224 * @param None
225 * @retval None
227 void FLASH_Unlock(void)
229 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
231 /* Authorize the FLASH Registers access */
232 FLASH->KEYR = FLASH_KEY1;
233 FLASH->KEYR = FLASH_KEY2;
238 * @brief Locks the FLASH control register access
239 * @param None
240 * @retval None
242 void FLASH_Lock(void)
244 /* Set the LOCK Bit to lock the FLASH Registers access */
245 FLASH->CR |= FLASH_CR_LOCK;
249 * @brief Erases a specified page in program memory.
250 * @note To correctly run this function, the FLASH_Unlock() function
251 * must be called before.
252 * @note Call the FLASH_Lock() to disable the flash memory access
253 * (recommended to protect the FLASH memory against possible unwanted operation)
254 * @param Page_Address: The page address in program memory to be erased.
255 * @note A Page is erased in the Program memory only if the address to load
256 * is the start address of a page (multiple of 1024 bytes).
257 * @retval FLASH Status: The returned value can be:
258 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
260 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
262 FLASH_Status status = FLASH_COMPLETE;
264 /* Check the parameters */
265 assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
267 /* Wait for last operation to be completed */
268 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
270 if(status == FLASH_COMPLETE)
272 /* If the previous operation is completed, proceed to erase the page */
273 FLASH->CR |= FLASH_CR_PER;
274 FLASH->AR = Page_Address;
275 FLASH->CR |= FLASH_CR_STRT;
277 /* Wait for last operation to be completed */
278 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
280 /* Disable the PER Bit */
281 FLASH->CR &= ~FLASH_CR_PER;
284 /* Return the Erase Status */
285 return status;
289 * @brief Erases all FLASH pages.
290 * @note To correctly run this function, the FLASH_Unlock() function
291 * must be called before.
292 * all the FLASH_Lock() to disable the flash memory access
293 * (recommended to protect the FLASH memory against possible unwanted operation)
294 * @param None
295 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
296 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
298 FLASH_Status FLASH_EraseAllPages(void)
300 FLASH_Status status = FLASH_COMPLETE;
302 /* Wait for last operation to be completed */
303 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
305 if(status == FLASH_COMPLETE)
307 /* if the previous operation is completed, proceed to erase all pages */
308 FLASH->CR |= FLASH_CR_MER;
309 FLASH->CR |= FLASH_CR_STRT;
311 /* Wait for last operation to be completed */
312 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
314 /* Disable the MER Bit */
315 FLASH->CR &= ~FLASH_CR_MER;
318 /* Return the Erase Status */
319 return status;
323 * @brief Programs a word at a specified address.
324 * @note To correctly run this function, the FLASH_Unlock() function
325 * must be called before.
326 * Call the FLASH_Lock() to disable the flash memory access
327 * (recommended to protect the FLASH memory against possible unwanted operation)
328 * @param Address: specifies the address to be programmed.
329 * @param Data: specifies the data to be programmed.
330 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
331 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
333 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
335 FLASH_Status status = FLASH_COMPLETE;
336 __IO uint32_t tmp = 0;
338 /* Check the parameters */
339 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
341 /* Wait for last operation to be completed */
342 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
344 if(status == FLASH_COMPLETE)
346 /* If the previous operation is completed, proceed to program the new first
347 half word */
348 FLASH->CR |= FLASH_CR_PG;
350 *(__IO uint16_t*)Address = (uint16_t)Data;
352 /* Wait for last operation to be completed */
353 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
355 if(status == FLASH_COMPLETE)
357 /* If the previous operation is completed, proceed to program the new second
358 half word */
359 tmp = Address + 2;
361 *(__IO uint16_t*) tmp = Data >> 16;
363 /* Wait for last operation to be completed */
364 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
366 /* Disable the PG Bit */
367 FLASH->CR &= ~FLASH_CR_PG;
369 else
371 /* Disable the PG Bit */
372 FLASH->CR &= ~FLASH_CR_PG;
376 /* Return the Program Status */
377 return status;
381 * @brief Programs a half word at a specified address.
382 * @note To correctly run this function, the FLASH_Unlock() function
383 * must be called before.
384 * Call the FLASH_Lock() to disable the flash memory access
385 * (recommended to protect the FLASH memory against possible unwanted operation)
386 * @param Address: specifies the address to be programmed.
387 * @param Data: specifies the data to be programmed.
388 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
389 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
391 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
393 FLASH_Status status = FLASH_COMPLETE;
395 /* Check the parameters */
396 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
398 /* Wait for last operation to be completed */
399 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
401 if(status == FLASH_COMPLETE)
403 /* If the previous operation is completed, proceed to program the new data */
404 FLASH->CR |= FLASH_CR_PG;
406 *(__IO uint16_t*)Address = Data;
408 /* Wait for last operation to be completed */
409 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
411 /* Disable the PG Bit */
412 FLASH->CR &= ~FLASH_CR_PG;
415 /* Return the Program Status */
416 return status;
420 * @}
423 /** @defgroup FLASH_Group3 Option Bytes Programming functions
424 * @brief Option Bytes Programming functions
426 @verbatim
427 ===============================================================================
428 ##### Option Bytes Programming functions #####
429 ===============================================================================
430 [..] This group includes the following functions:
431 (+) void FLASH_OB_Unlock(void);
432 (+) void FLASH_OB_Lock(void);
433 (+) void FLASH_OB_Erase(void);
434 (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
435 (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
436 (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
437 (+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
438 (+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
439 (+) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity);
440 (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
441 (+) FLASH_Status FLASH_OB_Launch(void);
442 (+) uint32_t FLASH_OB_GetUser(void);
443 (+) uint8_t FLASH_OB_GetWRP(void);
444 (+) uint8_t FLASH_OB_GetRDP(void);
445 [..] Any operation of erase or program should follow these steps:
446 (#) Call the FLASH_OB_Unlock() function to enable the FLASH option control
447 register access.
448 (#) Call one or several functions to program the desired Option Bytes:
449 (++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
450 => to Enable/Disable the desired sector write protection.
451 (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the
452 desired read Protection Level.
453 (++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
454 => to configure the user Option Bytes.
455 (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
456 => to set the boot1 mode
457 (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
458 => to Enable/Disable the VDDA monotoring.
459 (++) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity);
460 => to Enable/Disable the SRAM Parity check.
461 (++) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
462 => to write all user option bytes: OB_IWDG, OB_STOP, OB_STDBY,
463 OB_BOOT1, OB_VDDA_ANALOG and OB_VDD_SD12.
464 (#) Once all needed Option Bytes to be programmed are correctly written,
465 call the FLASH_OB_Launch() function to launch the Option Bytes
466 programming process.
467 (#@) When changing the IWDG mode from HW to SW or from SW to HW, a system
468 reset is needed to make the change effective.
469 (#) Call the FLASH_OB_Lock() function to disable the FLASH option control
470 register access (recommended to protect the Option Bytes against
471 possible unwanted operations).
473 @endverbatim
474 * @{
478 * @brief Unlocks the option bytes block access.
479 * @param None
480 * @retval None
482 void FLASH_OB_Unlock(void)
484 if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
486 /* Unlocking the option bytes block access */
487 FLASH->OPTKEYR = FLASH_OPTKEY1;
488 FLASH->OPTKEYR = FLASH_OPTKEY2;
493 * @brief Locks the option bytes block access.
494 * @param None
495 * @retval None
497 void FLASH_OB_Lock(void)
499 /* Set the OPTWREN Bit to lock the option bytes block access */
500 FLASH->CR &= ~FLASH_CR_OPTWRE;
504 * @brief Launch the option byte loading.
505 * @param None
506 * @retval None
508 void FLASH_OB_Launch(void)
510 /* Set the OBL_Launch bit to launch the option byte loading */
511 FLASH->CR |= FLASH_CR_OBL_LAUNCH;
515 * @brief Erases the FLASH option bytes.
516 * @note This functions erases all option bytes except the Read protection (RDP).
517 * @param None
518 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
519 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
521 FLASH_Status FLASH_OB_Erase(void)
523 uint16_t rdptmp = OB_RDP_Level_0;
525 FLASH_Status status = FLASH_COMPLETE;
527 /* Get the actual read protection Option Byte value */
528 if(FLASH_OB_GetRDP() != RESET)
530 rdptmp = 0x00;
533 /* Wait for last operation to be completed */
534 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
536 if(status == FLASH_COMPLETE)
538 /* If the previous operation is completed, proceed to erase the option bytes */
539 FLASH->CR |= FLASH_CR_OPTER;
540 FLASH->CR |= FLASH_CR_STRT;
542 /* Wait for last operation to be completed */
543 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
545 if(status == FLASH_COMPLETE)
547 /* If the erase operation is completed, disable the OPTER Bit */
548 FLASH->CR &= ~FLASH_CR_OPTER;
550 /* Enable the Option Bytes Programming operation */
551 FLASH->CR |= FLASH_CR_OPTPG;
553 /* Restore the last read protection Option Byte value */
554 OB->RDP = (uint16_t)rdptmp;
556 /* Wait for last operation to be completed */
557 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
559 if(status != FLASH_TIMEOUT)
561 /* if the program operation is completed, disable the OPTPG Bit */
562 FLASH->CR &= ~FLASH_CR_OPTPG;
565 else
567 if (status != FLASH_TIMEOUT)
569 /* Disable the OPTPG Bit */
570 FLASH->CR &= ~FLASH_CR_OPTPG;
574 /* Return the erase status */
575 return status;
579 * @brief Write protects the desired pages
580 * @note To correctly run this function, the FLASH_OB_Unlock() function
581 * must be called before.
582 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
583 * (recommended to protect the FLASH memory against possible unwanted operation)
584 * @param OB_WRP: specifies the address of the pages to be write protected.
585 * This parameter can be:
586 * @arg value between OB_WRP_Pages0to35 and OB_WRP_Pages60to63
587 * @arg OB_WRP_AllPages
588 * @retval FLASH Status: The returned value can be:
589 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
591 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
593 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
595 FLASH_Status status = FLASH_COMPLETE;
597 /* Check the parameters */
598 assert_param(IS_OB_WRP(OB_WRP));
600 OB_WRP = (uint32_t)(~OB_WRP);
601 WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
602 WRP1_Data = (uint16_t)((OB_WRP & OB_WRP0_nWRP0) >> 8);
604 /* Wait for last operation to be completed */
605 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
607 if(status == FLASH_COMPLETE)
609 FLASH->CR |= FLASH_CR_OPTPG;
611 if(WRP0_Data != 0xFF)
613 OB->WRP0 = WRP0_Data;
615 /* Wait for last operation to be completed */
616 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
618 if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
620 OB->WRP1 = WRP1_Data;
622 /* Wait for last operation to be completed */
623 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
625 if(status != FLASH_TIMEOUT)
627 /* if the program operation is completed, disable the OPTPG Bit */
628 FLASH->CR &= ~FLASH_CR_OPTPG;
631 /* Return the write protection operation Status */
632 return status;
636 * @brief Enables or disables the read out protection.
637 * @note To correctly run this function, the FLASH_OB_Unlock() function
638 * must be called before.
639 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
640 * (recommended to protect the FLASH memory against possible unwanted operation)
641 * @param FLASH_ReadProtection_Level: specifies the read protection level.
642 * This parameter can be:
643 * @arg OB_RDP_Level_0: No protection
644 * @arg OB_RDP_Level_1: Read protection of the memory
645 * @arg OB_RDP_Level_2: Chip protection
646 * @retval FLASH Status: The returned value can be:
647 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
649 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
651 FLASH_Status status = FLASH_COMPLETE;
653 /* Check the parameters */
654 assert_param(IS_OB_RDP(OB_RDP));
655 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
657 if(status == FLASH_COMPLETE)
659 FLASH->CR |= FLASH_CR_OPTER;
660 FLASH->CR |= FLASH_CR_STRT;
662 /* Wait for last operation to be completed */
663 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
665 if(status == FLASH_COMPLETE)
667 /* If the erase operation is completed, disable the OPTER Bit */
668 FLASH->CR &= ~FLASH_CR_OPTER;
670 /* Enable the Option Bytes Programming operation */
671 FLASH->CR |= FLASH_CR_OPTPG;
673 OB->RDP = OB_RDP;
675 /* Wait for last operation to be completed */
676 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
678 if(status != FLASH_TIMEOUT)
680 /* if the program operation is completed, disable the OPTPG Bit */
681 FLASH->CR &= ~FLASH_CR_OPTPG;
684 else
686 if(status != FLASH_TIMEOUT)
688 /* Disable the OPTER Bit */
689 FLASH->CR &= ~FLASH_CR_OPTER;
693 /* Return the protection operation Status */
694 return status;
698 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
699 * @param OB_IWDG: Selects the IWDG mode
700 * This parameter can be one of the following values:
701 * @arg OB_IWDG_SW: Software IWDG selected
702 * @arg OB_IWDG_HW: Hardware IWDG selected
703 * @param OB_STOP: Reset event when entering STOP mode.
704 * This parameter can be one of the following values:
705 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
706 * @arg OB_STOP_RST: Reset generated when entering in STOP
707 * @param OB_STDBY: Reset event when entering Standby mode.
708 * This parameter can be one of the following values:
709 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
710 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
711 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
712 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
714 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
716 FLASH_Status status = FLASH_COMPLETE;
718 /* Check the parameters */
719 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
720 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
721 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
723 /* Authorize the small information block programming */
724 FLASH->OPTKEYR = FLASH_KEY1;
725 FLASH->OPTKEYR = FLASH_KEY2;
727 /* Wait for last operation to be completed */
728 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
730 if(status == FLASH_COMPLETE)
732 /* Enable the Option Bytes Programming operation */
733 FLASH->CR |= FLASH_CR_OPTPG;
735 OB->USER = (uint8_t)((uint8_t)(OB_IWDG | OB_STOP) | (uint8_t)(OB_STDBY |0xF8));
737 /* Wait for last operation to be completed */
738 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
740 if(status != FLASH_TIMEOUT)
742 /* if the program operation is completed, disable the OPTPG Bit */
743 FLASH->CR &= ~FLASH_CR_OPTPG;
746 /* Return the Option Byte program Status */
747 return status;
751 * @brief Sets or resets the BOOT1.
752 * @param OB_BOOT1: Set or Reset the BOOT1.
753 * This parameter can be one of the following values:
754 * @arg OB_BOOT1_RESET: BOOT1 Reset
755 * @arg OB_BOOT1_SET: BOOT1 Set
756 * @retval None
758 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
760 FLASH_Status status = FLASH_COMPLETE;
762 /* Check the parameters */
763 assert_param(IS_OB_BOOT1(OB_BOOT1));
765 /* Authorize the small information block programming */
766 FLASH->OPTKEYR = FLASH_KEY1;
767 FLASH->OPTKEYR = FLASH_KEY2;
769 /* Wait for last operation to be completed */
770 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
772 if(status == FLASH_COMPLETE)
774 /* Enable the Option Bytes Programming operation */
775 FLASH->CR |= FLASH_CR_OPTPG;
777 OB->USER = OB_BOOT1|0xEF;
779 /* Wait for last operation to be completed */
780 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
782 if(status != FLASH_TIMEOUT)
784 /* if the program operation is completed, disable the OPTPG Bit */
785 FLASH->CR &= ~FLASH_CR_OPTPG;
788 /* Return the Option Byte program Status */
789 return status;
793 * @brief Sets or resets the analogue monitoring on VDDA Power source.
794 * @param OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
795 * This parameter can be one of the following values:
796 * @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
797 * @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
798 * @retval None
800 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
802 FLASH_Status status = FLASH_COMPLETE;
804 /* Check the parameters */
805 assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
807 /* Authorize the small information block programming */
808 FLASH->OPTKEYR = FLASH_KEY1;
809 FLASH->OPTKEYR = FLASH_KEY2;
811 /* Wait for last operation to be completed */
812 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
814 if(status == FLASH_COMPLETE)
816 /* Enable the Option Bytes Programming operation */
817 FLASH->CR |= FLASH_CR_OPTPG;
819 OB->USER = OB_VDDA_ANALOG |0xDF;
821 /* Wait for last operation to be completed */
822 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
824 if(status != FLASH_TIMEOUT)
826 /* if the program operation is completed, disable the OPTPG Bit */
827 FLASH->CR &= ~FLASH_CR_OPTPG;
830 /* Return the Option Byte program Status */
831 return status;
835 * @brief Sets or resets the SRAM partiy.
836 * @param OB_SRAM_Parity: Set or Reset the SRAM partiy enable bit.
837 * This parameter can be one of the following values:
838 * @arg OB_SRAM_PARITY_SET: Set SRAM partiy.
839 * @arg OB_SRAM_PARITY_RESET: Reset SRAM partiy.
840 * @retval None
842 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
844 FLASH_Status status = FLASH_COMPLETE;
846 /* Check the parameters */
847 assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
849 /* Wait for last operation to be completed */
850 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
852 if(status == FLASH_COMPLETE)
854 /* Enable the Option Bytes Programming operation */
855 FLASH->CR |= FLASH_CR_OPTPG;
857 OB->USER = OB_SRAM_Parity | 0xBF;
859 /* Wait for last operation to be completed */
860 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
862 if(status != FLASH_TIMEOUT)
864 /* if the program operation is completed, disable the OPTPG Bit */
865 FLASH->CR &= ~FLASH_CR_OPTPG;
868 /* Return the Option Byte program Status */
869 return status;
873 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY/ BOOT1 and OB_VDDA_ANALOG.
874 * @note To correctly run this function, the FLASH_OB_Unlock() function
875 * must be called before.
876 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
877 * (recommended to protect the FLASH memory against possible unwanted operation)
878 * @param OB_USER: Selects all user option bytes
879 * This parameter is a combination of the following values:
880 * @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
881 * @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
882 * @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
883 * @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
884 * @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
885 * @retval FLASH Status: The returned value can be:
886 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
888 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
890 FLASH_Status status = FLASH_COMPLETE;
892 /* Authorize the small information block programming */
893 FLASH->OPTKEYR = FLASH_KEY1;
894 FLASH->OPTKEYR = FLASH_KEY2;
896 /* Wait for last operation to be completed */
897 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
899 if(status == FLASH_COMPLETE)
901 /* Enable the Option Bytes Programming operation */
902 FLASH->CR |= FLASH_CR_OPTPG;
904 OB->USER = OB_USER | 0x88;
906 /* Wait for last operation to be completed */
907 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
909 if(status != FLASH_TIMEOUT)
911 /* if the program operation is completed, disable the OPTPG Bit */
912 FLASH->CR &= ~FLASH_CR_OPTPG;
915 /* Return the Option Byte program Status */
916 return status;
921 * @brief Programs a half word at a specified Option Byte Data address.
922 * @note To correctly run this function, the FLASH_OB_Unlock() function
923 * must be called before.
924 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
925 * (recommended to protect the FLASH memory against possible unwanted operation)
926 * @param Address: specifies the address to be programmed.
927 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
928 * @param Data: specifies the data to be programmed.
929 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
930 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
932 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
934 FLASH_Status status = FLASH_COMPLETE;
935 /* Check the parameters */
936 assert_param(IS_OB_DATA_ADDRESS(Address));
937 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
939 if(status == FLASH_COMPLETE)
941 /* Enables the Option Bytes Programming operation */
942 FLASH->CR |= FLASH_CR_OPTPG;
943 *(__IO uint16_t*)Address = Data;
945 /* Wait for last operation to be completed */
946 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
948 if(status != FLASH_TIMEOUT)
950 /* If the program operation is completed, disable the OPTPG Bit */
951 FLASH->CR &= ~FLASH_CR_OPTPG;
954 /* Return the Option Byte Data Program Status */
955 return status;
959 * @brief Returns the FLASH User Option Bytes values.
960 * @param None
961 * @retval The FLASH User Option Bytes .
963 uint8_t FLASH_OB_GetUser(void)
965 /* Return the User Option Byte */
966 return (uint8_t)(FLASH->OBR >> 8);
970 * @brief Returns the FLASH Write Protection Option Bytes value.
971 * @param None
972 * @retval The FLASH Write Protection Option Bytes value
974 uint32_t FLASH_OB_GetWRP(void)
976 /* Return the FLASH write protection Register value */
977 return (uint32_t)(FLASH->WRPR);
981 * @brief Checks whether the FLASH Read out Protection Status is set or not.
982 * @param None
983 * @retval FLASH ReadOut Protection Status(SET or RESET)
985 FlagStatus FLASH_OB_GetRDP(void)
987 FlagStatus readstatus = RESET;
989 if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
991 readstatus = SET;
993 else
995 readstatus = RESET;
997 return readstatus;
1001 * @}
1004 /** @defgroup FLASH_Group4 Interrupts and flags management functions
1005 * @brief Interrupts and flags management functions
1007 @verbatim
1008 ===============================================================================
1009 ##### Interrupts and flags management functions #####
1010 ===============================================================================
1012 @endverbatim
1013 * @{
1017 * @brief Enables or disables the specified FLASH interrupts.
1018 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
1019 * disabled.
1020 * This parameter can be any combination of the following values:
1021 * @arg FLASH_IT_EOP: FLASH end of programming Interrupt
1022 * @arg FLASH_IT_ERR: FLASH Error Interrupt
1023 * @retval None
1025 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
1027 /* Check the parameters */
1028 assert_param(IS_FLASH_IT(FLASH_IT));
1029 assert_param(IS_FUNCTIONAL_STATE(NewState));
1031 if(NewState != DISABLE)
1033 /* Enable the interrupt sources */
1034 FLASH->CR |= FLASH_IT;
1036 else
1038 /* Disable the interrupt sources */
1039 FLASH->CR &= ~(uint32_t)FLASH_IT;
1044 * @brief Checks whether the specified FLASH flag is set or not.
1045 * @param FLASH_FLAG: specifies the FLASH flag to check.
1046 * This parameter can be one of the following values:
1047 * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
1048 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
1049 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1050 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
1051 * @retval The new state of FLASH_FLAG (SET or RESET).
1053 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
1055 FlagStatus bitstatus = RESET;
1057 /* Check the parameters */
1058 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
1060 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
1062 bitstatus = SET;
1064 else
1066 bitstatus = RESET;
1068 /* Return the new state of FLASH_FLAG (SET or RESET) */
1069 return bitstatus;
1073 * @brief Clears the FLASH's pending flags.
1074 * @param FLASH_FLAG: specifies the FLASH flags to clear.
1075 * This parameter can be any combination of the following values:
1076 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
1077 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1078 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
1079 * @retval None
1081 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
1083 /* Check the parameters */
1084 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
1086 /* Clear the flags */
1087 FLASH->SR = FLASH_FLAG;
1091 * @brief Returns the FLASH Status.
1092 * @param None
1093 * @retval FLASH Status: The returned value can be:
1094 * FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
1096 FLASH_Status FLASH_GetStatus(void)
1098 FLASH_Status FLASHstatus = FLASH_COMPLETE;
1100 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
1102 FLASHstatus = FLASH_BUSY;
1104 else
1106 if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
1108 FLASHstatus = FLASH_ERROR_WRP;
1110 else
1112 if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
1114 FLASHstatus = FLASH_ERROR_PROGRAM;
1116 else
1118 FLASHstatus = FLASH_COMPLETE;
1122 /* Return the FLASH Status */
1123 return FLASHstatus;
1127 * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
1128 * @param Timeout: FLASH programming Timeout
1129 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
1130 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1132 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
1134 FLASH_Status status = FLASH_COMPLETE;
1136 /* Check for the FLASH Status */
1137 status = FLASH_GetStatus();
1139 /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
1140 while((status == FLASH_BUSY) && (Timeout != 0x00))
1142 status = FLASH_GetStatus();
1143 Timeout--;
1146 if(Timeout == 0x00 )
1148 status = FLASH_TIMEOUT;
1150 /* Return the operation status */
1151 return status;
1155 * @}
1159 * @}
1163 * @}
1167 * @}
1170 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/