stm32l4xx_hal_flash_ramfunc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. * + FLASH HalfPage Programming
  9. * + FLASH Power Down in Run mode
  10. *
  11. * @verbatim
  12. ==============================================================================
  13. ##### Flash RAM functions #####
  14. ==============================================================================
  15. *** ARM Compiler ***
  16. --------------------
  17. [..] RAM functions are defined using the toolchain options.
  18. Functions that are executed in RAM should reside in a separate
  19. source module. Using the 'Options for File' dialog you can simply change
  20. the 'Code / Const' area of a module to a memory space in physical RAM.
  21. Available memory areas are declared in the 'Target' tab of the
  22. Options for Target' dialog.
  23. *** ICCARM Compiler ***
  24. -----------------------
  25. [..] RAM functions are defined using a specific toolchain keyword "__ramfunc".
  26. *** GNU Compiler ***
  27. --------------------
  28. [..] RAM functions are defined using a specific toolchain attribute
  29. "__attribute__((section(".RamFunc")))".
  30. @endverbatim
  31. ******************************************************************************
  32. * @attention
  33. *
  34. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  35. * All rights reserved.</center></h2>
  36. *
  37. * This software component is licensed by ST under BSD 3-Clause license,
  38. * the "License"; You may not use this file except in compliance with the
  39. * License. You may obtain a copy of the License at:
  40. * opensource.org/licenses/BSD-3-Clause
  41. *
  42. ******************************************************************************
  43. */
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32l4xx_hal.h"
  46. /** @addtogroup STM32L4xx_HAL_Driver
  47. * @{
  48. */
  49. /** @defgroup FLASH_RAMFUNC FLASH_RAMFUNC
  50. * @brief FLASH functions executed from RAM
  51. * @{
  52. */
  53. #ifdef HAL_FLASH_MODULE_ENABLED
  54. /* Private typedef -----------------------------------------------------------*/
  55. /* Private define ------------------------------------------------------------*/
  56. /* Private macro -------------------------------------------------------------*/
  57. /* Private variables ---------------------------------------------------------*/
  58. extern FLASH_ProcessTypeDef pFlash;
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* Exported functions -------------------------------------------------------*/
  61. /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH in RAM function Exported Functions
  62. * @{
  63. */
  64. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions
  65. * @brief Data transfers functions
  66. *
  67. @verbatim
  68. ===============================================================================
  69. ##### ramfunc functions #####
  70. ===============================================================================
  71. [..]
  72. This subsection provides a set of functions that should be executed from RAM.
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Enable the Power down in Run Mode
  78. * @note This function should be called and executed from SRAM memory
  79. * @retval None
  80. */
  81. __RAM_FUNC HAL_FLASHEx_EnableRunPowerDown(void)
  82. {
  83. /* Enable the Power Down in Run mode*/
  84. __HAL_FLASH_POWER_DOWN_ENABLE();
  85. return HAL_OK;
  86. }
  87. /**
  88. * @brief Disable the Power down in Run Mode
  89. * @note This function should be called and executed from SRAM memory
  90. * @retval None
  91. */
  92. __RAM_FUNC HAL_FLASHEx_DisableRunPowerDown(void)
  93. {
  94. /* Disable the Power Down in Run mode*/
  95. __HAL_FLASH_POWER_DOWN_DISABLE();
  96. return HAL_OK;
  97. }
  98. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  99. /**
  100. * @brief Program the FLASH DBANK User Option Byte.
  101. *
  102. * @note To configure the user option bytes, the option lock bit OPTLOCK must
  103. * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
  104. * @note To modify the DBANK option byte, no PCROP region should be defined.
  105. * To deactivate PCROP, user should perform RDP changing
  106. *
  107. * @param DBankConfig: The FLASH DBANK User Option Byte value.
  108. * This parameter can be one of the following values:
  109. * @arg OB_DBANK_128_BITS: Single-bank with 128-bits data
  110. * @arg OB_DBANK_64_BITS: Dual-bank with 64-bits data
  111. *
  112. * @retval HAL status
  113. */
  114. __RAM_FUNC HAL_FLASHEx_OB_DBankConfig(uint32_t DBankConfig)
  115. {
  116. register uint32_t count, reg;
  117. HAL_StatusTypeDef status = HAL_ERROR;
  118. /* Process Locked */
  119. __HAL_LOCK(&pFlash);
  120. /* Check if the PCROP is disabled */
  121. reg = FLASH->PCROP1SR;
  122. if (reg > FLASH->PCROP1ER)
  123. {
  124. reg = FLASH->PCROP2SR;
  125. if (reg > FLASH->PCROP2ER)
  126. {
  127. /* Disable Flash prefetch */
  128. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  129. if (READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != 0U)
  130. {
  131. /* Disable Flash instruction cache */
  132. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  133. /* Flush Flash instruction cache */
  134. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  135. }
  136. if (READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
  137. {
  138. /* Disable Flash data cache */
  139. __HAL_FLASH_DATA_CACHE_DISABLE();
  140. /* Flush Flash data cache */
  141. __HAL_FLASH_DATA_CACHE_RESET();
  142. }
  143. /* Disable WRP zone 1 of 1st bank if needed */
  144. reg = FLASH->WRP1AR;
  145. if (((reg & FLASH_WRP1AR_WRP1A_STRT) >> FLASH_WRP1AR_WRP1A_STRT_Pos) <=
  146. ((reg & FLASH_WRP1AR_WRP1A_END) >> FLASH_WRP1AR_WRP1A_END_Pos))
  147. {
  148. MODIFY_REG(FLASH->WRP1AR, (FLASH_WRP1AR_WRP1A_STRT | FLASH_WRP1AR_WRP1A_END), FLASH_WRP1AR_WRP1A_STRT);
  149. }
  150. /* Disable WRP zone 2 of 1st bank if needed */
  151. reg = FLASH->WRP1BR;
  152. if (((reg & FLASH_WRP1BR_WRP1B_STRT) >> FLASH_WRP1BR_WRP1B_STRT_Pos) <=
  153. ((reg & FLASH_WRP1BR_WRP1B_END) >> FLASH_WRP1BR_WRP1B_END_Pos))
  154. {
  155. MODIFY_REG(FLASH->WRP1BR, (FLASH_WRP1BR_WRP1B_STRT | FLASH_WRP1BR_WRP1B_END), FLASH_WRP1BR_WRP1B_STRT);
  156. }
  157. /* Disable WRP zone 1 of 2nd bank if needed */
  158. reg = FLASH->WRP2AR;
  159. if (((reg & FLASH_WRP2AR_WRP2A_STRT) >> FLASH_WRP2AR_WRP2A_STRT_Pos) <=
  160. ((reg & FLASH_WRP2AR_WRP2A_END) >> FLASH_WRP2AR_WRP2A_END_Pos))
  161. {
  162. MODIFY_REG(FLASH->WRP2AR, (FLASH_WRP2AR_WRP2A_STRT | FLASH_WRP2AR_WRP2A_END), FLASH_WRP2AR_WRP2A_STRT);
  163. }
  164. /* Disable WRP zone 2 of 2nd bank if needed */
  165. reg = FLASH->WRP2BR;
  166. if (((reg & FLASH_WRP2BR_WRP2B_STRT) >> FLASH_WRP2BR_WRP2B_STRT_Pos) <=
  167. ((reg & FLASH_WRP2BR_WRP2B_END) >> FLASH_WRP2BR_WRP2B_END_Pos))
  168. {
  169. MODIFY_REG(FLASH->WRP2BR, (FLASH_WRP2BR_WRP2B_STRT | FLASH_WRP2BR_WRP2B_END), FLASH_WRP2BR_WRP2B_STRT);
  170. }
  171. /* Modify the DBANK user option byte */
  172. MODIFY_REG(FLASH->OPTR, FLASH_OPTR_DBANK, DBankConfig);
  173. /* Set OPTSTRT Bit */
  174. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  175. /* Wait for last operation to be completed */
  176. /* 8 is the number of required instruction cycles for the below loop statement (timeout expressed in ms) */
  177. count = FLASH_TIMEOUT_VALUE * (SystemCoreClock / 8U / 1000U);
  178. do
  179. {
  180. if (count == 0U)
  181. {
  182. break;
  183. }
  184. count--;
  185. } while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET);
  186. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  187. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  188. /* Set the bit to force the option byte reloading */
  189. SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
  190. }
  191. }
  192. /* Process Unlocked */
  193. __HAL_UNLOCK(&pFlash);
  194. return status;
  195. }
  196. #endif
  197. /**
  198. * @}
  199. */
  200. /**
  201. * @}
  202. */
  203. #endif /* HAL_FLASH_MODULE_ENABLED */
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @}
  209. */
  210. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/