Subversion Repositories AFRtranscoder

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f1xx_hal_flash_ex.c
4
  * @author  MCD Application Team
5
  * @brief   Extended FLASH HAL module driver.
6
  *    
7
  *          This file provides firmware functions to manage the following
8
  *          functionalities of the FLASH peripheral:
9
  *           + Extended Initialization/de-initialization functions
10
  *           + Extended I/O operation functions
11
  *           + Extended Peripheral Control functions
12
  *        
13
  @verbatim
14
  ==============================================================================
15
               ##### Flash peripheral extended features  #####
16
  ==============================================================================
17
 
18
                      ##### How to use this driver #####
19
  ==============================================================================
20
  [..] This driver provides functions to configure and program the FLASH memory
21
       of all STM32F1xxx devices. It includes
22
 
23
        (++) Set/Reset the write protection
24
        (++) Program the user Option Bytes
25
        (++) Get the Read protection Level
26
 
27
  @endverbatim
28
  ******************************************************************************
29
  * @attention
30
  *
31
  * Copyright (c) 2016 STMicroelectronics.
32
  * All rights reserved.
33
  *
34
  * This software is licensed under terms that can be found in the LICENSE file in
35
  * the root directory of this software component.
36
  * If no LICENSE file comes with this software, it is provided AS-IS.
37
  ******************************************************************************
38
  */
39
 
40
/* Includes ------------------------------------------------------------------*/
41
#include "stm32f1xx_hal.h"
42
 
43
/** @addtogroup STM32F1xx_HAL_Driver
44
  * @{
45
  */
46
#ifdef HAL_FLASH_MODULE_ENABLED
47
 
48
/** @addtogroup FLASH
49
  * @{
50
  */
51
/** @addtogroup FLASH_Private_Variables
52
 * @{
53
 */
54
/* Variables used for Erase pages under interruption*/
55
extern FLASH_ProcessTypeDef pFlash;
56
/**
57
  * @}
58
  */
59
 
60
/**
61
  * @}
62
  */
63
 
64
/** @defgroup FLASHEx FLASHEx
65
  * @brief FLASH HAL Extension module driver
66
  * @{
67
  */
68
 
69
/* Private typedef -----------------------------------------------------------*/
70
/* Private define ------------------------------------------------------------*/
71
/** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
72
 * @{
73
 */
74
#define FLASH_POSITION_IWDGSW_BIT        FLASH_OBR_IWDG_SW_Pos
75
#define FLASH_POSITION_OB_USERDATA0_BIT  FLASH_OBR_DATA0_Pos
76
#define FLASH_POSITION_OB_USERDATA1_BIT  FLASH_OBR_DATA1_Pos
77
/**
78
  * @}
79
  */
80
 
81
/* Private macro -------------------------------------------------------------*/
82
/** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
83
  * @{
84
  */
85
/**
86
  * @}
87
  */
88
 
89
/* Private variables ---------------------------------------------------------*/
90
/* Private function prototypes -----------------------------------------------*/
91
/** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
92
 * @{
93
 */
94
/* Erase operations */
95
static void              FLASH_MassErase(uint32_t Banks);
96
void    FLASH_PageErase(uint32_t PageAddress);
97
 
98
/* Option bytes control */
99
static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage);
100
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage);
101
static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
102
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig);
103
static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
104
static uint32_t          FLASH_OB_GetWRP(void);
105
static uint32_t          FLASH_OB_GetRDP(void);
106
static uint8_t           FLASH_OB_GetUser(void);
107
 
108
/**
109
  * @}
110
  */
111
 
112
/* Exported functions ---------------------------------------------------------*/
113
/** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
114
  * @{
115
  */
116
 
117
/** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
118
 *  @brief   FLASH Memory Erasing functions
119
  *
120
@verbatim  
121
  ==============================================================================
122
                ##### FLASH Erasing Programming functions #####
123
  ==============================================================================
124
 
125
    [..] The FLASH Memory Erasing functions, includes the following functions:
126
    (+) HAL_FLASHEx_Erase: return only when erase has been done
127
    (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback
128
        is called with parameter 0xFFFFFFFF
129
 
130
    [..] Any operation of erase should follow these steps:
131
    (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
132
        program memory access.
133
    (#) Call the desired function to erase page.
134
    (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
135
       (recommended to protect the FLASH memory against possible unwanted operation).
136
 
137
@endverbatim
138
  * @{
139
  */
140
 
141
 
142
/**
143
  * @brief  Perform a mass erase or erase the specified FLASH memory pages
144
  * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
145
  *         must be called before.
146
  *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
147
  *         (recommended to protect the FLASH memory against possible unwanted operation)
148
  * @param[in]  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
149
  *         contains the configuration information for the erasing.
150
  *
151
  * @param[out]  PageError pointer to variable  that
152
  *         contains the configuration information on faulty page in case of error
153
  *         (0xFFFFFFFF means that all the pages have been correctly erased)
154
  *
155
  * @retval HAL_StatusTypeDef HAL Status
156
  */
157
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
158
{
159
  HAL_StatusTypeDef status = HAL_ERROR;
160
  uint32_t address = 0U;
161
 
162
  /* Process Locked */
163
  __HAL_LOCK(&pFlash);
164
 
165
  /* Check the parameters */
166
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
167
 
168
  if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
169
  {
170
#if defined(FLASH_BANK2_END)
171
    if (pEraseInit->Banks == FLASH_BANK_BOTH)
172
    {
173
      /* Mass Erase requested for Bank1 and Bank2 */
174
      /* Wait for last operation to be completed */
175
      if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \
176
          (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK))
177
      {
178
        /*Mass erase to be done*/
179
        FLASH_MassErase(FLASH_BANK_BOTH);
180
 
181
        /* Wait for last operation to be completed */
182
        if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \
183
            (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK))
184
        {
185
          status = HAL_OK;
186
        }
187
 
188
        /* If the erase operation is completed, disable the MER Bit */
189
        CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
190
        CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
191
      }
192
    }
193
    else if (pEraseInit->Banks == FLASH_BANK_2)
194
    {
195
      /* Mass Erase requested for Bank2 */
196
      /* Wait for last operation to be completed */
197
      if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
198
      {
199
        /*Mass erase to be done*/
200
        FLASH_MassErase(FLASH_BANK_2);
201
 
202
        /* Wait for last operation to be completed */
203
        status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE);
204
 
205
        /* If the erase operation is completed, disable the MER Bit */
206
        CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
207
      }
208
    }
209
    else
210
#endif /* FLASH_BANK2_END */
211
    {
212
      /* Mass Erase requested for Bank1 */
213
      /* Wait for last operation to be completed */
214
      if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
215
      {
216
        /*Mass erase to be done*/
217
        FLASH_MassErase(FLASH_BANK_1);
218
 
219
        /* Wait for last operation to be completed */
220
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
221
 
222
        /* If the erase operation is completed, disable the MER Bit */
223
        CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
224
      }
225
    }
226
  }
227
  else
228
  {
229
    /* Page Erase is requested */
230
    /* Check the parameters */
231
    assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
232
    assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
233
 
234
#if defined(FLASH_BANK2_END)
235
    /* Page Erase requested on address located on bank2 */
236
    if(pEraseInit->PageAddress > FLASH_BANK1_END)
237
    {  
238
      /* Wait for last operation to be completed */
239
      if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
240
      {
241
        /*Initialization of PageError variable*/
242
        *PageError = 0xFFFFFFFFU;
243
 
244
        /* Erase by page by page to be done*/
245
        for(address = pEraseInit->PageAddress;
246
            address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE);
247
            address += FLASH_PAGE_SIZE)
248
        {
249
          FLASH_PageErase(address);
250
 
251
          /* Wait for last operation to be completed */
252
          status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE);
253
 
254
          /* If the erase operation is completed, disable the PER Bit */
255
          CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER);
256
 
257
          if (status != HAL_OK)
258
          {
259
            /* In case of error, stop erase procedure and return the faulty address */
260
            *PageError = address;
261
            break;
262
          }
263
        }
264
      }
265
    }
266
    else
267
#endif /* FLASH_BANK2_END */
268
   {
269
      /* Page Erase requested on address located on bank1 */
270
      /* Wait for last operation to be completed */
271
      if (FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)
272
      {
273
        /*Initialization of PageError variable*/
274
        *PageError = 0xFFFFFFFFU;
275
 
276
        /* Erase page by page to be done*/
277
        for(address = pEraseInit->PageAddress;
278
            address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
279
            address += FLASH_PAGE_SIZE)
280
        {
281
          FLASH_PageErase(address);
282
 
283
          /* Wait for last operation to be completed */
284
          status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
285
 
286
          /* If the erase operation is completed, disable the PER Bit */
287
          CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
288
 
289
          if (status != HAL_OK)
290
          {
291
            /* In case of error, stop erase procedure and return the faulty address */
292
            *PageError = address;
293
            break;
294
          }
295
        }
296
      }
297
    }
298
  }
299
 
300
  /* Process Unlocked */
301
  __HAL_UNLOCK(&pFlash);
302
 
303
  return status;
304
}
305
 
306
/**
307
  * @brief  Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled
308
  * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
309
  *         must be called before.
310
  *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
311
  *         (recommended to protect the FLASH memory against possible unwanted operation)
312
  * @param  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
313
  *         contains the configuration information for the erasing.
314
  *
315
  * @retval HAL_StatusTypeDef HAL Status
316
  */
317
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
318
{
319
  HAL_StatusTypeDef status = HAL_OK;
320
 
321
  /* Process Locked */
322
  __HAL_LOCK(&pFlash);
323
 
324
  /* If procedure already ongoing, reject the next one */
325
  if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
326
  {
327
    return HAL_ERROR;
328
  }
329
 
330
  /* Check the parameters */
331
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
332
 
333
  /* Enable End of FLASH Operation and Error source interrupts */
334
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
335
 
336
#if defined(FLASH_BANK2_END)
337
  /* Enable End of FLASH Operation and Error source interrupts */
338
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
339
 
340
#endif
341
  if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
342
  {
343
    /*Mass erase to be done*/
344
    pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
345
        FLASH_MassErase(pEraseInit->Banks);
346
  }
347
  else
348
  {
349
    /* Erase by page to be done*/
350
 
351
    /* Check the parameters */
352
    assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
353
    assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
354
 
355
    pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
356
    pFlash.DataRemaining = pEraseInit->NbPages;
357
    pFlash.Address = pEraseInit->PageAddress;
358
 
359
    /*Erase 1st page and wait for IT*/
360
    FLASH_PageErase(pEraseInit->PageAddress);
361
  }
362
 
363
  return status;
364
}
365
 
366
/**
367
  * @}
368
  */
369
 
370
/** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
371
 *  @brief   Option Bytes Programming functions
372
  *
373
@verbatim  
374
  ==============================================================================
375
                ##### Option Bytes Programming functions #####
376
  ==============================================================================  
377
    [..]
378
    This subsection provides a set of functions allowing to control the FLASH
379
    option bytes operations.
380
 
381
@endverbatim
382
  * @{
383
  */
384
 
385
/**
386
  * @brief  Erases the FLASH option bytes.
387
  * @note   This functions erases all option bytes except the Read protection (RDP).
388
  *         The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
389
  *         The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
390
  *         The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
391
  *         (system reset will occur)
392
  * @retval HAL status
393
  */
394
 
395
HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
396
{
397
  uint8_t rdptmp = OB_RDP_LEVEL_0;
398
  HAL_StatusTypeDef status = HAL_ERROR;
399
 
400
  /* Get the actual read protection Option Byte value */
401
  rdptmp = FLASH_OB_GetRDP();
402
 
403
  /* Wait for last operation to be completed */
404
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
405
 
406
  if(status == HAL_OK)
407
  {
408
    /* Clean the error context */
409
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
410
 
411
    /* If the previous operation is completed, proceed to erase the option bytes */
412
    SET_BIT(FLASH->CR, FLASH_CR_OPTER);
413
    SET_BIT(FLASH->CR, FLASH_CR_STRT);
414
 
415
    /* Wait for last operation to be completed */
416
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
417
 
418
    /* If the erase operation is completed, disable the OPTER Bit */
419
    CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
420
 
421
    if(status == HAL_OK)
422
    {
423
      /* Restore the last read protection Option Byte value */
424
      status = FLASH_OB_RDP_LevelConfig(rdptmp);
425
    }
426
  }
427
 
428
  /* Return the erase status */
429
  return status;
430
}
431
 
432
/**
433
  * @brief  Program option bytes
434
  * @note   The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
435
  *         The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
436
  *         The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
437
  *         (system reset will occur)
438
  *
439
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
440
  *         contains the configuration information for the programming.
441
  *
442
  * @retval HAL_StatusTypeDef HAL Status
443
  */
