Hal Drivers for L4

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

Committer:
EricLew
Date:
Wed Nov 25 17:30:43 2015 +0000
Revision:
2:7aef7655b0a8
Parent:
1:271ce2251692
commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EricLew 0:80ee8f3b695e 1 /**
EricLew 0:80ee8f3b695e 2 ******************************************************************************
EricLew 0:80ee8f3b695e 3 * @file stm32l4xx_hal_flash_ex.c
EricLew 0:80ee8f3b695e 4 * @author MCD Application Team
EricLew 0:80ee8f3b695e 5 * @version V1.1.0
EricLew 0:80ee8f3b695e 6 * @date 16-September-2015
EricLew 0:80ee8f3b695e 7 * @brief Extended FLASH HAL module driver.
EricLew 0:80ee8f3b695e 8 * This file provides firmware functions to manage the following
EricLew 0:80ee8f3b695e 9 * functionalities of the FLASH extended peripheral:
EricLew 0:80ee8f3b695e 10 * + Extended programming operations functions
EricLew 0:80ee8f3b695e 11 *
EricLew 0:80ee8f3b695e 12 @verbatim
EricLew 0:80ee8f3b695e 13 ==============================================================================
EricLew 0:80ee8f3b695e 14 ##### Flash Extended features #####
EricLew 0:80ee8f3b695e 15 ==============================================================================
EricLew 0:80ee8f3b695e 16
EricLew 0:80ee8f3b695e 17 [..] Comparing to other previous devices, the FLASH interface for STM32L4xx
EricLew 0:80ee8f3b695e 18 devices contains the following additional features
EricLew 0:80ee8f3b695e 19
EricLew 0:80ee8f3b695e 20 (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
EricLew 0:80ee8f3b695e 21 capability (RWW)
EricLew 0:80ee8f3b695e 22 (+) Dual bank memory organization
EricLew 0:80ee8f3b695e 23 (+) PCROP protection for all banks
EricLew 0:80ee8f3b695e 24
EricLew 0:80ee8f3b695e 25 ##### How to use this driver #####
EricLew 0:80ee8f3b695e 26 ==============================================================================
EricLew 0:80ee8f3b695e 27 [..] This driver provides functions to configure and program the FLASH memory
EricLew 0:80ee8f3b695e 28 of all STM32L4xx devices. It includes
EricLew 0:80ee8f3b695e 29 (#) Flash Memory Erase functions:
EricLew 0:80ee8f3b695e 30 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
EricLew 0:80ee8f3b695e 31 HAL_FLASH_Lock() functions
EricLew 0:80ee8f3b695e 32 (++) Erase function: Erase page, erase all sectors
EricLew 0:80ee8f3b695e 33 (++) There are two modes of erase :
EricLew 0:80ee8f3b695e 34 (+++) Polling Mode using HAL_FLASHEx_Erase()
EricLew 0:80ee8f3b695e 35 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
EricLew 0:80ee8f3b695e 36
EricLew 0:80ee8f3b695e 37 (#) Option Bytes Programming function: Use HAL_FLASHEx_OBProgram() to :
EricLew 0:80ee8f3b695e 38 (++) Set/Reset the write protection
EricLew 0:80ee8f3b695e 39 (++) Set the Read protection Level
EricLew 0:80ee8f3b695e 40 (++) Program the user Option Bytes
EricLew 0:80ee8f3b695e 41 (++) Configure the PCROP protection
EricLew 0:80ee8f3b695e 42
EricLew 0:80ee8f3b695e 43 (#) Get Option Bytes Configuration function: Use HAL_FLASHEx_OBGetConfig() to :
EricLew 0:80ee8f3b695e 44 (++) Get the value of a write protection area
EricLew 0:80ee8f3b695e 45 (++) Know if the read protection is activated
EricLew 0:80ee8f3b695e 46 (++) Get the value of the user Option Bytes
EricLew 0:80ee8f3b695e 47 (++) Get the value of a PCROP area
EricLew 0:80ee8f3b695e 48
EricLew 0:80ee8f3b695e 49 @endverbatim
EricLew 0:80ee8f3b695e 50 ******************************************************************************
EricLew 0:80ee8f3b695e 51 * @attention
EricLew 0:80ee8f3b695e 52 *
EricLew 0:80ee8f3b695e 53 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
EricLew 0:80ee8f3b695e 54 *
EricLew 0:80ee8f3b695e 55 * Redistribution and use in source and binary forms, with or without modification,
EricLew 0:80ee8f3b695e 56 * are permitted provided that the following conditions are met:
EricLew 0:80ee8f3b695e 57 * 1. Redistributions of source code must retain the above copyright notice,
EricLew 0:80ee8f3b695e 58 * this list of conditions and the following disclaimer.
EricLew 0:80ee8f3b695e 59 * 2. Redistributions in binary form must reproduce the above copyright notice,
EricLew 0:80ee8f3b695e 60 * this list of conditions and the following disclaimer in the documentation
EricLew 0:80ee8f3b695e 61 * and/or other materials provided with the distribution.
EricLew 0:80ee8f3b695e 62 * 3. Neither the name of STMicroelectronics nor the names of its contributors
EricLew 0:80ee8f3b695e 63 * may be used to endorse or promote products derived from this software
EricLew 0:80ee8f3b695e 64 * without specific prior written permission.
EricLew 0:80ee8f3b695e 65 *
EricLew 0:80ee8f3b695e 66 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
EricLew 0:80ee8f3b695e 67 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
EricLew 0:80ee8f3b695e 68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
EricLew 0:80ee8f3b695e 69 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
EricLew 0:80ee8f3b695e 70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
EricLew 0:80ee8f3b695e 71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
EricLew 0:80ee8f3b695e 72 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
EricLew 0:80ee8f3b695e 73 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
EricLew 0:80ee8f3b695e 74 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
EricLew 0:80ee8f3b695e 75 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
EricLew 0:80ee8f3b695e 76 *
EricLew 0:80ee8f3b695e 77 ******************************************************************************
EricLew 0:80ee8f3b695e 78 */
EricLew 0:80ee8f3b695e 79
EricLew 0:80ee8f3b695e 80 /* Includes ------------------------------------------------------------------*/
EricLew 0:80ee8f3b695e 81 #include "stm32l4xx_hal.h"
EricLew 1:271ce2251692 82 #include "stm32l476xx.h"
EricLew 0:80ee8f3b695e 83
EricLew 0:80ee8f3b695e 84 /** @addtogroup STM32L4xx_HAL_Driver
EricLew 0:80ee8f3b695e 85 * @{
EricLew 0:80ee8f3b695e 86 */
EricLew 1:271ce2251692 87 #define FLASH_OPTR_nRST_SHDW ((uint32_t)0x00004000)
EricLew 0:80ee8f3b695e 88 /** @defgroup FLASHEx FLASHEx
EricLew 0:80ee8f3b695e 89 * @brief FALSH Extended HAL module driver
EricLew 0:80ee8f3b695e 90 * @{
EricLew 0:80ee8f3b695e 91 */
EricLew 0:80ee8f3b695e 92
EricLew 0:80ee8f3b695e 93 #ifdef HAL_FLASH_MODULE_ENABLED
EricLew 0:80ee8f3b695e 94
EricLew 0:80ee8f3b695e 95 /* Private typedef -----------------------------------------------------------*/
EricLew 0:80ee8f3b695e 96 /* Private define ------------------------------------------------------------*/
EricLew 0:80ee8f3b695e 97 /* Private macro -------------------------------------------------------------*/
EricLew 0:80ee8f3b695e 98 /* Private variables ---------------------------------------------------------*/
EricLew 0:80ee8f3b695e 99 /** @defgroup FLASHEx_Private_Variables FLASHEx Private Variables
EricLew 0:80ee8f3b695e 100 * @{
EricLew 0:80ee8f3b695e 101 */
EricLew 0:80ee8f3b695e 102 extern FLASH_ProcessTypeDef pFlash;
EricLew 0:80ee8f3b695e 103 /**
EricLew 0:80ee8f3b695e 104 * @}
EricLew 0:80ee8f3b695e 105 */
EricLew 0:80ee8f3b695e 106
EricLew 0:80ee8f3b695e 107 /* Private function prototypes -----------------------------------------------*/
EricLew 0:80ee8f3b695e 108 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
EricLew 0:80ee8f3b695e 109 * @{
EricLew 0:80ee8f3b695e 110 */
EricLew 0:80ee8f3b695e 111 extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
EricLew 0:80ee8f3b695e 112 void FLASH_PageErase(uint32_t Page, uint32_t Banks);
EricLew 0:80ee8f3b695e 113 static void FLASH_MassErase(uint32_t Banks);
EricLew 0:80ee8f3b695e 114 void FLASH_FlushCaches(void);
EricLew 0:80ee8f3b695e 115 static HAL_StatusTypeDef FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset);
EricLew 0:80ee8f3b695e 116 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint32_t RDPLevel);
EricLew 0:80ee8f3b695e 117 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig);
EricLew 0:80ee8f3b695e 118 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr);
EricLew 0:80ee8f3b695e 119 static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t * WRPStartOffset, uint32_t * WRDPEndOffset);
EricLew 0:80ee8f3b695e 120 static uint32_t FLASH_OB_GetRDP(void);
EricLew 0:80ee8f3b695e 121 static uint32_t FLASH_OB_GetUser(void);
EricLew 0:80ee8f3b695e 122 static void FLASH_OB_GetPCROP(uint32_t * PCROPConfig, uint32_t * PCROPStartAddr, uint32_t * PCROPEndAddr);
EricLew 0:80ee8f3b695e 123 /**
EricLew 0:80ee8f3b695e 124 * @}
EricLew 0:80ee8f3b695e 125 */
EricLew 0:80ee8f3b695e 126
EricLew 0:80ee8f3b695e 127 /* Exported functions -------------------------------------------------------*/
EricLew 0:80ee8f3b695e 128 /** @defgroup FLASHEx_Exported_Functions FLASH Extended Exported Functions
EricLew 0:80ee8f3b695e 129 * @{
EricLew 0:80ee8f3b695e 130 */
EricLew 0:80ee8f3b695e 131
EricLew 0:80ee8f3b695e 132 /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
EricLew 0:80ee8f3b695e 133 * @brief Extended IO operation functions
EricLew 0:80ee8f3b695e 134 *
EricLew 0:80ee8f3b695e 135 @verbatim
EricLew 0:80ee8f3b695e 136 ===============================================================================
EricLew 0:80ee8f3b695e 137 ##### Extended programming operation functions #####
EricLew 0:80ee8f3b695e 138 ===============================================================================
EricLew 0:80ee8f3b695e 139 [..]
EricLew 0:80ee8f3b695e 140 This subsection provides a set of functions allowing to manage the Extended FLASH
EricLew 0:80ee8f3b695e 141 programming operations Operations.
EricLew 0:80ee8f3b695e 142
EricLew 0:80ee8f3b695e 143 @endverbatim
EricLew 0:80ee8f3b695e 144 * @{
EricLew 0:80ee8f3b695e 145 */
EricLew 0:80ee8f3b695e 146 /**
EricLew 0:80ee8f3b695e 147 * @brief Perform a mass erase or erase the specified FLASH memory pages.
EricLew 0:80ee8f3b695e 148 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
EricLew 0:80ee8f3b695e 149 * contains the configuration information for the erasing.
EricLew 0:80ee8f3b695e 150 *
EricLew 0:80ee8f3b695e 151 * @param[out] PageError : pointer to variable that contains the configuration
EricLew 0:80ee8f3b695e 152 * information on faulty page in case of error (0xFFFFFFFF means that all
EricLew 0:80ee8f3b695e 153 * the pages have been correctly erased)
EricLew 0:80ee8f3b695e 154 *
EricLew 0:80ee8f3b695e 155 * @retval HAL Status
EricLew 0:80ee8f3b695e 156 */
EricLew 0:80ee8f3b695e 157 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
EricLew 0:80ee8f3b695e 158 {
EricLew 0:80ee8f3b695e 159 HAL_StatusTypeDef status = HAL_ERROR;
EricLew 0:80ee8f3b695e 160 uint32_t page_index = 0;
EricLew 0:80ee8f3b695e 161
EricLew 0:80ee8f3b695e 162 /* Process Locked */
EricLew 0:80ee8f3b695e 163 __HAL_LOCK(&pFlash);
EricLew 0:80ee8f3b695e 164
EricLew 0:80ee8f3b695e 165 /* Check the parameters */
EricLew 0:80ee8f3b695e 166 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
EricLew 0:80ee8f3b695e 167
EricLew 0:80ee8f3b695e 168 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 169 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 170
EricLew 0:80ee8f3b695e 171 if (status == HAL_OK)
EricLew 0:80ee8f3b695e 172 {
EricLew 0:80ee8f3b695e 173 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
EricLew 0:80ee8f3b695e 174
EricLew 0:80ee8f3b695e 175 if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
EricLew 0:80ee8f3b695e 176 {
EricLew 0:80ee8f3b695e 177 /* Mass erase to be done */
EricLew 0:80ee8f3b695e 178 FLASH_MassErase(pEraseInit->Banks);
EricLew 0:80ee8f3b695e 179
EricLew 0:80ee8f3b695e 180 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 181 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 182
EricLew 0:80ee8f3b695e 183 /* If the erase operation is completed, disable the MER1 and MER2 Bits */
EricLew 0:80ee8f3b695e 184 CLEAR_BIT(FLASH->CR, (FLASH_CR_MER1 | FLASH_CR_MER2));
EricLew 0:80ee8f3b695e 185 }
EricLew 0:80ee8f3b695e 186 else
EricLew 0:80ee8f3b695e 187 {
EricLew 0:80ee8f3b695e 188 /*Initialization of PageError variable*/
EricLew 0:80ee8f3b695e 189 *PageError = 0xFFFFFFFF;
EricLew 0:80ee8f3b695e 190
EricLew 0:80ee8f3b695e 191 for(page_index = pEraseInit->Page; page_index < (pEraseInit->Page + pEraseInit->NbPages); page_index++)
EricLew 0:80ee8f3b695e 192 {
EricLew 0:80ee8f3b695e 193 FLASH_PageErase(page_index, pEraseInit->Banks);
EricLew 0:80ee8f3b695e 194
EricLew 0:80ee8f3b695e 195 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 196 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 197
EricLew 0:80ee8f3b695e 198 /* If the erase operation is completed, disable the PER Bit */
EricLew 0:80ee8f3b695e 199 CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
EricLew 0:80ee8f3b695e 200
EricLew 0:80ee8f3b695e 201 if (status != HAL_OK)
EricLew 0:80ee8f3b695e 202 {
EricLew 0:80ee8f3b695e 203 /* In case of error, stop erase procedure and return the faulty address */
EricLew 0:80ee8f3b695e 204 *PageError = page_index;
EricLew 0:80ee8f3b695e 205 break;
EricLew 0:80ee8f3b695e 206 }
EricLew 0:80ee8f3b695e 207 }
EricLew 0:80ee8f3b695e 208 }
EricLew 0:80ee8f3b695e 209
EricLew 0:80ee8f3b695e 210 /* Flush the caches to be sure of the data consistency */
EricLew 0:80ee8f3b695e 211 FLASH_FlushCaches();
EricLew 0:80ee8f3b695e 212 }
EricLew 0:80ee8f3b695e 213
EricLew 0:80ee8f3b695e 214 /* Process Unlocked */
EricLew 0:80ee8f3b695e 215 __HAL_UNLOCK(&pFlash);
EricLew 0:80ee8f3b695e 216
EricLew 0:80ee8f3b695e 217 return status;
EricLew 0:80ee8f3b695e 218 }
EricLew 0:80ee8f3b695e 219
EricLew 0:80ee8f3b695e 220 /**
EricLew 0:80ee8f3b695e 221 * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled.
EricLew 0:80ee8f3b695e 222 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
EricLew 0:80ee8f3b695e 223 * contains the configuration information for the erasing.
EricLew 0:80ee8f3b695e 224 *
EricLew 0:80ee8f3b695e 225 * @retval HAL Status
EricLew 0:80ee8f3b695e 226 */
EricLew 0:80ee8f3b695e 227 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
EricLew 0:80ee8f3b695e 228 {
EricLew 0:80ee8f3b695e 229 HAL_StatusTypeDef status = HAL_OK;
EricLew 0:80ee8f3b695e 230
EricLew 0:80ee8f3b695e 231 /* Process Locked */
EricLew 0:80ee8f3b695e 232 __HAL_LOCK(&pFlash);
EricLew 0:80ee8f3b695e 233
EricLew 0:80ee8f3b695e 234 /* Check the parameters */
EricLew 0:80ee8f3b695e 235 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
EricLew 0:80ee8f3b695e 236
EricLew 0:80ee8f3b695e 237 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
EricLew 0:80ee8f3b695e 238
EricLew 0:80ee8f3b695e 239 /* Enable End of Operation and Error interrupts */
EricLew 0:80ee8f3b695e 240 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
EricLew 0:80ee8f3b695e 241
EricLew 0:80ee8f3b695e 242 pFlash.Bank = pEraseInit->Banks;
EricLew 0:80ee8f3b695e 243
EricLew 0:80ee8f3b695e 244 if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
EricLew 0:80ee8f3b695e 245 {
EricLew 0:80ee8f3b695e 246 /* Mass erase to be done */
EricLew 0:80ee8f3b695e 247 pFlash.ProcedureOnGoing = FLASH_PROC_MASS_ERASE;
EricLew 0:80ee8f3b695e 248 FLASH_MassErase(pEraseInit->Banks);
EricLew 0:80ee8f3b695e 249 }
EricLew 0:80ee8f3b695e 250 else
EricLew 0:80ee8f3b695e 251 {
EricLew 0:80ee8f3b695e 252 /* Erase by page to be done */
EricLew 0:80ee8f3b695e 253 pFlash.ProcedureOnGoing = FLASH_PROC_PAGE_ERASE;
EricLew 0:80ee8f3b695e 254 pFlash.NbPagesToErase = pEraseInit->NbPages;
EricLew 0:80ee8f3b695e 255 pFlash.Page = pEraseInit->Page;
EricLew 0:80ee8f3b695e 256
EricLew 0:80ee8f3b695e 257 /*Erase 1st page and wait for IT */
EricLew 0:80ee8f3b695e 258 FLASH_PageErase(pEraseInit->Page, pEraseInit->Banks);
EricLew 0:80ee8f3b695e 259 }
EricLew 0:80ee8f3b695e 260
EricLew 0:80ee8f3b695e 261 return status;
EricLew 0:80ee8f3b695e 262 }
EricLew 0:80ee8f3b695e 263
EricLew 0:80ee8f3b695e 264 /**
EricLew 0:80ee8f3b695e 265 * @brief Program Option bytes.
EricLew 0:80ee8f3b695e 266 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
EricLew 0:80ee8f3b695e 267 * contains the configuration information for the programming.
EricLew 0:80ee8f3b695e 268 *
EricLew 0:80ee8f3b695e 269 * @retval HAL Status
EricLew 0:80ee8f3b695e 270 */
EricLew 0:80ee8f3b695e 271 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
EricLew 0:80ee8f3b695e 272 {
EricLew 0:80ee8f3b695e 273 HAL_StatusTypeDef status = HAL_ERROR;
EricLew 0:80ee8f3b695e 274
EricLew 0:80ee8f3b695e 275 /* Process Locked */
EricLew 0:80ee8f3b695e 276 __HAL_LOCK(&pFlash);
EricLew 0:80ee8f3b695e 277
EricLew 0:80ee8f3b695e 278 /* Check the parameters */
EricLew 0:80ee8f3b695e 279 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
EricLew 0:80ee8f3b695e 280
EricLew 0:80ee8f3b695e 281 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
EricLew 0:80ee8f3b695e 282
EricLew 0:80ee8f3b695e 283 /* Write protection configuration */
EricLew 0:80ee8f3b695e 284 if((pOBInit->OptionType & OPTIONBYTE_WRP) != RESET)
EricLew 0:80ee8f3b695e 285 {
EricLew 0:80ee8f3b695e 286 /* Configure of Write protection on the selected area */
EricLew 0:80ee8f3b695e 287 status = FLASH_OB_WRPConfig(pOBInit->WRPArea, pOBInit->WRPStartOffset, pOBInit->WRPEndOffset);
EricLew 0:80ee8f3b695e 288 }
EricLew 0:80ee8f3b695e 289
EricLew 0:80ee8f3b695e 290 /* Read protection configuration */
EricLew 0:80ee8f3b695e 291 if((pOBInit->OptionType & OPTIONBYTE_RDP) != RESET)
EricLew 0:80ee8f3b695e 292 {
EricLew 0:80ee8f3b695e 293 /* Configure the Read protection level */
EricLew 0:80ee8f3b695e 294 status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
EricLew 0:80ee8f3b695e 295 }
EricLew 0:80ee8f3b695e 296
EricLew 0:80ee8f3b695e 297 /* User Configuration */
EricLew 0:80ee8f3b695e 298 if((pOBInit->OptionType & OPTIONBYTE_USER) != RESET)
EricLew 0:80ee8f3b695e 299 {
EricLew 0:80ee8f3b695e 300 /* Configure the user option bytes */
EricLew 0:80ee8f3b695e 301 status = FLASH_OB_UserConfig(pOBInit->USERType, pOBInit->USERConfig);
EricLew 0:80ee8f3b695e 302 }
EricLew 0:80ee8f3b695e 303
EricLew 0:80ee8f3b695e 304 /* PCROP Configuration */
EricLew 0:80ee8f3b695e 305 if((pOBInit->OptionType & OPTIONBYTE_PCROP) != RESET)
EricLew 0:80ee8f3b695e 306 {
EricLew 0:80ee8f3b695e 307 /* Configure the Proprietary code readout protection */
EricLew 0:80ee8f3b695e 308 status = FLASH_OB_PCROPConfig(pOBInit->PCROPConfig, pOBInit->PCROPStartAddr, pOBInit->PCROPEndAddr);
EricLew 0:80ee8f3b695e 309 }
EricLew 0:80ee8f3b695e 310
EricLew 0:80ee8f3b695e 311 /* Process Unlocked */
EricLew 0:80ee8f3b695e 312 __HAL_UNLOCK(&pFlash);
EricLew 0:80ee8f3b695e 313
EricLew 0:80ee8f3b695e 314 return status;
EricLew 0:80ee8f3b695e 315 }
EricLew 0:80ee8f3b695e 316
EricLew 0:80ee8f3b695e 317 /**
EricLew 0:80ee8f3b695e 318 * @brief Get the Option bytes configuration.
EricLew 0:80ee8f3b695e 319 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that contains the
EricLew 0:80ee8f3b695e 320 * configuration information. The fields pOBInit->WRPArea and
EricLew 0:80ee8f3b695e 321 * pOBInit->PCROPConfig should indicate which area is requested
EricLew 0:80ee8f3b695e 322 * for the WRP and PCROP
EricLew 0:80ee8f3b695e 323 *
EricLew 0:80ee8f3b695e 324 * @retval None
EricLew 0:80ee8f3b695e 325 */
EricLew 0:80ee8f3b695e 326 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
EricLew 0:80ee8f3b695e 327 {
EricLew 0:80ee8f3b695e 328 pOBInit->OptionType = (OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_PCROP);
EricLew 0:80ee8f3b695e 329
EricLew 0:80ee8f3b695e 330 /* Get write protection on the selected area */
EricLew 0:80ee8f3b695e 331 FLASH_OB_GetWRP(pOBInit->WRPArea, &(pOBInit->WRPStartOffset), &(pOBInit->WRPEndOffset));
EricLew 0:80ee8f3b695e 332
EricLew 0:80ee8f3b695e 333 /* Get Read protection level */
EricLew 0:80ee8f3b695e 334 pOBInit->RDPLevel = FLASH_OB_GetRDP();
EricLew 0:80ee8f3b695e 335
EricLew 0:80ee8f3b695e 336 /* Get the user option bytes */
EricLew 0:80ee8f3b695e 337 pOBInit->USERConfig = FLASH_OB_GetUser();
EricLew 0:80ee8f3b695e 338
EricLew 0:80ee8f3b695e 339 /* Get the Proprietary code readout protection */
EricLew 0:80ee8f3b695e 340 FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr));
EricLew 0:80ee8f3b695e 341
EricLew 0:80ee8f3b695e 342 }
EricLew 0:80ee8f3b695e 343
EricLew 0:80ee8f3b695e 344 /**
EricLew 0:80ee8f3b695e 345 * @}
EricLew 0:80ee8f3b695e 346 */
EricLew 0:80ee8f3b695e 347
EricLew 0:80ee8f3b695e 348 /**
EricLew 0:80ee8f3b695e 349 * @}
EricLew 0:80ee8f3b695e 350 */
EricLew 0:80ee8f3b695e 351
EricLew 0:80ee8f3b695e 352 /* Private functions ---------------------------------------------------------*/
EricLew 0:80ee8f3b695e 353
EricLew 0:80ee8f3b695e 354 /** @addtogroup FLASHEx_Private_Functions
EricLew 0:80ee8f3b695e 355 * @{
EricLew 0:80ee8f3b695e 356 */
EricLew 0:80ee8f3b695e 357 /**
EricLew 0:80ee8f3b695e 358 * @brief Mass erase of FLASH memory.
EricLew 0:80ee8f3b695e 359 * @param Banks: Banks to be erased
EricLew 0:80ee8f3b695e 360 * This parameter can be one of the following values:
EricLew 0:80ee8f3b695e 361 * @arg FLASH_BANK_1: Bank1 to be erased
EricLew 0:80ee8f3b695e 362 * @arg FLASH_BANK_2: Bank2 to be erased
EricLew 0:80ee8f3b695e 363 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
EricLew 0:80ee8f3b695e 364 * @retval None
EricLew 0:80ee8f3b695e 365 */
EricLew 0:80ee8f3b695e 366 static void FLASH_MassErase(uint32_t Banks)
EricLew 0:80ee8f3b695e 367 {
EricLew 0:80ee8f3b695e 368 /* Check the parameters */
EricLew 0:80ee8f3b695e 369 assert_param(IS_FLASH_BANK(Banks));
EricLew 0:80ee8f3b695e 370
EricLew 0:80ee8f3b695e 371 /* Set the Mass Erase Bit for the bank 1 if requested */
EricLew 0:80ee8f3b695e 372 if((Banks & FLASH_BANK_1) != RESET)
EricLew 0:80ee8f3b695e 373 {
EricLew 0:80ee8f3b695e 374 SET_BIT(FLASH->CR, FLASH_CR_MER1);
EricLew 0:80ee8f3b695e 375 }
EricLew 0:80ee8f3b695e 376
EricLew 0:80ee8f3b695e 377 /* Set the Mass Erase Bit for the bank 2 if requested */
EricLew 0:80ee8f3b695e 378 if((Banks & FLASH_BANK_2) != RESET)
EricLew 0:80ee8f3b695e 379 {
EricLew 0:80ee8f3b695e 380 SET_BIT(FLASH->CR, FLASH_CR_MER2);
EricLew 0:80ee8f3b695e 381 }
EricLew 0:80ee8f3b695e 382
EricLew 0:80ee8f3b695e 383 /* Proceed to erase all sectors */
EricLew 0:80ee8f3b695e 384 SET_BIT(FLASH->CR, FLASH_CR_STRT);
EricLew 0:80ee8f3b695e 385 }
EricLew 0:80ee8f3b695e 386
EricLew 0:80ee8f3b695e 387 /**
EricLew 0:80ee8f3b695e 388 * @brief Erase the specified FLASH memory page.
EricLew 0:80ee8f3b695e 389 * @param Page: FLASH page to erase
EricLew 0:80ee8f3b695e 390 * This parameter must be a value between 0 and (max number of pages in the bank - 1)
EricLew 0:80ee8f3b695e 391 * @param Banks: Bank(s) where the page will be erased
EricLew 0:80ee8f3b695e 392 * This parameter can be one or a combination of the following values:
EricLew 0:80ee8f3b695e 393 * @arg FLASH_BANK_1: Page in bank 1 to be erased
EricLew 0:80ee8f3b695e 394 * @arg FLASH_BANK_2: Page in bank 2 to be erased
EricLew 0:80ee8f3b695e 395 * @retval None
EricLew 0:80ee8f3b695e 396 */
EricLew 0:80ee8f3b695e 397 void FLASH_PageErase(uint32_t Page, uint32_t Banks)
EricLew 0:80ee8f3b695e 398 {
EricLew 0:80ee8f3b695e 399 /* Check the parameters */
EricLew 0:80ee8f3b695e 400 assert_param(IS_FLASH_PAGE(Page));
EricLew 0:80ee8f3b695e 401 assert_param(IS_FLASH_BANK_EXCLUSIVE(Banks));
EricLew 0:80ee8f3b695e 402
EricLew 0:80ee8f3b695e 403 if((Banks & FLASH_BANK_1) != RESET)
EricLew 0:80ee8f3b695e 404 {
EricLew 0:80ee8f3b695e 405 CLEAR_BIT(FLASH->CR, FLASH_CR_BKER);
EricLew 0:80ee8f3b695e 406 }
EricLew 0:80ee8f3b695e 407 else
EricLew 0:80ee8f3b695e 408 {
EricLew 0:80ee8f3b695e 409 SET_BIT(FLASH->CR, FLASH_CR_BKER);
EricLew 0:80ee8f3b695e 410 }
EricLew 0:80ee8f3b695e 411
EricLew 0:80ee8f3b695e 412 /* Proceed to erase the page */
EricLew 0:80ee8f3b695e 413 MODIFY_REG(FLASH->CR, FLASH_CR_PNB, (Page << 3));
EricLew 0:80ee8f3b695e 414 SET_BIT(FLASH->CR, FLASH_CR_PER);
EricLew 0:80ee8f3b695e 415 SET_BIT(FLASH->CR, FLASH_CR_STRT);
EricLew 0:80ee8f3b695e 416 }
EricLew 0:80ee8f3b695e 417
EricLew 0:80ee8f3b695e 418 /**
EricLew 0:80ee8f3b695e 419 * @brief Flush the instruction and data caches.
EricLew 0:80ee8f3b695e 420 * @retval None
EricLew 0:80ee8f3b695e 421 */
EricLew 0:80ee8f3b695e 422 void FLASH_FlushCaches(void)
EricLew 0:80ee8f3b695e 423 {
EricLew 0:80ee8f3b695e 424 /* Flush instruction cache */
EricLew 0:80ee8f3b695e 425 if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != RESET)
EricLew 0:80ee8f3b695e 426 {
EricLew 0:80ee8f3b695e 427 /* Disable instruction cache */
EricLew 0:80ee8f3b695e 428 __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
EricLew 0:80ee8f3b695e 429 /* Reset instruction cache */
EricLew 0:80ee8f3b695e 430 __HAL_FLASH_INSTRUCTION_CACHE_RESET();
EricLew 0:80ee8f3b695e 431 /* Enable instruction cache */
EricLew 0:80ee8f3b695e 432 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
EricLew 0:80ee8f3b695e 433 }
EricLew 0:80ee8f3b695e 434
EricLew 0:80ee8f3b695e 435 /* Flush data cache */
EricLew 0:80ee8f3b695e 436 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
EricLew 0:80ee8f3b695e 437 {
EricLew 0:80ee8f3b695e 438 /* Disable data cache */
EricLew 0:80ee8f3b695e 439 __HAL_FLASH_DATA_CACHE_DISABLE();
EricLew 0:80ee8f3b695e 440 /* Reset data cache */
EricLew 0:80ee8f3b695e 441 __HAL_FLASH_DATA_CACHE_RESET();
EricLew 0:80ee8f3b695e 442 /* Enable data cache */
EricLew 0:80ee8f3b695e 443 __HAL_FLASH_DATA_CACHE_ENABLE();
EricLew 0:80ee8f3b695e 444 }
EricLew 0:80ee8f3b695e 445 }
EricLew 0:80ee8f3b695e 446
EricLew 0:80ee8f3b695e 447 /**
EricLew 0:80ee8f3b695e 448 * @brief Configure the write protection of the desired pages.
EricLew 0:80ee8f3b695e 449 *
EricLew 0:80ee8f3b695e 450 * @note When the memory read protection level is selected (RDP level = 1),
EricLew 0:80ee8f3b695e 451 * it is not possible to program or erase Flash memory if the CPU debug
EricLew 0:80ee8f3b695e 452 * features are connected (JTAG or single wire) or boot code is being
EricLew 0:80ee8f3b695e 453 * executed from RAM or System flash, even if WRP is not activated.
EricLew 0:80ee8f3b695e 454 * @note To configure the WRP options, the option lock bit OPTLOCK must be
EricLew 0:80ee8f3b695e 455 * cleared with the call of the HAL_FLASH_OB_Unlock() function.
EricLew 0:80ee8f3b695e 456 * @note To validate the WRP options, the option bytes must be reloaded
EricLew 0:80ee8f3b695e 457 * through the call of the HAL_FLASH_OB_Launch() function.
EricLew 0:80ee8f3b695e 458 *
EricLew 0:80ee8f3b695e 459 * @param WRPArea: specifies the area to be configured.
EricLew 0:80ee8f3b695e 460 * This parameter can be one of the following values:
EricLew 0:80ee8f3b695e 461 * @arg OB_WRPAREA_BANK1_AREAA: Flash Bank 1 Area A
EricLew 0:80ee8f3b695e 462 * @arg OB_WRPAREA_BANK1_AREAB: Flash Bank 1 Area B
EricLew 0:80ee8f3b695e 463 * @arg OB_WRPAREA_BANK2_AREAA: Flash Bank 2 Area A
EricLew 0:80ee8f3b695e 464 * @arg OB_WRPAREA_BANK2_AREAB: Flash Bank 2 Area B
EricLew 0:80ee8f3b695e 465 *
EricLew 0:80ee8f3b695e 466 * @param WRPStartOffset: specifies the start page of the write protected area
EricLew 0:80ee8f3b695e 467 * This parameter can be page number between 0 and (max number of pages in the bank - 1)
EricLew 0:80ee8f3b695e 468 *
EricLew 0:80ee8f3b695e 469 * @param WRDPEndOffset: specifies the end page of the write protected area
EricLew 0:80ee8f3b695e 470 * This parameter can be page number between WRPStartOffset and (max number of pages in the bank - 1)
EricLew 0:80ee8f3b695e 471 *
EricLew 0:80ee8f3b695e 472 * @retval HAL Status
EricLew 0:80ee8f3b695e 473 */
EricLew 0:80ee8f3b695e 474 static HAL_StatusTypeDef FLASH_OB_WRPConfig(uint32_t WRPArea, uint32_t WRPStartOffset, uint32_t WRDPEndOffset)
EricLew 0:80ee8f3b695e 475 {
EricLew 0:80ee8f3b695e 476 HAL_StatusTypeDef status = HAL_OK;
EricLew 0:80ee8f3b695e 477
EricLew 0:80ee8f3b695e 478 /* Check the parameters */
EricLew 0:80ee8f3b695e 479 assert_param(IS_OB_WRPAREA(WRPArea));
EricLew 0:80ee8f3b695e 480 assert_param(IS_FLASH_PAGE(WRPStartOffset));
EricLew 0:80ee8f3b695e 481 assert_param(IS_FLASH_PAGE(WRDPEndOffset));
EricLew 0:80ee8f3b695e 482
EricLew 0:80ee8f3b695e 483 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 484 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 485
EricLew 0:80ee8f3b695e 486 if(status == HAL_OK)
EricLew 0:80ee8f3b695e 487 {
EricLew 0:80ee8f3b695e 488 /* Configure the write protected area */
EricLew 0:80ee8f3b695e 489 if(WRPArea == OB_WRPAREA_BANK1_AREAA)
EricLew 0:80ee8f3b695e 490 {
EricLew 0:80ee8f3b695e 491 MODIFY_REG(FLASH->WRP1AR, (FLASH_WRP1AR_WRP1A_STRT | FLASH_WRP1AR_WRP1A_END),
EricLew 0:80ee8f3b695e 492 (WRPStartOffset | (WRDPEndOffset << 16)));
EricLew 0:80ee8f3b695e 493 }
EricLew 0:80ee8f3b695e 494 else if(WRPArea == OB_WRPAREA_BANK1_AREAB)
EricLew 0:80ee8f3b695e 495 {
EricLew 0:80ee8f3b695e 496 MODIFY_REG(FLASH->WRP1BR, (FLASH_WRP1BR_WRP1B_STRT | FLASH_WRP1BR_WRP1B_END),
EricLew 0:80ee8f3b695e 497 (WRPStartOffset | (WRDPEndOffset << 16)));
EricLew 0:80ee8f3b695e 498 }
EricLew 0:80ee8f3b695e 499 else if(WRPArea == OB_WRPAREA_BANK2_AREAA)
EricLew 0:80ee8f3b695e 500 {
EricLew 0:80ee8f3b695e 501 MODIFY_REG(FLASH->WRP2AR, (FLASH_WRP2AR_WRP2A_STRT | FLASH_WRP2AR_WRP2A_END),
EricLew 0:80ee8f3b695e 502 (WRPStartOffset | (WRDPEndOffset << 16)));
EricLew 0:80ee8f3b695e 503 }
EricLew 0:80ee8f3b695e 504 else if(WRPArea == OB_WRPAREA_BANK2_AREAB)
EricLew 0:80ee8f3b695e 505 {
EricLew 0:80ee8f3b695e 506 MODIFY_REG(FLASH->WRP2BR, (FLASH_WRP2BR_WRP2B_STRT | FLASH_WRP2BR_WRP2B_END),
EricLew 0:80ee8f3b695e 507 (WRPStartOffset | (WRDPEndOffset << 16)));
EricLew 0:80ee8f3b695e 508 }
EricLew 0:80ee8f3b695e 509
EricLew 0:80ee8f3b695e 510 /* Set OPTSTRT Bit */
EricLew 0:80ee8f3b695e 511 SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 512
EricLew 0:80ee8f3b695e 513 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 514 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 515
EricLew 0:80ee8f3b695e 516 /* If the option byte program operation is completed, disable the OPTSTRT Bit */
EricLew 0:80ee8f3b695e 517 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 518 }
EricLew 0:80ee8f3b695e 519
EricLew 0:80ee8f3b695e 520 return status;
EricLew 0:80ee8f3b695e 521 }
EricLew 0:80ee8f3b695e 522
EricLew 0:80ee8f3b695e 523 /**
EricLew 0:80ee8f3b695e 524 * @brief Set the read protection level.
EricLew 0:80ee8f3b695e 525 *
EricLew 0:80ee8f3b695e 526 * @note To configure the RDP level, the option lock bit OPTLOCK must be
EricLew 0:80ee8f3b695e 527 * cleared with the call of the HAL_FLASH_OB_Unlock() function.
EricLew 0:80ee8f3b695e 528 * @note To validate the RDP level, the option bytes must be reloaded
EricLew 0:80ee8f3b695e 529 * through the call of the HAL_FLASH_OB_Launch() function.
EricLew 0:80ee8f3b695e 530 * @note !!! Warning : When enabling OB_RDP level 2 it's no more possible
EricLew 0:80ee8f3b695e 531 * to go back to level 1 or 0 !!!
EricLew 0:80ee8f3b695e 532 *
EricLew 0:80ee8f3b695e 533 * @param RDPLevel: specifies the read protection level.
EricLew 0:80ee8f3b695e 534 * This parameter can be one of the following values:
EricLew 0:80ee8f3b695e 535 * @arg OB_RDP_LEVEL_0: No protection
EricLew 0:80ee8f3b695e 536 * @arg OB_RDP_LEVEL_1: Read protection of the memory
EricLew 0:80ee8f3b695e 537 * @arg OB_RDP_LEVEL_2: Full chip protection
EricLew 0:80ee8f3b695e 538 *
EricLew 0:80ee8f3b695e 539 * @retval HAL status
EricLew 0:80ee8f3b695e 540 */
EricLew 0:80ee8f3b695e 541 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint32_t RDPLevel)
EricLew 0:80ee8f3b695e 542 {
EricLew 0:80ee8f3b695e 543 HAL_StatusTypeDef status = HAL_OK;
EricLew 0:80ee8f3b695e 544
EricLew 0:80ee8f3b695e 545 /* Check the parameters */
EricLew 0:80ee8f3b695e 546 assert_param(IS_OB_RDP_LEVEL(RDPLevel));
EricLew 0:80ee8f3b695e 547
EricLew 0:80ee8f3b695e 548 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 549 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 550
EricLew 0:80ee8f3b695e 551 if(status == HAL_OK)
EricLew 0:80ee8f3b695e 552 {
EricLew 0:80ee8f3b695e 553 /* Configure the RDP level in the option bytes register */
EricLew 0:80ee8f3b695e 554 MODIFY_REG(FLASH->OPTR, FLASH_OPTR_RDP, RDPLevel);
EricLew 0:80ee8f3b695e 555
EricLew 0:80ee8f3b695e 556 /* Set OPTSTRT Bit */
EricLew 0:80ee8f3b695e 557 SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 558
EricLew 0:80ee8f3b695e 559 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 560 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 561
EricLew 0:80ee8f3b695e 562 /* If the option byte program operation is completed, disable the OPTSTRT Bit */
EricLew 0:80ee8f3b695e 563 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 564 }
EricLew 0:80ee8f3b695e 565
EricLew 0:80ee8f3b695e 566 return status;
EricLew 0:80ee8f3b695e 567 }
EricLew 0:80ee8f3b695e 568
EricLew 0:80ee8f3b695e 569 /**
EricLew 0:80ee8f3b695e 570 * @brief Program the FLASH User Option Byte.
EricLew 0:80ee8f3b695e 571 *
EricLew 0:80ee8f3b695e 572 * @note To configure the user option bytes, the option lock bit OPTLOCK must
EricLew 0:80ee8f3b695e 573 * be cleared with the call of the HAL_FLASH_OB_Unlock() function.
EricLew 0:80ee8f3b695e 574 * @note To validate the user option bytes, the option bytes must be reloaded
EricLew 0:80ee8f3b695e 575 * through the call of the HAL_FLASH_OB_Launch() function.
EricLew 0:80ee8f3b695e 576 *
EricLew 0:80ee8f3b695e 577 * @param UserType: The FLASH User Option Bytes to be modified
EricLew 0:80ee8f3b695e 578 * @param UserConfig: The FLASH User Option Bytes values:
EricLew 0:80ee8f3b695e 579 * BOR_LEV(Bit8-10), nRST_STOP(Bit12), nRST_STDBY(Bit13), IWDG_SW(Bit16),
EricLew 0:80ee8f3b695e 580 * IWDG_STOP(Bit17), IWDG_STDBY(Bit18), WWDG_SW(Bit19), BFB2(Bit20),
EricLew 0:80ee8f3b695e 581 * DUALBANK(Bit21), nBOOT1(Bit23), SRAM2_PE(Bit24) and SRAM2_RST(Bit25).
EricLew 0:80ee8f3b695e 582 *
EricLew 0:80ee8f3b695e 583 * @retval HAL status
EricLew 0:80ee8f3b695e 584 */
EricLew 0:80ee8f3b695e 585 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
EricLew 0:80ee8f3b695e 586 {
EricLew 0:80ee8f3b695e 587 uint32_t optr_reg_val = 0;
EricLew 0:80ee8f3b695e 588 uint32_t optr_reg_mask = 0;
EricLew 0:80ee8f3b695e 589 HAL_StatusTypeDef status = HAL_OK;
EricLew 0:80ee8f3b695e 590
EricLew 0:80ee8f3b695e 591 /* Check the parameters */
EricLew 0:80ee8f3b695e 592 assert_param(IS_OB_USER_TYPE(UserType));
EricLew 0:80ee8f3b695e 593
EricLew 0:80ee8f3b695e 594 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 595 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 596
EricLew 0:80ee8f3b695e 597 if(status == HAL_OK)
EricLew 0:80ee8f3b695e 598 {
EricLew 0:80ee8f3b695e 599 if((UserType & OB_USER_BOR_LEV) != RESET)
EricLew 0:80ee8f3b695e 600 {
EricLew 0:80ee8f3b695e 601 /* BOR level option byte should be modified */
EricLew 0:80ee8f3b695e 602 assert_param(IS_OB_USER_BOR_LEVEL(UserConfig & FLASH_OPTR_BOR_LEV));
EricLew 0:80ee8f3b695e 603
EricLew 0:80ee8f3b695e 604 /* Set value and mask for BOR level option byte */
EricLew 0:80ee8f3b695e 605 optr_reg_val |= (UserConfig & FLASH_OPTR_BOR_LEV);
EricLew 0:80ee8f3b695e 606 optr_reg_mask |= FLASH_OPTR_BOR_LEV;
EricLew 0:80ee8f3b695e 607 }
EricLew 0:80ee8f3b695e 608
EricLew 0:80ee8f3b695e 609 if((UserType & OB_USER_nRST_STOP) != RESET)
EricLew 0:80ee8f3b695e 610 {
EricLew 0:80ee8f3b695e 611 /* nRST_STOP option byte should be modified */
EricLew 0:80ee8f3b695e 612 assert_param(IS_OB_USER_STOP(UserConfig & FLASH_OPTR_nRST_STOP));
EricLew 0:80ee8f3b695e 613
EricLew 0:80ee8f3b695e 614 /* Set value and mask for nRST_STOP option byte */
EricLew 0:80ee8f3b695e 615 optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_STOP);
EricLew 0:80ee8f3b695e 616 optr_reg_mask |= FLASH_OPTR_nRST_STOP;
EricLew 0:80ee8f3b695e 617 }
EricLew 0:80ee8f3b695e 618
EricLew 0:80ee8f3b695e 619 if((UserType & OB_USER_nRST_STDBY) != RESET)
EricLew 0:80ee8f3b695e 620 {
EricLew 0:80ee8f3b695e 621 /* nRST_STDBY option byte should be modified */
EricLew 0:80ee8f3b695e 622 assert_param(IS_OB_USER_STANDBY(UserConfig & FLASH_OPTR_nRST_STDBY));
EricLew 0:80ee8f3b695e 623
EricLew 0:80ee8f3b695e 624 /* Set value and mask for nRST_STDBY option byte */
EricLew 0:80ee8f3b695e 625 optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_STDBY);
EricLew 0:80ee8f3b695e 626 optr_reg_mask |= FLASH_OPTR_nRST_STDBY;
EricLew 0:80ee8f3b695e 627 }
EricLew 0:80ee8f3b695e 628
EricLew 0:80ee8f3b695e 629 if((UserType & OB_USER_nRST_SHDW) != RESET)
EricLew 0:80ee8f3b695e 630 {
EricLew 0:80ee8f3b695e 631 /* nRST_SHDW option byte should be modified */
EricLew 0:80ee8f3b695e 632 assert_param(IS_OB_USER_SHUTDOWN(UserConfig & FLASH_OPTR_nRST_SHDW));
EricLew 0:80ee8f3b695e 633
EricLew 0:80ee8f3b695e 634 /* Set value and mask for nRST_SHDW option byte */
EricLew 0:80ee8f3b695e 635 optr_reg_val |= (UserConfig & FLASH_OPTR_nRST_SHDW);
EricLew 0:80ee8f3b695e 636 optr_reg_mask |= FLASH_OPTR_nRST_SHDW;
EricLew 0:80ee8f3b695e 637 }
EricLew 0:80ee8f3b695e 638
EricLew 0:80ee8f3b695e 639 if((UserType & OB_USER_IWDG_SW) != RESET)
EricLew 0:80ee8f3b695e 640 {
EricLew 0:80ee8f3b695e 641 /* IWDG_SW option byte should be modified */
EricLew 0:80ee8f3b695e 642 assert_param(IS_OB_USER_IWDG(UserConfig & FLASH_OPTR_IWDG_SW));
EricLew 0:80ee8f3b695e 643
EricLew 0:80ee8f3b695e 644 /* Set value and mask for IWDG_SW option byte */
EricLew 0:80ee8f3b695e 645 optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_SW);
EricLew 0:80ee8f3b695e 646 optr_reg_mask |= FLASH_OPTR_IWDG_SW;
EricLew 0:80ee8f3b695e 647 }
EricLew 0:80ee8f3b695e 648
EricLew 0:80ee8f3b695e 649 if((UserType & OB_USER_IWDG_STOP) != RESET)
EricLew 0:80ee8f3b695e 650 {
EricLew 0:80ee8f3b695e 651 /* IWDG_STOP option byte should be modified */
EricLew 0:80ee8f3b695e 652 assert_param(IS_OB_USER_IWDG_STOP(UserConfig & FLASH_OPTR_IWDG_STOP));
EricLew 0:80ee8f3b695e 653
EricLew 0:80ee8f3b695e 654 /* Set value and mask for IWDG_STOP option byte */
EricLew 0:80ee8f3b695e 655 optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_STOP);
EricLew 0:80ee8f3b695e 656 optr_reg_mask |= FLASH_OPTR_IWDG_STOP;
EricLew 0:80ee8f3b695e 657 }
EricLew 0:80ee8f3b695e 658
EricLew 0:80ee8f3b695e 659 if((UserType & OB_USER_IWDG_STDBY) != RESET)
EricLew 0:80ee8f3b695e 660 {
EricLew 0:80ee8f3b695e 661 /* IWDG_STDBY option byte should be modified */
EricLew 0:80ee8f3b695e 662 assert_param(IS_OB_USER_IWDG_STDBY(UserConfig & FLASH_OPTR_IWDG_STDBY));
EricLew 0:80ee8f3b695e 663
EricLew 0:80ee8f3b695e 664 /* Set value and mask for IWDG_STDBY option byte */
EricLew 0:80ee8f3b695e 665 optr_reg_val |= (UserConfig & FLASH_OPTR_IWDG_STDBY);
EricLew 0:80ee8f3b695e 666 optr_reg_mask |= FLASH_OPTR_IWDG_STDBY;
EricLew 0:80ee8f3b695e 667 }
EricLew 0:80ee8f3b695e 668
EricLew 0:80ee8f3b695e 669 if((UserType & OB_USER_WWDG_SW) != RESET)
EricLew 0:80ee8f3b695e 670 {
EricLew 0:80ee8f3b695e 671 /* WWDG_SW option byte should be modified */
EricLew 0:80ee8f3b695e 672 assert_param(IS_OB_USER_WWDG(UserConfig & FLASH_OPTR_WWDG_SW));
EricLew 0:80ee8f3b695e 673
EricLew 0:80ee8f3b695e 674 /* Set value and mask for WWDG_SW option byte */
EricLew 0:80ee8f3b695e 675 optr_reg_val |= (UserConfig & FLASH_OPTR_WWDG_SW);
EricLew 0:80ee8f3b695e 676 optr_reg_mask |= FLASH_OPTR_WWDG_SW;
EricLew 0:80ee8f3b695e 677 }
EricLew 0:80ee8f3b695e 678
EricLew 0:80ee8f3b695e 679 if((UserType & OB_USER_BFB2) != RESET)
EricLew 0:80ee8f3b695e 680 {
EricLew 0:80ee8f3b695e 681 /* BFB2 option byte should be modified */
EricLew 0:80ee8f3b695e 682 assert_param(IS_OB_USER_BFB2(UserConfig & FLASH_OPTR_BFB2));
EricLew 0:80ee8f3b695e 683
EricLew 0:80ee8f3b695e 684 /* Set value and mask for BFB2 option byte */
EricLew 0:80ee8f3b695e 685 optr_reg_val |= (UserConfig & FLASH_OPTR_BFB2);
EricLew 0:80ee8f3b695e 686 optr_reg_mask |= FLASH_OPTR_BFB2;
EricLew 0:80ee8f3b695e 687 }
EricLew 0:80ee8f3b695e 688
EricLew 0:80ee8f3b695e 689 if((UserType & OB_USER_DUALBANK) != RESET)
EricLew 0:80ee8f3b695e 690 {
EricLew 0:80ee8f3b695e 691 /* DUALBANK option byte should be modified */
EricLew 0:80ee8f3b695e 692 assert_param(IS_OB_USER_DUALBANK(UserConfig & FLASH_OPTR_DUALBANK));
EricLew 0:80ee8f3b695e 693
EricLew 0:80ee8f3b695e 694 /* Set value and mask for DUALBANK option byte */
EricLew 0:80ee8f3b695e 695 optr_reg_val |= (UserConfig & FLASH_OPTR_DUALBANK);
EricLew 0:80ee8f3b695e 696 optr_reg_mask |= FLASH_OPTR_DUALBANK;
EricLew 0:80ee8f3b695e 697 }
EricLew 0:80ee8f3b695e 698
EricLew 0:80ee8f3b695e 699 if((UserType & OB_USER_nBOOT1) != RESET)
EricLew 0:80ee8f3b695e 700 {
EricLew 0:80ee8f3b695e 701 /* nBOOT1 option byte should be modified */
EricLew 0:80ee8f3b695e 702 assert_param(IS_OB_USER_BOOT1(UserConfig & FLASH_OPTR_nBOOT1));
EricLew 0:80ee8f3b695e 703
EricLew 0:80ee8f3b695e 704 /* Set value and mask for nBOOT1 option byte */
EricLew 0:80ee8f3b695e 705 optr_reg_val |= (UserConfig & FLASH_OPTR_nBOOT1);
EricLew 0:80ee8f3b695e 706 optr_reg_mask |= FLASH_OPTR_nBOOT1;
EricLew 0:80ee8f3b695e 707 }
EricLew 0:80ee8f3b695e 708
EricLew 0:80ee8f3b695e 709 if((UserType & OB_USER_SRAM2_PE) != RESET)
EricLew 0:80ee8f3b695e 710 {
EricLew 0:80ee8f3b695e 711 /* SRAM2_PE option byte should be modified */
EricLew 0:80ee8f3b695e 712 assert_param(IS_OB_USER_SRAM2_PARITY(UserConfig & FLASH_OPTR_SRAM2_PE));
EricLew 0:80ee8f3b695e 713
EricLew 0:80ee8f3b695e 714 /* Set value and mask for SRAM2_PE option byte */
EricLew 0:80ee8f3b695e 715 optr_reg_val |= (UserConfig & FLASH_OPTR_SRAM2_PE);
EricLew 0:80ee8f3b695e 716 optr_reg_mask |= FLASH_OPTR_SRAM2_PE;
EricLew 0:80ee8f3b695e 717 }
EricLew 0:80ee8f3b695e 718
EricLew 0:80ee8f3b695e 719 if((UserType & OB_USER_SRAM2_RST) != RESET)
EricLew 0:80ee8f3b695e 720 {
EricLew 0:80ee8f3b695e 721 /* SRAM2_RST option byte should be modified */
EricLew 0:80ee8f3b695e 722 assert_param(IS_OB_USER_SRAM2_RST(UserConfig & FLASH_OPTR_SRAM2_RST));
EricLew 0:80ee8f3b695e 723
EricLew 0:80ee8f3b695e 724 /* Set value and mask for SRAM2_RST option byte */
EricLew 0:80ee8f3b695e 725 optr_reg_val |= (UserConfig & FLASH_OPTR_SRAM2_RST);
EricLew 0:80ee8f3b695e 726 optr_reg_mask |= FLASH_OPTR_SRAM2_RST;
EricLew 0:80ee8f3b695e 727 }
EricLew 0:80ee8f3b695e 728
EricLew 0:80ee8f3b695e 729 /* Configure the option bytes register */
EricLew 0:80ee8f3b695e 730 MODIFY_REG(FLASH->OPTR, optr_reg_mask, optr_reg_val);
EricLew 0:80ee8f3b695e 731
EricLew 0:80ee8f3b695e 732 /* Set OPTSTRT Bit */
EricLew 0:80ee8f3b695e 733 SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 734
EricLew 0:80ee8f3b695e 735 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 736 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 737
EricLew 0:80ee8f3b695e 738 /* If the option byte program operation is completed, disable the OPTSTRT Bit */
EricLew 0:80ee8f3b695e 739 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 740 }
EricLew 0:80ee8f3b695e 741
EricLew 0:80ee8f3b695e 742 return status;
EricLew 0:80ee8f3b695e 743 }
EricLew 0:80ee8f3b695e 744
EricLew 0:80ee8f3b695e 745 /**
EricLew 0:80ee8f3b695e 746 * @brief Configure the Proprietary code readout protection of the desired addresses.
EricLew 0:80ee8f3b695e 747 *
EricLew 0:80ee8f3b695e 748 * @note To configure the PCROP options, the option lock bit OPTLOCK must be
EricLew 0:80ee8f3b695e 749 * cleared with the call of the HAL_FLASH_OB_Unlock() function.
EricLew 0:80ee8f3b695e 750 * @note To validate the PCROP options, the option bytes must be reloaded
EricLew 0:80ee8f3b695e 751 * through the call of the HAL_FLASH_OB_Launch() function.
EricLew 0:80ee8f3b695e 752 *
EricLew 0:80ee8f3b695e 753 * @param PCROPConfig: specifies the configuration (Bank to be configured and PCROP_RDP option).
EricLew 0:80ee8f3b695e 754 * This parameter must be a combination of FLASH_BANK_1 or FLASH_BANK_2
EricLew 0:80ee8f3b695e 755 * with OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE
EricLew 0:80ee8f3b695e 756 *
EricLew 0:80ee8f3b695e 757 * @param PCROPStartAddr: specifies the start address of the Proprietary code readout protection
EricLew 0:80ee8f3b695e 758 * This parameter can be an address between begin and end of the bank
EricLew 0:80ee8f3b695e 759 *
EricLew 0:80ee8f3b695e 760 * @param PCROPEndAddr: specifies the end address of the Proprietary code readout protection
EricLew 0:80ee8f3b695e 761 * This parameter can be an address between PCROPStartAddr and end of the bank
EricLew 0:80ee8f3b695e 762 *
EricLew 0:80ee8f3b695e 763 * @retval HAL Status
EricLew 0:80ee8f3b695e 764 */
EricLew 0:80ee8f3b695e 765 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr)
EricLew 0:80ee8f3b695e 766 {
EricLew 0:80ee8f3b695e 767 HAL_StatusTypeDef status = HAL_OK;
EricLew 0:80ee8f3b695e 768 uint32_t reg_value = 0;
EricLew 0:80ee8f3b695e 769 uint32_t bank1_addr, bank2_addr;
EricLew 0:80ee8f3b695e 770
EricLew 0:80ee8f3b695e 771 /* Check the parameters */
EricLew 0:80ee8f3b695e 772 assert_param(IS_FLASH_BANK_EXCLUSIVE(PCROPConfig & FLASH_BANK_BOTH));
EricLew 0:80ee8f3b695e 773 assert_param(IS_OB_PCROP_RDP(PCROPConfig & FLASH_PCROP1ER_PCROP_RDP));
EricLew 0:80ee8f3b695e 774 assert_param(IS_FLASH_MAIN_MEM_ADDRESS(PCROPStartAddr));
EricLew 0:80ee8f3b695e 775 assert_param(IS_FLASH_MAIN_MEM_ADDRESS(PCROPEndAddr));
EricLew 0:80ee8f3b695e 776
EricLew 0:80ee8f3b695e 777 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 778 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 779
EricLew 0:80ee8f3b695e 780 if(status == HAL_OK)
EricLew 0:80ee8f3b695e 781 {
EricLew 0:80ee8f3b695e 782 /* Get the information about the bank swapping */
EricLew 0:80ee8f3b695e 783 if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0)
EricLew 0:80ee8f3b695e 784 {
EricLew 0:80ee8f3b695e 785 bank1_addr = FLASH_BASE;
EricLew 0:80ee8f3b695e 786 bank2_addr = FLASH_BASE + FLASH_BANK_SIZE;
EricLew 0:80ee8f3b695e 787 }
EricLew 0:80ee8f3b695e 788 else
EricLew 0:80ee8f3b695e 789 {
EricLew 0:80ee8f3b695e 790 bank1_addr = FLASH_BASE + FLASH_BANK_SIZE;
EricLew 0:80ee8f3b695e 791 bank2_addr = FLASH_BASE;
EricLew 0:80ee8f3b695e 792 }
EricLew 0:80ee8f3b695e 793
EricLew 0:80ee8f3b695e 794 /* Configure the Proprietary code readout protection */
EricLew 0:80ee8f3b695e 795 if((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_1)
EricLew 0:80ee8f3b695e 796 {
EricLew 0:80ee8f3b695e 797 reg_value = ((PCROPStartAddr - bank1_addr) >> 3);
EricLew 0:80ee8f3b695e 798 MODIFY_REG(FLASH->PCROP1SR, FLASH_PCROP1SR_PCROP1_STRT, reg_value);
EricLew 0:80ee8f3b695e 799
EricLew 0:80ee8f3b695e 800 reg_value = ((PCROPEndAddr - bank1_addr) >> 3);
EricLew 0:80ee8f3b695e 801 MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP1_END, reg_value);
EricLew 0:80ee8f3b695e 802 }
EricLew 0:80ee8f3b695e 803 else if((PCROPConfig & FLASH_BANK_BOTH) == FLASH_BANK_2)
EricLew 0:80ee8f3b695e 804 {
EricLew 0:80ee8f3b695e 805 reg_value = ((PCROPStartAddr - bank2_addr) >> 3);
EricLew 0:80ee8f3b695e 806 MODIFY_REG(FLASH->PCROP2SR, FLASH_PCROP2SR_PCROP2_STRT, reg_value);
EricLew 0:80ee8f3b695e 807
EricLew 0:80ee8f3b695e 808 reg_value = ((PCROPEndAddr - bank2_addr) >> 3);
EricLew 0:80ee8f3b695e 809 MODIFY_REG(FLASH->PCROP2ER, FLASH_PCROP2ER_PCROP2_END, reg_value);
EricLew 0:80ee8f3b695e 810 }
EricLew 0:80ee8f3b695e 811
EricLew 0:80ee8f3b695e 812 MODIFY_REG(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP_RDP, (PCROPConfig & FLASH_PCROP1ER_PCROP_RDP));
EricLew 0:80ee8f3b695e 813
EricLew 0:80ee8f3b695e 814 /* Set OPTSTRT Bit */
EricLew 0:80ee8f3b695e 815 SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 816
EricLew 0:80ee8f3b695e 817 /* Wait for last operation to be completed */
EricLew 0:80ee8f3b695e 818 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
EricLew 0:80ee8f3b695e 819
EricLew 0:80ee8f3b695e 820 /* If the option byte program operation is completed, disable the OPTSTRT Bit */
EricLew 0:80ee8f3b695e 821 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
EricLew 0:80ee8f3b695e 822 }
EricLew 0:80ee8f3b695e 823
EricLew 0:80ee8f3b695e 824 return status;
EricLew 0:80ee8f3b695e 825 }
EricLew 0:80ee8f3b695e 826
EricLew 0:80ee8f3b695e 827 /**
EricLew 0:80ee8f3b695e 828 * @brief Return the FLASH Write Protection Option Bytes value.
EricLew 0:80ee8f3b695e 829 *
EricLew 0:80ee8f3b695e 830 * @param[in] WRPArea: specifies the area to be returned.
EricLew 0:80ee8f3b695e 831 * This parameter can be one of the following values:
EricLew 0:80ee8f3b695e 832 * @arg OB_WRPAREA_BANK1_AREAA: Flash Bank 1 Area A
EricLew 0:80ee8f3b695e 833 * @arg OB_WRPAREA_BANK1_AREAB: Flash Bank 1 Area B
EricLew 0:80ee8f3b695e 834 * @arg OB_WRPAREA_BANK2_AREAA: Flash Bank 2 Area A
EricLew 0:80ee8f3b695e 835 * @arg OB_WRPAREA_BANK2_AREAB: Flash Bank 2 Area B
EricLew 0:80ee8f3b695e 836 *
EricLew 0:80ee8f3b695e 837 * @param[out] WRPStartOffset: specifies the address where to copied the start page
EricLew 0:80ee8f3b695e 838 * of the write protected area
EricLew 0:80ee8f3b695e 839 *
EricLew 0:80ee8f3b695e 840 * @param[out] WRDPEndOffset: specifies the address where to copied the end page of
EricLew 0:80ee8f3b695e 841 * the write protected area
EricLew 0:80ee8f3b695e 842 *
EricLew 0:80ee8f3b695e 843 * @retval None
EricLew 0:80ee8f3b695e 844 */
EricLew 0:80ee8f3b695e 845 static void FLASH_OB_GetWRP(uint32_t WRPArea, uint32_t * WRPStartOffset, uint32_t * WRDPEndOffset)
EricLew 0:80ee8f3b695e 846 {
EricLew 0:80ee8f3b695e 847 /* Check the parameters */
EricLew 0:80ee8f3b695e 848 assert_param(IS_OB_WRPAREA(WRPArea));
EricLew 0:80ee8f3b695e 849
EricLew 0:80ee8f3b695e 850 /* Get the configuration of the write protected area */
EricLew 0:80ee8f3b695e 851 if(WRPArea == OB_WRPAREA_BANK1_AREAA)
EricLew 0:80ee8f3b695e 852 {
EricLew 0:80ee8f3b695e 853 *WRPStartOffset = READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_STRT);
EricLew 0:80ee8f3b695e 854 *WRDPEndOffset = (READ_BIT(FLASH->WRP1AR, FLASH_WRP1AR_WRP1A_END) >> 16);
EricLew 0:80ee8f3b695e 855 }
EricLew 0:80ee8f3b695e 856 else if(WRPArea == OB_WRPAREA_BANK1_AREAB)
EricLew 0:80ee8f3b695e 857 {
EricLew 0:80ee8f3b695e 858 *WRPStartOffset = READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_STRT);
EricLew 0:80ee8f3b695e 859 *WRDPEndOffset = (READ_BIT(FLASH->WRP1BR, FLASH_WRP1BR_WRP1B_END) >> 16);
EricLew 0:80ee8f3b695e 860 }
EricLew 0:80ee8f3b695e 861 else if(WRPArea == OB_WRPAREA_BANK2_AREAA)
EricLew 0:80ee8f3b695e 862 {
EricLew 0:80ee8f3b695e 863 *WRPStartOffset = READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_STRT);
EricLew 0:80ee8f3b695e 864 *WRDPEndOffset = (READ_BIT(FLASH->WRP2AR, FLASH_WRP2AR_WRP2A_END) >> 16);
EricLew 0:80ee8f3b695e 865 }
EricLew 0:80ee8f3b695e 866 else if(WRPArea == OB_WRPAREA_BANK2_AREAB)
EricLew 0:80ee8f3b695e 867 {
EricLew 0:80ee8f3b695e 868 *WRPStartOffset = READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_STRT);
EricLew 0:80ee8f3b695e 869 *WRDPEndOffset = (READ_BIT(FLASH->WRP2BR, FLASH_WRP2BR_WRP2B_END) >> 16);
EricLew 0:80ee8f3b695e 870 }
EricLew 0:80ee8f3b695e 871 }
EricLew 0:80ee8f3b695e 872
EricLew 0:80ee8f3b695e 873 /**
EricLew 0:80ee8f3b695e 874 * @brief Return the FLASH Read Protection level.
EricLew 0:80ee8f3b695e 875 * @retval FLASH ReadOut Protection Status:
EricLew 0:80ee8f3b695e 876 * This return value can be one of the following values:
EricLew 0:80ee8f3b695e 877 * @arg OB_RDP_LEVEL_0: No protection
EricLew 0:80ee8f3b695e 878 * @arg OB_RDP_LEVEL_1: Read protection of the memory
EricLew 0:80ee8f3b695e 879 * @arg OB_RDP_LEVEL_2: Full chip protection
EricLew 0:80ee8f3b695e 880 */
EricLew 0:80ee8f3b695e 881 static uint32_t FLASH_OB_GetRDP(void)
EricLew 0:80ee8f3b695e 882 {
EricLew 0:80ee8f3b695e 883 if ((READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP) != OB_RDP_LEVEL_0) &&
EricLew 0:80ee8f3b695e 884 (READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP) != OB_RDP_LEVEL_2))
EricLew 0:80ee8f3b695e 885 {
EricLew 0:80ee8f3b695e 886 return (OB_RDP_LEVEL_1);
EricLew 0:80ee8f3b695e 887 }
EricLew 0:80ee8f3b695e 888 else
EricLew 0:80ee8f3b695e 889 {
EricLew 0:80ee8f3b695e 890 return (READ_BIT(FLASH->OPTR, FLASH_OPTR_RDP));
EricLew 0:80ee8f3b695e 891 }
EricLew 0:80ee8f3b695e 892 }
EricLew 0:80ee8f3b695e 893
EricLew 0:80ee8f3b695e 894 /**
EricLew 0:80ee8f3b695e 895 * @brief Return the FLASH User Option Byte value.
EricLew 0:80ee8f3b695e 896 * @retval The FLASH User Option Bytes values:
EricLew 0:80ee8f3b695e 897 * BOR_LEV(Bit8-10), nRST_STOP(Bit12), nRST_STDBY(Bit13), nRST_SHDW(Bit14),
EricLew 0:80ee8f3b695e 898 * IWDG_SW(Bit16), IWDG_STOP(Bit17), IWDG_STDBY(Bit18), WWDG_SW(Bit19),
EricLew 0:80ee8f3b695e 899 * BFB2(Bit20), DUALBANK(Bit21), nBOOT1(Bit23), SRAM2_PE(Bit24) and SRAM2_RST(Bit25).
EricLew 0:80ee8f3b695e 900 */
EricLew 0:80ee8f3b695e 901 static uint32_t FLASH_OB_GetUser(void)
EricLew 0:80ee8f3b695e 902 {
EricLew 0:80ee8f3b695e 903 uint32_t user_config = READ_REG(FLASH->OPTR);
EricLew 0:80ee8f3b695e 904 CLEAR_BIT(user_config, FLASH_OPTR_RDP);
EricLew 0:80ee8f3b695e 905
EricLew 0:80ee8f3b695e 906 return user_config;
EricLew 0:80ee8f3b695e 907 }
EricLew 0:80ee8f3b695e 908
EricLew 0:80ee8f3b695e 909 /**
EricLew 0:80ee8f3b695e 910 * @brief Return the FLASH Write Protection Option Bytes value.
EricLew 0:80ee8f3b695e 911 *
EricLew 0:80ee8f3b695e 912 * @param PCROPConfig [inout]: specifies the configuration (Bank to be configured and PCROP_RDP option).
EricLew 0:80ee8f3b695e 913 * This parameter must be a combination of FLASH_BANK_1 or FLASH_BANK_2
EricLew 0:80ee8f3b695e 914 * with OB_PCROP_RDP_NOT_ERASE or OB_PCROP_RDP_ERASE
EricLew 0:80ee8f3b695e 915 *
EricLew 0:80ee8f3b695e 916 * @param PCROPStartAddr [out]: specifies the address where to copied the start address
EricLew 0:80ee8f3b695e 917 * of the Proprietary code readout protection
EricLew 0:80ee8f3b695e 918 *
EricLew 0:80ee8f3b695e 919 * @param PCROPEndAddr [out]: specifies the address where to copied the end address of
EricLew 0:80ee8f3b695e 920 * the Proprietary code readout protection
EricLew 0:80ee8f3b695e 921 *
EricLew 0:80ee8f3b695e 922 * @retval None
EricLew 0:80ee8f3b695e 923 */
EricLew 0:80ee8f3b695e 924 static void FLASH_OB_GetPCROP(uint32_t * PCROPConfig, uint32_t * PCROPStartAddr, uint32_t * PCROPEndAddr)
EricLew 0:80ee8f3b695e 925 {
EricLew 0:80ee8f3b695e 926 uint32_t reg_value = 0;
EricLew 0:80ee8f3b695e 927 uint32_t bank1_addr, bank2_addr;
EricLew 0:80ee8f3b695e 928
EricLew 0:80ee8f3b695e 929 /* Check the parameters */
EricLew 0:80ee8f3b695e 930 assert_param(IS_FLASH_BANK_EXCLUSIVE((*PCROPConfig) & FLASH_BANK_BOTH));
EricLew 0:80ee8f3b695e 931
EricLew 0:80ee8f3b695e 932 /* Get the information about the bank swapping */
EricLew 0:80ee8f3b695e 933 if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0)
EricLew 0:80ee8f3b695e 934 {
EricLew 0:80ee8f3b695e 935 bank1_addr = FLASH_BASE;
EricLew 0:80ee8f3b695e 936 bank2_addr = FLASH_BASE + FLASH_BANK_SIZE;
EricLew 0:80ee8f3b695e 937 }
EricLew 0:80ee8f3b695e 938 else
EricLew 0:80ee8f3b695e 939 {
EricLew 0:80ee8f3b695e 940 bank1_addr = FLASH_BASE + FLASH_BANK_SIZE;
EricLew 0:80ee8f3b695e 941 bank2_addr = FLASH_BASE;
EricLew 0:80ee8f3b695e 942 }
EricLew 0:80ee8f3b695e 943
EricLew 0:80ee8f3b695e 944 if(((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_1)
EricLew 0:80ee8f3b695e 945 {
EricLew 0:80ee8f3b695e 946 reg_value = (READ_REG(FLASH->PCROP1SR) & FLASH_PCROP1SR_PCROP1_STRT);
EricLew 0:80ee8f3b695e 947 *PCROPStartAddr = (reg_value << 3) + bank1_addr;
EricLew 0:80ee8f3b695e 948
EricLew 0:80ee8f3b695e 949 reg_value = (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP1_END);
EricLew 0:80ee8f3b695e 950 *PCROPEndAddr = (reg_value << 3) + bank1_addr;
EricLew 0:80ee8f3b695e 951 }
EricLew 0:80ee8f3b695e 952 else if(((*PCROPConfig) & FLASH_BANK_BOTH) == FLASH_BANK_2)
EricLew 0:80ee8f3b695e 953 {
EricLew 0:80ee8f3b695e 954 reg_value = (READ_REG(FLASH->PCROP2SR) & FLASH_PCROP2SR_PCROP2_STRT);
EricLew 0:80ee8f3b695e 955 *PCROPStartAddr = (reg_value << 3) + bank2_addr;
EricLew 0:80ee8f3b695e 956
EricLew 0:80ee8f3b695e 957 reg_value = (READ_REG(FLASH->PCROP2ER) & FLASH_PCROP2ER_PCROP2_END);
EricLew 0:80ee8f3b695e 958 *PCROPEndAddr = (reg_value << 3) + bank2_addr;
EricLew 0:80ee8f3b695e 959 }
EricLew 0:80ee8f3b695e 960
EricLew 0:80ee8f3b695e 961 *PCROPConfig |= (READ_REG(FLASH->PCROP1ER) & FLASH_PCROP1ER_PCROP_RDP);
EricLew 0:80ee8f3b695e 962 }
EricLew 0:80ee8f3b695e 963 /**
EricLew 0:80ee8f3b695e 964 * @}
EricLew 0:80ee8f3b695e 965 */
EricLew 0:80ee8f3b695e 966
EricLew 0:80ee8f3b695e 967 /**
EricLew 0:80ee8f3b695e 968 * @}
EricLew 0:80ee8f3b695e 969 */
EricLew 0:80ee8f3b695e 970
EricLew 0:80ee8f3b695e 971 #endif /* HAL_FLASH_MODULE_ENABLED */
EricLew 0:80ee8f3b695e 972
EricLew 0:80ee8f3b695e 973 /**
EricLew 0:80ee8f3b695e 974 * @}
EricLew 0:80ee8f3b695e 975 */
EricLew 0:80ee8f3b695e 976
EricLew 0:80ee8f3b695e 977 /**
EricLew 0:80ee8f3b695e 978 * @}
EricLew 0:80ee8f3b695e 979 */
EricLew 0:80ee8f3b695e 980
EricLew 0:80ee8f3b695e 981 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
EricLew 0:80ee8f3b695e 982