Subversion Repositories EDIS_Ignition

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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