stm32f0xx_hal_flash_ex.c 30 KB

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