mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 24 14:45:08 2014 +0100
Revision:
237:f3da66175598
Synchronized with git revision 8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7

Full URL: https://github.com/mbedmicro/mbed/commit/8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7/

[NUCLEO_F334R8] Add platform files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 237:f3da66175598 1 /**
mbed_official 237:f3da66175598 2 ******************************************************************************
mbed_official 237:f3da66175598 3 * @file stm32f3xx_hal_flash_ex.c
mbed_official 237:f3da66175598 4 * @author MCD Application Team
mbed_official 237:f3da66175598 5 * @version V1.0.1
mbed_official 237:f3da66175598 6 * @date 18-June-2014
mbed_official 237:f3da66175598 7 * @brief Extended FLASH HAL module driver.
mbed_official 237:f3da66175598 8 *
mbed_official 237:f3da66175598 9 * This file provides firmware functions to manage the following
mbed_official 237:f3da66175598 10 * functionalities of the FLASH peripheral:
mbed_official 237:f3da66175598 11 * + Extended Initialization/de-initialization functions
mbed_official 237:f3da66175598 12 * + Extended I/O operation functions
mbed_official 237:f3da66175598 13 * + Extended Peripheral Control functions
mbed_official 237:f3da66175598 14 * + Extended Peripheral State functions
mbed_official 237:f3da66175598 15 *
mbed_official 237:f3da66175598 16 @verbatim
mbed_official 237:f3da66175598 17 ==============================================================================
mbed_official 237:f3da66175598 18 ##### Flash peripheral extended features #####
mbed_official 237:f3da66175598 19 ==============================================================================
mbed_official 237:f3da66175598 20
mbed_official 237:f3da66175598 21 ##### How to use this driver #####
mbed_official 237:f3da66175598 22 ==============================================================================
mbed_official 237:f3da66175598 23 [..] This driver provides functions to configure and program the FLASH memory
mbed_official 237:f3da66175598 24 of all STM32F3xxx devices. It includes
mbed_official 237:f3da66175598 25
mbed_official 237:f3da66175598 26 (++) Set/Reset the write protection
mbed_official 237:f3da66175598 27 (++) Program the user Option Bytes
mbed_official 237:f3da66175598 28 (++) Get the Read protection Level
mbed_official 237:f3da66175598 29
mbed_official 237:f3da66175598 30 @endverbatim
mbed_official 237:f3da66175598 31 ******************************************************************************
mbed_official 237:f3da66175598 32 * @attention
mbed_official 237:f3da66175598 33 *
mbed_official 237:f3da66175598 34 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 237:f3da66175598 35 *
mbed_official 237:f3da66175598 36 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 237:f3da66175598 37 * are permitted provided that the following conditions are met:
mbed_official 237:f3da66175598 38 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 237:f3da66175598 39 * this list of conditions and the following disclaimer.
mbed_official 237:f3da66175598 40 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 237:f3da66175598 41 * this list of conditions and the following disclaimer in the documentation
mbed_official 237:f3da66175598 42 * and/or other materials provided with the distribution.
mbed_official 237:f3da66175598 43 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 237:f3da66175598 44 * may be used to endorse or promote products derived from this software
mbed_official 237:f3da66175598 45 * without specific prior written permission.
mbed_official 237:f3da66175598 46 *
mbed_official 237:f3da66175598 47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 237:f3da66175598 48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 237:f3da66175598 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 237:f3da66175598 50 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 237:f3da66175598 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 237:f3da66175598 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 237:f3da66175598 53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 237:f3da66175598 54 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 237:f3da66175598 55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 237:f3da66175598 56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 237:f3da66175598 57 *
mbed_official 237:f3da66175598 58 ******************************************************************************
mbed_official 237:f3da66175598 59 */
mbed_official 237:f3da66175598 60
mbed_official 237:f3da66175598 61 /* Includes ------------------------------------------------------------------*/
mbed_official 237:f3da66175598 62 #include "stm32f3xx_hal.h"
mbed_official 237:f3da66175598 63
mbed_official 237:f3da66175598 64 /** @addtogroup STM32F3xx_HAL_Driver
mbed_official 237:f3da66175598 65 * @{
mbed_official 237:f3da66175598 66 */
mbed_official 237:f3da66175598 67
mbed_official 237:f3da66175598 68 /** @addtogroup FLASH
mbed_official 237:f3da66175598 69 * @brief FLASH HAL module driver
mbed_official 237:f3da66175598 70 * @{
mbed_official 237:f3da66175598 71 */
mbed_official 237:f3da66175598 72
mbed_official 237:f3da66175598 73 #ifdef HAL_FLASH_MODULE_ENABLED
mbed_official 237:f3da66175598 74
mbed_official 237:f3da66175598 75 /* Private typedef -----------------------------------------------------------*/
mbed_official 237:f3da66175598 76 /* Private define ------------------------------------------------------------*/
mbed_official 237:f3da66175598 77 #define HAL_FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
mbed_official 237:f3da66175598 78 /* Private macro -------------------------------------------------------------*/
mbed_official 237:f3da66175598 79 /* Private variables ---------------------------------------------------------*/
mbed_official 237:f3da66175598 80 /* Variables used for Erase pages under interruption*/
mbed_official 237:f3da66175598 81 extern FLASH_ProcessTypeDef pFlash;
mbed_official 237:f3da66175598 82 /* Private function prototypes -----------------------------------------------*/
mbed_official 237:f3da66175598 83 /* Erase operations */
mbed_official 237:f3da66175598 84 extern void FLASH_PageErase(uint32_t PageAddress);
mbed_official 237:f3da66175598 85 static void FLASH_MassErase(void);
mbed_official 237:f3da66175598 86
mbed_official 237:f3da66175598 87 /* Option bytes control */
mbed_official 237:f3da66175598 88 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage);
mbed_official 237:f3da66175598 89 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage);
mbed_official 237:f3da66175598 90 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
mbed_official 237:f3da66175598 91 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig);
mbed_official 237:f3da66175598 92 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
mbed_official 237:f3da66175598 93 static uint32_t FLASH_OB_GetWRP(void);
mbed_official 237:f3da66175598 94 static FlagStatus FLASH_OB_GetRDP(void);
mbed_official 237:f3da66175598 95 static uint8_t FLASH_OB_GetUser(void);
mbed_official 237:f3da66175598 96
mbed_official 237:f3da66175598 97 /* Private functions ---------------------------------------------------------*/
mbed_official 237:f3da66175598 98 extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
mbed_official 237:f3da66175598 99
mbed_official 237:f3da66175598 100 /** @addtogroup FLASH_Private_Functions
mbed_official 237:f3da66175598 101 * @{
mbed_official 237:f3da66175598 102 */
mbed_official 237:f3da66175598 103
mbed_official 237:f3da66175598 104 /** @addtogroup FLASH_Group1 Initialization/de-initialization functions
mbed_official 237:f3da66175598 105 * @brief Initialization and Configuration functions
mbed_official 237:f3da66175598 106 *
mbed_official 237:f3da66175598 107 @verbatim
mbed_official 237:f3da66175598 108 ===============================================================================
mbed_official 237:f3da66175598 109 ##### Initialization/de-initialization functions #####
mbed_official 237:f3da66175598 110 ===============================================================================
mbed_official 237:f3da66175598 111
mbed_official 237:f3da66175598 112 @endverbatim
mbed_official 237:f3da66175598 113 * @{
mbed_official 237:f3da66175598 114 */
mbed_official 237:f3da66175598 115
mbed_official 237:f3da66175598 116
mbed_official 237:f3da66175598 117 /**
mbed_official 237:f3da66175598 118 * @}
mbed_official 237:f3da66175598 119 */
mbed_official 237:f3da66175598 120
mbed_official 237:f3da66175598 121 /** @addtogroup FLASH_Group2 I/O operation functions
mbed_official 237:f3da66175598 122 * @brief I/O operation functions
mbed_official 237:f3da66175598 123 *
mbed_official 237:f3da66175598 124 @verbatim
mbed_official 237:f3da66175598 125 ===============================================================================
mbed_official 237:f3da66175598 126 ##### IO operation functions #####
mbed_official 237:f3da66175598 127 ===============================================================================
mbed_official 237:f3da66175598 128
mbed_official 237:f3da66175598 129 @endverbatim
mbed_official 237:f3da66175598 130 * @{
mbed_official 237:f3da66175598 131 */
mbed_official 237:f3da66175598 132 /**
mbed_official 237:f3da66175598 133 * @brief Perform a mass erase or erase the specified FLASH memory pages
mbed_official 237:f3da66175598 134 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 237:f3da66175598 135 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
mbed_official 237:f3da66175598 136 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
mbed_official 237:f3da66175598 137 * contains the configuration information for the erasing.
mbed_official 237:f3da66175598 138 *
mbed_official 237:f3da66175598 139 * @param[out] PageError: pointer to variable that
mbed_official 237:f3da66175598 140 * contains the configuration information on faulty page in case of error
mbed_official 237:f3da66175598 141 * (0xFFFFFFFF means that all the pages have been correctly erased)
mbed_official 237:f3da66175598 142 *
mbed_official 237:f3da66175598 143 * @retval HAL_StatusTypeDef HAL Status
mbed_official 237:f3da66175598 144 */
mbed_official 237:f3da66175598 145 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
mbed_official 237:f3da66175598 146 {
mbed_official 237:f3da66175598 147 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 237:f3da66175598 148 uint32_t address = 0;
mbed_official 237:f3da66175598 149
mbed_official 237:f3da66175598 150 /* Process Locked */
mbed_official 237:f3da66175598 151 __HAL_LOCK(&pFlash);
mbed_official 237:f3da66175598 152
mbed_official 237:f3da66175598 153 /* Check the parameters */
mbed_official 237:f3da66175598 154 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
mbed_official 237:f3da66175598 155
mbed_official 237:f3da66175598 156 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 157 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 158
mbed_official 237:f3da66175598 159 if (status == HAL_OK)
mbed_official 237:f3da66175598 160 {
mbed_official 237:f3da66175598 161 if (pEraseInit->TypeErase == TYPEERASE_MASSERASE)
mbed_official 237:f3da66175598 162 {
mbed_official 237:f3da66175598 163 /*Mass erase to be done*/
mbed_official 237:f3da66175598 164 FLASH_MassErase();
mbed_official 237:f3da66175598 165
mbed_official 237:f3da66175598 166 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 167 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 168
mbed_official 237:f3da66175598 169 /* Check FLASH End of Operation flag */
mbed_official 237:f3da66175598 170 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
mbed_official 237:f3da66175598 171 {
mbed_official 237:f3da66175598 172 /* Clear FLASH End of Operation pending bit */
mbed_official 237:f3da66175598 173 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
mbed_official 237:f3da66175598 174 }
mbed_official 237:f3da66175598 175
mbed_official 237:f3da66175598 176 /* If the erase operation is completed, disable the MER Bit */
mbed_official 237:f3da66175598 177 CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
mbed_official 237:f3da66175598 178 }
mbed_official 237:f3da66175598 179 else
mbed_official 237:f3da66175598 180 {
mbed_official 237:f3da66175598 181 /* Check the parameters */
mbed_official 237:f3da66175598 182 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
mbed_official 237:f3da66175598 183 assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
mbed_official 237:f3da66175598 184
mbed_official 237:f3da66175598 185 /*Initialization of PageError variable*/
mbed_official 237:f3da66175598 186 *PageError = 0xFFFFFFFF;
mbed_official 237:f3da66175598 187
mbed_official 237:f3da66175598 188 /* Erase by page by page to be done*/
mbed_official 237:f3da66175598 189 for(address = pEraseInit->PageAddress;
mbed_official 237:f3da66175598 190 address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE);
mbed_official 237:f3da66175598 191 address += FLASH_PAGE_SIZE)
mbed_official 237:f3da66175598 192 {
mbed_official 237:f3da66175598 193 FLASH_PageErase(address);
mbed_official 237:f3da66175598 194
mbed_official 237:f3da66175598 195 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 196 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 197
mbed_official 237:f3da66175598 198 /* Check FLASH End of Operation flag */
mbed_official 237:f3da66175598 199 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
mbed_official 237:f3da66175598 200 {
mbed_official 237:f3da66175598 201 /* Clear FLASH End of Operation pending bit */
mbed_official 237:f3da66175598 202 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
mbed_official 237:f3da66175598 203 }
mbed_official 237:f3da66175598 204
mbed_official 237:f3da66175598 205 /* If the erase operation is completed, disable the PER Bit */
mbed_official 237:f3da66175598 206 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
mbed_official 237:f3da66175598 207
mbed_official 237:f3da66175598 208 if (status != HAL_OK)
mbed_official 237:f3da66175598 209 {
mbed_official 237:f3da66175598 210 /* In case of error, stop erase procedure and return the faulty address */
mbed_official 237:f3da66175598 211 *PageError = address;
mbed_official 237:f3da66175598 212 break;
mbed_official 237:f3da66175598 213 }
mbed_official 237:f3da66175598 214 }
mbed_official 237:f3da66175598 215 }
mbed_official 237:f3da66175598 216 }
mbed_official 237:f3da66175598 217
mbed_official 237:f3da66175598 218 /* Process Unlocked */
mbed_official 237:f3da66175598 219 __HAL_UNLOCK(&pFlash);
mbed_official 237:f3da66175598 220
mbed_official 237:f3da66175598 221 return status;
mbed_official 237:f3da66175598 222 }
mbed_official 237:f3da66175598 223
mbed_official 237:f3da66175598 224 /**
mbed_official 237:f3da66175598 225 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
mbed_official 237:f3da66175598 226 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 237:f3da66175598 227 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
mbed_official 237:f3da66175598 228 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
mbed_official 237:f3da66175598 229 * contains the configuration information for the erasing.
mbed_official 237:f3da66175598 230 *
mbed_official 237:f3da66175598 231 * @retval HAL_StatusTypeDef HAL Status
mbed_official 237:f3da66175598 232 */
mbed_official 237:f3da66175598 233 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
mbed_official 237:f3da66175598 234 {
mbed_official 237:f3da66175598 235 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 236
mbed_official 237:f3da66175598 237 /* Process Locked */
mbed_official 237:f3da66175598 238 __HAL_LOCK(&pFlash);
mbed_official 237:f3da66175598 239
mbed_official 237:f3da66175598 240 /* Check the parameters */
mbed_official 237:f3da66175598 241 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
mbed_official 237:f3da66175598 242
mbed_official 237:f3da66175598 243 /* Enable End of FLASH Operation and Error source interrupts */
mbed_official 237:f3da66175598 244 __HAL_FLASH_ENABLE_IT((FLASH_IT_EOP | FLASH_IT_ERR));
mbed_official 237:f3da66175598 245
mbed_official 237:f3da66175598 246 if (pEraseInit->TypeErase == TYPEERASE_MASSERASE)
mbed_official 237:f3da66175598 247 {
mbed_official 237:f3da66175598 248 /*Mass erase to be done*/
mbed_official 237:f3da66175598 249 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
mbed_official 237:f3da66175598 250 FLASH_MassErase();
mbed_official 237:f3da66175598 251 }
mbed_official 237:f3da66175598 252 else
mbed_official 237:f3da66175598 253 {
mbed_official 237:f3da66175598 254 /* Erase by page to be done*/
mbed_official 237:f3da66175598 255
mbed_official 237:f3da66175598 256 /* Check the parameters */
mbed_official 237:f3da66175598 257 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
mbed_official 237:f3da66175598 258 assert_param(IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages));
mbed_official 237:f3da66175598 259
mbed_official 237:f3da66175598 260 pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
mbed_official 237:f3da66175598 261 pFlash.DataRemaining = pEraseInit->NbPages;
mbed_official 237:f3da66175598 262 pFlash.Address = pEraseInit->PageAddress;
mbed_official 237:f3da66175598 263
mbed_official 237:f3da66175598 264 /*Erase 1st page and wait for IT*/
mbed_official 237:f3da66175598 265 FLASH_PageErase(pEraseInit->PageAddress);
mbed_official 237:f3da66175598 266 }
mbed_official 237:f3da66175598 267
mbed_official 237:f3da66175598 268 return status;
mbed_official 237:f3da66175598 269 }
mbed_official 237:f3da66175598 270
mbed_official 237:f3da66175598 271 /**
mbed_official 237:f3da66175598 272 * @}
mbed_official 237:f3da66175598 273 */
mbed_official 237:f3da66175598 274
mbed_official 237:f3da66175598 275 /** @addtogroup FLASH_Group3 Peripheral Control functions
mbed_official 237:f3da66175598 276 * @brief Peripheral Control functions
mbed_official 237:f3da66175598 277 *
mbed_official 237:f3da66175598 278 @verbatim
mbed_official 237:f3da66175598 279 ===============================================================================
mbed_official 237:f3da66175598 280 ##### Peripheral Control functions #####
mbed_official 237:f3da66175598 281 ===============================================================================
mbed_official 237:f3da66175598 282 [..]
mbed_official 237:f3da66175598 283 This subsection provides a set of functions allowing to control the FLASH
mbed_official 237:f3da66175598 284 memory operations.
mbed_official 237:f3da66175598 285
mbed_official 237:f3da66175598 286 @endverbatim
mbed_official 237:f3da66175598 287 * @{
mbed_official 237:f3da66175598 288 */
mbed_official 237:f3da66175598 289 /**
mbed_official 237:f3da66175598 290 * @brief Erases the FLASH option bytes.
mbed_official 237:f3da66175598 291 * @note This functions erases all option bytes except the Read protection (RDP).
mbed_official 237:f3da66175598 292 * The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 237:f3da66175598 293 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
mbed_official 237:f3da66175598 294 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
mbed_official 237:f3da66175598 295 * (system reset will occur)
mbed_official 237:f3da66175598 296 * @param None
mbed_official 237:f3da66175598 297 * @retval HAL status
mbed_official 237:f3da66175598 298 */
mbed_official 237:f3da66175598 299
mbed_official 237:f3da66175598 300 HAL_StatusTypeDef HAL_FLASHEx_OBErase(void)
mbed_official 237:f3da66175598 301 {
mbed_official 237:f3da66175598 302 uint8_t rdptmp = OB_RDP_LEVEL_0;
mbed_official 237:f3da66175598 303 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 237:f3da66175598 304 FLASH_OBProgramInitTypeDef optionsbytes;
mbed_official 237:f3da66175598 305
mbed_official 237:f3da66175598 306 /* Get the actual read protection Option Byte value */
mbed_official 237:f3da66175598 307 HAL_FLASHEx_OBGetConfig(&optionsbytes);
mbed_official 237:f3da66175598 308 if(optionsbytes.RDPLevel != RESET)
mbed_official 237:f3da66175598 309 {
mbed_official 237:f3da66175598 310 rdptmp = OB_RDP_LEVEL_1;
mbed_official 237:f3da66175598 311 }
mbed_official 237:f3da66175598 312
mbed_official 237:f3da66175598 313 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 314 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 315
mbed_official 237:f3da66175598 316 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 317 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 318
mbed_official 237:f3da66175598 319 if(status == HAL_OK)
mbed_official 237:f3da66175598 320 {
mbed_official 237:f3da66175598 321 /* If the previous operation is completed, proceed to erase the option bytes */
mbed_official 237:f3da66175598 322 SET_BIT(FLASH->CR, FLASH_CR_OPTER);
mbed_official 237:f3da66175598 323 SET_BIT(FLASH->CR, FLASH_CR_STRT);
mbed_official 237:f3da66175598 324
mbed_official 237:f3da66175598 325 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 326 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 327
mbed_official 237:f3da66175598 328 /* If the erase operation is completed, disable the OPTER Bit */
mbed_official 237:f3da66175598 329 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER);
mbed_official 237:f3da66175598 330
mbed_official 237:f3da66175598 331 if(status == HAL_OK)
mbed_official 237:f3da66175598 332 {
mbed_official 237:f3da66175598 333 /* Restore the last read protection Option Byte value */
mbed_official 237:f3da66175598 334 optionsbytes.OptionType = OPTIONBYTE_RDP;
mbed_official 237:f3da66175598 335 optionsbytes.RDPLevel = rdptmp;
mbed_official 237:f3da66175598 336 status = HAL_FLASHEx_OBProgram(&optionsbytes);
mbed_official 237:f3da66175598 337 }
mbed_official 237:f3da66175598 338 }
mbed_official 237:f3da66175598 339
mbed_official 237:f3da66175598 340 /* Return the erase status */
mbed_official 237:f3da66175598 341 return status;
mbed_official 237:f3da66175598 342 }
mbed_official 237:f3da66175598 343
mbed_official 237:f3da66175598 344 /**
mbed_official 237:f3da66175598 345 * @brief Program option bytes
mbed_official 237:f3da66175598 346 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 237:f3da66175598 347 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
mbed_official 237:f3da66175598 348 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
mbed_official 237:f3da66175598 349 * (system reset will occur)
mbed_official 237:f3da66175598 350 *
mbed_official 237:f3da66175598 351 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
mbed_official 237:f3da66175598 352 * contains the configuration information for the programming.
mbed_official 237:f3da66175598 353 *
mbed_official 237:f3da66175598 354 * @retval HAL_StatusTypeDef HAL Status
mbed_official 237:f3da66175598 355 */
mbed_official 237:f3da66175598 356 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
mbed_official 237:f3da66175598 357 {
mbed_official 237:f3da66175598 358 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 237:f3da66175598 359
mbed_official 237:f3da66175598 360 /* Check the parameters */
mbed_official 237:f3da66175598 361 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
mbed_official 237:f3da66175598 362
mbed_official 237:f3da66175598 363 /* Write protection configuration */
mbed_official 237:f3da66175598 364 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
mbed_official 237:f3da66175598 365 {
mbed_official 237:f3da66175598 366 assert_param(IS_WRPSTATE(pOBInit->WRPState));
mbed_official 237:f3da66175598 367 if (pOBInit->WRPState == WRPSTATE_ENABLE)
mbed_official 237:f3da66175598 368 {
mbed_official 237:f3da66175598 369 /* Enable of Write protection on the selected page */
mbed_official 237:f3da66175598 370 status = FLASH_OB_EnableWRP(pOBInit->WRPPage);
mbed_official 237:f3da66175598 371 }
mbed_official 237:f3da66175598 372 else
mbed_official 237:f3da66175598 373 {
mbed_official 237:f3da66175598 374 /* Disable of Write protection on the selected page */
mbed_official 237:f3da66175598 375 status = FLASH_OB_DisableWRP(pOBInit->WRPPage);
mbed_official 237:f3da66175598 376 }
mbed_official 237:f3da66175598 377 }
mbed_official 237:f3da66175598 378
mbed_official 237:f3da66175598 379 /* Read protection configuration */
mbed_official 237:f3da66175598 380 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
mbed_official 237:f3da66175598 381 {
mbed_official 237:f3da66175598 382 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
mbed_official 237:f3da66175598 383 }
mbed_official 237:f3da66175598 384
mbed_official 237:f3da66175598 385 /* USER configuration */
mbed_official 237:f3da66175598 386 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
mbed_official 237:f3da66175598 387 {
mbed_official 237:f3da66175598 388 status = FLASH_OB_UserConfig(pOBInit->USERConfig);
mbed_official 237:f3da66175598 389 }
mbed_official 237:f3da66175598 390
mbed_official 237:f3da66175598 391 /* DATA configuration*/
mbed_official 237:f3da66175598 392 if((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA)
mbed_official 237:f3da66175598 393 {
mbed_official 237:f3da66175598 394 status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData);
mbed_official 237:f3da66175598 395 }
mbed_official 237:f3da66175598 396
mbed_official 237:f3da66175598 397 return status;
mbed_official 237:f3da66175598 398 }
mbed_official 237:f3da66175598 399
mbed_official 237:f3da66175598 400 /**
mbed_official 237:f3da66175598 401 * @brief Get the Option byte configuration
mbed_official 237:f3da66175598 402 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
mbed_official 237:f3da66175598 403 * contains the configuration information for the programming.
mbed_official 237:f3da66175598 404 *
mbed_official 237:f3da66175598 405 * @retval None
mbed_official 237:f3da66175598 406 */
mbed_official 237:f3da66175598 407 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
mbed_official 237:f3da66175598 408 {
mbed_official 237:f3da66175598 409 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;
mbed_official 237:f3da66175598 410
mbed_official 237:f3da66175598 411 /*Get WRP*/
mbed_official 237:f3da66175598 412 pOBInit->WRPPage = FLASH_OB_GetWRP();
mbed_official 237:f3da66175598 413
mbed_official 237:f3da66175598 414 /*Get RDP Level*/
mbed_official 237:f3da66175598 415 pOBInit->RDPLevel = FLASH_OB_GetRDP();
mbed_official 237:f3da66175598 416
mbed_official 237:f3da66175598 417 /*Get USER*/
mbed_official 237:f3da66175598 418 pOBInit->USERConfig = FLASH_OB_GetUser();
mbed_official 237:f3da66175598 419 }
mbed_official 237:f3da66175598 420
mbed_official 237:f3da66175598 421 /**
mbed_official 237:f3da66175598 422 * @}
mbed_official 237:f3da66175598 423 */
mbed_official 237:f3da66175598 424
mbed_official 237:f3da66175598 425 /**
mbed_official 237:f3da66175598 426 * @brief Mass erase of FLASH memory
mbed_official 237:f3da66175598 427 * @param None
mbed_official 237:f3da66175598 428 *
mbed_official 237:f3da66175598 429 * @retval None
mbed_official 237:f3da66175598 430 */
mbed_official 237:f3da66175598 431 static void FLASH_MassErase(void)
mbed_official 237:f3da66175598 432 {
mbed_official 237:f3da66175598 433 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 434 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 435
mbed_official 237:f3da66175598 436 /* Proceed to erase all sectors */
mbed_official 237:f3da66175598 437 SET_BIT(FLASH->CR, FLASH_CR_MER);
mbed_official 237:f3da66175598 438 SET_BIT(FLASH->CR, FLASH_CR_STRT);
mbed_official 237:f3da66175598 439 }
mbed_official 237:f3da66175598 440
mbed_official 237:f3da66175598 441 /**
mbed_official 237:f3da66175598 442 * @brief Enable the write protection of the desired pages
mbed_official 237:f3da66175598 443 * @note When the memory read protection level is selected (RDP level = 1),
mbed_official 237:f3da66175598 444 * it is not possible to program or erase the flash page i if CortexM4
mbed_official 237:f3da66175598 445 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
mbed_official 237:f3da66175598 446 *
mbed_official 237:f3da66175598 447 * @param WriteProtectPage: specifies the page(s) to be write protected.
mbed_official 237:f3da66175598 448 * The value of this parameter depend on device used within the same series
mbed_official 237:f3da66175598 449 * @retval HAL status
mbed_official 237:f3da66175598 450 */
mbed_official 237:f3da66175598 451 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage)
mbed_official 237:f3da66175598 452 {
mbed_official 237:f3da66175598 453 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 454 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
mbed_official 237:f3da66175598 455 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 456 uint16_t WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
mbed_official 237:f3da66175598 457 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 458
mbed_official 237:f3da66175598 459 /* Check the parameters */
mbed_official 237:f3da66175598 460 assert_param(IS_OB_WRP(WriteProtectPage));
mbed_official 237:f3da66175598 461
mbed_official 237:f3da66175598 462 WriteProtectPage = (uint32_t)(~WriteProtectPage);
mbed_official 237:f3da66175598 463 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
mbed_official 237:f3da66175598 464 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
mbed_official 237:f3da66175598 465 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 466 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
mbed_official 237:f3da66175598 467 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24);
mbed_official 237:f3da66175598 468 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 469
mbed_official 237:f3da66175598 470 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 471 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 472
mbed_official 237:f3da66175598 473 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 474 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 475
mbed_official 237:f3da66175598 476 if(status == HAL_OK)
mbed_official 237:f3da66175598 477 {
mbed_official 237:f3da66175598 478 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 479
mbed_official 237:f3da66175598 480 if(WRP0_Data != 0xFF)
mbed_official 237:f3da66175598 481 {
mbed_official 237:f3da66175598 482 OB->WRP0 &= WRP0_Data;
mbed_official 237:f3da66175598 483
mbed_official 237:f3da66175598 484 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 485 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 486 }
mbed_official 237:f3da66175598 487
mbed_official 237:f3da66175598 488 if((status == HAL_OK) && (WRP1_Data != 0xFF))
mbed_official 237:f3da66175598 489 {
mbed_official 237:f3da66175598 490 OB->WRP1 &= WRP1_Data;
mbed_official 237:f3da66175598 491
mbed_official 237:f3da66175598 492 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 493 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 494 }
mbed_official 237:f3da66175598 495
mbed_official 237:f3da66175598 496 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 497 if((status == HAL_OK) && (WRP2_Data != 0xFF))
mbed_official 237:f3da66175598 498 {
mbed_official 237:f3da66175598 499 OB->WRP2 &= WRP2_Data;
mbed_official 237:f3da66175598 500
mbed_official 237:f3da66175598 501 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 502 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 503 }
mbed_official 237:f3da66175598 504
mbed_official 237:f3da66175598 505 if((status == HAL_OK) && (WRP3_Data != 0xFF))
mbed_official 237:f3da66175598 506 {
mbed_official 237:f3da66175598 507 OB->WRP3 &= WRP3_Data;
mbed_official 237:f3da66175598 508
mbed_official 237:f3da66175598 509 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 510 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 511 }
mbed_official 237:f3da66175598 512 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 513
mbed_official 237:f3da66175598 514 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 237:f3da66175598 515 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 516 }
mbed_official 237:f3da66175598 517
mbed_official 237:f3da66175598 518 return status;
mbed_official 237:f3da66175598 519
mbed_official 237:f3da66175598 520 }
mbed_official 237:f3da66175598 521
mbed_official 237:f3da66175598 522 /**
mbed_official 237:f3da66175598 523 * @brief Disable the write protection of the desired pages
mbed_official 237:f3da66175598 524 * @note When the memory read protection level is selected (RDP level = 1),
mbed_official 237:f3da66175598 525 * it is not possible to program or erase the flash page i if CortexM4
mbed_official 237:f3da66175598 526 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
mbed_official 237:f3da66175598 527 *
mbed_official 237:f3da66175598 528 * @param WriteProtectPage: specifies the page(s) to be write unprotected.
mbed_official 237:f3da66175598 529 * The value of this parameter depend on device used within the same series
mbed_official 237:f3da66175598 530 * @retval HAL status
mbed_official 237:f3da66175598 531 */
mbed_official 237:f3da66175598 532 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
mbed_official 237:f3da66175598 533 {
mbed_official 237:f3da66175598 534 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 535 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
mbed_official 237:f3da66175598 536 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 537 uint16_t WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
mbed_official 237:f3da66175598 538 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 539
mbed_official 237:f3da66175598 540 /* Check the parameters */
mbed_official 237:f3da66175598 541 assert_param(IS_OB_WRP(WriteProtectPage));
mbed_official 237:f3da66175598 542
mbed_official 237:f3da66175598 543 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
mbed_official 237:f3da66175598 544 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
mbed_official 237:f3da66175598 545 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 546 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
mbed_official 237:f3da66175598 547 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24);
mbed_official 237:f3da66175598 548 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 549
mbed_official 237:f3da66175598 550 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 551 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 552
mbed_official 237:f3da66175598 553 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 554 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 555
mbed_official 237:f3da66175598 556 if(status == HAL_OK)
mbed_official 237:f3da66175598 557 {
mbed_official 237:f3da66175598 558 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 559
mbed_official 237:f3da66175598 560 if(WRP0_Data != 0xFF)
mbed_official 237:f3da66175598 561 {
mbed_official 237:f3da66175598 562 OB->WRP0 |= WRP0_Data;
mbed_official 237:f3da66175598 563
mbed_official 237:f3da66175598 564 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 565 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 566 }
mbed_official 237:f3da66175598 567
mbed_official 237:f3da66175598 568 if((status == HAL_OK) && (WRP1_Data != 0xFF))
mbed_official 237:f3da66175598 569 {
mbed_official 237:f3da66175598 570 OB->WRP1 |= WRP1_Data;
mbed_official 237:f3da66175598 571
mbed_official 237:f3da66175598 572 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 573 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 574 }
mbed_official 237:f3da66175598 575
mbed_official 237:f3da66175598 576 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 577 if((status == HAL_OK) && (WRP2_Data != 0xFF))
mbed_official 237:f3da66175598 578 {
mbed_official 237:f3da66175598 579 OB->WRP2 |= WRP2_Data;
mbed_official 237:f3da66175598 580
mbed_official 237:f3da66175598 581 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 582 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 583 }
mbed_official 237:f3da66175598 584
mbed_official 237:f3da66175598 585 if((status == HAL_OK) && (WRP3_Data != 0xFF))
mbed_official 237:f3da66175598 586 {
mbed_official 237:f3da66175598 587 OB->WRP3 |= WRP3_Data;
mbed_official 237:f3da66175598 588
mbed_official 237:f3da66175598 589 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 590 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 591 }
mbed_official 237:f3da66175598 592 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 593
mbed_official 237:f3da66175598 594 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 237:f3da66175598 595 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 596 }
mbed_official 237:f3da66175598 597
mbed_official 237:f3da66175598 598 return status;
mbed_official 237:f3da66175598 599 }
mbed_official 237:f3da66175598 600
mbed_official 237:f3da66175598 601 /**
mbed_official 237:f3da66175598 602 * @brief Set the read protection level.
mbed_official 237:f3da66175598 603 * @param ReadProtectLevel: specifies the read protection level.
mbed_official 237:f3da66175598 604 * This parameter can be one of the following values:
mbed_official 237:f3da66175598 605 * @arg OB_RDP_LEVEL_0: No protection
mbed_official 237:f3da66175598 606 * @arg OB_RDP_LEVEL_1: Read protection of the memory
mbed_official 237:f3da66175598 607 * @arg OB_RDP_LEVEL_2: Full chip protection
mbed_official 237:f3da66175598 608 *
mbed_official 237:f3da66175598 609 * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
mbed_official 237:f3da66175598 610 *
mbed_official 237:f3da66175598 611 * @retval HAL status
mbed_official 237:f3da66175598 612 */
mbed_official 237:f3da66175598 613 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel)
mbed_official 237:f3da66175598 614 {
mbed_official 237:f3da66175598 615 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 616
mbed_official 237:f3da66175598 617 /* Check the parameters */
mbed_official 237:f3da66175598 618 assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel));
mbed_official 237:f3da66175598 619
mbed_official 237:f3da66175598 620 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 621 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 622
mbed_official 237:f3da66175598 623 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 624 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 625
mbed_official 237:f3da66175598 626 if(status == HAL_OK)
mbed_official 237:f3da66175598 627 {
mbed_official 237:f3da66175598 628 /* Enable the Option Bytes Programming operation */
mbed_official 237:f3da66175598 629 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 630
mbed_official 237:f3da66175598 631 WRITE_REG(OB->RDP, ReadProtectLevel);
mbed_official 237:f3da66175598 632
mbed_official 237:f3da66175598 633 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 634 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 635
mbed_official 237:f3da66175598 636 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 237:f3da66175598 637 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 638 }
mbed_official 237:f3da66175598 639
mbed_official 237:f3da66175598 640 return status;
mbed_official 237:f3da66175598 641 }
mbed_official 237:f3da66175598 642
mbed_official 237:f3da66175598 643 /**
mbed_official 237:f3da66175598 644 * @brief Program the FLASH User Option Byte.
mbed_official 237:f3da66175598 645 * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
mbed_official 237:f3da66175598 646 * @param UserConfig: The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
mbed_official 237:f3da66175598 647 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
mbed_official 237:f3da66175598 648 * And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 .
mbed_official 237:f3da66175598 649 * @retval HAL status
mbed_official 237:f3da66175598 650 */
mbed_official 237:f3da66175598 651 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig)
mbed_official 237:f3da66175598 652 {
mbed_official 237:f3da66175598 653 HAL_StatusTypeDef status = HAL_OK;
mbed_official 237:f3da66175598 654
mbed_official 237:f3da66175598 655 /* Check the parameters */
mbed_official 237:f3da66175598 656 assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW)));
mbed_official 237:f3da66175598 657 assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST)));
mbed_official 237:f3da66175598 658 assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST)));
mbed_official 237:f3da66175598 659 assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET)));
mbed_official 237:f3da66175598 660 assert_param(IS_OB_VDDA_ANALOG((UserConfig&OB_VDDA_ANALOG_ON)));
mbed_official 237:f3da66175598 661 assert_param(IS_OB_SRAM_PARITY((UserConfig&OB_SRAM_PARITY_RESET)));
mbed_official 237:f3da66175598 662 #if defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 663 assert_param(IS_OB_SDACD_VDD_MONITOR((UserConfig&OB_SDACD_VDD_MONITOR_SET)));
mbed_official 237:f3da66175598 664 #endif /* STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 665
mbed_official 237:f3da66175598 666 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 667 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 668
mbed_official 237:f3da66175598 669 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 670 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 671
mbed_official 237:f3da66175598 672 if(status == HAL_OK)
mbed_official 237:f3da66175598 673 {
mbed_official 237:f3da66175598 674 /* Enable the Option Bytes Programming operation */
mbed_official 237:f3da66175598 675 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 676
mbed_official 237:f3da66175598 677 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F302xC) || defined(STM32F303x8) || defined(STM32F303xC) || defined(STM32F318xx) || defined(STM32F334x8) || defined(STM32F358xx)
mbed_official 237:f3da66175598 678 OB->USER = (UserConfig | 0x88);
mbed_official 237:f3da66175598 679 #endif /* STM32F301x8 || STM32F302x8 || STM32F302xC || STM32F303xC || STM32F318xx || STM32F334x8 || STM32F358xx */
mbed_official 237:f3da66175598 680 #if defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 681 OB->USER = (UserConfig | 0x08);
mbed_official 237:f3da66175598 682 #endif /* STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 683
mbed_official 237:f3da66175598 684 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 685 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 686
mbed_official 237:f3da66175598 687 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 237:f3da66175598 688 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 689 }
mbed_official 237:f3da66175598 690
mbed_official 237:f3da66175598 691 return status;
mbed_official 237:f3da66175598 692 }
mbed_official 237:f3da66175598 693
mbed_official 237:f3da66175598 694 /**
mbed_official 237:f3da66175598 695 * @brief Programs a half word at a specified Option Byte Data address.
mbed_official 237:f3da66175598 696 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 237:f3da66175598 697 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
mbed_official 237:f3da66175598 698 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
mbed_official 237:f3da66175598 699 * (system reset will occur)
mbed_official 237:f3da66175598 700 * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
mbed_official 237:f3da66175598 701 * @param Address: specifies the address to be programmed.
mbed_official 237:f3da66175598 702 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
mbed_official 237:f3da66175598 703 * @param Data: specifies the data to be programmed.
mbed_official 237:f3da66175598 704 * @retval HAL status
mbed_official 237:f3da66175598 705 */
mbed_official 237:f3da66175598 706 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
mbed_official 237:f3da66175598 707 {
mbed_official 237:f3da66175598 708 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 237:f3da66175598 709
mbed_official 237:f3da66175598 710 /* Check the parameters */
mbed_official 237:f3da66175598 711 assert_param(IS_OB_DATA_ADDRESS(Address));
mbed_official 237:f3da66175598 712
mbed_official 237:f3da66175598 713 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 714 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 715
mbed_official 237:f3da66175598 716 /* Clear pending flags (if any) */
mbed_official 237:f3da66175598 717 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 237:f3da66175598 718
mbed_official 237:f3da66175598 719 if(status == HAL_OK)
mbed_official 237:f3da66175598 720 {
mbed_official 237:f3da66175598 721 /* Enables the Option Bytes Programming operation */
mbed_official 237:f3da66175598 722 SET_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 723 *(__IO uint16_t*)Address = Data;
mbed_official 237:f3da66175598 724
mbed_official 237:f3da66175598 725 /* Wait for last operation to be completed */
mbed_official 237:f3da66175598 726 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
mbed_official 237:f3da66175598 727
mbed_official 237:f3da66175598 728 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 237:f3da66175598 729 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
mbed_official 237:f3da66175598 730 }
mbed_official 237:f3da66175598 731 /* Return the Option Byte Data Program Status */
mbed_official 237:f3da66175598 732 return status;
mbed_official 237:f3da66175598 733 }
mbed_official 237:f3da66175598 734
mbed_official 237:f3da66175598 735 /**
mbed_official 237:f3da66175598 736 * @brief Return the FLASH Write Protection Option Bytes value.
mbed_official 237:f3da66175598 737 * @param None
mbed_official 237:f3da66175598 738 * @retval The FLASH Write Protection Option Bytes value
mbed_official 237:f3da66175598 739 */
mbed_official 237:f3da66175598 740 static uint32_t FLASH_OB_GetWRP(void)
mbed_official 237:f3da66175598 741 {
mbed_official 237:f3da66175598 742 /* Return the FLASH write protection Register value */
mbed_official 237:f3da66175598 743 return (uint32_t)(READ_REG(FLASH->WRPR));
mbed_official 237:f3da66175598 744 }
mbed_official 237:f3da66175598 745
mbed_official 237:f3da66175598 746 /**
mbed_official 237:f3da66175598 747 * @brief Returns the FLASH Read Protection level.
mbed_official 237:f3da66175598 748 * @param None
mbed_official 237:f3da66175598 749 * @retval FLASH ReadOut Protection Status:
mbed_official 237:f3da66175598 750 * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
mbed_official 237:f3da66175598 751 * - RESET, when OB_RDP_Level_0 is set
mbed_official 237:f3da66175598 752 */
mbed_official 237:f3da66175598 753 static FlagStatus FLASH_OB_GetRDP(void)
mbed_official 237:f3da66175598 754 {
mbed_official 237:f3da66175598 755 FlagStatus readstatus = RESET;
mbed_official 237:f3da66175598 756
mbed_official 237:f3da66175598 757 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
mbed_official 237:f3da66175598 758 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
mbed_official 237:f3da66175598 759 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
mbed_official 237:f3da66175598 760 if ((uint8_t)READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT) != RESET)
mbed_official 237:f3da66175598 761 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
mbed_official 237:f3da66175598 762 /* STM32F302xC || STM32F303xC || STM32F358xx || */
mbed_official 237:f3da66175598 763 /* STM32F303x8 || STM32F334x8 || STM32F328xx */
mbed_official 237:f3da66175598 764 #if defined(STM32F373xC) || defined(STM32F378xx)
mbed_official 237:f3da66175598 765 if ((uint8_t)READ_BIT(FLASH->OBR, (FLASH_OBR_LEVEL1_PROT | FLASH_OBR_LEVEL2_PROT)) != RESET)
mbed_official 237:f3da66175598 766 #endif /* STM32F373xC || STM32F378xx */
mbed_official 237:f3da66175598 767 {
mbed_official 237:f3da66175598 768 readstatus = SET;
mbed_official 237:f3da66175598 769 }
mbed_official 237:f3da66175598 770 else
mbed_official 237:f3da66175598 771 {
mbed_official 237:f3da66175598 772 readstatus = RESET;
mbed_official 237:f3da66175598 773 }
mbed_official 237:f3da66175598 774 return readstatus;
mbed_official 237:f3da66175598 775 }
mbed_official 237:f3da66175598 776
mbed_official 237:f3da66175598 777 /**
mbed_official 237:f3da66175598 778 * @brief Return the FLASH User Option Byte value.
mbed_official 237:f3da66175598 779 * @param None
mbed_official 237:f3da66175598 780 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
mbed_official 237:f3da66175598 781 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
mbed_official 237:f3da66175598 782 * And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 .
mbed_official 237:f3da66175598 783 */
mbed_official 237:f3da66175598 784 static uint8_t FLASH_OB_GetUser(void)
mbed_official 237:f3da66175598 785 {
mbed_official 237:f3da66175598 786 /* Return the User Option Byte */
mbed_official 237:f3da66175598 787 return (uint8_t)(READ_REG(FLASH->OBR) >> 8);
mbed_official 237:f3da66175598 788 }
mbed_official 237:f3da66175598 789
mbed_official 237:f3da66175598 790 /**
mbed_official 237:f3da66175598 791 * @}
mbed_official 237:f3da66175598 792 */
mbed_official 237:f3da66175598 793
mbed_official 237:f3da66175598 794 #endif /* HAL_FLASH_MODULE_ENABLED */
mbed_official 237:f3da66175598 795
mbed_official 237:f3da66175598 796
mbed_official 237:f3da66175598 797 /**
mbed_official 237:f3da66175598 798 * @}
mbed_official 237:f3da66175598 799 */
mbed_official 237:f3da66175598 800
mbed_official 237:f3da66175598 801 /**
mbed_official 237:f3da66175598 802 * @}
mbed_official 237:f3da66175598 803 */
mbed_official 237:f3da66175598 804
mbed_official 237:f3da66175598 805 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/