Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32f0xx_hal_flash.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief FLASH HAL module driver. |
||
6 | * This file provides firmware functions to manage the following |
||
7 | * functionalities of the internal FLASH memory: |
||
8 | * + Program operations functions |
||
9 | * + Memory Control functions |
||
10 | * + Peripheral State functions |
||
11 | * |
||
12 | @verbatim |
||
13 | ============================================================================== |
||
14 | ##### FLASH peripheral features ##### |
||
15 | ============================================================================== |
||
16 | [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses |
||
17 | to the Flash memory. It implements the erase and program Flash memory operations |
||
18 | and the read and write protection mechanisms. |
||
19 | |||
20 | [..] The Flash memory interface accelerates code execution with a system of instruction |
||
21 | prefetch. |
||
22 | |||
23 | [..] The FLASH main features are: |
||
24 | (+) Flash memory read operations |
||
25 | (+) Flash memory program/erase operations |
||
26 | (+) Read / write protections |
||
27 | (+) Prefetch on I-Code |
||
28 | (+) Option Bytes programming |
||
29 | |||
30 | |||
31 | ##### How to use this driver ##### |
||
32 | ============================================================================== |
||
33 | [..] |
||
34 | This driver provides functions and macros to configure and program the FLASH |
||
35 | memory of all STM32F0xx devices. |
||
36 | |||
37 | (#) FLASH Memory I/O Programming functions: this group includes all needed |
||
38 | functions to erase and program the main memory: |
||
39 | (++) Lock and Unlock the FLASH interface |
||
40 | (++) Erase function: Erase page, erase all pages |
||
41 | (++) Program functions: half word, word and doubleword |
||
42 | (#) FLASH Option Bytes Programming functions: this group includes all needed |
||
43 | functions to manage the Option Bytes: |
||
44 | (++) Lock and Unlock the Option Bytes |
||
45 | (++) Set/Reset the write protection |
||
46 | (++) Set the Read protection Level |
||
47 | (++) Program the user Option Bytes |
||
48 | (++) Launch the Option Bytes loader |
||
49 | (++) Erase Option Bytes |
||
50 | (++) Program the data Option Bytes |
||
51 | (++) Get the Write protection. |
||
52 | (++) Get the user option bytes. |
||
53 | |||
54 | (#) Interrupts and flags management functions : this group |
||
55 | includes all needed functions to: |
||
56 | (++) Handle FLASH interrupts |
||
57 | (++) Wait for last FLASH operation according to its status |
||
58 | (++) Get error flag status |
||
59 | |||
60 | [..] In addition to these function, this driver includes a set of macros allowing |
||
61 | to handle the following operations: |
||
62 | |||
63 | (+) Set/Get the latency |
||
64 | (+) Enable/Disable the prefetch buffer |
||
65 | (+) Enable/Disable the FLASH interrupts |
||
66 | (+) Monitor the FLASH flags status |
||
67 | |||
68 | @endverbatim |
||
69 | ****************************************************************************** |
||
70 | * @attention |
||
71 | * |
||
72 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
73 | * All rights reserved.</center></h2> |
||
74 | * |
||
75 | * This software component is licensed by ST under BSD 3-Clause license, |
||
76 | * the "License"; You may not use this file except in compliance with the |
||
77 | * License. You may obtain a copy of the License at: |
||
78 | * opensource.org/licenses/BSD-3-Clause |
||
79 | * |
||
80 | ****************************************************************************** |
||
81 | */ |
||
82 | |||
83 | /* Includes ------------------------------------------------------------------*/ |
||
84 | #include "stm32f0xx_hal.h" |
||
85 | |||
86 | /** @addtogroup STM32F0xx_HAL_Driver |
||
87 | * @{ |
||
88 | */ |
||
89 | |||
90 | #ifdef HAL_FLASH_MODULE_ENABLED |
||
91 | |||
92 | /** @defgroup FLASH FLASH |
||
93 | * @brief FLASH HAL module driver |
||
94 | * @{ |
||
95 | */ |
||
96 | |||
97 | /* Private typedef -----------------------------------------------------------*/ |
||
98 | /* Private define ------------------------------------------------------------*/ |
||
99 | /** @defgroup FLASH_Private_Constants FLASH Private Constants |
||
100 | * @{ |
||
101 | */ |
||
102 | /** |
||
103 | * @} |
||
104 | */ |
||
105 | |||
106 | /* Private macro ---------------------------- ---------------------------------*/ |
||
107 | /** @defgroup FLASH_Private_Macros FLASH Private Macros |
||
108 | * @{ |
||
109 | */ |
||
110 | |||
111 | /** |
||
112 | * @} |
||
113 | */ |
||
114 | |||
115 | /* Private variables ---------------------------------------------------------*/ |
||
116 | /** @defgroup FLASH_Private_Variables FLASH Private Variables |
||
117 | * @{ |
||
118 | */ |
||
119 | /* Variables used for Erase pages under interruption*/ |
||
120 | FLASH_ProcessTypeDef pFlash; |
||
121 | /** |
||
122 | * @} |
||
123 | */ |
||
124 | |||
125 | /* Private function prototypes -----------------------------------------------*/ |
||
126 | /** @defgroup FLASH_Private_Functions FLASH Private Functions |
||
127 | * @{ |
||
128 | */ |
||
129 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data); |
||
130 | static void FLASH_SetErrorCode(void); |
||
131 | extern void FLASH_PageErase(uint32_t PageAddress); |
||
132 | /** |
||
133 | * @} |
||
134 | */ |
||
135 | |||
136 | /* Exported functions ---------------------------------------------------------*/ |
||
137 | /** @defgroup FLASH_Exported_Functions FLASH Exported Functions |
||
138 | * @{ |
||
139 | */ |
||
140 | |||
141 | /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions |
||
142 | * @brief Programming operation functions |
||
143 | * |
||
144 | @verbatim |
||
145 | @endverbatim |
||
146 | * @{ |
||
147 | */ |
||
148 | |||
149 | /** |
||
150 | * @brief Program halfword, word or double word at a specified address |
||
151 | * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
||
152 | * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface |
||
153 | * |
||
154 | * @note If an erase and a program operations are requested simultaneously, |
||
155 | * the erase operation is performed before the program one. |
||
156 | * |
||
157 | * @note FLASH should be previously erased before new programming (only exception to this |
||
158 | * is when 0x0000 is programmed) |
||
159 | * |
||
160 | * @param TypeProgram Indicate the way to program at a specified address. |
||
161 | * This parameter can be a value of @ref FLASH_Type_Program |
||
162 | * @param Address Specifie the address to be programmed. |
||
163 | * @param Data Specifie the data to be programmed |
||
164 | * |
||
165 | * @retval HAL_StatusTypeDef HAL Status |
||
166 | */ |
||
167 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
||
168 | { |
||
169 | HAL_StatusTypeDef status = HAL_ERROR; |
||
170 | uint8_t index = 0U; |
||
171 | uint8_t nbiterations = 0U; |
||
172 | |||
173 | /* Process Locked */ |
||
174 | __HAL_LOCK(&pFlash); |
||
175 | |||
176 | /* Check the parameters */ |
||
177 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
||
178 | assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); |
||
179 | |||
180 | /* Wait for last operation to be completed */ |
||
181 | status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); |
||
182 | |||
183 | if(status == HAL_OK) |
||
184 | { |
||
185 | if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
||
186 | { |
||
187 | /* Program halfword (16-bit) at a specified address. */ |
||
188 | nbiterations = 1U; |
||
189 | } |
||
190 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
||
191 | { |
||
192 | /* Program word (32-bit = 2*16-bit) at a specified address. */ |
||
193 | nbiterations = 2U; |
||
194 | } |
||
195 | else |
||
196 | { |
||
197 | /* Program double word (64-bit = 4*16-bit) at a specified address. */ |
||
198 | nbiterations = 4U; |
||
199 | } |
||
200 | |||
201 | for (index = 0U; index < nbiterations; index++) |
||
202 | { |
||
203 | FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index))); |
||
204 | |||
205 | /* Wait for last operation to be completed */ |
||
206 | status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); |
||
207 | |||
208 | /* If the program operation is completed, disable the PG Bit */ |
||
209 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
||
210 | /* In case of error, stop programming procedure */ |
||
211 | if (status != HAL_OK) |
||
212 | { |
||
213 | break; |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /* Process Unlocked */ |
||
219 | __HAL_UNLOCK(&pFlash); |
||
220 | |||
221 | return status; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @brief Program halfword, word or double word at a specified address with interrupt enabled. |
||
226 | * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface |
||
227 | * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface |
||
228 | * |
||
229 | * @note If an erase and a program operations are requested simultaneously, |
||
230 | * the erase operation is performed before the program one. |
||
231 | * |
||
232 | * @param TypeProgram Indicate the way to program at a specified address. |
||
233 | * This parameter can be a value of @ref FLASH_Type_Program |
||
234 | * @param Address Specifie the address to be programmed. |
||
235 | * @param Data Specifie the data to be programmed |
||
236 | * |
||
237 | * @retval HAL_StatusTypeDef HAL Status |
||
238 | */ |
||
239 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
||
240 | { |
||
241 | HAL_StatusTypeDef status = HAL_OK; |
||
242 | |||
243 | /* Process Locked */ |
||
244 | __HAL_LOCK(&pFlash); |
||
245 | |||
246 | /* Check the parameters */ |
||
247 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
||
248 | assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); |
||
249 | |||
250 | /* Enable End of FLASH Operation and Error source interrupts */ |
||
251 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
||
252 | |||
253 | pFlash.Address = Address; |
||
254 | pFlash.Data = Data; |
||
255 | |||
256 | if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
||
257 | { |
||
258 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD; |
||
259 | /* Program halfword (16-bit) at a specified address. */ |
||
260 | pFlash.DataRemaining = 1U; |
||
261 | } |
||
262 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
||
263 | { |
||
264 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD; |
||
265 | /* Program word (32-bit : 2*16-bit) at a specified address. */ |
||
266 | pFlash.DataRemaining = 2U; |
||
267 | } |
||
268 | else |
||
269 | { |
||
270 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD; |
||
271 | /* Program double word (64-bit : 4*16-bit) at a specified address. */ |
||
272 | pFlash.DataRemaining = 4U; |
||
273 | } |
||
274 | |||
275 | /* Program halfword (16-bit) at a specified address. */ |
||
276 | FLASH_Program_HalfWord(Address, (uint16_t)Data); |
||
277 | |||
278 | return status; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @brief This function handles FLASH interrupt request. |
||
283 | * @retval None |
||
284 | */ |
||
285 | void HAL_FLASH_IRQHandler(void) |
||
286 | { |
||
287 | uint32_t addresstmp = 0U; |
||
288 | |||
289 | /* Check FLASH operation error flags */ |
||
290 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
||
291 | { |
||
292 | /* Return the faulty address */ |
||
293 | addresstmp = pFlash.Address; |
||
294 | /* Reset address */ |
||
295 | pFlash.Address = 0xFFFFFFFFU; |
||
296 | |||
297 | /* Save the Error code */ |
||
298 | FLASH_SetErrorCode(); |
||
299 | |||
300 | /* FLASH error interrupt user callback */ |
||
301 | HAL_FLASH_OperationErrorCallback(addresstmp); |
||
302 | |||
303 | /* Stop the procedure ongoing */ |
||
304 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
||
305 | } |
||
306 | |||
307 | /* Check FLASH End of Operation flag */ |
||
308 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) |
||
309 | { |
||
310 | /* Clear FLASH End of Operation pending bit */ |
||
311 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
||
312 | |||
313 | /* Process can continue only if no error detected */ |
||
314 | if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE) |
||
315 | { |
||
316 | if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) |
||
317 | { |
||
318 | /* Nb of pages to erased can be decreased */ |
||
319 | pFlash.DataRemaining--; |
||
320 | |||
321 | /* Check if there are still pages to erase */ |
||
322 | if(pFlash.DataRemaining != 0U) |
||
323 | { |
||
324 | addresstmp = pFlash.Address; |
||
325 | /*Indicate user which sector has been erased */ |
||
326 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
||
327 | |||
328 | /*Increment sector number*/ |
||
329 | addresstmp = pFlash.Address + FLASH_PAGE_SIZE; |
||
330 | pFlash.Address = addresstmp; |
||
331 | |||
332 | /* If the erase operation is completed, disable the PER Bit */ |
||
333 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); |
||
334 | |||
335 | FLASH_PageErase(addresstmp); |
||
336 | } |
||
337 | else |
||
338 | { |
||
339 | /* No more pages to Erase, user callback can be called. */ |
||
340 | /* Reset Sector and stop Erase pages procedure */ |
||
341 | pFlash.Address = addresstmp = 0xFFFFFFFFU; |
||
342 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
||
343 | /* FLASH EOP interrupt user callback */ |
||
344 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
||
345 | } |
||
346 | } |
||
347 | else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
||
348 | { |
||
349 | /* Operation is completed, disable the MER Bit */ |
||
350 | CLEAR_BIT(FLASH->CR, FLASH_CR_MER); |
||
351 | |||
352 | /* MassErase ended. Return the selected bank */ |
||
353 | /* FLASH EOP interrupt user callback */ |
||
354 | HAL_FLASH_EndOfOperationCallback(0); |
||
355 | |||
356 | /* Stop Mass Erase procedure*/ |
||
357 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
||
358 | } |
||
359 | else |
||
360 | { |
||
361 | /* Nb of 16-bit data to program can be decreased */ |
||
362 | pFlash.DataRemaining--; |
||
363 | |||
364 | /* Check if there are still 16-bit data to program */ |
||
365 | if(pFlash.DataRemaining != 0U) |
||
366 | { |
||
367 | /* Increment address to 16-bit */ |
||
368 | pFlash.Address += 2; |
||
369 | addresstmp = pFlash.Address; |
||
370 | |||
371 | /* Shift to have next 16-bit data */ |
||
372 | pFlash.Data = (pFlash.Data >> 16U); |
||
373 | |||
374 | /* Operation is completed, disable the PG Bit */ |
||
375 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
||
376 | |||
377 | /*Program halfword (16-bit) at a specified address.*/ |
||
378 | FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data); |
||
379 | } |
||
380 | else |
||
381 | { |
||
382 | /* Program ended. Return the selected address */ |
||
383 | /* FLASH EOP interrupt user callback */ |
||
384 | if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) |
||
385 | { |
||
386 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
||
387 | } |
||
388 | else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD) |
||
389 | { |
||
390 | HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U); |
||
391 | } |
||
392 | else |
||
393 | { |
||
394 | HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U); |
||
395 | } |
||
396 | |||
397 | /* Reset Address and stop Program procedure */ |
||
398 | pFlash.Address = 0xFFFFFFFFU; |
||
399 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | |||
405 | |||
406 | if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE) |
||
407 | { |
||
408 | /* Operation is completed, disable the PG, PER and MER Bits */ |
||
409 | CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); |
||
410 | |||
411 | /* Disable End of FLASH Operation and Error source interrupts */ |
||
412 | __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); |
||
413 | |||
414 | /* Process Unlocked */ |
||
415 | __HAL_UNLOCK(&pFlash); |
||
416 | } |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * @brief FLASH end of operation interrupt callback |
||
421 | * @param ReturnValue The value saved in this parameter depends on the ongoing procedure |
||
422 | * - Mass Erase: No return value expected |
||
423 | * - Pages Erase: Address of the page which has been erased |
||
424 | * (if 0xFFFFFFFF, it means that all the selected pages have been erased) |
||
425 | * - Program: Address which was selected for data program |
||
426 | * @retval none |
||
427 | */ |
||
428 | __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) |
||
429 | { |
||
430 | /* Prevent unused argument(s) compilation warning */ |
||
431 | UNUSED(ReturnValue); |
||
432 | |||
433 | /* NOTE : This function Should not be modified, when the callback is needed, |
||
434 | the HAL_FLASH_EndOfOperationCallback could be implemented in the user file |
||
435 | */ |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @brief FLASH operation error interrupt callback |
||
440 | * @param ReturnValue The value saved in this parameter depends on the ongoing procedure |
||
441 | * - Mass Erase: No return value expected |
||
442 | * - Pages Erase: Address of the page which returned an error |
||
443 | * - Program: Address which was selected for data program |
||
444 | * @retval none |
||
445 | */ |
||
446 | __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) |
||
447 | { |
||
448 | /* Prevent unused argument(s) compilation warning */ |
||
449 | UNUSED(ReturnValue); |
||
450 | |||
451 | /* NOTE : This function Should not be modified, when the callback is needed, |
||
452 | the HAL_FLASH_OperationErrorCallback could be implemented in the user file |
||
453 | */ |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * @} |
||
458 | */ |
||
459 | |||
460 | /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions |
||
461 | * @brief management functions |
||
462 | * |
||
463 | @verbatim |
||
464 | =============================================================================== |
||
465 | ##### Peripheral Control functions ##### |
||
466 | =============================================================================== |
||
467 | [..] |
||
468 | This subsection provides a set of functions allowing to control the FLASH |
||
469 | memory operations. |
||
470 | |||
471 | @endverbatim |
||
472 | * @{ |
||
473 | */ |
||
474 | |||
475 | /** |
||
476 | * @brief Unlock the FLASH control register access |
||
477 | * @retval HAL Status |
||
478 | */ |
||
479 | HAL_StatusTypeDef HAL_FLASH_Unlock(void) |
||
480 | { |
||
481 | HAL_StatusTypeDef status = HAL_OK; |
||
482 | |||
483 | if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) |
||
484 | { |
||
485 | /* Authorize the FLASH Registers access */ |
||
486 | WRITE_REG(FLASH->KEYR, FLASH_KEY1); |
||
487 | WRITE_REG(FLASH->KEYR, FLASH_KEY2); |
||
488 | |||
489 | /* Verify Flash is unlocked */ |
||
490 | if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) |
||
491 | { |
||
492 | status = HAL_ERROR; |
||
493 | } |
||
494 | } |
||
495 | |||
496 | return status; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * @brief Locks the FLASH control register access |
||
501 | * @retval HAL Status |
||
502 | */ |
||
503 | HAL_StatusTypeDef HAL_FLASH_Lock(void) |
||
504 | { |
||
505 | /* Set the LOCK Bit to lock the FLASH Registers access */ |
||
506 | SET_BIT(FLASH->CR, FLASH_CR_LOCK); |
||
507 | |||
508 | return HAL_OK; |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @brief Unlock the FLASH Option Control Registers access. |
||
513 | * @retval HAL Status |
||
514 | */ |
||
515 | HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void) |
||
516 | { |
||
517 | if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE)) |
||
518 | { |
||
519 | /* Authorizes the Option Byte register programming */ |
||
520 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); |
||
521 | WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2); |
||
522 | } |
||
523 | else |
||
524 | { |
||
525 | return HAL_ERROR; |
||
526 | } |
||
527 | |||
528 | return HAL_OK; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * @brief Lock the FLASH Option Control Registers access. |
||
533 | * @retval HAL Status |
||
534 | */ |
||
535 | HAL_StatusTypeDef HAL_FLASH_OB_Lock(void) |
||
536 | { |
||
537 | /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */ |
||
538 | CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE); |
||
539 | |||
540 | return HAL_OK; |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * @brief Launch the option byte loading. |
||
545 | * @note This function will reset automatically the MCU. |
||
546 | * @retval HAL Status |
||
547 | */ |
||
548 | HAL_StatusTypeDef HAL_FLASH_OB_Launch(void) |
||
549 | { |
||
550 | /* Set the OBL_Launch bit to launch the option byte loading */ |
||
551 | SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH); |
||
552 | |||
553 | /* Wait for last operation to be completed */ |
||
554 | return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE)); |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * @} |
||
559 | */ |
||
560 | |||
561 | /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions |
||
562 | * @brief Peripheral errors functions |
||
563 | * |
||
564 | @verbatim |
||
565 | =============================================================================== |
||
566 | ##### Peripheral Errors functions ##### |
||
567 | =============================================================================== |
||
568 | [..] |
||
569 | This subsection permit to get in run-time errors of the FLASH peripheral. |
||
570 | |||
571 | @endverbatim |
||
572 | * @{ |
||
573 | */ |
||
574 | |||
575 | /** |
||
576 | * @brief Get the specific FLASH error flag. |
||
577 | * @retval FLASH_ErrorCode The returned value can be: |
||
578 | * @ref FLASH_Error_Codes |
||
579 | */ |
||
580 | uint32_t HAL_FLASH_GetError(void) |
||
581 | { |
||
582 | return pFlash.ErrorCode; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * @} |
||
587 | */ |
||
588 | |||
589 | /** |
||
590 | * @} |
||
591 | */ |
||
592 | |||
593 | /** @addtogroup FLASH_Private_Functions |
||
594 | * @{ |
||
595 | */ |
||
596 | |||
597 | /** |
||
598 | * @brief Program a half-word (16-bit) at a specified address. |
||
599 | * @param Address specify the address to be programmed. |
||
600 | * @param Data specify the data to be programmed. |
||
601 | * @retval None |
||
602 | */ |
||
603 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data) |
||
604 | { |
||
605 | /* Clean the error context */ |
||
606 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
||
607 | |||
608 | /* Proceed to program the new data */ |
||
609 | SET_BIT(FLASH->CR, FLASH_CR_PG); |
||
610 | |||
611 | /* Write data in the address */ |
||
612 | *(__IO uint16_t*)Address = Data; |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * @brief Wait for a FLASH operation to complete. |
||
617 | * @param Timeout maximum flash operation timeout |
||
618 | * @retval HAL Status |
||
619 | */ |
||
620 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout) |
||
621 | { |
||
622 | /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. |
||
623 | Even if the FLASH operation fails, the BUSY flag will be reset and an error |
||
624 | flag will be set */ |
||
625 | |||
626 | uint32_t tickstart = HAL_GetTick(); |
||
627 | |||
628 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) |
||
629 | { |
||
630 | if (Timeout != HAL_MAX_DELAY) |
||
631 | { |
||
632 | if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) |
||
633 | { |
||
634 | return HAL_TIMEOUT; |
||
635 | } |
||
636 | } |
||
637 | } |
||
638 | |||
639 | /* Check FLASH End of Operation flag */ |
||
640 | if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) |
||
641 | { |
||
642 | /* Clear FLASH End of Operation pending bit */ |
||
643 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
||
644 | } |
||
645 | |||
646 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || |
||
647 | __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
||
648 | { |
||
649 | /*Save the error code*/ |
||
650 | FLASH_SetErrorCode(); |
||
651 | return HAL_ERROR; |
||
652 | } |
||
653 | |||
654 | /* There is no error flag set */ |
||
655 | return HAL_OK; |
||
656 | } |
||
657 | |||
658 | |||
659 | /** |
||
660 | * @brief Set the specific FLASH error flag. |
||
661 | * @retval None |
||
662 | */ |
||
663 | static void FLASH_SetErrorCode(void) |
||
664 | { |
||
665 | uint32_t flags = 0U; |
||
666 | |||
667 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) |
||
668 | { |
||
669 | pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; |
||
670 | flags |= FLASH_FLAG_WRPERR; |
||
671 | } |
||
672 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) |
||
673 | { |
||
674 | pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG; |
||
675 | flags |= FLASH_FLAG_PGERR; |
||
676 | } |
||
677 | /* Clear FLASH error pending bits */ |
||
678 | __HAL_FLASH_CLEAR_FLAG(flags); |
||
679 | } |
||
680 | /** |
||
681 | * @} |
||
682 | */ |
||
683 | |||
684 | /** |
||
685 | * @} |
||
686 | */ |
||
687 | |||
688 | #endif /* HAL_FLASH_MODULE_ENABLED */ |
||
689 | |||
690 | /** |
||
691 | * @} |
||
692 | */ |
||
693 | |||
694 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |