2 ******************************************************************************
\r
3 * @file stm32l1xx_flash.c
\r
4 * @author MCD Application Team
\r
6 * @date 05-March-2012
\r
7 * @brief This file provides all the Flash firmware functions. These functions
\r
8 * can be executed from Internal FLASH or Internal SRAM memories.
\r
9 * The functions that should be called from SRAM are defined inside
\r
10 * the "stm32l1xx_flash_ramfunc.c" file.
\r
11 * This file provides firmware functions to manage the following
\r
12 * functionalities of the FLASH peripheral:
\r
13 * + FLASH Interface configuration
\r
14 * + FLASH Memory Programming
\r
15 * + DATA EEPROM Programming
\r
16 * + Option Bytes Programming
\r
17 * + Interrupts and flags management
\r
21 ==============================================================================
\r
22 ##### How to use this driver #####
\r
23 ==============================================================================
\r
24 [..] This driver provides functions to configure and program the Flash
\r
25 memory of all STM32L1xx devices.
\r
26 [..] These functions are split in 5 groups:
\r
27 (#) FLASH Interface configuration functions: this group includes
\r
28 the management of following features:
\r
29 (++) Set the latency.
\r
30 (++) Enable/Disable the prefetch buffer.
\r
31 (++) Enable/Disable the 64 bit Read Access.
\r
32 (++) Enable/Disable the RUN PowerDown mode.
\r
33 (++) Enable/Disable the SLEEP PowerDown mode.
\r
35 (#) FLASH Memory Programming functions: this group includes all
\r
36 needed functions to erase and program the main memory:
\r
37 (++) Lock and Unlock the Flash interface.
\r
38 (++) Erase function: Erase Page.
\r
39 (++) Program functions: Fast Word and Half Page(should be
\r
40 executed from internal SRAM).
\r
42 (#) DATA EEPROM Programming functions: this group includes all
\r
43 needed functions to erase and program the DATA EEPROM memory:
\r
44 (++) Lock and Unlock the DATA EEPROM interface.
\r
45 (++) Erase function: Erase Byte, erase HalfWord, erase Word, erase
\r
46 (++) Double Word (should be executed from internal SRAM).
\r
47 (++) Program functions: Fast Program Byte, Fast Program Half-Word,
\r
48 FastProgramWord, Program Byte, Program Half-Word,
\r
49 Program Word and Program Double-Word (should be executed
\r
50 from internal SRAM).
\r
52 (#) FLASH Option Bytes Programming functions: this group includes
\r
53 all needed functions to:
\r
54 (++) Lock and Unlock the Flash Option bytes.
\r
55 (++) Set/Reset the write protection.
\r
56 (++) Set the Read protection Level.
\r
57 (++) Set the BOR level.
\r
58 (++) rogram the user option Bytes.
\r
59 (++) Launch the Option Bytes loader.
\r
60 (++) Get the Write protection.
\r
61 (++) Get the read protection status.
\r
62 (++) Get the BOR level.
\r
63 (++) Get the user option bytes.
\r
65 (#) FLASH Interrupts and flag management functions: this group
\r
66 includes all needed functions to:
\r
67 (++) Enable/Disable the flash interrupt sources.
\r
68 (++) Get flags status.
\r
70 (++) Get Flash operation status.
\r
71 (++) Wait for last flash operation.
\r
75 ******************************************************************************
\r
78 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
80 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
81 * You may not use this file except in compliance with the License.
\r
82 * You may obtain a copy of the License at:
\r
84 * http://www.st.com/software_license_agreement_liberty_v2
\r
86 * Unless required by applicable law or agreed to in writing, software
\r
87 * distributed under the License is distributed on an "AS IS" BASIS,
\r
88 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
89 * See the License for the specific language governing permissions and
\r
90 * limitations under the License.
\r
92 ******************************************************************************
\r
95 /* Includes ------------------------------------------------------------------*/
\r
96 #include "stm32l1xx_flash.h"
\r
98 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
102 /** @defgroup FLASH
\r
103 * @brief FLASH driver modules
\r
107 /* Private typedef -----------------------------------------------------------*/
\r
108 /* Private define ------------------------------------------------------------*/
\r
111 #define WRP01_MASK ((uint32_t)0x0000FFFF)
\r
112 #define WRP23_MASK ((uint32_t)0xFFFF0000)
\r
113 #define WRP45_MASK ((uint32_t)0x0000FFFF)
\r
114 #define WRP67_MASK ((uint32_t)0xFFFF0000)
\r
115 #define WRP89_MASK ((uint32_t)0x0000FFFF)
\r
116 #define WRP1011_MASK ((uint32_t)0xFFFF0000)
\r
118 /* Private macro -------------------------------------------------------------*/
\r
119 /* Private variables ---------------------------------------------------------*/
\r
120 /* Private function prototypes -----------------------------------------------*/
\r
121 /* Private functions ---------------------------------------------------------*/
\r
123 /** @defgroup FLASH_Private_Functions
\r
127 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
\r
128 * @brief FLASH Interface configuration functions
\r
131 ==============================================================================
\r
132 ##### FLASH Interface configuration functions #####
\r
133 ==============================================================================
\r
135 [..] FLASH_Interface configuration_Functions, includes the following functions:
\r
136 (+) void FLASH_SetLatency(uint32_t FLASH_Latency):
\r
137 [..] To correctly read data from Flash memory, the number of wait states (LATENCY)
\r
138 must be correctly programmed according to the frequency of the CPU clock
\r
139 (HCLK) and the supply voltage of the device.
\r
141 ----------------------------------------------------------------
\r
142 | Wait states | HCLK clock frequency (MHz) |
\r
143 | |------------------------------------------------|
\r
144 | (Latency) | voltage range | voltage range |
\r
145 | | 1.65 V - 3.6 V | 2.0 V - 3.6 V |
\r
146 | |----------------|---------------|---------------|
\r
147 | | VCORE = 1.2 V | VCORE = 1.5 V | VCORE = 1.8 V |
\r
148 |-------------- |----------------|---------------|---------------|
\r
149 |0WS(1CPU cycle)|0 < HCLK <= 2 |0 < HCLK <= 8 |0 < HCLK <= 16 |
\r
150 |---------------|----------------|---------------|---------------|
\r
151 |1WS(2CPU cycle)|2 < HCLK <= 4 |8 < HCLK <= 16 |16 < HCLK <= 32|
\r
152 ----------------------------------------------------------------
\r
154 (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
\r
155 (+) void FLASH_ReadAccess64Cmd(FunctionalState NewState);
\r
156 (+) void FLASH_RUNPowerDownCmd(FunctionalState NewState);
\r
157 (+) void FLASH_SLEEPPowerDownCmd(FunctionalState NewState);
\r
158 (+) void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
\r
160 Here below the allowed configuration of Latency, 64Bit access and prefetch buffer
\r
162 --------------------------------------------------------------------------------
\r
163 | | ACC64 = 0 | ACC64 = 1 |
\r
164 | Latency |----------------|---------------|---------------|---------------|
\r
165 | | PRFTEN = 0 | PRFTEN = 1 | PRFTEN = 0 | PRFTEN = 1 |
\r
166 |---------------|----------------|---------------|---------------|---------------|
\r
167 |0WS(1CPU cycle)| YES | NO | YES | YES |
\r
168 |---------------|----------------|---------------|---------------|---------------|
\r
169 |1WS(2CPU cycle)| NO | NO | YES | YES |
\r
170 --------------------------------------------------------------------------------
\r
172 All these functions don't need the unlock sequence.
\r
179 * @brief Sets the code latency value.
\r
180 * @param FLASH_Latency: specifies the FLASH Latency value.
\r
181 * This parameter can be one of the following values:
\r
182 * @arg FLASH_Latency_0: FLASH Zero Latency cycle.
\r
183 * @arg FLASH_Latency_1: FLASH One Latency cycle.
\r
186 void FLASH_SetLatency(uint32_t FLASH_Latency)
\r
188 uint32_t tmpreg = 0;
\r
190 /* Check the parameters */
\r
191 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
\r
193 /* Read the ACR register */
\r
194 tmpreg = FLASH->ACR;
\r
196 /* Sets the Latency value */
\r
197 tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
\r
198 tmpreg |= FLASH_Latency;
\r
200 /* Write the ACR register */
\r
201 FLASH->ACR = tmpreg;
\r
205 * @brief Enables or disables the Prefetch Buffer.
\r
206 * @param NewState: new state of the FLASH prefetch buffer.
\r
207 * This parameter can be: ENABLE or DISABLE.
\r
210 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
\r
212 /* Check the parameters */
\r
213 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
215 if(NewState != DISABLE)
\r
217 FLASH->ACR |= FLASH_ACR_PRFTEN;
\r
221 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTEN));
\r
226 * @brief Enables or disables read access to flash by 64 bits.
\r
227 * @param NewState: new state of the FLASH read access mode.
\r
228 * This parameter can be: ENABLE or DISABLE.
\r
229 * @note If this bit is set, the Read access 64 bit is used.
\r
230 * If this bit is reset, the Read access 32 bit is used.
\r
231 * @note This bit cannot be written at the same time as the LATENCY and
\r
233 * To reset this bit, the LATENCY should be zero wait state and the
\r
237 void FLASH_ReadAccess64Cmd(FunctionalState NewState)
\r
239 /* Check the parameters */
\r
240 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
242 if(NewState != DISABLE)
\r
244 FLASH->ACR |= FLASH_ACR_ACC64;
\r
248 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_ACC64));
\r
253 * @brief Enable or disable the power down mode during Sleep mode.
\r
254 * @note This function is used to power down the FLASH when the system is in SLEEP LP mode.
\r
255 * @param NewState: new state of the power down mode during sleep mode.
\r
256 * This parameter can be: ENABLE or DISABLE.
\r
259 void FLASH_SLEEPPowerDownCmd(FunctionalState NewState)
\r
261 /* Check the parameters */
\r
262 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
264 if (NewState != DISABLE)
\r
266 /* Set the SLEEP_PD bit to put Flash in power down mode during sleep mode */
\r
267 FLASH->ACR |= FLASH_ACR_SLEEP_PD;
\r
271 /* Clear the SLEEP_PD bit in to put Flash in idle mode during sleep mode */
\r
272 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_SLEEP_PD));
\r
280 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
\r
281 * @brief FLASH Memory Programming functions
\r
284 ==============================================================================
\r
285 ##### FLASH Memory Programming functions #####
\r
286 ==============================================================================
\r
288 [..] The FLASH Memory Programming functions, includes the following functions:
\r
289 (+) void FLASH_Unlock(void);
\r
290 (+) void FLASH_Lock(void);
\r
291 (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
\r
292 (+) FLASH_Status FLASH_FastProgramWord(uint32_t Address, uint32_t Data);
\r
294 [..] Any operation of erase or program should follow these steps:
\r
295 (#) Call the FLASH_Unlock() function to enable the flash control register and
\r
296 program memory access.
\r
297 (#) Call the desired function to erase page or program data.
\r
298 (#) Call the FLASH_Lock() to disable the flash program memory access
\r
299 (recommended to protect the FLASH memory against possible unwanted operation).
\r
306 * @brief Unlocks the FLASH control register and program memory access.
\r
310 void FLASH_Unlock(void)
\r
312 if((FLASH->PECR & FLASH_PECR_PRGLOCK) != RESET)
\r
314 /* Unlocking the data memory and FLASH_PECR register access */
\r
315 DATA_EEPROM_Unlock();
\r
317 /* Unlocking the program memory access */
\r
318 FLASH->PRGKEYR = FLASH_PRGKEY1;
\r
319 FLASH->PRGKEYR = FLASH_PRGKEY2;
\r
324 * @brief Locks the Program memory access.
\r
328 void FLASH_Lock(void)
\r
330 /* Set the PRGLOCK Bit to lock the program memory access */
\r
331 FLASH->PECR |= FLASH_PECR_PRGLOCK;
\r
335 * @brief Erases a specified page in program memory.
\r
336 * @note To correctly run this function, the FLASH_Unlock() function
\r
337 * must be called before.
\r
338 * Call the FLASH_Lock() to disable the flash memory access
\r
339 * (recommended to protect the FLASH memory against possible unwanted operation)
\r
340 * @param Page_Address: The page address in program memory to be erased.
\r
341 * @note A Page is erased in the Program memory only if the address to load
\r
342 * is the start address of a page (multiple of 256 bytes).
\r
343 * @retval FLASH Status: The returned value can be:
\r
344 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
346 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
\r
348 FLASH_Status status = FLASH_COMPLETE;
\r
350 /* Check the parameters */
\r
351 assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
\r
353 /* Wait for last operation to be completed */
\r
354 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
356 if(status == FLASH_COMPLETE)
\r
358 /* If the previous operation is completed, proceed to erase the page */
\r
360 /* Set the ERASE bit */
\r
361 FLASH->PECR |= FLASH_PECR_ERASE;
\r
364 FLASH->PECR |= FLASH_PECR_PROG;
\r
366 /* Write 00000000h to the first word of the program page to erase */
\r
367 *(__IO uint32_t *)Page_Address = 0x00000000;
\r
369 /* Wait for last operation to be completed */
\r
370 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
372 /* If the erase operation is completed, disable the ERASE and PROG bits */
\r
373 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
\r
374 FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);
\r
376 /* Return the Erase Status */
\r
381 * @brief Programs a word at a specified address in program memory.
\r
382 * @note To correctly run this function, the FLASH_Unlock() function
\r
383 * must be called before.
\r
384 * Call the FLASH_Lock() to disable the flash memory access
\r
385 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
386 * @param Address: specifies the address to be written.
\r
387 * @param Data: specifies the data to be written.
\r
388 * @retval FLASH Status: The returned value can be:
\r
389 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
391 FLASH_Status FLASH_FastProgramWord(uint32_t Address, uint32_t Data)
\r
393 FLASH_Status status = FLASH_COMPLETE;
\r
395 /* Check the parameters */
\r
396 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
\r
398 /* Wait for last operation to be completed */
\r
399 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
401 if(status == FLASH_COMPLETE)
\r
403 /* If the previous operation is completed, proceed to program the new word */
\r
404 *(__IO uint32_t *)Address = Data;
\r
406 /* Wait for last operation to be completed */
\r
407 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
409 /* Return the Write Status */
\r
417 /** @defgroup FLASH_Group3 DATA EEPROM Programming functions
\r
418 * @brief DATA EEPROM Programming functions
\r
421 ===============================================================================
\r
422 ##### DATA EEPROM Programming functions #####
\r
423 ===============================================================================
\r
425 [..] The DATA_EEPROM Programming_Functions, includes the following functions:
\r
426 (+) void DATA_EEPROM_Unlock(void);
\r
427 (+) void DATA_EEPROM_Lock(void);
\r
428 (+) FLASH_Status DATA_EEPROM_EraseByte(uint32_t Address);
\r
429 (+) FLASH_Status DATA_EEPROM_EraseHalfWord(uint32_t Address);
\r
430 (+) FLASH_Status DATA_EEPROM_EraseWord(uint32_t Address);
\r
431 (+) FLASH_Status DATA_EEPROM_FastProgramByte(uint32_t Address, uint8_t Data);
\r
432 (+) FLASH_Status DATA_EEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);
\r
433 (+) FLASH_Status DATA_EEPROM_FastProgramWord(uint32_t Address, uint32_t Data);
\r
434 (+) FLASH_Status DATA_EEPROM_ProgramByte(uint32_t Address, uint8_t Data);
\r
435 (+) FLASH_Status DATA_EEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);
\r
436 (+) FLASH_Status DATA_EEPROM_ProgramWord(uint32_t Address, uint32_t Data);
\r
438 [..] Any operation of erase or program should follow these steps:
\r
439 (#) Call the DATA_EEPROM_Unlock() function to enable the data EEPROM access
\r
440 and Flash program erase control register access.
\r
441 (#) Call the desired function to erase or program data.
\r
442 (#) Call the DATA_EEPROM_Lock() to disable the data EEPROM access
\r
443 and Flash program erase control register access(recommended
\r
444 to protect the DATA_EEPROM against possible unwanted operation).
\r
451 * @brief Unlocks the data memory and FLASH_PECR register access.
\r
455 void DATA_EEPROM_Unlock(void)
\r
457 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
\r
459 /* Unlocking the Data memory and FLASH_PECR register access*/
\r
460 FLASH->PEKEYR = FLASH_PEKEY1;
\r
461 FLASH->PEKEYR = FLASH_PEKEY2;
\r
466 * @brief Locks the Data memory and FLASH_PECR register access.
\r
470 void DATA_EEPROM_Lock(void)
\r
472 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
\r
473 FLASH->PECR |= FLASH_PECR_PELOCK;
\r
477 * @brief Enables or disables DATA EEPROM fixed Time programming (2*Tprog).
\r
478 * @param NewState: new state of the DATA EEPROM fixed Time programming mode.
\r
479 * This parameter can be: ENABLE or DISABLE.
\r
482 void DATA_EEPROM_FixedTimeProgramCmd(FunctionalState NewState)
\r
484 /* Check the parameters */
\r
485 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
487 if(NewState != DISABLE)
\r
489 FLASH->PECR |= (uint32_t)FLASH_PECR_FTDW;
\r
493 FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
\r
498 * @brief Erase a byte in data memory.
\r
499 * @param Address: specifies the address to be erased.
\r
500 * @note This function can be used only for STM32L1XX_HD and STM32L1XX_MDP
\r
502 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
503 * must be called before.
\r
504 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
505 * and Flash program erase control register access(recommended to protect
\r
506 * the DATA_EEPROM against possible unwanted operation).
\r
507 * @retval FLASH Status: The returned value can be:
\r
508 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
510 FLASH_Status DATA_EEPROM_EraseByte(uint32_t Address)
\r
512 FLASH_Status status = FLASH_COMPLETE;
\r
514 /* Check the parameters */
\r
515 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
517 /* Wait for last operation to be completed */
\r
518 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
520 if(status == FLASH_COMPLETE)
\r
522 /* Write "00h" to valid address in the data memory" */
\r
523 *(__IO uint8_t *) Address = (uint8_t)0x00;
\r
526 /* Return the erase status */
\r
531 * @brief Erase a halfword in data memory.
\r
532 * @param Address: specifies the address to be erased.
\r
533 * @note This function can be used only for STM32L1XX_HD and STM32L1XX_MDP
\r
535 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
536 * must be called before.
\r
537 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
538 * and Flash program erase control register access(recommended to protect
\r
539 * the DATA_EEPROM against possible unwanted operation).
\r
540 * @retval FLASH Status: The returned value can be:
\r
541 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
543 FLASH_Status DATA_EEPROM_EraseHalfWord(uint32_t Address)
\r
545 FLASH_Status status = FLASH_COMPLETE;
\r
547 /* Check the parameters */
\r
548 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
550 /* Wait for last operation to be completed */
\r
551 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
553 if(status == FLASH_COMPLETE)
\r
555 /* Write "0000h" to valid address in the data memory" */
\r
556 *(__IO uint16_t *) Address = (uint16_t)0x0000;
\r
559 /* Return the erase status */
\r
564 * @brief Erase a word in data memory.
\r
565 * @param Address: specifies the address to be erased.
\r
566 * @note For STM32L1XX_MD, A data memory word is erased in the data memory only
\r
567 * if the address to load is the start address of a word (multiple of a word).
\r
568 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
569 * must be called before.
\r
570 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
571 * and Flash program erase control register access(recommended to protect
\r
572 * the DATA_EEPROM against possible unwanted operation).
\r
573 * @retval FLASH Status: The returned value can be:
\r
574 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
576 FLASH_Status DATA_EEPROM_EraseWord(uint32_t Address)
\r
578 FLASH_Status status = FLASH_COMPLETE;
\r
580 /* Check the parameters */
\r
581 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
583 /* Wait for last operation to be completed */
\r
584 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
586 if(status == FLASH_COMPLETE)
\r
588 /* Write "00000000h" to valid address in the data memory" */
\r
589 *(__IO uint32_t *) Address = 0x00000000;
\r
592 /* Return the erase status */
\r
597 * @brief Write a Byte at a specified address in data memory.
\r
598 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
599 * must be called before.
\r
600 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
601 * and Flash program erase control register access(recommended to protect
\r
602 * the DATA_EEPROM against possible unwanted operation).
\r
603 * @param Address: specifies the address to be written.
\r
604 * @param Data: specifies the data to be written.
\r
605 * @note This function assumes that the is data word is already erased.
\r
606 * @retval FLASH Status: The returned value can be:
\r
607 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
609 FLASH_Status DATA_EEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
\r
611 FLASH_Status status = FLASH_COMPLETE;
\r
612 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
613 uint32_t tmp = 0, tmpaddr = 0;
\r
616 /* Check the parameters */
\r
617 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
619 /* Wait for last operation to be completed */
\r
620 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
622 if(status == FLASH_COMPLETE)
\r
624 /* Clear the FTDW bit */
\r
625 FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
\r
627 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
628 if(Data != (uint8_t)0x00)
\r
630 /* If the previous operation is completed, proceed to write the new Data */
\r
631 *(__IO uint8_t *)Address = Data;
\r
633 /* Wait for last operation to be completed */
\r
634 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
638 tmpaddr = Address & 0xFFFFFFFC;
\r
639 tmp = * (__IO uint32_t *) tmpaddr;
\r
640 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
\r
642 status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
\r
643 status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
\r
645 #elif defined (STM32L1XX_HD) || defined (STM32L1XX_MDP)
\r
646 /* If the previous operation is completed, proceed to write the new Data */
\r
647 *(__IO uint8_t *)Address = Data;
\r
649 /* Wait for last operation to be completed */
\r
650 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
653 /* Return the Write Status */
\r
658 * @brief Writes a half word at a specified address in data memory.
\r
659 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
660 * must be called before.
\r
661 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
662 * and Flash program erase control register access(recommended to protect
\r
663 * the DATA_EEPROM against possible unwanted operation).
\r
664 * @param Address: specifies the address to be written.
\r
665 * @param Data: specifies the data to be written.
\r
666 * @note This function assumes that the is data word is already erased.
\r
667 * @retval FLASH Status: The returned value can be:
\r
668 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
670 FLASH_Status DATA_EEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
\r
672 FLASH_Status status = FLASH_COMPLETE;
\r
673 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
674 uint32_t tmp = 0, tmpaddr = 0;
\r
677 /* Check the parameters */
\r
678 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
680 /* Wait for last operation to be completed */
\r
681 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
683 if(status == FLASH_COMPLETE)
\r
685 /* Clear the FTDW bit */
\r
686 FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
\r
688 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
689 if(Data != (uint16_t)0x0000)
\r
691 /* If the previous operation is completed, proceed to write the new data */
\r
692 *(__IO uint16_t *)Address = Data;
\r
694 /* Wait for last operation to be completed */
\r
695 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
699 if((Address & 0x3) != 0x3)
\r
701 tmpaddr = Address & 0xFFFFFFFC;
\r
702 tmp = * (__IO uint32_t *) tmpaddr;
\r
703 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
\r
705 status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
\r
706 status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
\r
710 DATA_EEPROM_FastProgramByte(Address, 0x00);
\r
711 DATA_EEPROM_FastProgramByte(Address + 1, 0x00);
\r
714 #elif defined (STM32L1XX_HD) || defined (STM32L1XX_MDP)
\r
715 /* If the previous operation is completed, proceed to write the new data */
\r
716 *(__IO uint16_t *)Address = Data;
\r
718 /* Wait for last operation to be completed */
\r
719 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
722 /* Return the Write Status */
\r
727 * @brief Programs a word at a specified address in data memory.
\r
728 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
729 * must be called before.
\r
730 * Call the DATA_EEPROM_Lock() to the data EEPROM access
\r
731 * and Flash program erase control register access(recommended to protect
\r
732 * the DATA_EEPROM against possible unwanted operation).
\r
733 * @param Address: specifies the address to be written.
\r
734 * @param Data: specifies the data to be written.
\r
735 * @note This function assumes that the is data word is already erased.
\r
736 * @retval FLASH Status: The returned value can be:
\r
737 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
739 FLASH_Status DATA_EEPROM_FastProgramWord(uint32_t Address, uint32_t Data)
\r
741 FLASH_Status status = FLASH_COMPLETE;
\r
743 /* Check the parameters */
\r
744 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
746 /* Wait for last operation to be completed */
\r
747 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
749 if(status == FLASH_COMPLETE)
\r
751 /* Clear the FTDW bit */
\r
752 FLASH->PECR &= (uint32_t)(~((uint32_t)FLASH_PECR_FTDW));
\r
754 /* If the previous operation is completed, proceed to program the new data */
\r
755 *(__IO uint32_t *)Address = Data;
\r
757 /* Wait for last operation to be completed */
\r
758 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
760 /* Return the Write Status */
\r
765 * @brief Write a Byte at a specified address in data memory without erase.
\r
766 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
767 * must be called before.
\r
768 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
769 * and Flash program erase control register access(recommended to protect
\r
770 * the DATA_EEPROM against possible unwanted operation).
\r
771 * @note The function DATA_EEPROM_FixedTimeProgramCmd() can be called before
\r
772 * this function to configure the Fixed Time Programming.
\r
773 * @param Address: specifies the address to be written.
\r
774 * @param Data: specifies the data to be written.
\r
775 * @retval FLASH Status: The returned value can be:
\r
776 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
778 FLASH_Status DATA_EEPROM_ProgramByte(uint32_t Address, uint8_t Data)
\r
780 FLASH_Status status = FLASH_COMPLETE;
\r
781 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
782 uint32_t tmp = 0, tmpaddr = 0;
\r
785 /* Check the parameters */
\r
786 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
788 /* Wait for last operation to be completed */
\r
789 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
791 if(status == FLASH_COMPLETE)
\r
793 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
794 if(Data != (uint8_t) 0x00)
\r
796 *(__IO uint8_t *)Address = Data;
\r
798 /* Wait for last operation to be completed */
\r
799 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
804 tmpaddr = Address & 0xFFFFFFFC;
\r
805 tmp = * (__IO uint32_t *) tmpaddr;
\r
806 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
\r
808 status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
\r
809 status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
\r
811 #elif defined (STM32L1XX_HD) || defined (STM32L1XX_MDP)
\r
812 *(__IO uint8_t *)Address = Data;
\r
814 /* Wait for last operation to be completed */
\r
815 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
818 /* Return the Write Status */
\r
823 * @brief Writes a half word at a specified address in data memory without erase.
\r
824 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
825 * must be called before.
\r
826 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
827 * and Flash program erase control register access(recommended to protect
\r
828 * the DATA_EEPROM against possible unwanted operation).
\r
829 * @note The function DATA_EEPROM_FixedTimeProgramCmd() can be called before
\r
830 * this function to configure the Fixed Time Programming
\r
831 * @param Address: specifies the address to be written.
\r
832 * @param Data: specifies the data to be written.
\r
833 * @retval FLASH Status: The returned value can be:
\r
834 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
836 FLASH_Status DATA_EEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
\r
838 FLASH_Status status = FLASH_COMPLETE;
\r
839 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
840 uint32_t tmp = 0, tmpaddr = 0;
\r
843 /* Check the parameters */
\r
844 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
846 /* Wait for last operation to be completed */
\r
847 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
849 if(status == FLASH_COMPLETE)
\r
851 #if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
\r
852 if(Data != (uint16_t)0x0000)
\r
854 *(__IO uint16_t *)Address = Data;
\r
856 /* Wait for last operation to be completed */
\r
857 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
861 if((Address & 0x3) != 0x3)
\r
863 tmpaddr = Address & 0xFFFFFFFC;
\r
864 tmp = * (__IO uint32_t *) tmpaddr;
\r
865 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
\r
867 status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
\r
868 status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
\r
872 DATA_EEPROM_FastProgramByte(Address, 0x00);
\r
873 DATA_EEPROM_FastProgramByte(Address + 1, 0x00);
\r
876 #elif defined (STM32L1XX_HD) || defined (STM32L1XX_MDP)
\r
877 *(__IO uint16_t *)Address = Data;
\r
879 /* Wait for last operation to be completed */
\r
880 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
883 /* Return the Write Status */
\r
888 * @brief Programs a word at a specified address in data memory without erase.
\r
889 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
890 * must be called before.
\r
891 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
892 * and Flash program erase control register access(recommended to protect
\r
893 * the DATA_EEPROM against possible unwanted operation).
\r
894 * @note The function DATA_EEPROM_FixedTimeProgramCmd() can be called before
\r
895 * this function to configure the Fixed Time Programming.
\r
896 * @param Address: specifies the address to be written.
\r
897 * @param Data: specifies the data to be written.
\r
898 * @retval FLASH Status: The returned value can be:
\r
899 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
901 FLASH_Status DATA_EEPROM_ProgramWord(uint32_t Address, uint32_t Data)
\r
903 FLASH_Status status = FLASH_COMPLETE;
\r
905 /* Check the parameters */
\r
906 assert_param(IS_FLASH_DATA_ADDRESS(Address));
\r
908 /* Wait for last operation to be completed */
\r
909 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
911 if(status == FLASH_COMPLETE)
\r
913 *(__IO uint32_t *)Address = Data;
\r
915 /* Wait for last operation to be completed */
\r
916 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
918 /* Return the Write Status */
\r
926 /** @defgroup FLASH_Group4 Option Bytes Programming functions
\r
927 * @brief Option Bytes Programming functions
\r
930 ==============================================================================
\r
931 ##### Option Bytes Programming functions #####
\r
932 ==============================================================================
\r
934 [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
\r
935 (+) void FLASH_OB_Unlock(void);
\r
936 (+) void FLASH_OB_Lock(void);
\r
937 (+) void FLASH_OB_Launch(void);
\r
938 (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
\r
939 (+) FLASH_Status FLASH_OB_WRP1Config(uint32_t OB_WRP1, FunctionalState NewState);
\r
940 (+) FLASH_Status FLASH_OB_WRP2Config(uint32_t OB_WRP2, FunctionalState NewState);
\r
941 (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
\r
942 (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
\r
943 (+) FLASH_Status FLASH_OB_BORConfig(uint8_t OB_BOR);
\r
944 (+) uint8_t FLASH_OB_GetUser(void);
\r
945 (+) uint32_t FLASH_OB_GetWRP(void);
\r
946 (+) uint32_t FLASH_OB_GetWRP1(void);
\r
947 (+) uint32_t FLASH_OB_GetWRP2(void);
\r
948 (+) FlagStatus FLASH_OB_GetRDP(void);
\r
949 (+) uint8_t FLASH_OB_GetBOR(void);
\r
950 (+) FLASH_Status FLASH_OB_BootConfig(uint16_t OB_BOOT);
\r
952 [..] Any operation of erase or program should follow these steps:
\r
953 (#) Call the FLASH_OB_Unlock() function to enable the Flash option control
\r
955 (#) Call one or several functions to program the desired option bytes.
\r
956 (++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState) => to Enable/Disable
\r
957 the desired sector write protection.
\r
958 (++) void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level.
\r
959 (++) void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY) => to configure
\r
960 the user option Bytes: IWDG, STOP and the Standby.
\r
961 (++) void FLASH_OB_BORConfig(uint8_t OB_BOR) => to Set the BOR level.
\r
962 (++) FLASH_Status FLASH_ProgramOTP(uint32_t Address, uint32_t Data) => to program the OTP bytes .
\r
963 (#) Once all needed option bytes to be programmed are correctly written, call the
\r
964 FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
\r
965 (#) Call the FLASH_OB_Lock() to disable the Flash option control register access (recommended
\r
966 to protect the option Bytes against possible unwanted operations).
\r
973 * @brief Unlocks the option bytes block access.
\r
977 void FLASH_OB_Unlock(void)
\r
979 if((FLASH->PECR & FLASH_PECR_OPTLOCK) != RESET)
\r
981 /* Unlocking the data memory and FLASH_PECR register access */
\r
982 DATA_EEPROM_Unlock();
\r
984 /* Unlocking the option bytes block access */
\r
985 FLASH->OPTKEYR = FLASH_OPTKEY1;
\r
986 FLASH->OPTKEYR = FLASH_OPTKEY2;
\r
991 * @brief Locks the option bytes block access.
\r
995 void FLASH_OB_Lock(void)
\r
997 /* Set the OPTLOCK Bit to lock the option bytes block access */
\r
998 FLASH->PECR |= FLASH_PECR_OPTLOCK;
\r
1002 * @brief Launch the option byte loading.
\r
1006 void FLASH_OB_Launch(void)
\r
1008 /* Set the OBL_Launch bit to lauch the option byte loading */
\r
1009 FLASH->PECR |= FLASH_PECR_OBL_LAUNCH;
\r
1013 * @brief Write protects the desired pages.
\r
1014 * @note To correctly run this function, the FLASH_OB_Unlock() function
\r
1015 * must be called before.
\r
1016 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1017 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1018 * @param OB_WRP: specifies the address of the pages to be write protected.
\r
1019 * This parameter can be:
\r
1020 * @param value between OB_WRP_Pages0to15 and OB_WRP_Pages496to511
\r
1021 * @param OB_WRP_AllPages
\r
1022 * @param NewState: new state of the specified FLASH Pages Wtite protection.
\r
1023 * This parameter can be: ENABLE or DISABLE.
\r
1024 * @retval FLASH Status: The returned value can be:
\r
1025 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1027 FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
\r
1029 uint32_t WRP01_Data = 0, WRP23_Data = 0;
\r
1031 FLASH_Status status = FLASH_COMPLETE;
\r
1032 uint32_t tmp1 = 0, tmp2 = 0;
\r
1034 /* Check the parameters */
\r
1035 assert_param(IS_OB_WRP(OB_WRP));
\r
1036 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1038 /* Wait for last operation to be completed */
\r
1039 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1041 if(status == FLASH_COMPLETE)
\r
1043 if (NewState != DISABLE)
\r
1045 WRP01_Data = (uint16_t)(((OB_WRP & WRP01_MASK) | OB->WRP01));
\r
1046 WRP23_Data = (uint16_t)((((OB_WRP & WRP23_MASK)>>16 | OB->WRP23)));
\r
1047 tmp1 = (uint32_t)(~(WRP01_Data) << 16)|(WRP01_Data);
\r
1050 tmp2 = (uint32_t)(~(WRP23_Data) << 16)|(WRP23_Data);
\r
1051 OB->WRP23 = tmp2;
\r
1056 WRP01_Data = (uint16_t)(~OB_WRP & (WRP01_MASK & OB->WRP01));
\r
1057 WRP23_Data = (uint16_t)((((~OB_WRP & WRP23_MASK)>>16 & OB->WRP23)));
\r
1059 tmp1 = (uint32_t)((~WRP01_Data) << 16)|(WRP01_Data);
\r
1062 tmp2 = (uint32_t)((~WRP23_Data) << 16)|(WRP23_Data);
\r
1065 /* Wait for last operation to be completed */
\r
1066 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1069 /* Return the write protection operation Status */
\r
1074 * @brief Write protects the desired pages.
\r
1075 * @note This function can be used only for STM32L1XX_HD and STM32L1XX_MDP
\r
1076 * density devices.
\r
1077 * To correctly run this function, the FLASH_OB_Unlock() function
\r
1078 * must be called before.
\r
1079 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1080 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1081 * @param OB_WRP1: specifies the address of the pages to be write protected.
\r
1082 * This parameter can be:
\r
1083 * @arg value between OB_WRP_Pages512to527 and OB_WRP_Pages1008to1023
\r
1084 * @arg OB_WRP_AllPages
\r
1085 * @param NewState: new state of the specified FLASH Pages Wtite protection.
\r
1086 * This parameter can be: ENABLE or DISABLE.
\r
1087 * @retval FLASH Status: The returned value can be:
\r
1088 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1090 FLASH_Status FLASH_OB_WRP1Config(uint32_t OB_WRP1, FunctionalState NewState)
\r
1092 uint32_t WRP45_Data = 0, WRP67_Data = 0;
\r
1094 FLASH_Status status = FLASH_COMPLETE;
\r
1095 uint32_t tmp1 = 0, tmp2 = 0;
\r
1097 /* Check the parameters */
\r
1098 assert_param(IS_OB_WRP(OB_WRP1));
\r
1099 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1101 /* Wait for last operation to be completed */
\r
1102 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1104 if(status == FLASH_COMPLETE)
\r
1106 if (NewState != DISABLE)
\r
1108 WRP45_Data = (uint16_t)(((OB_WRP1 & WRP45_MASK) | OB->WRP45));
\r
1109 WRP67_Data = (uint16_t)((((OB_WRP1 & WRP67_MASK)>>16 | OB->WRP67)));
\r
1110 tmp1 = (uint32_t)(~(WRP45_Data) << 16)|(WRP45_Data);
\r
1113 tmp2 = (uint32_t)(~(WRP67_Data) << 16)|(WRP67_Data);
\r
1114 OB->WRP67 = tmp2;
\r
1119 WRP45_Data = (uint16_t)(~OB_WRP1 & (WRP45_MASK & OB->WRP45));
\r
1120 WRP67_Data = (uint16_t)((((~OB_WRP1 & WRP67_MASK)>>16 & OB->WRP67)));
\r
1122 tmp1 = (uint32_t)((~WRP45_Data) << 16)|(WRP45_Data);
\r
1125 tmp2 = (uint32_t)((~WRP67_Data) << 16)|(WRP67_Data);
\r
1128 /* Wait for last operation to be completed */
\r
1129 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1132 /* Return the write protection operation Status */
\r
1137 * @brief Write protects the desired pages.
\r
1138 * @note This function can be used only for STM32L1XX_HD density devices.
\r
1139 * To correctly run this function, the FLASH_OB_Unlock() function
\r
1140 * must be called before.
\r
1141 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1142 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1143 * @param OB_WRP2: specifies the address of the pages to be write protected.
\r
1144 * This parameter can be:
\r
1145 * @arg value between OB_WRP_Pages1024to1039 and OB_WRP_Pages1520to1535
\r
1146 * @arg OB_WRP_AllPages
\r
1147 * @param NewState: new state of the specified FLASH Pages Wtite protection.
\r
1148 * This parameter can be: ENABLE or DISABLE.
\r
1149 * @retval FLASH Status: The returned value can be:
\r
1150 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1152 FLASH_Status FLASH_OB_WRP2Config(uint32_t OB_WRP2, FunctionalState NewState)
\r
1154 uint32_t WRP89_Data = 0, WRP1011_Data = 0;
\r
1156 FLASH_Status status = FLASH_COMPLETE;
\r
1157 uint32_t tmp1 = 0, tmp2 = 0;
\r
1159 /* Check the parameters */
\r
1160 assert_param(IS_OB_WRP(OB_WRP2));
\r
1161 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1163 /* Wait for last operation to be completed */
\r
1164 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1166 if(status == FLASH_COMPLETE)
\r
1168 if (NewState != DISABLE)
\r
1170 WRP89_Data = (uint16_t)(((OB_WRP2 & WRP89_MASK) | OB->WRP89));
\r
1171 WRP1011_Data = (uint16_t)((((OB_WRP2 & WRP1011_MASK)>>16 | OB->WRP1011)));
\r
1172 tmp1 = (uint32_t)(~(WRP89_Data) << 16)|(WRP89_Data);
\r
1175 tmp2 = (uint32_t)(~(WRP1011_Data) << 16)|(WRP1011_Data);
\r
1176 OB->WRP1011 = tmp2;
\r
1181 WRP89_Data = (uint16_t)(~OB_WRP2 & (WRP89_MASK & OB->WRP89));
\r
1182 WRP1011_Data = (uint16_t)((((~OB_WRP2 & WRP1011_MASK)>>16 & OB->WRP1011)));
\r
1184 tmp1 = (uint32_t)((~WRP89_Data) << 16)|(WRP89_Data);
\r
1187 tmp2 = (uint32_t)((~WRP1011_Data) << 16)|(WRP1011_Data);
\r
1188 OB->WRP1011 = tmp2;
\r
1190 /* Wait for last operation to be completed */
\r
1191 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1194 /* Return the write protection operation Status */
\r
1199 * @brief Enables or disables the read out protection.
\r
1200 * @note To correctly run this function, the FLASH_OB_Unlock() function
\r
1201 * must be called before.
\r
1202 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1203 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1204 * @param FLASH_ReadProtection_Level: specifies the read protection level.
\r
1205 * This parameter can be:
\r
1206 * @arg OB_RDP_Level_0: No protection
\r
1207 * @arg OB_RDP_Level_1: Read protection of the memory
\r
1208 * @arg OB_RDP_Level_2: Chip protection
\r
1209 * @retval FLASH Status: The returned value can be:
\r
1210 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1212 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
\r
1214 FLASH_Status status = FLASH_COMPLETE;
\r
1216 uint32_t tmp2 = 0;
\r
1218 /* Check the parameters */
\r
1219 assert_param(IS_OB_RDP(OB_RDP));
\r
1220 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1222 /* calculate the option byte to write */
\r
1223 tmp1 = (uint8_t)(~(OB_RDP ));
\r
1224 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)OB_RDP));
\r
1226 if(status == FLASH_COMPLETE)
\r
1228 /* program read protection level */
\r
1232 /* Wait for last operation to be completed */
\r
1233 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1235 /* Return the Read protection operation Status */
\r
1240 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
\r
1241 * @note To correctly run this function, the FLASH_OB_Unlock() function
\r
1242 * must be called before.
\r
1243 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1244 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1245 * @param OB_IWDG: Selects the WDG mode.
\r
1246 * This parameter can be one of the following values:
\r
1247 * @arg OB_IWDG_SW: Software WDG selected
\r
1248 * @arg OB_IWDG_HW: Hardware WDG selected
\r
1249 * @param OB_STOP: Reset event when entering STOP mode.
\r
1250 * This parameter can be one of the following values:
\r
1251 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
\r
1252 * @arg OB_STOP_RST: Reset generated when entering in STOP
\r
1253 * @param OB_STDBY: Reset event when entering Standby mode.
\r
1254 * This parameter can be one of the following values:
\r
1255 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
\r
1256 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
\r
1257 * @retval FLASH Status: The returned value can be:
\r
1258 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1260 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
\r
1262 FLASH_Status status = FLASH_COMPLETE;
\r
1263 uint32_t tmp = 0, tmp1 = 0;
\r
1265 /* Check the parameters */
\r
1266 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
\r
1267 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
\r
1268 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
\r
1270 /* Get the User Option byte register */
\r
1271 tmp1 = (FLASH->OBR & 0x000F0000) >> 16;
\r
1273 /* Calculate the user option byte to write */
\r
1274 tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << ((uint32_t)0x10));
\r
1275 tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
\r
1277 /* Wait for last operation to be completed */
\r
1278 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1280 if(status == FLASH_COMPLETE)
\r
1282 /* Write the User Option Byte */
\r
1286 /* Wait for last operation to be completed */
\r
1287 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1289 /* Return the Option Byte program Status */
\r
1294 * @brief Programs the FLASH brownout reset threshold level Option Byte.
\r
1295 * @note To correctly run this function, the FLASH_OB_Unlock() function
\r
1296 * must be called before.
\r
1297 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1298 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1299 * @param OB_BOR: Selects the brownout reset threshold level.
\r
1300 * This parameter can be one of the following values:
\r
1301 * @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD
\r
1302 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
\r
1303 * @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
\r
1304 * @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
\r
1305 * @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
\r
1306 * @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
\r
1307 * @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
\r
1308 * @retval FLASH Status: The returned value can be:
\r
1309 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1311 FLASH_Status FLASH_OB_BORConfig(uint8_t OB_BOR)
\r
1313 FLASH_Status status = FLASH_COMPLETE;
\r
1314 uint32_t tmp = 0, tmp1 = 0;
\r
1316 /* Check the parameters */
\r
1317 assert_param(IS_OB_BOR_LEVEL(OB_BOR));
\r
1319 /* Get the User Option byte register */
\r
1320 tmp1 = (FLASH->OBR & 0x00F00000) >> 16;
\r
1322 /* Calculate the option byte to write */
\r
1323 tmp = (uint32_t)~(OB_BOR | tmp1)<<16;
\r
1324 tmp |= (OB_BOR | tmp1);
\r
1326 /* Wait for last operation to be completed */
\r
1327 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1329 if(status == FLASH_COMPLETE)
\r
1331 /* Write the BOR Option Byte */
\r
1335 /* Wait for last operation to be completed */
\r
1336 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1338 /* Return the Option Byte program Status */
\r
1343 * @brief Configures to boot from Bank1 or Bank2.
\r
1344 * @note This function can be used only for STM32L1XX_HD density devices.
\r
1345 * To correctly run this function, the FLASH_OB_Unlock() function
\r
1346 * must be called before.
\r
1347 * Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
\r
1348 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
1349 * @param OB_BOOT: select the FLASH Bank to boot from.
\r
1350 * This parameter can be one of the following values:
\r
1351 * @arg OB_BOOT_BANK2: At startup, if boot pins are set in boot from user Flash
\r
1352 * position and this parameter is selected the device will boot from Bank2 or Bank1,
\r
1353 * depending on the activation of the bank. The active banks are checked in
\r
1354 * the following order: Bank2, followed by Bank1.
\r
1355 * The active bank is recognized by the value programmed at the base address
\r
1356 * of the respective bank (corresponding to the initial stack pointer value
\r
1357 * in the interrupt vector table).
\r
1358 * @arg OB_BOOT_BANK1: At startup, if boot pins are set in boot from user Flash
\r
1359 * position and this parameter is selected the device will boot from Bank1(Default).
\r
1360 * For more information, please refer to AN2606 from www.st.com.
\r
1361 * @retval FLASH Status: The returned value can be:
\r
1362 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1364 FLASH_Status FLASH_OB_BootConfig(uint8_t OB_BOOT)
\r
1366 FLASH_Status status = FLASH_COMPLETE;
\r
1367 uint32_t tmp = 0, tmp1 = 0;
\r
1369 /* Check the parameters */
\r
1370 assert_param(IS_OB_BOOT_BANK(OB_BOOT));
\r
1372 /* Get the User Option byte register */
\r
1373 tmp1 = (FLASH->OBR & 0x007F0000) >> 16;
\r
1375 /* Calculate the option byte to write */
\r
1376 tmp = (uint32_t)~(OB_BOOT | tmp1)<<16;
\r
1377 tmp |= (OB_BOOT | tmp1);
\r
1379 /* Wait for last operation to be completed */
\r
1380 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1382 if(status == FLASH_COMPLETE)
\r
1384 /* Write the BOOT Option Byte */
\r
1388 /* Wait for last operation to be completed */
\r
1389 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
1391 /* Return the Option Byte program Status */
\r
1396 * @brief Returns the FLASH User Option Bytes values.
\r
1398 * @retval The FLASH User Option Bytes.
\r
1400 uint8_t FLASH_OB_GetUser(void)
\r
1402 /* Return the User Option Byte */
\r
1403 return (uint8_t)(FLASH->OBR >> 20);
\r
1407 * @brief Returns the FLASH Write Protection Option Bytes value.
\r
1409 * @retval The FLASH Write Protection Option Bytes value.
\r
1411 uint32_t FLASH_OB_GetWRP(void)
\r
1413 /* Return the FLASH write protection Register value */
\r
1414 return (uint32_t)(FLASH->WRPR);
\r
1418 * @brief Returns the FLASH Write Protection Option Bytes value.
\r
1419 * @note This function can be used only for STM32L1XX_HD and STM32L1XX_MDP
\r
1420 * density devices.
\r
1422 * @retval The FLASH Write Protection Option Bytes value.
\r
1424 uint32_t FLASH_OB_GetWRP1(void)
\r
1426 /* Return the FLASH write protection Register value */
\r
1427 return (uint32_t)(FLASH->WRPR1);
\r
1431 * @brief Returns the FLASH Write Protection Option Bytes value.
\r
1432 * @note This function can be used only for STM32L1XX_HD density devices.
\r
1434 * @retval The FLASH Write Protection Option Bytes value.
\r
1436 uint32_t FLASH_OB_GetWRP2(void)
\r
1438 /* Return the FLASH write protection Register value */
\r
1439 return (uint32_t)(FLASH->WRPR2);
\r
1443 * @brief Checks whether the FLASH Read out Protection Status is set or not.
\r
1445 * @retval FLASH ReadOut Protection Status(SET or RESET).
\r
1447 FlagStatus FLASH_OB_GetRDP(void)
\r
1449 FlagStatus readstatus = RESET;
\r
1451 if ((uint8_t)(FLASH->OBR) != (uint8_t)OB_RDP_Level_0)
\r
1457 readstatus = RESET;
\r
1459 return readstatus;
\r
1463 * @brief Returns the FLASH BOR level.
\r
1465 * @retval The FLASH User Option Bytes.
\r
1467 uint8_t FLASH_OB_GetBOR(void)
\r
1469 /* Return the BOR level */
\r
1470 return (uint8_t)((FLASH->OBR & (uint32_t)0x000F0000) >> 16);
\r
1477 /** @defgroup FLASH_Group5 Interrupts and flags management functions
\r
1478 * @brief Interrupts and flags management functions
\r
1481 ==============================================================================
\r
1482 ##### Interrupts and flags management functions #####
\r
1483 ==============================================================================
\r
1490 * @brief Enables or disables the specified FLASH interrupts.
\r
1491 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
\r
1493 * This parameter can be any combination of the following values:
\r
1494 * @arg FLASH_IT_EOP: FLASH end of programming Interrupt
\r
1495 * @arg FLASH_IT_ERR: FLASH Error Interrupt
\r
1498 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
\r
1500 /* Check the parameters */
\r
1501 assert_param(IS_FLASH_IT(FLASH_IT));
\r
1502 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
1504 if(NewState != DISABLE)
\r
1506 /* Enable the interrupt sources */
\r
1507 FLASH->PECR |= FLASH_IT;
\r
1511 /* Disable the interrupt sources */
\r
1512 FLASH->PECR &= ~(uint32_t)FLASH_IT;
\r
1517 * @brief Checks whether the specified FLASH flag is set or not.
\r
1518 * @param FLASH_FLAG: specifies the FLASH flag to check.
\r
1519 * This parameter can be one of the following values:
\r
1520 * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
\r
1521 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
\r
1522 * @arg FLASH_FLAG_READY: FLASH Ready flag after low power mode
\r
1523 * @arg FLASH_FLAG_ENDHV: FLASH End of high voltage flag
\r
1524 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
\r
1525 * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
\r
1526 * @arg FLASH_FLAG_SIZERR: FLASH size error flag
\r
1527 * @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
\r
1528 * @arg FLASH_FLAG_OPTVERRUSR: FLASH Option User validity error flag
\r
1529 * @retval The new state of FLASH_FLAG (SET or RESET).
\r
1531 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
\r
1533 FlagStatus bitstatus = RESET;
\r
1535 /* Check the parameters */
\r
1536 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
\r
1538 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
\r
1544 bitstatus = RESET;
\r
1546 /* Return the new state of FLASH_FLAG (SET or RESET) */
\r
1547 return bitstatus;
\r
1551 * @brief Clears the FLASH's pending flags.
\r
1552 * @param FLASH_FLAG: specifies the FLASH flags to clear.
\r
1553 * This parameter can be any combination of the following values:
\r
1554 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
\r
1555 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
\r
1556 * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
\r
1557 * @arg FLASH_FLAG_SIZERR: FLASH size error flag
\r
1558 * @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
\r
1559 * @arg FLASH_FLAG_OPTVERRUSR: FLASH Option User validity error flag
\r
1562 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
\r
1564 /* Check the parameters */
\r
1565 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
\r
1567 /* Clear the flags */
\r
1568 FLASH->SR = FLASH_FLAG;
\r
1572 * @brief Returns the FLASH Status.
\r
1574 * @retval FLASH Status: The returned value can be:
\r
1575 * FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
\r
1577 FLASH_Status FLASH_GetStatus(void)
\r
1579 FLASH_Status FLASHstatus = FLASH_COMPLETE;
\r
1581 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
\r
1583 FLASHstatus = FLASH_BUSY;
\r
1587 if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
\r
1589 FLASHstatus = FLASH_ERROR_WRP;
\r
1593 if((FLASH->SR & (uint32_t)0x1E00) != (uint32_t)0x00)
\r
1595 FLASHstatus = FLASH_ERROR_PROGRAM;
\r
1599 FLASHstatus = FLASH_COMPLETE;
\r
1603 /* Return the FLASH Status */
\r
1604 return FLASHstatus;
\r
1609 * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
\r
1610 * @param Timeout: FLASH programming Timeout.
\r
1611 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
\r
1612 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
1614 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
\r
1616 __IO FLASH_Status status = FLASH_COMPLETE;
\r
1618 /* Check for the FLASH Status */
\r
1619 status = FLASH_GetStatus();
\r
1621 /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
\r
1622 while((status == FLASH_BUSY) && (Timeout != 0x00))
\r
1624 status = FLASH_GetStatus();
\r
1628 if(Timeout == 0x00 )
\r
1630 status = FLASH_TIMEOUT;
\r
1632 /* Return the operation status */
\r
1652 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r