]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_flash_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_flash_ex.c
1 /**
2 ******************************************************************************
3 * @file stm32l0xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 06-February-2015
7 * @brief FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the internal FLASH memory:
10 * + FLASH Interface configuration
11 * + FLASH Memory Erasing
12 * + DATA EEPROM Programming/Erasing
13 * + Option Bytes Programming
14 * + Interrupts management
15 *
16 * @verbatim
17 ==============================================================================
18 ##### Flash peripheral Extended features #####
19 ==============================================================================
20
21 [..] Comparing to other products, the FLASH interface for STM32L0xx
22 devices contains the following additional features
23 (+) Erase functions
24 (+) DATA_EEPROM memory management
25 (+) BOOT option bit configuration
26 (+) PCROP protection for all sectors
27
28 ##### How to use this driver #####
29 ==============================================================================
30 [..] This driver provides functions to configure and program the FLASH memory
31 of all STM32L0xx. It includes:
32 (+) Full DATA_EEPROM erase and program management
33 (+) Boot activation
34 (+) PCROP protection configuration and control for all sectors
35
36 @endverbatim
37 ******************************************************************************
38 * @attention
39 *
40 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
41 *
42 * Redistribution and use in source and binary forms, with or without modification,
43 * are permitted provided that the following conditions are met:
44 * 1. Redistributions of source code must retain the above copyright notice,
45 * this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright notice,
47 * this list of conditions and the following disclaimer in the documentation
48 * and/or other materials provided with the distribution.
49 * 3. Neither the name of STMicroelectronics nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
60 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 ******************************************************************************
65 */
66
67 /* Includes ------------------------------------------------------------------*/
68 #include "stm32l0xx_hal.h"
69
70 /** @addtogroup STM32L0xx_HAL_Driver
71 * @{
72 */
73
74 /** @addtogroup FLASHEx
75 * @brief FLASH HAL Extension module driver
76 * @{
77 */
78
79 #ifdef HAL_FLASH_MODULE_ENABLED
80
81 /* Private typedef -----------------------------------------------------------*/
82 /* Private define ------------------------------------------------------------*/
83 /* Private macro -------------------------------------------------------------*/
84 /* Private variables ---------------------------------------------------------*/
85 /* Private function prototypes -----------------------------------------------*/
86 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
87 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
88 static uint8_t FLASH_OB_GetUser(void);
89 static uint8_t FLASH_OB_GetRDP(void);
90 static uint8_t FLASH_OB_GetBOR(void);
91 static uint8_t FLASH_OB_GetBOOTBit1(void);
92 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
93 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t Sector2, uint32_t NewState);
94 #else
95 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t NewState);
96 #endif
97 static HAL_StatusTypeDef FLASH_OB_PCROPSelectionConfig(uint32_t WPRMOD);
98 static uint32_t FLASH_OB_GetWRP(void);
99 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
100 static uint32_t FLASH_OB_GetWRP2(void);
101 #endif
102 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
103 static HAL_StatusTypeDef FLASH_OB_BOOTBit1Config(uint8_t OB_BOOT_Bit1);
104 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
105 static HAL_StatusTypeDef FLASH_OB_BFB2Config(uint8_t OB_BFB2);
106 #endif
107
108 /* Exported functions ---------------------------------------------------------*/
109
110 /** @addtogroup FLASHEx_Exported_Functions
111 * @{
112 */
113
114 /** @addtogroup FLASHEx_Exported_Functions_Group1
115 * @brief FLASH Memory Erasing functions
116 *
117 @verbatim
118 ==============================================================================
119 ##### FLASH Erasing Programming functions #####
120 ==============================================================================
121
122 [..] The FLASH Memory Erasing functions, includes the following functions:
123 (+) HAL_FLASHEx_Erase: return only when erase has been done
124 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback is called with parameter
125 0xFFFFFFFF
126
127 [..] Any operation of erase should follow these steps:
128 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
129 program memory access.
130 (#) Call the desired function to erase page.
131 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
132 (recommended to protect the FLASH memory against possible unwanted operation).
133
134 @endverbatim
135 * @{
136 */
137
138 /**
139 * @brief Erase the specified FLASH memory Pages
140 * @note To correctly run this function, the HAL_FLASH_Unlock() function
141 * must be called before.
142 * Call the HAL_FLASH_Lock() to disable the flash memory access
143 * (recommended to protect the FLASH memory against possible unwanted operation)
144 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
145 * contains the configuration information for the erasing.
146 *
147 * @param[out] PageError: pointer to variable that
148 * contains the configuration information on faulty sector in case of error
149 * (0xFFFFFFFF means that all the sectors have been correctly erased)
150 *
151 * @retval HAL_StatusTypeDef HAL Status
152 */
153 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
154 {
155 HAL_StatusTypeDef status = HAL_ERROR;
156 uint32_t index = 0;
157
158 /* Process Locked */
159 __HAL_LOCK(&ProcFlash);
160
161 /* Wait for last operation to be completed */
162 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
163
164 if (status == HAL_OK)
165 {
166 /* Clean the error context */
167 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
168
169 /*Initialization of PageError variable*/
170 *PageError = 0xFFFFFFFF;
171
172 /* Check the parameters */
173 assert_param(IS_NBPAGES(pEraseInit->NbPages));
174 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
175 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
176 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
177
178 /* Erase by sector by sector to be done*/
179 for(index = pEraseInit->PageAddress; index < ((pEraseInit->NbPages * FLASH_PAGE_SIZE)+ pEraseInit->PageAddress); index += FLASH_PAGE_SIZE)
180 {
181 FLASH_ErasePage(index);
182
183 /* Wait for last operation to be completed */
184 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
185
186 /* If the erase operation is completed, disable the ERASE Bit */
187 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
188 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
189
190 if (status != HAL_OK)
191 {
192 /* In case of error, stop erase procedure and return the faulty sector*/
193 *PageError = index;
194 break;
195 }
196 }
197 }
198
199 /* Process Unlocked */
200 __HAL_UNLOCK(&ProcFlash);
201
202 return status;
203 }
204
205 /**
206 * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
207 * @note To correctly run this function, the HAL_FLASH_Unlock() function
208 * must be called before.
209 * Call the HAL_FLASH_Lock() to disable the flash memory access
210 * (recommended to protect the FLASH memory against possible unwanted operation).
211 End of erase is done when HAL_FLASH_EndOfOperationCallback is called with parameter
212 0xFFFFFFFF
213 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
214 * contains the configuration information for the erasing.
215 *
216 * @retval HAL_StatusTypeDef HAL Status
217 */
218 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
219 {
220 HAL_StatusTypeDef status = HAL_OK;
221
222 /* Process Locked */
223 __HAL_LOCK(&ProcFlash);
224
225 /* Enable End of FLASH Operation interrupt */
226 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
227
228 /* Enable Error source interrupt */
229 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
230
231 /* Check the parameters */
232 assert_param(IS_NBPAGES(pEraseInit->NbPages));
233 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
234 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
235 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
236
237 /* Clean the error context */
238 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
239 ProcFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
240 ProcFlash.NbPagesToErase = pEraseInit->NbPages;
241 ProcFlash.Page = pEraseInit->PageAddress;
242
243 /* Erase 1st page and wait for IT */
244 FLASH_ErasePage(pEraseInit->PageAddress);
245 return status;
246 }
247
248 /**
249 * @}
250 */
251
252
253 /** @addtogroup FLASHEx_Exported_Functions_Group2
254 * @brief Option Bytes Programming functions
255 *
256 @verbatim
257 ==============================================================================
258 ##### Option Bytes Programming functions #####
259 ==============================================================================
260
261 [..] Any operation of erase or program should follow these steps:
262 (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
263 register access.
264 (#) Call following function to program the desired option bytes.
265 (++) HAL_FLASHEx_OBProgram:
266 - To Enable/Disable the desired sector write protection.
267 - To set the desired read Protection Level.
268 - To configure the user option Bytes: IWDG, STOP and the Standby.
269 - To Set the BOR level.
270 (#) Once all needed option bytes to be programmed are correctly written, call the
271 HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
272 (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
273 to protect the option Bytes against possible unwanted operations).
274
275 [..] Proprietary code Read Out Protection (PcROP):
276 (#) The PcROP sector is selected by using the same option bytes as the Write
277 protection (nWRPi bits). As a result, these 2 options are exclusive each other.
278 (#) In order to activate the PcROP (change the function of the nWRPi option bits),
279 the WPRMOD option bit must be activated.
280 (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
281 means: if WPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
282 is read/write protected.
283 (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
284 (++) HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
285 (++) HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
286 @endverbatim
287 * @{
288 */
289
290 /**
291 * @brief Program option bytes
292 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
293 * contains the configuration information for the programming.
294 *
295 * @retval HAL_StatusTypeDef HAL Status
296 */
297 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
298 {
299 HAL_StatusTypeDef status = HAL_ERROR;
300
301 /* Process Locked */
302 __HAL_LOCK(&ProcFlash);
303
304 /* Check the parameters */
305 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
306
307 /* Write protection configuration */
308 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
309 {
310 assert_param(IS_WRPSTATE(pOBInit->WRPState));
311 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
312 status = FLASH_OB_ProtectedSectorsConfig(pOBInit->WRPSector, pOBInit->WRPSector2, pOBInit->WRPState);
313 #else
314 status = FLASH_OB_ProtectedSectorsConfig(pOBInit->WRPSector, pOBInit->WRPState);
315 #endif
316 if (status != HAL_OK)
317 {
318 /* Process Unlocked */
319 __HAL_UNLOCK(&ProcFlash);
320 return status;
321 }
322 }
323
324 /* Read protection configuration */
325 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
326 {
327 status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
328 if (status != HAL_OK)
329 {
330 /* Process Unlocked */
331 __HAL_UNLOCK(&ProcFlash);
332 return status;
333 }
334 }
335
336 /* USER configuration */
337 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
338 {
339 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
340 pOBInit->USERConfig & OB_STOP_NORST,
341 pOBInit->USERConfig & OB_STDBY_NORST);
342 if (status != HAL_OK)
343 {
344 /* Process Unlocked */
345 __HAL_UNLOCK(&ProcFlash);
346 return status;
347 }
348 }
349
350 /* BOR Level configuration */
351 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
352 {
353 status = FLASH_OB_BORConfig(pOBInit->BORLevel);
354 }
355
356 /* Program BOOT Bit1 config option byte */
357 if ((pOBInit->OptionType & OPTIONBYTE_BOOT_BIT1) == OPTIONBYTE_BOOT_BIT1)
358 {
359 status = FLASH_OB_BOOTBit1Config(pOBInit->BOOTBit1Config);
360 }
361
362 /* Process Unlocked */
363 __HAL_UNLOCK(&ProcFlash);
364 return status;
365 }
366
367 /**
368 * @brief Get the Option byte configuration
369 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
370 * contains the configuration information for the programming.
371 *
372 * @retval None
373 */
374 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
375 {
376 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
377
378 /* Get WRP sector */
379 pOBInit->WRPSector = FLASH_OB_GetWRP();
380
381 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
382 pOBInit->WRPSector2 = FLASH_OB_GetWRP2();
383 #endif
384
385 /* Get RDP Level */
386 pOBInit->RDPLevel = FLASH_OB_GetRDP();
387
388 /* Get USER */
389 pOBInit->USERConfig = FLASH_OB_GetUser();
390
391 /* Get BOR Level */
392 pOBInit->BORLevel = FLASH_OB_GetBOR();
393
394 /* Get BOOT bit 1 config OB */
395 pOBInit->BOOTBit1Config = FLASH_OB_GetBOOTBit1();
396
397 }
398
399 /**
400 * @brief Program option bytes
401 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
402 * contains the configuration information for the programming.
403 *
404 * @retval HAL_StatusTypeDef HAL Status
405 */
406 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
407 {
408 HAL_StatusTypeDef status = HAL_ERROR;
409
410 /* Check the parameters */
411 assert_param(IS_OBEX(pAdvOBInit->OptionType));
412
413 /* Program PCROP option byte */
414 if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
415 {
416 /* Check the parameters */
417 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
418 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
419 status = FLASH_OB_ProtectedSectorsConfig(pAdvOBInit->PCROPSector, pAdvOBInit->PCROPSector2, pAdvOBInit->PCROPState);
420 #else
421 status = FLASH_OB_ProtectedSectorsConfig(pAdvOBInit->PCROPSector, pAdvOBInit->PCROPState);
422 #endif
423 }
424 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
425 if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
426 {
427 status = FLASH_OB_BFB2Config(pAdvOBInit->BootConfig);
428 }
429 #endif
430
431 return status;
432 }
433
434 /**
435 * @brief Get the OBEX byte configuration
436 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
437 * contains the configuration information for the programming.
438 *
439 * @retval None
440 */
441 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
442 {
443 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
444 pAdvOBInit->OptionType = OPTIONBYTE_PCROP| OPTIONBYTE_BOOTCONFIG;
445 #else
446 pAdvOBInit->OptionType = OPTIONBYTE_PCROP;
447 #endif
448 /* Get PCROP state */
449 pAdvOBInit->PCROPState = (FLASH->OPTR & FLASH_OPTR_WPRMOD) >> 8;
450 /* Get PCROP protected sector */
451 pAdvOBInit->PCROPSector = FLASH->WRPR;
452
453 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
454 /* Get PCROP protected sector */
455 pAdvOBInit->PCROPSector2 = FLASH->WRPR2;
456
457 /* Get boot bank config */
458 pAdvOBInit->BootConfig = (FLASH->OPTR & FLASH_OPTR_BFB2) >> 23;
459 #endif
460 }
461
462 /**
463 * @brief Select the Protection Mode (WPRMOD).
464 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
465 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
466 * @param None
467 * @retval HAL status
468 */
469 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
470 {
471 return (FLASH_OB_PCROPSelectionConfig(1));
472 }
473
474 /**
475 * @brief Deselect the Protection Mode (WPRMOD).
476 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
477 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
478 * @param None
479 * @retval HAL status
480 */
481 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
482 {
483 return (FLASH_OB_PCROPSelectionConfig(0));
484 }
485
486 /**
487 * @}
488 */
489
490 /** @addtogroup FLASHEx_Exported_Functions_Group3
491 * @brief DATA EEPROM Programming functions
492 *
493 @verbatim
494 ===============================================================================
495 ##### DATA EEPROM Programming functions #####
496 ===============================================================================
497
498 [..] The FLASH_DATAEEPROM Programming_Functions, includes the following functions:
499 (+) HAL_FLASHEx_DATAEEPROM_Unlock(void);
500 (+) HAL_FLASHEx_DATAEEPROM_Lock(void);
501 (+) HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address)
502 (+) HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
503
504 [..] Any operation of erase or program should follow these steps:
505 (#) Call the HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
506 and Flash program erase control register access.
507 (#) Call the desired function to erase or program data.
508 (#) Call the HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
509 and Flash program erase control register access(recommended
510 to protect the DATA_EEPROM against possible unwanted operation).
511
512 @endverbatim
513 * @{
514 */
515 /**
516 * @brief Unlocks the data memory and FLASH_PECR register access.
517 * @param None
518 * @retval HAL_StatusTypeDef HAL Status
519 */
520 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
521 {
522 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
523 {
524 /* Unlocking the Data memory and FLASH_PECR register access */
525 FLASH->PEKEYR = FLASH_PEKEY1;
526 FLASH->PEKEYR = FLASH_PEKEY2;
527 return HAL_OK;
528 }
529 else
530 {
531 return HAL_ERROR;
532 }
533 }
534
535 /**
536 * @brief Locks the Data memory and FLASH_PECR register access.
537 * @param None
538 * @retval HAL_StatusTypeDef HAL Status
539 */
540 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
541 {
542 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
543 SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
544 return HAL_OK;
545 }
546
547 /**
548 * @brief Erase a word in data memory.
549 * @param Address: specifies the address to be erased.
550 * @note To correctly run this function, the HAL_FLASHEx_DATAEEPROM_Unlock() function
551 * must be called before.
552 * Call the HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
553 * and Flash program erase control register access(recommended to protect
554 * the DATA_EEPROM against possible unwanted operation).
555 * @retval HAL status
556 */
557 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address)
558 {
559 HAL_StatusTypeDef status = HAL_OK;
560
561 /* Check the parameters */
562 assert_param(IS_FLASH_DATA_ADDRESS(Address));
563
564 /* Wait for last operation to be completed */
565 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
566
567 if(status == HAL_OK)
568 {
569 /* Clean the error context */
570 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
571
572 /* Write "00000000h" to valid address in the data memory" */
573 *(__IO uint32_t *) Address = 0x00000000;
574 }
575 return status;
576 }
577
578 /**
579 * @brief Program word at a specified address
580 * @param TypeProgram: Indicate the way to program at a specified address.
581 * This parameter can be a value of @ref FLASHEx_Type_Program_Data
582 * @param Address: specifies the address to be programmed.
583 * @param Data: specifies the data to be programmed
584 *
585 * @retval HAL_StatusTypeDef HAL Status
586 */
587
588 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
589 {
590 HAL_StatusTypeDef status = HAL_ERROR;
591
592 /* Process Locked */
593 __HAL_LOCK(&ProcFlash);
594
595 /* Check the parameters */
596 assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
597 assert_param(IS_FLASH_DATA_ADDRESS(Address));
598
599 /* Wait for last operation to be completed */
600 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
601
602 if(status == HAL_OK)
603 {
604 /* Clean the error context */
605 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
606
607 if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
608 {
609 /* Program word (32-bit) at a specified address */
610 *(__IO uint32_t *)Address = Data;
611 }
612 else if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
613 {
614 /* Program word (16-bit) at a specified address */
615 *(__IO uint16_t *)Address = (uint16_t) Data;
616 }
617 else if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
618 {
619 /*Program word (8-bit) at a specified address */
620 *(__IO uint8_t *)Address = (uint8_t) Data;
621 }
622 else
623 {
624 status = HAL_ERROR;
625 }
626
627 /* Wait for last operation to be completed */
628 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
629 }
630 /* Process Unlocked */
631 __HAL_UNLOCK(&ProcFlash);
632 return status;
633 }
634
635 /**
636 * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
637 * @retval None
638 */
639 void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
640 {
641 SET_BIT(FLASH->PECR, FLASH_PECR_FIX);
642 }
643
644 /**
645 * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
646 * @retval None
647 */
648 void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
649 {
650 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FIX);
651 }
652
653 /**
654 * @}
655 */
656
657 /**
658 * @brief Returns the FLASH User Option Bytes values.
659 * @param None
660 * @retval The FLASH User Option Bytes.
661 */
662 static uint8_t FLASH_OB_GetUser(void)
663 {
664 /* Return the User Option Byte */
665 return (uint8_t)((FLASH->OPTR & FLASH_OPTR_USER) >> 16);
666 }
667
668 /**
669 * @brief Returns the FLASH Read out Protection Level.
670 * @param None
671 * @retval FLASH RDP level.
672 */
673 static uint8_t FLASH_OB_GetRDP(void)
674 {
675 return (uint8_t)(FLASH->OPTR & FLASH_OPTR_RDPROT);
676 }
677
678 /**
679 * @brief Returns the FLASH BOR level.
680 * @param None
681 * @retval The BOR level Option Bytes.
682 */
683 static uint8_t FLASH_OB_GetBOR(void)
684 {
685 /* Return the BOR level */
686 return (uint8_t)((FLASH->OPTR & (uint32_t)FLASH_OPTR_BOR_LEV) >> 16);
687 }
688
689 /**
690 * @brief Returns the FLASH BOOT bit1 value.
691 * @param None
692 * @retval The BOOT bit 1 value Option Bytes.
693 */
694 static uint8_t FLASH_OB_GetBOOTBit1(void)
695 {
696 /* Return the BOR level */
697 return (FLASH->OPTR & FLASH_OPTR_BOOT1) >> 31;
698
699 }
700
701 /**
702 * @brief Returns the FLASH Write Protection Option Bytes value.
703 * @param None
704 * @retval The FLASH Write Protection Option Bytes value.
705 */
706 static uint32_t FLASH_OB_GetWRP(void)
707 {
708 /* Return the FLASH write protection Register value */
709 return (uint32_t)(FLASH->WRPR);
710 }
711
712 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
713 /**
714 * @brief Returns the FLASH Write Protection Option Bytes value.
715 * @param None
716 * @retval The FLASH Write Protection Option Bytes value.
717 */
718 static uint32_t FLASH_OB_GetWRP2(void)
719 {
720 /* Return the FLASH write protection Register value */
721 return (uint32_t)(FLASH->WRPR2);
722 }
723 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
724
725 /**
726 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
727 * @param OB_IWDG: Selects the WDG mode.
728 * This parameter can be one of the following values:
729 * @arg OB_IWDG_SW: Software WDG selected
730 * @arg OB_IWDG_HW: Hardware WDG selected
731 * @param OB_STOP: Reset event when entering STOP mode.
732 * This parameter can be one of the following values:
733 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
734 * @arg OB_STOP_RST: Reset generated when entering in STOP
735 * @param OB_STDBY: Reset event when entering Standby mode.
736 * This parameter can be one of the following values:
737 * @arg OB_STDBY_NORST: No reset generated when entering in STANDBY
738 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
739 * @retval HAL status
740 */
741 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
742 {
743 HAL_StatusTypeDef status = HAL_OK;
744 uint32_t tmp = 0, tmp1 = 0, OB_Bits = (uint32_t) (OB_IWDG | OB_STOP | OB_STDBY);
745
746 /* Check the parameters */
747 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
748 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
749 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
750
751 /* Clean the error context */
752 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
753
754 /* Get the User Option byte register */
755 tmp1 = OB->USER & ((~FLASH_OPTR_USER) >> 16);
756
757 /* Calculate the user option byte to write */
758 tmp = (~(OB_Bits | tmp1)) << 16;
759 tmp |= OB_Bits | tmp1;
760
761 /* Wait for last operation to be completed */
762 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
763
764 if(status == HAL_OK)
765 {
766 /* Clean the error context */
767 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
768 /* Program OB */
769 OB->USER = tmp;
770 /* Wait for last operation to be completed */
771 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
772 }
773 return status;
774 }
775
776 /**
777 * @brief Enables or disables the read out protection.
778 * @note To correctly run this function, the FLASH_OB_Unlock() function
779 * must be called before.
780 * @param OB_RDP: specifies the read protection level.
781 * This parameter can be:
782 * @arg OB_RDP_LEVEL0: No protection
783 * @arg OB_RDP_LEVEL1: Read protection of the memory
784 * @arg OB_RDP_LEVEL2: Chip protection
785 *
786 * !!!Warning!!! When enabling OB_RDP_LEVEL2 it's no more possible to go back to level 1 or 0
787 *
788 * @retval HAL status
789 */
790 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
791 {
792 HAL_StatusTypeDef status;
793 uint32_t tmp = 0, tmp1 = 0, OB_Bits = (uint32_t) OB_RDP;
794
795 /* Check the parameters */
796 assert_param(IS_OB_RDP(OB_RDP));
797
798 /* Calculate the option byte to write */
799 tmp = (OB->RDP & ((~FLASH_OPTR_RDPROT) & 0x0000FFFF)) | OB_Bits;
800 tmp1 = (~tmp << 16) | tmp;
801
802 /* Wait for last operation to be completed */
803 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
804
805 if(status == HAL_OK)
806 {
807 /* Clean the error context */
808 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
809 /* Program OB */
810 OB->RDP = tmp1;
811 /* Wait for last operation to be completed */
812 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
813 }
814 return status;
815 }
816
817 /**
818 * @brief Programs the FLASH brownout reset threshold level Option Byte.
819 * @param OB_BOR: Selects the brownout reset threshold level.
820 * This parameter can be one of the following values:
821 * @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD
822 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
823 * @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
824 * @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
825 * @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
826 * @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
827 * @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
828 * @retval HAL status
829 */
830 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
831 {
832 HAL_StatusTypeDef status = HAL_OK;
833 uint32_t tmp = 0, tmp1 = 0, OB_Bits = (uint32_t) OB_BOR;
834
835 /* Check the parameters */
836 assert_param(IS_OB_BOR_LEVEL(OB_BOR));
837
838 /* Get the User Option byte register */
839 tmp1 = OB->USER & ((~FLASH_OPTR_BOR_LEV) >> 16);
840
841 /* Calculate the user option byte to write */
842 tmp = (~(OB_Bits | tmp1)) << 16;
843 tmp |= OB_Bits | tmp1;
844
845 /* Wait for last operation to be completed */
846 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
847
848 if(status == HAL_OK)
849 {
850 /* Clean the error context */
851 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
852 /* Program OB */
853 OB->USER = tmp;
854 /* Wait for last operation to be completed */
855 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
856 }
857
858 return status;
859 }
860
861 /**
862 * @brief Sets or resets the BOOT bit1 option bit.
863 * @param OB_BOOT_BIT1: Set or Reset the BOOT bit1 option bit.
864 * This parameter can be one of the following values:
865 * @arg OB_BOOT_BIT1_RESET: BOOT1 option bit reset
866 * @arg OB_BOOT_BIT1_SET: BOOT1 option bit set
867 * @retval HAL status
868 */
869 static HAL_StatusTypeDef FLASH_OB_BOOTBit1Config(uint8_t OB_BOOT_BIT1)
870 {
871 HAL_StatusTypeDef status = HAL_OK;
872 uint32_t tmp = 0, tmp1 = 0, OB_Bits = ((uint32_t) OB_BOOT_BIT1) << 15;
873
874 /* Check the parameters */
875 assert_param(IS_OB_BOOT1(OB_BOOT_BIT1));
876
877 /* Get the User Option byte register */
878 tmp1 = OB->USER & ((~FLASH_OPTR_BOOT1) >> 16);
879
880 /* Calculate the user option byte to write */
881 tmp = (~(OB_Bits | tmp1)) << 16;
882 tmp |= OB_Bits | tmp1;
883
884 /* Wait for last operation to be completed */
885 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
886
887 if(status == HAL_OK)
888 {
889 /* Clean the error context */
890 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
891 /* Program OB */
892 OB->USER = tmp;
893 /* Wait for last operation to be completed */
894 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
895 }
896
897 return status;
898 }
899
900 /**
901 * @brief Select the Protection Mode (WPRMOD).
902 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
903 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
904 * @param OB_PcROP: Select the Protection Mode of WPR bits.
905 * This parameter can be:
906 * @arg OB_PCROP_SELECTED: nWRP control the read&write protection (PcROP) of respective user sectors.
907 * @arg OB_PCROP_DESELECTED: nWRP control the write protection of respective user sectors.
908 * @retval HAL status
909 */
910 static HAL_StatusTypeDef FLASH_OB_PCROPSelectionConfig(uint32_t WPRMOD)
911 {
912 HAL_StatusTypeDef status;
913 uint32_t tmp = 0, tmp1 = 0, OB_Bits = ((uint32_t) WPRMOD) << 8;
914
915 /* Get the User Option byte register */
916 tmp1 = OB->USER & ((~FLASH_OPTR_WPRMOD) >> 16);
917
918 /* Calculate the user option byte to write */
919 tmp = (~(OB_Bits | tmp1)) << 16;
920 tmp |= OB_Bits | tmp1;
921
922 /* Wait for last operation to be completed */
923 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
924
925 if(status == HAL_OK)
926 {
927 /* Clean the error context */
928 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
929 /* Program OB */
930 OB->RDP = tmp;
931 /* Wait for last operation to be completed */
932 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
933 }
934
935 return status;
936 }
937
938 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
939 /**
940 * @brief Sets or resets the BFB2 option bit.
941 * @param BFB2: Set or Reset the BFB2 option bit.
942 * This parameter can be one of the following values:
943 * @arg OB_BOOT_BANK1: BFB2 option bit reset
944 * @arg OB_BOOT_BANK2: BFB2 option bit set
945 * @retval None
946 */
947 static HAL_StatusTypeDef FLASH_OB_BFB2Config(uint8_t OB_BFB2)
948 {
949 HAL_StatusTypeDef status = HAL_OK;
950 uint32_t tmp = 0, tmp1 = 0, OB_Bits = ((uint32_t) OB_BFB2) << 7;
951
952 /* Check the parameters */
953 assert_param(IS_OB_BOOT_BANK(OB_BFB2));
954
955 /* Get the User Option byte register */
956 tmp1 = OB->USER & ((~FLASH_OPTR_BFB2) >> 16);
957
958 /* Calculate the user option byte to write */
959 tmp = (~(OB_Bits | tmp1)) << 16;
960 tmp |= OB_Bits | tmp1;
961
962 /* Wait for last operation to be completed */
963 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
964
965 if(status == HAL_OK)
966 {
967 /* Clean the error context */
968 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
969
970 /* Program OB */
971 OB->USER = tmp;
972
973 /* Wait for last operation to be completed */
974 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
975 }
976 return status;
977 }
978 #endif
979
980 /**
981 * @brief Write Option Byte of the desired pages of the Flash.
982 * @param Sector: specifies the sectors to be write protected.
983 * @param Sector2: specifies the sectors to be write protected only stm32l07xxx and stm32l08xxx devices
984 * @param NewState: new state of the specified FLASH Pages Wite protection.
985 * This parameter can be: ENABLE or DISABLE.
986 * @retval HAL_StatusTypeDef
987 */
988 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
989 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t Sector2, uint32_t NewState)
990 #else
991 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t NewState)
992 #endif
993 {
994 HAL_StatusTypeDef status = HAL_OK;
995 uint32_t WRP_Data = 0;
996 uint32_t OB_WRP = Sector;
997
998 /* Check the parameters */
999 assert_param(IS_FUNCTIONAL_STATE(NewState));
1000
1001 /* Wait for last operation to be completed */
1002 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1003
1004 if(status == HAL_OK)
1005 {
1006 /* Clean the error context */
1007 ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1008
1009 if (OB_WRP & 0x0000FFFF)
1010 {
1011 if (NewState != OB_WRPSTATE_DISABLE)
1012 {
1013 WRP_Data = (uint16_t)(((OB_WRP & WRP_MASK_LOW) | OB->WRP01));
1014 OB->WRP01 = (uint32_t)(~(WRP_Data) << 16) | (WRP_Data);
1015 }
1016 else
1017 {
1018 WRP_Data = (uint16_t)(~OB_WRP & (WRP_MASK_LOW & OB->WRP01));
1019 OB->WRP01 = (uint32_t)((~WRP_Data) << 16) | (WRP_Data);
1020 }
1021 }
1022 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
1023 if (OB_WRP & 0xFFFF0000)
1024 {
1025 if (NewState != OB_WRPSTATE_DISABLE)
1026 {
1027 WRP_Data = (uint16_t)((((OB_WRP & WRP_MASK_HIGH) >> 16 | OB->WRP23)));
1028 OB->WRP23 = (uint32_t)(~(WRP_Data) << 16) | (WRP_Data);
1029 }
1030 else
1031 {
1032 WRP_Data = (uint16_t)((((~OB_WRP & WRP_MASK_HIGH) >> 16 & OB->WRP23)));
1033 OB->WRP23 = (uint32_t)((~WRP_Data) << 16) | (WRP_Data);
1034 }
1035 }
1036
1037 OB_WRP = Sector2;
1038 if (OB_WRP & 0x0000FFFF)
1039 {
1040 if (NewState != OB_WRPSTATE_DISABLE)
1041 {
1042 WRP_Data = (uint16_t)(((OB_WRP & WRP_MASK_LOW) | OB->WRP45));
1043 OB->WRP45 =(uint32_t)(~(WRP_Data) << 16) | (WRP_Data);
1044 }
1045 else
1046 {
1047 WRP_Data = (uint16_t)(~OB_WRP & (WRP_MASK_LOW & OB->WRP45));
1048 OB->WRP45 = (uint32_t)((~WRP_Data) << 16) | (WRP_Data);
1049 }
1050 }
1051 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
1052 }
1053 /* Wait for last operation to be completed */
1054 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1055
1056 /* Return the write protection operation Status */
1057 return status;
1058 }
1059
1060 /**
1061 * @}
1062 */
1063 #endif /* HAL_FLASH_MODULE_ENABLED */
1064
1065 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum