Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f1xx_hal_flash.c
4
  * @author  MCD Application Team
5
  * @version V1.0.1
6
  * @date    31-July-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 State functions
13
  *        
14
  @verbatim
15
  ==============================================================================
16
                        ##### FLASH peripheral features #####
17
  ==============================================================================
18
  [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
19
       to the Flash memory. It implements the erase and program Flash memory operations
20
       and the read and write protection mechanisms.
21
 
22
  [..] The Flash memory interface accelerates code execution with a system of instruction
23
      prefetch.
24
 
25
  [..] The FLASH main features are:
26
      (+) Flash memory read operations
27
      (+) Flash memory program/erase operations
28
      (+) Read / write protections
29
      (+) Prefetch on I-Code
30
      (+) Option Bytes programming
31
 
32
 
33
                     ##### How to use this driver #####
34
  ==============================================================================
35
  [..]                            
36
      This driver provides functions and macros to configure and program the FLASH
37
      memory of all STM32F1xx devices.
38
 
39
      (#) FLASH Memory I/O Programming functions: this group includes all needed
40
          functions to erase and program the main memory:
41
        (++) Lock and Unlock the FLASH interface
42
        (++) Erase function: Erase page, erase all pages
43
        (++) Program functions: half word, word and doubleword
44
 
45
      (#) FLASH Option Bytes Programming functions: this group includes all needed
46
          functions to manage the Option Bytes:
47
        (++) Lock and Unlock the Option Bytes
48
        (++) Set/Reset the write protection
49
        (++) Set the Read protection Level
50
        (++) Program the user Option Bytes
51
        (++) Launch the Option Bytes loader
52
        (++) Erase Option Bytes
53
        (++) Program the data Option Bytes
54
        (++) Get the Write protection.
55
        (++) Get the user option bytes.
56
 
57
      (#) Interrupts and flags management functions : this group
58
          includes all needed functions to:
59
        (++) Handle FLASH interrupts
60
        (++) Wait for last FLASH operation according to its status
61
        (++) Get error flag status          
62
 
63
  [..] In addition to these function, this driver includes a set of macros allowing
64
       to handle the following operations:
65
 
66
      (+) Set the latency
67
      (+) Enable/Disable the prefetch buffer
68
      (+) Enable/Disable the half cycle access
69
      (+) Enable/Disable the FLASH interrupts
70
      (+) Monitor the FLASH flags status
71
 
72
  @endverbatim
73
  ******************************************************************************
74
  * @attention
75
  *
76
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
77
  *
78
  * Redistribution and use in source and binary forms, with or without modification,
79
  * are permitted provided that the following conditions are met:
80
  *   1. Redistributions of source code must retain the above copyright notice,
81
  *      this list of conditions and the following disclaimer.
82
  *   2. Redistributions in binary form must reproduce the above copyright notice,
83
  *      this list of conditions and the following disclaimer in the documentation
84
  *      and/or other materials provided with the distribution.
85
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
86
  *      may be used to endorse or promote products derived from this software
87
  *      without specific prior written permission.
88
  *
89
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
90
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
92
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
93
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
95
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
96
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
98
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99
  *
100
  ******************************************************************************  
101
  */
102
 
103
/* Includes ------------------------------------------------------------------*/
104
#include "stm32f1xx_hal.h"
105
 
106
/** @addtogroup STM32F1xx_HAL_Driver
107
  * @{
108
  */
109
 
110
#ifdef HAL_FLASH_MODULE_ENABLED
111
 
112
/** @defgroup FLASH FLASH
113
  * @brief FLASH HAL module driver
114
  * @{
115
  */
116
 
117
/* Private typedef -----------------------------------------------------------*/
118
/* Private define ------------------------------------------------------------*/
119
/** @defgroup FLASH_Private_Constants FLASH Private Constants
120
  * @{
121
  */
122
/**
123
  * @}
124
  */
125
 
126
/* Private macro ---------------------------- ---------------------------------*/
127
/** @defgroup FLASH_Private_Macros FLASH Private Macros
128
  * @{
129
  */
130
 
131
/**
132
  * @}
133
  */
134
 
135
/* Private variables ---------------------------------------------------------*/
136
/** @defgroup FLASH_Private_Variables FLASH Private Variables
137
  * @{
138
  */
139
/* Variables used for Erase pages under interruption*/
140
FLASH_ProcessTypeDef pFlash;
141
/**
142
  * @}
143
  */
144
 
145
/* Private function prototypes -----------------------------------------------*/
146
/** @defgroup FLASH_Private_Functions FLASH Private Functions
147
  * @{
148
  */
149
static  void   FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
150
static  void   FLASH_SetErrorCode(void);
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
@endverbatim
165
  * @{
166
  */
167
 
168
/**
169
  * @brief  Program halfword, word or double word at a specified address
170
  * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
171
  *         The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
172
  *
173
  * @note   If an erase and a program operations are requested simultaneously,    
174
  *         the erase operation is performed before the program one.
175
  *  
176
  * @note   FLASH should be previously erased before new programmation (only exception to this
177
  *         is when 0x0000 is programmed)
178
  *  
179
  * @param  TypeProgram:  Indicate the way to program at a specified address.
180
  *                       This parameter can be a value of @ref FLASH_Type_Program
181
  * @param  Address:      Specifies the address to be programmed.
182
  * @param  Data:         Specifies the data to be programmed
183
  *
184
  * @retval HAL_StatusTypeDef HAL Status
185
  */
186
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
187
{
188
  HAL_StatusTypeDef status = HAL_ERROR;
189
  uint8_t index = 0;
190
  uint8_t nbiterations = 0;
191
 
192
  /* Process Locked */
193
  __HAL_LOCK(&pFlash);
194
 
195
  /* Check the parameters */
196
  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
197
  assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
198
 
199
#if defined(FLASH_BANK2_END)
200
  if(Address <= FLASH_BANK1_END)
201
  {
202
#endif /* FLASH_BANK2_END */
203
    /* Wait for last operation to be completed */
204
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
205
#if defined(FLASH_BANK2_END)
206
  }
207
  else
208
  {
209
    /* Wait for last operation to be completed */
210
    status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE);
211
  }
212
#endif /* FLASH_BANK2_END */
213
 
214
  if(status == HAL_OK)
215
  {
216
    if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
217
    {
218
      /* Program halfword (16-bit) at a specified address. */
219
      nbiterations = 1;
220
    }
221
    else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
222
    {
223
      /* Program word (32-bit = 2*16-bit) at a specified address. */
224
      nbiterations = 2;
225
    }
226
    else
227
    {
228
      /* Program double word (64-bit = 4*16-bit) at a specified address. */
229
      nbiterations = 4;
230
    }
231
 
232
    for (index = 0; index < nbiterations; index++)
233
    {
234
      FLASH_Program_HalfWord((Address + (2*index)), (uint16_t)(Data >> (16*index)));
235
 
236
#if defined(FLASH_BANK2_END)
237
      if(Address <= FLASH_BANK1_END)
238
      {
239
#endif /* FLASH_BANK2_END */
240
        /* Wait for last operation to be completed */
241
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
242
 
243
        /* If the program operation is completed, disable the PG Bit */
244
        CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
245
#if defined(FLASH_BANK2_END)
246
      }
247
      else
248
      {
249
        /* Wait for last operation to be completed */
250
        status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE);
251
 
252
        /* If the program operation is completed, disable the PG Bit */
253
        CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG);
254
      }
255
#endif /* FLASH_BANK2_END */
256
      /* In case of error, stop programation procedure */
257
      if (status != HAL_OK)
258
      {
259
        break;
260
      }
261
    }
262
  }
263
 
264
  /* Process Unlocked */
265
  __HAL_UNLOCK(&pFlash);
266
 
267
  return status;
268
}
269
 
270
/**
271
  * @brief  Program halfword, word or double word at a specified address  with interrupt enabled.
272
  * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
273
  *         The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
274
  *
275
  * @note   If an erase and a program operations are requested simultaneously,    
276
  *         the erase operation is performed before the program one.
277
  *  
278
  * @param  TypeProgram: Indicate the way to program at a specified address.
279
  *                      This parameter can be a value of @ref FLASH_Type_Program
280
  * @param  Address:     Specifies the address to be programmed.
281
  * @param  Data:        Specifies the data to be programmed
282
  *
283
  * @retval HAL_StatusTypeDef HAL Status
284
  */
285
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
286
{
287
  HAL_StatusTypeDef status = HAL_OK;
288
 
289
  /* Process Locked */
290
  __HAL_LOCK(&pFlash);
291
 
292
  /* Check the parameters */
293
  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
294
  assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
295
 
296
#if defined(FLASH_BANK2_END)
297
  /* If procedure already ongoing, reject the next one */
298
  if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
299
  {
300
    return HAL_ERROR;
301
  }
302
 
303
  if(Address <= FLASH_BANK1_END)
304
  {
305
    /* Enable End of FLASH Operation and Error source interrupts */
306
    __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1);
307
 
308
  }else
309
  {
310
    /* Enable End of FLASH Operation and Error source interrupts */
311
    __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
312
  }
313
#else
314
  /* Enable End of FLASH Operation and Error source interrupts */
315
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
316
#endif /* FLASH_BANK2_END */
317
 
318
  pFlash.Address = Address;
319
  pFlash.Data = Data;
320
 
321
  if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
322
  {
323
    pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
324
    /*Program halfword (16-bit) at a specified address.*/
325
    pFlash.DataRemaining = 1;
326
  }
327
  else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
328
  {
329
    pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
330
    /*Program word (32-bit : 2*16-bit) at a specified address.*/
331
    pFlash.DataRemaining = 2;
332
  }
333
  else
334
  {
335
    pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
336
    /*Program double word (64-bit : 4*16-bit) at a specified address.*/
337
    pFlash.DataRemaining = 4;
338
  }
339
 
340
  /*Program halfword (16-bit) at a specified address.*/
341
  FLASH_Program_HalfWord(Address, (uint16_t)Data);
342
 
343
  return status;
344
}
345
 
346
/**
347
  * @brief This function handles FLASH interrupt request.
348
  * @retval None
349
  */
350
void HAL_FLASH_IRQHandler(void)
351
{
352
  uint32_t addresstmp = 0;
353
 
354
  /* Check FLASH operation error flags */
355
#if defined(FLASH_BANK2_END)
356
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK1) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK1) || \
357
    (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)))
