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.
 
 
 

774 lines
25 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  7. * @brief FLASH HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the internal FLASH memory:
  10. * + Program operations functions
  11. * + Memory Control functions
  12. * + Peripheral Errors functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### FLASH peripheral features #####
  17. ==============================================================================
  18. [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
  19. to the Flash memory. It implements the erase and program Flash memory operations
  20. and the read and write protection mechanisms.
  21. [..] The Flash memory interface accelerates code execution with a system of instruction
  22. prefetch and cache lines.
  23. [..] The FLASH main features are:
  24. (+) Flash memory read operations
  25. (+) Flash memory program/erase operations
  26. (+) Read / write protections
  27. (+) Option bytes programming
  28. (+) Prefetch on I-Code
  29. (+) 32 cache lines of 4*64 bits on I-Code
  30. (+) 8 cache lines of 4*64 bits on D-Code
  31. (+) Error code correction (ECC) : Data in flash are 72-bits word
  32. (8 bits added per double word)
  33. ##### How to use this driver #####
  34. ==============================================================================
  35. [..]
  36. This driver provides functions and macros to configure and program the FLASH
  37. memory of all STM32L4xx devices.
  38. (#) Flash Memory IO Programming functions:
  39. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  40. HAL_FLASH_Lock() functions
  41. (++) Program functions: double word and fast program (full row programming)
  42. (++) There Two modes of programming :
  43. (+++) Polling mode using HAL_FLASH_Program() function
  44. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  45. (#) Interrupts and flags management functions :
  46. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  47. (++) Callback functions are called when the flash operations are finished :
  48. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  49. HAL_FLASH_OperationErrorCallback()
  50. (++) Get error flag status by calling HAL_GetError()
  51. (#) Option bytes management functions :
  52. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  53. HAL_FLASH_OB_Lock() functions
  54. (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
  55. In this case, a reset is generated
  56. [..]
  57. In addition to these functions, this driver includes a set of macros allowing
  58. to handle the following operations:
  59. (+) Set the latency
  60. (+) Enable/Disable the prefetch buffer
  61. (+) Enable/Disable the Instruction cache and the Data cache
  62. (+) Reset the Instruction cache and the Data cache
  63. (+) Enable/Disable the Flash power-down during low-power run and sleep modes
  64. (+) Enable/Disable the Flash interrupts
  65. (+) Monitor the Flash flags status
  66. @endverbatim
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  71. *
  72. * Redistribution and use in source and binary forms, with or without modification,
  73. * are permitted provided that the following conditions are met:
  74. * 1. Redistributions of source code must retain the above copyright notice,
  75. * this list of conditions and the following disclaimer.
  76. * 2. Redistributions in binary form must reproduce the above copyright notice,
  77. * this list of conditions and the following disclaimer in the documentation
  78. * and/or other materials provided with the distribution.
  79. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  80. * may be used to endorse or promote products derived from this software
  81. * without specific prior written permission.
  82. *
  83. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  84. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  85. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  86. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  87. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  88. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  91. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  92. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  93. *
  94. ******************************************************************************
  95. */
  96. /* Includes ------------------------------------------------------------------*/
  97. #include "stm32l4xx_hal.h"
  98. /** @addtogroup STM32L4xx_HAL_Driver
  99. * @{
  100. */
  101. /** @defgroup FLASH FLASH
  102. * @brief FLASH HAL module driver
  103. * @{
  104. */
  105. #ifdef HAL_FLASH_MODULE_ENABLED
  106. /* Private typedef -----------------------------------------------------------*/
  107. /* Private defines -----------------------------------------------------------*/
  108. /* Private macros ------------------------------------------------------------*/
  109. /* Private variables ---------------------------------------------------------*/
  110. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  111. * @{
  112. */
  113. /**
  114. * @brief Variable used for Program/Erase sectors under interruption
  115. */
  116. FLASH_ProcessTypeDef pFlash;
  117. /**
  118. * @}
  119. */
  120. /* Private function prototypes -----------------------------------------------*/
  121. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  122. * @{
  123. */
  124. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  125. extern void FLASH_PageErase(uint32_t Page, uint32_t Banks);
  126. extern void FLASH_FlushCaches(void);
  127. static void FLASH_SetErrorCode(void);
  128. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  129. static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);
  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. ===============================================================================
  142. ##### Programming operation functions #####
  143. ===============================================================================
  144. [..]
  145. This subsection provides a set of functions allowing to manage the FLASH
  146. program operations.
  147. @endverbatim
  148. * @{
  149. */
  150. /**
  151. * @brief Program double word or fast program of a row at a specified address.
  152. * @param TypeProgram: Indicate the way to program at a specified address.
  153. * This parameter can be a value of @ref FLASH_Type_Program
  154. * @param Address: specifies the address to be programmed.
  155. * @param Data: specifies the data to be programmed
  156. * This parameter is the data for the double word program and the address where
  157. * are stored the data for the row fast program
  158. *
  159. * @retval HAL_StatusTypeDef HAL Status
  160. */
  161. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  162. {
  163. HAL_StatusTypeDef status = HAL_ERROR;
  164. uint32_t prog_bit = 0;
  165. /* Process Locked */
  166. __HAL_LOCK(&pFlash);
  167. /* Check the parameters */
  168. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  169. /* Wait for last operation to be completed */
  170. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  171. if(status == HAL_OK)
  172. {
  173. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  174. if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
  175. {
  176. /* Program double-word (64-bit) at a specified address */
  177. FLASH_Program_DoubleWord(Address, Data);
  178. prog_bit = FLASH_CR_PG;
  179. }
  180. else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  181. {
  182. /* Fast program a 32 row double-word (64-bit) at a specified address */
  183. FLASH_Program_Fast(Address, (uint32_t)Data);
  184. /* If it is the last row, the bit will be cleared at the end of the operation */
  185. if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
  186. {
  187. prog_bit = FLASH_CR_FSTPG;
  188. }
  189. }
  190. /* Wait for last operation to be completed */
  191. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  192. /* If the program operation is completed, disable the PG or FSTPG Bit */
  193. if (prog_bit != 0)
  194. {
  195. CLEAR_BIT(FLASH->CR, prog_bit);
  196. }
  197. }
  198. /* Process Unlocked */
  199. __HAL_UNLOCK(&pFlash);
  200. return status;
  201. }
  202. /**
  203. * @brief Program double word or fast program of a row at a specified address with interrupt enabled.
  204. * @param TypeProgram: Indicate the way to program at a specified address.
  205. * This parameter can be a value of @ref FLASH_Type_Program
  206. * @param Address: specifies the address to be programmed.
  207. * @param Data: specifies the data to be programmed
  208. * This parameter is the data for the double word program and the address where
  209. * are stored the data for the row fast program
  210. *
  211. * @retval HAL Status
  212. */
  213. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  214. {
  215. HAL_StatusTypeDef status = HAL_OK;
  216. /* Check the parameters */
  217. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  218. /* Process Locked */
  219. __HAL_LOCK(&pFlash);
  220. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  221. /* Set internal variables used by the IRQ handler */
  222. if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
  223. {
  224. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;
  225. }
  226. else
  227. {
  228. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  229. }
  230. pFlash.Address = Address;
  231. /* Enable End of Operation and Error interrupts */
  232. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  233. if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
  234. {
  235. /* Program double-word (64-bit) at a specified address */
  236. FLASH_Program_DoubleWord(Address, Data);
  237. }
  238. else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  239. {
  240. /* Fast program a 32 row double-word (64-bit) at a specified address */
  241. FLASH_Program_Fast(Address, (uint32_t)Data);
  242. }
  243. return status;
  244. }
  245. /**
  246. * @brief Handle FLASH interrupt request.
  247. * @retval None
  248. */
  249. void HAL_FLASH_IRQHandler(void)
  250. {
  251. uint32_t tmp_page;
  252. /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */
  253. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));
  254. CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);
  255. /* Disable the FSTPG Bit only if it is the last row programmed */
  256. if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
  257. {
  258. CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
  259. }
  260. /* Check FLASH operation error flags */
  261. if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
  262. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
  263. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)) ||
  264. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) ||
  265. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
  266. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
  267. {
  268. /*Save the error code*/
  269. FLASH_SetErrorCode();
  270. /* FLASH error interrupt user callback */
  271. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
  272. {
  273. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  274. }
  275. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
  276. {
  277. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  278. }
  279. else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) ||
  280. (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
  281. {
  282. HAL_FLASH_OperationErrorCallback(pFlash.Address);
  283. }
  284. HAL_FLASH_OperationErrorCallback(pFlash.Address);
  285. /*Stop the procedure ongoing*/
  286. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  287. }
  288. /* Check FLASH End of Operation flag */
  289. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  290. {
  291. /* Clear FLASH End of Operation pending bit */
  292. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  293. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
  294. {
  295. /* Nb of pages to erased can be decreased */
  296. pFlash.NbPagesToErase--;
  297. /* Check if there are still pages to erase*/
  298. if(pFlash.NbPagesToErase != 0)
  299. {
  300. /* Indicate user which page has been erased*/
  301. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  302. /* Increment page number */
  303. pFlash.Page++;
  304. tmp_page = pFlash.Page;
  305. FLASH_PageErase(tmp_page, pFlash.Bank);
  306. }
  307. else
  308. {
  309. /* No more pages to Erase */
  310. /* Reset Address and stop Erase pages procedure */
  311. pFlash.Page = 0xFFFFFFFF;
  312. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  313. /* Flush the caches to be sure of the data consistency */
  314. FLASH_FlushCaches() ;
  315. /* FLASH EOP interrupt user callback */
  316. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  317. }
  318. }
  319. else
  320. {
  321. if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
  322. {
  323. /* MassErase ended. Return the selected bank */
  324. /* Flush the caches to be sure of the data consistency */
  325. FLASH_FlushCaches() ;
  326. /* FLASH EOP interrupt user callback */
  327. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  328. }
  329. else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) ||
  330. (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
  331. {
  332. /* Program ended. Return the selected address */
  333. /* FLASH EOP interrupt user callback */
  334. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  335. }
  336. /*Clear the procedure ongoing*/
  337. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  338. }
  339. }
  340. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  341. {
  342. /* Disable End of Operation and Error interrupts */
  343. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  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. * Mass Erase: Bank number which has been requested to erase
  352. * Page Erase: Page which has been erased
  353. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  354. * Program: Address which was selected for data program
  355. * @retval None
  356. */
  357. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  358. {
  359. /* Prevent unused argument(s) compilation warning */
  360. UNUSED(ReturnValue);
  361. /* NOTE : This function should not be modified, when the callback is needed,
  362. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  363. */
  364. }
  365. /**
  366. * @brief FLASH operation error interrupt callback.
  367. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  368. * Mass Erase: Bank number which has been requested to erase
  369. * Page Erase: Page number which returned an error
  370. * Program: Address which was selected for data program
  371. * @retval None
  372. */
  373. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  374. {
  375. /* Prevent unused argument(s) compilation warning */
  376. UNUSED(ReturnValue);
  377. /* NOTE : This function should not be modified, when the callback is needed,
  378. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  379. */
  380. }
  381. /**
  382. * @}
  383. */
  384. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  385. * @brief Management functions
  386. *
  387. @verbatim
  388. ===============================================================================
  389. ##### Peripheral Control functions #####
  390. ===============================================================================
  391. [..]
  392. This subsection provides a set of functions allowing to control the FLASH
  393. memory operations.
  394. @endverbatim
  395. * @{
  396. */
  397. /**
  398. * @brief Unlock the FLASH control register access.
  399. * @retval HAL Status
  400. */
  401. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  402. {
  403. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  404. {
  405. /* Authorize the FLASH Registers access */
  406. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  407. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  408. }
  409. else
  410. {
  411. return HAL_ERROR;
  412. }
  413. return HAL_OK;
  414. }
  415. /**
  416. * @brief Lock the FLASH control register access.
  417. * @retval HAL Status
  418. */
  419. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  420. {
  421. /* Set the LOCK Bit to lock the FLASH Registers access */
  422. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  423. return HAL_OK;
  424. }
  425. /**
  426. * @brief Unlock the FLASH Option Bytes Registers access.
  427. * @retval HAL Status
  428. */
  429. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  430. {
  431. if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != RESET)
  432. {
  433. /* Authorizes the Option Byte register programming */
  434. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  435. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  436. }
  437. else
  438. {
  439. return HAL_ERROR;
  440. }
  441. return HAL_OK;
  442. }
  443. /**
  444. * @brief Lock the FLASH Option Bytes Registers access.
  445. * @retval HAL Status
  446. */
  447. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  448. {
  449. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  450. SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);
  451. return HAL_OK;
  452. }
  453. /**
  454. * @brief Launch the option byte loading.
  455. * @retval HAL Status
  456. */
  457. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  458. {
  459. /* Set the bit to force the option byte reloading */
  460. SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
  461. /* Wait for last operation to be completed */
  462. return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
  463. }
  464. /**
  465. * @}
  466. */
  467. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  468. * @brief Peripheral Errors functions
  469. *
  470. @verbatim
  471. ===============================================================================
  472. ##### Peripheral Errors functions #####
  473. ===============================================================================
  474. [..]
  475. This subsection permits to get in run-time Errors of the FLASH peripheral.
  476. @endverbatim
  477. * @{
  478. */
  479. /**
  480. * @brief Get the specific FLASH error flag.
  481. * @retval FLASH_ErrorCode: The returned value can be:
  482. * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
  483. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
  484. * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
  485. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  486. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
  487. * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
  488. * @arg HAL_FLASH_ERROR_NONE: No error set
  489. * @arg HAL_FLASH_ERROR_OP: FLASH Operation error
  490. * @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
  491. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
  492. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
  493. * @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
  494. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
  495. * @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error
  496. * @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error
  497. * @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error
  498. * @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error
  499. * @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected
  500. */
  501. uint32_t HAL_FLASH_GetError(void)
  502. {
  503. return pFlash.ErrorCode;
  504. }
  505. /**
  506. * @}
  507. */
  508. /**
  509. * @}
  510. */
  511. /* Private functions ---------------------------------------------------------*/
  512. /** @addtogroup FLASH_Private_Functions
  513. * @{
  514. */
  515. /**
  516. * @brief Wait for a FLASH operation to complete.
  517. * @param Timeout: maximum flash operation timeout
  518. * @retval HAL_StatusTypeDef HAL Status
  519. */
  520. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  521. {
  522. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  523. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  524. flag will be set */
  525. uint32_t timeout = HAL_GetTick() + Timeout;
  526. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  527. {
  528. if(Timeout != HAL_MAX_DELAY)
  529. {
  530. if(HAL_GetTick() >= timeout)
  531. {
  532. return HAL_TIMEOUT;
  533. }
  534. }
  535. }
  536. if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
  537. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
  538. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)) ||
  539. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) ||
  540. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
  541. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
  542. {
  543. /*Save the error code*/
  544. FLASH_SetErrorCode();
  545. return HAL_ERROR;
  546. }
  547. /* Check FLASH End of Operation flag */
  548. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  549. {
  550. /* Clear FLASH End of Operation pending bit */
  551. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  552. }
  553. /* If there is an error flag set */
  554. return HAL_OK;
  555. }
  556. /**
  557. * @brief Set the specific FLASH error flag.
  558. * @retval None
  559. */
  560. static void FLASH_SetErrorCode(void)
  561. {
  562. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))
  563. {
  564. pFlash.ErrorCode |= HAL_FLASH_ERROR_OP;
  565. }
  566. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR))
  567. {
  568. pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
  569. }
  570. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  571. {
  572. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  573. }
  574. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  575. {
  576. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  577. }
  578. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
  579. {
  580. pFlash.ErrorCode |= HAL_FLASH_ERROR_SIZ;
  581. }
  582. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))
  583. {
  584. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
  585. }
  586. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR))
  587. {
  588. pFlash.ErrorCode |= HAL_FLASH_ERROR_MIS;
  589. }
  590. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR))
  591. {
  592. pFlash.ErrorCode |= HAL_FLASH_ERROR_FAST;
  593. }
  594. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  595. {
  596. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  597. }
  598. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  599. {
  600. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  601. }
  602. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD))
  603. {
  604. pFlash.ErrorCode |= HAL_FLASH_ERROR_ECCD;
  605. }
  606. /* Clear error programming flags */
  607. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  608. }
  609. /**
  610. * @brief Program double-word (64-bit) at a specified address.
  611. * @param Address: specifies the address to be programmed.
  612. * @param Data: specifies the data to be programmed.
  613. * @retval None
  614. */
  615. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  616. {
  617. /* Check the parameters */
  618. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  619. /* Set PG bit */
  620. SET_BIT(FLASH->CR, FLASH_CR_PG);
  621. /* Program the double word */
  622. *(__IO uint32_t*)Address = (uint32_t)Data;
  623. *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
  624. }
  625. /**
  626. * @brief Fast program a 32 row double-word (64-bit) at a specified address.
  627. * @param Address: specifies the address to be programmed.
  628. * @param DataAddress: specifies the address where the data are stored.
  629. * @retval None
  630. */
  631. static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
  632. {
  633. uint8_t row_index = 32;
  634. __IO uint64_t *dest_addr = (__IO uint64_t*)Address;
  635. __IO uint64_t *src_addr = (__IO uint64_t*)DataAddress;
  636. /* Check the parameters */
  637. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
  638. /* Set FSTPG bit */
  639. SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
  640. /* Disable interrupts to avoid any interruption during the loop */
  641. __disable_irq();
  642. /* Program the 32 double word */
  643. do
  644. {
  645. *dest_addr++ = *src_addr++;
  646. } while (--row_index != 0);
  647. /* Re-enable the interrupts */
  648. __enable_irq();
  649. }
  650. /**
  651. * @}
  652. */
  653. #endif /* HAL_FLASH_MODULE_ENABLED */
  654. /**
  655. * @}
  656. */
  657. /**
  658. * @}
  659. */
  660. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/