stm32g4xx_hal_flash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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 Errors 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 and cache lines.
  21. [..] The FLASH main features are:
  22. (+) Flash memory read operations
  23. (+) Flash memory program/erase operations
  24. (+) Read / write protections
  25. (+) Option bytes programming
  26. (+) Prefetch on I-Code
  27. (+) 32 cache lines of 4*64 or 2*128 bits on I-Code
  28. (+) 8 cache lines of 4*64 or 2*128 bits on D-Code
  29. (+) Error code correction (ECC) : Data in flash are 72-bits word
  30. (8 bits added per double word)
  31. ##### How to use this driver #####
  32. ==============================================================================
  33. [..]
  34. This driver provides functions and macros to configure and program the FLASH
  35. memory of all STM32G4xx devices.
  36. (#) Flash Memory IO Programming functions:
  37. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  38. HAL_FLASH_Lock() functions
  39. (++) Program functions: double word and fast program (full row programming)
  40. (++) There are two modes of programming :
  41. (+++) Polling mode using HAL_FLASH_Program() function
  42. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  43. (#) Interrupts and flags management functions:
  44. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  45. (++) Callback functions are called when the flash operations are finished :
  46. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  47. HAL_FLASH_OperationErrorCallback()
  48. (++) Get error flag status by calling HAL_GetError()
  49. (#) Option bytes management functions:
  50. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  51. HAL_FLASH_OB_Lock() functions
  52. (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
  53. In this case, a reset is generated
  54. [..]
  55. In addition to these functions, this driver includes a set of macros allowing
  56. to handle the following operations:
  57. (+) Set the latency
  58. (+) Enable/Disable the prefetch buffer
  59. (+) Enable/Disable the Instruction cache and the Data cache
  60. (+) Reset the Instruction cache and the Data cache
  61. (+) Enable/Disable the Flash power-down during low-power run and sleep modes
  62. (+) Enable/Disable the Flash interrupts
  63. (+) Monitor the Flash flags status
  64. @endverbatim
  65. ******************************************************************************
  66. * @attention
  67. *
  68. * Copyright (c) 2019 STMicroelectronics.
  69. * All rights reserved.
  70. *
  71. * This software is licensed under terms that can be found in the LICENSE file in
  72. * the root directory of this software component.
  73. * If no LICENSE file comes with this software, it is provided AS-IS.
  74. ******************************************************************************
  75. */
  76. /* Includes ------------------------------------------------------------------*/
  77. #include "stm32g4xx_hal.h"
  78. /** @addtogroup STM32G4xx_HAL_Driver
  79. * @{
  80. */
  81. /** @defgroup FLASH FLASH
  82. * @brief FLASH HAL module driver
  83. * @{
  84. */
  85. #ifdef HAL_FLASH_MODULE_ENABLED
  86. /* Private typedef -----------------------------------------------------------*/
  87. /* Private defines -----------------------------------------------------------*/
  88. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  89. * @{
  90. */
  91. #define FLASH_NB_DOUBLE_WORDS_IN_ROW 32
  92. /**
  93. * @}
  94. */
  95. /* Private macros ------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  98. * @{
  99. */
  100. /**
  101. * @brief Variable used for Program/Erase sectors under interruption
  102. */
  103. FLASH_ProcessTypeDef pFlash = {.Lock = HAL_UNLOCKED,
  104. .ErrorCode = HAL_FLASH_ERROR_NONE,
  105. .ProcedureOnGoing = FLASH_PROC_NONE,
  106. .Address = 0U,
  107. .Bank = FLASH_BANK_1,
  108. .Page = 0U,
  109. .NbPagesToErase = 0U,
  110. .CacheToReactivate = FLASH_CACHE_DISABLED};
  111. /**
  112. * @}
  113. */
  114. /* Private function prototypes -----------------------------------------------*/
  115. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  116. * @{
  117. */
  118. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  119. static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);
  120. /**
  121. * @}
  122. */
  123. /* Exported functions --------------------------------------------------------*/
  124. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  125. * @{
  126. */
  127. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  128. * @brief Programming operation functions
  129. *
  130. @verbatim
  131. ===============================================================================
  132. ##### Programming operation functions #####
  133. ===============================================================================
  134. [..]
  135. This subsection provides a set of functions allowing to manage the FLASH
  136. program operations.
  137. @endverbatim
  138. * @{
  139. */
  140. /**
  141. * @brief Program double word or fast program of a row at a specified address.
  142. * @param TypeProgram Indicate the way to program at a specified address.
  143. * This parameter can be a value of @ref FLASH_Type_Program.
  144. * @param Address specifies the address to be programmed.
  145. * @param Data specifies the data to be programmed.
  146. * This parameter is the data for the double word program and the address where
  147. * are stored the data for the row fast program.
  148. *
  149. * @retval HAL_Status
  150. */
  151. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  152. {
  153. HAL_StatusTypeDef status;
  154. uint32_t prog_bit = 0;
  155. /* Check the parameters */
  156. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  157. /* Process Locked */
  158. __HAL_LOCK(&pFlash);
  159. /* Wait for last operation to be completed */
  160. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  161. if (status == HAL_OK)
  162. {
  163. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  164. /* Deactivate the data cache if they are activated to avoid data misbehavior */
  165. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  166. {
  167. /* Disable data cache */
  168. __HAL_FLASH_DATA_CACHE_DISABLE();
  169. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  170. }
  171. else
  172. {
  173. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  174. }
  175. if (TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
  176. {
  177. /* Program double-word (64-bit) at a specified address */
  178. FLASH_Program_DoubleWord(Address, Data);
  179. prog_bit = FLASH_CR_PG;
  180. }
  181. else if ((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  182. {
  183. /* Fast program a 32 row double-word (64-bit) at a specified address */
  184. FLASH_Program_Fast(Address, (uint32_t)Data);
  185. /* If it is the last row, the bit will be cleared at the end of the operation */
  186. if (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
  187. {
  188. prog_bit = FLASH_CR_FSTPG;
  189. }
  190. }
  191. else
  192. {
  193. /* Nothing to do */
  194. }
  195. /* Wait for last operation to be completed */
  196. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  197. /* If the program operation is completed, disable the PG or FSTPG Bit */
  198. if (prog_bit != 0U)
  199. {
  200. CLEAR_BIT(FLASH->CR, prog_bit);
  201. }
  202. /* Flush the caches to be sure of the data consistency */
  203. FLASH_FlushCaches();
  204. }
  205. /* Process Unlocked */
  206. __HAL_UNLOCK(&pFlash);
  207. /* return status */
  208. return status;
  209. }
  210. /**
  211. * @brief Program double word or fast program of a row at a specified address with interrupt enabled.
  212. * @param TypeProgram Indicate the way to program at a specified address.
  213. * This parameter can be a value of @ref FLASH_Type_Program.
  214. * @param Address specifies the address to be programmed.
  215. * @param Data specifies the data to be programmed.
  216. * This parameter is the data for the double word program and the address where
  217. * are stored the data for the row fast program.
  218. *
  219. * @retval HAL_Status
  220. */
  221. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  222. {
  223. HAL_StatusTypeDef status;
  224. /* Check the parameters */
  225. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  226. /* Process Locked */
  227. __HAL_LOCK(&pFlash);
  228. /* Reset error code */
  229. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  230. /* Deactivate the data cache if they are activated to avoid data misbehavior */
  231. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  232. {
  233. /* Disable data cache */
  234. __HAL_FLASH_DATA_CACHE_DISABLE();
  235. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  236. }
  237. else
  238. {
  239. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  240. }
  241. /* Wait for last operation to be completed */
  242. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  243. if (status != HAL_OK)
  244. {
  245. /* Process Unlocked */
  246. __HAL_UNLOCK(&pFlash);
  247. }
  248. else
  249. {
  250. /* Set internal variables used by the IRQ handler */
  251. if (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
  252. {
  253. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;
  254. }
  255. else
  256. {
  257. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  258. }
  259. pFlash.Address = Address;
  260. /* Enable End of Operation and Error interrupts */
  261. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  262. if (TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
  263. {
  264. /* Program double-word (64-bit) at a specified address */
  265. FLASH_Program_DoubleWord(Address, Data);
  266. }
  267. else if ((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  268. {
  269. /* Fast program a 32 row double-word (64-bit) at a specified address */
  270. FLASH_Program_Fast(Address, (uint32_t)Data);
  271. }
  272. else
  273. {
  274. /* Nothing to do */
  275. }
  276. }
  277. return status;
  278. }
  279. /**
  280. * @brief Handle FLASH interrupt request.
  281. * @retval None
  282. */
  283. void HAL_FLASH_IRQHandler(void)
  284. {
  285. uint32_t tmp_page;
  286. uint32_t error;
  287. FLASH_ProcedureTypeDef procedure;
  288. /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */
  289. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));
  290. #if defined (FLASH_OPTR_DBANK)
  291. CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);
  292. #endif
  293. /* Disable the FSTPG Bit only if it is the last row programmed */
  294. if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
  295. {
  296. CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
  297. }
  298. /* Check FLASH operation error flags */
  299. error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
  300. if (error != 0U)
  301. {
  302. /* Save the error code */
  303. pFlash.ErrorCode |= error;
  304. /* Clear error programming flags */
  305. __HAL_FLASH_CLEAR_FLAG(error);
  306. /* Flush the caches to be sure of the data consistency */
  307. FLASH_FlushCaches();
  308. /* FLASH error interrupt user callback */
  309. procedure = pFlash.ProcedureOnGoing;
  310. if (procedure == FLASH_PROC_PAGE_ERASE)
  311. {
  312. HAL_FLASH_OperationErrorCallback(pFlash.Page);
  313. }
  314. else if (procedure == FLASH_PROC_MASS_ERASE)
  315. {
  316. HAL_FLASH_OperationErrorCallback(pFlash.Bank);
  317. }
  318. else if ((procedure == FLASH_PROC_PROGRAM) ||
  319. (procedure == FLASH_PROC_PROGRAM_LAST))
  320. {
  321. HAL_FLASH_OperationErrorCallback(pFlash.Address);
  322. }
  323. else
  324. {
  325. /* Nothing to do */
  326. }
  327. /*Stop the procedure ongoing*/
  328. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  329. }
  330. /* Check FLASH End of Operation flag */
  331. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  332. {
  333. /* Clear FLASH End of Operation pending bit */
  334. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  335. if (pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
  336. {
  337. /* Nb of pages to erased can be decreased */
  338. pFlash.NbPagesToErase--;
  339. /* Check if there are still pages to erase*/
  340. if (pFlash.NbPagesToErase != 0U)
  341. {
  342. /* Indicate user which page has been erased*/
  343. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  344. /* Increment page number */
  345. pFlash.Page++;
  346. tmp_page = pFlash.Page;
  347. FLASH_PageErase(tmp_page, pFlash.Bank);
  348. }
  349. else
  350. {
  351. /* No more pages to Erase */
  352. /* Reset Address and stop Erase pages procedure */
  353. pFlash.Page = 0xFFFFFFFFU;
  354. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  355. /* Flush the caches to be sure of the data consistency */
  356. FLASH_FlushCaches();
  357. /* FLASH EOP interrupt user callback */
  358. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  359. }
  360. }
  361. else
  362. {
  363. /* Flush the caches to be sure of the data consistency */
  364. FLASH_FlushCaches();
  365. procedure = pFlash.ProcedureOnGoing;
  366. if (procedure == FLASH_PROC_MASS_ERASE)
  367. {
  368. /* MassErase ended. Return the selected bank */
  369. /* FLASH EOP interrupt user callback */
  370. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  371. }
  372. else if ((procedure == FLASH_PROC_PROGRAM) ||
  373. (procedure == FLASH_PROC_PROGRAM_LAST))
  374. {
  375. /* Program ended. Return the selected address */
  376. /* FLASH EOP interrupt user callback */
  377. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  378. }
  379. else
  380. {
  381. /* Nothing to do */
  382. }
  383. /*Clear the procedure ongoing*/
  384. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  385. }
  386. }
  387. if (pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  388. {
  389. /* Disable End of Operation and Error interrupts */
  390. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  391. /* Process Unlocked */
  392. __HAL_UNLOCK(&pFlash);
  393. }
  394. }
  395. /**
  396. * @brief FLASH end of operation interrupt callback.
  397. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure:
  398. * @arg Mass Erase: Bank number which has been requested to erase
  399. * @arg Page Erase: Page which has been erased
  400. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  401. * @arg Program: Address which was selected for data program
  402. * @retval None
  403. */
  404. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  405. {
  406. /* Prevent unused argument(s) compilation warning */
  407. UNUSED(ReturnValue);
  408. /* NOTE : This function should not be modified, when the callback is needed,
  409. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  410. */
  411. }
  412. /**
  413. * @brief FLASH operation error interrupt callback.
  414. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure:
  415. * @arg Mass Erase: Bank number which has been requested to erase
  416. * @arg Page Erase: Page number which returned an error
  417. * @arg Program: Address which was selected for data program
  418. * @retval None
  419. */
  420. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  421. {
  422. /* Prevent unused argument(s) compilation warning */
  423. UNUSED(ReturnValue);
  424. /* NOTE : This function should not be modified, when the callback is needed,
  425. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  426. */
  427. }
  428. /**
  429. * @}
  430. */
  431. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  432. * @brief Management functions
  433. *
  434. @verbatim
  435. ===============================================================================
  436. ##### Peripheral Control functions #####
  437. ===============================================================================
  438. [..]
  439. This subsection provides a set of functions allowing to control the FLASH
  440. memory operations.
  441. @endverbatim
  442. * @{
  443. */
  444. /**
  445. * @brief Unlock the FLASH control register access.
  446. * @retval HAL_Status
  447. */
  448. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  449. {
  450. HAL_StatusTypeDef status = HAL_OK;
  451. if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
  452. {
  453. /* Authorize the FLASH Registers access */
  454. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  455. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  456. /* verify Flash is unlocked */
  457. if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
  458. {
  459. status = HAL_ERROR;
  460. }
  461. }
  462. return status;
  463. }
  464. /**
  465. * @brief Lock the FLASH control register access.
  466. * @retval HAL_Status
  467. */
  468. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  469. {
  470. HAL_StatusTypeDef status = HAL_ERROR;
  471. /* Set the LOCK Bit to lock the FLASH Registers access */
  472. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  473. /* verify Flash is locked */
  474. if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
  475. {
  476. status = HAL_OK;
  477. }
  478. return status;
  479. }
  480. /**
  481. * @brief Unlock the FLASH Option Bytes Registers access.
  482. * @retval HAL_Status
  483. */
  484. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  485. {
  486. HAL_StatusTypeDef status = HAL_OK;
  487. if (READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)
  488. {
  489. /* Authorizes the Option Byte register programming */
  490. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  491. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  492. /* verify option bytes are unlocked */
  493. if (READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)
  494. {
  495. status = HAL_ERROR;
  496. }
  497. }
  498. return status;
  499. }
  500. /**
  501. * @brief Lock the FLASH Option Bytes Registers access.
  502. * @retval HAL_Status
  503. */
  504. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  505. {
  506. HAL_StatusTypeDef status = HAL_ERROR;
  507. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  508. SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);
  509. /* Verify option bytes are locked */
  510. if (READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)
  511. {
  512. status = HAL_OK;
  513. }
  514. return status;
  515. }
  516. /**
  517. * @brief Launch the option byte loading.
  518. * @retval HAL_Status
  519. */
  520. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  521. {
  522. /* Set the bit to force the option byte reloading */
  523. SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
  524. /* Wait for last operation to be completed */
  525. return (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
  526. }
  527. /**
  528. * @}
  529. */
  530. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  531. * @brief Peripheral Errors functions
  532. *
  533. @verbatim
  534. ===============================================================================
  535. ##### Peripheral Errors functions #####
  536. ===============================================================================
  537. [..]
  538. This subsection permits to get in run-time Errors of the FLASH peripheral.
  539. @endverbatim
  540. * @{
  541. */
  542. /**
  543. * @brief Get the specific FLASH error flag.
  544. * @retval FLASH_ErrorCode. The returned value can be:
  545. * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
  546. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
  547. * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
  548. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  549. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
  550. * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
  551. * @arg HAL_FLASH_ERROR_NONE: No error set
  552. * @arg HAL_FLASH_ERROR_OP: FLASH Operation error
  553. * @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
  554. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
  555. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
  556. * @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
  557. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
  558. * @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error
  559. * @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error
  560. * @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error
  561. * @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error
  562. */
  563. uint32_t HAL_FLASH_GetError(void)
  564. {
  565. return pFlash.ErrorCode;
  566. }
  567. /**
  568. * @}
  569. */
  570. /**
  571. * @}
  572. */
  573. /* Private functions ---------------------------------------------------------*/
  574. /** @addtogroup FLASH_Private_Functions
  575. * @{
  576. */
  577. /**
  578. * @brief Wait for a FLASH operation to complete.
  579. * @param Timeout maximum flash operation timeout.
  580. * @retval HAL_Status
  581. */
  582. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  583. {
  584. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  585. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  586. flag will be set */
  587. uint32_t tickstart = HAL_GetTick();
  588. uint32_t error;
  589. while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  590. {
  591. if ((HAL_GetTick() - tickstart) > Timeout)
  592. {
  593. return HAL_TIMEOUT;
  594. }
  595. }
  596. /* Check FLASH operation error flags */
  597. error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
  598. if (error != 0u)
  599. {
  600. /* Save the error code */
  601. pFlash.ErrorCode |= error;
  602. /* Clear error programming flags */
  603. __HAL_FLASH_CLEAR_FLAG(error);
  604. return HAL_ERROR;
  605. }
  606. /* Check FLASH End of Operation flag */
  607. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  608. {
  609. /* Clear FLASH End of Operation pending bit */
  610. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  611. }
  612. /* If there is an error flag set */
  613. return HAL_OK;
  614. }
  615. /**
  616. * @brief Program double-word (64-bit) at a specified address.
  617. * @param Address specifies the address to be programmed.
  618. * @param Data specifies the data to be programmed.
  619. * @retval None
  620. */
  621. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  622. {
  623. /* Check the parameters */
  624. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  625. /* Set PG bit */
  626. SET_BIT(FLASH->CR, FLASH_CR_PG);
  627. /* Program first word */
  628. *(uint32_t *)Address = (uint32_t)Data;
  629. /* Barrier to ensure programming is performed in 2 steps, in right order
  630. (independently of compiler optimization behavior) */
  631. __ISB();
  632. /* Program second word */
  633. *(uint32_t *)(Address + 4U) = (uint32_t)(Data >> 32U);
  634. }
  635. /**
  636. * @brief Fast program a row double-word (64-bit) at a specified address.
  637. * @param Address specifies the address to be programmed.
  638. * @param DataAddress specifies the address where the data are stored.
  639. * @retval None
  640. */
  641. static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
  642. {
  643. uint8_t row_index = (2 * FLASH_NB_DOUBLE_WORDS_IN_ROW);
  644. uint32_t *dest_addr = (uint32_t *)Address;
  645. uint32_t *src_addr = (uint32_t *)DataAddress;
  646. uint32_t primask_bit;
  647. /* Check the parameters */
  648. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
  649. /* Set FSTPG bit */
  650. SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
  651. /* Enter critical section: Disable interrupts to avoid any interruption during the loop */
  652. primask_bit = __get_PRIMASK();
  653. __disable_irq();
  654. /* Program the double words of the row */
  655. do
  656. {
  657. *dest_addr = *src_addr;
  658. dest_addr++;
  659. src_addr++;
  660. row_index--;
  661. }
  662. while (row_index != 0U);
  663. /* Exit critical section: restore previous priority mask */
  664. __set_PRIMASK(primask_bit);
  665. }
  666. /**
  667. * @}
  668. */
  669. #endif /* HAL_FLASH_MODULE_ENABLED */
  670. /**
  671. * @}
  672. */
  673. /**
  674. * @}
  675. */