You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

707 lines
21 KiB

  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</center></h2>
  64. *
  65. * Redistribution and use in source and binary forms, with or without modification,
  66. * are permitted provided that the following conditions are met:
  67. * 1. Redistributions of source code must retain the above copyright notice,
  68. * this list of conditions and the following disclaimer.
  69. * 2. Redistributions in binary form must reproduce the above copyright notice,
  70. * this list of conditions and the following disclaimer in the documentation
  71. * and/or other materials provided with the distribution.
  72. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  73. * may be used to endorse or promote products derived from this software
  74. * without specific prior written permission.
  75. *
  76. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  77. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  78. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  79. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  80. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  81. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  83. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  84. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  85. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  86. *
  87. ******************************************************************************
  88. */
  89. /* Includes ------------------------------------------------------------------*/
  90. #include "stm32f0xx_hal.h"
  91. /** @addtogroup STM32F0xx_HAL_Driver
  92. * @{
  93. */
  94. #ifdef HAL_FLASH_MODULE_ENABLED
  95. /** @defgroup FLASH FLASH
  96. * @brief FLASH HAL module driver
  97. * @{
  98. */
  99. /* Private typedef -----------------------------------------------------------*/
  100. /* Private define ------------------------------------------------------------*/
  101. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  102. * @{
  103. */
  104. /**
  105. * @}
  106. */
  107. /* Private macro ---------------------------- ---------------------------------*/
  108. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  109. * @{
  110. */
  111. /**
  112. * @}
  113. */
  114. /* Private variables ---------------------------------------------------------*/
  115. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  116. * @{
  117. */
  118. /* Variables used for Erase pages under interruption*/
  119. FLASH_ProcessTypeDef pFlash;
  120. /**
  121. * @}
  122. */
  123. /* Private function prototypes -----------------------------------------------*/
  124. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  125. * @{
  126. */
  127. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
  128. static void FLASH_SetErrorCode(void);
  129. extern void FLASH_PageErase(uint32_t PageAddress);
  130. /**
  131. * @}
  132. */
  133. /* Exported functions ---------------------------------------------------------*/
  134. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  135. * @{
  136. */
  137. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  138. * @brief Programming operation functions
  139. *
  140. @verbatim
  141. @endverbatim
  142. * @{
  143. */
  144. /**
  145. * @brief Program halfword, word or double word at a specified address
  146. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  147. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  148. *
  149. * @note If an erase and a program operations are requested simultaneously,
  150. * the erase operation is performed before the program one.
  151. *
  152. * @note FLASH should be previously erased before new programmation (only exception to this
  153. * is when 0x0000 is programmed)
  154. *
  155. * @param TypeProgram Indicate the way to program at a specified address.
  156. * This parameter can be a value of @ref FLASH_Type_Program
  157. * @param Address Specifie the address to be programmed.
  158. * @param Data Specifie the data to be programmed
  159. *
  160. * @retval HAL_StatusTypeDef HAL Status
  161. */
  162. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  163. {
  164. HAL_StatusTypeDef status = HAL_ERROR;
  165. uint8_t index = 0U;
  166. uint8_t nbiterations = 0U;
  167. /* Process Locked */
  168. __HAL_LOCK(&pFlash);
  169. /* Check the parameters */
  170. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  171. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  172. /* Wait for last operation to be completed */
  173. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  174. if(status == HAL_OK)
  175. {
  176. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  177. {
  178. /* Program halfword (16-bit) at a specified address. */
  179. nbiterations = 1U;
  180. }
  181. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  182. {
  183. /* Program word (32-bit = 2*16-bit) at a specified address. */
  184. nbiterations = 2U;
  185. }
  186. else
  187. {
  188. /* Program double word (64-bit = 4*16-bit) at a specified address. */
  189. nbiterations = 4U;
  190. }
  191. for (index = 0U; index < nbiterations; index++)
  192. {
  193. FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index)));
  194. /* Wait for last operation to be completed */
  195. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  196. /* If the program operation is completed, disable the PG Bit */
  197. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  198. /* In case of error, stop programation procedure */
  199. if (status != HAL_OK)
  200. {
  201. break;
  202. }
  203. }
  204. }
  205. /* Process Unlocked */
  206. __HAL_UNLOCK(&pFlash);
  207. return status;
  208. }
  209. /**
  210. * @brief Program halfword, word or double word at a specified address with interrupt enabled.
  211. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  212. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  213. *
  214. * @note If an erase and a program operations are requested simultaneously,
  215. * the erase operation is performed before the program one.
  216. *
  217. * @param TypeProgram Indicate the way to program at a specified address.
  218. * This parameter can be a value of @ref FLASH_Type_Program
  219. * @param Address Specifie the address to be programmed.
  220. * @param Data Specifie the data to be programmed
  221. *
  222. * @retval HAL_StatusTypeDef HAL Status
  223. */
  224. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  225. {
  226. HAL_StatusTypeDef status = HAL_OK;
  227. /* Process Locked */
  228. __HAL_LOCK(&pFlash);
  229. /* Check the parameters */
  230. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  231. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  232. /* Enable End of FLASH Operation and Error source interrupts */
  233. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  234. pFlash.Address = Address;
  235. pFlash.Data = Data;
  236. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  237. {
  238. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
  239. /* Program halfword (16-bit) at a specified address. */
  240. pFlash.DataRemaining = 1U;
  241. }
  242. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  243. {
  244. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
  245. /* Program word (32-bit : 2*16-bit) at a specified address. */
  246. pFlash.DataRemaining = 2U;
  247. }
  248. else
  249. {
  250. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
  251. /* Program double word (64-bit : 4*16-bit) at a specified address. */
  252. pFlash.DataRemaining = 4U;
  253. }
  254. /* Program halfword (16-bit) at a specified address. */
  255. FLASH_Program_HalfWord(Address, (uint16_t)Data);
  256. return status;
  257. }
  258. /**
  259. * @brief This function handles FLASH interrupt request.
  260. * @retval None
  261. */
  262. void HAL_FLASH_IRQHandler(void)
  263. {
  264. uint32_t addresstmp = 0U;
  265. /* Check FLASH operation error flags */
  266. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  267. {
  268. /* Return the faulty address */
  269. addresstmp = pFlash.Address;
  270. /* Reset address */
  271. pFlash.Address = 0xFFFFFFFFU;
  272. /* Save the Error code */
  273. FLASH_SetErrorCode();
  274. /* FLASH error interrupt user callback */
  275. HAL_FLASH_OperationErrorCallback(addresstmp);
  276. /* Stop the procedure ongoing */
  277. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  278. }
  279. /* Check FLASH End of Operation flag */
  280. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  281. {
  282. /* Clear FLASH End of Operation pending bit */
  283. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  284. /* Process can continue only if no error detected */
  285. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  286. {
  287. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  288. {
  289. /* Nb of pages to erased can be decreased */
  290. pFlash.DataRemaining--;
  291. /* Check if there are still pages to erase */
  292. if(pFlash.DataRemaining != 0U)
  293. {
  294. addresstmp = pFlash.Address;
  295. /*Indicate user which sector has been erased */
  296. HAL_FLASH_EndOfOperationCallback(addresstmp);
  297. /*Increment sector number*/
  298. addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
  299. pFlash.Address = addresstmp;
  300. /* If the erase operation is completed, disable the PER Bit */
  301. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  302. FLASH_PageErase(addresstmp);
  303. }
  304. else
  305. {
  306. /* No more pages to Erase, user callback can be called. */
  307. /* Reset Sector and stop Erase pages procedure */
  308. pFlash.Address = addresstmp = 0xFFFFFFFFU;
  309. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  310. /* FLASH EOP interrupt user callback */
  311. HAL_FLASH_EndOfOperationCallback(addresstmp);
  312. }
  313. }
  314. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  315. {
  316. /* Operation is completed, disable the MER Bit */
  317. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  318. /* MassErase ended. Return the selected bank */
  319. /* FLASH EOP interrupt user callback */
  320. HAL_FLASH_EndOfOperationCallback(0);
  321. /* Stop Mass Erase procedure*/
  322. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  323. }
  324. else
  325. {
  326. /* Nb of 16-bit data to program can be decreased */
  327. pFlash.DataRemaining--;
  328. /* Check if there are still 16-bit data to program */
  329. if(pFlash.DataRemaining != 0U)
  330. {
  331. /* Increment address to 16-bit */
  332. pFlash.Address += 2;
  333. addresstmp = pFlash.Address;
  334. /* Shift to have next 16-bit data */
  335. pFlash.Data = (pFlash.Data >> 16U);
  336. /* Operation is completed, disable the PG Bit */
  337. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  338. /*Program halfword (16-bit) at a specified address.*/
  339. FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
  340. }
  341. else
  342. {
  343. /* Program ended. Return the selected address */
  344. /* FLASH EOP interrupt user callback */
  345. if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
  346. {
  347. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  348. }
  349. else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
  350. {
  351. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U);
  352. }
  353. else
  354. {
  355. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U);
  356. }
  357. /* Reset Address and stop Program procedure */
  358. pFlash.Address = 0xFFFFFFFFU;
  359. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  360. }
  361. }
  362. }
  363. }
  364. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  365. {
  366. /* Operation is completed, disable the PG, PER and MER Bits */
  367. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
  368. /* Disable End of FLASH Operation and Error source interrupts */
  369. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  370. /* Process Unlocked */
  371. __HAL_UNLOCK(&pFlash);
  372. }
  373. }
  374. /**
  375. * @brief FLASH end of operation interrupt callback
  376. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  377. * - Mass Erase: No return value expected
  378. * - Pages Erase: Address of the page which has been erased
  379. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  380. * - Program: Address which was selected for data program
  381. * @retval none
  382. */
  383. __weak void HAL_FLASH_EndOfOperationCallback(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_EndOfOperationCallback could be implemented in the user file
  389. */
  390. }
  391. /**
  392. * @brief FLASH operation error interrupt callback
  393. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  394. * - Mass Erase: No return value expected
  395. * - Pages Erase: Address of the page which returned an error
  396. * - Program: Address which was selected for data program
  397. * @retval none
  398. */
  399. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  400. {
  401. /* Prevent unused argument(s) compilation warning */
  402. UNUSED(ReturnValue);
  403. /* NOTE : This function Should not be modified, when the callback is needed,
  404. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  405. */
  406. }
  407. /**
  408. * @}
  409. */
  410. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  411. * @brief management functions
  412. *
  413. @verbatim
  414. ===============================================================================
  415. ##### Peripheral Control functions #####
  416. ===============================================================================
  417. [..]
  418. This subsection provides a set of functions allowing to control the FLASH
  419. memory operations.
  420. @endverbatim
  421. * @{
  422. */
  423. /**
  424. * @brief Unlock the FLASH control register access
  425. * @retval HAL Status
  426. */
  427. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  428. {
  429. if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
  430. {
  431. /* Authorize the FLASH Registers access */
  432. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  433. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  434. }
  435. else
  436. {
  437. return HAL_ERROR;
  438. }
  439. return HAL_OK;
  440. }
  441. /**
  442. * @brief Locks the FLASH control register access
  443. * @retval HAL Status
  444. */
  445. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  446. {
  447. /* Set the LOCK Bit to lock the FLASH Registers access */
  448. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  449. return HAL_OK;
  450. }
  451. /**
  452. * @brief Unlock the FLASH Option Control Registers access.
  453. * @retval HAL Status
  454. */
  455. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  456. {
  457. if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
  458. {
  459. /* Authorizes the Option Byte register programming */
  460. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  461. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  462. }
  463. else
  464. {
  465. return HAL_ERROR;
  466. }
  467. return HAL_OK;
  468. }
  469. /**
  470. * @brief Lock the FLASH Option Control Registers access.
  471. * @retval HAL Status
  472. */
  473. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  474. {
  475. /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
  476. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
  477. return HAL_OK;
  478. }
  479. /**
  480. * @brief Launch the option byte loading.
  481. * @note This function will reset automatically the MCU.
  482. * @retval HAL Status
  483. */
  484. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  485. {
  486. /* Set the OBL_Launch bit to launch the option byte loading */
  487. SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
  488. /* Wait for last operation to be completed */
  489. return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE));
  490. }
  491. /**
  492. * @}
  493. */
  494. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
  495. * @brief Peripheral errors functions
  496. *
  497. @verbatim
  498. ===============================================================================
  499. ##### Peripheral Errors functions #####
  500. ===============================================================================
  501. [..]
  502. This subsection permit to get in run-time errors of the FLASH peripheral.
  503. @endverbatim
  504. * @{
  505. */
  506. /**
  507. * @brief Get the specific FLASH error flag.
  508. * @retval FLASH_ErrorCode The returned value can be:
  509. * @ref FLASH_Error_Codes
  510. */
  511. uint32_t HAL_FLASH_GetError(void)
  512. {
  513. return pFlash.ErrorCode;
  514. }
  515. /**
  516. * @}
  517. */
  518. /**
  519. * @}
  520. */
  521. /** @addtogroup FLASH_Private_Functions
  522. * @{
  523. */
  524. /**
  525. * @brief Program a half-word (16-bit) at a specified address.
  526. * @param Address specify the address to be programmed.
  527. * @param Data specify the data to be programmed.
  528. * @retval None
  529. */
  530. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
  531. {
  532. /* Clean the error context */
  533. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  534. /* Proceed to program the new data */
  535. SET_BIT(FLASH->CR, FLASH_CR_PG);
  536. /* Write data in the address */
  537. *(__IO uint16_t*)Address = Data;
  538. }
  539. /**
  540. * @brief Wait for a FLASH operation to complete.
  541. * @param Timeout maximum flash operation timeout
  542. * @retval HAL Status
  543. */
  544. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  545. {
  546. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  547. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  548. flag will be set */
  549. uint32_t tickstart = HAL_GetTick();
  550. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  551. {
  552. if (Timeout != HAL_MAX_DELAY)
  553. {
  554. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  555. {
  556. return HAL_TIMEOUT;
  557. }
  558. }
  559. }
  560. /* Check FLASH End of Operation flag */
  561. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  562. {
  563. /* Clear FLASH End of Operation pending bit */
  564. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  565. }
  566. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  567. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  568. {
  569. /*Save the error code*/
  570. FLASH_SetErrorCode();
  571. return HAL_ERROR;
  572. }
  573. /* There is no error flag set */
  574. return HAL_OK;
  575. }
  576. /**
  577. * @brief Set the specific FLASH error flag.
  578. * @retval None
  579. */
  580. static void FLASH_SetErrorCode(void)
  581. {
  582. uint32_t flags = 0U;
  583. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  584. {
  585. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  586. flags |= FLASH_FLAG_WRPERR;
  587. }
  588. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  589. {
  590. pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
  591. flags |= FLASH_FLAG_PGERR;
  592. }
  593. /* Clear FLASH error pending bits */
  594. __HAL_FLASH_CLEAR_FLAG(flags);
  595. }
  596. /**
  597. * @}
  598. */
  599. /**
  600. * @}
  601. */
  602. #endif /* HAL_FLASH_MODULE_ENABLED */
  603. /**
  604. * @}
  605. */
  606. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/