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.
 
 
 

522 lines
17 KiB

  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. * <h2><center>&copy; Copyright (c) 2016 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 "stm32l0xx_hal.h"
  41. /** @addtogroup STM32L0xx_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 STM32L07xxx/STM32L08xxx 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 16 Words).
  167. * @note This function can be used only for STM32L07xxx/STM32L08xxx 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 A half page is written to the program memory only if the first
  182. * address to load is the start address of a half page (multiple of 64
  183. * bytes) and the 15 remaining words to load are in the same half page.
  184. * @note During the Program memory half page write all read operations are
  185. * forbidden (this includes DMA read operations and debugger read
  186. * operations such as breakpoints, periodic updates, etc.).
  187. * @note If a PGAERR is set during a Program memory half page write, the
  188. * complete write operation is aborted. Software should then reset the
  189. * FPRG and PROG/DATA bits and restart the write operation from the
  190. * beginning.
  191. * @retval HAL status
  192. */
  193. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
  194. {
  195. uint32_t count = 0U;
  196. HAL_StatusTypeDef status = HAL_OK;
  197. /* Wait for last operation to be completed */
  198. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  199. if(status == HAL_OK)
  200. {
  201. /* Proceed to program the new half page */
  202. SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  203. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  204. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  205. /* Wait for last operation to be completed */
  206. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  207. if(status == HAL_OK)
  208. {
  209. /* Disable all IRQs */
  210. __disable_irq();
  211. /* Write the first half page directly with 16 different words */
  212. while(count < 16U)
  213. {
  214. /* Address1 doesn't need to be increased */
  215. *(__IO uint32_t*) Address1 = *pBuffer1;
  216. pBuffer1++;
  217. count ++;
  218. }
  219. /* Write the second half page directly with 16 different words */
  220. count = 0U;
  221. while(count < 16U)
  222. {
  223. /* Address2 doesn't need to be increased */
  224. *(__IO uint32_t*) Address2 = *pBuffer2;
  225. pBuffer2++;
  226. count ++;
  227. }
  228. /* Enable IRQs */
  229. __enable_irq();
  230. /* Wait for last operation to be completed */
  231. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  232. }
  233. /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
  234. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  235. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  236. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
  237. }
  238. /* Return the Write Status */
  239. return status;
  240. }
  241. #endif /* FLASH_PECR_PARALLBANK */
  242. /**
  243. * @brief Program a half page in program memory.
  244. * @param Address specifies the address to be written.
  245. * @param pBuffer pointer to the buffer containing the data to be written to
  246. * the half page.
  247. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  248. * must be called before.
  249. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  250. * (recommended to protect the FLASH memory against possible unwanted operation)
  251. * @note Half page write is possible only from SRAM.
  252. * @note A half page is written to the program memory only if the first
  253. * address to load is the start address of a half page (multiple of 64
  254. * bytes) and the 15 remaining words to load are in the same half page.
  255. * @note During the Program memory half page write all read operations are
  256. * forbidden (this includes DMA read operations and debugger read
  257. * operations such as breakpoints, periodic updates, etc.).
  258. * @note If a PGAERR is set during a Program memory half page write, the
  259. * complete write operation is aborted. Software should then reset the
  260. * FPRG and PROG/DATA bits and restart the write operation from the
  261. * beginning.
  262. * @retval HAL status
  263. */
  264. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t* pBuffer)
  265. {
  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. /* Proceed to program the new half page */
  273. SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  274. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  275. /* Disable all IRQs */
  276. __disable_irq();
  277. /* Write one half page directly with 16 different words */
  278. while(count < 16U)
  279. {
  280. /* Address doesn't need to be increased */
  281. *(__IO uint32_t*) Address = *pBuffer;
  282. pBuffer++;
  283. count ++;
  284. }
  285. /* Enable IRQs */
  286. __enable_irq();
  287. /* Wait for last operation to be completed */
  288. status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  289. /* If the write operation is completed, disable the PROG and FPRG bits */
  290. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  291. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
  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. * @arg @ref HAL_FLASH_ERROR_RD FLASH Read Protection error flag (PCROP)
  315. * @arg @ref HAL_FLASH_ERROR_SIZE FLASH Programming Parallelism error flag
  316. * @arg @ref HAL_FLASH_ERROR_PGA FLASH Programming Alignment error flag
  317. * @arg @ref HAL_FLASH_ERROR_WRP FLASH Write protected error flag
  318. * @arg @ref HAL_FLASH_ERROR_OPTV FLASH Option valid error flag
  319. * @arg @ref HAL_FLASH_ERROR_FWWERR FLASH Write or Erase operation aborted
  320. * @arg @ref HAL_FLASH_ERROR_NOTZERO FLASH Write operation is done in a not-erased region
  321. * @retval HAL Status
  322. */
  323. __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_GetError(uint32_t * Error)
  324. {
  325. *Error = pFlash.ErrorCode;
  326. return HAL_OK;
  327. }
  328. /**
  329. * @}
  330. */
  331. /**
  332. * @}
  333. */
  334. /** @addtogroup FLASH_RAMFUNC_Private_Functions
  335. * @{
  336. */
  337. /**
  338. * @brief Set the specific FLASH error flag.
  339. * @retval HAL Status
  340. */
  341. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_SetErrorCode(void)
  342. {
  343. uint32_t flags = 0;
  344. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  345. {
  346. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  347. flags |= FLASH_FLAG_WRPERR;
  348. }
  349. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
  350. {
  351. pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
  352. flags |= FLASH_FLAG_PGAERR;
  353. }
  354. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
  355. {
  356. pFlash.ErrorCode |= HAL_FLASH_ERROR_SIZE;
  357. flags |= FLASH_FLAG_SIZERR;
  358. }
  359. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  360. {
  361. /* WARNING : On the first cut of STM32L031xx and STM32L041xx devices,
  362. * (RefID = 0x1000) the FLASH_FLAG_OPTVERR bit was not behaving
  363. * as expected. If the user run an application using the first
  364. * cut of the STM32L031xx device or the first cut of the STM32L041xx
  365. * device, this error should be ignored. The revId of the device
  366. * can be retrieved via the HAL_GetREVID() function.
  367. *
  368. */
  369. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  370. flags |= FLASH_FLAG_OPTVERR;
  371. }
  372. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
  373. {
  374. pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
  375. flags |= FLASH_FLAG_RDERR;
  376. }
  377. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR))
  378. {
  379. pFlash.ErrorCode |= HAL_FLASH_ERROR_FWWERR;
  380. flags |= HAL_FLASH_ERROR_FWWERR;
  381. }
  382. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR))
  383. {
  384. pFlash.ErrorCode |= HAL_FLASH_ERROR_NOTZERO;
  385. flags |= FLASH_FLAG_NOTZEROERR;
  386. }
  387. /* Clear FLASH error pending bits */
  388. __HAL_FLASH_CLEAR_FLAG(flags);
  389. return HAL_OK;
  390. }
  391. /**
  392. * @brief Wait for a FLASH operation to complete.
  393. * @param Timeout maximum flash operationtimeout
  394. * @retval HAL status
  395. */
  396. static __RAM_FUNC HAL_StatusTypeDef FLASHRAM_WaitForLastOperation(uint32_t Timeout)
  397. {
  398. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  399. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  400. flag will be set */
  401. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00U))
  402. {
  403. Timeout--;
  404. }
  405. if(Timeout == 0x00U)
  406. {
  407. return HAL_TIMEOUT;
  408. }
  409. /* Check FLASH End of Operation flag */
  410. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  411. {
  412. /* Clear FLASH End of Operation pending bit */
  413. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  414. }
  415. if( __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  416. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) ||
  417. __HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) ||
  418. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  419. __HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) ||
  420. __HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR) ||
  421. __HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR) )
  422. {
  423. /*Save the error code*/
  424. /* WARNING : On the first cut of STM32L031xx and STM32L041xx devices,
  425. * (RefID = 0x1000) the FLASH_FLAG_OPTVERR bit was not behaving
  426. * as expected. If the user run an application using the first
  427. * cut of the STM32L031xx device or the first cut of the STM32L041xx
  428. * device, this error should be ignored. The revId of the device
  429. * can be retrieved via the HAL_GetREVID() function.
  430. *
  431. */
  432. FLASHRAM_SetErrorCode();
  433. return HAL_ERROR;
  434. }
  435. /* There is no error flag set */
  436. return HAL_OK;
  437. }
  438. /**
  439. * @}
  440. */
  441. /**
  442. * @}
  443. */
  444. #endif /* HAL_FLASH_MODULE_ENABLED */
  445. /**
  446. * @}
  447. */
  448. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/