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