stm32l4xx_hal_flash_ex.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. */