stm32f1xx_hal_flash_ex.c 36 KB

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