stm32f4xx_hal_flash.c 24.8 KB
Newer Older
Dave Hylands's avatar
Dave Hylands committed
1
2
3
4
/**
  ******************************************************************************
  * @file    stm32f4xx_hal_flash.c
  * @author  MCD Application Team
5
6
  * @version V1.5.1
  * @date    01-July-2016
Dave Hylands's avatar
Dave Hylands committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  * @brief   FLASH HAL module driver.
  *          This file provides firmware functions to manage the following 
  *          functionalities of the internal FLASH memory:
  *           + Program operations functions
  *           + Memory Control functions 
  *           + Peripheral Errors functions
  *         
  @verbatim
  ==============================================================================
                        ##### FLASH peripheral features #####
  ==============================================================================
           
  [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses 
       to the Flash memory. It implements the erase and program Flash memory operations 
       and the read and write protection mechanisms.
      
  [..] The Flash memory interface accelerates code execution with a system of instruction
       prefetch and cache lines. 

  [..] The FLASH main features are:
      (+) Flash memory read operations
      (+) Flash memory program/erase operations
      (+) Read / write protections
      (+) Prefetch on I-Code
      (+) 64 cache lines of 128 bits on I-Code
      (+) 8 cache lines of 128 bits on D-Code
      
      
                     ##### How to use this driver #####
  ==============================================================================
    [..]                             
      This driver provides functions and macros to configure and program the FLASH 
      memory of all STM32F4xx devices.
    
      (#) FLASH Memory IO Programming functions: 
           (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and 
                HAL_FLASH_Lock() functions
           (++) Program functions: byte, half word, word and double word
           (++) There Two modes of programming :
            (+++) Polling mode using HAL_FLASH_Program() function
            (+++) Interrupt mode using HAL_FLASH_Program_IT() function
    
      (#) Interrupts and flags management functions : 
           (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
           (++) Wait for last FLASH operation according to its status
           (++) Get error flag status by calling HAL_SetErrorCode()          

    [..] 
      In addition to these functions, this driver includes a set of macros allowing
      to handle the following operations:
       (+) Set the latency
       (+) Enable/Disable the prefetch buffer
       (+) Enable/Disable the Instruction cache and the Data cache
       (+) Reset the Instruction cache and the Data cache
       (+) Enable/Disable the FLASH interrupts
       (+) Monitor the FLASH flags status
          
  @endverbatim
  ******************************************************************************
  * @attention
  *
68
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
Dave Hylands's avatar
Dave Hylands committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

/** @addtogroup STM32F4xx_HAL_Driver
  * @{
  */

102
/** @defgroup FLASH FLASH
Dave Hylands's avatar
Dave Hylands committed
103
104
105
106
107
108
109
110
  * @brief FLASH HAL module driver
  * @{
  */

#ifdef HAL_FLASH_MODULE_ENABLED

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
111
112
113
114
115
116
117
/** @addtogroup FLASH_Private_Constants
  * @{
  */
#define FLASH_TIMEOUT_VALUE       ((uint32_t)50000U)/* 50 s */
/**
  * @}
  */         
Dave Hylands's avatar
Dave Hylands committed
118
119
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
120
121
122
/** @addtogroup FLASH_Private_Variables
  * @{
  */
Dave Hylands's avatar
Dave Hylands committed
123
124
/* Variable used for Erase sectors under interruption */
FLASH_ProcessTypeDef pFlash;
125
126
127
/**
  * @}
  */
Dave Hylands's avatar
Dave Hylands committed
128
129

/* Private function prototypes -----------------------------------------------*/
130
131
132
/** @addtogroup FLASH_Private_Functions
  * @{
  */
Dave Hylands's avatar
Dave Hylands committed
133
134
135
136
137
138
139
140
/* Program operations */
static void   FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
static void   FLASH_Program_Word(uint32_t Address, uint32_t Data);
static void   FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
static void   FLASH_Program_Byte(uint32_t Address, uint8_t Data);
static void   FLASH_SetErrorCode(void);

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
141
142
143
/**
  * @}
  */
