rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stm32f7xx_hal_flash_ex.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file stm32f7xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 30-December-2016
7 * @brief Extended FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the FLASH extension peripheral:
10 * + Extended programming operations functions
11 *
12 @verbatim
13 ==============================================================================
14 ##### Flash Extension features #####
15 ==============================================================================
16
17 [..] Comparing to other previous devices, the FLASH interface for STM32F76xx/STM32F77xx
18 devices contains the following additional features
19
20 (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
21 capability (RWW)
22 (+) Dual bank memory organization
23 (+) Dual boot mode
24
25 ##### How to use this driver #####
26 ==============================================================================
27 [..] This driver provides functions to configure and program the FLASH memory
28 of all STM32F7xx devices. It includes
29 (#) FLASH Memory Erase functions:
30 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
31 HAL_FLASH_Lock() functions
32 (++) Erase function: Erase sector, erase all sectors
33 (++) There are two modes of erase :
34 (+++) Polling Mode using HAL_FLASHEx_Erase()
35 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
36
37 (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
38 (++) Set/Reset the write protection
39 (++) Set the Read protection Level
40 (++) Set the BOR level
41 (++) Program the user Option Bytes
42
43 @endverbatim
44 ******************************************************************************
45 * @attention
46 *
47 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
48 *
49 * Redistribution and use in source and binary forms, with or without modification,
50 * are permitted provided that the following conditions are met:
51 * 1. Redistributions of source code must retain the above copyright notice,
52 * this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright notice,
54 * this list of conditions and the following disclaimer in the documentation
55 * and/or other materials provided with the distribution.
56 * 3. Neither the name of STMicroelectronics nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
61 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
67 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
68 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 *
71 ******************************************************************************
72 */
73
74/* Includes ------------------------------------------------------------------*/
75//#include "stm32f7xx_hal.h"
77#include "stm32f7xx_hal_flash.h"
78
79#define assert_param(expr) ((void)0)
80
81/** @addtogroup STM32F7xx_HAL_Driver
82 * @{
83 */
84
85/** @defgroup FLASHEx FLASHEx
86 * @brief FLASH HAL Extension module driver
87 * @{
88 */
89
90#define HAL_FLASH_MODULE_ENABLED
91
92#ifdef HAL_FLASH_MODULE_ENABLED
93
94/* Private typedef -----------------------------------------------------------*/
95/* Private define ------------------------------------------------------------*/
96/** @addtogroup FLASHEx_Private_Constants
97 * @{
98 */
99#define SECTOR_MASK 0xFFFFFF07U
100#define FLASH_TIMEOUT_VALUE 50000U/* 50 s */
101/**
102 * @}
103 */
104
105/* Private macro -------------------------------------------------------------*/
106/* Private variables ---------------------------------------------------------*/
107/** @addtogroup FLASHEx_Private_Variables
108 * @{
109 */
111/**
112 * @}
113 */
114
115/* Private function prototypes -----------------------------------------------*/
116/** @addtogroup FLASHEx_Private_Functions
117 * @{
118 */
119/* Option bytes control */
120static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector);
121static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector);
122static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
123static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
124static HAL_StatusTypeDef FLASH_OB_BootAddressConfig(uint32_t BootOption, uint32_t Address);
125static uint32_t FLASH_OB_GetUser(void);
126static uint32_t FLASH_OB_GetWRP(void);
127static uint8_t FLASH_OB_GetRDP(void);
128static uint32_t FLASH_OB_GetBOR(void);
129static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption);
130
131#if defined (FLASH_OPTCR_nDBANK)
132static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
133static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \
134 uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot);
135#else
136static void FLASH_MassErase(uint8_t VoltageRange);
137static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, uint32_t Iwdgstdby);
138#endif /* FLASH_OPTCR_nDBANK */
139
140#if defined (FLASH_OPTCR2_PCROP)
141static HAL_StatusTypeDef FLASH_OB_PCROP_Config(uint32_t PCROPSector);
142static HAL_StatusTypeDef FLASH_OB_PCROP_RDP_Config(uint32_t Pcrop_Rdp);
143static uint32_t FLASH_OB_GetPCROP(void);
144static uint32_t FLASH_OB_GetPCROPRDP(void);
145#endif /* FLASH_OPTCR2_PCROP */
146
147extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
148/**
149 * @}
150 */
151
152/* Exported functions --------------------------------------------------------*/
153/** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
154 * @{
155 */
156
157/** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
158 * @brief Extended IO operation functions
159 *
160@verbatim
161 ===============================================================================
162 ##### Extended programming operation functions #####
163 ===============================================================================
164 [..]
165 This subsection provides a set of functions allowing to manage the Extension FLASH
166 programming operations Operations.
167
168@endverbatim
169 * @{
170 */
171/**
172 * @brief Perform a mass erase or erase the specified FLASH memory sectors
173 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
174 * contains the configuration information for the erasing.
175 *
176 * @param[out] SectorError: pointer to variable that
177 * contains the configuration information on faulty sector in case of error
178 * (0xFFFFFFFF means that all the sectors have been correctly erased)
179 *
180 * @retval HAL Status
181 */
182HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
183{
184 HAL_StatusTypeDef status = HAL_ERROR;
185 uint32_t index = 0;
186
187 /* Process Locked */
188 __HAL_LOCK(&pFlash);
189
190 /* Check the parameters */
191 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
192
193 /* Wait for last operation to be completed */
194 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
195
196 if(status == HAL_OK)
197 {
198 /*Initialization of SectorError variable*/
199 *SectorError = 0xFFFFFFFFU;
200
201 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
202 {
203 /*Mass erase to be done*/
204#if defined (FLASH_OPTCR_nDBANK)
205 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
206#else
207 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange);
208#endif /* FLASH_OPTCR_nDBANK */
209
210 /* Wait for last operation to be completed */
211 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
212
213 /* if the erase operation is completed, disable the MER Bit */
214 FLASH->CR &= (~FLASH_MER_BIT);
215 }
216 else
217 {
218 /* Check the parameters */
219 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
220
221 /* Erase by sector by sector to be done*/
222 for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
223 {
224 FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);
225
226 /* Wait for last operation to be completed */
227 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
228
229 /* If the erase operation is completed, disable the SER Bit and SNB Bits */
230 CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
231
232 if(status != HAL_OK)
233 {
234 /* In case of error, stop erase procedure and return the faulty sector*/
235 *SectorError = index;
236 break;
237 }
238 }
239 }
240 }
241
242 /* Process Unlocked */
243 __HAL_UNLOCK(&pFlash);
244
245 return status;
246}
247
248/**
249 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
250 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
251 * contains the configuration information for the erasing.
252 *
253 * @retval HAL Status
254 */
255HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
256{
257 HAL_StatusTypeDef status = HAL_OK;
258
259 /* Process Locked */
260 __HAL_LOCK(&pFlash);
261
262 /* Check the parameters */
263 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
264
265 /* Enable End of FLASH Operation interrupt */
266 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
267
268 /* Enable Error source interrupt */
269 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
270
271 /* Clear pending flags (if any) */
272 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
273 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_ERSERR);
274
275 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
276 {
277 /*Mass erase to be done*/
278 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
279#if defined (FLASH_OPTCR_nDBANK)
280 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
281#else
282 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange);
283#endif /* FLASH_OPTCR_nDBANK */
284 }
285 else
286 {
287 /* Erase by sector to be done*/
288
289 /* Check the parameters */
290 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
291
292 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
293 pFlash.NbSectorsToErase = pEraseInit->NbSectors;
294 pFlash.Sector = pEraseInit->Sector;
295 pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
296
297 /*Erase 1st sector and wait for IT*/
298 FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
299 }
300
301 return status;
302}
303
304/**
305 * @brief Program option bytes
306 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
307 * contains the configuration information for the programming.
308 *
309 * @retval HAL Status
310 */
311HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
312{
313 HAL_StatusTypeDef status = HAL_ERROR;
314
315 /* Process Locked */
316 __HAL_LOCK(&pFlash);
317
318 /* Check the parameters */
319 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
320
321 /* Write protection configuration */
322 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
323 {
324 assert_param(IS_WRPSTATE(pOBInit->WRPState));
325 if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
326 {
327 /*Enable of Write protection on the selected Sector*/
328 status = FLASH_OB_EnableWRP(pOBInit->WRPSector);
329 }
330 else
331 {
332 /*Disable of Write protection on the selected Sector*/
333 status = FLASH_OB_DisableWRP(pOBInit->WRPSector);
334 }
335 }
336
337 /* Read protection configuration */
338 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
339 {
340 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
341 }
342
343 /* USER configuration */
344 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
345 {
346#if defined (FLASH_OPTCR_nDBANK)
347 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_WWDG_SW,
348 pOBInit->USERConfig & OB_IWDG_SW,
349 pOBInit->USERConfig & OB_STOP_NO_RST,
350 pOBInit->USERConfig & OB_STDBY_NO_RST,
351 pOBInit->USERConfig & OB_IWDG_STOP_ACTIVE,
352 pOBInit->USERConfig & OB_IWDG_STDBY_ACTIVE,
353 pOBInit->USERConfig & OB_NDBANK_SINGLE_BANK,
354 pOBInit->USERConfig & OB_DUAL_BOOT_DISABLE);
355#else
356 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_WWDG_SW,
357 pOBInit->USERConfig & OB_IWDG_SW,
358 pOBInit->USERConfig & OB_STOP_NO_RST,
359 pOBInit->USERConfig & OB_STDBY_NO_RST,
360 pOBInit->USERConfig & OB_IWDG_STOP_ACTIVE,
361 pOBInit->USERConfig & OB_IWDG_STDBY_ACTIVE);
362#endif /* FLASH_OPTCR_nDBANK */
363 }
364
365 /* BOR Level configuration */
366 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
367 {
368 status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
369 }
370
371 /* Boot 0 Address configuration */
372 if((pOBInit->OptionType & OPTIONBYTE_BOOTADDR_0) == OPTIONBYTE_BOOTADDR_0)
373 {
374 status = FLASH_OB_BootAddressConfig(OPTIONBYTE_BOOTADDR_0, pOBInit->BootAddr0);
375 }
376
377 /* Boot 1 Address configuration */
378 if((pOBInit->OptionType & OPTIONBYTE_BOOTADDR_1) == OPTIONBYTE_BOOTADDR_1)
379 {
380 status = FLASH_OB_BootAddressConfig(OPTIONBYTE_BOOTADDR_1, pOBInit->BootAddr1);
381 }
382
383#if defined (FLASH_OPTCR2_PCROP)
384 /* PCROP configuration */
385 if((pOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
386 {
387 status = FLASH_OB_PCROP_Config(pOBInit->PCROPSector);
388 }
389
390 /* PCROP_RDP configuration */
391 if((pOBInit->OptionType & OPTIONBYTE_PCROP_RDP) == OPTIONBYTE_PCROP_RDP)
392 {
393 status = FLASH_OB_PCROP_RDP_Config(pOBInit->PCROPRdp);
394 }
395#endif /* FLASH_OPTCR2_PCROP */
396
397 /* Process Unlocked */
398 __HAL_UNLOCK(&pFlash);
399
400 return status;
401}
402
403/**
404 * @brief Get the Option byte configuration
405 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
406 * contains the configuration information for the programming.
407 *
408 * @retval None
409 */
411{
412 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER |\
413 OPTIONBYTE_BOR | OPTIONBYTE_BOOTADDR_0 | OPTIONBYTE_BOOTADDR_1;
414
415 /*Get WRP*/
416 pOBInit->WRPSector = FLASH_OB_GetWRP();
417
418 /*Get RDP Level*/
419 pOBInit->RDPLevel = FLASH_OB_GetRDP();
420
421 /*Get USER*/
422 pOBInit->USERConfig = FLASH_OB_GetUser();
423
424 /*Get BOR Level*/
425 pOBInit->BORLevel = FLASH_OB_GetBOR();
426
427 /*Get Boot Address when Boot pin = 0 */
428 pOBInit->BootAddr0 = FLASH_OB_GetBootAddress(OPTIONBYTE_BOOTADDR_0);
429
430 /*Get Boot Address when Boot pin = 1 */
431 pOBInit->BootAddr1 = FLASH_OB_GetBootAddress(OPTIONBYTE_BOOTADDR_1);
432
433#if defined (FLASH_OPTCR2_PCROP)
434 /*Get PCROP Sectors */
435 pOBInit->PCROPSector = FLASH_OB_GetPCROP();
436
437 /*Get PCROP_RDP Value */
438 pOBInit->PCROPRdp = FLASH_OB_GetPCROPRDP();
439#endif /* FLASH_OPTCR2_PCROP */
440}
441/**
442 * @}
443 */
444
445#if defined (FLASH_OPTCR_nDBANK)
446/**
447 * @brief Full erase of FLASH memory sectors
448 * @param VoltageRange: The device voltage range which defines the erase parallelism.
449 * This parameter can be one of the following values:
450 * @arg VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
451 * the operation will be done by byte (8-bit)
452 * @arg VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
453 * the operation will be done by half word (16-bit)
454 * @arg VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
455 * the operation will be done by word (32-bit)
456 * @arg VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
457 * the operation will be done by double word (64-bit)
458 * @param Banks: Banks to be erased
459 * This parameter can be one of the following values:
460 * @arg FLASH_BANK_1: Bank1 to be erased
461 * @arg FLASH_BANK_2: Bank2 to be erased
462 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
463 *
464 * @retval HAL Status
465 */
466static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
467{
468 /* Check the parameters */
469 assert_param(IS_VOLTAGERANGE(VoltageRange));
470 assert_param(IS_FLASH_BANK(Banks));
471
472 /* if the previous operation is completed, proceed to erase all sectors */
473 FLASH->CR &= CR_PSIZE_MASK;
474 if(Banks == FLASH_BANK_BOTH)
475 {
476 /* bank1 & bank2 will be erased*/
477 FLASH->CR |= FLASH_MER_BIT;
478 }
479 else if(Banks == FLASH_BANK_2)
480 {
481 /*Only bank2 will be erased*/
482 FLASH->CR |= FLASH_CR_MER2;
483 }
484 else
485 {
486 /*Only bank1 will be erased*/
487 FLASH->CR |= FLASH_CR_MER1;
488 }
489 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8);
490 /* Data synchronous Barrier (DSB) Just after the write operation
491 This will force the CPU to respect the sequence of instruction (no optimization).*/
492 __DSB();
493}
494
495/**
496 * @brief Erase the specified FLASH memory sector
497 * @param Sector: FLASH sector to erase
498 * The value of this parameter depend on device used within the same series
499 * @param VoltageRange: The device voltage range which defines the erase parallelism.
500 * This parameter can be one of the following values:
501 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
502 * the operation will be done by byte (8-bit)
503 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
504 * the operation will be done by half word (16-bit)
505 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
506 * the operation will be done by word (32-bit)
507 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
508 * the operation will be done by double word (64-bit)
509 *
510 * @retval None
511 */
512void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
513{
514 uint32_t tmp_psize = 0;
515
516 /* Check the parameters */
517 assert_param(IS_FLASH_SECTOR(Sector));
518 assert_param(IS_VOLTAGERANGE(VoltageRange));
519
520 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
521 {
522 tmp_psize = FLASH_PSIZE_BYTE;
523 }
524 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
525 {
526 tmp_psize = FLASH_PSIZE_HALF_WORD;
527 }
528 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
529 {
530 tmp_psize = FLASH_PSIZE_WORD;
531 }
532 else
533 {
534 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
535 }
536
537 /* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */
538 if(Sector > FLASH_SECTOR_11)
539 {
540 Sector += 4;
541 }
542
543 /* If the previous operation is completed, proceed to erase the sector */
544 FLASH->CR &= CR_PSIZE_MASK;
545 FLASH->CR |= tmp_psize;
546 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
547 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
548 FLASH->CR |= FLASH_CR_STRT;
549
550 /* Data synchronous Barrier (DSB) Just after the write operation
551 This will force the CPU to respect the sequence of instruction (no optimization).*/
552 __DSB();
553}
554
555/**
556 * @brief Return the FLASH Write Protection Option Bytes value.
557 * @retval uint32_t FLASH Write Protection Option Bytes value
558 */
559static uint32_t FLASH_OB_GetWRP(void)
560{
561 /* Return the FLASH write protection Register value */
562 return ((uint32_t)(FLASH->OPTCR & 0x0FFF0000));
563}
564
565/**
566 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
567 * @param Wwdg: Selects the IWDG mode
568 * This parameter can be one of the following values:
569 * @arg OB_WWDG_SW: Software WWDG selected
570 * @arg OB_WWDG_HW: Hardware WWDG selected
571 * @param Iwdg: Selects the WWDG mode
572 * This parameter can be one of the following values:
573 * @arg OB_IWDG_SW: Software IWDG selected
574 * @arg OB_IWDG_HW: Hardware IWDG selected
575 * @param Stop: Reset event when entering STOP mode.
576 * This parameter can be one of the following values:
577 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
578 * @arg OB_STOP_RST: Reset generated when entering in STOP
579 * @param Stdby: Reset event when entering Standby mode.
580 * This parameter can be one of the following values:
581 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
582 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
583 * @param Iwdgstop: Independent watchdog counter freeze in Stop mode.
584 * This parameter can be one of the following values:
585 * @arg OB_IWDG_STOP_FREEZE: Freeze IWDG counter in STOP
586 * @arg OB_IWDG_STOP_ACTIVE: IWDG counter active in STOP
587 * @param Iwdgstdby: Independent watchdog counter freeze in standby mode.
588 * This parameter can be one of the following values:
589 * @arg OB_IWDG_STDBY_FREEZE: Freeze IWDG counter in STANDBY
590 * @arg OB_IWDG_STDBY_ACTIVE: IWDG counter active in STANDBY
591 * @param NDBank: Flash Single Bank mode enabled.
592 * This parameter can be one of the following values:
593 * @arg OB_NDBANK_SINGLE_BANK: enable 256 bits mode (Flash is a single bank)
594 * @arg OB_NDBANK_DUAL_BANK: disable 256 bits mode (Flash is a dual bank in 128 bits mode)
595 * @param NDBoot: Flash Dual boot mode disable.
596 * This parameter can be one of the following values:
597 * @arg OB_DUAL_BOOT_DISABLE: Disable Dual Boot
598 * @arg OB_DUAL_BOOT_ENABLE: Enable Dual Boot
599
600 * @retval HAL Status
601 */
602static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \
603 uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot)
604{
605 uint32_t useroptionmask = 0x00;
606 uint32_t useroptionvalue = 0x00;
607
608 HAL_StatusTypeDef status = HAL_OK;
609
610 /* Check the parameters */
611 assert_param(IS_OB_WWDG_SOURCE(Wwdg));
612 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
613 assert_param(IS_OB_STOP_SOURCE(Stop));
614 assert_param(IS_OB_STDBY_SOURCE(Stdby));
615 assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop));
616 assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby));
617 assert_param(IS_OB_NDBANK(NDBank));
618 assert_param(IS_OB_NDBOOT(NDBoot));
619
620 /* Wait for last operation to be completed */
621 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
622
623 if(status == HAL_OK)
624 {
625 useroptionmask = (FLASH_OPTCR_WWDG_SW | FLASH_OPTCR_IWDG_SW | FLASH_OPTCR_nRST_STOP | \
626 FLASH_OPTCR_nRST_STDBY | FLASH_OPTCR_IWDG_STOP | FLASH_OPTCR_IWDG_STDBY | \
627 FLASH_OPTCR_nDBOOT | FLASH_OPTCR_nDBANK);
628
629 useroptionvalue = (Iwdg | Wwdg | Stop | Stdby | Iwdgstop | Iwdgstdby | NDBoot | NDBank);
630
631 /* Update User Option Byte */
632 MODIFY_REG(FLASH->OPTCR, useroptionmask, useroptionvalue);
633 }
634
635 return status;
636}
637
638/**
639 * @brief Return the FLASH User Option Byte value.
640 * @retval uint32_t FLASH User Option Bytes values: WWDG_SW(Bit4), IWDG_SW(Bit5), nRST_STOP(Bit6),
641 * nRST_STDBY(Bit7), nDBOOT(Bit28), nDBANK(Bit29), IWDG_STDBY(Bit30) and IWDG_STOP(Bit31).
642 */
643static uint32_t FLASH_OB_GetUser(void)
644{
645 /* Return the User Option Byte */
646 return ((uint32_t)(FLASH->OPTCR & 0xF00000F0U));
647}
648#else
649
650/**
651 * @brief Full erase of FLASH memory sectors
652 * @param VoltageRange: The device voltage range which defines the erase parallelism.
653 * This parameter can be one of the following values:
654 * @arg VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
655 * the operation will be done by byte (8-bit)
656 * @arg VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
657 * the operation will be done by half word (16-bit)
658 * @arg VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
659 * the operation will be done by word (32-bit)
660 * @arg VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
661 * the operation will be done by double word (64-bit)
662 *
663 * @retval HAL Status
664 */
665static void FLASH_MassErase(uint8_t VoltageRange)
666{
667 /* Check the parameters */
668 assert_param(IS_VOLTAGERANGE(VoltageRange));
669
670 /* if the previous operation is completed, proceed to erase all sectors */
671 FLASH->CR &= CR_PSIZE_MASK;
672 FLASH->CR |= FLASH_CR_MER;
673 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8);
674 /* Data synchronous Barrier (DSB) Just after the write operation
675 This will force the CPU to respect the sequence of instruction (no optimization).*/
676 __DSB();
677}
678
679/**
680 * @brief Erase the specified FLASH memory sector
681 * @param Sector: FLASH sector to erase
682 * The value of this parameter depend on device used within the same series
683 * @param VoltageRange: The device voltage range which defines the erase parallelism.
684 * This parameter can be one of the following values:
685 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
686 * the operation will be done by byte (8-bit)
687 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
688 * the operation will be done by half word (16-bit)
689 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
690 * the operation will be done by word (32-bit)
691 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
692 * the operation will be done by double word (64-bit)
693 *
694 * @retval None
695 */
696void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
697{
698 uint32_t tmp_psize = 0;
699
700 /* Check the parameters */
701 assert_param(IS_FLASH_SECTOR(Sector));
702 assert_param(IS_VOLTAGERANGE(VoltageRange));
703
704 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
705 {
706 tmp_psize = FLASH_PSIZE_BYTE;
707 }
708 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
709 {
710 tmp_psize = FLASH_PSIZE_HALF_WORD;
711 }
712 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
713 {
714 tmp_psize = FLASH_PSIZE_WORD;
715 }
716 else
717 {
718 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
719 }
720
721 /* If the previous operation is completed, proceed to erase the sector */
722 FLASH->CR &= CR_PSIZE_MASK;
723 FLASH->CR |= tmp_psize;
724 FLASH->CR &= SECTOR_MASK;
725 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
726 FLASH->CR |= FLASH_CR_STRT;
727
728 /* Data synchronous Barrier (DSB) Just after the write operation
729 This will force the CPU to respect the sequence of instruction (no optimization).*/
730 __DSB();
731}
732
733/**
734 * @brief Return the FLASH Write Protection Option Bytes value.
735 * @retval uint32_t FLASH Write Protection Option Bytes value
736 */
737static uint32_t FLASH_OB_GetWRP(void)
738{
739 /* Return the FLASH write protection Register value */
740 return ((uint32_t)(FLASH->OPTCR & 0x00FF0000));
741}
742
743/**
744 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
745 * @param Wwdg: Selects the IWDG mode
746 * This parameter can be one of the following values:
747 * @arg OB_WWDG_SW: Software WWDG selected
748 * @arg OB_WWDG_HW: Hardware WWDG selected
749 * @param Iwdg: Selects the WWDG mode
750 * This parameter can be one of the following values:
751 * @arg OB_IWDG_SW: Software IWDG selected
752 * @arg OB_IWDG_HW: Hardware IWDG selected
753 * @param Stop: Reset event when entering STOP mode.
754 * This parameter can be one of the following values:
755 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
756 * @arg OB_STOP_RST: Reset generated when entering in STOP
757 * @param Stdby: Reset event when entering Standby mode.
758 * This parameter can be one of the following values:
759 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
760 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
761 * @param Iwdgstop: Independent watchdog counter freeze in Stop mode.
762 * This parameter can be one of the following values:
763 * @arg OB_IWDG_STOP_FREEZE: Freeze IWDG counter in STOP
764 * @arg OB_IWDG_STOP_ACTIVE: IWDG counter active in STOP
765 * @param Iwdgstdby: Independent watchdog counter freeze in standby mode.
766 * This parameter can be one of the following values:
767 * @arg OB_IWDG_STDBY_FREEZE: Freeze IWDG counter in STANDBY
768 * @arg OB_IWDG_STDBY_ACTIVE: IWDG counter active in STANDBY
769 * @retval HAL Status
770 */
771static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, uint32_t Iwdgstdby)
772{
773 uint32_t useroptionmask = 0x00;
774 uint32_t useroptionvalue = 0x00;
775
776 HAL_StatusTypeDef status = HAL_OK;
777
778 /* Check the parameters */
779 assert_param(IS_OB_WWDG_SOURCE(Wwdg));
780 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
781 assert_param(IS_OB_STOP_SOURCE(Stop));
782 assert_param(IS_OB_STDBY_SOURCE(Stdby));
783 assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop));
784 assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby));
785
786 /* Wait for last operation to be completed */
787 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
788
789 if(status == HAL_OK)
790 {
791 useroptionmask = (FLASH_OPTCR_WWDG_SW | FLASH_OPTCR_IWDG_SW | FLASH_OPTCR_nRST_STOP | \
792 FLASH_OPTCR_nRST_STDBY | FLASH_OPTCR_IWDG_STOP | FLASH_OPTCR_IWDG_STDBY);
793
794 useroptionvalue = (Iwdg | Wwdg | Stop | Stdby | Iwdgstop | Iwdgstdby);
795
796 /* Update User Option Byte */
797 MODIFY_REG(FLASH->OPTCR, useroptionmask, useroptionvalue);
798 }
799
800 return status;
801
802}
803
804/**
805 * @brief Return the FLASH User Option Byte value.
806 * @retval uint32_t FLASH User Option Bytes values: WWDG_SW(Bit4), IWDG_SW(Bit5), nRST_STOP(Bit6),
807 * nRST_STDBY(Bit7), IWDG_STDBY(Bit30) and IWDG_STOP(Bit31).
808 */
809static uint32_t FLASH_OB_GetUser(void)
810{
811 /* Return the User Option Byte */
812 return ((uint32_t)(FLASH->OPTCR & 0xC00000F0U));
813}
814#endif /* FLASH_OPTCR_nDBANK */
815
816/**
817 * @brief Enable the write protection of the desired bank1 or bank2 sectors
818 *
819 * @note When the memory read protection level is selected (RDP level = 1),
820 * it is not possible to program or erase the flash sector i if CortexM7
821 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
822 *
823 * @param WRPSector: specifies the sector(s) to be write protected.
824 * This parameter can be one of the following values:
825 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_7 (for STM32F74xxx/STM32F75xxx devices)
826 * or a value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_11 (in Single Bank mode for STM32F76xxx/STM32F77xxx devices)
827 * or a value between OB_WRP_DB_SECTOR_0 and OB_WRP_DB_SECTOR_23 (in Dual Bank mode for STM32F76xxx/STM32F77xxx devices)
828 * @arg OB_WRP_SECTOR_All
829 *
830 * @retval HAL FLASH State
831 */
832static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector)
833{
834 HAL_StatusTypeDef status = HAL_OK;
835
836 /* Check the parameters */
837 assert_param(IS_OB_WRP_SECTOR(WRPSector));
838
839 /* Wait for last operation to be completed */
840 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
841
842 if(status == HAL_OK)
843 {
844 /*Write protection enabled on sectors */
845 FLASH->OPTCR &= (~WRPSector);
846 }
847
848 return status;
849}
850
851/**
852 * @brief Disable the write protection of the desired bank1 or bank 2 sectors
853 *
854 * @note When the memory read protection level is selected (RDP level = 1),
855 * it is not possible to program or erase the flash sector i if CortexM4
856 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
857 *
858 * @param WRPSector: specifies the sector(s) to be write protected.
859 * This parameter can be one of the following values:
860 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_7 (for STM32F74xxx/STM32F75xxx devices)
861 * or a value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_11 (in Single Bank mode for STM32F76xxx/STM32F77xxx devices)
862 * or a value between OB_WRP_DB_SECTOR_0 and OB_WRP_DB_SECTOR_23 (in Dual Bank mode for STM32F76xxx/STM32F77xxx devices)
863 * @arg OB_WRP_Sector_All
864 *
865 *
866 * @retval HAL Status
867 */
868static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector)
869{
870 HAL_StatusTypeDef status = HAL_OK;
871
872 /* Check the parameters */
873 assert_param(IS_OB_WRP_SECTOR(WRPSector));
874
875 /* Wait for last operation to be completed */
876 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
877
878 if(status == HAL_OK)
879 {
880 /* Write protection disabled on sectors */
881 FLASH->OPTCR |= (WRPSector);
882 }
883
884 return status;
885}
886
887/**
888 * @brief Set the read protection level.
889 * @param Level: specifies the read protection level.
890 * This parameter can be one of the following values:
891 * @arg OB_RDP_LEVEL_0: No protection
892 * @arg OB_RDP_LEVEL_1: Read protection of the memory
893 * @arg OB_RDP_LEVEL_2: Full chip protection
894 *
895 * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
896 *
897 * @retval HAL Status
898 */
899static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
900{
901 HAL_StatusTypeDef status = HAL_OK;
902
903 /* Check the parameters */
904 assert_param(IS_OB_RDP_LEVEL(Level));
905
906 /* Wait for last operation to be completed */
907 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
908
909 if(status == HAL_OK)
910 {
911 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
912 }
913
914 return status;
915}
916
917/**
918 * @brief Set the BOR Level.
919 * @param Level: specifies the Option Bytes BOR Reset Level.
920 * This parameter can be one of the following values:
921 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
922 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
923 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
924 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
925 * @retval HAL Status
926 */
927static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
928{
929 /* Check the parameters */
930 assert_param(IS_OB_BOR_LEVEL(Level));
931
932 /* Set the BOR Level */
933 MODIFY_REG(FLASH->OPTCR, FLASH_OPTCR_BOR_LEV, Level);
934
935 return HAL_OK;
936
937}
938
939/**
940 * @brief Configure Boot base address.
941 *
942 * @param BootOption : specifies Boot base address depending from Boot pin = 0 or pin = 1
943 * This parameter can be one of the following values:
944 * @arg OPTIONBYTE_BOOTADDR_0 : Boot address based when Boot pin = 0
945 * @arg OPTIONBYTE_BOOTADDR_1 : Boot address based when Boot pin = 1
946 * @param Address: specifies Boot base address
947 * This parameter can be one of the following values:
948 * @arg OB_BOOTADDR_ITCM_RAM : Boot from ITCM RAM (0x00000000)
949 * @arg OB_BOOTADDR_SYSTEM : Boot from System memory bootloader (0x00100000)
950 * @arg OB_BOOTADDR_ITCM_FLASH : Boot from Flash on ITCM interface (0x00200000)
951 * @arg OB_BOOTADDR_AXIM_FLASH : Boot from Flash on AXIM interface (0x08000000)
952 * @arg OB_BOOTADDR_DTCM_RAM : Boot from DTCM RAM (0x20000000)
953 * @arg OB_BOOTADDR_SRAM1 : Boot from SRAM1 (0x20010000)
954 * @arg OB_BOOTADDR_SRAM2 : Boot from SRAM2 (0x2004C000)
955 *
956 * @retval HAL Status
957 */
958static HAL_StatusTypeDef FLASH_OB_BootAddressConfig(uint32_t BootOption, uint32_t Address)
959{
960 HAL_StatusTypeDef status = HAL_OK;
961
962 /* Check the parameters */
963 assert_param(IS_OB_BOOT_ADDRESS(Address));
964
965 /* Wait for last operation to be completed */
966 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
967
968 if(status == HAL_OK)
969 {
970 if(BootOption == OPTIONBYTE_BOOTADDR_0)
971 {
972 MODIFY_REG(FLASH->OPTCR1, FLASH_OPTCR1_BOOT_ADD0, Address);
973 }
974 else
975 {
976 MODIFY_REG(FLASH->OPTCR1, FLASH_OPTCR1_BOOT_ADD1, (Address << 16));
977 }
978 }
979
980 return status;
981}
982
983/**
984 * @brief Returns the FLASH Read Protection level.
985 * @retval FlagStatus FLASH ReadOut Protection Status:
986 * This parameter can be one of the following values:
987 * @arg OB_RDP_LEVEL_0: No protection
988 * @arg OB_RDP_LEVEL_1: Read protection of the memory
989 * @arg OB_RDP_LEVEL_2: Full chip protection
990 */
991static uint8_t FLASH_OB_GetRDP(void)
992{
993 uint8_t readstatus = OB_RDP_LEVEL_0;
994
995 if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS)) == OB_RDP_LEVEL_0)
996 {
997 readstatus = OB_RDP_LEVEL_0;
998 }
999 else if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS)) == OB_RDP_LEVEL_2)
1000 {
1001 readstatus = OB_RDP_LEVEL_2;
1002 }
1003 else
1004 {
1005 readstatus = OB_RDP_LEVEL_1;
1006 }
1007
1008 return readstatus;
1009}
1010
1011/**
1012 * @brief Returns the FLASH BOR level.
1013 * @retval uint32_t The FLASH BOR level:
1014 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
1015 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
1016 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
1017 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
1018 */
1019static uint32_t FLASH_OB_GetBOR(void)
1020{
1021 /* Return the FLASH BOR level */
1022 return ((uint32_t)(FLASH->OPTCR & 0x0C));
1023}
1024
1025/**
1026 * @brief Configure Boot base address.
1027 *
1028 * @param BootOption : specifies Boot base address depending from Boot pin = 0 or pin = 1
1029 * This parameter can be one of the following values:
1030 * @arg OPTIONBYTE_BOOTADDR_0 : Boot address based when Boot pin = 0
1031 * @arg OPTIONBYTE_BOOTADDR_1 : Boot address based when Boot pin = 1
1032 *
1033 * @retval uint32_t Boot Base Address:
1034 * - OB_BOOTADDR_ITCM_RAM : Boot from ITCM RAM (0x00000000)
1035 * - OB_BOOTADDR_SYSTEM : Boot from System memory bootloader (0x00100000)
1036 * - OB_BOOTADDR_ITCM_FLASH : Boot from Flash on ITCM interface (0x00200000)
1037 * - OB_BOOTADDR_AXIM_FLASH : Boot from Flash on AXIM interface (0x08000000)
1038 * - OB_BOOTADDR_DTCM_RAM : Boot from DTCM RAM (0x20000000)
1039 * - OB_BOOTADDR_SRAM1 : Boot from SRAM1 (0x20010000)
1040 * - OB_BOOTADDR_SRAM2 : Boot from SRAM2 (0x2004C000)
1041 */
1042static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption)
1043{
1044 uint32_t Address = 0;
1045
1046 /* Return the Boot base Address */
1047 if(BootOption == OPTIONBYTE_BOOTADDR_0)
1048 {
1049 Address = FLASH->OPTCR1 & FLASH_OPTCR1_BOOT_ADD0;
1050 }
1051 else
1052 {
1053 Address = ((FLASH->OPTCR1 & FLASH_OPTCR1_BOOT_ADD1) >> 16);
1054 }
1055
1056 return Address;
1057}
1058
1059#if defined (FLASH_OPTCR2_PCROP)
1060/**
1061 * @brief Set the PCROP protection for sectors.
1062 * @param PCROPSector: specifies the sector(s) to be PCROP protected.
1063 * This parameter can be one of the following values:
1064 * @arg OB_PCROP_SECTOR_x: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_7
1065 * @arg OB_PCROP_SECTOR_ALL
1066 *
1067 * @retval HAL Status
1068 */
1069static HAL_StatusTypeDef FLASH_OB_PCROP_Config(uint32_t PCROPSector)
1070{
1071 HAL_StatusTypeDef status = HAL_OK;
1072
1073 /* Check the parameters */
1074 assert_param(IS_OB_PCROP_SECTOR(PCROPSector));
1075
1076 /* Wait for last operation to be completed */
1077 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1078
1079 if(status == HAL_OK)
1080 {
1081 MODIFY_REG(FLASH->OPTCR2, FLASH_OPTCR2_PCROP, PCROPSector);
1082 }
1083
1084 return status;
1085}
1086
1087/**
1088 * @brief Set the PCROP_RDP value
1089 * @param Pcrop_Rdp: specifies the PCROP_RDP bit value.
1090 *
1091 * @retval HAL Status
1092 */
1093static HAL_StatusTypeDef FLASH_OB_PCROP_RDP_Config(uint32_t Pcrop_Rdp)
1094{
1095 HAL_StatusTypeDef status = HAL_OK;
1096
1097 /* Check the parameters */
1098 assert_param(IS_OB_PCROP_RDP_VALUE(Pcrop_Rdp));
1099
1100 /* Wait for last operation to be completed */
1101 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1102
1103 if(status == HAL_OK)
1104 {
1105 MODIFY_REG(FLASH->OPTCR2, FLASH_OPTCR2_PCROP_RDP, Pcrop_Rdp);
1106 }
1107
1108 return status;
1109}
1110
1111/**
1112 * @brief Return the FLASH PCROP Protection Option Bytes value.
1113 * @retval uint32_t FLASH PCROP Protection Option Bytes value
1114 */
1115static uint32_t FLASH_OB_GetPCROP(void)
1116{
1117 /* Return the FLASH write protection Register value */
1118 return ((uint32_t)(FLASH->OPTCR2 & FLASH_OPTCR2_PCROP));
1119}
1120
1121/**
1122 * @brief Return the FLASH PCROP_RDP option byte value.
1123 * @retval uint32_t FLASH PCROP_RDP option byte value
1124 */
1125static uint32_t FLASH_OB_GetPCROPRDP(void)
1126{
1127 /* Return the FLASH write protection Register value */
1128 return ((uint32_t)(FLASH->OPTCR2 & FLASH_OPTCR2_PCROP_RDP));
1129}
1130#endif /* FLASH_OPTCR2_PCROP */
1131
1132/**
1133 * @}
1134 */
1135
1136#endif /* HAL_FLASH_MODULE_ENABLED */
1137
1138/**
1139 * @}
1140 */
1141
1142/**
1143 * @}
1144 */
1145
1146/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
Wait for a FLASH operation to complete.
HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
Program option bytes.
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
Perform a mass erase or erase the specified FLASH memory sectors.
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
Get the Option byte configuration.
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled.
static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
Set the read protection level.
static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
Set the BOR Level.
static uint8_t FLASH_OB_GetRDP(void)
Returns the FLASH Read Protection level.
static uint32_t FLASH_OB_GetWRP(void)
Return the FLASH Write Protection Option Bytes value.
static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
Full erase of FLASH memory sectors.
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
Erase the specified FLASH memory sector.
static uint32_t FLASH_OB_GetUser(void)
Return the FLASH User Option Byte value.
static uint32_t FLASH_OB_GetBOR(void)
Returns the FLASH BOR level.
static uint32_t FLASH_OB_GetPCROPRDP(void)
Return the FLASH PCROP_RDP option byte value.
static HAL_StatusTypeDef FLASH_OB_BootAddressConfig(uint32_t BootOption, uint32_t Address)
Configure Boot base address.
static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector)
Enable the write protection of the desired bank1 or bank2 sectors.
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \ uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot)
Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
static HAL_StatusTypeDef FLASH_OB_PCROP_Config(uint32_t PCROPSector)
Set the PCROP protection for sectors.
static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption)
Configure Boot base address.
static uint32_t FLASH_OB_GetPCROP(void)
Return the FLASH PCROP Protection Option Bytes value.
static HAL_StatusTypeDef FLASH_OB_PCROP_RDP_Config(uint32_t Pcrop_Rdp)
Set the PCROP_RDP value.
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector)
Disable the write protection of the desired bank1 or bank 2 sectors.
FLASH_ProcessTypeDef pFlash
Header file of FLASH HAL module.
Header file of FLASH HAL Extension module.
FLASH Option Bytes Program structure definition.
FLASH handle Structure definition
__IO FLASH_ProcedureTypeDef ProcedureOnGoing