444
HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
445
{
446
  HAL_StatusTypeDef status = HAL_ERROR;
447
 
448
  /* Process Locked */
449
  __HAL_LOCK(&pFlash);
450
 
451
  /* Check the parameters */
452
  assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
453
 
454
  /* Write protection configuration */
455
  if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
456
  {
457
    assert_param(IS_WRPSTATE(pOBInit->WRPState));
458
    if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
459
    {
460
      /* Enable of Write protection on the selected page */
461
      status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
462
    }
463
    else
464
    {
465
      /* Disable of Write protection on the selected page */
466
      status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
467
    }
468
    if (status != HAL_OK)
469
    {
470
      /* Process Unlocked */
471
      __HAL_UNLOCK(&pFlash);
472
      return status;
473
    }
474
  }
475
 
476
  /* Read protection configuration */
477
  if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
478
  {
479
    status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
480
    if (status != HAL_OK)
481
    {
482
      /* Process Unlocked */
483
      __HAL_UNLOCK(&pFlash);
484
      return status;
485
    }
486
  }
487
 
488
  /* USER configuration */
489
  if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
490
  {
491
    status = FLASH_OB_UserConfig(pOBInit->USERConfig);
492
    if (status != HAL_OK)
493
    {
494
      /* Process Unlocked */
495
      __HAL_UNLOCK(&pFlash);
496
      return status;
497
    }
498
  }
499
 
500
  /* DATA configuration*/
501
  if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
502
  {
503
    status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
504
    if (status != HAL_OK)
505
    {
506
      /* Process Unlocked */
507
      __HAL_UNLOCK(&pFlash);
508
      return status;
509
    }
510
  }
511
 
512
  /* Process Unlocked */
513
  __HAL_UNLOCK(&pFlash);
514
 
515
  return status;
516
}
517
 
518
/**
519
  * @brief  Get the Option byte configuration
520
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
521
  *         contains the configuration information for the programming.
522
  *
523
  * @retval None
524
  */
525
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
526
{
527
  pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
528
 
529
  /*Get WRP*/
530
  pOBInit->WRPPage = FLASH_OB_GetWRP();
531
 
532
  /*Get RDP Level*/
533
  pOBInit->RDPLevel = FLASH_OB_GetRDP();
534
 
535
  /*Get USER*/
536
  pOBInit->USERConfig = FLASH_OB_GetUser();
537
}
538
 
539
/**
540
  * @brief  Get the Option byte user data
541
  * @param  DATAAdress Address of the option byte DATA
542
  *          This parameter can be one of the following values:
543
  *            @arg @ref OB_DATA_ADDRESS_DATA0
544
  *            @arg @ref OB_DATA_ADDRESS_DATA1
545
  * @retval Value programmed in USER data
546
  */
547
uint32_t HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress)
548
{
549
  uint32_t value = 0;
550
 
551
  if (DATAAdress == OB_DATA_ADDRESS_DATA0)
552
  {
553
    /* Get value programmed in OB USER Data0 */
554
    value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA0) >> FLASH_POSITION_OB_USERDATA0_BIT;
555
  }
556
  else
557
  {
558
    /* Get value programmed in OB USER Data1 */
559
    value = READ_BIT(FLASH->OBR, FLASH_OBR_DATA1) >> FLASH_POSITION_OB_USERDATA1_BIT;
560
  }
561
 
562
  return value;
563
}
564
 
565
/**
566
  * @}
567
  */
568
 
569
/**
570
  * @}
571
  */
572
 
573
/** @addtogroup FLASHEx_Private_Functions
574
 * @{
575
 */
576
 
577
/**
578
  * @brief  Full erase of FLASH memory Bank
579
  * @param  Banks Banks to be erased
580
  *          This parameter can be one of the following values:
581
  *            @arg @ref FLASH_BANK_1 Bank1 to be erased
582
  @if STM32F101xG
583
  *            @arg @ref FLASH_BANK_2 Bank2 to be erased
584
  *            @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased
585
  @endif
586
  @if STM32F103xG
587
  *            @arg @ref FLASH_BANK_2 Bank2 to be erased
588
  *            @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased
589
  @endif
590
  *
591
  * @retval None
592
  */
593
static void FLASH_MassErase(uint32_t Banks)
594
{
595
  /* Check the parameters */
596
  assert_param(IS_FLASH_BANK(Banks));
597
 
598
  /* Clean the error context */
599
  pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
600
 
601
#if defined(FLASH_BANK2_END)
602
  if(Banks == FLASH_BANK_BOTH)
603
  {
604
    /* bank1 & bank2 will be erased*/
605
    SET_BIT(FLASH->CR, FLASH_CR_MER);
606
    SET_BIT(FLASH->CR2, FLASH_CR2_MER);
607
    SET_BIT(FLASH->CR, FLASH_CR_STRT);
608
    SET_BIT(FLASH->CR2, FLASH_CR2_STRT);
609
  }
610
  else if(Banks == FLASH_BANK_2)
611
  {
612
    /*Only bank2 will be erased*/
613
    SET_BIT(FLASH->CR2, FLASH_CR2_MER);
614
    SET_BIT(FLASH->CR2, FLASH_CR2_STRT);
615
  }
616
  else
617
  {
618
#endif /* FLASH_BANK2_END */
619
#if !defined(FLASH_BANK2_END)
620
  /* Prevent unused argument(s) compilation warning */
621
  UNUSED(Banks);
622
#endif /* FLASH_BANK2_END */  
623
    /* Only bank1 will be erased*/
624
    SET_BIT(FLASH->CR, FLASH_CR_MER);
625
    SET_BIT(FLASH->CR, FLASH_CR_STRT);
626
#if defined(FLASH_BANK2_END)
627
  }
628
#endif /* FLASH_BANK2_END */
629
}
630
 
