stm32l4xx_hal_flash.c 29 KB

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