stm32h7xx_hal_flash_ex.c 64 KB

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