Dave Hylands's avatar
Dave Hylands committed
144

145
146
/* Exported functions --------------------------------------------------------*/
/** @defgroup FLASH_Exported_Functions FLASH Exported Functions
Dave Hylands's avatar
Dave Hylands committed
147
148
149
  * @{
  */
  
150
/** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions 
Dave Hylands's avatar
Dave Hylands committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
 *  @brief   Programming operation functions 
 *
@verbatim   
 ===============================================================================
                  ##### Programming operation functions #####
 ===============================================================================  
    [..]
    This subsection provides a set of functions allowing to manage the FLASH 
    program operations.

@endverbatim
  * @{
  */

/**
  * @brief  Program byte, halfword, word or double word at a specified address
  * @param  TypeProgram:  Indicate the way to program at a specified address.
  *                           This parameter can be a value of @ref FLASH_Type_Program
  * @param  Address:  specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed
  * 
  * @retval HAL_StatusTypeDef HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
{
  HAL_StatusTypeDef status = HAL_ERROR;
  
  /* Process Locked */
  __HAL_LOCK(&pFlash);
180
  
Dave Hylands's avatar
Dave Hylands committed
181
  /* Check the parameters */
182
183
  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  
Dave Hylands's avatar
Dave Hylands committed
184
  /* Wait for last operation to be completed */
185
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
Dave Hylands's avatar
Dave Hylands committed
186
187
188
  
  if(status == HAL_OK)
  {
189
    if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
Dave Hylands's avatar
Dave Hylands committed
190
191
    {
      /*Program byte (8-bit) at a specified address.*/
192
      FLASH_Program_Byte(Address, (uint8_t) Data);
Dave Hylands's avatar
Dave Hylands committed
193
    }
194
    else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
Dave Hylands's avatar
Dave Hylands committed
195
196
197
198
    {
      /*Program halfword (16-bit) at a specified address.*/
      FLASH_Program_HalfWord(Address, (uint16_t) Data);
    }
199
    else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
Dave Hylands's avatar
Dave Hylands committed
200
201
202
203
204
205
206
207
208
    {
      /*Program word (32-bit) at a specified address.*/
      FLASH_Program_Word(Address, (uint32_t) Data);
    }
    else
    {
      /*Program double word (64-bit) at a specified address.*/
      FLASH_Program_DoubleWord(Address, Data);
    }
209
    
Dave Hylands's avatar
Dave Hylands committed
210
    /* Wait for last operation to be completed */
211
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
Dave Hylands's avatar
Dave Hylands committed
212
213
    
    /* If the program operation is completed, disable the PG Bit */
214
    FLASH->CR &= (~FLASH_CR_PG);  
Dave Hylands's avatar
Dave Hylands committed
215
  }
216
  
Dave Hylands's avatar
Dave Hylands committed
217
218
  /* Process Unlocked */
  __HAL_UNLOCK(&pFlash);
219
  
Dave Hylands's avatar
Dave Hylands committed
220
221
222
223
224
225
226
227
228
229
  return status;
}

/**
  * @brief   Program byte, halfword, word or double word at a specified address  with interrupt enabled.
  * @param  TypeProgram:  Indicate the way to program at a specified address.
  *                           This parameter can be a value of @ref FLASH_Type_Program
  * @param  Address:  specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed
  * 
230
  * @retval HAL Status
Dave Hylands's avatar
Dave Hylands committed
231
232
233
234
235
236
237
238
239
  */
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
{
  HAL_StatusTypeDef status = HAL_OK;
  
  /* Process Locked */
  __HAL_LOCK(&pFlash);

  /* Check the parameters */
240
  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
Dave Hylands's avatar
Dave Hylands committed
241
242
243
244
245
246
247
248
249
250

  /* Enable End of FLASH Operation interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  
  /* Enable Error source interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);

  pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  pFlash.Address = Address;

251
  if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
Dave Hylands's avatar
Dave Hylands committed
252
253
254
255
  {
    /*Program byte (8-bit) at a specified address.*/
      FLASH_Program_Byte(Address, (uint8_t) Data);
  }
