rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stm32h7xx_hal_flash_ex.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file stm32h7xx_hal_flash_ex.c
4 * @author MCD Application Team
5 * @brief Extended FLASH HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the FLASH extension peripheral:
8 * + Extended programming operations functions
9 *
10 @verbatim
11 ==============================================================================
12 ##### Flash Extension features #####
13 ==============================================================================
14
15 [..] Comparing to other previous devices, the FLASH interface for STM32H7xx
16 devices contains the following additional features
17
18 (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
19 capability (RWW)
20 (+) Dual bank memory organization
21 (+) PCROP protection for all banks
22 (+) Global readout protection (RDP)
23 (+) Write protection
24 (+) Secure access only protection
25 (+) Bank / register swapping
26 (+) Cyclic Redundancy Check (CRC)
27
28 ##### How to use this driver #####
29 ==============================================================================
30 [..] This driver provides functions to configure and program the FLASH memory
31 of all STM32H7xx devices. It includes
32 (#) FLASH Memory Erase functions:
33 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
34 HAL_FLASH_Lock() functions
35 (++) Erase function: Sector erase, bank erase and dual-bank mass erase
36 (++) There are two modes of erase :
37 (+++) Polling Mode using HAL_FLASHEx_Erase()
38 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
39
40 (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to:
41 (++) Set/Reset the write protection per bank
42 (++) Set the Read protection Level
43 (++) Set the BOR level
44 (++) Program the user Option Bytes
45 (++) PCROP protection configuration and control per bank
46 (++) Secure area configuration and control per bank
47 (++) Core Boot address configuration
48 (++) TCM / AXI shared RAM configuration
49 (++) CPU Frequency Boost configuration
50
51 (#) FLASH Memory Lock and unlock per Bank: HAL_FLASHEx_Lock_Bank1(), HAL_FLASHEx_Unlock_Bank1(),
52 HAL_FLASHEx_Lock_Bank2() and HAL_FLASHEx_Unlock_Bank2() functions
53
54 (#) FLASH CRC computation function: Use HAL_FLASHEx_ComputeCRC() to:
55 (++) Enable CRC feature
56 (++) Program the desired burst size
57 (++) Define the user Flash Area on which the CRC has be computed
58 (++) Perform the CRC computation
59 (++) Disable CRC feature
60
61 @endverbatim
62 ******************************************************************************
63 * @attention
64 *
65 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
66 * All rights reserved.</center></h2>
67 *
68 * This software component is licensed by ST under BSD 3-Clause license,
69 * the "License"; You may not use this file except in compliance with the
70 * License. You may obtain a copy of the License at:
71 * opensource.org/licenses/BSD-3-Clause
72 *
73 ******************************************************************************
74 */
75
76/* Includes ------------------------------------------------------------------*/
77//#include "stm32h7xx_hal.h"
79#include "stm32h7xx_hal_flash.h"
80
81#define assert_param(expr) ((void)0)
82
83/** @addtogroup STM32H7xx_HAL_Driver
84 * @{
85 */
86
87/** @defgroup FLASHEx FLASHEx
88 * @brief FLASH HAL Extension module driver
89 * @{
90 */
91
92#define HAL_FLASH_MODULE_ENABLED
93
94#ifdef HAL_FLASH_MODULE_ENABLED
95
96/* Private typedef -----------------------------------------------------------*/
97/* Private define ------------------------------------------------------------*/
98/** @addtogroup FLASHEx_Private_Constants
99 * @{
100 */
101#define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
102
103/**
104 * @}
105 */
106/* Private macro -------------------------------------------------------------*/
107/* Private variables ---------------------------------------------------------*/
108/* Private function prototypes -----------------------------------------------*/
109/** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
110 * @{
111 */
112static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks);
113static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
114static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Bank);
115static void FLASH_OB_GetWRP(uint32_t *WRPState, uint32_t *WRPSector, uint32_t Bank);
116static void FLASH_OB_RDPConfig(uint32_t RDPLevel);
117static uint32_t FLASH_OB_GetRDP(void);
118static void FLASH_OB_PCROPConfig(uint32_t PCROConfigRDP, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks);
119static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr,uint32_t *PCROPEndAddr, uint32_t Bank);
120static void FLASH_OB_BOR_LevelConfig(uint32_t Level);
121static uint32_t FLASH_OB_GetBOR(void);
122static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig);
123static uint32_t FLASH_OB_GetUser(void);
124static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1);
125static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1);
126static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks);
127static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank);
128static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank);
129static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank);
130
131#if defined (DUAL_CORE)
132static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1);
133static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1);
134#endif /*DUAL_CORE*/
135
136#if defined (FLASH_OTPBL_LOCKBL)
137static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block);
138static uint32_t FLASH_OB_OTP_GetLock(void);
139#endif /* FLASH_OTPBL_LOCKBL */
140
141#if defined (FLASH_OPTSR2_TCM_AXI_SHARED)
142static void FLASH_OB_SharedRAM_Config(uint32_t SharedRamConfig);
143static uint32_t FLASH_OB_SharedRAM_GetConfig(void);
144#endif /* FLASH_OPTSR2_TCM_AXI_SHARED */
145
146#if defined (FLASH_OPTSR2_CPUFREQ_BOOST)
147static void FLASH_OB_CPUFreq_BoostConfig(uint32_t FreqBoost);
148static uint32_t FLASH_OB_CPUFreq_GetBoost(void);
149#endif /* FLASH_OPTSR2_CPUFREQ_BOOST */
150/**
151 * @}
152 */
153
154/* Exported functions ---------------------------------------------------------*/
155/** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
156 * @{
157 */
158
159/** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
160 * @brief Extended IO operation functions
161 *
162@verbatim
163 ===============================================================================
164 ##### Extended programming operation functions #####
165 ===============================================================================
166 [..]
167 This subsection provides a set of functions allowing to manage the Extension FLASH
168 programming operations Operations.
169
170@endverbatim
171 * @{
172 */
173/**
174 * @brief Perform a mass erase or erase the specified FLASH memory sectors
175 * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
176 * contains the configuration information for the erasing.
177 *
178 * @param[out] SectorError pointer to variable that contains the configuration
179 * information on faulty sector in case of error (0xFFFFFFFF means that all
180 * the sectors have been correctly erased)
181 *
182 * @retval HAL Status
183 */
184HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
185{
186 HAL_StatusTypeDef status = HAL_OK;
187 uint32_t sector_index;
188
189 /* Check the parameters */
190 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
191 assert_param(IS_FLASH_BANK(pEraseInit->Banks));
192
193 /* Process Locked */
194 __HAL_LOCK(&pFlash);
195
196 /* Reset error code */
197 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
198
199 /* Wait for last operation to be completed on Bank1 */
200 if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
201 {
202 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
203 {
204 status = HAL_ERROR;
205 }
206 }
207
208#if defined (DUAL_BANK)
209 /* Wait for last operation to be completed on Bank2 */
210 if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
211 {
212 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
213 {
214 status = HAL_ERROR;
215 }
216 }
217#endif /* DUAL_BANK */
218
219 if(status == HAL_OK)
220 {
221 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
222 {
223 /* Mass erase to be done */
224 FLASH_MassErase(pEraseInit->VoltageRange, pEraseInit->Banks);
225
226 /* Wait for last operation to be completed on Bank 1 */
227 if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
228 {
229 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
230 {
231 status = HAL_ERROR;
232 }
233 /* if the erase operation is completed, disable the Bank1 BER Bit */
234 FLASH->CR1 &= (~FLASH_CR_BER);
235 }
236#if defined (DUAL_BANK)
237 /* Wait for last operation to be completed on Bank 2 */
238 if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
239 {
240 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
241 {
242 status = HAL_ERROR;
243 }
244 /* if the erase operation is completed, disable the Bank2 BER Bit */
245 FLASH->CR2 &= (~FLASH_CR_BER);
246 }
247#endif /* DUAL_BANK */
248 }
249 else
250 {
251 /*Initialization of SectorError variable*/
252 *SectorError = 0xFFFFFFFFU;
253
254 /* Erase by sector by sector to be done*/
255 for(sector_index = pEraseInit->Sector; sector_index < (pEraseInit->NbSectors + pEraseInit->Sector); sector_index++)
256 {
257 FLASH_Erase_Sector(sector_index, pEraseInit->Banks, pEraseInit->VoltageRange);
258
259 if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
260 {
261 /* Wait for last operation to be completed */
262 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
263
264 /* If the erase operation is completed, disable the SER Bit */
265 FLASH->CR1 &= (~(FLASH_CR_SER | FLASH_CR_SNB));
266 }
267#if defined (DUAL_BANK)
268 if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
269 {
270 /* Wait for last operation to be completed */
271 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
272
273 /* If the erase operation is completed, disable the SER Bit */
274 FLASH->CR2 &= (~(FLASH_CR_SER | FLASH_CR_SNB));
275 }
276#endif /* DUAL_BANK */
277
278 if(status != HAL_OK)
279 {
280 /* In case of error, stop erase procedure and return the faulty sector */
281 *SectorError = sector_index;
282 break;
283 }
284 }
285 }
286 }
287
288 /* Process Unlocked */
289 __HAL_UNLOCK(&pFlash);
290
291 return status;
292}
293
294/**
295 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
296 * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
297 * contains the configuration information for the erasing.
298 *
299 * @retval HAL Status
300 */
301HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
302{
303 HAL_StatusTypeDef status = HAL_OK;
304
305 /* Check the parameters */
306 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
307 assert_param(IS_FLASH_BANK(pEraseInit->Banks));
308
309 /* Process Locked */
310 __HAL_LOCK(&pFlash);
311
312 /* Reset error code */
313 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
314
315 /* Wait for last operation to be completed on Bank 1 */
316 if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
317 {
318 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
319 {
320 status = HAL_ERROR;
321 }
322 }
323
324#if defined (DUAL_BANK)
325 /* Wait for last operation to be completed on Bank 2 */
326 if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
327 {
328 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
329 {
330 status = HAL_ERROR;
331 }
332 }
333#endif /* DUAL_BANK */
334
335 if (status != HAL_OK)
336 {
337 /* Process Unlocked */
338 __HAL_UNLOCK(&pFlash);
339 }
340 else
341 {
342 if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
343 {
344 /* Enable End of Operation and Error interrupts for Bank 1 */
345#if defined (FLASH_CR_OPERRIE)
346 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
347 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
348#else
349 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
350 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
351#endif /* FLASH_CR_OPERRIE */
352 }
353#if defined (DUAL_BANK)
354 if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
355 {
356 /* Enable End of Operation and Error interrupts for Bank 2 */
357#if defined (FLASH_CR_OPERRIE)
358 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
359 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
360#else
361 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
362 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
363#endif /* FLASH_CR_OPERRIE */
364 }
365#endif /* DUAL_BANK */
366
367 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
368 {
369 /*Mass erase to be done*/
370 if(pEraseInit->Banks == FLASH_BANK_1)
371 {
372 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE_BANK1;
373 }
374#if defined (DUAL_BANK)
375 else if(pEraseInit->Banks == FLASH_BANK_2)
376 {
377 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE_BANK2;
378 }
379#endif /* DUAL_BANK */
380 else
381 {
382 pFlash.ProcedureOnGoing = FLASH_PROC_ALLBANK_MASSERASE;
383 }
384
385 FLASH_MassErase(pEraseInit->VoltageRange, pEraseInit->Banks);
386 }
387 else
388 {
389 /* Erase by sector to be done */
390#if defined (DUAL_BANK)
391 if(pEraseInit->Banks == FLASH_BANK_1)
392 {
393 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK1;
394 }
395 else
396 {
397 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK2;
398 }
399#else
400 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK1;
401#endif /* DUAL_BANK */
402
403 pFlash.NbSectorsToErase = pEraseInit->NbSectors;
404 pFlash.Sector = pEraseInit->Sector;
405 pFlash.VoltageForErase = pEraseInit->VoltageRange;
406
407 /* Erase first sector and wait for IT */
408 FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->Banks, pEraseInit->VoltageRange);
409 }
410 }
411
412 return status;
413}
414
415/**
416 * @brief Program option bytes
417 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
418 * contains the configuration information for the programming.
419 *
420 * @retval HAL Status
421 */
422HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
423{
424 HAL_StatusTypeDef status;
425
426 /* Check the parameters */
427 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
428
429 /* Process Locked */
430 __HAL_LOCK(&pFlash);
431
432 /* Reset Error Code */
433 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
434
435 /* Wait for last operation to be completed */
436 if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
437 {
438 status = HAL_ERROR;
439 }
440#if defined (DUAL_BANK)
441 else if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
442 {
443 status = HAL_ERROR;
444 }
445#endif /* DUAL_BANK */
446 else
447 {
448 status = HAL_OK;
449 }
450
451 if(status == HAL_OK)
452 {
453 /*Write protection configuration*/
454 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
455 {
456 assert_param(IS_WRPSTATE(pOBInit->WRPState));
457
458 if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
459 {
460 /*Enable of Write protection on the selected Sector*/
461 FLASH_OB_EnableWRP(pOBInit->WRPSector,pOBInit->Banks);
462 }
463 else
464 {
465 /*Disable of Write protection on the selected Sector*/
466 FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
467 }
468 }
469
470 /* Read protection configuration */
471 if((pOBInit->OptionType & OPTIONBYTE_RDP) != 0U)
472 {
473 /* Configure the Read protection level */
475 }
476
477 /* User Configuration */
478 if((pOBInit->OptionType & OPTIONBYTE_USER) != 0U)
479 {
480 /* Configure the user option bytes */
481 FLASH_OB_UserConfig(pOBInit->USERType, pOBInit->USERConfig);
482 }
483
484 /* PCROP Configuration */
485 if((pOBInit->OptionType & OPTIONBYTE_PCROP) != 0U)
486 {
487 assert_param(IS_FLASH_BANK(pOBInit->Banks));
488
489 /*Configure the Proprietary code readout protection */
490 FLASH_OB_PCROPConfig(pOBInit->PCROPConfig, pOBInit->PCROPStartAddr, pOBInit->PCROPEndAddr, pOBInit->Banks);
491 }
492
493 /* BOR Level configuration */
494 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
495 {
497 }
498
499#if defined(DUAL_CORE)
500 /* CM7 Boot Address configuration */
501 if((pOBInit->OptionType & OPTIONBYTE_CM7_BOOTADD) == OPTIONBYTE_CM7_BOOTADD)
502 {
503 FLASH_OB_BootAddConfig(pOBInit->BootConfig, pOBInit->BootAddr0, pOBInit->BootAddr1);
504 }
505
506 /* CM4 Boot Address configuration */
507 if((pOBInit->OptionType & OPTIONBYTE_CM4_BOOTADD) == OPTIONBYTE_CM4_BOOTADD)
508 {
510 }
511#else /* Single Core*/
512 /* Boot Address configuration */
513 if((pOBInit->OptionType & OPTIONBYTE_BOOTADD) == OPTIONBYTE_BOOTADD)
514 {
515 FLASH_OB_BootAddConfig(pOBInit->BootConfig, pOBInit->BootAddr0, pOBInit->BootAddr1);
516 }
517#endif /*DUAL_CORE*/
518
519 /* Secure area configuration */
520 if((pOBInit->OptionType & OPTIONBYTE_SECURE_AREA) == OPTIONBYTE_SECURE_AREA)
521 {
523 }
524
525#if defined(FLASH_OTPBL_LOCKBL)
526 /* OTP Block Lock configuration */
527 if((pOBInit->OptionType & OPTIONBYTE_OTP_LOCK) == OPTIONBYTE_OTP_LOCK)
528 {
530 }
531#endif /* FLASH_OTPBL_LOCKBL */
532
533#if defined(FLASH_OPTSR2_TCM_AXI_SHARED)
534 /* TCM / AXI Shared RAM configuration */
535 if((pOBInit->OptionType & OPTIONBYTE_SHARED_RAM) == OPTIONBYTE_SHARED_RAM)
536 {
538 }
539#endif /* FLASH_OPTSR2_TCM_AXI_SHARED */
540
541#if defined(FLASH_OPTSR2_CPUFREQ_BOOST)
542 /* CPU Frequency Boost configuration */
543 if((pOBInit->OptionType & OPTIONBYTE_FREQ_BOOST) == OPTIONBYTE_FREQ_BOOST)
544 {
546 }
547#endif /* FLASH_OPTSR2_CPUFREQ_BOOST */
548 }
549
550 /* Process Unlocked */
551 __HAL_UNLOCK(&pFlash);
552
553 return status;
554}
555
556/**
557 * @brief Get the Option byte configuration
558 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
559 * contains the configuration information for the programming.
560 * @note The parameter Banks of the pOBInit structure must be set exclusively to FLASH_BANK_1 or FLASH_BANK_2,
561 * as this parameter is use to get the given Bank WRP, PCROP and secured area configuration.
562 *
563 * @retval None
564 */
566{
567 pOBInit->OptionType = (OPTIONBYTE_USER | OPTIONBYTE_RDP | OPTIONBYTE_BOR);
568
569 /* Get Read protection level */
570 pOBInit->RDPLevel = FLASH_OB_GetRDP();
571
572 /* Get the user option bytes */
573 pOBInit->USERConfig = FLASH_OB_GetUser();
574
575 /*Get BOR Level*/
576 pOBInit->BORLevel = FLASH_OB_GetBOR();
577
578#if defined (DUAL_BANK)
579 if ((pOBInit->Banks == FLASH_BANK_1) || (pOBInit->Banks == FLASH_BANK_2))
580#else
581 if (pOBInit->Banks == FLASH_BANK_1)
582#endif /* DUAL_BANK */
583 {
584 pOBInit->OptionType |= (OPTIONBYTE_WRP | OPTIONBYTE_PCROP | OPTIONBYTE_SECURE_AREA);
585
586 /* Get write protection on the selected area */
587 FLASH_OB_GetWRP(&(pOBInit->WRPState), &(pOBInit->WRPSector), pOBInit->Banks);
588
589 /* Get the Proprietary code readout protection */
590 FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr), pOBInit->Banks);
591
592 /*Get Bank Secure area*/
593 FLASH_OB_GetSecureArea(&(pOBInit->SecureAreaConfig), &(pOBInit->SecureAreaStartAddr), &(pOBInit->SecureAreaEndAddr), pOBInit->Banks);
594 }
595
596 /*Get Boot Address*/
597 FLASH_OB_GetBootAdd(&(pOBInit->BootAddr0), &(pOBInit->BootAddr1));
598#if defined(DUAL_CORE)
599 pOBInit->OptionType |= OPTIONBYTE_CM7_BOOTADD | OPTIONBYTE_CM4_BOOTADD;
600
601 /*Get CM4 Boot Address*/
602 FLASH_OB_GetCM4BootAdd(&(pOBInit->CM4BootAddr0), &(pOBInit->CM4BootAddr1));
603#else
604 pOBInit->OptionType |= OPTIONBYTE_BOOTADD;
605#endif /*DUAL_CORE*/
606
607#if defined (FLASH_OTPBL_LOCKBL)
608 pOBInit->OptionType |= OPTIONBYTE_OTP_LOCK;
609
610 /* Get OTP Block Lock */
612#endif /* FLASH_OTPBL_LOCKBL */
613
614#if defined (FLASH_OPTSR2_TCM_AXI_SHARED)
615 pOBInit->OptionType |= OPTIONBYTE_SHARED_RAM;
616
617 /* Get TCM / AXI Shared RAM */
619#endif /* FLASH_OPTSR2_TCM_AXI_SHARED */
620
621#if defined (FLASH_OPTSR2_CPUFREQ_BOOST)
622 pOBInit->OptionType |= OPTIONBYTE_FREQ_BOOST;
623
624 /* Get CPU Frequency Boost */
626#endif /* FLASH_OPTSR2_CPUFREQ_BOOST */
627}
628
629/**
630 * @brief Unlock the FLASH Bank1 control registers access
631 * @retval HAL Status
632 */
633HAL_StatusTypeDef HAL_FLASHEx_Unlock_Bank1(void)
634{
635 if(READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
636 {
637 /* Authorize the FLASH Bank1 Registers access */
638 WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
639 WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
640
641 /* Verify Flash Bank1 is unlocked */
642 if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
643 {
644 return HAL_ERROR;
645 }
646 }
647
648 return HAL_OK;
649}
650
651/**
652 * @brief Locks the FLASH Bank1 control registers access
653 * @retval HAL Status
654 */
655HAL_StatusTypeDef HAL_FLASHEx_Lock_Bank1(void)
656{
657 /* Set the LOCK Bit to lock the FLASH Bank1 Registers access */
658 SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
659 return HAL_OK;
660}
661
662#if defined (DUAL_BANK)
663/**
664 * @brief Unlock the FLASH Bank2 control registers access
665 * @retval HAL Status
666 */
667HAL_StatusTypeDef HAL_FLASHEx_Unlock_Bank2(void)
668{
669 if(READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
670 {
671 /* Authorize the FLASH Bank2 Registers access */
672 WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
673 WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
674
675 /* Verify Flash Bank1 is unlocked */
676 if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
677 {
678 return HAL_ERROR;
679 }
680 }
681
682 return HAL_OK;
683}
684
685/**
686 * @brief Locks the FLASH Bank2 control registers access
687 * @retval HAL Status
688 */
689HAL_StatusTypeDef HAL_FLASHEx_Lock_Bank2(void)
690{
691 /* Set the LOCK Bit to lock the FLASH Bank2 Registers access */
692 SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
693 return HAL_OK;
694}
695#endif /* DUAL_BANK */
696
697/*
698 * @brief Perform a CRC computation on the specified FLASH memory area
699 * @param pCRCInit pointer to an FLASH_CRCInitTypeDef structure that
700 * contains the configuration information for the CRC computation.
701 * @note CRC computation uses CRC-32 (Ethernet) polynomial 0x4C11DB7
702 * @note The application should avoid running a CRC on PCROP or secure-only
703 * user Flash memory area since it may alter the expected CRC value.
704 * A special error flag (CRC read error: CRCRDERR) can be used to
705 * detect such a case.
706 * @retval HAL Status
707*/
708HAL_StatusTypeDef HAL_FLASHEx_ComputeCRC(FLASH_CRCInitTypeDef *pCRCInit, uint32_t *CRC_Result)
709{
710 HAL_StatusTypeDef status;
711 uint32_t sector_index;
712
713 /* Check the parameters */
714 assert_param(IS_FLASH_BANK_EXCLUSIVE(pCRCInit->Bank));
715 assert_param(IS_FLASH_TYPECRC(pCRCInit->TypeCRC));
716
717 /* Wait for OB change operation to be completed */
718 status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
719
720 if (status == HAL_OK)
721 {
722 if (pCRCInit->Bank == FLASH_BANK_1)
723 {
724 /* Enable CRC feature */
725 FLASH->CR1 |= FLASH_CR_CRC_EN;
726
727 /* Clear CRC flags in Status Register: CRC end of calculation and CRC read error */
728 FLASH->CCR1 |= (FLASH_CCR_CLR_CRCEND | FLASH_CCR_CLR_CRCRDERR);
729
730 /* Clear current CRC result, program burst size and define memory area on which CRC has to be computed */
731 FLASH->CRCCR1 |= FLASH_CRCCR_CLEAN_CRC | pCRCInit->BurstSize | pCRCInit->TypeCRC;
732
733 if (pCRCInit->TypeCRC == FLASH_CRC_SECTORS)
734 {
735 /* Clear sectors list */
736 FLASH->CRCCR1 |= FLASH_CRCCR_CLEAN_SECT;
737
738 /* Select CRC sectors */
739 for(sector_index = pCRCInit->Sector; sector_index < (pCRCInit->NbSectors + pCRCInit->Sector); sector_index++)
740 {
741 FLASH_CRC_AddSector(sector_index, FLASH_BANK_1);
742 }
743 }
744 else if (pCRCInit->TypeCRC == FLASH_CRC_BANK)
745 {
746 /* Enable Bank 1 CRC select bit */
747 FLASH->CRCCR1 |= FLASH_CRCCR_ALL_BANK;
748 }
749 else
750 {
751 /* Select CRC start and end addresses */
752 FLASH_CRC_SelectAddress(pCRCInit->CRCStartAddr, pCRCInit->CRCEndAddr, FLASH_BANK_1);
753 }
754
755 /* Start the CRC calculation */
756 FLASH->CRCCR1 |= FLASH_CRCCR_START_CRC;
757
758 /* Wait on CRC busy flag */
759 status = FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
760
761 /* Return CRC result */
762 (*CRC_Result) = FLASH->CRCDATA;
763
764 /* Disable CRC feature */
765 FLASH->CR1 &= (~FLASH_CR_CRC_EN);
766
767 /* Clear CRC flags */
768 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCEND_BANK1 | FLASH_FLAG_CRCRDERR_BANK1);
769 }
770#if defined (DUAL_BANK)
771 else
772 {
773 /* Enable CRC feature */
774 FLASH->CR2 |= FLASH_CR_CRC_EN;
775
776 /* Clear CRC flags in Status Register: CRC end of calculation and CRC read error */
777 FLASH->CCR2 |= (FLASH_CCR_CLR_CRCEND | FLASH_CCR_CLR_CRCRDERR);
778
779 /* Clear current CRC result, program burst size and define memory area on which CRC has to be computed */
780 FLASH->CRCCR2 |= FLASH_CRCCR_CLEAN_CRC | pCRCInit->BurstSize | pCRCInit->TypeCRC;
781
782 if (pCRCInit->TypeCRC == FLASH_CRC_SECTORS)
783 {
784 /* Clear sectors list */
785 FLASH->CRCCR2 |= FLASH_CRCCR_CLEAN_SECT;
786
787 /* Add CRC sectors */
788 for(sector_index = pCRCInit->Sector; sector_index < (pCRCInit->NbSectors + pCRCInit->Sector); sector_index++)
789 {
790 FLASH_CRC_AddSector(sector_index, FLASH_BANK_2);
791 }
792 }
793 else if (pCRCInit->TypeCRC == FLASH_CRC_BANK)
794 {
795 /* Enable Bank 2 CRC select bit */
796 FLASH->CRCCR2 |= FLASH_CRCCR_ALL_BANK;
797 }
798 else
799 {
800 /* Select CRC start and end addresses */
801 FLASH_CRC_SelectAddress(pCRCInit->CRCStartAddr, pCRCInit->CRCEndAddr, FLASH_BANK_2);
802 }
803
804 /* Start the CRC calculation */
805 FLASH->CRCCR2 |= FLASH_CRCCR_START_CRC;
806
807 /* Wait on CRC busy flag */
808 status = FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
809
810 /* Return CRC result */
811 (*CRC_Result) = FLASH->CRCDATA;
812
813 /* Disable CRC feature */
814 FLASH->CR2 &= (~FLASH_CR_CRC_EN);
815
816 /* Clear CRC flags */
817 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCEND_BANK2 | FLASH_FLAG_CRCRDERR_BANK2);
818 }
819#endif /* DUAL_BANK */
820 }
821
822 return status;
823}
824
825/**
826 * @}
827 */
828
829/**
830 * @}
831 */
832
833/* Private functions ---------------------------------------------------------*/
834
835/** @addtogroup FLASHEx_Private_Functions
836 * @{
837 */
838
839/**
840 * @brief Mass erase of FLASH memory
841 * @param VoltageRange The device program/erase parallelism.
842 * This parameter can be one of the following values:
843 * @arg FLASH_VOLTAGE_RANGE_1 : Flash program/erase by 8 bits
844 * @arg FLASH_VOLTAGE_RANGE_2 : Flash program/erase by 16 bits
845 * @arg FLASH_VOLTAGE_RANGE_3 : Flash program/erase by 32 bits
846 * @arg FLASH_VOLTAGE_RANGE_4 : Flash program/erase by 64 bits
847 *
848 * @param Banks Banks to be erased
849 * This parameter can be one of the following values:
850 * @arg FLASH_BANK_1: Bank1 to be erased
851 * @arg FLASH_BANK_2: Bank2 to be erased
852 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
853 *
854 * @retval HAL Status
855 */
856static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks)
857{
858 /* Check the parameters */
859#if defined (FLASH_CR_PSIZE)
860 assert_param(IS_VOLTAGERANGE(VoltageRange));
861#else
862 UNUSED(VoltageRange);
863#endif /* FLASH_CR_PSIZE */
864 assert_param(IS_FLASH_BANK(Banks));
865
866#if defined (DUAL_BANK)
867 /* Flash Mass Erase */
868 if((Banks & FLASH_BANK_BOTH) == FLASH_BANK_BOTH)
869 {
870#if defined (FLASH_CR_PSIZE)
871 /* Reset Program/erase VoltageRange for Bank1 and Bank2 */
872 FLASH->CR1 &= (~FLASH_CR_PSIZE);
873 FLASH->CR2 &= (~FLASH_CR_PSIZE);
874
875 /* Set voltage range */
876 FLASH->CR1 |= VoltageRange;
877 FLASH->CR2 |= VoltageRange;
878#endif /* FLASH_CR_PSIZE */
879
880 /* Set Mass Erase Bit */
881 FLASH->OPTCR |= FLASH_OPTCR_MER;
882 }
883 else
884#endif /* DUAL_BANK */
885 {
886 /* Proceed to erase Flash Bank */
887 if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
888 {
889#if defined (FLASH_CR_PSIZE)
890 /* Set Program/erase VoltageRange for Bank1 */
891 FLASH->CR1 &= (~FLASH_CR_PSIZE);
892 FLASH->CR1 |= VoltageRange;
893#endif /* FLASH_CR_PSIZE */
894
895 /* Erase Bank1 */
896 FLASH->CR1 |= (FLASH_CR_BER | FLASH_CR_START);
897 }
898
899#if defined (DUAL_BANK)
900 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
901 {
902#if defined (FLASH_CR_PSIZE)
903 /* Set Program/erase VoltageRange for Bank2 */
904 FLASH->CR2 &= (~FLASH_CR_PSIZE);
905 FLASH->CR2 |= VoltageRange;
906#endif /* FLASH_CR_PSIZE */
907
908 /* Erase Bank2 */
909 FLASH->CR2 |= (FLASH_CR_BER | FLASH_CR_START);
910 }
911#endif /* DUAL_BANK */
912 }
913}
914
915/**
916 * @brief Erase the specified FLASH memory sector
917 * @param Sector FLASH sector to erase
918 * This parameter can be a value of @ref FLASH_Sectors
919 * @param Banks Banks to be erased
920 * This parameter can be one of the following values:
921 * @arg FLASH_BANK_1: Bank1 to be erased
922 * @arg FLASH_BANK_2: Bank2 to be erased
923 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
924 * @param VoltageRange The device program/erase parallelism.
925 * This parameter can be one of the following values:
926 * @arg FLASH_VOLTAGE_RANGE_1 : Flash program/erase by 8 bits
927 * @arg FLASH_VOLTAGE_RANGE_2 : Flash program/erase by 16 bits
928 * @arg FLASH_VOLTAGE_RANGE_3 : Flash program/erase by 32 bits
929 * @arg FLASH_VOLTAGE_RANGE_4 : Flash program/erase by 64 bits
930 *
931 * @retval None
932 */
933void FLASH_Erase_Sector(uint32_t Sector, uint32_t Banks, uint32_t VoltageRange)
934{
935 assert_param(IS_FLASH_SECTOR(Sector));
936 assert_param(IS_FLASH_BANK_EXCLUSIVE(Banks));
937#if defined (FLASH_CR_PSIZE)
938 assert_param(IS_VOLTAGERANGE(VoltageRange));
939#else
940 UNUSED(VoltageRange);
941#endif /* FLASH_CR_PSIZE */
942
943 if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
944 {
945#if defined (FLASH_CR_PSIZE)
946 /* Reset Program/erase VoltageRange and Sector Number for Bank1 */
947 FLASH->CR1 &= ~(FLASH_CR_PSIZE | FLASH_CR_SNB);
948
949 FLASH->CR1 |= (FLASH_CR_SER | VoltageRange | (Sector << FLASH_CR_SNB_Pos) | FLASH_CR_START);
950#else
951 /* Reset Sector Number for Bank1 */
952 FLASH->CR1 &= ~(FLASH_CR_SNB);
953
954 FLASH->CR1 |= (FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos) | FLASH_CR_START);
955#endif /* FLASH_CR_PSIZE */
956 }
957
958#if defined (DUAL_BANK)
959 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
960 {
961#if defined (FLASH_CR_PSIZE)
962 /* Reset Program/erase VoltageRange and Sector Number for Bank2 */
963 FLASH->CR2 &= ~(FLASH_CR_PSIZE | FLASH_CR_SNB);
964
965 FLASH->CR2 |= (FLASH_CR_SER | VoltageRange | (Sector << FLASH_CR_SNB_Pos) | FLASH_CR_START);
966#else
967 /* Reset Sector Number for Bank2 */
968 FLASH->CR2 &= ~(FLASH_CR_SNB);
969
970 FLASH->CR2 |= (FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos) | FLASH_CR_START);
971#endif /* FLASH_CR_PSIZE */
972 }
973#endif /* DUAL_BANK */
974}
975
976/**
977 * @brief Enable the write protection of the desired bank1 or bank 2 sectors
978 * @param WRPSector specifies the sector(s) to be write protected.
979 * This parameter can be one of the following values:
980 * @arg WRPSector: A combination of OB_WRP_SECTOR_0 to OB_WRP_SECTOR_7 or OB_WRP_SECTOR_All
981 *
982 * @param Banks the specific bank to apply WRP sectors
983 * This parameter can be one of the following values:
984 * @arg FLASH_BANK_1: enable WRP on specified bank1 sectors
985 * @arg FLASH_BANK_2: enable WRP on specified bank2 sectors
986 * @arg FLASH_BANK_BOTH: enable WRP on both bank1 and bank2 specified sectors
987 *
988 * @retval HAL FLASH State
989 */
990static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
991{
992 /* Check the parameters */
993 assert_param(IS_OB_WRP_SECTOR(WRPSector));
994 assert_param(IS_FLASH_BANK(Banks));
995
996 if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
997 {
998 /* Enable Write Protection for bank 1 */
999 FLASH->WPSN_PRG1 &= (~(WRPSector & FLASH_WPSN_WRPSN));
1000 }
1001
1002#if defined (DUAL_BANK)
1003 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1004 {
1005 /* Enable Write Protection for bank 2 */
1006 FLASH->WPSN_PRG2 &= (~(WRPSector & FLASH_WPSN_WRPSN));
1007 }
1008#endif /* DUAL_BANK */
1009}
1010
1011/**
1012 * @brief Disable the write protection of the desired bank1 or bank 2 sectors
1013 * @param WRPSector specifies the sector(s) to disable write protection.
1014 * This parameter can be one of the following values:
1015 * @arg WRPSector: A combination of FLASH_OB_WRP_SECTOR_0 to FLASH_OB_WRP_SECTOR_7 or FLASH_OB_WRP_SECTOR_All
1016 *
1017 * @param Banks the specific bank to apply WRP sectors
1018 * This parameter can be one of the following values:
1019 * @arg FLASH_BANK_1: disable WRP on specified bank1 sectors
1020 * @arg FLASH_BANK_2: disable WRP on specified bank2 sectors
1021 * @arg FLASH_BANK_BOTH: disable WRP on both bank1 and bank2 specified sectors
1022 *
1023 * @retval HAL FLASH State
1024 */
1025static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
1026{
1027 /* Check the parameters */
1028 assert_param(IS_OB_WRP_SECTOR(WRPSector));
1029 assert_param(IS_FLASH_BANK(Banks));
1030
1031 if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
1032 {
1033 /* Disable Write Protection for bank 1 */
1034 FLASH->WPSN_PRG1 |= (WRPSector & FLASH_WPSN_WRPSN);
1035 }
1036
1037#if defined (DUAL_BANK)
1038 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1039 {
1040 /* Disable Write Protection for bank 2 */
1041 FLASH->WPSN_PRG2 |= (WRPSector & FLASH_WPSN_WRPSN);
1042 }
1043#endif /* DUAL_BANK */
1044}
1045
1046/**
1047 * @brief Get the write protection of the given bank 1 or bank 2 sectors
1048 * @param WRPState gives the write protection state on the given bank.
1049 * This parameter can be one of the following values:
1050 * @arg WRPState: OB_WRPSTATE_DISABLE or OB_WRPSTATE_ENABLE
1051
1052 * @param WRPSector gives the write protected sector(s) on the given bank .
1053 * This parameter can be one of the following values:
1054 * @arg WRPSector: A combination of FLASH_OB_WRP_SECTOR_0 to FLASH_OB_WRP_SECTOR_7 or FLASH_OB_WRP_SECTOR_All
1055 *
1056 * @param Bank the specific bank to apply WRP sectors
1057 * This parameter can be exclusively one of the following values:
1058 * @arg FLASH_BANK_1: Get bank1 WRP sectors
1059 * @arg FLASH_BANK_2: Get bank2 WRP sectors
1060 * @arg FLASH_BANK_BOTH: note allowed in this functions
1061 *
1062 * @retval HAL FLASH State
1063 */
1064static void FLASH_OB_GetWRP(uint32_t *WRPState, uint32_t *WRPSector, uint32_t Bank)
1065{
1066 uint32_t regvalue = 0U;
1067
1068 if(Bank == FLASH_BANK_1)
1069 {
1070 regvalue = FLASH->WPSN_CUR1;
1071 }
1072
1073#if defined (DUAL_BANK)
1074 if(Bank == FLASH_BANK_2)
1075 {
1076 regvalue = FLASH->WPSN_CUR2;
1077 }
1078#endif /* DUAL_BANK */
1079
1080 (*WRPSector) = (~regvalue) & FLASH_WPSN_WRPSN;
1081
1082 if(*WRPSector == 0U)
1083 {
1084 (*WRPState) = OB_WRPSTATE_DISABLE;
1085 }
1086 else
1087 {
1088 (*WRPState) = OB_WRPSTATE_ENABLE;
1089 }
1090}
1091
1092/**
1093 * @brief Set the read protection level.
1094 *
1095 * @note To configure the RDP level, the option lock bit OPTLOCK must be
1096 * cleared with the call of the HAL_FLASH_OB_Unlock() function.
1097 * @note To validate the RDP level, the option bytes must be reloaded
1098 * through the call of the HAL_FLASH_OB_Launch() function.
1099 * @note !!! Warning : When enabling OB_RDP level 2 it's no more possible
1100 * to go back to level 1 or 0 !!!
1101 *
1102 * @param RDPLevel specifies the read protection level.
1103 * This parameter can be one of the following values:
1104 * @arg OB_RDP_LEVEL_0: No protection
1105 * @arg OB_RDP_LEVEL_1: Read protection of the memory
1106 * @arg OB_RDP_LEVEL_2: Full chip protection
1107 *
1108 * @retval HAL status
1109 */
1110static void FLASH_OB_RDPConfig(uint32_t RDPLevel)
1111{
1112 /* Check the parameters */
1113 assert_param(IS_OB_RDP_LEVEL(RDPLevel));
1114
1115 /* Configure the RDP level in the option bytes register */
1116 MODIFY_REG(FLASH->OPTSR_PRG, FLASH_OPTSR_RDP, RDPLevel);
1117}
1118
1119/**
1120 * @brief Get the read protection level.
1121 * @retval RDPLevel specifies the read protection level.
1122 * This return value can be one of the following values:
1123 * @arg OB_RDP_LEVEL_0: No protection
1124 * @arg OB_RDP_LEVEL_1: Read protection of the memory
1125 * @arg OB_RDP_LEVEL_2: Full chip protection
1126 */
1127static uint32_t FLASH_OB_GetRDP(void)
1128{
1129 uint32_t rdp_level = READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_RDP);
1130
1131 if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
1132 {
1133 return (OB_RDP_LEVEL_1);
1134 }
1135 else
1136 {
1137 return rdp_level;
1138 }
1139}
1140
1141#if defined(DUAL_CORE)
1142/**
1143 * @brief Program the FLASH User Option Byte.
1144 *
1145 * @note To configure the user option bytes, the option lock bit OPTLOCK must
1146 * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
1147 *
1148 * @note To validate the user option bytes, the option bytes must be reloaded
1149 * through the call of the HAL_FLASH_OB_Launch() function.
1150 *
1151 * @param UserType The FLASH User Option Bytes to be modified :
1152 * a combination of @ref FLASHEx_OB_USER_Type
1153 *
1154 * @param UserConfig The FLASH User Option Bytes values:
1155 * IWDG1_SW(Bit4), IWDG2_SW(Bit 5), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
1156 * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
1157 * SECURITY(Bit 21), BCM4(Bit 22), BCM7(Bit 23), nRST_STOP_D2(Bit 24),
1158 * nRST_STDY_D2(Bit 25), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
1159 *
1160 * @retval HAL status
1161 */
1162#else
1163/**
1164 * @brief Program the FLASH User Option Byte.
1165 *
1166 * @note To configure the user option bytes, the option lock bit OPTLOCK must
1167 * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
1168 *
1169 * @note To validate the user option bytes, the option bytes must be reloaded
1170 * through the call of the HAL_FLASH_OB_Launch() function.
1171 *
1172 * @param UserType The FLASH User Option Bytes to be modified :
1173 * a combination of @arg FLASHEx_OB_USER_Type
1174 *
1175 * @param UserConfig The FLASH User Option Bytes values:
1176 * IWDG_SW(Bit4), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
1177 * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
1178 * SECURITY(Bit 21), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
1179 *
1180 * @retval HAL status
1181 */
1182#endif /*DUAL_CORE*/
1183static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
1184{
1185 uint32_t optr_reg_val = 0;
1186 uint32_t optr_reg_mask = 0;
1187
1188 /* Check the parameters */
1189 assert_param(IS_OB_USER_TYPE(UserType));
1190
1191 if((UserType & OB_USER_IWDG1_SW) != 0U)
1192 {
1193 /* IWDG_HW option byte should be modified */
1194 assert_param(IS_OB_IWDG1_SOURCE(UserConfig & FLASH_OPTSR_IWDG1_SW));
1195
1196 /* Set value and mask for IWDG_HW option byte */
1197 optr_reg_val |= (UserConfig & FLASH_OPTSR_IWDG1_SW);
1198 optr_reg_mask |= FLASH_OPTSR_IWDG1_SW;
1199 }
1200#if defined(DUAL_CORE)
1201 if((UserType & OB_USER_IWDG2_SW) != 0U)
1202 {
1203 /* IWDG2_SW option byte should be modified */
1204 assert_param(IS_OB_IWDG2_SOURCE(UserConfig & FLASH_OPTSR_IWDG2_SW));
1205
1206 /* Set value and mask for IWDG2_SW option byte */
1207 optr_reg_val |= (UserConfig & FLASH_OPTSR_IWDG2_SW);
1208 optr_reg_mask |= FLASH_OPTSR_IWDG2_SW;
1209 }
1210#endif /*DUAL_CORE*/
1211 if((UserType & OB_USER_NRST_STOP_D1) != 0U)
1212 {
1213 /* NRST_STOP option byte should be modified */
1214 assert_param(IS_OB_STOP_D1_RESET(UserConfig & FLASH_OPTSR_NRST_STOP_D1));
1215
1216 /* Set value and mask for NRST_STOP option byte */
1217 optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STOP_D1);
1218 optr_reg_mask |= FLASH_OPTSR_NRST_STOP_D1;
1219 }
1220
1221 if((UserType & OB_USER_NRST_STDBY_D1) != 0U)
1222 {
1223 /* NRST_STDBY option byte should be modified */
1224 assert_param(IS_OB_STDBY_D1_RESET(UserConfig & FLASH_OPTSR_NRST_STBY_D1));
1225
1226 /* Set value and mask for NRST_STDBY option byte */
1227 optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STBY_D1);
1228 optr_reg_mask |= FLASH_OPTSR_NRST_STBY_D1;
1229 }
1230
1231 if((UserType & OB_USER_IWDG_STOP) != 0U)
1232 {
1233 /* IWDG_STOP option byte should be modified */
1234 assert_param(IS_OB_USER_IWDG_STOP(UserConfig & FLASH_OPTSR_FZ_IWDG_STOP));
1235
1236 /* Set value and mask for IWDG_STOP option byte */
1237 optr_reg_val |= (UserConfig & FLASH_OPTSR_FZ_IWDG_STOP);
1238 optr_reg_mask |= FLASH_OPTSR_FZ_IWDG_STOP;
1239 }
1240
1241 if((UserType & OB_USER_IWDG_STDBY) != 0U)
1242 {
1243 /* IWDG_STDBY option byte should be modified */
1244 assert_param(IS_OB_USER_IWDG_STDBY(UserConfig & FLASH_OPTSR_FZ_IWDG_SDBY));
1245
1246 /* Set value and mask for IWDG_STDBY option byte */
1247 optr_reg_val |= (UserConfig & FLASH_OPTSR_FZ_IWDG_SDBY);
1248 optr_reg_mask |= FLASH_OPTSR_FZ_IWDG_SDBY;
1249 }
1250
1251 if((UserType & OB_USER_ST_RAM_SIZE) != 0U)
1252 {
1253 /* ST_RAM_SIZE option byte should be modified */
1254 assert_param(IS_OB_USER_ST_RAM_SIZE(UserConfig & FLASH_OPTSR_ST_RAM_SIZE));
1255
1256 /* Set value and mask for ST_RAM_SIZE option byte */
1257 optr_reg_val |= (UserConfig & FLASH_OPTSR_ST_RAM_SIZE);
1258 optr_reg_mask |= FLASH_OPTSR_ST_RAM_SIZE;
1259 }
1260
1261 if((UserType & OB_USER_SECURITY) != 0U)
1262 {
1263 /* SECURITY option byte should be modified */
1264 assert_param(IS_OB_USER_SECURITY(UserConfig & FLASH_OPTSR_SECURITY));
1265
1266 /* Set value and mask for SECURITY option byte */
1267 optr_reg_val |= (UserConfig & FLASH_OPTSR_SECURITY);
1268 optr_reg_mask |= FLASH_OPTSR_SECURITY;
1269 }
1270
1271#if defined(DUAL_CORE)
1272 if((UserType & OB_USER_BCM4) != 0U)
1273 {
1274 /* BCM4 option byte should be modified */
1275 assert_param(IS_OB_USER_BCM4(UserConfig & FLASH_OPTSR_BCM4));
1276
1277 /* Set value and mask for BCM4 option byte */
1278 optr_reg_val |= (UserConfig & FLASH_OPTSR_BCM4);
1279 optr_reg_mask |= FLASH_OPTSR_BCM4;
1280 }
1281
1282 if((UserType & OB_USER_BCM7) != 0U)
1283 {
1284 /* BCM7 option byte should be modified */
1285 assert_param(IS_OB_USER_BCM7(UserConfig & FLASH_OPTSR_BCM7));
1286
1287 /* Set value and mask for BCM7 option byte */
1288 optr_reg_val |= (UserConfig & FLASH_OPTSR_BCM7);
1289 optr_reg_mask |= FLASH_OPTSR_BCM7;
1290 }
1291#endif /* DUAL_CORE */
1292
1293#if defined (FLASH_OPTSR_NRST_STOP_D2)
1294 if((UserType & OB_USER_NRST_STOP_D2) != 0U)
1295 {
1296 /* NRST_STOP option byte should be modified */
1297 assert_param(IS_OB_STOP_D2_RESET(UserConfig & FLASH_OPTSR_NRST_STOP_D2));
1298
1299 /* Set value and mask for NRST_STOP option byte */
1300 optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STOP_D2);
1301 optr_reg_mask |= FLASH_OPTSR_NRST_STOP_D2;
1302 }
1303
1304 if((UserType & OB_USER_NRST_STDBY_D2) != 0U)
1305 {
1306 /* NRST_STDBY option byte should be modified */
1307 assert_param(IS_OB_STDBY_D2_RESET(UserConfig & FLASH_OPTSR_NRST_STBY_D2));
1308
1309 /* Set value and mask for NRST_STDBY option byte */
1310 optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STBY_D2);
1311 optr_reg_mask |= FLASH_OPTSR_NRST_STBY_D2;
1312 }
1313#endif /* FLASH_OPTSR_NRST_STOP_D2 */
1314
1315#if defined (DUAL_BANK)
1316 if((UserType & OB_USER_SWAP_BANK) != 0U)
1317 {
1318 /* SWAP_BANK_OPT option byte should be modified */
1319 assert_param(IS_OB_USER_SWAP_BANK(UserConfig & FLASH_OPTSR_SWAP_BANK_OPT));
1320
1321 /* Set value and mask for SWAP_BANK_OPT option byte */
1322 optr_reg_val |= (UserConfig & FLASH_OPTSR_SWAP_BANK_OPT);
1323 optr_reg_mask |= FLASH_OPTSR_SWAP_BANK_OPT;
1324 }
1325#endif /* DUAL_BANK */
1326
1327 if((UserType & OB_USER_IOHSLV) != 0U)
1328 {
1329 /* IOHSLV_OPT option byte should be modified */
1330 assert_param(IS_OB_USER_IOHSLV(UserConfig & FLASH_OPTSR_IO_HSLV));
1331
1332 /* Set value and mask for IOHSLV_OPT option byte */
1333 optr_reg_val |= (UserConfig & FLASH_OPTSR_IO_HSLV);
1334 optr_reg_mask |= FLASH_OPTSR_IO_HSLV;
1335 }
1336
1337#if defined (FLASH_OPTSR_VDDMMC_HSLV)
1338 if((UserType & OB_USER_VDDMMC_HSLV) != 0U)
1339 {
1340 /* VDDMMC_HSLV option byte should be modified */
1341 assert_param(IS_OB_USER_VDDMMC_HSLV(UserConfig & FLASH_OPTSR_VDDMMC_HSLV));
1342
1343 /* Set value and mask for VDDMMC_HSLV option byte */
1344 optr_reg_val |= (UserConfig & FLASH_OPTSR_VDDMMC_HSLV);
1345 optr_reg_mask |= FLASH_OPTSR_VDDMMC_HSLV;
1346 }
1347#endif /* FLASH_OPTSR_VDDMMC_HSLV */
1348
1349 /* Configure the option bytes register */
1350 MODIFY_REG(FLASH->OPTSR_PRG, optr_reg_mask, optr_reg_val);
1351}
1352
1353#if defined(DUAL_CORE)
1354/**
1355 * @brief Return the FLASH User Option Byte value.
1356 * @retval The FLASH User Option Bytes values
1357 * IWDG1_SW(Bit4), IWDG2_SW(Bit 5), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
1358 * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
1359 * SECURITY(Bit 21), BCM4(Bit 22), BCM7(Bit 23), nRST_STOP_D2(Bit 24),
1360 * nRST_STDY_D2(Bit 25), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
1361 */
1362#else
1363/**
1364 * @brief Return the FLASH User Option Byte value.
1365 * @retval The FLASH User Option Bytes values
1366 * IWDG_SW(Bit4), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
1367 * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
1368 * SECURITY(Bit 21), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
1369 */
1370#endif /*DUAL_CORE*/
1371static uint32_t FLASH_OB_GetUser(void)
1372{
1373 uint32_t userConfig = READ_REG(FLASH->OPTSR_CUR);
1374 userConfig &= (~(FLASH_OPTSR_BOR_LEV | FLASH_OPTSR_RDP));
1375
1376 return userConfig;
1377}
1378
1379/**
1380 * @brief Configure the Proprietary code readout protection of the desired addresses
1381 *
1382 * @note To configure the PCROP options, the option lock bit OPTLOCK must be
1383 * cleared with the call of the HAL_FLASH_OB_Unlock() function.
1384 * @note To validate the PCROP options, the option bytes must be reloaded
1385 * through the call of the HAL_FLASH_OB_Launch() function.
1386 *
1387 * @param PCROPConfig specifies if the PCROP area for the given Bank shall be erased or not
1388 * when RDP level decreased from Level 1 to Level 0, or after a bank erase with protection removal
1389 * This parameter must be a value of @arg FLASHEx_OB_PCROP_RDP enumeration
1390 *
1391 * @param PCROPStartAddr specifies the start address of the Proprietary code readout protection
1392 * This parameter can be an address between begin and end of the bank
1393 *
1394 * @param PCROPEndAddr specifies the end address of the Proprietary code readout protection
1395 * This parameter can be an address between PCROPStartAddr and end of the bank
1396 *
1397 * @param Banks the specific bank to apply PCROP protection
1398 * This parameter can be one of the following values:
1399 * @arg FLASH_BANK_1: PCROP on specified bank1 area
1400 * @arg FLASH_BANK_2: PCROP on specified bank2 area
1401 * @arg FLASH_BANK_BOTH: PCROP on specified bank1 and bank2 area (same config will be applied on both banks)
1402 *
1403 * @retval None
1404 */
1405static void FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks)
1406{
1407 /* Check the parameters */
1408 assert_param(IS_FLASH_BANK(Banks));
1409 assert_param(IS_OB_PCROP_RDP(PCROPConfig));
1410
1411 if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
1412 {
1413 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(PCROPStartAddr));
1414 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(PCROPEndAddr));
1415
1416 /* Configure the Proprietary code readout protection */
1417 FLASH->PRAR_PRG1 = ((PCROPStartAddr - FLASH_BANK1_BASE) >> 8) | \
1418 (((PCROPEndAddr - FLASH_BANK1_BASE) >> 8) << FLASH_PRAR_PROT_AREA_END_Pos) | \
1419 PCROPConfig;
1420 }
1421
1422#if defined (DUAL_BANK)
1423 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1424 {
1425 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(PCROPStartAddr));
1426 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(PCROPEndAddr));
1427
1428 /* Configure the Proprietary code readout protection */
1429 FLASH->PRAR_PRG2 = ((PCROPStartAddr - FLASH_BANK2_BASE) >> 8) | \
1430 (((PCROPEndAddr - FLASH_BANK2_BASE) >> 8) << FLASH_PRAR_PROT_AREA_END_Pos) | \
1431 PCROPConfig;
1432 }
1433#endif /* DUAL_BANK */
1434}
1435
1436/**
1437 * @brief Get the Proprietary code readout protection configuration on a given Bank
1438 *
1439 * @param PCROPConfig indicates if the PCROP area for the given Bank shall be erased or not
1440 * when RDP level decreased from Level 1 to Level 0 or after a bank erase with protection removal
1441 *
1442 * @param PCROPStartAddr gives the start address of the Proprietary code readout protection of the bank
1443 *
1444 * @param PCROPEndAddr gives the end address of the Proprietary code readout protection of the bank
1445 *
1446 * @param Bank the specific bank to apply PCROP protection
1447 * This parameter can be exclusively one of the following values:
1448 * @arg FLASH_BANK_1: PCROP on specified bank1 area
1449 * @arg FLASH_BANK_2: PCROP on specified bank2 area
1450 * @arg FLASH_BANK_BOTH: is not allowed here
1451 *
1452 * @retval None
1453 */
1454static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr, uint32_t *PCROPEndAddr, uint32_t Bank)
1455{
1456 uint32_t regvalue = 0;
1457 uint32_t bankBase = 0;
1458
1459 if(Bank == FLASH_BANK_1)
1460 {
1461 regvalue = FLASH->PRAR_CUR1;
1462 bankBase = FLASH_BANK1_BASE;
1463 }
1464
1465#if defined (DUAL_BANK)
1466 if(Bank == FLASH_BANK_2)
1467 {
1468 regvalue = FLASH->PRAR_CUR2;
1469 bankBase = FLASH_BANK2_BASE;
1470 }
1471#endif /* DUAL_BANK */
1472
1473 (*PCROPConfig) = (regvalue & FLASH_PRAR_DMEP);
1474
1475 (*PCROPStartAddr) = ((regvalue & FLASH_PRAR_PROT_AREA_START) << 8) + bankBase;
1476 (*PCROPEndAddr) = (regvalue & FLASH_PRAR_PROT_AREA_END) >> FLASH_PRAR_PROT_AREA_END_Pos;
1477 (*PCROPEndAddr) = ((*PCROPEndAddr) << 8) + bankBase;
1478}
1479
1480/**
1481 * @brief Set the BOR Level.
1482 * @param Level specifies the Option Bytes BOR Reset Level.
1483 * This parameter can be one of the following values:
1484 * @arg OB_BOR_LEVEL0: Reset level threshold is set to 1.6V
1485 * @arg OB_BOR_LEVEL1: Reset level threshold is set to 2.1V
1486 * @arg OB_BOR_LEVEL2: Reset level threshold is set to 2.4V
1487 * @arg OB_BOR_LEVEL3: Reset level threshold is set to 2.7V
1488 * @retval None
1489 */
1490static void FLASH_OB_BOR_LevelConfig(uint32_t Level)
1491{
1492 assert_param(IS_OB_BOR_LEVEL(Level));
1493
1494 /* Configure BOR_LEV option byte */
1495 MODIFY_REG(FLASH->OPTSR_PRG, FLASH_OPTSR_BOR_LEV, Level);
1496}
1497
1498/**
1499 * @brief Get the BOR Level.
1500 * @retval The Option Bytes BOR Reset Level.
1501 * This parameter can be one of the following values:
1502 * @arg OB_BOR_LEVEL0: Reset level threshold is set to 1.6V
1503 * @arg OB_BOR_LEVEL1: Reset level threshold is set to 2.1V
1504 * @arg OB_BOR_LEVEL2: Reset level threshold is set to 2.4V
1505 * @arg OB_BOR_LEVEL3: Reset level threshold is set to 2.7V
1506 */
1507static uint32_t FLASH_OB_GetBOR(void)
1508{
1509 return (FLASH->OPTSR_CUR & FLASH_OPTSR_BOR_LEV);
1510}
1511
1512/**
1513 * @brief Set Boot address
1514 * @param BootOption Boot address option byte to be programmed,
1515 * This parameter must be a value of @ref FLASHEx_OB_BOOT_OPTION
1516 (OB_BOOT_ADD0, OB_BOOT_ADD1 or OB_BOOT_ADD_BOTH)
1517 *
1518 * @param BootAddress0 Specifies the Boot Address 0
1519 * @param BootAddress1 Specifies the Boot Address 1
1520 * @retval HAL Status
1521 */
1522static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
1523{
1524 /* Check the parameters */
1525 assert_param(IS_OB_BOOT_ADD_OPTION(BootOption));
1526
1527 if((BootOption & OB_BOOT_ADD0) == OB_BOOT_ADD0)
1528 {
1529 /* Check the parameters */
1530 assert_param(IS_BOOT_ADDRESS(BootAddress0));
1531
1532 /* Configure CM7 BOOT ADD0 */
1533#if defined(DUAL_CORE)
1534 MODIFY_REG(FLASH->BOOT7_PRG, FLASH_BOOT7_BCM7_ADD0, (BootAddress0 >> 16));
1535#else /* Single Core*/
1536 MODIFY_REG(FLASH->BOOT_PRG, FLASH_BOOT_ADD0, (BootAddress0 >> 16));
1537#endif /* DUAL_CORE */
1538 }
1539
1540 if((BootOption & OB_BOOT_ADD1) == OB_BOOT_ADD1)
1541 {
1542 /* Check the parameters */
1543 assert_param(IS_BOOT_ADDRESS(BootAddress1));
1544
1545 /* Configure CM7 BOOT ADD1 */
1546#if defined(DUAL_CORE)
1547 MODIFY_REG(FLASH->BOOT7_PRG, FLASH_BOOT7_BCM7_ADD1, BootAddress1);
1548#else /* Single Core*/
1549 MODIFY_REG(FLASH->BOOT_PRG, FLASH_BOOT_ADD1, BootAddress1);
1550#endif /* DUAL_CORE */
1551 }
1552}
1553
1554/**
1555 * @brief Get Boot address
1556 * @param BootAddress0 Specifies the Boot Address 0.
1557 * @param BootAddress1 Specifies the Boot Address 1.
1558 * @retval HAL Status
1559 */
1560static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
1561{
1562 uint32_t regvalue;
1563
1564#if defined(DUAL_CORE)
1565 regvalue = FLASH->BOOT7_CUR;
1566
1567 (*BootAddress0) = (regvalue & FLASH_BOOT7_BCM7_ADD0) << 16;
1568 (*BootAddress1) = (regvalue & FLASH_BOOT7_BCM7_ADD1);
1569#else /* Single Core */
1570 regvalue = FLASH->BOOT_CUR;
1571
1572 (*BootAddress0) = (regvalue & FLASH_BOOT_ADD0) << 16;
1573 (*BootAddress1) = (regvalue & FLASH_BOOT_ADD1);
1574#endif /* DUAL_CORE */
1575}
1576
1577#if defined(DUAL_CORE)
1578/**
1579 * @brief Set CM4 Boot address
1580 * @param BootOption Boot address option byte to be programmed,
1581 * This parameter must be a value of @ref FLASHEx_OB_BOOT_OPTION
1582 (OB_BOOT_ADD0, OB_BOOT_ADD1 or OB_BOOT_ADD_BOTH)
1583 *
1584 * @param BootAddress0 Specifies the CM4 Boot Address 0.
1585 * @param BootAddress1 Specifies the CM4 Boot Address 1.
1586 * @retval HAL Status
1587 */
1588static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
1589{
1590 /* Check the parameters */
1591 assert_param(IS_OB_BOOT_ADD_OPTION(BootOption));
1592
1593 if((BootOption & OB_BOOT_ADD0) == OB_BOOT_ADD0)
1594 {
1595 /* Check the parameters */
1596 assert_param(IS_BOOT_ADDRESS(BootAddress0));
1597
1598 /* Configure CM4 BOOT ADD0 */
1599 MODIFY_REG(FLASH->BOOT4_PRG, FLASH_BOOT4_BCM4_ADD0, (BootAddress0 >> 16));
1600
1601 }
1602
1603 if((BootOption & OB_BOOT_ADD1) == OB_BOOT_ADD1)
1604 {
1605 /* Check the parameters */
1606 assert_param(IS_BOOT_ADDRESS(BootAddress1));
1607
1608 /* Configure CM4 BOOT ADD1 */
1609 MODIFY_REG(FLASH->BOOT4_PRG, FLASH_BOOT4_BCM4_ADD1, BootAddress1);
1610 }
1611}
1612
1613/**
1614 * @brief Get CM4 Boot address
1615 * @param BootAddress0 Specifies the CM4 Boot Address 0.
1616 * @param BootAddress1 Specifies the CM4 Boot Address 1.
1617 * @retval HAL Status
1618 */
1619static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
1620{
1621 uint32_t regvalue;
1622
1623 regvalue = FLASH->BOOT4_CUR;
1624
1625 (*BootAddress0) = (regvalue & FLASH_BOOT4_BCM4_ADD0) << 16;
1626 (*BootAddress1) = (regvalue & FLASH_BOOT4_BCM4_ADD1);
1627}
1628#endif /*DUAL_CORE*/
1629
1630/**
1631 * @brief Set secure area configuration
1632 * @param SecureAreaConfig specify if the secure area will be deleted or not
1633 * when RDP level decreased from Level 1 to Level 0 or during a mass erase.
1634 *
1635 * @param SecureAreaStartAddr Specifies the secure area start address
1636 * @param SecureAreaEndAddr Specifies the secure area end address
1637 * @param Banks the specific bank to apply Security protection
1638 * This parameter can be one of the following values:
1639 * @arg FLASH_BANK_1: Secure area on specified bank1 area
1640 * @arg FLASH_BANK_2: Secure area on specified bank2 area
1641 * @arg FLASH_BANK_BOTH: Secure area on specified bank1 and bank2 area (same config will be applied on both banks)
1642 * @retval None
1643 */
1644static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks)
1645{
1646 /* Check the parameters */
1647 assert_param(IS_FLASH_BANK(Banks));
1648 assert_param(IS_OB_SECURE_RDP(SecureAreaConfig));
1649
1650 if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
1651 {
1652 /* Check the parameters */
1653 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(SecureAreaStartAddr));
1654 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(SecureAreaEndAddr));
1655
1656 /* Configure the secure area */
1657 FLASH->SCAR_PRG1 = ((SecureAreaStartAddr - FLASH_BANK1_BASE) >> 8) | \
1658 (((SecureAreaEndAddr - FLASH_BANK1_BASE) >> 8) << FLASH_SCAR_SEC_AREA_END_Pos) | \
1659 (SecureAreaConfig & FLASH_SCAR_DMES);
1660 }
1661
1662#if defined (DUAL_BANK)
1663 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1664 {
1665 /* Check the parameters */
1666 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(SecureAreaStartAddr));
1667 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(SecureAreaEndAddr));
1668
1669 /* Configure the secure area */
1670 FLASH->SCAR_PRG2 = ((SecureAreaStartAddr - FLASH_BANK2_BASE) >> 8) | \
1671 (((SecureAreaEndAddr - FLASH_BANK2_BASE) >> 8) << FLASH_SCAR_SEC_AREA_END_Pos) | \
1672 (SecureAreaConfig & FLASH_SCAR_DMES);
1673 }
1674#endif /* DUAL_BANK */
1675}
1676
1677/**
1678 * @brief Get secure area configuration
1679 * @param SecureAreaConfig indicates if the secure area will be deleted or not
1680 * when RDP level decreased from Level 1 to Level 0 or during a mass erase.
1681 * @param SecureAreaStartAddr gives the secure area start address
1682 * @param SecureAreaEndAddr gives the secure area end address
1683 * @param Bank Specifies the Bank
1684 * @retval None
1685 */
1686static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank)
1687{
1688 uint32_t regvalue = 0;
1689 uint32_t bankBase = 0;
1690
1691 /* Check Bank parameter value */
1692 if(Bank == FLASH_BANK_1)
1693 {
1694 regvalue = FLASH->SCAR_CUR1;
1695 bankBase = FLASH_BANK1_BASE;
1696 }
1697
1698#if defined (DUAL_BANK)
1699 if(Bank == FLASH_BANK_2)
1700 {
1701 regvalue = FLASH->SCAR_CUR2;
1702 bankBase = FLASH_BANK2_BASE;
1703 }
1704#endif /* DUAL_BANK */
1705
1706 /* Get the secure area settings */
1707 (*SecureAreaConfig) = (regvalue & FLASH_SCAR_DMES);
1708 (*SecureAreaStartAddr) = ((regvalue & FLASH_SCAR_SEC_AREA_START) << 8) + bankBase;
1709 (*SecureAreaEndAddr) = (regvalue & FLASH_SCAR_SEC_AREA_END) >> FLASH_SCAR_SEC_AREA_END_Pos;
1710 (*SecureAreaEndAddr) = ((*SecureAreaEndAddr) << 8) + bankBase;
1711}
1712
1713/**
1714 * @brief Add a CRC sector to the list of sectors on which the CRC will be calculated
1715 * @param Sector Specifies the CRC sector number
1716 * @param Bank Specifies the Bank
1717 * @retval None
1718 */
1719static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank)
1720{
1721 /* Check the parameters */
1722 assert_param(IS_FLASH_SECTOR(Sector));
1723
1724 if (Bank == FLASH_BANK_1)
1725 {
1726 /* Clear CRC sector */
1727 FLASH->CRCCR1 &= (~FLASH_CRCCR_CRC_SECT);
1728
1729 /* Select CRC Sector and activate ADD_SECT bit */
1730 FLASH->CRCCR1 |= Sector | FLASH_CRCCR_ADD_SECT;
1731 }
1732#if defined (DUAL_BANK)
1733 else
1734 {
1735 /* Clear CRC sector */
1736 FLASH->CRCCR2 &= (~FLASH_CRCCR_CRC_SECT);
1737
1738 /* Select CRC Sector and activate ADD_SECT bit */
1739 FLASH->CRCCR2 |= Sector | FLASH_CRCCR_ADD_SECT;
1740 }
1741#endif /* DUAL_BANK */
1742}
1743
1744/**
1745 * @brief Select CRC start and end memory addresses on which the CRC will be calculated
1746 * @param CRCStartAddr Specifies the CRC start address
1747 * @param CRCEndAddr Specifies the CRC end address
1748 * @param Bank Specifies the Bank
1749 * @retval None
1750 */
1751static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank)
1752{
1753 if (Bank == FLASH_BANK_1)
1754 {
1755 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(CRCStartAddr));
1756 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(CRCEndAddr));
1757
1758 /* Write CRC Start and End addresses */
1759 FLASH->CRCSADD1 = CRCStartAddr;
1760 FLASH->CRCEADD1 = CRCEndAddr;
1761 }
1762#if defined (DUAL_BANK)
1763 else
1764 {
1765 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(CRCStartAddr));
1766 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(CRCEndAddr));
1767
1768 /* Write CRC Start and End addresses */
1769 FLASH->CRCSADD2 = CRCStartAddr;
1770 FLASH->CRCEADD2 = CRCEndAddr;
1771 }
1772#endif /* DUAL_BANK */
1773}
1774/**
1775 * @}
1776 */
1777
1778#if defined (FLASH_OTPBL_LOCKBL)
1779/**
1780 * @brief Configure the OTP Block Lock.
1781 * @param OTP_Block specifies the OTP Block to lock.
1782 * This parameter can be a value of @ref FLASHEx_OTP_Blocks
1783 * @retval None
1784 */
1785static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block)
1786{
1787 /* Check the parameters */
1788 assert_param(IS_OTP_BLOCK(OTP_Block));
1789
1790 /* Configure the OTP Block lock in the option bytes register */
1791 FLASH->OTPBL_PRG |= (OTP_Block & FLASH_OTPBL_LOCKBL);
1792}
1793
1794/**
1795 * @brief Get the OTP Block Lock.
1796 * @retval OTP_Block specifies the OTP Block to lock.
1797 * This return value can be a value of @ref FLASHEx_OTP_Blocks
1798 */
1799static uint32_t FLASH_OB_OTP_GetLock(void)
1800{
1801 return (FLASH->OTPBL_CUR);
1802}
1803#endif /* FLASH_OTPBL_LOCKBL */
1804
1805#if defined (FLASH_OPTSR2_TCM_AXI_SHARED)
1806/**
1807 * @brief Configure the TCM / AXI Shared RAM.
1808 * @param SharedRamConfig specifies the Shared RAM configuration.
1809 * This parameter can be a value of @ref FLASHEx_OB_TCM_AXI_SHARED
1810 * @retval None
1811 */
1812static void FLASH_OB_SharedRAM_Config(uint32_t SharedRamConfig)
1813{
1814 /* Check the parameters */
1815 assert_param(IS_OB_USER_TCM_AXI_SHARED(SharedRamConfig));
1816
1817 /* Configure the TCM / AXI Shared RAM in the option bytes register */
1818 MODIFY_REG(FLASH->OPTSR2_PRG, FLASH_OPTSR2_TCM_AXI_SHARED, SharedRamConfig);
1819}
1820
1821/**
1822 * @brief Get the TCM / AXI Shared RAM configurtion.
1823 * @retval SharedRamConfig returns the TCM / AXI Shared RAM configuration.
1824 * This return value can be a value of @ref FLASHEx_OB_TCM_AXI_SHARED
1825 */
1826static uint32_t FLASH_OB_SharedRAM_GetConfig(void)
1827{
1828 return (FLASH->OPTSR2_CUR & FLASH_OPTSR2_TCM_AXI_SHARED);;
1829}
1830#endif /* FLASH_OPTSR2_TCM_AXI_SHARED */
1831
1832#if defined (FLASH_OPTSR2_CPUFREQ_BOOST)
1833/**
1834 * @brief Configure the CPU Frequency Boost.
1835 * @param FreqBoost specifies the CPU Frequency Boost state.
1836 * This parameter can be a value of @ref FLASHEx_OB_CPUFREQ_BOOST
1837 * @retval None
1838 */
1839static void FLASH_OB_CPUFreq_BoostConfig(uint32_t FreqBoost)
1840{
1841 /* Check the parameters */
1842 assert_param(IS_OB_USER_CPUFREQ_BOOST(FreqBoost));
1843
1844 /* Configure the CPU Frequency Boost in the option bytes register */
1845 MODIFY_REG(FLASH->OPTSR2_PRG, FLASH_OPTSR2_CPUFREQ_BOOST, FreqBoost);
1846}
1847
1848/**
1849 * @brief Get the CPU Frequency Boost state.
1850 * @retval FreqBoost returns the CPU Frequency Boost state.
1851 * This return value can be a value of @ref FLASHEx_OB_CPUFREQ_BOOST
1852 */
1853static uint32_t FLASH_OB_CPUFreq_GetBoost(void)
1854{
1855 return (FLASH->OPTSR2_CUR & FLASH_OPTSR2_CPUFREQ_BOOST);;
1856}
1857#endif /* FLASH_OPTSR2_CPUFREQ_BOOST */
1858
1859#endif /* HAL_FLASH_MODULE_ENABLED */
1860
1861/**
1862 * @}
1863 */
1864
1865/**
1866 * @}
1867 */
1868
1869/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef FLASH_CRC_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
Wait for a FLASH CRC computation to complete.
HAL_StatusTypeDef FLASH_OB_WaitForLastOperation(uint32_t Timeout)
Wait for a FLASH Option Bytes change operation to complete.
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
Wait for a FLASH operation to complete.
FLASH_ProcessTypeDef pFlash
HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
Program option bytes.
HAL_StatusTypeDef HAL_FLASHEx_Lock_Bank2(void)
Locks the FLASH Bank2 control registers access.
HAL_StatusTypeDef HAL_FLASHEx_Unlock_Bank2(void)
Unlock the FLASH Bank2 control registers access.
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_ComputeCRC(FLASH_CRCInitTypeDef *pCRCInit, uint32_t *CRC_Result)
HAL_StatusTypeDef HAL_FLASHEx_Unlock_Bank1(void)
Unlock the FLASH Bank1 control registers access.
HAL_StatusTypeDef HAL_FLASHEx_Lock_Bank1(void)
Locks the FLASH Bank1 control registers access.
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled.
static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block)
Configure the OTP Block Lock.
static uint32_t FLASH_OB_CPUFreq_GetBoost(void)
Get the CPU Frequency Boost state.
static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks)
Mass erase of FLASH memory.
static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
Program the FLASH User Option Byte.
static void FLASH_OB_RDPConfig(uint32_t RDPLevel)
Set the read protection level.
static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
Get CM4 Boot address.
static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
Get Boot address.
static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank)
Get secure area configuration.
static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
Set CM4 Boot address.
static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank)
Select CRC start and end memory addresses on which the CRC will be calculated.
static void FLASH_OB_CPUFreq_BoostConfig(uint32_t FreqBoost)
Configure the CPU Frequency Boost.
static void FLASH_OB_BOR_LevelConfig(uint32_t Level)
Set the BOR Level.
static uint16_t FLASH_OB_GetWRP(void)
Return the FLASH Write Protection Option Bytes value.
static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
Set Boot address.
static void FLASH_OB_SharedRAM_Config(uint32_t SharedRamConfig)
Configure the TCM / AXI Shared RAM.
static uint32_t FLASH_OB_OTP_GetLock(void)
Get the OTP Block Lock.
static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank)
Add a CRC sector to the list of sectors on which the CRC will be calculated.
static uint32_t FLASH_OB_GetPCROP(void)
Return the FLASH PCROP Protection Option Bytes value.
void FLASH_Erase_Sector(uint32_t Sector, uint32_t Banks, uint32_t VoltageRange)
Erase the specified FLASH memory sector.
static void FLASH_OB_PCROPConfig(uint32_t PCROConfigRDP, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks)
Configure the Proprietary code readout protection of the desired addresses.
static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks)
Set secure area configuration.
static uint32_t FLASH_OB_SharedRAM_GetConfig(void)
Get the TCM / AXI Shared RAM configurtion.
UNUSED(samplingTimeSeconds)
Header file of FLASH HAL module.
static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Bank)
Disable the write protection of the desired bank1 or bank 2 sectors.
static uint32_t FLASH_OB_GetRDP(void)
Get the read protection level.
static uint32_t FLASH_OB_GetUser(void)
Return the FLASH User Option Byte value.
static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
Enable the write protection of the desired bank1 or bank 2 sectors.
static uint32_t FLASH_OB_GetBOR(void)
Get the BOR Level.
Header file of FLASH HAL module.
FLASH Erase structure definition.
FLASH Option Bytes Program structure definition.
__IO FLASH_ProcedureTypeDef ProcedureOnGoing