stm32l4xx_hal_flash_ex.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 STM32L4xx
  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. ##### How to use this driver #####
  21. ==============================================================================
  22. [..] This driver provides functions to configure and program the FLASH memory
  23. of all STM32L4xx 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 page, erase all sectors
  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. (++) Set/Reset the write protection
  33. (++) Set the Read protection Level
  34. (++) Program the user Option Bytes
  35. (++) Configure the PCROP protection
  36. (#) Get Option Bytes Configuration function: Use HAL_FLASHEx_OBGetConfig() to :
  37. (++) Get the value of a write protection area
  38. (++) Know if the read protection is activated
  39. (++) Get the value of the user Option Bytes
  40. (++) Get the value of a PCROP area
  41. @endverbatim
  42. ******************************************************************************
  43. * @attention
  44. *
  45. * Copyright (c) 2017 STMicroelectronics.
  46. * All rights reserved.
  47. *
  48. * This software is licensed under terms that can be found in the LICENSE file in
  49. * the root directory of this software component.
  50. * If no LICENSE file comes with this software, it is provided AS-IS.
  51. ******************************************************************************
  52. */
  53. /* Includes ------------------------------------------------------------------*/
  54. #include "stm32l4xx_hal.h"
  55. /** @addtogroup STM32L4xx_HAL_Driver
  56. * @{
  57. */
  58. /** @defgroup FLASHEx FLASHEx
  59. * @brief FLASH Extended HAL module driver
  60. * @{
  61. */
  62. #ifdef HAL_FLASH_MODULE_ENABLED
  63. /* Private typedef -----------------------------------------------------------*/
  64. /* Private define ------------------------------------------------------------*/
  65. /* Private macro -------------------------------------------------------------*/
  66. /* Private variables ---------------------------------------------------------*/
  67. /* Private function prototypes -----------------------------------------------*/
  68. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  69. * @{
  70. */
  71. static void FLASH_MassErase(uint32_t Banks);
  72. static HAL_StatusTypeDef FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset);
  73. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint32_t RDPLevel);
  74. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig);
  75. static HAL_StatusTypeDef FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr);
  76. static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t * WRPStartOffset, uint32_t * WRDPEndOffset);
  77. static uint32_t FLASH_OB_GetRDP(void);
  78. static uint32_t FLASH_OB_GetUser(void);
  79. static void FLASH_OB_GetPCROP(uint32_t * PCROPConfig, uint32_t * PCROPStartAddr, uint32_t * PCROPEndAddr);
  80. /**
  81. * @}
  82. */
  83. /* Exported functions -------------------------------------------------------*/
  84. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  85. * @{
  86. */
  87. /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
  88. * @brief Extended IO operation functions
  89. *
  90. @verbatim
  91. ===============================================================================
  92. ##### Extended programming operation functions #####
  93. ===============================================================================
  94. [..]
  95. This subsection provides a set of functions allowing to manage the Extended FLASH
  96. programming operations Operations.
  97. @endverbatim
  98. * @{
  99. */
  100. /**
  101. * @brief Perform a mass erase or erase the specified FLASH memory pages.
  102. * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
  103. * contains the configuration information for the erasing.
  104. *
  105. * @param[out] PageError : pointer to variable that contains the configuration
  106. * information on faulty page in case of error (0xFFFFFFFF means that all
  107. * the pages have been correctly erased)
  108. *
  109. * @retval HAL Status
  110. */
  111. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
  112. {
  113. HAL_StatusTypeDef status;
  114. uint32_t page_index;
  115. /* Process Locked */
  116. __HAL_LOCK(&pFlash);
  117. /* Check the parameters */
  118. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  119. /* Wait for last operation to be completed */
  120. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  121. if (status == HAL_OK)
  122. {
  123. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  124. /* Deactivate the cache if they are activated to avoid data misbehavior */
  125. if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != 0U)
  126. {
  127. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  128. {
  129. /* Disable data cache */
  130. __HAL_FLASH_DATA_CACHE_DISABLE();
  131. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_DCACHE_ENABLED;
  132. }
  133. else
  134. {
  135. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_ENABLED;
  136. }
  137. }
  138. else if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  139. {
  140. /* Disable data cache */
  141. __HAL_FLASH_DATA_CACHE_DISABLE();
  142. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  143. }
  144. else
  145. {
  146. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  147. }
  148. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  149. {
  150. /* Mass erase to be done */
  151. FLASH_MassErase(pEraseInit->Banks);
  152. /* Wait for last operation to be completed */
  153. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  154. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  155. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  156. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  157. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  158. /* If the erase operation is completed, disable the MER1 and MER2 Bits */
  159. CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1 | FLASH_CR_MER2));
  160. #else
  161. /* If the erase operation is completed, disable the MER1 Bit */
  162. CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1));
  163. #endif
  164. }
  165. else
  166. {
  167. /*Initialization of PageError variable*/
  168. *PageError = 0xFFFFFFFFU;
  169. for(page_index = pEraseInit->Page; page_index < (pEraseInit->Page + pEraseInit->NbPages); page_index++)
  170. {
  171. FLASH_PageErase(page_index, pEraseInit->Banks);
  172. /* Wait for last operation to be completed */
  173. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  174. /* If the erase operation is completed, disable the PER Bit */
  175. CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
  176. if (status != HAL_OK)
  177. {
  178. /* In case of error, stop erase procedure and return the faulty address */
  179. *PageError = page_index;
  180. break;
  181. }
  182. }
  183. }
  184. /* Flush the caches to be sure of the data consistency */
  185. FLASH_FlushCaches();
  186. }
  187. /* Process Unlocked */
  188. __HAL_UNLOCK(&pFlash);
  189. return status;
  190. }
  191. /**
  192. * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled.
  193. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  194. * contains the configuration information for the erasing.
  195. *
  196. * @retval HAL Status
  197. */
  198. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  199. {
  200. HAL_StatusTypeDef status = HAL_OK;
  201. /* Process Locked */
  202. __HAL_LOCK(&pFlash);
  203. /* Check the parameters */
  204. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  205. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  206. /* Deactivate the cache if they are activated to avoid data misbehavior */
  207. if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != 0U)
  208. {
  209. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  210. {
  211. /* Disable data cache */
  212. __HAL_FLASH_DATA_CACHE_DISABLE();
  213. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_DCACHE_ENABLED;
  214. }
  215. else
  216. {
  217. pFlash.CacheToReactivate = FLASH_CACHE_ICACHE_ENABLED;
  218. }
  219. }
  220. else if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  221. {
  222. /* Disable data cache */
  223. __HAL_FLASH_DATA_CACHE_DISABLE();
  224. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  225. }
  226. else
  227. {
  228. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  229. }
  230. /* Enable End of Operation and Error interrupts */
  231. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  232. pFlash.Bank = pEraseInit->Banks;
  233. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  234. {
  235. /* Mass erase to be done */
  236. pFlash.ProcedureOnGoing = FLASH_PROC_MASS_ERASE;
  237. FLASH_MassErase(pEraseInit->Banks);
  238. }
  239. else
  240. {
  241. /* Erase by page to be done */
  242. pFlash.ProcedureOnGoing = FLASH_PROC_PAGE_ERASE;
  243. pFlash.NbPagesToErase = pEraseInit->NbPages;
  244. pFlash.Page = pEraseInit->Page;
  245. /*Erase 1st page and wait for IT */
  246. FLASH_PageErase(pEraseInit->Page, pEraseInit->Banks);
  247. }
  248. return status;
  249. }
  250. /**
  251. * @brief Program Option bytes.
  252. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  253. * contains the configuration information for the programming.
  254. *
  255. * @retval HAL Status
  256. */
  257. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  258. {
  259. HAL_StatusTypeDef status = HAL_OK;
  260. /* Process Locked */
  261. __HAL_LOCK(&pFlash);
  262. /* Check the parameters */
  263. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  264. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  265. /* Write protection configuration */
  266. if((pOBInit->OptionType & OPTIONBYTE_WRP) != 0U)
  267. {
  268. /* Configure of Write protection on the selected area */
  269. if(FLASH_OB_WRPConfig(pOBInit->WRPArea, pOBInit->WRPStartOffset, pOBInit->WRPEndOffset) != HAL_OK)
  270. {
  271. status = HAL_ERROR;
  272. }
  273. }
  274. /* Read protection configuration */
  275. if((pOBInit->OptionType & OPTIONBYTE_RDP) != 0U)
  276. {
  277. /* Configure the Read protection level */
  278. if(FLASH_OB_RDPConfig(pOBInit->RDPLevel) != HAL_OK)
  279. {
  280. status = HAL_ERROR;
  281. }
  282. }
  283. /* User Configuration */
  284. if((pOBInit->OptionType & OPTIONBYTE_USER) != 0U)
  285. {
  286. /* Configure the user option bytes */
  287. if(FLASH_OB_UserConfig(pOBInit->USERType, pOBInit->USERConfig) != HAL_OK)
  288. {
  289. status = HAL_ERROR;
  290. }
  291. }
  292. /* PCROP Configuration */
  293. if((pOBInit->OptionType & OPTIONBYTE_PCROP) != 0U)
  294. {
  295. if (pOBInit->PCROPStartAddr != pOBInit->PCROPEndAddr)
  296. {
  297. /* Configure the Proprietary code readout protection */
  298. if(FLASH_OB_PCROPConfig(pOBInit->PCROPConfig, pOBInit->PCROPStartAddr, pOBInit->PCROPEndAddr) != HAL_OK)
  299. {
  300. status = HAL_ERROR;
  301. }
  302. }
  303. }
  304. /* Process Unlocked */
  305. __HAL_UNLOCK(&pFlash);
  306. return status;
  307. }
  308. /**
  309. * @brief Get the Option bytes configuration.
  310. * @param pOBInit pointer to an FLASH_OBInitStruct structure that contains the
  311. * configuration information.
  312. * @note The fields pOBInit->WRPArea and pOBInit->PCROPConfig should indicate
  313. * which area is requested for the WRP and PCROP, else no information will be returned
  314. *
  315. * @retval None
  316. */
  317. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  318. {
  319. pOBInit->OptionType = (OPTIONBYTE_RDP | OPTIONBYTE_USER);
  320. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  321. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  322. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  323. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  324. if((pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAA) || (pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAB) ||
  325. (pOBInit->WRPArea == OB_WRPAREA_BANK2_AREAA) || (pOBInit->WRPArea == OB_WRPAREA_BANK2_AREAB))
  326. #else
  327. if((pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAA) || (pOBInit->WRPArea == OB_WRPAREA_BANK1_AREAB))
  328. #endif
  329. {
  330. pOBInit->OptionType |= OPTIONBYTE_WRP;
  331. /* Get write protection on the selected area */
  332. FLASH_OB_GetWRP(pOBInit->WRPArea, &(pOBInit->WRPStartOffset), &(pOBInit->WRPEndOffset));
  333. }
  334. /* Get Read protection level */
  335. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  336. /* Get the user option bytes */
  337. pOBInit->USERConfig = FLASH_OB_GetUser();
  338. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  339. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  340. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  341. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  342. if((pOBInit->PCROPConfig == FLASH_BANK_1) || (pOBInit->PCROPConfig == FLASH_BANK_2))
  343. #else
  344. if(pOBInit->PCROPConfig == FLASH_BANK_1)
  345. #endif
  346. {
  347. pOBInit->OptionType |= OPTIONBYTE_PCROP;
  348. /* Get the Proprietary code readout protection */
  349. FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr));
  350. }
  351. }
  352. /**
  353. * @}
  354. */
  355. #if defined (FLASH_CFGR_LVEN)
  356. /** @defgroup FLASHEx_Exported_Functions_Group2 Extended specific configuration functions
  357. * @brief Extended specific configuration functions
  358. *
  359. @verbatim
  360. ===============================================================================
  361. ##### Extended specific configuration functions #####
  362. ===============================================================================
  363. [..]
  364. This subsection provides a set of functions allowing to manage the Extended FLASH
  365. specific configurations.
  366. @endverbatim
  367. * @{
  368. */
  369. /**
  370. * @brief Configuration of the LVE pin of the Flash (managed by power controller
  371. * or forced to low in order to use an external SMPS)
  372. * @param ConfigLVE Configuration of the LVE pin,
  373. * This parameter can be one of the following values:
  374. * @arg FLASH_LVE_PIN_CTRL: LVE FLASH pin controlled by power controller
  375. * @arg FLASH_LVE_PIN_FORCED: LVE FLASH pin enforced to low (external SMPS used)
  376. *
  377. * @note Before enforcing the LVE pin to low, the SOC should be in low voltage
  378. * range 2 and the voltage VDD12 should be higher than 1.08V and SMPS is ON.
  379. *
  380. * @retval HAL Status
  381. */
  382. HAL_StatusTypeDef HAL_FLASHEx_ConfigLVEPin(uint32_t ConfigLVE)
  383. {
  384. HAL_StatusTypeDef status;
  385. /* Process Locked */
  386. __HAL_LOCK(&pFlash);
  387. /* Check the parameters */
  388. assert_param(IS_FLASH_LVE_PIN(ConfigLVE));
  389. /* Wait for last operation to be completed */
  390. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  391. if (status == HAL_OK)
  392. {
  393. /* Check that the voltage scaling is range 2 */
  394. if (HAL_PWREx_GetVoltageRange() == PWR_REGULATOR_VOLTAGE_SCALE2)
  395. {
  396. /* Configure the LVEN bit */
  397. MODIFY_REG(FLASH->CFGR, FLASH_CFGR_LVEN, ConfigLVE);
  398. /* Check that the bit has been correctly configured */
  399. if (READ_BIT(FLASH->CFGR, FLASH_CFGR_LVEN) != ConfigLVE)
  400. {
  401. status = HAL_ERROR;
  402. }
  403. }
  404. else
  405. {
  406. /* Not allow to force Flash LVE pin if not in voltage range 2 */
  407. status = HAL_ERROR;
  408. }
  409. }
  410. /* Process Unlocked */
  411. __HAL_UNLOCK(&pFlash);
  412. return status;
  413. }
  414. /**
  415. * @}
  416. */
  417. #endif /* FLASH_CFGR_LVEN */
  418. /**
  419. * @}
  420. */
  421. /* Private functions ---------------------------------------------------------*/
  422. /** @addtogroup FLASHEx_Private_Functions
  423. * @{
  424. */
  425. /**
  426. * @brief Mass erase of FLASH memory.
  427. * @param Banks Banks to be erased
  428. * This parameter can be one of the following values:
  429. * @arg FLASH_BANK_1: Bank1 to be erased
  430. * @arg FLASH_BANK_2: Bank2 to be erased
  431. * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
  432. * @retval None
  433. */
  434. static void FLASH_MassErase(uint32_t Banks)
  435. {
  436. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  437. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) != 0U)
  438. #endif
  439. {
  440. /* Check the parameters */
  441. assert_param(IS_FLASH_BANK(Banks));
  442. /* Set the Mass Erase Bit for the bank 1 if requested */
  443. if((Banks & FLASH_BANK_1) != 0U)
  444. {
  445. SET_BIT(FLASH->CR, FLASH_CR_MER1);
  446. }
  447. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  448. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  449. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  450. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  451. /* Set the Mass Erase Bit for the bank 2 if requested */
  452. if((Banks & FLASH_BANK_2) != 0U)
  453. {
  454. SET_BIT(FLASH->CR, FLASH_CR_MER2);
  455. }
  456. #endif
  457. }
  458. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  459. else
  460. {
  461. SET_BIT(FLASH->CR, (FLASH_CR_MER1 | FLASH_CR_MER2));
  462. }
  463. #endif
  464. /* Proceed to erase all sectors */
  465. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  466. }
  467. /**
  468. * @brief Erase the specified FLASH memory page.
  469. * @param Page FLASH page to erase
  470. * This parameter must be a value between 0 and (max number of pages in the bank - 1)
  471. * @param Banks Bank(s) where the page will be erased
  472. * This parameter can be one of the following values:
  473. * @arg FLASH_BANK_1: Page in bank 1 to be erased
  474. * @arg FLASH_BANK_2: Page in bank 2 to be erased
  475. * @retval None
  476. */
  477. void FLASH_PageErase(uint32_t Page, uint32_t Banks)
  478. {
  479. /* Check the parameters */
  480. assert_param(IS_FLASH_PAGE(Page));
  481. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  482. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  483. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  484. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  485. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  486. if(READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) == 0U)
  487. {
  488. CLEAR_BIT(FLASH->CR, FLASH_CR_BKER);
  489. }
  490. else
  491. #endif
  492. {
  493. assert_param(IS_FLASH_BANK_EXCLUSIVE(Banks));
  494. if((Banks & FLASH_BANK_1) != 0U)
  495. {
  496. CLEAR_BIT(FLASH->CR, FLASH_CR_BKER);
  497. }
  498. else
  499. {
  500. SET_BIT(FLASH->CR, FLASH_CR_BKER);
  501. }
  502. }
  503. #else
  504. /* Prevent unused argument(s) compilation warning */
  505. UNUSED(Banks);
  506. #endif
  507. /* Proceed to erase the page */
  508. MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((Page & 0xFFU) << FLASH_CR_PNB_Pos));
  509. SET_BIT(FLASH->CR, FLASH_CR_PER);
  510. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  511. }
  512. /**
  513. * @brief Flush the instruction and data caches.
  514. * @retval None
  515. */
  516. void FLASH_FlushCaches(void)
  517. {
  518. FLASH_CacheTypeDef cache = pFlash.CacheToReactivate;
  519. /* Flush instruction cache */
  520. if((cache == FLASH_CACHE_ICACHE_ENABLED) ||
  521. (cache == FLASH_CACHE_ICACHE_DCACHE_ENABLED))
  522. {
  523. /* Disable instruction cache */
  524. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  525. /* Reset instruction cache */
  526. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  527. /* Enable instruction cache */
  528. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  529. }
  530. /* Flush data cache */
  531. if((cache == FLASH_CACHE_DCACHE_ENABLED) ||
  532. (cache == FLASH_CACHE_ICACHE_DCACHE_ENABLED))
  533. {
  534. /* Reset data cache */
  535. __HAL_FLASH_DATA_CACHE_RESET();
  536. /* Enable data cache */
  537. __HAL_FLASH_DATA_CACHE_ENABLE();
  538. }
  539. /* Reset internal variable */
  540. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  541. }
  542. /**
  543. * @brief Configure the write protection of the desired pages.
  544. *
  545. * @note When the memory read protection level is selected (RDP level = 1),
  546. * it is not possible to program or erase Flash memory if the CPU debug
  547. * features are connected (JTAG or single wire) or boot code is being
  548. * executed from RAM or System flash, even if WRP is not activated.
  549. * @note To configure the WRP options, the option lock bit OPTLOCK must be
  550. * cleared with the call of the HAL_FLASH_OB_Unlock() function.
  551. * @note To validate the WRP options, the option bytes must be reloaded
  552. * through the call of the HAL_FLASH_OB_Launch() function.
  553. *
  554. * @param WRPArea specifies the area to be configured.
  555. * This parameter can be one of the following values:
  556. * @arg OB_WRPAREA_BANK1_AREAA: Flash Bank 1 Area A
  557. * @arg OB_WRPAREA_BANK1_AREAB: Flash Bank 1 Area B
  558. * @arg OB_WRPAREA_BANK2_AREAA: Flash Bank 2 Area A (don't apply for STM32L43x/STM32L44x devices)
  559. * @arg OB_WRPAREA_BANK2_AREAB: Flash Bank 2 Area B (don't apply for STM32L43x/STM32L44x devices)
  560. *
  561. * @param WRPStartOffset specifies the start page of the write protected area
  562. * This parameter can be page number between 0 and (max number of pages in the bank - 1)
  563. *
  564. * @param WRDPEndOffset specifies the end page of the write protected area
  565. * This parameter can be page number between WRPStartOffset and (max number of pages in the bank - 1)
  566. *
  567. * @retval HAL Status
  568. */
  569. static HAL_StatusTypeDef FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset)
  570. {
  571. HAL_StatusTypeDef status;
  572. /* Check the parameters */
  573. assert_param(IS_OB_WRPAREA(WRPArea));
  574. assert_param(IS_FLASH_PAGE(WRPStartOffset));
  575. assert_param(IS_FLASH_PAGE(WRDPEndOffset));
  576. /* Wait for last operation to be completed */
  577. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  578. if(status == HAL_OK)
  579. {
  580. /* Configure the write protected area */
  581. if(WRPArea == OB_WRPAREA_BANK1_AREAA)
  582. {
  583. MODIFY_REG(FLASH->WRP1AR, (FLASH_WRP1AR_WRP1A_STRT | FLASH_WRP1AR_WRP1A_END),
  584. (WRPStartOffset | (WRDPEndOffset << 16)));
  585. }
  586. else if(WRPArea == OB_WRPAREA_BANK1_AREAB)
  587. {
  588. MODIFY_REG(FLASH->WRP1BR, (FLASH_WRP1BR_WRP1B_STRT | FLASH_WRP1BR_WRP1B_END),
  589. (WRPStartOffset | (WRDPEndOffset << 16)));
  590. }
  591. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  592. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  593. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  594. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  595. else if(WRPArea == OB_WRPAREA_BANK2_AREAA)
  596. {
  597. MODIFY_REG(FLASH->WRP2AR, (FLASH_WRP2AR_WRP2A_STRT | FLASH_WRP2AR_WRP2A_END),
  598. (WRPStartOffset | (WRDPEndOffset << 16)));
  599. }
  600. else if(WRPArea == OB_WRPAREA_BANK2_AREAB)
  601. {
  602. MODIFY_REG(FLASH->WRP2BR, (FLASH_WRP2BR_WRP2B_STRT | FLASH_WRP2BR_WRP2B_END),
  603. (WRPStartOffset | (WRDPEndOffset << 16)));
  604. }
  605. #endif
  606. else
  607. {
  608. /* Nothing to do */
  609. }
  610. /* Set OPTSTRT Bit */
  611. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  612. /* Wait for last operation to be completed */
  613. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  614. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  615. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  616. }
  617. return status;
  618. }
  619. /**
  620. * @brief Set the read protection level.
  621. *
  622. * @note To configure the RDP level, the option lock bit OPTLOCK must be
  623. * cleared with the call of the HAL_FLASH_OB_Unlock() function.
  624. * @note To validate the RDP level, the option bytes must be reloaded
  625. * through the call of the HAL_FLASH_OB_Launch() function.
  626. * @note !!! Warning : When enabling OB_RDP level 2 it's no more possible
  627. * to go back to level 1 or 0 !!!
  628. *
  629. * @param RDPLevel specifies the read protection level.
  630. * This parameter can be one of the following values:
  631. * @arg OB_RDP_LEVEL_0: No protection
  632. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  633. * @arg OB_RDP_LEVEL_2: Full chip protection
  634. *
  635. * @retval HAL status
  636. */
  637. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint32_t RDPLevel)
  638. {
  639. HAL_StatusTypeDef status;
  640. /* Check the parameters */
  641. assert_param(IS_OB_RDP_LEVEL(RDPLevel));
  642. /* Wait for last operation to be completed */
  643. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  644. if(status == HAL_OK)
  645. {
  646. /* Configure the RDP level in the option bytes register */
  647. MODIFY_REG(FLASH->OPTR, FLASH_OPTR_RDP, RDPLevel);
  648. /* Set OPTSTRT Bit */
  649. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  650. /* Wait for last operation to be completed */
  651. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  652. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  653. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  654. }
  655. return status;
  656. }
  657. /**
  658. * @brief Program the FLASH User Option Byte.
  659. *
  660. * @note To configure the user option bytes, the option lock bit OPTLOCK must
  661. * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
  662. * @note To validate the user option bytes, the option bytes must be reloaded
  663. * through the call of the HAL_FLASH_OB_Launch() function.
  664. *
  665. * @param UserType The FLASH User Option Bytes to be modified
  666. * @param UserConfig The FLASH User Option Bytes values:
  667. * BOR_LEV(Bit8-10), nRST_STOP(Bit12), nRST_STDBY(Bit13), IWDG_SW(Bit16),
  668. * IWDG_STOP(Bit17), IWDG_STDBY(Bit18), WWDG_SW(Bit19), BFB2(Bit20),
  669. * DUALBANK(Bit21), nBOOT1(Bit23), SRAM2_PE(Bit24) and SRAM2_RST(Bit25).
  670. *
  671. * @retval HAL status
  672. */
  673. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
  674. {
  675. uint32_t optr_reg_val = 0;
  676. uint32_t optr_reg_mask = 0;
  677. HAL_StatusTypeDef status;
  678. /* Check the parameters */
  679. assert_param(IS_OB_USER_TYPE(UserType));
  680. /* Wait for last operation to be completed */
  681. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  682. if(status == HAL_OK)
  683. {
  684. if((UserType & OB_USER_BOR_LEV) != 0U)
  685. {
  686. /* BOR level option byte should be modified */
  687. assert_param(IS_OB_USER_BOR_LEVEL(UserConfig & FLASH_OPTR_BOR_LEV));
  688. /* Set value and mask for BOR level option byte */
  689. optr_reg_val |= (UserConfig & FLASH_OPTR_BOR_LEV);
  690. optr_reg_mask |= FLASH_OPTR_BOR_LEV;
  691. }
  692. if((UserType & OB_USER_nRST_STOP) != 0U)
  693. {
  694. /* nRST_STOP option byte should be modified */
  695. assert_param(IS_OB_USER_STOP(UserConfig & FLASH_OPTR_nRST_STOP));
  696. /* Set value and mask for nRST_STOP option byte */
  697. optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_STOP);
  698. optr_reg_mask |= FLASH_OPTR_nRST_STOP;
  699. }
  700. if((UserType & OB_USER_nRST_STDBY) != 0U)
  701. {
  702. /* nRST_STDBY option byte should be modified */
  703. assert_param(IS_OB_USER_STANDBY(UserConfig & FLASH_OPTR_nRST_STDBY));
  704. /* Set value and mask for nRST_STDBY option byte */
  705. optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_STDBY);
  706. optr_reg_mask |= FLASH_OPTR_nRST_STDBY;
  707. }
  708. if((UserType & OB_USER_nRST_SHDW) != 0U)
  709. {
  710. /* nRST_SHDW option byte should be modified */
  711. assert_param(IS_OB_USER_SHUTDOWN(UserConfig & FLASH_OPTR_nRST_SHDW));
  712. /* Set value and mask for nRST_SHDW option byte */
  713. optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_SHDW);
  714. optr_reg_mask |= FLASH_OPTR_nRST_SHDW;
  715. }
  716. if((UserType & OB_USER_IWDG_SW) != 0U)
  717. {
  718. /* IWDG_SW option byte should be modified */
  719. assert_param(IS_OB_USER_IWDG(UserConfig & FLASH_OPTR_IWDG_SW));
  720. /* Set value and mask for IWDG_SW option byte */
  721. optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_SW);
  722. optr_reg_mask |= FLASH_OPTR_IWDG_SW;
  723. }
  724. if((UserType & OB_USER_IWDG_STOP) != 0U)
  725. {
  726. /* IWDG_STOP option byte should be modified */
  727. assert_param(IS_OB_USER_IWDG_STOP(UserConfig & FLASH_OPTR_IWDG_STOP));
  728. /* Set value and mask for IWDG_STOP option byte */
  729. optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_STOP);
  730. optr_reg_mask |= FLASH_OPTR_IWDG_STOP;
  731. }
  732. if((UserType & OB_USER_IWDG_STDBY) != 0U)
  733. {
  734. /* IWDG_STDBY option byte should be modified */
  735. assert_param(IS_OB_USER_IWDG_STDBY(UserConfig & FLASH_OPTR_IWDG_STDBY));
  736. /* Set value and mask for IWDG_STDBY option byte */
  737. optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_STDBY);
  738. optr_reg_mask |= FLASH_OPTR_IWDG_STDBY;
  739. }
  740. if((UserType & OB_USER_WWDG_SW) != 0U)
  741. {
  742. /* WWDG_SW option byte should be modified */
  743. assert_param(IS_OB_USER_WWDG(UserConfig & FLASH_OPTR_WWDG_SW));
  744. /* Set value and mask for WWDG_SW option byte */
  745. optr_reg_val |= (UserConfig & FLASH_OPTR_WWDG_SW);
  746. optr_reg_mask |= FLASH_OPTR_WWDG_SW;
  747. }
  748. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  749. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  750. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  751. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  752. if((UserType & OB_USER_BFB2) != 0U)
  753. {
  754. /* BFB2 option byte should be modified */
  755. assert_param(IS_OB_USER_BFB2(UserConfig & FLASH_OPTR_BFB2));
  756. /* Set value and mask for BFB2 option byte */
  757. optr_reg_val |= (UserConfig & FLASH_OPTR_BFB2);
  758. optr_reg_mask |= FLASH_OPTR_BFB2;
  759. }
  760. if((UserType & OB_USER_DUALBANK) != 0U)
  761. {
  762. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  763. /* DUALBANK option byte should be modified */
  764. assert_param(IS_OB_USER_DUALBANK(UserConfig & FLASH_OPTR_DB1M));
  765. /* Set value and mask for DUALBANK option byte */
  766. optr_reg_val |= (UserConfig & FLASH_OPTR_DB1M);
  767. optr_reg_mask |= FLASH_OPTR_DB1M;
  768. #else
  769. /* DUALBANK option byte should be modified */
  770. assert_param(IS_OB_USER_DUALBANK(UserConfig & FLASH_OPTR_DUALBANK));
  771. /* Set value and mask for DUALBANK option byte */
  772. optr_reg_val |= (UserConfig & FLASH_OPTR_DUALBANK);
  773. optr_reg_mask |= FLASH_OPTR_DUALBANK;
  774. #endif
  775. }
  776. #endif
  777. if((UserType & OB_USER_nBOOT1) != 0U)
  778. {
  779. /* nBOOT1 option byte should be modified */
  780. assert_param(IS_OB_USER_BOOT1(UserConfig & FLASH_OPTR_nBOOT1));
  781. /* Set value and mask for nBOOT1 option byte */
  782. optr_reg_val |= (UserConfig & FLASH_OPTR_nBOOT1);
  783. optr_reg_mask |= FLASH_OPTR_nBOOT1;
  784. }
  785. if((UserType & OB_USER_SRAM2_PE) != 0U)
  786. {
  787. /* SRAM2_PE option byte should be modified */
  788. assert_param(IS_OB_USER_SRAM2_PARITY(UserConfig & FLASH_OPTR_SRAM2_PE));
  789. /* Set value and mask for SRAM2_PE option byte */
  790. optr_reg_val |= (UserConfig & FLASH_OPTR_SRAM2_PE);
  791. optr_reg_mask |= FLASH_OPTR_SRAM2_PE;
  792. }
  793. if((UserType & OB_USER_SRAM2_RST) != 0U)
  794. {
  795. /* SRAM2_RST option byte should be modified */
  796. assert_param(IS_OB_USER_SRAM2_RST(UserConfig & FLASH_OPTR_SRAM2_RST));
  797. /* Set value and mask for SRAM2_RST option byte */
  798. optr_reg_val |= (UserConfig & FLASH_OPTR_SRAM2_RST);
  799. optr_reg_mask |= FLASH_OPTR_SRAM2_RST;
  800. }
  801. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || \
  802. defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  803. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  804. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  805. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  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. #endif
  823. /* Configure the option bytes register */
  824. MODIFY_REG(FLASH->OPTR, optr_reg_mask, optr_reg_val);
  825. /* Set OPTSTRT Bit */
  826. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  827. /* Wait for last operation to be completed */
  828. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  829. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  830. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  831. }
  832. return status;
  833. }
  834. /**
  835. * @brief Configure the Proprietary code readout protection of the desired addresses.
  836. *
  837. * @note To configure the PCROP options, the option lock bit OPTLOCK must be
  838. * cleared with the call of the HAL_FLASH_OB_Unlock() function.
  839. * @note To validate the PCROP options, the option bytes must be reloaded
  840. * through the call of the HAL_FLASH_OB_Launch() function.
  841. *
  842. * @param PCROPConfig specifies the configuration (Bank to be configured and PCROP_RDP option).
  843. * This parameter must be a combination of FLASH_BANK_1 or FLASH_BANK_2
  844. * with OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE
  845. *
  846. * @param PCROPStartAddr specifies the start address of the Proprietary code readout protection
  847. * This parameter can be an address between begin and end of the bank
  848. *
  849. * @param PCROPEndAddr specifies the end address of the Proprietary code readout protection
  850. * This parameter can be an address between PCROPStartAddr and end of the bank
  851. *
  852. * @retval HAL Status
  853. */
  854. static HAL_StatusTypeDef FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr)
  855. {
  856. HAL_StatusTypeDef status;
  857. uint32_t reg_value;
  858. uint32_t bank1_addr;
  859. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  860. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  861. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  862. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  863. uint32_t bank2_addr;
  864. #endif
  865. /* Check the parameters */
  866. assert_param(IS_FLASH_BANK_EXCLUSIVE(PCROPConfig & FLASH_BANK_BOTH));
  867. assert_param(IS_OB_PCROP_RDP(PCROPConfig & FLASH_PCROP1ER_PCROP_RDP));
  868. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(PCROPStartAddr));
  869. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(PCROPEndAddr));
  870. /* Wait for last operation to be completed */
  871. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  872. if(status == HAL_OK)
  873. {
  874. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  875. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  876. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  877. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  878. /* Get the information about the bank swapping */
  879. if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0U)
  880. {
  881. bank1_addr = FLASH_BASE;
  882. bank2_addr = FLASH_BASE + FLASH_BANK_SIZE;
  883. }
  884. else
  885. {
  886. bank1_addr = FLASH_BASE + FLASH_BANK_SIZE;
  887. bank2_addr = FLASH_BASE;
  888. }
  889. #else
  890. bank1_addr = FLASH_BASE;
  891. #endif
  892. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  893. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) == 0U)
  894. {
  895. /* Configure the Proprietary code readout protection */
  896. if((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_1)
  897. {
  898. reg_value = ((PCROPStartAddr - FLASH_BASE) >> 4);
  899. MODIFY_REG(FLASH->PCROP1SR, FLASH_PCROP1SR_PCROP1_STRT, reg_value);
  900. reg_value = ((PCROPEndAddr - FLASH_BASE) >> 4);
  901. MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP1_END, reg_value);
  902. }
  903. else if((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_2)
  904. {
  905. reg_value = ((PCROPStartAddr - FLASH_BASE) >> 4);
  906. MODIFY_REG(FLASH->PCROP2SR, FLASH_PCROP2SR_PCROP2_STRT, reg_value);
  907. reg_value = ((PCROPEndAddr - FLASH_BASE) >> 4);
  908. MODIFY_REG(FLASH->PCROP2ER, FLASH_PCROP2ER_PCROP2_END, reg_value);
  909. }
  910. else
  911. {
  912. /* Nothing to do */
  913. }
  914. }
  915. else
  916. #endif
  917. {
  918. /* Configure the Proprietary code readout protection */
  919. if((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_1)
  920. {
  921. reg_value = ((PCROPStartAddr - bank1_addr) >> 3);
  922. MODIFY_REG(FLASH->PCROP1SR, FLASH_PCROP1SR_PCROP1_STRT, reg_value);
  923. reg_value = ((PCROPEndAddr - bank1_addr) >> 3);
  924. MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP1_END, reg_value);
  925. }
  926. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  927. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  928. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  929. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  930. else if((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_2)
  931. {
  932. reg_value = ((PCROPStartAddr - bank2_addr) >> 3);
  933. MODIFY_REG(FLASH->PCROP2SR, FLASH_PCROP2SR_PCROP2_STRT, reg_value);
  934. reg_value = ((PCROPEndAddr - bank2_addr) >> 3);
  935. MODIFY_REG(FLASH->PCROP2ER, FLASH_PCROP2ER_PCROP2_END, reg_value);
  936. }
  937. #endif
  938. else
  939. {
  940. /* Nothing to do */
  941. }
  942. }
  943. MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP_RDP, (PCROPConfig & FLASH_PCROP1ER_PCROP_RDP));
  944. /* Set OPTSTRT Bit */
  945. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  946. /* Wait for last operation to be completed */
  947. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  948. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  949. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  950. }
  951. return status;
  952. }
  953. /**
  954. * @brief Return the FLASH Write Protection Option Bytes value.
  955. *
  956. * @param[in] WRPArea: specifies the area to be returned.
  957. * This parameter can be one of the following values:
  958. * @arg OB_WRPAREA_BANK1_AREAA: Flash Bank 1 Area A
  959. * @arg OB_WRPAREA_BANK1_AREAB: Flash Bank 1 Area B
  960. * @arg OB_WRPAREA_BANK2_AREAA: Flash Bank 2 Area A (don't apply to STM32L43x/STM32L44x devices)
  961. * @arg OB_WRPAREA_BANK2_AREAB: Flash Bank 2 Area B (don't apply to STM32L43x/STM32L44x devices)
  962. *
  963. * @param[out] WRPStartOffset: specifies the address where to copied the start page
  964. * of the write protected area
  965. *
  966. * @param[out] WRDPEndOffset: specifies the address where to copied the end page of
  967. * the write protected area
  968. *
  969. * @retval None
  970. */
  971. static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t * WRPStartOffset, uint32_t * WRDPEndOffset)
  972. {
  973. /* Get the configuration of the write protected area */
  974. if(WRPArea == OB_WRPAREA_BANK1_AREAA)
  975. {
  976. *WRPStartOffset = READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_STRT);
  977. *WRDPEndOffset = (READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_END) >> 16);
  978. }
  979. else if(WRPArea == OB_WRPAREA_BANK1_AREAB)
  980. {
  981. *WRPStartOffset = READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_STRT);
  982. *WRDPEndOffset = (READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_END) >> 16);
  983. }
  984. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  985. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  986. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  987. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  988. else if(WRPArea == OB_WRPAREA_BANK2_AREAA)
  989. {
  990. *WRPStartOffset = READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_STRT);
  991. *WRDPEndOffset = (READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_END) >> 16);
  992. }
  993. else if(WRPArea == OB_WRPAREA_BANK2_AREAB)
  994. {
  995. *WRPStartOffset = READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_STRT);
  996. *WRDPEndOffset = (READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_END) >> 16);
  997. }
  998. #endif
  999. else
  1000. {
  1001. /* Nothing to do */
  1002. }
  1003. }
  1004. /**
  1005. * @brief Return the FLASH Read Protection level.
  1006. * @retval FLASH ReadOut Protection Status:
  1007. * This return value can be one of the following values:
  1008. * @arg OB_RDP_LEVEL_0: No protection
  1009. * @arg OB_RDP_LEVEL_1: Read protection of the memory
  1010. * @arg OB_RDP_LEVEL_2: Full chip protection
  1011. */
  1012. static uint32_t FLASH_OB_GetRDP(void)
  1013. {
  1014. uint32_t rdp_level = READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP);
  1015. if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
  1016. {
  1017. return (OB_RDP_LEVEL_1);
  1018. }
  1019. else
  1020. {
  1021. return (READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP));
  1022. }
  1023. }
  1024. /**
  1025. * @brief Return the FLASH User Option Byte value.
  1026. * @retval The FLASH User Option Bytes values:
  1027. * For STM32L47x/STM32L48x devices :
  1028. * BOR_LEV(Bit8-10), nRST_STOP(Bit12), nRST_STDBY(Bit13), nRST_SHDW(Bit14),
  1029. * IWDG_SW(Bit16), IWDG_STOP(Bit17), IWDG_STDBY(Bit18), WWDG_SW(Bit19),
  1030. * BFB2(Bit20), DUALBANK(Bit21), nBOOT1(Bit23), SRAM2_PE(Bit24) and SRAM2_RST(Bit25).
  1031. * For STM32L43x/STM32L44x devices :
  1032. * BOR_LEV(Bit8-10), nRST_STOP(Bit12), nRST_STDBY(Bit13), nRST_SHDW(Bit14),
  1033. * IWDG_SW(Bit16), IWDG_STOP(Bit17), IWDG_STDBY(Bit18), WWDG_SW(Bit19),
  1034. * nBOOT1(Bit23), SRAM2_PE(Bit24), SRAM2_RST(Bit25), nSWBOOT0(Bit26) and nBOOT0(Bit27).
  1035. */
  1036. static uint32_t FLASH_OB_GetUser(void)
  1037. {
  1038. uint32_t user_config = READ_REG(FLASH->OPTR);
  1039. CLEAR_BIT(user_config, FLASH_OPTR_RDP);
  1040. return user_config;
  1041. }
  1042. /**
  1043. * @brief Return the FLASH Write Protection Option Bytes value.
  1044. *
  1045. * @param PCROPConfig [inout]: specifies the configuration (Bank to be configured and PCROP_RDP option).
  1046. * This parameter must be a combination of FLASH_BANK_1 or FLASH_BANK_2
  1047. * with OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE
  1048. *
  1049. * @param PCROPStartAddr [out]: specifies the address where to copied the start address
  1050. * of the Proprietary code readout protection
  1051. *
  1052. * @param PCROPEndAddr [out]: specifies the address where to copied the end address of
  1053. * the Proprietary code readout protection
  1054. *
  1055. * @retval None
  1056. */
  1057. static void FLASH_OB_GetPCROP(uint32_t * PCROPConfig, uint32_t * PCROPStartAddr, uint32_t * PCROPEndAddr)
  1058. {
  1059. uint32_t reg_value;
  1060. uint32_t bank1_addr;
  1061. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1062. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1063. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  1064. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1065. uint32_t bank2_addr;
  1066. #endif
  1067. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1068. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1069. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  1070. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1071. /* Get the information about the bank swapping */
  1072. if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0U)
  1073. {
  1074. bank1_addr = FLASH_BASE;
  1075. bank2_addr = FLASH_BASE + FLASH_BANK_SIZE;
  1076. }
  1077. else
  1078. {
  1079. bank1_addr = FLASH_BASE + FLASH_BANK_SIZE;
  1080. bank2_addr = FLASH_BASE;
  1081. }
  1082. #else
  1083. bank1_addr = FLASH_BASE;
  1084. #endif
  1085. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1086. if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) == 0U)
  1087. {
  1088. if(((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_1)
  1089. {
  1090. reg_value = (READ_REG(FLASH->PCROP1SR) & FLASH_PCROP1SR_PCROP1_STRT);
  1091. *PCROPStartAddr = (reg_value << 4) + FLASH_BASE;
  1092. reg_value = (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP1_END);
  1093. *PCROPEndAddr = (reg_value << 4) + FLASH_BASE + 0xFU;
  1094. }
  1095. else if(((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_2)
  1096. {
  1097. reg_value = (READ_REG(FLASH->PCROP2SR) & FLASH_PCROP2SR_PCROP2_STRT);
  1098. *PCROPStartAddr = (reg_value << 4) + FLASH_BASE;
  1099. reg_value = (READ_REG(FLASH->PCROP2ER) & FLASH_PCROP2ER_PCROP2_END);
  1100. *PCROPEndAddr = (reg_value << 4) + FLASH_BASE + 0xFU;;
  1101. }
  1102. else
  1103. {
  1104. /* Nothing to do */
  1105. }
  1106. }
  1107. else
  1108. #endif
  1109. {
  1110. if(((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_1)
  1111. {
  1112. reg_value = (READ_REG(FLASH->PCROP1SR) & FLASH_PCROP1SR_PCROP1_STRT);
  1113. *PCROPStartAddr = (reg_value << 3) + bank1_addr;
  1114. reg_value = (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP1_END);
  1115. *PCROPEndAddr = (reg_value << 3) + bank1_addr + 0x7U;
  1116. }
  1117. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  1118. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  1119. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
  1120. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  1121. else if(((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_2)
  1122. {
  1123. reg_value = (READ_REG(FLASH->PCROP2SR) & FLASH_PCROP2SR_PCROP2_STRT);
  1124. *PCROPStartAddr = (reg_value << 3) + bank2_addr;
  1125. reg_value = (READ_REG(FLASH->PCROP2ER) & FLASH_PCROP2ER_PCROP2_END);
  1126. *PCROPEndAddr = (reg_value << 3) + bank2_addr + 0x7U;
  1127. }
  1128. #endif
  1129. else
  1130. {
  1131. /* Nothing to do */
  1132. }
  1133. }
  1134. *PCROPConfig |= (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP_RDP);
  1135. }
  1136. /**
  1137. * @}
  1138. */
  1139. /**
  1140. * @}
  1141. */
  1142. #endif /* HAL_FLASH_MODULE_ENABLED */
  1143. /**
  1144. * @}
  1145. */
  1146. /**
  1147. * @}
  1148. */