631
/**
632
  * @brief  Enable the write protection of the desired pages
633
  * @note   An option byte erase is done automatically in this function.
634
  * @note   When the memory read protection level is selected (RDP level = 1),
635
  *         it is not possible to program or erase the flash page i if
636
  *         debug features are connected or boot code is executed in RAM, even if nWRPi = 1
637
  *
638
  * @param  WriteProtectPage specifies the page(s) to be write protected.
639
  *         The value of this parameter depend on device used within the same series
640
  * @retval HAL status
641
  */
642
static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
643
{
644
  HAL_StatusTypeDef status = HAL_OK;
645
  uint16_t WRP0_Data = 0xFFFF;
646
#if defined(FLASH_WRP1_WRP1)
647
  uint16_t WRP1_Data = 0xFFFF;
648
#endif /* FLASH_WRP1_WRP1 */
649
#if defined(FLASH_WRP2_WRP2)
650
  uint16_t WRP2_Data = 0xFFFF;
651
#endif /* FLASH_WRP2_WRP2 */
652
#if defined(FLASH_WRP3_WRP3)
653
  uint16_t WRP3_Data = 0xFFFF;
654
#endif /* FLASH_WRP3_WRP3 */
655
 
656
  /* Check the parameters */
657
  assert_param(IS_OB_WRP(WriteProtectPage));
658
 
659
  /* Get current write protected pages and the new pages to be protected ******/
660
  WriteProtectPage = (uint32_t)(~((~FLASH_OB_GetWRP()) | WriteProtectPage));
661
 
662
#if defined(OB_WRP_PAGES0TO15MASK)
663
  WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
664
#elif defined(OB_WRP_PAGES0TO31MASK)
665
  WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
666
#endif /* OB_WRP_PAGES0TO31MASK */
667
 
668
#if defined(OB_WRP_PAGES16TO31MASK)
669
  WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
670
#elif defined(OB_WRP_PAGES32TO63MASK)
671
  WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
672
#endif /* OB_WRP_PAGES32TO63MASK */
673
 
674
#if defined(OB_WRP_PAGES64TO95MASK)
675
  WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U);
676
#endif /* OB_WRP_PAGES64TO95MASK */
677
#if defined(OB_WRP_PAGES32TO47MASK)
678
  WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
679
#endif /* OB_WRP_PAGES32TO47MASK */
680
 
681
#if defined(OB_WRP_PAGES96TO127MASK)
682
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U);
683
#elif defined(OB_WRP_PAGES48TO255MASK)
684
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U);
685
#elif defined(OB_WRP_PAGES48TO511MASK)
686
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U);
687
#elif defined(OB_WRP_PAGES48TO127MASK)
688
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
689
#endif /* OB_WRP_PAGES96TO127MASK */
690
 
691
  /* Wait for last operation to be completed */
692
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
693
 
694
  if(status == HAL_OK)
