stm32l1xx_hal_flash_ramfunc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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</center></h2>
  30. *
  31. * Redistribution and use in source and binary forms, with or without modification,
  32. * are permitted provided that the following conditions are met:
  33. * 1. Redistributions of source code must retain the above copyright notice,
  34. * this list of conditions and the following disclaimer.
  35. * 2. Redistributions in binary form must reproduce the above copyright notice,
  36. * this list of conditions and the following disclaimer in the documentation
  37. * and/or other materials provided with the distribution.
  38. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  39. * may be used to endorse or promote products derived from this software
  40. * without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  43. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  45. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  46. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  47. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  48. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  51. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. *
  53. ******************************************************************************
  54. */
  55. /* Includes ------------------------------------------------------------------*/
  56. #include "stm32l1xx_hal.h"
  57. /** @addtogroup STM32L1xx_HAL_Driver
  58. * @{
  59. */
  60. #ifdef HAL_FLASH_MODULE_ENABLED
  61. /** @addtogroup FLASH
  62. * @{
  63. */
  64. /** @addtogroup FLASH_Private_Variables
  65. * @{
  66. */
  67. extern FLASH_ProcessTypeDef pFlash;
  68. /**
  69. * @}
  70. */
  71. /**
  72. * @}
  73. */
  74. /** @defgroup FLASH_RAMFUNC FLASH_RAMFUNC
  75. * @brief FLASH functions executed from RAM
  76. * @{
  77. */
  78. /* Private typedef -----------------------------------------------------------*/
  79. /* Private define ------------------------------------------------------------*/
  80. /* Private macro -------------------------------------------------------------*/
  81. /* Private variables ---------------------------------------------------------*/
  82. /* Private function prototypes -----------------------------------------------*/
  83. /** @defgroup FLASH_RAMFUNC_Private_Functions FLASH RAM Private Functions
  84. * @{
  85. */
  86. static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout);
  87. static __RAM_FUNC FLASHRAM_SetErrorCode(void);
  88. /**
  89. * @}
  90. */
  91. /* Private functions ---------------------------------------------------------*/
  92. /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH RAM Exported Functions
  93. *
  94. @verbatim
  95. ===============================================================================
  96. ##### ramfunc functions #####
  97. ===============================================================================
  98. [..]
  99. This subsection provides a set of functions that should be executed from RAM
  100. transfers.
  101. @endverbatim
  102. * @{
  103. */
  104. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions
  105. * @{
  106. */
  107. /**
  108. * @brief Enable the power down mode during RUN mode.
  109. * @note This function can be used only when the user code is running from Internal SRAM.
  110. * @retval HAL status
  111. */
  112. __RAM_FUNC HAL_FLASHEx_EnableRunPowerDown(void)
  113. {
  114. /* Enable the Power Down in Run mode*/
  115. __HAL_FLASH_POWER_DOWN_ENABLE();
  116. return HAL_OK;
  117. }
  118. /**
  119. * @brief Disable the power down mode during RUN mode.
  120. * @note This function can be used only when the user code is running from Internal SRAM.
  121. * @retval HAL status
  122. */
  123. __RAM_FUNC HAL_FLASHEx_DisableRunPowerDown(void)
  124. {
  125. /* Disable the Power Down in Run mode*/
  126. __HAL_FLASH_POWER_DOWN_DISABLE();
  127. return HAL_OK;
  128. }
  129. /**
  130. * @}
  131. */
  132. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group2 Programming and erasing operation functions
  133. *
  134. @verbatim
  135. @endverbatim
  136. * @{
  137. */
  138. #if defined(FLASH_PECR_PARALLBANK)
  139. /**
  140. * @brief Erases a specified 2 pages in program memory in parallel.
  141. * @note This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5 devices.
  142. * To correctly run this function, the @ref HAL_FLASH_Unlock() function
  143. * must be called before.
  144. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  145. * (recommended to protect the FLASH memory against possible unwanted operation).
  146. * @param Page_Address1: The page address in program memory to be erased in
  147. * the first Bank (BANK1). This parameter should be between FLASH_BASE
  148. * and FLASH_BANK1_END.
  149. * @param Page_Address2: The page address in program memory to be erased in
  150. * the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
  151. * and FLASH_BANK2_END.
  152. * @note A Page is erased in the Program memory only if the address to load
  153. * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
  154. * @retval HAL status
  155. */
  156. __RAM_FUNC HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
  157. {
  158. HAL_StatusTypeDef status = HAL_OK;
  159. /* Wait for last operation to be completed */
  160. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  161. if(status == HAL_OK)
  162. {
  163. /* Proceed to erase the page */
  164. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  165. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  166. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  167. /* Write 00000000h to the first word of the first program page to erase */
  168. *(__IO uint32_t *)Page_Address1 = 0x00000000U;
  169. /* Write 00000000h to the first word of the second program page to erase */
  170. *(__IO uint32_t *)Page_Address2 = 0x00000000U;
  171. /* Wait for last operation to be completed */
  172. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  173. /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
  174. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  175. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  176. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  177. }
  178. /* Return the Erase Status */
  179. return status;
  180. }
  181. /**
  182. * @brief Program 2 half pages in program memory in parallel (half page size is 32 Words).
  183. * @note This function can be used only for STM32L151xD, STM32L152xD), STM32L162xD and Cat5 devices.
  184. * @param Address1: specifies the first address to be written in the first bank
  185. * (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
  186. * @param pBuffer1: pointer to the buffer containing the data to be written
  187. * to the first half page in the first bank.
  188. * @param Address2: specifies the second address to be written in the second bank
  189. * (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
  190. * @param pBuffer2: pointer to the buffer containing the data to be written
  191. * to the second half page in the second bank.
  192. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  193. * must be called before.
  194. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  195. * (recommended to protect the FLASH memory against possible unwanted operation).
  196. * @note Half page write is possible only from SRAM.
  197. * @note If there are more than 32 words to write, after 32 words another
  198. * Half Page programming operation starts and has to be finished.
  199. * @note A half page is written to the program memory only if the first
  200. * address to load is the start address of a half page (multiple of 128
  201. * bytes) and the 31 remaining words to load are in the same half page.
  202. * @note During the Program memory half page write all read operations are
  203. * forbidden (this includes DMA read operations and debugger read
  204. * operations such as breakpoints, periodic updates, etc.).
  205. * @note If a PGAERR is set during a Program memory half page write, the
  206. * complete write operation is aborted. Software should then reset the
  207. * FPRG and PROG/DATA bits and restart the write operation from the
  208. * beginning.
  209. * @retval HAL status
  210. */
  211. __RAM_FUNC HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
  212. {
  213. uint32_t count = 0U;
  214. HAL_StatusTypeDef status = HAL_OK;
  215. /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008U)
  216. This bit prevents the interruption of multicycle instructions and therefore
  217. will increase the interrupt latency. of Cortex-M3. */
  218. SET_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  219. /* Wait for last operation to be completed */
  220. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  221. if(status == HAL_OK)
  222. {
  223. /* Proceed to program the new half page */
  224. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  225. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  226. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  227. /* Wait for last operation to be completed */
  228. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  229. if(status == HAL_OK)
  230. {
  231. /* Disable all IRQs */
  232. __disable_irq();
  233. /* Write the first half page directly with 32 different words */
  234. while(count < 32U)
  235. {
  236. *(__IO uint32_t*) ((uint32_t)(Address1 + (4 * count))) = *pBuffer1;
  237. pBuffer1++;
  238. count ++;
  239. }
  240. /* Write the second half page directly with 32 different words */
  241. count = 0U;
  242. while(count < 32U)
  243. {
  244. *(__IO uint32_t*) ((uint32_t)(Address2 + (4 * count))) = *pBuffer2;
  245. pBuffer2++;
  246. count ++;
  247. }
  248. /* Enable IRQs */
  249. __enable_irq();
  250. /* Wait for last operation to be completed */
  251. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  252. }
  253. /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
  254. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  255. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  256. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  257. }
  258. CLEAR_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  259. /* Return the Write Status */
  260. return status;
  261. }
  262. #endif /* FLASH_PECR_PARALLBANK */
  263. /**
  264. * @brief Program a half page in program memory.
  265. * @param Address: specifies the address to be written.
  266. * @param pBuffer: pointer to the buffer containing the data to be written to
  267. * the half page.
  268. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  269. * must be called before.
  270. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  271. * (recommended to protect the FLASH memory against possible unwanted operation)
  272. * @note Half page write is possible only from SRAM.
  273. * @note If there are more than 32 words to write, after 32 words another
  274. * Half Page programming operation starts and has to be finished.
  275. * @note A half page is written to the program memory only if the first
  276. * address to load is the start address of a half page (multiple of 128
  277. * bytes) and the 31 remaining words to load are in the same half page.
  278. * @note During the Program memory half page write all read operations are
  279. * forbidden (this includes DMA read operations and debugger read
  280. * operations such as breakpoints, periodic updates, etc.).
  281. * @note If a PGAERR is set during a Program memory half page write, the
  282. * complete write operation is aborted. Software should then reset the
  283. * FPRG and PROG/DATA bits and restart the write operation from the
  284. * beginning.
  285. * @retval HAL status
  286. */
  287. __RAM_FUNC HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t* pBuffer)
  288. {
  289. uint32_t count = 0U;
  290. HAL_StatusTypeDef status = HAL_OK;
  291. /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008U)
  292. This bit prevents the interruption of multicycle instructions and therefore
  293. will increase the interrupt latency. of Cortex-M3. */
  294. SET_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  295. /* Wait for last operation to be completed */
  296. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  297. if(status == HAL_OK)
  298. {
  299. /* Proceed to program the new half page */
  300. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  301. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  302. /* Disable all IRQs */
  303. __disable_irq();
  304. /* Write one half page directly with 32 different words */
  305. while(count < 32U)
  306. {
  307. *(__IO uint32_t*) ((uint32_t)(Address + (4 * count))) = *pBuffer;
  308. pBuffer++;
  309. count ++;
  310. }
  311. /* Wait for last operation to be completed */
  312. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  313. /* Enable IRQs */
  314. __enable_irq();
  315. /* If the write operation is completed, disable the PROG and FPRG bits */
  316. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  317. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  318. }
  319. CLEAR_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  320. /* Return the Write Status */
  321. return status;
  322. }
  323. /**
  324. * @}
  325. */
  326. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group3 Peripheral errors functions
  327. * @brief Peripheral errors functions
  328. *
  329. @verbatim
  330. ===============================================================================
  331. ##### Peripheral errors functions #####
  332. ===============================================================================
  333. [..]
  334. This subsection permit to get in run-time errors of the FLASH peripheral.
  335. @endverbatim
  336. * @{
  337. */
  338. /**
  339. * @brief Get the specific FLASH errors flag.
  340. * @param Error pointer is the error value. It can be a mixed of:
  341. @if STM32L100xB
  342. @elif STM32L100xBA
  343. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  344. @elif STM32L151xB
  345. @elif STM32L151xBA
  346. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  347. @elif STM32L152xB
  348. @elif STM32L152xBA
  349. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  350. @elif STM32L100xC
  351. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  352. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  353. @elif STM32L151xC
  354. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  355. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  356. @elif STM32L152xC
  357. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  358. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  359. @elif STM32L162xC
  360. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  361. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  362. @else
  363. * @arg @ref HAL_FLASH_ERROR_OPTVUSR FLASH Option User validity error
  364. @endif
  365. * @arg @ref HAL_FLASH_ERROR_PGA FLASH Programming Alignment error flag
  366. * @arg @ref HAL_FLASH_ERROR_WRP FLASH Write protected error flag
  367. * @arg @ref HAL_FLASH_ERROR_OPTV FLASH Option valid error flag
  368. * @retval HAL Status
  369. */
  370. __RAM_FUNC HAL_FLASHEx_GetError(uint32_t * Error)
  371. {
  372. *Error = pFlash.ErrorCode;
  373. return HAL_OK;
  374. }
  375. /**
  376. * @}
  377. */
  378. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group4 DATA EEPROM functions
  379. *
  380. * @{
  381. */
  382. /**
  383. * @brief Erase a double word in data memory.
  384. * @param Address: specifies the address to be erased.
  385. * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
  386. * must be called before.
  387. * Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
  388. * and Flash program erase control register access(recommended to protect
  389. * the DATA_EEPROM against possible unwanted operation).
  390. * @note Data memory double word erase is possible only from SRAM.
  391. * @note A double word is erased to the data memory only if the first address
  392. * to load is the start address of a double word (multiple of 8 bytes).
  393. * @note During the Data memory double word erase, all read operations are
  394. * forbidden (this includes DMA read operations and debugger read
  395. * operations such as breakpoints, periodic updates, etc.).
  396. * @retval HAL status
  397. */
  398. __RAM_FUNC HAL_FLASHEx_DATAEEPROM_EraseDoubleWord(uint32_t Address)
  399. {
  400. HAL_StatusTypeDef status = HAL_OK;
  401. /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008U)
  402. This bit prevents the interruption of multicycle instructions and therefore
  403. will increase the interrupt latency. of Cortex-M3. */
  404. SET_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  405. /* Wait for last operation to be completed */
  406. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  407. if(status == HAL_OK)
  408. {
  409. /* If the previous operation is completed, proceed to erase the next double word */
  410. /* Set the ERASE bit */
  411. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  412. /* Set DATA bit */
  413. SET_BIT(FLASH->PECR, FLASH_PECR_DATA);
  414. /* Write 00000000h to the 2 words to erase */
  415. *(__IO uint32_t *)Address = 0x00000000U;
  416. Address += 4U;
  417. *(__IO uint32_t *)Address = 0x00000000U;
  418. /* Wait for last operation to be completed */
  419. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  420. /* If the erase operation is completed, disable the ERASE and DATA bits */
  421. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  422. CLEAR_BIT(FLASH->PECR, FLASH_PECR_DATA);
  423. }
  424. CLEAR_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  425. /* Return the erase status */
  426. return status;
  427. }
  428. /**
  429. * @brief Write a double word in data memory without erase.
  430. * @param Address: specifies the address to be written.
  431. * @param Data: specifies the data to be written.
  432. * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
  433. * must be called before.
  434. * Call the HAL_FLASH_EEPROM_Lock() to he data EEPROM access
  435. * and Flash program erase control register access(recommended to protect
  436. * the DATA_EEPROM against possible unwanted operation).
  437. * @note Data memory double word write is possible only from SRAM.
  438. * @note A data memory double word is written to the data memory only if the
  439. * first address to load is the start address of a double word (multiple
  440. * of double word).
  441. * @note During the Data memory double word write, all read operations are
  442. * forbidden (this includes DMA read operations and debugger read
  443. * operations such as breakpoints, periodic updates, etc.).
  444. * @retval HAL status
  445. */
  446. __RAM_FUNC HAL_FLASHEx_DATAEEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)
  447. {
  448. HAL_StatusTypeDef status = HAL_OK;
  449. /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008U)
  450. This bit prevents the interruption of multicycle instructions and therefore
  451. will increase the interrupt latency. of Cortex-M3. */
  452. SET_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  453. /* Wait for last operation to be completed */
  454. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  455. if(status == HAL_OK)
  456. {
  457. /* If the previous operation is completed, proceed to program the new data*/
  458. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  459. SET_BIT(FLASH->PECR, FLASH_PECR_DATA);
  460. /* Write the 2 words */
  461. *(__IO uint32_t *)Address = (uint32_t) Data;
  462. Address += 4U;
  463. *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);
  464. /* Wait for last operation to be completed */
  465. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  466. /* If the write operation is completed, disable the FPRG and DATA bits */
  467. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  468. CLEAR_BIT(FLASH->PECR, FLASH_PECR_DATA);
  469. }
  470. CLEAR_BIT(SCnSCB->ACTLR, SCnSCB_ACTLR_DISMCYCINT_Msk);
  471. /* Return the Write Status */
  472. return status;
  473. }
  474. /**
  475. * @}
  476. */
  477. /**
  478. * @}
  479. */
  480. /** @addtogroup FLASH_RAMFUNC_Private_Functions
  481. * @{
  482. */
  483. /**
  484. * @brief Set the specific FLASH error flag.
  485. * @retval HAL Status
  486. */
  487. static __RAM_FUNC FLASHRAM_SetErrorCode(void)
  488. {
  489. uint32_t flags = 0U;
  490. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  491. {
  492. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  493. flags |= FLASH_FLAG_WRPERR;
  494. }
  495. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  496. {
  497. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  498. flags |= FLASH_FLAG_PGAERR;
  499. }
  500. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  501. {
  502. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  503. flags |= FLASH_FLAG_OPTVERR;
  504. }
  505. #if defined(FLASH_SR_RDERR)
  506. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  507. {
  508. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  509. flags |= FLASH_FLAG_RDERR;
  510. }
  511. #endif /* FLASH_SR_RDERR */
  512. #if defined(FLASH_SR_OPTVERRUSR)
  513. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR))
  514. {
  515. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTVUSR;
  516. flags |= FLASH_FLAG_OPTVERRUSR;
  517. }
  518. #endif /* FLASH_SR_OPTVERRUSR */
  519. /* Clear FLASH error pending bits */
  520. __HAL_FLASH_CLEAR_FLAG(flags);
  521. return HAL_OK;
  522. }
  523. /**
  524. * @brief Wait for a FLASH operation to complete.
  525. * @param Timeout: maximum flash operationtimeout
  526. * @retval HAL status
  527. */
  528. static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout)
  529. {
  530. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  531. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  532. flag will be set */
  533. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00U))
  534. {
  535. Timeout--;
  536. }
  537. if(Timeout == 0x00U)
  538. {
  539. return HAL_TIMEOUT;
  540. }
  541. /* Check FLASH End of Operation flag */
  542. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  543. {
  544. /* Clear FLASH End of Operation pending bit */
  545. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  546. }
  547. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  548. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  549. #if defined(FLASH_SR_RDERR)
  550. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  551. #endif /* FLASH_SR_RDERR */
  552. #if defined(FLASH_SR_OPTVERRUSR)
  553. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) ||
  554. #endif /* FLASH_SR_OPTVERRUSR */
  555. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  556. {
  557. /*Save the error code*/
  558. FLASHRAM_SetErrorCode();
  559. return HAL_ERROR;
  560. }
  561. /* There is no error flag set */
  562. return HAL_OK;
  563. }
  564. /**
  565. * @}
  566. */
  567. /**
  568. * @}
  569. */
  570. #endif /* HAL_FLASH_MODULE_ENABLED */
  571. /**
  572. * @}
  573. */
  574. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/