stm32g0xx_hal_flash.c 22 KB

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