stm32g4xx_hal_flash_ramfunc.c 7.8 KB

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