256
  else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
Dave Hylands's avatar
Dave Hylands committed
257
258
259
260
  {
    /*Program halfword (16-bit) at a specified address.*/
    FLASH_Program_HalfWord(Address, (uint16_t) Data);
  }
261
  else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
Dave Hylands's avatar
Dave Hylands committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  {
    /*Program word (32-bit) at a specified address.*/
    FLASH_Program_Word(Address, (uint32_t) Data);
  }
  else
  {
    /*Program double word (64-bit) at a specified address.*/
    FLASH_Program_DoubleWord(Address, Data);
  }

  return status;
}

/**
  * @brief This function handles FLASH interrupt request.
  * @retval None
  */
void HAL_FLASH_IRQHandler(void)
{
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  uint32_t addresstmp = 0U;
  
  /* Check FLASH operation error flags */
  if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
    FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  {
    if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
    {
      /*return the faulty sector*/
      addresstmp = pFlash.Sector;
      pFlash.Sector = 0xFFFFFFFFU;
    }
    else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
    {
      /*return the faulty bank*/
      addresstmp = pFlash.Bank;
    }
    else
    {
      /*return the faulty address*/
      addresstmp = pFlash.Address;
    }
    
    /*Save the Error code*/
    FLASH_SetErrorCode();
    
    /* FLASH error interrupt user callback */
    HAL_FLASH_OperationErrorCallback(addresstmp);
    
    /*Stop the procedure ongoing*/
    pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  }
Dave Hylands's avatar
Dave Hylands committed
313
314
315
316
  
  /* Check FLASH End of Operation flag  */
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  {
317
318
319
    /* Clear FLASH End of Operation pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
    
Dave Hylands's avatar
Dave Hylands committed
320
321
322
323
    if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
    {
      /*Nb of sector to erased can be decreased*/
      pFlash.NbSectorsToErase--;
324
      
Dave Hylands's avatar
Dave Hylands committed
325
      /* Check if there are still sectors to erase*/
326
      if(pFlash.NbSectorsToErase != 0U)
Dave Hylands's avatar
Dave Hylands committed
327
      {
328
        addresstmp = pFlash.Sector;
Dave Hylands's avatar
Dave Hylands committed
329
        /*Indicate user which sector has been erased*/
330
331
        HAL_FLASH_EndOfOperationCallback(addresstmp);
        
Dave Hylands's avatar
Dave Hylands committed
332
        /*Increment sector number*/
333
334
335
        pFlash.Sector++;
        addresstmp = pFlash.Sector;
        FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
Dave Hylands's avatar
Dave Hylands committed
336
337
338
339
340
      }
      else
      {
        /*No more sectors to Erase, user callback can be called.*/
        /*Reset Sector and stop Erase sectors procedure*/
341
        pFlash.Sector = addresstmp = 0xFFFFFFFFU;
Dave Hylands's avatar
Dave Hylands committed
342
        pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
343
344
345
346
        
        /* Flush the caches to be sure of the data consistency */
        FLASH_FlushCaches() ;
                
Dave Hylands's avatar
Dave Hylands committed
347
        /* FLASH EOP interrupt user callback */
348
        HAL_FLASH_EndOfOperationCallback(addresstmp);
Dave Hylands's avatar
Dave Hylands committed
349
350
351
352
      }
    }
    else 
    {
353
      if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) 
Dave Hylands's avatar
Dave Hylands committed
354
      {
355
356
357
358
        /* MassErase ended. Return the selected bank */
        /* Flush the caches to be sure of the data consistency */
        FLASH_FlushCaches() ;

Dave Hylands's avatar
Dave Hylands committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
      }
      else
      {
        /*Program ended. Return the selected address*/
        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(pFlash.Address);
      }
      pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
    }
  }
  
  if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  {
374
375
376
    /* Operation is completed, disable the PG, SER, SNB and MER Bits */
    CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));

