stm32l4xx_hal_flash_ex.c 49 KB

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