Subversion Repositories EngineBay2

Rev

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

Rev 2 Rev 28
Line 1... Line 1...
1
/**
1
/**
2
  ******************************************************************************
2
  ******************************************************************************
3
  * @file    stm32l1xx_hal_flash_ex.c
3
  * @file    stm32l1xx_hal_flash_ex.c
4
  * @author  MCD Application Team
4
  * @author  MCD Application Team
5
  * @version V1.2.0
-
 
6
  * @date    01-July-2016
-
 
7
  * @brief   Extended FLASH HAL module driver.
5
  * @brief   Extended FLASH HAL module driver.
8
  *    
6
  *
9
  *          This file provides firmware functions to manage the following
7
  *          This file provides firmware functions to manage the following
10
  *          functionalities of the internal FLASH memory:
8
  *          functionalities of the internal FLASH memory:
11
  *            + FLASH Interface configuration
9
  *            + FLASH Interface configuration
12
  *            + FLASH Memory Erasing
10
  *            + FLASH Memory Erasing
13
  *            + DATA EEPROM Programming/Erasing
11
  *            + DATA EEPROM Programming/Erasing
14
  *            + Option Bytes Programming
12
  *            + Option Bytes Programming
15
  *            + Interrupts management
13
  *            + Interrupts management
16
  *    
14
  *
17
  @verbatim
15
  @verbatim
18
  ==============================================================================
16
  ==============================================================================
19
               ##### Flash peripheral Extended features  #####
17
               ##### Flash peripheral Extended features  #####
20
  ==============================================================================
18
  ==============================================================================
21
           
19
 
22
  [..] Comparing to other products, the FLASH interface for STM32L1xx
20
  [..] Comparing to other products, the FLASH interface for STM32L1xx
23
       devices contains the following additional features        
21
       devices contains the following additional features
24
       (+) Erase functions
22
       (+) Erase functions
25
       (+) DATA_EEPROM memory management
23
       (+) DATA_EEPROM memory management
26
       (+) BOOT option bit configuration      
24
       (+) BOOT option bit configuration
27
       (+) PCROP protection for all sectors
25
       (+) PCROP protection for all sectors
28
   
26
 
29
                      ##### How to use this driver #####
27
                      ##### How to use this driver #####
30
  ==============================================================================
28
  ==============================================================================
31
  [..] This driver provides functions to configure and program the FLASH memory
29
  [..] This driver provides functions to configure and program the FLASH memory
32
       of all STM32L1xx. It includes:
30
       of all STM32L1xx. It includes:
33
       (+) Full DATA_EEPROM erase and program management
31
       (+) Full DATA_EEPROM erase and program management
34
       (+) Boot activation
32
       (+) Boot activation
35
       (+) PCROP protection configuration and control for all pages
33
       (+) PCROP protection configuration and control for all pages
36
 
34
 
37
  @endverbatim
35
  @endverbatim
38
  ******************************************************************************
36
  ******************************************************************************
39
  * @attention
37
  * @attention
40
  *
38
  *
41
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
39
  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
42
  *
-
 
43
  * Redistribution and use in source and binary forms, with or without modification,
-
 
44
  * are permitted provided that the following conditions are met:
-
 
45
  *   1. Redistributions of source code must retain the above copyright notice,
-
 
46
  *      this list of conditions and the following disclaimer.
-
 
47
  *   2. Redistributions in binary form must reproduce the above copyright notice,
-
 
48
  *      this list of conditions and the following disclaimer in the documentation
-
 
49
  *      and/or other materials provided with the distribution.
-
 
50
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
-
 
51
  *      may be used to endorse or promote products derived from this software
-
 
52
  *      without specific prior written permission.
40
  * All rights reserved.</center></h2>
53
  *
41
  *
54
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42
  * This software component is licensed by ST under BSD 3-Clause license,
55
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-
 
56
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-
 
57
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
-
 
58
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-
 
59
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43
  * the "License"; You may not use this file except in compliance with the
60
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44
  * License. You may obtain a copy of the License at:
61
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-
 
62
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-
 
63
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
  *                        opensource.org/licenses/BSD-3-Clause
64
  *
46
  *
65
  ******************************************************************************  
47
  ******************************************************************************
66
  */
48
  */
67
 
49
 
68
/* Includes ------------------------------------------------------------------*/
50
/* Includes ------------------------------------------------------------------*/
69
#include "stm32l1xx_hal.h"
51
#include "stm32l1xx_hal.h"
70
 
52
 
71
/** @addtogroup STM32L1xx_HAL_Driver
53
/** @addtogroup STM32L1xx_HAL_Driver
Line 86... Line 68...
86
  */
68
  */
87
 
69
 
88
/**
70
/**
89
  * @}
71
  * @}
90
  */
72
  */
91
 
73
 
92
/** @defgroup FLASHEx FLASHEx
74
/** @defgroup FLASHEx FLASHEx
93
  * @brief FLASH HAL Extension module driver
75
  * @brief FLASH HAL Extension module driver
94
  * @{
76
  * @{
95
  */
77
  */
96
 
78
 
Line 107... Line 89...
107
/** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
89
/** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
108
  * @{
90
  * @{
109
  */
91
  */
110
/**
92
/**
111
  * @}
93
  * @}
112
  */
94
  */
113
 
95
 
114
/* Private variables ---------------------------------------------------------*/
96
/* Private variables ---------------------------------------------------------*/
115
/* Private function prototypes -----------------------------------------------*/
97
/* Private function prototypes -----------------------------------------------*/
116
/** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
98
/** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
117
 * @{
99
 * @{
Line 162... Line 144...
162
  */
144
  */
163
 
145
 
164
/** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
146
/** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
165
 *  @brief   FLASH Memory Erasing functions
147
 *  @brief   FLASH Memory Erasing functions
166
 *
148
 *
167
@verbatim  
149
@verbatim
168
  ==============================================================================
150
  ==============================================================================
169
                ##### FLASH Erasing Programming functions #####
151
                ##### FLASH Erasing Programming functions #####
170
  ==============================================================================
152
  ==============================================================================
171
 
153
 
172
    [..] The FLASH Memory Erasing functions, includes the following functions:
154
    [..] The FLASH Memory Erasing functions, includes the following functions:
173
    (+) @ref HAL_FLASHEx_Erase: return only when erase has been done
155
    (+) @ref HAL_FLASHEx_Erase: return only when erase has been done
174
    (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback
156
    (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback
175
        is called with parameter 0xFFFFFFFF
157
        is called with parameter 0xFFFFFFFF
176
 
158
 
177
    [..] Any operation of erase should follow these steps:
159
    [..] Any operation of erase should follow these steps:
178
    (#) Call the @ref HAL_FLASH_Unlock() function to enable the flash control register and
160
    (#) Call the @ref HAL_FLASH_Unlock() function to enable the flash control register and
179
        program memory access.
161
        program memory access.
180
    (#) Call the desired function to erase page.
162
    (#) Call the desired function to erase page.
181
    (#) Call the @ref HAL_FLASH_Lock() to disable the flash program memory access
163
    (#) Call the @ref HAL_FLASH_Lock() to disable the flash program memory access
182
       (recommended to protect the FLASH memory against possible unwanted operation).
164
       (recommended to protect the FLASH memory against possible unwanted operation).
183
 
165
 
184
@endverbatim
166
@endverbatim
185
  * @{
167
  * @{
186
  */
168
  */
187
 
169
 
188
/**
170
/**
189
  * @brief  Erase the specified FLASH memory Pages
171
  * @brief  Erase the specified FLASH memory Pages
190
  * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
172
  * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
191
  *         must be called before.
173
  *         must be called before.
192
  *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
174
  *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
193
  *         (recommended to protect the FLASH memory against possible unwanted operation)
175
  *         (recommended to protect the FLASH memory against possible unwanted operation)
194
  * @note   For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
176
  * @note   For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
195
  *         2 banks, user should perform pages erase by bank only.
177
  *         2 banks, user should perform pages erase by bank only.
196
  * @param[in]  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
178
  * @param[in]  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
197
  *         contains the configuration information for the erasing.
179
  *         contains the configuration information for the erasing.
198
  *
180
  *
199
  * @param[out]  PageError pointer to variable  that
181
  * @param[out]  PageError pointer to variable  that
200
  *         contains the configuration information on faulty page in case of error
182
  *         contains the configuration information on faulty page in case of error
201
  *         (0xFFFFFFFF means that all the pages have been correctly erased)
183
  *         (0xFFFFFFFF means that all the pages have been correctly erased)
202
  *
184
  *
203
  * @retval HAL_StatusTypeDef HAL Status
185
  * @retval HAL_StatusTypeDef HAL Status
204
  */
186
  */
205
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
187
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
206
{
188
{
207
  HAL_StatusTypeDef status = HAL_ERROR;
189
  HAL_StatusTypeDef status = HAL_ERROR;
208
  uint32_t address = 0;
190
  uint32_t address = 0U;
209
 
191
 
210
  /* Process Locked */
192
  /* Process Locked */
211
  __HAL_LOCK(&pFlash);
193
  __HAL_LOCK(&pFlash);
212
 
194
 
213
  /* Wait for last operation to be completed */
195
  /* Wait for last operation to be completed */
214
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
196
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
Line 220... Line 202...
220
 
202
 
221
    /* Check the parameters */
203
    /* Check the parameters */
222
    assert_param(IS_NBPAGES(pEraseInit->NbPages));
204
    assert_param(IS_NBPAGES(pEraseInit->NbPages));
223
    assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
205
    assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
224
    assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
206
    assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
225
    assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
207
    assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U));
226
 
208
 
227
#if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
209
#if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
228
    /* Check on which bank belongs the 1st address to erase */
210
    /* Check on which bank belongs the 1st address to erase */
229
    if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
211
    if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
230
    {
212
    {
231
      /* BANK1 */
213
      /* BANK1 */
232
      /* Check that last page to erase still belongs to BANK1 */
214
      /* Check that last page to erase still belongs to BANK1 */
233
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK1_END)
215
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK1_END)
234
      {
216
      {
235
        /*  Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
217
        /*  Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
236
            continuous */
218
            continuous */
237
        /* Process Unlocked */
219
        /* Process Unlocked */
238
        __HAL_UNLOCK(&pFlash);
220
        __HAL_UNLOCK(&pFlash);
Line 241... Line 223...
241
    }
223
    }
242
    else
224
    else
243
    {
225
    {
244
      /* BANK2 */
226
      /* BANK2 */
245
      /* Check that last page to erase still belongs to BANK2 */
227
      /* Check that last page to erase still belongs to BANK2 */
246
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK2_END)
228
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK2_END)
247
      {
229
      {
248
        /*  Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
230
        /*  Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
249
            continuous */
231
            continuous */
250
        /* Process Unlocked */
232
        /* Process Unlocked */
251
        __HAL_UNLOCK(&pFlash);
233
        __HAL_UNLOCK(&pFlash);
Line 253... Line 235...
253
      }
235
      }
254
    }
236
    }
255
#endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
237
#endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
256
 
238
 
257
    /* Erase page by page to be done*/
239
    /* Erase page by page to be done*/
258
    for(address = pEraseInit->PageAddress;
240
    for(address = pEraseInit->PageAddress;
259
        address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
241
        address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
260
        address += FLASH_PAGE_SIZE)
242
        address += FLASH_PAGE_SIZE)
