2 ******************************************************************************
3 * @file stm32f10x_flash.c
4 * @author MCD Application Team
7 * @brief This file provides all the FLASH firmware functions.
8 ******************************************************************************
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x_flash.h"
25 /** @addtogroup STM32F10x_StdPeriph_Driver
30 * @brief FLASH driver modules
34 /** @defgroup FLASH_Private_TypesDefinitions
42 /** @defgroup FLASH_Private_Defines
46 /* Flash Access Control Register bits */
47 #define ACR_LATENCY_Mask ((uint32_t)0x00000038)
48 #define ACR_HLFCYA_Mask ((uint32_t)0xFFFFFFF7)
49 #define ACR_PRFTBE_Mask ((uint32_t)0xFFFFFFEF)
51 /* Flash Access Control Register bits */
52 #define ACR_PRFTBS_Mask ((uint32_t)0x00000020)
54 /* Flash Control Register bits */
55 #define CR_PG_Set ((uint32_t)0x00000001)
56 #define CR_PG_Reset ((uint32_t)0x00001FFE)
57 #define CR_PER_Set ((uint32_t)0x00000002)
58 #define CR_PER_Reset ((uint32_t)0x00001FFD)
59 #define CR_MER_Set ((uint32_t)0x00000004)
60 #define CR_MER_Reset ((uint32_t)0x00001FFB)
61 #define CR_OPTPG_Set ((uint32_t)0x00000010)
62 #define CR_OPTPG_Reset ((uint32_t)0x00001FEF)
63 #define CR_OPTER_Set ((uint32_t)0x00000020)
64 #define CR_OPTER_Reset ((uint32_t)0x00001FDF)
65 #define CR_STRT_Set ((uint32_t)0x00000040)
66 #define CR_LOCK_Set ((uint32_t)0x00000080)
69 #define RDPRT_Mask ((uint32_t)0x00000002)
70 #define WRP0_Mask ((uint32_t)0x000000FF)
71 #define WRP1_Mask ((uint32_t)0x0000FF00)
72 #define WRP2_Mask ((uint32_t)0x00FF0000)
73 #define WRP3_Mask ((uint32_t)0xFF000000)
74 #define OB_USER_BFB2 ((uint16_t)0x0008)
77 #define RDP_Key ((uint16_t)0x00A5)
78 #define FLASH_KEY1 ((uint32_t)0x45670123)
79 #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
81 /* FLASH BANK address */
82 #define FLASH_BANK1_END_ADDRESS ((uint32_t)0x807FFFF)
84 /* Delay definition */
85 #define EraseTimeout ((uint32_t)0x000B0000)
86 #define ProgramTimeout ((uint32_t)0x00002000)
91 /** @defgroup FLASH_Private_Macros
99 /** @defgroup FLASH_Private_Variables
107 /** @defgroup FLASH_Private_FunctionPrototypes
115 /** @defgroup FLASH_Private_Functions
122 This driver provides functions to configure and program the Flash memory of all STM32F10x devices,
123 including the latest STM32F10x_XL density devices.
125 STM32F10x_XL devices feature up to 1 Mbyte with dual bank architecture for read-while-write (RWW) capability:
126 - bank1: fixed size of 512 Kbytes (256 pages of 2Kbytes each)
127 - bank2: up to 512 Kbytes (up to 256 pages of 2Kbytes each)
128 While other STM32F10x devices features only one bank with memory up to 512 Kbytes.
130 In version V3.3.0, some functions were updated and new ones were added to support
131 STM32F10x_XL devices. Thus some functions manages all devices, while other are
132 dedicated for XL devices only.
134 The table below presents the list of available functions depending on the used STM32F10x devices.
136 ***************************************************
137 * Legacy functions used for all STM32F10x devices *
138 ***************************************************
139 +----------------------------------------------------------------------------------------------------------------------------------+
140 | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
141 | | devices | devices | |
142 |----------------------------------------------------------------------------------------------------------------------------------|
143 |FLASH_SetLatency | Yes | Yes | No change |
144 |----------------------------------------------------------------------------------------------------------------------------------|
145 |FLASH_HalfCycleAccessCmd | Yes | Yes | No change |
146 |----------------------------------------------------------------------------------------------------------------------------------|
147 |FLASH_PrefetchBufferCmd | Yes | Yes | No change |
148 |----------------------------------------------------------------------------------------------------------------------------------|
149 |FLASH_Unlock | Yes | Yes | - For STM32F10X_XL devices: unlock Bank1 and Bank2. |
150 | | | | - For other devices: unlock Bank1 and it is equivalent |
151 | | | | to FLASH_UnlockBank1 function. |
152 |----------------------------------------------------------------------------------------------------------------------------------|
153 |FLASH_Lock | Yes | Yes | - For STM32F10X_XL devices: lock Bank1 and Bank2. |
154 | | | | - For other devices: lock Bank1 and it is equivalent |
155 | | | | to FLASH_LockBank1 function. |
156 |----------------------------------------------------------------------------------------------------------------------------------|
157 |FLASH_ErasePage | Yes | Yes | - For STM32F10x_XL devices: erase a page in Bank1 and Bank2 |
158 | | | | - For other devices: erase a page in Bank1 |
159 |----------------------------------------------------------------------------------------------------------------------------------|
160 |FLASH_EraseAllPages | Yes | Yes | - For STM32F10x_XL devices: erase all pages in Bank1 and Bank2 |
161 | | | | - For other devices: erase all pages in Bank1 |
162 |----------------------------------------------------------------------------------------------------------------------------------|
163 |FLASH_EraseOptionBytes | Yes | Yes | No change |
164 |----------------------------------------------------------------------------------------------------------------------------------|
165 |FLASH_ProgramWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |
166 |----------------------------------------------------------------------------------------------------------------------------------|
167 |FLASH_ProgramHalfWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |
168 |----------------------------------------------------------------------------------------------------------------------------------|
169 |FLASH_ProgramOptionByteData | Yes | Yes | No change |
170 |----------------------------------------------------------------------------------------------------------------------------------|
171 |FLASH_EnableWriteProtection | Yes | Yes | No change |
172 |----------------------------------------------------------------------------------------------------------------------------------|
173 |FLASH_ReadOutProtection | Yes | Yes | No change |
174 |----------------------------------------------------------------------------------------------------------------------------------|
175 |FLASH_UserOptionByteConfig | Yes | Yes | No change |
176 |----------------------------------------------------------------------------------------------------------------------------------|
177 |FLASH_GetUserOptionByte | Yes | Yes | No change |
178 |----------------------------------------------------------------------------------------------------------------------------------|
179 |FLASH_GetWriteProtectionOptionByte | Yes | Yes | No change |
180 |----------------------------------------------------------------------------------------------------------------------------------|
181 |FLASH_GetReadOutProtectionStatus | Yes | Yes | No change |
182 |----------------------------------------------------------------------------------------------------------------------------------|
183 |FLASH_GetPrefetchBufferStatus | Yes | Yes | No change |
184 |----------------------------------------------------------------------------------------------------------------------------------|
185 |FLASH_ITConfig | Yes | Yes | - For STM32F10x_XL devices: enable Bank1 and Bank2's interrupts|
186 | | | | - For other devices: enable Bank1's interrupts |
187 |----------------------------------------------------------------------------------------------------------------------------------|
188 |FLASH_GetFlagStatus | Yes | Yes | - For STM32F10x_XL devices: return Bank1 and Bank2's flag status|
189 | | | | - For other devices: return Bank1's flag status |
190 |----------------------------------------------------------------------------------------------------------------------------------|
191 |FLASH_ClearFlag | Yes | Yes | - For STM32F10x_XL devices: clear Bank1 and Bank2's flag |
192 | | | | - For other devices: clear Bank1's flag |
193 |----------------------------------------------------------------------------------------------------------------------------------|
194 |FLASH_GetStatus | Yes | Yes | - Return the status of Bank1 (for all devices) |
195 | | | | equivalent to FLASH_GetBank1Status function |
196 |----------------------------------------------------------------------------------------------------------------------------------|
197 |FLASH_WaitForLastOperation | Yes | Yes | - Wait for Bank1 last operation (for all devices) |
198 | | | | equivalent to: FLASH_WaitForLastBank1Operation function |
199 +----------------------------------------------------------------------------------------------------------------------------------+
201 ************************************************************************************************************************
202 * New functions used for all STM32F10x devices to manage Bank1: *
203 * - These functions are mainly useful for STM32F10x_XL density devices, to have separate control for Bank1 and bank2 *
204 * - For other devices, these functions are optional (covered by functions listed above) *
205 ************************************************************************************************************************
206 +----------------------------------------------------------------------------------------------------------------------------------+
207 | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
208 | | devices | devices | |
209 |----------------------------------------------------------------------------------------------------------------------------------|
210 | FLASH_UnlockBank1 | Yes | Yes | - Unlock Bank1 |
211 |----------------------------------------------------------------------------------------------------------------------------------|
212 |FLASH_LockBank1 | Yes | Yes | - Lock Bank1 |
213 |----------------------------------------------------------------------------------------------------------------------------------|
214 | FLASH_EraseAllBank1Pages | Yes | Yes | - Erase all pages in Bank1 |
215 |----------------------------------------------------------------------------------------------------------------------------------|
216 | FLASH_GetBank1Status | Yes | Yes | - Return the status of Bank1 |
217 |----------------------------------------------------------------------------------------------------------------------------------|
218 | FLASH_WaitForLastBank1Operation | Yes | Yes | - Wait for Bank1 last operation |
219 +----------------------------------------------------------------------------------------------------------------------------------+
221 *****************************************************************************
222 * New Functions used only with STM32F10x_XL density devices to manage Bank2 *
223 *****************************************************************************
224 +----------------------------------------------------------------------------------------------------------------------------------+
225 | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
226 | | devices | devices | |
227 |----------------------------------------------------------------------------------------------------------------------------------|
228 | FLASH_UnlockBank2 | Yes | No | - Unlock Bank2 |
229 |----------------------------------------------------------------------------------------------------------------------------------|
230 |FLASH_LockBank2 | Yes | No | - Lock Bank2 |
231 |----------------------------------------------------------------------------------------------------------------------------------|
232 | FLASH_EraseAllBank2Pages | Yes | No | - Erase all pages in Bank2 |
233 |----------------------------------------------------------------------------------------------------------------------------------|
234 | FLASH_GetBank2Status | Yes | No | - Return the status of Bank2 |
235 |----------------------------------------------------------------------------------------------------------------------------------|
236 | FLASH_WaitForLastBank2Operation | Yes | No | - Wait for Bank2 last operation |
237 |----------------------------------------------------------------------------------------------------------------------------------|
238 | FLASH_BootConfig | Yes | No | - Configure to boot from Bank1 or Bank2 |
239 +----------------------------------------------------------------------------------------------------------------------------------+
245 * @brief Sets the code latency value.
246 * @note This function can be used for all STM32F10x devices.
247 * @param FLASH_Latency: specifies the FLASH Latency value.
248 * This parameter can be one of the following values:
249 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
250 * @arg FLASH_Latency_1: FLASH One Latency cycle
251 * @arg FLASH_Latency_2: FLASH Two Latency cycles
254 void FLASH_SetLatency(uint32_t FLASH_Latency
)
258 /* Check the parameters */
259 assert_param(IS_FLASH_LATENCY(FLASH_Latency
));
261 /* Read the ACR register */
264 /* Sets the Latency value */
265 tmpreg
&= ACR_LATENCY_Mask
;
266 tmpreg
|= FLASH_Latency
;
268 /* Write the ACR register */
273 * @brief Enables or disables the Half cycle flash access.
274 * @note This function can be used for all STM32F10x devices.
275 * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
276 * This parameter can be one of the following values:
277 * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
278 * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
281 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess
)
283 /* Check the parameters */
284 assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess
));
286 /* Enable or disable the Half cycle access */
287 FLASH
->ACR
&= ACR_HLFCYA_Mask
;
288 FLASH
->ACR
|= FLASH_HalfCycleAccess
;
292 * @brief Enables or disables the Prefetch Buffer.
293 * @note This function can be used for all STM32F10x devices.
294 * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
295 * This parameter can be one of the following values:
296 * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
297 * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
300 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer
)
302 /* Check the parameters */
303 assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer
));
305 /* Enable or disable the Prefetch Buffer */
306 FLASH
->ACR
&= ACR_PRFTBE_Mask
;
307 FLASH
->ACR
|= FLASH_PrefetchBuffer
;
311 * @brief Unlocks the FLASH Program Erase Controller.
312 * @note This function can be used for all STM32F10x devices.
313 * - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.
314 * - For all other devices it unlocks Bank1 and it is equivalent
315 * to FLASH_UnlockBank1 function..
319 void FLASH_Unlock(void)
321 /* Authorize the FPEC of Bank1 Access */
322 FLASH
->KEYR
= FLASH_KEY1
;
323 FLASH
->KEYR
= FLASH_KEY2
;
326 /* Authorize the FPEC of Bank2 Access */
327 FLASH
->KEYR2
= FLASH_KEY1
;
328 FLASH
->KEYR2
= FLASH_KEY2
;
329 #endif /* STM32F10X_XL */
332 * @brief Unlocks the FLASH Bank1 Program Erase Controller.
333 * @note This function can be used for all STM32F10x devices.
334 * - For STM32F10X_XL devices this function unlocks Bank1.
335 * - For all other devices it unlocks Bank1 and it is
336 * equivalent to FLASH_Unlock function.
340 void FLASH_UnlockBank1(void)
342 /* Authorize the FPEC of Bank1 Access */
343 FLASH
->KEYR
= FLASH_KEY1
;
344 FLASH
->KEYR
= FLASH_KEY2
;
349 * @brief Unlocks the FLASH Bank2 Program Erase Controller.
350 * @note This function can be used only for STM32F10X_XL density devices.
354 void FLASH_UnlockBank2(void)
356 /* Authorize the FPEC of Bank2 Access */
357 FLASH
->KEYR2
= FLASH_KEY1
;
358 FLASH
->KEYR2
= FLASH_KEY2
;
361 #endif /* STM32F10X_XL */
364 * @brief Locks the FLASH Program Erase Controller.
365 * @note This function can be used for all STM32F10x devices.
366 * - For STM32F10X_XL devices this function Locks Bank1 and Bank2.
367 * - For all other devices it Locks Bank1 and it is equivalent
368 * to FLASH_LockBank1 function.
372 void FLASH_Lock(void)
374 /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */
375 FLASH
->CR
|= CR_LOCK_Set
;
378 /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */
379 FLASH
->CR2
|= CR_LOCK_Set
;
380 #endif /* STM32F10X_XL */
384 * @brief Locks the FLASH Bank1 Program Erase Controller.
385 * @note this function can be used for all STM32F10x devices.
386 * - For STM32F10X_XL devices this function Locks Bank1.
387 * - For all other devices it Locks Bank1 and it is equivalent
388 * to FLASH_Lock function.
392 void FLASH_LockBank1(void)
394 /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */
395 FLASH
->CR
|= CR_LOCK_Set
;
400 * @brief Locks the FLASH Bank2 Program Erase Controller.
401 * @note This function can be used only for STM32F10X_XL density devices.
405 void FLASH_LockBank2(void)
407 /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */
408 FLASH
->CR2
|= CR_LOCK_Set
;
410 #endif /* STM32F10X_XL */
413 * @brief Erases a specified FLASH page.
414 * @note This function can be used for all STM32F10x devices.
415 * @param Page_Address: The page address to be erased.
416 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
417 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
419 FLASH_Status
FLASH_ErasePage(uint32_t Page_Address
)
421 FLASH_Status status
= FLASH_COMPLETE
;
422 /* Check the parameters */
423 assert_param(IS_FLASH_ADDRESS(Page_Address
));
426 if(Page_Address
< FLASH_BANK1_END_ADDRESS
)
428 /* Wait for last operation to be completed */
429 status
= FLASH_WaitForLastBank1Operation(EraseTimeout
);
430 if(status
== FLASH_COMPLETE
)
432 /* if the previous operation is completed, proceed to erase the page */
433 FLASH
->CR
|= CR_PER_Set
;
434 FLASH
->AR
= Page_Address
;
435 FLASH
->CR
|= CR_STRT_Set
;
437 /* Wait for last operation to be completed */
438 status
= FLASH_WaitForLastBank1Operation(EraseTimeout
);
440 /* Disable the PER Bit */
441 FLASH
->CR
&= CR_PER_Reset
;
446 /* Wait for last operation to be completed */
447 status
= FLASH_WaitForLastBank2Operation(EraseTimeout
);
448 if(status
== FLASH_COMPLETE
)
450 /* if the previous operation is completed, proceed to erase the page */
451 FLASH
->CR2
|= CR_PER_Set
;
452 FLASH
->AR2
= Page_Address
;
453 FLASH
->CR2
|= CR_STRT_Set
;
455 /* Wait for last operation to be completed */
456 status
= FLASH_WaitForLastBank2Operation(EraseTimeout
);
458 /* Disable the PER Bit */
459 FLASH
->CR2
&= CR_PER_Reset
;
463 /* Wait for last operation to be completed */
464 status
= FLASH_WaitForLastOperation(EraseTimeout
);
466 if(status
== FLASH_COMPLETE
)
468 /* if the previous operation is completed, proceed to erase the page */
469 FLASH
->CR
|= CR_PER_Set
;
470 FLASH
->AR
= Page_Address
;
471 FLASH
->CR
|= CR_STRT_Set
;
473 /* Wait for last operation to be completed */
474 status
= FLASH_WaitForLastOperation(EraseTimeout
);
476 /* Disable the PER Bit */
477 FLASH
->CR
&= CR_PER_Reset
;
479 #endif /* STM32F10X_XL */
481 /* Return the Erase Status */
486 * @brief Erases all FLASH pages.
487 * @note This function can be used for all STM32F10x devices.
489 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
490 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
492 FLASH_Status
FLASH_EraseAllPages(void)
494 FLASH_Status status
= FLASH_COMPLETE
;
497 /* Wait for last operation to be completed */
498 status
= FLASH_WaitForLastBank1Operation(EraseTimeout
);
500 if(status
== FLASH_COMPLETE
)
502 /* if the previous operation is completed, proceed to erase all pages */
503 FLASH
->CR
|= CR_MER_Set
;
504 FLASH
->CR
|= CR_STRT_Set
;
506 /* Wait for last operation to be completed */
507 status
= FLASH_WaitForLastBank1Operation(EraseTimeout
);
509 /* Disable the MER Bit */
510 FLASH
->CR
&= CR_MER_Reset
;
512 if(status
== FLASH_COMPLETE
)
514 /* if the previous operation is completed, proceed to erase all pages */
515 FLASH
->CR2
|= CR_MER_Set
;
516 FLASH
->CR2
|= CR_STRT_Set
;
518 /* Wait for last operation to be completed */
519 status
= FLASH_WaitForLastBank2Operation(EraseTimeout
);
521 /* Disable the MER Bit */
522 FLASH
->CR2
&= CR_MER_Reset
;
525 /* Wait for last operation to be completed */
526 status
= FLASH_WaitForLastOperation(EraseTimeout
);
527 if(status
== FLASH_COMPLETE
)
529 /* if the previous operation is completed, proceed to erase all pages */
530 FLASH
->CR
|= CR_MER_Set
;
531 FLASH
->CR
|= CR_STRT_Set
;
533 /* Wait for last operation to be completed */
534 status
= FLASH_WaitForLastOperation(EraseTimeout
);
536 /* Disable the MER Bit */
537 FLASH
->CR
&= CR_MER_Reset
;
539 #endif /* STM32F10X_XL */
541 /* Return the Erase Status */
546 * @brief Erases all Bank1 FLASH pages.
547 * @note This function can be used for all STM32F10x devices.
548 * - For STM32F10X_XL devices this function erases all Bank1 pages.
549 * - For all other devices it erases all Bank1 pages and it is equivalent
550 * to FLASH_EraseAllPages function.
552 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
553 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
555 FLASH_Status
FLASH_EraseAllBank1Pages(void)
557 FLASH_Status status
= FLASH_COMPLETE
;
558 /* Wait for last operation to be completed */
559 status
= FLASH_WaitForLastBank1Operation(EraseTimeout
);
561 if(status
== FLASH_COMPLETE
)
563 /* if the previous operation is completed, proceed to erase all pages */
564 FLASH
->CR
|= CR_MER_Set
;
565 FLASH
->CR
|= CR_STRT_Set
;
567 /* Wait for last operation to be completed */
568 status
= FLASH_WaitForLastBank1Operation(EraseTimeout
);
570 /* Disable the MER Bit */
571 FLASH
->CR
&= CR_MER_Reset
;
573 /* Return the Erase Status */
579 * @brief Erases all Bank2 FLASH pages.
580 * @note This function can be used only for STM32F10x_XL density devices.
582 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
583 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
585 FLASH_Status
FLASH_EraseAllBank2Pages(void)
587 FLASH_Status status
= FLASH_COMPLETE
;
588 /* Wait for last operation to be completed */
589 status
= FLASH_WaitForLastBank2Operation(EraseTimeout
);
591 if(status
== FLASH_COMPLETE
)
593 /* if the previous operation is completed, proceed to erase all pages */
594 FLASH
->CR2
|= CR_MER_Set
;
595 FLASH
->CR2
|= CR_STRT_Set
;
597 /* Wait for last operation to be completed */
598 status
= FLASH_WaitForLastBank2Operation(EraseTimeout
);
600 /* Disable the MER Bit */
601 FLASH
->CR2
&= CR_MER_Reset
;
603 /* Return the Erase Status */
606 #endif /* STM32F10X_XL */
609 * @brief Erases the FLASH option bytes.
610 * @note This functions erases all option bytes except the Read protection (RDP).
611 * @note This function can be used for all STM32F10x devices.
613 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
614 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
616 FLASH_Status
FLASH_EraseOptionBytes(void)
618 uint16_t rdptmp
= RDP_Key
;
620 FLASH_Status status
= FLASH_COMPLETE
;
622 /* Get the actual read protection Option Byte value */
623 if(FLASH_GetReadOutProtectionStatus() != RESET
)
628 /* Wait for last operation to be completed */
629 status
= FLASH_WaitForLastOperation(EraseTimeout
);
630 if(status
== FLASH_COMPLETE
)
632 /* Authorize the small information block programming */
633 FLASH
->OPTKEYR
= FLASH_KEY1
;
634 FLASH
->OPTKEYR
= FLASH_KEY2
;
636 /* if the previous operation is completed, proceed to erase the option bytes */
637 FLASH
->CR
|= CR_OPTER_Set
;
638 FLASH
->CR
|= CR_STRT_Set
;
639 /* Wait for last operation to be completed */
640 status
= FLASH_WaitForLastOperation(EraseTimeout
);
642 if(status
== FLASH_COMPLETE
)
644 /* if the erase operation is completed, disable the OPTER Bit */
645 FLASH
->CR
&= CR_OPTER_Reset
;
647 /* Enable the Option Bytes Programming operation */
648 FLASH
->CR
|= CR_OPTPG_Set
;
649 /* Restore the last read protection Option Byte value */
650 OB
->RDP
= (uint16_t)rdptmp
;
651 /* Wait for last operation to be completed */
652 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
654 if(status
!= FLASH_TIMEOUT
)
656 /* if the program operation is completed, disable the OPTPG Bit */
657 FLASH
->CR
&= CR_OPTPG_Reset
;
662 if (status
!= FLASH_TIMEOUT
)
664 /* Disable the OPTPG Bit */
665 FLASH
->CR
&= CR_OPTPG_Reset
;
669 /* Return the erase status */
674 * @brief Programs a word at a specified address.
675 * @note This function can be used for all STM32F10x devices.
676 * @param Address: specifies the address to be programmed.
677 * @param Data: specifies the data to be programmed.
678 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
679 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
681 FLASH_Status
FLASH_ProgramWord(uint32_t Address
, uint32_t Data
)
683 FLASH_Status status
= FLASH_COMPLETE
;
684 __IO
uint32_t tmp
= 0;
686 /* Check the parameters */
687 assert_param(IS_FLASH_ADDRESS(Address
));
690 if(Address
< FLASH_BANK1_END_ADDRESS
- 2)
692 /* Wait for last operation to be completed */
693 status
= FLASH_WaitForLastBank1Operation(ProgramTimeout
);
694 if(status
== FLASH_COMPLETE
)
696 /* if the previous operation is completed, proceed to program the new first
698 FLASH
->CR
|= CR_PG_Set
;
700 *(__IO
uint16_t*)Address
= (uint16_t)Data
;
701 /* Wait for last operation to be completed */
702 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
704 if(status
== FLASH_COMPLETE
)
706 /* if the previous operation is completed, proceed to program the new second
710 *(__IO
uint16_t*) tmp
= Data
>> 16;
712 /* Wait for last operation to be completed */
713 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
715 /* Disable the PG Bit */
716 FLASH
->CR
&= CR_PG_Reset
;
720 /* Disable the PG Bit */
721 FLASH
->CR
&= CR_PG_Reset
;
725 else if(Address
== (FLASH_BANK1_END_ADDRESS
- 1))
727 /* Wait for last operation to be completed */
728 status
= FLASH_WaitForLastBank1Operation(ProgramTimeout
);
730 if(status
== FLASH_COMPLETE
)
732 /* if the previous operation is completed, proceed to program the new first
734 FLASH
->CR
|= CR_PG_Set
;
736 *(__IO
uint16_t*)Address
= (uint16_t)Data
;
738 /* Wait for last operation to be completed */
739 status
= FLASH_WaitForLastBank1Operation(ProgramTimeout
);
741 /* Disable the PG Bit */
742 FLASH
->CR
&= CR_PG_Reset
;
746 /* Disable the PG Bit */
747 FLASH
->CR
&= CR_PG_Reset
;
750 /* Wait for last operation to be completed */
751 status
= FLASH_WaitForLastBank2Operation(ProgramTimeout
);
753 if(status
== FLASH_COMPLETE
)
755 /* if the previous operation is completed, proceed to program the new second
757 FLASH
->CR2
|= CR_PG_Set
;
760 *(__IO
uint16_t*) tmp
= Data
>> 16;
762 /* Wait for last operation to be completed */
763 status
= FLASH_WaitForLastBank2Operation(ProgramTimeout
);
765 /* Disable the PG Bit */
766 FLASH
->CR2
&= CR_PG_Reset
;
770 /* Disable the PG Bit */
771 FLASH
->CR2
&= CR_PG_Reset
;
776 /* Wait for last operation to be completed */
777 status
= FLASH_WaitForLastBank2Operation(ProgramTimeout
);
779 if(status
== FLASH_COMPLETE
)
781 /* if the previous operation is completed, proceed to program the new first
783 FLASH
->CR2
|= CR_PG_Set
;
785 *(__IO
uint16_t*)Address
= (uint16_t)Data
;
786 /* Wait for last operation to be completed */
787 status
= FLASH_WaitForLastBank2Operation(ProgramTimeout
);
789 if(status
== FLASH_COMPLETE
)
791 /* if the previous operation is completed, proceed to program the new second
795 *(__IO
uint16_t*) tmp
= Data
>> 16;
797 /* Wait for last operation to be completed */
798 status
= FLASH_WaitForLastBank2Operation(ProgramTimeout
);
800 /* Disable the PG Bit */
801 FLASH
->CR2
&= CR_PG_Reset
;
805 /* Disable the PG Bit */
806 FLASH
->CR2
&= CR_PG_Reset
;
811 /* Wait for last operation to be completed */
812 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
814 if(status
== FLASH_COMPLETE
)
816 /* if the previous operation is completed, proceed to program the new first
818 FLASH
->CR
|= CR_PG_Set
;
820 *(__IO
uint16_t*)Address
= (uint16_t)Data
;
821 /* Wait for last operation to be completed */
822 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
824 if(status
== FLASH_COMPLETE
)
826 /* if the previous operation is completed, proceed to program the new second
830 *(__IO
uint16_t*) tmp
= Data
>> 16;
832 /* Wait for last operation to be completed */
833 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
835 /* Disable the PG Bit */
836 FLASH
->CR
&= CR_PG_Reset
;
840 /* Disable the PG Bit */
841 FLASH
->CR
&= CR_PG_Reset
;
844 #endif /* STM32F10X_XL */
846 /* Return the Program Status */
851 * @brief Programs a half word at a specified address.
852 * @note This function can be used for all STM32F10x devices.
853 * @param Address: specifies the address to be programmed.
854 * @param Data: specifies the data to be programmed.
855 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
856 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
858 FLASH_Status
FLASH_ProgramHalfWord(uint32_t Address
, uint16_t Data
)
860 FLASH_Status status
= FLASH_COMPLETE
;
861 /* Check the parameters */
862 assert_param(IS_FLASH_ADDRESS(Address
));
865 /* Wait for last operation to be completed */
866 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
868 if(Address
< FLASH_BANK1_END_ADDRESS
)
870 if(status
== FLASH_COMPLETE
)
872 /* if the previous operation is completed, proceed to program the new data */
873 FLASH
->CR
|= CR_PG_Set
;
875 *(__IO
uint16_t*)Address
= Data
;
876 /* Wait for last operation to be completed */
877 status
= FLASH_WaitForLastBank1Operation(ProgramTimeout
);
879 /* Disable the PG Bit */
880 FLASH
->CR
&= CR_PG_Reset
;
885 if(status
== FLASH_COMPLETE
)
887 /* if the previous operation is completed, proceed to program the new data */
888 FLASH
->CR2
|= CR_PG_Set
;
890 *(__IO
uint16_t*)Address
= Data
;
891 /* Wait for last operation to be completed */
892 status
= FLASH_WaitForLastBank2Operation(ProgramTimeout
);
894 /* Disable the PG Bit */
895 FLASH
->CR2
&= CR_PG_Reset
;
899 /* Wait for last operation to be completed */
900 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
902 if(status
== FLASH_COMPLETE
)
904 /* if the previous operation is completed, proceed to program the new data */
905 FLASH
->CR
|= CR_PG_Set
;
907 *(__IO
uint16_t*)Address
= Data
;
908 /* Wait for last operation to be completed */
909 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
911 /* Disable the PG Bit */
912 FLASH
->CR
&= CR_PG_Reset
;
914 #endif /* STM32F10X_XL */
916 /* Return the Program Status */
921 * @brief Programs a half word at a specified Option Byte Data address.
922 * @note This function can be used for all STM32F10x devices.
923 * @param Address: specifies the address to be programmed.
924 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
925 * @param Data: specifies the data to be programmed.
926 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
927 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
929 FLASH_Status
FLASH_ProgramOptionByteData(uint32_t Address
, uint8_t Data
)
931 FLASH_Status status
= FLASH_COMPLETE
;
932 /* Check the parameters */
933 assert_param(IS_OB_DATA_ADDRESS(Address
));
934 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
936 if(status
== FLASH_COMPLETE
)
938 /* Authorize the small information block programming */
939 FLASH
->OPTKEYR
= FLASH_KEY1
;
940 FLASH
->OPTKEYR
= FLASH_KEY2
;
941 /* Enables the Option Bytes Programming operation */
942 FLASH
->CR
|= CR_OPTPG_Set
;
943 *(__IO
uint16_t*)Address
= Data
;
945 /* Wait for last operation to be completed */
946 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
947 if(status
!= FLASH_TIMEOUT
)
949 /* if the program operation is completed, disable the OPTPG Bit */
950 FLASH
->CR
&= CR_OPTPG_Reset
;
953 /* Return the Option Byte Data Program Status */
958 * @brief Write protects the desired pages
959 * @note This function can be used for all STM32F10x devices.
960 * @param FLASH_Pages: specifies the address of the pages to be write protected.
961 * This parameter can be:
962 * @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31
963 * @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3
964 * and FLASH_WRProt_Pages124to127
965 * @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and
966 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
967 * @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and
968 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127
969 * @arg For @b STM32_XL-density_devices: value between FLASH_WRProt_Pages0to1 and
970 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to511
971 * @arg FLASH_WRProt_AllPages
972 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
973 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
975 FLASH_Status
FLASH_EnableWriteProtection(uint32_t FLASH_Pages
)
977 uint16_t WRP0_Data
= 0xFFFF, WRP1_Data
= 0xFFFF, WRP2_Data
= 0xFFFF, WRP3_Data
= 0xFFFF;
979 FLASH_Status status
= FLASH_COMPLETE
;
981 /* Check the parameters */
982 assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages
));
984 FLASH_Pages
= (uint32_t)(~FLASH_Pages
);
985 WRP0_Data
= (uint16_t)(FLASH_Pages
& WRP0_Mask
);
986 WRP1_Data
= (uint16_t)((FLASH_Pages
& WRP1_Mask
) >> 8);
987 WRP2_Data
= (uint16_t)((FLASH_Pages
& WRP2_Mask
) >> 16);
988 WRP3_Data
= (uint16_t)((FLASH_Pages
& WRP3_Mask
) >> 24);
990 /* Wait for last operation to be completed */
991 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
993 if(status
== FLASH_COMPLETE
)
995 /* Authorizes the small information block programming */
996 FLASH
->OPTKEYR
= FLASH_KEY1
;
997 FLASH
->OPTKEYR
= FLASH_KEY2
;
998 FLASH
->CR
|= CR_OPTPG_Set
;
999 if(WRP0_Data
!= 0xFF)
1001 OB
->WRP0
= WRP0_Data
;
1003 /* Wait for last operation to be completed */
1004 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1006 if((status
== FLASH_COMPLETE
) && (WRP1_Data
!= 0xFF))
1008 OB
->WRP1
= WRP1_Data
;
1010 /* Wait for last operation to be completed */
1011 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1013 if((status
== FLASH_COMPLETE
) && (WRP2_Data
!= 0xFF))
1015 OB
->WRP2
= WRP2_Data
;
1017 /* Wait for last operation to be completed */
1018 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1021 if((status
== FLASH_COMPLETE
)&& (WRP3_Data
!= 0xFF))
1023 OB
->WRP3
= WRP3_Data
;
1025 /* Wait for last operation to be completed */
1026 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1029 if(status
!= FLASH_TIMEOUT
)
1031 /* if the program operation is completed, disable the OPTPG Bit */
1032 FLASH
->CR
&= CR_OPTPG_Reset
;
1035 /* Return the write protection operation Status */
1040 * @brief Enables or disables the read out protection.
1041 * @note If the user has already programmed the other option bytes before calling
1042 * this function, he must re-program them since this function erases all option bytes.
1043 * @note This function can be used for all STM32F10x devices.
1044 * @param Newstate: new state of the ReadOut Protection.
1045 * This parameter can be: ENABLE or DISABLE.
1046 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
1047 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1049 FLASH_Status
FLASH_ReadOutProtection(FunctionalState NewState
)
1051 FLASH_Status status
= FLASH_COMPLETE
;
1052 /* Check the parameters */
1053 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1054 status
= FLASH_WaitForLastOperation(EraseTimeout
);
1055 if(status
== FLASH_COMPLETE
)
1057 /* Authorizes the small information block programming */
1058 FLASH
->OPTKEYR
= FLASH_KEY1
;
1059 FLASH
->OPTKEYR
= FLASH_KEY2
;
1060 FLASH
->CR
|= CR_OPTER_Set
;
1061 FLASH
->CR
|= CR_STRT_Set
;
1062 /* Wait for last operation to be completed */
1063 status
= FLASH_WaitForLastOperation(EraseTimeout
);
1064 if(status
== FLASH_COMPLETE
)
1066 /* if the erase operation is completed, disable the OPTER Bit */
1067 FLASH
->CR
&= CR_OPTER_Reset
;
1068 /* Enable the Option Bytes Programming operation */
1069 FLASH
->CR
|= CR_OPTPG_Set
;
1070 if(NewState
!= DISABLE
)
1078 /* Wait for last operation to be completed */
1079 status
= FLASH_WaitForLastOperation(EraseTimeout
);
1081 if(status
!= FLASH_TIMEOUT
)
1083 /* if the program operation is completed, disable the OPTPG Bit */
1084 FLASH
->CR
&= CR_OPTPG_Reset
;
1089 if(status
!= FLASH_TIMEOUT
)
1091 /* Disable the OPTER Bit */
1092 FLASH
->CR
&= CR_OPTER_Reset
;
1096 /* Return the protection operation Status */
1101 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
1102 * @note This function can be used for all STM32F10x devices.
1103 * @param OB_IWDG: Selects the IWDG mode
1104 * This parameter can be one of the following values:
1105 * @arg OB_IWDG_SW: Software IWDG selected
1106 * @arg OB_IWDG_HW: Hardware IWDG selected
1107 * @param OB_STOP: Reset event when entering STOP mode.
1108 * This parameter can be one of the following values:
1109 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
1110 * @arg OB_STOP_RST: Reset generated when entering in STOP
1111 * @param OB_STDBY: Reset event when entering Standby mode.
1112 * This parameter can be one of the following values:
1113 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
1114 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
1115 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
1116 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1118 FLASH_Status
FLASH_UserOptionByteConfig(uint16_t OB_IWDG
, uint16_t OB_STOP
, uint16_t OB_STDBY
)
1120 FLASH_Status status
= FLASH_COMPLETE
;
1122 /* Check the parameters */
1123 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG
));
1124 assert_param(IS_OB_STOP_SOURCE(OB_STOP
));
1125 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY
));
1127 /* Authorize the small information block programming */
1128 FLASH
->OPTKEYR
= FLASH_KEY1
;
1129 FLASH
->OPTKEYR
= FLASH_KEY2
;
1131 /* Wait for last operation to be completed */
1132 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1134 if(status
== FLASH_COMPLETE
)
1136 /* Enable the Option Bytes Programming operation */
1137 FLASH
->CR
|= CR_OPTPG_Set
;
1139 OB
->USER
= OB_IWDG
| (uint16_t)(OB_STOP
| (uint16_t)(OB_STDBY
| ((uint16_t)0xF8)));
1141 /* Wait for last operation to be completed */
1142 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1143 if(status
!= FLASH_TIMEOUT
)
1145 /* if the program operation is completed, disable the OPTPG Bit */
1146 FLASH
->CR
&= CR_OPTPG_Reset
;
1149 /* Return the Option Byte program Status */
1155 * @brief Configures to boot from Bank1 or Bank2.
1156 * @note This function can be used only for STM32F10x_XL density devices.
1157 * @param FLASH_BOOT: select the FLASH Bank to boot from.
1158 * This parameter can be one of the following values:
1159 * @arg FLASH_BOOT_Bank1: At startup, if boot pins are set in boot from user Flash
1160 * position and this parameter is selected the device will boot from Bank1(Default).
1161 * @arg FLASH_BOOT_Bank2: At startup, if boot pins are set in boot from user Flash
1162 * position and this parameter is selected the device will boot from Bank2 or Bank1,
1163 * depending on the activation of the bank. The active banks are checked in
1164 * the following order: Bank2, followed by Bank1.
1165 * The active bank is recognized by the value programmed at the base address
1166 * of the respective bank (corresponding to the initial stack pointer value
1167 * in the interrupt vector table).
1168 * For more information, please refer to AN2606 from www.st.com.
1169 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
1170 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1172 FLASH_Status
FLASH_BootConfig(uint16_t FLASH_BOOT
)
1174 FLASH_Status status
= FLASH_COMPLETE
;
1175 assert_param(IS_FLASH_BOOT(FLASH_BOOT
));
1176 /* Authorize the small information block programming */
1177 FLASH
->OPTKEYR
= FLASH_KEY1
;
1178 FLASH
->OPTKEYR
= FLASH_KEY2
;
1180 /* Wait for last operation to be completed */
1181 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1183 if(status
== FLASH_COMPLETE
)
1185 /* Enable the Option Bytes Programming operation */
1186 FLASH
->CR
|= CR_OPTPG_Set
;
1188 if(FLASH_BOOT
== FLASH_BOOT_Bank1
)
1190 OB
->USER
|= OB_USER_BFB2
;
1194 OB
->USER
&= (uint16_t)(~(uint16_t)(OB_USER_BFB2
));
1196 /* Wait for last operation to be completed */
1197 status
= FLASH_WaitForLastOperation(ProgramTimeout
);
1198 if(status
!= FLASH_TIMEOUT
)
1200 /* if the program operation is completed, disable the OPTPG Bit */
1201 FLASH
->CR
&= CR_OPTPG_Reset
;
1204 /* Return the Option Byte program Status */
1207 #endif /* STM32F10X_XL */
1210 * @brief Returns the FLASH User Option Bytes values.
1211 * @note This function can be used for all STM32F10x devices.
1213 * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
1214 * and RST_STDBY(Bit2).
1216 uint32_t FLASH_GetUserOptionByte(void)
1218 /* Return the User Option Byte */
1219 return (uint32_t)(FLASH
->OBR
>> 2);
1223 * @brief Returns the FLASH Write Protection Option Bytes Register value.
1224 * @note This function can be used for all STM32F10x devices.
1226 * @retval The FLASH Write Protection Option Bytes Register value
1228 uint32_t FLASH_GetWriteProtectionOptionByte(void)
1230 /* Return the Flash write protection Register value */
1231 return (uint32_t)(FLASH
->WRPR
);
1235 * @brief Checks whether the FLASH Read Out Protection Status is set or not.
1236 * @note This function can be used for all STM32F10x devices.
1238 * @retval FLASH ReadOut Protection Status(SET or RESET)
1240 FlagStatus
FLASH_GetReadOutProtectionStatus(void)
1242 FlagStatus readoutstatus
= RESET
;
1243 if ((FLASH
->OBR
& RDPRT_Mask
) != (uint32_t)RESET
)
1245 readoutstatus
= SET
;
1249 readoutstatus
= RESET
;
1251 return readoutstatus
;
1255 * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
1256 * @note This function can be used for all STM32F10x devices.
1258 * @retval FLASH Prefetch Buffer Status (SET or RESET).
1260 FlagStatus
FLASH_GetPrefetchBufferStatus(void)
1262 FlagStatus bitstatus
= RESET
;
1264 if ((FLASH
->ACR
& ACR_PRFTBS_Mask
) != (uint32_t)RESET
)
1272 /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
1277 * @brief Enables or disables the specified FLASH interrupts.
1278 * @note This function can be used for all STM32F10x devices.
1279 * - For STM32F10X_XL devices, enables or disables the specified FLASH interrupts
1280 for Bank1 and Bank2.
1281 * - For other devices it enables or disables the specified FLASH interrupts for Bank1.
1282 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
1283 * This parameter can be any combination of the following values:
1284 * @arg FLASH_IT_ERROR: FLASH Error Interrupt
1285 * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
1286 * @param NewState: new state of the specified Flash interrupts.
1287 * This parameter can be: ENABLE or DISABLE.
1290 void FLASH_ITConfig(uint32_t FLASH_IT
, FunctionalState NewState
)
1293 /* Check the parameters */
1294 assert_param(IS_FLASH_IT(FLASH_IT
));
1295 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1297 if((FLASH_IT
& 0x80000000) != 0x0)
1299 if(NewState
!= DISABLE
)
1301 /* Enable the interrupt sources */
1302 FLASH
->CR2
|= (FLASH_IT
& 0x7FFFFFFF);
1306 /* Disable the interrupt sources */
1307 FLASH
->CR2
&= ~(uint32_t)(FLASH_IT
& 0x7FFFFFFF);
1312 if(NewState
!= DISABLE
)
1314 /* Enable the interrupt sources */
1315 FLASH
->CR
|= FLASH_IT
;
1319 /* Disable the interrupt sources */
1320 FLASH
->CR
&= ~(uint32_t)FLASH_IT
;
1324 /* Check the parameters */
1325 assert_param(IS_FLASH_IT(FLASH_IT
));
1326 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1328 if(NewState
!= DISABLE
)
1330 /* Enable the interrupt sources */
1331 FLASH
->CR
|= FLASH_IT
;
1335 /* Disable the interrupt sources */
1336 FLASH
->CR
&= ~(uint32_t)FLASH_IT
;
1338 #endif /* STM32F10X_XL */
1342 * @brief Checks whether the specified FLASH flag is set or not.
1343 * @note This function can be used for all STM32F10x devices.
1344 * - For STM32F10X_XL devices, this function checks whether the specified
1345 * Bank1 or Bank2 flag is set or not.
1346 * - For other devices, it checks whether the specified Bank1 flag is
1348 * @param FLASH_FLAG: specifies the FLASH flag to check.
1349 * This parameter can be one of the following values:
1350 * @arg FLASH_FLAG_BSY: FLASH Busy flag
1351 * @arg FLASH_FLAG_PGERR: FLASH Program error flag
1352 * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
1353 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
1354 * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag
1355 * @retval The new state of FLASH_FLAG (SET or RESET).
1357 FlagStatus
FLASH_GetFlagStatus(uint32_t FLASH_FLAG
)
1359 FlagStatus bitstatus
= RESET
;
1362 /* Check the parameters */
1363 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG
)) ;
1364 if(FLASH_FLAG
== FLASH_FLAG_OPTERR
)
1366 if((FLASH
->OBR
& FLASH_FLAG_OPTERR
) != (uint32_t)RESET
)
1377 if((FLASH_FLAG
& 0x80000000) != 0x0)
1379 if((FLASH
->SR2
& FLASH_FLAG
) != (uint32_t)RESET
)
1390 if((FLASH
->SR
& FLASH_FLAG
) != (uint32_t)RESET
)
1401 /* Check the parameters */
1402 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG
)) ;
1403 if(FLASH_FLAG
== FLASH_FLAG_OPTERR
)
1405 if((FLASH
->OBR
& FLASH_FLAG_OPTERR
) != (uint32_t)RESET
)
1416 if((FLASH
->SR
& FLASH_FLAG
) != (uint32_t)RESET
)
1425 #endif /* STM32F10X_XL */
1427 /* Return the new state of FLASH_FLAG (SET or RESET) */
1432 * @brief Clears the FLASH's pending flags.
1433 * @note This function can be used for all STM32F10x devices.
1434 * - For STM32F10X_XL devices, this function clears Bank1 or Bank2’s pending flags
1435 * - For other devices, it clears Bank1’s pending flags.
1436 * @param FLASH_FLAG: specifies the FLASH flags to clear.
1437 * This parameter can be any combination of the following values:
1438 * @arg FLASH_FLAG_PGERR: FLASH Program error flag
1439 * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
1440 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
1443 void FLASH_ClearFlag(uint32_t FLASH_FLAG
)
1446 /* Check the parameters */
1447 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG
)) ;
1449 if((FLASH_FLAG
& 0x80000000) != 0x0)
1451 /* Clear the flags */
1452 FLASH
->SR2
= FLASH_FLAG
;
1456 /* Clear the flags */
1457 FLASH
->SR
= FLASH_FLAG
;
1461 /* Check the parameters */
1462 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG
)) ;
1464 /* Clear the flags */
1465 FLASH
->SR
= FLASH_FLAG
;
1466 #endif /* STM32F10X_XL */
1470 * @brief Returns the FLASH Status.
1471 * @note This function can be used for all STM32F10x devices, it is equivalent
1472 * to FLASH_GetBank1Status function.
1474 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
1475 * FLASH_ERROR_WRP or FLASH_COMPLETE
1477 FLASH_Status
FLASH_GetStatus(void)
1479 FLASH_Status flashstatus
= FLASH_COMPLETE
;
1481 if((FLASH
->SR
& FLASH_FLAG_BSY
) == FLASH_FLAG_BSY
)
1483 flashstatus
= FLASH_BUSY
;
1487 if((FLASH
->SR
& FLASH_FLAG_PGERR
) != 0)
1489 flashstatus
= FLASH_ERROR_PG
;
1493 if((FLASH
->SR
& FLASH_FLAG_WRPRTERR
) != 0 )
1495 flashstatus
= FLASH_ERROR_WRP
;
1499 flashstatus
= FLASH_COMPLETE
;
1503 /* Return the Flash Status */
1508 * @brief Returns the FLASH Bank1 Status.
1509 * @note This function can be used for all STM32F10x devices, it is equivalent
1510 * to FLASH_GetStatus function.
1512 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
1513 * FLASH_ERROR_WRP or FLASH_COMPLETE
1515 FLASH_Status
FLASH_GetBank1Status(void)
1517 FLASH_Status flashstatus
= FLASH_COMPLETE
;
1519 if((FLASH
->SR
& FLASH_FLAG_BANK1_BSY
) == FLASH_FLAG_BSY
)
1521 flashstatus
= FLASH_BUSY
;
1525 if((FLASH
->SR
& FLASH_FLAG_BANK1_PGERR
) != 0)
1527 flashstatus
= FLASH_ERROR_PG
;
1531 if((FLASH
->SR
& FLASH_FLAG_BANK1_WRPRTERR
) != 0 )
1533 flashstatus
= FLASH_ERROR_WRP
;
1537 flashstatus
= FLASH_COMPLETE
;
1541 /* Return the Flash Status */
1547 * @brief Returns the FLASH Bank2 Status.
1548 * @note This function can be used for STM32F10x_XL density devices.
1550 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
1551 * FLASH_ERROR_WRP or FLASH_COMPLETE
1553 FLASH_Status
FLASH_GetBank2Status(void)
1555 FLASH_Status flashstatus
= FLASH_COMPLETE
;
1557 if((FLASH
->SR2
& (FLASH_FLAG_BANK2_BSY
& 0x7FFFFFFF)) == (FLASH_FLAG_BANK2_BSY
& 0x7FFFFFFF))
1559 flashstatus
= FLASH_BUSY
;
1563 if((FLASH
->SR2
& (FLASH_FLAG_BANK2_PGERR
& 0x7FFFFFFF)) != 0)
1565 flashstatus
= FLASH_ERROR_PG
;
1569 if((FLASH
->SR2
& (FLASH_FLAG_BANK2_WRPRTERR
& 0x7FFFFFFF)) != 0 )
1571 flashstatus
= FLASH_ERROR_WRP
;
1575 flashstatus
= FLASH_COMPLETE
;
1579 /* Return the Flash Status */
1582 #endif /* STM32F10X_XL */
1584 * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
1585 * @note This function can be used for all STM32F10x devices,
1586 * it is equivalent to FLASH_WaitForLastBank1Operation.
1587 * - For STM32F10X_XL devices this function waits for a Bank1 Flash operation
1588 * to complete or a TIMEOUT to occur.
1589 * - For all other devices it waits for a Flash operation to complete
1590 * or a TIMEOUT to occur.
1591 * @param Timeout: FLASH programming Timeout
1592 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
1593 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1595 FLASH_Status
FLASH_WaitForLastOperation(uint32_t Timeout
)
1597 FLASH_Status status
= FLASH_COMPLETE
;
1599 /* Check for the Flash Status */
1600 status
= FLASH_GetBank1Status();
1601 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
1602 while((status
== FLASH_BUSY
) && (Timeout
!= 0x00))
1604 status
= FLASH_GetBank1Status();
1607 if(Timeout
== 0x00 )
1609 status
= FLASH_TIMEOUT
;
1611 /* Return the operation status */
1616 * @brief Waits for a Flash operation on Bank1 to complete or a TIMEOUT to occur.
1617 * @note This function can be used for all STM32F10x devices,
1618 * it is equivalent to FLASH_WaitForLastOperation.
1619 * @param Timeout: FLASH programming Timeout
1620 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
1621 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1623 FLASH_Status
FLASH_WaitForLastBank1Operation(uint32_t Timeout
)
1625 FLASH_Status status
= FLASH_COMPLETE
;
1627 /* Check for the Flash Status */
1628 status
= FLASH_GetBank1Status();
1629 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
1630 while((status
== FLASH_FLAG_BANK1_BSY
) && (Timeout
!= 0x00))
1632 status
= FLASH_GetBank1Status();
1635 if(Timeout
== 0x00 )
1637 status
= FLASH_TIMEOUT
;
1639 /* Return the operation status */
1645 * @brief Waits for a Flash operation on Bank2 to complete or a TIMEOUT to occur.
1646 * @note This function can be used only for STM32F10x_XL density devices.
1647 * @param Timeout: FLASH programming Timeout
1648 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
1649 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1651 FLASH_Status
FLASH_WaitForLastBank2Operation(uint32_t Timeout
)
1653 FLASH_Status status
= FLASH_COMPLETE
;
1655 /* Check for the Flash Status */
1656 status
= FLASH_GetBank2Status();
1657 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
1658 while((status
== (FLASH_FLAG_BANK2_BSY
& 0x7FFFFFFF)) && (Timeout
!= 0x00))
1660 status
= FLASH_GetBank2Status();
1663 if(Timeout
== 0x00 )
1665 status
= FLASH_TIMEOUT
;
1667 /* Return the operation status */
1670 #endif /* STM32F10X_XL */
1684 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/