2 ******************************************************************************
\r
3 * @file stm32f10x_flash.c
\r
4 * @author MCD Application Team
\r
7 * @brief This file provides all the FLASH firmware functions.
\r
8 ******************************************************************************
\r
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
\r
21 /* Includes ------------------------------------------------------------------*/
\r
22 #include "stm32f10x_flash.h"
\r
24 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
28 /** @defgroup FLASH
\r
29 * @brief FLASH driver modules
\r
33 /** @defgroup FLASH_Private_TypesDefinitions
\r
41 /** @defgroup FLASH_Private_Defines
\r
45 /* Flash Access Control Register bits */
\r
46 #define ACR_LATENCY_Mask ((uint32_t)0x00000038)
\r
47 #define ACR_HLFCYA_Mask ((uint32_t)0xFFFFFFF7)
\r
48 #define ACR_PRFTBE_Mask ((uint32_t)0xFFFFFFEF)
\r
50 /* Flash Access Control Register bits */
\r
51 #define ACR_PRFTBS_Mask ((uint32_t)0x00000020)
\r
53 /* Flash Control Register bits */
\r
54 #define CR_PG_Set ((uint32_t)0x00000001)
\r
55 #define CR_PG_Reset ((uint32_t)0x00001FFE)
\r
56 #define CR_PER_Set ((uint32_t)0x00000002)
\r
57 #define CR_PER_Reset ((uint32_t)0x00001FFD)
\r
58 #define CR_MER_Set ((uint32_t)0x00000004)
\r
59 #define CR_MER_Reset ((uint32_t)0x00001FFB)
\r
60 #define CR_OPTPG_Set ((uint32_t)0x00000010)
\r
61 #define CR_OPTPG_Reset ((uint32_t)0x00001FEF)
\r
62 #define CR_OPTER_Set ((uint32_t)0x00000020)
\r
63 #define CR_OPTER_Reset ((uint32_t)0x00001FDF)
\r
64 #define CR_STRT_Set ((uint32_t)0x00000040)
\r
65 #define CR_LOCK_Set ((uint32_t)0x00000080)
\r
68 #define RDPRT_Mask ((uint32_t)0x00000002)
\r
69 #define WRP0_Mask ((uint32_t)0x000000FF)
\r
70 #define WRP1_Mask ((uint32_t)0x0000FF00)
\r
71 #define WRP2_Mask ((uint32_t)0x00FF0000)
\r
72 #define WRP3_Mask ((uint32_t)0xFF000000)
\r
75 #define RDP_Key ((uint16_t)0x00A5)
\r
76 #define FLASH_KEY1 ((uint32_t)0x45670123)
\r
77 #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
\r
79 /* Delay definition */
\r
80 #define EraseTimeout ((uint32_t)0x00000FFF)
\r
81 #define ProgramTimeout ((uint32_t)0x0000000F)
\r
87 /** @defgroup FLASH_Private_Macros
\r
95 /** @defgroup FLASH_Private_Variables
\r
103 /** @defgroup FLASH_Private_FunctionPrototypes
\r
107 static void delay(void);
\r
112 /** @defgroup FLASH_Private_Functions
\r
117 * @brief Sets the code latency value.
\r
118 * @param FLASH_Latency: specifies the FLASH Latency value.
\r
119 * This parameter can be one of the following values:
\r
120 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
\r
121 * @arg FLASH_Latency_1: FLASH One Latency cycle
\r
122 * @arg FLASH_Latency_2: FLASH Two Latency cycles
\r
125 void FLASH_SetLatency(uint32_t FLASH_Latency)
\r
127 uint32_t tmpreg = 0;
\r
129 /* Check the parameters */
\r
130 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
\r
132 /* Read the ACR register */
\r
133 tmpreg = FLASH->ACR;
\r
135 /* Sets the Latency value */
\r
136 tmpreg &= ACR_LATENCY_Mask;
\r
137 tmpreg |= FLASH_Latency;
\r
139 /* Write the ACR register */
\r
140 FLASH->ACR = tmpreg;
\r
144 * @brief Enables or disables the Half cycle flash access.
\r
145 * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
\r
146 * This parameter can be one of the following values:
\r
147 * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
\r
148 * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
\r
151 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
\r
153 /* Check the parameters */
\r
154 assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
\r
156 /* Enable or disable the Half cycle access */
\r
157 FLASH->ACR &= ACR_HLFCYA_Mask;
\r
158 FLASH->ACR |= FLASH_HalfCycleAccess;
\r
162 * @brief Enables or disables the Prefetch Buffer.
\r
163 * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
\r
164 * This parameter can be one of the following values:
\r
165 * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
\r
166 * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
\r
169 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)
\r
171 /* Check the parameters */
\r
172 assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
\r
174 /* Enable or disable the Prefetch Buffer */
\r
175 FLASH->ACR &= ACR_PRFTBE_Mask;
\r
176 FLASH->ACR |= FLASH_PrefetchBuffer;
\r
180 * @brief Unlocks the FLASH Program Erase Controller.
\r
184 void FLASH_Unlock(void)
\r
186 /* Authorize the FPEC Access */
\r
187 FLASH->KEYR = FLASH_KEY1;
\r
188 FLASH->KEYR = FLASH_KEY2;
\r
192 * @brief Locks the FLASH Program Erase Controller.
\r
196 void FLASH_Lock(void)
\r
198 /* Set the Lock Bit to lock the FPEC and the FCR */
\r
199 FLASH->CR |= CR_LOCK_Set;
\r
203 * @brief Erases a specified FLASH page.
\r
204 * @param Page_Address: The page address to be erased.
\r
205 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
\r
206 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
208 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
\r
210 FLASH_Status status = FLASH_COMPLETE;
\r
211 /* Check the parameters */
\r
212 assert_param(IS_FLASH_ADDRESS(Page_Address));
\r
213 /* Wait for last operation to be completed */
\r
214 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
216 if(status == FLASH_COMPLETE)
\r
218 /* if the previous operation is completed, proceed to erase the page */
\r
219 FLASH->CR|= CR_PER_Set;
\r
220 FLASH->AR = Page_Address;
\r
221 FLASH->CR|= CR_STRT_Set;
\r
223 /* Wait for last operation to be completed */
\r
224 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
225 if(status != FLASH_TIMEOUT)
\r
227 /* if the erase operation is completed, disable the PER Bit */
\r
228 FLASH->CR &= CR_PER_Reset;
\r
231 /* Return the Erase Status */
\r
236 * @brief Erases all FLASH pages.
\r
238 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
239 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
241 FLASH_Status FLASH_EraseAllPages(void)
\r
243 FLASH_Status status = FLASH_COMPLETE;
\r
244 /* Wait for last operation to be completed */
\r
245 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
247 if(status == FLASH_COMPLETE)
\r
249 /* if the previous operation is completed, proceed to erase all pages */
\r
250 FLASH->CR |= CR_MER_Set;
\r
251 FLASH->CR |= CR_STRT_Set;
\r
253 /* Wait for last operation to be completed */
\r
254 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
255 if(status != FLASH_TIMEOUT)
\r
257 /* if the erase operation is completed, disable the MER Bit */
\r
258 FLASH->CR &= CR_MER_Reset;
\r
261 /* Return the Erase Status */
\r
266 * @brief Erases the FLASH option bytes.
\r
267 * @note This functions erases all option bytes and then deactivates the Read
\r
268 * protection. If the user needs to keep the Read protection activated,
\r
269 * he has to enable it after this function call (using
\r
270 * FLASH_ReadOutProtection function)
\r
272 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
273 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
275 FLASH_Status FLASH_EraseOptionBytes(void)
\r
277 FLASH_Status status = FLASH_COMPLETE;
\r
279 /* Wait for last operation to be completed */
\r
280 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
281 if(status == FLASH_COMPLETE)
\r
283 /* Authorize the small information block programming */
\r
284 FLASH->OPTKEYR = FLASH_KEY1;
\r
285 FLASH->OPTKEYR = FLASH_KEY2;
\r
287 /* if the previous operation is completed, proceed to erase the option bytes */
\r
288 FLASH->CR |= CR_OPTER_Set;
\r
289 FLASH->CR |= CR_STRT_Set;
\r
290 /* Wait for last operation to be completed */
\r
291 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
293 if(status == FLASH_COMPLETE)
\r
295 /* if the erase operation is completed, disable the OPTER Bit */
\r
296 FLASH->CR &= CR_OPTER_Reset;
\r
298 /* Enable the Option Bytes Programming operation */
\r
299 FLASH->CR |= CR_OPTPG_Set;
\r
300 /* Disable the Read protection */
\r
302 /* Wait for last operation to be completed */
\r
303 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
305 if(status != FLASH_TIMEOUT)
\r
307 /* if the program operation is completed, disable the OPTPG Bit */
\r
308 FLASH->CR &= CR_OPTPG_Reset;
\r
313 if (status != FLASH_TIMEOUT)
\r
315 /* Disable the OPTPG Bit */
\r
316 FLASH->CR &= CR_OPTPG_Reset;
\r
320 /* Return the erase status */
\r
325 * @brief Programs a word at a specified address.
\r
326 * @param Address: specifies the address to be programmed.
\r
327 * @param Data: specifies the data to be programmed.
\r
328 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
329 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
331 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
\r
333 FLASH_Status status = FLASH_COMPLETE;
\r
334 __IO uint32_t tmp = 0;
\r
336 /* Check the parameters */
\r
337 assert_param(IS_FLASH_ADDRESS(Address));
\r
338 /* Wait for last operation to be completed */
\r
339 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
341 if(status == FLASH_COMPLETE)
\r
343 /* if the previous operation is completed, proceed to program the new first
\r
345 FLASH->CR |= CR_PG_Set;
\r
347 *(__IO uint16_t*)Address = (uint16_t)Data;
\r
348 /* Wait for last operation to be completed */
\r
349 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
351 if(status == FLASH_COMPLETE)
\r
353 /* if the previous operation is completed, proceed to program the new second
\r
357 *(__IO uint16_t*) tmp = Data >> 16;
\r
359 /* Wait for last operation to be completed */
\r
360 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
362 if(status != FLASH_TIMEOUT)
\r
364 /* Disable the PG Bit */
\r
365 FLASH->CR &= CR_PG_Reset;
\r
370 if (status != FLASH_TIMEOUT)
\r
372 /* Disable the PG Bit */
\r
373 FLASH->CR &= CR_PG_Reset;
\r
377 /* Return the Program Status */
\r
382 * @brief Programs a half word at a specified address.
\r
383 * @param Address: specifies the address to be programmed.
\r
384 * @param Data: specifies the data to be programmed.
\r
385 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
386 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
388 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
\r
390 FLASH_Status status = FLASH_COMPLETE;
\r
391 /* Check the parameters */
\r
392 assert_param(IS_FLASH_ADDRESS(Address));
\r
393 /* Wait for last operation to be completed */
\r
394 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
396 if(status == FLASH_COMPLETE)
\r
398 /* if the previous operation is completed, proceed to program the new data */
\r
399 FLASH->CR |= CR_PG_Set;
\r
401 *(__IO uint16_t*)Address = Data;
\r
402 /* Wait for last operation to be completed */
\r
403 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
404 if(status != FLASH_TIMEOUT)
\r
406 /* if the program operation is completed, disable the PG Bit */
\r
407 FLASH->CR &= CR_PG_Reset;
\r
410 /* Return the Program Status */
\r
415 * @brief Programs a half word at a specified Option Byte Data address.
\r
416 * @param Address: specifies the address to be programmed.
\r
417 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
\r
418 * @param Data: specifies the data to be programmed.
\r
419 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
420 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
422 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
\r
424 FLASH_Status status = FLASH_COMPLETE;
\r
425 /* Check the parameters */
\r
426 assert_param(IS_OB_DATA_ADDRESS(Address));
\r
427 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
428 if(status == FLASH_COMPLETE)
\r
430 /* Authorize the small information block programming */
\r
431 FLASH->OPTKEYR = FLASH_KEY1;
\r
432 FLASH->OPTKEYR = FLASH_KEY2;
\r
433 /* Enables the Option Bytes Programming operation */
\r
434 FLASH->CR |= CR_OPTPG_Set;
\r
435 *(__IO uint16_t*)Address = Data;
\r
437 /* Wait for last operation to be completed */
\r
438 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
439 if(status != FLASH_TIMEOUT)
\r
441 /* if the program operation is completed, disable the OPTPG Bit */
\r
442 FLASH->CR &= CR_OPTPG_Reset;
\r
445 /* Return the Option Byte Data Program Status */
\r
450 * @brief Write protects the desired pages
\r
451 * @param FLASH_Pages: specifies the address of the pages to be write protected.
\r
452 * This parameter can be:
\r
453 * @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31
\r
454 * @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3
\r
455 * and FLASH_WRProt_Pages124to127
\r
456 * @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and
\r
457 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
\r
458 * @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and
\r
459 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127
\r
460 * @arg FLASH_WRProt_AllPages
\r
461 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
462 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
464 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
\r
466 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
\r
468 FLASH_Status status = FLASH_COMPLETE;
\r
470 /* Check the parameters */
\r
471 assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
\r
473 FLASH_Pages = (uint32_t)(~FLASH_Pages);
\r
474 WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);
\r
475 WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);
\r
476 WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);
\r
477 WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);
\r
479 /* Wait for last operation to be completed */
\r
480 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
482 if(status == FLASH_COMPLETE)
\r
484 /* Authorizes the small information block programming */
\r
485 FLASH->OPTKEYR = FLASH_KEY1;
\r
486 FLASH->OPTKEYR = FLASH_KEY2;
\r
487 FLASH->CR |= CR_OPTPG_Set;
\r
488 if(WRP0_Data != 0xFF)
\r
490 OB->WRP0 = WRP0_Data;
\r
492 /* Wait for last operation to be completed */
\r
493 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
495 if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
\r
497 OB->WRP1 = WRP1_Data;
\r
499 /* Wait for last operation to be completed */
\r
500 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
502 if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
\r
504 OB->WRP2 = WRP2_Data;
\r
506 /* Wait for last operation to be completed */
\r
507 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
510 if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
\r
512 OB->WRP3 = WRP3_Data;
\r
514 /* Wait for last operation to be completed */
\r
515 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
518 if(status != FLASH_TIMEOUT)
\r
520 /* if the program operation is completed, disable the OPTPG Bit */
\r
521 FLASH->CR &= CR_OPTPG_Reset;
\r
524 /* Return the write protection operation Status */
\r
529 * @brief Enables or disables the read out protection.
\r
530 * @note If the user has already programmed the other option bytes before calling
\r
531 * this function, he must re-program them since this function erases all option bytes.
\r
532 * @param Newstate: new state of the ReadOut Protection.
\r
533 * This parameter can be: ENABLE or DISABLE.
\r
534 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
535 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
537 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
\r
539 FLASH_Status status = FLASH_COMPLETE;
\r
540 /* Check the parameters */
\r
541 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
542 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
543 if(status == FLASH_COMPLETE)
\r
545 /* Authorizes the small information block programming */
\r
546 FLASH->OPTKEYR = FLASH_KEY1;
\r
547 FLASH->OPTKEYR = FLASH_KEY2;
\r
548 FLASH->CR |= CR_OPTER_Set;
\r
549 FLASH->CR |= CR_STRT_Set;
\r
550 /* Wait for last operation to be completed */
\r
551 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
552 if(status == FLASH_COMPLETE)
\r
554 /* if the erase operation is completed, disable the OPTER Bit */
\r
555 FLASH->CR &= CR_OPTER_Reset;
\r
556 /* Enable the Option Bytes Programming operation */
\r
557 FLASH->CR |= CR_OPTPG_Set;
\r
558 if(NewState != DISABLE)
\r
564 OB->RDP = RDP_Key;
\r
566 /* Wait for last operation to be completed */
\r
567 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
569 if(status != FLASH_TIMEOUT)
\r
571 /* if the program operation is completed, disable the OPTPG Bit */
\r
572 FLASH->CR &= CR_OPTPG_Reset;
\r
577 if(status != FLASH_TIMEOUT)
\r
579 /* Disable the OPTER Bit */
\r
580 FLASH->CR &= CR_OPTER_Reset;
\r
584 /* Return the protection operation Status */
\r
589 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
\r
590 * @param OB_IWDG: Selects the IWDG mode
\r
591 * This parameter can be one of the following values:
\r
592 * @arg OB_IWDG_SW: Software IWDG selected
\r
593 * @arg OB_IWDG_HW: Hardware IWDG selected
\r
594 * @param OB_STOP: Reset event when entering STOP mode.
\r
595 * This parameter can be one of the following values:
\r
596 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
\r
597 * @arg OB_STOP_RST: Reset generated when entering in STOP
\r
598 * @param OB_STDBY: Reset event when entering Standby mode.
\r
599 * This parameter can be one of the following values:
\r
600 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
\r
601 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
\r
602 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
603 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
605 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
\r
607 FLASH_Status status = FLASH_COMPLETE;
\r
609 /* Check the parameters */
\r
610 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
\r
611 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
\r
612 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
\r
614 /* Authorize the small information block programming */
\r
615 FLASH->OPTKEYR = FLASH_KEY1;
\r
616 FLASH->OPTKEYR = FLASH_KEY2;
\r
618 /* Wait for last operation to be completed */
\r
619 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
621 if(status == FLASH_COMPLETE)
\r
623 /* Enable the Option Bytes Programming operation */
\r
624 FLASH->CR |= CR_OPTPG_Set;
\r
626 OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8)));
\r
628 /* Wait for last operation to be completed */
\r
629 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
630 if(status != FLASH_TIMEOUT)
\r
632 /* if the program operation is completed, disable the OPTPG Bit */
\r
633 FLASH->CR &= CR_OPTPG_Reset;
\r
636 /* Return the Option Byte program Status */
\r
641 * @brief Returns the FLASH User Option Bytes values.
\r
643 * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
\r
644 * and RST_STDBY(Bit2).
\r
646 uint32_t FLASH_GetUserOptionByte(void)
\r
648 /* Return the User Option Byte */
\r
649 return (uint32_t)(FLASH->OBR >> 2);
\r
653 * @brief Returns the FLASH Write Protection Option Bytes Register value.
\r
655 * @retval The FLASH Write Protection Option Bytes Register value
\r
657 uint32_t FLASH_GetWriteProtectionOptionByte(void)
\r
659 /* Return the Falsh write protection Register value */
\r
660 return (uint32_t)(FLASH->WRPR);
\r
664 * @brief Checks whether the FLASH Read Out Protection Status is set or not.
\r
666 * @retval FLASH ReadOut Protection Status(SET or RESET)
\r
668 FlagStatus FLASH_GetReadOutProtectionStatus(void)
\r
670 FlagStatus readoutstatus = RESET;
\r
671 if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)
\r
673 readoutstatus = SET;
\r
677 readoutstatus = RESET;
\r
679 return readoutstatus;
\r
683 * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
\r
685 * @retval FLASH Prefetch Buffer Status (SET or RESET).
\r
687 FlagStatus FLASH_GetPrefetchBufferStatus(void)
\r
689 FlagStatus bitstatus = RESET;
\r
691 if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)
\r
699 /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
\r
704 * @brief Enables or disables the specified FLASH interrupts.
\r
705 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
\r
706 * This parameter can be any combination of the following values:
\r
707 * @arg FLASH_IT_ERROR: FLASH Error Interrupt
\r
708 * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
\r
709 * @param NewState: new state of the specified Flash interrupts.
\r
710 * This parameter can be: ENABLE or DISABLE.
\r
713 void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState)
\r
715 /* Check the parameters */
\r
716 assert_param(IS_FLASH_IT(FLASH_IT));
\r
717 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
718 if(NewState != DISABLE)
\r
720 /* Enable the interrupt sources */
\r
721 FLASH->CR |= FLASH_IT;
\r
725 /* Disable the interrupt sources */
\r
726 FLASH->CR &= ~(uint32_t)FLASH_IT;
\r
731 * @brief Checks whether the specified FLASH flag is set or not.
\r
732 * @param FLASH_FLAG: specifies the FLASH flag to check.
\r
733 * This parameter can be one of the following values:
\r
734 * @arg FLASH_FLAG_BSY: FLASH Busy flag
\r
735 * @arg FLASH_FLAG_PGERR: FLASH Program error flag
\r
736 * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
\r
737 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
\r
738 * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag
\r
739 * @retval The new state of FLASH_FLAG (SET or RESET).
\r
741 FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG)
\r
743 FlagStatus bitstatus = RESET;
\r
744 /* Check the parameters */
\r
745 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
\r
746 if(FLASH_FLAG == FLASH_FLAG_OPTERR)
\r
748 if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
\r
759 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
\r
768 /* Return the new state of FLASH_FLAG (SET or RESET) */
\r
773 * @brief Clears the FLASH
\92s pending flags.
\r
774 * @param FLASH_FLAG: specifies the FLASH flags to clear.
\r
775 * This parameter can be any combination of the following values:
\r
776 * @arg FLASH_FLAG_PGERR: FLASH Program error flag
\r
777 * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
\r
778 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
\r
781 void FLASH_ClearFlag(uint16_t FLASH_FLAG)
\r
783 /* Check the parameters */
\r
784 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
\r
786 /* Clear the flags */
\r
787 FLASH->SR = FLASH_FLAG;
\r
791 * @brief Returns the FLASH Status.
\r
793 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
\r
794 * FLASH_ERROR_WRP or FLASH_COMPLETE
\r
796 FLASH_Status FLASH_GetStatus(void)
\r
798 FLASH_Status flashstatus = FLASH_COMPLETE;
\r
800 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
\r
802 flashstatus = FLASH_BUSY;
\r
806 if((FLASH->SR & FLASH_FLAG_PGERR) != 0)
\r
808 flashstatus = FLASH_ERROR_PG;
\r
812 if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )
\r
814 flashstatus = FLASH_ERROR_WRP;
\r
818 flashstatus = FLASH_COMPLETE;
\r
822 /* Return the Flash Status */
\r
823 return flashstatus;
\r
827 * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
\r
828 * @param Timeout: FLASH progamming Timeout
\r
829 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
830 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
832 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
\r
834 FLASH_Status status = FLASH_COMPLETE;
\r
836 /* Check for the Flash Status */
\r
837 status = FLASH_GetStatus();
\r
838 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
\r
839 while((status == FLASH_BUSY) && (Timeout != 0x00))
\r
842 status = FLASH_GetStatus();
\r
845 if(Timeout == 0x00 )
\r
847 status = FLASH_TIMEOUT;
\r
849 /* Return the operation status */
\r
854 * @brief Inserts a time delay.
\r
858 static void delay(void)
\r
860 __IO uint32_t i = 0;
\r
861 for(i = 0xFF; i != 0; i--)
\r
878 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
\r