261
    {
243
    {
262
      FLASH_PageErase(address);
244
      FLASH_PageErase(address);
263
 
245
 
Line 266... Line 248...
266
 
248
 
267
      /* If the erase operation is completed, disable the ERASE Bit */
249
      /* If the erase operation is completed, disable the ERASE Bit */
268
      CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
250
      CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
269
      CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
251
      CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
270
 
252
 
271
      if (status != HAL_OK)
253
      if (status != HAL_OK)
272
      {
254
      {
273
        /* In case of error, stop erase procedure and return the faulty address */
255
        /* In case of error, stop erase procedure and return the faulty address */
274
        *PageError = address;
256
        *PageError = address;
275
        break;
257
        break;
276
      }
258
      }
Line 285... Line 267...
285
 
267
 
286
/**
268
/**
287
  * @brief  Perform a page erase of the specified FLASH memory pages  with interrupt enabled
269
  * @brief  Perform a page erase of the specified FLASH memory pages  with interrupt enabled
288
  * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
270
  * @note   To correctly run this function, the @ref HAL_FLASH_Unlock() function
289
  *         must be called before.
271
  *         must be called before.
290
  *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
272
  *         Call the @ref HAL_FLASH_Lock() to disable the flash memory access
291
  *         (recommended to protect the FLASH memory against possible unwanted operation)
273
  *         (recommended to protect the FLASH memory against possible unwanted operation)
292
  *          End of erase is done when @ref HAL_FLASH_EndOfOperationCallback is called with parameter
274
  *          End of erase is done when @ref HAL_FLASH_EndOfOperationCallback is called with parameter
293
  *          0xFFFFFFFF
275
  *          0xFFFFFFFF
294
  * @note   For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
276
  * @note   For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
295
  *         2 banks, user should perform pages erase by bank only.
277
  *         2 banks, user should perform pages erase by bank only.
296
  * @param  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
278
  * @param  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
297
  *         contains the configuration information for the erasing.
279
  *         contains the configuration information for the erasing.
298
  *
280
  *
299
  * @retval HAL_StatusTypeDef HAL Status
281
  * @retval HAL_StatusTypeDef HAL Status
300
  */
282
  */
301
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
283
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
302
{
284
{
303
  HAL_StatusTypeDef status = HAL_OK;
285
  HAL_StatusTypeDef status = HAL_ERROR;
304
 
286
 
305
  /* If procedure already ongoing, reject the next one */
287
  /* If procedure already ongoing, reject the next one */
306
  if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
288
  if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
307
  {
289
  {
308
    return HAL_ERROR;
290
    return HAL_ERROR;
Line 310... Line 292...
310
 
292
 
311
  /* Check the parameters */
293
  /* Check the parameters */
312
  assert_param(IS_NBPAGES(pEraseInit->NbPages));
294
  assert_param(IS_NBPAGES(pEraseInit->NbPages));
313
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
295
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
314
  assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
296
  assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
315
  assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
297
  assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U));
316
 
298
 
317
  /* Process Locked */
299
  /* Process Locked */
318
  __HAL_LOCK(&pFlash);
300
  __HAL_LOCK(&pFlash);
319
 
301
 
320
#if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
302
#if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
321
    /* Check on which bank belongs the 1st address to erase */
303
    /* Check on which bank belongs the 1st address to erase */
322
    if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
304
    if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
323
    {
305
    {
324
      /* BANK1 */
306
      /* BANK1 */
325
      /* Check that last page to erase still belongs to BANK1 */
307
      /* Check that last page to erase still belongs to BANK1 */
326
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK1_END)
308
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK1_END)
327
      {
309
      {
328
        /*  Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
310
        /*  Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
329
            continuous */
311
            continuous */
330
        /* Process Unlocked */
312
        /* Process Unlocked */
331
        __HAL_UNLOCK(&pFlash);
313
        __HAL_UNLOCK(&pFlash);
Line 334... Line 316...
334
    }
316
    }
335
    else
317
    else
336
    {
318
    {
337
      /* BANK2 */
319
      /* BANK2 */
338
      /* Check that last page to erase still belongs to BANK2 */
320
      /* Check that last page to erase still belongs to BANK2 */
339
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK2_END)
321
      if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK2_END)
340
      {
322
      {
341
        /*  Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
323
        /*  Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
342
            continuous */
324
            continuous */
343
        /* Process Unlocked */
325
        /* Process Unlocked */
344
        __HAL_UNLOCK(&pFlash);
326
        __HAL_UNLOCK(&pFlash);
345
        return HAL_ERROR;
327
        return HAL_ERROR;
346
      }
328
      }
347
    }
329
    }
348
#endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
330
#endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
349
 
331
 
350
  /* Enable End of FLASH Operation and Error source interrupts */
332
  /* Wait for last operation to be completed */
351
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
333
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
352
 
334
 
353
  pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
335
  if (status == HAL_OK)
-
 
336
  {
354
  pFlash.NbPagesToErase = pEraseInit->NbPages;
337
    /* Enable End of FLASH Operation and Error source interrupts */
355
  pFlash.Page = pEraseInit->PageAddress;
338
    __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
356
 
339
 
-
 
340
    pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
-
 
341
    pFlash.NbPagesToErase = pEraseInit->NbPages;
-
 
342
    pFlash.Page = pEraseInit->PageAddress;
-
 
343
 
357
  /*Erase 1st page and wait for IT*/
344
    /*Erase 1st page and wait for IT*/
358
  FLASH_PageErase(pEraseInit->PageAddress);
345
    FLASH_PageErase(pEraseInit->PageAddress);
-
 
346
  }
-
 
347
  else
-
 
348
  {
-
 
349
    /* Process Unlocked */
-
 
350
    __HAL_UNLOCK(&pFlash);
-
 
351
  }
359
 
352
 
360
  return status;
353
  return status;
361
}
354
}
362
 
355
 
363
/**
356
/**
Line 365... Line 358...
365
  */
358
  */
366
 
359
 
367
/** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
360
/** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
368
 *  @brief   Option Bytes Programming functions
361
 *  @brief   Option Bytes Programming functions
369
 *
362
 *
370
@verbatim  
363
@verbatim
-
 
364
  ==============================================================================
-
 
365
                ##### Option Bytes Programming functions #####
371
  ==============================================================================
366
  ==============================================================================
372
                ##### Option Bytes Programming functions #####
-
 
373
  ==============================================================================  
-
 
374
 
367
 
375
    [..] Any operation of erase or program should follow these steps:
368
    [..] Any operation of erase or program should follow these steps:
376
    (#) Call the @ref HAL_FLASH_OB_Unlock() function to enable the Flash option control
369
    (#) Call the @ref HAL_FLASH_OB_Unlock() function to enable the Flash option control
377
        register access.
370
        register access.
378
    (#) Call following function to program the desired option bytes.
371
    (#) Call following function to program the desired option bytes.
379
        (++) @ref HAL_FLASHEx_OBProgram:
372
        (++) @ref HAL_FLASHEx_OBProgram:
380
         - To Enable/Disable the desired sector write protection.
373
         - To Enable/Disable the desired sector write protection.
381
         - To set the desired read Protection Level.
374
         - To set the desired read Protection Level.
Line 387... Line 380...
387
        to protect the option Bytes against possible unwanted operations).
380
        to protect the option Bytes against possible unwanted operations).
388
 
381
 
389
    [..] Proprietary code Read Out Protection (PcROP):
382
    [..] Proprietary code Read Out Protection (PcROP):
390
    (#) The PcROP sector is selected by using the same option bytes as the Write
383
    (#) The PcROP sector is selected by using the same option bytes as the Write
391
        protection (nWRPi bits). As a result, these 2 options are exclusive each other.
384
        protection (nWRPi bits). As a result, these 2 options are exclusive each other.
392
    (#) In order to activate the PcROP (change the function of the nWRPi option bits),
385
    (#) In order to activate the PcROP (change the function of the nWRPi option bits),
393
        the SPRMOD option bit must be activated.
386
        the SPRMOD option bit must be activated.
394
    (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
387
    (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
395
        means: if SPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
388
        means: if SPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
396
        is read/write protected.
389
        is read/write protected.
397
    (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
390
    (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
Line 405... Line 398...
405
 
398
 
406
/**
399
/**
407
  * @brief  Program option bytes
400
  * @brief  Program option bytes
408
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
401
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
409
  *         contains the configuration information for the programming.
402
  *         contains the configuration information for the programming.
410
  *
403
  *
411
  * @retval HAL_StatusTypeDef HAL Status
404
  * @retval HAL_StatusTypeDef HAL Status
412
  */
405
  */
413
HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
406
HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
414
{
407
{
415
  HAL_StatusTypeDef status = HAL_ERROR;
408
  HAL_StatusTypeDef status = HAL_ERROR;
416
 
409
 
417
  /* Process Locked */
410
  /* Process Locked */
418
  __HAL_LOCK(&pFlash);
411
  __HAL_LOCK(&pFlash);
419
 
412
 
420
  /* Check the parameters */
413
  /* Check the parameters */
421
  assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
414
  assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
Line 439... Line 432...
439
      /* Process Unlocked */
432
      /* Process Unlocked */
440
      __HAL_UNLOCK(&pFlash);
433
      __HAL_UNLOCK(&pFlash);
441
      return status;
434
      return status;
442
    }
435
    }
443
  }
436
  }
444
 
437
 
445
  /* Read protection configuration*/
438
  /* Read protection configuration*/
446
  if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
439
  if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
447
  {
440
  {
448
    status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
441
    status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
449
    if (status != HAL_OK)
442
    if (status != HAL_OK)
Line 451... Line 444...
451
      /* Process Unlocked */
444
      /* Process Unlocked */
452
      __HAL_UNLOCK(&pFlash);
445
      __HAL_UNLOCK(&pFlash);
453
      return status;
446
      return status;
454
    }
447
    }
455
  }
448
  }
456
 
449
 
457
  /* USER  configuration*/
450
  /* USER  configuration*/
458
  if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
451
  if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
459
  {
452
  {
460
    status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
453
    status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
461
                                 pOBInit->USERConfig & OB_STOP_NORST,
454
                                 pOBInit->USERConfig & OB_STOP_NORST,
462
                                 pOBInit->USERConfig & OB_STDBY_NORST);
455
                                 pOBInit->USERConfig & OB_STDBY_NORST);
463
    if (status != HAL_OK)
456
    if (status != HAL_OK)
464
    {
457
    {
465
      /* Process Unlocked */
458
      /* Process Unlocked */
Line 487... Line 480...
487
 
480
 
488
/**
481
/**
489
  * @brief   Get the Option byte configuration
482
  * @brief   Get the Option byte configuration
490
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
483
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
491
  *         contains the configuration information for the programming.
484
  *         contains the configuration information for the programming.
492
  *
485
  *
493
  * @retval None
486
  * @retval None
494
  */
487
  */
495
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
488
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
496
{
489
{
497
  pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
490
  pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
Line 501... Line 494...
501
 
494
 
502
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)    \
495
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)    \
503
 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
496
 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
504
 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD)  \
497
 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD)  \
505
 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
498
 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
506
   
499
 
507
  /*Get WRP2*/
500
  /*Get WRP2*/
508
  pOBInit->WRPSector32To63 = (uint32_t)(FLASH->WRPR2);
501
  pOBInit->WRPSector32To63 = (uint32_t)(FLASH->WRPR2);
509
 
502
 
510
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
503
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
511
 
504
 
512
#if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
505
#if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
513
 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE)  \
506
 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE)  \
514
 || defined(STM32L162xE)
507
 || defined(STM32L162xE)
515
   
508
 
516
  /*Get WRP3*/
509
  /*Get WRP3*/
517
  pOBInit->WRPSector64To95 = (uint32_t)(FLASH->WRPR3);
510
  pOBInit->WRPSector64To95 = (uint32_t)(FLASH->WRPR3);
518
 
511
 
519
#endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
512
#endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
520
 
513
 
521
#if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
514
#if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
522
 || defined(STM32L152xDX) || defined(STM32L162xDX)
515
 || defined(STM32L152xDX) || defined(STM32L162xDX)
523
 
516
 
524
  /*Get WRP4*/
517
  /*Get WRP4*/
525
  pOBInit->WRPSector96To127 = (uint32_t)(FLASH->WRPR4);
518
  pOBInit->WRPSector96To127 = (uint32_t)(FLASH->WRPR4);
Line 535... Line 528...
535
  /*Get BOR Level*/
528
  /*Get BOR Level*/
536
  pOBInit->BORLevel   = FLASH_OB_GetBOR();
529
  pOBInit->BORLevel   = FLASH_OB_GetBOR();
537
}
530
}
538
 
531
 
539
#if defined(FLASH_OBR_SPRMOD) || defined(FLASH_OBR_nRST_BFB2)
532
#if defined(FLASH_OBR_SPRMOD) || defined(FLASH_OBR_nRST_BFB2)
540
   
533
 
541
/**
534
/**
542
  * @brief  Program option bytes
535
  * @brief  Program option bytes
543
  * @note   This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
536
  * @note   This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
544
  * @param  pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
537
  * @param  pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
545
  *         contains the configuration information for the programming.
538
  *         contains the configuration information for the programming.
546
  *
539
  *
547
  * @retval HAL_StatusTypeDef HAL Status
540
  * @retval HAL_StatusTypeDef HAL Status
548
  */
541
  */
549
HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
542
HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
550
{
543
{
551
  HAL_StatusTypeDef status = HAL_ERROR;
544
  HAL_StatusTypeDef status = HAL_ERROR;
552
 
545
 
553
  /* Check the parameters */
546
  /* Check the parameters */
554
  assert_param(IS_OBEX(pAdvOBInit->OptionType));
547
  assert_param(IS_OBEX(pAdvOBInit->OptionType));
555
 
548
 
556
#if defined(FLASH_OBR_SPRMOD)
549
#if defined(FLASH_OBR_SPRMOD)
557
   
550
 
558
  /* Program PCROP option byte*/
551
  /* Program PCROP option byte*/
559
  if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
552
  if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
560
  {
553
  {
561
    /* Check the parameters */
554
    /* Check the parameters */
562
    assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
555
    assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
Line 569... Line 562...
569
        return status;
562
        return status;
570
      }
563
      }
571
    }
564
    }
