]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_flash_ramfunc.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_flash_ramfunc.c
1 /**
2 ******************************************************************************
3 * @file stm32l0xx_hal_flash_ramfunc.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 06-February-2015
7 * @brief FLASH RAMFUNC driver.
8 * This file provides a Flash firmware functions which should be
9 * executed from internal SRAM
10 *
11 * @verbatim
12
13 *** ARM Compiler ***
14 --------------------
15 [..] RAM functions are defined using the toolchain options.
16 Functions that are be executed in RAM should reside in a separate
17 source module. Using the 'Options for File' dialog you can simply change
18 the 'Code / Const' area of a module to a memory space in physical RAM.
19 Available memory areas are declared in the 'Target' tab of the
20 Options for Target' dialog.
21
22 *** ICCARM Compiler ***
23 -----------------------
24 [..] RAM functions are defined using a specific toolchain keyword "__ramfunc".
25
26 *** GNU Compiler ***
27 --------------------
28 [..] RAM functions are defined using a specific toolchain attribute
29 "__attribute__((section(".RamFunc")))".
30
31 @endverbatim
32 ******************************************************************************
33 * @attention
34 *
35 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
36 *
37 * Redistribution and use in source and binary forms, with or without modification,
38 * are permitted provided that the following conditions are met:
39 * 1. Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright notice,
42 * this list of conditions and the following disclaimer in the documentation
43 * and/or other materials provided with the distribution.
44 * 3. Neither the name of STMicroelectronics nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 ******************************************************************************
60 */
61
62 /* Includes ------------------------------------------------------------------*/
63 #include "stm32l0xx_hal.h"
64
65 #ifdef HAL_FLASH_MODULE_ENABLED
66 /** @addtogroup STM32L0xx_HAL_Driver
67 * @{
68 */
69
70 /** @addtogroup FLASHRamfunc
71 * @brief FLASH functions executed from RAM
72 * @{
73 */
74
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 /* Private macro -------------------------------------------------------------*/
78 /* Private variables ---------------------------------------------------------*/
79 /* Private function prototypes -----------------------------------------------*/
80 static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout);
81 static __RAM_FUNC FLASHRAM_SetErrorCode(void);
82
83
84 /** @addtogroup FLASHRamfunc_Exported_Functions
85 *
86 @verbatim
87 ===============================================================================
88 ##### ramfunc functions #####
89 ===============================================================================
90 [..]
91 This subsection provides a set of functions that should be executed from RAM
92 transfers.
93
94 @endverbatim
95 * @{
96 */
97
98 /** @addtogroup FLASHRamfunc_Exported_Functions_Group1
99 * @{
100 */
101
102 /**
103 * @brief Enable the power down mode during RUN mode.
104 * @note This function can be used only when the user code is running from Internal SRAM.
105 * @param None
106 * @retval None
107 */
108 __RAM_FUNC HAL_FLASHEx_EnableRunPowerDown(void)
109 {
110 /* Enable the Power Down in Run mode*/
111 __HAL_FLASH_POWER_DOWN_ENABLE();
112 return HAL_OK;
113 }
114
115 /**
116 * @brief Disable the power down mode during RUN mode.
117 * @note This function can be used only when the user code is running from Internal SRAM.
118 * @param None
119 * @retval None
120 */
121 __RAM_FUNC HAL_FLASHEx_DisableRunPowerDown(void)
122 {
123 /* Disable the Power Down in Run mode*/
124 __HAL_FLASH_POWER_DOWN_DISABLE();
125 return HAL_OK;
126 }
127
128 /**
129 * @}
130 */
131
132 /** @addtogroup FLASHRamfunc_Exported_Functions_Group2
133 *
134 @verbatim
135 @endverbatim
136 * @{
137 */
138
139 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
140 /**
141 * @brief Erases a specified 2 pages in program memory in parallel.
142 * @note This function can be used only for STM32L07xxx/STM32L08xxx devices.
143 * To correctly run this function, the HAL_FLASH_Unlock() function
144 * must be called before.
145 * Call the HAL_FLASH_Lock() to disable the flash memory access
146 * (recommended to protect the FLASH memory against possible unwanted operation).
147 * @param Page_Address1: The page address in program memory to be erased in
148 * the first Bank (BANK1). This parameter should be between FLASH_BASE
149 * and FLASH_BANK1_END.
150 * @param Page_Address2: The page address in program memory to be erased in
151 * the second Bank (BANK2). This parameter should be between FLASH_BANK2_BASE
152 * and FLASH_BANK2_END.
153 * @note A Page is erased in the Program memory only if the address to load
154 * is the start address of a page (multiple of 128 bytes).
155 * @retval HAL Status: The returned value can be:
156 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
157 */
158 __RAM_FUNC HAL_FLASHEx_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
159 {
160 HAL_StatusTypeDef status = HAL_OK;
161
162 /* Wait for last operation to be completed */
163 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
164
165 if(status == HAL_OK)
166 {
167 /* Proceed to erase the page */
168 SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
169 SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
170 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
171
172 /* Write 00000000h to the first word of the first program page to erase */
173 *(__IO uint32_t *)Page_Address1 = 0x00000000;
174 /* Write 00000000h to the first word of the second program page to erase */
175 *(__IO uint32_t *)Page_Address2 = 0x00000000;
176
177 /* Wait for last operation to be completed */
178 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
179
180 /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
181 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
182 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
183 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
184 }
185 /* Return the erasesStatus */
186 return status;
187 }
188
189 /**
190 * @brief Programs 2 half pages in program memory in parallel. The half page size is 16 Words.
191 * @note This function can be used only for STM32L07xxx/STM32L08xxx devices.
192 * @param Address1: specifies the first address to be written in the first bank
193 * (BANK1). This parameter should be between FLASH_BASE and (FLASH_BANK1_END - FLASH_PAGE_SIZE).
194 * @param pBuffer1: pointer to the buffer containing the data to be written
195 * to the first half page in the first bank.
196 * @param Address2: specifies the second address to be written in the second bank
197 * (BANK2). This parameter should be between FLASH_BANK2_BASE and (FLASH_BANK2_END - FLASH_PAGE_SIZE).
198 * @param pBuffer2: pointer to the buffer containing the data to be written
199 * to the second half page in the second bank.
200 * @note To correctly run this function, the HAL_FLASH_Unlock() function
201 * must be called before.
202 * Call the HAL_FLASH_Lock() to disable the flash memory access
203 * (recommended to protect the FLASH memory against possible unwanted operation).
204 * @note Half page write is possible only from SRAM.
205 * @note A half page is written to the program memory only if the first
206 * address to load is the start address of a half page (multiple of 64
207 * bytes) and the 15 remaining words to load are in the same half page.
208 * @note During the Program memory half page write all read operations are
209 * forbidden (this includes DMA read operations and debugger read
210 * operations such as breakpoints, periodic updates, etc.).
211 * @note If a PGAERR is set during a Program memory half page write, the
212 * complete write operation is aborted. Software should then reset the
213 * FPRG and PROG/DATA bits and restart the write operation from the
214 * beginning.
215 * @retval HAL Status: The returned value can be:
216 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
217 */
218 __RAM_FUNC HAL_FLASHEx_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
219 {
220 uint32_t count;
221 HAL_StatusTypeDef status;
222
223 /* Wait for last operation to be completed */
224 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
225
226 if(status == HAL_OK)
227 {
228 /* Proceed to program the new half page */
229 SET_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
230 SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
231 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
232
233
234 /* Wait for last operation to be completed */
235 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
236
237 if(status == HAL_OK)
238 {
239 /* Write one half page,
240 Address1 doesn't need to be increased */
241 count = 0;
242
243 /* Disable all IRQs */
244 __disable_irq();
245
246 while(count < 16)
247 {
248 *(__IO uint32_t*) Address1 = *pBuffer1;
249 pBuffer1++;
250 count++;
251 }
252
253 /* Write the second half page,
254 Address2 doesn't need to be increased */
255 count = 0;
256 while(count < 16)
257 {
258 *(__IO uint32_t*) Address2 = *pBuffer2;
259 pBuffer2++;
260 count++;
261 }
262
263 /* Enable IRQs */
264 __enable_irq();
265
266 /* Wait for last operation to be completed */
267 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
268 }
269 /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
270 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
271 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
272 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PARALLBANK);
273 }
274 /* Return the Write Status */
275 return status;
276 }
277 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
278
279 /**
280 * @brief Program a half page in program memory.
281 * @param Address: specifies the address to be written.
282 * @param pBuffer: pointer to the buffer containing the data to be written to
283 * the half page.
284 * @note To correctly run this function, the HAL_FLASH_Unlock() function
285 * must be called before.
286 * Call the HAL_FLASH_Lock() to disable the flash memory access
287 * (recommended to protect the FLASH memory against possible unwanted operation)
288 * @note Half page write is possible only from SRAM.
289 * @note A half page is written to the program memory only if the first
290 * address to load is the start address of a half page (multiple of 64
291 * bytes) and the 15 remaining words to load are in the same half page.
292 * @note During the Program memory half page write all read operations are
293 * forbidden (this includes DMA read operations and debugger read
294 * operations such as breakpoints, periodic updates, etc.).
295 * @note If a PGAERR is set during a Program memory half page write, the
296 * complete write operation is aborted. Software should then reset the
297 * FPRG and PROG/DATA bits and restart the write operation from the
298 * beginning.
299 * @retval HAL Status: The returned value can be:
300 * HAL_ERROR, HAL_OK or HAL_TIMEOUT.
301 */
302 __RAM_FUNC HAL_FLASHEx_HalfPageProgram(uint32_t Address, uint32_t *pBuffer)
303 {
304 uint32_t count;
305 HAL_StatusTypeDef status;
306
307 /* Wait for last operation to be completed */
308 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
309
310 if(status == HAL_OK)
311 {
312 /* Proceed to program the new half page */
313 SET_BIT(FLASH->PECR, FLASH_PECR_FPRG);
314 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
315
316
317 count = 0;
318 /* Write one half page,
319 Address doesn't need to be increased */
320
321 /* Disable all IRQs */
322 __disable_irq();
323
324 while(count < 16)
325 {
326 *(__IO uint32_t*) Address = *pBuffer;
327 pBuffer++;
328 count++;
329 }
330
331 /* Enable IRQs */
332 __enable_irq();
333
334 /* Wait for last operation to be completed */
335 status = FLASHRAM_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
336
337 /* If the write operation is completed, disable the PROG and FPRG bits */
338 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
339 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FPRG);
340 }
341 /* Return the write status */
342 return status;
343 }
344
345 /**
346 * @brief Get the specific FLASH errors flag.
347 * @param error pointer is the error value. It can be a mixed of :
348 * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
349 * @arg HAL_FLASH_ERROR_SIZE: FLASH Programming Parallelism error flag
350 * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
351 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
352 * @arg HAL_FLASH_ERROR_OPTV: FLASH Option valid error flag
353 * @arg HAL_FLASH_ERROR_FWWERR: FLASH Write or Errase operation aborted
354 * @arg HAL_FLASH_ERROR_NOTZERO: FLASH Write operation is done in a not-erased region
355 */
356 __RAM_FUNC HAL_FLASHRAM_GetError(uint32_t * error)
357 {
358 *error = ProcFlash.ErrorCode;
359 return HAL_OK;
360 }
361
362
363
364 /**
365 * @}
366 */
367
368 /**
369 * @}
370 */
371
372 /** @defgroup FLASHRamfunc_Private_Functions FLASH RAM Private Functions
373 * @{
374 */
375
376 /**
377 * @brief Set the specific FLASH error flag.
378 * @param None
379 * @retval None
380 */
381 static __RAM_FUNC FLASHRAM_SetErrorCode(void)
382 {
383 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
384 {
385 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
386 }
387 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
388 {
389 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
390 }
391 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
392 {
393 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_SIZE;
394 }
395 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
396 {
397 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
398 }
399 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
400 {
401 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
402 }
403 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR))
404 {
405 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_FWWERR;
406 }
407 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR))
408 {
409 ProcFlash.ErrorCode |= HAL_FLASH_ERROR_NOTZERO;
410 }
411
412 /* Errors are now stored, clear errors flags */
413 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR |
414 FLASH_FLAG_OPTVERR | FLASH_FLAG_RDERR | FLASH_FLAG_FWWERR |
415 FLASH_FLAG_NOTZEROERR);
416 return HAL_OK;
417 }
418
419
420 /**
421 * @brief Wait for a FLASH operation to complete.
422 * @param Timeout: maximum flash operationtimeout
423 * @retval HAL status
424 */
425 static __RAM_FUNC FLASHRAM_WaitForLastOperation(uint32_t Timeout)
426 {
427 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
428 Even if the FLASH operation fails, the BUSY flag will be reset and an error
429 flag will be set */
430
431 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) && (Timeout != 0x00))
432 {
433 Timeout--;
434 }
435
436 if(Timeout == 0x00 )
437 {
438 return HAL_TIMEOUT;
439 }
440
441 if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) ||
442 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) ||
443 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) ||
444 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) ||
445 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) ||
446 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FWWERR) != RESET) ||
447 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_NOTZEROERR) != RESET) )
448 {
449 /*Save the error code*/
450 FLASHRAM_SetErrorCode();
451 return HAL_ERROR;
452 }
453
454 /* If there is an error flag set */
455 return HAL_OK;
456 }
457
458 /**
459 * @}
460 */
461
462 /**
463 * @}
464 */
465
466 /**
467 * @}
468 */
469 #endif /* HAL_FLASH_MODULE_ENABLED */
470
471 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
472
Imprint / Impressum