stm32f7xx_hal_flash.c 25 KB

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