358
#else
359
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
360
#endif /* FLASH_BANK2_END */
361
  {
362
    /*Save the Error code*/
363
    FLASH_SetErrorCode();
364
 
365
    /* FLASH error interrupt user callback */
366
    HAL_FLASH_OperationErrorCallback(pFlash.Address);
367
 
368
    /* Reset address and stop the procedure ongoing*/
369
    pFlash.Address = 0xFFFFFFFF;
370
    pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
371
  }
372
 
373
  /* Check FLASH End of Operation flag  */
374
#if defined(FLASH_BANK2_END)
375
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK1))
376
  {
377
    /* Clear FLASH End of Operation pending bit */
378
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK1);
379
#else
380
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
381
  {
382
    /* Clear FLASH End of Operation pending bit */
383
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
384
#endif /* FLASH_BANK2_END */
385
 
386
    /* Process can continue only if no error detected */
387
    if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
388
    {
389
      if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
390
      {
391
        /* Nb of pages to erased can be decreased */
392
        pFlash.DataRemaining--;
393
 
394
        /* Indicate user which page address has been erased*/
395
        HAL_FLASH_EndOfOperationCallback(pFlash.Address);
396
 
397
        /* Check if there are still pages to erase*/
398
        if(pFlash.DataRemaining != 0)
399
        {
400
          /* Increment page address to next page */
401
          pFlash.Address += FLASH_PAGE_SIZE;
402
          addresstmp = pFlash.Address;
403
 
404
          /* Operation is completed, disable the PER Bit */
405
          CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
406
 
407
          FLASH_PageErase(addresstmp);
408
        }
409
        else
410
        {
411
          /*No more pages to Erase*/
412
 
413
          /*Reset Address and stop Erase pages procedure*/
414
          pFlash.Address = 0xFFFFFFFF;
415
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
416
        }
417
      }
418
      else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
419
      {
420
        /* Operation is completed, disable the MER Bit */
421
        CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
422
 
423
#if defined(FLASH_BANK2_END)
424
        /* Stop Mass Erase procedure if no pending mass erase on other bank */
425
        if (HAL_IS_BIT_CLR(FLASH->CR2, FLASH_CR2_MER))
426
        {
427
#endif /* FLASH_BANK2_END */
428
          /* MassErase ended. Return the selected bank*/
429
          /* FLASH EOP interrupt user callback */
430
          HAL_FLASH_EndOfOperationCallback(0);
431
 
432
          /* Stop Mass Erase procedure*/
433
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
434
        }
435
#if defined(FLASH_BANK2_END)
436
      }
437
#endif /* FLASH_BANK2_END */
438
      else
439
      {
440
        /* Nb of 16-bit data to program can be decreased */
441
        pFlash.DataRemaining--;
442
 
443
        /* Check if there are still 16-bit data to program */
444
        if(pFlash.DataRemaining != 0)
445
        {
446
          /* Increment address to 16-bit */
447
          pFlash.Address += 2;
448
          addresstmp = pFlash.Address;
449
 
450
          /* Shift to have next 16-bit data */
451
          pFlash.Data = (pFlash.Data >> 16);
452
 
453
          /* Operation is completed, disable the PG Bit */
454
          CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
455
 
456
          /*Program halfword (16-bit) at a specified address.*/
457
          FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
458
        }
459
        else
460
        {
461
          /*Program ended. Return the selected address*/
462
          /* FLASH EOP interrupt user callback */
463
          if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
464
          {
465
            HAL_FLASH_EndOfOperationCallback(pFlash.Address);
466
          }
467
          else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
468
          {
469
            HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2);
470
          }
471
          else
472
          {
473
            HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6);
474
          }
475
 
476
          /* Reset Address and stop Program procedure*/
477
          pFlash.Address = 0xFFFFFFFF;
478
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
479
        }
480
      }
