a997ce3fff4f04f9d8596efa1200296c2937592b
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_flash.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_flash.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-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"
104
105 /** @addtogroup STM32F7xx_HAL_Driver
106 * @{
107 */
108
109 /** @defgroup FLASH FLASH
110 * @brief FLASH HAL module driver
111 * @{
112 */
113
114 #ifdef HAL_FLASH_MODULE_ENABLED
115
116 /* Private typedef -----------------------------------------------------------*/
117 /* Private define ------------------------------------------------------------*/
118 /** @addtogroup FLASH_Private_Constants
119 * @{
120 */
121 #define SECTOR_MASK ((uint32_t)0xFFFFFF07U)
122 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000U)/* 50 s */
123 /**
124 * @}
125 */
126 /* Private macro -------------------------------------------------------------*/
127 /* Private variables ---------------------------------------------------------*/
128 /** @addtogroup FLASH_Private_Variables
129 * @{
130 */
131 /* Variable used for Erase sectors under interruption */
132 FLASH_ProcessTypeDef pFlash;
133 /**
134 * @}
135 */
136
137 /* Private function prototypes -----------------------------------------------*/
138 /** @addtogroup FLASH_Private_Functions
139 * @{
140 */
141 /* Program operations */
142 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
143 static void FLASH_Program_Word(uint32_t Address, uint32_t Data);
144 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
145 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data);
146 static void FLASH_SetErrorCode(void);
147
148 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
149 /**
150 * @}
151 */
152
153 /* Exported functions --------------------------------------------------------*/
154 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
155 * @{
156 */
157
158 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
159 * @brief Programming operation functions
160 *
161 @verbatim
162 ===============================================================================
163 ##### Programming operation functions #####
164 ===============================================================================
165 [..]
166 This subsection provides a set of functions allowing to manage the FLASH
167 program operations.
168
169 @endverbatim
170 * @{
171 */
172
173 /**
174 * @brief Program byte, halfword, word or double word at a specified address
175 * @param TypeProgram: Indicate the way to program at a specified address.
176 * This parameter can be a value of @ref FLASH_Type_Program
177 * @param Address: specifies the address to be programmed.
178 * @param Data: specifies the data to be programmed
179 *
180 * @retval HAL_StatusTypeDef HAL Status
181 */
182 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
183 {
184 HAL_StatusTypeDef status = HAL_ERROR;
185
186 /* Process Locked */
187 __HAL_LOCK(&pFlash);
188
189 /* Check the parameters */
190 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
191
192 /* Wait for last operation to be completed */
193 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
194
195 if(status == HAL_OK)
196 {
197 switch(TypeProgram)
198 {
199 case FLASH_TYPEPROGRAM_BYTE :
200 {
201 /*Program byte (8-bit) at a specified address.*/
202 FLASH_Program_Byte(Address, (uint8_t) Data);
203 break;
204 }
205
206 case FLASH_TYPEPROGRAM_HALFWORD :
207 {
208 /*Program halfword (16-bit) at a specified address.*/
209 FLASH_Program_HalfWord(Address, (uint16_t) Data);
210 break;
211 }
212
213 case FLASH_TYPEPROGRAM_WORD :
214 {
215 /*Program word (32-bit) at a specified address.*/
216 FLASH_Program_Word(Address, (uint32_t) Data);
217 break;
218 }
219
220 case FLASH_TYPEPROGRAM_DOUBLEWORD :
221 {
222 /*Program double word (64-bit) at a specified address.*/
223 FLASH_Program_DoubleWord(Address, Data);
224 break;
225 }
226 default :
227 break;
228 }
229 /* Wait for last operation to be completed */
230 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
231
232 /* If the program operation is completed, disable the PG Bit */
233 FLASH->CR &= (~FLASH_CR_PG);
234 }
235
236 /* Process Unlocked */
237 __HAL_UNLOCK(&pFlash);
238
239 return status;
240 }
241
242 /**
243 * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled.
244 * @param TypeProgram: Indicate the way to program at a specified address.
245 * This parameter can be a value of @ref FLASH_Type_Program
246 * @param Address: specifies the address to be programmed.
247 * @param Data: specifies the data to be programmed
248 *
249 * @retval HAL Status
250 */
251 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
252 {
253 HAL_StatusTypeDef status = HAL_OK;
254
255 /* Process Locked */
256 __HAL_LOCK(&pFlash);
257
258 /* Check the parameters */
259 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
260
261 /* Enable End of FLASH Operation interrupt */
262 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
263
264 /* Enable Error source interrupt */
265 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
266
267 /* Clear pending flags (if any) */
268 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
269 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_ERSERR);
270
271 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
272 pFlash.Address = Address;
273
274 switch(TypeProgram)
275 {
276 case FLASH_TYPEPROGRAM_BYTE :
277 {
278 /*Program byte (8-bit) at a specified address.*/
279 FLASH_Program_Byte(Address, (uint8_t) Data);
280 break;
281 }
282
283 case FLASH_TYPEPROGRAM_HALFWORD :
284 {
285 /*Program halfword (16-bit) at a specified address.*/
286 FLASH_Program_HalfWord(Address, (uint16_t) Data);
287 break;
288 }
289
290 case FLASH_TYPEPROGRAM_WORD :
291 {
292 /*Program word (32-bit) at a specified address.*/
293 FLASH_Program_Word(Address, (uint32_t) Data);
294 break;
295 }
296
297 case FLASH_TYPEPROGRAM_DOUBLEWORD :
298 {
299 /*Program double word (64-bit) at a specified address.*/
300 FLASH_Program_DoubleWord(Address, Data);
301 break;
302 }
303 default :
304 break;
305 }
306 return status;
307 }
308
309 /**
310 * @brief This function handles FLASH interrupt request.
311 * @retval None
312 */
313 void HAL_FLASH_IRQHandler(void)
314 {
315 uint32_t temp = 0;
316
317 /* If the program operation is completed, disable the PG Bit */
318 FLASH->CR &= (~FLASH_CR_PG);
319
320 /* If the erase operation is completed, disable the SER Bit */
321 FLASH->CR &= (~FLASH_CR_SER);
322 FLASH->CR &= SECTOR_MASK;
323
324 /* if the erase operation is completed, disable the MER Bit */
325 FLASH->CR &= (~FLASH_MER_BIT);
326
327 /* Check FLASH End of Operation flag */
328 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
329 {
330 switch (pFlash.ProcedureOnGoing)
331 {
332 case FLASH_PROC_SECTERASE :
333 {
334 /* Nb of sector to erased can be decreased */
335 pFlash.NbSectorsToErase--;
336
337 /* Check if there are still sectors to erase */
338 if(pFlash.NbSectorsToErase != 0)
339 {
340 temp = pFlash.Sector;
341 /* Indicate user which sector has been erased */
342 HAL_FLASH_EndOfOperationCallback(temp);
343
344 /* Clear pending flags (if any) */
345 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
346
347 /* Increment sector number */
348 temp = ++pFlash.Sector;
349 FLASH_Erase_Sector(temp, pFlash.VoltageForErase);
350 }
351 else
352 {
353 /* No more sectors to Erase, user callback can be called.*/
354 /* Reset Sector and stop Erase sectors procedure */
355 pFlash.Sector = temp = 0xFFFFFFFFU;
356 /* FLASH EOP interrupt user callback */
357 HAL_FLASH_EndOfOperationCallback(temp);
358 /* Sector Erase procedure is completed */
359 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
360 /* Clear FLASH End of Operation pending bit */
361 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
362 }
363 break;
364 }
365
366 case FLASH_PROC_MASSERASE :
367 {
368 /* MassErase ended. Return the selected bank : in this product we don't have Banks */
369 /* FLASH EOP interrupt user callback */
370 HAL_FLASH_EndOfOperationCallback(0);
371 /* MAss Erase procedure is completed */
372 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
373 /* Clear FLASH End of Operation pending bit */
374 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
375 break;
376 }
377
378 case FLASH_PROC_PROGRAM :
379 {
380 /*Program ended. Return the selected address*/
381 /* FLASH EOP interrupt user callback */
382 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
383 /* Programming procedure is completed */
384 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
385 /* Clear FLASH End of Operation pending bit */
386 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
387 break;
388 }
389 default :
390 break;
391 }
392 }
393
394 /* Check FLASH operation error flags */
395 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR )) != 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*/
422 FLASH_SetErrorCode();
423
424 /* FLASH error interrupt user callback */
425 HAL_FLASH_OperationErrorCallback(temp);
426 /* Clear FLASH error pending bits */
427 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR );
428
429 /*Stop the procedure ongoing */
430 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
431 }
432
433 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
434 {
435 /* Disable End of FLASH Operation interrupt */
436 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
437
438 /* Disable Error source interrupt */
439 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
440
441 /* Process Unlocked */
442 __HAL_UNLOCK(&pFlash);
443 }
444
445 }
446
447 /**
448 * @brief FLASH end of operation interrupt callback
449 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
450 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
451 * all the selected sectors have been erased)
452 * - Program : Address which was selected for data program
453 * - Mass Erase : No return value expected
454 * @retval None
455 */
456 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
457 {
458 /* Prevent unused argument(s) compilation warning */
459 UNUSED(ReturnValue);
460 /* NOTE : This function Should not be modified, when the callback is needed,
461 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
462 */
463 }
464
465 /**
466 * @brief FLASH operation error interrupt callback
467 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
468 * - Sectors Erase: Sector which has been erased (if 0xFFFFFFFF, it means that
469 * all the selected sectors have been erased)
470 * - Program : Address which was selected for data program
471 * - Mass Erase : No return value expected
472 * @retval None
473 */
474 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
475 {
476 /* Prevent unused argument(s) compilation warning */
477 UNUSED(ReturnValue);
478 /* NOTE : This function Should not be modified, when the callback is needed,
479 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
480 */
481 }
482
483 /**
484 * @}
485 */
486
487 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
488 * @brief management functions
489 *
490 @verbatim
491 ===============================================================================
492 ##### Peripheral Control functions #####
493 ===============================================================================
494 [..]
495 This subsection provides a set of functions allowing to control the FLASH
496 memory operations.
497
498 @endverbatim
499 * @{
500 */
501
502 /**
503 * @brief Unlock the FLASH control register access
504 * @retval HAL Status
505 */
506 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
507 {
508 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
509 {
510 /* Authorize the FLASH Registers access */
511 FLASH->KEYR = FLASH_KEY1;
512 FLASH->KEYR = FLASH_KEY2;
513 }
514 else
515 {
516 return HAL_ERROR;
517 }
518
519 return HAL_OK;
520 }
521
522 /**
523 * @brief Locks the FLASH control register access
524 * @retval HAL Status
525 */
526 HAL_StatusTypeDef HAL_FLASH_Lock(void)
527 {
528 /* Set the LOCK Bit to lock the FLASH Registers access */
529 FLASH->CR |= FLASH_CR_LOCK;
530
531 return HAL_OK;
532 }
533
534 /**
535 * @brief Unlock the FLASH Option Control Registers access.
536 * @retval HAL Status
537 */
538 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
539 {
540 if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
541 {
542 /* Authorizes the Option Byte register programming */
543 FLASH->OPTKEYR = FLASH_OPT_KEY1;
544 FLASH->OPTKEYR = FLASH_OPT_KEY2;
545 }
546 else
547 {
548 return HAL_ERROR;
549 }
550
551 return HAL_OK;
552 }
553
554 /**
555 * @brief Lock the FLASH Option Control Registers access.
556 * @retval HAL Status
557 */
558 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
559 {
560 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
561 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
562
563 return HAL_OK;
564 }
565
566 /**
567 * @brief Launch the option byte loading.
568 * @retval HAL Status
569 */
570 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
571 {
572 /* Set the OPTSTRT bit in OPTCR register */
573 FLASH->OPTCR |= FLASH_OPTCR_OPTSTRT;
574
575 /* Wait for last operation to be completed */
576 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
577 }
578
579 /**
580 * @}
581 */
582
583 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
584 * @brief Peripheral Errors functions
585 *
586 @verbatim
587 ===============================================================================
588 ##### Peripheral Errors functions #####
589 ===============================================================================
590 [..]
591 This subsection permits to get in run-time Errors of the FLASH peripheral.
592
593 @endverbatim
594 * @{
595 */
596
597 /**
598 * @brief Get the specific FLASH error flag.
599 * @retval FLASH_ErrorCode: The returned value can be:
600 * @arg FLASH_ERROR_ERS: FLASH Erasing Sequence error flag
601 * @arg FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
602 * @arg FLASH_ERROR_PGA: FLASH Programming Alignment error flag
603 * @arg FLASH_ERROR_WRP: FLASH Write protected error flag
604 * @arg FLASH_ERROR_OPERATION: FLASH operation Error flag
605 */
606 uint32_t HAL_FLASH_GetError(void)
607 {
608 return pFlash.ErrorCode;
609 }
610
611 /**
612 * @}
613 */
614
615 /**
616 * @brief Wait for a FLASH operation to complete.
617 * @param Timeout: maximum flash operationtimeout
618 * @retval HAL Status
619 */
620 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
621 {
622 uint32_t tickstart = 0;
623
624 /* Clear Error Code */
625 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
626
627 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
628 Even if the FLASH operation fails, the BUSY flag will be reset and an error
629 flag will be set */
630 /* Get tick */
631 tickstart = HAL_GetTick();
632
633 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
634 {
635 if(Timeout != HAL_MAX_DELAY)
636 {
637 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
638 {
639 return HAL_TIMEOUT;
640 }
641 }
642 }
643
644 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
645 FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR )) != RESET)
646 {
647 /*Save the error code*/
648 FLASH_SetErrorCode();
649 return HAL_ERROR;
650 }
651
652 /* If there is an error flag set */
653 return HAL_OK;
654
655 }
656
657 /**
658 * @brief Program a double word (64-bit) at a specified address.
659 * @note This function must be used when the device voltage range is from
660 * 2.7V to 3.6V and an External Vpp is present.
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 */
669 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_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 FLASH->CR &= CR_PSIZE_MASK;
676 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
677 FLASH->CR |= FLASH_CR_PG;
678
679 *(__IO uint64_t*)Address = Data;
680
681 /* Data synchronous Barrier (DSB) Just after the write operation
682 This will force the CPU to respect the sequence of instruction (no optimization).*/
683 __DSB();
684 }
685
686
687 /**
688 * @brief Program word (32-bit) at a specified address.
689 * @note This function must be used when the device voltage range is from
690 * 2.7V to 3.6V.
691 *
692 * @note If an erase and a program operations are requested simultaneously,
693 * the erase operation is performed before the program one.
694 *
695 * @param Address: specifies the address to be programmed.
696 * @param Data: specifies the data to be programmed.
697 * @retval None
698 */
699 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
700 {
701 /* Check the parameters */
702 assert_param(IS_FLASH_ADDRESS(Address));
703
704 /* If the previous operation is completed, proceed to program the new data */
705 FLASH->CR &= CR_PSIZE_MASK;
706 FLASH->CR |= FLASH_PSIZE_WORD;
707 FLASH->CR |= FLASH_CR_PG;
708
709 *(__IO uint32_t*)Address = Data;
710
711 /* Data synchronous Barrier (DSB) Just after the write operation
712 This will force the CPU to respect the sequence of instruction (no optimization).*/
713 __DSB();
714 }
715
716 /**
717 * @brief Program a half-word (16-bit) at a specified address.
718 * @note This function must be used when the device voltage range is from
719 * 2.7V to 3.6V.
720 *
721 * @note If an erase and a program operations are requested simultaneously,
722 * the erase operation is performed before the program one.
723 *
724 * @param Address: specifies the address to be programmed.
725 * @param Data: specifies the data to be programmed.
726 * @retval None
727 */
728 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
729 {
730 /* Check the parameters */
731 assert_param(IS_FLASH_ADDRESS(Address));
732
733 /* If the previous operation is completed, proceed to program the new data */
734 FLASH->CR &= CR_PSIZE_MASK;
735 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
736 FLASH->CR |= FLASH_CR_PG;
737
738 *(__IO uint16_t*)Address = Data;
739
740 /* Data synchronous Barrier (DSB) Just after the write operation
741 This will force the CPU to respect the sequence of instruction (no optimization).*/
742 __DSB();
743
744 }
745
746 /**
747 * @brief Program byte (8-bit) at a specified address.
748 * @note This function must be used when the device voltage range is from
749 * 2.7V to 3.6V.
750 *
751 * @note If an erase and a program operations are requested simultaneously,
752 * the erase operation is performed before the program one.
753 *
754 * @param Address: specifies the address to be programmed.
755 * @param Data: specifies the data to be programmed.
756 * @retval None
757 */
758 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
759 {
760 /* Check the parameters */
761 assert_param(IS_FLASH_ADDRESS(Address));
762
763 /* If the previous operation is completed, proceed to program the new data */
764 FLASH->CR &= CR_PSIZE_MASK;
765 FLASH->CR |= FLASH_PSIZE_BYTE;
766 FLASH->CR |= FLASH_CR_PG;
767
768 *(__IO uint8_t*)Address = Data;
769
770 /* Data synchronous Barrier (DSB) Just after the write operation
771 This will force the CPU to respect the sequence of instruction (no optimization).*/
772 __DSB();
773 }
774
775 /**
776 * @brief Set the specific FLASH error flag.
777 * @retval None
778 */
779 static void FLASH_SetErrorCode(void)
780 {
781 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
782 {
783 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
784 }
785
786 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
787 {
788 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
789 }
790
791 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
792 {
793 pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
794 }
795
796 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ERSERR) != RESET)
797 {
798 pFlash.ErrorCode |= HAL_FLASH_ERROR_ERS;
799 }
800
801 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
802 {
803 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
804 }
805 }
806
807 /**
808 * @}
809 */
810
811 #endif /* HAL_FLASH_MODULE_ENABLED */
812
813 /**
814 * @}
815 */
816
817 /**
818 * @}
819 */
820
821 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/