1 /**
2 ******************************************************************************
3 * @file stm32f0xx_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 FLASH peripheral:
9 * + Extended Initialization/de-initialization functions
10 * + Extended I/O operation functions
11 * + Extended Peripheral Control functions
12 *
13 @verbatim
14 ==============================================================================
15 ##### Flash peripheral extended features #####
16 ==============================================================================
17
18 ##### How to use this driver #####
19 ==============================================================================
20 [..] This driver provides functions to configure and program the FLASH memory
21 of all STM32F0xxx devices. It includes
22
23 (++) Set/Reset the write protection
24 (++) Program the user Option Bytes
25 (++) Get the Read protection Level
26
27 @endverbatim
28 ******************************************************************************
29 * @attention
30 *
31 * Copyright (c) 2016 STMicroelectronics.
32 * All rights reserved.
33 *
34 * This software is licensed under terms that can be found in the LICENSE file in
35 * the root directory of this software component.
36 * If no LICENSE file comes with this software, it is provided AS-IS.
37 ******************************************************************************
38 */
39
40 /* Includes ------------------------------------------------------------------*/
41 #include "stm32f0xx_hal.h"
42
43 /** @addtogroup STM32F0xx_HAL_Driver
44 * @{
45 */
46 #ifdef HAL_FLASH_MODULE_ENABLED
47
48 /** @addtogroup FLASH
49 * @{
50 */
51 /** @addtogroup FLASH_Private_Variables
52 * @{
53 */
54 /* Variables used for Erase pages under interruption*/
55 extern FLASH_ProcessTypeDef pFlash;
56 /**
57 * @}
58 */
59
60 /**
61 * @}
62 */
63
64 /** @defgroup FLASHEx FLASHEx
65 * @brief FLASH HAL Extension module driver
66 * @{
67 */
68
69 /* Private typedef -----------------------------------------------------------*/
70 /* Private define ------------------------------------------------------------*/
71 /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
72 * @{
73 */
74 #define FLASH_POSITION_IWDGSW_BIT 8U
75 #define FLASH_POSITION_OB_USERDATA0_BIT 16U
76 #define FLASH_POSITION_OB_USERDATA1_BIT 24U
77 /**
78 * @}
79 */
80
81 /* Private macro -------------------------------------------------------------*/
82 /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
83 * @{
84 */
85 /**
86 * @}
87 */
88
89 /* Private variables ---------------------------------------------------------*/
90 /* Private function prototypes -----------------------------------------------*/
91 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
92 * @{
93 */
94 /* Erase operations */
95 static void FLASH_MassErase(void);
96 void FLASH_PageErase(uint32_t PageAddress);
97
98 /* Option bytes control */
99 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage);
100 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage);
101 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
102 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig);
103 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
104 static uint32_t FLASH_OB_GetWRP(void);
105 static uint32_t FLASH_OB_GetRDP(void);
106 static uint8_t FLASH_OB_GetUser(void);
107
108 /**
109 * @}
110 */
111
112 /* Exported functions ---------------------------------------------------------*/
113 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
114 * @{
115 */
116
117 /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
118 * @brief FLASH Memory Erasing functions
119 *
120 @verbatim
121 ==============================================================================
122 ##### FLASH Erasing Programming functions #####
123 ==============================================================================
124
125 [..] The FLASH Memory Erasing functions, includes the following functions:
126 (+) HAL_FLASHEx_Erase: return only when erase has been done
127 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback
128 is called with parameter 0xFFFFFFFF
129
130 [..] Any operation of erase should follow these steps:
131 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
132 program memory access.
133 (#) Call the desired function to erase page.
134 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
135 (recommended to protect the FLASH memory against possible unwanted operation).
136
137 @endverbatim
138 * @{
139 */
140
141
142 /**
143 * @brief Perform a mass erase or erase the specified FLASH memory pages
144 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
145 * must be called before.
146 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
147 * (recommended to protect the FLASH memory against possible unwanted operation)
148 * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
149 * contains the configuration information for the erasing.
150 *
151 * @param[out] PageError pointer to variable that
152 * contains the configuration information on faulty page in case of error
153 * (0xFFFFFFFF means that all the pages have been correctly erased)
154 *
155 * @retval HAL_StatusTypeDef HAL Status
156 */
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef * pEraseInit,uint32_t * PageError)157 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
158 {
159 HAL_StatusTypeDef status = HAL_ERROR;
160 uint32_t address = 0U;
161
162 /* Process Locked */
163 __HAL_LOCK(&pFlash);
164
165 /* Check the parameters */
166 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
167
168 if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
169 {
170 /* Mass Erase requested for Bank1 */
171 /* Wait for last operation to be completed */
172 if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
173 {
174 /*Mass erase to be done*/
175 FLASH_MassErase();
176
177 /* Wait for last operation to be completed */
178 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
179
180 /* If the erase operation is completed, disable the MER Bit */
181 CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
182 }
183 }
184 else
185 {
186 /* Page Erase is requested */
187 /* Check the parameters */
188 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
189 assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
190
191 /* Page Erase requested on address located on bank1 */
192 /* Wait for last operation to be completed */
193 if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
194 {
195 /*Initialization of PageError variable*/
196 *PageError = 0xFFFFFFFFU;
197
198 /* Erase page by page to be done*/
199 for(address = pEraseInit->PageAddress;
200 address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
201 address += FLASH_PAGE_SIZE)
202 {
203 FLASH_PageErase(address);
204
205 /* Wait for last operation to be completed */
206 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
207
208 /* If the erase operation is completed, disable the PER Bit */
209 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
210
211 if (status != HAL_OK)
212 {
213 /* In case of error, stop erase procedure and return the faulty address */
214 *PageError = address;
215 break;
216 }
217 }
218 }
219 }
220
221 /* Process Unlocked */
222 __HAL_UNLOCK(&pFlash);
223
224 return status;
225 }
226
227 /**
228 * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled
229 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
230 * must be called before.
231 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
232 * (recommended to protect the FLASH memory against possible unwanted operation)
233 * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
234 * contains the configuration information for the erasing.
235 *
236 * @retval HAL_StatusTypeDef HAL Status
237 */
HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef * pEraseInit)238 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
239 {
240 HAL_StatusTypeDef status = HAL_OK;
241
242 /* Process Locked */
243 __HAL_LOCK(&pFlash);
244
245 /* If procedure already ongoing, reject the next one */
246 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
247 {
248 return HAL_ERROR;
249 }
250
251 /* Check the parameters */
252 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
253
254 /* Enable End of FLASH Operation and Error source interrupts */
255 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
256
257 if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
258 {
259 /*Mass erase to be done*/
260 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
261 FLASH_MassErase();
262 }
263 else
264 {
265 /* Erase by page to be done*/
266
267 /* Check the parameters */
268 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
269 assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
270
271 pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
272 pFlash.DataRemaining = pEraseInit->NbPages;
273 pFlash.Address = pEraseInit->PageAddress;
274
275 /*Erase 1st page and wait for IT*/
276 FLASH_PageErase(pEraseInit->PageAddress);
277 }
278
279 return status;
280 }
281
282 /**
283 * @}
284 */
285
286 /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
287 * @brief Option Bytes Programming functions
288 *
289 @verbatim
290 ==============================================================================
291 ##### Option Bytes Programming functions #####
292 ==============================================================================
293 [..]
294 This subsection provides a set of functions allowing to control the FLASH
295 option bytes operations.
296
297 @endverbatim
298 * @{
299 */
300
301 /**
302 * @brief Erases the FLASH option bytes.
303 * @note This functions erases all option bytes except the Read protection (RDP).
304 * The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
305 * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
306 * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
307 * (system reset will occur)
308 * @retval HAL status
309 */
310
HAL_FLASHEx_OBErase(void)311 HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
312 {
313 uint8_t rdptmp = OB_RDP_LEVEL_0;
314 HAL_StatusTypeDef status = HAL_ERROR;
315
316 /* Get the actual read protection Option Byte value */
317 rdptmp = FLASH_OB_GetRDP();
318
319 /* Wait for last operation to be completed */
320 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
321
322 if(status == HAL_OK)
323 {
324 /* Clean the error context */
325 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
326
327 /* If the previous operation is completed, proceed to erase the option bytes */
328 SET_BIT(FLASH->CR, FLASH_CR_OPTER);
329 SET_BIT(FLASH->CR, FLASH_CR_STRT);
330
331 /* Wait for last operation to be completed */
332 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
333
334 /* If the erase operation is completed, disable the OPTER Bit */
335 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
336
337 if(status == HAL_OK)
338 {
339 /* Restore the last read protection Option Byte value */
340 status = FLASH_OB_RDP_LevelConfig(rdptmp);
341 }
342 }
343
344 /* Return the erase status */
345 return status;
346 }
347
348 /**
349 * @brief Program option bytes
350 * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
351 * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
352 * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
353 * (system reset will occur)
354 *
355 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
356 * contains the configuration information for the programming.
357 *
358 * @retval HAL_StatusTypeDef HAL Status
359 */
HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef * pOBInit)360 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
361 {
362 HAL_StatusTypeDef status = HAL_ERROR;
363
364 /* Process Locked */
365 __HAL_LOCK(&pFlash);
366
367 /* Check the parameters */
368 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
369
370 /* Write protection configuration */
371 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
372 {
373 assert_param(IS_WRPSTATE(pOBInit->WRPState));
374 if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
375 {
376 /* Enable of Write protection on the selected page */
377 status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
378 }
379 else
380 {
381 /* Disable of Write protection on the selected page */
382 status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
383 }
384 if (status != HAL_OK)
385 {
386 /* Process Unlocked */
387 __HAL_UNLOCK(&pFlash);
388 return status;
389 }
390 }
391
392 /* Read protection configuration */
393 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
394 {
395 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
396 if (status != HAL_OK)
397 {
398 /* Process Unlocked */
399 __HAL_UNLOCK(&pFlash);
400 return status;
401 }
402 }
403
404 /* USER configuration */
405 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
406 {
407 status = FLASH_OB_UserConfig(pOBInit->USERConfig);
408 if (status != HAL_OK)
409 {
410 /* Process Unlocked */
411 __HAL_UNLOCK(&pFlash);
412 return status;
413 }
414 }
415
416 /* DATA configuration*/
417 if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
418 {
419 status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
420 if (status != HAL_OK)
421 {
422 /* Process Unlocked */
423 __HAL_UNLOCK(&pFlash);
424 return status;
425 }
426 }
427
428 /* Process Unlocked */
429 __HAL_UNLOCK(&pFlash);
430
431 return status;
432 }
433
434 /**
435 * @brief Get the Option byte configuration
436 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
437 * contains the configuration information for the programming.
438 *
439 * @retval None
440 */
HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef * pOBInit)441 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
442 {
443 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
444
445 /*Get WRP*/
446 pOBInit->WRPPage = FLASH_OB_GetWRP();
447
448 /*Get RDP Level*/
449 pOBInit->RDPLevel = FLASH_OB_GetRDP();
450
451 /*Get USER*/
452 pOBInit->USERConfig = FLASH_OB_GetUser();
453 }
454
455 /**
456 * @brief Get the Option byte user data
457 * @param DATAAdress Address of the option byte DATA
458 * This parameter can be one of the following values:
459 * @arg @ref OB_DATA_ADDRESS_DATA0
460 * @arg @ref OB_DATA_ADDRESS_DATA1
461 * @retval Value programmed in USER data
462 */
HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress)463 uint32_t HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress)
464 {
465 uint32_t value = 0U;
466
467 if (DATAAdress == OB_DATA_ADDRESS_DATA0)
468 {
469 /* Get value programmed in OB USER Data0 */
470 value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA0) >> FLASH_POSITION_OB_USERDATA0_BIT;
471 }
472 else
473 {
474 /* Get value programmed in OB USER Data1 */
475 value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA1) >> FLASH_POSITION_OB_USERDATA1_BIT;
476 }
477
478 return value;
479 }
480
481 /**
482 * @}
483 */
484
485 /**
486 * @}
487 */
488
489 /** @addtogroup FLASHEx_Private_Functions
490 * @{
491 */
492
493 /**
494 * @brief Full erase of FLASH memory Bank
495 *
496 * @retval None
497 */
FLASH_MassErase(void)498 static void FLASH_MassErase(void)
499 {
500 /* Clean the error context */
501 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
502
503 /* Only bank1 will be erased*/
504 SET_BIT(FLASH->CR, FLASH_CR_MER);
505 SET_BIT(FLASH->CR, FLASH_CR_STRT);
506 }
507
508 /**
509 * @brief Enable the write protection of the desired pages
510 * @note An option byte erase is done automatically in this function.
511 * @note When the memory read protection level is selected (RDP level = 1),
512 * it is not possible to program or erase the flash page i if
513 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
514 *
515 * @param WriteProtectPage specifies the page(s) to be write protected.
516 * The value of this parameter depend on device used within the same series
517 * @retval HAL status
518 */
FLASH_OB_EnableWRP(uint32_t WriteProtectPage)519 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
520 {
521 HAL_StatusTypeDef status = HAL_OK;
522 uint16_t WRP0_Data = 0xFFFFU;
523 #if defined(OB_WRP1_WRP1)
524 uint16_t WRP1_Data = 0xFFFFU;
525 #endif /* OB_WRP1_WRP1 */
526 #if defined(OB_WRP2_WRP2)
527 uint16_t WRP2_Data = 0xFFFFU;
528 #endif /* OB_WRP2_WRP2 */
529 #if defined(OB_WRP3_WRP3)
530 uint16_t WRP3_Data = 0xFFFFU;
531 #endif /* OB_WRP3_WRP3 */
532
533 /* Check the parameters */
534 assert_param(IS_OB_WRP(WriteProtectPage));
535
536 /* Get current write protected pages and the new pages to be protected ******/
537 WriteProtectPage = (uint32_t)(~((~FLASH_OB_GetWRP()) | WriteProtectPage));
538
539 #if defined(OB_WRP_PAGES0TO15MASK)
540 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
541 #elif defined(OB_WRP_PAGES0TO31MASK)
542 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
543 #endif /* OB_WRP_PAGES0TO31MASK */
544
545 #if defined(OB_WRP_PAGES16TO31MASK)
546 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
547 #elif defined(OB_WRP_PAGES32TO63MASK)
548 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
549 #endif /* OB_WRP_PAGES32TO63MASK */
550
551 #if defined(OB_WRP_PAGES32TO47MASK)
552 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
553 #endif /* OB_WRP_PAGES32TO47MASK */
554
555 #if defined(OB_WRP_PAGES48TO63MASK)
556 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO63MASK) >> 24U);
557 #elif defined(OB_WRP_PAGES48TO127MASK)
558 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
559 #endif /* OB_WRP_PAGES48TO63MASK */
560
561 /* Wait for last operation to be completed */
562 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
563
564 if(status == HAL_OK)
565 {
566 /* Clean the error context */
567 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
568
569 /* To be able to write again option byte, need to perform a option byte erase */
570 status = HAL_FLASHEx_OBErase();
571 if (status == HAL_OK)
572 {
573 /* Enable write protection */
574 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
575
576 #if defined(OB_WRP0_WRP0)
577 if(WRP0_Data != 0xFFU)
578 {
579 OB->WRP0 &= WRP0_Data;
580
581 /* Wait for last operation to be completed */
582 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
583 }
584 #endif /* OB_WRP0_WRP0 */
585
586 #if defined(OB_WRP1_WRP1)
587 if((status == HAL_OK) && (WRP1_Data != 0xFFU))
588 {
589 OB->WRP1 &= WRP1_Data;
590
591 /* Wait for last operation to be completed */
592 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
593 }
594 #endif /* OB_WRP1_WRP1 */
595
596 #if defined(OB_WRP2_WRP2)
597 if((status == HAL_OK) && (WRP2_Data != 0xFFU))
598 {
599 OB->WRP2 &= WRP2_Data;
600
601 /* Wait for last operation to be completed */
602 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
603 }
604 #endif /* OB_WRP2_WRP2 */
605
606 #if defined(OB_WRP3_WRP3)
607 if((status == HAL_OK) && (WRP3_Data != 0xFFU))
608 {
609 OB->WRP3 &= WRP3_Data;
610
611 /* Wait for last operation to be completed */
612 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
613 }
614 #endif /* OB_WRP3_WRP3 */
615
616 /* if the program operation is completed, disable the OPTPG Bit */
617 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
618 }
619 }
620
621 return status;
622 }
623
624 /**
625 * @brief Disable the write protection of the desired pages
626 * @note An option byte erase is done automatically in this function.
627 * @note When the memory read protection level is selected (RDP level = 1),
628 * it is not possible to program or erase the flash page i if
629 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
630 *
631 * @param WriteProtectPage specifies the page(s) to be write unprotected.
632 * The value of this parameter depend on device used within the same series
633 * @retval HAL status
634 */
FLASH_OB_DisableWRP(uint32_t WriteProtectPage)635 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
636 {
637 HAL_StatusTypeDef status = HAL_OK;
638 uint16_t WRP0_Data = 0xFFFFU;
639 #if defined(OB_WRP1_WRP1)
640 uint16_t WRP1_Data = 0xFFFFU;
641 #endif /* OB_WRP1_WRP1 */
642 #if defined(OB_WRP2_WRP2)
643 uint16_t WRP2_Data = 0xFFFFU;
644 #endif /* OB_WRP2_WRP2 */
645 #if defined(OB_WRP3_WRP3)
646 uint16_t WRP3_Data = 0xFFFFU;
647 #endif /* OB_WRP3_WRP3 */
648
649 /* Check the parameters */
650 assert_param(IS_OB_WRP(WriteProtectPage));
651
652 /* Get current write protected pages and the new pages to be unprotected ******/
653 WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage);
654
655 #if defined(OB_WRP_PAGES0TO15MASK)
656 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
657 #elif defined(OB_WRP_PAGES0TO31MASK)
658 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
659 #endif /* OB_WRP_PAGES0TO31MASK */
660
661 #if defined(OB_WRP_PAGES16TO31MASK)
662 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
663 #elif defined(OB_WRP_PAGES32TO63MASK)
664 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
665 #endif /* OB_WRP_PAGES32TO63MASK */
666
667 #if defined(OB_WRP_PAGES32TO47MASK)
668 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
669 #endif /* OB_WRP_PAGES32TO47MASK */
670
671 #if defined(OB_WRP_PAGES48TO63MASK)
672 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO63MASK) >> 24U);
673 #elif defined(OB_WRP_PAGES48TO127MASK)
674 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
675 #endif /* OB_WRP_PAGES48TO63MASK */
676
677
678 /* Wait for last operation to be completed */
679 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
680
681 if(status == HAL_OK)
682 {
683 /* Clean the error context */
684 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
685
686 /* To be able to write again option byte, need to perform a option byte erase */
687 status = HAL_FLASHEx_OBErase();
688 if (status == HAL_OK)
689 {
690 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
691
692 #if defined(OB_WRP0_WRP0)
693 if(WRP0_Data != 0xFFU)
694 {
695 OB->WRP0 &= WRP0_Data;
696
697 /* Wait for last operation to be completed */
698 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
699 }
700 #endif /* OB_WRP0_WRP0 */
701
702 #if defined(OB_WRP1_WRP1)
703 if((status == HAL_OK) && (WRP1_Data != 0xFFU))
704 {
705 OB->WRP1 &= WRP1_Data;
706
707 /* Wait for last operation to be completed */
708 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
709 }
710 #endif /* OB_WRP1_WRP1 */
711
712 #if defined(OB_WRP2_WRP2)
713 if((status == HAL_OK) && (WRP2_Data != 0xFFU))
714 {
715 OB->WRP2 &= WRP2_Data;
716
717 /* Wait for last operation to be completed */
718 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
719 }
720 #endif /* OB_WRP2_WRP2 */
721
722 #if defined(OB_WRP3_WRP3)
723 if((status == HAL_OK) && (WRP3_Data != 0xFFU))
724 {
725 OB->WRP3 &= WRP3_Data;
726
727 /* Wait for last operation to be completed */
728 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
729 }
730 #endif /* OB_WRP3_WRP3 */
731
732 /* if the program operation is completed, disable the OPTPG Bit */
733 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
734 }
735 }
736 return status;
737 }
738
739 /**
740 * @brief Set the read protection level.
741 * @param ReadProtectLevel specifies the read protection level.
742 * This parameter can be one of the following values:
743 * @arg @ref OB_RDP_LEVEL_0 No protection
744 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
745 * @arg @ref OB_RDP_LEVEL_2 Full chip protection
746 * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
747 * @retval HAL status
748 */
FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)749 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
750 {
751 HAL_StatusTypeDef status = HAL_OK;
752
753 /* Check the parameters */
754 assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
755
756 /* Wait for last operation to be completed */
757 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
758
759 if(status == HAL_OK)
760 {
761 /* Clean the error context */
762 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
763
764 /* If the previous operation is completed, proceed to erase the option bytes */
765 SET_BIT(FLASH->CR, FLASH_CR_OPTER);
766 SET_BIT(FLASH->CR, FLASH_CR_STRT);
767
768 /* Wait for last operation to be completed */
769 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
770
771 /* If the erase operation is completed, disable the OPTER Bit */
772 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
773
774 if(status == HAL_OK)
775 {
776 /* Enable the Option Bytes Programming operation */
777 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
778
779 WRITE_REG(OB->RDP, ReadProtectLevel);
780
781 /* Wait for last operation to be completed */
782 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
783
784 /* if the program operation is completed, disable the OPTPG Bit */
785 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
786 }
787 }
788
789 return status;
790 }
791
792 /**
793 * @brief Program the FLASH User Option Byte.
794 * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
795 * @param UserConfig The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), nBOOT1(Bit4),
796 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
797 * For few devices, following option bytes are available: nBOOT0(Bit3) & BOOT_SEL(Bit7).
798 * @retval HAL status
799 */
FLASH_OB_UserConfig(uint8_t UserConfig)800 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
801 {
802 HAL_StatusTypeDef status = HAL_OK;
803
804 /* Check the parameters */
805 assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
806 assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
807 assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
808 assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
809 assert_param(IS_OB_VDDA_ANALOG((UserConfig&OB_VDDA_ANALOG_ON)));
810 assert_param(IS_OB_SRAM_PARITY((UserConfig&OB_SRAM_PARITY_RESET)));
811 #if defined(FLASH_OBR_BOOT_SEL)
812 assert_param(IS_OB_BOOT_SEL((UserConfig&OB_BOOT_SEL_SET)));
813 assert_param(IS_OB_BOOT0((UserConfig&OB_BOOT0_SET)));
814 #endif /* FLASH_OBR_BOOT_SEL */
815
816 /* Wait for last operation to be completed */
817 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
818
819 if(status == HAL_OK)
820 {
821 /* Clean the error context */
822 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
823
824 /* Enable the Option Bytes Programming operation */
825 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
826
827 #if defined(FLASH_OBR_BOOT_SEL)
828 OB->USER = UserConfig;
829 #else
830 OB->USER = (UserConfig | 0x88U);
831 #endif
832
833 /* Wait for last operation to be completed */
834 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
835
836 /* if the program operation is completed, disable the OPTPG Bit */
837 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
838 }
839
840 return status;
841 }
842
843 /**
844 * @brief Programs a half word at a specified Option Byte Data address.
845 * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
846 * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
847 * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
848 * (system reset will occur)
849 * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
850 * @param Address specifies the address to be programmed.
851 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
852 * @param Data specifies the data to be programmed.
853 * @retval HAL status
854 */
FLASH_OB_ProgramData(uint32_t Address,uint8_t Data)855 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
856 {
857 HAL_StatusTypeDef status = HAL_ERROR;
858
859 /* Check the parameters */
860 assert_param(IS_OB_DATA_ADDRESS(Address));
861
862 /* Wait for last operation to be completed */
863 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
864
865 if(status == HAL_OK)
866 {
867 /* Clean the error context */
868 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
869
870 /* Enables the Option Bytes Programming operation */
871 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
872 *(__IO uint16_t*)Address = Data;
873
874 /* Wait for last operation to be completed */
875 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
876
877 /* If the program operation is completed, disable the OPTPG Bit */
878 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
879 }
880 /* Return the Option Byte Data Program Status */
881 return status;
882 }
883
884 /**
885 * @brief Return the FLASH Write Protection Option Bytes value.
886 * @retval The FLASH Write Protection Option Bytes value
887 */
FLASH_OB_GetWRP(void)888 static uint32_t FLASH_OB_GetWRP(void)
889 {
890 /* Return the FLASH write protection Register value */
891 return (uint32_t)(READ_REG(FLASH->WRPR));
892 }
893
894 /**
895 * @brief Returns the FLASH Read Protection level.
896 * @retval FLASH RDP level
897 * This parameter can be one of the following values:
898 * @arg @ref OB_RDP_LEVEL_0 No protection
899 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
900 * @arg @ref OB_RDP_LEVEL_2 Full chip protection
901 */
FLASH_OB_GetRDP(void)902 static uint32_t FLASH_OB_GetRDP(void)
903 {
904 uint32_t tmp_reg;
905
906 /* Read RDP level bits */
907 tmp_reg = READ_BIT(FLASH->OBR, (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2));
908
909 if (tmp_reg == 0U)
910 {
911 return OB_RDP_LEVEL_0;
912 }
913 else if ((tmp_reg & FLASH_OBR_RDPRT2) == FLASH_OBR_RDPRT2)
914 {
915 return OB_RDP_LEVEL_2;
916 }
917 else
918 {
919 return OB_RDP_LEVEL_1;
920 }
921 }
922
923 /**
924 * @brief Return the FLASH User Option Byte value.
925 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), nBOOT1(Bit4),
926 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
927 * For few devices, following option bytes are available: nBOOT0(Bit3) & BOOT_SEL(Bit7).
928 */
FLASH_OB_GetUser(void)929 static uint8_t FLASH_OB_GetUser(void)
930 {
931 /* Return the User Option Byte */
932 return (uint8_t)((READ_REG(FLASH->OBR) & FLASH_OBR_USER) >> FLASH_POSITION_IWDGSW_BIT);
933 }
934
935 /**
936 * @}
937 */
938
939 /**
940 * @}
941 */
942
943 /** @addtogroup FLASH
944 * @{
945 */
946
947 /** @addtogroup FLASH_Private_Functions
948 * @{
949 */
950
951 /**
952 * @brief Erase the specified FLASH memory page
953 * @param PageAddress FLASH page to erase
954 * The value of this parameter depend on device used within the same series
955 *
956 * @retval None
957 */
FLASH_PageErase(uint32_t PageAddress)958 void FLASH_PageErase(uint32_t PageAddress)
959 {
960 /* Clean the error context */
961 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
962
963 /* Proceed to erase the page */
964 SET_BIT(FLASH->CR, FLASH_CR_PER);
965 WRITE_REG(FLASH->AR, PageAddress);
966 SET_BIT(FLASH->CR, FLASH_CR_STRT);
967 }
968
969 /**
970 * @}
971 */
972
973 /**
974 * @}
975 */
976
977 #endif /* HAL_FLASH_MODULE_ENABLED */
978 /**
979 * @}
980 */
981
982