481
    }
482
  }
483
 
484
#if defined(FLASH_BANK2_END)
485
  /* Check FLASH End of Operation flag  */
486
  if(__HAL_FLASH_GET_FLAG( FLASH_FLAG_EOP_BANK2))
487
  {
488
    /* Clear FLASH End of Operation pending bit */
489
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2);
490
 
491
    /* Process can continue only if no error detected */
492
    if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
493
    {
494
      if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
495
      {
496
        /* Nb of pages to erased can be decreased */
497
        pFlash.DataRemaining--;
498
 
499
        /* Indicate user which page address has been erased*/
500
        HAL_FLASH_EndOfOperationCallback(pFlash.Address);
501
 
502
        /* Check if there are still pages to erase*/
503
        if(pFlash.DataRemaining != 0)
504
        {
505
          /* Increment page address to next page */
506
          pFlash.Address += FLASH_PAGE_SIZE;
507
          addresstmp = pFlash.Address;
508
 
509
          /* Operation is completed, disable the PER Bit */
510
          CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER);
511
 
512
          FLASH_PageErase(addresstmp);
513
        }
514
        else
515
        {
516
          /*No more pages to Erase*/
517
 
518
          /*Reset Address and stop Erase pages procedure*/
519
          pFlash.Address = 0xFFFFFFFF;
520
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
521
        }
522
      }
