Subversion Repositories DashDisplay

Rev

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

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