stm32l4xx_hal_flash_ramfunc.c 7.9 KB

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