mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
235:685d5f11838f
test with CLOCK_SETUP = 0

Who changed what in which revision?

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