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 2010 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
73 #define OB_USER_BFB2 ((uint16_t)0x0008)
\r
76 #define RDP_Key ((uint16_t)0x00A5)
\r
77 #define FLASH_KEY1 ((uint32_t)0x45670123)
\r
78 #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
\r
80 /* FLASH BANK address */
\r
81 #define FLASH_BANK1_END_ADDRESS ((uint32_t)0x807FFFF)
\r
83 /* Delay definition */
\r
84 #define EraseTimeout ((uint32_t)0x000B0000)
\r
85 #define ProgramTimeout ((uint32_t)0x00002000)
\r
90 /** @defgroup FLASH_Private_Macros
\r
98 /** @defgroup FLASH_Private_Variables
\r
106 /** @defgroup FLASH_Private_FunctionPrototypes
\r
114 /** @defgroup FLASH_Private_Functions
\r
121 This driver provides functions to configure and program the Flash memory of all STM32F10x devices,
\r
122 including the latest STM32F10x_XL density devices.
\r
124 STM32F10x_XL devices feature up to 1 Mbyte with dual bank architecture for read-while-write (RWW) capability:
\r
125 - bank1: fixed size of 512 Kbytes (256 pages of 2Kbytes each)
\r
126 - bank2: up to 512 Kbytes (up to 256 pages of 2Kbytes each)
\r
127 While other STM32F10x devices features only one bank with memory up to 512 Kbytes.
\r
129 In version V3.3.0, some functions were updated and new ones were added to support
\r
130 STM32F10x_XL devices. Thus some functions manages all devices, while other are
\r
131 dedicated for XL devices only.
\r
133 The table below presents the list of available functions depending on the used STM32F10x devices.
\r
135 ***************************************************
\r
136 * Legacy functions used for all STM32F10x devices *
\r
137 ***************************************************
\r
138 +----------------------------------------------------------------------------------------------------------------------------------+
\r
139 | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
\r
140 | | devices | devices | |
\r
141 |----------------------------------------------------------------------------------------------------------------------------------|
\r
142 |FLASH_SetLatency | Yes | Yes | No change |
\r
143 |----------------------------------------------------------------------------------------------------------------------------------|
\r
144 |FLASH_HalfCycleAccessCmd | Yes | Yes | No change |
\r
145 |----------------------------------------------------------------------------------------------------------------------------------|
\r
146 |FLASH_PrefetchBufferCmd | Yes | Yes | No change |
\r
147 |----------------------------------------------------------------------------------------------------------------------------------|
\r
148 |FLASH_Unlock | Yes | Yes | - For STM32F10X_XL devices: unlock Bank1 and Bank2. |
\r
149 | | | | - For other devices: unlock Bank1 and it is equivalent |
\r
150 | | | | to FLASH_UnlockBank1 function. |
\r
151 |----------------------------------------------------------------------------------------------------------------------------------|
\r
152 |FLASH_Lock | Yes | Yes | - For STM32F10X_XL devices: lock Bank1 and Bank2. |
\r
153 | | | | - For other devices: lock Bank1 and it is equivalent |
\r
154 | | | | to FLASH_LockBank1 function. |
\r
155 |----------------------------------------------------------------------------------------------------------------------------------|
\r
156 |FLASH_ErasePage | Yes | Yes | - For STM32F10x_XL devices: erase a page in Bank1 and Bank2 |
\r
157 | | | | - For other devices: erase a page in Bank1 |
\r
158 |----------------------------------------------------------------------------------------------------------------------------------|
\r
159 |FLASH_EraseAllPages | Yes | Yes | - For STM32F10x_XL devices: erase all pages in Bank1 and Bank2 |
\r
160 | | | | - For other devices: erase all pages in Bank1 |
\r
161 |----------------------------------------------------------------------------------------------------------------------------------|
\r
162 |FLASH_EraseOptionBytes | Yes | Yes | No change |
\r
163 |----------------------------------------------------------------------------------------------------------------------------------|
\r
164 |FLASH_ProgramWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |
\r
165 |----------------------------------------------------------------------------------------------------------------------------------|
\r
166 |FLASH_ProgramHalfWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |
\r
167 |----------------------------------------------------------------------------------------------------------------------------------|
\r
168 |FLASH_ProgramOptionByteData | Yes | Yes | No change |
\r
169 |----------------------------------------------------------------------------------------------------------------------------------|
\r
170 |FLASH_EnableWriteProtection | Yes | Yes | No change |
\r
171 |----------------------------------------------------------------------------------------------------------------------------------|
\r
172 |FLASH_ReadOutProtection | Yes | Yes | No change |
\r
173 |----------------------------------------------------------------------------------------------------------------------------------|
\r
174 |FLASH_UserOptionByteConfig | Yes | Yes | No change |
\r
175 |----------------------------------------------------------------------------------------------------------------------------------|
\r
176 |FLASH_GetUserOptionByte | Yes | Yes | No change |
\r
177 |----------------------------------------------------------------------------------------------------------------------------------|
\r
178 |FLASH_GetWriteProtectionOptionByte | Yes | Yes | No change |
\r
179 |----------------------------------------------------------------------------------------------------------------------------------|
\r
180 |FLASH_GetReadOutProtectionStatus | Yes | Yes | No change |
\r
181 |----------------------------------------------------------------------------------------------------------------------------------|
\r
182 |FLASH_GetPrefetchBufferStatus | Yes | Yes | No change |
\r
183 |----------------------------------------------------------------------------------------------------------------------------------|
\r
184 |FLASH_ITConfig | Yes | Yes | - For STM32F10x_XL devices: enable Bank1 and Bank2's interrupts|
\r
185 | | | | - For other devices: enable Bank1's interrupts |
\r
186 |----------------------------------------------------------------------------------------------------------------------------------|
\r
187 |FLASH_GetFlagStatus | Yes | Yes | - For STM32F10x_XL devices: return Bank1 and Bank2's flag status|
\r
188 | | | | - For other devices: return Bank1's flag status |
\r
189 |----------------------------------------------------------------------------------------------------------------------------------|
\r
190 |FLASH_ClearFlag | Yes | Yes | - For STM32F10x_XL devices: clear Bank1 and Bank2's flag |
\r
191 | | | | - For other devices: clear Bank1's flag |
\r
192 |----------------------------------------------------------------------------------------------------------------------------------|
\r
193 |FLASH_GetStatus | Yes | Yes | - Return the status of Bank1 (for all devices) |
\r
194 | | | | equivalent to FLASH_GetBank1Status function |
\r
195 |----------------------------------------------------------------------------------------------------------------------------------|
\r
196 |FLASH_WaitForLastOperation | Yes | Yes | - Wait for Bank1 last operation (for all devices) |
\r
197 | | | | equivalent to: FLASH_WaitForLastBank1Operation function |
\r
198 +----------------------------------------------------------------------------------------------------------------------------------+
\r
200 ************************************************************************************************************************
\r
201 * New functions used for all STM32F10x devices to manage Bank1: *
\r
202 * - These functions are mainly useful for STM32F10x_XL density devices, to have separate control for Bank1 and bank2 *
\r
203 * - For other devices, these functions are optional (covered by functions listed above) *
\r
204 ************************************************************************************************************************
\r
205 +----------------------------------------------------------------------------------------------------------------------------------+
\r
206 | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
\r
207 | | devices | devices | |
\r
208 |----------------------------------------------------------------------------------------------------------------------------------|
\r
209 | FLASH_UnlockBank1 | Yes | Yes | - Unlock Bank1 |
\r
210 |----------------------------------------------------------------------------------------------------------------------------------|
\r
211 |FLASH_LockBank1 | Yes | Yes | - Lock Bank1 |
\r
212 |----------------------------------------------------------------------------------------------------------------------------------|
\r
213 | FLASH_EraseAllBank1Pages | Yes | Yes | - Erase all pages in Bank1 |
\r
214 |----------------------------------------------------------------------------------------------------------------------------------|
\r
215 | FLASH_GetBank1Status | Yes | Yes | - Return the status of Bank1 |
\r
216 |----------------------------------------------------------------------------------------------------------------------------------|
\r
217 | FLASH_WaitForLastBank1Operation | Yes | Yes | - Wait for Bank1 last operation |
\r
218 +----------------------------------------------------------------------------------------------------------------------------------+
\r
220 *****************************************************************************
\r
221 * New Functions used only with STM32F10x_XL density devices to manage Bank2 *
\r
222 *****************************************************************************
\r
223 +----------------------------------------------------------------------------------------------------------------------------------+
\r
224 | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |
\r
225 | | devices | devices | |
\r
226 |----------------------------------------------------------------------------------------------------------------------------------|
\r
227 | FLASH_UnlockBank2 | Yes | No | - Unlock Bank2 |
\r
228 |----------------------------------------------------------------------------------------------------------------------------------|
\r
229 |FLASH_LockBank2 | Yes | No | - Lock Bank2 |
\r
230 |----------------------------------------------------------------------------------------------------------------------------------|
\r
231 | FLASH_EraseAllBank2Pages | Yes | No | - Erase all pages in Bank2 |
\r
232 |----------------------------------------------------------------------------------------------------------------------------------|
\r
233 | FLASH_GetBank2Status | Yes | No | - Return the status of Bank2 |
\r
234 |----------------------------------------------------------------------------------------------------------------------------------|
\r
235 | FLASH_WaitForLastBank2Operation | Yes | No | - Wait for Bank2 last operation |
\r
236 |----------------------------------------------------------------------------------------------------------------------------------|
\r
237 | FLASH_BootConfig | Yes | No | - Configure to boot from Bank1 or Bank2 |
\r
238 +----------------------------------------------------------------------------------------------------------------------------------+
\r
244 * @brief Sets the code latency value.
\r
245 * @note This function can be used for all STM32F10x devices.
\r
246 * @param FLASH_Latency: specifies the FLASH Latency value.
\r
247 * This parameter can be one of the following values:
\r
248 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
\r
249 * @arg FLASH_Latency_1: FLASH One Latency cycle
\r
250 * @arg FLASH_Latency_2: FLASH Two Latency cycles
\r
253 void FLASH_SetLatency(uint32_t FLASH_Latency)
\r
255 uint32_t tmpreg = 0;
\r
257 /* Check the parameters */
\r
258 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
\r
260 /* Read the ACR register */
\r
261 tmpreg = FLASH->ACR;
\r
263 /* Sets the Latency value */
\r
264 tmpreg &= ACR_LATENCY_Mask;
\r
265 tmpreg |= FLASH_Latency;
\r
267 /* Write the ACR register */
\r
268 FLASH->ACR = tmpreg;
\r
272 * @brief Enables or disables the Half cycle flash access.
\r
273 * @note This function can be used for all STM32F10x devices.
\r
274 * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
\r
275 * This parameter can be one of the following values:
\r
276 * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
\r
277 * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
\r
280 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
\r
282 /* Check the parameters */
\r
283 assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
\r
285 /* Enable or disable the Half cycle access */
\r
286 FLASH->ACR &= ACR_HLFCYA_Mask;
\r
287 FLASH->ACR |= FLASH_HalfCycleAccess;
\r
291 * @brief Enables or disables the Prefetch Buffer.
\r
292 * @note This function can be used for all STM32F10x devices.
\r
293 * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
\r
294 * This parameter can be one of the following values:
\r
295 * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
\r
296 * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
\r
299 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)
\r
301 /* Check the parameters */
\r
302 assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
\r
304 /* Enable or disable the Prefetch Buffer */
\r
305 FLASH->ACR &= ACR_PRFTBE_Mask;
\r
306 FLASH->ACR |= FLASH_PrefetchBuffer;
\r
310 * @brief Unlocks the FLASH Program Erase Controller.
\r
311 * @note This function can be used for all STM32F10x devices.
\r
312 * - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.
\r
313 * - For all other devices it unlocks Bank1 and it is equivalent
\r
314 * to FLASH_UnlockBank1 function..
\r
318 void FLASH_Unlock(void)
\r
320 /* Authorize the FPEC of Bank1 Access */
\r
321 FLASH->KEYR = FLASH_KEY1;
\r
322 FLASH->KEYR = FLASH_KEY2;
\r
324 #ifdef STM32F10X_XL
\r
325 /* Authorize the FPEC of Bank2 Access */
\r
326 FLASH->KEYR2 = FLASH_KEY1;
\r
327 FLASH->KEYR2 = FLASH_KEY2;
\r
328 #endif /* STM32F10X_XL */
\r
331 * @brief Unlocks the FLASH Bank1 Program Erase Controller.
\r
332 * @note This function can be used for all STM32F10x devices.
\r
333 * - For STM32F10X_XL devices this function unlocks Bank1.
\r
334 * - For all other devices it unlocks Bank1 and it is
\r
335 * equivalent to FLASH_Unlock function.
\r
339 void FLASH_UnlockBank1(void)
\r
341 /* Authorize the FPEC of Bank1 Access */
\r
342 FLASH->KEYR = FLASH_KEY1;
\r
343 FLASH->KEYR = FLASH_KEY2;
\r
346 #ifdef STM32F10X_XL
\r
348 * @brief Unlocks the FLASH Bank2 Program Erase Controller.
\r
349 * @note This function can be used only for STM32F10X_XL density devices.
\r
353 void FLASH_UnlockBank2(void)
\r
355 /* Authorize the FPEC of Bank2 Access */
\r
356 FLASH->KEYR2 = FLASH_KEY1;
\r
357 FLASH->KEYR2 = FLASH_KEY2;
\r
360 #endif /* STM32F10X_XL */
\r
363 * @brief Locks the FLASH Program Erase Controller.
\r
364 * @note This function can be used for all STM32F10x devices.
\r
365 * - For STM32F10X_XL devices this function Locks Bank1 and Bank2.
\r
366 * - For all other devices it Locks Bank1 and it is equivalent
\r
367 * to FLASH_LockBank1 function.
\r
371 void FLASH_Lock(void)
\r
373 /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */
\r
374 FLASH->CR |= CR_LOCK_Set;
\r
376 #ifdef STM32F10X_XL
\r
377 /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */
\r
378 FLASH->CR2 |= CR_LOCK_Set;
\r
379 #endif /* STM32F10X_XL */
\r
383 * @brief Locks the FLASH Bank1 Program Erase Controller.
\r
384 * @note this function can be used for all STM32F10x devices.
\r
385 * - For STM32F10X_XL devices this function Locks Bank1.
\r
386 * - For all other devices it Locks Bank1 and it is equivalent
\r
387 * to FLASH_Lock function.
\r
391 void FLASH_LockBank1(void)
\r
393 /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */
\r
394 FLASH->CR |= CR_LOCK_Set;
\r
397 #ifdef STM32F10X_XL
\r
399 * @brief Locks the FLASH Bank2 Program Erase Controller.
\r
400 * @note This function can be used only for STM32F10X_XL density devices.
\r
404 void FLASH_LockBank2(void)
\r
406 /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */
\r
407 FLASH->CR2 |= CR_LOCK_Set;
\r
409 #endif /* STM32F10X_XL */
\r
412 * @brief Erases a specified FLASH page.
\r
413 * @note This function can be used for all STM32F10x devices.
\r
414 * @param Page_Address: The page address to be erased.
\r
415 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
\r
416 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
418 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
\r
420 FLASH_Status status = FLASH_COMPLETE;
\r
421 /* Check the parameters */
\r
422 assert_param(IS_FLASH_ADDRESS(Page_Address));
\r
424 #ifdef STM32F10X_XL
\r
425 if(Page_Address < FLASH_BANK1_END_ADDRESS)
\r
427 /* Wait for last operation to be completed */
\r
428 status = FLASH_WaitForLastBank1Operation(EraseTimeout);
\r
429 if(status == FLASH_COMPLETE)
\r
431 /* if the previous operation is completed, proceed to erase the page */
\r
432 FLASH->CR|= CR_PER_Set;
\r
433 FLASH->AR = Page_Address;
\r
434 FLASH->CR|= CR_STRT_Set;
\r
436 /* Wait for last operation to be completed */
\r
437 status = FLASH_WaitForLastBank1Operation(EraseTimeout);
\r
438 if(status != FLASH_TIMEOUT)
\r
440 /* if the erase operation is completed, disable the PER Bit */
\r
441 FLASH->CR &= CR_PER_Reset;
\r
447 /* Wait for last operation to be completed */
\r
448 status = FLASH_WaitForLastBank2Operation(EraseTimeout);
\r
449 if(status == FLASH_COMPLETE)
\r
451 /* if the previous operation is completed, proceed to erase the page */
\r
452 FLASH->CR2|= CR_PER_Set;
\r
453 FLASH->AR2 = Page_Address;
\r
454 FLASH->CR2|= CR_STRT_Set;
\r
456 /* Wait for last operation to be completed */
\r
457 status = FLASH_WaitForLastBank2Operation(EraseTimeout);
\r
458 if(status != FLASH_TIMEOUT)
\r
460 /* if the erase operation is completed, disable the PER Bit */
\r
461 FLASH->CR2 &= CR_PER_Reset;
\r
466 /* Wait for last operation to be completed */
\r
467 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
469 if(status == FLASH_COMPLETE)
\r
471 /* if the previous operation is completed, proceed to erase the page */
\r
472 FLASH->CR|= CR_PER_Set;
\r
473 FLASH->AR = Page_Address;
\r
474 FLASH->CR|= CR_STRT_Set;
\r
476 /* Wait for last operation to be completed */
\r
477 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
478 if(status != FLASH_TIMEOUT)
\r
480 /* if the erase operation is completed, disable the PER Bit */
\r
481 FLASH->CR &= CR_PER_Reset;
\r
484 #endif /* STM32F10X_XL */
\r
486 /* Return the Erase Status */
\r
491 * @brief Erases all FLASH pages.
\r
492 * @note This function can be used for all STM32F10x devices.
\r
494 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
495 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
497 FLASH_Status FLASH_EraseAllPages(void)
\r
499 FLASH_Status status = FLASH_COMPLETE;
\r
501 #ifdef STM32F10X_XL
\r
502 /* Wait for last operation to be completed */
\r
503 status = FLASH_WaitForLastBank1Operation(EraseTimeout);
\r
505 if(status == FLASH_COMPLETE)
\r
507 /* if the previous operation is completed, proceed to erase all pages */
\r
508 FLASH->CR |= CR_MER_Set;
\r
509 FLASH->CR |= CR_STRT_Set;
\r
511 /* Wait for last operation to be completed */
\r
512 status = FLASH_WaitForLastBank1Operation(EraseTimeout);
\r
513 if(status != FLASH_TIMEOUT)
\r
515 /* if the erase operation is completed, disable the MER Bit */
\r
516 FLASH->CR &= CR_MER_Reset;
\r
519 if(status == FLASH_COMPLETE)
\r
521 /* if the previous operation is completed, proceed to erase all pages */
\r
522 FLASH->CR2 |= CR_MER_Set;
\r
523 FLASH->CR2 |= CR_STRT_Set;
\r
525 /* Wait for last operation to be completed */
\r
526 status = FLASH_WaitForLastBank2Operation(EraseTimeout);
\r
527 if(status != FLASH_TIMEOUT)
\r
529 /* if the erase operation is completed, disable the MER Bit */
\r
530 FLASH->CR2 &= CR_MER_Reset;
\r
534 /* Wait for last operation to be completed */
\r
535 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
536 if(status == FLASH_COMPLETE)
\r
538 /* if the previous operation is completed, proceed to erase all pages */
\r
539 FLASH->CR |= CR_MER_Set;
\r
540 FLASH->CR |= CR_STRT_Set;
\r
542 /* Wait for last operation to be completed */
\r
543 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
544 if(status != FLASH_TIMEOUT)
\r
546 /* if the erase operation is completed, disable the MER Bit */
\r
547 FLASH->CR &= CR_MER_Reset;
\r
550 #endif /* STM32F10X_XL */
\r
552 /* Return the Erase Status */
\r
557 * @brief Erases all Bank1 FLASH pages.
\r
558 * @note This function can be used for all STM32F10x devices.
\r
559 * - For STM32F10X_XL devices this function erases all Bank1 pages.
\r
560 * - For all other devices it erases all Bank1 pages and it is equivalent
\r
561 * to FLASH_EraseAllPages function.
\r
563 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
564 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
566 FLASH_Status FLASH_EraseAllBank1Pages(void)
\r
568 FLASH_Status status = FLASH_COMPLETE;
\r
569 /* Wait for last operation to be completed */
\r
570 status = FLASH_WaitForLastBank1Operation(EraseTimeout);
\r
572 if(status == FLASH_COMPLETE)
\r
574 /* if the previous operation is completed, proceed to erase all pages */
\r
575 FLASH->CR |= CR_MER_Set;
\r
576 FLASH->CR |= CR_STRT_Set;
\r
578 /* Wait for last operation to be completed */
\r
579 status = FLASH_WaitForLastBank1Operation(EraseTimeout);
\r
580 if(status != FLASH_TIMEOUT)
\r
582 /* if the erase operation is completed, disable the MER Bit */
\r
583 FLASH->CR &= CR_MER_Reset;
\r
586 /* Return the Erase Status */
\r
590 #ifdef STM32F10X_XL
\r
592 * @brief Erases all Bank2 FLASH pages.
\r
593 * @note This function can be used only for STM32F10x_XL density devices.
\r
595 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
596 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
598 FLASH_Status FLASH_EraseAllBank2Pages(void)
\r
600 FLASH_Status status = FLASH_COMPLETE;
\r
601 /* Wait for last operation to be completed */
\r
602 status = FLASH_WaitForLastBank2Operation(EraseTimeout);
\r
604 if(status == FLASH_COMPLETE)
\r
606 /* if the previous operation is completed, proceed to erase all pages */
\r
607 FLASH->CR2 |= CR_MER_Set;
\r
608 FLASH->CR2 |= CR_STRT_Set;
\r
610 /* Wait for last operation to be completed */
\r
611 status = FLASH_WaitForLastBank2Operation(EraseTimeout);
\r
612 if(status != FLASH_TIMEOUT)
\r
614 /* if the erase operation is completed, disable the MER Bit */
\r
615 FLASH->CR2 &= CR_MER_Reset;
\r
618 /* Return the Erase Status */
\r
621 #endif /* STM32F10X_XL */
\r
624 * @brief Erases the FLASH option bytes.
\r
625 * @note This functions erases all option bytes except the Read protection (RDP).
\r
626 * @note This function can be used for all STM32F10x devices.
\r
628 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
629 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
631 FLASH_Status FLASH_EraseOptionBytes(void)
\r
633 uint16_t rdptmp = RDP_Key;
\r
635 FLASH_Status status = FLASH_COMPLETE;
\r
637 /* Get the actual read protection Option Byte value */
\r
638 if(FLASH_GetReadOutProtectionStatus() != RESET)
\r
643 /* Wait for last operation to be completed */
\r
644 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
645 if(status == FLASH_COMPLETE)
\r
647 /* Authorize the small information block programming */
\r
648 FLASH->OPTKEYR = FLASH_KEY1;
\r
649 FLASH->OPTKEYR = FLASH_KEY2;
\r
651 /* if the previous operation is completed, proceed to erase the option bytes */
\r
652 FLASH->CR |= CR_OPTER_Set;
\r
653 FLASH->CR |= CR_STRT_Set;
\r
654 /* Wait for last operation to be completed */
\r
655 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
657 if(status == FLASH_COMPLETE)
\r
659 /* if the erase operation is completed, disable the OPTER Bit */
\r
660 FLASH->CR &= CR_OPTER_Reset;
\r
662 /* Enable the Option Bytes Programming operation */
\r
663 FLASH->CR |= CR_OPTPG_Set;
\r
664 /* Restore the last read protection Option Byte value */
\r
665 OB->RDP = (uint16_t)rdptmp;
\r
666 /* Wait for last operation to be completed */
\r
667 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
669 if(status != FLASH_TIMEOUT)
\r
671 /* if the program operation is completed, disable the OPTPG Bit */
\r
672 FLASH->CR &= CR_OPTPG_Reset;
\r
677 if (status != FLASH_TIMEOUT)
\r
679 /* Disable the OPTPG Bit */
\r
680 FLASH->CR &= CR_OPTPG_Reset;
\r
684 /* Return the erase status */
\r
689 * @brief Programs a word at a specified address.
\r
690 * @note This function can be used for all STM32F10x devices.
\r
691 * @param Address: specifies the address to be programmed.
\r
692 * @param Data: specifies the data to be programmed.
\r
693 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
694 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
696 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
\r
698 FLASH_Status status = FLASH_COMPLETE;
\r
699 __IO uint32_t tmp = 0;
\r
701 /* Check the parameters */
\r
702 assert_param(IS_FLASH_ADDRESS(Address));
\r
704 #ifdef STM32F10X_XL
\r
705 if(Address < FLASH_BANK1_END_ADDRESS - 2)
\r
707 /* Wait for last operation to be completed */
\r
708 status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
\r
709 if(status == FLASH_COMPLETE)
\r
711 /* if the previous operation is completed, proceed to program the new first
\r
713 FLASH->CR |= CR_PG_Set;
\r
715 *(__IO uint16_t*)Address = (uint16_t)Data;
\r
716 /* Wait for last operation to be completed */
\r
717 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
719 if(status == FLASH_COMPLETE)
\r
721 /* if the previous operation is completed, proceed to program the new second
\r
725 *(__IO uint16_t*) tmp = Data >> 16;
\r
727 /* Wait for last operation to be completed */
\r
728 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
730 if(status != FLASH_TIMEOUT)
\r
732 /* Disable the PG Bit */
\r
733 FLASH->CR &= CR_PG_Reset;
\r
738 if (status != FLASH_TIMEOUT)
\r
740 /* Disable the PG Bit */
\r
741 FLASH->CR &= CR_PG_Reset;
\r
746 else if(Address == (FLASH_BANK1_END_ADDRESS - 1))
\r
748 /* Wait for last operation to be completed */
\r
749 status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
\r
751 if(status == FLASH_COMPLETE)
\r
753 /* if the previous operation is completed, proceed to program the new first
\r
755 FLASH->CR |= CR_PG_Set;
\r
757 *(__IO uint16_t*)Address = (uint16_t)Data;
\r
759 /* Wait for last operation to be completed */
\r
760 status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
\r
762 if(status != FLASH_TIMEOUT)
\r
764 /* Disable the PG Bit */
\r
765 FLASH->CR &= CR_PG_Reset;
\r
770 if (status != FLASH_TIMEOUT)
\r
772 /* Disable the PG Bit */
\r
773 FLASH->CR &= CR_PG_Reset;
\r
777 /* Wait for last operation to be completed */
\r
778 status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
\r
780 if(status == FLASH_COMPLETE)
\r
782 /* if the previous operation is completed, proceed to program the new second
\r
784 FLASH->CR2 |= CR_PG_Set;
\r
787 *(__IO uint16_t*) tmp = Data >> 16;
\r
789 /* Wait for last operation to be completed */
\r
790 status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
\r
792 if(status != FLASH_TIMEOUT)
\r
794 /* Disable the PG Bit */
\r
795 FLASH->CR2 &= CR_PG_Reset;
\r
800 if (status != FLASH_TIMEOUT)
\r
802 /* Disable the PG Bit */
\r
803 FLASH->CR2 &= CR_PG_Reset;
\r
809 /* Wait for last operation to be completed */
\r
810 status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
\r
812 if(status == FLASH_COMPLETE)
\r
814 /* if the previous operation is completed, proceed to program the new first
\r
816 FLASH->CR2 |= CR_PG_Set;
\r
818 *(__IO uint16_t*)Address = (uint16_t)Data;
\r
819 /* Wait for last operation to be completed */
\r
820 status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
\r
822 if(status == FLASH_COMPLETE)
\r
824 /* if the previous operation is completed, proceed to program the new second
\r
828 *(__IO uint16_t*) tmp = Data >> 16;
\r
830 /* Wait for last operation to be completed */
\r
831 status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
\r
833 if(status != FLASH_TIMEOUT)
\r
835 /* Disable the PG Bit */
\r
836 FLASH->CR2 &= CR_PG_Reset;
\r
841 if (status != FLASH_TIMEOUT)
\r
843 /* Disable the PG Bit */
\r
844 FLASH->CR2 &= CR_PG_Reset;
\r
850 /* Wait for last operation to be completed */
\r
851 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
853 if(status == FLASH_COMPLETE)
\r
855 /* if the previous operation is completed, proceed to program the new first
\r
857 FLASH->CR |= CR_PG_Set;
\r
859 *(__IO uint16_t*)Address = (uint16_t)Data;
\r
860 /* Wait for last operation to be completed */
\r
861 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
863 if(status == FLASH_COMPLETE)
\r
865 /* if the previous operation is completed, proceed to program the new second
\r
869 *(__IO uint16_t*) tmp = Data >> 16;
\r
871 /* Wait for last operation to be completed */
\r
872 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
874 if(status != FLASH_TIMEOUT)
\r
876 /* Disable the PG Bit */
\r
877 FLASH->CR &= CR_PG_Reset;
\r
882 if (status != FLASH_TIMEOUT)
\r
884 /* Disable the PG Bit */
\r
885 FLASH->CR &= CR_PG_Reset;
\r
889 #endif /* STM32F10X_XL */
\r
891 /* Return the Program Status */
\r
896 * @brief Programs a half word at a specified address.
\r
897 * @note This function can be used for all STM32F10x devices.
\r
898 * @param Address: specifies the address to be programmed.
\r
899 * @param Data: specifies the data to be programmed.
\r
900 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
901 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
903 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
\r
905 FLASH_Status status = FLASH_COMPLETE;
\r
906 /* Check the parameters */
\r
907 assert_param(IS_FLASH_ADDRESS(Address));
\r
909 #ifdef STM32F10X_XL
\r
910 /* Wait for last operation to be completed */
\r
911 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
913 if(Address < FLASH_BANK1_END_ADDRESS)
\r
915 if(status == FLASH_COMPLETE)
\r
917 /* if the previous operation is completed, proceed to program the new data */
\r
918 FLASH->CR |= CR_PG_Set;
\r
920 *(__IO uint16_t*)Address = Data;
\r
921 /* Wait for last operation to be completed */
\r
922 status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
\r
923 if(status != FLASH_TIMEOUT)
\r
925 /* if the program operation is completed, disable the PG Bit */
\r
926 FLASH->CR &= CR_PG_Reset;
\r
932 if(status == FLASH_COMPLETE)
\r
934 /* if the previous operation is completed, proceed to program the new data */
\r
935 FLASH->CR2 |= CR_PG_Set;
\r
937 *(__IO uint16_t*)Address = Data;
\r
938 /* Wait for last operation to be completed */
\r
939 status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
\r
940 if(status != FLASH_TIMEOUT)
\r
942 /* if the program operation is completed, disable the PG Bit */
\r
943 FLASH->CR2 &= CR_PG_Reset;
\r
948 /* Wait for last operation to be completed */
\r
949 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
951 if(status == FLASH_COMPLETE)
\r
953 /* if the previous operation is completed, proceed to program the new data */
\r
954 FLASH->CR |= CR_PG_Set;
\r
956 *(__IO uint16_t*)Address = Data;
\r
957 /* Wait for last operation to be completed */
\r
958 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
959 if(status != FLASH_TIMEOUT)
\r
961 /* if the program operation is completed, disable the PG Bit */
\r
962 FLASH->CR &= CR_PG_Reset;
\r
965 #endif /* STM32F10X_XL */
\r
967 /* Return the Program Status */
\r
972 * @brief Programs a half word at a specified Option Byte Data address.
\r
973 * @note This function can be used for all STM32F10x devices.
\r
974 * @param Address: specifies the address to be programmed.
\r
975 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
\r
976 * @param Data: specifies the data to be programmed.
\r
977 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
978 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
980 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
\r
982 FLASH_Status status = FLASH_COMPLETE;
\r
983 /* Check the parameters */
\r
984 assert_param(IS_OB_DATA_ADDRESS(Address));
\r
985 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
987 if(status == FLASH_COMPLETE)
\r
989 /* Authorize the small information block programming */
\r
990 FLASH->OPTKEYR = FLASH_KEY1;
\r
991 FLASH->OPTKEYR = FLASH_KEY2;
\r
992 /* Enables the Option Bytes Programming operation */
\r
993 FLASH->CR |= CR_OPTPG_Set;
\r
994 *(__IO uint16_t*)Address = Data;
\r
996 /* Wait for last operation to be completed */
\r
997 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
998 if(status != FLASH_TIMEOUT)
\r
1000 /* if the program operation is completed, disable the OPTPG Bit */
\r
1001 FLASH->CR &= CR_OPTPG_Reset;
\r
1004 /* Return the Option Byte Data Program Status */
\r
1009 * @brief Write protects the desired pages
\r
1010 * @note This function can be used for all STM32F10x devices.
\r
1011 * @param FLASH_Pages: specifies the address of the pages to be write protected.
\r
1012 * This parameter can be:
\r
1013 * @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31
\r
1014 * @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3
\r
1015 * and FLASH_WRProt_Pages124to127
\r
1016 * @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and
\r
1017 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
\r
1018 * @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and
\r
1019 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127
\r
1020 * @arg For @b STM32_XL-density_devices: value between FLASH_WRProt_Pages0to1 and
\r
1021 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to511
\r
1022 * @arg FLASH_WRProt_AllPages
\r
1023 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1024 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1026 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
\r
1028 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
\r
1030 FLASH_Status status = FLASH_COMPLETE;
\r
1032 /* Check the parameters */
\r
1033 assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
\r
1035 FLASH_Pages = (uint32_t)(~FLASH_Pages);
\r
1036 WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);
\r
1037 WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);
\r
1038 WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);
\r
1039 WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);
\r
1041 /* Wait for last operation to be completed */
\r
1042 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1044 if(status == FLASH_COMPLETE)
\r
1046 /* Authorizes the small information block programming */
\r
1047 FLASH->OPTKEYR = FLASH_KEY1;
\r
1048 FLASH->OPTKEYR = FLASH_KEY2;
\r
1049 FLASH->CR |= CR_OPTPG_Set;
\r
1050 if(WRP0_Data != 0xFF)
\r
1052 OB->WRP0 = WRP0_Data;
\r
1054 /* Wait for last operation to be completed */
\r
1055 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1057 if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
\r
1059 OB->WRP1 = WRP1_Data;
\r
1061 /* Wait for last operation to be completed */
\r
1062 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1064 if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
\r
1066 OB->WRP2 = WRP2_Data;
\r
1068 /* Wait for last operation to be completed */
\r
1069 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1072 if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
\r
1074 OB->WRP3 = WRP3_Data;
\r
1076 /* Wait for last operation to be completed */
\r
1077 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1080 if(status != FLASH_TIMEOUT)
\r
1082 /* if the program operation is completed, disable the OPTPG Bit */
\r
1083 FLASH->CR &= CR_OPTPG_Reset;
\r
1086 /* Return the write protection operation Status */
\r
1091 * @brief Enables or disables the read out protection.
\r
1092 * @note If the user has already programmed the other option bytes before calling
\r
1093 * this function, he must re-program them since this function erases all option bytes.
\r
1094 * @note This function can be used for all STM32F10x devices.
\r
1095 * @param Newstate: new state of the ReadOut Protection.
\r
1096 * This parameter can be: ENABLE or DISABLE.
\r
1097 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1098 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1100 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
\r
1102 FLASH_Status status = FLASH_COMPLETE;
\r
1103 /* Check the parameters */
\r
1104 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1105 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
1106 if(status == FLASH_COMPLETE)
\r
1108 /* Authorizes the small information block programming */
\r
1109 FLASH->OPTKEYR = FLASH_KEY1;
\r
1110 FLASH->OPTKEYR = FLASH_KEY2;
\r
1111 FLASH->CR |= CR_OPTER_Set;
\r
1112 FLASH->CR |= CR_STRT_Set;
\r
1113 /* Wait for last operation to be completed */
\r
1114 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
1115 if(status == FLASH_COMPLETE)
\r
1117 /* if the erase operation is completed, disable the OPTER Bit */
\r
1118 FLASH->CR &= CR_OPTER_Reset;
\r
1119 /* Enable the Option Bytes Programming operation */
\r
1120 FLASH->CR |= CR_OPTPG_Set;
\r
1121 if(NewState != DISABLE)
\r
1127 OB->RDP = RDP_Key;
\r
1129 /* Wait for last operation to be completed */
\r
1130 status = FLASH_WaitForLastOperation(EraseTimeout);
\r
1132 if(status != FLASH_TIMEOUT)
\r
1134 /* if the program operation is completed, disable the OPTPG Bit */
\r
1135 FLASH->CR &= CR_OPTPG_Reset;
\r
1140 if(status != FLASH_TIMEOUT)
\r
1142 /* Disable the OPTER Bit */
\r
1143 FLASH->CR &= CR_OPTER_Reset;
\r
1147 /* Return the protection operation Status */
\r
1152 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
\r
1153 * @note This function can be used for all STM32F10x devices.
\r
1154 * @param OB_IWDG: Selects the IWDG mode
\r
1155 * This parameter can be one of the following values:
\r
1156 * @arg OB_IWDG_SW: Software IWDG selected
\r
1157 * @arg OB_IWDG_HW: Hardware IWDG selected
\r
1158 * @param OB_STOP: Reset event when entering STOP mode.
\r
1159 * This parameter can be one of the following values:
\r
1160 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
\r
1161 * @arg OB_STOP_RST: Reset generated when entering in STOP
\r
1162 * @param OB_STDBY: Reset event when entering Standby mode.
\r
1163 * This parameter can be one of the following values:
\r
1164 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
\r
1165 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
\r
1166 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1167 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1169 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
\r
1171 FLASH_Status status = FLASH_COMPLETE;
\r
1173 /* Check the parameters */
\r
1174 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
\r
1175 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
\r
1176 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
\r
1178 /* Authorize the small information block programming */
\r
1179 FLASH->OPTKEYR = FLASH_KEY1;
\r
1180 FLASH->OPTKEYR = FLASH_KEY2;
\r
1182 /* Wait for last operation to be completed */
\r
1183 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1185 if(status == FLASH_COMPLETE)
\r
1187 /* Enable the Option Bytes Programming operation */
\r
1188 FLASH->CR |= CR_OPTPG_Set;
\r
1190 OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8)));
\r
1192 /* Wait for last operation to be completed */
\r
1193 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1194 if(status != FLASH_TIMEOUT)
\r
1196 /* if the program operation is completed, disable the OPTPG Bit */
\r
1197 FLASH->CR &= CR_OPTPG_Reset;
\r
1200 /* Return the Option Byte program Status */
\r
1204 #ifdef STM32F10X_XL
\r
1206 * @brief Configures to boot from Bank1 or Bank2.
\r
1207 * @note This function can be used only for STM32F10x_XL density devices.
\r
1208 * @param FLASH_BOOT: select the FLASH Bank to boot from.
\r
1209 * This parameter can be one of the following values:
\r
1210 * @arg FLASH_BOOT_Bank1: At startup, if boot pins are set in boot from user Flash
\r
1211 * position and this parameter is selected the device will boot from Bank1(Default).
\r
1212 * @arg FLASH_BOOT_Bank2: At startup, if boot pins are set in boot from user Flash
\r
1213 * position and this parameter is selected the device will boot from Bank2 or Bank1,
\r
1214 * depending on the activation of the bank. The active banks are checked in
\r
1215 * the following order: Bank2, followed by Bank1.
\r
1216 * The active bank is recognized by the value programmed at the base address
\r
1217 * of the respective bank (corresponding to the initial stack pointer value
\r
1218 * in the interrupt vector table).
\r
1219 * For more information, please refer to AN2606 from www.st.com.
\r
1220 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1221 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1223 FLASH_Status FLASH_BootConfig(uint16_t FLASH_BOOT)
\r
1225 FLASH_Status status = FLASH_COMPLETE;
\r
1226 assert_param(IS_FLASH_BOOT(FLASH_BOOT));
\r
1227 /* Authorize the small information block programming */
\r
1228 FLASH->OPTKEYR = FLASH_KEY1;
\r
1229 FLASH->OPTKEYR = FLASH_KEY2;
\r
1231 /* Wait for last operation to be completed */
\r
1232 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1234 if(status == FLASH_COMPLETE)
\r
1236 /* Enable the Option Bytes Programming operation */
\r
1237 FLASH->CR |= CR_OPTPG_Set;
\r
1239 if(FLASH_BOOT == FLASH_BOOT_Bank1)
\r
1241 OB->USER |= OB_USER_BFB2;
\r
1245 OB->USER &= (uint16_t)(~(uint16_t)(OB_USER_BFB2));
\r
1247 /* Wait for last operation to be completed */
\r
1248 status = FLASH_WaitForLastOperation(ProgramTimeout);
\r
1249 if(status != FLASH_TIMEOUT)
\r
1251 /* if the program operation is completed, disable the OPTPG Bit */
\r
1252 FLASH->CR &= CR_OPTPG_Reset;
\r
1255 /* Return the Option Byte program Status */
\r
1258 #endif /* STM32F10X_XL */
\r
1261 * @brief Returns the FLASH User Option Bytes values.
\r
1262 * @note This function can be used for all STM32F10x devices.
\r
1264 * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
\r
1265 * and RST_STDBY(Bit2).
\r
1267 uint32_t FLASH_GetUserOptionByte(void)
\r
1269 /* Return the User Option Byte */
\r
1270 return (uint32_t)(FLASH->OBR >> 2);
\r
1274 * @brief Returns the FLASH Write Protection Option Bytes Register value.
\r
1275 * @note This function can be used for all STM32F10x devices.
\r
1277 * @retval The FLASH Write Protection Option Bytes Register value
\r
1279 uint32_t FLASH_GetWriteProtectionOptionByte(void)
\r
1281 /* Return the Falsh write protection Register value */
\r
1282 return (uint32_t)(FLASH->WRPR);
\r
1286 * @brief Checks whether the FLASH Read Out Protection Status is set or not.
\r
1287 * @note This function can be used for all STM32F10x devices.
\r
1289 * @retval FLASH ReadOut Protection Status(SET or RESET)
\r
1291 FlagStatus FLASH_GetReadOutProtectionStatus(void)
\r
1293 FlagStatus readoutstatus = RESET;
\r
1294 if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)
\r
1296 readoutstatus = SET;
\r
1300 readoutstatus = RESET;
\r
1302 return readoutstatus;
\r
1306 * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
\r
1307 * @note This function can be used for all STM32F10x devices.
\r
1309 * @retval FLASH Prefetch Buffer Status (SET or RESET).
\r
1311 FlagStatus FLASH_GetPrefetchBufferStatus(void)
\r
1313 FlagStatus bitstatus = RESET;
\r
1315 if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)
\r
1321 bitstatus = RESET;
\r
1323 /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
\r
1324 return bitstatus;
\r
1328 * @brief Enables or disables the specified FLASH interrupts.
\r
1329 * @note This function can be used for all STM32F10x devices.
\r
1330 * - For STM32F10X_XL devices, enables or disables the specified FLASH interrupts
\r
1331 for Bank1 and Bank2.
\r
1332 * - For other devices it enables or disables the specified FLASH interrupts for Bank1.
\r
1333 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
\r
1334 * This parameter can be any combination of the following values:
\r
1335 * @arg FLASH_IT_ERROR: FLASH Error Interrupt
\r
1336 * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
\r
1337 * @param NewState: new state of the specified Flash interrupts.
\r
1338 * This parameter can be: ENABLE or DISABLE.
\r
1341 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
\r
1343 #ifdef STM32F10X_XL
\r
1344 /* Check the parameters */
\r
1345 assert_param(IS_FLASH_IT(FLASH_IT));
\r
1346 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1348 if((FLASH_IT & 0x80000000) != 0x0)
\r
1350 if(NewState != DISABLE)
\r
1352 /* Enable the interrupt sources */
\r
1353 FLASH->CR2 |= (FLASH_IT & 0x7FFFFFFF);
\r
1357 /* Disable the interrupt sources */
\r
1358 FLASH->CR2 &= ~(uint32_t)(FLASH_IT & 0x7FFFFFFF);
\r
1363 if(NewState != DISABLE)
\r
1365 /* Enable the interrupt sources */
\r
1366 FLASH->CR |= FLASH_IT;
\r
1370 /* Disable the interrupt sources */
\r
1371 FLASH->CR &= ~(uint32_t)FLASH_IT;
\r
1375 /* Check the parameters */
\r
1376 assert_param(IS_FLASH_IT(FLASH_IT));
\r
1377 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1379 if(NewState != DISABLE)
\r
1381 /* Enable the interrupt sources */
\r
1382 FLASH->CR |= FLASH_IT;
\r
1386 /* Disable the interrupt sources */
\r
1387 FLASH->CR &= ~(uint32_t)FLASH_IT;
\r
1389 #endif /* STM32F10X_XL */
\r
1393 * @brief Checks whether the specified FLASH flag is set or not.
\r
1394 * @note This function can be used for all STM32F10x devices.
\r
1395 * - For STM32F10X_XL devices, this function checks whether the specified
\r
1396 * Bank1 or Bank2 flag is set or not.
\r
1397 * - For other devices, it checks whether the specified Bank1 flag is
\r
1399 * @param FLASH_FLAG: specifies the FLASH flag to check.
\r
1400 * This parameter can be one of the following values:
\r
1401 * @arg FLASH_FLAG_BSY: FLASH Busy flag
\r
1402 * @arg FLASH_FLAG_PGERR: FLASH Program error flag
\r
1403 * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
\r
1404 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
\r
1405 * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag
\r
1406 * @retval The new state of FLASH_FLAG (SET or RESET).
\r
1408 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
\r
1410 FlagStatus bitstatus = RESET;
\r
1412 #ifdef STM32F10X_XL
\r
1413 /* Check the parameters */
\r
1414 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
\r
1415 if(FLASH_FLAG == FLASH_FLAG_OPTERR)
\r
1417 if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
\r
1423 bitstatus = RESET;
\r
1428 if((FLASH_FLAG & 0x80000000) != 0x0)
\r
1430 if((FLASH->SR2 & FLASH_FLAG) != (uint32_t)RESET)
\r
1436 bitstatus = RESET;
\r
1441 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
\r
1447 bitstatus = RESET;
\r
1452 /* Check the parameters */
\r
1453 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
\r
1454 if(FLASH_FLAG == FLASH_FLAG_OPTERR)
\r
1456 if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
\r
1462 bitstatus = RESET;
\r
1467 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
\r
1473 bitstatus = RESET;
\r
1476 #endif /* STM32F10X_XL */
\r
1478 /* Return the new state of FLASH_FLAG (SET or RESET) */
\r
1483 * @brief Clears the FLASH
\92s pending flags.
\r
1484 * @note This function can be used for all STM32F10x devices.
\r
1485 * - For STM32F10X_XL devices, this function clears Bank1 or Bank2
\92s pending flags
\r
1486 * - For other devices, it clears Bank1
\92s pending flags.
\r
1487 * @param FLASH_FLAG: specifies the FLASH flags to clear.
\r
1488 * This parameter can be any combination of the following values:
\r
1489 * @arg FLASH_FLAG_PGERR: FLASH Program error flag
\r
1490 * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
\r
1491 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
\r
1494 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
\r
1496 #ifdef STM32F10X_XL
\r
1497 /* Check the parameters */
\r
1498 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
\r
1500 if((FLASH_FLAG & 0x80000000) != 0x0)
\r
1502 /* Clear the flags */
\r
1503 FLASH->SR2 = FLASH_FLAG;
\r
1507 /* Clear the flags */
\r
1508 FLASH->SR = FLASH_FLAG;
\r
1512 /* Check the parameters */
\r
1513 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
\r
1515 /* Clear the flags */
\r
1516 FLASH->SR = FLASH_FLAG;
\r
1517 #endif /* STM32F10X_XL */
\r
1521 * @brief Returns the FLASH Status.
\r
1522 * @note This function can be used for all STM32F10x devices, it is equivalent
\r
1523 * to FLASH_GetBank1Status function.
\r
1525 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
\r
1526 * FLASH_ERROR_WRP or FLASH_COMPLETE
\r
1528 FLASH_Status FLASH_GetStatus(void)
\r
1530 FLASH_Status flashstatus = FLASH_COMPLETE;
\r
1532 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
\r
1534 flashstatus = FLASH_BUSY;
\r
1538 if((FLASH->SR & FLASH_FLAG_PGERR) != 0)
\r
1540 flashstatus = FLASH_ERROR_PG;
\r
1544 if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )
\r
1546 flashstatus = FLASH_ERROR_WRP;
\r
1550 flashstatus = FLASH_COMPLETE;
\r
1554 /* Return the Flash Status */
\r
1555 return flashstatus;
\r
1559 * @brief Returns the FLASH Bank1 Status.
\r
1560 * @note This function can be used for all STM32F10x devices, it is equivalent
\r
1561 * to FLASH_GetStatus function.
\r
1563 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
\r
1564 * FLASH_ERROR_WRP or FLASH_COMPLETE
\r
1566 FLASH_Status FLASH_GetBank1Status(void)
\r
1568 FLASH_Status flashstatus = FLASH_COMPLETE;
\r
1570 if((FLASH->SR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY)
\r
1572 flashstatus = FLASH_BUSY;
\r
1576 if((FLASH->SR & FLASH_FLAG_BANK1_PGERR) != 0)
\r
1578 flashstatus = FLASH_ERROR_PG;
\r
1582 if((FLASH->SR & FLASH_FLAG_BANK1_WRPRTERR) != 0 )
\r
1584 flashstatus = FLASH_ERROR_WRP;
\r
1588 flashstatus = FLASH_COMPLETE;
\r
1592 /* Return the Flash Status */
\r
1593 return flashstatus;
\r
1596 #ifdef STM32F10X_XL
\r
1598 * @brief Returns the FLASH Bank2 Status.
\r
1599 * @note This function can be used for STM32F10x_XL density devices.
\r
1601 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
\r
1602 * FLASH_ERROR_WRP or FLASH_COMPLETE
\r
1604 FLASH_Status FLASH_GetBank2Status(void)
\r
1606 FLASH_Status flashstatus = FLASH_COMPLETE;
\r
1608 if((FLASH->SR2 & (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF))
\r
1610 flashstatus = FLASH_BUSY;
\r
1614 if((FLASH->SR2 & (FLASH_FLAG_BANK2_PGERR & 0x7FFFFFFF)) != 0)
\r
1616 flashstatus = FLASH_ERROR_PG;
\r
1620 if((FLASH->SR2 & (FLASH_FLAG_BANK2_WRPRTERR & 0x7FFFFFFF)) != 0 )
\r
1622 flashstatus = FLASH_ERROR_WRP;
\r
1626 flashstatus = FLASH_COMPLETE;
\r
1630 /* Return the Flash Status */
\r
1631 return flashstatus;
\r
1633 #endif /* STM32F10X_XL */
\r
1635 * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
\r
1636 * @note This function can be used for all STM32F10x devices,
\r
1637 * it is equivalent to FLASH_WaitForLastBank1Operation.
\r
1638 * - For STM32F10X_XL devices this function waits for a Bank1 Flash operation
\r
1639 * to complete or a TIMEOUT to occur.
\r
1640 * - For all other devices it waits for a Flash operation to complete
\r
1641 * or a TIMEOUT to occur.
\r
1642 * @param Timeout: FLASH progamming Timeout
\r
1643 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1644 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1646 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
\r
1648 FLASH_Status status = FLASH_COMPLETE;
\r
1650 /* Check for the Flash Status */
\r
1651 status = FLASH_GetBank1Status();
\r
1652 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
\r
1653 while((status == FLASH_BUSY) && (Timeout != 0x00))
\r
1655 status = FLASH_GetBank1Status();
\r
1658 if(Timeout == 0x00 )
\r
1660 status = FLASH_TIMEOUT;
\r
1662 /* Return the operation status */
\r
1667 * @brief Waits for a Flash operation on Bank1 to complete or a TIMEOUT to occur.
\r
1668 * @note This function can be used for all STM32F10x devices,
\r
1669 * it is equivalent to FLASH_WaitForLastOperation.
\r
1670 * @param Timeout: FLASH progamming Timeout
\r
1671 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1672 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1674 FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout)
\r
1676 FLASH_Status status = FLASH_COMPLETE;
\r
1678 /* Check for the Flash Status */
\r
1679 status = FLASH_GetBank1Status();
\r
1680 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
\r
1681 while((status == FLASH_FLAG_BANK1_BSY) && (Timeout != 0x00))
\r
1683 status = FLASH_GetBank1Status();
\r
1686 if(Timeout == 0x00 )
\r
1688 status = FLASH_TIMEOUT;
\r
1690 /* Return the operation status */
\r
1694 #ifdef STM32F10X_XL
\r
1696 * @brief Waits for a Flash operation on Bank2 to complete or a TIMEOUT to occur.
\r
1697 * @note This function can be used only for STM32F10x_XL density devices.
\r
1698 * @param Timeout: FLASH progamming Timeout
\r
1699 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
\r
1700 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1702 FLASH_Status FLASH_WaitForLastBank2Operation(uint32_t Timeout)
\r
1704 FLASH_Status status = FLASH_COMPLETE;
\r
1706 /* Check for the Flash Status */
\r
1707 status = FLASH_GetBank2Status();
\r
1708 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
\r
1709 while((status == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) && (Timeout != 0x00))
\r
1711 status = FLASH_GetBank2Status();
\r
1714 if(Timeout == 0x00 )
\r
1716 status = FLASH_TIMEOUT;
\r
1718 /* Return the operation status */
\r
1721 #endif /* STM32F10X_XL */
\r
1735 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r