]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_flash_ex.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / stm32f0xx_hal_flash_ex.c
1 /**
2 ******************************************************************************
3 * @file stm32f0xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 11-December-2014
7 * @brief Extended FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the FLASH peripheral:
10 * + Extended Initialization/de-initialization functions
11 * + Extended I/O operation functions
12 * + Extended Peripheral Control functions
13 * + Extended Peripheral State functions
14 *
15 @verbatim
16 ==============================================================================
17 ##### Flash peripheral extended features #####
18 ==============================================================================
19
20 ##### How to use this driver #####
21 ==============================================================================
22 [..] This driver provides functions to configure and program the FLASH memory
23 of all STM32F0xxx devices. It includes
24
25 (+) Set/Reset the write protection
26 (+) Program the user Option Bytes
27 (+) Get the Read protection Level
28
29 @endverbatim
30 ******************************************************************************
31 * @attention
32 *
33 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
34 *
35 * Redistribution and use in source and binary forms, with or without modification,
36 * are permitted provided that the following conditions are met:
37 * 1. Redistributions of source code must retain the above copyright notice,
38 * this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright notice,
40 * this list of conditions and the following disclaimer in the documentation
41 * and/or other materials provided with the distribution.
42 * 3. Neither the name of STMicroelectronics nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
53 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 *
57 ******************************************************************************
58 */
59
60 /* Includes ------------------------------------------------------------------*/
61 #include "stm32f0xx_hal.h"
62
63 /** @addtogroup STM32F0xx_HAL_Driver
64 * @{
65 */
66
67 /** @defgroup FLASHEx FLASHEx Extended HAL module driver
68 * @brief FLASH Extended HAL module driver
69 * @{
70 */
71
72 #ifdef HAL_FLASH_MODULE_ENABLED
73
74 /* Private typedef -----------------------------------------------------------*/
75 /* Private define ------------------------------------------------------------*/
76
77 /** @addtogroup FLASHEx_Private_Constants FLASHEx Private Constants
78 * @{
79 */
80 #define HAL_FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
81
82 /**
83 * @}
84 */
85
86 /* Private macro -------------------------------------------------------------*/
87 /* Private variables ---------------------------------------------------------*/
88 /** @addtogroup FLASHEx_Private_Variables FLASHEx Private Variables
89 * @{
90 */
91
92 /* Variables used for Erase pages under interruption*/
93 extern FLASH_ProcessTypeDef pFlash;
94
95 /**
96 * @}
97 */
98
99 /* Private function prototypes -----------------------------------------------*/
100 /** @addtogroup FLASHEx_Private_Functions FLASHEx Private Functions
101 * @{
102 */
103
104 /* Erase operations */
105 extern void FLASH_PageErase(uint32_t PageAddress);
106 static void FLASH_MassErase(void);
107
108 /* Option bytes control */
109 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage);
110 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage);
111 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
112 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig);
113 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
114 static uint32_t FLASH_OB_GetWRP(void);
115 static FlagStatus FLASH_OB_GetRDP(void);
116 static uint8_t FLASH_OB_GetUser(void);
117
118 /**
119 * @}
120 */
121
122 /** @defgroup FLASHEx_Extern_Functions FLASHEx Extern Functions
123 * @{
124 */
125 /* Private functions ---------------------------------------------------------*/
126 extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
127
128 /**
129 * @}
130 */
131
132 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
133 * @{
134 */
135
136 /** @defgroup FLASHEx_Exported_Functions_Group2 Extended I/O operation functions
137 * @brief Extended I/O operation functions
138 *
139 @verbatim
140 ===============================================================================
141 ##### IO operation functions #####
142 ===============================================================================
143
144 @endverbatim
145 * @{
146 */
147 /**
148 * @brief Perform a mass erase or erase the specified FLASH memory pages
149 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
150 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
151 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
152 * contains the configuration information for the erasing.
153 *
154 * @param[out] PageError: pointer to variable that
155 * contains the configuration information on faulty page in case of error
156 * (0xFFFFFFFF means that all the pages have been correctly erased)
157 *
158 * @retval HAL_StatusTypeDef HAL Status
159 */
160 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
161 {
162 HAL_StatusTypeDef status = HAL_ERROR;
163 uint32_t address = 0;
164
165 /* Process Locked */
166 __HAL_LOCK(&pFlash);
167
168 /* Check the parameters */
169 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
170
171 /* Wait for last operation to be completed */
172 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
173
174 if (status == HAL_OK)
175 {
176 if (pEraseInit->TypeErase == TYPEERASE_MASSERASE)
177 {
178 /*Mass erase to be done*/
179 FLASH_MassErase();
180
181 /* Wait for last operation to be completed */
182 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
183
184 /* If the erase operation is completed, disable the MER Bit */
185 CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
186 }
187 else
188 {
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 /*Initialization of PageError variable*/
194 *PageError = 0xFFFFFFFF;
195
196 /* Erase by page by page to be done*/
197 for(address = pEraseInit->PageAddress;
198 address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE);
199 address += FLASH_PAGE_SIZE)
200 {
201 FLASH_PageErase(address);
202
203 /* Wait for last operation to be completed */
204 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
205
206 /* If the erase operation is completed, disable the PER Bit */
207 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
208
209 if (status != HAL_OK)
210 {
211 /* In case of error, stop erase procedure and return the faulty address */
212 *PageError = address;
213 break;
214 }
215 }
216 }
217 }
218
219 /* Process Unlocked */
220 __HAL_UNLOCK(&pFlash);
221
222 return status;
223 }
224
225 /**
226 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
227 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
228 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
229 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
230 * contains the configuration information for the erasing.
231 *
232 * @retval HAL_StatusTypeDef HAL Status
233 */
234 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
235 {
236 HAL_StatusTypeDef status = HAL_OK;
237
238 /* Process Locked */
239 __HAL_LOCK(&pFlash);
240
241 /* If procedure already ongoing, reject the next one */
242 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
243 {
244 return HAL_ERROR;
245 }
246
247 /* Check the parameters */
248 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
249
250 /* Enable End of FLASH Operation and Error source interrupts */
251 __HAL_FLASH_ENABLE_IT((FLASH_IT_EOP | FLASH_IT_ERR));
252
253 if (pEraseInit->TypeErase == TYPEERASE_MASSERASE)
254 {
255 /*Mass erase to be done*/
256 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
257 FLASH_MassErase();
258 }
259 else
260 {
261 /* Erase by page to be done*/
262
263 /* Check the parameters */
264 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
265 assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
266
267 pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
268 pFlash.DataRemaining = pEraseInit->NbPages;
269 pFlash.Address = pEraseInit->PageAddress;
270
271 /*Erase 1st page and wait for IT*/
272 FLASH_PageErase(pEraseInit->PageAddress);
273 }
274
275 return status;
276 }
277
278 /**
279 * @}
280 */
281
282 /** @defgroup FLASHEx_Exported_Functions_Group3 Extended Peripheral Control functions
283 * @brief Extended Peripheral Control functions
284 *
285 @verbatim
286 ===============================================================================
287 ##### Peripheral Control functions #####
288 ===============================================================================
289 [..]
290 This subsection provides a set of functions allowing to control the FLASH
291 memory operations.
292
293 @endverbatim
294 * @{
295 */
296 /**
297 * @brief Erases the FLASH option bytes.
298 * @note This functions erases all option bytes except the Read protection (RDP).
299 * The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
300 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
301 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
302 * (system reset will occur)
303 * @retval HAL status
304 */
305
306 HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
307 {
308 uint8_t rdptmp = OB_RDP_LEVEL_0;
309 HAL_StatusTypeDef status = HAL_ERROR;
310 FLASH_OBProgramInitTypeDef optionsbytes;
311
312 /* Get the actual read protection Option Byte value */
313 HAL_FLASHEx_OBGetConfig(&optionsbytes);
314 if(optionsbytes.RDPLevel != RESET)
315 {
316 rdptmp = OB_RDP_LEVEL_1;
317 }
318
319 /* Wait for last operation to be completed */
320 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
321
322 if(status == HAL_OK)
323 {
324 /* Clean the error context */
325 pFlash.ErrorCode = 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)HAL_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 optionsbytes.OptionType = OPTIONBYTE_RDP;
341 optionsbytes.RDPLevel = rdptmp;
342 status = HAL_FLASHEx_OBProgram(&optionsbytes);
343 }
344 }
345
346 /* Return the erase status */
347 return status;
348 }
349
350 /**
351 * @brief Program option bytes
352 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
353 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
354 * The function 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 */
362 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
363 {
364 HAL_StatusTypeDef status = HAL_ERROR;
365
366 /* Check the parameters */
367 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
368
369 /* Write protection configuration */
370 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
371 {
372 assert_param(IS_WRPSTATE(pOBInit->WRPState));
373 if (pOBInit->WRPState == WRPSTATE_ENABLE)
374 {
375 /* Enable of Write protection on the selected page */
376 status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
377 }
378 else
379 {
380 /* Disable of Write protection on the selected page */
381 status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
382 }
383 }
384
385 /* Read protection configuration */
386 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
387 {
388 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
389 }
390
391 /* USER configuration */
392 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
393 {
394 status = FLASH_OB_UserConfig(pOBInit->USERConfig);
395 }
396
397 /* DATA configuration*/
398 if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
399 {
400 status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
401 }
402
403 return status;
404 }
405
406 /**
407 * @brief Get the Option byte configuration
408 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
409 * contains the configuration information for the programming.
410 *
411 * @retval None
412 */
413 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
414 {
415 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
416
417 /*Get WRP*/
418 pOBInit->WRPPage = FLASH_OB_GetWRP();
419
420 /*Get RDP Level*/
421 pOBInit->RDPLevel = FLASH_OB_GetRDP();
422
423 /*Get USER*/
424 pOBInit->USERConfig = FLASH_OB_GetUser();
425 }
426
427 /**
428 * @}
429 */
430
431 /**
432 * @}
433 */
434
435 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
436 * @{
437 */
438
439 /**
440 * @brief Mass erase of FLASH memory
441 * @retval None
442 */
443 static void FLASH_MassErase(void)
444 {
445
446 /* Clean the error context */
447 pFlash.ErrorCode = FLASH_ERROR_NONE;
448
449 /* Proceed to erase all sectors */
450 SET_BIT(FLASH->CR, FLASH_CR_MER);
451 SET_BIT(FLASH->CR, FLASH_CR_STRT);
452 }
453
454 /**
455 * @brief Enable the write protection of the desired pages
456 * @note When the memory read protection level is selected (RDP level = 1),
457 * it is not possible to program or erase the flash page i if CortexM4
458 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
459 *
460 * @param WriteProtectPage: specifies the page(s) to be write protected.
461 * The value of this parameter depend on device used within the same series
462 * @retval HAL status
463 */
464 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
465 {
466 HAL_StatusTypeDef status = HAL_OK;
467 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F070x6) || \
468 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
469 uint16_t WRP0_Data = 0xFFFF;
470 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
471 uint16_t WRP1_Data = 0xFFFF;
472 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
473 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
474
475 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
476 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
477 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
478 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
479
480 /* Check the parameters */
481 assert_param(IS_OB_WRP(WriteProtectPage));
482
483 WriteProtectPage = (uint32_t)(~WriteProtectPage);
484 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F070x6) || \
485 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
486 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
487 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
488 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8);
489 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
490 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
491
492 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
493 defined(STM32F091xC) || defined(STM32F098xx)|| defined(STM32F030xC)
494 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
495 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
496 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
497 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)
498 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO63MASK) >> 24);
499 #endif /* STM32F071xB || STM32F072xB || STM32F078xx */
500 #if defined(STM32F091xC) || defined(STM32F098xx)
501 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24);
502 #endif /* STM32F091xC || STM32F098xx */
503 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
504
505 /* Wait for last operation to be completed */
506 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
507
508 if(status == HAL_OK)
509 {
510 /* Clean the error context */
511 pFlash.ErrorCode = FLASH_ERROR_NONE;
512
513 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
514
515 if(WRP0_Data != 0xFF)
516 {
517 OB->WRP0 &= WRP0_Data;
518
519 /* Wait for last operation to be completed */
520 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
521 }
522
523 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx) || \
524 defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
525 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
526 if((status == HAL_OK) && (WRP1_Data != 0xFF))
527 {
528 OB->WRP1 &= WRP1_Data;
529
530 /* Wait for last operation to be completed */
531 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
532 }
533 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx || STM32F071xB || STM32F070xB ||
534 STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */
535
536 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)|| defined(STM32F070xB) || \
537 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
538 if((status == HAL_OK) && (WRP2_Data != 0xFF))
539 {
540 OB->WRP2 &= WRP2_Data;
541
542 /* Wait for last operation to be completed */
543 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
544 }
545
546 if((status == HAL_OK) && (WRP3_Data != 0xFF))
547 {
548 OB->WRP3 &= WRP3_Data;
549
550 /* Wait for last operation to be completed */
551 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
552 }
553 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC*/
554
555 /* if the program operation is completed, disable the OPTPG Bit */
556 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
557 }
558
559 return status;
560 }
561
562 /**
563 * @brief Disable the write protection of the desired pages
564 * @note When the memory read protection level is selected (RDP level = 1),
565 * it is not possible to program or erase the flash page i if CortexM4
566 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
567 *
568 * @param WriteProtectPage: specifies the page(s) to be write unprotected.
569 * The value of this parameter depend on device used within the same series
570 * @retval HAL status
571 */
572 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
573 {
574 HAL_StatusTypeDef status = HAL_OK;
575 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F070x6) || \
576 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
577 uint16_t WRP0_Data = 0xFFFF;
578 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
579 uint16_t WRP1_Data = 0xFFFF;
580 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
581 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
582
583 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
584 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
585 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
586 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
587
588 /* Check the parameters */
589 assert_param(IS_OB_WRP(WriteProtectPage));
590
591 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || STM32F070x6 || \
592 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
593 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
594 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
595 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8);
596 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
597 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
598
599 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
600 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
601 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
602 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
603 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
604 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)
605 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO63MASK) >> 24);
606 #endif /* STM32F071xB || STM32F072xB || STM32F078xx */
607 #if defined(STM32F091xC) || defined(STM32F098xx)
608 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24);
609 #endif /* STM32F091xC || STM32F098xx */
610 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC || STM32F070xB */
611
612 /* Wait for last operation to be completed */
613 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
614
615 if(status == HAL_OK)
616 {
617 /* Clean the error context */
618 pFlash.ErrorCode = FLASH_ERROR_NONE;
619
620 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
621
622 if(WRP0_Data != 0xFF)
623 {
624 OB->WRP0 |= WRP0_Data;
625
626 /* Wait for last operation to be completed */
627 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
628 }
629
630 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx) || \
631 defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
632 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
633 if((status == HAL_OK) && (WRP1_Data != 0xFF))
634 {
635 OB->WRP1 |= WRP1_Data;
636
637 /* Wait for last operation to be completed */
638 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
639 }
640 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx || STM32F071xB || STM32F070xB ||
641 STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */
642
643 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
644 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
645 if((status == HAL_OK) && (WRP2_Data != 0xFF))
646 {
647 OB->WRP2 |= WRP2_Data;
648
649 /* Wait for last operation to be completed */
650 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
651 }
652
653 if((status == HAL_OK) && (WRP3_Data != 0xFF))
654 {
655 OB->WRP3 |= WRP3_Data;
656
657 /* Wait for last operation to be completed */
658 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
659 }
660 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
661
662 /* if the program operation is completed, disable the OPTPG Bit */
663 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
664 }
665 return status;
666 }
667
668 /**
669 * @brief Set the read protection level.
670 * @param ReadProtectLevel: specifies the read protection level.
671 * This parameter can be one of the following values:
672 * @arg OB_RDP_LEVEL_0: No protection
673 * @arg OB_RDP_LEVEL_1: Read protection of the memory
674 * @arg OB_RDP_LEVEL_2: Full chip protection
675 *
676 * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
677 *
678 * @retval HAL status
679 */
680 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
681 {
682 HAL_StatusTypeDef status = HAL_OK;
683
684 /* Check the parameters */
685 assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
686
687 /* Wait for last operation to be completed */
688 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
689
690 if(status == HAL_OK)
691 {
692 /* Clean the error context */
693 pFlash.ErrorCode = FLASH_ERROR_NONE;
694
695 /* Enable the Option Bytes Programming operation */
696 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
697
698 WRITE_REG(OB->RDP, ReadProtectLevel);
699
700 /* Wait for last operation to be completed */
701 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
702
703 /* if the program operation is completed, disable the OPTPG Bit */
704 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
705 }
706
707 return status;
708 }
709
710 /**
711 * @brief Program the FLASH User Option Byte.
712 * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
713 * @param UserConfig: The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
714 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
715 * @retval HAL status
716 */
717 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
718 {
719 HAL_StatusTypeDef status = HAL_OK;
720
721 /* Check the parameters */
722 assert_param(IS_OB_WDG_SOURCE((UserConfig&OB_WDG_SW)));
723 assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
724 assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
725 assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
726 assert_param(IS_OB_VDDA_ANALOG((UserConfig&OB_VDDA_ANALOG_ON)));
727 assert_param(IS_OB_SRAM_PARITY((UserConfig&OB_RAM_PARITY_CHECK_RESET)));
728 #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)|| defined(STM32F070x6)
729 assert_param(IS_OB_BOOT_SEL((UserConfig&OB_BOOT_SEL_SET)));
730 assert_param(IS_OB_BOOT0((UserConfig&OB_BOOT0_SET)));
731 #endif /* STM32F042x6 || STM32F048xx || STM32F091xC || STM32F098xx || STM32F030xC || STM32F070x6 */
732
733 /* Wait for last operation to be completed */
734 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
735
736 if(status == HAL_OK)
737 {
738 /* Clean the error context */
739 pFlash.ErrorCode = FLASH_ERROR_NONE;
740
741 /* Enable the Option Bytes Programming operation */
742 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
743
744 #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)|| defined(STM32F070x6)
745 OB->USER = UserConfig;
746 #else
747 OB->USER = (UserConfig | 0x88);
748 #endif /* STM32F042x6 || STM32F048xx || STM32F091xC || STM32F098xx || STM32F030xC || STM32F070x6 */
749
750 /* Wait for last operation to be completed */
751 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
752
753 /* if the program operation is completed, disable the OPTPG Bit */
754 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
755 }
756
757 return status;
758 }
759
760 /**
761 * @brief Programs a half word at a specified Option Byte Data address.
762 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
763 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
764 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
765 * (system reset will occur)
766 * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
767 * @param Address: specifies the address to be programmed.
768 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
769 * @param Data: specifies the data to be programmed.
770 * @retval HAL status
771 */
772 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
773 {
774 HAL_StatusTypeDef status = HAL_ERROR;
775
776 /* Check the parameters */
777 assert_param(IS_OB_DATA_ADDRESS(Address));
778
779 /* Wait for last operation to be completed */
780 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
781
782 if(status == HAL_OK)
783 {
784 /* Clean the error context */
785 pFlash.ErrorCode = FLASH_ERROR_NONE;
786
787 /* Enables the Option Bytes Programming operation */
788 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
789 *(__IO uint16_t*)Address = Data;
790
791 /* Wait for last operation to be completed */
792 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
793
794 /* If the program operation is completed, disable the OPTPG Bit */
795 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
796 }
797 /* Return the Option Byte Data Program Status */
798 return status;
799 }
800
801 /**
802 * @brief Return the FLASH Write Protection Option Bytes value.
803 * @retval The FLASH Write Protection Option Bytes value
804 */
805 static uint32_t FLASH_OB_GetWRP(void)
806 {
807 /* Return the FLASH write protection Register value */
808 return (uint32_t)(READ_REG(FLASH->WRPR));
809 }
810
811 /**
812 * @brief Returns the FLASH Read Protection level.
813 * @retval FLASH ReadOut Protection Status:
814 * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
815 * - RESET, when OB_RDP_Level_0 is set
816 */
817 static FlagStatus FLASH_OB_GetRDP(void)
818 {
819 FlagStatus readstatus = RESET;
820
821 if ((uint8_t)READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT1) != RESET)
822 {
823 readstatus = SET;
824 }
825
826 return readstatus;
827 }
828
829 /**
830 * @brief Return the FLASH User Option Byte value.
831 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
832 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
833 */
834 static uint8_t FLASH_OB_GetUser(void)
835 {
836 /* Return the User Option Byte */
837 return (uint8_t)(READ_REG(FLASH->OBR) >> 8);
838 }
839
840 /**
841 * @}
842 */
843
844 #endif /* HAL_FLASH_MODULE_ENABLED */
845
846 /**
847 * @}
848 */
849
850 /**
851 * @}
852 */
853
854 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum