001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /**
ganlikun 0:13413ea9a877 2 ******************************************************************************
ganlikun 0:13413ea9a877 3 * @file stm32f4xx_hal_flash_ex.c
ganlikun 0:13413ea9a877 4 * @author MCD Application Team
ganlikun 0:13413ea9a877 5 * @version V1.7.1
ganlikun 0:13413ea9a877 6 * @date 14-April-2017
ganlikun 0:13413ea9a877 7 * @brief Extended FLASH HAL module driver.
ganlikun 0:13413ea9a877 8 * This file provides firmware functions to manage the following
ganlikun 0:13413ea9a877 9 * functionalities of the FLASH extension peripheral:
ganlikun 0:13413ea9a877 10 * + Extended programming operations functions
ganlikun 0:13413ea9a877 11 *
ganlikun 0:13413ea9a877 12 @verbatim
ganlikun 0:13413ea9a877 13 ==============================================================================
ganlikun 0:13413ea9a877 14 ##### Flash Extension features #####
ganlikun 0:13413ea9a877 15 ==============================================================================
ganlikun 0:13413ea9a877 16
ganlikun 0:13413ea9a877 17 [..] Comparing to other previous devices, the FLASH interface for STM32F427xx/437xx and
ganlikun 0:13413ea9a877 18 STM32F429xx/439xx devices contains the following additional features
ganlikun 0:13413ea9a877 19
ganlikun 0:13413ea9a877 20 (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
ganlikun 0:13413ea9a877 21 capability (RWW)
ganlikun 0:13413ea9a877 22 (+) Dual bank memory organization
ganlikun 0:13413ea9a877 23 (+) PCROP protection for all banks
ganlikun 0:13413ea9a877 24
ganlikun 0:13413ea9a877 25 ##### How to use this driver #####
ganlikun 0:13413ea9a877 26 ==============================================================================
ganlikun 0:13413ea9a877 27 [..] This driver provides functions to configure and program the FLASH memory
ganlikun 0:13413ea9a877 28 of all STM32F427xx/437xx, STM32F429xx/439xx, STM32F469xx/479xx and STM32F446xx
ganlikun 0:13413ea9a877 29 devices. It includes
ganlikun 0:13413ea9a877 30 (#) FLASH Memory Erase functions:
ganlikun 0:13413ea9a877 31 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
ganlikun 0:13413ea9a877 32 HAL_FLASH_Lock() functions
ganlikun 0:13413ea9a877 33 (++) Erase function: Erase sector, erase all sectors
ganlikun 0:13413ea9a877 34 (++) There are two modes of erase :
ganlikun 0:13413ea9a877 35 (+++) Polling Mode using HAL_FLASHEx_Erase()
ganlikun 0:13413ea9a877 36 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
ganlikun 0:13413ea9a877 37
ganlikun 0:13413ea9a877 38 (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
ganlikun 0:13413ea9a877 39 (++) Set/Reset the write protection
ganlikun 0:13413ea9a877 40 (++) Set the Read protection Level
ganlikun 0:13413ea9a877 41 (++) Set the BOR level
ganlikun 0:13413ea9a877 42 (++) Program the user Option Bytes
ganlikun 0:13413ea9a877 43 (#) Advanced Option Bytes Programming functions: Use HAL_FLASHEx_AdvOBProgram() to :
ganlikun 0:13413ea9a877 44 (++) Extended space (bank 2) erase function
ganlikun 0:13413ea9a877 45 (++) Full FLASH space (2 Mo) erase (bank 1 and bank 2)
ganlikun 0:13413ea9a877 46 (++) Dual Boot activation
ganlikun 0:13413ea9a877 47 (++) Write protection configuration for bank 2
ganlikun 0:13413ea9a877 48 (++) PCROP protection configuration and control for both banks
ganlikun 0:13413ea9a877 49
ganlikun 0:13413ea9a877 50 @endverbatim
ganlikun 0:13413ea9a877 51 ******************************************************************************
ganlikun 0:13413ea9a877 52 * @attention
ganlikun 0:13413ea9a877 53 *
ganlikun 0:13413ea9a877 54 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
ganlikun 0:13413ea9a877 55 *
ganlikun 0:13413ea9a877 56 * Redistribution and use in source and binary forms, with or without modification,
ganlikun 0:13413ea9a877 57 * are permitted provided that the following conditions are met:
ganlikun 0:13413ea9a877 58 * 1. Redistributions of source code must retain the above copyright notice,
ganlikun 0:13413ea9a877 59 * this list of conditions and the following disclaimer.
ganlikun 0:13413ea9a877 60 * 2. Redistributions in binary form must reproduce the above copyright notice,
ganlikun 0:13413ea9a877 61 * this list of conditions and the following disclaimer in the documentation
ganlikun 0:13413ea9a877 62 * and/or other materials provided with the distribution.
ganlikun 0:13413ea9a877 63 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ganlikun 0:13413ea9a877 64 * may be used to endorse or promote products derived from this software
ganlikun 0:13413ea9a877 65 * without specific prior written permission.
ganlikun 0:13413ea9a877 66 *
ganlikun 0:13413ea9a877 67 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ganlikun 0:13413ea9a877 68 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ganlikun 0:13413ea9a877 69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ganlikun 0:13413ea9a877 70 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ganlikun 0:13413ea9a877 71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ganlikun 0:13413ea9a877 72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ganlikun 0:13413ea9a877 73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ganlikun 0:13413ea9a877 74 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ganlikun 0:13413ea9a877 75 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ganlikun 0:13413ea9a877 76 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ganlikun 0:13413ea9a877 77 *
ganlikun 0:13413ea9a877 78 ******************************************************************************
ganlikun 0:13413ea9a877 79 */
ganlikun 0:13413ea9a877 80
ganlikun 0:13413ea9a877 81 /* Includes ------------------------------------------------------------------*/
ganlikun 0:13413ea9a877 82 #include "stm32f4xx_hal.h"
ganlikun 0:13413ea9a877 83
ganlikun 0:13413ea9a877 84 /** @addtogroup STM32F4xx_HAL_Driver
ganlikun 0:13413ea9a877 85 * @{
ganlikun 0:13413ea9a877 86 */
ganlikun 0:13413ea9a877 87
ganlikun 0:13413ea9a877 88 /** @defgroup FLASHEx FLASHEx
ganlikun 0:13413ea9a877 89 * @brief FLASH HAL Extension module driver
ganlikun 0:13413ea9a877 90 * @{
ganlikun 0:13413ea9a877 91 */
ganlikun 0:13413ea9a877 92
ganlikun 0:13413ea9a877 93 #ifdef HAL_FLASH_MODULE_ENABLED
ganlikun 0:13413ea9a877 94
ganlikun 0:13413ea9a877 95 /* Private typedef -----------------------------------------------------------*/
ganlikun 0:13413ea9a877 96 /* Private define ------------------------------------------------------------*/
ganlikun 0:13413ea9a877 97 /** @addtogroup FLASHEx_Private_Constants
ganlikun 0:13413ea9a877 98 * @{
ganlikun 0:13413ea9a877 99 */
ganlikun 0:13413ea9a877 100 #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
ganlikun 0:13413ea9a877 101 /**
ganlikun 0:13413ea9a877 102 * @}
ganlikun 0:13413ea9a877 103 */
ganlikun 0:13413ea9a877 104
ganlikun 0:13413ea9a877 105 /* Private macro -------------------------------------------------------------*/
ganlikun 0:13413ea9a877 106 /* Private variables ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 107 /** @addtogroup FLASHEx_Private_Variables
ganlikun 0:13413ea9a877 108 * @{
ganlikun 0:13413ea9a877 109 */
ganlikun 0:13413ea9a877 110 extern FLASH_ProcessTypeDef pFlash;
ganlikun 0:13413ea9a877 111 /**
ganlikun 0:13413ea9a877 112 * @}
ganlikun 0:13413ea9a877 113 */
ganlikun 0:13413ea9a877 114
ganlikun 0:13413ea9a877 115 /* Private function prototypes -----------------------------------------------*/
ganlikun 0:13413ea9a877 116 /** @addtogroup FLASHEx_Private_Functions
ganlikun 0:13413ea9a877 117 * @{
ganlikun 0:13413ea9a877 118 */
ganlikun 0:13413ea9a877 119 /* Option bytes control */
ganlikun 0:13413ea9a877 120 static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
ganlikun 0:13413ea9a877 121 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
ganlikun 0:13413ea9a877 122 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks);
ganlikun 0:13413ea9a877 123 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
ganlikun 0:13413ea9a877 124 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby);
ganlikun 0:13413ea9a877 125 static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
ganlikun 0:13413ea9a877 126 static uint8_t FLASH_OB_GetUser(void);
ganlikun 0:13413ea9a877 127 static uint16_t FLASH_OB_GetWRP(void);
ganlikun 0:13413ea9a877 128 static uint8_t FLASH_OB_GetRDP(void);
ganlikun 0:13413ea9a877 129 static uint8_t FLASH_OB_GetBOR(void);
ganlikun 0:13413ea9a877 130
ganlikun 0:13413ea9a877 131 #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) ||\
ganlikun 0:13413ea9a877 132 defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||\
ganlikun 0:13413ea9a877 133 defined(STM32F423xx)
ganlikun 0:13413ea9a877 134 static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector);
ganlikun 0:13413ea9a877 135 static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector);
ganlikun 0:13413ea9a877 136 #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx
ganlikun 0:13413ea9a877 137 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 138
ganlikun 0:13413ea9a877 139 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
ganlikun 0:13413ea9a877 140 static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
ganlikun 0:13413ea9a877 141 static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
ganlikun 0:13413ea9a877 142 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig);
ganlikun 0:13413ea9a877 143 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 144
ganlikun 0:13413ea9a877 145 extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
ganlikun 0:13413ea9a877 146 /**
ganlikun 0:13413ea9a877 147 * @}
ganlikun 0:13413ea9a877 148 */
ganlikun 0:13413ea9a877 149
ganlikun 0:13413ea9a877 150 /* Exported functions --------------------------------------------------------*/
ganlikun 0:13413ea9a877 151 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
ganlikun 0:13413ea9a877 152 * @{
ganlikun 0:13413ea9a877 153 */
ganlikun 0:13413ea9a877 154
ganlikun 0:13413ea9a877 155 /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
ganlikun 0:13413ea9a877 156 * @brief Extended IO operation functions
ganlikun 0:13413ea9a877 157 *
ganlikun 0:13413ea9a877 158 @verbatim
ganlikun 0:13413ea9a877 159 ===============================================================================
ganlikun 0:13413ea9a877 160 ##### Extended programming operation functions #####
ganlikun 0:13413ea9a877 161 ===============================================================================
ganlikun 0:13413ea9a877 162 [..]
ganlikun 0:13413ea9a877 163 This subsection provides a set of functions allowing to manage the Extension FLASH
ganlikun 0:13413ea9a877 164 programming operations.
ganlikun 0:13413ea9a877 165
ganlikun 0:13413ea9a877 166 @endverbatim
ganlikun 0:13413ea9a877 167 * @{
ganlikun 0:13413ea9a877 168 */
ganlikun 0:13413ea9a877 169 /**
ganlikun 0:13413ea9a877 170 * @brief Perform a mass erase or erase the specified FLASH memory sectors
ganlikun 0:13413ea9a877 171 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
ganlikun 0:13413ea9a877 172 * contains the configuration information for the erasing.
ganlikun 0:13413ea9a877 173 *
ganlikun 0:13413ea9a877 174 * @param[out] SectorError: pointer to variable that
ganlikun 0:13413ea9a877 175 * contains the configuration information on faulty sector in case of error
ganlikun 0:13413ea9a877 176 * (0xFFFFFFFFU means that all the sectors have been correctly erased)
ganlikun 0:13413ea9a877 177 *
ganlikun 0:13413ea9a877 178 * @retval HAL Status
ganlikun 0:13413ea9a877 179 */
ganlikun 0:13413ea9a877 180 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
ganlikun 0:13413ea9a877 181 {
ganlikun 0:13413ea9a877 182 HAL_StatusTypeDef status = HAL_ERROR;
ganlikun 0:13413ea9a877 183 uint32_t index = 0U;
ganlikun 0:13413ea9a877 184
ganlikun 0:13413ea9a877 185 /* Process Locked */
ganlikun 0:13413ea9a877 186 __HAL_LOCK(&pFlash);
ganlikun 0:13413ea9a877 187
ganlikun 0:13413ea9a877 188 /* Check the parameters */
ganlikun 0:13413ea9a877 189 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
ganlikun 0:13413ea9a877 190
ganlikun 0:13413ea9a877 191 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 192 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 193
ganlikun 0:13413ea9a877 194 if(status == HAL_OK)
ganlikun 0:13413ea9a877 195 {
ganlikun 0:13413ea9a877 196 /*Initialization of SectorError variable*/
ganlikun 0:13413ea9a877 197 *SectorError = 0xFFFFFFFFU;
ganlikun 0:13413ea9a877 198
ganlikun 0:13413ea9a877 199 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
ganlikun 0:13413ea9a877 200 {
ganlikun 0:13413ea9a877 201 /*Mass erase to be done*/
ganlikun 0:13413ea9a877 202 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
ganlikun 0:13413ea9a877 203
ganlikun 0:13413ea9a877 204 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 205 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 206
ganlikun 0:13413ea9a877 207 /* if the erase operation is completed, disable the MER Bit */
ganlikun 0:13413ea9a877 208 FLASH->CR &= (~FLASH_MER_BIT);
ganlikun 0:13413ea9a877 209 }
ganlikun 0:13413ea9a877 210 else
ganlikun 0:13413ea9a877 211 {
ganlikun 0:13413ea9a877 212 /* Check the parameters */
ganlikun 0:13413ea9a877 213 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
ganlikun 0:13413ea9a877 214
ganlikun 0:13413ea9a877 215 /* Erase by sector by sector to be done*/
ganlikun 0:13413ea9a877 216 for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
ganlikun 0:13413ea9a877 217 {
ganlikun 0:13413ea9a877 218 FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);
ganlikun 0:13413ea9a877 219
ganlikun 0:13413ea9a877 220 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 221 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 222
ganlikun 0:13413ea9a877 223 /* If the erase operation is completed, disable the SER and SNB Bits */
ganlikun 0:13413ea9a877 224 CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
ganlikun 0:13413ea9a877 225
ganlikun 0:13413ea9a877 226 if(status != HAL_OK)
ganlikun 0:13413ea9a877 227 {
ganlikun 0:13413ea9a877 228 /* In case of error, stop erase procedure and return the faulty sector*/
ganlikun 0:13413ea9a877 229 *SectorError = index;
ganlikun 0:13413ea9a877 230 break;
ganlikun 0:13413ea9a877 231 }
ganlikun 0:13413ea9a877 232 }
ganlikun 0:13413ea9a877 233 }
ganlikun 0:13413ea9a877 234 /* Flush the caches to be sure of the data consistency */
ganlikun 0:13413ea9a877 235 FLASH_FlushCaches();
ganlikun 0:13413ea9a877 236 }
ganlikun 0:13413ea9a877 237
ganlikun 0:13413ea9a877 238 /* Process Unlocked */
ganlikun 0:13413ea9a877 239 __HAL_UNLOCK(&pFlash);
ganlikun 0:13413ea9a877 240
ganlikun 0:13413ea9a877 241 return status;
ganlikun 0:13413ea9a877 242 }
ganlikun 0:13413ea9a877 243
ganlikun 0:13413ea9a877 244 /**
ganlikun 0:13413ea9a877 245 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
ganlikun 0:13413ea9a877 246 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
ganlikun 0:13413ea9a877 247 * contains the configuration information for the erasing.
ganlikun 0:13413ea9a877 248 *
ganlikun 0:13413ea9a877 249 * @retval HAL Status
ganlikun 0:13413ea9a877 250 */
ganlikun 0:13413ea9a877 251 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
ganlikun 0:13413ea9a877 252 {
ganlikun 0:13413ea9a877 253 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 254
ganlikun 0:13413ea9a877 255 /* Process Locked */
ganlikun 0:13413ea9a877 256 __HAL_LOCK(&pFlash);
ganlikun 0:13413ea9a877 257
ganlikun 0:13413ea9a877 258 /* Check the parameters */
ganlikun 0:13413ea9a877 259 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
ganlikun 0:13413ea9a877 260
ganlikun 0:13413ea9a877 261 /* Enable End of FLASH Operation interrupt */
ganlikun 0:13413ea9a877 262 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
ganlikun 0:13413ea9a877 263
ganlikun 0:13413ea9a877 264 /* Enable Error source interrupt */
ganlikun 0:13413ea9a877 265 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
ganlikun 0:13413ea9a877 266
ganlikun 0:13413ea9a877 267 /* Clear pending flags (if any) */
ganlikun 0:13413ea9a877 268 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
ganlikun 0:13413ea9a877 269 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
ganlikun 0:13413ea9a877 270
ganlikun 0:13413ea9a877 271 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
ganlikun 0:13413ea9a877 272 {
ganlikun 0:13413ea9a877 273 /*Mass erase to be done*/
ganlikun 0:13413ea9a877 274 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
ganlikun 0:13413ea9a877 275 pFlash.Bank = pEraseInit->Banks;
ganlikun 0:13413ea9a877 276 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
ganlikun 0:13413ea9a877 277 }
ganlikun 0:13413ea9a877 278 else
ganlikun 0:13413ea9a877 279 {
ganlikun 0:13413ea9a877 280 /* Erase by sector to be done*/
ganlikun 0:13413ea9a877 281
ganlikun 0:13413ea9a877 282 /* Check the parameters */
ganlikun 0:13413ea9a877 283 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
ganlikun 0:13413ea9a877 284
ganlikun 0:13413ea9a877 285 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
ganlikun 0:13413ea9a877 286 pFlash.NbSectorsToErase = pEraseInit->NbSectors;
ganlikun 0:13413ea9a877 287 pFlash.Sector = pEraseInit->Sector;
ganlikun 0:13413ea9a877 288 pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
ganlikun 0:13413ea9a877 289
ganlikun 0:13413ea9a877 290 /*Erase 1st sector and wait for IT*/
ganlikun 0:13413ea9a877 291 FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
ganlikun 0:13413ea9a877 292 }
ganlikun 0:13413ea9a877 293
ganlikun 0:13413ea9a877 294 return status;
ganlikun 0:13413ea9a877 295 }
ganlikun 0:13413ea9a877 296
ganlikun 0:13413ea9a877 297 /**
ganlikun 0:13413ea9a877 298 * @brief Program option bytes
ganlikun 0:13413ea9a877 299 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
ganlikun 0:13413ea9a877 300 * contains the configuration information for the programming.
ganlikun 0:13413ea9a877 301 *
ganlikun 0:13413ea9a877 302 * @retval HAL Status
ganlikun 0:13413ea9a877 303 */
ganlikun 0:13413ea9a877 304 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
ganlikun 0:13413ea9a877 305 {
ganlikun 0:13413ea9a877 306 HAL_StatusTypeDef status = HAL_ERROR;
ganlikun 0:13413ea9a877 307
ganlikun 0:13413ea9a877 308 /* Process Locked */
ganlikun 0:13413ea9a877 309 __HAL_LOCK(&pFlash);
ganlikun 0:13413ea9a877 310
ganlikun 0:13413ea9a877 311 /* Check the parameters */
ganlikun 0:13413ea9a877 312 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
ganlikun 0:13413ea9a877 313
ganlikun 0:13413ea9a877 314 /*Write protection configuration*/
ganlikun 0:13413ea9a877 315 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
ganlikun 0:13413ea9a877 316 {
ganlikun 0:13413ea9a877 317 assert_param(IS_WRPSTATE(pOBInit->WRPState));
ganlikun 0:13413ea9a877 318 if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
ganlikun 0:13413ea9a877 319 {
ganlikun 0:13413ea9a877 320 /*Enable of Write protection on the selected Sector*/
ganlikun 0:13413ea9a877 321 status = FLASH_OB_EnableWRP(pOBInit->WRPSector, pOBInit->Banks);
ganlikun 0:13413ea9a877 322 }
ganlikun 0:13413ea9a877 323 else
ganlikun 0:13413ea9a877 324 {
ganlikun 0:13413ea9a877 325 /*Disable of Write protection on the selected Sector*/
ganlikun 0:13413ea9a877 326 status = FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
ganlikun 0:13413ea9a877 327 }
ganlikun 0:13413ea9a877 328 }
ganlikun 0:13413ea9a877 329
ganlikun 0:13413ea9a877 330 /*Read protection configuration*/
ganlikun 0:13413ea9a877 331 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
ganlikun 0:13413ea9a877 332 {
ganlikun 0:13413ea9a877 333 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
ganlikun 0:13413ea9a877 334 }
ganlikun 0:13413ea9a877 335
ganlikun 0:13413ea9a877 336 /*USER configuration*/
ganlikun 0:13413ea9a877 337 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
ganlikun 0:13413ea9a877 338 {
ganlikun 0:13413ea9a877 339 status = FLASH_OB_UserConfig(pOBInit->USERConfig&OB_IWDG_SW,
ganlikun 0:13413ea9a877 340 pOBInit->USERConfig&OB_STOP_NO_RST,
ganlikun 0:13413ea9a877 341 pOBInit->USERConfig&OB_STDBY_NO_RST);
ganlikun 0:13413ea9a877 342 }
ganlikun 0:13413ea9a877 343
ganlikun 0:13413ea9a877 344 /*BOR Level configuration*/
ganlikun 0:13413ea9a877 345 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
ganlikun 0:13413ea9a877 346 {
ganlikun 0:13413ea9a877 347 status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
ganlikun 0:13413ea9a877 348 }
ganlikun 0:13413ea9a877 349
ganlikun 0:13413ea9a877 350 /* Process Unlocked */
ganlikun 0:13413ea9a877 351 __HAL_UNLOCK(&pFlash);
ganlikun 0:13413ea9a877 352
ganlikun 0:13413ea9a877 353 return status;
ganlikun 0:13413ea9a877 354 }
ganlikun 0:13413ea9a877 355
ganlikun 0:13413ea9a877 356 /**
ganlikun 0:13413ea9a877 357 * @brief Get the Option byte configuration
ganlikun 0:13413ea9a877 358 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
ganlikun 0:13413ea9a877 359 * contains the configuration information for the programming.
ganlikun 0:13413ea9a877 360 *
ganlikun 0:13413ea9a877 361 * @retval None
ganlikun 0:13413ea9a877 362 */
ganlikun 0:13413ea9a877 363 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
ganlikun 0:13413ea9a877 364 {
ganlikun 0:13413ea9a877 365 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
ganlikun 0:13413ea9a877 366
ganlikun 0:13413ea9a877 367 /*Get WRP*/
ganlikun 0:13413ea9a877 368 pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP();
ganlikun 0:13413ea9a877 369
ganlikun 0:13413ea9a877 370 /*Get RDP Level*/
ganlikun 0:13413ea9a877 371 pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP();
ganlikun 0:13413ea9a877 372
ganlikun 0:13413ea9a877 373 /*Get USER*/
ganlikun 0:13413ea9a877 374 pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser();
ganlikun 0:13413ea9a877 375
ganlikun 0:13413ea9a877 376 /*Get BOR Level*/
ganlikun 0:13413ea9a877 377 pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR();
ganlikun 0:13413ea9a877 378 }
ganlikun 0:13413ea9a877 379
ganlikun 0:13413ea9a877 380 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
ganlikun 0:13413ea9a877 381 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
ganlikun 0:13413ea9a877 382 defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) ||\
ganlikun 0:13413ea9a877 383 defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
ganlikun 0:13413ea9a877 384 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
ganlikun 0:13413ea9a877 385 /**
ganlikun 0:13413ea9a877 386 * @brief Program option bytes
ganlikun 0:13413ea9a877 387 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
ganlikun 0:13413ea9a877 388 * contains the configuration information for the programming.
ganlikun 0:13413ea9a877 389 *
ganlikun 0:13413ea9a877 390 * @retval HAL Status
ganlikun 0:13413ea9a877 391 */
ganlikun 0:13413ea9a877 392 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
ganlikun 0:13413ea9a877 393 {
ganlikun 0:13413ea9a877 394 HAL_StatusTypeDef status = HAL_ERROR;
ganlikun 0:13413ea9a877 395
ganlikun 0:13413ea9a877 396 /* Check the parameters */
ganlikun 0:13413ea9a877 397 assert_param(IS_OBEX(pAdvOBInit->OptionType));
ganlikun 0:13413ea9a877 398
ganlikun 0:13413ea9a877 399 /*Program PCROP option byte*/
ganlikun 0:13413ea9a877 400 if(((pAdvOBInit->OptionType) & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
ganlikun 0:13413ea9a877 401 {
ganlikun 0:13413ea9a877 402 /* Check the parameters */
ganlikun 0:13413ea9a877 403 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
ganlikun 0:13413ea9a877 404 if((pAdvOBInit->PCROPState) == OB_PCROP_STATE_ENABLE)
ganlikun 0:13413ea9a877 405 {
ganlikun 0:13413ea9a877 406 /*Enable of Write protection on the selected Sector*/
ganlikun 0:13413ea9a877 407 #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
ganlikun 0:13413ea9a877 408 defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
ganlikun 0:13413ea9a877 409 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
ganlikun 0:13413ea9a877 410 status = FLASH_OB_EnablePCROP(pAdvOBInit->Sectors);
ganlikun 0:13413ea9a877 411 #else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 412 status = FLASH_OB_EnablePCROP(pAdvOBInit->SectorsBank1, pAdvOBInit->SectorsBank2, pAdvOBInit->Banks);
ganlikun 0:13413ea9a877 413 #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
ganlikun 0:13413ea9a877 414 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 415 }
ganlikun 0:13413ea9a877 416 else
ganlikun 0:13413ea9a877 417 {
ganlikun 0:13413ea9a877 418 /*Disable of Write protection on the selected Sector*/
ganlikun 0:13413ea9a877 419 #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
ganlikun 0:13413ea9a877 420 defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
ganlikun 0:13413ea9a877 421 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
ganlikun 0:13413ea9a877 422 status = FLASH_OB_DisablePCROP(pAdvOBInit->Sectors);
ganlikun 0:13413ea9a877 423 #else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 424 status = FLASH_OB_DisablePCROP(pAdvOBInit->SectorsBank1, pAdvOBInit->SectorsBank2, pAdvOBInit->Banks);
ganlikun 0:13413ea9a877 425 #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
ganlikun 0:13413ea9a877 426 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 427 }
ganlikun 0:13413ea9a877 428 }
ganlikun 0:13413ea9a877 429
ganlikun 0:13413ea9a877 430 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
ganlikun 0:13413ea9a877 431 /*Program BOOT config option byte*/
ganlikun 0:13413ea9a877 432 if(((pAdvOBInit->OptionType) & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
ganlikun 0:13413ea9a877 433 {
ganlikun 0:13413ea9a877 434 status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
ganlikun 0:13413ea9a877 435 }
ganlikun 0:13413ea9a877 436 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 437
ganlikun 0:13413ea9a877 438 return status;
ganlikun 0:13413ea9a877 439 }
ganlikun 0:13413ea9a877 440
ganlikun 0:13413ea9a877 441 /**
ganlikun 0:13413ea9a877 442 * @brief Get the OBEX byte configuration
ganlikun 0:13413ea9a877 443 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
ganlikun 0:13413ea9a877 444 * contains the configuration information for the programming.
ganlikun 0:13413ea9a877 445 *
ganlikun 0:13413ea9a877 446 * @retval None
ganlikun 0:13413ea9a877 447 */
ganlikun 0:13413ea9a877 448 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
ganlikun 0:13413ea9a877 449 {
ganlikun 0:13413ea9a877 450 #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
ganlikun 0:13413ea9a877 451 defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
ganlikun 0:13413ea9a877 452 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
ganlikun 0:13413ea9a877 453 /*Get Sector*/
ganlikun 0:13413ea9a877 454 pAdvOBInit->Sectors = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
ganlikun 0:13413ea9a877 455 #else /* STM32F427xx || STM32F437xx || STM32F429xx|| STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 456 /*Get Sector for Bank1*/
ganlikun 0:13413ea9a877 457 pAdvOBInit->SectorsBank1 = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
ganlikun 0:13413ea9a877 458
ganlikun 0:13413ea9a877 459 /*Get Sector for Bank2*/
ganlikun 0:13413ea9a877 460 pAdvOBInit->SectorsBank2 = (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
ganlikun 0:13413ea9a877 461
ganlikun 0:13413ea9a877 462 /*Get Boot config OB*/
ganlikun 0:13413ea9a877 463 pAdvOBInit->BootConfig = *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS;
ganlikun 0:13413ea9a877 464 #endif /* STM32F401xC || STM32F401xE || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
ganlikun 0:13413ea9a877 465 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 466 }
ganlikun 0:13413ea9a877 467
ganlikun 0:13413ea9a877 468 /**
ganlikun 0:13413ea9a877 469 * @brief Select the Protection Mode
ganlikun 0:13413ea9a877 470 *
ganlikun 0:13413ea9a877 471 * @note After PCROP activated Option Byte modification NOT POSSIBLE! excepted
ganlikun 0:13413ea9a877 472 * Global Read Out Protection modification (from level1 to level0)
ganlikun 0:13413ea9a877 473 * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
ganlikun 0:13413ea9a877 474 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
ganlikun 0:13413ea9a877 475 * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F401xx/STM32F411xx/STM32F446xx/
ganlikun 0:13413ea9a877 476 * STM32F469xx/STM32F479xx/STM32F412xx/STM32F413xx devices.
ganlikun 0:13413ea9a877 477 *
ganlikun 0:13413ea9a877 478 * @retval HAL Status
ganlikun 0:13413ea9a877 479 */
ganlikun 0:13413ea9a877 480 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
ganlikun 0:13413ea9a877 481 {
ganlikun 0:13413ea9a877 482 uint8_t optiontmp = 0xFF;
ganlikun 0:13413ea9a877 483
ganlikun 0:13413ea9a877 484 /* Mask SPRMOD bit */
ganlikun 0:13413ea9a877 485 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
ganlikun 0:13413ea9a877 486
ganlikun 0:13413ea9a877 487 /* Update Option Byte */
ganlikun 0:13413ea9a877 488 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_SELECTED | optiontmp);
ganlikun 0:13413ea9a877 489
ganlikun 0:13413ea9a877 490 return HAL_OK;
ganlikun 0:13413ea9a877 491 }
ganlikun 0:13413ea9a877 492
ganlikun 0:13413ea9a877 493 /**
ganlikun 0:13413ea9a877 494 * @brief Deselect the Protection Mode
ganlikun 0:13413ea9a877 495 *
ganlikun 0:13413ea9a877 496 * @note After PCROP activated Option Byte modification NOT POSSIBLE! excepted
ganlikun 0:13413ea9a877 497 * Global Read Out Protection modification (from level1 to level0)
ganlikun 0:13413ea9a877 498 * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
ganlikun 0:13413ea9a877 499 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
ganlikun 0:13413ea9a877 500 * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F401xx/STM32F411xx/STM32F446xx/
ganlikun 0:13413ea9a877 501 * STM32F469xx/STM32F479xx/STM32F412xx/STM32F413xx devices.
ganlikun 0:13413ea9a877 502 *
ganlikun 0:13413ea9a877 503 * @retval HAL Status
ganlikun 0:13413ea9a877 504 */
ganlikun 0:13413ea9a877 505 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
ganlikun 0:13413ea9a877 506 {
ganlikun 0:13413ea9a877 507 uint8_t optiontmp = 0xFF;
ganlikun 0:13413ea9a877 508
ganlikun 0:13413ea9a877 509 /* Mask SPRMOD bit */
ganlikun 0:13413ea9a877 510 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
ganlikun 0:13413ea9a877 511
ganlikun 0:13413ea9a877 512 /* Update Option Byte */
ganlikun 0:13413ea9a877 513 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_DESELECTED | optiontmp);
ganlikun 0:13413ea9a877 514
ganlikun 0:13413ea9a877 515 return HAL_OK;
ganlikun 0:13413ea9a877 516 }
ganlikun 0:13413ea9a877 517 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F401xC || STM32F401xE || STM32F410xx ||\
ganlikun 0:13413ea9a877 518 STM32F411xE || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx ||
ganlikun 0:13413ea9a877 519 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 520
ganlikun 0:13413ea9a877 521 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
ganlikun 0:13413ea9a877 522 /**
ganlikun 0:13413ea9a877 523 * @brief Returns the FLASH Write Protection Option Bytes value for Bank 2
ganlikun 0:13413ea9a877 524 * @note This function can be used only for STM32F42xxx/STM32F43xxx/STM32F469xx/STM32F479xx devices.
ganlikun 0:13413ea9a877 525 * @retval The FLASH Write Protection Option Bytes value
ganlikun 0:13413ea9a877 526 */
ganlikun 0:13413ea9a877 527 uint16_t HAL_FLASHEx_OB_GetBank2WRP(void)
ganlikun 0:13413ea9a877 528 {
ganlikun 0:13413ea9a877 529 /* Return the FLASH write protection Register value */
ganlikun 0:13413ea9a877 530 return (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
ganlikun 0:13413ea9a877 531 }
ganlikun 0:13413ea9a877 532 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 533
ganlikun 0:13413ea9a877 534 /**
ganlikun 0:13413ea9a877 535 * @}
ganlikun 0:13413ea9a877 536 */
ganlikun 0:13413ea9a877 537
ganlikun 0:13413ea9a877 538 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
ganlikun 0:13413ea9a877 539 /**
ganlikun 0:13413ea9a877 540 * @brief Full erase of FLASH memory sectors
ganlikun 0:13413ea9a877 541 * @param VoltageRange: The device voltage range which defines the erase parallelism.
ganlikun 0:13413ea9a877 542 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 543 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
ganlikun 0:13413ea9a877 544 * the operation will be done by byte (8-bit)
ganlikun 0:13413ea9a877 545 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
ganlikun 0:13413ea9a877 546 * the operation will be done by half word (16-bit)
ganlikun 0:13413ea9a877 547 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
ganlikun 0:13413ea9a877 548 * the operation will be done by word (32-bit)
ganlikun 0:13413ea9a877 549 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
ganlikun 0:13413ea9a877 550 * the operation will be done by double word (64-bit)
ganlikun 0:13413ea9a877 551 *
ganlikun 0:13413ea9a877 552 * @param Banks: Banks to be erased
ganlikun 0:13413ea9a877 553 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 554 * @arg FLASH_BANK_1: Bank1 to be erased
ganlikun 0:13413ea9a877 555 * @arg FLASH_BANK_2: Bank2 to be erased
ganlikun 0:13413ea9a877 556 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
ganlikun 0:13413ea9a877 557 *
ganlikun 0:13413ea9a877 558 * @retval HAL Status
ganlikun 0:13413ea9a877 559 */
ganlikun 0:13413ea9a877 560 static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
ganlikun 0:13413ea9a877 561 {
ganlikun 0:13413ea9a877 562 /* Check the parameters */
ganlikun 0:13413ea9a877 563 assert_param(IS_VOLTAGERANGE(VoltageRange));
ganlikun 0:13413ea9a877 564 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 565
ganlikun 0:13413ea9a877 566 /* if the previous operation is completed, proceed to erase all sectors */
ganlikun 0:13413ea9a877 567 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
ganlikun 0:13413ea9a877 568
ganlikun 0:13413ea9a877 569 if(Banks == FLASH_BANK_BOTH)
ganlikun 0:13413ea9a877 570 {
ganlikun 0:13413ea9a877 571 /* bank1 & bank2 will be erased*/
ganlikun 0:13413ea9a877 572 FLASH->CR |= FLASH_MER_BIT;
ganlikun 0:13413ea9a877 573 }
ganlikun 0:13413ea9a877 574 else if(Banks == FLASH_BANK_1)
ganlikun 0:13413ea9a877 575 {
ganlikun 0:13413ea9a877 576 /*Only bank1 will be erased*/
ganlikun 0:13413ea9a877 577 FLASH->CR |= FLASH_CR_MER1;
ganlikun 0:13413ea9a877 578 }
ganlikun 0:13413ea9a877 579 else
ganlikun 0:13413ea9a877 580 {
ganlikun 0:13413ea9a877 581 /*Only bank2 will be erased*/
ganlikun 0:13413ea9a877 582 FLASH->CR |= FLASH_CR_MER2;
ganlikun 0:13413ea9a877 583 }
ganlikun 0:13413ea9a877 584 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8U);
ganlikun 0:13413ea9a877 585 }
ganlikun 0:13413ea9a877 586
ganlikun 0:13413ea9a877 587 /**
ganlikun 0:13413ea9a877 588 * @brief Erase the specified FLASH memory sector
ganlikun 0:13413ea9a877 589 * @param Sector: FLASH sector to erase
ganlikun 0:13413ea9a877 590 * The value of this parameter depend on device used within the same series
ganlikun 0:13413ea9a877 591 * @param VoltageRange: The device voltage range which defines the erase parallelism.
ganlikun 0:13413ea9a877 592 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 593 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
ganlikun 0:13413ea9a877 594 * the operation will be done by byte (8-bit)
ganlikun 0:13413ea9a877 595 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
ganlikun 0:13413ea9a877 596 * the operation will be done by half word (16-bit)
ganlikun 0:13413ea9a877 597 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
ganlikun 0:13413ea9a877 598 * the operation will be done by word (32-bit)
ganlikun 0:13413ea9a877 599 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
ganlikun 0:13413ea9a877 600 * the operation will be done by double word (64-bit)
ganlikun 0:13413ea9a877 601 *
ganlikun 0:13413ea9a877 602 * @retval None
ganlikun 0:13413ea9a877 603 */
ganlikun 0:13413ea9a877 604 void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
ganlikun 0:13413ea9a877 605 {
ganlikun 0:13413ea9a877 606 uint32_t tmp_psize = 0U;
ganlikun 0:13413ea9a877 607
ganlikun 0:13413ea9a877 608 /* Check the parameters */
ganlikun 0:13413ea9a877 609 assert_param(IS_FLASH_SECTOR(Sector));
ganlikun 0:13413ea9a877 610 assert_param(IS_VOLTAGERANGE(VoltageRange));
ganlikun 0:13413ea9a877 611
ganlikun 0:13413ea9a877 612 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
ganlikun 0:13413ea9a877 613 {
ganlikun 0:13413ea9a877 614 tmp_psize = FLASH_PSIZE_BYTE;
ganlikun 0:13413ea9a877 615 }
ganlikun 0:13413ea9a877 616 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
ganlikun 0:13413ea9a877 617 {
ganlikun 0:13413ea9a877 618 tmp_psize = FLASH_PSIZE_HALF_WORD;
ganlikun 0:13413ea9a877 619 }
ganlikun 0:13413ea9a877 620 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
ganlikun 0:13413ea9a877 621 {
ganlikun 0:13413ea9a877 622 tmp_psize = FLASH_PSIZE_WORD;
ganlikun 0:13413ea9a877 623 }
ganlikun 0:13413ea9a877 624 else
ganlikun 0:13413ea9a877 625 {
ganlikun 0:13413ea9a877 626 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
ganlikun 0:13413ea9a877 627 }
ganlikun 0:13413ea9a877 628
ganlikun 0:13413ea9a877 629 /* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */
ganlikun 0:13413ea9a877 630 if(Sector > FLASH_SECTOR_11)
ganlikun 0:13413ea9a877 631 {
ganlikun 0:13413ea9a877 632 Sector += 4U;
ganlikun 0:13413ea9a877 633 }
ganlikun 0:13413ea9a877 634 /* If the previous operation is completed, proceed to erase the sector */
ganlikun 0:13413ea9a877 635 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
ganlikun 0:13413ea9a877 636 FLASH->CR |= tmp_psize;
ganlikun 0:13413ea9a877 637 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
ganlikun 0:13413ea9a877 638 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
ganlikun 0:13413ea9a877 639 FLASH->CR |= FLASH_CR_STRT;
ganlikun 0:13413ea9a877 640 }
ganlikun 0:13413ea9a877 641
ganlikun 0:13413ea9a877 642 /**
ganlikun 0:13413ea9a877 643 * @brief Enable the write protection of the desired bank1 or bank 2 sectors
ganlikun 0:13413ea9a877 644 *
ganlikun 0:13413ea9a877 645 * @note When the memory read protection level is selected (RDP level = 1),
ganlikun 0:13413ea9a877 646 * it is not possible to program or erase the flash sector i if CortexM4
ganlikun 0:13413ea9a877 647 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
ganlikun 0:13413ea9a877 648 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
ganlikun 0:13413ea9a877 649 *
ganlikun 0:13413ea9a877 650 * @param WRPSector: specifies the sector(s) to be write protected.
ganlikun 0:13413ea9a877 651 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 652 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_23
ganlikun 0:13413ea9a877 653 * @arg OB_WRP_SECTOR_All
ganlikun 0:13413ea9a877 654 * @note BANK2 starts from OB_WRP_SECTOR_12
ganlikun 0:13413ea9a877 655 *
ganlikun 0:13413ea9a877 656 * @param Banks: Enable write protection on all the sectors for the specific bank
ganlikun 0:13413ea9a877 657 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 658 * @arg FLASH_BANK_1: WRP on all sectors of bank1
ganlikun 0:13413ea9a877 659 * @arg FLASH_BANK_2: WRP on all sectors of bank2
ganlikun 0:13413ea9a877 660 * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
ganlikun 0:13413ea9a877 661 *
ganlikun 0:13413ea9a877 662 * @retval HAL FLASH State
ganlikun 0:13413ea9a877 663 */
ganlikun 0:13413ea9a877 664 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
ganlikun 0:13413ea9a877 665 {
ganlikun 0:13413ea9a877 666 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 667
ganlikun 0:13413ea9a877 668 /* Check the parameters */
ganlikun 0:13413ea9a877 669 assert_param(IS_OB_WRP_SECTOR(WRPSector));
ganlikun 0:13413ea9a877 670 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 671
ganlikun 0:13413ea9a877 672 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 673 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 674
ganlikun 0:13413ea9a877 675 if(status == HAL_OK)
ganlikun 0:13413ea9a877 676 {
ganlikun 0:13413ea9a877 677 if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
ganlikun 0:13413ea9a877 678 (WRPSector < OB_WRP_SECTOR_12))
ganlikun 0:13413ea9a877 679 {
ganlikun 0:13413ea9a877 680 if(WRPSector == OB_WRP_SECTOR_All)
ganlikun 0:13413ea9a877 681 {
ganlikun 0:13413ea9a877 682 /*Write protection on all sector of BANK1*/
ganlikun 0:13413ea9a877 683 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~(WRPSector>>12));
ganlikun 0:13413ea9a877 684 }
ganlikun 0:13413ea9a877 685 else
ganlikun 0:13413ea9a877 686 {
ganlikun 0:13413ea9a877 687 /*Write protection done on sectors of BANK1*/
ganlikun 0:13413ea9a877 688 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
ganlikun 0:13413ea9a877 689 }
ganlikun 0:13413ea9a877 690 }
ganlikun 0:13413ea9a877 691 else
ganlikun 0:13413ea9a877 692 {
ganlikun 0:13413ea9a877 693 /*Write protection done on sectors of BANK2*/
ganlikun 0:13413ea9a877 694 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
ganlikun 0:13413ea9a877 695 }
ganlikun 0:13413ea9a877 696
ganlikun 0:13413ea9a877 697 /*Write protection on all sector of BANK2*/
ganlikun 0:13413ea9a877 698 if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
ganlikun 0:13413ea9a877 699 {
ganlikun 0:13413ea9a877 700 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 701 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 702
ganlikun 0:13413ea9a877 703 if(status == HAL_OK)
ganlikun 0:13413ea9a877 704 {
ganlikun 0:13413ea9a877 705 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
ganlikun 0:13413ea9a877 706 }
ganlikun 0:13413ea9a877 707 }
ganlikun 0:13413ea9a877 708
ganlikun 0:13413ea9a877 709 }
ganlikun 0:13413ea9a877 710 return status;
ganlikun 0:13413ea9a877 711 }
ganlikun 0:13413ea9a877 712
ganlikun 0:13413ea9a877 713 /**
ganlikun 0:13413ea9a877 714 * @brief Disable the write protection of the desired bank1 or bank 2 sectors
ganlikun 0:13413ea9a877 715 *
ganlikun 0:13413ea9a877 716 * @note When the memory read protection level is selected (RDP level = 1),
ganlikun 0:13413ea9a877 717 * it is not possible to program or erase the flash sector i if CortexM4
ganlikun 0:13413ea9a877 718 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
ganlikun 0:13413ea9a877 719 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
ganlikun 0:13413ea9a877 720 *
ganlikun 0:13413ea9a877 721 * @param WRPSector: specifies the sector(s) to be write protected.
ganlikun 0:13413ea9a877 722 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 723 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_23
ganlikun 0:13413ea9a877 724 * @arg OB_WRP_Sector_All
ganlikun 0:13413ea9a877 725 * @note BANK2 starts from OB_WRP_SECTOR_12
ganlikun 0:13413ea9a877 726 *
ganlikun 0:13413ea9a877 727 * @param Banks: Disable write protection on all the sectors for the specific bank
ganlikun 0:13413ea9a877 728 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 729 * @arg FLASH_BANK_1: Bank1 to be erased
ganlikun 0:13413ea9a877 730 * @arg FLASH_BANK_2: Bank2 to be erased
ganlikun 0:13413ea9a877 731 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
ganlikun 0:13413ea9a877 732 *
ganlikun 0:13413ea9a877 733 * @retval HAL Status
ganlikun 0:13413ea9a877 734 */
ganlikun 0:13413ea9a877 735 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
ganlikun 0:13413ea9a877 736 {
ganlikun 0:13413ea9a877 737 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 738
ganlikun 0:13413ea9a877 739 /* Check the parameters */
ganlikun 0:13413ea9a877 740 assert_param(IS_OB_WRP_SECTOR(WRPSector));
ganlikun 0:13413ea9a877 741 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 742
ganlikun 0:13413ea9a877 743 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 744 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 745
ganlikun 0:13413ea9a877 746 if(status == HAL_OK)
ganlikun 0:13413ea9a877 747 {
ganlikun 0:13413ea9a877 748 if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
ganlikun 0:13413ea9a877 749 (WRPSector < OB_WRP_SECTOR_12))
ganlikun 0:13413ea9a877 750 {
ganlikun 0:13413ea9a877 751 if(WRPSector == OB_WRP_SECTOR_All)
ganlikun 0:13413ea9a877 752 {
ganlikun 0:13413ea9a877 753 /*Write protection on all sector of BANK1*/
ganlikun 0:13413ea9a877 754 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
ganlikun 0:13413ea9a877 755 }
ganlikun 0:13413ea9a877 756 else
ganlikun 0:13413ea9a877 757 {
ganlikun 0:13413ea9a877 758 /*Write protection done on sectors of BANK1*/
ganlikun 0:13413ea9a877 759 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
ganlikun 0:13413ea9a877 760 }
ganlikun 0:13413ea9a877 761 }
ganlikun 0:13413ea9a877 762 else
ganlikun 0:13413ea9a877 763 {
ganlikun 0:13413ea9a877 764 /*Write protection done on sectors of BANK2*/
ganlikun 0:13413ea9a877 765 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
ganlikun 0:13413ea9a877 766 }
ganlikun 0:13413ea9a877 767
ganlikun 0:13413ea9a877 768 /*Write protection on all sector of BANK2*/
ganlikun 0:13413ea9a877 769 if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
ganlikun 0:13413ea9a877 770 {
ganlikun 0:13413ea9a877 771 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 772 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 773
ganlikun 0:13413ea9a877 774 if(status == HAL_OK)
ganlikun 0:13413ea9a877 775 {
ganlikun 0:13413ea9a877 776 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
ganlikun 0:13413ea9a877 777 }
ganlikun 0:13413ea9a877 778 }
ganlikun 0:13413ea9a877 779
ganlikun 0:13413ea9a877 780 }
ganlikun 0:13413ea9a877 781
ganlikun 0:13413ea9a877 782 return status;
ganlikun 0:13413ea9a877 783 }
ganlikun 0:13413ea9a877 784
ganlikun 0:13413ea9a877 785 /**
ganlikun 0:13413ea9a877 786 * @brief Configure the Dual Bank Boot.
ganlikun 0:13413ea9a877 787 *
ganlikun 0:13413ea9a877 788 * @note This function can be used only for STM32F42xxx/43xxx devices.
ganlikun 0:13413ea9a877 789 *
ganlikun 0:13413ea9a877 790 * @param BootConfig specifies the Dual Bank Boot Option byte.
ganlikun 0:13413ea9a877 791 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 792 * @arg OB_Dual_BootEnabled: Dual Bank Boot Enable
ganlikun 0:13413ea9a877 793 * @arg OB_Dual_BootDisabled: Dual Bank Boot Disabled
ganlikun 0:13413ea9a877 794 * @retval None
ganlikun 0:13413ea9a877 795 */
ganlikun 0:13413ea9a877 796 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig)
ganlikun 0:13413ea9a877 797 {
ganlikun 0:13413ea9a877 798 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 799
ganlikun 0:13413ea9a877 800 /* Check the parameters */
ganlikun 0:13413ea9a877 801 assert_param(IS_OB_BOOT(BootConfig));
ganlikun 0:13413ea9a877 802
ganlikun 0:13413ea9a877 803 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 804 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 805
ganlikun 0:13413ea9a877 806 if(status == HAL_OK)
ganlikun 0:13413ea9a877 807 {
ganlikun 0:13413ea9a877 808 /* Set Dual Bank Boot */
ganlikun 0:13413ea9a877 809 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BFB2);
ganlikun 0:13413ea9a877 810 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= BootConfig;
ganlikun 0:13413ea9a877 811 }
ganlikun 0:13413ea9a877 812
ganlikun 0:13413ea9a877 813 return status;
ganlikun 0:13413ea9a877 814 }
ganlikun 0:13413ea9a877 815
ganlikun 0:13413ea9a877 816 /**
ganlikun 0:13413ea9a877 817 * @brief Enable the read/write protection (PCROP) of the desired
ganlikun 0:13413ea9a877 818 * sectors of Bank 1 and/or Bank 2.
ganlikun 0:13413ea9a877 819 * @note This function can be used only for STM32F42xxx/43xxx devices.
ganlikun 0:13413ea9a877 820 * @param SectorBank1 Specifies the sector(s) to be read/write protected or unprotected for bank1.
ganlikun 0:13413ea9a877 821 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 822 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_11
ganlikun 0:13413ea9a877 823 * @arg OB_PCROP_SECTOR__All
ganlikun 0:13413ea9a877 824 * @param SectorBank2 Specifies the sector(s) to be read/write protected or unprotected for bank2.
ganlikun 0:13413ea9a877 825 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 826 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_12 and OB_PCROP_SECTOR_23
ganlikun 0:13413ea9a877 827 * @arg OB_PCROP_SECTOR__All
ganlikun 0:13413ea9a877 828 * @param Banks Enable PCROP protection on all the sectors for the specific bank
ganlikun 0:13413ea9a877 829 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 830 * @arg FLASH_BANK_1: WRP on all sectors of bank1
ganlikun 0:13413ea9a877 831 * @arg FLASH_BANK_2: WRP on all sectors of bank2
ganlikun 0:13413ea9a877 832 * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
ganlikun 0:13413ea9a877 833 *
ganlikun 0:13413ea9a877 834 * @retval HAL Status
ganlikun 0:13413ea9a877 835 */
ganlikun 0:13413ea9a877 836 static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks)
ganlikun 0:13413ea9a877 837 {
ganlikun 0:13413ea9a877 838 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 839
ganlikun 0:13413ea9a877 840 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 841
ganlikun 0:13413ea9a877 842 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 843 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 844
ganlikun 0:13413ea9a877 845 if(status == HAL_OK)
ganlikun 0:13413ea9a877 846 {
ganlikun 0:13413ea9a877 847 if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
ganlikun 0:13413ea9a877 848 {
ganlikun 0:13413ea9a877 849 assert_param(IS_OB_PCROP(SectorBank1));
ganlikun 0:13413ea9a877 850 /*Write protection done on sectors of BANK1*/
ganlikun 0:13413ea9a877 851 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)SectorBank1;
ganlikun 0:13413ea9a877 852 }
ganlikun 0:13413ea9a877 853 else
ganlikun 0:13413ea9a877 854 {
ganlikun 0:13413ea9a877 855 assert_param(IS_OB_PCROP(SectorBank2));
ganlikun 0:13413ea9a877 856 /*Write protection done on sectors of BANK2*/
ganlikun 0:13413ea9a877 857 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
ganlikun 0:13413ea9a877 858 }
ganlikun 0:13413ea9a877 859
ganlikun 0:13413ea9a877 860 /*Write protection on all sector of BANK2*/
ganlikun 0:13413ea9a877 861 if(Banks == FLASH_BANK_BOTH)
ganlikun 0:13413ea9a877 862 {
ganlikun 0:13413ea9a877 863 assert_param(IS_OB_PCROP(SectorBank2));
ganlikun 0:13413ea9a877 864 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 865 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 866
ganlikun 0:13413ea9a877 867 if(status == HAL_OK)
ganlikun 0:13413ea9a877 868 {
ganlikun 0:13413ea9a877 869 /*Write protection done on sectors of BANK2*/
ganlikun 0:13413ea9a877 870 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
ganlikun 0:13413ea9a877 871 }
ganlikun 0:13413ea9a877 872 }
ganlikun 0:13413ea9a877 873
ganlikun 0:13413ea9a877 874 }
ganlikun 0:13413ea9a877 875
ganlikun 0:13413ea9a877 876 return status;
ganlikun 0:13413ea9a877 877 }
ganlikun 0:13413ea9a877 878
ganlikun 0:13413ea9a877 879
ganlikun 0:13413ea9a877 880 /**
ganlikun 0:13413ea9a877 881 * @brief Disable the read/write protection (PCROP) of the desired
ganlikun 0:13413ea9a877 882 * sectors of Bank 1 and/or Bank 2.
ganlikun 0:13413ea9a877 883 * @note This function can be used only for STM32F42xxx/43xxx devices.
ganlikun 0:13413ea9a877 884 * @param SectorBank1 specifies the sector(s) to be read/write protected or unprotected for bank1.
ganlikun 0:13413ea9a877 885 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 886 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_0 and OB_PCROP_SECTOR_11
ganlikun 0:13413ea9a877 887 * @arg OB_PCROP_SECTOR__All
ganlikun 0:13413ea9a877 888 * @param SectorBank2 Specifies the sector(s) to be read/write protected or unprotected for bank2.
ganlikun 0:13413ea9a877 889 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 890 * @arg OB_PCROP: A value between OB_PCROP_SECTOR_12 and OB_PCROP_SECTOR_23
ganlikun 0:13413ea9a877 891 * @arg OB_PCROP_SECTOR__All
ganlikun 0:13413ea9a877 892 * @param Banks Disable PCROP protection on all the sectors for the specific bank
ganlikun 0:13413ea9a877 893 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 894 * @arg FLASH_BANK_1: WRP on all sectors of bank1
ganlikun 0:13413ea9a877 895 * @arg FLASH_BANK_2: WRP on all sectors of bank2
ganlikun 0:13413ea9a877 896 * @arg FLASH_BANK_BOTH: WRP on all sectors of bank1 & bank2
ganlikun 0:13413ea9a877 897 *
ganlikun 0:13413ea9a877 898 * @retval HAL Status
ganlikun 0:13413ea9a877 899 */
ganlikun 0:13413ea9a877 900 static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks)
ganlikun 0:13413ea9a877 901 {
ganlikun 0:13413ea9a877 902 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 903
ganlikun 0:13413ea9a877 904 /* Check the parameters */
ganlikun 0:13413ea9a877 905 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 906
ganlikun 0:13413ea9a877 907 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 908 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 909
ganlikun 0:13413ea9a877 910 if(status == HAL_OK)
ganlikun 0:13413ea9a877 911 {
ganlikun 0:13413ea9a877 912 if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
ganlikun 0:13413ea9a877 913 {
ganlikun 0:13413ea9a877 914 assert_param(IS_OB_PCROP(SectorBank1));
ganlikun 0:13413ea9a877 915 /*Write protection done on sectors of BANK1*/
ganlikun 0:13413ea9a877 916 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~SectorBank1);
ganlikun 0:13413ea9a877 917 }
ganlikun 0:13413ea9a877 918 else
ganlikun 0:13413ea9a877 919 {
ganlikun 0:13413ea9a877 920 /*Write protection done on sectors of BANK2*/
ganlikun 0:13413ea9a877 921 assert_param(IS_OB_PCROP(SectorBank2));
ganlikun 0:13413ea9a877 922 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
ganlikun 0:13413ea9a877 923 }
ganlikun 0:13413ea9a877 924
ganlikun 0:13413ea9a877 925 /*Write protection on all sector of BANK2*/
ganlikun 0:13413ea9a877 926 if(Banks == FLASH_BANK_BOTH)
ganlikun 0:13413ea9a877 927 {
ganlikun 0:13413ea9a877 928 assert_param(IS_OB_PCROP(SectorBank2));
ganlikun 0:13413ea9a877 929 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 930 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 931
ganlikun 0:13413ea9a877 932 if(status == HAL_OK)
ganlikun 0:13413ea9a877 933 {
ganlikun 0:13413ea9a877 934 /*Write protection done on sectors of BANK2*/
ganlikun 0:13413ea9a877 935 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
ganlikun 0:13413ea9a877 936 }
ganlikun 0:13413ea9a877 937 }
ganlikun 0:13413ea9a877 938
ganlikun 0:13413ea9a877 939 }
ganlikun 0:13413ea9a877 940
ganlikun 0:13413ea9a877 941 return status;
ganlikun 0:13413ea9a877 942
ganlikun 0:13413ea9a877 943 }
ganlikun 0:13413ea9a877 944
ganlikun 0:13413ea9a877 945 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
ganlikun 0:13413ea9a877 946
ganlikun 0:13413ea9a877 947 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
ganlikun 0:13413ea9a877 948 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
ganlikun 0:13413ea9a877 949 defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) ||\
ganlikun 0:13413ea9a877 950 defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||\
ganlikun 0:13413ea9a877 951 defined(STM32F423xx)
ganlikun 0:13413ea9a877 952 /**
ganlikun 0:13413ea9a877 953 * @brief Mass erase of FLASH memory
ganlikun 0:13413ea9a877 954 * @param VoltageRange: The device voltage range which defines the erase parallelism.
ganlikun 0:13413ea9a877 955 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 956 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
ganlikun 0:13413ea9a877 957 * the operation will be done by byte (8-bit)
ganlikun 0:13413ea9a877 958 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
ganlikun 0:13413ea9a877 959 * the operation will be done by half word (16-bit)
ganlikun 0:13413ea9a877 960 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
ganlikun 0:13413ea9a877 961 * the operation will be done by word (32-bit)
ganlikun 0:13413ea9a877 962 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
ganlikun 0:13413ea9a877 963 * the operation will be done by double word (64-bit)
ganlikun 0:13413ea9a877 964 *
ganlikun 0:13413ea9a877 965 * @param Banks: Banks to be erased
ganlikun 0:13413ea9a877 966 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 967 * @arg FLASH_BANK_1: Bank1 to be erased
ganlikun 0:13413ea9a877 968 *
ganlikun 0:13413ea9a877 969 * @retval None
ganlikun 0:13413ea9a877 970 */
ganlikun 0:13413ea9a877 971 static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
ganlikun 0:13413ea9a877 972 {
ganlikun 0:13413ea9a877 973 /* Check the parameters */
ganlikun 0:13413ea9a877 974 assert_param(IS_VOLTAGERANGE(VoltageRange));
ganlikun 0:13413ea9a877 975 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 976
ganlikun 0:13413ea9a877 977 /* If the previous operation is completed, proceed to erase all sectors */
ganlikun 0:13413ea9a877 978 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
ganlikun 0:13413ea9a877 979 FLASH->CR |= FLASH_CR_MER;
ganlikun 0:13413ea9a877 980 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8U);
ganlikun 0:13413ea9a877 981 }
ganlikun 0:13413ea9a877 982
ganlikun 0:13413ea9a877 983 /**
ganlikun 0:13413ea9a877 984 * @brief Erase the specified FLASH memory sector
ganlikun 0:13413ea9a877 985 * @param Sector: FLASH sector to erase
ganlikun 0:13413ea9a877 986 * The value of this parameter depend on device used within the same series
ganlikun 0:13413ea9a877 987 * @param VoltageRange: The device voltage range which defines the erase parallelism.
ganlikun 0:13413ea9a877 988 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 989 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
ganlikun 0:13413ea9a877 990 * the operation will be done by byte (8-bit)
ganlikun 0:13413ea9a877 991 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
ganlikun 0:13413ea9a877 992 * the operation will be done by half word (16-bit)
ganlikun 0:13413ea9a877 993 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
ganlikun 0:13413ea9a877 994 * the operation will be done by word (32-bit)
ganlikun 0:13413ea9a877 995 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
ganlikun 0:13413ea9a877 996 * the operation will be done by double word (64-bit)
ganlikun 0:13413ea9a877 997 *
ganlikun 0:13413ea9a877 998 * @retval None
ganlikun 0:13413ea9a877 999 */
ganlikun 0:13413ea9a877 1000 void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
ganlikun 0:13413ea9a877 1001 {
ganlikun 0:13413ea9a877 1002 uint32_t tmp_psize = 0U;
ganlikun 0:13413ea9a877 1003
ganlikun 0:13413ea9a877 1004 /* Check the parameters */
ganlikun 0:13413ea9a877 1005 assert_param(IS_FLASH_SECTOR(Sector));
ganlikun 0:13413ea9a877 1006 assert_param(IS_VOLTAGERANGE(VoltageRange));
ganlikun 0:13413ea9a877 1007
ganlikun 0:13413ea9a877 1008 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
ganlikun 0:13413ea9a877 1009 {
ganlikun 0:13413ea9a877 1010 tmp_psize = FLASH_PSIZE_BYTE;
ganlikun 0:13413ea9a877 1011 }
ganlikun 0:13413ea9a877 1012 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
ganlikun 0:13413ea9a877 1013 {
ganlikun 0:13413ea9a877 1014 tmp_psize = FLASH_PSIZE_HALF_WORD;
ganlikun 0:13413ea9a877 1015 }
ganlikun 0:13413ea9a877 1016 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
ganlikun 0:13413ea9a877 1017 {
ganlikun 0:13413ea9a877 1018 tmp_psize = FLASH_PSIZE_WORD;
ganlikun 0:13413ea9a877 1019 }
ganlikun 0:13413ea9a877 1020 else
ganlikun 0:13413ea9a877 1021 {
ganlikun 0:13413ea9a877 1022 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
ganlikun 0:13413ea9a877 1023 }
ganlikun 0:13413ea9a877 1024
ganlikun 0:13413ea9a877 1025 /* If the previous operation is completed, proceed to erase the sector */
ganlikun 0:13413ea9a877 1026 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
ganlikun 0:13413ea9a877 1027 FLASH->CR |= tmp_psize;
ganlikun 0:13413ea9a877 1028 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
ganlikun 0:13413ea9a877 1029 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
ganlikun 0:13413ea9a877 1030 FLASH->CR |= FLASH_CR_STRT;
ganlikun 0:13413ea9a877 1031 }
ganlikun 0:13413ea9a877 1032
ganlikun 0:13413ea9a877 1033 /**
ganlikun 0:13413ea9a877 1034 * @brief Enable the write protection of the desired bank 1 sectors
ganlikun 0:13413ea9a877 1035 *
ganlikun 0:13413ea9a877 1036 * @note When the memory read protection level is selected (RDP level = 1),
ganlikun 0:13413ea9a877 1037 * it is not possible to program or erase the flash sector i if CortexM4
ganlikun 0:13413ea9a877 1038 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
ganlikun 0:13413ea9a877 1039 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
ganlikun 0:13413ea9a877 1040 *
ganlikun 0:13413ea9a877 1041 * @param WRPSector: specifies the sector(s) to be write protected.
ganlikun 0:13413ea9a877 1042 * The value of this parameter depend on device used within the same series
ganlikun 0:13413ea9a877 1043 *
ganlikun 0:13413ea9a877 1044 * @param Banks: Enable write protection on all the sectors for the specific bank
ganlikun 0:13413ea9a877 1045 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1046 * @arg FLASH_BANK_1: WRP on all sectors of bank1
ganlikun 0:13413ea9a877 1047 *
ganlikun 0:13413ea9a877 1048 * @retval HAL Status
ganlikun 0:13413ea9a877 1049 */
ganlikun 0:13413ea9a877 1050 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
ganlikun 0:13413ea9a877 1051 {
ganlikun 0:13413ea9a877 1052 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 1053
ganlikun 0:13413ea9a877 1054 /* Check the parameters */
ganlikun 0:13413ea9a877 1055 assert_param(IS_OB_WRP_SECTOR(WRPSector));
ganlikun 0:13413ea9a877 1056 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 1057
ganlikun 0:13413ea9a877 1058 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 1059 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 1060
ganlikun 0:13413ea9a877 1061 if(status == HAL_OK)
ganlikun 0:13413ea9a877 1062 {
ganlikun 0:13413ea9a877 1063 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
ganlikun 0:13413ea9a877 1064 }
ganlikun 0:13413ea9a877 1065
ganlikun 0:13413ea9a877 1066 return status;
ganlikun 0:13413ea9a877 1067 }
ganlikun 0:13413ea9a877 1068
ganlikun 0:13413ea9a877 1069 /**
ganlikun 0:13413ea9a877 1070 * @brief Disable the write protection of the desired bank 1 sectors
ganlikun 0:13413ea9a877 1071 *
ganlikun 0:13413ea9a877 1072 * @note When the memory read protection level is selected (RDP level = 1),
ganlikun 0:13413ea9a877 1073 * it is not possible to program or erase the flash sector i if CortexM4
ganlikun 0:13413ea9a877 1074 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
ganlikun 0:13413ea9a877 1075 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
ganlikun 0:13413ea9a877 1076 *
ganlikun 0:13413ea9a877 1077 * @param WRPSector: specifies the sector(s) to be write protected.
ganlikun 0:13413ea9a877 1078 * The value of this parameter depend on device used within the same series
ganlikun 0:13413ea9a877 1079 *
ganlikun 0:13413ea9a877 1080 * @param Banks: Enable write protection on all the sectors for the specific bank
ganlikun 0:13413ea9a877 1081 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1082 * @arg FLASH_BANK_1: WRP on all sectors of bank1
ganlikun 0:13413ea9a877 1083 *
ganlikun 0:13413ea9a877 1084 * @retval HAL Status
ganlikun 0:13413ea9a877 1085 */
ganlikun 0:13413ea9a877 1086 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
ganlikun 0:13413ea9a877 1087 {
ganlikun 0:13413ea9a877 1088 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 1089
ganlikun 0:13413ea9a877 1090 /* Check the parameters */
ganlikun 0:13413ea9a877 1091 assert_param(IS_OB_WRP_SECTOR(WRPSector));
ganlikun 0:13413ea9a877 1092 assert_param(IS_FLASH_BANK(Banks));
ganlikun 0:13413ea9a877 1093
ganlikun 0:13413ea9a877 1094 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 1095 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 1096
ganlikun 0:13413ea9a877 1097 if(status == HAL_OK)
ganlikun 0:13413ea9a877 1098 {
ganlikun 0:13413ea9a877 1099 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
ganlikun 0:13413ea9a877 1100 }
ganlikun 0:13413ea9a877 1101
ganlikun 0:13413ea9a877 1102 return status;
ganlikun 0:13413ea9a877 1103 }
ganlikun 0:13413ea9a877 1104 #endif /* STM32F40xxx || STM32F41xxx || STM32F401xx || STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx
ganlikun 0:13413ea9a877 1105 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 1106
ganlikun 0:13413ea9a877 1107 #if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
ganlikun 0:13413ea9a877 1108 defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
ganlikun 0:13413ea9a877 1109 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
ganlikun 0:13413ea9a877 1110 /**
ganlikun 0:13413ea9a877 1111 * @brief Enable the read/write protection (PCROP) of the desired sectors.
ganlikun 0:13413ea9a877 1112 * @note This function can be used only for STM32F401xx devices.
ganlikun 0:13413ea9a877 1113 * @param Sector specifies the sector(s) to be read/write protected or unprotected.
ganlikun 0:13413ea9a877 1114 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1115 * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector5
ganlikun 0:13413ea9a877 1116 * @arg OB_PCROP_Sector_All
ganlikun 0:13413ea9a877 1117 * @retval HAL Status
ganlikun 0:13413ea9a877 1118 */
ganlikun 0:13413ea9a877 1119 static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector)
ganlikun 0:13413ea9a877 1120 {
ganlikun 0:13413ea9a877 1121 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 1122
ganlikun 0:13413ea9a877 1123 /* Check the parameters */
ganlikun 0:13413ea9a877 1124 assert_param(IS_OB_PCROP(Sector));
ganlikun 0:13413ea9a877 1125
ganlikun 0:13413ea9a877 1126 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 1127 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 1128
ganlikun 0:13413ea9a877 1129 if(status == HAL_OK)
ganlikun 0:13413ea9a877 1130 {
ganlikun 0:13413ea9a877 1131 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)Sector;
ganlikun 0:13413ea9a877 1132 }
ganlikun 0:13413ea9a877 1133
ganlikun 0:13413ea9a877 1134 return status;
ganlikun 0:13413ea9a877 1135 }
ganlikun 0:13413ea9a877 1136
ganlikun 0:13413ea9a877 1137
ganlikun 0:13413ea9a877 1138 /**
ganlikun 0:13413ea9a877 1139 * @brief Disable the read/write protection (PCROP) of the desired sectors.
ganlikun 0:13413ea9a877 1140 * @note This function can be used only for STM32F401xx devices.
ganlikun 0:13413ea9a877 1141 * @param Sector specifies the sector(s) to be read/write protected or unprotected.
ganlikun 0:13413ea9a877 1142 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1143 * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector5
ganlikun 0:13413ea9a877 1144 * @arg OB_PCROP_Sector_All
ganlikun 0:13413ea9a877 1145 * @retval HAL Status
ganlikun 0:13413ea9a877 1146 */
ganlikun 0:13413ea9a877 1147 static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector)
ganlikun 0:13413ea9a877 1148 {
ganlikun 0:13413ea9a877 1149 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 1150
ganlikun 0:13413ea9a877 1151 /* Check the parameters */
ganlikun 0:13413ea9a877 1152 assert_param(IS_OB_PCROP(Sector));
ganlikun 0:13413ea9a877 1153
ganlikun 0:13413ea9a877 1154 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 1155 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 1156
ganlikun 0:13413ea9a877 1157 if(status == HAL_OK)
ganlikun 0:13413ea9a877 1158 {
ganlikun 0:13413ea9a877 1159 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~Sector);
ganlikun 0:13413ea9a877 1160 }
ganlikun 0:13413ea9a877 1161
ganlikun 0:13413ea9a877 1162 return status;
ganlikun 0:13413ea9a877 1163
ganlikun 0:13413ea9a877 1164 }
ganlikun 0:13413ea9a877 1165 #endif /* STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx
ganlikun 0:13413ea9a877 1166 STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 1167
ganlikun 0:13413ea9a877 1168 /**
ganlikun 0:13413ea9a877 1169 * @brief Set the read protection level.
ganlikun 0:13413ea9a877 1170 * @param Level: specifies the read protection level.
ganlikun 0:13413ea9a877 1171 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1172 * @arg OB_RDP_LEVEL_0: No protection
ganlikun 0:13413ea9a877 1173 * @arg OB_RDP_LEVEL_1: Read protection of the memory
ganlikun 0:13413ea9a877 1174 * @arg OB_RDP_LEVEL_2: Full chip protection
ganlikun 0:13413ea9a877 1175 *
ganlikun 0:13413ea9a877 1176 * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
ganlikun 0:13413ea9a877 1177 *
ganlikun 0:13413ea9a877 1178 * @retval HAL Status
ganlikun 0:13413ea9a877 1179 */
ganlikun 0:13413ea9a877 1180 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
ganlikun 0:13413ea9a877 1181 {
ganlikun 0:13413ea9a877 1182 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 1183
ganlikun 0:13413ea9a877 1184 /* Check the parameters */
ganlikun 0:13413ea9a877 1185 assert_param(IS_OB_RDP_LEVEL(Level));
ganlikun 0:13413ea9a877 1186
ganlikun 0:13413ea9a877 1187 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 1188 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 1189
ganlikun 0:13413ea9a877 1190 if(status == HAL_OK)
ganlikun 0:13413ea9a877 1191 {
ganlikun 0:13413ea9a877 1192 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
ganlikun 0:13413ea9a877 1193 }
ganlikun 0:13413ea9a877 1194
ganlikun 0:13413ea9a877 1195 return status;
ganlikun 0:13413ea9a877 1196 }
ganlikun 0:13413ea9a877 1197
ganlikun 0:13413ea9a877 1198 /**
ganlikun 0:13413ea9a877 1199 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
ganlikun 0:13413ea9a877 1200 * @param Iwdg: Selects the IWDG mode
ganlikun 0:13413ea9a877 1201 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1202 * @arg OB_IWDG_SW: Software IWDG selected
ganlikun 0:13413ea9a877 1203 * @arg OB_IWDG_HW: Hardware IWDG selected
ganlikun 0:13413ea9a877 1204 * @param Stop: Reset event when entering STOP mode.
ganlikun 0:13413ea9a877 1205 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1206 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
ganlikun 0:13413ea9a877 1207 * @arg OB_STOP_RST: Reset generated when entering in STOP
ganlikun 0:13413ea9a877 1208 * @param Stdby: Reset event when entering Standby mode.
ganlikun 0:13413ea9a877 1209 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1210 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
ganlikun 0:13413ea9a877 1211 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
ganlikun 0:13413ea9a877 1212 * @retval HAL Status
ganlikun 0:13413ea9a877 1213 */
ganlikun 0:13413ea9a877 1214 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
ganlikun 0:13413ea9a877 1215 {
ganlikun 0:13413ea9a877 1216 uint8_t optiontmp = 0xFF;
ganlikun 0:13413ea9a877 1217 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 1218
ganlikun 0:13413ea9a877 1219 /* Check the parameters */
ganlikun 0:13413ea9a877 1220 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
ganlikun 0:13413ea9a877 1221 assert_param(IS_OB_STOP_SOURCE(Stop));
ganlikun 0:13413ea9a877 1222 assert_param(IS_OB_STDBY_SOURCE(Stdby));
ganlikun 0:13413ea9a877 1223
ganlikun 0:13413ea9a877 1224 /* Wait for last operation to be completed */
ganlikun 0:13413ea9a877 1225 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
ganlikun 0:13413ea9a877 1226
ganlikun 0:13413ea9a877 1227 if(status == HAL_OK)
ganlikun 0:13413ea9a877 1228 {
ganlikun 0:13413ea9a877 1229 /* Mask OPTLOCK, OPTSTRT, BOR_LEV and BFB2 bits */
ganlikun 0:13413ea9a877 1230 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1F);
ganlikun 0:13413ea9a877 1231
ganlikun 0:13413ea9a877 1232 /* Update User Option Byte */
ganlikun 0:13413ea9a877 1233 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = Iwdg | (uint8_t)(Stdby | (uint8_t)(Stop | ((uint8_t)optiontmp)));
ganlikun 0:13413ea9a877 1234 }
ganlikun 0:13413ea9a877 1235
ganlikun 0:13413ea9a877 1236 return status;
ganlikun 0:13413ea9a877 1237 }
ganlikun 0:13413ea9a877 1238
ganlikun 0:13413ea9a877 1239 /**
ganlikun 0:13413ea9a877 1240 * @brief Set the BOR Level.
ganlikun 0:13413ea9a877 1241 * @param Level: specifies the Option Bytes BOR Reset Level.
ganlikun 0:13413ea9a877 1242 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1243 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
ganlikun 0:13413ea9a877 1244 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
ganlikun 0:13413ea9a877 1245 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
ganlikun 0:13413ea9a877 1246 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
ganlikun 0:13413ea9a877 1247 * @retval HAL Status
ganlikun 0:13413ea9a877 1248 */
ganlikun 0:13413ea9a877 1249 static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
ganlikun 0:13413ea9a877 1250 {
ganlikun 0:13413ea9a877 1251 /* Check the parameters */
ganlikun 0:13413ea9a877 1252 assert_param(IS_OB_BOR_LEVEL(Level));
ganlikun 0:13413ea9a877 1253
ganlikun 0:13413ea9a877 1254 /* Set the BOR Level */
ganlikun 0:13413ea9a877 1255 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
ganlikun 0:13413ea9a877 1256 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= Level;
ganlikun 0:13413ea9a877 1257
ganlikun 0:13413ea9a877 1258 return HAL_OK;
ganlikun 0:13413ea9a877 1259
ganlikun 0:13413ea9a877 1260 }
ganlikun 0:13413ea9a877 1261
ganlikun 0:13413ea9a877 1262 /**
ganlikun 0:13413ea9a877 1263 * @brief Return the FLASH User Option Byte value.
ganlikun 0:13413ea9a877 1264 * @retval uint8_t FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
ganlikun 0:13413ea9a877 1265 * and RST_STDBY(Bit2).
ganlikun 0:13413ea9a877 1266 */
ganlikun 0:13413ea9a877 1267 static uint8_t FLASH_OB_GetUser(void)
ganlikun 0:13413ea9a877 1268 {
ganlikun 0:13413ea9a877 1269 /* Return the User Option Byte */
ganlikun 0:13413ea9a877 1270 return ((uint8_t)(FLASH->OPTCR & 0xE0));
ganlikun 0:13413ea9a877 1271 }
ganlikun 0:13413ea9a877 1272
ganlikun 0:13413ea9a877 1273 /**
ganlikun 0:13413ea9a877 1274 * @brief Return the FLASH Write Protection Option Bytes value.
ganlikun 0:13413ea9a877 1275 * @retval uint16_t FLASH Write Protection Option Bytes value
ganlikun 0:13413ea9a877 1276 */
ganlikun 0:13413ea9a877 1277 static uint16_t FLASH_OB_GetWRP(void)
ganlikun 0:13413ea9a877 1278 {
ganlikun 0:13413ea9a877 1279 /* Return the FLASH write protection Register value */
ganlikun 0:13413ea9a877 1280 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
ganlikun 0:13413ea9a877 1281 }
ganlikun 0:13413ea9a877 1282
ganlikun 0:13413ea9a877 1283 /**
ganlikun 0:13413ea9a877 1284 * @brief Returns the FLASH Read Protection level.
ganlikun 0:13413ea9a877 1285 * @retval FLASH ReadOut Protection Status:
ganlikun 0:13413ea9a877 1286 * This parameter can be one of the following values:
ganlikun 0:13413ea9a877 1287 * @arg OB_RDP_LEVEL_0: No protection
ganlikun 0:13413ea9a877 1288 * @arg OB_RDP_LEVEL_1: Read protection of the memory
ganlikun 0:13413ea9a877 1289 * @arg OB_RDP_LEVEL_2: Full chip protection
ganlikun 0:13413ea9a877 1290 */
ganlikun 0:13413ea9a877 1291 static uint8_t FLASH_OB_GetRDP(void)
ganlikun 0:13413ea9a877 1292 {
ganlikun 0:13413ea9a877 1293 uint8_t readstatus = OB_RDP_LEVEL_0;
ganlikun 0:13413ea9a877 1294
ganlikun 0:13413ea9a877 1295 if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_2))
ganlikun 0:13413ea9a877 1296 {
ganlikun 0:13413ea9a877 1297 readstatus = OB_RDP_LEVEL_2;
ganlikun 0:13413ea9a877 1298 }
ganlikun 0:13413ea9a877 1299 else if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_1))
ganlikun 0:13413ea9a877 1300 {
ganlikun 0:13413ea9a877 1301 readstatus = OB_RDP_LEVEL_1;
ganlikun 0:13413ea9a877 1302 }
ganlikun 0:13413ea9a877 1303 else
ganlikun 0:13413ea9a877 1304 {
ganlikun 0:13413ea9a877 1305 readstatus = OB_RDP_LEVEL_0;
ganlikun 0:13413ea9a877 1306 }
ganlikun 0:13413ea9a877 1307
ganlikun 0:13413ea9a877 1308 return readstatus;
ganlikun 0:13413ea9a877 1309 }
ganlikun 0:13413ea9a877 1310
ganlikun 0:13413ea9a877 1311 /**
ganlikun 0:13413ea9a877 1312 * @brief Returns the FLASH BOR level.
ganlikun 0:13413ea9a877 1313 * @retval uint8_t The FLASH BOR level:
ganlikun 0:13413ea9a877 1314 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
ganlikun 0:13413ea9a877 1315 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
ganlikun 0:13413ea9a877 1316 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
ganlikun 0:13413ea9a877 1317 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
ganlikun 0:13413ea9a877 1318 */
ganlikun 0:13413ea9a877 1319 static uint8_t FLASH_OB_GetBOR(void)
ganlikun 0:13413ea9a877 1320 {
ganlikun 0:13413ea9a877 1321 /* Return the FLASH BOR level */
ganlikun 0:13413ea9a877 1322 return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
ganlikun 0:13413ea9a877 1323 }
ganlikun 0:13413ea9a877 1324
ganlikun 0:13413ea9a877 1325 /**
ganlikun 0:13413ea9a877 1326 * @brief Flush the instruction and data caches
ganlikun 0:13413ea9a877 1327 * @retval None
ganlikun 0:13413ea9a877 1328 */
ganlikun 0:13413ea9a877 1329 void FLASH_FlushCaches(void)
ganlikun 0:13413ea9a877 1330 {
ganlikun 0:13413ea9a877 1331 /* Flush instruction cache */
ganlikun 0:13413ea9a877 1332 if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN)!= RESET)
ganlikun 0:13413ea9a877 1333 {
ganlikun 0:13413ea9a877 1334 /* Disable instruction cache */
ganlikun 0:13413ea9a877 1335 __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
ganlikun 0:13413ea9a877 1336 /* Reset instruction cache */
ganlikun 0:13413ea9a877 1337 __HAL_FLASH_INSTRUCTION_CACHE_RESET();
ganlikun 0:13413ea9a877 1338 /* Enable instruction cache */
ganlikun 0:13413ea9a877 1339 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
ganlikun 0:13413ea9a877 1340 }
ganlikun 0:13413ea9a877 1341
ganlikun 0:13413ea9a877 1342 /* Flush data cache */
ganlikun 0:13413ea9a877 1343 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
ganlikun 0:13413ea9a877 1344 {
ganlikun 0:13413ea9a877 1345 /* Disable data cache */
ganlikun 0:13413ea9a877 1346 __HAL_FLASH_DATA_CACHE_DISABLE();
ganlikun 0:13413ea9a877 1347 /* Reset data cache */
ganlikun 0:13413ea9a877 1348 __HAL_FLASH_DATA_CACHE_RESET();
ganlikun 0:13413ea9a877 1349 /* Enable data cache */
ganlikun 0:13413ea9a877 1350 __HAL_FLASH_DATA_CACHE_ENABLE();
ganlikun 0:13413ea9a877 1351 }
ganlikun 0:13413ea9a877 1352 }
ganlikun 0:13413ea9a877 1353
ganlikun 0:13413ea9a877 1354 /**
ganlikun 0:13413ea9a877 1355 * @}
ganlikun 0:13413ea9a877 1356 */
ganlikun 0:13413ea9a877 1357
ganlikun 0:13413ea9a877 1358 #endif /* HAL_FLASH_MODULE_ENABLED */
ganlikun 0:13413ea9a877 1359
ganlikun 0:13413ea9a877 1360 /**
ganlikun 0:13413ea9a877 1361 * @}
ganlikun 0:13413ea9a877 1362 */
ganlikun 0:13413ea9a877 1363
ganlikun 0:13413ea9a877 1364 /**
ganlikun 0:13413ea9a877 1365 * @}
ganlikun 0:13413ea9a877 1366 */
ganlikun 0:13413ea9a877 1367
ganlikun 0:13413ea9a877 1368 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
ganlikun 0:13413ea9a877 1369