1 /**
2 ******************************************************************************
3 * @file stm32f4xx_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 Errors functions
11 *
12 @verbatim
13 ==============================================================================
14 ##### FLASH peripheral features #####
15 ==============================================================================
16
17 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
18 to the Flash memory. It implements the erase and program Flash memory operations
19 and the read and write protection mechanisms.
20
21 [..] The Flash memory interface accelerates code execution with a system of instruction
22 prefetch and cache lines.
23
24 [..] The FLASH main features are:
25 (+) Flash memory read operations
26 (+) Flash memory program/erase operations
27 (+) Read / write protections
28 (+) Prefetch on I-Code
29 (+) 64 cache lines of 128 bits on I-Code
30 (+) 8 cache lines of 128 bits on D-Code
31
32
33 ##### How to use this driver #####
34 ==============================================================================
35 [..]
36 This driver provides functions and macros to configure and program the FLASH
37 memory of all STM32F4xx devices.
38
39 (#) FLASH Memory IO Programming functions:
40 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
41 HAL_FLASH_Lock() functions
42 (++) Program functions: byte, half word, word and double word
43 (++) There Two modes of programming :
44 (+++) Polling mode using HAL_FLASH_Program() function
45 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
46
47 (#) Interrupts and flags management functions :
48 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
49 (++) Wait for last FLASH operation according to its status
50 (++) Get error flag status by calling HAL_SetErrorCode()
51
52 [..]
53 In addition to these functions, this driver includes a set of macros allowing
54 to handle the following operations:
55 (+) Set the latency
56 (+) Enable/Disable the prefetch buffer
57 (+) Enable/Disable the Instruction cache and the Data cache
58 (+) Reset the Instruction cache and the Data cache
59 (+) Enable/Disable the FLASH interrupts
60 (+) Monitor the FLASH flags status
61
62 @endverbatim
63 ******************************************************************************
64 * @attention
65 *
66 * Copyright (c) 2017 STMicroelectronics.
67 * All rights reserved.
68 *
69 * This software is licensed under terms that can be found in the LICENSE file in
70 * the root directory of this software component.
71 * If no LICENSE file comes with this software, it is provided AS-IS.
72 ******************************************************************************
73 */
74
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f4xx_hal.h"
77
78 /** @addtogroup STM32F4xx_HAL_Driver
79 * @{
80 */
81
82 /** @defgroup FLASH FLASH
83 * @brief FLASH HAL module driver
84 * @{
85 */
86
87 #ifdef HAL_FLASH_MODULE_ENABLED
88
89 /* Private typedef -----------------------------------------------------------*/
90 /* Private define ------------------------------------------------------------*/
91 /** @addtogroup FLASH_Private_Constants
92 * @{
93 */
94 #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
95 /**
96 * @}
97 */
98 /* Private macro -------------------------------------------------------------*/
99 /* Private variables ---------------------------------------------------------*/
100 /** @addtogroup FLASH_Private_Variables
101 * @{
102 */
103 /* Variable used for Erase sectors under interruption */
104 FLASH_ProcessTypeDef pFlash;
105 /**
106 * @}
107 */
108
109 /* Private function prototypes -----------------------------------------------*/
110 /** @addtogroup FLASH_Private_Functions
111 * @{
112 */
113 /* Program operations */
114 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
115 static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
116 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
117 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
118 static void FLASH_SetErrorCode(void);
119
120 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
121 /**
122 * @}
123 */
124
125 /* Exported functions --------------------------------------------------------*/
126 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
127 * @{
128 */
129
130 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
131 * @brief Programming operation functions
132 *
133 @verbatim
134 ===============================================================================
135 ##### Programming operation functions #####
136 ===============================================================================
137 [..]
138 This subsection provides a set of functions allowing to manage the FLASH
139 program operations.
140
141 @endverbatim
142 * @{
143 */
144
145 /**
146 * @brief Program byte, halfword, word or double word at a specified address
147 * @param TypeProgram Indicate the way to program at a specified address.
148 * This parameter can be a value of @ref FLASH_Type_Program
149 * @param Address specifies the address to be programmed.
150 * @param Data specifies the data to be programmed
151 *
152 * @retval HAL_StatusTypeDef HAL Status
153 */
HAL_FLASH_Program(uint32_t TypeProgram,uint32_t Address,uint64_t Data)154 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
155 {
156 HAL_StatusTypeDef status = HAL_ERROR;
157
158 /* Process Locked */
159 __HAL_LOCK(&pFlash);
160
161 /* Check the parameters */
162 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
163
164 /* Wait for last operation to be completed */
165 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
166
167 if(status == HAL_OK)
168 {
169 if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
170 {
171 /*Program byte (8-bit) at a specified address.*/
172 FLASH_Program_Byte(Address, (uint8_t) Data);
173 }
174 else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
175 {
176 /*Program halfword (16-bit) at a specified address.*/
177 FLASH_Program_HalfWord(Address, (uint16_t) Data);
178 }
179 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
180 {
181 /*Program word (32-bit) at a specified address.*/
182 FLASH_Program_Word(Address, (uint32_t) Data);
183 }
184 else
185 {
186 /*Program double word (64-bit) at a specified address.*/
187 FLASH_Program_DoubleWord(Address, Data);
188 }
189
190 /* Wait for last operation to be completed */
191 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
192
193 /* If the program operation is completed, disable the PG Bit */
194 FLASH->CR &= (~FLASH_CR_PG);
195 }
196
197 /* Process Unlocked */
198 __HAL_UNLOCK(&pFlash);
199
200 return status;
201 }
202
203 /**
204 * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
205 * @param TypeProgram Indicate the way to program at a specified address.
206 * This parameter can be a value of @ref FLASH_Type_Program
207 * @param Address specifies the address to be programmed.
208 * @param Data specifies the data to be programmed
209 *
210 * @retval HAL Status
211 */
HAL_FLASH_Program_IT(uint32_t TypeProgram,uint32_t Address,uint64_t Data)212 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
213 {
214 HAL_StatusTypeDef status = HAL_OK;
215
216 /* Process Locked */
217 __HAL_LOCK(&pFlash);
218
219 /* Check the parameters */
220 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
221
222 /* Enable End of FLASH Operation interrupt */
223 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
224
225 /* Enable Error source interrupt */
226 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
227
228 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
229 pFlash.Address = Address;
230
231 if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
232 {
233 /*Program byte (8-bit) at a specified address.*/
234 FLASH_Program_Byte(Address, (uint8_t) Data);
235 }
236 else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
237 {
238 /*Program halfword (16-bit) at a specified address.*/
239 FLASH_Program_HalfWord(Address, (uint16_t) Data);
240 }
241 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
242 {
243 /*Program word (32-bit) at a specified address.*/
244 FLASH_Program_Word(Address, (uint32_t) Data);
245 }
246 else
247 {
248 /*Program double word (64-bit) at a specified address.*/
249 FLASH_Program_DoubleWord(Address, Data);
250 }
251
252 return status;
253 }
254
255 /**
256 * @brief This function handles FLASH interrupt request.
257 * @retval None
258 */
HAL_FLASH_IRQHandler(void)259 void HAL_FLASH_IRQHandler(void)
260 {
261 uint32_t addresstmp = 0U;
262
263 /* Check FLASH operation error flags */
264 #if defined(FLASH_SR_RDERR)
265 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
266 FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
267 #else
268 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
269 FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
270 #endif /* FLASH_SR_RDERR */
271 {
272 if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
273 {
274 /*return the faulty sector*/
275 addresstmp = pFlash.Sector;
276 pFlash.Sector = 0xFFFFFFFFU;
277 }
278 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
279 {
280 /*return the faulty bank*/
281 addresstmp = pFlash.Bank;
282 }
283 else
284 {
285 /*return the faulty address*/
286 addresstmp = pFlash.Address;
287 }
288
289 /*Save the Error code*/
290 FLASH_SetErrorCode();
291
292 /* FLASH error interrupt user callback */
293 HAL_FLASH_OperationErrorCallback(addresstmp);
294
295 /*Stop the procedure ongoing*/
296 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
297 }
298
299 /* Check FLASH End of Operation flag */
300 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
301 {
302 /* Clear FLASH End of Operation pending bit */
303 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
304
305 if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
306 {
307 /*Nb of sector to erased can be decreased*/
308 pFlash.NbSectorsToErase--;
309
310 /* Check if there are still sectors to erase*/
311 if(pFlash.NbSectorsToErase != 0U)
312 {
313 addresstmp = pFlash.Sector;
314 /*Indicate user which sector has been erased*/
315 HAL_FLASH_EndOfOperationCallback(addresstmp);
316
317 /*Increment sector number*/
318 pFlash.Sector++;
319 addresstmp = pFlash.Sector;
320 FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
321 }
322 else
323 {
324 /*No more sectors to Erase, user callback can be called.*/
325 /*Reset Sector and stop Erase sectors procedure*/
326 pFlash.Sector = addresstmp = 0xFFFFFFFFU;
327 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
328
329 /* Flush the caches to be sure of the data consistency */
330 FLASH_FlushCaches() ;
331
332 /* FLASH EOP interrupt user callback */
333 HAL_FLASH_EndOfOperationCallback(addresstmp);
334 }
335 }
336 else
337 {
338 if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
339 {
340 /* MassErase ended. Return the selected bank */
341 /* Flush the caches to be sure of the data consistency */
342 FLASH_FlushCaches() ;
343
344 /* FLASH EOP interrupt user callback */
345 HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
346 }
347 else
348 {
349 /*Program ended. Return the selected address*/
350 /* FLASH EOP interrupt user callback */
351 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
352 }
353 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
354 }
355 }
356
357 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
358 {
359 /* Operation is completed, disable the PG, SER, SNB and MER Bits */
360 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
361
362 /* Disable End of FLASH Operation interrupt */
363 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
364
365 /* Disable Error source interrupt */
366 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
367
368 /* Process Unlocked */
369 __HAL_UNLOCK(&pFlash);
370 }
371 }
372
373 /**
374 * @brief FLASH end of operation interrupt callback
375 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
376 * Mass Erase: Bank number which has been requested to erase
377 * Sectors Erase: Sector which has been erased
378 * (if 0xFFFFFFFFU, it means that all the selected sectors have been erased)
379 * Program: Address which was selected for data program
380 * @retval None
381 */
HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)382 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
383 {
384 /* Prevent unused argument(s) compilation warning */
385 UNUSED(ReturnValue);
386 /* NOTE : This function Should not be modified, when the callback is needed,
387 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
388 */
389 }
390
391 /**
392 * @brief FLASH operation error interrupt callback
393 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
394 * Mass Erase: Bank number which has been requested to erase
395 * Sectors Erase: Sector number which returned an error
396 * Program: Address which was selected for data program
397 * @retval None
398 */
HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)399 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
400 {
401 /* Prevent unused argument(s) compilation warning */
402 UNUSED(ReturnValue);
403 /* NOTE : This function Should not be modified, when the callback is needed,
404 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
405 */
406 }
407
408 /**
409 * @}
410 */
411
412 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
413 * @brief management functions
414 *
415 @verbatim
416 ===============================================================================
417 ##### Peripheral Control functions #####
418 ===============================================================================
419 [..]
420 This subsection provides a set of functions allowing to control the FLASH
421 memory operations.
422
423 @endverbatim
424 * @{
425 */
426
427 /**
428 * @brief Unlock the FLASH control register access
429 * @retval HAL Status
430 */
HAL_FLASH_Unlock(void)431 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
432 {
433 HAL_StatusTypeDef status = HAL_OK;
434
435 if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
436 {
437 /* Authorize the FLASH Registers access */
438 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
439 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
440
441 /* Verify Flash is unlocked */
442 if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
443 {
444 status = HAL_ERROR;
445 }
446 }
447
448 return status;
449 }
450
451 /**
452 * @brief Locks the FLASH control register access
453 * @retval HAL Status
454 */
HAL_FLASH_Lock(void)455 HAL_StatusTypeDef HAL_FLASH_Lock(void)
456 {
457 /* Set the LOCK Bit to lock the FLASH Registers access */
458 FLASH->CR |= FLASH_CR_LOCK;
459
460 return HAL_OK;
461 }
462
463 /**
464 * @brief Unlock the FLASH Option Control Registers access.
465 * @retval HAL Status
466 */
HAL_FLASH_OB_Unlock(void)467 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
468 {
469 if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
470 {
471 /* Authorizes the Option Byte register programming */
472 FLASH->OPTKEYR = FLASH_OPT_KEY1;
473 FLASH->OPTKEYR = FLASH_OPT_KEY2;
474 }
475 else
476 {
477 return HAL_ERROR;
478 }
479
480 return HAL_OK;
481 }
482
483 /**
484 * @brief Lock the FLASH Option Control Registers access.
485 * @retval HAL Status
486 */
HAL_FLASH_OB_Lock(void)487 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
488 {
489 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
490 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
491
492 return HAL_OK;
493 }
494
495 /**
496 * @brief Launch the option byte loading.
497 * @retval HAL Status
498 */
HAL_FLASH_OB_Launch(void)499 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
500 {
501 /* Set the OPTSTRT bit in OPTCR register */
502 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
503
504 /* Wait for last operation to be completed */
505 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
506 }
507
508 /**
509 * @}
510 */
511
512 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
513 * @brief Peripheral Errors functions
514 *
515 @verbatim
516 ===============================================================================
517 ##### Peripheral Errors functions #####
518 ===============================================================================
519 [..]
520 This subsection permits to get in run-time Errors of the FLASH peripheral.
521
522 @endverbatim
523 * @{
524 */
525
526 /**
527 * @brief Get the specific FLASH error flag.
528 * @retval FLASH_ErrorCode: The returned value can be a combination of:
529 * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
530 * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
531 * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
532 * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
533 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
534 * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
535 */
HAL_FLASH_GetError(void)536 uint32_t HAL_FLASH_GetError(void)
537 {
538 return pFlash.ErrorCode;
539 }
540
541 /**
542 * @}
543 */
544
545 /**
546 * @brief Wait for a FLASH operation to complete.
547 * @param Timeout maximum flash operationtimeout
548 * @retval HAL Status
549 */
FLASH_WaitForLastOperation(uint32_t Timeout)550 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
551 {
552 uint32_t tickstart = 0U;
553
554 /* Clear Error Code */
555 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
556
557 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
558 Even if the FLASH operation fails, the BUSY flag will be reset and an error
559 flag will be set */
560 /* Get tick */
561 tickstart = HAL_GetTick();
562
563 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
564 {
565 if(Timeout != HAL_MAX_DELAY)
566 {
567 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
568 {
569 return HAL_TIMEOUT;
570 }
571 }
572 }
573
574 /* Check FLASH End of Operation flag */
575 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
576 {
577 /* Clear FLASH End of Operation pending bit */
578 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
579 }
580 #if defined(FLASH_SR_RDERR)
581 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
582 FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
583 #else
584 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
585 FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
586 #endif /* FLASH_SR_RDERR */
587 {
588 /*Save the error code*/
589 FLASH_SetErrorCode();
590 return HAL_ERROR;
591 }
592
593 /* If there is no error flag set */
594 return HAL_OK;
595
596 }
597
598 /**
599 * @brief Program a double word (64-bit) at a specified address.
600 * @note This function must be used when the device voltage range is from
601 * 2.7V to 3.6V and Vpp in the range 7V to 9V.
602 *
603 * @note If an erase and a program operations are requested simultaneously,
604 * the erase operation is performed before the program one.
605 *
606 * @param Address specifies the address to be programmed.
607 * @param Data specifies the data to be programmed.
608 * @retval None
609 */
FLASH_Program_DoubleWord(uint32_t Address,uint64_t Data)610 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
611 {
612 /* Check the parameters */
613 assert_param(IS_FLASH_ADDRESS(Address));
614
615 /* If the previous operation is completed, proceed to program the new data */
616 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
617 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
618 FLASH->CR |= FLASH_CR_PG;
619
620 /* Program first word */
621 *(__IO uint32_t*)Address = (uint32_t)Data;
622
623 /* Barrier to ensure programming is performed in 2 steps, in right order
624 (independently of compiler optimization behavior) */
625 __ISB();
626
627 /* Program second word */
628 *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
629 }
630
631
632 /**
633 * @brief Program word (32-bit) at a specified address.
634 * @note This function must be used when the device voltage range is from
635 * 2.7V to 3.6V.
636 *
637 * @note If an erase and a program operations are requested simultaneously,
638 * the erase operation is performed before the program one.
639 *
640 * @param Address specifies the address to be programmed.
641 * @param Data specifies the data to be programmed.
642 * @retval None
643 */
FLASH_Program_Word(uint32_t Address,uint32_t Data)644 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
645 {
646 /* Check the parameters */
647 assert_param(IS_FLASH_ADDRESS(Address));
648
649 /* If the previous operation is completed, proceed to program the new data */
650 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
651 FLASH->CR |= FLASH_PSIZE_WORD;
652 FLASH->CR |= FLASH_CR_PG;
653
654 *(__IO uint32_t*)Address = Data;
655 }
656
657 /**
658 * @brief Program a half-word (16-bit) at a specified address.
659 * @note This function must be used when the device voltage range is from
660 * 2.1V to 3.6V.
661 *
662 * @note If an erase and a program operations are requested simultaneously,
663 * the erase operation is performed before the program one.
664 *
665 * @param Address specifies the address to be programmed.
666 * @param Data specifies the data to be programmed.
667 * @retval None
668 */
FLASH_Program_HalfWord(uint32_t Address,uint16_t Data)669 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
670 {
671 /* Check the parameters */
672 assert_param(IS_FLASH_ADDRESS(Address));
673
674 /* If the previous operation is completed, proceed to program the new data */
675 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
676 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
677 FLASH->CR |= FLASH_CR_PG;
678
679 *(__IO uint16_t*)Address = Data;
680 }
681
682 /**
683 * @brief Program byte (8-bit) at a specified address.
684 * @note This function must be used when the device voltage range is from
685 * 1.8V to 3.6V.
686 *
687 * @note If an erase and a program operations are requested simultaneously,
688 * the erase operation is performed before the program one.
689 *
690 * @param Address specifies the address to be programmed.
691 * @param Data specifies the data to be programmed.
692 * @retval None
693 */
FLASH_Program_Byte(uint32_t Address,uint8_t Data)694 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
695 {
696 /* Check the parameters */
697 assert_param(IS_FLASH_ADDRESS(Address));
698
699 /* If the previous operation is completed, proceed to program the new data */
700 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
701 FLASH->CR |= FLASH_PSIZE_BYTE;
702 FLASH->CR |= FLASH_CR_PG;
703
704 *(__IO uint8_t*)Address = Data;
705 }
706
707 /**
708 * @brief Set the specific FLASH error flag.
709 * @retval None
710 */
FLASH_SetErrorCode(void)711 static void FLASH_SetErrorCode(void)
712 {
713 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
714 {
715 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
716
717 /* Clear FLASH write protection error pending bit */
718 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR);
719 }
720
721 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
722 {
723 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
724
725 /* Clear FLASH Programming alignment error pending bit */
726 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
727 }
728
729 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
730 {
731 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
732
733 /* Clear FLASH Programming parallelism error pending bit */
734 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
735 }
736
737 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
738 {
739 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
740
741 /* Clear FLASH Programming sequence error pending bit */
742 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
743 }
744 #if defined(FLASH_SR_RDERR)
745 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
746 {
747 pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
748
749 /* Clear FLASH Proprietary readout protection error pending bit */
750 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
751 }
752 #endif /* FLASH_SR_RDERR */
753 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
754 {
755 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
756
757 /* Clear FLASH Operation error pending bit */
758 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
759 }
760 }
761
762 /**
763 * @}
764 */
765
766 #endif /* HAL_FLASH_MODULE_ENABLED */
767
768 /**
769 * @}
770 */
771
772 /**
773 * @}
774 */
775
776