Dave Hylands's avatar
Dave Hylands committed
377
378
    /* Disable End of FLASH Operation interrupt */
    __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
379
    
Dave Hylands's avatar
Dave Hylands committed
380
381
    /* Disable Error source interrupt */
    __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
382
    
Dave Hylands's avatar
Dave Hylands committed
383
384
385
386
387
388
389
390
    /* Process Unlocked */
    __HAL_UNLOCK(&pFlash);
  }
}

/**
  * @brief  FLASH end of operation interrupt callback
  * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
391
392
  *                  Mass Erase: Bank number which has been requested to erase
  *                  Sectors Erase: Sector which has been erased 
393
  *                    (if 0xFFFFFFFFU, it means that all the selected sectors have been erased)
394
395
  *                  Program: Address which was selected for data program
  * @retval None
Dave Hylands's avatar
Dave Hylands committed
396
397
398
  */
__weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
{
399
400
  /* Prevent unused argument(s) compilation warning */
  UNUSED(ReturnValue);
Dave Hylands's avatar
Dave Hylands committed
401
402
403
404
405
406
407
408
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
   */ 
}

/**
  * @brief  FLASH operation error interrupt callback
  * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
409
410
411
412
  *                 Mass Erase: Bank number which has been requested to erase
  *                 Sectors Erase: Sector number which returned an error
  *                 Program: Address which was selected for data program
  * @retval None
Dave Hylands's avatar
Dave Hylands committed
413
414
415
  */
__weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
{
416
417
  /* Prevent unused argument(s) compilation warning */
  UNUSED(ReturnValue);
Dave Hylands's avatar
Dave Hylands committed
418
419
420
421
422
423
424
425
426
  /* NOTE : This function Should not be modified, when the callback is needed,
            the HAL_FLASH_OperationErrorCallback could be implemented in the user file
   */ 
}

/**
  * @}
  */

427
/** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions 
Dave Hylands's avatar
Dave Hylands committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
 *  @brief   management functions 
 *
@verbatim   
 ===============================================================================
                      ##### Peripheral Control functions #####
 ===============================================================================  
    [..]
    This subsection provides a set of functions allowing to control the FLASH 
    memory operations.

@endverbatim
  * @{
  */

/**
  * @brief  Unlock the FLASH control register access
444
  * @retval HAL Status
Dave Hylands's avatar
Dave Hylands committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  */
HAL_StatusTypeDef HAL_FLASH_Unlock(void)
{
  if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  {
    /* Authorize the FLASH Registers access */
    FLASH->KEYR = FLASH_KEY1;
    FLASH->KEYR = FLASH_KEY2;
  }
  else
  {
    return HAL_ERROR;
  }
  
  return HAL_OK; 
}

/**
  * @brief  Locks the FLASH control register access
464
  * @retval HAL Status
Dave Hylands's avatar
Dave Hylands committed
465
466
467
468
469
470
471
472
473
474
475
  */
HAL_StatusTypeDef HAL_FLASH_Lock(void)
{
  /* Set the LOCK Bit to lock the FLASH Registers access */
  FLASH->CR |= FLASH_CR_LOCK;
  
  return HAL_OK;  
}

/**
  * @brief  Unlock the FLASH Option Control Registers access.
476
  * @retval HAL Status
Dave Hylands's avatar
Dave Hylands committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  */
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
{
  if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
  {
    /* Authorizes the Option Byte register programming */
    FLASH->OPTKEYR = FLASH_OPT_KEY1;
    FLASH->OPTKEYR = FLASH_OPT_KEY2;
  }
  else
  {
    return HAL_ERROR;
  }  
  
  return HAL_OK;  
}