572
    else
565
    else
573
    {
566
    {
574
      /* Disable of Write protection on the selected Sector*/
567
      /* Disable of Write protection on the selected Sector*/
575
      status = FLASH_OB_PCROPConfig(pAdvOBInit, DISABLE);
568
      status = FLASH_OB_PCROPConfig(pAdvOBInit, DISABLE);
576
      if (status != HAL_OK)
569
      if (status != HAL_OK)
577
      {
570
      {
578
        return status;
571
        return status;
579
      }
572
      }
580
    }
573
    }
581
  }
574
  }
582
 
575
 
583
#endif /* FLASH_OBR_SPRMOD */
576
#endif /* FLASH_OBR_SPRMOD */
584
 
577
 
585
#if defined(FLASH_OBR_nRST_BFB2)
578
#if defined(FLASH_OBR_nRST_BFB2)
586
   
579
 
587
  /* Program BOOT config option byte */
580
  /* Program BOOT config option byte */
588
  if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
581
  if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
589
  {
582
  {
590
    status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
583
    status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
591
  }
584
  }
592
 
585
 
593
#endif /* FLASH_OBR_nRST_BFB2 */
586
#endif /* FLASH_OBR_nRST_BFB2 */
594
 
587
 
595
  return status;
588
  return status;
596
}
589
}
597
 
590
 
598
/**
591
/**
599
  * @brief  Get the OBEX byte configuration
592
  * @brief  Get the OBEX byte configuration
600
  * @note   This function can be used only for Cat2  & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
593
  * @note   This function can be used only for Cat2  & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
601
  * @param  pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
594
  * @param  pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
602
  *         contains the configuration information for the programming.
595
  *         contains the configuration information for the programming.
603
  *
596
  *
604
  * @retval None
597
  * @retval None
605
  */
598
  */
606
void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
599
void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
607
{
600
{
608
  pAdvOBInit->OptionType = 0;
601
  pAdvOBInit->OptionType = 0U;
609
 
602
 
610
#if defined(FLASH_OBR_SPRMOD)
603
#if defined(FLASH_OBR_SPRMOD)
611
     
604
 
612
  pAdvOBInit->OptionType |= OPTIONBYTE_PCROP;
605
  pAdvOBInit->OptionType |= OPTIONBYTE_PCROP;
613
 
606
 
614
  /*Get PCROP state */
607
  /*Get PCROP state */
615
  pAdvOBInit->PCROPState = (FLASH->OBR & FLASH_OBR_SPRMOD) >> POSITION_VAL(FLASH_OBR_SPRMOD);
608
  pAdvOBInit->PCROPState = (FLASH->OBR & FLASH_OBR_SPRMOD) >> POSITION_VAL(FLASH_OBR_SPRMOD);
616
 
609
 
617
  /*Get PCROP protected sector from 0 to 31 */
610
  /*Get PCROP protected sector from 0 to 31 */
618
  pAdvOBInit->PCROPSector0To31 = FLASH->WRPR1;
611
  pAdvOBInit->PCROPSector0To31 = FLASH->WRPR1;
619
 
612
 
620
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
613
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
621
 
614
 
622
  /*Get PCROP protected sector from 32 to 63 */
615
  /*Get PCROP protected sector from 32 to 63 */
623
  pAdvOBInit->PCROPSector32To63 = FLASH->WRPR2;
616
  pAdvOBInit->PCROPSector32To63 = FLASH->WRPR2;
624
 
617
 
625
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
618
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
626
#endif /* FLASH_OBR_SPRMOD */
619
#endif /* FLASH_OBR_SPRMOD */
627
 
620
 
628
#if defined(FLASH_OBR_nRST_BFB2)
621
#if defined(FLASH_OBR_nRST_BFB2)
629
     
622
 
630
  pAdvOBInit->OptionType |= OPTIONBYTE_BOOTCONFIG;
623
  pAdvOBInit->OptionType |= OPTIONBYTE_BOOTCONFIG;
631
 
624
 
632
  /* Get Boot config OB */
625
  /* Get Boot config OB */
633
  pAdvOBInit->BootConfig = (FLASH->OBR & FLASH_OBR_nRST_BFB2) >> 16;
626
  pAdvOBInit->BootConfig = (FLASH->OBR & FLASH_OBR_nRST_BFB2) >> 16U;
634
 
627
 
635
#endif /* FLASH_OBR_nRST_BFB2 */
628
#endif /* FLASH_OBR_nRST_BFB2 */
636
}
629
}
637
 
630
 
638
#endif /* FLASH_OBR_SPRMOD || FLASH_OBR_nRST_BFB2 */
631
#endif /* FLASH_OBR_SPRMOD || FLASH_OBR_nRST_BFB2 */
Line 640... Line 633...
640
#if defined(FLASH_OBR_SPRMOD)
633
#if defined(FLASH_OBR_SPRMOD)
641
 
634
 
642
/**
635
/**
643
  * @brief  Select the Protection Mode (SPRMOD).
636
  * @brief  Select the Protection Mode (SPRMOD).
644
  * @note   This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
637
  * @note   This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
645
  * @note   Once SPRMOD bit is active, unprotection of a protected sector is not possible
638
  * @note   Once SPRMOD bit is active, unprotection of a protected sector is not possible
646
  * @note   Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
639
  * @note   Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
647
  * @retval HAL status
640
  * @retval HAL status
648
  */
641
  */
649
HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
642
HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
650
{
643
{
651
  HAL_StatusTypeDef status = HAL_OK;
644
  HAL_StatusTypeDef status = HAL_OK;
652
  uint16_t tmp1 = 0;
645
  uint16_t tmp1 = 0U;
653
  uint32_t tmp2 = 0;
646
  uint32_t tmp2 = 0U;
654
  uint8_t optiontmp = 0;
647
  uint8_t optiontmp = 0U;
655
  uint16_t optiontmp2 = 0;
648
  uint16_t optiontmp2 = 0U;
656
 
649
 
657
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
650
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
658
 
651
 
659
  /* Mask RDP Byte */
652
  /* Mask RDP Byte */
660
  optiontmp =  (uint8_t)(*(__IO uint8_t *)(OB_BASE));
653
  optiontmp =  (uint8_t)(*(__IO uint8_t *)(OB_BASE));
661
 
654
 
662
  /* Update Option Byte */
655
  /* Update Option Byte */
663
  optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
656
  optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
664
 
657
 
665
  /* calculate the option byte to write */
658
  /* calculate the option byte to write */
666
  tmp1 = (uint16_t)(~(optiontmp2 ));
659
  tmp1 = (uint16_t)(~(optiontmp2 ));
667
  tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
660
  tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
668
 
661
 
669
  if(status == HAL_OK)
662
  if(status == HAL_OK)
670
  {        
663
  {
671
    /* Clean the error context */
664
    /* Clean the error context */
672
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
665
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
673
 
666
 
674
    /* program PCRop */
667
    /* program PCRop */
675
    OB->RDP = tmp2;
668
    OB->RDP = tmp2;
676
   
669
 
677
    /* Wait for last operation to be completed */
670
    /* Wait for last operation to be completed */
678
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
671
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
679
  }
672
  }
680
 
673
 
681
  /* Return the Read protection operation Status */
674
  /* Return the Read protection operation Status */
682
  return status;            
675
  return status;
683
}
676
}
684
 
677
 
685
/**
678
/**
686
  * @brief  Deselect the Protection Mode (SPRMOD).
679
  * @brief  Deselect the Protection Mode (SPRMOD).
687
  * @note   This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
680
  * @note   This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
688
  * @note   Once SPRMOD bit is active, unprotection of a protected sector is not possible
681
  * @note   Once SPRMOD bit is active, unprotection of a protected sector is not possible
689
  * @note   Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
682
  * @note   Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
690
  * @retval HAL status
683
  * @retval HAL status
691
  */
684
  */
692
HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
685
HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
693
{
686
{
694
  HAL_StatusTypeDef status = HAL_OK;
687
  HAL_StatusTypeDef status = HAL_OK;
695
  uint16_t tmp1 = 0;
688
  uint16_t tmp1 = 0U;
696
  uint32_t tmp2 = 0;
689
  uint32_t tmp2 = 0U;
697
  uint8_t optiontmp = 0;
690
  uint8_t optiontmp = 0U;
698
  uint16_t optiontmp2 = 0;
691
  uint16_t optiontmp2 = 0U;
699
 
692
 
700
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
693
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
701
 
694
 
702
  /* Mask RDP Byte */
695
  /* Mask RDP Byte */
703
  optiontmp =  (uint8_t)(*(__IO uint8_t *)(OB_BASE));
696
  optiontmp =  (uint8_t)(*(__IO uint8_t *)(OB_BASE));
704
 
697
 
705
  /* Update Option Byte */
698
  /* Update Option Byte */
706
  optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
699
  optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
707
 
700
 
708
  /* calculate the option byte to write */
701
  /* calculate the option byte to write */
709
  tmp1 = (uint16_t)(~(optiontmp2 ));
702
  tmp1 = (uint16_t)(~(optiontmp2 ));
710
  tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
703
  tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
711
 
704
 
712
  if(status == HAL_OK)
705
  if(status == HAL_OK)
713
  {        
706
  {
714
    /* Clean the error context */
707
    /* Clean the error context */
715
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
708
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
716
 
709
 
717
    /* program PCRop */
710
    /* program PCRop */
718
    OB->RDP = tmp2;
711
    OB->RDP = tmp2;
719
   
712
 
720
    /* Wait for last operation to be completed */
713
    /* Wait for last operation to be completed */
721
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
714
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
722
  }
715
  }
723
 
716
 
724
  /* Return the Read protection operation Status */
717
  /* Return the Read protection operation Status */
725
  return status;            
718
  return status;
726
}
719
}
727
 
720
 
728
#endif /* FLASH_OBR_SPRMOD */
721
#endif /* FLASH_OBR_SPRMOD */
729
 
722
 
730
/**
723
/**
Line 732... Line 725...
732
  */
725
  */
733
 
726
 
734
/** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
727
/** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
735
 *  @brief   DATA EEPROM Programming functions
728
 *  @brief   DATA EEPROM Programming functions
736
 *
729
 *
737
@verbatim  
730
@verbatim
-
 
731
 ===============================================================================
-
 
732
                     ##### DATA EEPROM Programming functions #####
738
 ===============================================================================
733
 ===============================================================================
739
                     ##### DATA EEPROM Programming functions #####
-
 
740
 ===============================================================================  
-
 
741
 
734
 
742
    [..] Any operation of erase or program should follow these steps:
735
    [..] Any operation of erase or program should follow these steps:
743
    (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
736
    (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
744
        and Flash program erase control register access.
737
        and Flash program erase control register access.
745
    (#) Call the desired function to erase or program data.
738
    (#) Call the desired function to erase or program data.
746
    (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
739
    (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
Line 756... Line 749...
756
  * @retval HAL_StatusTypeDef HAL Status
749
  * @retval HAL_StatusTypeDef HAL Status
757
  */
750
  */
758
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
751
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
759
{
752
{
760
  if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
753
  if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
761
  {  
754
  {
762
    /* Unlocking the Data memory and FLASH_PECR register access*/
755
    /* Unlocking the Data memory and FLASH_PECR register access*/
763
    FLASH->PEKEYR = FLASH_PEKEY1;
756
    FLASH->PEKEYR = FLASH_PEKEY1;
764
    FLASH->PEKEYR = FLASH_PEKEY2;
757
    FLASH->PEKEYR = FLASH_PEKEY2;
765
  }
758
  }
766
  else
759
  else
767
  {
760
  {
768
    return HAL_ERROR;
761
    return HAL_ERROR;
769
  }
762
  }
770
  return HAL_OK;  
763
  return HAL_OK;
771
}
764
}
772
 
765
 
773
/**
766
/**
774
  * @brief  Locks the Data memory and FLASH_PECR register access.
767
  * @brief  Locks the Data memory and FLASH_PECR register access.
775
  * @retval HAL_StatusTypeDef HAL Status
768
  * @retval HAL_StatusTypeDef HAL Status
776
  */
769
  */
777
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
770
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
778
{
771
{
779
  /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
772
  /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
780
  SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
773
  SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
781
 
774
 
782
  return HAL_OK;
775
  return HAL_OK;
783
}
776
}
784
 
777
 
