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.

stm32l1xx_hal_flash_ramfunc.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_flash_ramfunc.c
  4. * @author MCD Application Team
  5. * @brief FLASH RAMFUNC driver.
  6. * This file provides a Flash firmware functions which should be
  7. * executed from internal SRAM
  8. *
  9. * @verbatim
  10. *** ARM Compiler ***
  11. --------------------
  12. [..] RAM functions are defined using the toolchain options.
  13. Functions that are be executed in RAM should reside in a separate
  14. source module. Using the 'Options for File' dialog you can simply change
  15. the 'Code / Const' area of a module to a memory space in physical RAM.
  16. Available memory areas are declared in the 'Target' tab of the
  17. Options for Target' dialog.
  18. *** ICCARM Compiler ***
  19. -----------------------
  20. [..] RAM functions are defined using a specific toolchain keyword "__ramfunc".
  21. *** GNU Compiler ***
  22. --------------------
  23. [..] RAM functions are defined using a specific toolchain attribute
  24. "__attribute__((section(".RamFunc")))".
  25. @endverbatim
  26. ******************************************************************************
  27. * @attention
  28. *
  29. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  30. * All rights reserved.</center></h2>
  31. *
  32. * This software component is licensed by ST under BSD 3-Clause license,
  33. * the "License"; You may not use this file except in compliance with the
  34. * License. You may obtain a copy of the License at:
  35. * opensource.org/licenses/BSD-3-Clause
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32l1xx_hal.h"
  41. /** @addtogroup STM32L1xx_HAL_Driver
  42. * @{
  43. */
  44. #ifdef HAL_FLASH_MODULE_ENABLED
  45. /** @addtogroup FLASH
  46. * @{
  47. */
  48. /** @addtogroup FLASH_Private_Variables
  49. * @{
  50. */
  51. extern FLASH_ProcessTypeDef pFlash;
  52. /**
  53. * @}
  54. */
  55. /**
  56. * @}
  57. */
  58. /** @defgroup FLASH_RAMFUNC FLASH_RAMFUNC
  59. * @brief FLASH functions executed from RAM
  60. * @{
  61. */
  62. /* Private typedef -----------------------------------------------------------*/
  63. /* Private define ------------------------------------------------------------*/
  64. /* Private macro -------------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private function prototypes -----------------------------------------------*/
  67. /** @defgroup FLASH_RAMFUNC_Private_Functions FLASH RAM Private Functions
  68. * @{
  69. */
  70. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout);
  71. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void);
  72. /**
  73. * @}
  74. */
  75. /* Private functions ---------------------------------------------------------*/
  76. /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH RAM Exported Functions
  77. *
  78. @verbatim
  79. ===============================================================================
  80. ##### ramfunc functions #####
  81. ===============================================================================
  82. [..]
  83. This subsection provides a set of functions that should be executed from RAM
  84. transfers.
  85. @endverbatim
  86. * @{
  87. */
  88. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions
  89. * @{
  90. */
  91. /**
  92. * @brief Enable the power down mode during RUN mode.
  93. * @note This function can be used only when the user code is running from Internal SRAM.
  94. * @retval HAL status
  95. */
  96. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EnableRunPowerDown(void)
  97. {
  98. /* Enable the Power Down in Run mode*/
  99. __HAL_FLASH_POWER_DOWN_ENABLE();
  100. return HAL_OK;
  101. }
  102. /**
  103. * @brief Disable the power down mode during RUN mode.
  104. * @note This function can be used only when the user code is running from Internal SRAM.
  105. * @retval HAL status
  106. */
  107. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DisableRunPowerDown(void)
  108. {
  109. /* Disable the Power Down in Run mode*/
  110. __HAL_FLASH_POWER_DOWN_DISABLE();
  111. return HAL_OK;
  112. }
  113. /**
  114. * @}
  115. */
  116. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group2 Programming and erasing operation functions
  117. *
  118. @verbatim
  119. @endverbatim
  120. * @{
  121. */
  122. #if defined(FLASH_PECR_PARALLBANK)
  123. /**
  124. * @brief Erases a specified 2 pages in program memory in parallel.
  125. * @note This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5 devices.
  126. * To correctly run this function, the @ref HAL_FLASH_Unlock() function
  127. * must be called before.
  128. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  129. * (recommended to protect the FLASH memory against possible unwanted operation).
  130. * @param Page_Address1: The page address in program memory to be erased in
  131. * the first Bank (BANK1). This parameter should be between FLASH_BASE
  132. * and FLASH_BANK1_END.
  133. * @param Page_Address2: The page address in program memory to be erased in
  134. * the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
  135. * and FLASH_BANK2_END.
  136. * @note A Page is erased in the Program memory only if the address to load
  137. * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
  138. * @retval HAL status
  139. */
  140. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
  141. {
  142. HAL_StatusTypeDef status = HAL_OK;
  143. /* Wait for last operation to be completed */
  144. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  145. if(status == HAL_OK)
  146. {
  147. /* Proceed to erase the page */
  148. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  149. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  150. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  151. /* Write 00000000h to the first word of the first program page to erase */
  152. *(__IO uint32_t *)Page_Address1 = 0x00000000U;
  153. /* Write 00000000h to the first word of the second program page to erase */
  154. *(__IO uint32_t *)Page_Address2 = 0x00000000U;
  155. /* Wait for last operation to be completed */
  156. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  157. /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
  158. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  159. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  160. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  161. }
  162. /* Return the Erase Status */
  163. return status;
  164. }
  165. /**
  166. * @brief Program 2 half pages in program memory in parallel (half page size is 32 Words).
  167. * @note This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5 devices.
  168. * @param Address1: specifies the first address to be written in the first bank
  169. * (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
  170. * @param pBuffer1: pointer to the buffer containing the data to be written
  171. * to the first half page in the first bank.
  172. * @param Address2: specifies the second address to be written in the second bank
  173. * (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
  174. * @param pBuffer2: pointer to the buffer containing the data to be written
  175. * to the second half page in the second bank.
  176. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  177. * must be called before.
  178. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  179. * (recommended to protect the FLASH memory against possible unwanted operation).
  180. * @note Half page write is possible only from SRAM.
  181. * @note If there are more than 32 words to write, after 32 words another
  182. * Half Page programming operation starts and has to be finished.
  183. * @note A half page is written to the program memory only if the first
  184. * address to load is the start address of a half page (multiple of 128
  185. * bytes) and the 31 remaining words to load are in the same half page.
  186. * @note During the Program memory half page write all read operations are
  187. * forbidden (this includes DMA read operations and debugger read
  188. * operations such as breakpoints, periodic updates, etc.).
  189. * @note If a PGAERR is set during a Program memory half page write, the
  190. * complete write operation is aborted. Software should then reset the
  191. * FPRG and PROG/DATA bits and restart the write operation from the
  192. * beginning.
  193. * @retval HAL status
  194. */
  195. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
  196. {
  197. uint32_t primask_bit;
  198. uint32_t count = 0U;
  199. HAL_StatusTypeDef status = HAL_OK;
  200. /* Wait for last operation to be completed */
  201. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  202. if(status == HAL_OK)
  203. {
  204. /* Disable all IRQs */
  205. primask_bit = __get_PRIMASK();
  206. __disable_irq();
  207. /* Proceed to program the new half page */
  208. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  209. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  210. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  211. /* Write the first half page directly with 32 different words */
  212. while(count < 32U)
  213. {
  214. *(__IO uint32_t*) ((uint32_t)(Address1 + (4 * count))) = *pBuffer1;
  215. pBuffer1++;
  216. count ++;
  217. }
  218. /* Write the second half page directly with 32 different words */
  219. count = 0U;
  220. while(count < 32U)
  221. {
  222. *(__IO uint32_t*) ((uint32_t)(Address2 + (4 * count))) = *pBuffer2;
  223. pBuffer2++;
  224. count ++;
  225. }
  226. /* Wait for last operation to be completed */
  227. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  228. /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
  229. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  230. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  231. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  232. /* Enable IRQs */
  233. __set_PRIMASK(primask_bit);
  234. }
  235. /* Return the Write Status */
  236. return status;
  237. }
  238. #endif /* FLASH_PECR_PARALLBANK */
  239. /**
  240. * @brief Program a half page in program memory.
  241. * @param Address specifies the address to be written.
  242. * @param pBuffer pointer to the buffer containing the data to be written to
  243. * the half page.
  244. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  245. * must be called before.
  246. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  247. * (recommended to protect the FLASH memory against possible unwanted operation)
  248. * @note Half page write is possible only from SRAM.
  249. * @note If there are more than 32 words to write, after 32 words another
  250. * Half Page programming operation starts and has to be finished.
  251. * @note A half page is written to the program memory only if the first
  252. * address to load is the start address of a half page (multiple of 128
  253. * bytes) and the 31 remaining words to load are in the same half page.
  254. * @note During the Program memory half page write all read operations are
  255. * forbidden (this includes DMA read operations and debugger read
  256. * operations such as breakpoints, periodic updates, etc.).
  257. * @note If a PGAERR is set during a Program memory half page write, the
  258. * complete write operation is aborted. Software should then reset the
  259. * FPRG and PROG/DATA bits and restart the write operation from the
  260. * beginning.
  261. * @retval HAL status
  262. */
  263. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t* pBuffer)
  264. {
  265. uint32_t primask_bit;
  266. uint32_t count = 0U;
  267. HAL_StatusTypeDef status = HAL_OK;
  268. /* Wait for last operation to be completed */
  269. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  270. if(status == HAL_OK)
  271. {
  272. /* Disable all IRQs */
  273. primask_bit = __get_PRIMASK();
  274. __disable_irq();
  275. /* Proceed to program the new half page */
  276. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  277. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  278. /* Write one half page directly with 32 different words */
  279. while(count < 32U)
  280. {
  281. *(__IO uint32_t*) ((uint32_t)(Address + (4 * count))) = *pBuffer;
  282. pBuffer++;
  283. count ++;
  284. }
  285. /* Wait for last operation to be completed */
  286. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  287. /* If the write operation is completed, disable the PROG and FPRG bits */
  288. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  289. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  290. /* Enable IRQs */
  291. __set_PRIMASK(primask_bit);
  292. }
  293. /* Return the Write Status */
  294. return status;
  295. }
  296. /**
  297. * @}
  298. */
  299. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group3 Peripheral errors functions
  300. * @brief Peripheral errors functions
  301. *
  302. @verbatim
  303. ===============================================================================
  304. ##### Peripheral errors functions #####
  305. ===============================================================================
  306. [..]
  307. This subsection permit to get in run-time errors of the FLASH peripheral.
  308. @endverbatim
  309. * @{
  310. */
  311. /**
  312. * @brief Get the specific FLASH errors flag.
  313. * @param Error pointer is the error value. It can be a mixed of:
  314. @if STM32L100xB
  315. @elif STM32L100xBA
  316. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  317. @elif STM32L151xB
  318. @elif STM32L151xBA
  319. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  320. @elif STM32L152xB
  321. @elif STM32L152xBA
  322. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  323. @elif STM32L100xC
  324. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  325. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  326. @elif STM32L151xC
  327. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  328. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  329. @elif STM32L152xC
  330. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  331. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  332. @elif STM32L162xC
  333. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  334. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  335. @else
  336. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  337. @endif
  338. * @arg @ref HAL_FLASH_ERROR_PGA FLASH Programming Alignment error flag
  339. * @arg @ref HAL_FLASH_ERROR_WRP FLASH Write protected error flag
  340. * @arg @ref HAL_FLASH_ERROR_OPTV FLASH Option valid error flag
  341. * @retval HAL Status
  342. */
  343. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_GetError(uint32_t * Error)
  344. {
  345. *Error = pFlash.ErrorCode;
  346. return HAL_OK;
  347. }
  348. /**
  349. * @}
  350. */
  351. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group4 DATA EEPROM functions
  352. *
  353. * @{
  354. */
  355. /**
  356. * @brief Erase a double word in data memory.
  357. * @param Address specifies the address to be erased.
  358. * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
  359. * must be called before.
  360. * Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
  361. * and Flash program erase control register access(recommended to protect
  362. * the DATA_EEPROM against possible unwanted operation).
  363. * @note Data memory double word erase is possible only from SRAM.
  364. * @note A double word is erased to the data memory only if the first address
  365. * to load is the start address of a double word (multiple of 8 bytes).
  366. * @note During the Data memory double word erase, all read operations are
  367. * forbidden (this includes DMA read operations and debugger read
  368. * operations such as breakpoints, periodic updates, etc.).
  369. * @retval HAL status
  370. */
  371. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_EraseDoubleWord(uint32_t Address)
  372. {
  373. uint32_t primask_bit;
  374. HAL_StatusTypeDef status = HAL_OK;
  375. /* Wait for last operation to be completed */
  376. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  377. if(status == HAL_OK)
  378. {
  379. /* Disable all IRQs */
  380. primask_bit = __get_PRIMASK();
  381. __disable_irq();
  382. /* If the previous operation is completed, proceed to erase the next double word */
  383. /* Set the ERASE bit */
  384. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  385. /* Set DATA bit */
  386. SET_BIT(FLASH->PECR, FLASH_PECR_DATA);
  387. /* Write 00000000h to the 2 words to erase */
  388. *(__IO uint32_t *)Address = 0x00000000U;
  389. Address += 4U;
  390. *(__IO uint32_t *)Address = 0x00000000U;
  391. /* Wait for last operation to be completed */
  392. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  393. /* If the erase operation is completed, disable the ERASE and DATA bits */
  394. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  395. CLEAR_BIT(FLASH->PECR, FLASH_PECR_DATA);
  396. /* Enable IRQs */
  397. __set_PRIMASK(primask_bit);
  398. }
  399. /* Return the erase status */
  400. return status;
  401. }
  402. /**
  403. * @brief Write a double word in data memory without erase.
  404. * @param Address specifies the address to be written.
  405. * @param Data specifies the data to be written.
  406. * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
  407. * must be called before.
  408. * Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
  409. * and Flash program erase control register access(recommended to protect
  410. * the DATA_EEPROM against possible unwanted operation).
  411. * @note Data memory double word write is possible only from SRAM.
  412. * @note A data memory double word is written to the data memory only if the
  413. * first address to load is the start address of a double word (multiple
  414. * of double word).
  415. * @note During the Data memory double word write, all read operations are
  416. * forbidden (this includes DMA read operations and debugger read
  417. * operations such as breakpoints, periodic updates, etc.).
  418. * @retval HAL status
  419. */
  420. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)
  421. {
  422. uint32_t primask_bit;
  423. HAL_StatusTypeDef status = HAL_OK;
  424. /* Wait for last operation to be completed */
  425. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  426. if(status == HAL_OK)
  427. {
  428. /* Disable all IRQs */
  429. primask_bit = __get_PRIMASK();
  430. __disable_irq();
  431. /* If the previous operation is completed, proceed to program the new data*/
  432. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  433. SET_BIT(FLASH->PECR, FLASH_PECR_DATA);
  434. /* Write the 2 words */
  435. *(__IO uint32_t *)Address = (uint32_t) Data;
  436. Address += 4U;
  437. *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);
  438. /* Wait for last operation to be completed */
  439. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  440. /* If the write operation is completed, disable the FPRG and DATA bits */
  441. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  442. CLEAR_BIT(FLASH->PECR, FLASH_PECR_DATA);
  443. /* Enable IRQs */
  444. __set_PRIMASK(primask_bit);
  445. }
  446. /* Return the Write Status */
  447. return status;
  448. }
  449. /**
  450. * @}
  451. */
  452. /**
  453. * @}
  454. */
  455. /** @addtogroup FLASH_RAMFUNC_Private_Functions
  456. * @{
  457. */
  458. /**
  459. * @brief Set the specific FLASH error flag.
  460. * @retval HAL Status
  461. */
  462. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void)
  463. {
  464. uint32_t flags = 0U;
  465. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  466. {
  467. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  468. flags |= FLASH_FLAG_WRPERR;
  469. }
  470. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  471. {
  472. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  473. flags |= FLASH_FLAG_PGAERR;
  474. }
  475. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  476. {
  477. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  478. flags |= FLASH_FLAG_OPTVERR;
  479. }
  480. #if defined(FLASH_SR_RDERR)
  481. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  482. {
  483. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  484. flags |= FLASH_FLAG_RDERR;
  485. }
  486. #endif /* FLASH_SR_RDERR */
  487. #if defined(FLASH_SR_OPTVERRUSR)
  488. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR))
  489. {
  490. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTVUSR;
  491. flags |= FLASH_FLAG_OPTVERRUSR;
  492. }
  493. #endif /* FLASH_SR_OPTVERRUSR */
  494. /* Clear FLASH error pending bits */
  495. __HAL_FLASH_CLEAR_FLAG(flags);
  496. return HAL_OK;
  497. }
  498. /**
  499. * @brief Wait for a FLASH operation to complete.
  500. * @param Timeout maximum flash operationtimeout
  501. * @retval HAL status
  502. */
  503. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout)
  504. {
  505. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  506. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  507. flag will be set */
  508. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00U))
  509. {
  510. Timeout--;
  511. }
  512. if(Timeout == 0x00U)
  513. {
  514. return HAL_TIMEOUT;
  515. }
  516. /* Check FLASH End of Operation flag */
  517. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  518. {
  519. /* Clear FLASH End of Operation pending bit */
  520. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  521. }
  522. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  523. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  524. #if defined(FLASH_SR_RDERR)
  525. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  526. #endif /* FLASH_SR_RDERR */
  527. #if defined(FLASH_SR_OPTVERRUSR)
  528. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  529. #endif /* FLASH_SR_OPTVERRUSR */
  530. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  531. {
  532. /*Save the error code*/
  533. FLASHRAM_SetErrorCode();
  534. return HAL_ERROR;
  535. }
  536. /* There is no error flag set */
  537. return HAL_OK;
  538. }
  539. /**
  540. * @}
  541. */
  542. /**
  543. * @}
  544. */
  545. #endif /* HAL_FLASH_MODULE_ENABLED */
  546. /**
  547. * @}
  548. */
  549. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/