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.
 
 
 

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