785
/**
778
/**
786
  * @brief  Erase a word in data memory.
779
  * @brief  Erase a word in data memory.
Line 788... Line 781...
788
  * @param  TypeErase  Indicate the way to erase at a specified address.
781
  * @param  TypeErase  Indicate the way to erase at a specified address.
789
  *         This parameter can be a value of @ref FLASH_Type_Program
782
  *         This parameter can be a value of @ref FLASH_Type_Program
790
  * @note   To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
783
  * @note   To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
791
  *         must be called before.
784
  *         must be called before.
792
  *         Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
785
  *         Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
793
  *         and Flash program erase control register access(recommended to protect
786
  *         and Flash program erase control register access(recommended to protect
794
  *         the DATA_EEPROM against possible unwanted operation).
787
  *         the DATA_EEPROM against possible unwanted operation).
795
  * @retval HAL_StatusTypeDef HAL Status
788
  * @retval HAL_StatusTypeDef HAL Status
796
  */
789
  */
797
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
790
HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
798
{
791
{
799
  HAL_StatusTypeDef status = HAL_OK;
792
  HAL_StatusTypeDef status = HAL_OK;
800
 
793
 
801
  /* Check the parameters */
794
  /* Check the parameters */
802
  assert_param(IS_TYPEPROGRAMDATA(TypeErase));
795
  assert_param(IS_TYPEERASEDATA(TypeErase));
803
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
796
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
804
 
797
 
805
  /* Wait for last operation to be completed */
798
  /* Wait for last operation to be completed */
806
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
799
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
807
 
800
 
808
  if(status == HAL_OK)
801
  if(status == HAL_OK)
809
  {
802
  {
810
    /* Clean the error context */
803
    /* Clean the error context */
811
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
804
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
812
 
805
 
813
    if(TypeErase == FLASH_TYPEERASEDATA_WORD)
806
    if(TypeErase == FLASH_TYPEERASEDATA_WORD)
814
    {
807
    {
815
      /* Write 00000000h to valid address in the data memory */
808
      /* Write 00000000h to valid address in the data memory */
816
      *(__IO uint32_t *) Address = 0x00000000;
809
      *(__IO uint32_t *) Address = 0x00000000U;
817
    }
810
    }
818
 
811
 
819
    if(TypeErase == FLASH_TYPEERASEDATA_HALFWORD)
812
    if(TypeErase == FLASH_TYPEERASEDATA_HALFWORD)
820
    {
813
    {
821
      /* Write 0000h to valid address in the data memory */
814
      /* Write 0000h to valid address in the data memory */
Line 828... Line 821...
828
      *(__IO uint8_t *) Address = (uint8_t)0x00;
821
      *(__IO uint8_t *) Address = (uint8_t)0x00;
829
    }
822
    }
830
 
823
 
831
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
824
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
832
  }
825
  }
833
   
826
 
834
  /* Return the erase status */
827
  /* Return the erase status */
835
  return status;
828
  return status;
836
}  
829
}
837
 
830
 
838
/**
831
/**
839
  * @brief  Program word at a specified address
832
  * @brief  Program word at a specified address
840
  * @note   To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
833
  * @note   To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
841
  *         must be called before.
834
  *         must be called before.
842
  *         Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
835
  *         Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
843
  *         and Flash program erase control register access(recommended to protect
836
  *         and Flash program erase control register access(recommended to protect
844
  *         the DATA_EEPROM against possible unwanted operation).
837
  *         the DATA_EEPROM against possible unwanted operation).
845
  * @note   The function @ref HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
838
  * @note   The function @ref HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
846
  *         this function to configure the Fixed Time Programming.
839
  *         this function to configure the Fixed Time Programming.
847
  * @param  TypeProgram  Indicate the way to program at a specified address.
840
  * @param  TypeProgram  Indicate the way to program at a specified address.
848
  *         This parameter can be a value of @ref FLASHEx_Type_Program_Data
841
  *         This parameter can be a value of @ref FLASHEx_Type_Program_Data
849
  * @param  Address  specifies the address to be programmed.
842
  * @param  Address  specifie the address to be programmed.
850
  * @param  Data specifies the data to be programmed
843
  * @param  Data     specifie the data to be programmed
851
  *
844
  *
852
  * @retval HAL_StatusTypeDef HAL Status
845
  * @retval HAL_StatusTypeDef HAL Status
853
  */
846
  */
854
 
847
 
855
HAL_StatusTypeDef   HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
848
HAL_StatusTypeDef   HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
856
{
849
{
857
  HAL_StatusTypeDef status = HAL_ERROR;
850
  HAL_StatusTypeDef status = HAL_ERROR;
858
 
851
 
859
  /* Process Locked */
852
  /* Process Locked */
860
  __HAL_LOCK(&pFlash);
853
  __HAL_LOCK(&pFlash);
861
 
854
 
862
  /* Check the parameters */
855
  /* Check the parameters */
863
  assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
856
  assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
864
 
857
 
865
  /* Wait for last operation to be completed */
858
  /* Wait for last operation to be completed */
866
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
859
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
867
 
860
 
868
  if(status == HAL_OK)
861
  if(status == HAL_OK)
869
  {
862
  {
870
    /* Clean the error context */
863
    /* Clean the error context */
871
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
864
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
872
 
865
 
873
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTBYTE)
-
 
874
    {
-
 
875
      /*Program word (8-bit) at a specified address.*/
-
 
876
      status = FLASH_DATAEEPROM_FastProgramByte(Address, (uint8_t) Data);
-
 
877
    }
-
 
878
   
-
 
879
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTHALFWORD)
-
 
880
    {
-
 
881
      /* Program halfword (16-bit) at a specified address.*/
-
 
882
      status = FLASH_DATAEEPROM_FastProgramHalfWord(Address, (uint16_t) Data);
-
 
883
    }    
-
 
884
   
-
 
885
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTWORD)
-
 
886
    {
-
 
887
      /* Program word (32-bit) at a specified address.*/
-
 
888
      status = FLASH_DATAEEPROM_FastProgramWord(Address, (uint32_t) Data);
-
 
889
    }
-
 
890
   
-
 
891
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
866
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
892
    {
867
    {
893
      /* Program word (32-bit) at a specified address.*/
868
      /* Program word (32-bit) at a specified address.*/
894
      status = FLASH_DATAEEPROM_ProgramWord(Address, (uint32_t) Data);
869
      status = FLASH_DATAEEPROM_ProgramWord(Address, (uint32_t) Data);
895
    }
870
    }
896
       
-
 
897
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
871
    else if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
898
    {
872
    {
899
      /* Program halfword (16-bit) at a specified address.*/
873
      /* Program halfword (16-bit) at a specified address.*/
900
      status = FLASH_DATAEEPROM_ProgramHalfWord(Address, (uint16_t) Data);
874
      status = FLASH_DATAEEPROM_ProgramHalfWord(Address, (uint16_t) Data);
901
    }
875
    }
902
       
-
 
903
    if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
876
    else if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
904
    {
877
    {
905
      /* Program byte (8-bit) at a specified address.*/
878
      /* Program byte (8-bit) at a specified address.*/
906
      status = FLASH_DATAEEPROM_ProgramByte(Address, (uint8_t) Data);
879
      status = FLASH_DATAEEPROM_ProgramByte(Address, (uint8_t) Data);
907
    }
880
    }
-
 
881
    else if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTBYTE)
-
 
882
    {
-
 
883
      /*Program word (8-bit) at a specified address.*/
-
 
884
      status = FLASH_DATAEEPROM_FastProgramByte(Address, (uint8_t) Data);
-
 
885
    }
-
 
886
    else if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTHALFWORD)
-
 
887
    {
-
 
888
      /* Program halfword (16-bit) at a specified address.*/
-
 
889
      status = FLASH_DATAEEPROM_FastProgramHalfWord(Address, (uint16_t) Data);
-
 
890
    }
-
 
891
    else if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTWORD)
-
 
892
    {
-
 
893
      /* Program word (32-bit) at a specified address.*/
-
 
894
      status = FLASH_DATAEEPROM_FastProgramWord(Address, (uint32_t) Data);
-
 
895
    }
-
 
896
    else
-
 
897
    {
-
 
898
      status = HAL_ERROR;
-
 
899
    }
-
 
900
 
908
  }
901
  }
909
 
902
 
910
  /* Process Unlocked */
903
  /* Process Unlocked */
911
  __HAL_UNLOCK(&pFlash);
904
  __HAL_UNLOCK(&pFlash);
912
 
905
 
913
  return status;
906
  return status;
914
}
907
}
Line 950... Line 943...
950
*/
943
*/
951
/**
944
/**
952
  * @brief  Enables or disables the read out protection.
945
  * @brief  Enables or disables the read out protection.
953
  * @note   To correctly run this function, the @ref HAL_FLASH_OB_Unlock() function
946
  * @note   To correctly run this function, the @ref HAL_FLASH_OB_Unlock() function
954
  *         must be called before.
947
  *         must be called before.
955
  * @param  OB_RDP specifies the read protection level.
948
  * @param  OB_RDP specifies the read protection level.
956
  *   This parameter can be:
949
  *   This parameter can be:
957
  *     @arg @ref OB_RDP_LEVEL_0 No protection
950
  *     @arg @ref OB_RDP_LEVEL_0 No protection
958
  *     @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
951
  *     @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
959
  *     @arg @ref OB_RDP_LEVEL_2 Chip protection
952
  *     @arg @ref OB_RDP_LEVEL_2 Chip protection
960
  *
953
  *
961
  *  !!!Warning!!! When enabling OB_RDP_LEVEL_2 it's no more possible to go back to level 1 or 0
954
  *  !!!Warning!!! When enabling OB_RDP_LEVEL_2 it's no more possible to go back to level 1 or 0
962
  *  
955
  *
963
  * @retval HAL status
956
  * @retval HAL status
964
  */
957
  */
965
static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
958
static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
966
{
959
{
967
  HAL_StatusTypeDef status = HAL_OK;
960
  HAL_StatusTypeDef status = HAL_OK;
968
  uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
961
  uint32_t tmp1 = 0U, tmp2 = 0U, tmp3 = 0U;
969
 
962
 
970
  /* Check the parameters */
963
  /* Check the parameters */
971
  assert_param(IS_OB_RDP(OB_RDP));
964
  assert_param(IS_OB_RDP(OB_RDP));
972
 
965
 
973
  tmp1 = (uint32_t)(OB->RDP & FLASH_OBR_RDPRT);
966
  tmp1 = (uint32_t)(OB->RDP & FLASH_OBR_RDPRT);
974
 
967
 
975
  /* According to errata sheet, DocID022054 Rev 5, par2.1.5
968
  /* According to errata sheet, DocID022054 Rev 5, par2.1.5
976
  Before setting Level0 in the RDP register, check that the current level is not equal to Level0.
969
  Before setting Level0 in the RDP register, check that the current level is not equal to Level0.
977
  If the current level is not equal to Level0, Level0 can be activated.
970
  If the current level is not equal to Level0, Level0 can be activated.
978
  If the current level is Level0 then the RDP register must not be written again with Level0. */
971
  If the current level is Level0 then the RDP register must not be written again with Level0. */
979
 
972
 
980
  if ((tmp1 == OB_RDP_LEVEL_0) && (OB_RDP == OB_RDP_LEVEL_0))
973
  if ((tmp1 == OB_RDP_LEVEL_0) && (OB_RDP == OB_RDP_LEVEL_0))
981
  {
974
  {
982
    /*current level is Level0 then the RDP register must not be written again with Level0. */
975
    /*current level is Level0 then the RDP register must not be written again with Level0. */
983
    status = HAL_ERROR;
976
    status = HAL_ERROR;
984
  }
977
  }
985
  else
978
  else
986
  {
979
  {
987
#if defined(FLASH_OBR_SPRMOD)
980
#if defined(FLASH_OBR_SPRMOD)
988
    /* Mask SPRMOD bit */
981
    /* Mask SPRMOD bit */
989
    tmp3 = (uint32_t)(OB->RDP & FLASH_OBR_SPRMOD);
982
    tmp3 = (uint32_t)(OB->RDP & FLASH_OBR_SPRMOD);
990
#endif
983
#endif
991
 
984
 
992
    /* calculate the option byte to write */
985
    /* calculate the option byte to write */
993
    tmp1 = (~((uint32_t)(OB_RDP | tmp3)));
986
    tmp1 = (~((uint32_t)(OB_RDP | tmp3)));
994
    tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)(OB_RDP | tmp3)));
987
    tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)(OB_RDP | tmp3)));
995
 
988
 
996
    /* Wait for last operation to be completed */
989
    /* Wait for last operation to be completed */
997
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
990
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
998
 
991
 
999
    if(status == HAL_OK)
992
    if(status == HAL_OK)
Line 1015... Line 1008...
1015
 
1008
 
