stm32f3xx_hal_flash.c 21 KB

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