523
      else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
524
      {
525
        /* Operation is completed, disable the MER Bit */
526
        CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
527
 
528
        if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_MER))
529
        {
530
          /* MassErase ended. Return the selected bank*/
531
          /* FLASH EOP interrupt user callback */
532
          HAL_FLASH_EndOfOperationCallback(0);
533
 
534
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
535
        }
536
      }
537
      else
538
      {
539
        /* Nb of 16-bit data to program can be decreased */
540
        pFlash.DataRemaining--;
541
 
542
        /* Check if there are still 16-bit data to program */
543
        if(pFlash.DataRemaining != 0)
544
        {
545
          /* Increment address to 16-bit */
546
          pFlash.Address += 2;
547
          addresstmp = pFlash.Address;
548
 
549
          /* Shift to have next 16-bit data */
550
          pFlash.Data = (pFlash.Data >> 16);
551
 
552
          /* Operation is completed, disable the PG Bit */
553
          CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG);
554
 
555
          /*Program halfword (16-bit) at a specified address.*/
556
          FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
557
        }
558
        else
559
        {
560
          /*Program ended. Return the selected address*/
561
          /* FLASH EOP interrupt user callback */
562
          if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
563
          {
564
            HAL_FLASH_EndOfOperationCallback(pFlash.Address);
565
          }
566
          else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
567
          {
568
            HAL_FLASH_EndOfOperationCallback(pFlash.Address-2);
569
          }
570
          else
571
          {
572
            HAL_FLASH_EndOfOperationCallback(pFlash.Address-6);
573
          }
574
 
575
          /* Reset Address and stop Program procedure*/
576
          pFlash.Address = 0xFFFFFFFF;
577
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
578
        }
