stm32f7xx_hal_flash.c 25 KB

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