stm32l0xx_hal_flash_ramfunc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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. * Copyright (c) 2016 STMicroelectronics.
  30. * All rights reserved.
  31. *
  32. * This software is licensed under terms that can be found in the LICENSE file in
  33. * the root directory of this software component.
  34. * If no LICENSE file comes with this software, it is provided AS-IS.
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "stm32l0xx_hal.h"
  39. /** @addtogroup STM32L0xx_HAL_Driver
  40. * @{
  41. */
  42. #ifdef HAL_FLASH_MODULE_ENABLED
  43. /** @addtogroup FLASH
  44. * @{
  45. */
  46. /** @addtogroup FLASH_Private_Variables
  47. * @{
  48. */
  49. extern FLASH_ProcessTypeDef pFlash;
  50. /**
  51. * @}
  52. */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup FLASH_RAMFUNC FLASH_RAMFUNC
  57. * @brief FLASH functions executed from RAM
  58. * @{
  59. */
  60. /* Private typedef -----------------------------------------------------------*/
  61. /* Private define ------------------------------------------------------------*/
  62. /* Private macro -------------------------------------------------------------*/
  63. /* Private variables ---------------------------------------------------------*/
  64. /* Private function prototypes -----------------------------------------------*/
  65. /** @defgroup FLASH_RAMFUNC_Private_Functions FLASH RAM Private Functions
  66. * @{
  67. */
  68. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout);
  69. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void);
  70. /**
  71. * @}
  72. */
  73. /* Private functions ---------------------------------------------------------*/
  74. /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH RAM Exported Functions
  75. *
  76. @verbatim
  77. ===============================================================================
  78. ##### ramfunc functions #####
  79. ===============================================================================
  80. [..]
  81. This subsection provides a set of functions that should be executed from RAM
  82. transfers.
  83. @endverbatim
  84. * @{
  85. */
  86. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions
  87. * @{
  88. */
  89. /**
  90. * @brief Enable the power down mode during RUN mode.
  91. * @note This function can be used only when the user code is running from Internal SRAM.
  92. * @retval HAL status
  93. */
  94. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EnableRunPowerDown(void)
  95. {
  96. /* Enable the Power Down in Run mode*/
  97. __HAL_FLASH_POWER_DOWN_ENABLE();
  98. return HAL_OK;
  99. }
  100. /**
  101. * @brief Disable the power down mode during RUN mode.
  102. * @note This function can be used only when the user code is running from Internal SRAM.
  103. * @retval HAL status
  104. */
  105. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DisableRunPowerDown(void)
  106. {
  107. /* Disable the Power Down in Run mode*/
  108. __HAL_FLASH_POWER_DOWN_DISABLE();
  109. return HAL_OK;
  110. }
  111. /**
  112. * @}
  113. */
  114. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group2 Programming and erasing operation functions
  115. *
  116. @verbatim
  117. @endverbatim
  118. * @{
  119. */
  120. #if defined(FLASH_PECR_PARALLBANK)
  121. /**
  122. * @brief Erases a specified 2 pages in program memory in parallel.
  123. * @note This function can be used only for STM32L07xxx/STM32L08xxx devices.
  124. * To correctly run this function, the @ref HAL_FLASH_Unlock() function
  125. * must be called before.
  126. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  127. * (recommended to protect the FLASH memory against possible unwanted operation).
  128. * @param Page_Address1: The page address in program memory to be erased in
  129. * the first Bank (BANK1). This parameter should be between FLASH_BASE
  130. * and FLASH_BANK1_END.
  131. * @param Page_Address2: The page address in program memory to be erased in
  132. * the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
  133. * and FLASH_BANK2_END.
  134. * @note A Page is erased in the Program memory only if the address to load
  135. * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
  136. * @retval HAL status
  137. */
  138. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
  139. {
  140. HAL_StatusTypeDef status = HAL_OK;
  141. /* Wait for last operation to be completed */
  142. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  143. if(status == HAL_OK)
  144. {
  145. /* Proceed to erase the page */
  146. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  147. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  148. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  149. /* Write 00000000h to the first word of the first program page to erase */
  150. *(__IO uint32_t *)Page_Address1 = 0x00000000U;
  151. /* Write 00000000h to the first word of the second program page to erase */
  152. *(__IO uint32_t *)Page_Address2 = 0x00000000U;
  153. /* Wait for last operation to be completed */
  154. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  155. /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
  156. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  157. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  158. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  159. }
  160. /* Return the Erase Status */
  161. return status;
  162. }
  163. /**
  164. * @brief Program 2 half pages in program memory in parallel (half page size is 16 Words).
  165. * @note This function can be used only for STM32L07xxx/STM32L08xxx devices.
  166. * @param Address1: specifies the first address to be written in the first bank
  167. * (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
  168. * @param pBuffer1: pointer to the buffer containing the data to be written
  169. * to the first half page in the first bank.
  170. * @param Address2: specifies the second address to be written in the second bank
  171. * (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
  172. * @param pBuffer2: pointer to the buffer containing the data to be written
  173. * to the second half page in the second bank.
  174. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  175. * must be called before.
  176. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  177. * (recommended to protect the FLASH memory against possible unwanted operation).
  178. * @note Half page write is possible only from SRAM.
  179. * @note A half page is written to the program memory only if the first
  180. * address to load is the start address of a half page (multiple of 64
  181. * bytes) and the 15 remaining words to load are in the same half page.
  182. * @note During the Program memory half page write all read operations are
  183. * forbidden (this includes DMA read operations and debugger read
  184. * operations such as breakpoints, periodic updates, etc.).
  185. * @note If a PGAERR is set during a Program memory half page write, the
  186. * complete write operation is aborted. Software should then reset the
  187. * FPRG and PROG/DATA bits and restart the write operation from the
  188. * beginning.
  189. * @retval HAL status
  190. */
  191. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
  192. {
  193. uint32_t count = 0U;
  194. HAL_StatusTypeDef status = HAL_OK;
  195. /* Wait for last operation to be completed */
  196. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  197. if(status == HAL_OK)
  198. {
  199. /* Proceed to program the new half page */
  200. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  201. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  202. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  203. /* Wait for last operation to be completed */
  204. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  205. if(status == HAL_OK)
  206. {
  207. /* Disable all IRQs */
  208. __disable_irq();
  209. /* Write the first half page directly with 16 different words */
  210. while(count < 16U)
  211. {
  212. /* Address1 doesn't need to be increased */
  213. *(__IO uint32_t*) Address1 = *pBuffer1;
  214. pBuffer1++;
  215. count ++;
  216. }
  217. /* Write the second half page directly with 16 different words */
  218. count = 0U;
  219. while(count < 16U)
  220. {
  221. /* Address2 doesn't need to be increased */
  222. *(__IO uint32_t*) Address2 = *pBuffer2;
  223. pBuffer2++;
  224. count ++;
  225. }
  226. /* Enable IRQs */
  227. __enable_irq();
  228. /* Wait for last operation to be completed */
  229. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  230. }
  231. /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
  232. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  233. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  234. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  235. }
  236. /* Return the Write Status */
  237. return status;
  238. }
  239. #endif /* FLASH_PECR_PARALLBANK */
  240. /**
  241. * @brief Program a half page in program memory.
  242. * @param Address specifies the address to be written.
  243. * @param pBuffer pointer to the buffer containing the data to be written to
  244. * the half page.
  245. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  246. * must be called before.
  247. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  248. * (recommended to protect the FLASH memory against possible unwanted operation)
  249. * @note Half page write is possible only from SRAM.
  250. * @note A half page is written to the program memory only if the first
  251. * address to load is the start address of a half page (multiple of 64
  252. * bytes) and the 15 remaining words to load are in the same half page.
  253. * @note During the Program memory half page write all read operations are
  254. * forbidden (this includes DMA read operations and debugger read
  255. * operations such as breakpoints, periodic updates, etc.).
  256. * @note If a PGAERR is set during a Program memory half page write, the
  257. * complete write operation is aborted. Software should then reset the
  258. * FPRG and PROG/DATA bits and restart the write operation from the
  259. * beginning.
  260. * @retval HAL status
  261. */
  262. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t* pBuffer)
  263. {
  264. uint32_t count = 0U;
  265. HAL_StatusTypeDef status = HAL_OK;
  266. /* Wait for last operation to be completed */
  267. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  268. if(status == HAL_OK)
  269. {
  270. /* Proceed to program the new half page */
  271. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  272. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  273. /* Disable all IRQs */
  274. __disable_irq();
  275. /* Write one half page directly with 16 different words */
  276. while(count < 16U)
  277. {
  278. /* Address doesn't need to be increased */
  279. *(__IO uint32_t*) Address = *pBuffer;
  280. pBuffer++;
  281. count ++;
  282. }
  283. /* Enable IRQs */
  284. __enable_irq();
  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. }
  291. /* Return the Write Status */
  292. return status;
  293. }
  294. /**
  295. * @}
  296. */
  297. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group3 Peripheral errors functions
  298. * @brief Peripheral errors functions
  299. *
  300. @verbatim
  301. ===============================================================================
  302. ##### Peripheral errors functions #####
  303. ===============================================================================
  304. [..]
  305. This subsection permit to get in run-time errors of the FLASH peripheral.
  306. @endverbatim
  307. * @{
  308. */
  309. /**
  310. * @brief Get the specific FLASH errors flag.
  311. * @param Error pointer is the error value. It can be a mixed of:
  312. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  313. * @arg @ref HAL_FLASH_ERROR_SIZE FLASH Programming Parallelism error flag
  314. * @arg @ref HAL_FLASH_ERROR_PGA FLASH Programming Alignment error flag
  315. * @arg @ref HAL_FLASH_ERROR_WRP FLASH Write protected error flag
  316. * @arg @ref HAL_FLASH_ERROR_OPTV FLASH Option valid error flag
  317. * @arg @ref HAL_FLASH_ERROR_FWWERR FLASH Write or Erase operation aborted
  318. * @arg @ref HAL_FLASH_ERROR_NOTZERO FLASH Write operation is done in a not-erased region
  319. * @retval HAL Status
  320. */
  321. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_GetError(uint32_t * Error)
  322. {
  323. *Error = pFlash.ErrorCode;
  324. return HAL_OK;
  325. }
  326. /**
  327. * @}
  328. */
  329. /**
  330. * @}
  331. */
  332. /** @addtogroup FLASH_RAMFUNC_Private_Functions
  333. * @{
  334. */
  335. /**
  336. * @brief Set the specific FLASH error flag.
  337. * @retval HAL Status
  338. */
  339. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void)
  340. {
  341. uint32_t flags = 0;
  342. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  343. {
  344. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  345. flags |= FLASH_FLAG_WRPERR;
  346. }
  347. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  348. {
  349. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  350. flags |= FLASH_FLAG_PGAERR;
  351. }
  352. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
  353. {
  354. pFlash.ErrorCode |= HAL_FLASH_ERROR_SIZE;
  355. flags |= FLASH_FLAG_SIZERR;
  356. }
  357. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  358. {
  359. /* WARNING : On the first cut of STM32L031xx and STM32L041xx devices,
  360. * (RefID = 0x1000) the FLASH_FLAG_OPTVERR bit was not behaving
  361. * as expected. If the user run an application using the first
  362. * cut of the STM32L031xx device or the first cut of the STM32L041xx
  363. * device, this error should be ignored. The revId of the device
  364. * can be retrieved via the HAL_GetREVID() function.
  365. *
  366. */
  367. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  368. flags |= FLASH_FLAG_OPTVERR;
  369. }
  370. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  371. {
  372. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  373. flags |= FLASH_FLAG_RDERR;
  374. }
  375. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR))
  376. {
  377. pFlash.ErrorCode |= HAL_FLASH_ERROR_FWWERR;
  378. flags |= HAL_FLASH_ERROR_FWWERR;
  379. }
  380. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR))
  381. {
  382. pFlash.ErrorCode |= HAL_FLASH_ERROR_NOTZERO;
  383. flags |= FLASH_FLAG_NOTZEROERR;
  384. }
  385. /* Clear FLASH error pending bits */
  386. __HAL_FLASH_CLEAR_FLAG(flags);
  387. return HAL_OK;
  388. }
  389. /**
  390. * @brief Wait for a FLASH operation to complete.
  391. * @param Timeout maximum flash operationtimeout
  392. * @retval HAL status
  393. */
  394. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout)
  395. {
  396. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  397. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  398. flag will be set */
  399. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00U))
  400. {
  401. Timeout--;
  402. }
  403. if(Timeout == 0x00U)
  404. {
  405. return HAL_TIMEOUT;
  406. }
  407. /* Check FLASH End of Operation flag */
  408. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  409. {
  410. /* Clear FLASH End of Operation pending bit */
  411. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  412. }
  413. if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  414. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) ||
  415. __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) ||
  416. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  417. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  418. __HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR) ||
  419. __HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR) )
  420. {
  421. /*Save the error code*/
  422. /* WARNING : On the first cut of STM32L031xx and STM32L041xx devices,
  423. * (RefID = 0x1000) the FLASH_FLAG_OPTVERR bit was not behaving
  424. * as expected. If the user run an application using the first
  425. * cut of the STM32L031xx device or the first cut of the STM32L041xx
  426. * device, this error should be ignored. The revId of the device
  427. * can be retrieved via the HAL_GetREVID() function.
  428. *
  429. */
  430. FLASHRAM_SetErrorCode();
  431. return HAL_ERROR;
  432. }
  433. /* There is no error flag set */
  434. return HAL_OK;
  435. }
  436. /**
  437. * @}
  438. */
  439. /**
  440. * @}
  441. */
  442. #endif /* HAL_FLASH_MODULE_ENABLED */
  443. /**
  444. * @}
  445. */