1 /**
2 ******************************************************************************
3 * @file stm32l0xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @brief Extended FLASH HAL module driver.
6 *
7 * This file provides firmware functions to manage the following
8 * functionalities of the internal FLASH memory:
9 * + FLASH Interface configuration
10 * + FLASH Memory Erasing
11 * + DATA EEPROM Programming/Erasing
12 * + Option Bytes Programming
13 * + Interrupts management
14 *
15 @verbatim
16 ==============================================================================
17 ##### Flash peripheral Extended features #####
18 ==============================================================================
19
20 [..] Comparing to other products, the FLASH interface for STM32L0xx
21 devices contains the following additional features
22 (+) Erase functions
23 (+) DATA_EEPROM memory management
24 (+) BOOT option bit configuration
25 (+) PCROP protection for all sectors
26
27 ##### How to use this driver #####
28 ==============================================================================
29 [..] This driver provides functions to configure and program the FLASH memory
30 of all STM32L0xx. It includes:
31 (+) Full DATA_EEPROM erase and program management
32 (+) Boot activation
33 (+) PCROP protection configuration and control for all pages
34
35 @endverbatim
36 ******************************************************************************
37 * @attention
38 *
39 * Copyright (c) 2016 STMicroelectronics.
40 * All rights reserved.
41 *
42 * This software is licensed under terms that can be found in the LICENSE file in
43 * the root directory of this software component.
44 * If no LICENSE file comes with this software, it is provided AS-IS.
45 ******************************************************************************
46 */
47
48 /* Includes ------------------------------------------------------------------*/
49 #include "stm32l0xx_hal.h"
50
51 /** @addtogroup STM32L0xx_HAL_Driver
52 * @{
53 */
54 #ifdef HAL_FLASH_MODULE_ENABLED
55
56 /** @addtogroup FLASH
57 * @{
58 */
59 /** @addtogroup FLASH_Private_Variables
60 * @{
61 */
62 /**
63 * @}
64 */
65
66 /**
67 * @}
68 */
69
70 /** @defgroup FLASHEx FLASHEx
71 * @brief FLASH HAL Extension module driver
72 * @{
73 */
74
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
78 * @{
79 */
80 /**
81 * @}
82 */
83
84 /* Private macro -------------------------------------------------------------*/
85 /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
86 * @{
87 */
88 /**
89 * @}
90 */
91
92 /* Private variables ---------------------------------------------------------*/
93 /* Private function prototypes -----------------------------------------------*/
94 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
95 * @{
96 */
97 #if defined(FLASH_OPTR_BFB2)
98 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT);
99 #endif /* FLASH_OPTR_BFB2 */
100 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
101 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
102 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
103 static uint8_t FLASH_OB_GetRDP(void);
104 static uint8_t FLASH_OB_GetUser(void);
105 static uint8_t FLASH_OB_GetBOR(void);
106 static uint8_t FLASH_OB_GetBOOTBit1(void);
107 static HAL_StatusTypeDef FLASH_OB_BOOTBit1Config(uint8_t OB_BootBit1);
108 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
109 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t Sector2, uint32_t NewState);
110 #else
111 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t NewState);
112 #endif
113 static uint32_t FLASH_OB_GetWRP(void);
114 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
115 static uint32_t FLASH_OB_GetWRP2(void);
116 #endif
117
118 /**
119 * @}
120 */
121
122 /* Exported functions ---------------------------------------------------------*/
123 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
124 * @{
125 */
126
127 /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
128 * @brief FLASH Memory Erasing functions
129 *
130 @verbatim
131 ==============================================================================
132 ##### FLASH Erasing Programming functions #####
133 ==============================================================================
134
135 [..] The FLASH Memory Erasing functions, includes the following functions:
136 (+) HAL_FLASHEx_Erase: return only when erase has been done
137 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback
138 is called with parameter 0xFFFFFFFF
139
140 [..] Any operation of erase should follow these steps:
141 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
142 program memory access.
143 (#) Call the desired function to erase page.
144 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
145 (recommended to protect the FLASH memory against possible unwanted operation).
146
147 @endverbatim
148 * @{
149 */
150
151 /**
152 * @brief Erase the specified FLASH memory Pages
153 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
154 * must be called before.
155 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
156 * (recommended to protect the FLASH memory against possible unwanted operation)
157 * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
158 * contains the configuration information for the erasing.
159 *
160 * @param[out] PageError pointer to variable that
161 * contains the configuration information on faulty page in case of error
162 * (0xFFFFFFFF means that all the pages have been correctly erased)
163 *
164 * @retval HAL_StatusTypeDef HAL Status
165 */
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef * pEraseInit,uint32_t * PageError)166 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
167 {
168 HAL_StatusTypeDef status = HAL_ERROR;
169 uint32_t address = 0U;
170
171 /* Process Locked */
172 __HAL_LOCK(&pFlash);
173
174 /* Wait for last operation to be completed */
175 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
176
177 if (status == HAL_OK)
178 {
179 /*Initialization of PageError variable*/
180 *PageError = 0xFFFFFFFFU;
181
182 /* Check the parameters */
183 assert_param(IS_NBPAGES(pEraseInit->NbPages));
184 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
185 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
186 assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U));
187
188 /* Erase page by page to be done*/
189 for(address = pEraseInit->PageAddress;
190 address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
191 address += FLASH_PAGE_SIZE)
192 {
193 FLASH_PageErase(address);
194
195 /* Wait for last operation to be completed */
196 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
197
198 /* If the erase operation is completed, disable the ERASE Bit */
199 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
200 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
201
202 if (status != HAL_OK)
203 {
204 /* In case of error, stop erase procedure and return the faulty address */
205 *PageError = address;
206 break;
207 }
208 }
209 }
210
211 /* Process Unlocked */
212 __HAL_UNLOCK(&pFlash);
213
214 return status;
215 }
216
217 /**
218 * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
219 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
220 * must be called before.
221 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
222 * (recommended to protect the FLASH memory against possible unwanted operation)
223 * End of erase is done when @ref HAL_FLASH_EndOfOperationCallback is called with parameter
224 * 0xFFFFFFFF
225 * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
226 * contains the configuration information for the erasing.
227 *
228 * @retval HAL_StatusTypeDef HAL Status
229 */
HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef * pEraseInit)230 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
231 {
232 HAL_StatusTypeDef status = HAL_ERROR;
233
234 /* If procedure already ongoing, reject the next one */
235 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
236 {
237 return HAL_ERROR;
238 }
239
240 /* Check the parameters */
241 assert_param(IS_NBPAGES(pEraseInit->NbPages));
242 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
243 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
244 assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
245
246 /* Process Locked */
247 __HAL_LOCK(&pFlash);
248
249 /* Wait for last operation to be completed */
250 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
251
252 if (status == HAL_OK)
253 {
254 /* Enable End of FLASH Operation and Error source interrupts */
255 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
256
257 pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
258 pFlash.NbPagesToErase = pEraseInit->NbPages;
259 pFlash.Page = pEraseInit->PageAddress;
260
261 /*Erase 1st page and wait for IT*/
262 FLASH_PageErase(pEraseInit->PageAddress);
263 }
264 else
265 {
266 /* Process Unlocked */
267 __HAL_UNLOCK(&pFlash);
268 }
269
270 return status;
271 }
272
273 /**
274 * @}
275 */
276
277 /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
278 * @brief Option Bytes Programming functions
279 *
280 @verbatim
281 ==============================================================================
282 ##### Option Bytes Programming functions #####
283 ==============================================================================
284
285 [..] Any operation of erase or program should follow these steps:
286 (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
287 register access.
288 (#) Call following function to program the desired option bytes.
289 (++) HAL_FLASHEx_OBProgram:
290 - To Enable/Disable the desired sector write protection.
291 - To set the desired read Protection Level.
292 - To configure the user option Bytes: IWDG, STOP and the Standby.
293 - To Set the BOR level.
294 (#) Once all needed option bytes to be programmed are correctly written, call the
295 HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
296 (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
297 to protect the option Bytes against possible unwanted operations).
298
299 [..] Proprietary code Read Out Protection (PcROP):
300 (#) The PcROP sector is selected by using the same option bytes as the Write
301 protection (nWRPi bits). As a result, these 2 options are exclusive each other.
302 (#) In order to activate the PcROP (change the function of the nWRPi option bits),
303 the WPRMOD option bit must be activated.
304 (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
305 means: if WPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
306 is read/write protected.
307 (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
308 (++) HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
309 (++) HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
310
311 @endverbatim
312 * @{
313 */
314
315 /**
316 * @brief Program option bytes
317 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
318 * contains the configuration information for the programming.
319 *
320 * @retval HAL_StatusTypeDef HAL Status
321 */
HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef * pOBInit)322 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
323 {
324 HAL_StatusTypeDef status = HAL_ERROR;
325
326 /* Process Locked */
327 __HAL_LOCK(&pFlash);
328
329 /* Check the parameters */
330 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
331
332 /*Write protection configuration*/
333 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
334 {
335 assert_param(IS_WRPSTATE(pOBInit->WRPState));
336 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
337 status = FLASH_OB_ProtectedSectorsConfig(pOBInit->WRPSector, pOBInit->WRPSector2, pOBInit->WRPState);
338 #else
339 status = FLASH_OB_ProtectedSectorsConfig(pOBInit->WRPSector, pOBInit->WRPState);
340 #endif
341 if (status != HAL_OK)
342 {
343 /* Process Unlocked */
344 __HAL_UNLOCK(&pFlash);
345 return status;
346 }
347 }
348
349 /* Read protection configuration*/
350 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
351 {
352 status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
353 if (status != HAL_OK)
354 {
355 /* Process Unlocked */
356 __HAL_UNLOCK(&pFlash);
357 return status;
358 }
359 }
360
361 /* USER configuration*/
362 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
363 {
364 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
365 pOBInit->USERConfig & OB_STOP_NORST,
366 pOBInit->USERConfig & OB_STDBY_NORST);
367 if (status != HAL_OK)
368 {
369 /* Process Unlocked */
370 __HAL_UNLOCK(&pFlash);
371 return status;
372 }
373 }
374
375 /* BOR Level configuration*/
376 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
377 {
378 status = FLASH_OB_BORConfig(pOBInit->BORLevel);
379 if (status != HAL_OK)
380 {
381 /* Process Unlocked */
382 __HAL_UNLOCK(&pFlash);
383 return status;
384 }
385 }
386
387 /* Program BOOT Bit1 config option byte */
388 if ((pOBInit->OptionType & OPTIONBYTE_BOOT_BIT1) == OPTIONBYTE_BOOT_BIT1)
389 {
390 status = FLASH_OB_BOOTBit1Config(pOBInit->BOOTBit1Config);
391 }
392 /* Process Unlocked */
393 __HAL_UNLOCK(&pFlash);
394
395 return status;
396 }
397
398 /**
399 * @brief Get the Option byte configuration
400 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
401 * contains the configuration information for the programming.
402 *
403 * @retval None
404 */
HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef * pOBInit)405 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
406 {
407 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
408
409 /* Get WRP sector */
410 pOBInit->WRPSector = FLASH_OB_GetWRP();
411
412 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
413 pOBInit->WRPSector2 = FLASH_OB_GetWRP2();
414 #endif
415
416 /*Get RDP Level*/
417 pOBInit->RDPLevel = FLASH_OB_GetRDP();
418
419 /*Get USER*/
420 pOBInit->USERConfig = FLASH_OB_GetUser();
421
422 /*Get BOR Level*/
423 pOBInit->BORLevel = FLASH_OB_GetBOR();
424
425 /* Get BOOT bit 1 config OB */
426 pOBInit->BOOTBit1Config = FLASH_OB_GetBOOTBit1();
427 }
428
429 #if defined(FLASH_OPTR_WPRMOD) || defined(FLASH_OPTR_BFB2)
430
431 /**
432 * @brief Program option bytes
433 * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
434 * contains the configuration information for the programming.
435 *
436 * @retval HAL_StatusTypeDef HAL Status
437 */
HAL_FLASHEx_AdvOBProgram(FLASH_AdvOBProgramInitTypeDef * pAdvOBInit)438 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
439 {
440 HAL_StatusTypeDef status = HAL_ERROR;
441
442 /* Check the parameters */
443 assert_param(IS_OBEX(pAdvOBInit->OptionType));
444
445 #if defined(FLASH_OPTR_WPRMOD)
446
447 /* Program PCROP option byte*/
448 if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
449 {
450 /* Check the parameters */
451 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
452 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
453 status = FLASH_OB_ProtectedSectorsConfig(pAdvOBInit->PCROPSector, pAdvOBInit->PCROPSector2, pAdvOBInit->PCROPState);
454 #else
455 status = FLASH_OB_ProtectedSectorsConfig(pAdvOBInit->PCROPSector, pAdvOBInit->PCROPState);
456 #endif
457 }
458
459 #endif /* FLASH_OPTR_WPRMOD */
460
461 #if defined(FLASH_OPTR_BFB2)
462
463 /* Program BOOT config option byte */
464 if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
465 {
466 status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
467 }
468
469 #endif /* FLASH_OPTR_BFB2 */
470
471 return status;
472 }
473
474 /**
475 * @brief Get the OBEX byte configuration
476 * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
477 * contains the configuration information for the programming.
478 *
479 * @retval None
480 */
HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef * pAdvOBInit)481 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
482 {
483 pAdvOBInit->OptionType = 0;
484
485 #if defined(FLASH_OPTR_WPRMOD)
486
487 pAdvOBInit->OptionType |= OPTIONBYTE_PCROP;
488
489
490 /* Get PCROP state */
491 pAdvOBInit->PCROPState = (FLASH->OPTR & FLASH_OPTR_WPRMOD) >> FLASH_OPTR_WPRMOD_Pos;
492 /* Get PCROP protected sector */
493 pAdvOBInit->PCROPSector = FLASH->WRPR;
494
495 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
496 /* Get PCROP protected sector */
497 pAdvOBInit->PCROPSector2 = FLASH->WRPR2;
498 #endif
499 #endif /* FLASH_OPTR_WPRMOD */
500
501 #if defined(FLASH_OPTR_BFB2)
502
503 pAdvOBInit->OptionType |= OPTIONBYTE_BOOTCONFIG;
504
505 /* Get Boot config OB */
506 pAdvOBInit->BootConfig = (FLASH->OPTR & FLASH_OPTR_BFB2) >> 16U;
507
508 #endif /* FLASH_OPTR_BFB2 */
509 }
510
511 #endif /* FLASH_OPTR_WPRMOD || FLASH_OPTR_BFB2 */
512
513 #if defined(FLASH_OPTR_WPRMOD)
514
515 /**
516 * @brief Select the Protection Mode (WPRMOD).
517 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
518 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
519 * @retval HAL status
520 */
HAL_FLASHEx_OB_SelectPCROP(void)521 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
522 {
523 HAL_StatusTypeDef status = HAL_OK;
524 uint16_t tmp1 = 0;
525 uint32_t tmp2 = 0;
526 uint8_t optiontmp = 0;
527 uint16_t optiontmp2 = 0;
528
529 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
530
531 /* Mask RDP Byte */
532 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
533
534 /* Update Option Byte */
535 optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
536
537 /* calculate the option byte to write */
538 tmp1 = (uint16_t)(~(optiontmp2 ));
539 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
540
541 if(status == HAL_OK)
542 {
543 /* Clean the error context */
544 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
545
546 /* program PCRop */
547 OB->RDP = tmp2;
548
549 /* Wait for last operation to be completed */
550 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
551 }
552
553 /* Return the Read protection operation Status */
554 return status;
555 }
556
557 /**
558 * @brief Deselect the Protection Mode (WPRMOD).
559 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
560 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
561 * @retval HAL status
562 */
HAL_FLASHEx_OB_DeSelectPCROP(void)563 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
564 {
565 HAL_StatusTypeDef status = HAL_OK;
566 uint16_t tmp1 = 0;
567 uint32_t tmp2 = 0;
568 uint8_t optiontmp = 0;
569 uint16_t optiontmp2 = 0;
570
571 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
572
573 /* Mask RDP Byte */
574 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
575
576 /* Update Option Byte */
577 optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
578
579 /* calculate the option byte to write */
580 tmp1 = (uint16_t)(~(optiontmp2 ));
581 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
582
583 if(status == HAL_OK)
584 {
585 /* Clean the error context */
586 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
587
588 /* program PCRop */
589 OB->RDP = tmp2;
590
591 /* Wait for last operation to be completed */
592 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
593 }
594
595 /* Return the Read protection operation Status */
596 return status;
597 }
598
599 #endif /* FLASH_OPTR_WPRMOD */
600
601 /**
602 * @}
603 */
604
605 /** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
606 * @brief DATA EEPROM Programming functions
607 *
608 @verbatim
609 ===============================================================================
610 ##### DATA EEPROM Programming functions #####
611 ===============================================================================
612
613 [..] Any operation of erase or program should follow these steps:
614 (#) Call the HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
615 and Flash program erase control register access.
616 (#) Call the desired function to erase or program data.
617 (#) Call the HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
618 and Flash program erase control register access(recommended
619 to protect the DATA_EEPROM against possible unwanted operation).
620
621 @endverbatim
622 * @{
623 */
624
625 /**
626 * @brief Unlocks the data memory and FLASH_PECR register access.
627 * @retval HAL_StatusTypeDef HAL Status
628 */
HAL_FLASHEx_DATAEEPROM_Unlock(void)629 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
630 {
631 uint32_t primask_bit;
632
633 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
634 {
635 /* Disable interrupts to avoid any interruption during unlock sequence */
636 primask_bit = __get_PRIMASK();
637 __disable_irq();
638
639 /* Unlocking the Data memory and FLASH_PECR register access*/
640 FLASH->PEKEYR = FLASH_PEKEY1;
641 FLASH->PEKEYR = FLASH_PEKEY2;
642
643 /* Re-enable the interrupts: restore previous priority mask */
644 __set_PRIMASK(primask_bit);
645
646 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
647 {
648 return HAL_ERROR;
649 }
650 }
651
652 return HAL_OK;
653 }
654
655 /**
656 * @brief Locks the Data memory and FLASH_PECR register access.
657 * @retval HAL_StatusTypeDef HAL Status
658 */
HAL_FLASHEx_DATAEEPROM_Lock(void)659 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
660 {
661 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
662 SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
663
664 return HAL_OK;
665 }
666
667 /**
668 * @brief Erase a word in data memory.
669 * @param Address specifies the address to be erased.
670 * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
671 * must be called before.
672 * Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
673 * and Flash program erase control register access(recommended to protect
674 * the DATA_EEPROM against possible unwanted operation).
675 * @retval HAL_StatusTypeDef HAL Status
676 */
HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address)677 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address)
678 {
679 HAL_StatusTypeDef status = HAL_OK;
680
681 /* Check the parameters */
682 assert_param(IS_FLASH_DATA_ADDRESS(Address));
683
684 /* Wait for last operation to be completed */
685 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
686
687 if(status == HAL_OK)
688 {
689 /* Clean the error context */
690 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
691
692 /* Write 00000000h to valid address in the data memory */
693 *(__IO uint32_t *) Address = 0x00000000U;
694
695 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
696 }
697
698 /* Return the erase status */
699 return status;
700 }
701
702 /**
703 * @brief Program word at a specified address
704 * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
705 * must be called before.
706 * Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
707 * and Flash program erase control register access(recommended to protect
708 * the DATA_EEPROM against possible unwanted operation).
709 * @note The function @ref HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
710 * this function to configure the Fixed Time Programming.
711 * @param TypeProgram Indicate the way to program at a specified address.
712 * This parameter can be a value of @ref FLASHEx_Type_Program_Data
713 * @param Address specifie the address to be programmed.
714 * @param Data specifie the data to be programmed
715 *
716 * @retval HAL_StatusTypeDef HAL Status
717 */
718
HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram,uint32_t Address,uint32_t Data)719 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
720 {
721 HAL_StatusTypeDef status = HAL_ERROR;
722
723 /* Process Locked */
724 __HAL_LOCK(&pFlash);
725
726 /* Check the parameters */
727 assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
728 assert_param(IS_FLASH_DATA_ADDRESS(Address));
729
730 /* Wait for last operation to be completed */
731 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
732
733 if(status == HAL_OK)
734 {
735 /* Clean the error context */
736 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
737
738 if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
739 {
740 /* Program word (32-bit) at a specified address.*/
741 *(__IO uint32_t *)Address = Data;
742 }
743 else if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
744 {
745 /* Program halfword (16-bit) at a specified address.*/
746 *(__IO uint16_t *)Address = (uint16_t) Data;
747 }
748 else if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
749 {
750 /* Program byte (8-bit) at a specified address.*/
751 *(__IO uint8_t *)Address = (uint8_t) Data;
752 }
753 else
754 {
755 status = HAL_ERROR;
756 }
757
758 if (status == HAL_OK)
759 {
760 /* Wait for last operation to be completed */
761 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
762 }
763 }
764
765 /* Process Unlocked */
766 __HAL_UNLOCK(&pFlash);
767
768 return status;
769 }
770
771 /**
772 * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
773 * @retval None
774 */
HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)775 void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
776 {
777 SET_BIT(FLASH->PECR, FLASH_PECR_FIX);
778 }
779
780 /**
781 * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
782 * @retval None
783 */
HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)784 void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
785 {
786 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FIX);
787 }
788
789 /**
790 * @}
791 */
792
793 /**
794 * @}
795 */
796
797 /** @addtogroup FLASHEx_Private_Functions
798 * @{
799 */
800
801 /*
802 ==============================================================================
803 OPTIONS BYTES
804 ==============================================================================
805 */
806 /**
807 * @brief Enables or disables the read out protection.
808 * @note To correctly run this function, the @ref HAL_FLASH_OB_Unlock() function
809 * must be called before.
810 * @param OB_RDP specifies the read protection level.
811 * This parameter can be:
812 * @arg @ref OB_RDP_LEVEL_0 No protection
813 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
814 * @arg @ref OB_RDP_LEVEL_2 Chip protection
815 *
816 * !!!Warning!!! When enabling OB_RDP_LEVEL_2 it's no more possible to go back to level 1 or 0
817 *
818 * @retval HAL status
819 */
FLASH_OB_RDPConfig(uint8_t OB_RDP)820 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
821 {
822 HAL_StatusTypeDef status = HAL_OK;
823 uint32_t tmp1, tmp2;
824
825 /* Check the parameters */
826 assert_param(IS_OB_RDP(OB_RDP));
827
828 tmp1 = (uint32_t)(OB->RDP & ((~FLASH_OPTR_RDPROT) & 0x0000FFFF));
829
830 /* Calculate the option byte to write */
831 tmp1 |= (uint32_t)(OB_RDP);
832 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(~tmp1) << 16U)) | tmp1);
833
834 /* Wait for last operation to be completed */
835 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
836
837 if(status == HAL_OK)
838 {
839 /* Clean the error context */
840 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
841
842 /* program read protection level */
843 OB->RDP = tmp2;
844
845 /* Wait for last operation to be completed */
846 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
847 }
848
849 /* Return the Read protection operation Status */
850 return status;
851 }
852
853 /**
854 * @brief Programs the FLASH brownout reset threshold level Option Byte.
855 * @param OB_BOR Selects the brownout reset threshold level.
856 * This parameter can be one of the following values:
857 * @arg @ref OB_BOR_OFF BOR is disabled at power down, the reset is asserted when the VDD
858 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
859 * @arg @ref OB_BOR_LEVEL1 BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
860 * @arg @ref OB_BOR_LEVEL2 BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
861 * @arg @ref OB_BOR_LEVEL3 BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
862 * @arg @ref OB_BOR_LEVEL4 BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
863 * @arg @ref OB_BOR_LEVEL5 BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
864 * @retval HAL status
865 */
FLASH_OB_BORConfig(uint8_t OB_BOR)866 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
867 {
868 HAL_StatusTypeDef status = HAL_OK;
869 uint32_t tmp = 0, tmp1 = 0;
870
871 /* Check the parameters */
872 assert_param(IS_OB_BOR_LEVEL(OB_BOR));
873
874 /* Get the User Option byte register */
875 tmp1 = OB->USER & ((~FLASH_OPTR_BOR_LEV) >> 16U);
876
877 /* Calculate the option byte to write - [0xFF | nUSER | 0x00 | USER]*/
878 tmp = (uint32_t)~((OB_BOR | tmp1)) << 16U;
879 tmp |= (OB_BOR | tmp1);
880
881 /* Wait for last operation to be completed */
882 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
883
884 if(status == HAL_OK)
885 {
886 /* Clean the error context */
887 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
888
889 /* Write the BOR Option Byte */
890 OB->USER = tmp;
891
892 /* Wait for last operation to be completed */
893 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
894 }
895
896 /* Return the Option Byte BOR programming Status */
897 return status;
898 }
899
900 /**
901 * @brief Sets or resets the BOOT bit1 option bit.
902 * @param OB_BootBit1 Set or Reset the BOOT bit1 option bit.
903 * This parameter can be one of the following values:
904 * @arg @ref OB_BOOT_BIT1_RESET BOOT1 option bit reset
905 * @arg @ref OB_BOOT_BIT1_SET BOOT1 option bit set
906 * @retval HAL status
907 */
FLASH_OB_BOOTBit1Config(uint8_t OB_BootBit1)908 static HAL_StatusTypeDef FLASH_OB_BOOTBit1Config(uint8_t OB_BootBit1)
909 {
910 HAL_StatusTypeDef status = HAL_OK;
911 uint32_t tmp = 0, tmp1 = 0, OB_Bits = ((uint32_t) OB_BootBit1) << 15;
912
913 /* Check the parameters */
914 assert_param(IS_OB_BOOT1(OB_BootBit1));
915
916 /* Get the User Option byte register */
917 tmp1 = OB->USER & ((~FLASH_OPTR_BOOT1) >> 16U);
918
919 /* Calculate the user option byte to write */
920 tmp = (~(OB_Bits | tmp1)) << 16U;
921 tmp |= OB_Bits | tmp1;
922
923 /* Wait for last operation to be completed */
924 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
925
926 if(status == HAL_OK)
927 {
928 /* Clean the error context */
929 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
930 /* Program OB */
931 OB->USER = tmp;
932 /* Wait for last operation to be completed */
933 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
934 }
935
936 return status;
937 }
938
939 /**
940 * @brief Returns the FLASH User Option Bytes values.
941 * @retval The FLASH User Option Bytes.
942 */
FLASH_OB_GetUser(void)943 static uint8_t FLASH_OB_GetUser(void)
944 {
945 /* Return the User Option Byte */
946 return (uint8_t)((FLASH->OPTR & FLASH_OPTR_USER) >> 16U);
947 }
948
949 /**
950 * @brief Returns the FLASH Read Protection level.
951 * @retval FLASH RDP level
952 * This parameter can be one of the following values:
953 * @arg @ref OB_RDP_LEVEL_0 No protection
954 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
955 * @arg @ref OB_RDP_LEVEL_2 Full chip protection
956 */
FLASH_OB_GetRDP(void)957 static uint8_t FLASH_OB_GetRDP(void)
958 {
959 uint8_t rdp_level = READ_BIT(FLASH->OPTR, FLASH_OPTR_RDPROT);
960
961 if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
962 {
963 return (OB_RDP_LEVEL_1);
964 }
965 else
966 {
967 return rdp_level;
968 }
969 }
970
971 /**
972 * @brief Returns the FLASH BOR level.
973 * @retval The BOR level Option Bytes.
974 */
FLASH_OB_GetBOR(void)975 static uint8_t FLASH_OB_GetBOR(void)
976 {
977 /* Return the BOR level */
978 return (uint8_t)((FLASH->OPTR & (uint32_t)FLASH_OPTR_BOR_LEV) >> 16U);
979 }
980
981 /**
982 * @brief Returns the FLASH BOOT bit1 value.
983 * @retval The BOOT bit 1 value Option Bytes.
984 */
FLASH_OB_GetBOOTBit1(void)985 static uint8_t FLASH_OB_GetBOOTBit1(void)
986 {
987 /* Return the BOR level */
988 return (FLASH->OPTR & FLASH_OPTR_BOOT1) >> FLASH_OPTR_BOOT1_Pos;
989
990 }
991
992 /**
993 * @brief Returns the FLASH Write Protection Option Bytes value.
994 * @retval The FLASH Write Protection Option Bytes value.
995 */
FLASH_OB_GetWRP(void)996 static uint32_t FLASH_OB_GetWRP(void)
997 {
998 /* Return the FLASH write protection Register value */
999 return (uint32_t)(FLASH->WRPR);
1000 }
1001
1002 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
1003 /**
1004 * @brief Returns the FLASH Write Protection Option Bytes value.
1005 * @retval The FLASH Write Protection Option Bytes value.
1006 */
FLASH_OB_GetWRP2(void)1007 static uint32_t FLASH_OB_GetWRP2(void)
1008 {
1009 /* Return the FLASH write protection Register value */
1010 return (uint32_t)(FLASH->WRPR2);
1011 }
1012 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
1013
1014 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
1015 /**
1016 * @brief Write Option Byte of the desired pages of the Flash.
1017 * @param Sector specifies the sectors to be write protected.
1018 * @param Sector2 specifies the sectors to be write protected (only stm32l07xxx and stm32l08xxx devices)
1019 * @param NewState new state of the specified FLASH Pages Write protection.
1020 * This parameter can be:
1021 * @arg @ref OB_WRPSTATE_ENABLE
1022 * @arg @ref OB_WRPSTATE_DISABLE
1023 * @retval HAL_StatusTypeDef
1024 */
FLASH_OB_ProtectedSectorsConfig(uint32_t Sector,uint32_t Sector2,uint32_t NewState)1025 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t Sector2, uint32_t NewState)
1026 #else
1027 /**
1028 * @brief Write Option Byte of the desired pages of the Flash.
1029 * @param Sector specifies the sectors to be write protected.
1030 * @param NewState new state of the specified FLASH Pages Write protection.
1031 * This parameter can be:
1032 * @arg @ref OB_WRPSTATE_ENABLE
1033 * @arg @ref OB_WRPSTATE_DISABLE
1034 * @retval HAL_StatusTypeDef
1035 */
1036 static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t NewState)
1037 #endif
1038 {
1039 HAL_StatusTypeDef status = HAL_OK;
1040 uint32_t WRP_Data = 0;
1041 uint32_t OB_WRP = Sector;
1042
1043 /* Wait for last operation to be completed */
1044 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1045
1046 if(status == HAL_OK)
1047 {
1048 /* Clean the error context */
1049 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1050
1051 /* Update WRP only if at least 1 selected sector */
1052 if (OB_WRP != 0x00000000U)
1053 {
1054 if ((OB_WRP & WRP_MASK_LOW) != 0x00000000U)
1055 {
1056 if (NewState != OB_WRPSTATE_DISABLE)
1057 {
1058 WRP_Data = (uint16_t)(((OB_WRP & WRP_MASK_LOW) | OB->WRP01));
1059 OB->WRP01 = (uint32_t)(~(WRP_Data) << 16U) | (WRP_Data);
1060 }
1061 else
1062 {
1063 WRP_Data = (uint16_t)(~OB_WRP & (WRP_MASK_LOW & OB->WRP01));
1064 OB->WRP01 = (uint32_t)((~WRP_Data) << 16U) | (WRP_Data);
1065 }
1066 }
1067 }
1068 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
1069 /* Update WRP only if at least 1 selected sector */
1070 if (OB_WRP != 0x00000000U)
1071 {
1072 if ((OB_WRP & WRP_MASK_HIGH) != 0x00000000U)
1073 {
1074 if (NewState != OB_WRPSTATE_DISABLE)
1075 {
1076 WRP_Data = (uint16_t)((((OB_WRP & WRP_MASK_HIGH) >> 16U | OB->WRP23)));
1077 OB->WRP23 = (uint32_t)(~(WRP_Data) << 16U) | (WRP_Data);
1078 }
1079 else
1080 {
1081 WRP_Data = (uint16_t)((((~OB_WRP & WRP_MASK_HIGH) >> 16U & OB->WRP23)));
1082 OB->WRP23 = (uint32_t)((~WRP_Data) << 16U) | (WRP_Data);
1083 }
1084 }
1085 }
1086
1087 OB_WRP = Sector2;
1088 /* Update WRP only if at least 1 selected sector */
1089 if (OB_WRP != 0x00000000U)
1090 {
1091 if ((OB_WRP & WRP_MASK_LOW) != 0x00000000U)
1092 {
1093 if (NewState != OB_WRPSTATE_DISABLE)
1094 {
1095 WRP_Data = (uint16_t)(((OB_WRP & WRP_MASK_LOW) | OB->WRP45));
1096 OB->WRP45 =(uint32_t)(~(WRP_Data) << 16U) | (WRP_Data);
1097 }
1098 else
1099 {
1100 WRP_Data = (uint16_t)(~OB_WRP & (WRP_MASK_LOW & OB->WRP45));
1101 OB->WRP45 = (uint32_t)((~WRP_Data) << 16U) | (WRP_Data);
1102 }
1103 }
1104 }
1105 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
1106 }
1107 /* Wait for last operation to be completed */
1108 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1109
1110 /* Return the write protection operation Status */
1111 return status;
1112 }
1113
1114 /**
1115 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
1116 * @param OB_IWDG Selects the WDG mode.
1117 * This parameter can be one of the following values:
1118 * @arg @ref OB_IWDG_SW Software WDG selected
1119 * @arg @ref OB_IWDG_HW Hardware WDG selected
1120 * @param OB_STOP Reset event when entering STOP mode.
1121 * This parameter can be one of the following values:
1122 * @arg @ref OB_STOP_NORST No reset generated when entering in STOP
1123 * @arg @ref OB_STOP_RST Reset generated when entering in STOP
1124 * @param OB_STDBY Reset event when entering Standby mode.
1125 * This parameter can be one of the following values:
1126 * @arg @ref OB_STDBY_NORST No reset generated when entering in STANDBY
1127 * @arg @ref OB_STDBY_RST Reset generated when entering in STANDBY
1128 * @retval HAL status
1129 */
FLASH_OB_UserConfig(uint8_t OB_IWDG,uint8_t OB_STOP,uint8_t OB_STDBY)1130 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
1131 {
1132 HAL_StatusTypeDef status = HAL_OK;
1133 uint32_t tmp = 0, tmp1 = 0;
1134
1135 /* Check the parameters */
1136 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
1137 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
1138 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
1139
1140 /* Get the User Option byte register */
1141 tmp1 = OB->USER & ((~FLASH_OPTR_USER) >> 16U);
1142
1143 /* Calculate the user option byte to write */
1144 tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16U);
1145 tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
1146
1147 /* Wait for last operation to be completed */
1148 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1149
1150 if(status == HAL_OK)
1151 {
1152 /* Clean the error context */
1153 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1154
1155 /* Write the User Option Byte */
1156 OB->USER = tmp;
1157
1158 /* Wait for last operation to be completed */
1159 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1160 }
1161
1162 /* Return the Option Byte program Status */
1163 return status;
1164 }
1165
1166 #if defined(FLASH_OPTR_BFB2)
1167 /**
1168 * @brief Configures to boot from Bank1 or Bank2.
1169 * @param OB_BOOT select the FLASH Bank to boot from.
1170 * This parameter can be one of the following values:
1171 * This parameter can be one of the following values:
1172 * @arg @ref OB_BOOT_BANK1 BFB2 option bit reset
1173 * @arg @ref OB_BOOT_BANK2 BFB2 option bit set
1174 * @retval HAL status
1175 */
FLASH_OB_BootConfig(uint8_t OB_BOOT)1176 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
1177 {
1178 HAL_StatusTypeDef status = HAL_OK;
1179 uint32_t tmp = 0U, tmp1 = 0U;
1180
1181 /* Check the parameters */
1182 assert_param(IS_OB_BOOT_BANK(OB_BOOT));
1183
1184 /* Get the User Option byte register and BOR Level*/
1185 tmp1 = OB->USER & ((~FLASH_OPTR_BFB2) >> 16U);
1186
1187 /* Calculate the option byte to write */
1188 tmp = (uint32_t)~(OB_BOOT | tmp1) << 16U;
1189 tmp |= (OB_BOOT | tmp1);
1190
1191 /* Wait for last operation to be completed */
1192 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1193
1194 if(status == HAL_OK)
1195 {
1196 /* Clean the error context */
1197 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1198
1199 /* Write the BOOT Option Byte */
1200 OB->USER = tmp;
1201
1202 /* Wait for last operation to be completed */
1203 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1204 }
1205
1206 /* Return the Option Byte program Status */
1207 return status;
1208 }
1209
1210 #endif /* FLASH_OPTR_BFB2 */
1211
1212 /**
1213 * @}
1214 */
1215
1216 /**
1217 * @}
1218 */
1219
1220 /** @addtogroup FLASH
1221 * @{
1222 */
1223
1224
1225 /** @addtogroup FLASH_Private_Functions
1226 * @{
1227 */
1228
1229 /**
1230 * @brief Erases a specified page in program memory.
1231 * @param PageAddress The page address in program memory to be erased.
1232 * @note A Page is erased in the Program memory only if the address to load
1233 * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
1234 * @retval None
1235 */
FLASH_PageErase(uint32_t PageAddress)1236 void FLASH_PageErase(uint32_t PageAddress)
1237 {
1238 /* Clean the error context */
1239 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1240
1241 /* Set the ERASE bit */
1242 SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
1243
1244 /* Set PROG bit */
1245 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
1246
1247 /* Write 00000000h to the first word of the program page to erase */
1248 *(__IO uint32_t *)(uint32_t)(PageAddress & ~(FLASH_PAGE_SIZE - 1)) = 0x00000000;
1249 }
1250
1251 /**
1252 * @}
1253 */
1254
1255 /**
1256 * @}
1257 */
1258
1259 #endif /* HAL_FLASH_MODULE_ENABLED */
1260 /**
1261 * @}
1262 */
1263
1264