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