stm32f0xx_hal_flash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @brief FLASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the internal FLASH memory:
  8. * + Program operations functions
  9. * + Memory Control functions
  10. * + Peripheral State functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### FLASH peripheral features #####
  15. ==============================================================================
  16. [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
  17. to the Flash memory. It implements the erase and program Flash memory operations
  18. and the read and write protection mechanisms.
  19. [..] The Flash memory interface accelerates code execution with a system of instruction
  20. prefetch.
  21. [..] The FLASH main features are:
  22. (+) Flash memory read operations
  23. (+) Flash memory program/erase operations
  24. (+) Read / write protections
  25. (+) Prefetch on I-Code
  26. (+) Option Bytes programming
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. This driver provides functions and macros to configure and program the FLASH
  31. memory of all STM32F0xx devices.
  32. (#) FLASH Memory I/O Programming functions: this group includes all needed
  33. functions to erase and program the main memory:
  34. (++) Lock and Unlock the FLASH interface
  35. (++) Erase function: Erase page, erase all pages
  36. (++) Program functions: half word, word and doubleword
  37. (#) FLASH Option Bytes Programming functions: this group includes all needed
  38. functions to manage the Option Bytes:
  39. (++) Lock and Unlock the Option Bytes
  40. (++) Set/Reset the write protection
  41. (++) Set the Read protection Level
  42. (++) Program the user Option Bytes
  43. (++) Launch the Option Bytes loader
  44. (++) Erase Option Bytes
  45. (++) Program the data Option Bytes
  46. (++) Get the Write protection.
  47. (++) Get the user option bytes.
  48. (#) Interrupts and flags management functions : this group
  49. includes all needed functions to:
  50. (++) Handle FLASH interrupts
  51. (++) Wait for last FLASH operation according to its status
  52. (++) Get error flag status
  53. [..] In addition to these function, this driver includes a set of macros allowing
  54. to handle the following operations:
  55. (+) Set/Get the latency
  56. (+) Enable/Disable the prefetch buffer
  57. (+) Enable/Disable the FLASH interrupts
  58. (+) Monitor the FLASH flags status
  59. @endverbatim
  60. ******************************************************************************
  61. * @attention
  62. *
  63. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  64. * All rights reserved.</center></h2>
  65. *
  66. * This software component is licensed by ST under BSD 3-Clause license,
  67. * the "License"; You may not use this file except in compliance with the
  68. * License. You may obtain a copy of the License at:
  69. * opensource.org/licenses/BSD-3-Clause
  70. *
  71. ******************************************************************************
  72. */
  73. /* Includes ------------------------------------------------------------------*/
  74. #include "stm32f0xx_hal.h"
  75. /** @addtogroup STM32F0xx_HAL_Driver
  76. * @{
  77. */
  78. #ifdef HAL_FLASH_MODULE_ENABLED
  79. /** @defgroup FLASH FLASH
  80. * @brief FLASH HAL module driver
  81. * @{
  82. */
  83. /* Private typedef -----------------------------------------------------------*/
  84. /* Private define ------------------------------------------------------------*/
  85. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  86. * @{
  87. */
  88. /**
  89. * @}
  90. */
  91. /* Private macro ---------------------------- ---------------------------------*/
  92. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  93. * @{
  94. */
  95. /**
  96. * @}
  97. */
  98. /* Private variables ---------------------------------------------------------*/
  99. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  100. * @{
  101. */
  102. /* Variables used for Erase pages under interruption*/
  103. FLASH_ProcessTypeDef pFlash;
  104. /**
  105. * @}
  106. */
  107. /* Private function prototypes -----------------------------------------------*/
  108. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  109. * @{
  110. */
  111. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
  112. static void FLASH_SetErrorCode(void);
  113. extern void FLASH_PageErase(uint32_t PageAddress);
  114. /**
  115. * @}
  116. */
  117. /* Exported functions ---------------------------------------------------------*/
  118. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  119. * @{
  120. */
  121. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  122. * @brief Programming operation functions
  123. *
  124. @verbatim
  125. @endverbatim
  126. * @{
  127. */
  128. /**
  129. * @brief Program halfword, word or double word at a specified address
  130. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  131. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  132. *
  133. * @note If an erase and a program operations are requested simultaneously,
  134. * the erase operation is performed before the program one.
  135. *
  136. * @note FLASH should be previously erased before new programming (only exception to this
  137. * is when 0x0000 is programmed)
  138. *
  139. * @param TypeProgram Indicate the way to program at a specified address.
  140. * This parameter can be a value of @ref FLASH_Type_Program
  141. * @param Address Specifie the address to be programmed.
  142. * @param Data Specifie the data to be programmed
  143. *
  144. * @retval HAL_StatusTypeDef HAL Status
  145. */
  146. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  147. {
  148. HAL_StatusTypeDef status = HAL_ERROR;
  149. uint8_t index = 0U;
  150. uint8_t nbiterations = 0U;
  151. /* Process Locked */
  152. __HAL_LOCK(&pFlash);
  153. /* Check the parameters */
  154. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  155. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  156. /* Wait for last operation to be completed */
  157. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  158. if(status == HAL_OK)
  159. {
  160. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  161. {
  162. /* Program halfword (16-bit) at a specified address. */
  163. nbiterations = 1U;
  164. }
  165. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  166. {
  167. /* Program word (32-bit = 2*16-bit) at a specified address. */
  168. nbiterations = 2U;
  169. }
  170. else
  171. {
  172. /* Program double word (64-bit = 4*16-bit) at a specified address. */
  173. nbiterations = 4U;
  174. }
  175. for (index = 0U; index < nbiterations; index++)
  176. {
  177. FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index)));
  178. /* Wait for last operation to be completed */
  179. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  180. /* If the program operation is completed, disable the PG Bit */
  181. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  182. /* In case of error, stop programming procedure */
  183. if (status != HAL_OK)
  184. {
  185. break;
  186. }
  187. }
  188. }
  189. /* Process Unlocked */
  190. __HAL_UNLOCK(&pFlash);
  191. return status;
  192. }
  193. /**
  194. * @brief Program halfword, word or double word at a specified address with interrupt enabled.
  195. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  196. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  197. *
  198. * @note If an erase and a program operations are requested simultaneously,
  199. * the erase operation is performed before the program one.
  200. *
  201. * @param TypeProgram Indicate the way to program at a specified address.
  202. * This parameter can be a value of @ref FLASH_Type_Program
  203. * @param Address Specifie the address to be programmed.
  204. * @param Data Specifie the data to be programmed
  205. *
  206. * @retval HAL_StatusTypeDef HAL Status
  207. */
  208. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  209. {
  210. HAL_StatusTypeDef status = HAL_OK;
  211. /* Process Locked */
  212. __HAL_LOCK(&pFlash);
  213. /* Check the parameters */
  214. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  215. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  216. /* Enable End of FLASH Operation and Error source interrupts */
  217. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  218. pFlash.Address = Address;
  219. pFlash.Data = Data;
  220. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  221. {
  222. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
  223. /* Program halfword (16-bit) at a specified address. */
  224. pFlash.DataRemaining = 1U;
  225. }
  226. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  227. {
  228. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
  229. /* Program word (32-bit : 2*16-bit) at a specified address. */
  230. pFlash.DataRemaining = 2U;
  231. }
  232. else
  233. {
  234. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
  235. /* Program double word (64-bit : 4*16-bit) at a specified address. */
  236. pFlash.DataRemaining = 4U;
  237. }
  238. /* Program halfword (16-bit) at a specified address. */
  239. FLASH_Program_HalfWord(Address, (uint16_t)Data);
  240. return status;
  241. }
  242. /**
  243. * @brief This function handles FLASH interrupt request.
  244. * @retval None
  245. */
  246. void HAL_FLASH_IRQHandler(void)
  247. {
  248. uint32_t addresstmp = 0U;
  249. /* Check FLASH operation error flags */
  250. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  251. {
  252. /* Return the faulty address */
  253. addresstmp = pFlash.Address;
  254. /* Reset address */
  255. pFlash.Address = 0xFFFFFFFFU;
  256. /* Save the Error code */
  257. FLASH_SetErrorCode();
  258. /* FLASH error interrupt user callback */
  259. HAL_FLASH_OperationErrorCallback(addresstmp);
  260. /* Stop the procedure ongoing */
  261. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  262. }
  263. /* Check FLASH End of Operation flag */
  264. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  265. {
  266. /* Clear FLASH End of Operation pending bit */
  267. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  268. /* Process can continue only if no error detected */
  269. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  270. {
  271. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  272. {
  273. /* Nb of pages to erased can be decreased */
  274. pFlash.DataRemaining--;
  275. /* Check if there are still pages to erase */
  276. if(pFlash.DataRemaining != 0U)
  277. {
  278. addresstmp = pFlash.Address;
  279. /*Indicate user which sector has been erased */
  280. HAL_FLASH_EndOfOperationCallback(addresstmp);
  281. /*Increment sector number*/
  282. addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
  283. pFlash.Address = addresstmp;
  284. /* If the erase operation is completed, disable the PER Bit */
  285. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  286. FLASH_PageErase(addresstmp);
  287. }
  288. else
  289. {
  290. /* No more pages to Erase, user callback can be called. */
  291. /* Reset Sector and stop Erase pages procedure */
  292. pFlash.Address = addresstmp = 0xFFFFFFFFU;
  293. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  294. /* FLASH EOP interrupt user callback */
  295. HAL_FLASH_EndOfOperationCallback(addresstmp);
  296. }
  297. }
  298. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  299. {
  300. /* Operation is completed, disable the MER Bit */
  301. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  302. /* MassErase ended. Return the selected bank */
  303. /* FLASH EOP interrupt user callback */
  304. HAL_FLASH_EndOfOperationCallback(0);
  305. /* Stop Mass Erase procedure*/
  306. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  307. }
  308. else
  309. {
  310. /* Nb of 16-bit data to program can be decreased */
  311. pFlash.DataRemaining--;
  312. /* Check if there are still 16-bit data to program */
  313. if(pFlash.DataRemaining != 0U)
  314. {
  315. /* Increment address to 16-bit */
  316. pFlash.Address += 2;
  317. addresstmp = pFlash.Address;
  318. /* Shift to have next 16-bit data */
  319. pFlash.Data = (pFlash.Data >> 16U);
  320. /* Operation is completed, disable the PG Bit */
  321. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  322. /*Program halfword (16-bit) at a specified address.*/
  323. FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
  324. }
  325. else
  326. {
  327. /* Program ended. Return the selected address */
  328. /* FLASH EOP interrupt user callback */
  329. if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
  330. {
  331. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  332. }
  333. else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
  334. {
  335. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U);
  336. }
  337. else
  338. {
  339. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U);
  340. }
  341. /* Reset Address and stop Program procedure */
  342. pFlash.Address = 0xFFFFFFFFU;
  343. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  344. }
  345. }
  346. }
  347. }
  348. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  349. {
  350. /* Operation is completed, disable the PG, PER and MER Bits */
  351. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
  352. /* Disable End of FLASH Operation and Error source interrupts */
  353. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  354. /* Process Unlocked */
  355. __HAL_UNLOCK(&pFlash);
  356. }
  357. }
  358. /**
  359. * @brief FLASH end of operation interrupt callback
  360. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  361. * - Mass Erase: No return value expected
  362. * - Pages Erase: Address of the page which has been erased
  363. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  364. * - Program: Address which was selected for data program
  365. * @retval none
  366. */
  367. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  368. {
  369. /* Prevent unused argument(s) compilation warning */
  370. UNUSED(ReturnValue);
  371. /* NOTE : This function Should not be modified, when the callback is needed,
  372. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  373. */
  374. }
  375. /**
  376. * @brief FLASH operation error interrupt callback
  377. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  378. * - Mass Erase: No return value expected
  379. * - Pages Erase: Address of the page which returned an error
  380. * - Program: Address which was selected for data program
  381. * @retval none
  382. */
  383. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  384. {
  385. /* Prevent unused argument(s) compilation warning */
  386. UNUSED(ReturnValue);
  387. /* NOTE : This function Should not be modified, when the callback is needed,
  388. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  389. */
  390. }
  391. /**
  392. * @}
  393. */
  394. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  395. * @brief management functions
  396. *
  397. @verbatim
  398. ===============================================================================
  399. ##### Peripheral Control functions #####
  400. ===============================================================================
  401. [..]
  402. This subsection provides a set of functions allowing to control the FLASH
  403. memory operations.
  404. @endverbatim
  405. * @{
  406. */
  407. /**
  408. * @brief Unlock the FLASH control register access
  409. * @retval HAL Status
  410. */
  411. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  412. {
  413. HAL_StatusTypeDef status = HAL_OK;
  414. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  415. {
  416. /* Authorize the FLASH Registers access */
  417. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  418. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  419. /* Verify Flash is unlocked */
  420. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  421. {
  422. status = HAL_ERROR;
  423. }
  424. }
  425. return status;
  426. }
  427. /**
  428. * @brief Locks the FLASH control register access
  429. * @retval HAL Status
  430. */
  431. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  432. {
  433. /* Set the LOCK Bit to lock the FLASH Registers access */
  434. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  435. return HAL_OK;
  436. }
  437. /**
  438. * @brief Unlock the FLASH Option Control Registers access.
  439. * @retval HAL Status
  440. */
  441. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  442. {
  443. if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
  444. {
  445. /* Authorizes the Option Byte register programming */
  446. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  447. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  448. }
  449. else
  450. {
  451. return HAL_ERROR;
  452. }
  453. return HAL_OK;
  454. }
  455. /**
  456. * @brief Lock the FLASH Option Control Registers access.
  457. * @retval HAL Status
  458. */
  459. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  460. {
  461. /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
  462. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
  463. return HAL_OK;
  464. }
  465. /**
  466. * @brief Launch the option byte loading.
  467. * @note This function will reset automatically the MCU.
  468. * @retval HAL Status
  469. */
  470. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  471. {
  472. /* Set the OBL_Launch bit to launch the option byte loading */
  473. SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
  474. /* Wait for last operation to be completed */
  475. return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE));
  476. }
  477. /**
  478. * @}
  479. */
  480. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
  481. * @brief Peripheral errors functions
  482. *
  483. @verbatim
  484. ===============================================================================
  485. ##### Peripheral Errors functions #####
  486. ===============================================================================
  487. [..]
  488. This subsection permit to get in run-time errors of the FLASH peripheral.
  489. @endverbatim
  490. * @{
  491. */
  492. /**
  493. * @brief Get the specific FLASH error flag.
  494. * @retval FLASH_ErrorCode The returned value can be:
  495. * @ref FLASH_Error_Codes
  496. */
  497. uint32_t HAL_FLASH_GetError(void)
  498. {
  499. return pFlash.ErrorCode;
  500. }
  501. /**
  502. * @}
  503. */
  504. /**
  505. * @}
  506. */
  507. /** @addtogroup FLASH_Private_Functions
  508. * @{
  509. */
  510. /**
  511. * @brief Program a half-word (16-bit) at a specified address.
  512. * @param Address specify the address to be programmed.
  513. * @param Data specify the data to be programmed.
  514. * @retval None
  515. */
  516. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
  517. {
  518. /* Clean the error context */
  519. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  520. /* Proceed to program the new data */
  521. SET_BIT(FLASH->CR, FLASH_CR_PG);
  522. /* Write data in the address */
  523. *(__IO uint16_t*)Address = Data;
  524. }
  525. /**
  526. * @brief Wait for a FLASH operation to complete.
  527. * @param Timeout maximum flash operation timeout
  528. * @retval HAL Status
  529. */
  530. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  531. {
  532. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  533. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  534. flag will be set */
  535. uint32_t tickstart = HAL_GetTick();
  536. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  537. {
  538. if (Timeout != HAL_MAX_DELAY)
  539. {
  540. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  541. {
  542. return HAL_TIMEOUT;
  543. }
  544. }
  545. }
  546. /* Check FLASH End of Operation flag */
  547. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  548. {
  549. /* Clear FLASH End of Operation pending bit */
  550. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  551. }
  552. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  553. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  554. {
  555. /*Save the error code*/
  556. FLASH_SetErrorCode();
  557. return HAL_ERROR;
  558. }
  559. /* There is no error flag set */
  560. return HAL_OK;
  561. }
  562. /**
  563. * @brief Set the specific FLASH error flag.
  564. * @retval None
  565. */
  566. static void FLASH_SetErrorCode(void)
  567. {
  568. uint32_t flags = 0U;
  569. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  570. {
  571. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  572. flags |= FLASH_FLAG_WRPERR;
  573. }
  574. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  575. {
  576. pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
  577. flags |= FLASH_FLAG_PGERR;
  578. }
  579. /* Clear FLASH error pending bits */
  580. __HAL_FLASH_CLEAR_FLAG(flags);
  581. }
  582. /**
  583. * @}
  584. */
  585. /**
  586. * @}
  587. */
  588. #endif /* HAL_FLASH_MODULE_ENABLED */
  589. /**
  590. * @}
  591. */
  592. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/