stm32l1xx_hal_flash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 State 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.
  21. [..] The FLASH main features are:
  22. (+) Flash memory read operations
  23. (+) Flash memory program/erase operations
  24. (+) Read / write protections
  25. (+) Prefetch on I-Code
  26. (+) Option Bytes programming
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. This driver provides functions and macros to configure and program the FLASH
  31. memory of all STM32L1xx devices.
  32. (#) FLASH Memory I/O Programming functions: this group includes all needed
  33. functions to erase and program the main memory:
  34. (++) Lock and Unlock the FLASH interface
  35. (++) Erase function: Erase page
  36. (++) Program functions: Fast Word and Half Page(should be
  37. executed from internal SRAM).
  38. (#) DATA EEPROM Programming functions: this group includes all
  39. needed functions to erase and program the DATA EEPROM memory:
  40. (++) Lock and Unlock the DATA EEPROM interface.
  41. (++) Erase function: Erase Byte, erase HalfWord, erase Word, erase
  42. Double Word (should be executed from internal SRAM).
  43. (++) Program functions: Fast Program Byte, Fast Program Half-Word,
  44. FastProgramWord, Program Byte, Program Half-Word,
  45. Program Word and Program Double-Word (should be executed
  46. from internal SRAM).
  47. (#) FLASH Option Bytes Programming functions: this group includes all needed
  48. functions to manage the Option Bytes:
  49. (++) Lock and Unlock the Option Bytes
  50. (++) Set/Reset the write protection
  51. (++) Set the Read protection Level
  52. (++) Program the user Option Bytes
  53. (++) Launch the Option Bytes loader
  54. (++) Set/Get the Read protection Level.
  55. (++) Set/Get the BOR level.
  56. (++) Get the Write protection.
  57. (++) Get the user option bytes.
  58. (#) Interrupts and flags management functions : this group
  59. includes all needed functions to:
  60. (++) Handle FLASH interrupts
  61. (++) Wait for last FLASH operation according to its status
  62. (++) Get error flag status
  63. (#) FLASH Interface configuration functions: this group includes
  64. the management of following features:
  65. (++) Enable/Disable the RUN PowerDown mode.
  66. (++) Enable/Disable the SLEEP PowerDown mode.
  67. (#) FLASH Peripheral State methods: this group includes
  68. the management of following features:
  69. (++) Wait for the FLASH operation
  70. (++) Get the specific FLASH error flag
  71. [..] In addition to these function, this driver includes a set of macros allowing
  72. to handle the following operations:
  73. (+) Set/Get the latency
  74. (+) Enable/Disable the prefetch buffer
  75. (+) Enable/Disable the 64 bit Read Access.
  76. (+) Enable/Disable the Flash power-down
  77. (+) Enable/Disable the FLASH interrupts
  78. (+) Monitor the FLASH flags status
  79. ##### Programming operation functions #####
  80. ===============================================================================
  81. [..]
  82. This subsection provides a set of functions allowing to manage the FLASH
  83. program operations.
  84. [..] The FLASH Memory Programming functions, includes the following functions:
  85. (+) HAL_FLASH_Unlock(void);
  86. (+) HAL_FLASH_Lock(void);
  87. (+) HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  88. (+) HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  89. [..] Any operation of erase or program should follow these steps:
  90. (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
  91. program memory access.
  92. (#) Call the desired function to erase page or program data.
  93. (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
  94. (recommended to protect the FLASH memory against possible unwanted operation).
  95. ##### Option Bytes Programming functions #####
  96. ==============================================================================
  97. [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
  98. (+) HAL_FLASH_OB_Unlock(void);
  99. (+) HAL_FLASH_OB_Lock(void);
  100. (+) HAL_FLASH_OB_Launch(void);
  101. (+) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  102. (+) HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  103. [..] Any operation of erase or program should follow these steps:
  104. (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
  105. register access.
  106. (#) Call the following functions to program the desired option bytes.
  107. (++) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  108. (#) Once all needed option bytes to be programmed are correctly written, call the
  109. HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  110. (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
  111. to protect the option Bytes against possible unwanted operations).
  112. [..] Proprietary code Read Out Protection (PcROP):
  113. (#) The PcROP sector is selected by using the same option bytes as the Write
  114. protection. As a result, these 2 options are exclusive each other.
  115. (#) To activate PCROP mode for Flash sectors(s), you need to follow the sequence below:
  116. (++) Use this function HAL_FLASHEx_AdvOBProgram with PCROPState = OB_PCROP_STATE_ENABLE.
  117. @endverbatim
  118. ******************************************************************************
  119. * @attention
  120. *
  121. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  122. *
  123. * Redistribution and use in source and binary forms, with or without modification,
  124. * are permitted provided that the following conditions are met:
  125. * 1. Redistributions of source code must retain the above copyright notice,
  126. * this list of conditions and the following disclaimer.
  127. * 2. Redistributions in binary form must reproduce the above copyright notice,
  128. * this list of conditions and the following disclaimer in the documentation
  129. * and/or other materials provided with the distribution.
  130. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  131. * may be used to endorse or promote products derived from this software
  132. * without specific prior written permission.
  133. *
  134. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  135. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  136. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  137. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  138. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  139. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  140. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  141. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  142. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  143. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  144. *
  145. ******************************************************************************
  146. */
  147. /* Includes ------------------------------------------------------------------*/
  148. #include "stm32l1xx_hal.h"
  149. /** @addtogroup STM32L1xx_HAL_Driver
  150. * @{
  151. */
  152. #ifdef HAL_FLASH_MODULE_ENABLED
  153. /** @defgroup FLASH FLASH
  154. * @brief FLASH HAL module driver
  155. * @{
  156. */
  157. /* Private typedef -----------------------------------------------------------*/
  158. /* Private define ------------------------------------------------------------*/
  159. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  160. * @{
  161. */
  162. /**
  163. * @}
  164. */
  165. /* Private macro ---------------------------- ---------------------------------*/
  166. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  167. * @{
  168. */
  169. /**
  170. * @}
  171. */
  172. /* Private variables ---------------------------------------------------------*/
  173. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  174. * @{
  175. */
  176. /* Variables used for Erase pages under interruption*/
  177. FLASH_ProcessTypeDef pFlash;
  178. /**
  179. * @}
  180. */
  181. /* Private function prototypes -----------------------------------------------*/
  182. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  183. * @{
  184. */
  185. static void FLASH_SetErrorCode(void);
  186. extern void FLASH_PageErase(uint32_t PageAddress);
  187. /**
  188. * @}
  189. */
  190. /* Exported functions ---------------------------------------------------------*/
  191. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  192. * @{
  193. */
  194. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  195. * @brief Programming operation functions
  196. *
  197. @verbatim
  198. @endverbatim
  199. * @{
  200. */
  201. /**
  202. * @brief Program word at a specified address
  203. * @note To correctly run this function, the HAL_FLASH_Unlock() function
  204. * must be called before.
  205. * Call the HAL_FLASH_Lock() to disable the flash memory access
  206. * (recommended to protect the FLASH memory against possible unwanted operation).
  207. *
  208. * @param TypeProgram Indicate the way to program at a specified address.
  209. * This parameter can be a value of @ref FLASH_Type_Program
  210. * @param Address Specifie the address to be programmed.
  211. * @param Data Specifie the data to be programmed
  212. *
  213. * @retval HAL_StatusTypeDef HAL Status
  214. */
  215. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  216. {
  217. HAL_StatusTypeDef status = HAL_ERROR;
  218. /* Process Locked */
  219. __HAL_LOCK(&pFlash);
  220. /* Check the parameters */
  221. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  222. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  223. /* Wait for last operation to be completed */
  224. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  225. if(status == HAL_OK)
  226. {
  227. /* Clean the error context */
  228. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  229. /*Program word (32-bit) at a specified address.*/
  230. *(__IO uint32_t *)Address = Data;
  231. /* Wait for last operation to be completed */
  232. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  233. }
  234. /* Process Unlocked */
  235. __HAL_UNLOCK(&pFlash);
  236. return status;
  237. }
  238. /**
  239. * @brief Program word at a specified address with interrupt enabled.
  240. *
  241. * @param TypeProgram Indicate the way to program at a specified address.
  242. * This parameter can be a value of @ref FLASH_Type_Program
  243. * @param Address Specifie the address to be programmed.
  244. * @param Data Specifie the data to be programmed
  245. *
  246. * @retval HAL_StatusTypeDef HAL Status
  247. */
  248. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  249. {
  250. HAL_StatusTypeDef status = HAL_OK;
  251. /* Process Locked */
  252. __HAL_LOCK(&pFlash);
  253. /* Check the parameters */
  254. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  255. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  256. /* Enable End of FLASH Operation and Error source interrupts */
  257. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  258. pFlash.Address = Address;
  259. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  260. /* Clean the error context */
  261. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  262. if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  263. {
  264. /* Program word (32-bit) at a specified address. */
  265. *(__IO uint32_t *)Address = Data;
  266. }
  267. return status;
  268. }
  269. /**
  270. * @brief This function handles FLASH interrupt request.
  271. * @retval None
  272. */
  273. void HAL_FLASH_IRQHandler(void)
  274. {
  275. uint32_t addresstmp = 0U;
  276. /* Check FLASH operation error flags */
  277. if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  278. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) ||
  279. __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) ||
  280. #if defined(FLASH_SR_RDERR)
  281. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  282. #endif /* FLASH_SR_RDERR */
  283. #if defined(FLASH_SR_OPTVERRUSR)
  284. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  285. #endif /* FLASH_SR_OPTVERRUSR */
  286. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) )
  287. {
  288. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  289. {
  290. /* Return the faulty sector */
  291. addresstmp = pFlash.Page;
  292. pFlash.Page = 0xFFFFFFFFU;
  293. }
  294. else
  295. {
  296. /* Return the faulty address */
  297. addresstmp = pFlash.Address;
  298. }
  299. /* Save the Error code */
  300. FLASH_SetErrorCode();
  301. /* FLASH error interrupt user callback */
  302. HAL_FLASH_OperationErrorCallback(addresstmp);
  303. /* Stop the procedure ongoing */
  304. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  305. }
  306. /* Check FLASH End of Operation flag */
  307. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  308. {
  309. /* Clear FLASH End of Operation pending bit */
  310. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  311. /* Process can continue only if no error detected */
  312. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  313. {
  314. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  315. {
  316. /* Nb of pages to erased can be decreased */
  317. pFlash.NbPagesToErase--;
  318. /* Check if there are still pages to erase */
  319. if(pFlash.NbPagesToErase != 0U)
  320. {
  321. addresstmp = pFlash.Page;
  322. /*Indicate user which sector has been erased */
  323. HAL_FLASH_EndOfOperationCallback(addresstmp);
  324. /*Increment sector number*/
  325. addresstmp = pFlash.Page + FLASH_PAGE_SIZE;
  326. pFlash.Page = addresstmp;
  327. /* If the erase operation is completed, disable the ERASE Bit */
  328. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  329. FLASH_PageErase(addresstmp);
  330. }
  331. else
  332. {
  333. /* No more pages to Erase, user callback can be called. */
  334. /* Reset Sector and stop Erase pages procedure */
  335. pFlash.Page = addresstmp = 0xFFFFFFFFU;
  336. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  337. /* FLASH EOP interrupt user callback */
  338. HAL_FLASH_EndOfOperationCallback(addresstmp);
  339. }
  340. }
  341. else
  342. {
  343. /* If the program operation is completed, disable the PROG Bit */
  344. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  345. /* Program ended. Return the selected address */
  346. /* FLASH EOP interrupt user callback */
  347. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  348. /* Reset Address and stop Program procedure */
  349. pFlash.Address = 0xFFFFFFFFU;
  350. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  351. }
  352. }
  353. }
  354. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  355. {
  356. /* Operation is completed, disable the PROG and ERASE */
  357. CLEAR_BIT(FLASH->PECR, (FLASH_PECR_ERASE | FLASH_PECR_PROG));
  358. /* Disable End of FLASH Operation and Error source interrupts */
  359. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  360. /* Process Unlocked */
  361. __HAL_UNLOCK(&pFlash);
  362. }
  363. }
  364. /**
  365. * @brief FLASH end of operation interrupt callback
  366. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  367. * - Pages Erase: Address of the page which has been erased
  368. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  369. * - Program: Address which was selected for data program
  370. * @retval none
  371. */
  372. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  373. {
  374. /* Prevent unused argument(s) compilation warning */
  375. UNUSED(ReturnValue);
  376. /* NOTE : This function Should not be modified, when the callback is needed,
  377. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  378. */
  379. }
  380. /**
  381. * @brief FLASH operation error interrupt callback
  382. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  383. * - Pages Erase: Address of the page which returned an error
  384. * - Program: Address which was selected for data program
  385. * @retval none
  386. */
  387. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  388. {
  389. /* Prevent unused argument(s) compilation warning */
  390. UNUSED(ReturnValue);
  391. /* NOTE : This function Should not be modified, when the callback is needed,
  392. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  393. */
  394. }
  395. /**
  396. * @}
  397. */
  398. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  399. * @brief management functions
  400. *
  401. @verbatim
  402. ===============================================================================
  403. ##### Peripheral Control functions #####
  404. ===============================================================================
  405. [..]
  406. This subsection provides a set of functions allowing to control the FLASH
  407. memory operations.
  408. @endverbatim
  409. * @{
  410. */
  411. /**
  412. * @brief Unlock the FLASH control register access
  413. * @retval HAL Status
  414. */
  415. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  416. {
  417. if (HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PRGLOCK))
  418. {
  419. /* Unlocking FLASH_PECR register access*/
  420. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  421. {
  422. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1);
  423. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2);
  424. }
  425. /* Unlocking the program memory access */
  426. WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY1);
  427. WRITE_REG(FLASH->PRGKEYR, FLASH_PRGKEY2);
  428. }
  429. else
  430. {
  431. return HAL_ERROR;
  432. }
  433. return HAL_OK;
  434. }
  435. /**
  436. * @brief Locks the FLASH control register access
  437. * @retval HAL Status
  438. */
  439. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  440. {
  441. /* Set the PRGLOCK Bit to lock the FLASH Registers access */
  442. SET_BIT(FLASH->PECR, FLASH_PECR_PRGLOCK);
  443. return HAL_OK;
  444. }
  445. /**
  446. * @brief Unlock the FLASH Option Control Registers access.
  447. * @retval HAL Status
  448. */
  449. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  450. {
  451. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_OPTLOCK))
  452. {
  453. /* Unlocking FLASH_PECR register access*/
  454. if(HAL_IS_BIT_SET(FLASH->PECR, FLASH_PECR_PELOCK))
  455. {
  456. /* Unlocking FLASH_PECR register access*/
  457. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY1);
  458. WRITE_REG(FLASH->PEKEYR, FLASH_PEKEY2);
  459. }
  460. /* Unlocking the option bytes block access */
  461. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  462. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  463. }
  464. else
  465. {
  466. return HAL_ERROR;
  467. }
  468. return HAL_OK;
  469. }
  470. /**
  471. * @brief Lock the FLASH Option Control Registers access.
  472. * @retval HAL Status
  473. */
  474. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  475. {
  476. /* Set the OPTLOCK Bit to lock the option bytes block access */
  477. SET_BIT(FLASH->PECR, FLASH_PECR_OPTLOCK);
  478. return HAL_OK;
  479. }
  480. /**
  481. * @brief Launch the option byte loading.
  482. * @note This function will reset automatically the MCU.
  483. * @retval HAL Status
  484. */
  485. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  486. {
  487. /* Set the OBL_Launch bit to launch the option byte loading */
  488. SET_BIT(FLASH->PECR, FLASH_PECR_OBL_LAUNCH);
  489. /* Wait for last operation to be completed */
  490. return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE));
  491. }
  492. /**
  493. * @}
  494. */
  495. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
  496. * @brief Peripheral errors functions
  497. *
  498. @verbatim
  499. ===============================================================================
  500. ##### Peripheral Errors functions #####
  501. ===============================================================================
  502. [..]
  503. This subsection permit to get in run-time errors of the FLASH peripheral.
  504. @endverbatim
  505. * @{
  506. */
  507. /**
  508. * @brief Get the specific FLASH error flag.
  509. * @retval FLASH_ErrorCode The returned value can be:
  510. * @ref FLASH_Error_Codes
  511. */
  512. uint32_t HAL_FLASH_GetError(void)
  513. {
  514. return pFlash.ErrorCode;
  515. }
  516. /**
  517. * @}
  518. */
  519. /**
  520. * @}
  521. */
  522. /** @addtogroup FLASH_Private_Functions
  523. * @{
  524. */
  525. /**
  526. * @brief Wait for a FLASH operation to complete.
  527. * @param Timeout maximum flash operation timeout
  528. * @retval HAL Status
  529. */
  530. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  531. {
  532. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  533. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  534. flag will be set */
  535. uint32_t tickstart = HAL_GetTick();
  536. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  537. {
  538. if (Timeout != HAL_MAX_DELAY)
  539. {
  540. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  541. {
  542. return HAL_TIMEOUT;
  543. }
  544. }
  545. }
  546. /* Check FLASH End of Operation flag */
  547. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  548. {
  549. /* Clear FLASH End of Operation pending bit */
  550. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  551. }
  552. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  553. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  554. #if defined(FLASH_SR_RDERR)
  555. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  556. #endif /* FLASH_SR_RDERR */
  557. #if defined(FLASH_SR_OPTVERRUSR)
  558. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  559. #endif /* FLASH_SR_OPTVERRUSR */
  560. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  561. {
  562. /*Save the error code*/
  563. FLASH_SetErrorCode();
  564. return HAL_ERROR;
  565. }
  566. /* There is no error flag set */
  567. return HAL_OK;
  568. }
  569. /**
  570. * @brief Set the specific FLASH error flag.
  571. * @retval None
  572. */
  573. static void FLASH_SetErrorCode(void)
  574. {
  575. uint32_t flags = 0U;
  576. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  577. {
  578. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  579. flags |= FLASH_FLAG_WRPERR;
  580. }
  581. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  582. {
  583. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  584. flags |= FLASH_FLAG_PGAERR;
  585. }
  586. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  587. {
  588. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  589. flags |= FLASH_FLAG_OPTVERR;
  590. }
  591. #if defined(FLASH_SR_RDERR)
  592. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  593. {
  594. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  595. flags |= FLASH_FLAG_RDERR;
  596. }
  597. #endif /* FLASH_SR_RDERR */
  598. #if defined(FLASH_SR_OPTVERRUSR)
  599. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR))
  600. {
  601. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTVUSR;
  602. flags |= FLASH_FLAG_OPTVERRUSR;
  603. }
  604. #endif /* FLASH_SR_OPTVERRUSR */
  605. /* Clear FLASH error pending bits */
  606. __HAL_FLASH_CLEAR_FLAG(flags);
  607. }
  608. /**
  609. * @}
  610. */
  611. /**
  612. * @}
  613. */
  614. #endif /* HAL_FLASH_MODULE_ENABLED */
  615. /**
  616. * @}
  617. */
  618. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/