提交 | 用户 | age
|
bfc108
|
1 |
/**
|
Q |
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</center></h2>
|
|
73 |
*
|
|
74 |
* Redistribution and use in source and binary forms, with or without modification,
|
|
75 |
* are permitted provided that the following conditions are met:
|
|
76 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
77 |
* this list of conditions and the following disclaimer.
|
|
78 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
79 |
* this list of conditions and the following disclaimer in the documentation
|
|
80 |
* and/or other materials provided with the distribution.
|
|
81 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
82 |
* may be used to endorse or promote products derived from this software
|
|
83 |
* without specific prior written permission.
|
|
84 |
*
|
|
85 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
86 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
87 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
88 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
89 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
90 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
91 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
92 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
93 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
94 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
95 |
*
|
|
96 |
******************************************************************************
|
|
97 |
*/
|
|
98 |
|
|
99 |
/* Includes ------------------------------------------------------------------*/
|
|
100 |
#include "stm32f0xx_hal.h"
|
|
101 |
|
|
102 |
/** @addtogroup STM32F0xx_HAL_Driver
|
|
103 |
* @{
|
|
104 |
*/
|
|
105 |
|
|
106 |
#ifdef HAL_FLASH_MODULE_ENABLED
|
|
107 |
|
|
108 |
/** @defgroup FLASH FLASH
|
|
109 |
* @brief FLASH HAL module driver
|
|
110 |
* @{
|
|
111 |
*/
|
|
112 |
|
|
113 |
/* Private typedef -----------------------------------------------------------*/
|
|
114 |
/* Private define ------------------------------------------------------------*/
|
|
115 |
/** @defgroup FLASH_Private_Constants FLASH Private Constants
|
|
116 |
* @{
|
|
117 |
*/
|
|
118 |
/**
|
|
119 |
* @}
|
|
120 |
*/
|
|
121 |
|
|
122 |
/* Private macro ---------------------------- ---------------------------------*/
|
|
123 |
/** @defgroup FLASH_Private_Macros FLASH Private Macros
|
|
124 |
* @{
|
|
125 |
*/
|
|
126 |
|
|
127 |
/**
|
|
128 |
* @}
|
|
129 |
*/
|
|
130 |
|
|
131 |
/* Private variables ---------------------------------------------------------*/
|
|
132 |
/** @defgroup FLASH_Private_Variables FLASH Private Variables
|
|
133 |
* @{
|
|
134 |
*/
|
|
135 |
/* Variables used for Erase pages under interruption*/
|
|
136 |
FLASH_ProcessTypeDef pFlash;
|
|
137 |
/**
|
|
138 |
* @}
|
|
139 |
*/
|
|
140 |
|
|
141 |
/* Private function prototypes -----------------------------------------------*/
|
|
142 |
/** @defgroup FLASH_Private_Functions FLASH Private Functions
|
|
143 |
* @{
|
|
144 |
*/
|
|
145 |
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
|
|
146 |
static void FLASH_SetErrorCode(void);
|
|
147 |
extern void FLASH_PageErase(uint32_t PageAddress);
|
|
148 |
/**
|
|
149 |
* @}
|
|
150 |
*/
|
|
151 |
|
|
152 |
/* Exported functions ---------------------------------------------------------*/
|
|
153 |
/** @defgroup FLASH_Exported_Functions FLASH Exported Functions
|
|
154 |
* @{
|
|
155 |
*/
|
|
156 |
|
|
157 |
/** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
|
|
158 |
* @brief Programming operation functions
|
|
159 |
*
|
|
160 |
@verbatim
|
|
161 |
@endverbatim
|
|
162 |
* @{
|
|
163 |
*/
|
|
164 |
|
|
165 |
/**
|
|
166 |
* @brief Program halfword, word or double word at a specified address
|
|
167 |
* @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
|
|
168 |
* The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
|
|
169 |
*
|
|
170 |
* @note If an erase and a program operations are requested simultaneously,
|
|
171 |
* the erase operation is performed before the program one.
|
|
172 |
*
|
|
173 |
* @note FLASH should be previously erased before new programmation (only exception to this
|
|
174 |
* is when 0x0000 is programmed)
|
|
175 |
*
|
|
176 |
* @param TypeProgram Indicate the way to program at a specified address.
|
|
177 |
* This parameter can be a value of @ref FLASH_Type_Program
|
|
178 |
* @param Address Specifie the address to be programmed.
|
|
179 |
* @param Data Specifie the data to be programmed
|
|
180 |
*
|
|
181 |
* @retval HAL_StatusTypeDef HAL Status
|
|
182 |
*/
|
|
183 |
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
|
|
184 |
{
|
|
185 |
HAL_StatusTypeDef status = HAL_ERROR;
|
|
186 |
uint8_t index = 0U;
|
|
187 |
uint8_t nbiterations = 0U;
|
|
188 |
|
|
189 |
/* Process Locked */
|
|
190 |
__HAL_LOCK(&pFlash);
|
|
191 |
|
|
192 |
/* Check the parameters */
|
|
193 |
assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
|
|
194 |
assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
|
|
195 |
|
|
196 |
/* Wait for last operation to be completed */
|
|
197 |
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
|
|
198 |
|
|
199 |
if(status == HAL_OK)
|
|
200 |
{
|
|
201 |
if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
|
|
202 |
{
|
|
203 |
/* Program halfword (16-bit) at a specified address. */
|
|
204 |
nbiterations = 1U;
|
|
205 |
}
|
|
206 |
else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
|
|
207 |
{
|
|
208 |
/* Program word (32-bit = 2*16-bit) at a specified address. */
|
|
209 |
nbiterations = 2U;
|
|
210 |
}
|
|
211 |
else
|
|
212 |
{
|
|
213 |
/* Program double word (64-bit = 4*16-bit) at a specified address. */
|
|
214 |
nbiterations = 4U;
|
|
215 |
}
|
|
216 |
|
|
217 |
for (index = 0U; index < nbiterations; index++)
|
|
218 |
{
|
|
219 |
FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index)));
|
|
220 |
|
|
221 |
/* Wait for last operation to be completed */
|
|
222 |
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
|
|
223 |
|
|
224 |
/* If the program operation is completed, disable the PG Bit */
|
|
225 |
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
|
|
226 |
/* In case of error, stop programation procedure */
|
|
227 |
if (status != HAL_OK)
|
|
228 |
{
|
|
229 |
break;
|
|
230 |
}
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
/* Process Unlocked */
|
|
235 |
__HAL_UNLOCK(&pFlash);
|
|
236 |
|
|
237 |
return status;
|
|
238 |
}
|
|
239 |
|
|
240 |
/**
|
|
241 |
* @brief Program halfword, word or double word at a specified address with interrupt enabled.
|
|
242 |
* @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
|
|
243 |
* The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
|
|
244 |
*
|
|
245 |
* @note If an erase and a program operations are requested simultaneously,
|
|
246 |
* the erase operation is performed before the program one.
|
|
247 |
*
|
|
248 |
* @param TypeProgram Indicate the way to program at a specified address.
|
|
249 |
* This parameter can be a value of @ref FLASH_Type_Program
|
|
250 |
* @param Address Specifie the address to be programmed.
|
|
251 |
* @param Data Specifie the data to be programmed
|
|
252 |
*
|
|
253 |
* @retval HAL_StatusTypeDef HAL Status
|
|
254 |
*/
|
|
255 |
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
|
|
256 |
{
|
|
257 |
HAL_StatusTypeDef status = HAL_OK;
|
|
258 |
|
|
259 |
/* Process Locked */
|
|
260 |
__HAL_LOCK(&pFlash);
|
|
261 |
|
|
262 |
/* Check the parameters */
|
|
263 |
assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
|
|
264 |
assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
|
|
265 |
|
|
266 |
/* Enable End of FLASH Operation and Error source interrupts */
|
|
267 |
__HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
|
|
268 |
|
|
269 |
pFlash.Address = Address;
|
|
270 |
pFlash.Data = Data;
|
|
271 |
|
|
272 |
if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
|
|
273 |
{
|
|
274 |
pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
|
|
275 |
/* Program halfword (16-bit) at a specified address. */
|
|
276 |
pFlash.DataRemaining = 1U;
|
|
277 |
}
|
|
278 |
else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
|
|
279 |
{
|
|
280 |
pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
|
|
281 |
/* Program word (32-bit : 2*16-bit) at a specified address. */
|
|
282 |
pFlash.DataRemaining = 2U;
|
|
283 |
}
|
|
284 |
else
|
|
285 |
{
|
|
286 |
pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
|
|
287 |
/* Program double word (64-bit : 4*16-bit) at a specified address. */
|
|
288 |
pFlash.DataRemaining = 4U;
|
|
289 |
}
|
|
290 |
|
|
291 |
/* Program halfword (16-bit) at a specified address. */
|
|
292 |
FLASH_Program_HalfWord(Address, (uint16_t)Data);
|
|
293 |
|
|
294 |
return status;
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* @brief This function handles FLASH interrupt request.
|
|
299 |
* @retval None
|
|
300 |
*/
|
|
301 |
void HAL_FLASH_IRQHandler(void)
|
|
302 |
{
|
|
303 |
uint32_t addresstmp = 0U;
|
|
304 |
|
|
305 |
/* Check FLASH operation error flags */
|
|
306 |
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
|
|
307 |
{
|
|
308 |
/* Return the faulty address */
|
|
309 |
addresstmp = pFlash.Address;
|
|
310 |
/* Reset address */
|
|
311 |
pFlash.Address = 0xFFFFFFFFU;
|
|
312 |
|
|
313 |
/* Save the Error code */
|
|
314 |
FLASH_SetErrorCode();
|
|
315 |
|
|
316 |
/* FLASH error interrupt user callback */
|
|
317 |
HAL_FLASH_OperationErrorCallback(addresstmp);
|
|
318 |
|
|
319 |
/* Stop the procedure ongoing */
|
|
320 |
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
321 |
}
|
|
322 |
|
|
323 |
/* Check FLASH End of Operation flag */
|
|
324 |
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
|
|
325 |
{
|
|
326 |
/* Clear FLASH End of Operation pending bit */
|
|
327 |
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
|
|
328 |
|
|
329 |
/* Process can continue only if no error detected */
|
|
330 |
if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
|
|
331 |
{
|
|
332 |
if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
|
|
333 |
{
|
|
334 |
/* Nb of pages to erased can be decreased */
|
|
335 |
pFlash.DataRemaining--;
|
|
336 |
|
|
337 |
/* Check if there are still pages to erase */
|
|
338 |
if(pFlash.DataRemaining != 0U)
|
|
339 |
{
|
|
340 |
addresstmp = pFlash.Address;
|
|
341 |
/*Indicate user which sector has been erased */
|
|
342 |
HAL_FLASH_EndOfOperationCallback(addresstmp);
|
|
343 |
|
|
344 |
/*Increment sector number*/
|
|
345 |
addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
|
|
346 |
pFlash.Address = addresstmp;
|
|
347 |
|
|
348 |
/* If the erase operation is completed, disable the PER Bit */
|
|
349 |
CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
|
|
350 |
|
|
351 |
FLASH_PageErase(addresstmp);
|
|
352 |
}
|
|
353 |
else
|
|
354 |
{
|
|
355 |
/* No more pages to Erase, user callback can be called. */
|
|
356 |
/* Reset Sector and stop Erase pages procedure */
|
|
357 |
pFlash.Address = addresstmp = 0xFFFFFFFFU;
|
|
358 |
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
359 |
/* FLASH EOP interrupt user callback */
|
|
360 |
HAL_FLASH_EndOfOperationCallback(addresstmp);
|
|
361 |
}
|
|
362 |
}
|
|
363 |
else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
|
|
364 |
{
|
|
365 |
/* Operation is completed, disable the MER Bit */
|
|
366 |
CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
|
|
367 |
|
|
368 |
/* MassErase ended. Return the selected bank */
|
|
369 |
/* FLASH EOP interrupt user callback */
|
|
370 |
HAL_FLASH_EndOfOperationCallback(0);
|
|
371 |
|
|
372 |
/* Stop Mass Erase procedure*/
|
|
373 |
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
374 |
}
|
|
375 |
else
|
|
376 |
{
|
|
377 |
/* Nb of 16-bit data to program can be decreased */
|
|
378 |
pFlash.DataRemaining--;
|
|
379 |
|
|
380 |
/* Check if there are still 16-bit data to program */
|
|
381 |
if(pFlash.DataRemaining != 0U)
|
|
382 |
{
|
|
383 |
/* Increment address to 16-bit */
|
|
384 |
pFlash.Address += 2;
|
|
385 |
addresstmp = pFlash.Address;
|
|
386 |
|
|
387 |
/* Shift to have next 16-bit data */
|
|
388 |
pFlash.Data = (pFlash.Data >> 16U);
|
|
389 |
|
|
390 |
/* Operation is completed, disable the PG Bit */
|
|
391 |
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
|
|
392 |
|
|
393 |
/*Program halfword (16-bit) at a specified address.*/
|
|
394 |
FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
|
|
395 |
}
|
|
396 |
else
|
|
397 |
{
|
|
398 |
/* Program ended. Return the selected address */
|
|
399 |
/* FLASH EOP interrupt user callback */
|
|
400 |
if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
|
|
401 |
{
|
|
402 |
HAL_FLASH_EndOfOperationCallback(pFlash.Address);
|
|
403 |
}
|
|
404 |
else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
|
|
405 |
{
|
|
406 |
HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U);
|
|
407 |
}
|
|
408 |
else
|
|
409 |
{
|
|
410 |
HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U);
|
|
411 |
}
|
|
412 |
|
|
413 |
/* Reset Address and stop Program procedure */
|
|
414 |
pFlash.Address = 0xFFFFFFFFU;
|
|
415 |
pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
|
|
416 |
}
|
|
417 |
}
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
|
|
422 |
if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
|
|
423 |
{
|
|
424 |
/* Operation is completed, disable the PG, PER and MER Bits */
|
|
425 |
CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
|
|
426 |
|
|
427 |
/* Disable End of FLASH Operation and Error source interrupts */
|
|
428 |
__HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
|
|
429 |
|
|
430 |
/* Process Unlocked */
|
|
431 |
__HAL_UNLOCK(&pFlash);
|
|
432 |
}
|
|
433 |
}
|
|
434 |
|
|
435 |
/**
|
|
436 |
* @brief FLASH end of operation interrupt callback
|
|
437 |
* @param ReturnValue The value saved in this parameter depends on the ongoing procedure
|
|
438 |
* - Mass Erase: No return value expected
|
|
439 |
* - Pages Erase: Address of the page which has been erased
|
|
440 |
* (if 0xFFFFFFFF, it means that all the selected pages have been erased)
|
|
441 |
* - Program: Address which was selected for data program
|
|
442 |
* @retval none
|
|
443 |
*/
|
|
444 |
__weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
|
|
445 |
{
|
|
446 |
/* Prevent unused argument(s) compilation warning */
|
|
447 |
UNUSED(ReturnValue);
|
|
448 |
|
|
449 |
/* NOTE : This function Should not be modified, when the callback is needed,
|
|
450 |
the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
|
|
451 |
*/
|
|
452 |
}
|
|
453 |
|
|
454 |
/**
|
|
455 |
* @brief FLASH operation error interrupt callback
|
|
456 |
* @param ReturnValue The value saved in this parameter depends on the ongoing procedure
|
|
457 |
* - Mass Erase: No return value expected
|
|
458 |
* - Pages Erase: Address of the page which returned an error
|
|
459 |
* - Program: Address which was selected for data program
|
|
460 |
* @retval none
|
|
461 |
*/
|
|
462 |
__weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
|
|
463 |
{
|
|
464 |
/* Prevent unused argument(s) compilation warning */
|
|
465 |
UNUSED(ReturnValue);
|
|
466 |
|
|
467 |
/* NOTE : This function Should not be modified, when the callback is needed,
|
|
468 |
the HAL_FLASH_OperationErrorCallback could be implemented in the user file
|
|
469 |
*/
|
|
470 |
}
|
|
471 |
|
|
472 |
/**
|
|
473 |
* @}
|
|
474 |
*/
|
|
475 |
|
|
476 |
/** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
|
|
477 |
* @brief management functions
|
|
478 |
*
|
|
479 |
@verbatim
|
|
480 |
===============================================================================
|
|
481 |
##### Peripheral Control functions #####
|
|
482 |
===============================================================================
|
|
483 |
[..]
|
|
484 |
This subsection provides a set of functions allowing to control the FLASH
|
|
485 |
memory operations.
|
|
486 |
|
|
487 |
@endverbatim
|
|
488 |
* @{
|
|
489 |
*/
|
|
490 |
|
|
491 |
/**
|
|
492 |
* @brief Unlock the FLASH control register access
|
|
493 |
* @retval HAL Status
|
|
494 |
*/
|
|
495 |
HAL_StatusTypeDef HAL_FLASH_Unlock(void)
|
|
496 |
{
|
|
497 |
if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
|
|
498 |
{
|
|
499 |
/* Authorize the FLASH Registers access */
|
|
500 |
WRITE_REG(FLASH->KEYR, FLASH_KEY1);
|
|
501 |
WRITE_REG(FLASH->KEYR, FLASH_KEY2);
|
|
502 |
}
|
|
503 |
else
|
|
504 |
{
|
|
505 |
return HAL_ERROR;
|
|
506 |
}
|
|
507 |
|
|
508 |
return HAL_OK;
|
|
509 |
}
|
|
510 |
|
|
511 |
/**
|
|
512 |
* @brief Locks the FLASH control register access
|
|
513 |
* @retval HAL Status
|
|
514 |
*/
|
|
515 |
HAL_StatusTypeDef HAL_FLASH_Lock(void)
|
|
516 |
{
|
|
517 |
/* Set the LOCK Bit to lock the FLASH Registers access */
|
|
518 |
SET_BIT(FLASH->CR, FLASH_CR_LOCK);
|
|
519 |
|
|
520 |
return HAL_OK;
|
|
521 |
}
|
|
522 |
|
|
523 |
/**
|
|
524 |
* @brief Unlock the FLASH Option Control Registers access.
|
|
525 |
* @retval HAL Status
|
|
526 |
*/
|
|
527 |
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
|
|
528 |
{
|
|
529 |
if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
|
|
530 |
{
|
|
531 |
/* Authorizes the Option Byte register programming */
|
|
532 |
WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
|
|
533 |
WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
|
|
534 |
}
|
|
535 |
else
|
|
536 |
{
|
|
537 |
return HAL_ERROR;
|
|
538 |
}
|
|
539 |
|
|
540 |
return HAL_OK;
|
|
541 |
}
|
|
542 |
|
|
543 |
/**
|
|
544 |
* @brief Lock the FLASH Option Control Registers access.
|
|
545 |
* @retval HAL Status
|
|
546 |
*/
|
|
547 |
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
|
|
548 |
{
|
|
549 |
/* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
|
|
550 |
CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
|
|
551 |
|
|
552 |
return HAL_OK;
|
|
553 |
}
|
|
554 |
|
|
555 |
/**
|
|
556 |
* @brief Launch the option byte loading.
|
|
557 |
* @note This function will reset automatically the MCU.
|
|
558 |
* @retval HAL Status
|
|
559 |
*/
|
|
560 |
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
|
|
561 |
{
|
|
562 |
/* Set the OBL_Launch bit to launch the option byte loading */
|
|
563 |
SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
|
|
564 |
|
|
565 |
/* Wait for last operation to be completed */
|
|
566 |
return(FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE));
|
|
567 |
}
|
|
568 |
|
|
569 |
/**
|
|
570 |
* @}
|
|
571 |
*/
|
|
572 |
|
|
573 |
/** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
|
|
574 |
* @brief Peripheral errors functions
|
|
575 |
*
|
|
576 |
@verbatim
|
|
577 |
===============================================================================
|
|
578 |
##### Peripheral Errors functions #####
|
|
579 |
===============================================================================
|
|
580 |
[..]
|
|
581 |
This subsection permit to get in run-time errors of the FLASH peripheral.
|
|
582 |
|
|
583 |
@endverbatim
|
|
584 |
* @{
|
|
585 |
*/
|
|
586 |
|
|
587 |
/**
|
|
588 |
* @brief Get the specific FLASH error flag.
|
|
589 |
* @retval FLASH_ErrorCode The returned value can be:
|
|
590 |
* @ref FLASH_Error_Codes
|
|
591 |
*/
|
|
592 |
uint32_t HAL_FLASH_GetError(void)
|
|
593 |
{
|
|
594 |
return pFlash.ErrorCode;
|
|
595 |
}
|
|
596 |
|
|
597 |
/**
|
|
598 |
* @}
|
|
599 |
*/
|
|
600 |
|
|
601 |
/**
|
|
602 |
* @}
|
|
603 |
*/
|
|
604 |
|
|
605 |
/** @addtogroup FLASH_Private_Functions
|
|
606 |
* @{
|
|
607 |
*/
|
|
608 |
|
|
609 |
/**
|
|
610 |
* @brief Program a half-word (16-bit) at a specified address.
|
|
611 |
* @param Address specify the address to be programmed.
|
|
612 |
* @param Data specify the data to be programmed.
|
|
613 |
* @retval None
|
|
614 |
*/
|
|
615 |
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
|
|
616 |
{
|
|
617 |
/* Clean the error context */
|
|
618 |
pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
|
|
619 |
|
|
620 |
/* Proceed to program the new data */
|
|
621 |
SET_BIT(FLASH->CR, FLASH_CR_PG);
|
|
622 |
|
|
623 |
/* Write data in the address */
|
|
624 |
*(__IO uint16_t*)Address = Data;
|
|
625 |
}
|
|
626 |
|
|
627 |
/**
|
|
628 |
* @brief Wait for a FLASH operation to complete.
|
|
629 |
* @param Timeout maximum flash operation timeout
|
|
630 |
* @retval HAL Status
|
|
631 |
*/
|
|
632 |
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
|
|
633 |
{
|
|
634 |
/* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
|
|
635 |
Even if the FLASH operation fails, the BUSY flag will be reset and an error
|
|
636 |
flag will be set */
|
|
637 |
|
|
638 |
uint32_t tickstart = HAL_GetTick();
|
|
639 |
|
|
640 |
while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
|
|
641 |
{
|
|
642 |
if (Timeout != HAL_MAX_DELAY)
|
|
643 |
{
|
|
644 |
if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
|
|
645 |
{
|
|
646 |
return HAL_TIMEOUT;
|
|
647 |
}
|
|
648 |
}
|
|
649 |
}
|
|
650 |
|
|
651 |
/* Check FLASH End of Operation flag */
|
|
652 |
if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
|
|
653 |
{
|
|
654 |
/* Clear FLASH End of Operation pending bit */
|
|
655 |
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
|
|
656 |
}
|
|
657 |
|
|
658 |
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
|
|
659 |
__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
|
|
660 |
{
|
|
661 |
/*Save the error code*/
|
|
662 |
FLASH_SetErrorCode();
|
|
663 |
return HAL_ERROR;
|
|
664 |
}
|
|
665 |
|
|
666 |
/* There is no error flag set */
|
|
667 |
return HAL_OK;
|
|
668 |
}
|
|
669 |
|
|
670 |
|
|
671 |
/**
|
|
672 |
* @brief Set the specific FLASH error flag.
|
|
673 |
* @retval None
|
|
674 |
*/
|
|
675 |
static void FLASH_SetErrorCode(void)
|
|
676 |
{
|
|
677 |
uint32_t flags = 0U;
|
|
678 |
|
|
679 |
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
|
|
680 |
{
|
|
681 |
pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
|
|
682 |
flags |= FLASH_FLAG_WRPERR;
|
|
683 |
}
|
|
684 |
if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
|
|
685 |
{
|
|
686 |
pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
|
|
687 |
flags |= FLASH_FLAG_PGERR;
|
|
688 |
}
|
|
689 |
/* Clear FLASH error pending bits */
|
|
690 |
__HAL_FLASH_CLEAR_FLAG(flags);
|
|
691 |
}
|
|
692 |
/**
|
|
693 |
* @}
|
|
694 |
*/
|
|
695 |
|
|
696 |
/**
|
|
697 |
* @}
|
|
698 |
*/
|
|
699 |
|
|
700 |
#endif /* HAL_FLASH_MODULE_ENABLED */
|
|
701 |
|
|
702 |
/**
|
|
703 |
* @}
|
|
704 |
*/
|
|
705 |
|
|
706 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|