/**
  * @brief  Lock the FLASH Option Control Registers access.
496
  * @retval HAL Status 
Dave Hylands's avatar
Dave Hylands committed
497
498
499
500
501
502
503
504
505
506
507
  */
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
{
  /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
  FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
  
  return HAL_OK;  
}

/**
  * @brief  Launch the option byte loading.
508
  * @retval HAL Status
Dave Hylands's avatar
Dave Hylands committed
509
510
511
512
513
514
515
  */
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
{
  /* Set the OPTSTRT bit in OPTCR register */
  *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;

  /* Wait for last operation to be completed */
516
  return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)); 
Dave Hylands's avatar
Dave Hylands committed
517
518
519
520
521
522
}

/**
  * @}
  */

523
/** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions 
Dave Hylands's avatar
Dave Hylands committed
524
525
526
527
528
529
530
 *  @brief   Peripheral Errors functions 
 *
@verbatim   
 ===============================================================================
                ##### Peripheral Errors functions #####
 ===============================================================================  
    [..]
531
    This subsection permits to get in run-time Errors of the FLASH peripheral.
Dave Hylands's avatar
Dave Hylands committed
532
533
534
535
536
537
538

@endverbatim
  * @{
  */

/**
  * @brief  Get the specific FLASH error flag.
539
540
541
542
543
544
545
546
547
  * @retval FLASH_ErrorCode: The returned value can be a combination of:
  *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
  *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag 
  *            @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag  
  *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
  *            @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag 
  */
uint32_t HAL_FLASH_GetError(void)
Dave Hylands's avatar
Dave Hylands committed
548
549
550
551
552
553
554
555
556
557
558
{ 
   return pFlash.ErrorCode;
}  
  
/**
  * @}
  */    

/**
  * @brief  Wait for a FLASH operation to complete.
  * @param  Timeout: maximum flash operationtimeout
559
  * @retval HAL Status
Dave Hylands's avatar
Dave Hylands committed
560
561
562
  */
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{ 
563
564
565
566
567
  uint32_t tickstart = 0U;
  
  /* Clear Error Code */
  pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  
Dave Hylands's avatar
Dave Hylands committed
568
569
570
  /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
     Even if the FLASH operation fails, the BUSY flag will be reset and an error
     flag will be set */
571
572
573
  /* Get tick */
  tickstart = HAL_GetTick();

Dave Hylands's avatar
Dave Hylands committed
574
575
576
577
  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) 
  { 
    if(Timeout != HAL_MAX_DELAY)
    {
578
      if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
Dave Hylands's avatar
Dave Hylands committed
579
580
581
582
583
      {
        return HAL_TIMEOUT;
      }
    } 
  }
584
585
586
587
588
589
590

  /* Check FLASH End of Operation flag  */
  if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  {
    /* Clear FLASH End of Operation pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  }
Dave Hylands's avatar
Dave Hylands committed
591
592
593
594
595
596
597
598
599
  
  if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
                           FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  {
    /*Save the error code*/
    FLASH_SetErrorCode();
    return HAL_ERROR;
  }

600
  /* If there is no error flag set */
Dave Hylands's avatar
Dave Hylands committed
601
602
603
604
605
606
607
  return HAL_OK;
  
}  

/**
  * @brief  Program a double word (64-bit) at a specified address.
  * @note   This function must be used when the device voltage range is from
608
  *         2.7V to 3.6V and Vpp in the range 7V to 9V.
Dave Hylands's avatar
Dave Hylands committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  *
  * @note   If an erase and a program operations are requested simultaneously,    
  *         the erase operation is performed before the program one.
  *  
  * @param  Address: specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed.
  * @retval None
  */
static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
  /* Check the parameters */
  assert_param(IS_FLASH_ADDRESS(Address));
  
  /* If the previous operation is completed, proceed to program the new data */
623
  CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
Dave Hylands's avatar
Dave Hylands committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
  FLASH->CR |= FLASH_CR_PG;

  *(__IO uint64_t*)Address = Data;
}