1016
/**
1009
/**
1017
  * @brief  Programs the FLASH brownout reset threshold level Option Byte.
1010
  * @brief  Programs the FLASH brownout reset threshold level Option Byte.
1018
  * @param  OB_BOR Selects the brownout reset threshold level.
1011
  * @param  OB_BOR Selects the brownout reset threshold level.
1019
  *   This parameter can be one of the following values:
1012
  *   This parameter can be one of the following values:
1020
  *     @arg @ref OB_BOR_OFF BOR is disabled at power down, the reset is asserted when the VDD
1013
  *     @arg @ref OB_BOR_OFF BOR is disabled at power down, the reset is asserted when the VDD
1021
  *                      power supply reaches the PDR(Power Down Reset) threshold (1.5V)
1014
  *                      power supply reaches the PDR(Power Down Reset) threshold (1.5V)
1022
  *     @arg @ref OB_BOR_LEVEL1 BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
1015
  *     @arg @ref OB_BOR_LEVEL1 BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
1023
  *     @arg @ref OB_BOR_LEVEL2 BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
1016
  *     @arg @ref OB_BOR_LEVEL2 BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
1024
  *     @arg @ref OB_BOR_LEVEL3 BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
1017
  *     @arg @ref OB_BOR_LEVEL3 BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
1025
  *     @arg @ref OB_BOR_LEVEL4 BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
1018
  *     @arg @ref OB_BOR_LEVEL4 BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
Line 1027... Line 1020...
1027
  * @retval HAL status
1020
  * @retval HAL status
1028
  */
1021
  */
1029
static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
1022
static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
1030
{
1023
{
1031
  HAL_StatusTypeDef status = HAL_OK;
1024
  HAL_StatusTypeDef status = HAL_OK;
1032
  uint32_t tmp = 0, tmp1 = 0;
1025
  uint32_t tmp = 0U, tmp1 = 0U;
1033
 
1026
 
1034
  /* Check the parameters */
1027
  /* Check the parameters */
1035
  assert_param(IS_OB_BOR_LEVEL(OB_BOR));
1028
  assert_param(IS_OB_BOR_LEVEL(OB_BOR));
1036
 
1029
 
1037
  /* Get the User Option byte register */
1030
  /* Get the User Option byte register */
1038
  tmp1 = OB->USER & ((~FLASH_OBR_BOR_LEV) >> 16);
1031
  tmp1 = OB->USER & ((~FLASH_OBR_BOR_LEV) >> 16U);
1039
 
1032
 
1040
  /* Calculate the option byte to write - [0xFF | nUSER | 0x00 | USER]*/
1033
  /* Calculate the option byte to write - [0xFFU | nUSER | 0x00U | USER]*/
1041
  tmp = (uint32_t)~((OB_BOR | tmp1)) << 16;
1034
  tmp = (uint32_t)~((OB_BOR | tmp1)) << 16U;
1042
  tmp |= (OB_BOR | tmp1);
1035
  tmp |= (OB_BOR | tmp1);
1043
   
1036
 
1044
  /* Wait for last operation to be completed */
1037
  /* Wait for last operation to be completed */
1045
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1038
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1046
 
1039
 
1047
  if(status == HAL_OK)
1040
  if(status == HAL_OK)
1048
  {  
1041
  {
1049
    /* Clean the error context */
1042
    /* Clean the error context */
1050
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1043
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1051
 
1044
 
1052
    /* Write the BOR Option Byte */            
1045
    /* Write the BOR Option Byte */
1053
    OB->USER = tmp;
1046
    OB->USER = tmp;
1054
 
1047
 
1055
    /* Wait for last operation to be completed */
1048
    /* Wait for last operation to be completed */
1056
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1049
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1057
  }
1050
  }
1058
 
1051
 
1059
  /* Return the Option Byte BOR programmation Status */
1052
  /* Return the Option Byte BOR Programming Status */
1060
  return status;
1053
  return status;
1061
}
1054
}
1062
 
1055
 
1063
/**
1056
/**
1064
  * @brief  Returns the FLASH User Option Bytes values.
1057
  * @brief  Returns the FLASH User Option Bytes values.
1065
  * @retval The FLASH User Option Bytes.
1058
  * @retval The FLASH User Option Bytes.
1066
  */
1059
  */
1067
static uint8_t FLASH_OB_GetUser(void)
1060
static uint8_t FLASH_OB_GetUser(void)
1068
{
1061
{
1069
  /* Return the User Option Byte */
1062
  /* Return the User Option Byte */
1070
  return (uint8_t)((FLASH->OBR & FLASH_OBR_USER) >> 16);
1063
  return (uint8_t)((FLASH->OBR & (FLASH_OBR_IWDG_SW | FLASH_OBR_nRST_STOP | FLASH_OBR_nRST_STDBY)) >> 16U);
1071
}
1064
}
1072
 
1065
 
1073
/**
1066
/**
1074
  * @brief  Returns the FLASH Read Protection level.
1067
  * @brief  Returns the FLASH Read Protection level.
1075
  * @retval FLASH RDP level
1068
  * @retval FLASH RDP level
Line 1078... Line 1071...
1078
  *            @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
1071
  *            @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
1079
  *            @arg @ref OB_RDP_LEVEL_2 Full chip protection
1072
  *            @arg @ref OB_RDP_LEVEL_2 Full chip protection
1080
  */
1073
  */
1081
static uint8_t FLASH_OB_GetRDP(void)
1074
static uint8_t FLASH_OB_GetRDP(void)
1082
{
1075
{
1083
  return (uint8_t)(FLASH->OBR & FLASH_OBR_RDPRT);
1076
  uint8_t rdp_level = (uint8_t)(FLASH->OBR & FLASH_OBR_RDPRT);
-
 
1077
 
-
 
1078
  if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
-
 
1079
  {
-
 
1080
    return (OB_RDP_LEVEL_1);
-
 
1081
  }
-
 
1082
  else
-
 
1083
  {
-
 
1084
    return (rdp_level);
-
 
1085
  }
1084
}
1086
}
1085
 
1087
 
1086
/**
1088
/**
1087
  * @brief  Returns the FLASH BOR level.
1089
  * @brief  Returns the FLASH BOR level.
1088
  * @retval The BOR level Option Bytes.
1090
  * @retval The BOR level Option Bytes.
1089
  */
1091
  */
1090
static uint8_t FLASH_OB_GetBOR(void)
1092
static uint8_t FLASH_OB_GetBOR(void)
1091
{
1093
{
1092
  /* Return the BOR level */
1094
  /* Return the BOR level */
1093
  return (uint8_t)((FLASH->OBR & (uint32_t)FLASH_OBR_BOR_LEV) >> 16);
1095
  return (uint8_t)((FLASH->OBR & (uint32_t)FLASH_OBR_BOR_LEV) >> 16U);
1094
}
1096
}
1095
 
1097
 
1096
/**
1098
/**
1097
  * @brief  Write protects the desired pages of the first 64KB of the Flash.
1099
  * @brief  Write protects the desired pages of the first 64KB of the Flash.
1098
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
1100
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
Line 1102... Line 1104...
1102
  * @retval HAL_StatusTypeDef
1104
  * @retval HAL_StatusTypeDef
1103
  */
1105
  */
1104
static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState)
1106
static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState)
1105
{
1107
{
1106
  HAL_StatusTypeDef status = HAL_OK;
1108
  HAL_StatusTypeDef status = HAL_OK;
1107
 
1109
 
1108
  /* Wait for last operation to be completed */
1110
  /* Wait for last operation to be completed */
1109
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1111
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1110
 
1112
 
1111
  if(status == HAL_OK)
1113
  if(status == HAL_OK)
1112
  {
1114
  {
1113
    /* Clean the error context */
1115
    /* Clean the error context */
1114
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1116
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1115
 
1117
 
1116
    /* WRP for sector between 0 to 31 */
1118
    /* WRP for sector between 0 to 31 */
1117
    if (pOBInit->WRPSector0To31 != 0)
1119
    if (pOBInit->WRPSector0To31 != 0U)
1118
    {
1120
    {
1119
      FLASH_OB_WRPConfigWRP1OrPCROP1(pOBInit->WRPSector0To31, NewState);
1121
      FLASH_OB_WRPConfigWRP1OrPCROP1(pOBInit->WRPSector0To31, NewState);
1120
    }
1122
    }
1121
   
1123
 
1122
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)    \
1124
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)    \
1123
 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
1125
 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
1124
 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD)  \
1126
 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD)  \
1125
 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
1127
 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
1126
     
1128
 
1127
    /* Pages for Cat3, Cat4 & Cat5 devices*/
1129
    /* Pages for Cat3, Cat4 & Cat5 devices*/
1128
    /* WRP for sector between 32 to 63 */
1130
    /* WRP for sector between 32 to 63 */
1129
    if (pOBInit->WRPSector32To63 != 0)
1131
    if (pOBInit->WRPSector32To63 != 0U)
1130
    {
1132
    {
1131
      FLASH_OB_WRPConfigWRP2OrPCROP2(pOBInit->WRPSector32To63, NewState);
1133
      FLASH_OB_WRPConfigWRP2OrPCROP2(pOBInit->WRPSector32To63, NewState);
1132
    }
1134
    }
1133
   
1135
 
1134
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
1136
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
1135
 
1137
 
1136
#if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
1138
#if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
1137
 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE)  \
1139
 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE)  \
1138
 || defined(STM32L162xE)
1140
 || defined(STM32L162xE)
1139
     
1141
 
1140
    /* Pages for devices with FLASH >= 256KB*/
1142
    /* Pages for devices with FLASH >= 256KB*/
1141
    /* WRP for sector between 64 to 95 */
1143
    /* WRP for sector between 64 to 95 */
1142
    if (pOBInit->WRPSector64To95 != 0)
1144
    if (pOBInit->WRPSector64To95 != 0U)
1143
    {
1145
    {
1144
      FLASH_OB_WRPConfigWRP3(pOBInit->WRPSector64To95, NewState);
1146
      FLASH_OB_WRPConfigWRP3(pOBInit->WRPSector64To95, NewState);
1145
    }
1147
    }
1146
   
1148
 
1147
#endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1149
#endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1148
 
1150
 
1149
#if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
1151
#if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
1150
 || defined(STM32L152xDX) || defined(STM32L162xDX)
1152
 || defined(STM32L152xDX) || defined(STM32L162xDX)
1151
 
1153
 
1152
    /* Pages for Cat5 devices*/
1154
    /* Pages for Cat5 devices*/
1153
    /* WRP for sector between 96 to 127 */
1155
    /* WRP for sector between 96 to 127 */
1154
    if (pOBInit->WRPSector96To127 != 0)
1156
    if (pOBInit->WRPSector96To127 != 0U)
1155
    {
1157
    {
1156
      FLASH_OB_WRPConfigWRP4(pOBInit->WRPSector96To127, NewState);
1158
      FLASH_OB_WRPConfigWRP4(pOBInit->WRPSector96To127, NewState);
1157
    }
1159
    }
1158
   
1160
 
1159
#endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
1161
#endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
1160
 
1162
 
1161
    /* Wait for last operation to be completed */
1163
    /* Wait for last operation to be completed */
1162
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1164
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1163
  }
1165
  }
1164
 
1166
 
1165
  /* Return the write protection operation Status */
1167
  /* Return the write protection operation Status */
1166
  return status;      
1168
  return status;
1167
}
1169
}
1168
 
1170
 
1169
#if defined(STM32L151xBA) || defined(STM32L152xBA) || defined(STM32L151xC) || defined(STM32L152xC) \
1171
#if defined(STM32L151xBA) || defined(STM32L152xBA) || defined(STM32L151xC) || defined(STM32L152xC) \
1170
 || defined(STM32L162xC)
1172
 || defined(STM32L162xC)
1171
/**
1173
/**
1172
  * @brief  Enables the read/write protection (PCROP) of the desired
1174
  * @brief  Enables the read/write protection (PCROP) of the desired
1173
  *         sectors.
1175
  *         sectors.
1174
  * @note   This function can be used only for Cat2 & Cat3 devices
1176
  * @note   This function can be used only for Cat2 & Cat3 devices
1175
  * @param  pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
1177
  * @param  pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
1176
  *         contains PCROP parameters.
1178
  *         contains PCROP parameters.
1177
  * @param  NewState new state of the specified FLASH Pages read/Write protection.
1179
  * @param  NewState new state of the specified FLASH Pages read/Write protection.
Line 1180... Line 1182...
1180
  */
1182
  */
