stm32f0xx_hal_flash_ex.c 31 KB

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