stm32l4xx_hal_flash.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief FLASH HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the internal FLASH memory:
  10. * + Program operations functions
  11. * + Memory Control functions
  12. * + Peripheral Errors functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### FLASH peripheral features #####
  17. ==============================================================================
  18. [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
  19. to the Flash memory. It implements the erase and program Flash memory operations
  20. and the read and write protection mechanisms.
  21. [..] The Flash memory interface accelerates code execution with a system of instruction
  22. prefetch and cache lines.
  23. [..] The FLASH main features are:
  24. (+) Flash memory read operations
  25. (+) Flash memory program/erase operations
  26. (+) Read / write protections
  27. (+) Option bytes programming
  28. (+) Prefetch on I-Code
  29. (+) 32 cache lines of 4*64 bits on I-Code
  30. (+) 8 cache lines of 4*64 bits on D-Code
  31. (+) Error code correction (ECC) : Data in flash are 72-bits word
  32. (8 bits added per double word)
  33. ##### How to use this driver #####
  34. ==============================================================================
  35. [..]
  36. This driver provides functions and macros to configure and program the FLASH
  37. memory of all STM32L4xx devices.
  38. (#) Flash Memory IO Programming functions:
  39. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  40. HAL_FLASH_Lock() functions
  41. (++) Program functions: double word and fast program (full row programming)
  42. (++) There Two modes of programming :
  43. (+++) Polling mode using HAL_FLASH_Program() function
  44. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  45. (#) Interrupts and flags management functions :
  46. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  47. (++) Callback functions are called when the flash operations are finished :
  48. HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
  49. HAL_FLASH_OperationErrorCallback()
  50. (++) Get error flag status by calling HAL_GetError()
  51. (#) Option bytes management functions :
  52. (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
  53. HAL_FLASH_OB_Lock() functions
  54. (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
  55. In this case, a reset is generated
  56. [..]
  57. In addition to these functions, this driver includes a set of macros allowing
  58. to handle the following operations:
  59. (+) Set the latency
  60. (+) Enable/Disable the prefetch buffer
  61. (+) Enable/Disable the Instruction cache and the Data cache
  62. (+) Reset the Instruction cache and the Data cache
  63. (+) Enable/Disable the Flash power-down during low-power run and sleep modes
  64. (+) Enable/Disable the Flash interrupts
  65. (+) Monitor the Flash flags status
  66. @endverbatim
  67. ******************************************************************************
  68. * @attention
  69. *
  70. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  71. *
  72. * Redistribution and use in source and binary forms, with or without modification,
  73. * are permitted provided that the following conditions are met:
  74. * 1. Redistributions of source code must retain the above copyright notice,
  75. * this list of conditions and the following disclaimer.
  76. * 2. Redistributions in binary form must reproduce the above copyright notice,
  77. * this list of conditions and the following disclaimer in the documentation
  78. * and/or other materials provided with the distribution.
  79. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  80. * may be used to endorse or promote products derived from this software
  81. * without specific prior written permission.
  82. *
  83. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  84. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  85. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  86. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  87. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  88. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  91. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  92. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  93. *
  94. ******************************************************************************
  95. */
  96. /* Includes ------------------------------------------------------------------*/
  97. #include "stm32l4xx_hal.h"
  98. /** @addtogroup STM32L4xx_HAL_Driver
  99. * @{
  100. */
  101. /** @defgroup FLASH FLASH
  102. * @brief FLASH HAL module driver
  103. * @{
  104. */
  105. #ifdef HAL_FLASH_MODULE_ENABLED
  106. /* Private typedef -----------------------------------------------------------*/
  107. /* Private defines -----------------------------------------------------------*/
  108. #define FLASH_NB_DOUBLE_WORDS_IN_ROW 32
  109. /* Private macros ------------------------------------------------------------*/
  110. /* Private variables ---------------------------------------------------------*/
  111. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  112. * @{
  113. */
  114. /**
  115. * @brief Variable used for Program/Erase sectors under interruption
  116. */
  117. FLASH_ProcessTypeDef pFlash;
  118. /**
  119. * @}
  120. */
  121. /* Private function prototypes -----------------------------------------------*/
  122. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  123. * @{
  124. */
  125. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  126. extern void FLASH_PageErase(uint32_t Page, uint32_t Banks);
  127. extern void FLASH_FlushCaches(void);
  128. static void FLASH_SetErrorCode(void);
  129. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  130. static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);
  131. /**
  132. * @}
  133. */
  134. /* Exported functions --------------------------------------------------------*/
  135. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  136. * @{
  137. */
  138. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  139. * @brief Programming operation functions
  140. *
  141. @verbatim
  142. ===============================================================================
  143. ##### Programming operation functions #####
  144. ===============================================================================
  145. [..]
  146. This subsection provides a set of functions allowing to manage the FLASH
  147. program operations.
  148. @endverbatim
  149. * @{
  150. */
  151. /**
  152. * @brief Program double word or fast program of a row at a specified address.
  153. * @param TypeProgram: Indicate the way to program at a specified address.
  154. * This parameter can be a value of @ref FLASH_Type_Program
  155. * @param Address: specifies the address to be programmed.
  156. * @param Data: specifies the data to be programmed
  157. * This parameter is the data for the double word program and the address where
  158. * are stored the data for the row fast program
  159. *
  160. * @retval HAL_StatusTypeDef HAL Status
  161. */
  162. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  163. {
  164. HAL_StatusTypeDef status = HAL_ERROR;
  165. uint32_t prog_bit = 0;
  166. /* Process Locked */
  167. __HAL_LOCK(&pFlash);
  168. /* Check the parameters */
  169. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  170. /* Wait for last operation to be completed */
  171. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  172. if(status == HAL_OK)
  173. {
  174. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  175. /* Deactivate the data cache if they are activated to avoid data misbehavior */
  176. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
  177. {
  178. /* Disable data cache */
  179. __HAL_FLASH_DATA_CACHE_DISABLE();
  180. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  181. }
  182. else
  183. {
  184. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  185. }
  186. if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
  187. {
  188. /* Program double-word (64-bit) at a specified address */
  189. FLASH_Program_DoubleWord(Address, Data);
  190. prog_bit = FLASH_CR_PG;
  191. }
  192. else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  193. {
  194. /* Fast program a 32 row double-word (64-bit) at a specified address */
  195. FLASH_Program_Fast(Address, (uint32_t)Data);
  196. /* If it is the last row, the bit will be cleared at the end of the operation */
  197. if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
  198. {
  199. prog_bit = FLASH_CR_FSTPG;
  200. }
  201. }
  202. /* Wait for last operation to be completed */
  203. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  204. /* If the program operation is completed, disable the PG or FSTPG Bit */
  205. if (prog_bit != 0)
  206. {
  207. CLEAR_BIT(FLASH->CR, prog_bit);
  208. }
  209. /* Flush the caches to be sure of the data consistency */
  210. FLASH_FlushCaches();
  211. }
  212. /* Process Unlocked */
  213. __HAL_UNLOCK(&pFlash);
  214. return status;
  215. }
  216. /**
  217. * @brief Program double word or fast program of a row at a specified address with interrupt enabled.
  218. * @param TypeProgram: Indicate the way to program at a specified address.
  219. * This parameter can be a value of @ref FLASH_Type_Program
  220. * @param Address: specifies the address to be programmed.
  221. * @param Data: specifies the data to be programmed
  222. * This parameter is the data for the double word program and the address where
  223. * are stored the data for the row fast program
  224. *
  225. * @retval HAL Status
  226. */
  227. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  228. {
  229. HAL_StatusTypeDef status = HAL_OK;
  230. /* Check the parameters */
  231. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  232. /* Process Locked */
  233. __HAL_LOCK(&pFlash);
  234. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  235. /* Deactivate the data cache if they are activated to avoid data misbehavior */
  236. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
  237. {
  238. /* Disable data cache */
  239. __HAL_FLASH_DATA_CACHE_DISABLE();
  240. pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
  241. }
  242. else
  243. {
  244. pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
  245. }
  246. /* Set internal variables used by the IRQ handler */
  247. if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
  248. {
  249. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;
  250. }
  251. else
  252. {
  253. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  254. }
  255. pFlash.Address = Address;
  256. /* Enable End of Operation and Error interrupts */
  257. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  258. if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
  259. {
  260. /* Program double-word (64-bit) at a specified address */
  261. FLASH_Program_DoubleWord(Address, Data);
  262. }
  263. else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  264. {
  265. /* Fast program a 32 row double-word (64-bit) at a specified address */
  266. FLASH_Program_Fast(Address, (uint32_t)Data);
  267. }
  268. return status;
  269. }
  270. /**
  271. * @brief Handle FLASH interrupt request.
  272. * @retval None
  273. */
  274. void HAL_FLASH_IRQHandler(void)
  275. {
  276. uint32_t tmp_page;
  277. /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */
  278. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));
  279. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  280. defined (STM32L496xx) || defined (STM32L4A6xx)
  281. CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);
  282. #endif
  283. /* Disable the FSTPG Bit only if it is the last row programmed */
  284. if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
  285. {
  286. CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
  287. }
  288. /* Check FLASH operation error flags */
  289. if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
  290. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
  291. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)) ||
  292. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) ||
  293. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
  294. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  295. defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  296. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY)))
  297. #else
  298. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
  299. #endif
  300. {
  301. /*Save the error code*/
  302. FLASH_SetErrorCode();
  303. /* Flush the caches to be sure of the data consistency */
  304. FLASH_FlushCaches() ;
  305. /* FLASH error interrupt user callback */
  306. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
  307. {
  308. HAL_FLASH_OperationErrorCallback(pFlash.Page);
  309. }
  310. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
  311. {
  312. HAL_FLASH_OperationErrorCallback(pFlash.Bank);
  313. }
  314. else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) ||
  315. (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
  316. {
  317. HAL_FLASH_OperationErrorCallback(pFlash.Address);
  318. }
  319. /*Stop the procedure ongoing*/
  320. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  321. }
  322. /* Check FLASH End of Operation flag */
  323. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  324. {
  325. /* Clear FLASH End of Operation pending bit */
  326. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  327. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
  328. {
  329. /* Nb of pages to erased can be decreased */
  330. pFlash.NbPagesToErase--;
  331. /* Check if there are still pages to erase*/
  332. if(pFlash.NbPagesToErase != 0)
  333. {
  334. /* Indicate user which page has been erased*/
  335. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  336. /* Increment page number */
  337. pFlash.Page++;
  338. tmp_page = pFlash.Page;
  339. FLASH_PageErase(tmp_page, pFlash.Bank);
  340. }
  341. else
  342. {
  343. /* No more pages to Erase */
  344. /* Reset Address and stop Erase pages procedure */
  345. pFlash.Page = 0xFFFFFFFF;
  346. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  347. /* Flush the caches to be sure of the data consistency */
  348. FLASH_FlushCaches() ;
  349. /* FLASH EOP interrupt user callback */
  350. HAL_FLASH_EndOfOperationCallback(pFlash.Page);
  351. }
  352. }
  353. else
  354. {
  355. /* Flush the caches to be sure of the data consistency */
  356. FLASH_FlushCaches() ;
  357. if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
  358. {
  359. /* MassErase ended. Return the selected bank */
  360. /* FLASH EOP interrupt user callback */
  361. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  362. }
  363. else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) ||
  364. (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
  365. {
  366. /* Program ended. Return the selected address */
  367. /* FLASH EOP interrupt user callback */
  368. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  369. }
  370. /*Clear the procedure ongoing*/
  371. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  372. }
  373. }
  374. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  375. {
  376. /* Disable End of Operation and Error interrupts */
  377. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
  378. /* Process Unlocked */
  379. __HAL_UNLOCK(&pFlash);
  380. }
  381. }
  382. /**
  383. * @brief FLASH end of operation interrupt callback.
  384. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  385. * Mass Erase: Bank number which has been requested to erase
  386. * Page Erase: Page which has been erased
  387. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  388. * Program: Address which was selected for data program
  389. * @retval None
  390. */
  391. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  392. {
  393. /* Prevent unused argument(s) compilation warning */
  394. UNUSED(ReturnValue);
  395. /* NOTE : This function should not be modified, when the callback is needed,
  396. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  397. */
  398. }
  399. /**
  400. * @brief FLASH operation error interrupt callback.
  401. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  402. * Mass Erase: Bank number which has been requested to erase
  403. * Page Erase: Page number which returned an error
  404. * Program: Address which was selected for data program
  405. * @retval None
  406. */
  407. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  408. {
  409. /* Prevent unused argument(s) compilation warning */
  410. UNUSED(ReturnValue);
  411. /* NOTE : This function should not be modified, when the callback is needed,
  412. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  413. */
  414. }
  415. /**
  416. * @}
  417. */
  418. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  419. * @brief Management functions
  420. *
  421. @verbatim
  422. ===============================================================================
  423. ##### Peripheral Control functions #####
  424. ===============================================================================
  425. [..]
  426. This subsection provides a set of functions allowing to control the FLASH
  427. memory operations.
  428. @endverbatim
  429. * @{
  430. */
  431. /**
  432. * @brief Unlock the FLASH control register access.
  433. * @retval HAL Status
  434. */
  435. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  436. {
  437. #if defined(STM32L451xx) || defined(STM32L452xx) || defined(STM32L462xx)
  438. /* Clear OPTVERR bit when initially improperly raised */
  439. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
  440. #endif
  441. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  442. {
  443. /* Authorize the FLASH Registers access */
  444. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  445. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  446. }
  447. else
  448. {
  449. return HAL_ERROR;
  450. }
  451. return HAL_OK;
  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) != RESET)
  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. * @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected
  539. */
  540. uint32_t HAL_FLASH_GetError(void)
  541. {
  542. return pFlash.ErrorCode;
  543. }
  544. /**
  545. * @}
  546. */
  547. /**
  548. * @}
  549. */
  550. /* Private functions ---------------------------------------------------------*/
  551. /** @addtogroup FLASH_Private_Functions
  552. * @{
  553. */
  554. /**
  555. * @brief Wait for a FLASH operation to complete.
  556. * @param Timeout: maximum flash operation timeout
  557. * @retval HAL_StatusTypeDef HAL Status
  558. */
  559. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  560. {
  561. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  562. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  563. flag will be set */
  564. uint32_t tickstart = HAL_GetTick();
  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. if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
  576. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
  577. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)) ||
  578. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) ||
  579. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
  580. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  581. defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  582. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY)))
  583. #else
  584. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
  585. #endif
  586. {
  587. /*Save the error code*/
  588. FLASH_SetErrorCode();
  589. return HAL_ERROR;
  590. }
  591. /* Check FLASH End of Operation flag */
  592. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  593. {
  594. /* Clear FLASH End of Operation pending bit */
  595. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  596. }
  597. /* If there is an error flag set */
  598. return HAL_OK;
  599. }
  600. /**
  601. * @brief Set the specific FLASH error flag.
  602. * @retval None
  603. */
  604. static void FLASH_SetErrorCode(void)
  605. {
  606. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))
  607. {
  608. pFlash.ErrorCode |= HAL_FLASH_ERROR_OP;
  609. }
  610. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR))
  611. {
  612. pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
  613. }
  614. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  615. {
  616. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  617. }
  618. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  619. {
  620. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  621. }
  622. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
  623. {
  624. pFlash.ErrorCode |= HAL_FLASH_ERROR_SIZ;
  625. }
  626. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))
  627. {
  628. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
  629. }
  630. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR))
  631. {
  632. pFlash.ErrorCode |= HAL_FLASH_ERROR_MIS;
  633. }
  634. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR))
  635. {
  636. pFlash.ErrorCode |= HAL_FLASH_ERROR_FAST;
  637. }
  638. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  639. {
  640. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  641. }
  642. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  643. {
  644. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  645. }
  646. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD))
  647. {
  648. pFlash.ErrorCode |= HAL_FLASH_ERROR_ECCD;
  649. }
  650. #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
  651. defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
  652. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY))
  653. {
  654. pFlash.ErrorCode |= HAL_FLASH_ERROR_PEMPTY;
  655. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PEMPTY);
  656. }
  657. #endif
  658. /* Clear error programming flags */
  659. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
  660. }
  661. /**
  662. * @brief Program double-word (64-bit) at a specified address.
  663. * @param Address: specifies the address to be programmed.
  664. * @param Data: specifies the data to be programmed.
  665. * @retval None
  666. */
  667. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  668. {
  669. /* Check the parameters */
  670. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  671. /* Set PG bit */
  672. SET_BIT(FLASH->CR, FLASH_CR_PG);
  673. /* Program the double word */
  674. *(__IO uint32_t*)Address = (uint32_t)Data;
  675. *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
  676. }
  677. /**
  678. * @brief Fast program a row double-word (64-bit) at a specified address.
  679. * @param Address: specifies the address to be programmed.
  680. * @param DataAddress: specifies the address where the data are stored.
  681. * @retval None
  682. */
  683. static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
  684. {
  685. uint8_t row_index = (2*FLASH_NB_DOUBLE_WORDS_IN_ROW);
  686. __IO uint32_t *dest_addr = (__IO uint32_t*)Address;
  687. __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
  688. /* Check the parameters */
  689. assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
  690. /* Set FSTPG bit */
  691. SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
  692. /* Disable interrupts to avoid any interruption during the loop */
  693. __disable_irq();
  694. /* Program the double word of the row */
  695. do
  696. {
  697. *dest_addr++ = *src_addr++;
  698. } while (--row_index != 0);
  699. /* Re-enable the interrupts */
  700. __enable_irq();
  701. }
  702. /**
  703. * @}
  704. */
  705. #endif /* HAL_FLASH_MODULE_ENABLED */
  706. /**
  707. * @}
  708. */
  709. /**
  710. * @}
  711. */
  712. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/