1181
static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState)
1183
static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState)
1182
{
1184
{
1183
  HAL_StatusTypeDef status = HAL_OK;
1185
  HAL_StatusTypeDef status = HAL_OK;
1184
  FunctionalState pcropstate = DISABLE;
1186
  FunctionalState pcropstate = DISABLE;
1185
 
1187
 
1186
  /* Wait for last operation to be completed */
1188
  /* Wait for last operation to be completed */
1187
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1189
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1188
 
1190
 
1189
  /* Invert state to use same function of WRP */
1191
  /* Invert state to use same function of WRP */
1190
  if (NewState == DISABLE)
1192
  if (NewState == DISABLE)
1191
  {
1193
  {
1192
    pcropstate = ENABLE;
1194
    pcropstate = ENABLE;
1193
  }
1195
  }
1194
       
1196
 
1195
  if(status == HAL_OK)
1197
  if(status == HAL_OK)
1196
  {
1198
  {
1197
    /* Clean the error context */
1199
    /* Clean the error context */
1198
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1200
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1199
 
1201
 
1200
    /* Pages for Cat2 devices*/
1202
    /* Pages for Cat2 devices*/
1201
    /* PCROP for sector between 0 to 31 */
1203
    /* PCROP for sector between 0 to 31 */
1202
    if (pAdvOBInit->PCROPSector0To31 != 0)
1204
    if (pAdvOBInit->PCROPSector0To31 != 0U)
1203
    {
1205
    {
1204
      FLASH_OB_WRPConfigWRP1OrPCROP1(pAdvOBInit->PCROPSector0To31, pcropstate);
1206
      FLASH_OB_WRPConfigWRP1OrPCROP1(pAdvOBInit->PCROPSector0To31, pcropstate);
1205
    }
1207
    }
1206
   
1208
 
1207
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
1209
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
1208
 
1210
 
1209
    /* Pages for Cat3 devices*/
1211
    /* Pages for Cat3 devices*/
1210
    /* WRP for sector between 32 to 63 */
1212
    /* WRP for sector between 32 to 63 */
1211
    if (pAdvOBInit->PCROPSector32To63 != 0)
1213
    if (pAdvOBInit->PCROPSector32To63 != 0U)
1212
    {
1214
    {
1213
      FLASH_OB_WRPConfigWRP2OrPCROP2(pAdvOBInit->PCROPSector32To63, pcropstate);
1215
      FLASH_OB_WRPConfigWRP2OrPCROP2(pAdvOBInit->PCROPSector32To63, pcropstate);
1214
    }
1216
    }
1215
   
1217
 
1216
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC  */
1218
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC  */
1217
   
1219
 
1218
    /* Wait for last operation to be completed */
1220
    /* Wait for last operation to be completed */
1219
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1221
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1220
  }
1222
  }
1221
 
1223
 
1222
  /* Return the write protection operation Status */
1224
  /* Return the write protection operation Status */
1223
  return status;      
1225
  return status;
1224
}
1226
}
1225
#endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1227
#endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1226
 
1228
 
1227
/**
1229
/**
1228
  * @brief  Write protects the desired pages of the first 128KB of the Flash.
1230
  * @brief  Write protects the desired pages of the first 128KB of the Flash.
Line 1232... Line 1234...
1232
  *   This parameter can be: ENABLE or DISABLE.
1234
  *   This parameter can be: ENABLE or DISABLE.
1233
  * @retval None
1235
  * @retval None
1234
  */
1236
  */
1235
static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState)
1237
static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState)
1236
{
1238
{
1237
  uint32_t wrp01data = 0, wrp23data = 0;
1239
  uint32_t wrp01data = 0U, wrp23data = 0U;
1238
 
1240
 
1239
  uint32_t tmp1 = 0, tmp2 = 0;
1241
  uint32_t tmp1 = 0U, tmp2 = 0U;
1240
 
1242
 
1241
  /* Check the parameters */
1243
  /* Check the parameters */
1242
  assert_param(IS_OB_WRP(WRP1OrPCROP1));
1244
  assert_param(IS_OB_WRP(WRP1OrPCROP1));
1243
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1245
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1244
 
1246
 
1245
  if (NewState != DISABLE)
1247
  if (NewState != DISABLE)
1246
  {
1248
  {
1247
    wrp01data = (uint16_t)(((WRP1OrPCROP1 & WRP_MASK_LOW) | OB->WRP01));
1249
    wrp01data = (uint16_t)(((WRP1OrPCROP1 & WRP_MASK_LOW) | OB->WRP01));
1248
    wrp23data = (uint16_t)((((WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 | OB->WRP23)));
1250
    wrp23data = (uint16_t)((((WRP1OrPCROP1 & WRP_MASK_HIGH)>>16U | OB->WRP23)));
1249
    tmp1 = (uint32_t)(~(wrp01data) << 16)|(wrp01data);
1251
    tmp1 = (uint32_t)(~(wrp01data) << 16U)|(wrp01data);
1250
    OB->WRP01 = tmp1;
1252
    OB->WRP01 = tmp1;
1251
 
1253
 
1252
    tmp2 = (uint32_t)(~(wrp23data) << 16)|(wrp23data);
1254
    tmp2 = (uint32_t)(~(wrp23data) << 16U)|(wrp23data);
1253
    OB->WRP23 = tmp2;      
1255
    OB->WRP23 = tmp2;
1254
  }
1256
  }
1255
  else
1257
  else
1256
  {
1258
  {
1257
    wrp01data = (uint16_t)(~WRP1OrPCROP1 & (WRP_MASK_LOW & OB->WRP01));
1259
    wrp01data = (uint16_t)(~WRP1OrPCROP1 & (WRP_MASK_LOW & OB->WRP01));
1258
    wrp23data = (uint16_t)((((~WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 & OB->WRP23)));
1260
    wrp23data = (uint16_t)((((~WRP1OrPCROP1 & WRP_MASK_HIGH)>>16U & OB->WRP23)));
1259
 
1261
 
1260
    tmp1 = (uint32_t)((~wrp01data) << 16)|(wrp01data);
1262
    tmp1 = (uint32_t)((~wrp01data) << 16U)|(wrp01data);
1261
    OB->WRP01 = tmp1;
1263
    OB->WRP01 = tmp1;
1262
   
1264
 
1263
    tmp2 = (uint32_t)((~wrp23data) << 16)|(wrp23data);
1265
    tmp2 = (uint32_t)((~wrp23data) << 16U)|(wrp23data);
1264
    OB->WRP23 = tmp2;
1266
    OB->WRP23 = tmp2;
1265
  }
1267
  }
1266
}
1268
}
1267
 
1269
 
1268
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)    \
1270
#if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)    \
Line 1278... Line 1280...
1278
  *   This parameter can be: ENABLE or DISABLE.
1280
  *   This parameter can be: ENABLE or DISABLE.
1279
  * @retval None
1281
  * @retval None
1280
  */
1282
  */
1281
static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState)
1283
static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState)
1282
{
1284
{
1283
  uint32_t wrp45data = 0, wrp67data = 0;
1285
  uint32_t wrp45data = 0U, wrp67data = 0U;
1284
 
1286
 
1285
  uint32_t tmp1 = 0, tmp2 = 0;
1287
  uint32_t tmp1 = 0U, tmp2 = 0U;
1286
 
1288
 
1287
  /* Check the parameters */
1289
  /* Check the parameters */
1288
  assert_param(IS_OB_WRP(WRP2OrPCROP2));
1290
  assert_param(IS_OB_WRP(WRP2OrPCROP2));
1289
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1291
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1290
 
1292
 
1291
  if (NewState != DISABLE)
1293
  if (NewState != DISABLE)
1292
  {
1294
  {
1293
    wrp45data = (uint16_t)(((WRP2OrPCROP2 & WRP_MASK_LOW) | OB->WRP45));
1295
    wrp45data = (uint16_t)(((WRP2OrPCROP2 & WRP_MASK_LOW) | OB->WRP45));
1294
    wrp67data = (uint16_t)((((WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 | OB->WRP67)));
1296
    wrp67data = (uint16_t)((((WRP2OrPCROP2 & WRP_MASK_HIGH)>>16U | OB->WRP67)));
1295
    tmp1 = (uint32_t)(~(wrp45data) << 16)|(wrp45data);
1297
    tmp1 = (uint32_t)(~(wrp45data) << 16U)|(wrp45data);
1296
    OB->WRP45 = tmp1;
1298
    OB->WRP45 = tmp1;
1297
   
1299
 
1298
    tmp2 = (uint32_t)(~(wrp67data) << 16)|(wrp67data);
1300
    tmp2 = (uint32_t)(~(wrp67data) << 16U)|(wrp67data);
1299
    OB->WRP67 = tmp2;
1301
    OB->WRP67 = tmp2;
1300
  }
1302
  }
1301
  else
1303
  else
1302
  {
1304
  {
1303
    wrp45data = (uint16_t)(~WRP2OrPCROP2 & (WRP_MASK_LOW & OB->WRP45));
1305
    wrp45data = (uint16_t)(~WRP2OrPCROP2 & (WRP_MASK_LOW & OB->WRP45));
1304
    wrp67data = (uint16_t)((((~WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 & OB->WRP67)));
1306
    wrp67data = (uint16_t)((((~WRP2OrPCROP2 & WRP_MASK_HIGH)>>16U & OB->WRP67)));
1305
   
1307
 
1306
    tmp1 = (uint32_t)((~wrp45data) << 16)|(wrp45data);
1308
    tmp1 = (uint32_t)((~wrp45data) << 16U)|(wrp45data);
1307
    OB->WRP45 = tmp1;
1309
    OB->WRP45 = tmp1;
1308
   
1310
 
1309
    tmp2 = (uint32_t)((~wrp67data) << 16)|(wrp67data);
1311
    tmp2 = (uint32_t)((~wrp67data) << 16U)|(wrp67data);
1310
    OB->WRP67 = tmp2;
1312
    OB->WRP67 = tmp2;
1311
  }
1313
  }
1312
}
1314
}
1313
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
1315
#endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
1314
 
1316
 
Line 1324... Line 1326...
1324
  *   This parameter can be: ENABLE or DISABLE.
1326
  *   This parameter can be: ENABLE or DISABLE.
1325
  * @retval None
1327
  * @retval None
1326
  */
1328
  */
1327
static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState)
1329
static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState)
1328
{
1330
{
1329
  uint32_t wrp89data = 0, wrp1011data = 0;
1331
  uint32_t wrp89data = 0U, wrp1011data = 0U;
1330
 
1332
 
1331
  uint32_t tmp1 = 0, tmp2 = 0;
1333
  uint32_t tmp1 = 0U, tmp2 = 0U;
1332
 
1334
 
1333
  /* Check the parameters */
1335
  /* Check the parameters */
1334
  assert_param(IS_OB_WRP(WRP3));
1336
  assert_param(IS_OB_WRP(WRP3));
1335
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1337
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1336
 
1338
 
1337
  if (NewState != DISABLE)
1339
  if (NewState != DISABLE)
1338
  {
1340
  {
1339
    wrp89data = (uint16_t)(((WRP3 & WRP_MASK_LOW) | OB->WRP89));
1341
    wrp89data = (uint16_t)(((WRP3 & WRP_MASK_LOW) | OB->WRP89));
1340
    wrp1011data = (uint16_t)((((WRP3 & WRP_MASK_HIGH)>>16 | OB->WRP1011)));
1342
    wrp1011data = (uint16_t)((((WRP3 & WRP_MASK_HIGH)>>16U | OB->WRP1011)));
1341
    tmp1 = (uint32_t)(~(wrp89data) << 16)|(wrp89data);
1343
    tmp1 = (uint32_t)(~(wrp89data) << 16U)|(wrp89data);
1342
    OB->WRP89 = tmp1;
1344
    OB->WRP89 = tmp1;
1343
 
1345
 
1344
    tmp2 = (uint32_t)(~(wrp1011data) << 16)|(wrp1011data);
1346
    tmp2 = (uint32_t)(~(wrp1011data) << 16U)|(wrp1011data);
1345
    OB->WRP1011 = tmp2;      
1347
    OB->WRP1011 = tmp2;
1346
  }
1348
  }
1347
  else
1349
  else
1348
  {
1350
  {
1349
    wrp89data = (uint16_t)(~WRP3 & (WRP_MASK_LOW & OB->WRP89));
1351
    wrp89data = (uint16_t)(~WRP3 & (WRP_MASK_LOW & OB->WRP89));
1350
    wrp1011data = (uint16_t)((((~WRP3 & WRP_MASK_HIGH)>>16 & OB->WRP1011)));
1352
    wrp1011data = (uint16_t)((((~WRP3 & WRP_MASK_HIGH)>>16U & OB->WRP1011)));
1351
 
1353
 
1352
    tmp1 = (uint32_t)((~wrp89data) << 16)|(wrp89data);
1354
    tmp1 = (uint32_t)((~wrp89data) << 16U)|(wrp89data);
1353
    OB->WRP89 = tmp1;
1355
    OB->WRP89 = tmp1;
1354
 
1356
 
1355
    tmp2 = (uint32_t)((~wrp1011data) << 16)|(wrp1011data);
1357
    tmp2 = (uint32_t)((~wrp1011data) << 16U)|(wrp1011data);
1356
    OB->WRP1011 = tmp2;
1358
    OB->WRP1011 = tmp2;
1357
  }
1359
  }
1358
}
1360
}
1359
#endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1361
#endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1360
 
