source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c

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