Hal Drivers for L4

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

Committer:
EricLew
Date:
Mon Nov 02 19:37:23 2015 +0000
Revision:
0:80ee8f3b695e
Child:
1:271ce2251692
Errors are with definitions of LCD and QSPI functions. I believe all .h and .c files are  uploaded, but there may need to be certain functions called.

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