1362
 
Line 1369... Line 1371...
1369
  *   This parameter can be: ENABLE or DISABLE.
1371
  *   This parameter can be: ENABLE or DISABLE.
1370
  * @retval None
1372
  * @retval None
1371
  */
1373
  */
1372
static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState)
1374
static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState)
1373
{
1375
{
1374
  uint32_t wrp1213data = 0, wrp1415data = 0;
1376
  uint32_t wrp1213data = 0U, wrp1415data = 0U;
1375
 
1377
 
1376
  uint32_t tmp1 = 0, tmp2 = 0;
1378
  uint32_t tmp1 = 0U, tmp2 = 0U;
1377
 
1379
 
1378
  /* Check the parameters */
1380
  /* Check the parameters */
1379
  assert_param(IS_OB_WRP(WRP4));
1381
  assert_param(IS_OB_WRP(WRP4));
1380
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1382
  assert_param(IS_FUNCTIONAL_STATE(NewState));
1381
 
1383
 
1382
  if (NewState != DISABLE)
1384
  if (NewState != DISABLE)
1383
  {
1385
  {
1384
    wrp1213data = (uint16_t)(((WRP4 & WRP_MASK_LOW) | OB->WRP1213));
1386
    wrp1213data = (uint16_t)(((WRP4 & WRP_MASK_LOW) | OB->WRP1213));
1385
    wrp1415data = (uint16_t)((((WRP4 & WRP_MASK_HIGH)>>16 | OB->WRP1415)));
1387
    wrp1415data = (uint16_t)((((WRP4 & WRP_MASK_HIGH)>>16U | OB->WRP1415)));
1386
    tmp1 = (uint32_t)(~(wrp1213data) << 16)|(wrp1213data);
1388
    tmp1 = (uint32_t)(~(wrp1213data) << 16U)|(wrp1213data);
1387
    OB->WRP1213 = tmp1;
1389
    OB->WRP1213 = tmp1;
1388
 
1390
 
1389
    tmp2 = (uint32_t)(~(wrp1415data) << 16)|(wrp1415data);
1391
    tmp2 = (uint32_t)(~(wrp1415data) << 16U)|(wrp1415data);
1390
    OB->WRP1415 = tmp2;      
1392
    OB->WRP1415 = tmp2;
1391
  }
1393
  }
1392
  else
1394
  else
1393
  {
1395
  {
1394
    wrp1213data = (uint16_t)(~WRP4 & (WRP_MASK_LOW & OB->WRP1213));
1396
    wrp1213data = (uint16_t)(~WRP4 & (WRP_MASK_LOW & OB->WRP1213));
1395
    wrp1415data = (uint16_t)((((~WRP4 & WRP_MASK_HIGH)>>16 & OB->WRP1415)));
1397
    wrp1415data = (uint16_t)((((~WRP4 & WRP_MASK_HIGH)>>16U & OB->WRP1415)));
1396
 
1398
 
1397
    tmp1 = (uint32_t)((~wrp1213data) << 16)|(wrp1213data);
1399
    tmp1 = (uint32_t)((~wrp1213data) << 16U)|(wrp1213data);
1398
    OB->WRP1213 = tmp1;
1400
    OB->WRP1213 = tmp1;
1399
 
1401
 
1400
    tmp2 = (uint32_t)((~wrp1415data) << 16)|(wrp1415data);
1402
    tmp2 = (uint32_t)((~wrp1415data) << 16U)|(wrp1415data);
1401
    OB->WRP1415 = tmp2;
1403
    OB->WRP1415 = tmp2;
1402
  }
1404
  }
1403
}
1405
}
1404
#endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
1406
#endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
1405
 
1407
 
Line 1419... Line 1421...
1419
  *     @arg @ref OB_STDBY_RST Reset generated when entering in STANDBY
1421
  *     @arg @ref OB_STDBY_RST Reset generated when entering in STANDBY
1420
  * @retval HAL status
1422
  * @retval HAL status
1421
  */
1423
  */
1422
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
1424
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
1423
{
1425
{
1424
  HAL_StatusTypeDef status = HAL_OK;
1426
  HAL_StatusTypeDef status = HAL_OK;
1425
  uint32_t tmp = 0, tmp1 = 0;
1427
  uint32_t tmp = 0U, tmp1 = 0U;
1426
 
1428
 
1427
  /* Check the parameters */
1429
  /* Check the parameters */
1428
  assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
1430
  assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
1429
  assert_param(IS_OB_STOP_SOURCE(OB_STOP));
1431
  assert_param(IS_OB_STOP_SOURCE(OB_STOP));
1430
  assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
1432
  assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
1431
 
1433
 
1432
  /* Get the User Option byte register */
1434
  /* Get the User Option byte register */
1433
  tmp1 = OB->USER & ((~FLASH_OBR_USER) >> 16);
1435
  tmp1 = OB->USER & ((~(FLASH_OBR_IWDG_SW | FLASH_OBR_nRST_STOP | FLASH_OBR_nRST_STDBY)) >> 16U);
1434
 
1436
 
1435
  /* Calculate the user option byte to write */
1437
  /* Calculate the user option byte to write */
1436
  tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16);
1438
  tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16U);
1437
  tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
1439
  tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
1438
 
1440
 
1439
  /* Wait for last operation to be completed */
1441
  /* Wait for last operation to be completed */
1440
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1442
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1441
 
1443
 
1442
  if(status == HAL_OK)
1444
  if(status == HAL_OK)
1443
  {  
1445
  {
1444
    /* Clean the error context */
1446
    /* Clean the error context */
1445
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1447
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1446
 
1448
 
1447
    /* Write the User Option Byte */
1449
    /* Write the User Option Byte */
1448
    OB->USER = tmp;
1450
    OB->USER = tmp;
1449
   
1451
 
1450
    /* Wait for last operation to be completed */
1452
    /* Wait for last operation to be completed */
1451
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1453
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1452
  }
1454
  }
1453
 
1455
 
1454
  /* Return the Option Byte program Status */
1456
  /* Return the Option Byte program Status */
Line 1467... Line 1469...
1467
  *        The active bank is recognized by the value programmed at the base address
1469
  *        The active bank is recognized by the value programmed at the base address
1468
  *        of the respective bank (corresponding to the initial stack pointer value
1470
  *        of the respective bank (corresponding to the initial stack pointer value
1469
  *        in the interrupt vector table).
1471
  *        in the interrupt vector table).
1470
  *     @arg @ref OB_BOOT_BANK1 At startup, if boot pins are set in boot from user Flash
1472
  *     @arg @ref OB_BOOT_BANK1 At startup, if boot pins are set in boot from user Flash
1471
  *        position and this parameter is selected the device will boot from Bank1(Default).
1473
  *        position and this parameter is selected the device will boot from Bank1(Default).
1472
  *        For more information, please refer to AN2606 from www.st.com.
1474
  *        For more information, please refer to AN2606 from www.st.com.
1473
  * @retval HAL status
1475
  * @retval HAL status
1474
  */
1476
  */
