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