695
  {
696
    /* Clean the error context */
697
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
698
 
699
    /* To be able to write again option byte, need to perform a option byte erase */
700
    status = HAL_FLASHEx_OBErase();
701
    if (status == HAL_OK)  
702
    {
703
      /* Enable write protection */
704
      SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
705
 
706
#if defined(FLASH_WRP0_WRP0)
707
      if(WRP0_Data != 0xFFU)
708
      {
709
        OB->WRP0 &= WRP0_Data;
710
 
711
        /* Wait for last operation to be completed */
712
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
713
      }
714
#endif /* FLASH_WRP0_WRP0 */
715
 
716
#if defined(FLASH_WRP1_WRP1)
717
      if((status == HAL_OK) && (WRP1_Data != 0xFFU))
718
      {
719
        OB->WRP1 &= WRP1_Data;
720
 
721
        /* Wait for last operation to be completed */
722
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
723
      }
724
#endif /* FLASH_WRP1_WRP1 */
725
 
726
#if defined(FLASH_WRP2_WRP2)
727
      if((status == HAL_OK) && (WRP2_Data != 0xFFU))
728
      {
729
        OB->WRP2 &= WRP2_Data;
730
 
731
        /* Wait for last operation to be completed */
732
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
733
      }
734
#endif /* FLASH_WRP2_WRP2 */
735
 
736
#if defined(FLASH_WRP3_WRP3)
737
      if((status == HAL_OK) && (WRP3_Data != 0xFFU))
738
      {
739
        OB->WRP3 &= WRP3_Data;
740
 
741
        /* Wait for last operation to be completed */
742
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
743
      }
744
#endif /* FLASH_WRP3_WRP3 */
745
 
746
      /* if the program operation is completed, disable the OPTPG Bit */
747
      CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
748
    }
749
  }
750
 
751
  return status;
752
}
753
 
754
/**
755
  * @brief  Disable the write protection of the desired pages
756
  * @note   An option byte erase is done automatically in this function.
757
  * @note   When the memory read protection level is selected (RDP level = 1),
758
  *         it is not possible to program or erase the flash page i if  
759
  *         debug features are connected or boot code is executed in RAM, even if nWRPi = 1
760
  *
761
  * @param  WriteProtectPage specifies the page(s) to be write unprotected.
762
  *         The value of this parameter depend on device used within the same series
763
  * @retval HAL status
764
  */
765
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
766
{
767
  HAL_StatusTypeDef status = HAL_OK;
768
  uint16_t WRP0_Data = 0xFFFF;
769
#if defined(FLASH_WRP1_WRP1)
770
  uint16_t WRP1_Data = 0xFFFF;
771
#endif /* FLASH_WRP1_WRP1 */
772
#if defined(FLASH_WRP2_WRP2)
773
  uint16_t WRP2_Data = 0xFFFF;
774
#endif /* FLASH_WRP2_WRP2 */
775
#if defined(FLASH_WRP3_WRP3)
776
  uint16_t WRP3_Data = 0xFFFF;
777
#endif /* FLASH_WRP3_WRP3 */
778
 
779
  /* Check the parameters */
780
  assert_param(IS_OB_WRP(WriteProtectPage));
781
 
782
  /* Get current write protected pages and the new pages to be unprotected ******/
783
  WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage);
784
 
785
#if defined(OB_WRP_PAGES0TO15MASK)
786
  WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
787
#elif defined(OB_WRP_PAGES0TO31MASK)
788
  WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
789
#endif /* OB_WRP_PAGES0TO31MASK */
790
 
791
#if defined(OB_WRP_PAGES16TO31MASK)
792
  WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U);
793
#elif defined(OB_WRP_PAGES32TO63MASK)
794
  WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U);
795
#endif /* OB_WRP_PAGES32TO63MASK */
796
 
797
#if defined(OB_WRP_PAGES64TO95MASK)
798
  WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U);
799
#endif /* OB_WRP_PAGES64TO95MASK */
800
#if defined(OB_WRP_PAGES32TO47MASK)
801
  WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U);
802
#endif /* OB_WRP_PAGES32TO47MASK */
803
 
804
#if defined(OB_WRP_PAGES96TO127MASK)
805
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U);
806
#elif defined(OB_WRP_PAGES48TO255MASK)
807
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U);
808
#elif defined(OB_WRP_PAGES48TO511MASK)
809
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U);
810
#elif defined(OB_WRP_PAGES48TO127MASK)
811
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U);
812
#endif /* OB_WRP_PAGES96TO127MASK */
813
 
814
 
815
  /* Wait for last operation to be completed */
816
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
817
 
818
  if(status == HAL_OK)
