stm32f4xx_hal_flash.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-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 FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
  101. /**
  102. * @}
  103. */
  104. /* Private macro -------------------------------------------------------------*/
  105. /* Private variables ---------------------------------------------------------*/
  106. /** @addtogroup FLASH_Private_Variables
  107. * @{
  108. */
  109. /* Variable used for Erase sectors under interruption */
  110. FLASH_ProcessTypeDef pFlash;
  111. /**
  112. * @}
  113. */
  114. /* Private function prototypes -----------------------------------------------*/
  115. /** @addtogroup FLASH_Private_Functions
  116. * @{
  117. */
  118. /* Program operations */
  119. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  120. static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
  121. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
  122. static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
  123. static void FLASH_SetErrorCode(void);
  124. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  125. /**
  126. * @}
  127. */
  128. /* Exported functions --------------------------------------------------------*/
  129. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  130. * @{
  131. */
  132. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  133. * @brief Programming operation functions
  134. *
  135. @verbatim
  136. ===============================================================================
  137. ##### Programming operation functions #####
  138. ===============================================================================
  139. [..]
  140. This subsection provides a set of functions allowing to manage the FLASH
  141. program operations.
  142. @endverbatim
  143. * @{
  144. */
  145. /**
  146. * @brief Program byte, halfword, word or double word at a specified address
  147. * @param TypeProgram: Indicate the way to program at a specified address.
  148. * This parameter can be a value of @ref FLASH_Type_Program
  149. * @param Address: specifies the address to be programmed.
  150. * @param Data: specifies the data to be programmed
  151. *
  152. * @retval HAL_StatusTypeDef HAL Status
  153. */
  154. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  155. {
  156. HAL_StatusTypeDef status = HAL_ERROR;
  157. /* Process Locked */
  158. __HAL_LOCK(&pFlash);
  159. /* Check the parameters */
  160. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  161. /* Wait for last operation to be completed */
  162. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  163. if(status == HAL_OK)
  164. {
  165. if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  166. {
  167. /*Program byte (8-bit) at a specified address.*/
  168. FLASH_Program_Byte(Address, (uint8_t) Data);
  169. }
  170. else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  171. {
  172. /*Program halfword (16-bit) at a specified address.*/
  173. FLASH_Program_HalfWord(Address, (uint16_t) Data);
  174. }
  175. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  176. {
  177. /*Program word (32-bit) at a specified address.*/
  178. FLASH_Program_Word(Address, (uint32_t) Data);
  179. }
  180. else
  181. {
  182. /*Program double word (64-bit) at a specified address.*/
  183. FLASH_Program_DoubleWord(Address, Data);
  184. }
  185. /* Wait for last operation to be completed */
  186. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  187. /* If the program operation is completed, disable the PG Bit */
  188. FLASH->CR &= (~FLASH_CR_PG);
  189. }
  190. /* Process Unlocked */
  191. __HAL_UNLOCK(&pFlash);
  192. return status;
  193. }
  194. /**
  195. * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
  196. * @param TypeProgram: Indicate the way to program at a specified address.
  197. * This parameter can be a value of @ref FLASH_Type_Program
  198. * @param Address: specifies the address to be programmed.
  199. * @param Data: specifies the data to be programmed
  200. *
  201. * @retval HAL Status
  202. */
  203. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  204. {
  205. HAL_StatusTypeDef status = HAL_OK;
  206. /* Process Locked */
  207. __HAL_LOCK(&pFlash);
  208. /* Check the parameters */
  209. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  210. /* Enable End of FLASH Operation interrupt */
  211. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  212. /* Enable Error source interrupt */
  213. __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  214. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  215. pFlash.Address = Address;
  216. if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  217. {
  218. /*Program byte (8-bit) at a specified address.*/
  219. FLASH_Program_Byte(Address, (uint8_t) Data);
  220. }
  221. else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  222. {
  223. /*Program halfword (16-bit) at a specified address.*/
  224. FLASH_Program_HalfWord(Address, (uint16_t) Data);
  225. }
  226. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  227. {
  228. /*Program word (32-bit) at a specified address.*/
  229. FLASH_Program_Word(Address, (uint32_t) Data);
  230. }
  231. else
  232. {
  233. /*Program double word (64-bit) at a specified address.*/
  234. FLASH_Program_DoubleWord(Address, Data);
  235. }
  236. return status;
  237. }
  238. /**
  239. * @brief This function handles FLASH interrupt request.
  240. * @retval None
  241. */
  242. void HAL_FLASH_IRQHandler(void)
  243. {
  244. uint32_t addresstmp = 0;
  245. /* Check FLASH operation error flags */
  246. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  247. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  248. {
  249. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
  250. {
  251. /*return the faulty sector*/
  252. addresstmp = pFlash.Sector;
  253. pFlash.Sector = 0xFFFFFFFF;
  254. }
  255. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  256. {
  257. /*return the faulty bank*/
  258. addresstmp = pFlash.Bank;
  259. }
  260. else
  261. {
  262. /*return the faulty address*/
  263. addresstmp = pFlash.Address;
  264. }
  265. /*Save the Error code*/
  266. FLASH_SetErrorCode();
  267. /* FLASH error interrupt user callback */
  268. HAL_FLASH_OperationErrorCallback(addresstmp);
  269. /*Stop the procedure ongoing*/
  270. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  271. }
  272. /* Check FLASH End of Operation flag */
  273. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  274. {
  275. /* Clear FLASH End of Operation pending bit */
  276. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  277. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
  278. {
  279. /*Nb of sector to erased can be decreased*/
  280. pFlash.NbSectorsToErase--;
  281. /* Check if there are still sectors to erase*/
  282. if(pFlash.NbSectorsToErase != 0)
  283. {
  284. addresstmp = pFlash.Sector;
  285. /*Indicate user which sector has been erased*/
  286. HAL_FLASH_EndOfOperationCallback(addresstmp);
  287. /*Increment sector number*/
  288. pFlash.Sector++;
  289. addresstmp = pFlash.Sector;
  290. FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
  291. }
  292. else
  293. {
  294. /*No more sectors to Erase, user callback can be called.*/
  295. /*Reset Sector and stop Erase sectors procedure*/
  296. pFlash.Sector = addresstmp = 0xFFFFFFFF;
  297. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  298. /* Flush the caches to be sure of the data consistency */
  299. FLASH_FlushCaches() ;
  300. /* FLASH EOP interrupt user callback */
  301. HAL_FLASH_EndOfOperationCallback(addresstmp);
  302. }
  303. }
  304. else
  305. {
  306. if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  307. {
  308. /* MassErase ended. Return the selected bank */
  309. /* Flush the caches to be sure of the data consistency */
  310. FLASH_FlushCaches() ;
  311. /* FLASH EOP interrupt user callback */
  312. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  313. }
  314. else
  315. {
  316. /*Program ended. Return the selected address*/
  317. /* FLASH EOP interrupt user callback */
  318. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  319. }
  320. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  321. }
  322. }
  323. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  324. {
  325. /* Operation is completed, disable the PG, SER, SNB and MER Bits */
  326. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
  327. /* Disable End of FLASH Operation interrupt */
  328. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
  329. /* Disable Error source interrupt */
  330. __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
  331. /* Process Unlocked */
  332. __HAL_UNLOCK(&pFlash);
  333. }
  334. }
  335. /**
  336. * @brief FLASH end of operation interrupt callback
  337. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  338. * Mass Erase: Bank number which has been requested to erase
  339. * Sectors Erase: Sector which has been erased
  340. * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
  341. * Program: Address which was selected for data program
  342. * @retval None
  343. */
  344. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  345. {
  346. /* Prevent unused argument(s) compilation warning */
  347. UNUSED(ReturnValue);
  348. /* NOTE : This function Should not be modified, when the callback is needed,
  349. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  350. */
  351. }
  352. /**
  353. * @brief FLASH operation error interrupt callback
  354. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  355. * Mass Erase: Bank number which has been requested to erase
  356. * Sectors Erase: Sector number which returned an error
  357. * Program: Address which was selected for data program
  358. * @retval None
  359. */
  360. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  361. {
  362. /* Prevent unused argument(s) compilation warning */
  363. UNUSED(ReturnValue);
  364. /* NOTE : This function Should not be modified, when the callback is needed,
  365. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  366. */
  367. }
  368. /**
  369. * @}
  370. */
  371. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  372. * @brief management functions
  373. *
  374. @verbatim
  375. ===============================================================================
  376. ##### Peripheral Control functions #####
  377. ===============================================================================
  378. [..]
  379. This subsection provides a set of functions allowing to control the FLASH
  380. memory operations.
  381. @endverbatim
  382. * @{
  383. */
  384. /**
  385. * @brief Unlock the FLASH control register access
  386. * @retval HAL Status
  387. */
  388. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  389. {
  390. if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  391. {
  392. /* Authorize the FLASH Registers access */
  393. FLASH->KEYR = FLASH_KEY1;
  394. FLASH->KEYR = FLASH_KEY2;
  395. }
  396. else
  397. {
  398. return HAL_ERROR;
  399. }
  400. return HAL_OK;
  401. }
  402. /**
  403. * @brief Locks the FLASH control register access
  404. * @retval HAL Status
  405. */
  406. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  407. {
  408. /* Set the LOCK Bit to lock the FLASH Registers access */
  409. FLASH->CR |= FLASH_CR_LOCK;
  410. return HAL_OK;
  411. }
  412. /**
  413. * @brief Unlock the FLASH Option Control Registers access.
  414. * @retval HAL Status
  415. */
  416. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  417. {
  418. if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
  419. {
  420. /* Authorizes the Option Byte register programming */
  421. FLASH->OPTKEYR = FLASH_OPT_KEY1;
  422. FLASH->OPTKEYR = FLASH_OPT_KEY2;
  423. }
  424. else
  425. {
  426. return HAL_ERROR;
  427. }
  428. return HAL_OK;
  429. }
  430. /**
  431. * @brief Lock the FLASH Option Control Registers access.
  432. * @retval HAL Status
  433. */
  434. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  435. {
  436. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  437. FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
  438. return HAL_OK;
  439. }
  440. /**
  441. * @brief Launch the option byte loading.
  442. * @retval HAL Status
  443. */
  444. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  445. {
  446. /* Set the OPTSTRT bit in OPTCR register */
  447. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  448. /* Wait for last operation to be completed */
  449. return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
  450. }
  451. /**
  452. * @}
  453. */
  454. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  455. * @brief Peripheral Errors functions
  456. *
  457. @verbatim
  458. ===============================================================================
  459. ##### Peripheral Errors functions #####
  460. ===============================================================================
  461. [..]
  462. This subsection permits to get in run-time Errors of the FLASH peripheral.
  463. @endverbatim
  464. * @{
  465. */
  466. /**
  467. * @brief Get the specific FLASH error flag.
  468. * @retval FLASH_ErrorCode: The returned value can be a combination of:
  469. * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
  470. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
  471. * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
  472. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  473. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
  474. * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
  475. */
  476. uint32_t HAL_FLASH_GetError(void)
  477. {
  478. return pFlash.ErrorCode;
  479. }
  480. /**
  481. * @}
  482. */
  483. /**
  484. * @brief Wait for a FLASH operation to complete.
  485. * @param Timeout: maximum flash operationtimeout
  486. * @retval HAL Status
  487. */
  488. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  489. {
  490. uint32_t tickstart = 0;
  491. /* Clear Error Code */
  492. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  493. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  494. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  495. flag will be set */
  496. /* Get tick */
  497. tickstart = HAL_GetTick();
  498. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
  499. {
  500. if(Timeout != HAL_MAX_DELAY)
  501. {
  502. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  503. {
  504. return HAL_TIMEOUT;
  505. }
  506. }
  507. }
  508. /* Check FLASH End of Operation flag */
  509. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  510. {
  511. /* Clear FLASH End of Operation pending bit */
  512. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  513. }
  514. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  515. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  516. {
  517. /*Save the error code*/
  518. FLASH_SetErrorCode();
  519. return HAL_ERROR;
  520. }
  521. /* If there is no error flag set */
  522. return HAL_OK;
  523. }
  524. /**
  525. * @brief Program a double word (64-bit) at a specified address.
  526. * @note This function must be used when the device voltage range is from
  527. * 2.7V to 3.6V and Vpp in the range 7V to 9V.
  528. *
  529. * @note If an erase and a program operations are requested simultaneously,
  530. * the erase operation is performed before the program one.
  531. *
  532. * @param Address: specifies the address to be programmed.
  533. * @param Data: specifies the data to be programmed.
  534. * @retval None
  535. */
  536. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  537. {
  538. /* Check the parameters */
  539. assert_param(IS_FLASH_ADDRESS(Address));
  540. /* If the previous operation is completed, proceed to program the new data */
  541. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  542. FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
  543. FLASH->CR |= FLASH_CR_PG;
  544. *(__IO uint64_t*)Address = Data;
  545. }
  546. /**
  547. * @brief Program word (32-bit) at a specified address.
  548. * @note This function must be used when the device voltage range is from
  549. * 2.7V to 3.6V.
  550. *
  551. * @note If an erase and a program operations are requested simultaneously,
  552. * the erase operation is performed before the program one.
  553. *
  554. * @param Address: specifies the address to be programmed.
  555. * @param Data: specifies the data to be programmed.
  556. * @retval None
  557. */
  558. static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
  559. {
  560. /* Check the parameters */
  561. assert_param(IS_FLASH_ADDRESS(Address));
  562. /* If the previous operation is completed, proceed to program the new data */
  563. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  564. FLASH->CR |= FLASH_PSIZE_WORD;
  565. FLASH->CR |= FLASH_CR_PG;
  566. *(__IO uint32_t*)Address = Data;
  567. }
  568. /**
  569. * @brief Program a half-word (16-bit) at a specified address.
  570. * @note This function must be used when the device voltage range is from
  571. * 2.1V to 3.6V.
  572. *
  573. * @note If an erase and a program operations are requested simultaneously,
  574. * the erase operation is performed before the program one.
  575. *
  576. * @param Address: specifies the address to be programmed.
  577. * @param Data: specifies the data to be programmed.
  578. * @retval None
  579. */
  580. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
  581. {
  582. /* Check the parameters */
  583. assert_param(IS_FLASH_ADDRESS(Address));
  584. /* If the previous operation is completed, proceed to program the new data */
  585. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  586. FLASH->CR |= FLASH_PSIZE_HALF_WORD;
  587. FLASH->CR |= FLASH_CR_PG;
  588. *(__IO uint16_t*)Address = Data;
  589. }
  590. /**
  591. * @brief Program byte (8-bit) at a specified address.
  592. * @note This function must be used when the device voltage range is from
  593. * 1.8V to 3.6V.
  594. *
  595. * @note If an erase and a program operations are requested simultaneously,
  596. * the erase operation is performed before the program one.
  597. *
  598. * @param Address: specifies the address to be programmed.
  599. * @param Data: specifies the data to be programmed.
  600. * @retval None
  601. */
  602. static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
  603. {
  604. /* Check the parameters */
  605. assert_param(IS_FLASH_ADDRESS(Address));
  606. /* If the previous operation is completed, proceed to program the new data */
  607. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  608. FLASH->CR |= FLASH_PSIZE_BYTE;
  609. FLASH->CR |= FLASH_CR_PG;
  610. *(__IO uint8_t*)Address = Data;
  611. }
  612. /**
  613. * @brief Set the specific FLASH error flag.
  614. * @retval None
  615. */
  616. static void FLASH_SetErrorCode(void)
  617. {
  618. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
  619. {
  620. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  621. /* Clear FLASH write protection error pending bit */
  622. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR);
  623. }
  624. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
  625. {
  626. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  627. /* Clear FLASH Programming alignment error pending bit */
  628. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
  629. }
  630. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
  631. {
  632. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
  633. /* Clear FLASH Programming parallelism error pending bit */
  634. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
  635. }
  636. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
  637. {
  638. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
  639. /* Clear FLASH Programming sequence error pending bit */
  640. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
  641. }
  642. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
  643. {
  644. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  645. /* Clear FLASH Proprietary readout protection error pending bit */
  646. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
  647. }
  648. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
  649. {
  650. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
  651. /* Clear FLASH Operation error pending bit */
  652. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
  653. }
  654. }
  655. /**
  656. * @}
  657. */
  658. #endif /* HAL_FLASH_MODULE_ENABLED */
  659. /**
  660. * @}
  661. */
  662. /**
  663. * @}
  664. */
  665. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/