stm32l4xx_hal_flash_ramfunc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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</center></h2>
  35. *
  36. * Redistribution and use in source and binary forms, with or without modification,
  37. * are permitted provided that the following conditions are met:
  38. * 1. Redistributions of source code must retain the above copyright notice,
  39. * this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright notice,
  41. * this list of conditions and the following disclaimer in the documentation
  42. * and/or other materials provided with the distribution.
  43. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  50. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  51. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  54. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  55. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  56. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. *
  58. ******************************************************************************
  59. */
  60. /* Includes ------------------------------------------------------------------*/
  61. #include "stm32l4xx_hal.h"
  62. /** @addtogroup STM32L4xx_HAL_Driver
  63. * @{
  64. */
  65. /** @defgroup FLASH_RAMFUNC FLASH_RAMFUNC
  66. * @brief FLASH functions executed from RAM
  67. * @{
  68. */
  69. #ifdef HAL_FLASH_MODULE_ENABLED
  70. /* Private typedef -----------------------------------------------------------*/
  71. /* Private define ------------------------------------------------------------*/
  72. /* Private macro -------------------------------------------------------------*/
  73. /* Private variables ---------------------------------------------------------*/
  74. extern FLASH_ProcessTypeDef pFlash;
  75. /* Private function prototypes -----------------------------------------------*/
  76. /* Exported functions -------------------------------------------------------*/
  77. /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH in RAM function Exported Functions
  78. * @{
  79. */
  80. /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions
  81. * @brief Data transfers functions
  82. *
  83. @verbatim
  84. ===============================================================================
  85. ##### ramfunc functions #####
  86. ===============================================================================
  87. [..]
  88. This subsection provides a set of functions that should be executed from RAM.
  89. @endverbatim
  90. * @{
  91. */
  92. /**
  93. * @brief Enable the Power down in Run Mode
  94. * @note This function should be called and executed from SRAM memory
  95. * @retval None
  96. */
  97. __RAM_FUNC HAL_FLASHEx_EnableRunPowerDown(void)
  98. {
  99. /* Enable the Power Down in Run mode*/
  100. __HAL_FLASH_POWER_DOWN_ENABLE();
  101. return HAL_OK;
  102. }
  103. /**
  104. * @brief Disable the Power down in Run Mode
  105. * @note This function should be called and executed from SRAM memory
  106. * @retval None
  107. */
  108. __RAM_FUNC HAL_FLASHEx_DisableRunPowerDown(void)
  109. {
  110. /* Disable the Power Down in Run mode*/
  111. __HAL_FLASH_POWER_DOWN_DISABLE();
  112. return HAL_OK;
  113. }
  114. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  115. /**
  116. * @brief Program the FLASH DBANK User Option Byte.
  117. *
  118. * @note To configure the user option bytes, the option lock bit OPTLOCK must
  119. * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
  120. * @note To modify the DBANK option byte, no PCROP region should be defined.
  121. * To deactivate PCROP, user should perform RDP changing
  122. *
  123. * @param DBankConfig: The FLASH DBANK User Option Byte value.
  124. * This parameter can be one of the following values:
  125. * @arg OB_DBANK_128_BITS: Single-bank with 128-bits data
  126. * @arg OB_DBANK_64_BITS: Dual-bank with 64-bits data
  127. *
  128. * @retval HAL status
  129. */
  130. __RAM_FUNC HAL_FLASHEx_OB_DBankConfig(uint32_t DBankConfig)
  131. {
  132. register uint32_t count, reg;
  133. HAL_StatusTypeDef status = HAL_ERROR;
  134. /* Process Locked */
  135. __HAL_LOCK(&pFlash);
  136. /* Check if the PCROP is disabled */
  137. reg = FLASH->PCROP1SR;
  138. if (reg > FLASH->PCROP1ER)
  139. {
  140. reg = FLASH->PCROP2SR;
  141. if (reg > FLASH->PCROP2ER)
  142. {
  143. /* Disable Flash prefetch */
  144. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  145. if (READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != RESET)
  146. {
  147. /* Disable Flash instruction cache */
  148. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  149. /* Flush Flash instruction cache */
  150. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  151. }
  152. if (READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
  153. {
  154. /* Disable Flash data cache */
  155. __HAL_FLASH_DATA_CACHE_DISABLE();
  156. /* Flush Flash data cache */
  157. __HAL_FLASH_DATA_CACHE_RESET();
  158. }
  159. /* Disable WRP zone 1 of 1st bank if needed */
  160. reg = FLASH->WRP1AR;
  161. if (((reg & FLASH_WRP1AR_WRP1A_STRT) >> POSITION_VAL(FLASH_WRP1AR_WRP1A_STRT)) <=
  162. ((reg & FLASH_WRP1AR_WRP1A_END) >> POSITION_VAL(FLASH_WRP1AR_WRP1A_END)))
  163. {
  164. MODIFY_REG(FLASH->WRP1AR, (FLASH_WRP1AR_WRP1A_STRT | FLASH_WRP1AR_WRP1A_END), FLASH_WRP1AR_WRP1A_STRT);
  165. }
  166. /* Disable WRP zone 2 of 1st bank if needed */
  167. reg = FLASH->WRP1BR;
  168. if (((reg & FLASH_WRP1BR_WRP1B_STRT) >> POSITION_VAL(FLASH_WRP1BR_WRP1B_STRT)) <=
  169. ((reg & FLASH_WRP1BR_WRP1B_END) >> POSITION_VAL(FLASH_WRP1BR_WRP1B_END)))
  170. {
  171. MODIFY_REG(FLASH->WRP1BR, (FLASH_WRP1BR_WRP1B_STRT | FLASH_WRP1BR_WRP1B_END), FLASH_WRP1BR_WRP1B_STRT);
  172. }
  173. /* Disable WRP zone 1 of 2nd bank if needed */
  174. reg = FLASH->WRP2AR;
  175. if (((reg & FLASH_WRP2AR_WRP2A_STRT) >> POSITION_VAL(FLASH_WRP2AR_WRP2A_STRT)) <=
  176. ((reg & FLASH_WRP2AR_WRP2A_END) >> POSITION_VAL(FLASH_WRP2AR_WRP2A_END)))
  177. {
  178. MODIFY_REG(FLASH->WRP2AR, (FLASH_WRP2AR_WRP2A_STRT | FLASH_WRP2AR_WRP2A_END), FLASH_WRP2AR_WRP2A_STRT);
  179. }
  180. /* Disable WRP zone 2 of 2nd bank if needed */
  181. reg = FLASH->WRP2BR;
  182. if (((reg & FLASH_WRP2BR_WRP2B_STRT) >> POSITION_VAL(FLASH_WRP2BR_WRP2B_STRT)) <=
  183. ((reg & FLASH_WRP2BR_WRP2B_END) >> POSITION_VAL(FLASH_WRP2BR_WRP2B_END)))
  184. {
  185. MODIFY_REG(FLASH->WRP2BR, (FLASH_WRP2BR_WRP2B_STRT | FLASH_WRP2BR_WRP2B_END), FLASH_WRP2BR_WRP2B_STRT);
  186. }
  187. /* Modify the DBANK user option byte */
  188. MODIFY_REG(FLASH->OPTR, FLASH_OPTR_DBANK, DBankConfig);
  189. /* Set OPTSTRT Bit */
  190. SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  191. /* Wait for last operation to be completed */
  192. /* 8 is the number of required instruction cycles for the below loop statement (timeout expressed in ms) */
  193. count = FLASH_TIMEOUT_VALUE * (SystemCoreClock / 8 / 1000);
  194. do
  195. {
  196. if (count-- == 0)
  197. {
  198. break;
  199. }
  200. } while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET);
  201. /* If the option byte program operation is completed, disable the OPTSTRT Bit */
  202. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
  203. /* Set the bit to force the option byte reloading */
  204. SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
  205. }
  206. }
  207. /* Process Unlocked */
  208. __HAL_UNLOCK(&pFlash);
  209. return status;
  210. }
  211. #endif
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @}
  217. */
  218. #endif /* HAL_FLASH_MODULE_ENABLED */
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/