stm32f0xx_hal_flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @version V1.3.1
  6. * @date 29-January-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. /**
  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. @endverbatim
  143. * @{
  144. */
  145. /**
  146. * @brief Program halfword, word or double word at a specified address
  147. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  148. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  149. *
  150. * @note If an erase and a program operations are requested simultaneously,
  151. * the erase operation is performed before the program one.
  152. *
  153. * @note FLASH should be previously erased before new programmation (only exception to this
  154. * is when 0x0000 is programmed)
  155. *
  156. * @param TypeProgram: Indicate the way to program at a specified address.
  157. * This parameter can be a value of @ref FLASH_Type_Program
  158. * @param Address: Specifies the address to be programmed.
  159. * @param Data: Specifies the data to be programmed
  160. *
  161. * @retval HAL_StatusTypeDef HAL Status
  162. */
  163. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  164. {
  165. HAL_StatusTypeDef status = HAL_ERROR;
  166. uint8_t index = 0;
  167. uint8_t nbiterations = 0;
  168. /* Process Locked */
  169. __HAL_LOCK(&pFlash);
  170. /* Check the parameters */
  171. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  172. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  173. /* Wait for last operation to be completed */
  174. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  175. if(status == HAL_OK)
  176. {
  177. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  178. {
  179. /* Program halfword (16-bit) at a specified address. */
  180. nbiterations = 1;
  181. }
  182. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  183. {
  184. /* Program word (32-bit = 2*16-bit) at a specified address. */
  185. nbiterations = 2;
  186. }
  187. else
  188. {
  189. /* Program double word (64-bit = 4*16-bit) at a specified address. */
  190. nbiterations = 4;
  191. }
  192. for (index = 0; index < nbiterations; index++)
  193. {
  194. FLASH_Program_HalfWord((Address + (2*index)), (uint16_t)(Data >> (16*index)));
  195. /* Wait for last operation to be completed */
  196. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  197. /* If the program operation is completed, disable the PG Bit */
  198. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  199. /* In case of error, stop programation procedure */
  200. if (status != HAL_OK)
  201. {
  202. break;
  203. }
  204. }
  205. }
  206. /* Process Unlocked */
  207. __HAL_UNLOCK(&pFlash);
  208. return status;
  209. }
  210. /**
  211. * @brief Program halfword, word or double word at a specified address with interrupt enabled.
  212. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  213. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  214. *
  215. * @note If an erase and a program operations are requested simultaneously,
  216. * the erase operation is performed before the program one.
  217. *
  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. *
  223. * @retval HAL_StatusTypeDef HAL Status
  224. */
  225. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  226. {
  227. HAL_StatusTypeDef status = HAL_OK;
  228. /* Process Locked */
  229. __HAL_LOCK(&pFlash);
  230. /* Check the parameters */
  231. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  232. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  233. /* Enable End of FLASH Operation and Error source interrupts */
  234. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  235. pFlash.Address = Address;
  236. pFlash.Data = Data;
  237. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  238. {
  239. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
  240. /*Program halfword (16-bit) at a specified address.*/
  241. pFlash.DataRemaining = 1;
  242. }
  243. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  244. {
  245. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
  246. /*Program word (32-bit : 2*16-bit) at a specified address.*/
  247. pFlash.DataRemaining = 2;
  248. }
  249. else
  250. {
  251. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
  252. /*Program double word (64-bit : 4*16-bit) at a specified address.*/
  253. pFlash.DataRemaining = 4;
  254. }
  255. /*Program halfword (16-bit) at a specified address.*/
  256. FLASH_Program_HalfWord(Address, (uint16_t)Data);
  257. return status;
  258. }
  259. /**
  260. * @brief This function handles FLASH interrupt request.
  261. * @retval None
  262. */
  263. void HAL_FLASH_IRQHandler(void)
  264. {
  265. uint32_t addresstmp = 0;
  266. /* Check FLASH operation error flags */
  267. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  268. {
  269. /*return the faulty address*/
  270. addresstmp = pFlash.Address;
  271. /* Reset address */
  272. pFlash.Address = 0xFFFFFFFFU;
  273. /*Save the Error code*/
  274. FLASH_SetErrorCode();
  275. /* FLASH error interrupt user callback */
  276. HAL_FLASH_OperationErrorCallback(addresstmp);
  277. /* Stop the procedure ongoing*/
  278. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  279. }
  280. /* Check FLASH End of Operation flag */
  281. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  282. {
  283. /* Clear FLASH End of Operation pending bit */
  284. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  285. /* Process can continue only if no error detected */
  286. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  287. {
  288. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  289. {
  290. /* Nb of pages to erased can be decreased */
  291. pFlash.DataRemaining--;
  292. /* Check if there are still pages to erase*/
  293. if(pFlash.DataRemaining != 0)
  294. {
  295. addresstmp = pFlash.Address;
  296. /*Indicate user which sector has been erased*/
  297. HAL_FLASH_EndOfOperationCallback(addresstmp);
  298. /*Increment sector number*/
  299. addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
  300. pFlash.Address = addresstmp;
  301. /* If the erase operation is completed, disable the PER Bit */
  302. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  303. FLASH_PageErase(addresstmp);
  304. }
  305. else
  306. {
  307. /*No more pages to Erase, user callback can be called.*/
  308. /*Reset Sector and stop Erase pages procedure*/
  309. pFlash.Address = addresstmp = 0xFFFFFFFFU;
  310. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  311. /* FLASH EOP interrupt user callback */
  312. HAL_FLASH_EndOfOperationCallback(addresstmp);
  313. }
  314. }
  315. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  316. {
  317. /* Operation is completed, disable the MER Bit */
  318. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  319. /* MassErase ended. Return the selected bank*/
  320. /* FLASH EOP interrupt user callback */
  321. HAL_FLASH_EndOfOperationCallback(0);
  322. /* Stop Mass Erase procedure*/
  323. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  324. }
  325. else
  326. {
  327. /* Nb of 16-bit data to program can be decreased */
  328. pFlash.DataRemaining--;
  329. /* Check if there are still 16-bit data to program */
  330. if(pFlash.DataRemaining != 0)
  331. {
  332. /* Increment address to 16-bit */
  333. pFlash.Address += 2;
  334. addresstmp = pFlash.Address;
  335. /* Shift to have next 16-bit data */
  336. pFlash.Data = (pFlash.Data >> 16);
  337. /* Operation is completed, disable the PG Bit */
  338. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  339. /*Program halfword (16-bit) at a specified address.*/
  340. FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
  341. }
  342. else
  343. {
  344. /*Program ended. Return the selected address*/
  345. /* FLASH EOP interrupt user callback */
  346. if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
  347. {
  348. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  349. }
  350. else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
  351. {
  352. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2);
  353. }
  354. else
  355. {
  356. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6);
  357. }
  358. /* Reset Address and stop Program procedure*/
  359. pFlash.Address = 0xFFFFFFFFU;
  360. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  361. }
  362. }
  363. }
  364. }
  365. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  366. {
  367. /* Operation is completed, disable the PG, PER and MER Bits */
  368. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
  369. /* Disable End of FLASH Operation and Error source interrupts */
  370. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  371. /* Process Unlocked */
  372. __HAL_UNLOCK(&pFlash);
  373. }
  374. }
  375. /**
  376. * @brief FLASH end of operation interrupt callback
  377. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  378. * - Mass Erase: No return value expected
  379. * - Pages Erase: Address of the page which has been erased
  380. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  381. * - Program: Address which was selected for data program
  382. * @retval none
  383. */
  384. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  385. {
  386. /* Prevent unused argument(s) compilation warning */
  387. UNUSED(ReturnValue);
  388. /* NOTE : This function Should not be modified, when the callback is needed,
  389. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  390. */
  391. }
  392. /**
  393. * @brief FLASH operation error interrupt callback
  394. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  395. * - Mass Erase: No return value expected
  396. * - Pages Erase: Address of the page which returned an error
  397. * - Program: Address which was selected for data program
  398. * @retval none
  399. */
  400. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  401. {
  402. /* Prevent unused argument(s) compilation warning */
  403. UNUSED(ReturnValue);
  404. /* NOTE : This function Should not be modified, when the callback is needed,
  405. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  406. */
  407. }
  408. /**
  409. * @}
  410. */
  411. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  412. * @brief management functions
  413. *
  414. @verbatim
  415. ===============================================================================
  416. ##### Peripheral Control functions #####
  417. ===============================================================================
  418. [..]
  419. This subsection provides a set of functions allowing to control the FLASH
  420. memory operations.
  421. @endverbatim
  422. * @{
  423. */
  424. /**
  425. * @brief Unlock the FLASH control register access
  426. * @retval HAL Status
  427. */
  428. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  429. {
  430. if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
  431. {
  432. /* Authorize the FLASH Registers access */
  433. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  434. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  435. }
  436. else
  437. {
  438. return HAL_ERROR;
  439. }
  440. return HAL_OK;
  441. }
  442. /**
  443. * @brief Locks the FLASH control register access
  444. * @retval HAL Status
  445. */
  446. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  447. {
  448. /* Set the LOCK Bit to lock the FLASH Registers access */
  449. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  450. return HAL_OK;
  451. }
  452. /**
  453. * @brief Unlock the FLASH Option Control Registers access.
  454. * @retval HAL Status
  455. */
  456. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  457. {
  458. if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
  459. {
  460. /* Authorizes the Option Byte register programming */
  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. /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
  477. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
  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_StatusTypeDef 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->CR, FLASH_CR_OBL_LAUNCH);
  489. /* Wait for last operation to be completed */
  490. return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
  491. }
  492. /**
  493. * @}
  494. */
  495. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State functions
  496. * @brief Peripheral State functions
  497. *
  498. @verbatim
  499. ===============================================================================
  500. ##### Peripheral State functions #####
  501. ===============================================================================
  502. [..]
  503. This subsection permit to get in run-time the status 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 Program a half-word (16-bit) at a specified address.
  527. * @param Address: specifies the address to be programmed.
  528. * @param Data: specifies the data to be programmed.
  529. * @retval None
  530. */
  531. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
  532. {
  533. /* Clean the error context */
  534. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  535. /* Proceed to program the new data */
  536. SET_BIT(FLASH->CR, FLASH_CR_PG);
  537. /* Write data in the address */
  538. *(__IO uint16_t*)Address = Data;
  539. }
  540. /**
  541. * @brief Wait for a FLASH operation to complete.
  542. * @param Timeout: maximum flash operation timeout
  543. * @retval HAL_StatusTypeDef HAL Status
  544. */
  545. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  546. {
  547. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  548. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  549. flag will be set */
  550. uint32_t tickstart = HAL_GetTick();
  551. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  552. {
  553. if (Timeout != HAL_MAX_DELAY)
  554. {
  555. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  556. {
  557. return HAL_TIMEOUT;
  558. }
  559. }
  560. }
  561. /* Check FLASH End of Operation flag */
  562. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  563. {
  564. /* Clear FLASH End of Operation pending bit */
  565. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  566. }
  567. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  568. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  569. {
  570. /*Save the error code*/
  571. FLASH_SetErrorCode();
  572. return HAL_ERROR;
  573. }
  574. /* If there is no error flag set */
  575. return HAL_OK;
  576. }
  577. /**
  578. * @brief Set the specific FLASH error flag.
  579. * @retval None
  580. */
  581. static void FLASH_SetErrorCode(void)
  582. {
  583. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  584. {
  585. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  586. }
  587. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  588. {
  589. pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
  590. }
  591. /* Clear FLASH error pending bits */
  592. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
  593. }
  594. /**
  595. * @}
  596. */
  597. /**
  598. * @}
  599. */
  600. #endif /* HAL_FLASH_MODULE_ENABLED */
  601. /**
  602. * @}
  603. */
  604. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/