annotate f103c8/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c @ 4:ca4f5b55b391

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