stm32f1xx_hal_flash_ex.c 36 KB

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