819
  {
820
    /* Clean the error context */
821
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
822
 
823
    /* To be able to write again option byte, need to perform a option byte erase */
824
    status = HAL_FLASHEx_OBErase();
825
    if (status == HAL_OK)  
826
    {
827
      SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
828
 
829
#if defined(FLASH_WRP0_WRP0)
830
      if(WRP0_Data != 0xFFU)
831
      {
832
        OB->WRP0 |= WRP0_Data;
833
 
834
        /* Wait for last operation to be completed */
835
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
836
      }
837
#endif /* FLASH_WRP0_WRP0 */
838
 
839
#if defined(FLASH_WRP1_WRP1)
840
      if((status == HAL_OK) && (WRP1_Data != 0xFFU))
841
      {
842
        OB->WRP1 |= WRP1_Data;
843
 
844
        /* Wait for last operation to be completed */
845
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
846
      }
847
#endif /* FLASH_WRP1_WRP1 */
848
 
849
#if defined(FLASH_WRP2_WRP2)
850
      if((status == HAL_OK) && (WRP2_Data != 0xFFU))
851
      {
852
        OB->WRP2 |= WRP2_Data;
853
 
854
        /* Wait for last operation to be completed */
855
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
856
      }
857
#endif /* FLASH_WRP2_WRP2 */
858
 
859
#if defined(FLASH_WRP3_WRP3)
860
      if((status == HAL_OK) && (WRP3_Data != 0xFFU))
861
      {
862
        OB->WRP3 |= WRP3_Data;
863
 
864
        /* Wait for last operation to be completed */
865
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
866
      }
867
#endif /* FLASH_WRP3_WRP3 */
868
 
869
      /* if the program operation is completed, disable the OPTPG Bit */
870
      CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
871
    }
872
  }
873
  return status;
874
}
875
 
876
/**
877
  * @brief  Set the read protection level.
878
  * @param  ReadProtectLevel specifies the read protection level.
879
  *         This parameter can be one of the following values:
880
  *            @arg @ref OB_RDP_LEVEL_0 No protection
881
  *            @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
882
  * @retval HAL status
883
  */
884
static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
885
{
886
  HAL_StatusTypeDef status = HAL_OK;
887
 
888
  /* Check the parameters */
889
  assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
890
 
891
  /* Wait for last operation to be completed */
892
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
893
 
894
  if(status == HAL_OK)
895
  {
896
    /* Clean the error context */
897
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
898
 
899
    /* If the previous operation is completed, proceed to erase the option bytes */
900
    SET_BIT(FLASH->CR, FLASH_CR_OPTER);
901
    SET_BIT(FLASH->CR, FLASH_CR_STRT);
902
 
903
    /* Wait for last operation to be completed */
904
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
905
 
906
    /* If the erase operation is completed, disable the OPTER Bit */
907
    CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
908
 
909
    if(status == HAL_OK)
910
    {
911
      /* Enable the Option Bytes Programming operation */
912
      SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
913
 
914
      WRITE_REG(OB->RDP, ReadProtectLevel);
915
 
916
      /* Wait for last operation to be completed */
917
      status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
918
 
919
      /* if the program operation is completed, disable the OPTPG Bit */
920
      CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
921
    }
922
  }
923
 
924
  return status;
925
}
926
 
927
/**
928
  * @brief  Program the FLASH User Option Byte.    
929
  * @note   Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
930
  * @param  UserConfig The FLASH User Option Bytes values FLASH_OBR_IWDG_SW(Bit2),
931
  *         FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4).
932
  *         And BFBF2(Bit5) for STM32F101xG and STM32F103xG .
933
  * @retval HAL status
934
  */
935
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
936
{
937
  HAL_StatusTypeDef status = HAL_OK;
938
 
939
  /* Check the parameters */
940
  assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
941
  assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
942
  assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
943
#if defined(FLASH_BANK2_END)
944
  assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
945
#endif /* FLASH_BANK2_END */
946
 
947
  /* Wait for last operation to be completed */
948
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
949
 
950
  if(status == HAL_OK)
951
  {    
952
    /* Clean the error context */
953
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
954
 
955
    /* Enable the Option Bytes Programming operation */
956
    SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
957
 
958
#if defined(FLASH_BANK2_END)
959
    OB->USER = (UserConfig | 0xF0U);
960
#else
961
    OB->USER = (UserConfig | 0x88U);
962
#endif /* FLASH_BANK2_END */
963
 
964
    /* Wait for last operation to be completed */
965
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
966
 
967
    /* if the program operation is completed, disable the OPTPG Bit */
968
    CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
969
  }
970
 
971
  return status;
972
}
973
 
974
/**
975
  * @brief  Programs a half word at a specified Option Byte Data address.
976
  * @note   The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
977
  *         The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
978
  *         The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
979
  *         (system reset will occur)
980
  *         Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
981
  * @param  Address specifies the address to be programmed.
982
  *         This parameter can be 0x1FFFF804 or 0x1FFFF806.
983
  * @param  Data specifies the data to be programmed.
984
  * @retval HAL status
985
  */
