rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stm32f4xx_hal_flash_ex.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file stm32f4xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @version V1.4.0
6 * @date 14-August-2015
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 STM32F427xx/437xx and
18 STM32F429xx/439xx 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 (+) PCROP protection for all banks
24
25 ##### How to use this driver #####
26 ==============================================================================
27 [..] This driver provides functions to configure and program the FLASH memory
28 of all STM32F427xx/437xx, STM32F429xx/439xx, STM32F469xx/479xx and STM32F446xx
29 devices. It includes
30 (#) FLASH Memory Erase functions:
31 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
32 HAL_FLASH_Lock() functions
33 (++) Erase function: Erase sector, erase all sectors
34 (++) There are two modes of erase :
35 (+++) Polling Mode using HAL_FLASHEx_Erase()
36 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
37
38 (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
39 (++) Set/Reset the write protection
40 (++) Set the Read protection Level
41 (++) Set the BOR level
42 (++) Program the user Option Bytes
43 (#) Advanced Option Bytes Programming functions: Use HAL_FLASHEx_AdvOBProgram() to :
44 (++) Extended space (bank 2) erase function
45 (++) Full FLASH space (2 Mo) erase (bank 1 and bank 2)
46 (++) Dual Boot activation
47 (++) Write protection configuration for bank 2
48 (++) PCROP protection configuration and control for both banks
49
50 @endverbatim
51 ******************************************************************************
52 * @attention
53 *
54 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
55 *
56 * Redistribution and use in source and binary forms, with or without modification,
57 * are permitted provided that the following conditions are met:
58 * 1. Redistributions of source code must retain the above copyright notice,
59 * this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright notice,
61 * this list of conditions and the following disclaimer in the documentation
62 * and/or other materials provided with the distribution.
63 * 3. Neither the name of STMicroelectronics nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
68 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
70 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
74 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
76 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77 *
78 ******************************************************************************
79 */
80
81/* Includes ------------------------------------------------------------------*/
82
83#ifdef STM32F407xx
84#define STM32F40_41xxx
85#endif
86#define assert_param(expr) ((void)0)
87
88#define HAL_FLASH_MODULE_ENABLED
89#define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))
90
91
93#include "stm32f4xx_hal_flash.h"
94
95/** @addtogroup STM32F4xx_HAL_Driver
96 * @{
97 */
98
99/** @defgroup FLASHEx FLASHEx
100 * @brief FLASH HAL Extension module driver
101 * @{
102 */
103
104#ifdef HAL_FLASH_MODULE_ENABLED
105
106/* Private typedef -----------------------------------------------------------*/
107/* Private define ------------------------------------------------------------*/
108/** @addtogroup FLASHEx_Private_Constants
109 * @{
110 */
111#define FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
112/**
113 * @}
114 */
115
116/* Private macro -------------------------------------------------------------*/
117/* Private variables ---------------------------------------------------------*/
118/** @addtogroup FLASHEx_Private_Variables
119 * @{
120 */
122/**
123 * @}
124 */
125
126/* Private function prototypes -----------------------------------------------*/
127/** @addtogroup FLASHEx_Private_Functions
128 * @{
129 */
130/* Option bytes control */
131static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
132void FLASH_FlushCaches(void);
133static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
134static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks);
135static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
136static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby);
137static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
138static uint8_t FLASH_OB_GetUser(void);
139static uint16_t FLASH_OB_GetWRP(void);
140static uint8_t FLASH_OB_GetRDP(void);
141static uint8_t FLASH_OB_GetBOR(void);
142
143#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) ||\
144 defined(STM32F446xx)
145static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector);
146static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector);
147#endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx */
148
149#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
150static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
151static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
152static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig);
153#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
154
155extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
156/**
157 * @}
158 */
159
160/* Exported functions --------------------------------------------------------*/
161/** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
162 * @{
163 */
164
165/** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
166 * @brief Extended IO operation functions
167 *
168@verbatim
169 ===============================================================================
170 ##### Extended programming operation functions #####
171 ===============================================================================
172 [..]
173 This subsection provides a set of functions allowing to manage the Extension FLASH
174 programming operations.
175
176@endverbatim
177 * @{
178 */
179/**
180 * @brief Perform a mass erase or erase the specified FLASH memory sectors
181 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
182 * contains the configuration information for the erasing.
183 *
184 * @param[out] SectorError: pointer to variable that
185 * contains the configuration information on faulty sector in case of error
186 * (0xFFFFFFFF means that all the sectors have been correctly erased)
187 *
188 * @retval HAL Status
189 */
190HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
191{
192 HAL_StatusTypeDef status = HAL_ERROR;
193 uint32_t index = 0;
194
195 /* Process Locked */
196 __HAL_LOCK(&pFlash);
197
198 /* Check the parameters */
199 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
200
201 /* Wait for last operation to be completed */
202 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
203
204 if(status == HAL_OK)
205 {
206 /*Initialization of SectorError variable*/
207 *SectorError = 0xFFFFFFFF;
208
209 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
210 {
211 /*Mass erase to be done*/
212 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
213
214 /* Wait for last operation to be completed */
215 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
216
217 /* if the erase operation is completed, disable the MER Bit */
218 FLASH->CR &= (~FLASH_MER_BIT);
219 }
220 else
221 {
222 /* Check the parameters */
223 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
224
225 /* Erase by sector by sector to be done*/
226 for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
227 {
228 FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);
229
230 /* Wait for last operation to be completed */
231 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
232
233 /* If the erase operation is completed, disable the SER and SNB Bits */
234 CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
235
236 if(status != HAL_OK)
237 {
238 /* In case of error, stop erase procedure and return the faulty sector*/
239 *SectorError = index;
240 break;
241 }
242 }
243 }
244 /* Flush the caches to be sure of the data consistency */
246 }
247
248 /* Process Unlocked */
249 __HAL_UNLOCK(&pFlash);
250
251 return status;
252}
253
254/**
255 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
256 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
257 * contains the configuration information for the erasing.
258 *
259 * @retval HAL Status
260 */
261HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
262{
263 HAL_StatusTypeDef status = HAL_OK;
264
265 /* Process Locked */
266 __HAL_LOCK(&pFlash);
267
268 /* Check the parameters */
269 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
270
271 /* Enable End of FLASH Operation interrupt */
272 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
273
274 /* Enable Error source interrupt */
275 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
276
277 /* Clear pending flags (if any) */
278 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
279 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
280
281 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
282 {
283 /*Mass erase to be done*/
284 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
285 pFlash.Bank = pEraseInit->Banks;
286 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
287 }
288 else
289 {
290 /* Erase by sector to be done*/
291
292 /* Check the parameters */
293 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
294
295 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
296 pFlash.NbSectorsToErase = pEraseInit->NbSectors;
297 pFlash.Sector = pEraseInit->Sector;
298 pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
299
300 /*Erase 1st sector and wait for IT*/
301 FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
302 }
303
304 return status;
305}
306
307/**
308 * @brief Program option bytes
309 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
310 * contains the configuration information for the programming.
311 *
312 * @retval HAL Status
313 */
315{
316 HAL_StatusTypeDef status = HAL_ERROR;
317
318 /* Process Locked */
319 __HAL_LOCK(&pFlash);
320
321 /* Check the parameters */
322 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
323
324 /*Write protection configuration*/
325 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
326 {
327 assert_param(IS_WRPSTATE(pOBInit->WRPState));
328 if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
329 {
330 /*Enable of Write protection on the selected Sector*/
331 status = FLASH_OB_EnableWRP(pOBInit->WRPSector, pOBInit->Banks);
332 }
333 else
334 {
335 /*Disable of Write protection on the selected Sector*/
336 status = FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
337 }
338 }
339
340 /*Read protection configuration*/
341 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
342 {
343 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
344 }
345
346 /*USER configuration*/
347 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
348 {
349 status = FLASH_OB_UserConfig(pOBInit->USERConfig&OB_IWDG_SW,
350 pOBInit->USERConfig&OB_STOP_NO_RST,
351 pOBInit->USERConfig&OB_STDBY_NO_RST);
352 }
353
354 /*BOR Level configuration*/
355 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
356 {
357 status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
358 }
359
360 /* Process Unlocked */
361 __HAL_UNLOCK(&pFlash);
362
363 return status;
364}
365
366/**
367 * @brief Flush the instruction and data caches
368 * @retval None
369 */
371{
372 /* Flush instruction cache */
373 if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN))
374 {
375 /* Disable instruction cache */
376 __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
377 /* Reset instruction cache */
378 __HAL_FLASH_INSTRUCTION_CACHE_RESET();
379 /* Enable instruction cache */
380 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
381 }
382
383 /* Flush data cache */
384 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN))
385 {
386 /* Disable data cache */
387 __HAL_FLASH_DATA_CACHE_DISABLE();
388 /* Reset data cache */
389 __HAL_FLASH_DATA_CACHE_RESET();
390 /* Enable data cache */
391 __HAL_FLASH_DATA_CACHE_ENABLE();
392 }
393}
394
395/**
396 * @brief Get the Option byte configuration
397 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
398 * contains the configuration information for the programming.
399 *
400 * @retval None
401 */
403{
404 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
405
406 /*Get WRP*/
407 pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP();
408
409 /*Get RDP Level*/
410 pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP();
411
412 /*Get USER*/
413 pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser();
414
415 /*Get BOR Level*/
416 pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR();
417}
418
419#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
420 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
421 defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) ||\
422 defined(STM32F479xx)
423/**
424 * @brief Program option bytes
425 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
426 * contains the configuration information for the programming.
427 *
428 * @retval HAL Status
429 */
431{
432 HAL_StatusTypeDef status = HAL_ERROR;
433
434 /* Check the parameters */
435 assert_param(IS_OBEX(pAdvOBInit->OptionType));
436
437 /*Program PCROP option byte*/
438 if(((pAdvOBInit->OptionType) & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
439 {
440 /* Check the parameters */
441 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
442 if((pAdvOBInit->PCROPState) == OB_PCROP_STATE_ENABLE)
443 {
444 /*Enable of Write protection on the selected Sector*/
445#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
446 defined(STM32F411xE) || defined(STM32F446xx)
447 status = FLASH_OB_EnablePCROP(pAdvOBInit->Sectors);
448#else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
449 status = FLASH_OB_EnablePCROP(pAdvOBInit->SectorsBank1, pAdvOBInit->SectorsBank2, pAdvOBInit->Banks);
450#endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx */
451 }
452 else
453 {
454 /*Disable of Write protection on the selected Sector*/
455#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
456 defined(STM32F411xE) || defined(STM32F446xx)
457 status = FLASH_OB_DisablePCROP(pAdvOBInit->Sectors);
458#else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
459 status = FLASH_OB_DisablePCROP(pAdvOBInit->SectorsBank1, pAdvOBInit->SectorsBank2, pAdvOBInit->Banks);
460#endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx */
461 }
462 }
463
464#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
465 /*Program BOOT config option byte*/
466 if(((pAdvOBInit->OptionType) & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
467 {
468 status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
469 }
470#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
471
472 return status;
473}
474
475/**
476 * @brief Get the OBEX byte configuration
477 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
478 * contains the configuration information for the programming.
479 *
480 * @retval None
481 */
483{
484#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
485 defined(STM32F411xE) || defined(STM32F446xx)
486 /*Get Sector*/
487 pAdvOBInit->Sectors = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
488#else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
489 /*Get Sector for Bank1*/
490 pAdvOBInit->SectorsBank1 = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
491
492 /*Get Sector for Bank2*/
493 pAdvOBInit->SectorsBank2 = (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
494
495 /*Get Boot config OB*/
496 pAdvOBInit->BootConfig = *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS;
497#endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx */
498}
499
500/**
501 * @brief Select the Protection Mode
502 *
503 * @note After PCROP activated Option Byte modification NOT POSSIBLE! excepted
504 * Global Read Out Protection modification (from level1 to level0)
505 * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
506 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
507 * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F401xx/STM32F411xx/STM32F446xx/
508 * STM32F469xx/STM32F479xx devices.
509 *
510 * @retval HAL Status
511 */
512HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
513{
514 uint8_t optiontmp = 0xFF;
515
516 /* Mask SPRMOD bit */
517 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
518
519 /* Update Option Byte */
520 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_SELECTED | optiontmp);
521
522 return HAL_OK;
523}
524
525/**
526 * @brief Deselect the Protection Mode
527 *
528 * @note After PCROP activated Option Byte modification NOT POSSIBLE! excepted
529 * Global Read Out Protection modification (from level1 to level0)
530 * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
531 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
532 * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F401xx/STM32F411xx/STM32F446xx/
533 * STM32F469xx/STM32F479xx devices.
534 *
535 * @retval HAL Status
536 */
537HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
538{
539 uint8_t optiontmp = 0xFF;
540
541 /* Mask SPRMOD bit */
542 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
543
544 /* Update Option Byte */
545 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_DESELECTED | optiontmp);
546
547 return HAL_OK;
548}
549#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F401xC || STM32F401xE || STM32F410xx ||\
550 STM32F411xE || STM32F469xx || STM32F479xx */
551
552#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
553/**
554 * @brief Returns the FLASH Write Protection Option Bytes value for Bank 2
555 * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F469xx/STM32F479xx devices.
556 * @retval The FLASH Write Protection Option Bytes value
557 */
559{
560 /* Return the FLASH write protection Register value */
561 return (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
562}
563#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
564
565/**
566 * @}
567 */
568
569#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
570/**
571 * @brief Full erase of FLASH memory sectors
572 * @param VoltageRange: The device voltage range which defines the erase parallelism.
573 * This parameter can be one of the following values:
574 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
575 * the operation will be done by byte (8-bit)
576 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
577 * the operation will be done by half word (16-bit)
578 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
579 * the operation will be done by word (32-bit)
580 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
581 * the operation will be done by double word (64-bit)
582 *
583 * @param Banks: Banks to be erased
584 * This parameter can be one of the following values:
585 * @arg FLASH_BANK_1: Bank1 to be erased
586 * @arg FLASH_BANK_2: Bank2 to be erased
587 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
588 *
589 * @retval HAL Status
590 */
591static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
592{
593 uint32_t tmp_psize = 0;
594
595 /* Check the parameters */
596 assert_param(IS_VOLTAGERANGE(VoltageRange));
597 assert_param(IS_FLASH_BANK(Banks));
598
599 /* if the previous operation is completed, proceed to erase all sectors */
600 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
601 FLASH->CR |= tmp_psize;
602 if(Banks == FLASH_BANK_BOTH)
603 {
604 /* bank1 & bank2 will be erased*/
605 FLASH->CR |= FLASH_MER_BIT;
606 }
607 else if(Banks == FLASH_BANK_1)
608 {
609 /*Only bank1 will be erased*/
610 FLASH->CR |= FLASH_CR_MER1;
611 }
612 else
613 {
614 /*Only bank2 will be erased*/
615 FLASH->CR |= FLASH_CR_MER2;
616 }
617 FLASH->CR |= FLASH_CR_STRT;
618}
619
620/**
621 * @brief Erase the specified FLASH memory sector
622 * @param Sector: FLASH sector to erase
623 * The value of this parameter depend on device used within the same series
624 * @param VoltageRange: The device voltage range which defines the erase parallelism.
625 * This parameter can be one of the following values:
626 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
627 * the operation will be done by byte (8-bit)
628 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
629 * the operation will be done by half word (16-bit)
630 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
631 * the operation will be done by word (32-bit)
632 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
633 * the operation will be done by double word (64-bit)
634 *
635 * @retval None
636 */
637void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
638{
639 uint32_t tmp_psize = 0;
640
641 /* Check the parameters */
642 assert_param(IS_FLASH_SECTOR(Sector));
643 assert_param(IS_VOLTAGERANGE(VoltageRange));
644
645 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
646 {
647 tmp_psize = FLASH_PSIZE_BYTE;
648 }
649 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
650 {
651 tmp_psize = FLASH_PSIZE_HALF_WORD;
652 }
653 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
654 {
655 tmp_psize = FLASH_PSIZE_WORD;
656 }
657 else
658 {
659 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
660 }
661
662 /* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */
663 if(Sector > FLASH_SECTOR_11)
664 {
665 Sector += 4;
666 }
667 /* If the previous operation is completed, proceed to erase the sector */
668 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
669 FLASH->CR |= tmp_psize;
670 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
671 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
672 FLASH->CR |= FLASH_CR_STRT;
673}
674
675/**
676 * @brief Enable the write protection of the desired bank1 or bank 2 sectors
677 *
678 * @note When the memory read protection level is selected (RDP level = 1),
679 * it is not possible to program or erase the flash sector i if CortexM4
680 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
681 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
682 *
683 * @param WRPSector: specifies the sector(s) to be write protected.
684 * This parameter can be one of the following values:
685 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_23
686 * @arg OB_WRP_SECTOR_All
687 * @note BANK2 starts from OB_WRP_SECTOR_12
688 *
689 * @param Banks: Enable write protection on all the sectors for the specific bank
690 * This parameter can be one of the following values:
691 * @arg FLASH_BANK_1: WRP on all sectors of bank1
692 * @arg FLASH_BANK_2: WRP on all sectors of bank2
693 * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
694 *
695 * @retval HAL FLASH State
696 */
697static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
698{
699 HAL_StatusTypeDef status = HAL_OK;
700
701 /* Check the parameters */
702 assert_param(IS_OB_WRP_SECTOR(WRPSector));
703 assert_param(IS_FLASH_BANK(Banks));
704
705 /* Wait for last operation to be completed */
706 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
707
708 if(status == HAL_OK)
709 {
710 if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
711 (WRPSector < OB_WRP_SECTOR_12))
712 {
713 if(WRPSector == OB_WRP_SECTOR_All)
714 {
715 /*Write protection on all sector of BANK1*/
716 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~(WRPSector>>12));
717 }
718 else
719 {
720 /*Write protection done on sectors of BANK1*/
721 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
722 }
723 }
724 else
725 {
726 /*Write protection done on sectors of BANK2*/
727 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
728 }
729
730 /*Write protection on all sector of BANK2*/
731 if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
732 {
733 /* Wait for last operation to be completed */
734 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
735
736 if(status == HAL_OK)
737 {
738 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
739 }
740 }
741
742 }
743 return status;
744}
745
746/**
747 * @brief Disable the write protection of the desired bank1 or bank 2 sectors
748 *
749 * @note When the memory read protection level is selected (RDP level = 1),
750 * it is not possible to program or erase the flash sector i if CortexM4
751 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
752 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
753 *
754 * @param WRPSector: specifies the sector(s) to be write protected.
755 * This parameter can be one of the following values:
756 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_23
757 * @arg OB_WRP_Sector_All
758 * @note BANK2 starts from OB_WRP_SECTOR_12
759 *
760 * @param Banks: Disable write protection on all the sectors for the specific bank
761 * This parameter can be one of the following values:
762 * @arg FLASH_BANK_1: Bank1 to be erased
763 * @arg FLASH_BANK_2: Bank2 to be erased
764 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
765 *
766 * @retval HAL Status
767 */
768static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
769{
770 HAL_StatusTypeDef status = HAL_OK;
771
772 /* Check the parameters */
773 assert_param(IS_OB_WRP_SECTOR(WRPSector));
774 assert_param(IS_FLASH_BANK(Banks));
775
776 /* Wait for last operation to be completed */
777 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
778
779 if(status == HAL_OK)
780 {
781 if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
782 (WRPSector < OB_WRP_SECTOR_12))
783 {
784 if(WRPSector == OB_WRP_SECTOR_All)
785 {
786 /*Write protection on all sector of BANK1*/
787 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
788 }
789 else
790 {
791 /*Write protection done on sectors of BANK1*/
792 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
793 }
794 }
795 else
796 {
797 /*Write protection done on sectors of BANK2*/
798 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
799 }
800
801 /*Write protection on all sector of BANK2*/
802 if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
803 {
804 /* Wait for last operation to be completed */
805 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
806
807 if(status == HAL_OK)
808 {
809 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
810 }
811 }
812
813 }
814
815 return status;
816}
817
818/**
819 * @brief Configure the Dual Bank Boot.
820 *
821 * @note This function can be used only for STM32F42xxx/43xxx devices.
822 *
823 * @param BootConfig specifies the Dual Bank Boot Option byte.
824 * This parameter can be one of the following values:
825 * @arg OB_Dual_BootEnabled: Dual Bank Boot Enable
826 * @arg OB_Dual_BootDisabled: Dual Bank Boot Disabled
827 * @retval None
828 */
829static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig)
830{
831 HAL_StatusTypeDef status = HAL_OK;
832
833 /* Check the parameters */
834 assert_param(IS_OB_BOOT(BootConfig));
835
836 /* Wait for last operation to be completed */
837 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
838
839 if(status == HAL_OK)
840 {
841 /* Set Dual Bank Boot */
842 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BFB2);
843 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= BootConfig;
844 }
845
846 return status;
847}
848
849/**
850 * @brief Enable the read/write protection (PCROP) of the desired
851 * sectors of Bank 1 and/or Bank 2.
852 * @note This function can be used only for STM32F42xxx/43xxx devices.
853 * @param SectorBank1 Specifies the sector(s) to be read/write protected or unprotected for bank1.
854 * This parameter can be one of the following values:
855 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_11
856 * @arg OB_PCROP_SECTOR__All
857 * @param SectorBank2 Specifies the sector(s) to be read/write protected or unprotected for bank2.
858 * This parameter can be one of the following values:
859 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_12 and OB_PCROP_SECTOR_23
860 * @arg OB_PCROP_SECTOR__All
861 * @param Banks Enable PCROP protection on all the sectors for the specific bank
862 * This parameter can be one of the following values:
863 * @arg FLASH_BANK_1: WRP on all sectors of bank1
864 * @arg FLASH_BANK_2: WRP on all sectors of bank2
865 * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
866 *
867 * @retval HAL Status
868 */
869static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks)
870{
871 HAL_StatusTypeDef status = HAL_OK;
872
873 assert_param(IS_FLASH_BANK(Banks));
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 if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
881 {
882 assert_param(IS_OB_PCROP(SectorBank1));
883 /*Write protection done on sectors of BANK1*/
884 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)SectorBank1;
885 }
886 else
887 {
888 assert_param(IS_OB_PCROP(SectorBank2));
889 /*Write protection done on sectors of BANK2*/
890 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
891 }
892
893 /*Write protection on all sector of BANK2*/
894 if(Banks == FLASH_BANK_BOTH)
895 {
896 assert_param(IS_OB_PCROP(SectorBank2));
897 /* Wait for last operation to be completed */
898 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
899
900 if(status == HAL_OK)
901 {
902 /*Write protection done on sectors of BANK2*/
903 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
904 }
905 }
906
907 }
908
909 return status;
910}
911
912
913/**
914 * @brief Disable the read/write protection (PCROP) of the desired
915 * sectors of Bank 1 and/or Bank 2.
916 * @note This function can be used only for STM32F42xxx/43xxx devices.
917 * @param SectorBank1 specifies the sector(s) to be read/write protected or unprotected for bank1.
918 * This parameter can be one of the following values:
919 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_11
920 * @arg OB_PCROP_SECTOR__All
921 * @param SectorBank2 Specifies the sector(s) to be read/write protected or unprotected for bank2.
922 * This parameter can be one of the following values:
923 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_12 and OB_PCROP_SECTOR_23
924 * @arg OB_PCROP_SECTOR__All
925 * @param Banks Disable PCROP protection on all the sectors for the specific bank
926 * This parameter can be one of the following values:
927 * @arg FLASH_BANK_1: WRP on all sectors of bank1
928 * @arg FLASH_BANK_2: WRP on all sectors of bank2
929 * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
930 *
931 * @retval HAL Status
932 */
933static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks)
934{
935 HAL_StatusTypeDef status = HAL_OK;
936
937 /* Check the parameters */
938 assert_param(IS_FLASH_BANK(Banks));
939
940 /* Wait for last operation to be completed */
941 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
942
943 if(status == HAL_OK)
944 {
945 if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
946 {
947 assert_param(IS_OB_PCROP(SectorBank1));
948 /*Write protection done on sectors of BANK1*/
949 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~SectorBank1);
950 }
951 else
952 {
953 /*Write protection done on sectors of BANK2*/
954 assert_param(IS_OB_PCROP(SectorBank2));
955 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
956 }
957
958 /*Write protection on all sector of BANK2*/
959 if(Banks == FLASH_BANK_BOTH)
960 {
961 assert_param(IS_OB_PCROP(SectorBank2));
962 /* Wait for last operation to be completed */
963 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
964
965 if(status == HAL_OK)
966 {
967 /*Write protection done on sectors of BANK2*/
968 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
969 }
970 }
971
972 }
973
974 return status;
975
976}
977
978#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
979
980#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
981 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
982 defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx)
983/**
984 * @brief Mass erase of FLASH memory
985 * @param VoltageRange: The device voltage range which defines the erase parallelism.
986 * This parameter can be one of the following values:
987 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
988 * the operation will be done by byte (8-bit)
989 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
990 * the operation will be done by half word (16-bit)
991 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
992 * the operation will be done by word (32-bit)
993 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
994 * the operation will be done by double word (64-bit)
995 *
996 * @param Banks: Banks to be erased
997 * This parameter can be one of the following values:
998 * @arg FLASH_BANK_1: Bank1 to be erased
999 *
1000 * @retval None
1001 */
1002static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
1003{
1004 (void)VoltageRange;
1005 (void)Banks;
1006
1007 uint32_t tmp_psize = 0;
1008
1009 /* Check the parameters */
1010 assert_param(IS_VOLTAGERANGE(VoltageRange));
1011 assert_param(IS_FLASH_BANK(Banks));
1012
1013 /* If the previous operation is completed, proceed to erase all sectors */
1014 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
1015 FLASH->CR |= tmp_psize;
1016 FLASH->CR |= FLASH_CR_MER;
1017 FLASH->CR |= FLASH_CR_STRT;
1018}
1019
1020/**
1021 * @brief Erase the specified FLASH memory sector
1022 * @param Sector: FLASH sector to erase
1023 * The value of this parameter depend on device used within the same series
1024 * @param VoltageRange: The device voltage range which defines the erase parallelism.
1025 * This parameter can be one of the following values:
1026 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
1027 * the operation will be done by byte (8-bit)
1028 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
1029 * the operation will be done by half word (16-bit)
1030 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
1031 * the operation will be done by word (32-bit)
1032 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
1033 * the operation will be done by double word (64-bit)
1034 *
1035 * @retval None
1036 */
1037void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
1038{
1039 uint32_t tmp_psize = 0;
1040
1041 /* Check the parameters */
1042 assert_param(IS_FLASH_SECTOR(Sector));
1043 assert_param(IS_VOLTAGERANGE(VoltageRange));
1044
1045 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
1046 {
1047 tmp_psize = FLASH_PSIZE_BYTE;
1048 }
1049 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
1050 {
1051 tmp_psize = FLASH_PSIZE_HALF_WORD;
1052 }
1053 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
1054 {
1055 tmp_psize = FLASH_PSIZE_WORD;
1056 }
1057 else
1058 {
1059 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
1060 }
1061
1062 /* If the previous operation is completed, proceed to erase the sector */
1063 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
1064 FLASH->CR |= tmp_psize;
1065 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
1066 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
1067 FLASH->CR |= FLASH_CR_STRT;
1068}
1069
1070/**
1071 * @brief Enable the write protection of the desired bank 1 sectors
1072 *
1073 * @note When the memory read protection level is selected (RDP level = 1),
1074 * it is not possible to program or erase the flash sector i if CortexM4
1075 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
1076 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
1077 *
1078 * @param WRPSector: specifies the sector(s) to be write protected.
1079 * The value of this parameter depend on device used within the same series
1080 *
1081 * @param Banks: Enable write protection on all the sectors for the specific bank
1082 * This parameter can be one of the following values:
1083 * @arg FLASH_BANK_1: WRP on all sectors of bank1
1084 *
1085 * @retval HAL Status
1086 */
1087static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
1088{
1089 (void)Banks;
1090
1091 HAL_StatusTypeDef status = HAL_OK;
1092
1093 /* Check the parameters */
1094 assert_param(IS_OB_WRP_SECTOR(WRPSector));
1095 assert_param(IS_FLASH_BANK(Banks));
1096
1097 /* Wait for last operation to be completed */
1098 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1099
1100 if(status == HAL_OK)
1101 {
1102 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
1103 }
1104
1105 return status;
1106}
1107
1108/**
1109 * @brief Disable the write protection of the desired bank 1 sectors
1110 *
1111 * @note When the memory read protection level is selected (RDP level = 1),
1112 * it is not possible to program or erase the flash sector i if CortexM4
1113 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
1114 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
1115 *
1116 * @param WRPSector: specifies the sector(s) to be write protected.
1117 * The value of this parameter depend on device used within the same series
1118 *
1119 * @param Banks: Enable write protection on all the sectors for the specific bank
1120 * This parameter can be one of the following values:
1121 * @arg FLASH_BANK_1: WRP on all sectors of bank1
1122 *
1123 * @retval HAL Status
1124 */
1125static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
1126{
1127 (void)Banks;
1128
1129 HAL_StatusTypeDef status = HAL_OK;
1130
1131 /* Check the parameters */
1132 assert_param(IS_OB_WRP_SECTOR(WRPSector));
1133 assert_param(IS_FLASH_BANK(Banks));
1134
1135 /* Wait for last operation to be completed */
1136 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1137
1138 if(status == HAL_OK)
1139 {
1140 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
1141 }
1142
1143 return status;
1144}
1145#endif /* STM32F40xxx || STM32F41xxx || STM32F401xx || STM32F410xx || STM32F411xE || STM32F446xx */
1146
1147#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
1148 defined(STM32F411xE) || defined(STM32F446xx)
1149/**
1150 * @brief Enable the read/write protection (PCROP) of the desired sectors.
1151 * @note This function can be used only for STM32F401xx devices.
1152 * @param Sector specifies the sector(s) to be read/write protected or unprotected.
1153 * This parameter can be one of the following values:
1154 * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector5
1155 * @arg OB_PCROP_Sector_All
1156 * @retval HAL Status
1157 */
1158static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector)
1159{
1160 HAL_StatusTypeDef status = HAL_OK;
1161
1162 /* Check the parameters */
1163 assert_param(IS_OB_PCROP(Sector));
1164
1165 /* Wait for last operation to be completed */
1166 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1167
1168 if(status == HAL_OK)
1169 {
1170 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)Sector;
1171 }
1172
1173 return status;
1174}
1175
1176
1177/**
1178 * @brief Disable the read/write protection (PCROP) of the desired sectors.
1179 * @note This function can be used only for STM32F401xx devices.
1180 * @param Sector specifies the sector(s) to be read/write protected or unprotected.
1181 * This parameter can be one of the following values:
1182 * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector5
1183 * @arg OB_PCROP_Sector_All
1184 * @retval HAL Status
1185 */
1186static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector)
1187{
1188 HAL_StatusTypeDef status = HAL_OK;
1189
1190 /* Check the parameters */
1191 assert_param(IS_OB_PCROP(Sector));
1192
1193 /* Wait for last operation to be completed */
1194 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1195
1196 if(status == HAL_OK)
1197 {
1198 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~Sector);
1199 }
1200
1201 return status;
1202
1203}
1204#endif /* STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx */
1205
1206/**
1207 * @brief Set the read protection level.
1208 * @param Level: specifies the read protection level.
1209 * This parameter can be one of the following values:
1210 * @arg OB_RDP_LEVEL_0: No protection
1211 * @arg OB_RDP_LEVEL_1: Read protection of the memory
1212 * @arg OB_RDP_LEVEL_2: Full chip protection
1213 *
1214 * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
1215 *
1216 * @retval HAL Status
1217 */
1218static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
1219{
1220 HAL_StatusTypeDef status = HAL_OK;
1221
1222 /* Check the parameters */
1223 assert_param(IS_OB_RDP_LEVEL(Level));
1224
1225 /* Wait for last operation to be completed */
1226 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1227
1228 if(status == HAL_OK)
1229 {
1230 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
1231 }
1232
1233 return status;
1234}
1235
1236/**
1237 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
1238 * @param Iwdg: Selects the IWDG mode
1239 * This parameter can be one of the following values:
1240 * @arg OB_IWDG_SW: Software IWDG selected
1241 * @arg OB_IWDG_HW: Hardware IWDG selected
1242 * @param Stop: Reset event when entering STOP mode.
1243 * This parameter can be one of the following values:
1244 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
1245 * @arg OB_STOP_RST: Reset generated when entering in STOP
1246 * @param Stdby: Reset event when entering Standby mode.
1247 * This parameter can be one of the following values:
1248 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
1249 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
1250 * @retval HAL Status
1251 */
1252static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
1253{
1254 uint8_t optiontmp = 0xFF;
1255 HAL_StatusTypeDef status = HAL_OK;
1256
1257 /* Check the parameters */
1258 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
1259 assert_param(IS_OB_STOP_SOURCE(Stop));
1260 assert_param(IS_OB_STDBY_SOURCE(Stdby));
1261
1262 /* Wait for last operation to be completed */
1263 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1264
1265 if(status == HAL_OK)
1266 {
1267 /* Mask OPTLOCK, OPTSTRT, BOR_LEV and BFB2 bits */
1268 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1F);
1269
1270 /* Update User Option Byte */
1271 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = Iwdg | (uint8_t)(Stdby | (uint8_t)(Stop | ((uint8_t)optiontmp)));
1272 }
1273
1274 return status;
1275}
1276
1277/**
1278 * @brief Set the BOR Level.
1279 * @param Level: specifies the Option Bytes BOR Reset Level.
1280 * This parameter can be one of the following values:
1281 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
1282 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
1283 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
1284 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
1285 * @retval HAL Status
1286 */
1287static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
1288{
1289 /* Check the parameters */
1290 assert_param(IS_OB_BOR_LEVEL(Level));
1291
1292 /* Set the BOR Level */
1293 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
1294 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= Level;
1295
1296 return HAL_OK;
1297
1298}
1299
1300/**
1301 * @brief Return the FLASH User Option Byte value.
1302 * @retval uint8_t FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
1303 * and RST_STDBY(Bit2).
1304 */
1305static uint8_t FLASH_OB_GetUser(void)
1306{
1307 /* Return the User Option Byte */
1308 return ((uint8_t)(FLASH->OPTCR & 0xE0));
1309}
1310
1311/**
1312 * @brief Return the FLASH Write Protection Option Bytes value.
1313 * @retval uint16_t FLASH Write Protection Option Bytes value
1314 */
1315static uint16_t FLASH_OB_GetWRP(void)
1316{
1317 /* Return the FLASH write protection Register value */
1318 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
1319}
1320
1321/**
1322 * @brief Returns the FLASH Read Protection level.
1323 * @retval FLASH ReadOut Protection Status:
1324 * This parameter can be one of the following values:
1325 * @arg OB_RDP_LEVEL_0: No protection
1326 * @arg OB_RDP_LEVEL_1: Read protection of the memory
1327 * @arg OB_RDP_LEVEL_2: Full chip protection
1328 */
1329static uint8_t FLASH_OB_GetRDP(void)
1330{
1331 uint8_t readstatus = OB_RDP_LEVEL_0;
1332
1333 if (*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_2)
1334 {
1335 readstatus = OB_RDP_LEVEL_2;
1336 }
1337 else if (*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_1)
1338 {
1339 readstatus = OB_RDP_LEVEL_1;
1340 }
1341 else
1342 {
1343 readstatus = OB_RDP_LEVEL_0;
1344 }
1345
1346 return readstatus;
1347}
1348
1349/**
1350 * @brief Returns the FLASH BOR level.
1351 * @retval uint8_t The FLASH BOR level:
1352 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
1353 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
1354 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
1355 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
1356 */
1357static uint8_t FLASH_OB_GetBOR(void)
1358{
1359 /* Return the FLASH BOR level */
1360 return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
1361}
1362
1363/**
1364 * @}
1365 */
1366
1367#endif /* HAL_FLASH_MODULE_ENABLED */
1368
1369/**
1370 * @}
1371 */
1372
1373/**
1374 * @}
1375 */
1376
1377/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
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.
uint16_t HAL_FLASHEx_OB_GetBank2WRP(void)
Returns the FLASH Write Protection Option Bytes value for Bank 2.
void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
Get the OBEX byte configuration.
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
Get the Option byte configuration.
HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
Select the Protection Mode.
HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
Deselect the Protection Mode.
HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
Program option bytes.
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled.
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
Erase the specified FLASH memory sector.
void FLASH_FlushCaches(void)
Flush the instruction and data caches.
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
Disable the write protection of the desired bank1 or bank 2 sectors.
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 uint16_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.
static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector)
Enable the read/write protection (PCROP) of the desired sectors.
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector)
Disable the read/write protection (PCROP) of the desired sectors.
static uint8_t FLASH_OB_GetUser(void)
Return the FLASH User Option Byte value.
static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
Enable the write protection of the desired bank1 or bank 2 sectors.
static uint8_t FLASH_OB_GetBOR(void)
Returns the FLASH BOR level.
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
Wait for a FLASH operation to complete.
static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig)
Configure the Dual Bank Boot.
FLASH_ProcessTypeDef pFlash
Header file of FLASH HAL module.
Header file of FLASH HAL Extension module.
FLASH Advanced Option Bytes Program structure definition.
FLASH Option Bytes Program structure definition.
FLASH handle Structure definition
__IO FLASH_ProcedureTypeDef ProcedureOnGoing