stm32f1xx_hal_flash_ex.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 STM32F1xxx 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 "stm32f1xx_hal.h"
  40. /** @addtogroup STM32F1xx_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 FLASH_OBR_IWDG_SW_Pos
  68. #define FLASH_POSITION_OB_USERDATA0_BIT FLASH_OBR_DATA0_Pos
  69. #define FLASH_POSITION_OB_USERDATA1_BIT FLASH_OBR_DATA1_Pos
  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(uint32_t Banks);
  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. (+) @ref HAL_FLASHEx_Erase: return only when erase has been done
  113. (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback
  114. is called with parameter 0xFFFFFFFF
  115. [..] Any operation of erase should follow these steps:
  116. (#) Call the @ref 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 @ref 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. #if defined(FLASH_BANK2_END)
  150. if (pEraseInit->Banks == FLASH_BANK_BOTH)
  151. {
  152. /* Mass Erase requested for Bank1 and Bank2 */
  153. /* Wait for last operation to be completed */
  154. if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \
  155. (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK))
  156. {
  157. /*Mass erase to be done*/
  158. FLASH_MassErase(FLASH_BANK_BOTH);
  159. /* Wait for last operation to be completed */
  160. if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \
  161. (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK))
  162. {
  163. status = HAL_OK;
  164. }
  165. /* If the erase operation is completed, disable the MER Bit */
  166. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  167. CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
  168. }
  169. }
  170. else if (pEraseInit->Banks == FLASH_BANK_2)
  171. {
  172. /* Mass Erase requested for Bank2 */
  173. /* Wait for last operation to be completed */
  174. if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
  175. {
  176. /*Mass erase to be done*/
  177. FLASH_MassErase(FLASH_BANK_2);
  178. /* Wait for last operation to be completed */
  179. status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE);
  180. /* If the erase operation is completed, disable the MER Bit */
  181. CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
  182. }
  183. }
  184. else
  185. #endif /* FLASH_BANK2_END */
  186. {
  187. /* Mass Erase requested for Bank1 */
  188. /* Wait for last operation to be completed */
  189. if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
  190. {
  191. /*Mass erase to be done*/
  192. FLASH_MassErase(FLASH_BANK_1);
  193. /* Wait for last operation to be completed */
  194. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  195. /* If the erase operation is completed, disable the MER Bit */
  196. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  197. }
  198. }
  199. }
  200. else
  201. {
  202. /* Page Erase is requested */
  203. /* Check the parameters */
  204. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  205. assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
  206. #if defined(FLASH_BANK2_END)
  207. /* Page Erase requested on address located on bank2 */
  208. if(pEraseInit->PageAddress > FLASH_BANK1_END)
  209. {
  210. /* Wait for last operation to be completed */
  211. if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
  212. {
  213. /*Initialization of PageError variable*/
  214. *PageError = 0xFFFFFFFFU;
  215. /* Erase by page by page to be done*/
  216. for(address = pEraseInit->PageAddress;
  217. address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE);
  218. address += FLASH_PAGE_SIZE)
  219. {
  220. FLASH_PageErase(address);
  221. /* Wait for last operation to be completed */
  222. status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE);
  223. /* If the erase operation is completed, disable the PER Bit */
  224. CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER);
  225. if (status != HAL_OK)
  226. {
  227. /* In case of error, stop erase procedure and return the faulty address */
  228. *PageError = address;
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. else
  235. #endif /* FLASH_BANK2_END */
  236. {
  237. /* Page Erase requested on address located on bank1 */
  238. /* Wait for last operation to be completed */
  239. if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
  240. {
  241. /*Initialization of PageError variable*/
  242. *PageError = 0xFFFFFFFFU;
  243. /* Erase page by page to be done*/
  244. for(address = pEraseInit->PageAddress;
  245. address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
  246. address += FLASH_PAGE_SIZE)
  247. {
  248. FLASH_PageErase(address);
  249. /* Wait for last operation to be completed */
  250. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  251. /* If the erase operation is completed, disable the PER Bit */
  252. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  253. if (status != HAL_OK)
  254. {
  255. /* In case of error, stop erase procedure and return the faulty address */
  256. *PageError = address;
  257. break;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. /* Process Unlocked */
  264. __HAL_UNLOCK(&pFlash);
  265. return status;
  266. }
  267. /**
  268. * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled
  269. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  270. * must be called before.
  271. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  272. * (recommended to protect the FLASH memory against possible unwanted operation)
  273. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  274. * contains the configuration information for the erasing.
  275. *
  276. * @retval HAL_StatusTypeDef HAL Status
  277. */
  278. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  279. {
  280. HAL_StatusTypeDef status = HAL_OK;
  281. /* Process Locked */
  282. __HAL_LOCK(&pFlash);
  283. /* If procedure already ongoing, reject the next one */
  284. if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  285. {
  286. return HAL_ERROR;
  287. }
  288. /* Check the parameters */
  289. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  290. /* Enable End of FLASH Operation and Error source interrupts */
  291. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  292. #if defined(FLASH_BANK2_END)
  293. /* Enable End of FLASH Operation and Error source interrupts */
  294. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
  295. #endif
  296. if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  297. {
  298. /*Mass erase to be done*/
  299. pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
  300. FLASH_MassErase(pEraseInit->Banks);
  301. }
  302. else
  303. {
  304. /* Erase by page to be done*/
  305. /* Check the parameters */
  306. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  307. assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
  308. pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
  309. pFlash.DataRemaining = pEraseInit->NbPages;
  310. pFlash.Address = pEraseInit->PageAddress;
  311. /*Erase 1st page and wait for IT*/
  312. FLASH_PageErase(pEraseInit->PageAddress);
  313. }
  314. return status;
  315. }
  316. /**
  317. * @}
  318. */
  319. /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
  320. * @brief Option Bytes Programming functions
  321. *
  322. @verbatim
  323. ==============================================================================
  324. ##### Option Bytes Programming functions #####
  325. ==============================================================================
  326. [..]
  327. This subsection provides a set of functions allowing to control the FLASH
  328. option bytes operations.
  329. @endverbatim
  330. * @{
  331. */
  332. /**
  333. * @brief Erases the FLASH option bytes.
  334. * @note This functions erases all option bytes except the Read protection (RDP).
  335. * The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  336. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  337. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  338. * (system reset will occur)
  339. * @retval HAL status
  340. */
  341. HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
  342. {
  343. uint8_t rdptmp = OB_RDP_LEVEL_0;
  344. HAL_StatusTypeDef status = HAL_ERROR;
  345. /* Get the actual read protection Option Byte value */
  346. rdptmp = FLASH_OB_GetRDP();
  347. /* Wait for last operation to be completed */
  348. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  349. if(status == HAL_OK)
  350. {
  351. /* Clean the error context */
  352. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  353. /* If the previous operation is completed, proceed to erase the option bytes */
  354. SET_BIT(FLASH->CR, FLASH_CR_OPTER);
  355. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  356. /* Wait for last operation to be completed */
  357. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  358. /* If the erase operation is completed, disable the OPTER Bit */
  359. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
  360. if(status == HAL_OK)
  361. {
  362. /* Restore the last read protection Option Byte value */
  363. status = FLASH_OB_RDP_LevelConfig(rdptmp);
  364. }
  365. }
  366. /* Return the erase status */
  367. return status;
  368. }
  369. /**
  370. * @brief Program option bytes
  371. * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  372. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  373. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  374. * (system reset will occur)
  375. *
  376. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  377. * contains the configuration information for the programming.
  378. *
  379. * @retval HAL_StatusTypeDef HAL Status
  380. */
  381. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  382. {
  383. HAL_StatusTypeDef status = HAL_ERROR;
  384. /* Process Locked */
  385. __HAL_LOCK(&pFlash);
  386. /* Check the parameters */
  387. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  388. /* Write protection configuration */
  389. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  390. {
  391. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  392. if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  393. {
  394. /* Enable of Write protection on the selected page */
  395. status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
  396. }
  397. else
  398. {
  399. /* Disable of Write protection on the selected page */
  400. status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
  401. }
  402. if (status != HAL_OK)
  403. {
  404. /* Process Unlocked */
  405. __HAL_UNLOCK(&pFlash);
  406. return status;
  407. }
  408. }
  409. /* Read protection configuration */
  410. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  411. {
  412. status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
  413. if (status != HAL_OK)
  414. {
  415. /* Process Unlocked */
  416. __HAL_UNLOCK(&pFlash);
  417. return status;
  418. }
  419. }
  420. /* USER configuration */
  421. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  422. {
  423. status = FLASH_OB_UserConfig(pOBInit->USERConfig);
  424. if (status != HAL_OK)
  425. {
  426. /* Process Unlocked */
  427. __HAL_UNLOCK(&pFlash);
  428. return status;
  429. }
  430. }
  431. /* DATA configuration*/
  432. if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
  433. {
  434. status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
  435. if (status != HAL_OK)
  436. {
  437. /* Process Unlocked */
  438. __HAL_UNLOCK(&pFlash);
  439. return status;
  440. }
  441. }
  442. /* Process Unlocked */
  443. __HAL_UNLOCK(&pFlash);
  444. return status;
  445. }
  446. /**
  447. * @brief Get the Option byte configuration
  448. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  449. * contains the configuration information for the programming.
  450. *
  451. * @retval None
  452. */
  453. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  454. {
  455. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
  456. /*Get WRP*/
  457. pOBInit->WRPPage = FLASH_OB_GetWRP();
  458. /*Get RDP Level*/
  459. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  460. /*Get USER*/
  461. pOBInit->USERConfig = FLASH_OB_GetUser();
  462. }
  463. /**
  464. * @brief Get the Option byte user data
  465. * @param DATAAdress Address of the option byte DATA
  466. * This parameter can be one of the following values:
  467. * @arg @ref OB_DATA_ADDRESS_DATA0
  468. * @arg @ref OB_DATA_ADDRESS_DATA1
  469. * @retval Value programmed in USER data
  470. */
  471. uint32_t HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress)
  472. {
  473. uint32_t value = 0;
  474. if (DATAAdress == OB_DATA_ADDRESS_DATA0)
  475. {
  476. /* Get value programmed in OB USER Data0 */
  477. value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA0) >> FLASH_POSITION_OB_USERDATA0_BIT;
  478. }
  479. else
  480. {
  481. /* Get value programmed in OB USER Data1 */
  482. value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA1) >> FLASH_POSITION_OB_USERDATA1_BIT;
  483. }
  484. return value;
  485. }
  486. /**
  487. * @}
  488. */
  489. /**
  490. * @}
  491. */
  492. /** @addtogroup FLASHEx_Private_Functions
  493. * @{
  494. */
  495. /**
  496. * @brief Full erase of FLASH memory Bank
  497. * @param Banks Banks to be erased
  498. * This parameter can be one of the following values:
  499. * @arg @ref FLASH_BANK_1 Bank1 to be erased
  500. @if STM32F101xG
  501. * @arg @ref FLASH_BANK_2 Bank2 to be erased
  502. * @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased
  503. @endif
  504. @if STM32F103xG
  505. * @arg @ref FLASH_BANK_2 Bank2 to be erased
  506. * @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased
  507. @endif
  508. *
  509. * @retval None
  510. */
  511. static void FLASH_MassErase(uint32_t Banks)
  512. {
  513. /* Check the parameters */
  514. assert_param(IS_FLASH_BANK(Banks));
  515. /* Clean the error context */
  516. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  517. #if defined(FLASH_BANK2_END)
  518. if(Banks == FLASH_BANK_BOTH)
  519. {
  520. /* bank1 & bank2 will be erased*/
  521. SET_BIT(FLASH->CR, FLASH_CR_MER);
  522. SET_BIT(FLASH->CR2, FLASH_CR2_MER);
  523. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  524. SET_BIT(FLASH->CR2, FLASH_CR2_STRT);
  525. }
  526. else if(Banks == FLASH_BANK_2)
  527. {
  528. /*Only bank2 will be erased*/
  529. SET_BIT(FLASH->CR2, FLASH_CR2_MER);
  530. SET_BIT(FLASH->CR2, FLASH_CR2_STRT);
  531. }
  532. else
  533. {
  534. #endif /* FLASH_BANK2_END */
  535. #if !defined(FLASH_BANK2_END)
  536. /* Prevent unused argument(s) compilation warning */
  537. UNUSED(Banks);
  538. #endif /* FLASH_BANK2_END */
  539. /* Only bank1 will be erased*/
  540. SET_BIT(FLASH->CR, FLASH_CR_MER);
  541. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  542. #if defined(FLASH_BANK2_END)
  543. }
  544. #endif /* FLASH_BANK2_END */
  545. }
  546. /**
  547. * @brief Enable the write protection of the desired pages
  548. * @note An option byte erase is done automatically in this function.
  549. * @note When the memory read protection level is selected (RDP level = 1),
  550. * it is not possible to program or erase the flash page i if
  551. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  552. *
  553. * @param WriteProtectPage specifies the page(s) to be write protected.
  554. * The value of this parameter depend on device used within the same series
  555. * @retval HAL status
  556. */
  557. static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
  558. {
  559. HAL_StatusTypeDef status = HAL_OK;
  560. uint16_t WRP0_Data = 0xFFFF;
  561. #if defined(FLASH_WRP1_WRP1)
  562. uint16_t WRP1_Data = 0xFFFF;
  563. #endif /* FLASH_WRP1_WRP1 */
  564. #if defined(FLASH_WRP2_WRP2)
  565. uint16_t WRP2_Data = 0xFFFF;
  566. #endif /* FLASH_WRP2_WRP2 */
  567. #if defined(FLASH_WRP3_WRP3)
  568. uint16_t WRP3_Data = 0xFFFF;
  569. #endif /* FLASH_WRP3_WRP3 */
  570. /* Check the parameters */
  571. assert_param(IS_OB_WRP(WriteProtectPage));
  572. /* Get current write protected pages and the new pages to be protected ******/
  573. WriteProtectPage = (uint32_t)(~((~FLASH_OB_GetWRP()) | WriteProtectPage));
  574. #if defined(OB_WRP_PAGES0TO15MASK)
  575. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
  576. #elif defined(OB_WRP_PAGES0TO31MASK)
  577. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
  578. #endif /* OB_WRP_PAGES0TO31MASK */
  579. #if defined(OB_WRP_PAGES16TO31MASK)
  580. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
  581. #elif defined(OB_WRP_PAGES32TO63MASK)
  582. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
  583. #endif /* OB_WRP_PAGES32TO63MASK */
  584. #if defined(OB_WRP_PAGES64TO95MASK)
  585. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U);
  586. #endif /* OB_WRP_PAGES64TO95MASK */
  587. #if defined(OB_WRP_PAGES32TO47MASK)
  588. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
  589. #endif /* OB_WRP_PAGES32TO47MASK */
  590. #if defined(OB_WRP_PAGES96TO127MASK)
  591. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U);
  592. #elif defined(OB_WRP_PAGES48TO255MASK)
  593. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U);
  594. #elif defined(OB_WRP_PAGES48TO511MASK)
  595. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U);
  596. #elif defined(OB_WRP_PAGES48TO127MASK)
  597. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
  598. #endif /* OB_WRP_PAGES96TO127MASK */
  599. /* Wait for last operation to be completed */
  600. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  601. if(status == HAL_OK)
  602. {
  603. /* Clean the error context */
  604. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  605. /* To be able to write again option byte, need to perform a option byte erase */
  606. status = HAL_FLASHEx_OBErase();
  607. if (status == HAL_OK)
  608. {
  609. /* Enable write protection */
  610. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  611. #if defined(FLASH_WRP0_WRP0)
  612. if(WRP0_Data != 0xFFU)
  613. {
  614. OB->WRP0 &= WRP0_Data;
  615. /* Wait for last operation to be completed */
  616. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  617. }
  618. #endif /* FLASH_WRP0_WRP0 */
  619. #if defined(FLASH_WRP1_WRP1)
  620. if((status == HAL_OK) && (WRP1_Data != 0xFFU))
  621. {
  622. OB->WRP1 &= WRP1_Data;
  623. /* Wait for last operation to be completed */
  624. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  625. }
  626. #endif /* FLASH_WRP1_WRP1 */
  627. #if defined(FLASH_WRP2_WRP2)
  628. if((status == HAL_OK) && (WRP2_Data != 0xFFU))
  629. {
  630. OB->WRP2 &= WRP2_Data;
  631. /* Wait for last operation to be completed */
  632. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  633. }
  634. #endif /* FLASH_WRP2_WRP2 */
  635. #if defined(FLASH_WRP3_WRP3)
  636. if((status == HAL_OK) && (WRP3_Data != 0xFFU))
  637. {
  638. OB->WRP3 &= WRP3_Data;
  639. /* Wait for last operation to be completed */
  640. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  641. }
  642. #endif /* FLASH_WRP3_WRP3 */
  643. /* if the program operation is completed, disable the OPTPG Bit */
  644. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  645. }
  646. }
  647. return status;
  648. }
  649. /**
  650. * @brief Disable the write protection of the desired pages
  651. * @note An option byte erase is done automatically in this function.
  652. * @note When the memory read protection level is selected (RDP level = 1),
  653. * it is not possible to program or erase the flash page i if
  654. * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
  655. *
  656. * @param WriteProtectPage specifies the page(s) to be write unprotected.
  657. * The value of this parameter depend on device used within the same series
  658. * @retval HAL status
  659. */
  660. static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
  661. {
  662. HAL_StatusTypeDef status = HAL_OK;
  663. uint16_t WRP0_Data = 0xFFFF;
  664. #if defined(FLASH_WRP1_WRP1)
  665. uint16_t WRP1_Data = 0xFFFF;
  666. #endif /* FLASH_WRP1_WRP1 */
  667. #if defined(FLASH_WRP2_WRP2)
  668. uint16_t WRP2_Data = 0xFFFF;
  669. #endif /* FLASH_WRP2_WRP2 */
  670. #if defined(FLASH_WRP3_WRP3)
  671. uint16_t WRP3_Data = 0xFFFF;
  672. #endif /* FLASH_WRP3_WRP3 */
  673. /* Check the parameters */
  674. assert_param(IS_OB_WRP(WriteProtectPage));
  675. /* Get current write protected pages and the new pages to be unprotected ******/
  676. WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage);
  677. #if defined(OB_WRP_PAGES0TO15MASK)
  678. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
  679. #elif defined(OB_WRP_PAGES0TO31MASK)
  680. WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
  681. #endif /* OB_WRP_PAGES0TO31MASK */
  682. #if defined(OB_WRP_PAGES16TO31MASK)
  683. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
  684. #elif defined(OB_WRP_PAGES32TO63MASK)
  685. WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
  686. #endif /* OB_WRP_PAGES32TO63MASK */
  687. #if defined(OB_WRP_PAGES64TO95MASK)
  688. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U);
  689. #endif /* OB_WRP_PAGES64TO95MASK */
  690. #if defined(OB_WRP_PAGES32TO47MASK)
  691. WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
  692. #endif /* OB_WRP_PAGES32TO47MASK */
  693. #if defined(OB_WRP_PAGES96TO127MASK)
  694. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U);
  695. #elif defined(OB_WRP_PAGES48TO255MASK)
  696. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U);
  697. #elif defined(OB_WRP_PAGES48TO511MASK)
  698. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U);
  699. #elif defined(OB_WRP_PAGES48TO127MASK)
  700. WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
  701. #endif /* OB_WRP_PAGES96TO127MASK */
  702. /* Wait for last operation to be completed */
  703. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  704. if(status == HAL_OK)
  705. {
  706. /* Clean the error context */
  707. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  708. /* To be able to write again option byte, need to perform a option byte erase */
  709. status = HAL_FLASHEx_OBErase();
  710. if (status == HAL_OK)
  711. {
  712. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  713. #if defined(FLASH_WRP0_WRP0)
  714. if(WRP0_Data != 0xFFU)
  715. {
  716. OB->WRP0 |= WRP0_Data;
  717. /* Wait for last operation to be completed */
  718. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  719. }
  720. #endif /* FLASH_WRP0_WRP0 */
  721. #if defined(FLASH_WRP1_WRP1)
  722. if((status == HAL_OK) && (WRP1_Data != 0xFFU))
  723. {
  724. OB->WRP1 |= WRP1_Data;
  725. /* Wait for last operation to be completed */
  726. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  727. }
  728. #endif /* FLASH_WRP1_WRP1 */
  729. #if defined(FLASH_WRP2_WRP2)
  730. if((status == HAL_OK) && (WRP2_Data != 0xFFU))
  731. {
  732. OB->WRP2 |= WRP2_Data;
  733. /* Wait for last operation to be completed */
  734. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  735. }
  736. #endif /* FLASH_WRP2_WRP2 */
  737. #if defined(FLASH_WRP3_WRP3)
  738. if((status == HAL_OK) && (WRP3_Data != 0xFFU))
  739. {
  740. OB->WRP3 |= WRP3_Data;
  741. /* Wait for last operation to be completed */
  742. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  743. }
  744. #endif /* FLASH_WRP3_WRP3 */
  745. /* if the program operation is completed, disable the OPTPG Bit */
  746. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  747. }
  748. }
  749. return status;
  750. }
  751. /**
  752. * @brief Set the read protection level.
  753. * @param ReadProtectLevel specifies the read protection level.
  754. * This parameter can be one of the following values:
  755. * @arg @ref OB_RDP_LEVEL_0 No protection
  756. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  757. * @retval HAL status
  758. */
  759. static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
  760. {
  761. HAL_StatusTypeDef status = HAL_OK;
  762. /* Check the parameters */
  763. assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
  764. /* Wait for last operation to be completed */
  765. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  766. if(status == HAL_OK)
  767. {
  768. /* Clean the error context */
  769. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  770. /* If the previous operation is completed, proceed to erase the option bytes */
  771. SET_BIT(FLASH->CR, FLASH_CR_OPTER);
  772. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  773. /* Wait for last operation to be completed */
  774. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  775. /* If the erase operation is completed, disable the OPTER Bit */
  776. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
  777. if(status == HAL_OK)
  778. {
  779. /* Enable the Option Bytes Programming operation */
  780. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  781. WRITE_REG(OB->RDP, ReadProtectLevel);
  782. /* Wait for last operation to be completed */
  783. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  784. /* if the program operation is completed, disable the OPTPG Bit */
  785. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  786. }
  787. }
  788. return status;
  789. }
  790. /**
  791. * @brief Program the FLASH User Option Byte.
  792. * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
  793. * @param UserConfig The FLASH User Option Bytes values FLASH_OBR_IWDG_SW(Bit2),
  794. * FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4).
  795. * And BFBF2(Bit5) for STM32F101xG and STM32F103xG .
  796. * @retval HAL status
  797. */
  798. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
  799. {
  800. HAL_StatusTypeDef status = HAL_OK;
  801. /* Check the parameters */
  802. assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
  803. assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
  804. assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
  805. #if defined(FLASH_BANK2_END)
  806. assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
  807. #endif /* FLASH_BANK2_END */
  808. /* Wait for last operation to be completed */
  809. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  810. if(status == HAL_OK)
  811. {
  812. /* Clean the error context */
  813. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  814. /* Enable the Option Bytes Programming operation */
  815. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  816. #if defined(FLASH_BANK2_END)
  817. OB->USER = (UserConfig | 0xF0U);
  818. #else
  819. OB->USER = (UserConfig | 0x88U);
  820. #endif /* FLASH_BANK2_END */
  821. /* Wait for last operation to be completed */
  822. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  823. /* if the program operation is completed, disable the OPTPG Bit */
  824. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  825. }
  826. return status;
  827. }
  828. /**
  829. * @brief Programs a half word at a specified Option Byte Data address.
  830. * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  831. * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
  832. * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
  833. * (system reset will occur)
  834. * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
  835. * @param Address specifies the address to be programmed.
  836. * This parameter can be 0x1FFFF804 or 0x1FFFF806.
  837. * @param Data specifies the data to be programmed.
  838. * @retval HAL status
  839. */
  840. static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
  841. {
  842. HAL_StatusTypeDef status = HAL_ERROR;
  843. /* Check the parameters */
  844. assert_param(IS_OB_DATA_ADDRESS(Address));
  845. /* Wait for last operation to be completed */
  846. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  847. if(status == HAL_OK)
  848. {
  849. /* Clean the error context */
  850. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  851. /* Enables the Option Bytes Programming operation */
  852. SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
  853. *(__IO uint16_t*)Address = Data;
  854. /* Wait for last operation to be completed */
  855. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  856. /* If the program operation is completed, disable the OPTPG Bit */
  857. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
  858. }
  859. /* Return the Option Byte Data Program Status */
  860. return status;
  861. }
  862. /**
  863. * @brief Return the FLASH Write Protection Option Bytes value.
  864. * @retval The FLASH Write Protection Option Bytes value
  865. */
  866. static uint32_t FLASH_OB_GetWRP(void)
  867. {
  868. /* Return the FLASH write protection Register value */
  869. return (uint32_t)(READ_REG(FLASH->WRPR));
  870. }
  871. /**
  872. * @brief Returns the FLASH Read Protection level.
  873. * @retval FLASH RDP level
  874. * This parameter can be one of the following values:
  875. * @arg @ref OB_RDP_LEVEL_0 No protection
  876. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  877. */
  878. static uint32_t FLASH_OB_GetRDP(void)
  879. {
  880. uint32_t readstatus = OB_RDP_LEVEL_0;
  881. uint32_t tmp_reg = 0U;
  882. /* Read RDP level bits */
  883. tmp_reg = READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT);
  884. if (tmp_reg == FLASH_OBR_RDPRT)
  885. {
  886. readstatus = OB_RDP_LEVEL_1;
  887. }
  888. else
  889. {
  890. readstatus = OB_RDP_LEVEL_0;
  891. }
  892. return readstatus;
  893. }
  894. /**
  895. * @brief Return the FLASH User Option Byte value.
  896. * @retval The FLASH User Option Bytes values: FLASH_OBR_IWDG_SW(Bit2),
  897. * FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4).
  898. * And FLASH_OBR_BFB2(Bit5) for STM32F101xG and STM32F103xG .
  899. */
  900. static uint8_t FLASH_OB_GetUser(void)
  901. {
  902. /* Return the User Option Byte */
  903. return (uint8_t)((READ_REG(FLASH->OBR) & FLASH_OBR_USER) >> FLASH_POSITION_IWDGSW_BIT);
  904. }
  905. /**
  906. * @}
  907. */
  908. /**
  909. * @}
  910. */
  911. /** @addtogroup FLASH
  912. * @{
  913. */
  914. /** @addtogroup FLASH_Private_Functions
  915. * @{
  916. */
  917. /**
  918. * @brief Erase the specified FLASH memory page
  919. * @param PageAddress FLASH page to erase
  920. * The value of this parameter depend on device used within the same series
  921. *
  922. * @retval None
  923. */
  924. void FLASH_PageErase(uint32_t PageAddress)
  925. {
  926. /* Clean the error context */
  927. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  928. #if defined(FLASH_BANK2_END)
  929. if(PageAddress > FLASH_BANK1_END)
  930. {
  931. /* Proceed to erase the page */
  932. SET_BIT(FLASH->CR2, FLASH_CR2_PER);
  933. WRITE_REG(FLASH->AR2, PageAddress);
  934. SET_BIT(FLASH->CR2, FLASH_CR2_STRT);
  935. }
  936. else
  937. {
  938. #endif /* FLASH_BANK2_END */
  939. /* Proceed to erase the page */
  940. SET_BIT(FLASH->CR, FLASH_CR_PER);
  941. WRITE_REG(FLASH->AR, PageAddress);
  942. SET_BIT(FLASH->CR, FLASH_CR_STRT);
  943. #if defined(FLASH_BANK2_END)
  944. }
  945. #endif /* FLASH_BANK2_END */
  946. }
  947. /**
  948. * @}
  949. */
  950. /**
  951. * @}
  952. */
  953. #endif /* HAL_FLASH_MODULE_ENABLED */
  954. /**
  955. * @}
  956. */
  957. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/