stm32f4xx_hal_flash.c 24 KB

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