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.
 
 
 

787 lines
24 KiB

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