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.
 
 
 

1275 lines
40 KiB

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