Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

722 lines
23 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 STM32L1xx 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
  36. (++) Program functions: Fast Word and Half Page(should be
  37. executed from internal SRAM).
  38. (#) DATA EEPROM Programming functions: this group includes all
  39. needed functions to erase and program the DATA EEPROM memory:
  40. (++) Lock and Unlock the DATA EEPROM interface.
  41. (++) Erase function: Erase Byte, erase HalfWord, erase Word, erase
  42. Double Word (should be executed from internal SRAM).
  43. (++) Program functions: Fast Program Byte, Fast Program Half-Word,
  44. FastProgramWord, Program Byte, Program Half-Word,
  45. Program Word and Program Double-Word (should be executed
  46. from internal SRAM).
  47. (#) FLASH Option Bytes Programming functions: this group includes all needed
  48. functions to manage the Option Bytes:
  49. (++) Lock and Unlock the Option Bytes
  50. (++) Set/Reset the write protection
  51. (++) Set the Read protection Level
  52. (++) Program the user Option Bytes
  53. (++) Launch the Option Bytes loader
  54. (++) Set/Get the Read protection Level.
  55. (++) Set/Get the BOR level.
  56. (++) Get the Write protection.
  57. (++) Get the user option bytes.
  58. (#) Interrupts and flags management functions : this group
  59. includes all needed functions to:
  60. (++) Handle FLASH interrupts
  61. (++) Wait for last FLASH operation according to its status
  62. (++) Get error flag status
  63. (#) FLASH Interface configuration functions: this group includes
  64. the management of following features:
  65. (++) Enable/Disable the RUN PowerDown mode.
  66. (++) Enable/Disable the SLEEP PowerDown mode.
  67. (#) FLASH Peripheral State methods: this group includes
  68. the management of following features:
  69. (++) Wait for the FLASH operation
  70. (++) Get the specific FLASH error flag
  71. [..] In addition to these function, this driver includes a set of macros allowing
  72. to handle the following operations:
  73. (+) Set/Get the latency
  74. (+) Enable/Disable the prefetch buffer
  75. (+) Enable/Disable the 64 bit Read Access.
  76. (+) Enable/Disable the Flash power-down
  77. (+) Enable/Disable the FLASH interrupts
  78. (+) Monitor the FLASH flags status
  79. ##### Programming operation functions #####
  80. ===============================================================================
  81. [..]
  82. This subsection provides a set of functions allowing to manage the FLASH
  83. program operations.
  84. [..] The FLASH Memory Programming functions, includes the following functions:
  85. (+) HAL_FLASH_Unlock(void);
  86. (+) HAL_FLASH_Lock(void);
  87. (+) HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  88. (+) HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  89. [..] Any operation of erase or program should follow these steps:
  90. (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
  91. program memory access.
  92. (#) Call the desired function to erase page or program data.
  93. (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
  94. (recommended to protect the FLASH memory against possible unwanted operation).
  95. ##### Option Bytes Programming functions #####
  96. ==============================================================================
  97. [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
  98. (+) HAL_FLASH_OB_Unlock(void);
  99. (+) HAL_FLASH_OB_Lock(void);
  100. (+) HAL_FLASH_OB_Launch(void);
  101. (+) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  102. (+) HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  103. [..] Any operation of erase or program should follow these steps:
  104. (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
  105. register access.
  106. (#) Call the following functions to program the desired option bytes.
  107. (++) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  108. (#) Once all needed option bytes to be programmed are correctly written, call the
  109. HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  110. (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
  111. to protect the option Bytes against possible unwanted operations).
  112. [..] Proprietary code Read Out Protection (PcROP):
  113. (#) The PcROP sector is selected by using the same option bytes as the Write
  114. protection. As a result, these 2 options are exclusive each other.
  115. (#) To activate PCROP mode for Flash sectors(s), you need to follow the sequence below:
  116. (++) Use this function HAL_FLASHEx_AdvOBProgram with PCROPState = OB_PCROP_STATE_ENABLE.
  117. @endverbatim
  118. ******************************************************************************
  119. * @attention
  120. *
  121. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  122. * All rights reserved.</center></h2>
  123. *
  124. * This software component is licensed by ST under BSD 3-Clause license,
  125. * the "License"; You may not use this file except in compliance with the
  126. * License. You may obtain a copy of the License at:
  127. * opensource.org/licenses/BSD-3-Clause
  128. *
  129. ******************************************************************************
  130. */
  131. /* Includes ------------------------------------------------------------------*/
  132. #include "stm32l1xx_hal.h"
  133. /** @addtogroup STM32L1xx_HAL_Driver
  134. * @{
  135. */
  136. #ifdef HAL_FLASH_MODULE_ENABLED
  137. /** @defgroup FLASH FLASH
  138. * @brief FLASH HAL module driver
  139. * @{
  140. */
  141. /* Private typedef -----------------------------------------------------------*/
  142. /* Private define ------------------------------------------------------------*/
  143. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  144. * @{
  145. */
  146. /**
  147. * @}
  148. */
  149. /* Private macro ---------------------------- ---------------------------------*/
  150. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  151. * @{
  152. */
  153. /**
  154. * @}
  155. */
  156. /* Private variables ---------------------------------------------------------*/
  157. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  158. * @{
  159. */
  160. /* Variables used for Erase pages under interruption*/
  161. FLASH_ProcessTypeDef pFlash;
  162. /**
  163. * @}
  164. */
  165. /* Private function prototypes -----------------------------------------------*/
  166. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  167. * @{
  168. */
  169. static void FLASH_SetErrorCode(void);
  170. extern void FLASH_PageErase(uint32_t PageAddress);
  171. /**
  172. * @}
  173. */
  174. /* Exported functions ---------------------------------------------------------*/
  175. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  176. * @{
  177. */
  178. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  179. * @brief Programming operation functions
  180. *
  181. @verbatim
  182. @endverbatim
  183. * @{
  184. */
  185. /**
  186. * @brief Program word at a specified address
  187. * @note To correctly run this function, the HAL_FLASH_Unlock() function
  188. * must be called before.
  189. * Call the HAL_FLASH_Lock() to disable the flash memory access
  190. * (recommended to protect the FLASH memory against possible unwanted operation).
  191. *
  192. * @param TypeProgram Indicate the way to program at a specified address.
  193. * This parameter can be a value of @ref FLASH_Type_Program
  194. * @param Address Specifie the address to be programmed.
  195. * @param Data Specifie the data to be programmed
  196. *
  197. * @retval HAL_StatusTypeDef HAL Status
  198. */
  199. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  200. {
  201. HAL_StatusTypeDef status = HAL_ERROR;
  202. /* Process Locked */
  203. __HAL_LOCK(&pFlash);
  204. /* Check the parameters */
  205. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  206. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  207. /* Wait for last operation to be completed */
  208. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  209. if(status == HAL_OK)
  210. {
  211. /* Clean the error context */
  212. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  213. /*Program word (32-bit) at a specified address.*/
  214. *(__IO uint32_t *)Address = Data;
  215. /* Wait for last operation to be completed */
  216. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  217. }
  218. /* Process Unlocked */
  219. __HAL_UNLOCK(&pFlash);
  220. return status;
  221. }
  222. /**
  223. * @brief Program word at a specified address with interrupt enabled.
  224. *
  225. * @param TypeProgram Indicate the way to program at a specified address.
  226. * This parameter can be a value of @ref FLASH_Type_Program
  227. * @param Address Specifie the address to be programmed.
  228. * @param Data Specifie the data to be programmed
  229. *
  230. * @retval HAL_StatusTypeDef HAL Status
  231. */
  232. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  233. {
  234. HAL_StatusTypeDef status = HAL_OK;
  235. /* Process Locked */
  236. __HAL_LOCK(&pFlash);
  237. /* Check the parameters */
  238. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  239. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  240. /* Enable End of FLASH Operation and Error source interrupts */
  241. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  242. pFlash.Address = Address;
  243. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  244. /* Clean the error context */
  245. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  246. if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  247. {
  248. /* Program word (32-bit) at a specified address. */
  249. *(__IO uint32_t *)Address = Data;
  250. }
  251. return status;
  252. }
  253. /**
  254. * @brief This function handles FLASH interrupt request.
  255. * @retval None
  256. */
  257. void HAL_FLASH_IRQHandler(void)
  258. {
  259. uint32_t addresstmp = 0U;
  260. /* Check FLASH operation error flags */
  261. if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  262. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) ||
  263. __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) ||
  264. #if defined(FLASH_SR_RDERR)
  265. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  266. #endif /* FLASH_SR_RDERR */
  267. #if defined(FLASH_SR_OPTVERRUSR)
  268. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  269. #endif /* FLASH_SR_OPTVERRUSR */
  270. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) )
  271. {
  272. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  273. {
  274. /* Return the faulty sector */
  275. addresstmp = pFlash.Page;
  276. pFlash.Page = 0xFFFFFFFFU;
  277. }
  278. else
  279. {
  280. /* Return the faulty address */
  281. addresstmp = pFlash.Address;
  282. }
  283. /* Save the Error code */
  284. FLASH_SetErrorCode();
  285. /* FLASH error interrupt user callback */
  286. HAL_FLASH_OperationErrorCallback(addresstmp);
  287. /* Stop the procedure ongoing */
  288. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  289. }
  290. /* Check FLASH End of Operation flag */
  291. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  292. {
  293. /* Clear FLASH End of Operation pending bit */
  294. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  295. /* Process can continue only if no error detected */
  296. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  297. {
  298. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  299. {
  300. /* Nb of pages to erased can be decreased */
  301. pFlash.NbPagesToErase--;
  302. /* Check if there are still pages to erase */
  303. if(pFlash.NbPagesToErase != 0U)
  304. {
  305. addresstmp = pFlash.Page;
  306. /*Indicate user which sector has been erased */
  307. HAL_FLASH_EndOfOperationCallback(addresstmp);
  308. /*Increment sector number*/
  309. addresstmp = pFlash.Page + FLASH_PAGE_SIZE;
  310. pFlash.Page = addresstmp;
  311. /* If the erase operation is completed, disable the ERASE Bit */
  312. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  313. FLASH_PageErase(addresstmp);
  314. }
  315. else
  316. {
  317. /* No more pages to Erase, user callback can be called. */
  318. /* Reset Sector and stop Erase pages procedure */
  319. pFlash.Page = addresstmp = 0xFFFFFFFFU;
  320. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  321. /* FLASH EOP interrupt user callback */
  322. HAL_FLASH_EndOfOperationCallback(addresstmp);
  323. }
  324. }
  325. else
  326. {
  327. /* If the program operation is completed, disable the PROG Bit */
  328. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  329. /* Program ended. Return the selected address */
  330. /* FLASH EOP interrupt user callback */
  331. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  332. /* Reset Address and stop Program procedure */
  333. pFlash.Address = 0xFFFFFFFFU;
  334. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  335. }
  336. }
  337. }
  338. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  339. {
  340. /* Operation is completed, disable the PROG and ERASE */
  341. CLEAR_BIT(FLASH->PECR, (FLASH_PECR_ERASE | FLASH_PECR_PROG));
  342. /* Disable End of FLASH Operation and Error source interrupts */
  343. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  344. /* Process Unlocked */
  345. __HAL_UNLOCK(&pFlash);
  346. }
  347. }
  348. /**
  349. * @brief FLASH end of operation interrupt callback
  350. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  351. * - Pages Erase: Address of the page which has been erased
  352. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  353. * - Program: Address which was selected for data program
  354. * @retval none
  355. */
  356. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  357. {
  358. /* Prevent unused argument(s) compilation warning */
  359. UNUSED(ReturnValue);
  360. /* NOTE : This function Should not be modified, when the callback is needed,
  361. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  362. */
  363. }
  364. /**
  365. * @brief FLASH operation error interrupt callback
  366. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  367. * - Pages Erase: Address of the page which returned an error
  368. * - Program: Address which was selected for data program
  369. * @retval none
  370. */
  371. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  372. {
  373. /* Prevent unused argument(s) compilation warning */
  374. UNUSED(ReturnValue);
  375. /* NOTE : This function Should not be modified, when the callback is needed,
  376. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  377. */
  378. }
  379. /**
  380. * @}
  381. */
  382. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  383. * @brief management functions
  384. *
  385. @verbatim
  386. ===============================================================================
  387. ##### Peripheral Control functions #####
  388. ===============================================================================
  389. [..]
  390. This subsection provides a set of functions allowing to control the FLASH
  391. memory operations.
  392. @endverbatim
  393. * @{
  394. */
  395. /**
  396. * @brief Unlock the FLASH control register access
  397. * @retval HAL Status
  398. */
  399. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  400. {
  401. if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PRGLOCK))
  402. {
  403. /* Unlocking FLASH_PECR register access*/
  404. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  405. {
  406. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1);
  407. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2);
  408. /* Verify that PELOCK is unlocked */
  409. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  410. {
  411. return HAL_ERROR;
  412. }
  413. }
  414. /* Unlocking the program memory access */
  415. WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY1);
  416. WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY2);
  417. /* Verify that PRGLOCK is unlocked */
  418. if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PRGLOCK))
  419. {
  420. return HAL_ERROR;
  421. }
  422. }
  423. return HAL_OK;
  424. }
  425. /**
  426. * @brief Locks the FLASH control register access
  427. * @retval HAL Status
  428. */
  429. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  430. {
  431. /* Set the PRGLOCK Bit to lock the FLASH Registers access */
  432. SET_BIT(FLASH->PECR, FLASH_PECR_PRGLOCK);
  433. return HAL_OK;
  434. }
  435. /**
  436. * @brief Unlock the FLASH Option Control Registers access.
  437. * @retval HAL Status
  438. */
  439. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  440. {
  441. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_OPTLOCK))
  442. {
  443. /* Unlocking FLASH_PECR register access*/
  444. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  445. {
  446. /* Unlocking FLASH_PECR register access*/
  447. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1);
  448. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2);
  449. /* Verify that PELOCK is unlocked */
  450. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  451. {
  452. return HAL_ERROR;
  453. }
  454. }
  455. /* Unlocking the option bytes block access */
  456. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  457. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  458. /* Verify that OPTLOCK is unlocked */
  459. if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_OPTLOCK))
  460. {
  461. return HAL_ERROR;
  462. }
  463. }
  464. return HAL_OK;
  465. }
  466. /**
  467. * @brief Lock the FLASH Option Control Registers access.
  468. * @retval HAL Status
  469. */
  470. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  471. {
  472. /* Set the OPTLOCK Bit to lock the option bytes block access */
  473. SET_BIT(FLASH->PECR, FLASH_PECR_OPTLOCK);
  474. return HAL_OK;
  475. }
  476. /**
  477. * @brief Launch the option byte loading.
  478. * @note This function will reset automatically the MCU.
  479. * @retval HAL Status
  480. */
  481. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  482. {
  483. /* Set the OBL_Launch bit to launch the option byte loading */
  484. SET_BIT(FLASH->PECR, FLASH_PECR_OBL_LAUNCH);
  485. /* Wait for last operation to be completed */
  486. return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE));
  487. }
  488. /**
  489. * @}
  490. */
  491. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
  492. * @brief Peripheral errors functions
  493. *
  494. @verbatim
  495. ===============================================================================
  496. ##### Peripheral Errors functions #####
  497. ===============================================================================
  498. [..]
  499. This subsection permit to get in run-time errors of the FLASH peripheral.
  500. @endverbatim
  501. * @{
  502. */
  503. /**
  504. * @brief Get the specific FLASH error flag.
  505. * @retval FLASH_ErrorCode The returned value can be:
  506. * @ref FLASH_Error_Codes
  507. */
  508. uint32_t HAL_FLASH_GetError(void)
  509. {
  510. return pFlash.ErrorCode;
  511. }
  512. /**
  513. * @}
  514. */
  515. /**
  516. * @}
  517. */
  518. /** @addtogroup FLASH_Private_Functions
  519. * @{
  520. */
  521. /**
  522. * @brief Wait for a FLASH operation to complete.
  523. * @param Timeout maximum flash operation timeout
  524. * @retval HAL Status
  525. */
  526. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  527. {
  528. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  529. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  530. flag will be set */
  531. uint32_t tickstart = HAL_GetTick();
  532. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  533. {
  534. if (Timeout != HAL_MAX_DELAY)
  535. {
  536. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  537. {
  538. return HAL_TIMEOUT;
  539. }
  540. }
  541. }
  542. /* Check FLASH End of Operation flag */
  543. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  544. {
  545. /* Clear FLASH End of Operation pending bit */
  546. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  547. }
  548. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  549. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  550. #if defined(FLASH_SR_RDERR)
  551. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  552. #endif /* FLASH_SR_RDERR */
  553. #if defined(FLASH_SR_OPTVERRUSR)
  554. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  555. #endif /* FLASH_SR_OPTVERRUSR */
  556. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  557. {
  558. /*Save the error code*/
  559. FLASH_SetErrorCode();
  560. return HAL_ERROR;
  561. }
  562. /* There is no error flag set */
  563. return HAL_OK;
  564. }
  565. /**
  566. * @brief Set the specific FLASH error flag.
  567. * @retval None
  568. */
  569. static void FLASH_SetErrorCode(void)
  570. {
  571. uint32_t flags = 0U;
  572. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  573. {
  574. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  575. flags |= FLASH_FLAG_WRPERR;
  576. }
  577. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  578. {
  579. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  580. flags |= FLASH_FLAG_PGAERR;
  581. }
  582. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  583. {
  584. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  585. flags |= FLASH_FLAG_OPTVERR;
  586. }
  587. #if defined(FLASH_SR_RDERR)
  588. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  589. {
  590. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  591. flags |= FLASH_FLAG_RDERR;
  592. }
  593. #endif /* FLASH_SR_RDERR */
  594. #if defined(FLASH_SR_OPTVERRUSR)
  595. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR))
  596. {
  597. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTVUSR;
  598. flags |= FLASH_FLAG_OPTVERRUSR;
  599. }
  600. #endif /* FLASH_SR_OPTVERRUSR */
  601. /* Clear FLASH error pending bits */
  602. __HAL_FLASH_CLEAR_FLAG(flags);
  603. }
  604. /**
  605. * @}
  606. */
  607. /**
  608. * @}
  609. */
  610. #endif /* HAL_FLASH_MODULE_ENABLED */
  611. /**
  612. * @}
  613. */
  614. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/