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.
 
 
 

1001 lines
31 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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 FLASH peripheral:
  9. * + Extended Initialization/de-initialization functions
  10. * + Extended I/O operation functions
  11. * + Extended Peripheral Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### Flash peripheral extended features #####
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..] This driver provides functions to configure and program the FLASH memory
  20. of all STM32F0xxx devices. It includes
  21. (++) Set/Reset the write protection
  22. (++) Program the user Option Bytes
  23. (++) Get the Read protection Level
  24. @endverbatim
  25. ******************************************************************************
  26. * @attention
  27. *
  28. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  29. *
  30. * Redistribution and use in source and binary forms, with or without modification,
  31. * are permitted provided that the following conditions are met:
  32. * 1. Redistributions of source code must retain the above copyright notice,
  33. * this list of conditions and the following disclaimer.
  34. * 2. Redistributions in binary form must reproduce the above copyright notice,
  35. * this list of conditions and the following disclaimer in the documentation
  36. * and/or other materials provided with the distribution.
  37. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  38. * may be used to endorse or promote products derived from this software
  39. * without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  48. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  50. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. ******************************************************************************
  53. */
  54. /* Includes ------------------------------------------------------------------*/
  55. #include "stm32f0xx_hal.h"
  56. /** @addtogroup STM32F0xx_HAL_Driver
  57. * @{
  58. */
  59. #ifdef HAL_FLASH_MODULE_ENABLED
  60. /** @addtogroup FLASH
  61. * @{
  62. */
  63. /** @addtogroup FLASH_Private_Variables
  64. * @{
  65. */
  66. /* Variables used for Erase pages under interruption*/
  67. extern FLASH_ProcessTypeDef pFlash;
  68. /**
  69. * @}
  70. */
  71. /**
  72. * @}
  73. */
  74. /** @defgroup FLASHEx FLASHEx
  75. * @brief FLASH HAL Extension module driver
  76. * @{
  77. */
  78. /* Private typedef -----------------------------------------------------------*/
  79. /* Private define ------------------------------------------------------------*/
  80. /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
  81. * @{
  82. */
  83. #define FLASH_POSITION_IWDGSW_BIT 8U
  84. #define FLASH_POSITION_OB_USERDATA0_BIT 16U
  85. #define FLASH_POSITION_OB_USERDATA1_BIT 24U
  86. /**
  87. * @}
  88. */
  89. /* Private macro -------------------------------------------------------------*/
  90. /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
  91. * @{
  92. */
  93. /**
  94. * @}
  95. */
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private function prototypes -----------------------------------------------*/
  98. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  99. * @{
  100. */
  101. /* Erase operations */
  102. static void FLASH_MassErase(void);
  103. void FLASH_PageErase(uint32_t PageAddress);
  104. /* Option bytes control */
  105. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage);
  106. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage);
  107. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
  108. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig);
  109. static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
  110. static uint32_t FLASH_OB_GetWRP(void);
  111. static uint32_t FLASH_OB_GetRDP(void);
  112. static uint8_t FLASH_OB_GetUser(void);
  113. /**
  114. * @}
  115. */
  116. /* Exported functions ---------------------------------------------------------*/
  117. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  118. * @{
  119. */
  120. /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
  121. * @brief FLASH Memory Erasing functions
  122. *
  123. @verbatim
  124. ==============================================================================
  125. ##### FLASH Erasing Programming functions #####
  126. ==============================================================================
  127. [..] The FLASH Memory Erasing functions, includes the following functions:
  128. (+) @ref HAL_FLASHEx_Erase: return only when erase has been done
  129. (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback
  130. is called with parameter 0xFFFFFFFF
  131. [..] Any operation of erase should follow these steps:
  132. (#) Call the @ref HAL_FLASH_Unlock() function to enable the flash control register and
  133. program memory access.
  134. (#) Call the desired function to erase page.
  135. (#) Call the @ref HAL_FLASH_Lock() to disable the flash program memory access
  136. (recommended to protect the FLASH memory against possible unwanted operation).
  137. @endverbatim
  138. * @{
  139. */
  140. /**
  141. * @brief Perform a mass erase or erase the specified FLASH memory pages
  142. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  143. * must be called before.
  144. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  145. * (recommended to protect the FLASH memory against possible unwanted operation)
  146. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  147. * contains the configuration information for the erasing.
  148. *
  149. * @param[out] PageError pointer to variable that
  150. * contains the configuration information on faulty page in case of error
  151. * (0xFFFFFFFF means that all the pages have been correctly erased)
  152. *
  153. * @retval HAL_StatusTypeDef HAL Status
  154. */
  155. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
  156. {
  157. HAL_StatusTypeDef status = HAL_ERROR;
  158. uint32_t address = 0U;
  159. /* Process Locked */
  160. __HAL_LOCK(&pFlash);
  161. /* Check the parameters */
  162. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  163. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  164. {
  165. /* Mass Erase requested for Bank1 */
  166. /* Wait for last operation to be completed */
  167. if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
  168. {
  169. /*Mass erase to be done*/
  170. FLASH_MassErase();
  171. /* Wait for last operation to be completed */
  172. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  173. /* If the erase operation is completed, disable the MER Bit */
  174. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  175. }
  176. }
  177. else
  178. {
  179. /* Page Erase is requested */
  180. /* Check the parameters */
  181. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  182. assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
  183. /* Page Erase requested on address located on bank1 */
  184. /* Wait for last operation to be completed */
  185. if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
  186. {
  187. /*Initialization of PageError variable*/
  188. *PageError = 0xFFFFFFFFU;
  189. /* Erase page by page to be done*/
  190. for(address = pEraseInit->PageAddress;
  191. address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
  192. address += FLASH_PAGE_SIZE)
  193. {
  194. FLASH_PageErase(address);
  195. /* Wait for last operation to be completed */
  196. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  197. /* If the erase operation is completed, disable the PER Bit */
  198. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  199. if (status != HAL_OK)
  200. {
  201. /* In case of error, stop erase procedure and return the faulty address */
  202. *PageError = address;
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. /* Process Unlocked */
  209. __HAL_UNLOCK(&pFlash);
  210. return status;
  211. }
  212. /**
  213. * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled
  214. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  215. * must be called before.
  216. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  217. * (recommended to protect the FLASH memory against possible unwanted operation)
  218. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  219. * contains the configuration information for the erasing.
  220. *
  221. * @retval HAL_StatusTypeDef HAL Status
  222. */
  223. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  224. {
  225. HAL_StatusTypeDef status = HAL_OK;
  226. /* Process Locked */
  227. __HAL_LOCK(&pFlash);
  228. /* If procedure already ongoing, reject the next one */
  229. if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  230. {
  231. return HAL_ERROR;
  232. }
  233. /* Check the parameters */
  234. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  235. /* Enable End of FLASH Operation and Error source interrupts */
  236. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  237. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  238. {
  239. /*Mass erase to be done*/
  240. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
  241. FLASH_MassErase();
  242. }
  243. else
  244. {
  245. /* Erase by page to be done*/
  246. /* Check the parameters */
  247. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  248. assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
  249. pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
  250. pFlash.DataRemaining = pEraseInit->NbPages;
  251. pFlash.Address = pEraseInit->PageAddress;
  252. /*Erase 1st page and wait for IT*/
  253. FLASH_PageErase(pEraseInit->PageAddress);
  254. }
  255. return status;
  256. }
  257. /**
  258. * @}
  259. */
  260. /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
  261. * @brief Option Bytes Programming functions
  262. *
  263. @verbatim
  264. ==============================================================================
  265. ##### Option Bytes Programming functions #####
  266. ==============================================================================
  267. [..]
  268. This subsection provides a set of functions allowing to control the FLASH
  269. option bytes operations.
  270. @endverbatim
  271. * @{
  272. */
  273. /**
  274. * @brief Erases the FLASH option bytes.
  275. * @note This functions erases all option bytes except the Read protection (RDP).
  276. * The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  277. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  278. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  279. * (system reset will occur)
  280. * @retval HAL status
  281. */
  282. HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
  283. {
  284. uint8_t rdptmp = OB_RDP_LEVEL_0;
  285. HAL_StatusTypeDef status = HAL_ERROR;
  286. /* Get the actual read protection Option Byte value */
  287. rdptmp = FLASH_OB_GetRDP();
  288. /* Wait for last operation to be completed */
  289. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  290. if(status == HAL_OK)
  291. {
  292. /* Clean the error context */
  293. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  294. /* If the previous operation is completed, proceed to erase the option bytes */
  295. SET_BIT(FLASH->CR, FLASH_CR_OPTER);
  296. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  297. /* Wait for last operation to be completed */
  298. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  299. /* If the erase operation is completed, disable the OPTER Bit */
  300. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
  301. if(status == HAL_OK)
  302. {
  303. /* Restore the last read protection Option Byte value */
  304. status = FLASH_OB_RDP_LevelConfig(rdptmp);
  305. }
  306. }
  307. /* Return the erase status */
  308. return status;
  309. }
  310. /**
  311. * @brief Program option bytes
  312. * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  313. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  314. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  315. * (system reset will occur)
  316. *
  317. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  318. * contains the configuration information for the programming.
  319. *
  320. * @retval HAL_StatusTypeDef HAL Status
  321. */
  322. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  323. {
  324. HAL_StatusTypeDef status = HAL_ERROR;
  325. /* Process Locked */
  326. __HAL_LOCK(&pFlash);
  327. /* Check the parameters */
  328. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  329. /* Write protection configuration */
  330. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  331. {
  332. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  333. if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  334. {
  335. /* Enable of Write protection on the selected page */
  336. status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
  337. }
  338. else
  339. {
  340. /* Disable of Write protection on the selected page */
  341. status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
  342. }
  343. if (status != HAL_OK)
  344. {
  345. /* Process Unlocked */
  346. __HAL_UNLOCK(&pFlash);
  347. return status;
  348. }
  349. }
  350. /* Read protection configuration */
  351. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  352. {
  353. status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
  354. if (status != HAL_OK)
  355. {
  356. /* Process Unlocked */
  357. __HAL_UNLOCK(&pFlash);
  358. return status;
  359. }
  360. }
  361. /* USER configuration */
  362. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  363. {
  364. status = FLASH_OB_UserConfig(pOBInit->USERConfig);
  365. if (status != HAL_OK)
  366. {
  367. /* Process Unlocked */
  368. __HAL_UNLOCK(&pFlash);
  369. return status;
  370. }
  371. }
  372. /* DATA configuration*/
  373. if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
  374. {
  375. status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
  376. if (status != HAL_OK)
  377. {
  378. /* Process Unlocked */
  379. __HAL_UNLOCK(&pFlash);
  380. return status;
  381. }
  382. }
  383. /* Process Unlocked */
  384. __HAL_UNLOCK(&pFlash);
  385. return status;
  386. }
  387. /**
  388. * @brief Get the Option byte configuration
  389. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  390. * contains the configuration information for the programming.
  391. *
  392. * @retval None
  393. */
  394. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  395. {
  396. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
  397. /*Get WRP*/
  398. pOBInit->WRPPage = FLASH_OB_GetWRP();
  399. /*Get RDP Level*/
  400. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  401. /*Get USER*/
  402. pOBInit->USERConfig = FLASH_OB_GetUser();
  403. }
  404. /**
  405. * @brief Get the Option byte user data
  406. * @param DATAAdress Address of the option byte DATA
  407. * This parameter can be one of the following values:
  408. * @arg @ref OB_DATA_ADDRESS_DATA0
  409. * @arg @ref OB_DATA_ADDRESS_DATA1
  410. * @retval Value programmed in USER data
  411. */
  412. uint32_t HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress)
  413. {
  414. uint32_t value = 0U;
  415. if (DATAAdress == OB_DATA_ADDRESS_DATA0)
  416. {
  417. /* Get value programmed in OB USER Data0 */
  418. value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA0) >> FLASH_POSITION_OB_USERDATA0_BIT;
  419. }
  420. else
  421. {
  422. /* Get value programmed in OB USER Data1 */
  423. value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA1) >> FLASH_POSITION_OB_USERDATA1_BIT;
  424. }
  425. return value;
  426. }
  427. /**
  428. * @}
  429. */
  430. /**
  431. * @}
  432. */
  433. /** @addtogroup FLASHEx_Private_Functions
  434. * @{
  435. */
  436. /**
  437. * @brief Full erase of FLASH memory Bank
  438. *
  439. * @retval None
  440. */
  441. static void FLASH_MassErase(void)
  442. {
  443. /* Clean the error context */
  444. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  445. /* Only bank1 will be erased*/
  446. SET_BIT(FLASH->CR, FLASH_CR_MER);
  447. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  448. }
  449. /**
  450. * @brief Enable the write protection of the desired pages
  451. * @note An option byte erase is done automatically in this function.
  452. * @note When the memory read protection level is selected (RDP level = 1),
  453. * it is not possible to program or erase the flash page i if
  454. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  455. *
  456. * @param WriteProtectPage specifies the page(s) to be write protected.
  457. * The value of this parameter depend on device used within the same series
  458. * @retval HAL status
  459. */
  460. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
  461. {
  462. HAL_StatusTypeDef status = HAL_OK;
  463. uint16_t WRP0_Data = 0xFFFFU;
  464. #if defined(OB_WRP1_WRP1)
  465. uint16_t WRP1_Data = 0xFFFFU;
  466. #endif /* OB_WRP1_WRP1 */
  467. #if defined(OB_WRP2_WRP2)
  468. uint16_t WRP2_Data = 0xFFFFU;
  469. #endif /* OB_WRP2_WRP2 */
  470. #if defined(OB_WRP3_WRP3)
  471. uint16_t WRP3_Data = 0xFFFFU;
  472. #endif /* OB_WRP3_WRP3 */
  473. /* Check the parameters */
  474. assert_param(IS_OB_WRP(WriteProtectPage));
  475. /* Get current write protected pages and the new pages to be protected ******/
  476. WriteProtectPage = (uint32_t)(~((~FLASH_OB_GetWRP()) | WriteProtectPage));
  477. #if defined(OB_WRP_PAGES0TO15MASK)
  478. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
  479. #elif defined(OB_WRP_PAGES0TO31MASK)
  480. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
  481. #endif /* OB_WRP_PAGES0TO31MASK */
  482. #if defined(OB_WRP_PAGES16TO31MASK)
  483. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
  484. #elif defined(OB_WRP_PAGES32TO63MASK)
  485. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
  486. #endif /* OB_WRP_PAGES32TO63MASK */
  487. #if defined(OB_WRP_PAGES32TO47MASK)
  488. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
  489. #endif /* OB_WRP_PAGES32TO47MASK */
  490. #if defined(OB_WRP_PAGES48TO63MASK)
  491. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO63MASK) >> 24U);
  492. #elif defined(OB_WRP_PAGES48TO127MASK)
  493. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
  494. #endif /* OB_WRP_PAGES48TO63MASK */
  495. /* Wait for last operation to be completed */
  496. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  497. if(status == HAL_OK)
  498. {
  499. /* Clean the error context */
  500. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  501. /* To be able to write again option byte, need to perform a option byte erase */
  502. status = HAL_FLASHEx_OBErase();
  503. if (status == HAL_OK)
  504. {
  505. /* Enable write protection */
  506. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  507. #if defined(OB_WRP0_WRP0)
  508. if(WRP0_Data != 0xFFU)
  509. {
  510. OB->WRP0 &= WRP0_Data;
  511. /* Wait for last operation to be completed */
  512. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  513. }
  514. #endif /* OB_WRP0_WRP0 */
  515. #if defined(OB_WRP1_WRP1)
  516. if((status == HAL_OK) && (WRP1_Data != 0xFFU))
  517. {
  518. OB->WRP1 &= WRP1_Data;
  519. /* Wait for last operation to be completed */
  520. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  521. }
  522. #endif /* OB_WRP1_WRP1 */
  523. #if defined(OB_WRP2_WRP2)
  524. if((status == HAL_OK) && (WRP2_Data != 0xFFU))
  525. {
  526. OB->WRP2 &= WRP2_Data;
  527. /* Wait for last operation to be completed */
  528. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  529. }
  530. #endif /* OB_WRP2_WRP2 */
  531. #if defined(OB_WRP3_WRP3)
  532. if((status == HAL_OK) && (WRP3_Data != 0xFFU))
  533. {
  534. OB->WRP3 &= WRP3_Data;
  535. /* Wait for last operation to be completed */
  536. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  537. }
  538. #endif /* OB_WRP3_WRP3 */
  539. /* if the program operation is completed, disable the OPTPG Bit */
  540. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  541. }
  542. }
  543. return status;
  544. }
  545. /**
  546. * @brief Disable the write protection of the desired pages
  547. * @note An option byte erase is done automatically in this function.
  548. * @note When the memory read protection level is selected (RDP level = 1),
  549. * it is not possible to program or erase the flash page i if
  550. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  551. *
  552. * @param WriteProtectPage specifies the page(s) to be write unprotected.
  553. * The value of this parameter depend on device used within the same series
  554. * @retval HAL status
  555. */
  556. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
  557. {
  558. HAL_StatusTypeDef status = HAL_OK;
  559. uint16_t WRP0_Data = 0xFFFFU;
  560. #if defined(OB_WRP1_WRP1)
  561. uint16_t WRP1_Data = 0xFFFFU;
  562. #endif /* OB_WRP1_WRP1 */
  563. #if defined(OB_WRP2_WRP2)
  564. uint16_t WRP2_Data = 0xFFFFU;
  565. #endif /* OB_WRP2_WRP2 */
  566. #if defined(OB_WRP3_WRP3)
  567. uint16_t WRP3_Data = 0xFFFFU;
  568. #endif /* OB_WRP3_WRP3 */
  569. /* Check the parameters */
  570. assert_param(IS_OB_WRP(WriteProtectPage));
  571. /* Get current write protected pages and the new pages to be unprotected ******/
  572. WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage);
  573. #if defined(OB_WRP_PAGES0TO15MASK)
  574. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
  575. #elif defined(OB_WRP_PAGES0TO31MASK)
  576. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
  577. #endif /* OB_WRP_PAGES0TO31MASK */
  578. #if defined(OB_WRP_PAGES16TO31MASK)
  579. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
  580. #elif defined(OB_WRP_PAGES32TO63MASK)
  581. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
  582. #endif /* OB_WRP_PAGES32TO63MASK */
  583. #if defined(OB_WRP_PAGES32TO47MASK)
  584. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
  585. #endif /* OB_WRP_PAGES32TO47MASK */
  586. #if defined(OB_WRP_PAGES48TO63MASK)
  587. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO63MASK) >> 24U);
  588. #elif defined(OB_WRP_PAGES48TO127MASK)
  589. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
  590. #endif /* OB_WRP_PAGES48TO63MASK */
  591. /* Wait for last operation to be completed */
  592. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  593. if(status == HAL_OK)
  594. {
  595. /* Clean the error context */
  596. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  597. /* To be able to write again option byte, need to perform a option byte erase */
  598. status = HAL_FLASHEx_OBErase();
  599. if (status == HAL_OK)
  600. {
  601. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  602. #if defined(OB_WRP0_WRP0)
  603. if(WRP0_Data != 0xFFU)
  604. {
  605. OB->WRP0 |= WRP0_Data;
  606. /* Wait for last operation to be completed */
  607. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  608. }
  609. #endif /* OB_WRP0_WRP0 */
  610. #if defined(OB_WRP1_WRP1)
  611. if((status == HAL_OK) && (WRP1_Data != 0xFFU))
  612. {
  613. OB->WRP1 |= WRP1_Data;
  614. /* Wait for last operation to be completed */
  615. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  616. }
  617. #endif /* OB_WRP1_WRP1 */
  618. #if defined(OB_WRP2_WRP2)
  619. if((status == HAL_OK) && (WRP2_Data != 0xFFU))
  620. {
  621. OB->WRP2 |= WRP2_Data;
  622. /* Wait for last operation to be completed */
  623. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  624. }
  625. #endif /* OB_WRP2_WRP2 */
  626. #if defined(OB_WRP3_WRP3)
  627. if((status == HAL_OK) && (WRP3_Data != 0xFFU))
  628. {
  629. OB->WRP3 |= WRP3_Data;
  630. /* Wait for last operation to be completed */
  631. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  632. }
  633. #endif /* OB_WRP3_WRP3 */
  634. /* if the program operation is completed, disable the OPTPG Bit */
  635. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  636. }
  637. }
  638. return status;
  639. }
  640. /**
  641. * @brief Set the read protection level.
  642. * @param ReadProtectLevel specifies the read protection level.
  643. * This parameter can be one of the following values:
  644. * @arg @ref OB_RDP_LEVEL_0 No protection
  645. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  646. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  647. * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  648. * @retval HAL status
  649. */
  650. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
  651. {
  652. HAL_StatusTypeDef status = HAL_OK;
  653. /* Check the parameters */
  654. assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
  655. /* Wait for last operation to be completed */
  656. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  657. if(status == HAL_OK)
  658. {
  659. /* Clean the error context */
  660. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  661. /* If the previous operation is completed, proceed to erase the option bytes */
  662. SET_BIT(FLASH->CR, FLASH_CR_OPTER);
  663. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  664. /* Wait for last operation to be completed */
  665. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  666. /* If the erase operation is completed, disable the OPTER Bit */
  667. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
  668. if(status == HAL_OK)
  669. {
  670. /* Enable the Option Bytes Programming operation */
  671. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  672. WRITE_REG(OB->RDP, ReadProtectLevel);
  673. /* Wait for last operation to be completed */
  674. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  675. /* if the program operation is completed, disable the OPTPG Bit */
  676. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  677. }
  678. }
  679. return status;
  680. }
  681. /**
  682. * @brief Program the FLASH User Option Byte.
  683. * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
  684. * @param UserConfig The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), nBOOT1(Bit4),
  685. * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
  686. * For few devices, following option bytes are available: nBOOT0(Bit3) & BOOT_SEL(Bit7).
  687. * @retval HAL status
  688. */
  689. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
  690. {
  691. HAL_StatusTypeDef status = HAL_OK;
  692. /* Check the parameters */
  693. assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
  694. assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
  695. assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
  696. assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
  697. assert_param(IS_OB_VDDA_ANALOG((UserConfig&OB_VDDA_ANALOG_ON)));
  698. assert_param(IS_OB_SRAM_PARITY((UserConfig&OB_SRAM_PARITY_RESET)));
  699. #if defined(FLASH_OBR_BOOT_SEL)
  700. assert_param(IS_OB_BOOT_SEL((UserConfig&OB_BOOT_SEL_SET)));
  701. assert_param(IS_OB_BOOT0((UserConfig&OB_BOOT0_SET)));
  702. #endif /* FLASH_OBR_BOOT_SEL */
  703. /* Wait for last operation to be completed */
  704. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  705. if(status == HAL_OK)
  706. {
  707. /* Clean the error context */
  708. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  709. /* Enable the Option Bytes Programming operation */
  710. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  711. #if defined(FLASH_OBR_BOOT_SEL)
  712. OB->USER = UserConfig;
  713. #else
  714. OB->USER = (UserConfig | 0x88U);
  715. #endif
  716. /* Wait for last operation to be completed */
  717. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  718. /* if the program operation is completed, disable the OPTPG Bit */
  719. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  720. }
  721. return status;
  722. }
  723. /**
  724. * @brief Programs a half word at a specified Option Byte Data address.
  725. * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  726. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  727. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  728. * (system reset will occur)
  729. * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
  730. * @param Address specifies the address to be programmed.
  731. * This parameter can be 0x1FFFF804 or 0x1FFFF806.
  732. * @param Data specifies the data to be programmed.
  733. * @retval HAL status
  734. */
  735. static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
  736. {
  737. HAL_StatusTypeDef status = HAL_ERROR;
  738. /* Check the parameters */
  739. assert_param(IS_OB_DATA_ADDRESS(Address));
  740. /* Wait for last operation to be completed */
  741. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  742. if(status == HAL_OK)
  743. {
  744. /* Clean the error context */
  745. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  746. /* Enables the Option Bytes Programming operation */
  747. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  748. *(__IO uint16_t*)Address = Data;
  749. /* Wait for last operation to be completed */
  750. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  751. /* If the program operation is completed, disable the OPTPG Bit */
  752. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  753. }
  754. /* Return the Option Byte Data Program Status */
  755. return status;
  756. }
  757. /**
  758. * @brief Return the FLASH Write Protection Option Bytes value.
  759. * @retval The FLASH Write Protection Option Bytes value
  760. */
  761. static uint32_t FLASH_OB_GetWRP(void)
  762. {
  763. /* Return the FLASH write protection Register value */
  764. return (uint32_t)(READ_REG(FLASH->WRPR));
  765. }
  766. /**
  767. * @brief Returns the FLASH Read Protection level.
  768. * @retval FLASH RDP level
  769. * This parameter can be one of the following values:
  770. * @arg @ref OB_RDP_LEVEL_0 No protection
  771. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  772. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  773. */
  774. static uint32_t FLASH_OB_GetRDP(void)
  775. {
  776. uint32_t tmp_reg = 0U;
  777. /* Read RDP level bits */
  778. tmp_reg = READ_BIT(FLASH->OBR, (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2));
  779. if (tmp_reg == FLASH_OBR_RDPRT1)
  780. {
  781. return OB_RDP_LEVEL_1;
  782. }
  783. else if (tmp_reg == FLASH_OBR_RDPRT2)
  784. {
  785. return OB_RDP_LEVEL_2;
  786. }
  787. else
  788. {
  789. return OB_RDP_LEVEL_0;
  790. }
  791. }
  792. /**
  793. * @brief Return the FLASH User Option Byte value.
  794. * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), nBOOT1(Bit4),
  795. * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
  796. * For few devices, following option bytes are available: nBOOT0(Bit3) & BOOT_SEL(Bit7).
  797. */
  798. static uint8_t FLASH_OB_GetUser(void)
  799. {
  800. /* Return the User Option Byte */
  801. return (uint8_t)((READ_REG(FLASH->OBR) & FLASH_OBR_USER) >> FLASH_POSITION_IWDGSW_BIT);
  802. }
  803. /**
  804. * @}
  805. */
  806. /**
  807. * @}
  808. */
  809. /** @addtogroup FLASH
  810. * @{
  811. */
  812. /** @addtogroup FLASH_Private_Functions
  813. * @{
  814. */
  815. /**
  816. * @brief Erase the specified FLASH memory page
  817. * @param PageAddress FLASH page to erase
  818. * The value of this parameter depend on device used within the same series
  819. *
  820. * @retval None
  821. */
  822. void FLASH_PageErase(uint32_t PageAddress)
  823. {
  824. /* Clean the error context */
  825. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  826. /* Proceed to erase the page */
  827. SET_BIT(FLASH->CR, FLASH_CR_PER);
  828. WRITE_REG(FLASH->AR, PageAddress);
  829. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  830. }
  831. /**
  832. * @}
  833. */
  834. /**
  835. * @}
  836. */
  837. #endif /* HAL_FLASH_MODULE_ENABLED */
  838. /**
  839. * @}
  840. */
  841. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/