You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1616 lines
54 KiB

  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. [..] Comparing to other previous devices, the FLASH interface for STM32H7xx
  15. devices contains the following additional features
  16. (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
  17. capability (RWW)
  18. (+) Dual bank memory organization
  19. (+) PCROP protection for all banks
  20. (+) Global readout protection (RDP)
  21. (+) Write protection
  22. (+) Secure access only protection
  23. (+) Bank / register swapping
  24. (+) Cyclic Redundancy Check (CRC)
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..] This driver provides functions to configure and program the FLASH memory
  28. of all STM32H7xx devices. It includes
  29. (#) FLASH Memory Erase functions:
  30. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  31. HAL_FLASH_Lock() functions
  32. (++) Erase function: Sector erase, bank erase and dual-bank mass erase
  33. (++) There are two modes of erase :
  34. (+++) Polling Mode using HAL_FLASHEx_Erase()
  35. (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
  36. (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to:
  37. (++) Set/Reset the write protection per bank
  38. (++) Set the Read protection Level
  39. (++) Set the BOR level
  40. (++) Program the user Option Bytes
  41. (++) PCROP protection configuration and control per bank
  42. (++) Secure area configuration and control per bank
  43. (++) Core Boot address configuration
  44. (#) FLASH Memory Lock and unlock per Bank: HAL_FLASHEx_Lock_Bank1(), HAL_FLASHEx_Unlock_Bank1(),
  45. HAL_FLASHEx_Lock_Bank2() and HAL_FLASHEx_Unlock_Bank2() functions
  46. (#) FLASH CRC computation function: Use HAL_FLASHEx_ComputeCRC() to:
  47. (++) Enable CRC feature
  48. (++) Program the desired burst size
  49. (++) Define the user Flash Area on which the CRC has be computed
  50. (++) Perform the CRC computation
  51. (++) Disable CRC feature
  52. @endverbatim
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
  57. * All rights reserved.</center></h2>
  58. *
  59. * This software component is licensed by ST under BSD 3-Clause license,
  60. * the "License"; You may not use this file except in compliance with the
  61. * License. You may obtain a copy of the License at:
  62. * opensource.org/licenses/BSD-3-Clause
  63. *
  64. ******************************************************************************
  65. */
  66. /* Includes ------------------------------------------------------------------*/
  67. #include "stm32h7xx_hal.h"
  68. /** @addtogroup STM32H7xx_HAL_Driver
  69. * @{
  70. */
  71. /** @defgroup FLASHEx FLASHEx
  72. * @brief FLASH HAL Extension module driver
  73. * @{
  74. */
  75. #ifdef HAL_FLASH_MODULE_ENABLED
  76. /* Private typedef -----------------------------------------------------------*/
  77. /* Private define ------------------------------------------------------------*/
  78. /** @addtogroup FLASHEx_Private_Constants
  79. * @{
  80. */
  81. #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
  82. /**
  83. * @}
  84. */
  85. /* Private macro -------------------------------------------------------------*/
  86. /* Private variables ---------------------------------------------------------*/
  87. /* Private function prototypes -----------------------------------------------*/
  88. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  89. * @{
  90. */
  91. static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks);
  92. static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
  93. static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Bank);
  94. static void FLASH_OB_GetWRP(uint32_t *WRPState, uint32_t *WRPSector, uint32_t Bank);
  95. static void FLASH_OB_RDPConfig(uint32_t RDPLevel);
  96. static uint32_t FLASH_OB_GetRDP(void);
  97. static void FLASH_OB_PCROPConfig(uint32_t PCROConfigRDP, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks);
  98. static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr,uint32_t *PCROPEndAddr, uint32_t Bank);
  99. static void FLASH_OB_BOR_LevelConfig(uint32_t Level);
  100. static uint32_t FLASH_OB_GetBOR(void);
  101. static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig);
  102. static uint32_t FLASH_OB_GetUser(void);
  103. static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1);
  104. static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1);
  105. static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks);
  106. static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank);
  107. static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank);
  108. static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank);
  109. #if defined(DUAL_CORE)
  110. static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1);
  111. static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1);
  112. #endif /*DUAL_CORE*/
  113. /**
  114. * @}
  115. */
  116. /* Exported functions ---------------------------------------------------------*/
  117. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  118. * @{
  119. */
  120. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  121. * @brief Extended IO operation functions
  122. *
  123. @verbatim
  124. ===============================================================================
  125. ##### Extended programming operation functions #####
  126. ===============================================================================
  127. [..]
  128. This subsection provides a set of functions allowing to manage the Extension FLASH
  129. programming operations Operations.
  130. @endverbatim
  131. * @{
  132. */
  133. /**
  134. * @brief Perform a mass erase or erase the specified FLASH memory sectors
  135. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  136. * contains the configuration information for the erasing.
  137. *
  138. * @param[out] SectorError pointer to variable that contains the configuration
  139. * information on faulty sector in case of error (0xFFFFFFFF means that all
  140. * the sectors have been correctly erased)
  141. *
  142. * @retval HAL Status
  143. */
  144. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
  145. {
  146. HAL_StatusTypeDef status = HAL_OK;
  147. uint32_t sector_index;
  148. /* Check the parameters */
  149. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  150. assert_param(IS_FLASH_BANK(pEraseInit->Banks));
  151. /* Process Locked */
  152. __HAL_LOCK(&pFlash);
  153. /* Reset error code */
  154. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  155. /* Wait for last operation to be completed on Bank1 */
  156. if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
  157. {
  158. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
  159. {
  160. status = HAL_ERROR;
  161. }
  162. }
  163. /* Wait for last operation to be completed on Bank2 */
  164. if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
  165. {
  166. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
  167. {
  168. status = HAL_ERROR;
  169. }
  170. }
  171. if(status == HAL_OK)
  172. {
  173. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  174. {
  175. /* Mass erase to be done */
  176. FLASH_MassErase(pEraseInit->VoltageRange, pEraseInit->Banks);
  177. /* Wait for last operation to be completed */
  178. if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
  179. {
  180. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
  181. {
  182. status = HAL_ERROR;
  183. }
  184. /* if the erase operation is completed, disable the Bank1 BER Bit */
  185. FLASH->CR1 &= (~FLASH_CR_BER);
  186. }
  187. if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
  188. {
  189. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
  190. {
  191. status = HAL_ERROR;
  192. }
  193. /* if the erase operation is completed, disable the Bank2 BER Bit */
  194. FLASH->CR2 &= (~FLASH_CR_BER);
  195. }
  196. }
  197. else
  198. {
  199. /*Initialization of SectorError variable*/
  200. *SectorError = 0xFFFFFFFFU;
  201. /* Erase by sector by sector to be done*/
  202. for(sector_index = pEraseInit->Sector; sector_index < (pEraseInit->NbSectors + pEraseInit->Sector); sector_index++)
  203. {
  204. FLASH_Erase_Sector(sector_index, pEraseInit->Banks, pEraseInit->VoltageRange);
  205. if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
  206. {
  207. /* Wait for last operation to be completed */
  208. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
  209. /* If the erase operation is completed, disable the SER Bit */
  210. FLASH->CR1 &= (~(FLASH_CR_SER | FLASH_CR_SNB));
  211. }
  212. if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
  213. {
  214. /* Wait for last operation to be completed */
  215. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
  216. /* If the erase operation is completed, disable the SER Bit */
  217. FLASH->CR2 &= (~(FLASH_CR_SER | FLASH_CR_SNB));
  218. }
  219. if(status != HAL_OK)
  220. {
  221. /* In case of error, stop erase procedure and return the faulty sector */
  222. *SectorError = sector_index;
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. /* Process Unlocked */
  229. __HAL_UNLOCK(&pFlash);
  230. return status;
  231. }
  232. /**
  233. * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
  234. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  235. * contains the configuration information for the erasing.
  236. *
  237. * @retval HAL Status
  238. */
  239. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  240. {
  241. HAL_StatusTypeDef status = HAL_OK;
  242. /* Check the parameters */
  243. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  244. assert_param(IS_FLASH_BANK(pEraseInit->Banks));
  245. /* Process Locked */
  246. __HAL_LOCK(&pFlash);
  247. /* Reset error code */
  248. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  249. /* Wait for last operation to be completed */
  250. if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
  251. {
  252. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
  253. {
  254. status = HAL_ERROR;
  255. }
  256. }
  257. if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
  258. {
  259. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
  260. {
  261. status = HAL_ERROR;
  262. }
  263. }
  264. if (status != HAL_OK)
  265. {
  266. /* Process Unlocked */
  267. __HAL_UNLOCK(&pFlash);
  268. }
  269. else
  270. {
  271. if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
  272. {
  273. /* Enable End of Operation and Error interrupts for Bank 1 */
  274. __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
  275. FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
  276. }
  277. if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
  278. {
  279. /* Enable End of Operation and Error interrupts for Bank 2 */
  280. __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
  281. FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
  282. }
  283. if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  284. {
  285. /*Mass erase to be done*/
  286. if(pEraseInit->Banks == FLASH_BANK_1)
  287. {
  288. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE_BANK1;
  289. }
  290. else if(pEraseInit->Banks == FLASH_BANK_2)
  291. {
  292. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE_BANK2;
  293. }
  294. else
  295. {
  296. pFlash.ProcedureOnGoing = FLASH_PROC_ALLBANK_MASSERASE;
  297. }
  298. FLASH_MassErase(pEraseInit->VoltageRange, pEraseInit->Banks);
  299. }
  300. else
  301. {
  302. /* Erase by sector to be done */
  303. if(pEraseInit->Banks == FLASH_BANK_1)
  304. {
  305. pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK1;
  306. }
  307. else
  308. {
  309. pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK2;
  310. }
  311. pFlash.NbSectorsToErase = pEraseInit->NbSectors;
  312. pFlash.Sector = pEraseInit->Sector;
  313. pFlash.VoltageForErase = pEraseInit->VoltageRange;
  314. /* Erase first sector and wait for IT */
  315. FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->Banks, pEraseInit->VoltageRange);
  316. }
  317. }
  318. return status;
  319. }
  320. /**
  321. * @brief Program option bytes
  322. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  323. * contains the configuration information for the programming.
  324. *
  325. * @retval HAL Status
  326. */
  327. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  328. {
  329. HAL_StatusTypeDef status;
  330. /* Check the parameters */
  331. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  332. /* Process Locked */
  333. __HAL_LOCK(&pFlash);
  334. /* Reset Error Code */
  335. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  336. /* Wait for last operation to be completed */
  337. if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
  338. {
  339. status = HAL_ERROR;
  340. }
  341. else if(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
  342. {
  343. status = HAL_ERROR;
  344. }
  345. else
  346. {
  347. status = HAL_OK;
  348. }
  349. if(status == HAL_OK)
  350. {
  351. /*Write protection configuration*/
  352. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  353. {
  354. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  355. if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  356. {
  357. /*Enable of Write protection on the selected Sector*/
  358. FLASH_OB_EnableWRP(pOBInit->WRPSector,pOBInit->Banks);
  359. }
  360. else
  361. {
  362. /*Disable of Write protection on the selected Sector*/
  363. FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
  364. }
  365. }
  366. /* Read protection configuration */
  367. if((pOBInit->OptionType & OPTIONBYTE_RDP) != 0U)
  368. {
  369. /* Configure the Read protection level */
  370. FLASH_OB_RDPConfig(pOBInit->RDPLevel);
  371. }
  372. /* User Configuration */
  373. if((pOBInit->OptionType & OPTIONBYTE_USER) != 0U)
  374. {
  375. /* Configure the user option bytes */
  376. FLASH_OB_UserConfig(pOBInit->USERType, pOBInit->USERConfig);
  377. }
  378. /* PCROP Configuration */
  379. if((pOBInit->OptionType & OPTIONBYTE_PCROP) != 0U)
  380. {
  381. assert_param(IS_FLASH_BANK(pOBInit->Banks));
  382. /*Configure the Proprietary code readout protection */
  383. FLASH_OB_PCROPConfig(pOBInit->PCROPConfig, pOBInit->PCROPStartAddr, pOBInit->PCROPEndAddr, pOBInit->Banks);
  384. }
  385. /* BOR Level configuration */
  386. if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
  387. {
  388. FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
  389. }
  390. #if defined(DUAL_CORE)
  391. /*CM7 Boot Address configuration*/
  392. if((pOBInit->OptionType & OPTIONBYTE_CM7_BOOTADD) == OPTIONBYTE_CM7_BOOTADD)
  393. {
  394. FLASH_OB_BootAddConfig(pOBInit->BootConfig, pOBInit->BootAddr0, pOBInit->BootAddr1);
  395. }
  396. /*CM4 Boot Address configuration*/
  397. if((pOBInit->OptionType & OPTIONBYTE_CM4_BOOTADD) == OPTIONBYTE_CM4_BOOTADD)
  398. {
  399. FLASH_OB_CM4BootAddConfig(pOBInit->CM4BootConfig, pOBInit->CM4BootAddr0, pOBInit->CM4BootAddr1);
  400. }
  401. #else /* Single Core*/
  402. /*Boot Address configuration*/
  403. if((pOBInit->OptionType & OPTIONBYTE_BOOTADD) == OPTIONBYTE_BOOTADD)
  404. {
  405. FLASH_OB_BootAddConfig(pOBInit->BootConfig, pOBInit->BootAddr0, pOBInit->BootAddr1);
  406. }
  407. #endif /*DUAL_CORE*/
  408. /*Bank1 secure area configuration*/
  409. if((pOBInit->OptionType & OPTIONBYTE_SECURE_AREA) == OPTIONBYTE_SECURE_AREA)
  410. {
  411. FLASH_OB_SecureAreaConfig(pOBInit->SecureAreaConfig, pOBInit->SecureAreaStartAddr, pOBInit->SecureAreaEndAddr,pOBInit->Banks);
  412. }
  413. }
  414. /* Process Unlocked */
  415. __HAL_UNLOCK(&pFlash);
  416. return status;
  417. }
  418. /**
  419. * @brief Get the Option byte configuration
  420. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  421. * contains the configuration information for the programming.
  422. * @note The parameter Banks of the pOBInit structure must be set exclusively to FLASH_BANK_1 or FLASH_BANK_2,
  423. * as this parameter is use to get the given Bank WRP, PCROP and secured area configuration.
  424. *
  425. * @retval None
  426. */
  427. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  428. {
  429. pOBInit->OptionType = (OPTIONBYTE_USER | OPTIONBYTE_RDP | OPTIONBYTE_BOR);
  430. /* Get Read protection level */
  431. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  432. /* Get the user option bytes */
  433. pOBInit->USERConfig = FLASH_OB_GetUser();
  434. /*Get BOR Level*/
  435. pOBInit->BORLevel = FLASH_OB_GetBOR();
  436. if ((pOBInit->Banks == FLASH_BANK_1) || (pOBInit->Banks == FLASH_BANK_2))
  437. {
  438. pOBInit->OptionType |= (OPTIONBYTE_WRP | OPTIONBYTE_PCROP | OPTIONBYTE_SECURE_AREA);
  439. /* Get write protection on the selected area */
  440. FLASH_OB_GetWRP(&(pOBInit->WRPState), &(pOBInit->WRPSector), pOBInit->Banks);
  441. /* Get the Proprietary code readout protection */
  442. FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr), pOBInit->Banks);
  443. /*Get Bank Secure area*/
  444. FLASH_OB_GetSecureArea(&(pOBInit->SecureAreaConfig), &(pOBInit->SecureAreaStartAddr), &(pOBInit->SecureAreaEndAddr), pOBInit->Banks);
  445. }
  446. /*Get Boot Address*/
  447. FLASH_OB_GetBootAdd(&(pOBInit->BootAddr0), &(pOBInit->BootAddr1));
  448. #if defined(DUAL_CORE)
  449. pOBInit->OptionType |= OPTIONBYTE_CM7_BOOTADD | OPTIONBYTE_CM4_BOOTADD;
  450. /*Get CM4 Boot Address*/
  451. FLASH_OB_GetCM4BootAdd(&(pOBInit->CM4BootAddr0), &(pOBInit->CM4BootAddr1));
  452. #else
  453. pOBInit->OptionType |= OPTIONBYTE_BOOTADD;
  454. #endif /*DUAL_CORE*/
  455. }
  456. /**
  457. * @brief Unlock the FLASH Bank1 control registers access
  458. * @retval HAL Status
  459. */
  460. HAL_StatusTypeDef HAL_FLASHEx_Unlock_Bank1(void)
  461. {
  462. if(READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
  463. {
  464. /* Authorize the FLASH Bank1 Registers access */
  465. WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
  466. WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
  467. /* Verify Flash Bank1 is unlocked */
  468. if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
  469. {
  470. return HAL_ERROR;
  471. }
  472. }
  473. return HAL_OK;
  474. }
  475. /**
  476. * @brief Locks the FLASH Bank1 control registers access
  477. * @retval HAL Status
  478. */
  479. HAL_StatusTypeDef HAL_FLASHEx_Lock_Bank1(void)
  480. {
  481. /* Set the LOCK Bit to lock the FLASH Bank1 Registers access */
  482. SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
  483. return HAL_OK;
  484. }
  485. /**
  486. * @brief Unlock the FLASH Bank2 control registers access
  487. * @retval HAL Status
  488. */
  489. HAL_StatusTypeDef HAL_FLASHEx_Unlock_Bank2(void)
  490. {
  491. if(READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
  492. {
  493. /* Authorize the FLASH Bank2 Registers access */
  494. WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
  495. WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
  496. /* Verify Flash Bank1 is unlocked */
  497. if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
  498. {
  499. return HAL_ERROR;
  500. }
  501. }
  502. return HAL_OK;
  503. }
  504. /**
  505. * @brief Locks the FLASH Bank2 control registers access
  506. * @retval HAL Status
  507. */
  508. HAL_StatusTypeDef HAL_FLASHEx_Lock_Bank2(void)
  509. {
  510. /* Set the LOCK Bit to lock the FLASH Bank2 Registers access */
  511. SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
  512. return HAL_OK;
  513. }
  514. /*
  515. * @brief Perform a CRC computation on the specified FLASH memory area
  516. * @param pCRCInit pointer to an FLASH_CRCInitTypeDef structure that
  517. * contains the configuration information for the CRC computation.
  518. * @note CRC computation uses CRC-32 (Ethernet) polynomial 0x4C11DB7
  519. * @note The application should avoid running a CRC on PCROP or secure-only
  520. * user Flash memory area since it may alter the expected CRC value.
  521. * A special error flag (CRC read error: CRCRDERR) can be used to
  522. * detect such a case.
  523. * @retval HAL Status
  524. */
  525. HAL_StatusTypeDef HAL_FLASHEx_ComputeCRC(FLASH_CRCInitTypeDef *pCRCInit, uint32_t *CRC_Result)
  526. {
  527. HAL_StatusTypeDef status;
  528. uint32_t sector_index;
  529. /* Check the parameters */
  530. assert_param(IS_FLASH_BANK_EXCLUSIVE(pCRCInit->Bank));
  531. assert_param(IS_FLASH_TYPECRC(pCRCInit->TypeCRC));
  532. /* Wait for OB change operation to be completed */
  533. status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  534. if (status == HAL_OK)
  535. {
  536. if (pCRCInit->Bank == FLASH_BANK_1)
  537. {
  538. /* Enable CRC feature */
  539. FLASH->CR1 |= FLASH_CR_CRC_EN;
  540. /* Clear CRC flags in Status Register: CRC end of calculation and CRC read error */
  541. FLASH->CCR1 |= (FLASH_CCR_CLR_CRCEND | FLASH_CCR_CLR_CRCRDERR);
  542. /* Clear current CRC result, program burst size and define memory area on which CRC has to be computed */
  543. FLASH->CRCCR1 |= FLASH_CRCCR_CLEAN_CRC | pCRCInit->BurstSize | pCRCInit->TypeCRC;
  544. if (pCRCInit->TypeCRC == FLASH_CRC_SECTORS)
  545. {
  546. /* Clear sectors list */
  547. FLASH->CRCCR1 |= FLASH_CRCCR_CLEAN_SECT;
  548. /* Select CRC sectors */
  549. for(sector_index = pCRCInit->Sector; sector_index < (pCRCInit->NbSectors + pCRCInit->Sector); sector_index++)
  550. {
  551. FLASH_CRC_AddSector(sector_index, FLASH_BANK_1);
  552. }
  553. }
  554. else if (pCRCInit->TypeCRC == FLASH_CRC_BANK)
  555. {
  556. /* Enable Bank 1 CRC select bit */
  557. FLASH->CRCCR1 |= FLASH_CRCCR_ALL_BANK;
  558. }
  559. else
  560. {
  561. /* Select CRC start and end addresses */
  562. FLASH_CRC_SelectAddress(pCRCInit->CRCStartAddr, pCRCInit->CRCEndAddr, FLASH_BANK_1);
  563. }
  564. /* Start the CRC calculation */
  565. FLASH->CRCCR1 |= FLASH_CRCCR_START_CRC;
  566. /* Wait on CRC busy flag */
  567. status = FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
  568. /* Return CRC result */
  569. (*CRC_Result) = FLASH->CRCDATA;
  570. /* Disable CRC feature */
  571. FLASH->CR1 &= (~FLASH_CR_CRC_EN);
  572. /* Clear CRC flags */
  573. __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCEND_BANK1 | FLASH_FLAG_CRCRDERR_BANK1);
  574. }
  575. else
  576. {
  577. /* Enable CRC feature */
  578. FLASH->CR2 |= FLASH_CR_CRC_EN;
  579. /* Clear CRC flags in Status Register: CRC end of calculation and CRC read error */
  580. FLASH->CCR2 |= (FLASH_CCR_CLR_CRCEND | FLASH_CCR_CLR_CRCRDERR);
  581. /* Clear current CRC result, program burst size and define memory area on which CRC has to be computed */
  582. FLASH->CRCCR2 |= FLASH_CRCCR_CLEAN_CRC | pCRCInit->BurstSize | pCRCInit->TypeCRC;
  583. if (pCRCInit->TypeCRC == FLASH_CRC_SECTORS)
  584. {
  585. /* Clear sectors list */
  586. FLASH->CRCCR2 |= FLASH_CRCCR_CLEAN_SECT;
  587. /* Add CRC sectors */
  588. for(sector_index = pCRCInit->Sector; sector_index < (pCRCInit->NbSectors + pCRCInit->Sector); sector_index++)
  589. {
  590. FLASH_CRC_AddSector(sector_index, FLASH_BANK_2);
  591. }
  592. }
  593. else if (pCRCInit->TypeCRC == FLASH_CRC_BANK)
  594. {
  595. /* Enable Bank 2 CRC select bit */
  596. FLASH->CRCCR2 |= FLASH_CRCCR_ALL_BANK;
  597. }
  598. else
  599. {
  600. /* Select CRC start and end addresses */
  601. FLASH_CRC_SelectAddress(pCRCInit->CRCStartAddr, pCRCInit->CRCEndAddr, FLASH_BANK_2);
  602. }
  603. /* Start the CRC calculation */
  604. FLASH->CRCCR2 |= FLASH_CRCCR_START_CRC;
  605. /* Wait on CRC busy flag */
  606. status = FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
  607. /* Return CRC result */
  608. (*CRC_Result) = FLASH->CRCDATA;
  609. /* Disable CRC feature */
  610. FLASH->CR2 &= (~FLASH_CR_CRC_EN);
  611. /* Clear CRC flags */
  612. __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCEND_BANK2 | FLASH_FLAG_CRCRDERR_BANK2);
  613. }
  614. }
  615. return status;
  616. }
  617. /**
  618. * @}
  619. */
  620. /**
  621. * @}
  622. */
  623. /* Private functions ---------------------------------------------------------*/
  624. /** @addtogroup FLASHEx_Private_Functions
  625. * @{
  626. */
  627. /**
  628. * @brief Mass erase of FLASH memory
  629. * @param VoltageRange The device program/erase parallelism.
  630. * This parameter can be one of the following values:
  631. * @arg FLASH_VOLTAGE_RANGE_1 : Flash program/erase by 8 bits
  632. * @arg FLASH_VOLTAGE_RANGE_2 : Flash program/erase by 16 bits
  633. * @arg FLASH_VOLTAGE_RANGE_3 : Flash program/erase by 32 bits
  634. * @arg FLASH_VOLTAGE_RANGE_4 : Flash program/erase by 64 bits
  635. *
  636. * @param Banks Banks to be erased
  637. * This parameter can be one of the following values:
  638. * @arg FLASH_BANK_1: Bank1 to be erased
  639. * @arg FLASH_BANK_2: Bank2 to be erased
  640. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  641. *
  642. * @retval HAL Status
  643. */
  644. static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks)
  645. {
  646. /* Check the parameters */
  647. assert_param(IS_VOLTAGERANGE(VoltageRange));
  648. assert_param(IS_FLASH_BANK(Banks));
  649. /* Flash Mass Erase */
  650. if((Banks & FLASH_BANK_BOTH) == FLASH_BANK_BOTH)
  651. {
  652. /* Reset Program/erase VoltageRange for Bank1 and Bank2 */
  653. FLASH->CR1 &= (~FLASH_CR_PSIZE);
  654. FLASH->CR2 &= (~FLASH_CR_PSIZE);
  655. /* Set voltage range */
  656. FLASH->CR1 |= VoltageRange;
  657. FLASH->CR2 |= VoltageRange;
  658. /* Set Mass Erase Bit */
  659. FLASH->OPTCR |= FLASH_OPTCR_MER;
  660. }
  661. else
  662. {
  663. /* Proceed to erase Flash Bank */
  664. if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  665. {
  666. /* Reset Program/erase VoltageRange for Bank1 */
  667. FLASH->CR1 &= (~FLASH_CR_PSIZE);
  668. /* Bank1 will be erased, and set voltage range */
  669. FLASH->CR1 |= FLASH_CR_BER | VoltageRange;
  670. FLASH->CR1 |= FLASH_CR_START;
  671. }
  672. if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  673. {
  674. /* Reset Program/erase VoltageRange for Bank2 */
  675. FLASH->CR2 &= (~FLASH_CR_PSIZE);
  676. /* Bank2 will be erased, and set voltage range */
  677. FLASH->CR2 |= FLASH_CR_BER | VoltageRange;
  678. FLASH->CR2 |= FLASH_CR_START;
  679. }
  680. }
  681. }
  682. /**
  683. * @brief Erase the specified FLASH memory sector
  684. * @param Sector FLASH sector to erase
  685. * This parameter can be a value of @ref FLASH_Sectors
  686. * @param Banks Banks to be erased
  687. * This parameter can be one of the following values:
  688. * @arg FLASH_BANK_1: Bank1 to be erased
  689. * @arg FLASH_BANK_2: Bank2 to be erased
  690. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  691. * @param VoltageRange The device program/erase parallelism.
  692. * This parameter can be one of the following values:
  693. * @arg FLASH_VOLTAGE_RANGE_1 : Flash program/erase by 8 bits
  694. * @arg FLASH_VOLTAGE_RANGE_2 : Flash program/erase by 16 bits
  695. * @arg FLASH_VOLTAGE_RANGE_3 : Flash program/erase by 32 bits
  696. * @arg FLASH_VOLTAGE_RANGE_4 : Flash program/erase by 64 bits
  697. *
  698. * @retval None
  699. */
  700. void FLASH_Erase_Sector(uint32_t Sector, uint32_t Banks, uint32_t VoltageRange)
  701. {
  702. assert_param(IS_FLASH_SECTOR(Sector));
  703. assert_param(IS_FLASH_BANK_EXCLUSIVE(Banks));
  704. assert_param(IS_VOLTAGERANGE(VoltageRange));
  705. if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  706. {
  707. /* reset Program/erase VoltageRange for Bank1 */
  708. FLASH->CR1 &= ~(FLASH_CR_PSIZE | FLASH_CR_SNB);
  709. FLASH->CR1 |= (FLASH_CR_SER | VoltageRange | (Sector << FLASH_CR_SNB_Pos));
  710. FLASH->CR1 |= FLASH_CR_START;
  711. }
  712. if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  713. {
  714. /* reset Program/erase VoltageRange for Bank2 */
  715. FLASH->CR2 &= ~(FLASH_CR_PSIZE | FLASH_CR_SNB);
  716. FLASH->CR2 |= (FLASH_CR_SER | VoltageRange | (Sector << FLASH_CR_SNB_Pos));
  717. FLASH->CR2 |= FLASH_CR_START;
  718. }
  719. }
  720. /**
  721. * @brief Enable the write protection of the desired bank1 or bank 2 sectors
  722. * @param WRPSector specifies the sector(s) to be write protected.
  723. * This parameter can be one of the following values:
  724. * @arg WRPSector: A combination of OB_WRP_SECTOR_0 to OB_WRP_SECTOR_7 or OB_WRP_SECTOR_All
  725. *
  726. * @param Banks the specific bank to apply WRP sectors
  727. * This parameter can be one of the following values:
  728. * @arg FLASH_BANK_1: enable WRP on specified bank1 sectors
  729. * @arg FLASH_BANK_2: enable WRP on specified bank2 sectors
  730. * @arg FLASH_BANK_BOTH: enable WRP on both bank1 and bank2 specified sectors
  731. *
  732. * @retval HAL FLASH State
  733. */
  734. static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
  735. {
  736. /* Check the parameters */
  737. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  738. assert_param(IS_FLASH_BANK(Banks));
  739. if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  740. {
  741. /* Enable Write Protection for bank 1 */
  742. FLASH->WPSN_PRG1 &= (~(WRPSector & FLASH_WPSN_WRPSN));
  743. }
  744. if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  745. {
  746. /* Enable Write Protection for bank 2 */
  747. FLASH->WPSN_PRG2 &= (~(WRPSector & FLASH_WPSN_WRPSN));
  748. }
  749. }
  750. /**
  751. * @brief Disable the write protection of the desired bank1 or bank 2 sectors
  752. * @param WRPSector specifies the sector(s) to disable write protection.
  753. * This parameter can be one of the following values:
  754. * @arg WRPSector: A combination of FLASH_OB_WRP_SECTOR_0 to FLASH_OB_WRP_SECTOR_7 or FLASH_OB_WRP_SECTOR_All
  755. *
  756. * @param Banks the specific bank to apply WRP sectors
  757. * This parameter can be one of the following values:
  758. * @arg FLASH_BANK_1: disable WRP on specified bank1 sectors
  759. * @arg FLASH_BANK_2: disable WRP on specified bank2 sectors
  760. * @arg FLASH_BANK_BOTH: disable WRP on both bank1 and bank2 specified sectors
  761. *
  762. * @retval HAL FLASH State
  763. */
  764. static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
  765. {
  766. /* Check the parameters */
  767. assert_param(IS_OB_WRP_SECTOR(WRPSector));
  768. assert_param(IS_FLASH_BANK(Banks));
  769. if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  770. {
  771. /* Disable Write Protection for bank 1 */
  772. FLASH->WPSN_PRG1 |= (WRPSector & FLASH_WPSN_WRPSN);
  773. }
  774. if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  775. {
  776. /* Disable Write Protection for bank 2 */
  777. FLASH->WPSN_PRG2 |= (WRPSector & FLASH_WPSN_WRPSN);
  778. }
  779. }
  780. /**
  781. * @brief Get the write protection of the given bank 1 or bank 2 sectors
  782. * @param WRPState gives the write protection state on the given bank.
  783. * This parameter can be one of the following values:
  784. * @arg WRPState: OB_WRPSTATE_DISABLE or OB_WRPSTATE_ENABLE
  785. * @param WRPSector gives the write protected sector(s) on the given bank .
  786. * This parameter can be one of the following values:
  787. * @arg WRPSector: A combination of FLASH_OB_WRP_SECTOR_0 to FLASH_OB_WRP_SECTOR_7 or FLASH_OB_WRP_SECTOR_All
  788. *
  789. * @param Bank the specific bank to apply WRP sectors
  790. * This parameter can be exclusively one of the following values:
  791. * @arg FLASH_BANK_1: Get bank1 WRP sectors
  792. * @arg FLASH_BANK_2: Get bank2 WRP sectors
  793. * @arg FLASH_BANK_BOTH: note allowed in this functions
  794. *
  795. * @retval HAL FLASH State
  796. */
  797. static void FLASH_OB_GetWRP(uint32_t *WRPState, uint32_t *WRPSector, uint32_t Bank)
  798. {
  799. uint32_t regvalue = 0U;
  800. if((Bank & FLASH_BANK_BOTH) == FLASH_BANK_1)
  801. {
  802. regvalue = FLASH->WPSN_CUR1;
  803. }
  804. if((Bank & FLASH_BANK_BOTH) == FLASH_BANK_2)
  805. {
  806. regvalue = FLASH->WPSN_CUR2;
  807. }
  808. (*WRPSector) = (~regvalue) & FLASH_WPSN_WRPSN;
  809. if(*WRPSector == 0U)
  810. {
  811. (*WRPState) = OB_WRPSTATE_DISABLE;
  812. }
  813. else
  814. {
  815. (*WRPState) = OB_WRPSTATE_ENABLE;
  816. }
  817. }
  818. /**
  819. * @brief Set the read protection level.
  820. *
  821. * @note To configure the RDP level, the option lock bit OPTLOCK must be
  822. * cleared with the call of the HAL_FLASH_OB_Unlock() function.
  823. * @note To validate the RDP level, the option bytes must be reloaded
  824. * through the call of the HAL_FLASH_OB_Launch() function.
  825. * @note !!! Warning : When enabling OB_RDP level 2 it's no more possible
  826. * to go back to level 1 or 0 !!!
  827. *
  828. * @param RDPLevel specifies the read protection level.
  829. * This parameter can be one of the following values:
  830. * @arg OB_RDP_LEVEL_0: No protection
  831. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  832. * @arg OB_RDP_LEVEL_2: Full chip protection
  833. *
  834. * @retval HAL status
  835. */
  836. static void FLASH_OB_RDPConfig(uint32_t RDPLevel)
  837. {
  838. /* Check the parameters */
  839. assert_param(IS_OB_RDP_LEVEL(RDPLevel));
  840. /* Configure the RDP level in the option bytes register */
  841. MODIFY_REG(FLASH->OPTSR_PRG, FLASH_OPTSR_RDP, RDPLevel);
  842. }
  843. /**
  844. * @brief Get the read protection level.
  845. * @retval RDPLevel specifies the read protection level.
  846. * This return value can be one of the following values:
  847. * @arg OB_RDP_LEVEL_0: No protection
  848. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  849. * @arg OB_RDP_LEVEL_2: Full chip protection
  850. */
  851. static uint32_t FLASH_OB_GetRDP(void)
  852. {
  853. uint32_t rdp_level = READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_RDP);
  854. if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
  855. {
  856. return (OB_RDP_LEVEL_1);
  857. }
  858. else
  859. {
  860. return rdp_level;
  861. }
  862. }
  863. #if defined(DUAL_CORE)
  864. /**
  865. * @brief Program the FLASH User Option Byte.
  866. *
  867. * @note To configure the user option bytes, the option lock bit OPTLOCK must
  868. * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
  869. *
  870. * @note To validate the user option bytes, the option bytes must be reloaded
  871. * through the call of the HAL_FLASH_OB_Launch() function.
  872. *
  873. * @param UserType The FLASH User Option Bytes to be modified :
  874. * a combination of @ref FLASHEx_OB_USER_Type
  875. *
  876. * @param UserConfig The FLASH User Option Bytes values:
  877. * IWDG1_SW(Bit4), IWDG2_SW(Bit 5), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
  878. * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
  879. * SECURITY(Bit 21), BCM4(Bit 22), BCM7(Bit 23), nRST_STOP_D2(Bit 24),
  880. * nRST_STDY_D2(Bit 25), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
  881. *
  882. * @retval HAL status
  883. */
  884. #else
  885. /**
  886. * @brief Program the FLASH User Option Byte.
  887. *
  888. * @note To configure the user option bytes, the option lock bit OPTLOCK must
  889. * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
  890. *
  891. * @note To validate the user option bytes, the option bytes must be reloaded
  892. * through the call of the HAL_FLASH_OB_Launch() function.
  893. *
  894. * @param UserType The FLASH User Option Bytes to be modified :
  895. * a combination of @arg FLASHEx_OB_USER_Type
  896. *
  897. * @param UserConfig The FLASH User Option Bytes values:
  898. * IWDG_SW(Bit4), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
  899. * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
  900. * SECURITY(Bit 21), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
  901. *
  902. * @retval HAL status
  903. */
  904. #endif /*DUAL_CORE*/
  905. static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
  906. {
  907. uint32_t optr_reg_val = 0;
  908. uint32_t optr_reg_mask = 0;
  909. /* Check the parameters */
  910. assert_param(IS_OB_USER_TYPE(UserType));
  911. if((UserType & OB_USER_IWDG1_SW) != 0U)
  912. {
  913. /* IWDG_HW option byte should be modified */
  914. assert_param(IS_OB_IWDG1_SOURCE(UserConfig & FLASH_OPTSR_IWDG1_SW));
  915. /* Set value and mask for IWDG_HW option byte */
  916. optr_reg_val |= (UserConfig & FLASH_OPTSR_IWDG1_SW);
  917. optr_reg_mask |= FLASH_OPTSR_IWDG1_SW;
  918. }
  919. #if defined(DUAL_CORE)
  920. if((UserType & OB_USER_IWDG2_SW) != 0U)
  921. {
  922. /* IWDG2_SW option byte should be modified */
  923. assert_param(IS_OB_IWDG2_SOURCE(UserConfig & FLASH_OPTSR_IWDG2_SW));
  924. /* Set value and mask for IWDG2_SW option byte */
  925. optr_reg_val |= (UserConfig & FLASH_OPTSR_IWDG2_SW);
  926. optr_reg_mask |= FLASH_OPTSR_IWDG2_SW;
  927. }
  928. #endif /*DUAL_CORE*/
  929. if((UserType & OB_USER_NRST_STOP_D1) != 0U)
  930. {
  931. /* NRST_STOP option byte should be modified */
  932. assert_param(IS_OB_STOP_D1_RESET(UserConfig & FLASH_OPTSR_NRST_STOP_D1));
  933. /* Set value and mask for NRST_STOP option byte */
  934. optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STOP_D1);
  935. optr_reg_mask |= FLASH_OPTSR_NRST_STOP_D1;
  936. }
  937. if((UserType & OB_USER_NRST_STDBY_D1) != 0U)
  938. {
  939. /* NRST_STDBY option byte should be modified */
  940. assert_param(IS_OB_STDBY_D1_RESET(UserConfig & FLASH_OPTSR_NRST_STBY_D1));
  941. /* Set value and mask for NRST_STDBY option byte */
  942. optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STBY_D1);
  943. optr_reg_mask |= FLASH_OPTSR_NRST_STBY_D1;
  944. }
  945. if((UserType & OB_USER_IWDG_STOP) != 0U)
  946. {
  947. /* IWDG_STOP option byte should be modified */
  948. assert_param(IS_OB_USER_IWDG_STOP(UserConfig & FLASH_OPTSR_FZ_IWDG_STOP));
  949. /* Set value and mask for IWDG_STOP option byte */
  950. optr_reg_val |= (UserConfig & FLASH_OPTSR_FZ_IWDG_STOP);
  951. optr_reg_mask |= FLASH_OPTSR_FZ_IWDG_STOP;
  952. }
  953. if((UserType & OB_USER_IWDG_STDBY) != 0U)
  954. {
  955. /* IWDG_STDBY option byte should be modified */
  956. assert_param(IS_OB_USER_IWDG_STDBY(UserConfig & FLASH_OPTSR_FZ_IWDG_SDBY));
  957. /* Set value and mask for IWDG_STDBY option byte */
  958. optr_reg_val |= (UserConfig & FLASH_OPTSR_FZ_IWDG_SDBY);
  959. optr_reg_mask |= FLASH_OPTSR_FZ_IWDG_SDBY;
  960. }
  961. if((UserType & OB_USER_ST_RAM_SIZE) != 0U)
  962. {
  963. /* ST_RAM_SIZE option byte should be modified */
  964. assert_param(IS_OB_USER_ST_RAM_SIZE(UserConfig & FLASH_OPTSR_ST_RAM_SIZE));
  965. /* Set value and mask for ST_RAM_SIZE option byte */
  966. optr_reg_val |= (UserConfig & FLASH_OPTSR_ST_RAM_SIZE);
  967. optr_reg_mask |= FLASH_OPTSR_ST_RAM_SIZE;
  968. }
  969. if((UserType & OB_USER_SECURITY) != 0U)
  970. {
  971. /* SECURITY option byte should be modified */
  972. assert_param(IS_OB_USER_SECURITY(UserConfig & FLASH_OPTSR_SECURITY));
  973. /* Set value and mask for SECURITY option byte */
  974. optr_reg_val |= (UserConfig & FLASH_OPTSR_SECURITY);
  975. optr_reg_mask |= FLASH_OPTSR_SECURITY;
  976. }
  977. #if defined(DUAL_CORE)
  978. if((UserType & OB_USER_BCM4) != 0U)
  979. {
  980. /* BCM4 option byte should be modified */
  981. assert_param(IS_OB_USER_BCM4(UserConfig & FLASH_OPTSR_BCM4));
  982. /* Set value and mask for BCM4 option byte */
  983. optr_reg_val |= (UserConfig & FLASH_OPTSR_BCM4);
  984. optr_reg_mask |= FLASH_OPTSR_BCM4;
  985. }
  986. if((UserType & OB_USER_BCM7) != 0U)
  987. {
  988. /* BCM7 option byte should be modified */
  989. assert_param(IS_OB_USER_BCM7(UserConfig & FLASH_OPTSR_BCM7));
  990. /* Set value and mask for BCM7 option byte */
  991. optr_reg_val |= (UserConfig & FLASH_OPTSR_BCM7);
  992. optr_reg_mask |= FLASH_OPTSR_BCM7;
  993. }
  994. if((UserType & OB_USER_NRST_STOP_D2) != 0U)
  995. {
  996. /* NRST_STOP option byte should be modified */
  997. assert_param(IS_OB_STOP_D2_RESET(UserConfig & FLASH_OPTSR_NRST_STOP_D2));
  998. /* Set value and mask for NRST_STOP option byte */
  999. optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STOP_D2);
  1000. optr_reg_mask |= FLASH_OPTSR_NRST_STOP_D2;
  1001. }
  1002. if((UserType & OB_USER_NRST_STDBY_D2) != 0U)
  1003. {
  1004. /* NRST_STDBY option byte should be modified */
  1005. assert_param(IS_OB_STDBY_D2_RESET(UserConfig & FLASH_OPTSR_NRST_STBY_D2));
  1006. /* Set value and mask for NRST_STDBY option byte */
  1007. optr_reg_val |= (UserConfig & FLASH_OPTSR_NRST_STBY_D2);
  1008. optr_reg_mask |= FLASH_OPTSR_NRST_STBY_D2;
  1009. }
  1010. #endif /*DUAL_CORE*/
  1011. if((UserType & OB_USER_SWAP_BANK) != 0U)
  1012. {
  1013. /* SWAP_BANK_OPT option byte should be modified */
  1014. assert_param(IS_OB_USER_SWAP_BANK(UserConfig & FLASH_OPTSR_SWAP_BANK_OPT));
  1015. /* Set value and mask for SWAP_BANK_OPT option byte */
  1016. optr_reg_val |= (UserConfig & FLASH_OPTSR_SWAP_BANK_OPT);
  1017. optr_reg_mask |= FLASH_OPTSR_SWAP_BANK_OPT;
  1018. }
  1019. if((UserType & OB_USER_IOHSLV) != 0U)
  1020. {
  1021. /* IOHSLV_OPT option byte should be modified */
  1022. assert_param(IS_OB_USER_IOHSLV(UserConfig & FLASH_OPTSR_IO_HSLV));
  1023. /* Set value and mask for IOHSLV_OPT option byte */
  1024. optr_reg_val |= (UserConfig & FLASH_OPTSR_IO_HSLV);
  1025. optr_reg_mask |= FLASH_OPTSR_IO_HSLV;
  1026. }
  1027. /* Configure the option bytes register */
  1028. MODIFY_REG(FLASH->OPTSR_PRG, optr_reg_mask, optr_reg_val);
  1029. }
  1030. #if defined(DUAL_CORE)
  1031. /**
  1032. * @brief Return the FLASH User Option Byte value.
  1033. * @retval The FLASH User Option Bytes values
  1034. * IWDG1_SW(Bit4), IWDG2_SW(Bit 5), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
  1035. * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
  1036. * SECURITY(Bit 21), BCM4(Bit 22), BCM7(Bit 23), nRST_STOP_D2(Bit 24),
  1037. * nRST_STDY_D2(Bit 25), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
  1038. */
  1039. #else
  1040. /**
  1041. * @brief Return the FLASH User Option Byte value.
  1042. * @retval The FLASH User Option Bytes values
  1043. * IWDG_SW(Bit4), nRST_STOP_D1(Bit 6), nRST_STDY_D1(Bit 7),
  1044. * FZ_IWDG_STOP(Bit 17), FZ_IWDG_SDBY(Bit 18), ST_RAM_SIZE(Bit[19:20]),
  1045. * SECURITY(Bit 21), IO_HSLV (Bit 29) and SWAP_BANK_OPT(Bit 31).
  1046. */
  1047. #endif /*DUAL_CORE*/
  1048. static uint32_t FLASH_OB_GetUser(void)
  1049. {
  1050. uint32_t userConfig = READ_REG(FLASH->OPTSR_CUR);
  1051. userConfig &= (~(FLASH_OPTSR_BOR_LEV | FLASH_OPTSR_RDP));
  1052. return userConfig;
  1053. }
  1054. /**
  1055. * @brief Configure the Proprietary code readout protection of the desired addresses
  1056. *
  1057. * @note To configure the PCROP options, the option lock bit OPTLOCK must be
  1058. * cleared with the call of the HAL_FLASH_OB_Unlock() function.
  1059. * @note To validate the PCROP options, the option bytes must be reloaded
  1060. * through the call of the HAL_FLASH_OB_Launch() function.
  1061. *
  1062. * @param PCROPConfig specifies if the PCROP area for the given Bank shall be erased or not
  1063. * when RDP level decreased from Level 1 to Level 0, or after a bank erase with protection removal
  1064. * This parameter must be a value of @arg FLASHEx_OB_PCROP_RDP enumeration
  1065. *
  1066. * @param PCROPStartAddr specifies the start address of the Proprietary code readout protection
  1067. * This parameter can be an address between begin and end of the bank
  1068. *
  1069. * @param PCROPEndAddr specifies the end address of the Proprietary code readout protection
  1070. * This parameter can be an address between PCROPStartAddr and end of the bank
  1071. *
  1072. * @param Banks the specific bank to apply PCROP protection
  1073. * This parameter can be one of the following values:
  1074. * @arg FLASH_BANK_1: PCROP on specified bank1 area
  1075. * @arg FLASH_BANK_2: PCROP on specified bank2 area
  1076. * @arg FLASH_BANK_BOTH: PCROP on specified bank1 and bank2 area (same config will be applied on both banks)
  1077. *
  1078. * @retval None
  1079. */
  1080. static void FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks)
  1081. {
  1082. /* Check the parameters */
  1083. assert_param(IS_FLASH_BANK(Banks));
  1084. assert_param(IS_OB_PCROP_RDP(PCROPConfig));
  1085. if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  1086. {
  1087. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(PCROPStartAddr));
  1088. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(PCROPEndAddr));
  1089. /* Configure the Proprietary code readout protection */
  1090. FLASH->PRAR_PRG1 = ((PCROPStartAddr - FLASH_BANK1_BASE) >> 8) | \
  1091. (((PCROPEndAddr - FLASH_BANK1_BASE) >> 8) << FLASH_PRAR_PROT_AREA_END_Pos) | \
  1092. PCROPConfig;
  1093. }
  1094. if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  1095. {
  1096. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(PCROPStartAddr));
  1097. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(PCROPEndAddr));
  1098. /* Configure the Proprietary code readout protection */
  1099. FLASH->PRAR_PRG2 = ((PCROPStartAddr - FLASH_BANK2_BASE) >> 8) | \
  1100. (((PCROPEndAddr - FLASH_BANK2_BASE) >> 8) << FLASH_PRAR_PROT_AREA_END_Pos) | \
  1101. PCROPConfig;
  1102. }
  1103. }
  1104. /**
  1105. * @brief Get the Proprietary code readout protection configuration on a given Bank
  1106. *
  1107. * @param PCROPConfig indicates if the PCROP area for the given Bank shall be erased or not
  1108. * when RDP level decreased from Level 1 to Level 0 or after a bank erase with protection removal
  1109. *
  1110. * @param PCROPStartAddr gives the start address of the Proprietary code readout protection of the bank
  1111. *
  1112. * @param PCROPEndAddr gives the end address of the Proprietary code readout protection of the bank
  1113. *
  1114. * @param Bank the specific bank to apply PCROP protection
  1115. * This parameter can be exclusively one of the following values:
  1116. * @arg FLASH_BANK_1: PCROP on specified bank1 area
  1117. * @arg FLASH_BANK_2: PCROP on specified bank2 area
  1118. * @arg FLASH_BANK_BOTH: is not allowed here
  1119. *
  1120. * @retval None
  1121. */
  1122. static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr, uint32_t *PCROPEndAddr, uint32_t Bank)
  1123. {
  1124. uint32_t regvalue = 0;
  1125. uint32_t bankBase = 0;
  1126. if((Bank & FLASH_BANK_BOTH) == FLASH_BANK_1)
  1127. {
  1128. regvalue = FLASH->PRAR_CUR1;
  1129. bankBase = FLASH_BANK1_BASE;
  1130. }
  1131. if((Bank & FLASH_BANK_BOTH) == FLASH_BANK_2)
  1132. {
  1133. regvalue = FLASH->PRAR_CUR2;
  1134. bankBase = FLASH_BANK2_BASE;
  1135. }
  1136. (*PCROPConfig) = (regvalue & FLASH_PRAR_DMEP);
  1137. (*PCROPStartAddr) = ((regvalue & FLASH_PRAR_PROT_AREA_START) << 8) + bankBase;
  1138. (*PCROPEndAddr) = (regvalue & FLASH_PRAR_PROT_AREA_END) >> FLASH_PRAR_PROT_AREA_END_Pos;
  1139. (*PCROPEndAddr) = ((*PCROPEndAddr) << 8) + bankBase;
  1140. }
  1141. /**
  1142. * @brief Set the BOR Level.
  1143. * @param Level specifies the Option Bytes BOR Reset Level.
  1144. * This parameter can be one of the following values:
  1145. * @arg OB_BOR_LEVEL0: Reset level threshold is set to 1.6V
  1146. * @arg OB_BOR_LEVEL1: Reset level threshold is set to 2.1V
  1147. * @arg OB_BOR_LEVEL2: Reset level threshold is set to 2.4V
  1148. * @arg OB_BOR_LEVEL3: Reset level threshold is set to 2.7V
  1149. * @retval None
  1150. */
  1151. static void FLASH_OB_BOR_LevelConfig(uint32_t Level)
  1152. {
  1153. assert_param(IS_OB_BOR_LEVEL(Level));
  1154. /* Configure BOR_LEV option byte */
  1155. MODIFY_REG(FLASH->OPTSR_PRG, FLASH_OPTSR_BOR_LEV, Level);
  1156. }
  1157. /**
  1158. * @brief Get the BOR Level.
  1159. * @retval The Option Bytes BOR Reset Level.
  1160. * This parameter can be one of the following values:
  1161. * @arg OB_BOR_LEVEL0: Reset level threshold is set to 1.6V
  1162. * @arg OB_BOR_LEVEL1: Reset level threshold is set to 2.1V
  1163. * @arg OB_BOR_LEVEL2: Reset level threshold is set to 2.4V
  1164. * @arg OB_BOR_LEVEL3: Reset level threshold is set to 2.7V
  1165. */
  1166. static uint32_t FLASH_OB_GetBOR(void)
  1167. {
  1168. return (FLASH->OPTSR_CUR & FLASH_OPTSR_BOR_LEV);
  1169. }
  1170. /**
  1171. * @brief Set Boot address
  1172. * @param BootOption Boot address option byte to be programmed,
  1173. * This parameter must be a value of @ref FLASHEx_OB_BOOT_OPTION
  1174. (OB_BOOT_ADD0, OB_BOOT_ADD1 or OB_BOOT_ADD_BOTH)
  1175. *
  1176. * @param BootAddress0 Specifies the Boot Address 0
  1177. * @param BootAddress1 Specifies the Boot Address 1
  1178. * @retval HAL Status
  1179. */
  1180. static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
  1181. {
  1182. /* Check the parameters */
  1183. assert_param(IS_OB_BOOT_ADD_OPTION(BootOption));
  1184. if((BootOption & OB_BOOT_ADD0) == OB_BOOT_ADD0)
  1185. {
  1186. /* Check the parameters */
  1187. assert_param(IS_BOOT_ADDRESS(BootAddress0));
  1188. /* Configure CM7 BOOT ADD0 */
  1189. #if defined(DUAL_CORE)
  1190. MODIFY_REG(FLASH->BOOT7_PRG, FLASH_BOOT7_BCM7_ADD0, (BootAddress0 >> 16));
  1191. #else /* Single Core*/
  1192. MODIFY_REG(FLASH->BOOT_PRG, FLASH_BOOT_ADD0, (BootAddress0 >> 16));
  1193. #endif /* DUAL_CORE */
  1194. }
  1195. if((BootOption & OB_BOOT_ADD1) == OB_BOOT_ADD1)
  1196. {
  1197. /* Check the parameters */
  1198. assert_param(IS_BOOT_ADDRESS(BootAddress1));
  1199. /* Configure CM7 BOOT ADD1 */
  1200. #if defined(DUAL_CORE)
  1201. MODIFY_REG(FLASH->BOOT7_PRG, FLASH_BOOT7_BCM7_ADD1, BootAddress1);
  1202. #else /* Single Core*/
  1203. MODIFY_REG(FLASH->BOOT_PRG, FLASH_BOOT_ADD1, BootAddress1);
  1204. #endif /* DUAL_CORE */
  1205. }
  1206. }
  1207. /**
  1208. * @brief Get Boot address
  1209. * @param BootAddress0 Specifies the Boot Address 0.
  1210. * @param BootAddress1 Specifies the Boot Address 1.
  1211. * @retval HAL Status
  1212. */
  1213. static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
  1214. {
  1215. uint32_t regvalue;
  1216. #if defined(DUAL_CORE)
  1217. regvalue = FLASH->BOOT7_CUR;
  1218. (*BootAddress0) = (regvalue & FLASH_BOOT7_BCM7_ADD0) << 16;
  1219. (*BootAddress1) = (regvalue & FLASH_BOOT7_BCM7_ADD1);
  1220. #else /* Single Core */
  1221. regvalue = FLASH->BOOT_CUR;
  1222. (*BootAddress0) = (regvalue & FLASH_BOOT_ADD0) << 16;
  1223. (*BootAddress1) = (regvalue & FLASH_BOOT_ADD1);
  1224. #endif /* DUAL_CORE */
  1225. }
  1226. #if defined(DUAL_CORE)
  1227. /**
  1228. * @brief Set CM4 Boot address
  1229. * @param BootOption Boot address option byte to be programmed,
  1230. * This parameter must be a value of @ref FLASHEx_OB_BOOT_OPTION
  1231. (OB_BOOT_ADD0, OB_BOOT_ADD1 or OB_BOOT_ADD_BOTH)
  1232. *
  1233. * @param BootAddress0 Specifies the CM4 Boot Address 0.
  1234. * @param BootAddress1 Specifies the CM4 Boot Address 1.
  1235. * @retval HAL Status
  1236. */
  1237. static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
  1238. {
  1239. /* Check the parameters */
  1240. assert_param(IS_OB_BOOT_ADD_OPTION(BootOption));
  1241. if((BootOption & OB_BOOT_ADD0) == OB_BOOT_ADD0)
  1242. {
  1243. /* Check the parameters */
  1244. assert_param(IS_BOOT_ADDRESS(BootAddress0));
  1245. /* Configure CM4 BOOT ADD0 */
  1246. MODIFY_REG(FLASH->BOOT4_PRG, FLASH_BOOT4_BCM4_ADD0, (BootAddress0 >> 16));
  1247. }
  1248. if((BootOption & OB_BOOT_ADD1) == OB_BOOT_ADD1)
  1249. {
  1250. /* Check the parameters */
  1251. assert_param(IS_BOOT_ADDRESS(BootAddress1));
  1252. /* Configure CM4 BOOT ADD1 */
  1253. MODIFY_REG(FLASH->BOOT4_PRG, FLASH_BOOT4_BCM4_ADD1, BootAddress1);
  1254. }
  1255. }
  1256. /**
  1257. * @brief Get CM4 Boot address
  1258. * @param BootAddress0 Specifies the CM4 Boot Address 0.
  1259. * @param BootAddress1 Specifies the CM4 Boot Address 1.
  1260. * @retval HAL Status
  1261. */
  1262. static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
  1263. {
  1264. uint32_t regvalue;
  1265. regvalue = FLASH->BOOT4_CUR;
  1266. (*BootAddress0) = (regvalue & FLASH_BOOT4_BCM4_ADD0) << 16;
  1267. (*BootAddress1) = (regvalue & FLASH_BOOT4_BCM4_ADD1);
  1268. }
  1269. #endif /*DUAL_CORE*/
  1270. /**
  1271. * @brief Set secure area configuration
  1272. * @param SecureAreaConfig specify if the secure area will be deleted or not
  1273. * when RDP level decreased from Level 1 to Level 0 or during a mass erase.
  1274. *
  1275. * @param SecureAreaStartAddr Specifies the secure area start address
  1276. * @param SecureAreaEndAddr Specifies the secure area end address
  1277. * @param Banks the specific bank to apply Security protection
  1278. * This parameter can be one of the following values:
  1279. * @arg FLASH_BANK_1: Secure area on specified bank1 area
  1280. * @arg FLASH_BANK_2: Secure area on specified bank2 area
  1281. * @arg FLASH_BANK_BOTH: Secure area on specified bank1 and bank2 area (same config will be applied on both banks)
  1282. * @retval None
  1283. */
  1284. static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks)
  1285. {
  1286. /* Check the parameters */
  1287. assert_param(IS_FLASH_BANK(Banks));
  1288. assert_param(IS_OB_SECURE_RDP(SecureAreaConfig));
  1289. if((Banks & FLASH_BANK_1) == FLASH_BANK_1)
  1290. {
  1291. /* Check the parameters */
  1292. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(SecureAreaStartAddr));
  1293. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(SecureAreaEndAddr));
  1294. /* Configure the secure area */
  1295. FLASH->SCAR_PRG1 = ((SecureAreaStartAddr - FLASH_BANK1_BASE) >> 8) | \
  1296. (((SecureAreaEndAddr - FLASH_BANK1_BASE) >> 8) << FLASH_SCAR_SEC_AREA_END_Pos) | \
  1297. (SecureAreaConfig & FLASH_SCAR_DMES);
  1298. }
  1299. if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
  1300. {
  1301. /* Check the parameters */
  1302. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(SecureAreaStartAddr));
  1303. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(SecureAreaEndAddr));
  1304. /* Configure the secure area */
  1305. FLASH->SCAR_PRG2 = ((SecureAreaStartAddr - FLASH_BANK2_BASE) >> 8) | \
  1306. (((SecureAreaEndAddr - FLASH_BANK2_BASE) >> 8) << FLASH_SCAR_SEC_AREA_END_Pos) | \
  1307. (SecureAreaConfig & FLASH_SCAR_DMES);
  1308. }
  1309. }
  1310. /**
  1311. * @brief Get secure area configuration
  1312. * @param SecureAreaConfig indicates if the secure area will be deleted or not
  1313. * when RDP level decreased from Level 1 to Level 0 or during a mass erase.
  1314. * @param SecureAreaStartAddr gives the secure area start address
  1315. * @param SecureAreaEndAddr gives the secure area end address
  1316. * @param Bank Specifies the Bank
  1317. * @retval None
  1318. */
  1319. static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank)
  1320. {
  1321. uint32_t regvalue = 0;
  1322. uint32_t bankBase = 0;
  1323. /* Check Bank parameter value */
  1324. if((Bank & FLASH_BANK_BOTH) == FLASH_BANK_1)
  1325. {
  1326. regvalue = FLASH->SCAR_CUR1;
  1327. bankBase = FLASH_BANK1_BASE;
  1328. }
  1329. if((Bank & FLASH_BANK_BOTH) == FLASH_BANK_2)
  1330. {
  1331. regvalue = FLASH->SCAR_CUR2;
  1332. bankBase = FLASH_BANK2_BASE;
  1333. }
  1334. /* Get the secure area settings */
  1335. (*SecureAreaConfig) = (regvalue & FLASH_SCAR_DMES);
  1336. (*SecureAreaStartAddr) = ((regvalue & FLASH_SCAR_SEC_AREA_START) << 8) + bankBase;
  1337. (*SecureAreaEndAddr) = (regvalue & FLASH_SCAR_SEC_AREA_END) >> FLASH_SCAR_SEC_AREA_END_Pos;
  1338. (*SecureAreaEndAddr) = ((*SecureAreaEndAddr) << 8) + bankBase;
  1339. }
  1340. /**
  1341. * @brief Add a CRC sector to the list of sectors on which the CRC will be calculated
  1342. * @param Sector Specifies the CRC sector number
  1343. * @param Bank Specifies the Bank
  1344. * @retval None
  1345. */
  1346. static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank)
  1347. {
  1348. /* Check the parameters */
  1349. assert_param(IS_FLASH_SECTOR(Sector));
  1350. if (Bank == FLASH_BANK_1)
  1351. {
  1352. /* Clear CRC sector */
  1353. FLASH->CRCCR1 &= (~FLASH_CRCCR_CRC_SECT);
  1354. /* Select CRC Sector and activate ADD_SECT bit */
  1355. FLASH->CRCCR1 |= Sector | FLASH_CRCCR_ADD_SECT;
  1356. }
  1357. else
  1358. {
  1359. /* Clear CRC sector */
  1360. FLASH->CRCCR2 &= (~FLASH_CRCCR_CRC_SECT);
  1361. /* Select CRC Sector and activate ADD_SECT bit */
  1362. FLASH->CRCCR2 |= Sector | FLASH_CRCCR_ADD_SECT;
  1363. }
  1364. }
  1365. /**
  1366. * @brief Select CRC start and end memory addresses on which the CRC will be calculated
  1367. * @param CRCStartAddr Specifies the CRC start address
  1368. * @param CRCEndAddr Specifies the CRC end address
  1369. * @param Bank Specifies the Bank
  1370. * @retval None
  1371. */
  1372. static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank)
  1373. {
  1374. if (Bank == FLASH_BANK_1)
  1375. {
  1376. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(CRCStartAddr));
  1377. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK1(CRCEndAddr));
  1378. /* Write CRC Start and End addresses */
  1379. FLASH->CRCSADD1 = CRCStartAddr;
  1380. FLASH->CRCEADD1 = CRCEndAddr;
  1381. }
  1382. else
  1383. {
  1384. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(CRCStartAddr));
  1385. assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(CRCEndAddr));
  1386. /* Write CRC Start and End addresses */
  1387. FLASH->CRCSADD2 = CRCStartAddr;
  1388. FLASH->CRCEADD2 = CRCEndAddr;
  1389. }
  1390. }
  1391. /**
  1392. * @}
  1393. */
  1394. #endif /* HAL_FLASH_MODULE_ENABLED */
  1395. /**
  1396. * @}
  1397. */
  1398. /**
  1399. * @}
  1400. */
  1401. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/