You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_flash.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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.
  58. * All rights reserved.</center></h2>
  59. *
  60. * This software component is licensed by ST under BSD 3-Clause license,
  61. * the "License"; You may not use this file except in compliance with the
  62. * License. You may obtain a copy of the License at:
  63. * opensource.org/licenses/BSD-3-Clause
  64. *
  65. ******************************************************************************
  66. */
  67. /* Includes ------------------------------------------------------------------*/
  68. #include "stm32f4xx_hal.h"
  69. /** @addtogroup STM32F4xx_HAL_Driver
  70. * @{
  71. */
  72. /** @defgroup FLASH FLASH
  73. * @brief FLASH HAL module driver
  74. * @{
  75. */
  76. #ifdef HAL_FLASH_MODULE_ENABLED
  77. /* Private typedef -----------------------------------------------------------*/
  78. /* Private define ------------------------------------------------------------*/
  79. /** @addtogroup FLASH_Private_Constants
  80. * @{
  81. */
  82. #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
  83. /**
  84. * @}
  85. */
  86. /* Private macro -------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /** @addtogroup FLASH_Private_Variables
  89. * @{
  90. */
  91. /* Variable used for Erase sectors under interruption */
  92. FLASH_ProcessTypeDef pFlash;
  93. /**
  94. * @}
  95. */
  96. /* Private function prototypes -----------------------------------------------*/
  97. /** @addtogroup FLASH_Private_Functions
  98. * @{
  99. */
  100. /* Program operations */
  101. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
  102. static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
  103. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
  104. static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
  105. static void FLASH_SetErrorCode(void);
  106. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  107. /**
  108. * @}
  109. */
  110. /* Exported functions --------------------------------------------------------*/
  111. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  112. * @{
  113. */
  114. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  115. * @brief Programming operation functions
  116. *
  117. @verbatim
  118. ===============================================================================
  119. ##### Programming operation functions #####
  120. ===============================================================================
  121. [..]
  122. This subsection provides a set of functions allowing to manage the FLASH
  123. program operations.
  124. @endverbatim
  125. * @{
  126. */
  127. /**
  128. * @brief Program byte, halfword, word or double word at a specified address
  129. * @param TypeProgram Indicate the way to program at a specified address.
  130. * This parameter can be a value of @ref FLASH_Type_Program
  131. * @param Address specifies the address to be programmed.
  132. * @param Data specifies the data to be programmed
  133. *
  134. * @retval HAL_StatusTypeDef HAL Status
  135. */
  136. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  137. {
  138. HAL_StatusTypeDef status = HAL_ERROR;
  139. /* Process Locked */
  140. __HAL_LOCK(&pFlash);
  141. /* Check the parameters */
  142. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  143. /* Wait for last operation to be completed */
  144. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  145. if(status == HAL_OK)
  146. {
  147. if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  148. {
  149. /*Program byte (8-bit) at a specified address.*/
  150. FLASH_Program_Byte(Address, (uint8_t) Data);
  151. }
  152. else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  153. {
  154. /*Program halfword (16-bit) at a specified address.*/
  155. FLASH_Program_HalfWord(Address, (uint16_t) Data);
  156. }
  157. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  158. {
  159. /*Program word (32-bit) at a specified address.*/
  160. FLASH_Program_Word(Address, (uint32_t) Data);
  161. }
  162. else
  163. {
  164. /*Program double word (64-bit) at a specified address.*/
  165. FLASH_Program_DoubleWord(Address, Data);
  166. }
  167. /* Wait for last operation to be completed */
  168. status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
  169. /* If the program operation is completed, disable the PG Bit */
  170. FLASH->CR &= (~FLASH_CR_PG);
  171. }
  172. /* Process Unlocked */
  173. __HAL_UNLOCK(&pFlash);
  174. return status;
  175. }
  176. /**
  177. * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
  178. * @param TypeProgram Indicate the way to program at a specified address.
  179. * This parameter can be a value of @ref FLASH_Type_Program
  180. * @param Address specifies the address to be programmed.
  181. * @param Data specifies the data to be programmed
  182. *
  183. * @retval HAL Status
  184. */
  185. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  186. {
  187. HAL_StatusTypeDef status = HAL_OK;
  188. /* Process Locked */
  189. __HAL_LOCK(&pFlash);
  190. /* Check the parameters */
  191. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  192. /* Enable End of FLASH Operation interrupt */
  193. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  194. /* Enable Error source interrupt */
  195. __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  196. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  197. pFlash.Address = Address;
  198. if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  199. {
  200. /*Program byte (8-bit) at a specified address.*/
  201. FLASH_Program_Byte(Address, (uint8_t) Data);
  202. }
  203. else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  204. {
  205. /*Program halfword (16-bit) at a specified address.*/
  206. FLASH_Program_HalfWord(Address, (uint16_t) Data);
  207. }
  208. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  209. {
  210. /*Program word (32-bit) at a specified address.*/
  211. FLASH_Program_Word(Address, (uint32_t) Data);
  212. }
  213. else
  214. {
  215. /*Program double word (64-bit) at a specified address.*/
  216. FLASH_Program_DoubleWord(Address, Data);
  217. }
  218. return status;
  219. }
  220. /**
  221. * @brief This function handles FLASH interrupt request.
  222. * @retval None
  223. */
  224. void HAL_FLASH_IRQHandler(void)
  225. {
  226. uint32_t addresstmp = 0U;
  227. /* Check FLASH operation error flags */
  228. #if defined(FLASH_SR_RDERR)
  229. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  230. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  231. #else
  232. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  233. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
  234. #endif /* FLASH_SR_RDERR */
  235. {
  236. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
  237. {
  238. /*return the faulty sector*/
  239. addresstmp = pFlash.Sector;
  240. pFlash.Sector = 0xFFFFFFFFU;
  241. }
  242. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  243. {
  244. /*return the faulty bank*/
  245. addresstmp = pFlash.Bank;
  246. }
  247. else
  248. {
  249. /*return the faulty address*/
  250. addresstmp = pFlash.Address;
  251. }
  252. /*Save the Error code*/
  253. FLASH_SetErrorCode();
  254. /* FLASH error interrupt user callback */
  255. HAL_FLASH_OperationErrorCallback(addresstmp);
  256. /*Stop the procedure ongoing*/
  257. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  258. }
  259. /* Check FLASH End of Operation flag */
  260. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  261. {
  262. /* Clear FLASH End of Operation pending bit */
  263. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  264. if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
  265. {
  266. /*Nb of sector to erased can be decreased*/
  267. pFlash.NbSectorsToErase--;
  268. /* Check if there are still sectors to erase*/
  269. if(pFlash.NbSectorsToErase != 0U)
  270. {
  271. addresstmp = pFlash.Sector;
  272. /*Indicate user which sector has been erased*/
  273. HAL_FLASH_EndOfOperationCallback(addresstmp);
  274. /*Increment sector number*/
  275. pFlash.Sector++;
  276. addresstmp = pFlash.Sector;
  277. FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
  278. }
  279. else
  280. {
  281. /*No more sectors to Erase, user callback can be called.*/
  282. /*Reset Sector and stop Erase sectors procedure*/
  283. pFlash.Sector = addresstmp = 0xFFFFFFFFU;
  284. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  285. /* Flush the caches to be sure of the data consistency */
  286. FLASH_FlushCaches() ;
  287. /* FLASH EOP interrupt user callback */
  288. HAL_FLASH_EndOfOperationCallback(addresstmp);
  289. }
  290. }
  291. else
  292. {
  293. if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  294. {
  295. /* MassErase ended. Return the selected bank */
  296. /* Flush the caches to be sure of the data consistency */
  297. FLASH_FlushCaches() ;
  298. /* FLASH EOP interrupt user callback */
  299. HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
  300. }
  301. else
  302. {
  303. /*Program ended. Return the selected address*/
  304. /* FLASH EOP interrupt user callback */
  305. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  306. }
  307. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  308. }
  309. }
  310. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  311. {
  312. /* Operation is completed, disable the PG, SER, SNB and MER Bits */
  313. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
  314. /* Disable End of FLASH Operation interrupt */
  315. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
  316. /* Disable Error source interrupt */
  317. __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
  318. /* Process Unlocked */
  319. __HAL_UNLOCK(&pFlash);
  320. }
  321. }
  322. /**
  323. * @brief FLASH end of operation interrupt callback
  324. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  325. * Mass Erase: Bank number which has been requested to erase
  326. * Sectors Erase: Sector which has been erased
  327. * (if 0xFFFFFFFFU, it means that all the selected sectors have been erased)
  328. * Program: Address which was selected for data program
  329. * @retval None
  330. */
  331. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  332. {
  333. /* Prevent unused argument(s) compilation warning */
  334. UNUSED(ReturnValue);
  335. /* NOTE : This function Should not be modified, when the callback is needed,
  336. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  337. */
  338. }
  339. /**
  340. * @brief FLASH operation error interrupt callback
  341. * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
  342. * Mass Erase: Bank number which has been requested to erase
  343. * Sectors Erase: Sector number which returned an error
  344. * Program: Address which was selected for data program
  345. * @retval None
  346. */
  347. __weak void HAL_FLASH_OperationErrorCallback(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_OperationErrorCallback could be implemented in the user file
  353. */
  354. }
  355. /**
  356. * @}
  357. */
  358. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  359. * @brief management functions
  360. *
  361. @verbatim
  362. ===============================================================================
  363. ##### Peripheral Control functions #####
  364. ===============================================================================
  365. [..]
  366. This subsection provides a set of functions allowing to control the FLASH
  367. memory operations.
  368. @endverbatim
  369. * @{
  370. */
  371. /**
  372. * @brief Unlock the FLASH control register access
  373. * @retval HAL Status
  374. */
  375. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  376. {
  377. HAL_StatusTypeDef status = HAL_OK;
  378. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  379. {
  380. /* Authorize the FLASH Registers access */
  381. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  382. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  383. /* Verify Flash is unlocked */
  384. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  385. {
  386. status = HAL_ERROR;
  387. }
  388. }
  389. return status;
  390. }
  391. /**
  392. * @brief Locks the FLASH control register access
  393. * @retval HAL Status
  394. */
  395. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  396. {
  397. /* Set the LOCK Bit to lock the FLASH Registers access */
  398. FLASH->CR |= FLASH_CR_LOCK;
  399. return HAL_OK;
  400. }
  401. /**
  402. * @brief Unlock the FLASH Option Control Registers access.
  403. * @retval HAL Status
  404. */
  405. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  406. {
  407. if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
  408. {
  409. /* Authorizes the Option Byte register programming */
  410. FLASH->OPTKEYR = FLASH_OPT_KEY1;
  411. FLASH->OPTKEYR = FLASH_OPT_KEY2;
  412. }
  413. else
  414. {
  415. return HAL_ERROR;
  416. }
  417. return HAL_OK;
  418. }
  419. /**
  420. * @brief Lock the FLASH Option Control Registers access.
  421. * @retval HAL Status
  422. */
  423. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  424. {
  425. /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  426. FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
  427. return HAL_OK;
  428. }
  429. /**
  430. * @brief Launch the option byte loading.
  431. * @retval HAL Status
  432. */
  433. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  434. {
  435. /* Set the OPTSTRT bit in OPTCR register */
  436. *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
  437. /* Wait for last operation to be completed */
  438. return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
  439. }
  440. /**
  441. * @}
  442. */
  443. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  444. * @brief Peripheral Errors functions
  445. *
  446. @verbatim
  447. ===============================================================================
  448. ##### Peripheral Errors functions #####
  449. ===============================================================================
  450. [..]
  451. This subsection permits to get in run-time Errors of the FLASH peripheral.
  452. @endverbatim
  453. * @{
  454. */
  455. /**
  456. * @brief Get the specific FLASH error flag.
  457. * @retval FLASH_ErrorCode: The returned value can be a combination of:
  458. * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
  459. * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
  460. * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
  461. * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  462. * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
  463. * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
  464. */
  465. uint32_t HAL_FLASH_GetError(void)
  466. {
  467. return pFlash.ErrorCode;
  468. }
  469. /**
  470. * @}
  471. */
  472. /**
  473. * @brief Wait for a FLASH operation to complete.
  474. * @param Timeout maximum flash operationtimeout
  475. * @retval HAL Status
  476. */
  477. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  478. {
  479. uint32_t tickstart = 0U;
  480. /* Clear Error Code */
  481. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  482. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  483. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  484. flag will be set */
  485. /* Get tick */
  486. tickstart = HAL_GetTick();
  487. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
  488. {
  489. if(Timeout != HAL_MAX_DELAY)
  490. {
  491. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  492. {
  493. return HAL_TIMEOUT;
  494. }
  495. }
  496. }
  497. /* Check FLASH End of Operation flag */
  498. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  499. {
  500. /* Clear FLASH End of Operation pending bit */
  501. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  502. }
  503. #if defined(FLASH_SR_RDERR)
  504. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  505. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  506. #else
  507. if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
  508. FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
  509. #endif /* FLASH_SR_RDERR */
  510. {
  511. /*Save the error code*/
  512. FLASH_SetErrorCode();
  513. return HAL_ERROR;
  514. }
  515. /* If there is no error flag set */
  516. return HAL_OK;
  517. }
  518. /**
  519. * @brief Program a double word (64-bit) at a specified address.
  520. * @note This function must be used when the device voltage range is from
  521. * 2.7V to 3.6V and Vpp in the range 7V to 9V.
  522. *
  523. * @note If an erase and a program operations are requested simultaneously,
  524. * the erase operation is performed before the program one.
  525. *
  526. * @param Address specifies the address to be programmed.
  527. * @param Data specifies the data to be programmed.
  528. * @retval None
  529. */
  530. static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
  531. {
  532. /* Check the parameters */
  533. assert_param(IS_FLASH_ADDRESS(Address));
  534. /* If the previous operation is completed, proceed to program the new data */
  535. CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
  536. FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
  537. FLASH->CR |= FLASH_CR_PG;
  538. /* Program first word */
  539. *(__IO uint32_t*)Address = (uint32_t)Data;
  540. /* Barrier to ensure programming is performed in 2 steps, in right order
  541. (independently of compiler optimization behavior) */
  542. __ISB();
  543. /* Program second word */
  544. *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
  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 defined(FLASH_SR_RDERR)
  643. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
  644. {
  645. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  646. /* Clear FLASH Proprietary readout protection error pending bit */
  647. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
  648. }
  649. #endif /* FLASH_SR_RDERR */
  650. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
  651. {
  652. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
  653. /* Clear FLASH Operation error pending bit */
  654. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
  655. }
  656. }
  657. /**
  658. * @}
  659. */
  660. #endif /* HAL_FLASH_MODULE_ENABLED */
  661. /**
  662. * @}
  663. */
  664. /**
  665. * @}
  666. */
  667. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/