579
      }
580
    }
581
  }
582
#endif 
583
 
584
  if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
585
  {
586
#if defined(FLASH_BANK2_END)
587
    /* Operation is completed, disable the PG, PER and MER Bits for both bank */
588
    CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
589
    CLEAR_BIT(FLASH->CR2, (FLASH_CR2_PG | FLASH_CR2_PER | FLASH_CR2_MER));  
590
 
591
    /* Disable End of FLASH Operation and Error source interrupts for both banks */
592
    __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1 | FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
593
#else
594
    /* Operation is completed, disable the PG, PER and MER Bits */
595
    CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
596
 
597
    /* Disable End of FLASH Operation and Error source interrupts */
598
    __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
599
#endif /* FLASH_BANK2_END */
600
 
601
    /* Process Unlocked */
602
    __HAL_UNLOCK(&pFlash);
603
  }
604
}
605
 
606
 
607
/**
608
  * @brief  FLASH end of operation interrupt callback
609
  * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
610
  *                 - Mass Erase: No return value expected
611
  *                 - Pages Erase: Address of the page which has been erased
612
  *                 - Program: Address which was selected for data program
613
  * @retval none
614
  */
615
__weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
616
{
617
  /* NOTE : This function Should not be modified, when the callback is needed,
618
            the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
619
   */
620
}
621
 
622
/**
623
  * @brief  FLASH operation error interrupt callback
624
  * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
625
  *                 - Mass Erase: No return value expected
626
  *                 - Pages Erase: Address of the page which returned an error
627
  *                 - Program: Address which was selected for data program
628
  * @retval none
629
  */
630
__weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
631
{
632
  /* NOTE : This function Should not be modified, when the callback is needed,
633
            the HAL_FLASH_OperationErrorCallback could be implemented in the user file
634
   */
635
}
636
 
637
/**
638
  * @}
639
  */
640
 
641
/** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
642
 *  @brief   management functions
643
 *
644
@verbatim  
645
 ===============================================================================
646
                      ##### Peripheral Control functions #####
647
 ===============================================================================  
648
    [..]
649
    This subsection provides a set of functions allowing to control the FLASH
650
    memory operations.
651
 
652
@endverbatim
653
  * @{
654
  */
655
 
656
/**
657
  * @brief  Unlock the FLASH control register access
658
  * @retval HAL Status
659
  */
