stm32f0xx_hal_flash.c 22 KB

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