/**
  * @brief  Program word (32-bit) at a specified address.
  * @note   This function must be used when the device voltage range is from
  *         2.7V to 3.6V.
  *
  * @note   If an erase and a program operations are requested simultaneously,    
  *         the erase operation is performed before the program one.
  *  
  * @param  Address: specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed.
  * @retval None
  */
static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
{
  /* Check the parameters */
  assert_param(IS_FLASH_ADDRESS(Address));
  
  /* If the previous operation is completed, proceed to program the new data */
649
  CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
Dave Hylands's avatar
Dave Hylands committed
650
651
652
653
654
655
656
657
658
  FLASH->CR |= FLASH_PSIZE_WORD;
  FLASH->CR |= FLASH_CR_PG;

  *(__IO uint32_t*)Address = Data;
}

/**
  * @brief  Program a half-word (16-bit) at a specified address.
  * @note   This function must be used when the device voltage range is from
659
  *         2.1V to 3.6V.
Dave Hylands's avatar
Dave Hylands committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  *
  * @note   If an erase and a program operations are requested simultaneously,    
  *         the erase operation is performed before the program one.
  *  
  * @param  Address: specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed.
  * @retval None
  */
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
{
  /* Check the parameters */
  assert_param(IS_FLASH_ADDRESS(Address));
  
  /* If the previous operation is completed, proceed to program the new data */
674
  CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
Dave Hylands's avatar
Dave Hylands committed
675
676
677
678
679
680
681
682
683
  FLASH->CR |= FLASH_PSIZE_HALF_WORD;
  FLASH->CR |= FLASH_CR_PG;

  *(__IO uint16_t*)Address = Data;
}

/**
  * @brief  Program byte (8-bit) at a specified address.
  * @note   This function must be used when the device voltage range is from
684
  *         1.8V to 3.6V.
Dave Hylands's avatar
Dave Hylands committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  *
  * @note   If an erase and a program operations are requested simultaneously,    
  *         the erase operation is performed before the program one.
  *  
  * @param  Address: specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed.
  * @retval None
  */
static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
{
  /* Check the parameters */
  assert_param(IS_FLASH_ADDRESS(Address));
  
  /* If the previous operation is completed, proceed to program the new data */
699
  CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
Dave Hylands's avatar
Dave Hylands committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
  FLASH->CR |= FLASH_PSIZE_BYTE;
  FLASH->CR |= FLASH_CR_PG;

  *(__IO uint8_t*)Address = Data;
}

/**
  * @brief  Set the specific FLASH error flag.
  * @retval None
  */
static void FLASH_SetErrorCode(void)
{ 
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
  {
714
715
716
717
   pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
   
   /* Clear FLASH write protection error pending bit */
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR);
Dave Hylands's avatar
Dave Hylands committed
718
719
720
721
  }
  
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
  {
722
723
724
725
   pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
   
   /* Clear FLASH Programming alignment error pending bit */
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
Dave Hylands's avatar
Dave Hylands committed
726
727
728
729
  }
  
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
  {
730
731
732
733
    pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
    
    /* Clear FLASH Programming parallelism error pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
Dave Hylands's avatar
Dave Hylands committed
734
735
736
737
  }
  
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
  {
738
739
740
741
    pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
    
    /* Clear FLASH Programming sequence error pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
Dave Hylands's avatar
Dave Hylands committed
742
743
744
745
  }
  
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
  {
746
747
748
749
    pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
    
    /* Clear FLASH Proprietary readout protection error pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
Dave Hylands's avatar
Dave Hylands committed
750
751
752
753
  }
  
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
  {
754
755
756
757
    pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
    
    /* Clear FLASH Operation error pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
Dave Hylands's avatar
Dave Hylands committed
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
  }
}

/**
  * @}
  */

#endif /* HAL_FLASH_MODULE_ENABLED */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/