1 /**
2 ******************************************************************************
3 * @file stm32f30x_flash.c
4 * @author MCD Application Team
5 * @version V1.2.3
6 * @date 10-July-2015
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the FLASH peripheral:
9 * + FLASH Interface configuration
10 * + FLASH Memory Programming
11 * + Option Bytes Programming
12 * + Interrupts and flags management
13 *
14 @verbatim
15
16 ===============================================================================
17 ##### How to use this driver #####
18 ===============================================================================
19 [..] This driver provides functions to configure and program the FLASH
20 memory of all STM32F30x devices. These functions are split in 4 groups:
21 (#) FLASH Interface configuration functions: this group includes the
22 management of following features:
23 (++) Set the latency.
24 (++) Enable/Disable the Half Cycle Access.
25 (++) Enable/Disable the prefetch buffer.
26 (#) FLASH Memory Programming functions: this group includes all needed
27 functions to erase and program the main memory:
28 (++) Lock and Unlock the FLASH interface.
29 (++) Erase function: Erase page, erase all pages.
30 (++) Program functions: Half Word and Word write.
31 (#) FLASH Option Bytes Programming functions: this group includes all
32 needed functions to manage the Option Bytes:
33 (++) Lock and Unlock the Flash Option bytes.
34 (++) Launch the Option Bytes loader
35 (++) Erase the Option Bytes
36 (++) Set/Reset the write protection
37 (++) Set the Read protection Level
38 (++) Program the user option Bytes
39 (++) Set/Reset the BOOT1 bit
40 (++) Enable/Disable the VDDA Analog Monitoring
41 (++) Enable/Disable the SRAM parity
42 (++) Get the user option bytes
43 (++) Get the Write protection
44 (++) Get the read protection status
45 (#) FLASH Interrupts and flags management functions: this group includes
46 all needed functions to:
47 (++) Enable/Disable the FLASH interrupt sources.
48 (++) Get flags status.
49 (++) Clear flags.
50 (++) Get FLASH operation status.
51 (++) Wait for last FLASH operation.
52
53 @endverbatim
54
55 ******************************************************************************
56 * @attention
57 *
58 * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2>
59 *
60 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
61 * You may not use this file except in compliance with the License.
62 * You may obtain a copy of the License at:
63 *
64 * http://www.st.com/software_license_agreement_liberty_v2
65 *
66 * Unless required by applicable law or agreed to in writing, software
67 * distributed under the License is distributed on an "AS IS" BASIS,
68 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
69 * See the License for the specific language governing permissions and
70 * limitations under the License.
71 *
72 ******************************************************************************
73 */
74
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f30x_flash.h"
77
78 /** @addtogroup STM32F30x_StdPeriph_Driver
79 * @{
80 */
81
82 /** @defgroup FLASH
83 * @brief FLASH driver modules
84 * @{
85 */
86
87 /* Private typedef -----------------------------------------------------------*/
88 /* Private define ------------------------------------------------------------*/
89
90 /* FLASH Mask */
91 #define RDPRT_MASK ((uint32_t)0x00000002)
92 #define WRP01_MASK ((uint32_t)0x0000FFFF)
93 #define WRP23_MASK ((uint32_t)0xFFFF0000)
94 /* Private macro -------------------------------------------------------------*/
95 /* Private variables ---------------------------------------------------------*/
96 /* Private function prototypes -----------------------------------------------*/
97 /* Private functions ---------------------------------------------------------*/
98
99 /** @defgroup FLASH_Private_Functions
100 * @{
101 */
102
103 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
104 * @brief FLASH Interface configuration functions
105 *
106
107 @verbatim
108 ===============================================================================
109 ##### FLASH Interface configuration functions #####
110 ===============================================================================
111 [..] This group includes the following functions:
112 (+) void FLASH_SetLatency(uint32_t FLASH_Latency);
113 (+) void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);
114 (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
115 [..] The unlock sequence is not needed for these functions.
116
117 @endverbatim
118 * @{
119 */
120
121 /**
122 * @brief Sets the code latency value.
123 * @param FLASH_Latency: specifies the FLASH Latency value.
124 * This parameter can be one of the following values:
125 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
126 * @arg FLASH_Latency_1: FLASH One Latency cycle
127 * @arg FLASH_Latency_2: FLASH Two Latency cycles
128 * @retval None
129 */
FLASH_SetLatency(uint32_t FLASH_Latency)130 void FLASH_SetLatency(uint32_t FLASH_Latency)
131 {
132 uint32_t tmpreg = 0;
133
134 /* Check the parameters */
135 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
136
137 /* Read the ACR register */
138 tmpreg = FLASH->ACR;
139
140 /* Sets the Latency value */
141 tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
142 tmpreg |= FLASH_Latency;
143
144 /* Write the ACR register */
145 FLASH->ACR = tmpreg;
146 }
147
148 /**
149 * @brief Enables or disables the Half cycle flash access.
150 * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
151 * This parameter can be one of the following values:
152 * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
153 * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
154 * @retval None
155 */
FLASH_HalfCycleAccessCmd(FunctionalState NewState)156 void FLASH_HalfCycleAccessCmd(FunctionalState NewState)
157 {
158 /* Check the parameters */
159 assert_param(IS_FUNCTIONAL_STATE(NewState));
160
161 if(NewState != DISABLE)
162 {
163 FLASH->ACR |= FLASH_ACR_HLFCYA;
164 }
165 else
166 {
167 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_HLFCYA));
168 }
169 }
170
171 /**
172 * @brief Enables or disables the Prefetch Buffer.
173 * @param NewState: new state of the Prefetch Buffer.
174 * This parameter can be: ENABLE or DISABLE.
175 * @retval None
176 */
FLASH_PrefetchBufferCmd(FunctionalState NewState)177 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
178 {
179 /* Check the parameters */
180 assert_param(IS_FUNCTIONAL_STATE(NewState));
181
182 if(NewState != DISABLE)
183 {
184 FLASH->ACR |= FLASH_ACR_PRFTBE;
185 }
186 else
187 {
188 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
189 }
190 }
191
192 /**
193 * @}
194 */
195
196 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
197 * @brief FLASH Memory Programming functions
198 *
199 @verbatim
200 ===============================================================================
201 ##### FLASH Memory Programming functions #####
202 ===============================================================================
203 [..] This group includes the following functions:
204 (+) void FLASH_Unlock(void);
205 (+) void FLASH_Lock(void);
206 (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
207 (+) FLASH_Status FLASH_EraseAllPages(void);
208 (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
209 (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
210 [..] Any operation of erase or program should follow these steps:
211 (#) Call the FLASH_Unlock() function to enable the FLASH control register
212 program memory access.
213 (#) Call the desired function to erase page or program data.
214 (#) Call the FLASH_Lock() function to disable the FLASH control register
215 access (recommended to protect the FLASH memory against possible
216 unwanted operation).
217
218 @endverbatim
219 * @{
220 */
221
222 /**
223 * @brief Unlocks the FLASH control register access
224 * @param None
225 * @retval None
226 */
FLASH_Unlock(void)227 void FLASH_Unlock(void)
228 {
229 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
230 {
231 /* Authorize the FLASH Registers access */
232 FLASH->KEYR = FLASH_KEY1;
233 FLASH->KEYR = FLASH_KEY2;
234 }
235 }
236
237 /**
238 * @brief Locks the FLASH control register access
239 * @param None
240 * @retval None
241 */
FLASH_Lock(void)242 void FLASH_Lock(void)
243 {
244 /* Set the LOCK Bit to lock the FLASH Registers access */
245 FLASH->CR |= FLASH_CR_LOCK;
246 }
247
248 /**
249 * @brief Erases a specified page in program memory.
250 * @note To correctly run this function, the FLASH_Unlock() function
251 * must be called before.
252 * @note Call the FLASH_Lock() to disable the flash memory access
253 * (recommended to protect the FLASH memory against possible unwanted operation)
254 * @param Page_Address: The page address in program memory to be erased.
255 * @note A Page is erased in the Program memory only if the address to load
256 * is the start address of a page (multiple of 1024 bytes).
257 * @retval FLASH Status: The returned value can be:
258 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
259 */
FLASH_ErasePage(uint32_t Page_Address)260 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
261 {
262 FLASH_Status status = FLASH_COMPLETE;
263
264 /* Check the parameters */
265 assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
266
267 /* Wait for last operation to be completed */
268 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
269
270 if(status == FLASH_COMPLETE)
271 {
272 /* If the previous operation is completed, proceed to erase the page */
273 FLASH->CR |= FLASH_CR_PER;
274 FLASH->AR = Page_Address;
275 FLASH->CR |= FLASH_CR_STRT;
276
277 /* Wait for last operation to be completed */
278 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
279
280 /* Disable the PER Bit */
281 FLASH->CR &= ~FLASH_CR_PER;
282 }
283
284 /* Return the Erase Status */
285 return status;
286 }
287
288 /**
289 * @brief Erases all FLASH pages.
290 * @note To correctly run this function, the FLASH_Unlock() function
291 * must be called before.
292 * all the FLASH_Lock() to disable the flash memory access
293 * (recommended to protect the FLASH memory against possible unwanted operation)
294 * @param None
295 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
296 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
297 */
FLASH_EraseAllPages(void)298 FLASH_Status FLASH_EraseAllPages(void)
299 {
300 FLASH_Status status = FLASH_COMPLETE;
301
302 /* Wait for last operation to be completed */
303 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
304
305 if(status == FLASH_COMPLETE)
306 {
307 /* if the previous operation is completed, proceed to erase all pages */
308 FLASH->CR |= FLASH_CR_MER;
309 FLASH->CR |= FLASH_CR_STRT;
310
311 /* Wait for last operation to be completed */
312 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
313
314 /* Disable the MER Bit */
315 FLASH->CR &= ~FLASH_CR_MER;
316 }
317
318 /* Return the Erase Status */
319 return status;
320 }
321
322 /**
323 * @brief Programs a word at a specified address.
324 * @note To correctly run this function, the FLASH_Unlock() function
325 * must be called before.
326 * Call the FLASH_Lock() to disable the flash memory access
327 * (recommended to protect the FLASH memory against possible unwanted operation)
328 * @param Address: specifies the address to be programmed.
329 * @param Data: specifies the data to be programmed.
330 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
331 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
332 */
FLASH_ProgramWord(uint32_t Address,uint32_t Data)333 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
334 {
335 FLASH_Status status = FLASH_COMPLETE;
336 __IO uint32_t tmp = 0;
337
338 /* Check the parameters */
339 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
340
341 /* Wait for last operation to be completed */
342 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
343
344 if(status == FLASH_COMPLETE)
345 {
346 /* If the previous operation is completed, proceed to program the new first
347 half word */
348 FLASH->CR |= FLASH_CR_PG;
349
350 *(__IO uint16_t*)Address = (uint16_t)Data;
351
352 /* Wait for last operation to be completed */
353 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
354
355 if(status == FLASH_COMPLETE)
356 {
357 /* If the previous operation is completed, proceed to program the new second
358 half word */
359 tmp = Address + 2;
360
361 *(__IO uint16_t*) tmp = Data >> 16;
362
363 /* Wait for last operation to be completed */
364 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
365
366 /* Disable the PG Bit */
367 FLASH->CR &= ~FLASH_CR_PG;
368 }
369 else
370 {
371 /* Disable the PG Bit */
372 FLASH->CR &= ~FLASH_CR_PG;
373 }
374 }
375
376 /* Return the Program Status */
377 return status;
378 }
379
380 /**
381 * @brief Programs a half word at a specified address.
382 * @note To correctly run this function, the FLASH_Unlock() function
383 * must be called before.
384 * Call the FLASH_Lock() to disable the flash memory access
385 * (recommended to protect the FLASH memory against possible unwanted operation)
386 * @param Address: specifies the address to be programmed.
387 * @param Data: specifies the data to be programmed.
388 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
389 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
390 */
FLASH_ProgramHalfWord(uint32_t Address,uint16_t Data)391 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
392 {
393 FLASH_Status status = FLASH_COMPLETE;
394
395 /* Check the parameters */
396 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
397
398 /* Wait for last operation to be completed */
399 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
400
401 if(status == FLASH_COMPLETE)
402 {
403 /* If the previous operation is completed, proceed to program the new data */
404 FLASH->CR |= FLASH_CR_PG;
405
406 *(__IO uint16_t*)Address = Data;
407
408 /* Wait for last operation to be completed */
409 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
410
411 /* Disable the PG Bit */
412 FLASH->CR &= ~FLASH_CR_PG;
413 }
414
415 /* Return the Program Status */
416 return status;
417 }
418
419 /**
420 * @}
421 */
422
423 /** @defgroup FLASH_Group3 Option Bytes Programming functions
424 * @brief Option Bytes Programming functions
425 *
426 @verbatim
427 ===============================================================================
428 ##### Option Bytes Programming functions #####
429 ===============================================================================
430 [..] This group includes the following functions:
431 (+) void FLASH_OB_Unlock(void);
432 (+) void FLASH_OB_Lock(void);
433 (+) void FLASH_OB_Erase(void);
434 (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
435 (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
436 (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
437 (+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
438 (+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
439 (+) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity);
440 (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
441 (+) FLASH_Status FLASH_OB_Launch(void);
442 (+) uint32_t FLASH_OB_GetUser(void);
443 (+) uint8_t FLASH_OB_GetWRP(void);
444 (+) uint8_t FLASH_OB_GetRDP(void);
445 [..] Any operation of erase or program should follow these steps:
446 (#) Call the FLASH_OB_Unlock() function to enable the FLASH option control
447 register access.
448 (#) Call one or several functions to program the desired Option Bytes:
449 (++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
450 => to Enable/Disable the desired sector write protection.
451 (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the
452 desired read Protection Level.
453 (++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
454 => to configure the user Option Bytes.
455 (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
456 => to set the boot1 mode
457 (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
458 => to Enable/Disable the VDDA monitoring.
459 (++) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity);
460 => to Enable/Disable the SRAM Parity check.
461 (++) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
462 => to write all user option bytes: OB_IWDG, OB_STOP, OB_STDBY,
463 OB_BOOT1, OB_VDDA_ANALOG and OB_VDD_SD12.
464 (#) Once all needed Option Bytes to be programmed are correctly written,
465 call the FLASH_OB_Launch() function to launch the Option Bytes
466 programming process.
467 (#@) When changing the IWDG mode from HW to SW or from SW to HW, a system
468 reset is needed to make the change effective.
469 (#) Call the FLASH_OB_Lock() function to disable the FLASH option control
470 register access (recommended to protect the Option Bytes against
471 possible unwanted operations).
472
473 @endverbatim
474 * @{
475 */
476
477 /**
478 * @brief Unlocks the option bytes block access.
479 * @param None
480 * @retval None
481 */
FLASH_OB_Unlock(void)482 void FLASH_OB_Unlock(void)
483 {
484 if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
485 {
486 /* Unlocking the option bytes block access */
487 FLASH->OPTKEYR = FLASH_OPTKEY1;
488 FLASH->OPTKEYR = FLASH_OPTKEY2;
489 }
490 }
491
492 /**
493 * @brief Locks the option bytes block access.
494 * @param None
495 * @retval None
496 */
FLASH_OB_Lock(void)497 void FLASH_OB_Lock(void)
498 {
499 /* Set the OPTWREN Bit to lock the option bytes block access */
500 FLASH->CR &= ~FLASH_CR_OPTWRE;
501 }
502
503 /**
504 * @brief Launch the option byte loading.
505 * @param None
506 * @retval None
507 */
FLASH_OB_Launch(void)508 void FLASH_OB_Launch(void)
509 {
510 /* Set the OBL_Launch bit to launch the option byte loading */
511 FLASH->CR |= FLASH_CR_OBL_LAUNCH;
512 }
513
514 /**
515 * @brief Erases the FLASH option bytes.
516 * @note This functions erases all option bytes except the Read protection (RDP).
517 * @param None
518 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
519 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
520 */
FLASH_OB_Erase(void)521 FLASH_Status FLASH_OB_Erase(void)
522 {
523 uint16_t rdptmp = OB_RDP_Level_0;
524
525 FLASH_Status status = FLASH_COMPLETE;
526
527 /* Get the actual read protection Option Byte value */
528 if(FLASH_OB_GetRDP() != RESET)
529 {
530 rdptmp = 0x00;
531 }
532
533 /* Wait for last operation to be completed */
534 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
535
536 if(status == FLASH_COMPLETE)
537 {
538 /* If the previous operation is completed, proceed to erase the option bytes */
539 FLASH->CR |= FLASH_CR_OPTER;
540 FLASH->CR |= FLASH_CR_STRT;
541
542 /* Wait for last operation to be completed */
543 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
544
545 if(status == FLASH_COMPLETE)
546 {
547 /* If the erase operation is completed, disable the OPTER Bit */
548 FLASH->CR &= ~FLASH_CR_OPTER;
549
550 /* Enable the Option Bytes Programming operation */
551 FLASH->CR |= FLASH_CR_OPTPG;
552
553 /* Restore the last read protection Option Byte value */
554 OB->RDP = (uint16_t)rdptmp;
555
556 /* Wait for last operation to be completed */
557 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
558
559 if(status != FLASH_TIMEOUT)
560 {
561 /* if the program operation is completed, disable the OPTPG Bit */
562 FLASH->CR &= ~FLASH_CR_OPTPG;
563 }
564 }
565 else
566 {
567 if (status != FLASH_TIMEOUT)
568 {
569 /* Disable the OPTPG Bit */
570 FLASH->CR &= ~FLASH_CR_OPTPG;
571 }
572 }
573 }
574 /* Return the erase status */
575 return status;
576 }
577
578 /**
579 * @brief Write protects the desired pages
580 * @note To correctly run this function, the FLASH_OB_Unlock() function
581 * must be called before.
582 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
583 * (recommended to protect the FLASH memory against possible unwanted operation)
584 * @param OB_WRP: specifies the address of the pages to be write protected.
585 * This parameter can be:
586 * @arg value between OB_WRP_Pages0to35 and OB_WRP_Pages60to63
587 * @arg OB_WRP_AllPages
588 * @retval FLASH Status: The returned value can be:
589 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
590 */
FLASH_OB_EnableWRP(uint32_t OB_WRP)591 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
592 {
593 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
594
595 FLASH_Status status = FLASH_COMPLETE;
596
597 /* Check the parameters */
598 assert_param(IS_OB_WRP(OB_WRP));
599
600 OB_WRP = (uint32_t)(~OB_WRP);
601 WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
602 WRP1_Data = (uint16_t)((OB_WRP >> 8) & OB_WRP0_WRP0);
603 WRP2_Data = (uint16_t)((OB_WRP >> 16) & OB_WRP0_WRP0) ;
604 WRP3_Data = (uint16_t)((OB_WRP >> 24) & OB_WRP0_WRP0) ;
605
606 /* Wait for last operation to be completed */
607 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
608
609 if(status == FLASH_COMPLETE)
610 {
611 FLASH->CR |= FLASH_CR_OPTPG;
612
613 if(WRP0_Data != 0xFF)
614 {
615 OB->WRP0 = WRP0_Data;
616
617 /* Wait for last operation to be completed */
618 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
619 }
620 if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
621 {
622 OB->WRP1 = WRP1_Data;
623
624 /* Wait for last operation to be completed */
625 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
626 }
627 if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
628 {
629 OB->WRP2 = WRP2_Data;
630
631 /* Wait for last operation to be completed */
632 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
633 }
634 if((status == FLASH_COMPLETE) && (WRP3_Data != 0xFF))
635 {
636 OB->WRP3 = WRP3_Data;
637
638 /* Wait for last operation to be completed */
639 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
640 }
641 if(status != FLASH_TIMEOUT)
642 {
643 /* if the program operation is completed, disable the OPTPG Bit */
644 FLASH->CR &= ~FLASH_CR_OPTPG;
645 }
646 }
647 /* Return the write protection operation Status */
648 return status;
649 }
650
651 /**
652 * @brief Enables or disables the read out protection.
653 * @note To correctly run this function, the FLASH_OB_Unlock() function
654 * must be called before.
655 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
656 * (recommended to protect the FLASH memory against possible unwanted operation)
657 * @param FLASH_ReadProtection_Level: specifies the read protection level.
658 * This parameter can be:
659 * @arg OB_RDP_Level_0: No protection
660 * @arg OB_RDP_Level_1: Read protection of the memory
661 * @arg OB_RDP_Level_2: Chip protection
662 * @retval FLASH Status: The returned value can be:
663 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
664 */
FLASH_OB_RDPConfig(uint8_t OB_RDP)665 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
666 {
667 FLASH_Status status = FLASH_COMPLETE;
668
669 /* Check the parameters */
670 assert_param(IS_OB_RDP(OB_RDP));
671 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
672
673 if(status == FLASH_COMPLETE)
674 {
675 FLASH->CR |= FLASH_CR_OPTER;
676 FLASH->CR |= FLASH_CR_STRT;
677
678 /* Wait for last operation to be completed */
679 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
680
681 if(status == FLASH_COMPLETE)
682 {
683 /* If the erase operation is completed, disable the OPTER Bit */
684 FLASH->CR &= ~FLASH_CR_OPTER;
685
686 /* Enable the Option Bytes Programming operation */
687 FLASH->CR |= FLASH_CR_OPTPG;
688
689 OB->RDP = OB_RDP;
690
691 /* Wait for last operation to be completed */
692 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
693
694 if(status != FLASH_TIMEOUT)
695 {
696 /* if the program operation is completed, disable the OPTPG Bit */
697 FLASH->CR &= ~FLASH_CR_OPTPG;
698 }
699 }
700 else
701 {
702 if(status != FLASH_TIMEOUT)
703 {
704 /* Disable the OPTER Bit */
705 FLASH->CR &= ~FLASH_CR_OPTER;
706 }
707 }
708 }
709 /* Return the protection operation Status */
710 return status;
711 }
712
713 /**
714 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
715 * @param OB_IWDG: Selects the IWDG mode
716 * This parameter can be one of the following values:
717 * @arg OB_IWDG_SW: Software IWDG selected
718 * @arg OB_IWDG_HW: Hardware IWDG selected
719 * @param OB_STOP: Reset event when entering STOP mode.
720 * This parameter can be one of the following values:
721 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
722 * @arg OB_STOP_RST: Reset generated when entering in STOP
723 * @param OB_STDBY: Reset event when entering Standby mode.
724 * This parameter can be one of the following values:
725 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
726 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
727 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
728 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
729 */
FLASH_OB_UserConfig(uint8_t OB_IWDG,uint8_t OB_STOP,uint8_t OB_STDBY)730 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
731 {
732 FLASH_Status status = FLASH_COMPLETE;
733
734 /* Check the parameters */
735 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
736 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
737 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
738
739 /* Authorize the small information block programming */
740 FLASH->OPTKEYR = FLASH_KEY1;
741 FLASH->OPTKEYR = FLASH_KEY2;
742
743 /* Wait for last operation to be completed */
744 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
745
746 if(status == FLASH_COMPLETE)
747 {
748 /* Enable the Option Bytes Programming operation */
749 FLASH->CR |= FLASH_CR_OPTPG;
750
751 OB->USER = (uint8_t)((uint8_t)(OB_IWDG | OB_STOP) | (uint8_t)(OB_STDBY |0xF8));
752
753 /* Wait for last operation to be completed */
754 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
755
756 if(status != FLASH_TIMEOUT)
757 {
758 /* if the program operation is completed, disable the OPTPG Bit */
759 FLASH->CR &= ~FLASH_CR_OPTPG;
760 }
761 }
762 /* Return the Option Byte program Status */
763 return status;
764 }
765
766 /**
767 * @brief Sets or resets the BOOT1.
768 * @param OB_BOOT1: Set or Reset the BOOT1.
769 * This parameter can be one of the following values:
770 * @arg OB_BOOT1_RESET: BOOT1 Reset
771 * @arg OB_BOOT1_SET: BOOT1 Set
772 * @retval None
773 */
FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)774 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
775 {
776 FLASH_Status status = FLASH_COMPLETE;
777
778 /* Check the parameters */
779 assert_param(IS_OB_BOOT1(OB_BOOT1));
780
781 /* Authorize the small information block programming */
782 FLASH->OPTKEYR = FLASH_KEY1;
783 FLASH->OPTKEYR = FLASH_KEY2;
784
785 /* Wait for last operation to be completed */
786 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
787
788 if(status == FLASH_COMPLETE)
789 {
790 /* Enable the Option Bytes Programming operation */
791 FLASH->CR |= FLASH_CR_OPTPG;
792
793 OB->USER = OB_BOOT1|0xEF;
794
795 /* Wait for last operation to be completed */
796 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
797
798 if(status != FLASH_TIMEOUT)
799 {
800 /* if the program operation is completed, disable the OPTPG Bit */
801 FLASH->CR &= ~FLASH_CR_OPTPG;
802 }
803 }
804 /* Return the Option Byte program Status */
805 return status;
806 }
807
808 /**
809 * @brief Sets or resets the analogue monitoring on VDDA Power source.
810 * @param OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
811 * This parameter can be one of the following values:
812 * @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
813 * @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
814 * @retval None
815 */
FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)816 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
817 {
818 FLASH_Status status = FLASH_COMPLETE;
819
820 /* Check the parameters */
821 assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
822
823 /* Authorize the small information block programming */
824 FLASH->OPTKEYR = FLASH_KEY1;
825 FLASH->OPTKEYR = FLASH_KEY2;
826
827 /* Wait for last operation to be completed */
828 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
829
830 if(status == FLASH_COMPLETE)
831 {
832 /* Enable the Option Bytes Programming operation */
833 FLASH->CR |= FLASH_CR_OPTPG;
834
835 OB->USER = OB_VDDA_ANALOG |0xDF;
836
837 /* Wait for last operation to be completed */
838 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
839
840 if(status != FLASH_TIMEOUT)
841 {
842 /* if the program operation is completed, disable the OPTPG Bit */
843 FLASH->CR &= ~FLASH_CR_OPTPG;
844 }
845 }
846 /* Return the Option Byte program Status */
847 return status;
848 }
849
850 /**
851 * @brief Sets or resets the SRAM parity.
852 * @param OB_SRAM_Parity: Set or Reset the SRAM parity enable bit.
853 * This parameter can be one of the following values:
854 * @arg OB_SRAM_PARITY_SET: Set SRAM parity.
855 * @arg OB_SRAM_PARITY_RESET: Reset SRAM parity.
856 * @retval None
857 */
FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)858 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
859 {
860 FLASH_Status status = FLASH_COMPLETE;
861
862 /* Check the parameters */
863 assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
864
865 /* Wait for last operation to be completed */
866 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
867
868 if(status == FLASH_COMPLETE)
869 {
870 /* Enable the Option Bytes Programming operation */
871 FLASH->CR |= FLASH_CR_OPTPG;
872
873 OB->USER = OB_SRAM_Parity | 0xBF;
874
875 /* Wait for last operation to be completed */
876 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
877
878 if(status != FLASH_TIMEOUT)
879 {
880 /* if the program operation is completed, disable the OPTPG Bit */
881 FLASH->CR &= ~FLASH_CR_OPTPG;
882 }
883 }
884 /* Return the Option Byte program Status */
885 return status;
886 }
887
888 /**
889 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY/ BOOT1 and OB_VDDA_ANALOG.
890 * @note To correctly run this function, the FLASH_OB_Unlock() function
891 * must be called before.
892 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
893 * (recommended to protect the FLASH memory against possible unwanted operation)
894 * @param OB_USER: Selects all user option bytes
895 * This parameter is a combination of the following values:
896 * @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
897 * @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
898 * @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
899 * @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
900 * @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
901 * @retval FLASH Status: The returned value can be:
902 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
903 */
FLASH_OB_WriteUser(uint8_t OB_USER)904 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
905 {
906 FLASH_Status status = FLASH_COMPLETE;
907
908 /* Authorize the small information block programming */
909 FLASH->OPTKEYR = FLASH_KEY1;
910 FLASH->OPTKEYR = FLASH_KEY2;
911
912 /* Wait for last operation to be completed */
913 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
914
915 if(status == FLASH_COMPLETE)
916 {
917 /* Enable the Option Bytes Programming operation */
918 FLASH->CR |= FLASH_CR_OPTPG;
919
920 OB->USER = OB_USER | 0x88;
921
922 /* Wait for last operation to be completed */
923 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
924
925 if(status != FLASH_TIMEOUT)
926 {
927 /* if the program operation is completed, disable the OPTPG Bit */
928 FLASH->CR &= ~FLASH_CR_OPTPG;
929 }
930 }
931 /* Return the Option Byte program Status */
932 return status;
933
934 }
935
936 /**
937 * @brief Programs a half word at a specified Option Byte Data address.
938 * @note To correctly run this function, the FLASH_OB_Unlock() function
939 * must be called before.
940 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
941 * (recommended to protect the FLASH memory against possible unwanted operation)
942 * @param Address: specifies the address to be programmed.
943 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
944 * @param Data: specifies the data to be programmed.
945 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
946 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
947 */
FLASH_ProgramOptionByteData(uint32_t Address,uint8_t Data)948 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
949 {
950 FLASH_Status status = FLASH_COMPLETE;
951 /* Check the parameters */
952 assert_param(IS_OB_DATA_ADDRESS(Address));
953 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
954
955 if(status == FLASH_COMPLETE)
956 {
957 /* Enables the Option Bytes Programming operation */
958 FLASH->CR |= FLASH_CR_OPTPG;
959 *(__IO uint16_t*)Address = Data;
960
961 /* Wait for last operation to be completed */
962 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
963
964 if(status != FLASH_TIMEOUT)
965 {
966 /* If the program operation is completed, disable the OPTPG Bit */
967 FLASH->CR &= ~FLASH_CR_OPTPG;
968 }
969 }
970 /* Return the Option Byte Data Program Status */
971 return status;
972 }
973
974 /**
975 * @brief Returns the FLASH User Option Bytes values.
976 * @param None
977 * @retval The FLASH User Option Bytes .
978 */
FLASH_OB_GetUser(void)979 uint8_t FLASH_OB_GetUser(void)
980 {
981 /* Return the User Option Byte */
982 return (uint8_t)(FLASH->OBR >> 8);
983 }
984
985 /**
986 * @brief Returns the FLASH Write Protection Option Bytes value.
987 * @param None
988 * @retval The FLASH Write Protection Option Bytes value
989 */
FLASH_OB_GetWRP(void)990 uint32_t FLASH_OB_GetWRP(void)
991 {
992 /* Return the FLASH write protection Register value */
993 return (uint32_t)(FLASH->WRPR);
994 }
995
996 /**
997 * @brief Checks whether the FLASH Read out Protection Status is set or not.
998 * @param None
999 * @retval FLASH ReadOut Protection Status(SET or RESET)
1000 */
FLASH_OB_GetRDP(void)1001 FlagStatus FLASH_OB_GetRDP(void)
1002 {
1003 FlagStatus readstatus = RESET;
1004
1005 if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
1006 {
1007 readstatus = SET;
1008 }
1009 else
1010 {
1011 readstatus = RESET;
1012 }
1013 return readstatus;
1014 }
1015
1016 /**
1017 * @}
1018 */
1019
1020 /** @defgroup FLASH_Group4 Interrupts and flags management functions
1021 * @brief Interrupts and flags management functions
1022 *
1023 @verbatim
1024 ===============================================================================
1025 ##### Interrupts and flags management functions #####
1026 ===============================================================================
1027
1028 @endverbatim
1029 * @{
1030 */
1031
1032 /**
1033 * @brief Enables or disables the specified FLASH interrupts.
1034 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
1035 * disabled.
1036 * This parameter can be any combination of the following values:
1037 * @arg FLASH_IT_EOP: FLASH end of programming Interrupt
1038 * @arg FLASH_IT_ERR: FLASH Error Interrupt
1039 * @retval None
1040 */
FLASH_ITConfig(uint32_t FLASH_IT,FunctionalState NewState)1041 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
1042 {
1043 /* Check the parameters */
1044 assert_param(IS_FLASH_IT(FLASH_IT));
1045 assert_param(IS_FUNCTIONAL_STATE(NewState));
1046
1047 if(NewState != DISABLE)
1048 {
1049 /* Enable the interrupt sources */
1050 FLASH->CR |= FLASH_IT;
1051 }
1052 else
1053 {
1054 /* Disable the interrupt sources */
1055 FLASH->CR &= ~(uint32_t)FLASH_IT;
1056 }
1057 }
1058
1059 /**
1060 * @brief Checks whether the specified FLASH flag is set or not.
1061 * @param FLASH_FLAG: specifies the FLASH flag to check.
1062 * This parameter can be one of the following values:
1063 * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
1064 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag
1065 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1066 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
1067 * @retval The new state of FLASH_FLAG (SET or RESET).
1068 */
FLASH_GetFlagStatus(uint32_t FLASH_FLAG)1069 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
1070 {
1071 FlagStatus bitstatus = RESET;
1072
1073 /* Check the parameters */
1074 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
1075
1076 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
1077 {
1078 bitstatus = SET;
1079 }
1080 else
1081 {
1082 bitstatus = RESET;
1083 }
1084 /* Return the new state of FLASH_FLAG (SET or RESET) */
1085 return bitstatus;
1086 }
1087
1088 /**
1089 * @brief Clears the FLASH's pending flags.
1090 * @param FLASH_FLAG: specifies the FLASH flags to clear.
1091 * This parameter can be any combination of the following values:
1092 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag
1093 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
1094 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
1095 * @retval None
1096 */
FLASH_ClearFlag(uint32_t FLASH_FLAG)1097 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
1098 {
1099 /* Check the parameters */
1100 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
1101
1102 /* Clear the flags */
1103 FLASH->SR = FLASH_FLAG;
1104 }
1105
1106 /**
1107 * @brief Returns the FLASH Status.
1108 * @param None
1109 * @retval FLASH Status: The returned value can be:
1110 * FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
1111 */
FLASH_GetStatus(void)1112 FLASH_Status FLASH_GetStatus(void)
1113 {
1114 FLASH_Status FLASHstatus = FLASH_COMPLETE;
1115
1116 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
1117 {
1118 FLASHstatus = FLASH_BUSY;
1119 }
1120 else
1121 {
1122 if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
1123 {
1124 FLASHstatus = FLASH_ERROR_WRP;
1125 }
1126 else
1127 {
1128 if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
1129 {
1130 FLASHstatus = FLASH_ERROR_PROGRAM;
1131 }
1132 else
1133 {
1134 FLASHstatus = FLASH_COMPLETE;
1135 }
1136 }
1137 }
1138 /* Return the FLASH Status */
1139 return FLASHstatus;
1140 }
1141
1142 /**
1143 * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
1144 * @param Timeout: FLASH programming Timeout
1145 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
1146 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
1147 */
FLASH_WaitForLastOperation(uint32_t Timeout)1148 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
1149 {
1150 FLASH_Status status = FLASH_COMPLETE;
1151
1152 /* Check for the FLASH Status */
1153 status = FLASH_GetStatus();
1154
1155 /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
1156 while((status == FLASH_BUSY) && (Timeout != 0x00))
1157 {
1158 status = FLASH_GetStatus();
1159 Timeout--;
1160 }
1161
1162 if(Timeout == 0x00 )
1163 {
1164 status = FLASH_TIMEOUT;
1165 }
1166 /* Return the operation status */
1167 return status;
1168 }
1169
1170 /**
1171 * @}
1172 */
1173
1174 /**
1175 * @}
1176 */
1177
1178 /**
1179 * @}
1180 */
1181
1182 /**
1183 * @}
1184 */
1185
1186 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1187