0
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file stm32l4xx_hal_flash.c
|
|
4 * @author MCD Application Team
|
|
5 * @version V1.6.0
|
|
6 * @date 28-October-2016
|
|
7 * @brief FLASH HAL module driver.
|
|
8 * This file provides firmware functions to manage the following
|
|
9 * functionalities of the internal FLASH memory:
|
|
10 * + Program operations functions
|
|
11 * + Memory Control functions
|
|
12 * + Peripheral Errors functions
|
|
13 *
|
|
14 @verbatim
|
|
15 ==============================================================================
|
|
16 ##### FLASH peripheral features #####
|
|
17 ==============================================================================
|
|
18
|
|
19 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
|
|
20 to the Flash memory. It implements the erase and program Flash memory operations
|
|
21 and the read and write protection mechanisms.
|
|
22
|
|
23 [..] The Flash memory interface accelerates code execution with a system of instruction
|
|
24 prefetch and cache lines.
|
|
25
|
|
26 [..] The FLASH main features are:
|
|
27 (+) Flash memory read operations
|
|
28 (+) Flash memory program/erase operations
|
|
29 (+) Read / write protections
|
|
30 (+) Option bytes programming
|
|
31 (+) Prefetch on I-Code
|
|
32 (+) 32 cache lines of 4*64 bits on I-Code
|
|
33 (+) 8 cache lines of 4*64 bits on D-Code
|
|
34 (+) Error code correction (ECC) : Data in flash are 72-bits word
|
|
35 (8 bits added per double word)
|
|
36
|
|
37
|
|
38 ##### How to use this driver #####
|
|
39 ==============================================================================
|
|
40 [..]
|
|
41 This driver provides functions and macros to configure and program the FLASH
|
|
42 memory of all STM32L4xx devices.
|
|
43
|
|
44 (#) Flash Memory IO Programming functions:
|
|
45 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
|
|
46 HAL_FLASH_Lock() functions
|
|
47 (++) Program functions: double word and fast program (full row programming)
|
|
48 (++) There Two modes of programming :
|
|
49 (+++) Polling mode using HAL_FLASH_Program() function
|
|
50 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
|
|
51
|
|
52 (#) Interrupts and flags management functions :
|
|
53 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
|
|
54 (++) Callback functions are called when the flash operations are finished :
|
|
55 HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
|
|
56 HAL_FLASH_OperationErrorCallback()
|
|
57 (++) Get error flag status by calling HAL_GetError()
|
|
58
|
|
59 (#) Option bytes management functions :
|
|
60 (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
|
|
61 HAL_FLASH_OB_Lock() functions
|
|
62 (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
|
|
63 In this case, a reset is generated
|
|
64
|
|
65 [..]
|
|
66 In addition to these functions, this driver includes a set of macros allowing
|
|
67 to handle the following operations:
|
|
68 (+) Set the latency
|
|
69 (+) Enable/Disable the prefetch buffer
|
|
70 (+) Enable/Disable the Instruction cache and the Data cache
|
|
71 (+) Reset the Instruction cache and the Data cache
|
|
72 (+) Enable/Disable the Flash power-down during low-power run and sleep modes
|
|
73 (+) Enable/Disable the Flash interrupts
|
|
74 (+) Monitor the Flash flags status
|
|
75
|
|
76 @endverbatim
|
|
77 ******************************************************************************
|
|
78 * @attention
|
|
79 *
|
|
80 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
|
|
81 *
|
|
82 * Redistribution and use in source and binary forms, with or without modification,
|
|
83 * are permitted provided that the following conditions are met:
|
|
84 * 1. Redistributions of source code must retain the above copyright notice,
|
|
85 * this list of conditions and the following disclaimer.
|
|
86 * 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
87 * this list of conditions and the following disclaimer in the documentation
|
|
88 * and/or other materials provided with the distribution.
|
|
89 * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
90 * may be used to endorse or promote products derived from this software
|
|
91 * without specific prior written permission.
|
|
92 *
|
|
93 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
94 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
96 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
97 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
98 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
99 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
100 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
101 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
102 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
103 *
|
|
104 ******************************************************************************
|
|
105 */
|
|
106
|
|
107 /* Includes ------------------------------------------------------------------*/
|
|
108 #include "stm32l4xx_hal.h"
|
|
109
|
|
110 /** @addtogroup STM32L4xx_HAL_Driver
|
|
111 * @{
|
|
112 */
|
|
113
|
|
114 /** @defgroup FLASH FLASH
|
|
115 * @brief FLASH HAL module driver
|
|
116 * @{
|
|
117 */
|
|
118
|
|
119 #ifdef HAL_FLASH_MODULE_ENABLED
|
|
120
|
|
121 /* Private typedef -----------------------------------------------------------*/
|
|
122 /* Private defines -----------------------------------------------------------*/
|
|
123 #define FLASH_NB_DOUBLE_WORDS_IN_ROW 32
|
|
124 /* Private macros ------------------------------------------------------------*/
|
|
125 /* Private variables ---------------------------------------------------------*/
|
|
126 /** @defgroup FLASH_Private_Variables FLASH Private Variables
|
|
127 * @{
|
|
128 */
|
|
129 /**
|
|
130 * @brief Variable used for Program/Erase sectors under interruption
|
|
131 */
|
|
132 FLASH_ProcessTypeDef pFlash;
|
|
133 /**
|
|
134 * @}
|
|
135 */
|
|
136
|
|
137 /* Private function prototypes -----------------------------------------------*/
|
|
138 /** @defgroup FLASH_Private_Functions FLASH Private Functions
|
|
139 * @{
|
|
140 */
|
|
141 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
|
|
142 extern void FLASH_PageErase(uint32_t Page, uint32_t Banks);
|
|
143 extern void FLASH_FlushCaches(void);
|
|
144 static void FLASH_SetErrorCode(void);
|
|
145 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
|
|
146 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);
|
|
147 /**
|
|
148 * @}
|
|
149 */
|
|
150
|
|
151 /* Exported functions --------------------------------------------------------*/
|
|
152 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
|
|
153 * @{
|
|
154 */
|
|
155
|
|
156 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
|
|
157 * @brief Programming operation functions
|
|
158 *
|
|
159 @verbatim
|
|
160 ===============================================================================
|
|
161 ##### Programming operation functions #####
|
|
162 ===============================================================================
|
|
163 [..]
|
|
164 This subsection provides a set of functions allowing to manage the FLASH
|
|
165 program operations.
|
|
166
|
|
167 @endverbatim
|
|
168 * @{
|
|
169 */
|
|
170
|
|
171 /**
|
|
172 * @brief Program double word or fast program of a row at a specified address.
|
|
173 * @param TypeProgram: Indicate the way to program at a specified address.
|
|
174 * This parameter can be a value of @ref FLASH_Type_Program
|
|
175 * @param Address: specifies the address to be programmed.
|
|
176 * @param Data: specifies the data to be programmed
|
|
177 * This parameter is the data for the double word program and the address where
|
|
178 * are stored the data for the row fast program
|
|
179 *
|
|
180 * @retval HAL_StatusTypeDef HAL Status
|
|
181 */
|
|
182 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
|
|
183 {
|
|
184 HAL_StatusTypeDef status = HAL_ERROR;
|
|
185 uint32_t prog_bit = 0;
|
|
186
|
|
187 /* Process Locked */
|
|
188 __HAL_LOCK(&pFlash);
|
|
189
|
|
190 /* Check the parameters */
|
|
191 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
|
|
192
|
|
193 /* Wait for last operation to be completed */
|
|
194 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
|
|
195
|
|
196 if(status == HAL_OK)
|
|
197 {
|
|
198 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
|
|
199
|
|
200 /* Deactivate the data cache if they are activated to avoid data misbehavior */
|
|
201 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
|
|
202 {
|
|
203 /* Disable data cache */
|
|
204 __HAL_FLASH_DATA_CACHE_DISABLE();
|
|
205 pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
|
|
206 }
|
|
207 else
|
|
208 {
|
|
209 pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
|
|
210 }
|
|
211
|
|
212 if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
|
|
213 {
|
|
214 /* Program double-word (64-bit) at a specified address */
|
|
215 FLASH_Program_DoubleWord(Address, Data);
|
|
216 prog_bit = FLASH_CR_PG;
|
|
217 }
|
|
218 else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
|
|
219 {
|
|
220 /* Fast program a 32 row double-word (64-bit) at a specified address */
|
|
221 FLASH_Program_Fast(Address, (uint32_t)Data);
|
|
222
|
|
223 /* If it is the last row, the bit will be cleared at the end of the operation */
|
|
224 if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
|
|
225 {
|
|
226 prog_bit = FLASH_CR_FSTPG;
|
|
227 }
|
|
228 }
|
|
229
|
|
230 /* Wait for last operation to be completed */
|
|
231 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
|
|
232
|
|
233 /* If the program operation is completed, disable the PG or FSTPG Bit */
|
|
234 if (prog_bit != 0)
|
|
235 {
|
|
236 CLEAR_BIT(FLASH->CR, prog_bit);
|
|
237 }
|
|
238
|
|
239 /* Flush the caches to be sure of the data consistency */
|
|
240 FLASH_FlushCaches();
|
|
241 }
|
|
242
|
|
243 /* Process Unlocked */
|
|
244 __HAL_UNLOCK(&pFlash);
|
|
245
|
|
246 return status;
|
|
247 }
|
|
248
|
|
249 /**
|
|
250 * @brief Program double word or fast program of a row at a specified address with interrupt enabled.
|
|
251 * @param TypeProgram: Indicate the way to program at a specified address.
|
|
252 * This parameter can be a value of @ref FLASH_Type_Program
|
|
253 * @param Address: specifies the address to be programmed.
|
|
254 * @param Data: specifies the data to be programmed
|
|
255 * This parameter is the data for the double word program and the address where
|
|
256 * are stored the data for the row fast program
|
|
257 *
|
|
258 * @retval HAL Status
|
|
259 */
|
|
260 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
|
|
261 {
|
|
262 HAL_StatusTypeDef status = HAL_OK;
|
|
263
|
|
264 /* Check the parameters */
|
|
265 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
|
|
266
|
|
267 /* Process Locked */
|
|
268 __HAL_LOCK(&pFlash);
|
|
269
|
|
270 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
|
|
271
|
|
272 /* Deactivate the data cache if they are activated to avoid data misbehavior */
|
|
273 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
|
|
274 {
|
|
275 /* Disable data cache */
|
|
276 __HAL_FLASH_DATA_CACHE_DISABLE();
|
|
277 pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
|
|
278 }
|
|
279 else
|
|
280 {
|
|
281 pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
|
|
282 }
|
|
283
|
|
284 /* Set internal variables used by the IRQ handler */
|
|
285 if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
|
|
286 {
|
|
287 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;
|
|
288 }
|
|
289 else
|
|
290 {
|
|
291 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
|
|
292 }
|
|
293 pFlash.Address = Address;
|
|
294
|
|
295 /* Enable End of Operation and Error interrupts */
|
|
296 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
|
|
297
|
|
298 if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
|
|
299 {
|
|
300 /* Program double-word (64-bit) at a specified address */
|
|
301 FLASH_Program_DoubleWord(Address, Data);
|
|
302 }
|
|
303 else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
|
|
304 {
|
|
305 /* Fast program a 32 row double-word (64-bit) at a specified address */
|
|
306 FLASH_Program_Fast(Address, (uint32_t)Data);
|
|
307 }
|
|
308
|
|
309 return status;
|
|
310 }
|
|
311
|
|
312 /**
|
|
313 * @brief Handle FLASH interrupt request.
|
|
314 * @retval None
|
|
315 */
|
|
316 void HAL_FLASH_IRQHandler(void)
|
|
317 {
|
|
318 uint32_t tmp_page;
|
|
319
|
|
320 /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */
|
|
321 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));
|
|
322 #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx)
|
|
323 CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);
|
|
324 #endif
|
|
325
|
|
326 /* Disable the FSTPG Bit only if it is the last row programmed */
|
|
327 if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
|
|
328 {
|
|
329 CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
|
|
330 }
|
|
331
|
|
332 /* Check FLASH operation error flags */
|
|
333 if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
|
|
334 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
|
|
335 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)) ||
|
|
336 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) ||
|
|
337 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
|
|
338 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
|
|
339 defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
|
|
340 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY)))
|
|
341 #else
|
|
342 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
|
|
343 #endif
|
|
344 {
|
|
345 /*Save the error code*/
|
|
346 FLASH_SetErrorCode();
|
|
347
|
|
348 /* Flush the caches to be sure of the data consistency */
|
|
349 FLASH_FlushCaches() ;
|
|
350
|
|
351 /* FLASH error interrupt user callback */
|
|
352 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
|
|
353 {
|
|
354 HAL_FLASH_OperationErrorCallback(pFlash.Page);
|
|
355 }
|
|
356 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
|
|
357 {
|
|
358 HAL_FLASH_OperationErrorCallback(pFlash.Bank);
|
|
359 }
|
|
360 else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) ||
|
|
361 (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
|
|
362 {
|
|
363 HAL_FLASH_OperationErrorCallback(pFlash.Address);
|
|
364 }
|
|
365
|
|
366 /*Stop the procedure ongoing*/
|
|
367 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
368 }
|
|
369
|
|
370 /* Check FLASH End of Operation flag */
|
|
371 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
|
|
372 {
|
|
373 /* Clear FLASH End of Operation pending bit */
|
|
374 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
|
|
375
|
|
376 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
|
|
377 {
|
|
378 /* Nb of pages to erased can be decreased */
|
|
379 pFlash.NbPagesToErase--;
|
|
380
|
|
381 /* Check if there are still pages to erase*/
|
|
382 if(pFlash.NbPagesToErase != 0)
|
|
383 {
|
|
384 /* Indicate user which page has been erased*/
|
|
385 HAL_FLASH_EndOfOperationCallback(pFlash.Page);
|
|
386
|
|
387 /* Increment page number */
|
|
388 pFlash.Page++;
|
|
389 tmp_page = pFlash.Page;
|
|
390 FLASH_PageErase(tmp_page, pFlash.Bank);
|
|
391 }
|
|
392 else
|
|
393 {
|
|
394 /* No more pages to Erase */
|
|
395 /* Reset Address and stop Erase pages procedure */
|
|
396 pFlash.Page = 0xFFFFFFFF;
|
|
397 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
398
|
|
399 /* Flush the caches to be sure of the data consistency */
|
|
400 FLASH_FlushCaches() ;
|
|
401
|
|
402 /* FLASH EOP interrupt user callback */
|
|
403 HAL_FLASH_EndOfOperationCallback(pFlash.Page);
|
|
404 }
|
|
405 }
|
|
406 else
|
|
407 {
|
|
408 /* Flush the caches to be sure of the data consistency */
|
|
409 FLASH_FlushCaches() ;
|
|
410
|
|
411 if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
|
|
412 {
|
|
413 /* MassErase ended. Return the selected bank */
|
|
414 /* FLASH EOP interrupt user callback */
|
|
415 HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
|
|
416 }
|
|
417 else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) ||
|
|
418 (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
|
|
419 {
|
|
420 /* Program ended. Return the selected address */
|
|
421 /* FLASH EOP interrupt user callback */
|
|
422 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
|
|
423 }
|
|
424
|
|
425 /*Clear the procedure ongoing*/
|
|
426 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
427 }
|
|
428 }
|
|
429
|
|
430 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
|
|
431 {
|
|
432 /* Disable End of Operation and Error interrupts */
|
|
433 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
|
|
434
|
|
435 /* Process Unlocked */
|
|
436 __HAL_UNLOCK(&pFlash);
|
|
437 }
|
|
438 }
|
|
439
|
|
440 /**
|
|
441 * @brief FLASH end of operation interrupt callback.
|
|
442 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
|
|
443 * Mass Erase: Bank number which has been requested to erase
|
|
444 * Page Erase: Page which has been erased
|
|
445 * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
|
|
446 * Program: Address which was selected for data program
|
|
447 * @retval None
|
|
448 */
|
|
449 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
|
|
450 {
|
|
451 /* Prevent unused argument(s) compilation warning */
|
|
452 UNUSED(ReturnValue);
|
|
453
|
|
454 /* NOTE : This function should not be modified, when the callback is needed,
|
|
455 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
|
|
456 */
|
|
457 }
|
|
458
|
|
459 /**
|
|
460 * @brief FLASH operation error interrupt callback.
|
|
461 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
|
|
462 * Mass Erase: Bank number which has been requested to erase
|
|
463 * Page Erase: Page number which returned an error
|
|
464 * Program: Address which was selected for data program
|
|
465 * @retval None
|
|
466 */
|
|
467 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
|
|
468 {
|
|
469 /* Prevent unused argument(s) compilation warning */
|
|
470 UNUSED(ReturnValue);
|
|
471
|
|
472 /* NOTE : This function should not be modified, when the callback is needed,
|
|
473 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
|
|
474 */
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * @}
|
|
479 */
|
|
480
|
|
481 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
|
|
482 * @brief Management functions
|
|
483 *
|
|
484 @verbatim
|
|
485 ===============================================================================
|
|
486 ##### Peripheral Control functions #####
|
|
487 ===============================================================================
|
|
488 [..]
|
|
489 This subsection provides a set of functions allowing to control the FLASH
|
|
490 memory operations.
|
|
491
|
|
492 @endverbatim
|
|
493 * @{
|
|
494 */
|
|
495
|
|
496 /**
|
|
497 * @brief Unlock the FLASH control register access.
|
|
498 * @retval HAL Status
|
|
499 */
|
|
500 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
|
|
501 {
|
|
502 if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
|
|
503 {
|
|
504 /* Authorize the FLASH Registers access */
|
|
505 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
|
|
506 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
|
|
507 }
|
|
508 else
|
|
509 {
|
|
510 return HAL_ERROR;
|
|
511 }
|
|
512
|
|
513 return HAL_OK;
|
|
514 }
|
|
515
|
|
516 /**
|
|
517 * @brief Lock the FLASH control register access.
|
|
518 * @retval HAL Status
|
|
519 */
|
|
520 HAL_StatusTypeDef HAL_FLASH_Lock(void)
|
|
521 {
|
|
522 /* Set the LOCK Bit to lock the FLASH Registers access */
|
|
523 SET_BIT(FLASH->CR, FLASH_CR_LOCK);
|
|
524
|
|
525 return HAL_OK;
|
|
526 }
|
|
527
|
|
528 /**
|
|
529 * @brief Unlock the FLASH Option Bytes Registers access.
|
|
530 * @retval HAL Status
|
|
531 */
|
|
532 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
|
|
533 {
|
|
534 if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != RESET)
|
|
535 {
|
|
536 /* Authorizes the Option Byte register programming */
|
|
537 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
|
|
538 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
|
|
539 }
|
|
540 else
|
|
541 {
|
|
542 return HAL_ERROR;
|
|
543 }
|
|
544
|
|
545 return HAL_OK;
|
|
546 }
|
|
547
|
|
548 /**
|
|
549 * @brief Lock the FLASH Option Bytes Registers access.
|
|
550 * @retval HAL Status
|
|
551 */
|
|
552 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
|
|
553 {
|
|
554 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
|
|
555 SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);
|
|
556
|
|
557 return HAL_OK;
|
|
558 }
|
|
559
|
|
560 /**
|
|
561 * @brief Launch the option byte loading.
|
|
562 * @retval HAL Status
|
|
563 */
|
|
564 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
|
|
565 {
|
|
566 /* Set the bit to force the option byte reloading */
|
|
567 SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
|
|
568
|
|
569 /* Wait for last operation to be completed */
|
|
570 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
|
|
571 }
|
|
572
|
|
573 /**
|
|
574 * @}
|
|
575 */
|
|
576
|
|
577 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
|
|
578 * @brief Peripheral Errors functions
|
|
579 *
|
|
580 @verbatim
|
|
581 ===============================================================================
|
|
582 ##### Peripheral Errors functions #####
|
|
583 ===============================================================================
|
|
584 [..]
|
|
585 This subsection permits to get in run-time Errors of the FLASH peripheral.
|
|
586
|
|
587 @endverbatim
|
|
588 * @{
|
|
589 */
|
|
590
|
|
591 /**
|
|
592 * @brief Get the specific FLASH error flag.
|
|
593 * @retval FLASH_ErrorCode: The returned value can be:
|
|
594 * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
|
|
595 * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
|
|
596 * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
|
|
597 * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
|
|
598 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
|
|
599 * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
|
|
600 * @arg HAL_FLASH_ERROR_NONE: No error set
|
|
601 * @arg HAL_FLASH_ERROR_OP: FLASH Operation error
|
|
602 * @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
|
|
603 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
|
|
604 * @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
|
|
605 * @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
|
|
606 * @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
|
|
607 * @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error
|
|
608 * @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error
|
|
609 * @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error
|
|
610 * @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error
|
|
611 * @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)
|
|
612 * @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected
|
|
613 */
|
|
614 uint32_t HAL_FLASH_GetError(void)
|
|
615 {
|
|
616 return pFlash.ErrorCode;
|
|
617 }
|
|
618
|
|
619 /**
|
|
620 * @}
|
|
621 */
|
|
622
|
|
623 /**
|
|
624 * @}
|
|
625 */
|
|
626
|
|
627 /* Private functions ---------------------------------------------------------*/
|
|
628
|
|
629 /** @addtogroup FLASH_Private_Functions
|
|
630 * @{
|
|
631 */
|
|
632
|
|
633 /**
|
|
634 * @brief Wait for a FLASH operation to complete.
|
|
635 * @param Timeout: maximum flash operation timeout
|
|
636 * @retval HAL_StatusTypeDef HAL Status
|
|
637 */
|
|
638 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
|
|
639 {
|
|
640 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
|
|
641 Even if the FLASH operation fails, the BUSY flag will be reset and an error
|
|
642 flag will be set */
|
|
643
|
|
644 uint32_t timeout = HAL_GetTick() + Timeout;
|
|
645
|
|
646 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
|
|
647 {
|
|
648 if(Timeout != HAL_MAX_DELAY)
|
|
649 {
|
|
650 if(HAL_GetTick() >= timeout)
|
|
651 {
|
|
652 return HAL_TIMEOUT;
|
|
653 }
|
|
654 }
|
|
655 }
|
|
656
|
|
657 if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) ||
|
|
658 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) ||
|
|
659 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR)) ||
|
|
660 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) ||
|
|
661 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
|
|
662 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
|
|
663 defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
|
|
664 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY)))
|
|
665 #else
|
|
666 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
|
|
667 #endif
|
|
668 {
|
|
669 /*Save the error code*/
|
|
670 FLASH_SetErrorCode();
|
|
671
|
|
672 return HAL_ERROR;
|
|
673 }
|
|
674
|
|
675 /* Check FLASH End of Operation flag */
|
|
676 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
|
|
677 {
|
|
678 /* Clear FLASH End of Operation pending bit */
|
|
679 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
|
|
680 }
|
|
681
|
|
682 /* If there is an error flag set */
|
|
683 return HAL_OK;
|
|
684 }
|
|
685
|
|
686 /**
|
|
687 * @brief Set the specific FLASH error flag.
|
|
688 * @retval None
|
|
689 */
|
|
690 static void FLASH_SetErrorCode(void)
|
|
691 {
|
|
692 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))
|
|
693 {
|
|
694 pFlash.ErrorCode |= HAL_FLASH_ERROR_OP;
|
|
695 }
|
|
696
|
|
697 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR))
|
|
698 {
|
|
699 pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
|
|
700 }
|
|
701
|
|
702 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
|
|
703 {
|
|
704 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
|
|
705 }
|
|
706
|
|
707 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
|
|
708 {
|
|
709 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
|
|
710 }
|
|
711
|
|
712 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
|
|
713 {
|
|
714 pFlash.ErrorCode |= HAL_FLASH_ERROR_SIZ;
|
|
715 }
|
|
716
|
|
717 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))
|
|
718 {
|
|
719 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
|
|
720 }
|
|
721
|
|
722 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR))
|
|
723 {
|
|
724 pFlash.ErrorCode |= HAL_FLASH_ERROR_MIS;
|
|
725 }
|
|
726
|
|
727 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR))
|
|
728 {
|
|
729 pFlash.ErrorCode |= HAL_FLASH_ERROR_FAST;
|
|
730 }
|
|
731
|
|
732 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
|
|
733 {
|
|
734 pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
|
|
735 }
|
|
736
|
|
737 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
|
|
738 {
|
|
739 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
|
|
740 }
|
|
741
|
|
742 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD))
|
|
743 {
|
|
744 pFlash.ErrorCode |= HAL_FLASH_ERROR_ECCD;
|
|
745 }
|
|
746
|
|
747 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
|
|
748 defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
|
|
749 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PEMPTY))
|
|
750 {
|
|
751 pFlash.ErrorCode |= HAL_FLASH_ERROR_PEMPTY;
|
|
752 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PEMPTY);
|
|
753 }
|
|
754 #endif
|
|
755
|
|
756 /* Clear error programming flags */
|
|
757 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
|
|
758 }
|
|
759
|
|
760 /**
|
|
761 * @brief Program double-word (64-bit) at a specified address.
|
|
762 * @param Address: specifies the address to be programmed.
|
|
763 * @param Data: specifies the data to be programmed.
|
|
764 * @retval None
|
|
765 */
|
|
766 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
|
|
767 {
|
|
768 /* Check the parameters */
|
|
769 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
|
|
770
|
|
771 /* Set PG bit */
|
|
772 SET_BIT(FLASH->CR, FLASH_CR_PG);
|
|
773
|
|
774 /* Program the double word */
|
|
775 *(__IO uint32_t*)Address = (uint32_t)Data;
|
|
776 *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
|
|
777 }
|
|
778
|
|
779 /**
|
|
780 * @brief Fast program a row double-word (64-bit) at a specified address.
|
|
781 * @param Address: specifies the address to be programmed.
|
|
782 * @param DataAddress: specifies the address where the data are stored.
|
|
783 * @retval None
|
|
784 */
|
|
785 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
|
|
786 {
|
|
787 uint8_t row_index = (2*FLASH_NB_DOUBLE_WORDS_IN_ROW);
|
|
788 __IO uint32_t *dest_addr = (__IO uint32_t*)Address;
|
|
789 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
|
|
790
|
|
791 /* Check the parameters */
|
|
792 assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
|
|
793
|
|
794 /* Set FSTPG bit */
|
|
795 SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
|
|
796
|
|
797 /* Disable interrupts to avoid any interruption during the loop */
|
|
798 __disable_irq();
|
|
799
|
|
800 /* Program the double word of the row */
|
|
801 do
|
|
802 {
|
|
803 *dest_addr++ = *src_addr++;
|
|
804 } while (--row_index != 0);
|
|
805
|
|
806 /* Re-enable the interrupts */
|
|
807 __enable_irq();
|
|
808 }
|
|
809
|
|
810 /**
|
|
811 * @}
|
|
812 */
|
|
813
|
|
814 #endif /* HAL_FLASH_MODULE_ENABLED */
|
|
815
|
|
816 /**
|
|
817 * @}
|
|
818 */
|
|
819
|
|
820 /**
|
|
821 * @}
|
|
822 */
|
|
823
|
|
824 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|