Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 3 * @file stm32f2xx_hal_flash_ex.c
sahilmgandhi 18:6a4db94011d3 4 * @author MCD Application Team
sahilmgandhi 18:6a4db94011d3 5 * @version V1.1.3
sahilmgandhi 18:6a4db94011d3 6 * @date 29-June-2016
sahilmgandhi 18:6a4db94011d3 7 * @brief Extended FLASH HAL module driver.
sahilmgandhi 18:6a4db94011d3 8 * This file provides firmware functions to manage the following
sahilmgandhi 18:6a4db94011d3 9 * functionalities of the FLASH extension peripheral:
sahilmgandhi 18:6a4db94011d3 10 * + Extended programming operations functions
sahilmgandhi 18:6a4db94011d3 11 *
sahilmgandhi 18:6a4db94011d3 12 @verbatim
sahilmgandhi 18:6a4db94011d3 13 ==============================================================================
sahilmgandhi 18:6a4db94011d3 14 ##### Flash Extension features #####
sahilmgandhi 18:6a4db94011d3 15 ==============================================================================
sahilmgandhi 18:6a4db94011d3 16
sahilmgandhi 18:6a4db94011d3 17 ##### How to use this driver #####
sahilmgandhi 18:6a4db94011d3 18 ==============================================================================
sahilmgandhi 18:6a4db94011d3 19 [..] This driver provides functions to configure and program the FLASH memory
sahilmgandhi 18:6a4db94011d3 20 of all STM32F2xx devices. It includes
sahilmgandhi 18:6a4db94011d3 21 (#) FLASH Memory Erase functions:
sahilmgandhi 18:6a4db94011d3 22 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
sahilmgandhi 18:6a4db94011d3 23 HAL_FLASH_Lock() functions
sahilmgandhi 18:6a4db94011d3 24 (++) Erase function: Erase sector, erase all sectors
sahilmgandhi 18:6a4db94011d3 25 (++) There are two modes of erase :
sahilmgandhi 18:6a4db94011d3 26 (+++) Polling Mode using HAL_FLASHEx_Erase()
sahilmgandhi 18:6a4db94011d3 27 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
sahilmgandhi 18:6a4db94011d3 28
sahilmgandhi 18:6a4db94011d3 29 (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
sahilmgandhi 18:6a4db94011d3 30 (++) Set/Reset the write protection
sahilmgandhi 18:6a4db94011d3 31 (++) Set the Read protection Level
sahilmgandhi 18:6a4db94011d3 32 (++) Set the BOR level
sahilmgandhi 18:6a4db94011d3 33 (++) Program the user Option Bytes
sahilmgandhi 18:6a4db94011d3 34
sahilmgandhi 18:6a4db94011d3 35 @endverbatim
sahilmgandhi 18:6a4db94011d3 36 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 37 * @attention
sahilmgandhi 18:6a4db94011d3 38 *
sahilmgandhi 18:6a4db94011d3 39 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
sahilmgandhi 18:6a4db94011d3 40 *
sahilmgandhi 18:6a4db94011d3 41 * Redistribution and use in source and binary forms, with or without modification,
sahilmgandhi 18:6a4db94011d3 42 * are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 43 * 1. Redistributions of source code must retain the above copyright notice,
sahilmgandhi 18:6a4db94011d3 44 * this list of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 45 * 2. Redistributions in binary form must reproduce the above copyright notice,
sahilmgandhi 18:6a4db94011d3 46 * this list of conditions and the following disclaimer in the documentation
sahilmgandhi 18:6a4db94011d3 47 * and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 48 * 3. Neither the name of STMicroelectronics nor the names of its contributors
sahilmgandhi 18:6a4db94011d3 49 * may be used to endorse or promote products derived from this software
sahilmgandhi 18:6a4db94011d3 50 * without specific prior written permission.
sahilmgandhi 18:6a4db94011d3 51 *
sahilmgandhi 18:6a4db94011d3 52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
sahilmgandhi 18:6a4db94011d3 53 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sahilmgandhi 18:6a4db94011d3 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sahilmgandhi 18:6a4db94011d3 55 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
sahilmgandhi 18:6a4db94011d3 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sahilmgandhi 18:6a4db94011d3 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sahilmgandhi 18:6a4db94011d3 58 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
sahilmgandhi 18:6a4db94011d3 59 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
sahilmgandhi 18:6a4db94011d3 60 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
sahilmgandhi 18:6a4db94011d3 61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 62 *
sahilmgandhi 18:6a4db94011d3 63 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 64 */
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 /* Includes ------------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 67 #include "stm32f2xx_hal.h"
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 /** @addtogroup STM32F2xx_HAL_Driver
sahilmgandhi 18:6a4db94011d3 70 * @{
sahilmgandhi 18:6a4db94011d3 71 */
sahilmgandhi 18:6a4db94011d3 72
sahilmgandhi 18:6a4db94011d3 73 /** @defgroup FLASHEx FLASHEx
sahilmgandhi 18:6a4db94011d3 74 * @brief FLASH HAL Extension module driver
sahilmgandhi 18:6a4db94011d3 75 * @{
sahilmgandhi 18:6a4db94011d3 76 */
sahilmgandhi 18:6a4db94011d3 77
sahilmgandhi 18:6a4db94011d3 78 #ifdef HAL_FLASH_MODULE_ENABLED
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 /* Private typedef -----------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 81 /* Private define ------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 82 /** @addtogroup FLASHEx_Private_Constants
sahilmgandhi 18:6a4db94011d3 83 * @{
sahilmgandhi 18:6a4db94011d3 84 */
sahilmgandhi 18:6a4db94011d3 85 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000U)/* 50 s */
sahilmgandhi 18:6a4db94011d3 86 /**
sahilmgandhi 18:6a4db94011d3 87 * @}
sahilmgandhi 18:6a4db94011d3 88 */
sahilmgandhi 18:6a4db94011d3 89
sahilmgandhi 18:6a4db94011d3 90 /* Private macro -------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 91 /* Private variables ---------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 92 /** @addtogroup FLASHEx_Private_Variables
sahilmgandhi 18:6a4db94011d3 93 * @{
sahilmgandhi 18:6a4db94011d3 94 */
sahilmgandhi 18:6a4db94011d3 95 extern FLASH_ProcessTypeDef pFlash;
sahilmgandhi 18:6a4db94011d3 96 /**
sahilmgandhi 18:6a4db94011d3 97 * @}
sahilmgandhi 18:6a4db94011d3 98 */
sahilmgandhi 18:6a4db94011d3 99
sahilmgandhi 18:6a4db94011d3 100 /* Private function prototypes -----------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 101 /** @addtogroup FLASHEx_Private_Functions
sahilmgandhi 18:6a4db94011d3 102 * @{
sahilmgandhi 18:6a4db94011d3 103 */
sahilmgandhi 18:6a4db94011d3 104 /* Option bytes control */
sahilmgandhi 18:6a4db94011d3 105 static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks);
sahilmgandhi 18:6a4db94011d3 106 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
sahilmgandhi 18:6a4db94011d3 107 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks);
sahilmgandhi 18:6a4db94011d3 108 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level);
sahilmgandhi 18:6a4db94011d3 109 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby);
sahilmgandhi 18:6a4db94011d3 110 static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level);
sahilmgandhi 18:6a4db94011d3 111 static uint8_t FLASH_OB_GetUser(void);
sahilmgandhi 18:6a4db94011d3 112 static uint16_t FLASH_OB_GetWRP(void);
sahilmgandhi 18:6a4db94011d3 113 static uint8_t FLASH_OB_GetRDP(void);
sahilmgandhi 18:6a4db94011d3 114 static uint8_t FLASH_OB_GetBOR(void);
sahilmgandhi 18:6a4db94011d3 115
sahilmgandhi 18:6a4db94011d3 116 extern HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
sahilmgandhi 18:6a4db94011d3 117 /**
sahilmgandhi 18:6a4db94011d3 118 * @}
sahilmgandhi 18:6a4db94011d3 119 */
sahilmgandhi 18:6a4db94011d3 120
sahilmgandhi 18:6a4db94011d3 121 /* Exported functions --------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 122 /** @defgroup FLASHEx_Exported_Functions FLASH Exported Functions
sahilmgandhi 18:6a4db94011d3 123 * @{
sahilmgandhi 18:6a4db94011d3 124 */
sahilmgandhi 18:6a4db94011d3 125
sahilmgandhi 18:6a4db94011d3 126 /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
sahilmgandhi 18:6a4db94011d3 127 * @brief Extended IO operation functions
sahilmgandhi 18:6a4db94011d3 128 *
sahilmgandhi 18:6a4db94011d3 129 @verbatim
sahilmgandhi 18:6a4db94011d3 130 ===============================================================================
sahilmgandhi 18:6a4db94011d3 131 ##### Extended programming operation functions #####
sahilmgandhi 18:6a4db94011d3 132 ===============================================================================
sahilmgandhi 18:6a4db94011d3 133 [..]
sahilmgandhi 18:6a4db94011d3 134 This subsection provides a set of functions allowing to manage the Extension FLASH
sahilmgandhi 18:6a4db94011d3 135 programming operations.
sahilmgandhi 18:6a4db94011d3 136
sahilmgandhi 18:6a4db94011d3 137 @endverbatim
sahilmgandhi 18:6a4db94011d3 138 * @{
sahilmgandhi 18:6a4db94011d3 139 */
sahilmgandhi 18:6a4db94011d3 140 /**
sahilmgandhi 18:6a4db94011d3 141 * @brief Perform a mass erase or erase the specified FLASH memory sectors
sahilmgandhi 18:6a4db94011d3 142 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 143 * contains the configuration information for the erasing.
sahilmgandhi 18:6a4db94011d3 144 *
sahilmgandhi 18:6a4db94011d3 145 * @param[out] SectorError: pointer to variable that
sahilmgandhi 18:6a4db94011d3 146 * contains the configuration information on faulty sector in case of error
sahilmgandhi 18:6a4db94011d3 147 * (0xFFFFFFFF means that all the sectors have been correctly erased)
sahilmgandhi 18:6a4db94011d3 148 *
sahilmgandhi 18:6a4db94011d3 149 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 150 */
sahilmgandhi 18:6a4db94011d3 151 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
sahilmgandhi 18:6a4db94011d3 152 {
sahilmgandhi 18:6a4db94011d3 153 HAL_StatusTypeDef status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 154 uint32_t index = 0U;
sahilmgandhi 18:6a4db94011d3 155
sahilmgandhi 18:6a4db94011d3 156 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 157 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 158
sahilmgandhi 18:6a4db94011d3 159 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 160 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
sahilmgandhi 18:6a4db94011d3 161
sahilmgandhi 18:6a4db94011d3 162 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 163 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 164
sahilmgandhi 18:6a4db94011d3 165 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 166 {
sahilmgandhi 18:6a4db94011d3 167 /*Initialization of SectorError variable*/
sahilmgandhi 18:6a4db94011d3 168 *SectorError = 0xFFFFFFFFU;
sahilmgandhi 18:6a4db94011d3 169
sahilmgandhi 18:6a4db94011d3 170 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
sahilmgandhi 18:6a4db94011d3 171 {
sahilmgandhi 18:6a4db94011d3 172 /*Mass erase to be done*/
sahilmgandhi 18:6a4db94011d3 173 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
sahilmgandhi 18:6a4db94011d3 174
sahilmgandhi 18:6a4db94011d3 175 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 176 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 177
sahilmgandhi 18:6a4db94011d3 178 /* if the erase operation is completed, disable the MER Bit */
sahilmgandhi 18:6a4db94011d3 179 FLASH->CR &= (~FLASH_MER_BIT);
sahilmgandhi 18:6a4db94011d3 180 }
sahilmgandhi 18:6a4db94011d3 181 else
sahilmgandhi 18:6a4db94011d3 182 {
sahilmgandhi 18:6a4db94011d3 183 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 184 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
sahilmgandhi 18:6a4db94011d3 185
sahilmgandhi 18:6a4db94011d3 186 /* Erase by sector by sector to be done*/
sahilmgandhi 18:6a4db94011d3 187 for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
sahilmgandhi 18:6a4db94011d3 188 {
sahilmgandhi 18:6a4db94011d3 189 FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);
sahilmgandhi 18:6a4db94011d3 190
sahilmgandhi 18:6a4db94011d3 191 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 192 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 193
sahilmgandhi 18:6a4db94011d3 194 /* If the erase operation is completed, disable the SER and SNB Bits */
sahilmgandhi 18:6a4db94011d3 195 CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
sahilmgandhi 18:6a4db94011d3 196
sahilmgandhi 18:6a4db94011d3 197 if(status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 198 {
sahilmgandhi 18:6a4db94011d3 199 /* In case of error, stop erase procedure and return the faulty sector*/
sahilmgandhi 18:6a4db94011d3 200 *SectorError = index;
sahilmgandhi 18:6a4db94011d3 201 break;
sahilmgandhi 18:6a4db94011d3 202 }
sahilmgandhi 18:6a4db94011d3 203 }
sahilmgandhi 18:6a4db94011d3 204 }
sahilmgandhi 18:6a4db94011d3 205 /* Flush the caches to be sure of the data consistency */
sahilmgandhi 18:6a4db94011d3 206 FLASH_FlushCaches();
sahilmgandhi 18:6a4db94011d3 207 }
sahilmgandhi 18:6a4db94011d3 208
sahilmgandhi 18:6a4db94011d3 209 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 210 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 211
sahilmgandhi 18:6a4db94011d3 212 return status;
sahilmgandhi 18:6a4db94011d3 213 }
sahilmgandhi 18:6a4db94011d3 214
sahilmgandhi 18:6a4db94011d3 215 /**
sahilmgandhi 18:6a4db94011d3 216 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
sahilmgandhi 18:6a4db94011d3 217 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 218 * contains the configuration information for the erasing.
sahilmgandhi 18:6a4db94011d3 219 *
sahilmgandhi 18:6a4db94011d3 220 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 221 */
sahilmgandhi 18:6a4db94011d3 222 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
sahilmgandhi 18:6a4db94011d3 223 {
sahilmgandhi 18:6a4db94011d3 224 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 225
sahilmgandhi 18:6a4db94011d3 226 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 227 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 228
sahilmgandhi 18:6a4db94011d3 229 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 230 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
sahilmgandhi 18:6a4db94011d3 231
sahilmgandhi 18:6a4db94011d3 232 /* Enable End of FLASH Operation interrupt */
sahilmgandhi 18:6a4db94011d3 233 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
sahilmgandhi 18:6a4db94011d3 234
sahilmgandhi 18:6a4db94011d3 235 /* Enable Error source interrupt */
sahilmgandhi 18:6a4db94011d3 236 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
sahilmgandhi 18:6a4db94011d3 237
sahilmgandhi 18:6a4db94011d3 238 /* Clear pending flags (if any) */
sahilmgandhi 18:6a4db94011d3 239 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
sahilmgandhi 18:6a4db94011d3 240 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
sahilmgandhi 18:6a4db94011d3 241
sahilmgandhi 18:6a4db94011d3 242 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
sahilmgandhi 18:6a4db94011d3 243 {
sahilmgandhi 18:6a4db94011d3 244 /*Mass erase to be done*/
sahilmgandhi 18:6a4db94011d3 245 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
sahilmgandhi 18:6a4db94011d3 246 pFlash.Bank = pEraseInit->Banks;
sahilmgandhi 18:6a4db94011d3 247 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
sahilmgandhi 18:6a4db94011d3 248 }
sahilmgandhi 18:6a4db94011d3 249 else
sahilmgandhi 18:6a4db94011d3 250 {
sahilmgandhi 18:6a4db94011d3 251 /* Erase by sector to be done*/
sahilmgandhi 18:6a4db94011d3 252
sahilmgandhi 18:6a4db94011d3 253 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 254 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
sahilmgandhi 18:6a4db94011d3 255
sahilmgandhi 18:6a4db94011d3 256 pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
sahilmgandhi 18:6a4db94011d3 257 pFlash.NbSectorsToErase = pEraseInit->NbSectors;
sahilmgandhi 18:6a4db94011d3 258 pFlash.Sector = pEraseInit->Sector;
sahilmgandhi 18:6a4db94011d3 259 pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;
sahilmgandhi 18:6a4db94011d3 260
sahilmgandhi 18:6a4db94011d3 261 /*Erase 1st sector and wait for IT*/
sahilmgandhi 18:6a4db94011d3 262 FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
sahilmgandhi 18:6a4db94011d3 263 }
sahilmgandhi 18:6a4db94011d3 264
sahilmgandhi 18:6a4db94011d3 265 return status;
sahilmgandhi 18:6a4db94011d3 266 }
sahilmgandhi 18:6a4db94011d3 267
sahilmgandhi 18:6a4db94011d3 268 /**
sahilmgandhi 18:6a4db94011d3 269 * @brief Program option bytes
sahilmgandhi 18:6a4db94011d3 270 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
sahilmgandhi 18:6a4db94011d3 271 * contains the configuration information for the programming.
sahilmgandhi 18:6a4db94011d3 272 *
sahilmgandhi 18:6a4db94011d3 273 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 274 */
sahilmgandhi 18:6a4db94011d3 275 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
sahilmgandhi 18:6a4db94011d3 276 {
sahilmgandhi 18:6a4db94011d3 277 HAL_StatusTypeDef status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 278
sahilmgandhi 18:6a4db94011d3 279 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 280 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 281
sahilmgandhi 18:6a4db94011d3 282 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 283 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
sahilmgandhi 18:6a4db94011d3 284
sahilmgandhi 18:6a4db94011d3 285 /*Write protection configuration*/
sahilmgandhi 18:6a4db94011d3 286 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
sahilmgandhi 18:6a4db94011d3 287 {
sahilmgandhi 18:6a4db94011d3 288 assert_param(IS_WRPSTATE(pOBInit->WRPState));
sahilmgandhi 18:6a4db94011d3 289 if(pOBInit->WRPState == OB_WRPSTATE_ENABLE)
sahilmgandhi 18:6a4db94011d3 290 {
sahilmgandhi 18:6a4db94011d3 291 /*Enable of Write protection on the selected Sector*/
sahilmgandhi 18:6a4db94011d3 292 status = FLASH_OB_EnableWRP(pOBInit->WRPSector, pOBInit->Banks);
sahilmgandhi 18:6a4db94011d3 293 }
sahilmgandhi 18:6a4db94011d3 294 else
sahilmgandhi 18:6a4db94011d3 295 {
sahilmgandhi 18:6a4db94011d3 296 /*Disable of Write protection on the selected Sector*/
sahilmgandhi 18:6a4db94011d3 297 status = FLASH_OB_DisableWRP(pOBInit->WRPSector, pOBInit->Banks);
sahilmgandhi 18:6a4db94011d3 298 }
sahilmgandhi 18:6a4db94011d3 299 }
sahilmgandhi 18:6a4db94011d3 300
sahilmgandhi 18:6a4db94011d3 301 /*Read protection configuration*/
sahilmgandhi 18:6a4db94011d3 302 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
sahilmgandhi 18:6a4db94011d3 303 {
sahilmgandhi 18:6a4db94011d3 304 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel);
sahilmgandhi 18:6a4db94011d3 305 }
sahilmgandhi 18:6a4db94011d3 306
sahilmgandhi 18:6a4db94011d3 307 /*USER configuration*/
sahilmgandhi 18:6a4db94011d3 308 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
sahilmgandhi 18:6a4db94011d3 309 {
sahilmgandhi 18:6a4db94011d3 310 status = FLASH_OB_UserConfig(pOBInit->USERConfig&OB_IWDG_SW,
sahilmgandhi 18:6a4db94011d3 311 pOBInit->USERConfig&OB_STOP_NO_RST,
sahilmgandhi 18:6a4db94011d3 312 pOBInit->USERConfig&OB_STDBY_NO_RST);
sahilmgandhi 18:6a4db94011d3 313 }
sahilmgandhi 18:6a4db94011d3 314
sahilmgandhi 18:6a4db94011d3 315 /*BOR Level configuration*/
sahilmgandhi 18:6a4db94011d3 316 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
sahilmgandhi 18:6a4db94011d3 317 {
sahilmgandhi 18:6a4db94011d3 318 status = FLASH_OB_BOR_LevelConfig(pOBInit->BORLevel);
sahilmgandhi 18:6a4db94011d3 319 }
sahilmgandhi 18:6a4db94011d3 320
sahilmgandhi 18:6a4db94011d3 321 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 322 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 323
sahilmgandhi 18:6a4db94011d3 324 return status;
sahilmgandhi 18:6a4db94011d3 325 }
sahilmgandhi 18:6a4db94011d3 326
sahilmgandhi 18:6a4db94011d3 327 /**
sahilmgandhi 18:6a4db94011d3 328 * @brief Get the Option byte configuration
sahilmgandhi 18:6a4db94011d3 329 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
sahilmgandhi 18:6a4db94011d3 330 * contains the configuration information for the programming.
sahilmgandhi 18:6a4db94011d3 331 *
sahilmgandhi 18:6a4db94011d3 332 * @retval None
sahilmgandhi 18:6a4db94011d3 333 */
sahilmgandhi 18:6a4db94011d3 334 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
sahilmgandhi 18:6a4db94011d3 335 {
sahilmgandhi 18:6a4db94011d3 336 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
sahilmgandhi 18:6a4db94011d3 337
sahilmgandhi 18:6a4db94011d3 338 /*Get WRP*/
sahilmgandhi 18:6a4db94011d3 339 pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP();
sahilmgandhi 18:6a4db94011d3 340
sahilmgandhi 18:6a4db94011d3 341 /*Get RDP Level*/
sahilmgandhi 18:6a4db94011d3 342 pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP();
sahilmgandhi 18:6a4db94011d3 343
sahilmgandhi 18:6a4db94011d3 344 /*Get USER*/
sahilmgandhi 18:6a4db94011d3 345 pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser();
sahilmgandhi 18:6a4db94011d3 346
sahilmgandhi 18:6a4db94011d3 347 /*Get BOR Level*/
sahilmgandhi 18:6a4db94011d3 348 pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR();
sahilmgandhi 18:6a4db94011d3 349 }
sahilmgandhi 18:6a4db94011d3 350
sahilmgandhi 18:6a4db94011d3 351 /**
sahilmgandhi 18:6a4db94011d3 352 * @}
sahilmgandhi 18:6a4db94011d3 353 */
sahilmgandhi 18:6a4db94011d3 354
sahilmgandhi 18:6a4db94011d3 355 /**
sahilmgandhi 18:6a4db94011d3 356 * @brief Erase the specified FLASH memory sector
sahilmgandhi 18:6a4db94011d3 357 * @param Sector: FLASH sector to erase
sahilmgandhi 18:6a4db94011d3 358 * The value of this parameter depend on device used within the same series
sahilmgandhi 18:6a4db94011d3 359 * @param VoltageRange: The device voltage range which defines the erase parallelism.
sahilmgandhi 18:6a4db94011d3 360 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 361 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
sahilmgandhi 18:6a4db94011d3 362 * the operation will be done by byte (8-bit)
sahilmgandhi 18:6a4db94011d3 363 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
sahilmgandhi 18:6a4db94011d3 364 * the operation will be done by half word (16-bit)
sahilmgandhi 18:6a4db94011d3 365 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
sahilmgandhi 18:6a4db94011d3 366 * the operation will be done by word (32-bit)
sahilmgandhi 18:6a4db94011d3 367 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
sahilmgandhi 18:6a4db94011d3 368 * the operation will be done by double word (64-bit)
sahilmgandhi 18:6a4db94011d3 369 *
sahilmgandhi 18:6a4db94011d3 370 * @retval None
sahilmgandhi 18:6a4db94011d3 371 */
sahilmgandhi 18:6a4db94011d3 372 void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
sahilmgandhi 18:6a4db94011d3 373 {
sahilmgandhi 18:6a4db94011d3 374 uint32_t tmp_psize = 0U;
sahilmgandhi 18:6a4db94011d3 375
sahilmgandhi 18:6a4db94011d3 376 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 377 assert_param(IS_FLASH_SECTOR(Sector));
sahilmgandhi 18:6a4db94011d3 378 assert_param(IS_VOLTAGERANGE(VoltageRange));
sahilmgandhi 18:6a4db94011d3 379
sahilmgandhi 18:6a4db94011d3 380 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
sahilmgandhi 18:6a4db94011d3 381 {
sahilmgandhi 18:6a4db94011d3 382 tmp_psize = FLASH_PSIZE_BYTE;
sahilmgandhi 18:6a4db94011d3 383 }
sahilmgandhi 18:6a4db94011d3 384 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
sahilmgandhi 18:6a4db94011d3 385 {
sahilmgandhi 18:6a4db94011d3 386 tmp_psize = FLASH_PSIZE_HALF_WORD;
sahilmgandhi 18:6a4db94011d3 387 }
sahilmgandhi 18:6a4db94011d3 388 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
sahilmgandhi 18:6a4db94011d3 389 {
sahilmgandhi 18:6a4db94011d3 390 tmp_psize = FLASH_PSIZE_WORD;
sahilmgandhi 18:6a4db94011d3 391 }
sahilmgandhi 18:6a4db94011d3 392 else
sahilmgandhi 18:6a4db94011d3 393 {
sahilmgandhi 18:6a4db94011d3 394 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
sahilmgandhi 18:6a4db94011d3 395 }
sahilmgandhi 18:6a4db94011d3 396
sahilmgandhi 18:6a4db94011d3 397 /* If the previous operation is completed, proceed to erase the sector */
sahilmgandhi 18:6a4db94011d3 398 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
sahilmgandhi 18:6a4db94011d3 399 FLASH->CR |= tmp_psize;
sahilmgandhi 18:6a4db94011d3 400 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
sahilmgandhi 18:6a4db94011d3 401 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
sahilmgandhi 18:6a4db94011d3 402 FLASH->CR |= FLASH_CR_STRT;
sahilmgandhi 18:6a4db94011d3 403 }
sahilmgandhi 18:6a4db94011d3 404
sahilmgandhi 18:6a4db94011d3 405 /**
sahilmgandhi 18:6a4db94011d3 406 * @brief Flush the instruction and data caches
sahilmgandhi 18:6a4db94011d3 407 * @retval None
sahilmgandhi 18:6a4db94011d3 408 */
sahilmgandhi 18:6a4db94011d3 409 void FLASH_FlushCaches(void)
sahilmgandhi 18:6a4db94011d3 410 {
sahilmgandhi 18:6a4db94011d3 411 /* Flush instruction cache */
sahilmgandhi 18:6a4db94011d3 412 if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) != RESET)
sahilmgandhi 18:6a4db94011d3 413 {
sahilmgandhi 18:6a4db94011d3 414 /* Disable instruction cache */
sahilmgandhi 18:6a4db94011d3 415 __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
sahilmgandhi 18:6a4db94011d3 416 /* Reset instruction cache */
sahilmgandhi 18:6a4db94011d3 417 __HAL_FLASH_INSTRUCTION_CACHE_RESET();
sahilmgandhi 18:6a4db94011d3 418 /* Enable instruction cache */
sahilmgandhi 18:6a4db94011d3 419 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
sahilmgandhi 18:6a4db94011d3 420 }
sahilmgandhi 18:6a4db94011d3 421
sahilmgandhi 18:6a4db94011d3 422 /* Flush data cache */
sahilmgandhi 18:6a4db94011d3 423 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != RESET)
sahilmgandhi 18:6a4db94011d3 424 {
sahilmgandhi 18:6a4db94011d3 425 /* Disable data cache */
sahilmgandhi 18:6a4db94011d3 426 __HAL_FLASH_DATA_CACHE_DISABLE();
sahilmgandhi 18:6a4db94011d3 427 /* Reset data cache */
sahilmgandhi 18:6a4db94011d3 428 __HAL_FLASH_DATA_CACHE_RESET();
sahilmgandhi 18:6a4db94011d3 429 /* Enable data cache */
sahilmgandhi 18:6a4db94011d3 430 __HAL_FLASH_DATA_CACHE_ENABLE();
sahilmgandhi 18:6a4db94011d3 431 }
sahilmgandhi 18:6a4db94011d3 432 }
sahilmgandhi 18:6a4db94011d3 433
sahilmgandhi 18:6a4db94011d3 434 /**
sahilmgandhi 18:6a4db94011d3 435 * @brief Mass erase of FLASH memory
sahilmgandhi 18:6a4db94011d3 436 * @param VoltageRange: The device voltage range which defines the erase parallelism.
sahilmgandhi 18:6a4db94011d3 437 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 438 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
sahilmgandhi 18:6a4db94011d3 439 * the operation will be done by byte (8-bit)
sahilmgandhi 18:6a4db94011d3 440 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
sahilmgandhi 18:6a4db94011d3 441 * the operation will be done by half word (16-bit)
sahilmgandhi 18:6a4db94011d3 442 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
sahilmgandhi 18:6a4db94011d3 443 * the operation will be done by word (32-bit)
sahilmgandhi 18:6a4db94011d3 444 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
sahilmgandhi 18:6a4db94011d3 445 * the operation will be done by double word (64-bit)
sahilmgandhi 18:6a4db94011d3 446 *
sahilmgandhi 18:6a4db94011d3 447 * @param Banks: Banks to be erased
sahilmgandhi 18:6a4db94011d3 448 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 449 * @arg FLASH_BANK_1: Bank1 to be erased
sahilmgandhi 18:6a4db94011d3 450 *
sahilmgandhi 18:6a4db94011d3 451 * @retval None
sahilmgandhi 18:6a4db94011d3 452 */
sahilmgandhi 18:6a4db94011d3 453 static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
sahilmgandhi 18:6a4db94011d3 454 {
sahilmgandhi 18:6a4db94011d3 455 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 456 assert_param(IS_VOLTAGERANGE(VoltageRange));
sahilmgandhi 18:6a4db94011d3 457 assert_param(IS_FLASH_BANK(Banks));
sahilmgandhi 18:6a4db94011d3 458
sahilmgandhi 18:6a4db94011d3 459 /* If the previous operation is completed, proceed to erase all sectors */
sahilmgandhi 18:6a4db94011d3 460 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
sahilmgandhi 18:6a4db94011d3 461 FLASH->CR |= FLASH_CR_MER;
sahilmgandhi 18:6a4db94011d3 462 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8U);
sahilmgandhi 18:6a4db94011d3 463 }
sahilmgandhi 18:6a4db94011d3 464
sahilmgandhi 18:6a4db94011d3 465 /**
sahilmgandhi 18:6a4db94011d3 466 * @brief Enable the write protection of the desired bank 1 sectors
sahilmgandhi 18:6a4db94011d3 467 *
sahilmgandhi 18:6a4db94011d3 468 * @note When the memory read protection level is selected (RDP level = 1),
sahilmgandhi 18:6a4db94011d3 469 * it is not possible to program or erase the flash sector i if CortexM3
sahilmgandhi 18:6a4db94011d3 470 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
sahilmgandhi 18:6a4db94011d3 471 *
sahilmgandhi 18:6a4db94011d3 472 * @param WRPSector: specifies the sector(s) to be write protected.
sahilmgandhi 18:6a4db94011d3 473 * The value of this parameter depend on device used within the same series
sahilmgandhi 18:6a4db94011d3 474 *
sahilmgandhi 18:6a4db94011d3 475 * @param Banks: Enable write protection on all the sectors for the specific bank
sahilmgandhi 18:6a4db94011d3 476 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 477 * @arg FLASH_BANK_1: WRP on all sectors of bank1
sahilmgandhi 18:6a4db94011d3 478 *
sahilmgandhi 18:6a4db94011d3 479 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 480 */
sahilmgandhi 18:6a4db94011d3 481 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
sahilmgandhi 18:6a4db94011d3 482 {
sahilmgandhi 18:6a4db94011d3 483 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 484
sahilmgandhi 18:6a4db94011d3 485 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 486 assert_param(IS_OB_WRP_SECTOR(WRPSector));
sahilmgandhi 18:6a4db94011d3 487 assert_param(IS_FLASH_BANK(Banks));
sahilmgandhi 18:6a4db94011d3 488
sahilmgandhi 18:6a4db94011d3 489 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 490 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 491
sahilmgandhi 18:6a4db94011d3 492 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 493 {
sahilmgandhi 18:6a4db94011d3 494 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
sahilmgandhi 18:6a4db94011d3 495 }
sahilmgandhi 18:6a4db94011d3 496
sahilmgandhi 18:6a4db94011d3 497 return status;
sahilmgandhi 18:6a4db94011d3 498 }
sahilmgandhi 18:6a4db94011d3 499
sahilmgandhi 18:6a4db94011d3 500 /**
sahilmgandhi 18:6a4db94011d3 501 * @brief Disable the write protection of the desired bank 1 sectors
sahilmgandhi 18:6a4db94011d3 502 *
sahilmgandhi 18:6a4db94011d3 503 * @note When the memory read protection level is selected (RDP level = 1),
sahilmgandhi 18:6a4db94011d3 504 * it is not possible to program or erase the flash sector if CortexM3
sahilmgandhi 18:6a4db94011d3 505 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
sahilmgandhi 18:6a4db94011d3 506 *
sahilmgandhi 18:6a4db94011d3 507 * @param WRPSector: specifies the sector(s) to be write protected.
sahilmgandhi 18:6a4db94011d3 508 * The value of this parameter depend on device used within the same series
sahilmgandhi 18:6a4db94011d3 509 *
sahilmgandhi 18:6a4db94011d3 510 * @param Banks: Enable write protection on all the sectors for the specific bank
sahilmgandhi 18:6a4db94011d3 511 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 512 * @arg FLASH_BANK_1: WRP on all sectors of bank1
sahilmgandhi 18:6a4db94011d3 513 *
sahilmgandhi 18:6a4db94011d3 514 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 515 */
sahilmgandhi 18:6a4db94011d3 516 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
sahilmgandhi 18:6a4db94011d3 517 {
sahilmgandhi 18:6a4db94011d3 518 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 519
sahilmgandhi 18:6a4db94011d3 520 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 521 assert_param(IS_OB_WRP_SECTOR(WRPSector));
sahilmgandhi 18:6a4db94011d3 522 assert_param(IS_FLASH_BANK(Banks));
sahilmgandhi 18:6a4db94011d3 523
sahilmgandhi 18:6a4db94011d3 524 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 525 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 526
sahilmgandhi 18:6a4db94011d3 527 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 528 {
sahilmgandhi 18:6a4db94011d3 529 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
sahilmgandhi 18:6a4db94011d3 530 }
sahilmgandhi 18:6a4db94011d3 531
sahilmgandhi 18:6a4db94011d3 532 return status;
sahilmgandhi 18:6a4db94011d3 533 }
sahilmgandhi 18:6a4db94011d3 534
sahilmgandhi 18:6a4db94011d3 535 /**
sahilmgandhi 18:6a4db94011d3 536 * @brief Set the read protection level.
sahilmgandhi 18:6a4db94011d3 537 * @param Level: specifies the read protection level.
sahilmgandhi 18:6a4db94011d3 538 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 539 * @arg OB_RDP_LEVEL_0: No protection
sahilmgandhi 18:6a4db94011d3 540 * @arg OB_RDP_LEVEL_1: Read protection of the memory
sahilmgandhi 18:6a4db94011d3 541 * @arg OB_RDP_LEVEL_2: Full chip protection
sahilmgandhi 18:6a4db94011d3 542 *
sahilmgandhi 18:6a4db94011d3 543 * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
sahilmgandhi 18:6a4db94011d3 544 *
sahilmgandhi 18:6a4db94011d3 545 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 546 */
sahilmgandhi 18:6a4db94011d3 547 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
sahilmgandhi 18:6a4db94011d3 548 {
sahilmgandhi 18:6a4db94011d3 549 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 550
sahilmgandhi 18:6a4db94011d3 551 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 552 assert_param(IS_OB_RDP_LEVEL(Level));
sahilmgandhi 18:6a4db94011d3 553
sahilmgandhi 18:6a4db94011d3 554 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 555 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 556
sahilmgandhi 18:6a4db94011d3 557 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 558 {
sahilmgandhi 18:6a4db94011d3 559 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
sahilmgandhi 18:6a4db94011d3 560 }
sahilmgandhi 18:6a4db94011d3 561
sahilmgandhi 18:6a4db94011d3 562 return status;
sahilmgandhi 18:6a4db94011d3 563 }
sahilmgandhi 18:6a4db94011d3 564
sahilmgandhi 18:6a4db94011d3 565 /**
sahilmgandhi 18:6a4db94011d3 566 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
sahilmgandhi 18:6a4db94011d3 567 * @param Iwdg: Selects the IWDG mode
sahilmgandhi 18:6a4db94011d3 568 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 569 * @arg OB_IWDG_SW: Software IWDG selected
sahilmgandhi 18:6a4db94011d3 570 * @arg OB_IWDG_HW: Hardware IWDG selected
sahilmgandhi 18:6a4db94011d3 571 * @param Stop: Reset event when entering STOP mode.
sahilmgandhi 18:6a4db94011d3 572 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 573 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
sahilmgandhi 18:6a4db94011d3 574 * @arg OB_STOP_RST: Reset generated when entering in STOP
sahilmgandhi 18:6a4db94011d3 575 * @param Stdby: Reset event when entering Standby mode.
sahilmgandhi 18:6a4db94011d3 576 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 577 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
sahilmgandhi 18:6a4db94011d3 578 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
sahilmgandhi 18:6a4db94011d3 579 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 580 */
sahilmgandhi 18:6a4db94011d3 581 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
sahilmgandhi 18:6a4db94011d3 582 {
sahilmgandhi 18:6a4db94011d3 583 uint8_t optiontmp = 0xFFU;
sahilmgandhi 18:6a4db94011d3 584 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 585
sahilmgandhi 18:6a4db94011d3 586 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 587 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
sahilmgandhi 18:6a4db94011d3 588 assert_param(IS_OB_STOP_SOURCE(Stop));
sahilmgandhi 18:6a4db94011d3 589 assert_param(IS_OB_STDBY_SOURCE(Stdby));
sahilmgandhi 18:6a4db94011d3 590
sahilmgandhi 18:6a4db94011d3 591 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 592 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 593
sahilmgandhi 18:6a4db94011d3 594 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 595 {
sahilmgandhi 18:6a4db94011d3 596 /* Mask OPTLOCK, OPTSTRT, BOR_LEV and BFB2 bits */
sahilmgandhi 18:6a4db94011d3 597 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1FU);
sahilmgandhi 18:6a4db94011d3 598
sahilmgandhi 18:6a4db94011d3 599 /* Update User Option Byte */
sahilmgandhi 18:6a4db94011d3 600 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = Iwdg | (uint8_t)(Stdby | (uint8_t)(Stop | ((uint8_t)optiontmp)));
sahilmgandhi 18:6a4db94011d3 601 }
sahilmgandhi 18:6a4db94011d3 602
sahilmgandhi 18:6a4db94011d3 603 return status;
sahilmgandhi 18:6a4db94011d3 604 }
sahilmgandhi 18:6a4db94011d3 605
sahilmgandhi 18:6a4db94011d3 606 /**
sahilmgandhi 18:6a4db94011d3 607 * @brief Set the BOR Level.
sahilmgandhi 18:6a4db94011d3 608 * @param Level: specifies the Option Bytes BOR Reset Level.
sahilmgandhi 18:6a4db94011d3 609 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 610 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
sahilmgandhi 18:6a4db94011d3 611 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
sahilmgandhi 18:6a4db94011d3 612 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
sahilmgandhi 18:6a4db94011d3 613 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
sahilmgandhi 18:6a4db94011d3 614 * @retval HAL Status
sahilmgandhi 18:6a4db94011d3 615 */
sahilmgandhi 18:6a4db94011d3 616 static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
sahilmgandhi 18:6a4db94011d3 617 {
sahilmgandhi 18:6a4db94011d3 618 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 619 assert_param(IS_OB_BOR_LEVEL(Level));
sahilmgandhi 18:6a4db94011d3 620
sahilmgandhi 18:6a4db94011d3 621 /* Set the BOR Level */
sahilmgandhi 18:6a4db94011d3 622 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
sahilmgandhi 18:6a4db94011d3 623 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= Level;
sahilmgandhi 18:6a4db94011d3 624
sahilmgandhi 18:6a4db94011d3 625 return HAL_OK;
sahilmgandhi 18:6a4db94011d3 626
sahilmgandhi 18:6a4db94011d3 627 }
sahilmgandhi 18:6a4db94011d3 628
sahilmgandhi 18:6a4db94011d3 629 /**
sahilmgandhi 18:6a4db94011d3 630 * @brief Return the FLASH User Option Byte value.
sahilmgandhi 18:6a4db94011d3 631 * @retval uint8_t FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
sahilmgandhi 18:6a4db94011d3 632 * and RST_STDBY(Bit2).
sahilmgandhi 18:6a4db94011d3 633 */
sahilmgandhi 18:6a4db94011d3 634 static uint8_t FLASH_OB_GetUser(void)
sahilmgandhi 18:6a4db94011d3 635 {
sahilmgandhi 18:6a4db94011d3 636 /* Return the User Option Byte */
sahilmgandhi 18:6a4db94011d3 637 return ((uint8_t)(FLASH->OPTCR & 0xE0U));
sahilmgandhi 18:6a4db94011d3 638 }
sahilmgandhi 18:6a4db94011d3 639
sahilmgandhi 18:6a4db94011d3 640 /**
sahilmgandhi 18:6a4db94011d3 641 * @brief Return the FLASH Write Protection Option Bytes value.
sahilmgandhi 18:6a4db94011d3 642 * @retval uint16_t FLASH Write Protection Option Bytes value
sahilmgandhi 18:6a4db94011d3 643 */
sahilmgandhi 18:6a4db94011d3 644 static uint16_t FLASH_OB_GetWRP(void)
sahilmgandhi 18:6a4db94011d3 645 {
sahilmgandhi 18:6a4db94011d3 646 /* Return the FLASH write protection Register value */
sahilmgandhi 18:6a4db94011d3 647 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
sahilmgandhi 18:6a4db94011d3 648 }
sahilmgandhi 18:6a4db94011d3 649
sahilmgandhi 18:6a4db94011d3 650 /**
sahilmgandhi 18:6a4db94011d3 651 * @brief Returns the FLASH Read Protection level.
sahilmgandhi 18:6a4db94011d3 652 * @retval FLASH ReadOut Protection Status:
sahilmgandhi 18:6a4db94011d3 653 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 654 * @arg OB_RDP_LEVEL_0: No protection
sahilmgandhi 18:6a4db94011d3 655 * @arg OB_RDP_LEVEL_1: Read protection of the memory
sahilmgandhi 18:6a4db94011d3 656 * @arg OB_RDP_LEVEL_2: Full chip protection
sahilmgandhi 18:6a4db94011d3 657 */
sahilmgandhi 18:6a4db94011d3 658 static uint8_t FLASH_OB_GetRDP(void)
sahilmgandhi 18:6a4db94011d3 659 {
sahilmgandhi 18:6a4db94011d3 660 uint8_t readstatus = OB_RDP_LEVEL_0;
sahilmgandhi 18:6a4db94011d3 661
sahilmgandhi 18:6a4db94011d3 662 if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_2))
sahilmgandhi 18:6a4db94011d3 663 {
sahilmgandhi 18:6a4db94011d3 664 readstatus = OB_RDP_LEVEL_2;
sahilmgandhi 18:6a4db94011d3 665 }
sahilmgandhi 18:6a4db94011d3 666 else if((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_1))
sahilmgandhi 18:6a4db94011d3 667 {
sahilmgandhi 18:6a4db94011d3 668 readstatus = OB_RDP_LEVEL_1;
sahilmgandhi 18:6a4db94011d3 669 }
sahilmgandhi 18:6a4db94011d3 670 else
sahilmgandhi 18:6a4db94011d3 671 {
sahilmgandhi 18:6a4db94011d3 672 readstatus = OB_RDP_LEVEL_0;
sahilmgandhi 18:6a4db94011d3 673 }
sahilmgandhi 18:6a4db94011d3 674
sahilmgandhi 18:6a4db94011d3 675 return readstatus;
sahilmgandhi 18:6a4db94011d3 676 }
sahilmgandhi 18:6a4db94011d3 677
sahilmgandhi 18:6a4db94011d3 678 /**
sahilmgandhi 18:6a4db94011d3 679 * @brief Returns the FLASH BOR level.
sahilmgandhi 18:6a4db94011d3 680 * @retval uint8_t The FLASH BOR level:
sahilmgandhi 18:6a4db94011d3 681 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
sahilmgandhi 18:6a4db94011d3 682 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
sahilmgandhi 18:6a4db94011d3 683 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
sahilmgandhi 18:6a4db94011d3 684 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
sahilmgandhi 18:6a4db94011d3 685 */
sahilmgandhi 18:6a4db94011d3 686 static uint8_t FLASH_OB_GetBOR(void)
sahilmgandhi 18:6a4db94011d3 687 {
sahilmgandhi 18:6a4db94011d3 688 /* Return the FLASH BOR level */
sahilmgandhi 18:6a4db94011d3 689 return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0CU);
sahilmgandhi 18:6a4db94011d3 690 }
sahilmgandhi 18:6a4db94011d3 691
sahilmgandhi 18:6a4db94011d3 692 /**
sahilmgandhi 18:6a4db94011d3 693 * @}
sahilmgandhi 18:6a4db94011d3 694 */
sahilmgandhi 18:6a4db94011d3 695
sahilmgandhi 18:6a4db94011d3 696 #endif /* HAL_FLASH_MODULE_ENABLED */
sahilmgandhi 18:6a4db94011d3 697
sahilmgandhi 18:6a4db94011d3 698 /**
sahilmgandhi 18:6a4db94011d3 699 * @}
sahilmgandhi 18:6a4db94011d3 700 */
sahilmgandhi 18:6a4db94011d3 701
sahilmgandhi 18:6a4db94011d3 702 /**
sahilmgandhi 18:6a4db94011d3 703 * @}
sahilmgandhi 18:6a4db94011d3 704 */
sahilmgandhi 18:6a4db94011d3 705
sahilmgandhi 18:6a4db94011d3 706 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/