stm32f4xx_hal_flash.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @brief FLASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the internal FLASH memory:
  8. * + Program operations functions
  9. * + Memory Control functions
  10. * + Peripheral Errors functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### FLASH peripheral features #####
  15. ==============================================================================
  16. [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
  17. to the Flash memory. It implements the erase and program Flash memory operations
  18. and the read and write protection mechanisms.
  19. [..] The Flash memory interface accelerates code execution with a system of instruction
  20. prefetch and cache lines.
  21. [..] The FLASH main features are:
  22. (+) Flash memory read operations
  23. (+) Flash memory program/erase operations
  24. (+) Read / write protections
  25. (+) Prefetch on I-Code
  26. (+) 64 cache lines of 128 bits on I-Code
  27. (+) 8 cache lines of 128 bits on D-Code
  28. ##### How to use this driver #####
  29. ==============================================================================
  30. [..]
  31. This driver provides functions and macros to configure and program the FLASH
  32. memory of all STM32F4xx devices.
  33. (#) FLASH Memory IO Programming functions:
  34. (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
  35. HAL_FLASH_Lock() functions
  36. (++) Program functions: byte, half word, word and double word
  37. (++) There Two modes of programming :
  38. (+++) Polling mode using HAL_FLASH_Program() function
  39. (+++) Interrupt mode using HAL_FLASH_Program_IT() function
  40. (#) Interrupts and flags management functions :
  41. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  42. (++) Wait for last FLASH operation according to its status
  43. (++) Get error flag status by calling HAL_SetErrorCode()
  44. [..]
  45. In addition to these functions, this driver includes a set of macros allowing
  46. to handle the following operations:
  47. (+) Set the latency
  48. (+) Enable/Disable the prefetch buffer
  49. (+) Enable/Disable the Instruction cache and the Data cache
  50. (+) Reset the Instruction cache and the Data cache
  51. (+) Enable/Disable the FLASH interrupts
  52. (+) Monitor the FLASH flags status
  53. @endverbatim
  54. ******************************************************************************
  55. * @attention
  56. *
  57. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  58. *
  59. * Redistribution and use in source and binary forms, with or without modification,
  60. * are permitted provided that the following conditions are met:
  61. * 1. Redistributions of source code must retain the above copyright notice,
  62. * this list of conditions and the following disclaimer.
  63. * 2. Redistributions in binary form must reproduce the above copyright notice,
  64. * this list of conditions and the following disclaimer in the documentation
  65. * and/or other materials provided with the distribution.
  66. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  67. * may be used to endorse or promote products derived from this software
  68. * without specific prior written permission.
  69. *
  70. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  71. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  73. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  74. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  75. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  76. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  77. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  78. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  79. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  80. *
  81. ******************************************************************************
  82. */
  83. /* Includes ------------------------------------------------------------------*/
  84. #include "stm32f4xx_hal.h"
  85. /** @addtogroup STM32F4xx_HAL_Driver
  86. * @{
  87. */
  88. /** @defgroup FLASH FLASH
  89. * @brief FLASH HAL module driver
  90. * @{
  91. */
  92. #ifdef HAL_FLASH_MODULE_ENABLED
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /** @addtogroup FLASH_Private_Constants
  96. * @{
  97. */
  98. #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
  99. /**
  100. * @}
  101. */
  102. /* Private macro -------------------------------------------------------------*/
  103. /* Private variables ---------------------------------------------------------*/
  104. /** @addtogroup FLASH_Private_Variables
  105. * @{
  106. */
  107. /* Variable used for Erase sectors under interruption */
  108. FLASH_ProcessTypeDef pFlash;
  109. /**
  110. * @}
  111. */
  112. /* Private function prototypes -----------------------------------------------*/
  113. /** @addtogroup FLASH_Private_Functions
  114. * @{
  115. */
  116. /* Program operations */
  117. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  118. static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
  119. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
  120. static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
  121. static void FLASH_SetErrorCode(void);
  122. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  123. /**
  124. * @}
  125. */
  126. /* Exported functions --------------------------------------------------------*/
  127. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  128. * @{
  129. */
  130. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  131. * @brief Programming operation functions
  132. *
  133. @verbatim
  134. ===============================================================================
  135. ##### Programming operation functions #####
  136. ===============================================================================
  137. [..]
  138. This subsection provides a set of functions allowing to manage the FLASH
  139. program operations.
  140. @endverbatim
  141. * @{
  142. */
  143. /**
  144. * @brief Program byte, halfword, word or double word at a specified address
  145. * @param TypeProgram Indicate the way to program at a specified address.
  146. * This parameter can be a value of @ref FLASH_Type_Program
  147. * @param Address specifies the address to be programmed.
  148. * @param Data specifies the data to be programmed
  149. *
  150. * @retval HAL_StatusTypeDef HAL Status
  151. */
  152. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  153. {
  154. HAL_StatusTypeDef status = HAL_ERROR;
  155. /* Process Locked */
  156. __HAL_LOCK(&pFlash);
  157. /* Check the parameters */
  158. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  159. /* Wait for last operation to be completed */
  160. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  161. if(status == HAL_OK)
  162. {
  163. if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  164. {
  165. /*Program byte (8-bit) at a specified address.*/
  166. FLASH_Program_Byte(Address, (uint8_t) Data);
  167. }
  168. else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  169. {
  170. /*Program halfword (16-bit) at a specified address.*/
  171. FLASH_Program_HalfWord(Address, (uint16_t) Data);
  172. }
  173. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  174. {
  175. /*Program word (32-bit) at a specified address.*/
  176. FLASH_Program_Word(Address, (uint32_t) Data);
  177. }
  178. else
  179. {
  180. /*Program double word (64-bit) at a specified address.*/
  181. FLASH_Program_DoubleWord(Address, Data);
  182. }
  183. /* Wait for last operation to be completed */
  184. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  185. /* If the program operation is completed, disable the PG Bit */
  186. FLASH->CR &= (~FLASH_CR_PG);
  187. }
  188. /* Process Unlocked */
  189. __HAL_UNLOCK(&pFlash);
  190. return status;
  191. }
  192. /**
  193. * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
  194. * @param TypeProgram Indicate the way to program at a specified address.
  195. * This parameter can be a value of @ref FLASH_Type_Program
  196. * @param Address specifies the address to be programmed.
  197. * @param Data specifies the data to be programmed
  198. *
  199. * @retval HAL Status
  200. */
  201. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  202. {
  203. HAL_StatusTypeDef status = HAL_OK;
  204. /* Process Locked */
  205. __HAL_LOCK(&pFlash);
  206. /* Check the parameters */
  207. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  208. /* Enable End of FLASH Operation interrupt */
  209. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  210. /* Enable Error source interrupt */
  211. __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  212. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  213. pFlash.Address = Address;
  214. if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  215. {
  216. /*Program byte (8-bit) at a specified address.*/
  217. FLASH_Program_Byte(Address, (uint8_t) Data);
  218. }
  219. else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  220. {
  221. /*Program halfword (16-bit) at a specified address.*/
  222. FLASH_Program_HalfWord(Address, (uint16_t) Data);
  223. }
  224. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  225. {
  226. /*Program word (32-bit) at a specified address.*/
  227. FLASH_Program_Word(Address, (uint32_t) Data);
  228. }
  229. else
  230. {
  231. /*Program double word (64-bit) at a specified address.*/
  232. FLASH_Program_DoubleWord(Address, Data);
  233. }
  234. return status;
  235. }
  236. /**
  237. * @brief This function handles FLASH interrupt request.
  238. * @retval None
  239. */
  240. void HAL_FLASH_IRQHandler(void)
  241. {
  242. uint32_t addresstmp = 0U;
  243. /* Check FLASH operation error flags */
  244. #if defined(FLASH_SR_RDERR)
  245. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  246. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  247. #else
  248. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  249. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
  250. #endif /* FLASH_SR_RDERR */
  251. {
  252. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
  253. {
  254. /*return the faulty sector*/
  255. addresstmp = pFlash.Sector;
  256. pFlash.Sector = 0xFFFFFFFFU;
  257. }
  258. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  259. {
  260. /*return the faulty bank*/
  261. addresstmp = pFlash.Bank;
  262. }
  263. else
  264. {
  265. /*return the faulty address*/
  266. addresstmp = pFlash.Address;
  267. }
  268. /*Save the Error code*/
  269. FLASH_SetErrorCode();
  270. /* FLASH error interrupt user callback */
  271. HAL_FLASH_OperationErrorCallback(addresstmp);
  272. /*Stop the procedure ongoing*/
  273. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  274. }
  275. /* Check FLASH End of Operation flag */
  276. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  277. {
  278. /* Clear FLASH End of Operation pending bit */
  279. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  280. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
  281. {
  282. /*Nb of sector to erased can be decreased*/
  283. pFlash.NbSectorsToErase--;
  284. /* Check if there are still sectors to erase*/
  285. if(pFlash.NbSectorsToErase != 0U)
  286. {
  287. addresstmp = pFlash.Sector;
  288. /*Indicate user which sector has been erased*/
  289. HAL_FLASH_EndOfOperationCallback(addresstmp);
  290. /*Increment sector number*/
  291. pFlash.Sector++;
  292. addresstmp = pFlash.Sector;
  293. FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
  294. }
  295. else
  296. {
  297. /*No more sectors to Erase, user callback can be called.*/
  298. /*Reset Sector and stop Erase sectors procedure*/
  299. pFlash.Sector = addresstmp = 0xFFFFFFFFU;
  300. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  301. /* Flush the caches to be sure of the data consistency */
  302. FLASH_FlushCaches() ;
  303. /* FLASH EOP interrupt user callback */
  304. HAL_FLASH_EndOfOperationCallback(addresstmp);
  305. }
  306. }
  307. else
  308. {
  309. if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  310. {
  311. /* MassErase ended. Return the selected bank */
  312. /* Flush the caches to be sure of the data consistency */
  313. FLASH_FlushCaches() ;
  314. /* FLASH EOP interrupt user callback */
  315. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  316. }
  317. else
  318. {
  319. /*Program ended. Return the selected address*/
  320. /* FLASH EOP interrupt user callback */
  321. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  322. }
  323. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  324. }
  325. }
  326. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  327. {
  328. /* Operation is completed, disable the PG, SER, SNB and MER Bits */
  329. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
  330. /* Disable End of FLASH Operation interrupt */
  331. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
  332. /* Disable Error source interrupt */
  333. __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
  334. /* Process Unlocked */
  335. __HAL_UNLOCK(&pFlash);
  336. }
  337. }
  338. /**
  339. * @brief FLASH end of operation interrupt callback
  340. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  341. * Mass Erase: Bank number which has been requested to erase
  342. * Sectors Erase: Sector which has been erased
  343. * (if 0xFFFFFFFFU, it means that all the selected sectors have been erased)
  344. * Program: Address which was selected for data program
  345. * @retval None
  346. */
  347. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  348. {
  349. /* Prevent unused argument(s) compilation warning */
  350. UNUSED(ReturnValue);
  351. /* NOTE : This function Should not be modified, when the callback is needed,
  352. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  353. */
  354. }
  355. /**
  356. * @brief FLASH operation error interrupt callback
  357. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  358. * Mass Erase: Bank number which has been requested to erase
  359. * Sectors Erase: Sector number which returned an error
  360. * Program: Address which was selected for data program
  361. * @retval None
  362. */
  363. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  364. {
  365. /* Prevent unused argument(s) compilation warning */
  366. UNUSED(ReturnValue);
  367. /* NOTE : This function Should not be modified, when the callback is needed,
  368. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  369. */
  370. }
  371. /**
  372. * @}
  373. */
  374. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  375. * @brief management functions
  376. *
  377. @verbatim
  378. ===============================================================================
  379. ##### Peripheral Control functions #####
  380. ===============================================================================
  381. [..]
  382. This subsection provides a set of functions allowing to control the FLASH
  383. memory operations.
  384. @endverbatim
  385. * @{
  386. */
  387. /**
  388. * @brief Unlock the FLASH control register access
  389. * @retval HAL Status
  390. */
  391. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  392. {
  393. HAL_StatusTypeDef status = HAL_OK;
  394. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  395. {
  396. /* Authorize the FLASH Registers access */
  397. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  398. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  399. /* Verify Flash is unlocked */
  400. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  401. {
  402. status = HAL_ERROR;
  403. }
  404. }
  405. return status;
  406. }
  407. /**
  408. * @brief Locks the FLASH control register access
  409. * @retval HAL Status
  410. */
  411. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  412. {
  413. /* Set the LOCK Bit to lock the FLASH Registers access */
  414. FLASH->CR |= FLASH_CR_LOCK;
  415. return HAL_OK;
  416. }
  417. /**
  418. * @brief Unlock the FLASH Option Control Registers access.
  419. * @retval HAL Status
  420. */
  421. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  422. {
  423. if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
  424. {
  425. /* Authorizes the Option Byte register programming */
  426. FLASH->OPTKEYR = FLASH_OPT_KEY1;
  427. FLASH->OPTKEYR = FLASH_OPT_KEY2;
  428. }
  429. else
  430. {
  431. return HAL_ERROR;
  432. }
  433. return HAL_OK;
  434. }
  435. /**
  436. * @brief Lock the FLASH Option Control Registers access.
  437. * @retval HAL Status
  438. */
  439. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  440. {
  441. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  442. FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
  443. return HAL_OK;
  444. }
  445. /**
  446. * @brief Launch the option byte loading.
  447. * @retval HAL Status
  448. */
  449. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  450. {
  451. /* Set the OPTSTRT bit in OPTCR register */
  452. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  453. /* Wait for last operation to be completed */
  454. return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
  455. }
  456. /**
  457. * @}
  458. */
  459. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  460. * @brief Peripheral Errors functions
  461. *
  462. @verbatim
  463. ===============================================================================
  464. ##### Peripheral Errors functions #####
  465. ===============================================================================
  466. [..]
  467. This subsection permits to get in run-time Errors of the FLASH peripheral.
  468. @endverbatim
  469. * @{
  470. */
  471. /**
  472. * @brief Get the specific FLASH error flag.
  473. * @retval FLASH_ErrorCode: The returned value can be a combination of:
  474. * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
  475. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
  476. * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
  477. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  478. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
  479. * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
  480. */
  481. uint32_t HAL_FLASH_GetError(void)
  482. {
  483. return pFlash.ErrorCode;
  484. }
  485. /**
  486. * @}
  487. */
  488. /**
  489. * @brief Wait for a FLASH operation to complete.
  490. * @param Timeout maximum flash operationtimeout
  491. * @retval HAL Status
  492. */
  493. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  494. {
  495. uint32_t tickstart = 0U;
  496. /* Clear Error Code */
  497. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  498. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  499. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  500. flag will be set */
  501. /* Get tick */
  502. tickstart = HAL_GetTick();
  503. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
  504. {
  505. if(Timeout != HAL_MAX_DELAY)
  506. {
  507. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  508. {
  509. return HAL_TIMEOUT;
  510. }
  511. }
  512. }
  513. /* Check FLASH End of Operation flag */
  514. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  515. {
  516. /* Clear FLASH End of Operation pending bit */
  517. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  518. }
  519. #if defined(FLASH_SR_RDERR)
  520. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  521. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  522. #else
  523. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  524. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
  525. #endif /* FLASH_SR_RDERR */
  526. {
  527. /*Save the error code*/
  528. FLASH_SetErrorCode();
  529. return HAL_ERROR;
  530. }
  531. /* If there is no error flag set */
  532. return HAL_OK;
  533. }
  534. /**
  535. * @brief Program a double word (64-bit) at a specified address.
  536. * @note This function must be used when the device voltage range is from
  537. * 2.7V to 3.6V and Vpp in the range 7V to 9V.
  538. *
  539. * @note If an erase and a program operations are requested simultaneously,
  540. * the erase operation is performed before the program one.
  541. *
  542. * @param Address specifies the address to be programmed.
  543. * @param Data specifies the data to be programmed.
  544. * @retval None
  545. */
  546. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  547. {
  548. /* Check the parameters */
  549. assert_param(IS_FLASH_ADDRESS(Address));
  550. /* If the previous operation is completed, proceed to program the new data */
  551. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  552. FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
  553. FLASH->CR |= FLASH_CR_PG;
  554. /* Program the double-word */
  555. *(__IO uint32_t*)Address = (uint32_t)Data;
  556. *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
  557. }
  558. /**
  559. * @brief Program word (32-bit) at a specified address.
  560. * @note This function must be used when the device voltage range is from
  561. * 2.7V to 3.6V.
  562. *
  563. * @note If an erase and a program operations are requested simultaneously,
  564. * the erase operation is performed before the program one.
  565. *
  566. * @param Address specifies the address to be programmed.
  567. * @param Data specifies the data to be programmed.
  568. * @retval None
  569. */
  570. static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
  571. {
  572. /* Check the parameters */
  573. assert_param(IS_FLASH_ADDRESS(Address));
  574. /* If the previous operation is completed, proceed to program the new data */
  575. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  576. FLASH->CR |= FLASH_PSIZE_WORD;
  577. FLASH->CR |= FLASH_CR_PG;
  578. *(__IO uint32_t*)Address = Data;
  579. }
  580. /**
  581. * @brief Program a half-word (16-bit) at a specified address.
  582. * @note This function must be used when the device voltage range is from
  583. * 2.1V to 3.6V.
  584. *
  585. * @note If an erase and a program operations are requested simultaneously,
  586. * the erase operation is performed before the program one.
  587. *
  588. * @param Address specifies the address to be programmed.
  589. * @param Data specifies the data to be programmed.
  590. * @retval None
  591. */
  592. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
  593. {
  594. /* Check the parameters */
  595. assert_param(IS_FLASH_ADDRESS(Address));
  596. /* If the previous operation is completed, proceed to program the new data */
  597. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  598. FLASH->CR |= FLASH_PSIZE_HALF_WORD;
  599. FLASH->CR |= FLASH_CR_PG;
  600. *(__IO uint16_t*)Address = Data;
  601. }
  602. /**
  603. * @brief Program byte (8-bit) at a specified address.
  604. * @note This function must be used when the device voltage range is from
  605. * 1.8V to 3.6V.
  606. *
  607. * @note If an erase and a program operations are requested simultaneously,
  608. * the erase operation is performed before the program one.
  609. *
  610. * @param Address specifies the address to be programmed.
  611. * @param Data specifies the data to be programmed.
  612. * @retval None
  613. */
  614. static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
  615. {
  616. /* Check the parameters */
  617. assert_param(IS_FLASH_ADDRESS(Address));
  618. /* If the previous operation is completed, proceed to program the new data */
  619. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  620. FLASH->CR |= FLASH_PSIZE_BYTE;
  621. FLASH->CR |= FLASH_CR_PG;
  622. *(__IO uint8_t*)Address = Data;
  623. }
  624. /**
  625. * @brief Set the specific FLASH error flag.
  626. * @retval None
  627. */
  628. static void FLASH_SetErrorCode(void)
  629. {
  630. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
  631. {
  632. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  633. /* Clear FLASH write protection error pending bit */
  634. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR);
  635. }
  636. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
  637. {
  638. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  639. /* Clear FLASH Programming alignment error pending bit */
  640. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
  641. }
  642. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
  643. {
  644. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
  645. /* Clear FLASH Programming parallelism error pending bit */
  646. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
  647. }
  648. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
  649. {
  650. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
  651. /* Clear FLASH Programming sequence error pending bit */
  652. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
  653. }
  654. #if defined(FLASH_SR_RDERR)
  655. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
  656. {
  657. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  658. /* Clear FLASH Proprietary readout protection error pending bit */
  659. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
  660. }
  661. #endif /* FLASH_SR_RDERR */
  662. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
  663. {
  664. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
  665. /* Clear FLASH Operation error pending bit */
  666. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
  667. }
  668. }
  669. /**
  670. * @}
  671. */
  672. #endif /* HAL_FLASH_MODULE_ENABLED */
  673. /**
  674. * @}
  675. */
  676. /**
  677. * @}
  678. */
  679. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/