stm32l4xx_hal_flash.c 23 KB

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