1
0

stm32l0xx_hal_flash.c 26 KB

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