stm32f4xx_hal_flash.c 24 KB

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