stm32l4xx_hal_flash_ex.c 46 KB

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