660
HAL_StatusTypeDef HAL_FLASH_Unlock(void)
661
{
662
  if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
663
  {
664
    /* Authorize the FLASH Registers access */
665
    WRITE_REG(FLASH->KEYR, FLASH_KEY1);
666
    WRITE_REG(FLASH->KEYR, FLASH_KEY2);
667
  }
668
  else
669
  {
670
    return HAL_ERROR;
671
  }
672
 
673
#if defined(FLASH_BANK2_END)
674
  if (HAL_IS_BIT_SET(FLASH->CR2, FLASH_CR2_LOCK))
675
  {
676
    /* Authorize the FLASH BANK2 Registers access */
677
    WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
678
    WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
679
  }
680
  else
681
  {
682
    return HAL_ERROR;
683
  }
684
 
685
#endif /* FLASH_BANK2_END */
686
  return HAL_OK;
687
}
688
 
689
/**
690
  * @brief  Locks the FLASH control register access
691
  * @retval HAL Status
692
  */
693
HAL_StatusTypeDef HAL_FLASH_Lock(void)
694
{
695
  /* Set the LOCK Bit to lock the FLASH Registers access */
696
  SET_BIT(FLASH->CR, FLASH_CR_LOCK);
697
 
698
#if defined(FLASH_BANK2_END)
699
  /* Set the LOCK Bit to lock the FLASH BANK2 Registers access */
700
  SET_BIT(FLASH->CR2, FLASH_CR2_LOCK);
701
#endif /* FLASH_BANK2_END */
702
 
703
  return HAL_OK;  
704
}
705
 
706
 
707
/**
708
  * @brief  Unlock the FLASH Option Control Registers access.
709
  * @retval HAL Status
710
  */
711
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
712
{
713
  if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
714
  {
715
    /* Authorizes the Option Byte register programming */
716
    WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
717
    WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
718
  }
719
  else
720
  {
721
    return HAL_ERROR;
722
  }  
723
 
724
  return HAL_OK;  
725
}
726
 
727
/**
728
  * @brief  Lock the FLASH Option Control Registers access.
729
  * @retval HAL Status
730
  */
731
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
732
{
733
  /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
734
  CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
735
 
736
  return HAL_OK;  
737
}
738
 
739
/**
740
  * @brief  Launch the option byte loading.
741
  * @note   This function will reset automatically the MCU.
742
  * @retval HAL_StatusTypeDef HAL Status
743
  */
744
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
745
{
746
  /* Initiates a system reset request to launch the option byte loading */
747
  HAL_NVIC_SystemReset();
748
 
749
  return HAL_OK;  
750
}
751
 
752
/**
753
  * @}
754
  */  
755
 
756
/** @defgroup FLASH_Exported_Functions_Group3 Peripheral State functions
757
 *  @brief   Peripheral State functions
758
 *
759
@verbatim  
760
 ===============================================================================
761
                      ##### Peripheral State functions #####
762
 ===============================================================================  
763
    [..]
764
    This subsection permit to get in run-time the status of the FLASH peripheral.
765
 
766
@endverbatim
767
  * @{
768
  */
769
 
770
/**
771
  * @brief  Get the specific FLASH error flag.
772
  * @retval FLASH_ErrorCode: The returned value can be:
773
  *            @ref FLASH_Error_Codes
774
  */
775
uint32_t HAL_FLASH_GetError(void)
776
{
777
   return pFlash.ErrorCode;
778
}  
779
/**
780
  * @}
781
  */
782
 
783
/**
784
  * @}
785
  */
786
 
787
/** @addtogroup FLASH_Private_Functions
788
 * @{
789
 */
790
/**
791
  * @brief  Program a half-word (16-bit) at a specified address.
792
  * @param  Address: specifies the address to be programmed.
793
  * @param  Data: specifies the data to be programmed.
794
  * @retval None
795
  */
