stm32l5xx_hal_flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /**
  2. ******************************************************************************
  3. * @file stm32l5xx_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. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2019 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file in
  19. * the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. ******************************************************************************
  22. @verbatim
  23. ==============================================================================
  24. ##### Flash peripheral features #####
  25. ==============================================================================
  26. [..] The Flash memory interface manages CPU AHB C-Bus accesses
  27. to the Flash memory. It implements the erase and program Flash memory operations
  28. and the read and write protection mechanisms.
  29. [..] The Flash memory interface implements the TrustZone security features (TZ) supported
  30. by ARM Cortex-M33 core (CM33).
  31. [..] The FLASH main features are:
  32. (+) Flash memory read operations
  33. (+) Flash memory program/erase operations
  34. (+) Read / write protections
  35. (+) Option bytes programming
  36. (+) TrustZone aware
  37. (+) Watermark-based area protection including secure hide area
  38. (+) Block-based page protection
  39. (+) Error code correction (ECC) : Data in flash are 72-bits word
  40. (8 bits added per double word)
  41. ##### How to use this driver #####
  42. ==============================================================================
  43. [..]
  44. This driver provides functions and macros to configure and program the FLASH
  45. memory of all STM32L5xx devices.
  46. (#) Flash Memory IO Programming functions:
  47. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  48. HAL_FLASH_Lock() functions
  49. (++) Program functions: double word
  50. (++) There Two modes of programming :
  51. (+++) Polling mode using HAL_FLASH_Program() function
  52. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  53. (#) Interrupts and flags management functions :
  54. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  55. (++) Callback functions are called when the flash operations are finished :
  56. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  57. HAL_FLASH_OperationErrorCallback()
  58. (++) Get error flag status by calling HAL_GetError()
  59. (#) Option bytes management functions :
  60. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  61. HAL_FLASH_OB_Lock() functions
  62. (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
  63. In this case, a reset is generated
  64. [..]
  65. In addition to these functions, this driver includes a set of macros allowing
  66. to handle the following operations:
  67. (+) Set the latency
  68. (+) Enable/Disable the Flash power-down during low-power run and sleep modes
  69. (+) Enable/Disable the Flash interrupts
  70. (+) Monitor the Flash flags status
  71. @endverbatim
  72. ******************************************************************************
  73. */
  74. /* Includes ------------------------------------------------------------------*/
  75. #include "stm32l5xx_hal.h"
  76. /** @addtogroup STM32L5xx_HAL_Driver
  77. * @{
  78. */
  79. /** @defgroup FLASH FLASH
  80. * @brief FLASH HAL module driver
  81. * @{
  82. */
  83. #ifdef HAL_FLASH_MODULE_ENABLED
  84. /* Private typedef -----------------------------------------------------------*/
  85. /* Private defines -----------------------------------------------------------*/
  86. /* Private macros ------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  89. * @{
  90. */
  91. /**
  92. * @brief Variable used for Program/Erase sectors under interruption
  93. */
  94. FLASH_ProcessTypeDef pFlash = {.Lock = HAL_UNLOCKED, \
  95. .ErrorCode = HAL_FLASH_ERROR_NONE, \
  96. .ProcedureOnGoing = 0U, \
  97. .Address = 0U, \
  98. .Bank = FLASH_BANK_1, \
  99. .Page = 0U, \
  100. .NbPagesToErase = 0U};
  101. /**
  102. * @}
  103. */
  104. /* Private function prototypes -----------------------------------------------*/
  105. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  106. * @{
  107. */
  108. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  109. /**
  110. * @}
  111. */
  112. /* Exported functions --------------------------------------------------------*/
  113. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  114. * @{
  115. */
  116. /** @defgroup FLASH_Exported_Functions_Group1 Programming Operation functions
  117. * @brief Programming Operation functions
  118. *
  119. @verbatim
  120. ===============================================================================
  121. ##### Programming Operation functions #####
  122. ===============================================================================
  123. [..]
  124. This subsection provides a set of functions allowing to manage the FLASH
  125. program operations.
  126. @endverbatim
  127. * @{
  128. */
  129. /**
  130. * @brief Program double word at a specified address.
  131. * @param TypeProgram Indicate the way to program at a specified address.
  132. * This parameter can be a value of @ref FLASH_Type_Program
  133. * @param Address specifies the address to be programmed.
  134. * @param Data specifies the data to be programmed
  135. * This parameter is the data for the double word program
  136. *
  137. * @retval HAL_StatusTypeDef HAL Status
  138. */
  139. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  140. {
  141. HAL_StatusTypeDef status;
  142. __IO uint32_t *reg;
  143. /* Check the parameters */
  144. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  145. /* Process Locked */
  146. __HAL_LOCK(&pFlash);
  147. /* Reset error code */
  148. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  149. /* Wait for last operation to be completed */
  150. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  151. if(status == HAL_OK)
  152. {
  153. pFlash.ProcedureOnGoing = TypeProgram;
  154. reg = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  155. /* Program double-word (64-bit) at a specified address */
  156. FLASH_Program_DoubleWord(Address, Data);
  157. /* Wait for last operation to be completed */
  158. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  159. /* If the program operation is completed, disable the PG Bit */
  160. CLEAR_BIT((*reg), (pFlash.ProcedureOnGoing & ~(FLASH_NON_SECURE_MASK)));
  161. }
  162. /* Process Unlocked */
  163. __HAL_UNLOCK(&pFlash);
  164. return status;
  165. }
  166. /**
  167. * @brief Program double word at a specified address with interrupt enabled.
  168. * @param TypeProgram Indicate the way to program at a specified address.
  169. * This parameter can be a value of @ref FLASH_Type_Program
  170. * @param Address specifies the address to be programmed.
  171. * @param Data specifies the data to be programmed
  172. * This parameter is the data for the double word program
  173. *
  174. * @retval HAL Status
  175. */
  176. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  177. {
  178. HAL_StatusTypeDef status;
  179. __IO uint32_t *reg_cr;
  180. /* Check the parameters */
  181. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  182. /* Process Locked */
  183. __HAL_LOCK(&pFlash);
  184. /* Reset error code */
  185. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  186. /* Wait for last operation to be completed */
  187. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  188. if (status != HAL_OK)
  189. {
  190. /* Process Unlocked */
  191. __HAL_UNLOCK(&pFlash);
  192. }
  193. else
  194. {
  195. /* Set internal variables used by the IRQ handler */
  196. pFlash.ProcedureOnGoing = TypeProgram;
  197. pFlash.Address = Address;
  198. /* Access to SECCR or NSCR depends on operation type */
  199. reg_cr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  200. /* Enable End of Operation and Error interrupts */
  201. (*reg_cr) |= (FLASH_IT_EOP | FLASH_IT_OPERR);
  202. /* Program double-word (64-bit) at a specified address */
  203. FLASH_Program_DoubleWord(Address, Data);
  204. }
  205. return status;
  206. }
  207. /**
  208. * @brief Handle FLASH interrupt request.
  209. * @retval None
  210. */
  211. void HAL_FLASH_IRQHandler(void)
  212. {
  213. uint32_t param = 0U;
  214. uint32_t error, type;
  215. __IO uint32_t *reg;
  216. __IO uint32_t *reg_sr;
  217. type = (pFlash.ProcedureOnGoing & ~(FLASH_NON_SECURE_MASK));
  218. reg = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  219. reg_sr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECSR) : &(FLASH_NS->NSSR);
  220. /* Save Flash errors */
  221. error = (*reg_sr) & FLASH_FLAG_SR_ERRORS;
  222. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  223. error |= (FLASH->NSSR & FLASH_FLAG_OPTWERR);
  224. #endif /* __ARM_FEATURE_CMSE */
  225. /* Set parameter of the callback */
  226. if(type == FLASH_TYPEERASE_PAGES)
  227. {
  228. param = pFlash.Page;
  229. }
  230. else if(type == FLASH_TYPEERASE_MASSERASE)
  231. {
  232. param = pFlash.Bank;
  233. }
  234. else if(type == FLASH_TYPEPROGRAM_DOUBLEWORD)
  235. {
  236. param = pFlash.Address;
  237. }
  238. else
  239. {
  240. /* Empty statement (to be compliant MISRA 15.7) */
  241. }
  242. /* Clear bit on the on-going procedure */
  243. CLEAR_BIT((*reg), type);
  244. /* Check FLASH operation error flags */
  245. if(error != 0U)
  246. {
  247. /* Save the error code */
  248. pFlash.ErrorCode |= error;
  249. /* Clear error programming flags */
  250. (*reg_sr) = error;
  251. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  252. if ((error & FLASH_FLAG_OPTWERR) != 0U)
  253. {
  254. FLASH->NSSR = FLASH_FLAG_OPTWERR;
  255. }
  256. #endif /* __ARM_FEATURE_CMSE */
  257. /* Stop the procedure ongoing */
  258. pFlash.ProcedureOnGoing = 0U;
  259. /* FLASH error interrupt user callback */
  260. HAL_FLASH_OperationErrorCallback(param);
  261. }
  262. /* Check FLASH End of Operation flag */
  263. if (((*reg_sr) & FLASH_FLAG_EOP) != 0U)
  264. {
  265. /* Clear FLASH End of Operation pending bit */
  266. (*reg_sr) = FLASH_FLAG_EOP;
  267. if(type == FLASH_TYPEERASE_PAGES)
  268. {
  269. /* Nb of pages to erased can be decreased */
  270. pFlash.NbPagesToErase--;
  271. /* Check if there are still pages to erase*/
  272. if(pFlash.NbPagesToErase != 0U)
  273. {
  274. /* Increment page number */
  275. pFlash.Page++;
  276. FLASH_PageErase(pFlash.Page, pFlash.Bank);
  277. }
  278. else
  279. {
  280. /* No more pages to Erase */
  281. pFlash.ProcedureOnGoing = 0U;
  282. param = 0xFFFFFFFFU;
  283. }
  284. }
  285. else
  286. {
  287. /*Clear the procedure ongoing*/
  288. pFlash.ProcedureOnGoing = 0U;
  289. }
  290. /* FLASH EOP interrupt user callback */
  291. HAL_FLASH_EndOfOperationCallback(param);
  292. }
  293. if(pFlash.ProcedureOnGoing == 0U)
  294. {
  295. /* Disable End of Operation and Error interrupts */
  296. (*reg) &= ~(FLASH_IT_EOP | FLASH_IT_OPERR);
  297. /* Process Unlocked */
  298. __HAL_UNLOCK(&pFlash);
  299. }
  300. }
  301. /**
  302. * @brief FLASH end of operation interrupt callback.
  303. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure :
  304. * @arg Mass Erase: Bank number which has been requested to erase
  305. * @arg Page Erase: Page which has been erased
  306. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  307. * @arg Program: Address which was selected for data program
  308. * @retval None
  309. */
  310. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  311. {
  312. /* Prevent unused argument(s) compilation warning */
  313. UNUSED(ReturnValue);
  314. /* NOTE : This function should not be modified, when the callback is needed,
  315. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  316. */
  317. }
  318. /**
  319. * @brief FLASH operation error interrupt callback.
  320. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure :
  321. * @arg Mass Erase: Bank number which has been requested to erase
  322. * @arg Page Erase: Page number which returned an error
  323. * @arg Program: Address which was selected for data program
  324. * @retval None
  325. */
  326. __weak void HAL_FLASH_OperationErrorCallback(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_OperationErrorCallback could be implemented in the user file
  332. */
  333. }
  334. /**
  335. * @}
  336. */
  337. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  338. * @brief Management functions
  339. *
  340. @verbatim
  341. ===============================================================================
  342. ##### Peripheral Control functions #####
  343. ===============================================================================
  344. [..]
  345. This subsection provides a set of functions allowing to control the FLASH
  346. memory operations.
  347. @endverbatim
  348. * @{
  349. */
  350. /**
  351. * @brief Unlock the FLASH control register access.
  352. * @retval HAL Status
  353. */
  354. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  355. {
  356. HAL_StatusTypeDef status = HAL_OK;
  357. if(READ_BIT(FLASH->NSCR, FLASH_NSCR_NSLOCK) != 0u)
  358. {
  359. /* Authorize the FLASH Registers access */
  360. WRITE_REG(FLASH->NSKEYR, FLASH_KEY1);
  361. WRITE_REG(FLASH->NSKEYR, FLASH_KEY2);
  362. /* verify Flash is unlocked */
  363. if (READ_BIT(FLASH->NSCR, FLASH_NSCR_NSLOCK) != 0u)
  364. {
  365. status = HAL_ERROR;
  366. }
  367. }
  368. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  369. if (status == HAL_OK)
  370. {
  371. if(READ_BIT(FLASH->SECCR, FLASH_SECCR_SECLOCK) != 0u)
  372. {
  373. /* Authorize the FLASH Registers access */
  374. WRITE_REG(FLASH->SECKEYR, FLASH_KEY1);
  375. WRITE_REG(FLASH->SECKEYR, FLASH_KEY2);
  376. /* verify Flash is unlocked */
  377. if (READ_BIT(FLASH->SECCR, FLASH_SECCR_SECLOCK) != 0u)
  378. {
  379. status = HAL_ERROR;
  380. }
  381. }
  382. }
  383. #endif
  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. /* Set the LOCK Bit to lock the FLASH Registers access */
  394. SET_BIT(FLASH->NSCR, FLASH_NSCR_NSLOCK);
  395. /* verify Flash is locked */
  396. if (READ_BIT(FLASH->NSCR, FLASH_NSCR_NSLOCK) != 0u)
  397. {
  398. status = HAL_OK;
  399. }
  400. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  401. if (status == HAL_OK)
  402. {
  403. SET_BIT(FLASH->SECCR, FLASH_SECCR_SECLOCK);
  404. /* verify Flash is locked */
  405. if (READ_BIT(FLASH->SECCR, FLASH_SECCR_SECLOCK) != 0u)
  406. {
  407. status = HAL_OK;
  408. }
  409. }
  410. #endif
  411. return status;
  412. }
  413. /**
  414. * @brief Unlock the FLASH Option Bytes Registers access.
  415. * @retval HAL Status
  416. */
  417. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  418. {
  419. if(READ_BIT(FLASH->NSCR, FLASH_NSCR_OPTLOCK) != 0u)
  420. {
  421. /* Authorizes the Option Byte register programming */
  422. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  423. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  424. /* Verify that the Option Bytes are unlocked */
  425. if (READ_BIT(FLASH->NSCR, FLASH_NSCR_OPTLOCK) != 0u)
  426. {
  427. return HAL_ERROR;
  428. }
  429. }
  430. return HAL_OK;
  431. }
  432. /**
  433. * @brief Lock the FLASH Option Bytes Registers access.
  434. * @retval HAL Status
  435. */
  436. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  437. {
  438. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  439. SET_BIT(FLASH->NSCR, FLASH_NSCR_OPTLOCK);
  440. /* Verify that the Option Bytes are locked */
  441. if (READ_BIT(FLASH->NSCR, FLASH_NSCR_OPTLOCK) != 0u)
  442. {
  443. return HAL_OK;
  444. }
  445. return HAL_ERROR;
  446. }
  447. /**
  448. * @brief Launch the option byte loading.
  449. * @retval HAL Status
  450. */
  451. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  452. {
  453. /* Set the bit to force the option byte reloading */
  454. SET_BIT(FLASH->NSCR, FLASH_NSCR_OBL_LAUNCH);
  455. /* We should not reach here : Option byte launch generates Option byte reset
  456. so return error */
  457. return HAL_ERROR;
  458. }
  459. /**
  460. * @}
  461. */
  462. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  463. * @brief Peripheral Errors functions
  464. *
  465. @verbatim
  466. ===============================================================================
  467. ##### Peripheral Errors functions #####
  468. ===============================================================================
  469. [..]
  470. This subsection permits to get in run-time Errors of the FLASH peripheral.
  471. @endverbatim
  472. * @{
  473. */
  474. /**
  475. * @brief Get the specific FLASH error flag.
  476. * @retval FLASH_ErrorCode The returned value can be:
  477. * @arg HAL_FLASH_ERROR_NONE: No error set
  478. * @arg HAL_FLASH_ERROR_OP: FLASH Operation error
  479. * @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
  480. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
  481. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
  482. * @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
  483. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
  484. * @arg HAL_FLASH_ERROR_OPTW: FLASH Option modification error
  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. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  508. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  509. flag will be set */
  510. uint32_t timeout = HAL_GetTick() + Timeout;
  511. uint32_t error;
  512. __IO uint32_t *reg_sr;
  513. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  514. {
  515. if(Timeout != HAL_MAX_DELAY)
  516. {
  517. if(HAL_GetTick() >= timeout)
  518. {
  519. return HAL_TIMEOUT;
  520. }
  521. }
  522. }
  523. /* Access to SECSR or NSSR registers depends on operation type */
  524. reg_sr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECSR) : &(FLASH_NS->NSSR);
  525. /* Check FLASH operation error flags */
  526. error = ((*reg_sr) & FLASH_FLAG_SR_ERRORS);
  527. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  528. error |= (FLASH->NSSR & FLASH_FLAG_OPTWERR);
  529. #endif /* __ARM_FEATURE_CMSE */
  530. if(error != 0u)
  531. {
  532. /*Save the error code*/
  533. pFlash.ErrorCode |= error;
  534. /* Clear error programming flags */
  535. (*reg_sr) = error;
  536. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  537. if ((error & FLASH_FLAG_OPTWERR) != 0U)
  538. {
  539. FLASH->NSSR = FLASH_FLAG_OPTWERR;
  540. }
  541. #endif /* __ARM_FEATURE_CMSE */
  542. return HAL_ERROR;
  543. }
  544. /* Check FLASH End of Operation flag */
  545. if (((*reg_sr) & FLASH_FLAG_EOP) != 0U)
  546. {
  547. /* Clear FLASH End of Operation pending bit */
  548. (*reg_sr) = FLASH_FLAG_EOP;
  549. }
  550. /* If there is an error flag set */
  551. return HAL_OK;
  552. }
  553. /**
  554. * @brief Program double-word (64-bit) at a specified address.
  555. * @param Address specifies the address to be programmed.
  556. * @param Data specifies the data to be programmed.
  557. * @retval None
  558. */
  559. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  560. {
  561. uint32_t primask_bit;
  562. __IO uint32_t *reg;
  563. /* Check the parameters */
  564. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  565. /* Access to SECCR or NSCR registers depends on operation type */
  566. reg = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECCR) : &(FLASH_NS->NSCR);
  567. /* Disable interrupts to avoid any interruption during the double word programming */
  568. primask_bit = __get_PRIMASK();
  569. __disable_irq();
  570. /* Set PG bit */
  571. SET_BIT((*reg), FLASH_NSCR_NSPG);
  572. /* Program first word */
  573. *(uint32_t*)Address = (uint32_t)Data;
  574. /* Barrier to ensure programming is performed in 2 steps, in right order
  575. (independently of compiler optimization behavior) */
  576. __ISB();
  577. /* Program second word */
  578. *(uint32_t*)(Address+4U) = (uint32_t)(Data >> 32U);
  579. /* Re-enable the interrupts */
  580. __set_PRIMASK(primask_bit);
  581. }
  582. /**
  583. * @}
  584. */
  585. #endif /* HAL_FLASH_MODULE_ENABLED */
  586. /**
  587. * @}
  588. */
  589. /**
  590. * @}
  591. */