stm32l0xx_hal_flash_ex.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_flash_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended FLASH HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the internal FLASH memory:
  9. * + FLASH Interface configuration
  10. * + FLASH Memory Erasing
  11. * + DATA EEPROM Programming/Erasing
  12. * + Option Bytes Programming
  13. * + Interrupts management
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### Flash peripheral Extended features #####
  18. ==============================================================================
  19. [..] Comparing to other products, the FLASH interface for STM32L0xx
  20. devices contains the following additional features
  21. (+) Erase functions
  22. (+) DATA_EEPROM memory management
  23. (+) BOOT option bit configuration
  24. (+) PCROP protection for all sectors
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..] This driver provides functions to configure and program the FLASH memory
  28. of all STM32L0xx. It includes:
  29. (+) Full DATA_EEPROM erase and program management
  30. (+) Boot activation
  31. (+) PCROP protection configuration and control for all pages
  32. @endverbatim
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * Copyright (c) 2016 STMicroelectronics.
  37. * All rights reserved.
  38. *
  39. * This software is licensed under terms that can be found in the LICENSE file in
  40. * the root directory of this software component.
  41. * If no LICENSE file comes with this software, it is provided AS-IS.
  42. ******************************************************************************
  43. */
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32l0xx_hal.h"
  46. /** @addtogroup STM32L0xx_HAL_Driver
  47. * @{
  48. */
  49. #ifdef HAL_FLASH_MODULE_ENABLED
  50. /** @addtogroup FLASH
  51. * @{
  52. */
  53. /** @addtogroup FLASH_Private_Variables
  54. * @{
  55. */
  56. /**
  57. * @}
  58. */
  59. /**
  60. * @}
  61. */
  62. /** @defgroup FLASHEx FLASHEx
  63. * @brief FLASH HAL Extension module driver
  64. * @{
  65. */
  66. /* Private typedef -----------------------------------------------------------*/
  67. /* Private define ------------------------------------------------------------*/
  68. /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
  69. * @{
  70. */
  71. /**
  72. * @}
  73. */
  74. /* Private macro -------------------------------------------------------------*/
  75. /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
  76. * @{
  77. */
  78. /**
  79. * @}
  80. */
  81. /* Private variables ---------------------------------------------------------*/
  82. /* Private function prototypes -----------------------------------------------*/
  83. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  84. * @{
  85. */
  86. #if defined(FLASH_OPTR_BFB2)
  87. static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT);
  88. #endif /* FLASH_OPTR_BFB2 */
  89. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
  90. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  91. static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
  92. static uint8_t FLASH_OB_GetRDP(void);
  93. static uint8_t FLASH_OB_GetUser(void);
  94. static uint8_t FLASH_OB_GetBOR(void);
  95. static uint8_t FLASH_OB_GetBOOTBit1(void);
  96. static HAL_StatusTypeDef FLASH_OB_BOOTBit1Config(uint8_t OB_BootBit1);
  97. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  98. static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t Sector2, uint32_t NewState);
  99. #else
  100. static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t NewState);
  101. #endif
  102. static uint32_t FLASH_OB_GetWRP(void);
  103. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  104. static uint32_t FLASH_OB_GetWRP2(void);
  105. #endif
  106. /**
  107. * @}
  108. */
  109. /* Exported functions ---------------------------------------------------------*/
  110. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  111. * @{
  112. */
  113. /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
  114. * @brief FLASH Memory Erasing functions
  115. *
  116. @verbatim
  117. ==============================================================================
  118. ##### FLASH Erasing Programming functions #####
  119. ==============================================================================
  120. [..] The FLASH Memory Erasing functions, includes the following functions:
  121. (+) HAL_FLASHEx_Erase: return only when erase has been done
  122. (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback
  123. is called with parameter 0xFFFFFFFF
  124. [..] Any operation of erase should follow these steps:
  125. (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
  126. program memory access.
  127. (#) Call the desired function to erase page.
  128. (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
  129. (recommended to protect the FLASH memory against possible unwanted operation).
  130. @endverbatim
  131. * @{
  132. */
  133. /**
  134. * @brief Erase the specified FLASH memory Pages
  135. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  136. * must be called before.
  137. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  138. * (recommended to protect the FLASH memory against possible unwanted operation)
  139. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  140. * contains the configuration information for the erasing.
  141. *
  142. * @param[out] PageError pointer to variable that
  143. * contains the configuration information on faulty page in case of error
  144. * (0xFFFFFFFF means that all the pages have been correctly erased)
  145. *
  146. * @retval HAL_StatusTypeDef HAL Status
  147. */
  148. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
  149. {
  150. HAL_StatusTypeDef status = HAL_ERROR;
  151. uint32_t address = 0U;
  152. /* Process Locked */
  153. __HAL_LOCK(&pFlash);
  154. /* Wait for last operation to be completed */
  155. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  156. if (status == HAL_OK)
  157. {
  158. /*Initialization of PageError variable*/
  159. *PageError = 0xFFFFFFFFU;
  160. /* Check the parameters */
  161. assert_param(IS_NBPAGES(pEraseInit->NbPages));
  162. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  163. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  164. assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U));
  165. /* Erase page by page to be done*/
  166. for(address = pEraseInit->PageAddress;
  167. address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
  168. address += FLASH_PAGE_SIZE)
  169. {
  170. FLASH_PageErase(address);
  171. /* Wait for last operation to be completed */
  172. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  173. /* If the erase operation is completed, disable the ERASE Bit */
  174. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  175. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  176. if (status != HAL_OK)
  177. {
  178. /* In case of error, stop erase procedure and return the faulty address */
  179. *PageError = address;
  180. break;
  181. }
  182. }
  183. }
  184. /* Process Unlocked */
  185. __HAL_UNLOCK(&pFlash);
  186. return status;
  187. }
  188. /**
  189. * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
  190. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  191. * must be called before.
  192. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  193. * (recommended to protect the FLASH memory against possible unwanted operation)
  194. * End of erase is done when @ref HAL_FLASH_EndOfOperationCallback is called with parameter
  195. * 0xFFFFFFFF
  196. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  197. * contains the configuration information for the erasing.
  198. *
  199. * @retval HAL_StatusTypeDef HAL Status
  200. */
  201. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  202. {
  203. HAL_StatusTypeDef status = HAL_ERROR;
  204. /* If procedure already ongoing, reject the next one */
  205. if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  206. {
  207. return HAL_ERROR;
  208. }
  209. /* Check the parameters */
  210. assert_param(IS_NBPAGES(pEraseInit->NbPages));
  211. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  212. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  213. assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
  214. /* Process Locked */
  215. __HAL_LOCK(&pFlash);
  216. /* Wait for last operation to be completed */
  217. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  218. if (status == HAL_OK)
  219. {
  220. /* Enable End of FLASH Operation and Error source interrupts */
  221. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  222. pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
  223. pFlash.NbPagesToErase = pEraseInit->NbPages;
  224. pFlash.Page = pEraseInit->PageAddress;
  225. /*Erase 1st page and wait for IT*/
  226. FLASH_PageErase(pEraseInit->PageAddress);
  227. }
  228. else
  229. {
  230. /* Process Unlocked */
  231. __HAL_UNLOCK(&pFlash);
  232. }
  233. return status;
  234. }
  235. /**
  236. * @}
  237. */
  238. /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
  239. * @brief Option Bytes Programming functions
  240. *
  241. @verbatim
  242. ==============================================================================
  243. ##### Option Bytes Programming functions #####
  244. ==============================================================================
  245. [..] Any operation of erase or program should follow these steps:
  246. (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
  247. register access.
  248. (#) Call following function to program the desired option bytes.
  249. (++) HAL_FLASHEx_OBProgram:
  250. - To Enable/Disable the desired sector write protection.
  251. - To set the desired read Protection Level.
  252. - To configure the user option Bytes: IWDG, STOP and the Standby.
  253. - To Set the BOR level.
  254. (#) Once all needed option bytes to be programmed are correctly written, call the
  255. HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  256. (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
  257. to protect the option Bytes against possible unwanted operations).
  258. [..] Proprietary code Read Out Protection (PcROP):
  259. (#) The PcROP sector is selected by using the same option bytes as the Write
  260. protection (nWRPi bits). As a result, these 2 options are exclusive each other.
  261. (#) In order to activate the PcROP (change the function of the nWRPi option bits),
  262. the WPRMOD option bit must be activated.
  263. (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
  264. means: if WPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
  265. is read/write protected.
  266. (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
  267. (++) HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
  268. (++) HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
  269. @endverbatim
  270. * @{
  271. */
  272. /**
  273. * @brief Program option bytes
  274. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  275. * contains the configuration information for the programming.
  276. *
  277. * @retval HAL_StatusTypeDef HAL Status
  278. */
  279. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  280. {
  281. HAL_StatusTypeDef status = HAL_ERROR;
  282. /* Process Locked */
  283. __HAL_LOCK(&pFlash);
  284. /* Check the parameters */
  285. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  286. /*Write protection configuration*/
  287. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  288. {
  289. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  290. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  291. status = FLASH_OB_ProtectedSectorsConfig(pOBInit->WRPSector, pOBInit->WRPSector2, pOBInit->WRPState);
  292. #else
  293. status = FLASH_OB_ProtectedSectorsConfig(pOBInit->WRPSector, pOBInit->WRPState);
  294. #endif
  295. if (status != HAL_OK)
  296. {
  297. /* Process Unlocked */
  298. __HAL_UNLOCK(&pFlash);
  299. return status;
  300. }
  301. }
  302. /* Read protection configuration*/
  303. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  304. {
  305. status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
  306. if (status != HAL_OK)
  307. {
  308. /* Process Unlocked */
  309. __HAL_UNLOCK(&pFlash);
  310. return status;
  311. }
  312. }
  313. /* USER configuration*/
  314. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  315. {
  316. status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
  317. pOBInit->USERConfig & OB_STOP_NORST,
  318. pOBInit->USERConfig & OB_STDBY_NORST);
  319. if (status != HAL_OK)
  320. {
  321. /* Process Unlocked */
  322. __HAL_UNLOCK(&pFlash);
  323. return status;
  324. }
  325. }
  326. /* BOR Level configuration*/
  327. if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
  328. {
  329. status = FLASH_OB_BORConfig(pOBInit->BORLevel);
  330. if (status != HAL_OK)
  331. {
  332. /* Process Unlocked */
  333. __HAL_UNLOCK(&pFlash);
  334. return status;
  335. }
  336. }
  337. /* Program BOOT Bit1 config option byte */
  338. if ((pOBInit->OptionType & OPTIONBYTE_BOOT_BIT1) == OPTIONBYTE_BOOT_BIT1)
  339. {
  340. status = FLASH_OB_BOOTBit1Config(pOBInit->BOOTBit1Config);
  341. }
  342. /* Process Unlocked */
  343. __HAL_UNLOCK(&pFlash);
  344. return status;
  345. }
  346. /**
  347. * @brief Get the Option byte configuration
  348. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  349. * contains the configuration information for the programming.
  350. *
  351. * @retval None
  352. */
  353. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  354. {
  355. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  356. /* Get WRP sector */
  357. pOBInit->WRPSector = FLASH_OB_GetWRP();
  358. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  359. pOBInit->WRPSector2 = FLASH_OB_GetWRP2();
  360. #endif
  361. /*Get RDP Level*/
  362. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  363. /*Get USER*/
  364. pOBInit->USERConfig = FLASH_OB_GetUser();
  365. /*Get BOR Level*/
  366. pOBInit->BORLevel = FLASH_OB_GetBOR();
  367. /* Get BOOT bit 1 config OB */
  368. pOBInit->BOOTBit1Config = FLASH_OB_GetBOOTBit1();
  369. }
  370. #if defined(FLASH_OPTR_WPRMOD) || defined(FLASH_OPTR_BFB2)
  371. /**
  372. * @brief Program option bytes
  373. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  374. * contains the configuration information for the programming.
  375. *
  376. * @retval HAL_StatusTypeDef HAL Status
  377. */
  378. HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
  379. {
  380. HAL_StatusTypeDef status = HAL_ERROR;
  381. /* Check the parameters */
  382. assert_param(IS_OBEX(pAdvOBInit->OptionType));
  383. #if defined(FLASH_OPTR_WPRMOD)
  384. /* Program PCROP option byte*/
  385. if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
  386. {
  387. /* Check the parameters */
  388. assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
  389. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  390. status = FLASH_OB_ProtectedSectorsConfig(pAdvOBInit->PCROPSector, pAdvOBInit->PCROPSector2, pAdvOBInit->PCROPState);
  391. #else
  392. status = FLASH_OB_ProtectedSectorsConfig(pAdvOBInit->PCROPSector, pAdvOBInit->PCROPState);
  393. #endif
  394. }
  395. #endif /* FLASH_OPTR_WPRMOD */
  396. #if defined(FLASH_OPTR_BFB2)
  397. /* Program BOOT config option byte */
  398. if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
  399. {
  400. status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
  401. }
  402. #endif /* FLASH_OPTR_BFB2 */
  403. return status;
  404. }
  405. /**
  406. * @brief Get the OBEX byte configuration
  407. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  408. * contains the configuration information for the programming.
  409. *
  410. * @retval None
  411. */
  412. void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
  413. {
  414. pAdvOBInit->OptionType = 0;
  415. #if defined(FLASH_OPTR_WPRMOD)
  416. pAdvOBInit->OptionType |= OPTIONBYTE_PCROP;
  417. /* Get PCROP state */
  418. pAdvOBInit->PCROPState = (FLASH->OPTR & FLASH_OPTR_WPRMOD) >> FLASH_OPTR_WPRMOD_Pos;
  419. /* Get PCROP protected sector */
  420. pAdvOBInit->PCROPSector = FLASH->WRPR;
  421. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  422. /* Get PCROP protected sector */
  423. pAdvOBInit->PCROPSector2 = FLASH->WRPR2;
  424. #endif
  425. #endif /* FLASH_OPTR_WPRMOD */
  426. #if defined(FLASH_OPTR_BFB2)
  427. pAdvOBInit->OptionType |= OPTIONBYTE_BOOTCONFIG;
  428. /* Get Boot config OB */
  429. pAdvOBInit->BootConfig = (FLASH->OPTR & FLASH_OPTR_BFB2) >> 16U;
  430. #endif /* FLASH_OPTR_BFB2 */
  431. }
  432. #endif /* FLASH_OPTR_WPRMOD || FLASH_OPTR_BFB2 */
  433. #if defined(FLASH_OPTR_WPRMOD)
  434. /**
  435. * @brief Select the Protection Mode (WPRMOD).
  436. * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
  437. * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
  438. * @retval HAL status
  439. */
  440. HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
  441. {
  442. HAL_StatusTypeDef status = HAL_OK;
  443. uint16_t tmp1 = 0;
  444. uint32_t tmp2 = 0;
  445. uint8_t optiontmp = 0;
  446. uint16_t optiontmp2 = 0;
  447. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  448. /* Mask RDP Byte */
  449. optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
  450. /* Update Option Byte */
  451. optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
  452. /* calculate the option byte to write */
  453. tmp1 = (uint16_t)(~(optiontmp2 ));
  454. tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
  455. if(status == HAL_OK)
  456. {
  457. /* Clean the error context */
  458. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  459. /* program PCRop */
  460. OB->RDP = tmp2;
  461. /* Wait for last operation to be completed */
  462. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  463. }
  464. /* Return the Read protection operation Status */
  465. return status;
  466. }
  467. /**
  468. * @brief Deselect the Protection Mode (WPRMOD).
  469. * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
  470. * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
  471. * @retval HAL status
  472. */
  473. HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
  474. {
  475. HAL_StatusTypeDef status = HAL_OK;
  476. uint16_t tmp1 = 0;
  477. uint32_t tmp2 = 0;
  478. uint8_t optiontmp = 0;
  479. uint16_t optiontmp2 = 0;
  480. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  481. /* Mask RDP Byte */
  482. optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
  483. /* Update Option Byte */
  484. optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
  485. /* calculate the option byte to write */
  486. tmp1 = (uint16_t)(~(optiontmp2 ));
  487. tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
  488. if(status == HAL_OK)
  489. {
  490. /* Clean the error context */
  491. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  492. /* program PCRop */
  493. OB->RDP = tmp2;
  494. /* Wait for last operation to be completed */
  495. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  496. }
  497. /* Return the Read protection operation Status */
  498. return status;
  499. }
  500. #endif /* FLASH_OPTR_WPRMOD */
  501. /**
  502. * @}
  503. */
  504. /** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
  505. * @brief DATA EEPROM Programming functions
  506. *
  507. @verbatim
  508. ===============================================================================
  509. ##### DATA EEPROM Programming functions #####
  510. ===============================================================================
  511. [..] Any operation of erase or program should follow these steps:
  512. (#) Call the HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
  513. and Flash program erase control register access.
  514. (#) Call the desired function to erase or program data.
  515. (#) Call the HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
  516. and Flash program erase control register access(recommended
  517. to protect the DATA_EEPROM against possible unwanted operation).
  518. @endverbatim
  519. * @{
  520. */
  521. /**
  522. * @brief Unlocks the data memory and FLASH_PECR register access.
  523. * @retval HAL_StatusTypeDef HAL Status
  524. */
  525. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
  526. {
  527. uint32_t primask_bit;
  528. if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
  529. {
  530. /* Disable interrupts to avoid any interruption during unlock sequence */
  531. primask_bit = __get_PRIMASK();
  532. __disable_irq();
  533. /* Unlocking the Data memory and FLASH_PECR register access*/
  534. FLASH->PEKEYR = FLASH_PEKEY1;
  535. FLASH->PEKEYR = FLASH_PEKEY2;
  536. /* Re-enable the interrupts: restore previous priority mask */
  537. __set_PRIMASK(primask_bit);
  538. if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
  539. {
  540. return HAL_ERROR;
  541. }
  542. }
  543. return HAL_OK;
  544. }
  545. /**
  546. * @brief Locks the Data memory and FLASH_PECR register access.
  547. * @retval HAL_StatusTypeDef HAL Status
  548. */
  549. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
  550. {
  551. /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
  552. SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
  553. return HAL_OK;
  554. }
  555. /**
  556. * @brief Erase a word in data memory.
  557. * @param Address specifies the address to be erased.
  558. * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
  559. * must be called before.
  560. * Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
  561. * and Flash program erase control register access(recommended to protect
  562. * the DATA_EEPROM against possible unwanted operation).
  563. * @retval HAL_StatusTypeDef HAL Status
  564. */
  565. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address)
  566. {
  567. HAL_StatusTypeDef status = HAL_OK;
  568. /* Check the parameters */
  569. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  570. /* Wait for last operation to be completed */
  571. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  572. if(status == HAL_OK)
  573. {
  574. /* Clean the error context */
  575. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  576. /* Write 00000000h to valid address in the data memory */
  577. *(__IO uint32_t *) Address = 0x00000000U;
  578. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  579. }
  580. /* Return the erase status */
  581. return status;
  582. }
  583. /**
  584. * @brief Program word at a specified address
  585. * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
  586. * must be called before.
  587. * Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
  588. * and Flash program erase control register access(recommended to protect
  589. * the DATA_EEPROM against possible unwanted operation).
  590. * @note The function @ref HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
  591. * this function to configure the Fixed Time Programming.
  592. * @param TypeProgram Indicate the way to program at a specified address.
  593. * This parameter can be a value of @ref FLASHEx_Type_Program_Data
  594. * @param Address specifie the address to be programmed.
  595. * @param Data specifie the data to be programmed
  596. *
  597. * @retval HAL_StatusTypeDef HAL Status
  598. */
  599. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  600. {
  601. HAL_StatusTypeDef status = HAL_ERROR;
  602. /* Process Locked */
  603. __HAL_LOCK(&pFlash);
  604. /* Check the parameters */
  605. assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
  606. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  607. /* Wait for last operation to be completed */
  608. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  609. if(status == HAL_OK)
  610. {
  611. /* Clean the error context */
  612. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  613. if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
  614. {
  615. /* Program word (32-bit) at a specified address.*/
  616. *(__IO uint32_t *)Address = Data;
  617. }
  618. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
  619. {
  620. /* Program halfword (16-bit) at a specified address.*/
  621. *(__IO uint16_t *)Address = (uint16_t) Data;
  622. }
  623. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
  624. {
  625. /* Program byte (8-bit) at a specified address.*/
  626. *(__IO uint8_t *)Address = (uint8_t) Data;
  627. }
  628. else
  629. {
  630. status = HAL_ERROR;
  631. }
  632. if (status == HAL_OK)
  633. {
  634. /* Wait for last operation to be completed */
  635. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  636. }
  637. }
  638. /* Process Unlocked */
  639. __HAL_UNLOCK(&pFlash);
  640. return status;
  641. }
  642. /**
  643. * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
  644. * @retval None
  645. */
  646. void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
  647. {
  648. SET_BIT(FLASH->PECR, FLASH_PECR_FIX);
  649. }
  650. /**
  651. * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
  652. * @retval None
  653. */
  654. void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
  655. {
  656. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FIX);
  657. }
  658. /**
  659. * @}
  660. */
  661. /**
  662. * @}
  663. */
  664. /** @addtogroup FLASHEx_Private_Functions
  665. * @{
  666. */
  667. /*
  668. ==============================================================================
  669. OPTIONS BYTES
  670. ==============================================================================
  671. */
  672. /**
  673. * @brief Enables or disables the read out protection.
  674. * @note To correctly run this function, the @ref HAL_FLASH_OB_Unlock() function
  675. * must be called before.
  676. * @param OB_RDP specifies the read protection level.
  677. * This parameter can be:
  678. * @arg @ref OB_RDP_LEVEL_0 No protection
  679. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  680. * @arg @ref OB_RDP_LEVEL_2 Chip protection
  681. *
  682. * !!!Warning!!! When enabling OB_RDP_LEVEL_2 it's no more possible to go back to level 1 or 0
  683. *
  684. * @retval HAL status
  685. */
  686. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
  687. {
  688. HAL_StatusTypeDef status = HAL_OK;
  689. uint32_t tmp1, tmp2;
  690. /* Check the parameters */
  691. assert_param(IS_OB_RDP(OB_RDP));
  692. tmp1 = (uint32_t)(OB->RDP & ((~FLASH_OPTR_RDPROT) & 0x0000FFFF));
  693. /* Calculate the option byte to write */
  694. tmp1 |= (uint32_t)(OB_RDP);
  695. tmp2 = (uint32_t)(((uint32_t)((uint32_t)(~tmp1) << 16U)) | tmp1);
  696. /* Wait for last operation to be completed */
  697. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  698. if(status == HAL_OK)
  699. {
  700. /* Clean the error context */
  701. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  702. /* program read protection level */
  703. OB->RDP = tmp2;
  704. /* Wait for last operation to be completed */
  705. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  706. }
  707. /* Return the Read protection operation Status */
  708. return status;
  709. }
  710. /**
  711. * @brief Programs the FLASH brownout reset threshold level Option Byte.
  712. * @param OB_BOR Selects the brownout reset threshold level.
  713. * This parameter can be one of the following values:
  714. * @arg @ref OB_BOR_OFF BOR is disabled at power down, the reset is asserted when the VDD
  715. * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
  716. * @arg @ref OB_BOR_LEVEL1 BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
  717. * @arg @ref OB_BOR_LEVEL2 BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
  718. * @arg @ref OB_BOR_LEVEL3 BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
  719. * @arg @ref OB_BOR_LEVEL4 BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
  720. * @arg @ref OB_BOR_LEVEL5 BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
  721. * @retval HAL status
  722. */
  723. static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
  724. {
  725. HAL_StatusTypeDef status = HAL_OK;
  726. uint32_t tmp = 0, tmp1 = 0;
  727. /* Check the parameters */
  728. assert_param(IS_OB_BOR_LEVEL(OB_BOR));
  729. /* Get the User Option byte register */
  730. tmp1 = OB->USER & ((~FLASH_OPTR_BOR_LEV) >> 16U);
  731. /* Calculate the option byte to write - [0xFF | nUSER | 0x00 | USER]*/
  732. tmp = (uint32_t)~((OB_BOR | tmp1)) << 16U;
  733. tmp |= (OB_BOR | tmp1);
  734. /* Wait for last operation to be completed */
  735. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  736. if(status == HAL_OK)
  737. {
  738. /* Clean the error context */
  739. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  740. /* Write the BOR Option Byte */
  741. OB->USER = tmp;
  742. /* Wait for last operation to be completed */
  743. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  744. }
  745. /* Return the Option Byte BOR programming Status */
  746. return status;
  747. }
  748. /**
  749. * @brief Sets or resets the BOOT bit1 option bit.
  750. * @param OB_BootBit1 Set or Reset the BOOT bit1 option bit.
  751. * This parameter can be one of the following values:
  752. * @arg @ref OB_BOOT_BIT1_RESET BOOT1 option bit reset
  753. * @arg @ref OB_BOOT_BIT1_SET BOOT1 option bit set
  754. * @retval HAL status
  755. */
  756. static HAL_StatusTypeDef FLASH_OB_BOOTBit1Config(uint8_t OB_BootBit1)
  757. {
  758. HAL_StatusTypeDef status = HAL_OK;
  759. uint32_t tmp = 0, tmp1 = 0, OB_Bits = ((uint32_t) OB_BootBit1) << 15;
  760. /* Check the parameters */
  761. assert_param(IS_OB_BOOT1(OB_BootBit1));
  762. /* Get the User Option byte register */
  763. tmp1 = OB->USER & ((~FLASH_OPTR_BOOT1) >> 16U);
  764. /* Calculate the user option byte to write */
  765. tmp = (~(OB_Bits | tmp1)) << 16U;
  766. tmp |= OB_Bits | tmp1;
  767. /* Wait for last operation to be completed */
  768. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  769. if(status == HAL_OK)
  770. {
  771. /* Clean the error context */
  772. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  773. /* Program OB */
  774. OB->USER = tmp;
  775. /* Wait for last operation to be completed */
  776. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  777. }
  778. return status;
  779. }
  780. /**
  781. * @brief Returns the FLASH User Option Bytes values.
  782. * @retval The FLASH User Option Bytes.
  783. */
  784. static uint8_t FLASH_OB_GetUser(void)
  785. {
  786. /* Return the User Option Byte */
  787. return (uint8_t)((FLASH->OPTR & FLASH_OPTR_USER) >> 16U);
  788. }
  789. /**
  790. * @brief Returns the FLASH Read Protection level.
  791. * @retval FLASH RDP level
  792. * This parameter can be one of the following values:
  793. * @arg @ref OB_RDP_LEVEL_0 No protection
  794. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  795. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  796. */
  797. static uint8_t FLASH_OB_GetRDP(void)
  798. {
  799. uint8_t rdp_level = READ_BIT(FLASH->OPTR, FLASH_OPTR_RDPROT);
  800. if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
  801. {
  802. return (OB_RDP_LEVEL_1);
  803. }
  804. else
  805. {
  806. return rdp_level;
  807. }
  808. }
  809. /**
  810. * @brief Returns the FLASH BOR level.
  811. * @retval The BOR level Option Bytes.
  812. */
  813. static uint8_t FLASH_OB_GetBOR(void)
  814. {
  815. /* Return the BOR level */
  816. return (uint8_t)((FLASH->OPTR & (uint32_t)FLASH_OPTR_BOR_LEV) >> 16U);
  817. }
  818. /**
  819. * @brief Returns the FLASH BOOT bit1 value.
  820. * @retval The BOOT bit 1 value Option Bytes.
  821. */
  822. static uint8_t FLASH_OB_GetBOOTBit1(void)
  823. {
  824. /* Return the BOR level */
  825. return (FLASH->OPTR & FLASH_OPTR_BOOT1) >> FLASH_OPTR_BOOT1_Pos;
  826. }
  827. /**
  828. * @brief Returns the FLASH Write Protection Option Bytes value.
  829. * @retval The FLASH Write Protection Option Bytes value.
  830. */
  831. static uint32_t FLASH_OB_GetWRP(void)
  832. {
  833. /* Return the FLASH write protection Register value */
  834. return (uint32_t)(FLASH->WRPR);
  835. }
  836. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  837. /**
  838. * @brief Returns the FLASH Write Protection Option Bytes value.
  839. * @retval The FLASH Write Protection Option Bytes value.
  840. */
  841. static uint32_t FLASH_OB_GetWRP2(void)
  842. {
  843. /* Return the FLASH write protection Register value */
  844. return (uint32_t)(FLASH->WRPR2);
  845. }
  846. #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
  847. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  848. /**
  849. * @brief Write Option Byte of the desired pages of the Flash.
  850. * @param Sector specifies the sectors to be write protected.
  851. * @param Sector2 specifies the sectors to be write protected (only stm32l07xxx and stm32l08xxx devices)
  852. * @param NewState new state of the specified FLASH Pages Write protection.
  853. * This parameter can be:
  854. * @arg @ref OB_WRPSTATE_ENABLE
  855. * @arg @ref OB_WRPSTATE_DISABLE
  856. * @retval HAL_StatusTypeDef
  857. */
  858. static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t Sector2, uint32_t NewState)
  859. #else
  860. /**
  861. * @brief Write Option Byte of the desired pages of the Flash.
  862. * @param Sector specifies the sectors to be write protected.
  863. * @param NewState new state of the specified FLASH Pages Write protection.
  864. * This parameter can be:
  865. * @arg @ref OB_WRPSTATE_ENABLE
  866. * @arg @ref OB_WRPSTATE_DISABLE
  867. * @retval HAL_StatusTypeDef
  868. */
  869. static HAL_StatusTypeDef FLASH_OB_ProtectedSectorsConfig(uint32_t Sector, uint32_t NewState)
  870. #endif
  871. {
  872. HAL_StatusTypeDef status = HAL_OK;
  873. uint32_t WRP_Data = 0;
  874. uint32_t OB_WRP = Sector;
  875. /* Wait for last operation to be completed */
  876. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  877. if(status == HAL_OK)
  878. {
  879. /* Clean the error context */
  880. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  881. /* Update WRP only if at least 1 selected sector */
  882. if (OB_WRP != 0x00000000U)
  883. {
  884. if ((OB_WRP & WRP_MASK_LOW) != 0x00000000U)
  885. {
  886. if (NewState != OB_WRPSTATE_DISABLE)
  887. {
  888. WRP_Data = (uint16_t)(((OB_WRP & WRP_MASK_LOW) | OB->WRP01));
  889. OB->WRP01 = (uint32_t)(~(WRP_Data) << 16U) | (WRP_Data);
  890. }
  891. else
  892. {
  893. WRP_Data = (uint16_t)(~OB_WRP & (WRP_MASK_LOW & OB->WRP01));
  894. OB->WRP01 = (uint32_t)((~WRP_Data) << 16U) | (WRP_Data);
  895. }
  896. }
  897. }
  898. #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  899. /* Update WRP only if at least 1 selected sector */
  900. if (OB_WRP != 0x00000000U)
  901. {
  902. if ((OB_WRP & WRP_MASK_HIGH) != 0x00000000U)
  903. {
  904. if (NewState != OB_WRPSTATE_DISABLE)
  905. {
  906. WRP_Data = (uint16_t)((((OB_WRP & WRP_MASK_HIGH) >> 16U | OB->WRP23)));
  907. OB->WRP23 = (uint32_t)(~(WRP_Data) << 16U) | (WRP_Data);
  908. }
  909. else
  910. {
  911. WRP_Data = (uint16_t)((((~OB_WRP & WRP_MASK_HIGH) >> 16U & OB->WRP23)));
  912. OB->WRP23 = (uint32_t)((~WRP_Data) << 16U) | (WRP_Data);
  913. }
  914. }
  915. }
  916. OB_WRP = Sector2;
  917. /* Update WRP only if at least 1 selected sector */
  918. if (OB_WRP != 0x00000000U)
  919. {
  920. if ((OB_WRP & WRP_MASK_LOW) != 0x00000000U)
  921. {
  922. if (NewState != OB_WRPSTATE_DISABLE)
  923. {
  924. WRP_Data = (uint16_t)(((OB_WRP & WRP_MASK_LOW) | OB->WRP45));
  925. OB->WRP45 =(uint32_t)(~(WRP_Data) << 16U) | (WRP_Data);
  926. }
  927. else
  928. {
  929. WRP_Data = (uint16_t)(~OB_WRP & (WRP_MASK_LOW & OB->WRP45));
  930. OB->WRP45 = (uint32_t)((~WRP_Data) << 16U) | (WRP_Data);
  931. }
  932. }
  933. }
  934. #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
  935. }
  936. /* Wait for last operation to be completed */
  937. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  938. /* Return the write protection operation Status */
  939. return status;
  940. }
  941. /**
  942. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  943. * @param OB_IWDG Selects the WDG mode.
  944. * This parameter can be one of the following values:
  945. * @arg @ref OB_IWDG_SW Software WDG selected
  946. * @arg @ref OB_IWDG_HW Hardware WDG selected
  947. * @param OB_STOP Reset event when entering STOP mode.
  948. * This parameter can be one of the following values:
  949. * @arg @ref OB_STOP_NORST No reset generated when entering in STOP
  950. * @arg @ref OB_STOP_RST Reset generated when entering in STOP
  951. * @param OB_STDBY Reset event when entering Standby mode.
  952. * This parameter can be one of the following values:
  953. * @arg @ref OB_STDBY_NORST No reset generated when entering in STANDBY
  954. * @arg @ref OB_STDBY_RST Reset generated when entering in STANDBY
  955. * @retval HAL status
  956. */
  957. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  958. {
  959. HAL_StatusTypeDef status = HAL_OK;
  960. uint32_t tmp = 0, tmp1 = 0;
  961. /* Check the parameters */
  962. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  963. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  964. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  965. /* Get the User Option byte register */
  966. tmp1 = OB->USER & ((~FLASH_OPTR_USER) >> 16U);
  967. /* Calculate the user option byte to write */
  968. tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16U);
  969. tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
  970. /* Wait for last operation to be completed */
  971. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  972. if(status == HAL_OK)
  973. {
  974. /* Clean the error context */
  975. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  976. /* Write the User Option Byte */
  977. OB->USER = tmp;
  978. /* Wait for last operation to be completed */
  979. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  980. }
  981. /* Return the Option Byte program Status */
  982. return status;
  983. }
  984. #if defined(FLASH_OPTR_BFB2)
  985. /**
  986. * @brief Configures to boot from Bank1 or Bank2.
  987. * @param OB_BOOT select the FLASH Bank to boot from.
  988. * This parameter can be one of the following values:
  989. * This parameter can be one of the following values:
  990. * @arg @ref OB_BOOT_BANK1 BFB2 option bit reset
  991. * @arg @ref OB_BOOT_BANK2 BFB2 option bit set
  992. * @retval HAL status
  993. */
  994. static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
  995. {
  996. HAL_StatusTypeDef status = HAL_OK;
  997. uint32_t tmp = 0U, tmp1 = 0U;
  998. /* Check the parameters */
  999. assert_param(IS_OB_BOOT_BANK(OB_BOOT));
  1000. /* Get the User Option byte register and BOR Level*/
  1001. tmp1 = OB->USER & ((~FLASH_OPTR_BFB2) >> 16U);
  1002. /* Calculate the option byte to write */
  1003. tmp = (uint32_t)~(OB_BOOT | tmp1) << 16U;
  1004. tmp |= (OB_BOOT | tmp1);
  1005. /* Wait for last operation to be completed */
  1006. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1007. if(status == HAL_OK)
  1008. {
  1009. /* Clean the error context */
  1010. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  1011. /* Write the BOOT Option Byte */
  1012. OB->USER = tmp;
  1013. /* Wait for last operation to be completed */
  1014. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1015. }
  1016. /* Return the Option Byte program Status */
  1017. return status;
  1018. }
  1019. #endif /* FLASH_OPTR_BFB2 */
  1020. /**
  1021. * @}
  1022. */
  1023. /**
  1024. * @}
  1025. */
  1026. /** @addtogroup FLASH
  1027. * @{
  1028. */
  1029. /** @addtogroup FLASH_Private_Functions
  1030. * @{
  1031. */
  1032. /**
  1033. * @brief Erases a specified page in program memory.
  1034. * @param PageAddress The page address in program memory to be erased.
  1035. * @note A Page is erased in the Program memory only if the address to load
  1036. * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
  1037. * @retval None
  1038. */
  1039. void FLASH_PageErase(uint32_t PageAddress)
  1040. {
  1041. /* Clean the error context */
  1042. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  1043. /* Set the ERASE bit */
  1044. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  1045. /* Set PROG bit */
  1046. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  1047. /* Write 00000000h to the first word of the program page to erase */
  1048. *(__IO uint32_t *)(uint32_t)(PageAddress & ~(FLASH_PAGE_SIZE - 1)) = 0x00000000;
  1049. }
  1050. /**
  1051. * @}
  1052. */
  1053. /**
  1054. * @}
  1055. */
  1056. #endif /* HAL_FLASH_MODULE_ENABLED */
  1057. /**
  1058. * @}
  1059. */