986
static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
987
{
988
  HAL_StatusTypeDef status = HAL_ERROR;
989
 
990
  /* Check the parameters */
991
  assert_param(IS_OB_DATA_ADDRESS(Address));
992
 
993
  /* Wait for last operation to be completed */
994
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
995
 
996
  if(status == HAL_OK)
997
  {
998
    /* Clean the error context */
999
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1000
 
1001
    /* Enables the Option Bytes Programming operation */
1002
    SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
1003
    *(__IO uint16_t*)Address = Data;
1004
 
1005
    /* Wait for last operation to be completed */
1006
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
1007
 
1008
    /* If the program operation is completed, disable the OPTPG Bit */
1009
    CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
1010
  }
1011
  /* Return the Option Byte Data Program Status */
1012
  return status;
1013
}
1014
 
1015
/**
1016
  * @brief  Return the FLASH Write Protection Option Bytes value.
1017
  * @retval The FLASH Write Protection Option Bytes value
1018
  */
1019
static uint32_t FLASH_OB_GetWRP(void)
1020
{
1021
  /* Return the FLASH write protection Register value */
1022
  return (uint32_t)(READ_REG(FLASH->WRPR));
1023
}
1024
 
1025
/**
1026
  * @brief  Returns the FLASH Read Protection level.
1027
  * @retval FLASH RDP level
1028
  *         This parameter can be one of the following values:
1029
  *            @arg @ref OB_RDP_LEVEL_0 No protection
1030
  *            @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
1031
  */
1032
static uint32_t FLASH_OB_GetRDP(void)
1033
{
1034
  uint32_t readstatus = OB_RDP_LEVEL_0;
1035
  uint32_t tmp_reg = 0U;
1036
 
1037
  /* Read RDP level bits */
1038
  tmp_reg = READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT);
1039
 
1040
  if (tmp_reg == FLASH_OBR_RDPRT)
1041
  {
1042
    readstatus = OB_RDP_LEVEL_1;
1043
  }
1044
  else
1045
  {
1046
    readstatus = OB_RDP_LEVEL_0;
1047
  }
1048
 
1049
  return readstatus;
1050
}
1051
 
1052
/**
1053
  * @brief  Return the FLASH User Option Byte value.
1054
  * @retval The FLASH User Option Bytes values: FLASH_OBR_IWDG_SW(Bit2),
1055
  *         FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4).
1056
  *         And FLASH_OBR_BFB2(Bit5) for STM32F101xG and STM32F103xG .
1057
  */
1058
static uint8_t FLASH_OB_GetUser(void)
1059
{
1060
  /* Return the User Option Byte */
1061
  return (uint8_t)((READ_REG(FLASH->OBR) & FLASH_OBR_USER) >> FLASH_POSITION_IWDGSW_BIT);
1062
}
1063
 
1064
/**
1065
  * @}
1066
  */
1067
 
1068
/**
1069
  * @}
1070
  */
1071
 
1072
/** @addtogroup FLASH
1073
  * @{
1074
  */
1075
 
1076
/** @addtogroup FLASH_Private_Functions
1077
 * @{
1078
 */
1079
 
1080
/**
1081
  * @brief  Erase the specified FLASH memory page
1082
  * @param  PageAddress FLASH page to erase
1083
  *         The value of this parameter depend on device used within the same series      
1084
  *
1085
  * @retval None
1086
  */
1087
void FLASH_PageErase(uint32_t PageAddress)
1088
{
1089
  /* Clean the error context */
1090
  pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1091
 
1092
#if defined(FLASH_BANK2_END)
1093
  if(PageAddress > FLASH_BANK1_END)
1094
  {
1095
    /* Proceed to erase the page */
1096
    SET_BIT(FLASH->CR2, FLASH_CR2_PER);
1097
    WRITE_REG(FLASH->AR2, PageAddress);
1098
    SET_BIT(FLASH->CR2, FLASH_CR2_STRT);
1099
  }
1100
  else
1101
  {
1102
#endif /* FLASH_BANK2_END */
1103
    /* Proceed to erase the page */
1104
    SET_BIT(FLASH->CR, FLASH_CR_PER);
1105
    WRITE_REG(FLASH->AR, PageAddress);
1106
    SET_BIT(FLASH->CR, FLASH_CR_STRT);
1107
#if defined(FLASH_BANK2_END)
1108
  }
1109
#endif /* FLASH_BANK2_END */
1110
}
1111
 
1112
/**
1113
  * @}
1114
  */
1115
 
1116
/**
1117
  * @}
1118
  */
1119
 
1120
#endif /* HAL_FLASH_MODULE_ENABLED */
1121
/**
1122
  * @}
1123
  */
1124