stm32f3xx_hal_flash_ex.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_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 STM32F3xxx 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 "stm32f3xx_hal.h"
  38. /** @addtogroup STM32F3xx_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 (uint32_t)POSITION_VAL(FLASH_OBR_IWDG_SW)
  66. #define FLASH_POSITION_OB_USERDATA0_BIT (uint32_t)POSITION_VAL(FLASH_OBR_DATA0)
  67. #define FLASH_POSITION_OB_USERDATA1_BIT (uint32_t)POSITION_VAL(FLASH_OBR_DATA1)
  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. #endif /* OB_WRP_PAGES0TO31MASK */
  462. #if defined(OB_WRP_PAGES16TO31MASK)
  463. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
  464. #endif /* OB_WRP_PAGES32TO63MASK */
  465. #if defined(OB_WRP_PAGES32TO47MASK)
  466. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
  467. #endif /* OB_WRP_PAGES32TO47MASK */
  468. #if defined(OB_WRP_PAGES48TO127MASK)
  469. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
  470. #elif defined(OB_WRP_PAGES48TO255MASK)
  471. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U);
  472. #endif /* OB_WRP_PAGES48TO63MASK */
  473. /* Wait for last operation to be completed */
  474. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  475. if(status == HAL_OK)
  476. {
  477. /* Clean the error context */
  478. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  479. /* To be able to write again option byte, need to perform a option byte erase */
  480. status = HAL_FLASHEx_OBErase();
  481. if (status == HAL_OK)
  482. {
  483. /* Enable write protection */
  484. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  485. #if defined(OB_WRP0_WRP0)
  486. if(WRP0_Data != 0xFFU)
  487. {
  488. OB->WRP0 &= WRP0_Data;
  489. /* Wait for last operation to be completed */
  490. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  491. }
  492. #endif /* OB_WRP0_WRP0 */
  493. #if defined(OB_WRP1_WRP1)
  494. if((status == HAL_OK) && (WRP1_Data != 0xFFU))
  495. {
  496. OB->WRP1 &= WRP1_Data;
  497. /* Wait for last operation to be completed */
  498. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  499. }
  500. #endif /* OB_WRP1_WRP1 */
  501. #if defined(OB_WRP2_WRP2)
  502. if((status == HAL_OK) && (WRP2_Data != 0xFFU))
  503. {
  504. OB->WRP2 &= WRP2_Data;
  505. /* Wait for last operation to be completed */
  506. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  507. }
  508. #endif /* OB_WRP2_WRP2 */
  509. #if defined(OB_WRP3_WRP3)
  510. if((status == HAL_OK) && (WRP3_Data != 0xFFU))
  511. {
  512. OB->WRP3 &= WRP3_Data;
  513. /* Wait for last operation to be completed */
  514. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  515. }
  516. #endif /* OB_WRP3_WRP3 */
  517. /* if the program operation is completed, disable the OPTPG Bit */
  518. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  519. }
  520. }
  521. return status;
  522. }
  523. /**
  524. * @brief Disable the write protection of the desired pages
  525. * @note An option byte erase is done automatically in this function.
  526. * @note When the memory read protection level is selected (RDP level = 1),
  527. * it is not possible to program or erase the flash page i if
  528. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  529. *
  530. * @param WriteProtectPage specifies the page(s) to be write unprotected.
  531. * The value of this parameter depend on device used within the same series
  532. * @retval HAL status
  533. */
  534. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
  535. {
  536. HAL_StatusTypeDef status = HAL_OK;
  537. uint16_t WRP0_Data = 0xFFFFU;
  538. #if defined(OB_WRP1_WRP1)
  539. uint16_t WRP1_Data = 0xFFFFU;
  540. #endif /* OB_WRP1_WRP1 */
  541. #if defined(OB_WRP2_WRP2)
  542. uint16_t WRP2_Data = 0xFFFFU;
  543. #endif /* OB_WRP2_WRP2 */
  544. #if defined(OB_WRP3_WRP3)
  545. uint16_t WRP3_Data = 0xFFFFU;
  546. #endif /* OB_WRP3_WRP3 */
  547. /* Check the parameters */
  548. assert_param(IS_OB_WRP(WriteProtectPage));
  549. /* Get current write protected pages and the new pages to be unprotected ******/
  550. WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage);
  551. #if defined(OB_WRP_PAGES0TO15MASK)
  552. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
  553. #endif /* OB_WRP_PAGES0TO31MASK */
  554. #if defined(OB_WRP_PAGES16TO31MASK)
  555. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
  556. #endif /* OB_WRP_PAGES32TO63MASK */
  557. #if defined(OB_WRP_PAGES32TO47MASK)
  558. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
  559. #endif /* OB_WRP_PAGES32TO47MASK */
  560. #if defined(OB_WRP_PAGES48TO127MASK)
  561. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
  562. #elif defined(OB_WRP_PAGES48TO255MASK)
  563. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U);
  564. #endif /* OB_WRP_PAGES48TO63MASK */
  565. /* Wait for last operation to be completed */
  566. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  567. if(status == HAL_OK)
  568. {
  569. /* Clean the error context */
  570. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  571. /* To be able to write again option byte, need to perform a option byte erase */
  572. status = HAL_FLASHEx_OBErase();
  573. if (status == HAL_OK)
  574. {
  575. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  576. #if defined(OB_WRP0_WRP0)
  577. if(WRP0_Data != 0xFFU)
  578. {
  579. OB->WRP0 = WRP0_Data;
  580. /* Wait for last operation to be completed */
  581. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  582. }
  583. #endif /* OB_WRP0_WRP0 */
  584. #if defined(OB_WRP1_WRP1)
  585. if((status == HAL_OK) && (WRP1_Data != 0xFFU))
  586. {
  587. OB->WRP1 = WRP1_Data;
  588. /* Wait for last operation to be completed */
  589. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  590. }
  591. #endif /* OB_WRP1_WRP1 */
  592. #if defined(OB_WRP2_WRP2)
  593. if((status == HAL_OK) && (WRP2_Data != 0xFFU))
  594. {
  595. OB->WRP2 = WRP2_Data;
  596. /* Wait for last operation to be completed */
  597. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  598. }
  599. #endif /* OB_WRP2_WRP2 */
  600. #if defined(OB_WRP3_WRP3)
  601. if((status == HAL_OK) && (WRP3_Data != 0xFFU))
  602. {
  603. OB->WRP3 = WRP3_Data;
  604. /* Wait for last operation to be completed */
  605. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  606. }
  607. #endif /* OB_WRP3_WRP3 */
  608. /* if the program operation is completed, disable the OPTPG Bit */
  609. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  610. }
  611. }
  612. return status;
  613. }
  614. /**
  615. * @brief Set the read protection level.
  616. * @param ReadProtectLevel specifies the read protection level.
  617. * This parameter can be one of the following values:
  618. * @arg @ref OB_RDP_LEVEL_0 No protection
  619. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  620. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  621. * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  622. * @retval HAL status
  623. */
  624. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
  625. {
  626. HAL_StatusTypeDef status = HAL_OK;
  627. /* Check the parameters */
  628. assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
  629. /* Wait for last operation to be completed */
  630. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  631. if(status == HAL_OK)
  632. {
  633. /* Clean the error context */
  634. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  635. /* If the previous operation is completed, proceed to erase the option bytes */
  636. SET_BIT(FLASH->CR, FLASH_CR_OPTER);
  637. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  638. /* Wait for last operation to be completed */
  639. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  640. /* If the erase operation is completed, disable the OPTER Bit */
  641. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
  642. if(status == HAL_OK)
  643. {
  644. /* Enable the Option Bytes Programming operation */
  645. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  646. WRITE_REG(OB->RDP, ReadProtectLevel);
  647. /* Wait for last operation to be completed */
  648. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  649. /* if the program operation is completed, disable the OPTPG Bit */
  650. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  651. }
  652. }
  653. return status;
  654. }
  655. /**
  656. * @brief Program the FLASH User Option Byte.
  657. * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
  658. * @param UserConfig The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), nBOOT1(Bit4),
  659. * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
  660. * And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 .
  661. * @retval HAL status
  662. */
  663. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
  664. {
  665. HAL_StatusTypeDef status = HAL_OK;
  666. /* Check the parameters */
  667. assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
  668. assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
  669. assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
  670. assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
  671. assert_param(IS_OB_VDDA_ANALOG((UserConfig&OB_VDDA_ANALOG_ON)));
  672. assert_param(IS_OB_SRAM_PARITY((UserConfig&OB_SRAM_PARITY_RESET)));
  673. #if defined(FLASH_OBR_SDADC12_VDD_MONITOR)
  674. assert_param(IS_OB_SDACD_VDD_MONITOR((UserConfig&OB_SDACD_VDD_MONITOR_SET)));
  675. #endif /* FLASH_OBR_SDADC12_VDD_MONITOR */
  676. /* Wait for last operation to be completed */
  677. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  678. if(status == HAL_OK)
  679. {
  680. /* Clean the error context */
  681. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  682. /* Enable the Option Bytes Programming operation */
  683. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  684. #if defined(FLASH_OBR_SDADC12_VDD_MONITOR)
  685. OB->USER = (UserConfig | 0x08U);
  686. #else
  687. OB->USER = (UserConfig | 0x88U);
  688. #endif
  689. /* Wait for last operation to be completed */
  690. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  691. /* if the program operation is completed, disable the OPTPG Bit */
  692. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  693. }
  694. return status;
  695. }
  696. /**
  697. * @brief Programs a half word at a specified Option Byte Data address.
  698. * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  699. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  700. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  701. * (system reset will occur)
  702. * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
  703. * @param Address specifies the address to be programmed.
  704. * This parameter can be 0x1FFFF804 or 0x1FFFF806.
  705. * @param Data specifies the data to be programmed.
  706. * @retval HAL status
  707. */
  708. static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
  709. {
  710. HAL_StatusTypeDef status = HAL_ERROR;
  711. /* Check the parameters */
  712. assert_param(IS_OB_DATA_ADDRESS(Address));
  713. /* Wait for last operation to be completed */
  714. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  715. if(status == HAL_OK)
  716. {
  717. /* Clean the error context */
  718. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  719. /* Enables the Option Bytes Programming operation */
  720. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  721. *(__IO uint16_t*)Address = Data;
  722. /* Wait for last operation to be completed */
  723. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  724. /* If the program operation is completed, disable the OPTPG Bit */
  725. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  726. }
  727. /* Return the Option Byte Data Program Status */
  728. return status;
  729. }
  730. /**
  731. * @brief Return the FLASH Write Protection Option Bytes value.
  732. * @retval The FLASH Write Protection Option Bytes value
  733. */
  734. static uint32_t FLASH_OB_GetWRP(void)
  735. {
  736. /* Return the FLASH write protection Register value */
  737. return (uint32_t)(READ_REG(FLASH->WRPR));
  738. }
  739. /**
  740. * @brief Returns the FLASH Read Protection level.
  741. * @retval FLASH RDP level
  742. * This parameter can be one of the following values:
  743. * @arg @ref OB_RDP_LEVEL_0 No protection
  744. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  745. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  746. */
  747. static uint32_t FLASH_OB_GetRDP(void)
  748. {
  749. uint32_t tmp_reg = 0U;
  750. /* Read RDP level bits */
  751. #if defined(FLASH_OBR_RDPRT)
  752. tmp_reg = READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT);
  753. #elif defined(FLASH_OBR_LEVEL1_PROT)
  754. tmp_reg = READ_BIT(FLASH->OBR, (FLASH_OBR_LEVEL1_PROT | FLASH_OBR_LEVEL2_PROT));
  755. #endif /* FLASH_OBR_RDPRT */
  756. #if defined(FLASH_OBR_RDPRT)
  757. if (tmp_reg == FLASH_OBR_RDPRT_2)
  758. #elif defined(FLASH_OBR_LEVEL1_PROT)
  759. if (tmp_reg == FLASH_OBR_LEVEL2_PROT)
  760. #endif /* FLASH_OBR_RDPRT */
  761. {
  762. return OB_RDP_LEVEL_2;
  763. }
  764. else if (tmp_reg == 0U)
  765. {
  766. return OB_RDP_LEVEL_0;
  767. }
  768. else
  769. {
  770. return OB_RDP_LEVEL_1;
  771. }
  772. }
  773. /**
  774. * @brief Return the FLASH User Option Byte value.
  775. * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), nBOOT1(Bit4),
  776. * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
  777. * And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 .
  778. */
  779. static uint8_t FLASH_OB_GetUser(void)
  780. {
  781. /* Return the User Option Byte */
  782. return (uint8_t)((READ_REG(FLASH->OBR) & FLASH_OBR_USER) >> FLASH_POSITION_IWDGSW_BIT);
  783. }
  784. /**
  785. * @}
  786. */
  787. /**
  788. * @}
  789. */
  790. /** @addtogroup FLASH
  791. * @{
  792. */
  793. /** @addtogroup FLASH_Private_Functions
  794. * @{
  795. */
  796. /**
  797. * @brief Erase the specified FLASH memory page
  798. * @param PageAddress FLASH page to erase
  799. * The value of this parameter depend on device used within the same series
  800. *
  801. * @retval None
  802. */
  803. void FLASH_PageErase(uint32_t PageAddress)
  804. {
  805. /* Clean the error context */
  806. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  807. /* Proceed to erase the page */
  808. SET_BIT(FLASH->CR, FLASH_CR_PER);
  809. WRITE_REG(FLASH->AR, PageAddress);
  810. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  811. }
  812. /**
  813. * @}
  814. */
  815. /**
  816. * @}
  817. */
  818. #endif /* HAL_FLASH_MODULE_ENABLED */
  819. /**
  820. * @}
  821. */