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