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