You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1149 lines
40 KiB

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