796
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
797
{
798
  /* Clean the error context */
799
  pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
800
 
801
#if defined(FLASH_BANK2_END)
802
  if(Address <= FLASH_BANK1_END)
803
  {
804
#endif /* FLASH_BANK2_END */
805
    /* Proceed to program the new data */
806
    SET_BIT(FLASH->CR, FLASH_CR_PG);
807
#if defined(FLASH_BANK2_END)
808
  }
809
  else
810
  {
811
    /* Proceed to program the new data */
812
    SET_BIT(FLASH->CR2, FLASH_CR2_PG);
813
  }
814
#endif /* FLASH_BANK2_END */
815
 
816
  /* Write data in the address */
817
  *(__IO uint16_t*)Address = Data;
818
}
819
 
820
/**
821
  * @brief  Wait for a FLASH operation to complete.
822
  * @param  Timeout: maximum flash operationtimeout
823
  * @retval HAL_StatusTypeDef HAL Status
824
  */
825
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
826
{
827
  /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
828
     Even if the FLASH operation fails, the BUSY flag will be reset and an error
829
     flag will be set */
830
 
831
  uint32_t tickstart = HAL_GetTick();
832
 
833
  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
834
  {
835
    if (Timeout != HAL_MAX_DELAY)
836
    {
837
      if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
838
      {
839
        return HAL_TIMEOUT;
840
      }
841
    }
842
  }
843
 
844
  /* Check FLASH End of Operation flag  */
845
  if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
846
  {
847
    /* Clear FLASH End of Operation pending bit */
848
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
849
  }
850
 
851
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || \
852
     __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
853
  {
854
    /*Save the error code*/
855
    FLASH_SetErrorCode();
856
    return HAL_ERROR;
857
  }
858
 
859
  /* If there is no error flag set */
860
  return HAL_OK;
861
 
862
}
863
 
864
#if defined(FLASH_BANK2_END)
865
/**
866
  * @brief  Wait for a FLASH BANK2 operation to complete.
867
  * @param  Timeout: maximum flash operation timeout
868
  * @retval HAL_StatusTypeDef HAL Status
869
  */
870
HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout)
871
{
872
  /* Wait for the FLASH BANK2 operation to complete by polling on BUSY flag to be reset.
873
     Even if the FLASH BANK2 operation fails, the BUSY flag will be reset and an error
874
     flag will be set */
875
 
876
  uint32_t tickstart = HAL_GetTick();
877
 
878
  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY_BANK2))
879
  {
880
    if (Timeout != HAL_MAX_DELAY)
881
    {
882
      if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
883
      {
884
        return HAL_TIMEOUT;
885
      }
886
    }
887
  }
888
 
889
  /* Check FLASH End of Operation flag  */
890
  if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK2))
891
  {
892
    /* Clear FLASH End of Operation pending bit */
893
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2);
894
  }
895
 
896
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))
897
  {
898
    /*Save the error code*/
899
    FLASH_SetErrorCode();
900
    return HAL_ERROR;
901
  }
902
 
903
  /* If there is an error flag set */
904
  return HAL_OK;
905
 
906
}
907
#endif /* FLASH_BANK2_END */
908
 
909
/**
910
  * @brief  Set the specific FLASH error flag.
911
  * @retval None
912
  */
913
static void FLASH_SetErrorCode(void)
914
{
915
#if defined(FLASH_BANK2_END)
916
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2))
917
#else
918
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
919
#endif /* FLASH_BANK2_END */
920
  {
921
    pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
922
  }
923
#if defined(FLASH_BANK2_END)
924
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))
925
#else
926
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
927
#endif /* FLASH_BANK2_END */
928
  {
929
     pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
930
  }
931
 
932
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
933
  {
934
     pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
935
     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
936
  }
937
 
938
  /* Clear FLASH error pending bits */
939
#if defined(FLASH_BANK2_END)
940
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGERR | FLASH_FLAG_PGERR_BANK2);
941
#else
942
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
943
#endif /* FLASH_BANK2_END */
944
}  
945
/**
946
  * @}
947
  */
948
 
949
/**
950
  * @}
951
  */
952
 
953
#endif /* HAL_FLASH_MODULE_ENABLED */
954
 
955
/**
956
  * @}
957
  */
958
 
959
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/