1475
static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
1477
static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
1476
{
1478
{
1477
  HAL_StatusTypeDef status = HAL_OK;
1479
  HAL_StatusTypeDef status = HAL_OK;
1478
  uint32_t tmp = 0, tmp1 = 0;
1480
  uint32_t tmp = 0U, tmp1 = 0U;
1479
 
1481
 
1480
  /* Check the parameters */
1482
  /* Check the parameters */
1481
  assert_param(IS_OB_BOOT_BANK(OB_BOOT));
1483
  assert_param(IS_OB_BOOT_BANK(OB_BOOT));
1482
 
1484
 
1483
  /* Get the User Option byte register  and BOR Level*/
1485
  /* Get the User Option byte register  and BOR Level*/
1484
  tmp1 = OB->USER & ((~FLASH_OBR_nRST_BFB2) >> 16);
1486
  tmp1 = OB->USER & ((~FLASH_OBR_nRST_BFB2) >> 16U);
1485
 
1487
 
1486
  /* Calculate the option byte to write */
1488
  /* Calculate the option byte to write */
1487
  tmp = (uint32_t)~(OB_BOOT | tmp1) << 16;
1489
  tmp = (uint32_t)~(OB_BOOT | tmp1) << 16U;
1488
  tmp |= (OB_BOOT | tmp1);
1490
  tmp |= (OB_BOOT | tmp1);
1489
 
1491
 
1490
  /* Wait for last operation to be completed */
1492
  /* Wait for last operation to be completed */
1491
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1493
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1492
 
1494
 
1493
  if(status == HAL_OK)
1495
  if(status == HAL_OK)
1494
  {  
1496
  {
1495
    /* Clean the error context */
1497
    /* Clean the error context */
1496
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1498
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
1497
 
1499
 
1498
    /* Write the BOOT Option Byte */
1500
    /* Write the BOOT Option Byte */
1499
    OB->USER = tmp;
1501
    OB->USER = tmp;
1500
   
1502
 
1501
    /* Wait for last operation to be completed */
1503
    /* Wait for last operation to be completed */
1502
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1504
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1503
  }
1505
  }
1504
 
1506
 
1505
  /* Return the Option Byte program Status */
1507
  /* Return the Option Byte program Status */
Line 1523... Line 1525...
1523
  */
1525
  */
1524
static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
1526
static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
1525
{
1527
{
1526
  HAL_StatusTypeDef status = HAL_OK;
1528
  HAL_StatusTypeDef status = HAL_OK;
1527
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1529
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1528
  uint32_t tmp = 0, tmpaddr = 0;
1530
  uint32_t tmp = 0U, tmpaddr = 0U;
1529
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1531
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1530
 
1532
 
1531
  /* Check the parameters */
1533
  /* Check the parameters */
1532
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1534
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1533
 
1535
 
1534
  /* Wait for last operation to be completed */
1536
  /* Wait for last operation to be completed */
1535
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1537
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1536
   
1538
 
1537
  if(status == HAL_OK)
1539
  if(status == HAL_OK)
1538
  {
1540
  {
1539
    /* Clear the FTDW bit */
1541
    /* Clear the FTDW bit */
1540
    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1542
    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1541
 
1543
 
1542
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1544
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1543
    /* Possible only on Cat1 devices */
1545
    /* Possible only on Cat1 devices */
1544
    if(Data != (uint8_t)0x00)
1546
    if(Data != (uint8_t)0x00U)
1545
    {
1547
    {
1546
      /* If the previous operation is completed, proceed to write the new Data */
1548
      /* If the previous operation is completed, proceed to write the new Data */
1547
      *(__IO uint8_t *)Address = Data;
1549
      *(__IO uint8_t *)Address = Data;
1548
           
1550
 
1549
      /* Wait for last operation to be completed */
1551
      /* Wait for last operation to be completed */
1550
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1552
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1551
    }
1553
    }
1552
    else
1554
    else
1553
    {
1555
    {
1554
      tmpaddr = Address & 0xFFFFFFFC;
1556
      tmpaddr = Address & 0xFFFFFFFCU;
1555
      tmp = * (__IO uint32_t *) tmpaddr;
1557
      tmp = * (__IO uint32_t *) tmpaddr;
1556
      tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
1558
      tmpaddr = 0xFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
1557
      tmp &= ~tmpaddr;
1559
      tmp &= ~tmpaddr;
1558
      status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1560
      status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
1559
      /* Process Unlocked */
1561
      /* Process Unlocked */
1560
      __HAL_UNLOCK(&pFlash);
1562
      __HAL_UNLOCK(&pFlash);
1561
      status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1563
      status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
1562
      /* Process Locked */
1564
      /* Process Locked */
1563
      __HAL_LOCK(&pFlash);
1565
      __HAL_LOCK(&pFlash);
1564
    }
1566
    }
1565
#else /*!Cat1*/ 
1567
#else /*!Cat1*/
1566
    /* If the previous operation is completed, proceed to write the new Data */
1568
    /* If the previous operation is completed, proceed to write the new Data */
1567
    *(__IO uint8_t *)Address = Data;
1569
    *(__IO uint8_t *)Address = Data;
1568
           
1570
 
1569
    /* Wait for last operation to be completed */
1571
    /* Wait for last operation to be completed */
1570
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1572
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1571
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1573
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1572
  }
1574
  }
1573
  /* Return the Write Status */
1575
  /* Return the Write Status */
Line 1583... Line 1585...
1583
  */
1585
  */
1584
static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
1586
static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
1585
{
1587
{
1586
  HAL_StatusTypeDef status = HAL_OK;
1588
  HAL_StatusTypeDef status = HAL_OK;
1587
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1589
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1588
  uint32_t tmp = 0, tmpaddr = 0;
1590
  uint32_t tmp = 0U, tmpaddr = 0U;
1589
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1591
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1590
 
1592
 
1591
  /* Check the parameters */
1593
  /* Check the parameters */
1592
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1594
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1593
 
1595
 
1594
  /* Wait for last operation to be completed */
1596
  /* Wait for last operation to be completed */
1595
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1597
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1596
   
1598
 
1597
  if(status == HAL_OK)
1599
  if(status == HAL_OK)
1598
  {
1600
  {
1599
    /* Clear the FTDW bit */
1601
    /* Clear the FTDW bit */
1600
    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1602
    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1601
 
1603
 
1602
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1604
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1603
    /* Possible only on Cat1 devices */
1605
    /* Possible only on Cat1 devices */
1604
    if(Data != (uint16_t)0x0000)
1606
    if(Data != (uint16_t)0x0000U)
1605
    {
1607
    {
1606
      /* If the previous operation is completed, proceed to write the new data */
1608
      /* If the previous operation is completed, proceed to write the new data */
1607
      *(__IO uint16_t *)Address = Data;
1609
      *(__IO uint16_t *)Address = Data;
1608
 
1610
 
1609
      /* Wait for last operation to be completed */
1611
      /* Wait for last operation to be completed */
1610
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1612
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1611
    }
1613
    }
1612
    else
1614
    else
1613
    {
1615
    {
1614
      /* Process Unlocked */
1616
      /* Process Unlocked */
1615
      __HAL_UNLOCK(&pFlash);
1617
      __HAL_UNLOCK(&pFlash);
1616
      if((Address & 0x3) != 0x3)
1618
      if((Address & 0x3U) != 0x3U)
1617
      {
1619
      {
1618
        tmpaddr = Address & 0xFFFFFFFC;
1620
        tmpaddr = Address & 0xFFFFFFFCU;
1619
        tmp = * (__IO uint32_t *) tmpaddr;
1621
        tmp = * (__IO uint32_t *) tmpaddr;
1620
        tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
1622
        tmpaddr = 0xFFFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
1621
        tmp &= ~tmpaddr;        
1623
        tmp &= ~tmpaddr;
1622
        status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1624
        status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
1623
        status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1625
        status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
1624
      }
1626
      }
1625
      else
1627
      else
1626
      {
1628
      {
1627
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
1629
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00U);
1628
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
1630
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1U, 0x00U);
1629
      }
1631
      }
1630
      /* Process Locked */
1632
      /* Process Locked */
1631
      __HAL_LOCK(&pFlash);
1633
      __HAL_LOCK(&pFlash);
1632
    }
1634
    }
1633
#else /* !Cat1 */
1635
#else /* !Cat1 */
1634
    /* If the previous operation is completed, proceed to write the new data */
1636
    /* If the previous operation is completed, proceed to write the new data */
1635
    *(__IO uint16_t *)Address = Data;
1637
    *(__IO uint16_t *)Address = Data;
1636
 
1638
 
1637
    /* Wait for last operation to be completed */
1639
    /* Wait for last operation to be completed */
1638
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1640
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1639
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1641
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1640
  }
1642
  }
1641
  /* Return the Write Status */
1643
  /* Return the Write Status */
Line 1653... Line 1655...
1653
{
1655
{
1654
  HAL_StatusTypeDef status = HAL_OK;
1656
  HAL_StatusTypeDef status = HAL_OK;
1655
 
1657
 
1656
  /* Check the parameters */
1658
  /* Check the parameters */
1657
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1659
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1658
 
1660
 
1659
  /* Wait for last operation to be completed */
1661
  /* Wait for last operation to be completed */
1660
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1662
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1661
 
1663
 
1662
  if(status == HAL_OK)
1664
  if(status == HAL_OK)
1663
  {
1665
  {
1664
    /* Clear the FTDW bit */
1666
    /* Clear the FTDW bit */
1665
    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1667
    CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1666
 
1668
 
1667
    /* If the previous operation is completed, proceed to program the new data */    
1669
    /* If the previous operation is completed, proceed to program the new data */
1668
    *(__IO uint32_t *)Address = Data;
1670
    *(__IO uint32_t *)Address = Data;
1669
   
1671
 
1670
    /* Wait for last operation to be completed */
1672
    /* Wait for last operation to be completed */
1671
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);      
1673
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1672
  }
1674
  }
1673
  /* Return the Write Status */
1675
  /* Return the Write Status */
1674
  return status;
1676
  return status;
1675
}
1677
}
1676
 
1678
 
Line 1682... Line 1684...
1682
  */
1684
  */
1683
static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data)
1685
static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data)
1684
{
1686
{
1685
  HAL_StatusTypeDef status = HAL_OK;
1687
  HAL_StatusTypeDef status = HAL_OK;
1686
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1688
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1687
  uint32_t tmp = 0, tmpaddr = 0;
1689
  uint32_t tmp = 0U, tmpaddr = 0U;
1688
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1690
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1689
 
1691
 
1690
  /* Check the parameters */
1692
  /* Check the parameters */
1691
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1693
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1692
 
1694
 
1693
  /* Wait for last operation to be completed */
1695
  /* Wait for last operation to be completed */
1694
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1696
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1695
 
1697
 
1696
  if(status == HAL_OK)
1698
  if(status == HAL_OK)
1697
  {
1699
  {
1698
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1700
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1699
    if(Data != (uint8_t) 0x00)
1701
    if(Data != (uint8_t) 0x00U)
1700
    {  
1702
    {
1701
      *(__IO uint8_t *)Address = Data;
1703
      *(__IO uint8_t *)Address = Data;
1702
   
1704
 
1703
      /* Wait for last operation to be completed */
1705
      /* Wait for last operation to be completed */
1704
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1706
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1705
 
1707
 
1706
    }
1708
    }
1707
    else
1709
    else
1708
    {
1710
    {
1709
      tmpaddr = Address & 0xFFFFFFFC;
1711
      tmpaddr = Address & 0xFFFFFFFCU;
1710
      tmp = * (__IO uint32_t *) tmpaddr;
1712
      tmp = * (__IO uint32_t *) tmpaddr;
1711
      tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
1713
      tmpaddr = 0xFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
1712
      tmp &= ~tmpaddr;        
1714
      tmp &= ~tmpaddr;
1713
      status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1715
      status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
1714
      /* Process Unlocked */
1716
      /* Process Unlocked */
1715
      __HAL_UNLOCK(&pFlash);
1717
      __HAL_UNLOCK(&pFlash);
1716
      status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1718
      status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
1717
      /* Process Locked */
1719
      /* Process Locked */
1718
      __HAL_LOCK(&pFlash);
1720
      __HAL_LOCK(&pFlash);
1719
    }
1721
    }
1720
#else /* Not Cat1*/
1722
#else /* Not Cat1*/
1721
    *(__IO uint8_t *)Address = Data;
1723
    *(__IO uint8_t *)Address = Data;
1722
   
1724
 
1723
    /* Wait for last operation to be completed */
1725
    /* Wait for last operation to be completed */
1724
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1726
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1725
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1727
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1726
  }
1728
  }
1727
  /* Return the Write Status */
1729
  /* Return the Write Status */
Line 1736... Line 1738...
1736
  */
1738
  */
1737
static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
1739
static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
1738
{
1740
{
1739
  HAL_StatusTypeDef status = HAL_OK;
1741
  HAL_StatusTypeDef status = HAL_OK;
1740
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1742
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1741
  uint32_t tmp = 0, tmpaddr = 0;
1743
  uint32_t tmp = 0U, tmpaddr = 0U;
1742
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1744
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1743
 
1745
 
1744
  /* Check the parameters */
1746
  /* Check the parameters */
1745
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1747
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1746
 
1748
 
1747
  /* Wait for last operation to be completed */
1749
  /* Wait for last operation to be completed */
1748
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1750
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1749
 
1751
 
1750
  if(status == HAL_OK)
1752
  if(status == HAL_OK)
1751
  {
1753
  {
1752
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1754
#if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
1753
    if(Data != (uint16_t)0x0000)
1755
    if(Data != (uint16_t)0x0000U)
1754
    {
1756
    {
1755
      *(__IO uint16_t *)Address = Data;
1757
      *(__IO uint16_t *)Address = Data;
1756
   
1758
 
1757
      /* Wait for last operation to be completed */
1759
      /* Wait for last operation to be completed */
1758
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1760
      status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1759
    }
1761
    }
1760
    else
1762
    else
1761
    {
1763
    {
1762
      /* Process Unlocked */
1764
      /* Process Unlocked */
1763
      __HAL_UNLOCK(&pFlash);
1765
      __HAL_UNLOCK(&pFlash);
1764
      if((Address & 0x3) != 0x3)
1766
      if((Address & 0x3U) != 0x3U)
1765
      {
1767
      {
1766
        tmpaddr = Address & 0xFFFFFFFC;
1768
        tmpaddr = Address & 0xFFFFFFFCU;
1767
        tmp = * (__IO uint32_t *) tmpaddr;
1769
        tmp = * (__IO uint32_t *) tmpaddr;
1768
        tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
1770
        tmpaddr = 0xFFFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
1769
        tmp &= ~tmpaddr;          
1771
        tmp &= ~tmpaddr;
1770
        status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1772
        status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
1771
        status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1773
        status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
1772
      }
1774
      }
1773
      else
1775
      else
1774
      {
1776
      {
1775
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
1777
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00U);
1776
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
1778
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1U, 0x00U);
1777
      }
1779
      }
1778
      /* Process Locked */
1780
      /* Process Locked */
1779
      __HAL_LOCK(&pFlash);
1781
      __HAL_LOCK(&pFlash);
1780
    }
1782
    }
1781
#else /* Not Cat1*/
1783
#else /* Not Cat1*/
1782
    *(__IO uint16_t *)Address = Data;
1784
    *(__IO uint16_t *)Address = Data;
1783
   
1785
 
1784
    /* Wait for last operation to be completed */
1786
    /* Wait for last operation to be completed */
1785
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1787
    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1786
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1788
#endif /* STM32L100xB || STM32L151xB || STM32L152xB  */
1787
  }
1789
  }
1788
  /* Return the Write Status */
1790
  /* Return the Write Status */
Line 1796... Line 1798...
1796
  * @retval HAL status
1798
  * @retval HAL status
1797
  */
1799
  */
1798
static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data)
1800
static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data)
1799
{
1801
{
1800
  HAL_StatusTypeDef status = HAL_OK;
1802
  HAL_StatusTypeDef status = HAL_OK;
1801
 
1803
 
1802
  /* Check the parameters */
1804
  /* Check the parameters */
1803
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1805
  assert_param(IS_FLASH_DATA_ADDRESS(Address));
1804
 
1806
 
1805
  /* Wait for last operation to be completed */
1807
  /* Wait for last operation to be completed */
1806
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1808
  status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
1807
 
1809
 
1808
  if(status == HAL_OK)
1810
  if(status == HAL_OK)
1809
  {
1811
  {
1810
    *(__IO uint32_t *)Address = Data;
1812
    *(__IO uint32_t *)Address = Data;
1811
 
1813
 
1812
    /* Wait for last operation to be completed */
1814
    /* Wait for last operation to be completed */
Line 1834... Line 1836...
1834
 */
1836
 */
1835
 
1837
 
1836
/**
1838
/**
1837
  * @brief  Erases a specified page in program memory.
1839
  * @brief  Erases a specified page in program memory.
1838
  * @param  PageAddress The page address in program memory to be erased.
1840
  * @param  PageAddress The page address in program memory to be erased.
1839
  * @note   A Page is erased in the Program memory only if the address to load
1841
  * @note   A Page is erased in the Program memory only if the address to load
1840
  *         is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
1842
  *         is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
1841
  * @retval None
1843
  * @retval None
1842
  */
1844
  */
1843
void FLASH_PageErase(uint32_t PageAddress)
1845
void FLASH_PageErase(uint32_t PageAddress)
1844
{
1846
{
Line 1852... Line 1854...
1852
  SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
1854
  SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
1853
 
1855
 
1854
  /* Write 00000000h to the first word of the program page to erase */
1856
  /* Write 00000000h to the first word of the program page to erase */
1855
  *(__IO uint32_t *)(uint32_t)(PageAddress & ~(FLASH_PAGE_SIZE - 1)) = 0x00000000;
1857
  *(__IO uint32_t *)(uint32_t)(PageAddress & ~(FLASH_PAGE_SIZE - 1)) = 0x00000000;
1856
}
1858
}
1857
 
1859
 
1858
/**
1860
/**
1859
  * @}
1861
  * @}
1860
  */
1862
  */
1861
 
1863
 
1862
/**
1864
/**