stm32g4xx_hal_flash_ex.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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 extended peripheral:
  8. * + Extended programming operations functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### Flash Extended features #####
  13. ==============================================================================
  14. [..] Comparing to other previous devices, the FLASH interface for STM32G4xx
  15. devices contains the following additional features
  16. (+) Capacity up to 512 Kbytes with dual bank architecture supporting read-while-write
  17. capability (RWW)
  18. (+) Dual bank 64-bits memory organization with possibility of single bank 128-bits
  19. (+) Protected areas including WRP, PCROP and Securable memory
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..] This driver provides functions to configure and program the FLASH memory
  23. of all STM32G4xx devices. It includes
  24. (#) Flash Memory Erase functions:
  25. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  26. HAL_FLASH_Lock() functions
  27. (++) Erase function: Erase pages, or mass erase banks
  28. (++) There are two modes of erase :
  29. (+++) Polling Mode using HAL_FLASHEx_Erase()
  30. (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
  31. (#) Option Bytes Programming function: Use HAL_FLASHEx_OBProgram() to:
  32. (++) Configure the write protection areas (WRP)
  33. (++) Set the Read protection Level (RDP)
  34. (++) Program the user Option Bytes
  35. (++) Configure the Proprietary Code ReadOut protection areas (PCROP)
  36. (++) Configure the Securable memory areas
  37. (++) Configure the Boot Lock
  38. (#) Get Option Bytes Configuration function: Use HAL_FLASHEx_OBGetConfig() to:
  39. (++) Get the configuration of write protection areas (WRP)
  40. (++) Get the level of read protection (RDP)
  41. (++) Get the value of the user Option Bytes
  42. (++) Get the configuration of Proprietary Code ReadOut Protection areas (PCROP)
  43. (++) Get the configuration of Securable memory areas
  44. (++) Get the status of Boot Lock
  45. (#) Activation of Securable memory area: Use HAL_FLASHEx_EnableSecMemProtection()
  46. (++) Deny the access to securable memory area
  47. (#) Enable or disable debugger: Use HAL_FLASHEx_EnableDebugger() or
  48. HAL_FLASHEx_DisableDebugger()
  49. @endverbatim
  50. ******************************************************************************
  51. * @attention
  52. *
  53. * Copyright (c) 2019 STMicroelectronics.
  54. * All rights reserved.
  55. *
  56. * This software is licensed under terms that can be found in the LICENSE file in
  57. * the root directory of this software component.
  58. * If no LICENSE file comes with this software, it is provided AS-IS.
  59. ******************************************************************************
  60. */
  61. /* Includes ------------------------------------------------------------------*/
  62. #include "stm32g4xx_hal.h"
  63. /** @addtogroup STM32G4xx_HAL_Driver
  64. * @{
  65. */
  66. /** @defgroup FLASHEx FLASHEx
  67. * @brief FLASH Extended HAL module driver
  68. * @{
  69. */
  70. #ifdef HAL_FLASH_MODULE_ENABLED
  71. /* Private typedef -----------------------------------------------------------*/
  72. /* Private define ------------------------------------------------------------*/
  73. /* Private macro -------------------------------------------------------------*/
  74. /* Private variables ---------------------------------------------------------*/
  75. /* Private function prototypes -----------------------------------------------*/
  76. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  77. * @{
  78. */
  79. static void FLASH_MassErase(uint32_t Banks);
  80. static HAL_StatusTypeDef FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset);
  81. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint32_t RDPLevel);
  82. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig);
  83. static HAL_StatusTypeDef FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr);
  84. static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t *WRPStartOffset, uint32_t *WRDPEndOffset);
  85. static uint32_t FLASH_OB_GetRDP(void);
  86. static uint32_t FLASH_OB_GetUser(void);
  87. static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr, uint32_t *PCROPEndAddr);
  88. static HAL_StatusTypeDef FLASH_OB_SecMemConfig(uint32_t SecMemBank, uint32_t SecMemSize);
  89. static void FLASH_OB_GetSecMem(uint32_t SecMemBank, uint32_t *SecMemSize);
  90. static HAL_StatusTypeDef FLASH_OB_BootLockConfig(uint32_t BootLockConfig);
  91. static uint32_t FLASH_OB_GetBootLock(void);
  92. /**
  93. * @}
  94. */
  95. /* Exported functions -------------------------------------------------------*/
  96. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  97. * @{
  98. */
  99. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  100. * @brief Extended IO operation functions
  101. *
  102. @verbatim
  103. ===============================================================================
  104. ##### Extended programming operation functions #####
  105. ===============================================================================
  106. [..]
  107. This subsection provides a set of functions allowing to manage the Extended FLASH
  108. programming operations Operations.
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Perform a mass erase or erase the specified FLASH memory pages.
  114. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  115. * contains the configuration information for the erasing.
  116. * @param[out] PageError pointer to variable that contains the configuration
  117. * information on faulty page in case of error (0xFFFFFFFF means that all
  118. * the pages have been correctly erased).
  119. * @retval HAL_Status
  120. */
  121. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
  122. {
  123. HAL_StatusTypeDef status;
  124. uint32_t page_index;
  125. /* Check the parameters */
  126. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  127. /* Process Locked */
  128. __HAL_LOCK(&pFlash);
  129. /* Wait for last operation to be completed */
  130. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  131. if (status == HAL_OK)
  132. {
  133. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  134. /* Deactivate the cache if they are activated to avoid data misbehavior */
  135. if (READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != 0U)
  136. {
  137. if (READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  138. {
  139. /* Disable data cache */
  140. __HAL_FLASH_DATA_CACHE_DISABLE();
  141. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_DCACHE_ENABLED;
  142. }
  143. else
  144. {
  145. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_ENABLED;
  146. }
  147. }
  148. else if (READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  149. {
  150. /* Disable data cache */
  151. __HAL_FLASH_DATA_CACHE_DISABLE();
  152. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  153. }
  154. else
  155. {
  156. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  157. }
  158. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  159. {
  160. /* Mass erase to be done */
  161. FLASH_MassErase(pEraseInit->Banks);
  162. /* Wait for last operation to be completed */
  163. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  164. #if defined (FLASH_OPTR_DBANK)
  165. /* If the erase operation is completed, disable the MER1 and MER2 Bits */
  166. CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1 | FLASH_CR_MER2));
  167. #else
  168. /* If the erase operation is completed, disable the MER1 Bit */
  169. CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1));
  170. #endif
  171. }
  172. else
  173. {
  174. /*Initialization of PageError variable*/
  175. *PageError = 0xFFFFFFFFU;
  176. for (page_index = pEraseInit->Page; page_index < (pEraseInit->Page + pEraseInit->NbPages); page_index++)
  177. {
  178. FLASH_PageErase(page_index, pEraseInit->Banks);
  179. /* Wait for last operation to be completed */
  180. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  181. /* If the erase operation is completed, disable the PER Bit */
  182. CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
  183. if (status != HAL_OK)
  184. {
  185. /* In case of error, stop erase procedure and return the faulty page */
  186. *PageError = page_index;
  187. break;
  188. }
  189. }
  190. }
  191. /* Flush the caches to be sure of the data consistency */
  192. FLASH_FlushCaches();
  193. }
  194. /* Process Unlocked */
  195. __HAL_UNLOCK(&pFlash);
  196. return status;
  197. }
  198. /**
  199. * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled.
  200. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  201. * contains the configuration information for the erasing.
  202. * @retval HAL_Status
  203. */
  204. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  205. {
  206. HAL_StatusTypeDef status = HAL_OK;
  207. /* Process Locked */
  208. __HAL_LOCK(&pFlash);
  209. /* Check the parameters */
  210. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  211. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  212. /* Deactivate the cache if they are activated to avoid data misbehavior */
  213. if (READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != 0U)
  214. {
  215. if (READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  216. {
  217. /* Disable data cache */
  218. __HAL_FLASH_DATA_CACHE_DISABLE();
  219. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_DCACHE_ENABLED;
  220. }
  221. else
  222. {
  223. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_ENABLED;
  224. }
  225. }
  226. else if (READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  227. {
  228. /* Disable data cache */
  229. __HAL_FLASH_DATA_CACHE_DISABLE();
  230. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  231. }
  232. else
  233. {
  234. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  235. }
  236. /* Enable End of Operation and Error interrupts */
  237. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  238. pFlash.Bank = pEraseInit->Banks;
  239. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  240. {
  241. /* Mass erase to be done */
  242. pFlash.ProcedureOnGoing = FLASH_PROC_MASS_ERASE;
  243. FLASH_MassErase(pEraseInit->Banks);
  244. }
  245. else
  246. {
  247. /* Erase by page to be done */
  248. pFlash.ProcedureOnGoing = FLASH_PROC_PAGE_ERASE;
  249. pFlash.NbPagesToErase = pEraseInit->NbPages;
  250. pFlash.Page = pEraseInit->Page;
  251. /*Erase 1st page and wait for IT */
  252. FLASH_PageErase(pEraseInit->Page, pEraseInit->Banks);
  253. }
  254. return status;
  255. }
  256. /**
  257. * @brief Program Option bytes.
  258. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  259. * contains the configuration information for the programming.
  260. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  261. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  262. * @note New option bytes configuration will be taken into account in two cases:
  263. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  264. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  265. * @retval HAL_Status
  266. */
  267. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  268. {
  269. HAL_StatusTypeDef status = HAL_OK;
  270. /* Check the parameters */
  271. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  272. /* Process Locked */
  273. __HAL_LOCK(&pFlash);
  274. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  275. /* Write protection configuration */
  276. if ((pOBInit->OptionType & OPTIONBYTE_WRP) != 0U)
  277. {
  278. /* Configure of Write protection on the selected area */
  279. if (FLASH_OB_WRPConfig(pOBInit->WRPArea, pOBInit->WRPStartOffset, pOBInit->WRPEndOffset) != HAL_OK)
  280. {
  281. status = HAL_ERROR;
  282. }
  283. }
  284. /* Read protection configuration */
  285. if ((pOBInit->OptionType & OPTIONBYTE_RDP) != 0U)
  286. {
  287. /* Configure the Read protection level */
  288. if (FLASH_OB_RDPConfig(pOBInit->RDPLevel) != HAL_OK)
  289. {
  290. status = HAL_ERROR;
  291. }
  292. }
  293. /* User Configuration */
  294. if ((pOBInit->OptionType & OPTIONBYTE_USER) != 0U)
  295. {
  296. /* Configure the user option bytes */
  297. if (FLASH_OB_UserConfig(pOBInit->USERType, pOBInit->USERConfig) != HAL_OK)
  298. {
  299. status = HAL_ERROR;
  300. }
  301. }
  302. /* PCROP Configuration */
  303. if ((pOBInit->OptionType & OPTIONBYTE_PCROP) != 0U)
  304. {
  305. if (pOBInit->PCROPStartAddr != pOBInit->PCROPEndAddr)
  306. {
  307. /* Configure the Proprietary code readout protection */
  308. if (FLASH_OB_PCROPConfig(pOBInit->PCROPConfig, pOBInit->PCROPStartAddr, pOBInit->PCROPEndAddr) != HAL_OK)
  309. {
  310. status = HAL_ERROR;
  311. }
  312. }
  313. }
  314. /* Securable memory Configuration */
  315. if ((pOBInit->OptionType & OPTIONBYTE_SEC) != 0U)
  316. {
  317. /* Configure the securable memory area */
  318. if (FLASH_OB_SecMemConfig(pOBInit->SecBank, pOBInit->SecSize) != HAL_OK)
  319. {
  320. status = HAL_ERROR;
  321. }
  322. }
  323. /* Boot Entry Point Configuration */
  324. if ((pOBInit->OptionType & OPTIONBYTE_BOOT_LOCK) != 0U)
  325. {
  326. /* Configure the boot unique entry point option */
  327. if (FLASH_OB_BootLockConfig(pOBInit->BootEntryPoint) != HAL_OK)
  328. {
  329. status = HAL_ERROR;
  330. }
  331. }
  332. /* Process Unlocked */
  333. __HAL_UNLOCK(&pFlash);
  334. return status;
  335. }
  336. /**
  337. * @brief Get the Option bytes configuration.
  338. * @param pOBInit pointer to an FLASH_OBInitStruct structure that contains the
  339. * configuration information.
  340. * @note The fields pOBInit->WRPArea and pOBInit->PCROPConfig should indicate
  341. * which area is requested for the WRP and PCROP, else no information will be returned.
  342. * @retval None
  343. */
  344. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  345. {
  346. pOBInit->OptionType = (OPTIONBYTE_RDP | OPTIONBYTE_USER);
  347. #if defined (FLASH_OPTR_DBANK)
  348. if ((pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAA) || (pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAB) ||
  349. (pOBInit->WRPArea == OB_WRPAREA_BANK2_AREAA) || (pOBInit->WRPArea == OB_WRPAREA_BANK2_AREAB))
  350. #else
  351. if ((pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAA) || (pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAB))
  352. #endif
  353. {
  354. pOBInit->OptionType |= OPTIONBYTE_WRP;
  355. /* Get write protection on the selected area */
  356. FLASH_OB_GetWRP(pOBInit->WRPArea, &(pOBInit->WRPStartOffset), &(pOBInit->WRPEndOffset));
  357. }
  358. /* Get Read protection level */
  359. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  360. /* Get the user option bytes */
  361. pOBInit->USERConfig = FLASH_OB_GetUser();
  362. #if defined (FLASH_OPTR_DBANK)
  363. if ((pOBInit->PCROPConfig == FLASH_BANK_1) || (pOBInit->PCROPConfig == FLASH_BANK_2))
  364. #else
  365. if (pOBInit->PCROPConfig == FLASH_BANK_1)
  366. #endif
  367. {
  368. pOBInit->OptionType |= OPTIONBYTE_PCROP;
  369. /* Get the Proprietary code readout protection */
  370. FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr));
  371. }
  372. pOBInit->OptionType |= OPTIONBYTE_BOOT_LOCK;
  373. /* Get the boot entry point */
  374. pOBInit->BootEntryPoint = FLASH_OB_GetBootLock();
  375. /* Get the securable memory area configuration */
  376. #if defined (FLASH_OPTR_DBANK)
  377. if ((pOBInit->SecBank == FLASH_BANK_1) || (pOBInit->SecBank == FLASH_BANK_2))
  378. #else
  379. if (pOBInit->SecBank == FLASH_BANK_1)
  380. #endif
  381. {
  382. pOBInit->OptionType |= OPTIONBYTE_SEC;
  383. FLASH_OB_GetSecMem(pOBInit->SecBank, &(pOBInit->SecSize));
  384. }
  385. }
  386. /**
  387. * @brief Enable the FLASH Securable Memory protection.
  388. * @param Bank: Bank to be protected
  389. * This parameter can be one of the following values:
  390. * @arg FLASH_BANK_1: Bank1 to be protected
  391. * @arg FLASH_BANK_2: Bank2 to be protected (*)
  392. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be protected (*)
  393. * @note (*) availability depends on devices
  394. * @retval HAL Status
  395. */
  396. HAL_StatusTypeDef HAL_FLASHEx_EnableSecMemProtection(uint32_t Bank)
  397. {
  398. #if defined (FLASH_OPTR_DBANK)
  399. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) != 0U)
  400. {
  401. /* Check the parameters */
  402. assert_param(IS_FLASH_BANK(Bank));
  403. /* Enable the Securable Memory Protection Bit for the bank 1 if requested */
  404. if ((Bank & FLASH_BANK_1) != 0U)
  405. {
  406. SET_BIT(FLASH->CR, FLASH_CR_SEC_PROT1);
  407. }
  408. /* Enable the Securable Memory Protection Bit for the bank 2 if requested */
  409. if ((Bank & FLASH_BANK_2) != 0U)
  410. {
  411. SET_BIT(FLASH->CR, FLASH_CR_SEC_PROT2);
  412. }
  413. }
  414. else
  415. {
  416. SET_BIT(FLASH->CR, FLASH_CR_SEC_PROT1);
  417. }
  418. #else
  419. /* Prevent unused argument(s) compilation warning */
  420. UNUSED(Bank);
  421. SET_BIT(FLASH->CR, FLASH_CR_SEC_PROT1);
  422. #endif /* FLASH_OPTR_DBANK */
  423. return HAL_OK;
  424. }
  425. /**
  426. * @brief Enable Debugger.
  427. * @note After calling this API, flash interface allow debugger intrusion.
  428. * @retval None
  429. */
  430. void HAL_FLASHEx_EnableDebugger(void)
  431. {
  432. FLASH->ACR |= FLASH_ACR_DBG_SWEN;
  433. }
  434. /**
  435. * @brief Disable Debugger.
  436. * @note After calling this API, Debugger is disabled: it's no more possible to
  437. * break, see CPU register, etc...
  438. * @retval None
  439. */
  440. void HAL_FLASHEx_DisableDebugger(void)
  441. {
  442. FLASH->ACR &= ~FLASH_ACR_DBG_SWEN;
  443. }
  444. /**
  445. * @}
  446. */
  447. /**
  448. * @}
  449. */
  450. /* Private functions ---------------------------------------------------------*/
  451. /** @addtogroup FLASHEx_Private_Functions
  452. * @{
  453. */
  454. /**
  455. * @brief Mass erase of FLASH memory.
  456. * @param Banks Banks to be erased.
  457. * This parameter can be one of the following values:
  458. * @arg FLASH_BANK_1: Bank1 to be erased
  459. * @arg FLASH_BANK_2: Bank2 to be erased (*)
  460. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased (*)
  461. * @note (*) availability depends on devices
  462. * @retval None
  463. */
  464. static void FLASH_MassErase(uint32_t Banks)
  465. {
  466. #if defined (FLASH_OPTR_DBANK)
  467. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) != 0U)
  468. #endif
  469. {
  470. /* Check the parameters */
  471. assert_param(IS_FLASH_BANK(Banks));
  472. /* Set the Mass Erase Bit for the bank 1 if requested */
  473. if ((Banks & FLASH_BANK_1) != 0U)
  474. {
  475. SET_BIT(FLASH->CR, FLASH_CR_MER1);
  476. }
  477. #if defined (FLASH_OPTR_DBANK)
  478. /* Set the Mass Erase Bit for the bank 2 if requested */
  479. if ((Banks & FLASH_BANK_2) != 0U)
  480. {
  481. SET_BIT(FLASH->CR, FLASH_CR_MER2);
  482. }
  483. #endif
  484. }
  485. #if defined (FLASH_OPTR_DBANK)
  486. else
  487. {
  488. SET_BIT(FLASH->CR, (FLASH_CR_MER1 | FLASH_CR_MER2));
  489. }
  490. #endif
  491. /* Proceed to erase all sectors */
  492. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  493. }
  494. /**
  495. * @brief Erase the specified FLASH memory page.
  496. * @param Page FLASH page to erase.
  497. * This parameter must be a value between 0 and (max number of pages in the bank - 1).
  498. * @param Banks Bank where the page will be erased.
  499. * This parameter can be one of the following values:
  500. * @arg FLASH_BANK_1: Page in bank 1 to be erased
  501. * @arg FLASH_BANK_2: Page in bank 2 to be erased (*)
  502. * @note (*) availability depends on devices
  503. * @retval None
  504. */
  505. void FLASH_PageErase(uint32_t Page, uint32_t Banks)
  506. {
  507. /* Check the parameters */
  508. assert_param(IS_FLASH_PAGE(Page));
  509. #if defined (FLASH_OPTR_DBANK)
  510. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) == 0U)
  511. {
  512. CLEAR_BIT(FLASH->CR, FLASH_CR_BKER);
  513. }
  514. else
  515. {
  516. assert_param(IS_FLASH_BANK_EXCLUSIVE(Banks));
  517. if ((Banks & FLASH_BANK_1) != 0U)
  518. {
  519. CLEAR_BIT(FLASH->CR, FLASH_CR_BKER);
  520. }
  521. else
  522. {
  523. SET_BIT(FLASH->CR, FLASH_CR_BKER);
  524. }
  525. }
  526. #else
  527. /* Prevent unused argument(s) compilation warning */
  528. UNUSED(Banks);
  529. #endif /* FLASH_OPTR_DBANK */
  530. /* Proceed to erase the page */
  531. MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((Page & 0xFFU) << FLASH_CR_PNB_Pos));
  532. SET_BIT(FLASH->CR, FLASH_CR_PER);
  533. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  534. }
  535. /**
  536. * @brief Flush the instruction and data caches.
  537. * @retval None
  538. */
  539. void FLASH_FlushCaches(void)
  540. {
  541. FLASH_CacheTypeDef cache = pFlash.CacheToReactivate;
  542. /* Flush instruction cache */
  543. if ((cache == FLASH_CACHE_ICACHE_ENABLED) ||
  544. (cache == FLASH_CACHE_ICACHE_DCACHE_ENABLED))
  545. {
  546. /* Disable instruction cache */
  547. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  548. /* Reset instruction cache */
  549. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  550. /* Enable instruction cache */
  551. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  552. }
  553. /* Flush data cache */
  554. if ((cache == FLASH_CACHE_DCACHE_ENABLED) ||
  555. (cache == FLASH_CACHE_ICACHE_DCACHE_ENABLED))
  556. {
  557. /* Reset data cache */
  558. __HAL_FLASH_DATA_CACHE_RESET();
  559. /* Enable data cache */
  560. __HAL_FLASH_DATA_CACHE_ENABLE();
  561. }
  562. /* Reset internal variable */
  563. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  564. }
  565. /**
  566. * @brief Configure the write protection area into Option Bytes.
  567. * @note When the memory read protection level is selected (RDP level = 1),
  568. * it is not possible to program or erase Flash memory if the CPU debug
  569. * features are connected (JTAG or single wire) or boot code is being
  570. * executed from RAM or System flash, even if WRP is not activated.
  571. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  572. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  573. * @note New option bytes configuration will be taken into account in two cases:
  574. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  575. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  576. * @param WRPArea specifies the area to be configured.
  577. * This parameter can be one of the following values:
  578. * @arg OB_WRPAREA_BANK1_AREAA: Flash Bank 1 Area A
  579. * @arg OB_WRPAREA_BANK1_AREAB: Flash Bank 1 Area B
  580. * @arg OB_WRPAREA_BANK2_AREAA: Flash Bank 2 Area A (*)
  581. * @arg OB_WRPAREA_BANK2_AREAB: Flash Bank 2 Area B (*)
  582. * @note (*) availability depends on devices
  583. * @param WRPStartOffset specifies the start page of the write protected area.
  584. * This parameter can be page number between 0 and (max number of pages in the bank - 1).
  585. * @param WRDPEndOffset specifies the end page of the write protected area.
  586. * This parameter can be page number between WRPStartOffset and (max number of pages in the bank - 1).
  587. * @retval HAL_Status
  588. */
  589. static HAL_StatusTypeDef FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset)
  590. {
  591. HAL_StatusTypeDef status;
  592. /* Check the parameters */
  593. assert_param(IS_OB_WRPAREA(WRPArea));
  594. assert_param(IS_FLASH_PAGE(WRPStartOffset));
  595. assert_param(IS_FLASH_PAGE(WRDPEndOffset));
  596. /* Wait for last operation to be completed */
  597. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  598. if (status == HAL_OK)
  599. {
  600. /* Configure the write protected area */
  601. if (WRPArea == OB_WRPAREA_BANK1_AREAA)
  602. {
  603. FLASH->WRP1AR = ((WRDPEndOffset << FLASH_WRP1AR_WRP1A_END_Pos) | WRPStartOffset);
  604. }
  605. else if (WRPArea == OB_WRPAREA_BANK1_AREAB)
  606. {
  607. FLASH->WRP1BR = ((WRDPEndOffset << FLASH_WRP1BR_WRP1B_END_Pos) | WRPStartOffset);
  608. }
  609. #if defined (FLASH_OPTR_DBANK)
  610. else if (WRPArea == OB_WRPAREA_BANK2_AREAA)
  611. {
  612. FLASH->WRP2AR = ((WRDPEndOffset << FLASH_WRP2AR_WRP2A_END_Pos) | WRPStartOffset);
  613. }
  614. else if (WRPArea == OB_WRPAREA_BANK2_AREAB)
  615. {
  616. FLASH->WRP2BR = ((WRDPEndOffset << FLASH_WRP2BR_WRP2B_END_Pos) | WRPStartOffset);
  617. }
  618. #endif
  619. else
  620. {
  621. /* Nothing to do */
  622. }
  623. /* Set OPTSTRT Bit */
  624. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  625. /* Wait for last operation to be completed */
  626. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  627. }
  628. return status;
  629. }
  630. /**
  631. * @brief Set the read protection level into Option Bytes.
  632. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  633. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  634. * @note New option bytes configuration will be taken into account in two cases:
  635. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  636. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  637. * @note !!! Warning : When enabling OB_RDP level 2 it's no more possible
  638. * to go back to level 1 or 0 !!!
  639. * @param RDPLevel specifies the read protection level.
  640. * This parameter can be one of the following values:
  641. * @arg OB_RDP_LEVEL_0: No protection
  642. * @arg OB_RDP_LEVEL_1: Memory Read protection
  643. * @arg OB_RDP_LEVEL_2: Full chip protection
  644. *
  645. * @retval HAL_Status
  646. */
  647. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint32_t RDPLevel)
  648. {
  649. HAL_StatusTypeDef status;
  650. /* Check the parameters */
  651. assert_param(IS_OB_RDP_LEVEL(RDPLevel));
  652. /* Wait for last operation to be completed */
  653. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  654. if (status == HAL_OK)
  655. {
  656. /* Configure the RDP level in the option bytes register */
  657. MODIFY_REG(FLASH->OPTR, FLASH_OPTR_RDP, RDPLevel);
  658. /* Set OPTSTRT Bit */
  659. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  660. /* Wait for last operation to be completed */
  661. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  662. }
  663. return status;
  664. }
  665. /**
  666. * @brief Program the FLASH User Option Bytes.
  667. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  668. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  669. * @note New option bytes configuration will be taken into account in two cases:
  670. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  671. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  672. * @param UserType The FLASH User Option Bytes to be modified.
  673. * This parameter can be a combination of @ref FLASH_OB_USER_Type.
  674. * @param UserConfig The selected User Option Bytes values:
  675. * This parameter can be a combination of @ref FLASH_OB_USER_BOR_LEVEL,
  676. * @ref FLASH_OB_USER_nRST_STOP, @ref FLASH_OB_USER_nRST_STANDBY ,
  677. * @ref FLASH_OB_USER_nRST_SHUTDOWN, @ref FLASH_OB_USER_IWDG_SW,
  678. * @ref FLASH_OB_USER_IWDG_STOP, @ref FLASH_OB_USER_IWDG_STANDBY,
  679. * @ref FLASH_OB_USER_WWDG_SW, @ref FLASH_OB_USER_WWDG_SW,
  680. * @ref FLASH_OB_USER_BFB2 (*), @ref FLASH_OB_USER_nBOOT1,
  681. * @ref FLASH_OB_USER_SRAM_PE, @ref FLASH_OB_USER_CCMSRAM_RST,
  682. * @ref FLASH_OB_USER_nSWBOOT0, @ref FLASH_OB_USER_nBOOT0,
  683. * @ref FLASH_OB_USER_NRST_MODE, @ref FLASH_OB_USER_INTERNAL_RESET_HOLDER
  684. * @note (*) availability depends on devices
  685. * @retval HAL_Status
  686. */
  687. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
  688. {
  689. uint32_t optr_reg_val = 0;
  690. uint32_t optr_reg_mask = 0;
  691. HAL_StatusTypeDef status;
  692. /* Check the parameters */
  693. assert_param(IS_OB_USER_TYPE(UserType));
  694. /* Wait for last operation to be completed */
  695. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  696. if (status == HAL_OK)
  697. {
  698. #if defined(FLASH_OPTR_PB4_PUPEN)
  699. if ((UserType & OB_USER_PB4_PUPEN) != 0U)
  700. {
  701. /* PB4_PUPEN option byte should be modified */
  702. assert_param(IS_OB_USER_PB4_PUPEN(UserConfig & FLASH_OPTR_PB4_PUPEN));
  703. /* Set value and mask for PB4_PUPEN option byte */
  704. optr_reg_val |= (UserConfig & FLASH_OPTR_PB4_PUPEN);
  705. optr_reg_mask |= FLASH_OPTR_PB4_PUPEN;
  706. }
  707. #endif /* FLASH_OPTR_PB4_PUPEN */
  708. if ((UserType & OB_USER_BOR_LEV) != 0U)
  709. {
  710. /* BOR level option byte should be modified */
  711. assert_param(IS_OB_USER_BOR_LEVEL(UserConfig & FLASH_OPTR_BOR_LEV));
  712. /* Set value and mask for BOR level option byte */
  713. optr_reg_val |= (UserConfig & FLASH_OPTR_BOR_LEV);
  714. optr_reg_mask |= FLASH_OPTR_BOR_LEV;
  715. }
  716. if ((UserType & OB_USER_nRST_STOP) != 0U)
  717. {
  718. /* nRST_STOP option byte should be modified */
  719. assert_param(IS_OB_USER_STOP(UserConfig & FLASH_OPTR_nRST_STOP));
  720. /* Set value and mask for nRST_STOP option byte */
  721. optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_STOP);
  722. optr_reg_mask |= FLASH_OPTR_nRST_STOP;
  723. }
  724. if ((UserType & OB_USER_nRST_STDBY) != 0U)
  725. {
  726. /* nRST_STDBY option byte should be modified */
  727. assert_param(IS_OB_USER_STANDBY(UserConfig & FLASH_OPTR_nRST_STDBY));
  728. /* Set value and mask for nRST_STDBY option byte */
  729. optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_STDBY);
  730. optr_reg_mask |= FLASH_OPTR_nRST_STDBY;
  731. }
  732. if ((UserType & OB_USER_nRST_SHDW) != 0U)
  733. {
  734. /* nRST_SHDW option byte should be modified */
  735. assert_param(IS_OB_USER_SHUTDOWN(UserConfig & FLASH_OPTR_nRST_SHDW));
  736. /* Set value and mask for nRST_SHDW option byte */
  737. optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_SHDW);
  738. optr_reg_mask |= FLASH_OPTR_nRST_SHDW;
  739. }
  740. if ((UserType & OB_USER_IWDG_SW) != 0U)
  741. {
  742. /* IWDG_SW option byte should be modified */
  743. assert_param(IS_OB_USER_IWDG(UserConfig & FLASH_OPTR_IWDG_SW));
  744. /* Set value and mask for IWDG_SW option byte */
  745. optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_SW);
  746. optr_reg_mask |= FLASH_OPTR_IWDG_SW;
  747. }
  748. if ((UserType & OB_USER_IWDG_STOP) != 0U)
  749. {
  750. /* IWDG_STOP option byte should be modified */
  751. assert_param(IS_OB_USER_IWDG_STOP(UserConfig & FLASH_OPTR_IWDG_STOP));
  752. /* Set value and mask for IWDG_STOP option byte */
  753. optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_STOP);
  754. optr_reg_mask |= FLASH_OPTR_IWDG_STOP;
  755. }
  756. if ((UserType & OB_USER_IWDG_STDBY) != 0U)
  757. {
  758. /* IWDG_STDBY option byte should be modified */
  759. assert_param(IS_OB_USER_IWDG_STDBY(UserConfig & FLASH_OPTR_IWDG_STDBY));
  760. /* Set value and mask for IWDG_STDBY option byte */
  761. optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_STDBY);
  762. optr_reg_mask |= FLASH_OPTR_IWDG_STDBY;
  763. }
  764. if ((UserType & OB_USER_WWDG_SW) != 0U)
  765. {
  766. /* WWDG_SW option byte should be modified */
  767. assert_param(IS_OB_USER_WWDG(UserConfig & FLASH_OPTR_WWDG_SW));
  768. /* Set value and mask for WWDG_SW option byte */
  769. optr_reg_val |= (UserConfig & FLASH_OPTR_WWDG_SW);
  770. optr_reg_mask |= FLASH_OPTR_WWDG_SW;
  771. }
  772. #if defined (FLASH_OPTR_BFB2)
  773. if ((UserType & OB_USER_BFB2) != 0U)
  774. {
  775. /* BFB2 option byte should be modified */
  776. assert_param(IS_OB_USER_BFB2(UserConfig & FLASH_OPTR_BFB2));
  777. /* Set value and mask for BFB2 option byte */
  778. optr_reg_val |= (UserConfig & FLASH_OPTR_BFB2);
  779. optr_reg_mask |= FLASH_OPTR_BFB2;
  780. }
  781. #endif
  782. if ((UserType & OB_USER_nBOOT1) != 0U)
  783. {
  784. /* nBOOT1 option byte should be modified */
  785. assert_param(IS_OB_USER_BOOT1(UserConfig & FLASH_OPTR_nBOOT1));
  786. /* Set value and mask for nBOOT1 option byte */
  787. optr_reg_val |= (UserConfig & FLASH_OPTR_nBOOT1);
  788. optr_reg_mask |= FLASH_OPTR_nBOOT1;
  789. }
  790. if ((UserType & OB_USER_SRAM_PE) != 0U)
  791. {
  792. /* SRAM_PE option byte should be modified */
  793. assert_param(IS_OB_USER_SRAM_PARITY(UserConfig & FLASH_OPTR_SRAM_PE));
  794. /* Set value and mask for SRAM_PE option byte */
  795. optr_reg_val |= (UserConfig & FLASH_OPTR_SRAM_PE);
  796. optr_reg_mask |= FLASH_OPTR_SRAM_PE;
  797. }
  798. if ((UserType & OB_USER_CCMSRAM_RST) != 0U)
  799. {
  800. /* CCMSRAM_RST option byte should be modified */
  801. assert_param(IS_OB_USER_CCMSRAM_RST(UserConfig & FLASH_OPTR_CCMSRAM_RST));
  802. /* Set value and mask for CCMSRAM_RST option byte */
  803. optr_reg_val |= (UserConfig & FLASH_OPTR_CCMSRAM_RST);
  804. optr_reg_mask |= FLASH_OPTR_CCMSRAM_RST;
  805. }
  806. if ((UserType & OB_USER_nSWBOOT0) != 0U)
  807. {
  808. /* nSWBOOT0 option byte should be modified */
  809. assert_param(IS_OB_USER_SWBOOT0(UserConfig & FLASH_OPTR_nSWBOOT0));
  810. /* Set value and mask for nSWBOOT0 option byte */
  811. optr_reg_val |= (UserConfig & FLASH_OPTR_nSWBOOT0);
  812. optr_reg_mask |= FLASH_OPTR_nSWBOOT0;
  813. }
  814. if ((UserType & OB_USER_nBOOT0) != 0U)
  815. {
  816. /* nBOOT0 option byte should be modified */
  817. assert_param(IS_OB_USER_BOOT0(UserConfig & FLASH_OPTR_nBOOT0));
  818. /* Set value and mask for nBOOT0 option byte */
  819. optr_reg_val |= (UserConfig & FLASH_OPTR_nBOOT0);
  820. optr_reg_mask |= FLASH_OPTR_nBOOT0;
  821. }
  822. if ((UserType & OB_USER_NRST_MODE) != 0U)
  823. {
  824. /* Reset Configuration option byte should be modified */
  825. assert_param(IS_OB_USER_NRST_MODE(UserConfig & FLASH_OPTR_NRST_MODE));
  826. /* Set value and mask for Reset Configuration option byte */
  827. optr_reg_val |= (UserConfig & FLASH_OPTR_NRST_MODE);
  828. optr_reg_mask |= FLASH_OPTR_NRST_MODE;
  829. }
  830. if ((UserType & OB_USER_IRHEN) != 0U)
  831. {
  832. /* IRH option byte should be modified */
  833. assert_param(IS_OB_USER_IRHEN(UserConfig & FLASH_OPTR_IRHEN));
  834. /* Set value and mask for IRH option byte */
  835. optr_reg_val |= (UserConfig & FLASH_OPTR_IRHEN);
  836. optr_reg_mask |= FLASH_OPTR_IRHEN;
  837. }
  838. /* Configure the option bytes register */
  839. MODIFY_REG(FLASH->OPTR, optr_reg_mask, optr_reg_val);
  840. /* Set OPTSTRT Bit */
  841. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  842. /* Wait for last operation to be completed */
  843. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  844. }
  845. return status;
  846. }
  847. /**
  848. * @brief Configure the Proprietary code readout protection area into Option Bytes.
  849. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  850. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  851. * @note New option bytes configuration will be taken into account in two cases:
  852. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  853. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  854. * @param PCROPConfig specifies the configuration (Bank to be configured and PCROP_RDP option).
  855. * This parameter must be a combination of FLASH_BANK_1 or FLASH_BANK_2 (*)
  856. * with OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE.
  857. * @note (*) availability depends on devices
  858. * @param PCROPStartAddr specifies the start address of the Proprietary code readout protection.
  859. * This parameter can be an address between begin and end of the bank.
  860. * @param PCROPEndAddr specifies the end address of the Proprietary code readout protection.
  861. * This parameter can be an address between PCROPStartAddr and end of the bank.
  862. * @retval HAL_Status
  863. */
  864. static HAL_StatusTypeDef FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr)
  865. {
  866. HAL_StatusTypeDef status;
  867. uint32_t reg_value;
  868. uint32_t bank1_addr;
  869. #if defined (FLASH_OPTR_DBANK)
  870. uint32_t bank2_addr;
  871. #endif
  872. /* Check the parameters */
  873. assert_param(IS_FLASH_BANK_EXCLUSIVE(PCROPConfig & FLASH_BANK_BOTH));
  874. assert_param(IS_OB_PCROP_RDP(PCROPConfig & FLASH_PCROP1ER_PCROP_RDP));
  875. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(PCROPStartAddr));
  876. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(PCROPEndAddr));
  877. /* Wait for last operation to be completed */
  878. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  879. if (status == HAL_OK)
  880. {
  881. #if defined (FLASH_OPTR_DBANK)
  882. /* Get the information about the bank swapping */
  883. if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0U)
  884. {
  885. bank1_addr = FLASH_BASE;
  886. bank2_addr = FLASH_BASE + FLASH_BANK_SIZE;
  887. }
  888. else
  889. {
  890. bank1_addr = FLASH_BASE + FLASH_BANK_SIZE;
  891. bank2_addr = FLASH_BASE;
  892. }
  893. #else
  894. bank1_addr = FLASH_BASE;
  895. #endif
  896. #if defined (FLASH_OPTR_DBANK)
  897. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) == 0U)
  898. {
  899. /* Configure the Proprietary code readout protection */
  900. if ((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_1)
  901. {
  902. reg_value = ((PCROPStartAddr - FLASH_BASE) >> 4);
  903. MODIFY_REG(FLASH->PCROP1SR, FLASH_PCROP1SR_PCROP1_STRT, reg_value);
  904. reg_value = ((PCROPEndAddr - FLASH_BASE) >> 4);
  905. MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP1_END, reg_value);
  906. }
  907. else if ((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_2)
  908. {
  909. reg_value = ((PCROPStartAddr - FLASH_BASE) >> 4);
  910. MODIFY_REG(FLASH->PCROP2SR, FLASH_PCROP2SR_PCROP2_STRT, reg_value);
  911. reg_value = ((PCROPEndAddr - FLASH_BASE) >> 4);
  912. MODIFY_REG(FLASH->PCROP2ER, FLASH_PCROP2ER_PCROP2_END, reg_value);
  913. }
  914. else
  915. {
  916. /* Nothing to do */
  917. }
  918. }
  919. else
  920. #endif
  921. {
  922. /* Configure the Proprietary code readout protection */
  923. if ((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_1)
  924. {
  925. reg_value = ((PCROPStartAddr - bank1_addr) >> 3);
  926. MODIFY_REG(FLASH->PCROP1SR, FLASH_PCROP1SR_PCROP1_STRT, reg_value);
  927. reg_value = ((PCROPEndAddr - bank1_addr) >> 3);
  928. MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP1_END, reg_value);
  929. }
  930. #if defined (FLASH_OPTR_DBANK)
  931. else if ((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_2)
  932. {
  933. reg_value = ((PCROPStartAddr - bank2_addr) >> 3);
  934. MODIFY_REG(FLASH->PCROP2SR, FLASH_PCROP2SR_PCROP2_STRT, reg_value);
  935. reg_value = ((PCROPEndAddr - bank2_addr) >> 3);
  936. MODIFY_REG(FLASH->PCROP2ER, FLASH_PCROP2ER_PCROP2_END, reg_value);
  937. }
  938. #endif
  939. else
  940. {
  941. /* Nothing to do */
  942. }
  943. }
  944. MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP_RDP, (PCROPConfig & FLASH_PCROP1ER_PCROP_RDP));
  945. /* Set OPTSTRT Bit */
  946. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  947. /* Wait for last operation to be completed */
  948. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  949. }
  950. return status;
  951. }
  952. /**
  953. * @brief Configure the Securable memory area into Option Bytes.
  954. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  955. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  956. * @note New option bytes configuration will be taken into account in two cases:
  957. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  958. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  959. * @param SecBank specifies bank of securable memory area to be configured.
  960. * This parameter can be one of the following values:
  961. * @arg FLASH_BANK_1: Securable memory in Bank1 to be configured
  962. * @arg FLASH_BANK_2: Securable memory in Bank2 to be configured (*)
  963. * @note (*) availability depends on devices
  964. * @param SecSize specifies the number of pages of the Securable memory area,
  965. * starting from first page of the bank.
  966. * This parameter can be page number between 0 and (max number of pages in the bank - 1)
  967. * @retval HAL Status
  968. */
  969. static HAL_StatusTypeDef FLASH_OB_SecMemConfig(uint32_t SecBank, uint32_t SecSize)
  970. {
  971. HAL_StatusTypeDef status;
  972. /* Check the parameters */
  973. assert_param(IS_FLASH_BANK_EXCLUSIVE(SecBank));
  974. assert_param(IS_OB_SECMEM_SIZE(SecSize));
  975. /* Wait for last operation to be completed */
  976. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  977. if (status == HAL_OK)
  978. {
  979. /* Configure the write protected area */
  980. if (SecBank == FLASH_BANK_1)
  981. {
  982. MODIFY_REG(FLASH->SEC1R, FLASH_SEC1R_SEC_SIZE1, SecSize);
  983. }
  984. #if defined (FLASH_OPTR_DBANK)
  985. else if (SecBank == FLASH_BANK_2)
  986. {
  987. MODIFY_REG(FLASH->SEC2R, FLASH_SEC2R_SEC_SIZE2, SecSize);
  988. }
  989. else
  990. {
  991. /* Nothing to do */
  992. }
  993. #endif
  994. /* Set OPTSTRT Bit */
  995. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  996. /* Wait for last operation to be completed */
  997. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  998. }
  999. return status;
  1000. }
  1001. /**
  1002. * @brief Configure the Boot Lock into Option Bytes.
  1003. * @note To configure any option bytes, the option lock bit OPTLOCK must be
  1004. * cleared with the call of HAL_FLASH_OB_Unlock() function.
  1005. * @note New option bytes configuration will be taken into account in two cases:
  1006. * - after an option bytes launch through the call of HAL_FLASH_OB_Launch()
  1007. * - after a power reset (BOR reset or exit from Standby/Shutdown modes)
  1008. * @param BootLockConfig specifies the boot lock configuration.
  1009. * This parameter can be one of the following values:
  1010. * @arg OB_BOOT_LOCK_ENABLE: Enable Boot Lock
  1011. * @arg OB_BOOT_LOCK_DISABLE: Disable Boot Lock
  1012. *
  1013. * @retval HAL_Status
  1014. */
  1015. static HAL_StatusTypeDef FLASH_OB_BootLockConfig(uint32_t BootLockConfig)
  1016. {
  1017. HAL_StatusTypeDef status;
  1018. /* Check the parameters */
  1019. assert_param(IS_OB_BOOT_LOCK(BootLockConfig));
  1020. /* Wait for last operation to be completed */
  1021. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  1022. if (status == HAL_OK)
  1023. {
  1024. MODIFY_REG(FLASH->SEC1R, FLASH_SEC1R_BOOT_LOCK, BootLockConfig);
  1025. /* Set OPTSTRT Bit */
  1026. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  1027. /* Wait for last operation to be completed */
  1028. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  1029. }
  1030. return status;
  1031. }
  1032. /**
  1033. * @brief Return the Securable memory area configuration into Option Bytes.
  1034. * @param[in] SecBank specifies the bank where securable memory area is located.
  1035. * This parameter can be one of the following values:
  1036. * @arg FLASH_BANK_1: Securable memory in Bank1
  1037. * @arg FLASH_BANK_2: Securable memory in Bank2 (*)
  1038. * @note (*) availability depends on devices
  1039. * @param[out] SecSize specifies the number of pages used in the securable
  1040. memory area of the bank.
  1041. * @retval None
  1042. */
  1043. static void FLASH_OB_GetSecMem(uint32_t SecBank, uint32_t *SecSize)
  1044. {
  1045. /* Get the configuration of the securable memory area */
  1046. if (SecBank == FLASH_BANK_1)
  1047. {
  1048. *SecSize = READ_BIT(FLASH->SEC1R, FLASH_SEC1R_SEC_SIZE1);
  1049. }
  1050. #if defined (FLASH_OPTR_DBANK)
  1051. else if (SecBank == FLASH_BANK_2)
  1052. {
  1053. *SecSize = READ_BIT(FLASH->SEC2R, FLASH_SEC2R_SEC_SIZE2);
  1054. }
  1055. else
  1056. {
  1057. /* Nothing to do */
  1058. }
  1059. #endif
  1060. }
  1061. /**
  1062. * @brief Return the Boot Lock configuration into Option Byte.
  1063. * @retval BootLockConfig.
  1064. * This return value can be one of the following values:
  1065. * @arg OB_BOOT_LOCK_ENABLE: Boot lock enabled
  1066. * @arg OB_BOOT_LOCK_DISABLE: Boot lock disabled
  1067. */
  1068. static uint32_t FLASH_OB_GetBootLock(void)
  1069. {
  1070. return (READ_REG(FLASH->SEC1R) & FLASH_SEC1R_BOOT_LOCK);
  1071. }
  1072. /**
  1073. * @brief Return the Write Protection configuration into Option Bytes.
  1074. * @param[in] WRPArea specifies the area to be returned.
  1075. * This parameter can be one of the following values:
  1076. * @arg OB_WRPAREA_BANK1_AREAA: Flash Bank 1 Area A
  1077. * @arg OB_WRPAREA_BANK1_AREAB: Flash Bank 1 Area B
  1078. * @arg OB_WRPAREA_BANK2_AREAA: Flash Bank 2 Area A (don't apply to STM32G43x/STM32G44x devices)
  1079. * @arg OB_WRPAREA_BANK2_AREAB: Flash Bank 2 Area B (don't apply to STM32G43x/STM32G44x devices)
  1080. * @param[out] WRPStartOffset specifies the address where to copied the start page
  1081. * of the write protected area.
  1082. * @param[out] WRDPEndOffset specifies the address where to copied the end page of
  1083. * the write protected area.
  1084. * @retval None
  1085. */
  1086. static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t *WRPStartOffset, uint32_t *WRDPEndOffset)
  1087. {
  1088. /* Get the configuration of the write protected area */
  1089. if (WRPArea == OB_WRPAREA_BANK1_AREAA)
  1090. {
  1091. *WRPStartOffset = READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_STRT);
  1092. *WRDPEndOffset = (READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_END) >> FLASH_WRP1AR_WRP1A_END_Pos);
  1093. }
  1094. else if (WRPArea == OB_WRPAREA_BANK1_AREAB)
  1095. {
  1096. *WRPStartOffset = READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_STRT);
  1097. *WRDPEndOffset = (READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_END) >> FLASH_WRP1BR_WRP1B_END_Pos);
  1098. }
  1099. #if defined (FLASH_OPTR_DBANK)
  1100. else if (WRPArea == OB_WRPAREA_BANK2_AREAA)
  1101. {
  1102. *WRPStartOffset = READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_STRT);
  1103. *WRDPEndOffset = (READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_END) >> FLASH_WRP2AR_WRP2A_END_Pos);
  1104. }
  1105. else if (WRPArea == OB_WRPAREA_BANK2_AREAB)
  1106. {
  1107. *WRPStartOffset = READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_STRT);
  1108. *WRDPEndOffset = (READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_END) >> FLASH_WRP2BR_WRP2B_END_Pos);
  1109. }
  1110. #endif
  1111. else
  1112. {
  1113. /* Nothing to do */
  1114. }
  1115. }
  1116. /**
  1117. * @brief Return the FLASH Read Protection level into Option Bytes.
  1118. * @retval RDP_Level
  1119. * This return value can be one of the following values:
  1120. * @arg OB_RDP_LEVEL_0: No protection
  1121. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  1122. * @arg OB_RDP_LEVEL_2: Full chip protection
  1123. */
  1124. static uint32_t FLASH_OB_GetRDP(void)
  1125. {
  1126. uint32_t rdp_level = READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP);
  1127. if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
  1128. {
  1129. return (OB_RDP_LEVEL_1);
  1130. }
  1131. else
  1132. {
  1133. return rdp_level;
  1134. }
  1135. }
  1136. /**
  1137. * @brief Return the FLASH User Option Byte value.
  1138. * @retval OB_user_config
  1139. * This return value is a combination of @ref FLASH_OB_USER_BOR_LEVEL,
  1140. * @ref FLASH_OB_USER_nRST_STOP, @ref FLASH_OB_USER_nRST_STANDBY,
  1141. * @ref FLASH_OB_USER_nRST_SHUTDOWN, @ref FLASH_OB_USER_IWDG_SW,
  1142. * @ref FLASH_OB_USER_IWDG_STOP, @ref FLASH_OB_USER_IWDG_STANDBY,
  1143. * @ref FLASH_OB_USER_WWDG_SW, @ref FLASH_OB_USER_WWDG_SW,
  1144. * @ref FLASH_OB_USER_BFB2 (*), @ref FLASH_OB_USER_DBANK (*),
  1145. * @ref FLASH_OB_USER_nBOOT1, @ref FLASH_OB_USER_SRAM_PE,
  1146. * @ref FLASH_OB_USER_CCMSRAM_RST, @ref OB_USER_nSWBOOT0,@ref FLASH_OB_USER_nBOOT0,
  1147. * @ref FLASH_OB_USER_NRST_MODE, @ref FLASH_OB_USER_INTERNAL_RESET_HOLDER
  1148. * @note (*) availability depends on devices
  1149. */
  1150. static uint32_t FLASH_OB_GetUser(void)
  1151. {
  1152. uint32_t user_config = READ_REG(FLASH->OPTR);
  1153. CLEAR_BIT(user_config, FLASH_OPTR_RDP);
  1154. return user_config;
  1155. }
  1156. /**
  1157. * @brief Return the FLASH PCROP configuration into Option Bytes.
  1158. * @param[in,out] PCROPConfig specifies the configuration (Bank to be configured and PCROP_RDP option).
  1159. * This parameter must be a combination of FLASH_BANK_1 or FLASH_BANK_2
  1160. * with OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE.
  1161. * @param[out] PCROPStartAddr specifies the address where to copied the start address
  1162. * of the Proprietary code readout protection.
  1163. * @param[out] PCROPEndAddr specifies the address where to copied the end address of
  1164. * the Proprietary code readout protection.
  1165. * @retval None
  1166. */
  1167. static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr, uint32_t *PCROPEndAddr)
  1168. {
  1169. uint32_t reg_value;
  1170. uint32_t bank1_addr;
  1171. #if defined (FLASH_OPTR_DBANK)
  1172. uint32_t bank2_addr;
  1173. /* Get the information about the bank swapping */
  1174. if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0U)
  1175. {
  1176. bank1_addr = FLASH_BASE;
  1177. bank2_addr = FLASH_BASE + FLASH_BANK_SIZE;
  1178. }
  1179. else
  1180. {
  1181. bank1_addr = FLASH_BASE + FLASH_BANK_SIZE;
  1182. bank2_addr = FLASH_BASE;
  1183. }
  1184. #else
  1185. bank1_addr = FLASH_BASE;
  1186. #endif
  1187. #if defined (FLASH_OPTR_DBANK)
  1188. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) == 0U)
  1189. {
  1190. if (((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_1)
  1191. {
  1192. reg_value = (READ_REG(FLASH->PCROP1SR) & FLASH_PCROP1SR_PCROP1_STRT);
  1193. *PCROPStartAddr = (reg_value << 4) + FLASH_BASE;
  1194. reg_value = (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP1_END);
  1195. *PCROPEndAddr = (reg_value << 4) + FLASH_BASE;
  1196. }
  1197. else if (((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_2)
  1198. {
  1199. reg_value = (READ_REG(FLASH->PCROP2SR) & FLASH_PCROP2SR_PCROP2_STRT);
  1200. *PCROPStartAddr = (reg_value << 4) + FLASH_BASE;
  1201. reg_value = (READ_REG(FLASH->PCROP2ER) & FLASH_PCROP2ER_PCROP2_END);
  1202. *PCROPEndAddr = (reg_value << 4) + FLASH_BASE;
  1203. }
  1204. else
  1205. {
  1206. /* Nothing to do */
  1207. }
  1208. }
  1209. else
  1210. #endif
  1211. {
  1212. if (((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_1)
  1213. {
  1214. reg_value = (READ_REG(FLASH->PCROP1SR) & FLASH_PCROP1SR_PCROP1_STRT);
  1215. *PCROPStartAddr = (reg_value << 3) + bank1_addr;
  1216. reg_value = (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP1_END);
  1217. *PCROPEndAddr = (reg_value << 3) + bank1_addr;
  1218. }
  1219. #if defined (FLASH_OPTR_DBANK)
  1220. else if (((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_2)
  1221. {
  1222. reg_value = (READ_REG(FLASH->PCROP2SR) & FLASH_PCROP2SR_PCROP2_STRT);
  1223. *PCROPStartAddr = (reg_value << 3) + bank2_addr;
  1224. reg_value = (READ_REG(FLASH->PCROP2ER) & FLASH_PCROP2ER_PCROP2_END);
  1225. *PCROPEndAddr = (reg_value << 3) + bank2_addr;
  1226. }
  1227. #endif
  1228. else
  1229. {
  1230. /* Nothing to do */
  1231. }
  1232. }
  1233. *PCROPConfig |= (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP_RDP);
  1234. }
  1235. /**
  1236. * @}
  1237. */
  1238. /**
  1239. * @}
  1240. */
  1241. #endif /* HAL_FLASH_MODULE_ENABLED */
  1242. /**
  1243. * @}
  1244. */
  1245. /**
  1246. * @}
  1247. */