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