MacroRat / MouseCode

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 stm32l1xx_hal_flash_ex.c
sahilmgandhi 18:6a4db94011d3 4 * @author MCD Application Team
sahilmgandhi 18:6a4db94011d3 5 * @version V1.2.0
sahilmgandhi 18:6a4db94011d3 6 * @date 01-July-2016
sahilmgandhi 18:6a4db94011d3 7 * @brief Extended FLASH HAL module driver.
sahilmgandhi 18:6a4db94011d3 8 *
sahilmgandhi 18:6a4db94011d3 9 * This file provides firmware functions to manage the following
sahilmgandhi 18:6a4db94011d3 10 * functionalities of the internal FLASH memory:
sahilmgandhi 18:6a4db94011d3 11 * + FLASH Interface configuration
sahilmgandhi 18:6a4db94011d3 12 * + FLASH Memory Erasing
sahilmgandhi 18:6a4db94011d3 13 * + DATA EEPROM Programming/Erasing
sahilmgandhi 18:6a4db94011d3 14 * + Option Bytes Programming
sahilmgandhi 18:6a4db94011d3 15 * + Interrupts management
sahilmgandhi 18:6a4db94011d3 16 *
sahilmgandhi 18:6a4db94011d3 17 @verbatim
sahilmgandhi 18:6a4db94011d3 18 ==============================================================================
sahilmgandhi 18:6a4db94011d3 19 ##### Flash peripheral Extended features #####
sahilmgandhi 18:6a4db94011d3 20 ==============================================================================
sahilmgandhi 18:6a4db94011d3 21
sahilmgandhi 18:6a4db94011d3 22 [..] Comparing to other products, the FLASH interface for STM32L1xx
sahilmgandhi 18:6a4db94011d3 23 devices contains the following additional features
sahilmgandhi 18:6a4db94011d3 24 (+) Erase functions
sahilmgandhi 18:6a4db94011d3 25 (+) DATA_EEPROM memory management
sahilmgandhi 18:6a4db94011d3 26 (+) BOOT option bit configuration
sahilmgandhi 18:6a4db94011d3 27 (+) PCROP protection for all sectors
sahilmgandhi 18:6a4db94011d3 28
sahilmgandhi 18:6a4db94011d3 29 ##### How to use this driver #####
sahilmgandhi 18:6a4db94011d3 30 ==============================================================================
sahilmgandhi 18:6a4db94011d3 31 [..] This driver provides functions to configure and program the FLASH memory
sahilmgandhi 18:6a4db94011d3 32 of all STM32L1xx. It includes:
sahilmgandhi 18:6a4db94011d3 33 (+) Full DATA_EEPROM erase and program management
sahilmgandhi 18:6a4db94011d3 34 (+) Boot activation
sahilmgandhi 18:6a4db94011d3 35 (+) PCROP protection configuration and control for all pages
sahilmgandhi 18:6a4db94011d3 36
sahilmgandhi 18:6a4db94011d3 37 @endverbatim
sahilmgandhi 18:6a4db94011d3 38 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 39 * @attention
sahilmgandhi 18:6a4db94011d3 40 *
sahilmgandhi 18:6a4db94011d3 41 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
sahilmgandhi 18:6a4db94011d3 42 *
sahilmgandhi 18:6a4db94011d3 43 * Redistribution and use in source and binary forms, with or without modification,
sahilmgandhi 18:6a4db94011d3 44 * are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 45 * 1. Redistributions of source code must retain the above copyright notice,
sahilmgandhi 18:6a4db94011d3 46 * this list of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 47 * 2. Redistributions in binary form must reproduce the above copyright notice,
sahilmgandhi 18:6a4db94011d3 48 * this list of conditions and the following disclaimer in the documentation
sahilmgandhi 18:6a4db94011d3 49 * and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 50 * 3. Neither the name of STMicroelectronics nor the names of its contributors
sahilmgandhi 18:6a4db94011d3 51 * may be used to endorse or promote products derived from this software
sahilmgandhi 18:6a4db94011d3 52 * without specific prior written permission.
sahilmgandhi 18:6a4db94011d3 53 *
sahilmgandhi 18:6a4db94011d3 54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
sahilmgandhi 18:6a4db94011d3 55 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sahilmgandhi 18:6a4db94011d3 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sahilmgandhi 18:6a4db94011d3 57 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
sahilmgandhi 18:6a4db94011d3 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sahilmgandhi 18:6a4db94011d3 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sahilmgandhi 18:6a4db94011d3 60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
sahilmgandhi 18:6a4db94011d3 61 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
sahilmgandhi 18:6a4db94011d3 62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
sahilmgandhi 18:6a4db94011d3 63 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 64 *
sahilmgandhi 18:6a4db94011d3 65 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 66 */
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68 /* Includes ------------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 69 #include "stm32l1xx_hal.h"
sahilmgandhi 18:6a4db94011d3 70
sahilmgandhi 18:6a4db94011d3 71 /** @addtogroup STM32L1xx_HAL_Driver
sahilmgandhi 18:6a4db94011d3 72 * @{
sahilmgandhi 18:6a4db94011d3 73 */
sahilmgandhi 18:6a4db94011d3 74 #ifdef HAL_FLASH_MODULE_ENABLED
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 /** @addtogroup FLASH
sahilmgandhi 18:6a4db94011d3 77 * @{
sahilmgandhi 18:6a4db94011d3 78 */
sahilmgandhi 18:6a4db94011d3 79 /** @addtogroup FLASH_Private_Variables
sahilmgandhi 18:6a4db94011d3 80 * @{
sahilmgandhi 18:6a4db94011d3 81 */
sahilmgandhi 18:6a4db94011d3 82 /* Variables used for Erase pages under interruption*/
sahilmgandhi 18:6a4db94011d3 83 extern FLASH_ProcessTypeDef pFlash;
sahilmgandhi 18:6a4db94011d3 84 /**
sahilmgandhi 18:6a4db94011d3 85 * @}
sahilmgandhi 18:6a4db94011d3 86 */
sahilmgandhi 18:6a4db94011d3 87
sahilmgandhi 18:6a4db94011d3 88 /**
sahilmgandhi 18:6a4db94011d3 89 * @}
sahilmgandhi 18:6a4db94011d3 90 */
sahilmgandhi 18:6a4db94011d3 91
sahilmgandhi 18:6a4db94011d3 92 /** @defgroup FLASHEx FLASHEx
sahilmgandhi 18:6a4db94011d3 93 * @brief FLASH HAL Extension module driver
sahilmgandhi 18:6a4db94011d3 94 * @{
sahilmgandhi 18:6a4db94011d3 95 */
sahilmgandhi 18:6a4db94011d3 96
sahilmgandhi 18:6a4db94011d3 97 /* Private typedef -----------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 98 /* Private define ------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 99 /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
sahilmgandhi 18:6a4db94011d3 100 * @{
sahilmgandhi 18:6a4db94011d3 101 */
sahilmgandhi 18:6a4db94011d3 102 /**
sahilmgandhi 18:6a4db94011d3 103 * @}
sahilmgandhi 18:6a4db94011d3 104 */
sahilmgandhi 18:6a4db94011d3 105
sahilmgandhi 18:6a4db94011d3 106 /* Private macro -------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 107 /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
sahilmgandhi 18:6a4db94011d3 108 * @{
sahilmgandhi 18:6a4db94011d3 109 */
sahilmgandhi 18:6a4db94011d3 110 /**
sahilmgandhi 18:6a4db94011d3 111 * @}
sahilmgandhi 18:6a4db94011d3 112 */
sahilmgandhi 18:6a4db94011d3 113
sahilmgandhi 18:6a4db94011d3 114 /* Private variables ---------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 115 /* Private function prototypes -----------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 116 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
sahilmgandhi 18:6a4db94011d3 117 * @{
sahilmgandhi 18:6a4db94011d3 118 */
sahilmgandhi 18:6a4db94011d3 119 void FLASH_PageErase(uint32_t PageAddress);
sahilmgandhi 18:6a4db94011d3 120 static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState);
sahilmgandhi 18:6a4db94011d3 121 static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState);
sahilmgandhi 18:6a4db94011d3 122 #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
sahilmgandhi 18:6a4db94011d3 123 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
sahilmgandhi 18:6a4db94011d3 124 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
sahilmgandhi 18:6a4db94011d3 125 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 126 static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState);
sahilmgandhi 18:6a4db94011d3 127 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 128 #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
sahilmgandhi 18:6a4db94011d3 129 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
sahilmgandhi 18:6a4db94011d3 130 || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 131 static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState);
sahilmgandhi 18:6a4db94011d3 132 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 133 #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
sahilmgandhi 18:6a4db94011d3 134 || defined(STM32L152xDX) || defined(STM32L162xDX)
sahilmgandhi 18:6a4db94011d3 135 static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState);
sahilmgandhi 18:6a4db94011d3 136 #endif /* STM32L151xE || STM32L152xE || STM32L151xDX || ... */
sahilmgandhi 18:6a4db94011d3 137 #if defined(FLASH_OBR_SPRMOD)
sahilmgandhi 18:6a4db94011d3 138 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState);
sahilmgandhi 18:6a4db94011d3 139 #endif /* FLASH_OBR_SPRMOD */
sahilmgandhi 18:6a4db94011d3 140 #if defined(FLASH_OBR_nRST_BFB2)
sahilmgandhi 18:6a4db94011d3 141 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT);
sahilmgandhi 18:6a4db94011d3 142 #endif /* FLASH_OBR_nRST_BFB2 */
sahilmgandhi 18:6a4db94011d3 143 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
sahilmgandhi 18:6a4db94011d3 144 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
sahilmgandhi 18:6a4db94011d3 145 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
sahilmgandhi 18:6a4db94011d3 146 static uint8_t FLASH_OB_GetRDP(void);
sahilmgandhi 18:6a4db94011d3 147 static uint8_t FLASH_OB_GetUser(void);
sahilmgandhi 18:6a4db94011d3 148 static uint8_t FLASH_OB_GetBOR(void);
sahilmgandhi 18:6a4db94011d3 149 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data);
sahilmgandhi 18:6a4db94011d3 150 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);
sahilmgandhi 18:6a4db94011d3 151 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data);
sahilmgandhi 18:6a4db94011d3 152 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data);
sahilmgandhi 18:6a4db94011d3 153 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);
sahilmgandhi 18:6a4db94011d3 154 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data);
sahilmgandhi 18:6a4db94011d3 155 /**
sahilmgandhi 18:6a4db94011d3 156 * @}
sahilmgandhi 18:6a4db94011d3 157 */
sahilmgandhi 18:6a4db94011d3 158
sahilmgandhi 18:6a4db94011d3 159 /* Exported functions ---------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 160 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
sahilmgandhi 18:6a4db94011d3 161 * @{
sahilmgandhi 18:6a4db94011d3 162 */
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
sahilmgandhi 18:6a4db94011d3 165 * @brief FLASH Memory Erasing functions
sahilmgandhi 18:6a4db94011d3 166 *
sahilmgandhi 18:6a4db94011d3 167 @verbatim
sahilmgandhi 18:6a4db94011d3 168 ==============================================================================
sahilmgandhi 18:6a4db94011d3 169 ##### FLASH Erasing Programming functions #####
sahilmgandhi 18:6a4db94011d3 170 ==============================================================================
sahilmgandhi 18:6a4db94011d3 171
sahilmgandhi 18:6a4db94011d3 172 [..] The FLASH Memory Erasing functions, includes the following functions:
sahilmgandhi 18:6a4db94011d3 173 (+) @ref HAL_FLASHEx_Erase: return only when erase has been done
sahilmgandhi 18:6a4db94011d3 174 (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback
sahilmgandhi 18:6a4db94011d3 175 is called with parameter 0xFFFFFFFF
sahilmgandhi 18:6a4db94011d3 176
sahilmgandhi 18:6a4db94011d3 177 [..] Any operation of erase should follow these steps:
sahilmgandhi 18:6a4db94011d3 178 (#) Call the @ref HAL_FLASH_Unlock() function to enable the flash control register and
sahilmgandhi 18:6a4db94011d3 179 program memory access.
sahilmgandhi 18:6a4db94011d3 180 (#) Call the desired function to erase page.
sahilmgandhi 18:6a4db94011d3 181 (#) Call the @ref HAL_FLASH_Lock() to disable the flash program memory access
sahilmgandhi 18:6a4db94011d3 182 (recommended to protect the FLASH memory against possible unwanted operation).
sahilmgandhi 18:6a4db94011d3 183
sahilmgandhi 18:6a4db94011d3 184 @endverbatim
sahilmgandhi 18:6a4db94011d3 185 * @{
sahilmgandhi 18:6a4db94011d3 186 */
sahilmgandhi 18:6a4db94011d3 187
sahilmgandhi 18:6a4db94011d3 188 /**
sahilmgandhi 18:6a4db94011d3 189 * @brief Erase the specified FLASH memory Pages
sahilmgandhi 18:6a4db94011d3 190 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
sahilmgandhi 18:6a4db94011d3 191 * must be called before.
sahilmgandhi 18:6a4db94011d3 192 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
sahilmgandhi 18:6a4db94011d3 193 * (recommended to protect the FLASH memory against possible unwanted operation)
sahilmgandhi 18:6a4db94011d3 194 * @note For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
sahilmgandhi 18:6a4db94011d3 195 * 2 banks, user should perform pages erase by bank only.
sahilmgandhi 18:6a4db94011d3 196 * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 197 * contains the configuration information for the erasing.
sahilmgandhi 18:6a4db94011d3 198 *
sahilmgandhi 18:6a4db94011d3 199 * @param[out] PageError pointer to variable that
sahilmgandhi 18:6a4db94011d3 200 * contains the configuration information on faulty page in case of error
sahilmgandhi 18:6a4db94011d3 201 * (0xFFFFFFFF means that all the pages have been correctly erased)
sahilmgandhi 18:6a4db94011d3 202 *
sahilmgandhi 18:6a4db94011d3 203 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 204 */
sahilmgandhi 18:6a4db94011d3 205 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
sahilmgandhi 18:6a4db94011d3 206 {
sahilmgandhi 18:6a4db94011d3 207 HAL_StatusTypeDef status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 208 uint32_t address = 0;
sahilmgandhi 18:6a4db94011d3 209
sahilmgandhi 18:6a4db94011d3 210 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 211 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 212
sahilmgandhi 18:6a4db94011d3 213 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 214 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 215
sahilmgandhi 18:6a4db94011d3 216 if (status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 217 {
sahilmgandhi 18:6a4db94011d3 218 /*Initialization of PageError variable*/
sahilmgandhi 18:6a4db94011d3 219 *PageError = 0xFFFFFFFFU;
sahilmgandhi 18:6a4db94011d3 220
sahilmgandhi 18:6a4db94011d3 221 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 222 assert_param(IS_NBPAGES(pEraseInit->NbPages));
sahilmgandhi 18:6a4db94011d3 223 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
sahilmgandhi 18:6a4db94011d3 224 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
sahilmgandhi 18:6a4db94011d3 225 assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
sahilmgandhi 18:6a4db94011d3 226
sahilmgandhi 18:6a4db94011d3 227 #if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
sahilmgandhi 18:6a4db94011d3 228 /* Check on which bank belongs the 1st address to erase */
sahilmgandhi 18:6a4db94011d3 229 if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
sahilmgandhi 18:6a4db94011d3 230 {
sahilmgandhi 18:6a4db94011d3 231 /* BANK1 */
sahilmgandhi 18:6a4db94011d3 232 /* Check that last page to erase still belongs to BANK1 */
sahilmgandhi 18:6a4db94011d3 233 if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK1_END)
sahilmgandhi 18:6a4db94011d3 234 {
sahilmgandhi 18:6a4db94011d3 235 /* Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
sahilmgandhi 18:6a4db94011d3 236 continuous */
sahilmgandhi 18:6a4db94011d3 237 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 238 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 239 return HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 240 }
sahilmgandhi 18:6a4db94011d3 241 }
sahilmgandhi 18:6a4db94011d3 242 else
sahilmgandhi 18:6a4db94011d3 243 {
sahilmgandhi 18:6a4db94011d3 244 /* BANK2 */
sahilmgandhi 18:6a4db94011d3 245 /* Check that last page to erase still belongs to BANK2 */
sahilmgandhi 18:6a4db94011d3 246 if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK2_END)
sahilmgandhi 18:6a4db94011d3 247 {
sahilmgandhi 18:6a4db94011d3 248 /* Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
sahilmgandhi 18:6a4db94011d3 249 continuous */
sahilmgandhi 18:6a4db94011d3 250 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 251 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 252 return HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 253 }
sahilmgandhi 18:6a4db94011d3 254 }
sahilmgandhi 18:6a4db94011d3 255 #endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
sahilmgandhi 18:6a4db94011d3 256
sahilmgandhi 18:6a4db94011d3 257 /* Erase page by page to be done*/
sahilmgandhi 18:6a4db94011d3 258 for(address = pEraseInit->PageAddress;
sahilmgandhi 18:6a4db94011d3 259 address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
sahilmgandhi 18:6a4db94011d3 260 address += FLASH_PAGE_SIZE)
sahilmgandhi 18:6a4db94011d3 261 {
sahilmgandhi 18:6a4db94011d3 262 FLASH_PageErase(address);
sahilmgandhi 18:6a4db94011d3 263
sahilmgandhi 18:6a4db94011d3 264 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 265 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 266
sahilmgandhi 18:6a4db94011d3 267 /* If the erase operation is completed, disable the ERASE Bit */
sahilmgandhi 18:6a4db94011d3 268 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
sahilmgandhi 18:6a4db94011d3 269 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
sahilmgandhi 18:6a4db94011d3 270
sahilmgandhi 18:6a4db94011d3 271 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 272 {
sahilmgandhi 18:6a4db94011d3 273 /* In case of error, stop erase procedure and return the faulty address */
sahilmgandhi 18:6a4db94011d3 274 *PageError = address;
sahilmgandhi 18:6a4db94011d3 275 break;
sahilmgandhi 18:6a4db94011d3 276 }
sahilmgandhi 18:6a4db94011d3 277 }
sahilmgandhi 18:6a4db94011d3 278 }
sahilmgandhi 18:6a4db94011d3 279
sahilmgandhi 18:6a4db94011d3 280 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 281 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 282
sahilmgandhi 18:6a4db94011d3 283 return status;
sahilmgandhi 18:6a4db94011d3 284 }
sahilmgandhi 18:6a4db94011d3 285
sahilmgandhi 18:6a4db94011d3 286 /**
sahilmgandhi 18:6a4db94011d3 287 * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
sahilmgandhi 18:6a4db94011d3 288 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
sahilmgandhi 18:6a4db94011d3 289 * must be called before.
sahilmgandhi 18:6a4db94011d3 290 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
sahilmgandhi 18:6a4db94011d3 291 * (recommended to protect the FLASH memory against possible unwanted operation)
sahilmgandhi 18:6a4db94011d3 292 * End of erase is done when @ref HAL_FLASH_EndOfOperationCallback is called with parameter
sahilmgandhi 18:6a4db94011d3 293 * 0xFFFFFFFF
sahilmgandhi 18:6a4db94011d3 294 * @note For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
sahilmgandhi 18:6a4db94011d3 295 * 2 banks, user should perform pages erase by bank only.
sahilmgandhi 18:6a4db94011d3 296 * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 297 * contains the configuration information for the erasing.
sahilmgandhi 18:6a4db94011d3 298 *
sahilmgandhi 18:6a4db94011d3 299 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 300 */
sahilmgandhi 18:6a4db94011d3 301 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
sahilmgandhi 18:6a4db94011d3 302 {
sahilmgandhi 18:6a4db94011d3 303 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 304
sahilmgandhi 18:6a4db94011d3 305 /* If procedure already ongoing, reject the next one */
sahilmgandhi 18:6a4db94011d3 306 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
sahilmgandhi 18:6a4db94011d3 307 {
sahilmgandhi 18:6a4db94011d3 308 return HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 309 }
sahilmgandhi 18:6a4db94011d3 310
sahilmgandhi 18:6a4db94011d3 311 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 312 assert_param(IS_NBPAGES(pEraseInit->NbPages));
sahilmgandhi 18:6a4db94011d3 313 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
sahilmgandhi 18:6a4db94011d3 314 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
sahilmgandhi 18:6a4db94011d3 315 assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
sahilmgandhi 18:6a4db94011d3 316
sahilmgandhi 18:6a4db94011d3 317 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 318 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 319
sahilmgandhi 18:6a4db94011d3 320 #if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
sahilmgandhi 18:6a4db94011d3 321 /* Check on which bank belongs the 1st address to erase */
sahilmgandhi 18:6a4db94011d3 322 if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
sahilmgandhi 18:6a4db94011d3 323 {
sahilmgandhi 18:6a4db94011d3 324 /* BANK1 */
sahilmgandhi 18:6a4db94011d3 325 /* Check that last page to erase still belongs to BANK1 */
sahilmgandhi 18:6a4db94011d3 326 if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK1_END)
sahilmgandhi 18:6a4db94011d3 327 {
sahilmgandhi 18:6a4db94011d3 328 /* Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
sahilmgandhi 18:6a4db94011d3 329 continuous */
sahilmgandhi 18:6a4db94011d3 330 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 331 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 332 return HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 333 }
sahilmgandhi 18:6a4db94011d3 334 }
sahilmgandhi 18:6a4db94011d3 335 else
sahilmgandhi 18:6a4db94011d3 336 {
sahilmgandhi 18:6a4db94011d3 337 /* BANK2 */
sahilmgandhi 18:6a4db94011d3 338 /* Check that last page to erase still belongs to BANK2 */
sahilmgandhi 18:6a4db94011d3 339 if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1) > FLASH_BANK2_END)
sahilmgandhi 18:6a4db94011d3 340 {
sahilmgandhi 18:6a4db94011d3 341 /* Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
sahilmgandhi 18:6a4db94011d3 342 continuous */
sahilmgandhi 18:6a4db94011d3 343 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 344 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 345 return HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 346 }
sahilmgandhi 18:6a4db94011d3 347 }
sahilmgandhi 18:6a4db94011d3 348 #endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
sahilmgandhi 18:6a4db94011d3 349
sahilmgandhi 18:6a4db94011d3 350 /* Enable End of FLASH Operation and Error source interrupts */
sahilmgandhi 18:6a4db94011d3 351 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
sahilmgandhi 18:6a4db94011d3 352
sahilmgandhi 18:6a4db94011d3 353 pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
sahilmgandhi 18:6a4db94011d3 354 pFlash.NbPagesToErase = pEraseInit->NbPages;
sahilmgandhi 18:6a4db94011d3 355 pFlash.Page = pEraseInit->PageAddress;
sahilmgandhi 18:6a4db94011d3 356
sahilmgandhi 18:6a4db94011d3 357 /*Erase 1st page and wait for IT*/
sahilmgandhi 18:6a4db94011d3 358 FLASH_PageErase(pEraseInit->PageAddress);
sahilmgandhi 18:6a4db94011d3 359
sahilmgandhi 18:6a4db94011d3 360 return status;
sahilmgandhi 18:6a4db94011d3 361 }
sahilmgandhi 18:6a4db94011d3 362
sahilmgandhi 18:6a4db94011d3 363 /**
sahilmgandhi 18:6a4db94011d3 364 * @}
sahilmgandhi 18:6a4db94011d3 365 */
sahilmgandhi 18:6a4db94011d3 366
sahilmgandhi 18:6a4db94011d3 367 /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
sahilmgandhi 18:6a4db94011d3 368 * @brief Option Bytes Programming functions
sahilmgandhi 18:6a4db94011d3 369 *
sahilmgandhi 18:6a4db94011d3 370 @verbatim
sahilmgandhi 18:6a4db94011d3 371 ==============================================================================
sahilmgandhi 18:6a4db94011d3 372 ##### Option Bytes Programming functions #####
sahilmgandhi 18:6a4db94011d3 373 ==============================================================================
sahilmgandhi 18:6a4db94011d3 374
sahilmgandhi 18:6a4db94011d3 375 [..] Any operation of erase or program should follow these steps:
sahilmgandhi 18:6a4db94011d3 376 (#) Call the @ref HAL_FLASH_OB_Unlock() function to enable the Flash option control
sahilmgandhi 18:6a4db94011d3 377 register access.
sahilmgandhi 18:6a4db94011d3 378 (#) Call following function to program the desired option bytes.
sahilmgandhi 18:6a4db94011d3 379 (++) @ref HAL_FLASHEx_OBProgram:
sahilmgandhi 18:6a4db94011d3 380 - To Enable/Disable the desired sector write protection.
sahilmgandhi 18:6a4db94011d3 381 - To set the desired read Protection Level.
sahilmgandhi 18:6a4db94011d3 382 - To configure the user option Bytes: IWDG, STOP and the Standby.
sahilmgandhi 18:6a4db94011d3 383 - To Set the BOR level.
sahilmgandhi 18:6a4db94011d3 384 (#) Once all needed option bytes to be programmed are correctly written, call the
sahilmgandhi 18:6a4db94011d3 385 @ref HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
sahilmgandhi 18:6a4db94011d3 386 (#) Call the @ref HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
sahilmgandhi 18:6a4db94011d3 387 to protect the option Bytes against possible unwanted operations).
sahilmgandhi 18:6a4db94011d3 388
sahilmgandhi 18:6a4db94011d3 389 [..] Proprietary code Read Out Protection (PcROP):
sahilmgandhi 18:6a4db94011d3 390 (#) The PcROP sector is selected by using the same option bytes as the Write
sahilmgandhi 18:6a4db94011d3 391 protection (nWRPi bits). As a result, these 2 options are exclusive each other.
sahilmgandhi 18:6a4db94011d3 392 (#) In order to activate the PcROP (change the function of the nWRPi option bits),
sahilmgandhi 18:6a4db94011d3 393 the SPRMOD option bit must be activated.
sahilmgandhi 18:6a4db94011d3 394 (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
sahilmgandhi 18:6a4db94011d3 395 means: if SPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
sahilmgandhi 18:6a4db94011d3 396 is read/write protected.
sahilmgandhi 18:6a4db94011d3 397 (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
sahilmgandhi 18:6a4db94011d3 398 (++) @ref HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
sahilmgandhi 18:6a4db94011d3 399 (++) @ref HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
sahilmgandhi 18:6a4db94011d3 400 (#) PcROP is available only in STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices.
sahilmgandhi 18:6a4db94011d3 401
sahilmgandhi 18:6a4db94011d3 402 @endverbatim
sahilmgandhi 18:6a4db94011d3 403 * @{
sahilmgandhi 18:6a4db94011d3 404 */
sahilmgandhi 18:6a4db94011d3 405
sahilmgandhi 18:6a4db94011d3 406 /**
sahilmgandhi 18:6a4db94011d3 407 * @brief Program option bytes
sahilmgandhi 18:6a4db94011d3 408 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
sahilmgandhi 18:6a4db94011d3 409 * contains the configuration information for the programming.
sahilmgandhi 18:6a4db94011d3 410 *
sahilmgandhi 18:6a4db94011d3 411 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 412 */
sahilmgandhi 18:6a4db94011d3 413 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
sahilmgandhi 18:6a4db94011d3 414 {
sahilmgandhi 18:6a4db94011d3 415 HAL_StatusTypeDef status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 416
sahilmgandhi 18:6a4db94011d3 417 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 418 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 419
sahilmgandhi 18:6a4db94011d3 420 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 421 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
sahilmgandhi 18:6a4db94011d3 422
sahilmgandhi 18:6a4db94011d3 423 /*Write protection configuration*/
sahilmgandhi 18:6a4db94011d3 424 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
sahilmgandhi 18:6a4db94011d3 425 {
sahilmgandhi 18:6a4db94011d3 426 assert_param(IS_WRPSTATE(pOBInit->WRPState));
sahilmgandhi 18:6a4db94011d3 427 if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
sahilmgandhi 18:6a4db94011d3 428 {
sahilmgandhi 18:6a4db94011d3 429 /* Enable of Write protection on the selected Sector*/
sahilmgandhi 18:6a4db94011d3 430 status = FLASH_OB_WRPConfig(pOBInit, ENABLE);
sahilmgandhi 18:6a4db94011d3 431 }
sahilmgandhi 18:6a4db94011d3 432 else
sahilmgandhi 18:6a4db94011d3 433 {
sahilmgandhi 18:6a4db94011d3 434 /* Disable of Write protection on the selected Sector*/
sahilmgandhi 18:6a4db94011d3 435 status = FLASH_OB_WRPConfig(pOBInit, DISABLE);
sahilmgandhi 18:6a4db94011d3 436 }
sahilmgandhi 18:6a4db94011d3 437 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 438 {
sahilmgandhi 18:6a4db94011d3 439 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 440 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 441 return status;
sahilmgandhi 18:6a4db94011d3 442 }
sahilmgandhi 18:6a4db94011d3 443 }
sahilmgandhi 18:6a4db94011d3 444
sahilmgandhi 18:6a4db94011d3 445 /* Read protection configuration*/
sahilmgandhi 18:6a4db94011d3 446 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
sahilmgandhi 18:6a4db94011d3 447 {
sahilmgandhi 18:6a4db94011d3 448 status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
sahilmgandhi 18:6a4db94011d3 449 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 450 {
sahilmgandhi 18:6a4db94011d3 451 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 452 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 453 return status;
sahilmgandhi 18:6a4db94011d3 454 }
sahilmgandhi 18:6a4db94011d3 455 }
sahilmgandhi 18:6a4db94011d3 456
sahilmgandhi 18:6a4db94011d3 457 /* USER configuration*/
sahilmgandhi 18:6a4db94011d3 458 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
sahilmgandhi 18:6a4db94011d3 459 {
sahilmgandhi 18:6a4db94011d3 460 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
sahilmgandhi 18:6a4db94011d3 461 pOBInit->USERConfig & OB_STOP_NORST,
sahilmgandhi 18:6a4db94011d3 462 pOBInit->USERConfig & OB_STDBY_NORST);
sahilmgandhi 18:6a4db94011d3 463 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 464 {
sahilmgandhi 18:6a4db94011d3 465 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 466 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 467 return status;
sahilmgandhi 18:6a4db94011d3 468 }
sahilmgandhi 18:6a4db94011d3 469 }
sahilmgandhi 18:6a4db94011d3 470
sahilmgandhi 18:6a4db94011d3 471 /* BOR Level configuration*/
sahilmgandhi 18:6a4db94011d3 472 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
sahilmgandhi 18:6a4db94011d3 473 {
sahilmgandhi 18:6a4db94011d3 474 status = FLASH_OB_BORConfig(pOBInit->BORLevel);
sahilmgandhi 18:6a4db94011d3 475 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 476 {
sahilmgandhi 18:6a4db94011d3 477 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 478 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 479 return status;
sahilmgandhi 18:6a4db94011d3 480 }
sahilmgandhi 18:6a4db94011d3 481 }
sahilmgandhi 18:6a4db94011d3 482 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 483 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 484
sahilmgandhi 18:6a4db94011d3 485 return status;
sahilmgandhi 18:6a4db94011d3 486 }
sahilmgandhi 18:6a4db94011d3 487
sahilmgandhi 18:6a4db94011d3 488 /**
sahilmgandhi 18:6a4db94011d3 489 * @brief Get the Option byte configuration
sahilmgandhi 18:6a4db94011d3 490 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
sahilmgandhi 18:6a4db94011d3 491 * contains the configuration information for the programming.
sahilmgandhi 18:6a4db94011d3 492 *
sahilmgandhi 18:6a4db94011d3 493 * @retval None
sahilmgandhi 18:6a4db94011d3 494 */
sahilmgandhi 18:6a4db94011d3 495 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
sahilmgandhi 18:6a4db94011d3 496 {
sahilmgandhi 18:6a4db94011d3 497 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
sahilmgandhi 18:6a4db94011d3 498
sahilmgandhi 18:6a4db94011d3 499 /*Get WRP1*/
sahilmgandhi 18:6a4db94011d3 500 pOBInit->WRPSector0To31 = (uint32_t)(FLASH->WRPR1);
sahilmgandhi 18:6a4db94011d3 501
sahilmgandhi 18:6a4db94011d3 502 #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
sahilmgandhi 18:6a4db94011d3 503 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
sahilmgandhi 18:6a4db94011d3 504 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
sahilmgandhi 18:6a4db94011d3 505 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 506
sahilmgandhi 18:6a4db94011d3 507 /*Get WRP2*/
sahilmgandhi 18:6a4db94011d3 508 pOBInit->WRPSector32To63 = (uint32_t)(FLASH->WRPR2);
sahilmgandhi 18:6a4db94011d3 509
sahilmgandhi 18:6a4db94011d3 510 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 511
sahilmgandhi 18:6a4db94011d3 512 #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
sahilmgandhi 18:6a4db94011d3 513 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
sahilmgandhi 18:6a4db94011d3 514 || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 515
sahilmgandhi 18:6a4db94011d3 516 /*Get WRP3*/
sahilmgandhi 18:6a4db94011d3 517 pOBInit->WRPSector64To95 = (uint32_t)(FLASH->WRPR3);
sahilmgandhi 18:6a4db94011d3 518
sahilmgandhi 18:6a4db94011d3 519 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 520
sahilmgandhi 18:6a4db94011d3 521 #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
sahilmgandhi 18:6a4db94011d3 522 || defined(STM32L152xDX) || defined(STM32L162xDX)
sahilmgandhi 18:6a4db94011d3 523
sahilmgandhi 18:6a4db94011d3 524 /*Get WRP4*/
sahilmgandhi 18:6a4db94011d3 525 pOBInit->WRPSector96To127 = (uint32_t)(FLASH->WRPR4);
sahilmgandhi 18:6a4db94011d3 526
sahilmgandhi 18:6a4db94011d3 527 #endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
sahilmgandhi 18:6a4db94011d3 528
sahilmgandhi 18:6a4db94011d3 529 /*Get RDP Level*/
sahilmgandhi 18:6a4db94011d3 530 pOBInit->RDPLevel = FLASH_OB_GetRDP();
sahilmgandhi 18:6a4db94011d3 531
sahilmgandhi 18:6a4db94011d3 532 /*Get USER*/
sahilmgandhi 18:6a4db94011d3 533 pOBInit->USERConfig = FLASH_OB_GetUser();
sahilmgandhi 18:6a4db94011d3 534
sahilmgandhi 18:6a4db94011d3 535 /*Get BOR Level*/
sahilmgandhi 18:6a4db94011d3 536 pOBInit->BORLevel = FLASH_OB_GetBOR();
sahilmgandhi 18:6a4db94011d3 537 }
sahilmgandhi 18:6a4db94011d3 538
sahilmgandhi 18:6a4db94011d3 539 #if defined(FLASH_OBR_SPRMOD) || defined(FLASH_OBR_nRST_BFB2)
sahilmgandhi 18:6a4db94011d3 540
sahilmgandhi 18:6a4db94011d3 541 /**
sahilmgandhi 18:6a4db94011d3 542 * @brief Program option bytes
sahilmgandhi 18:6a4db94011d3 543 * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
sahilmgandhi 18:6a4db94011d3 544 * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 545 * contains the configuration information for the programming.
sahilmgandhi 18:6a4db94011d3 546 *
sahilmgandhi 18:6a4db94011d3 547 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 548 */
sahilmgandhi 18:6a4db94011d3 549 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
sahilmgandhi 18:6a4db94011d3 550 {
sahilmgandhi 18:6a4db94011d3 551 HAL_StatusTypeDef status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 552
sahilmgandhi 18:6a4db94011d3 553 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 554 assert_param(IS_OBEX(pAdvOBInit->OptionType));
sahilmgandhi 18:6a4db94011d3 555
sahilmgandhi 18:6a4db94011d3 556 #if defined(FLASH_OBR_SPRMOD)
sahilmgandhi 18:6a4db94011d3 557
sahilmgandhi 18:6a4db94011d3 558 /* Program PCROP option byte*/
sahilmgandhi 18:6a4db94011d3 559 if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
sahilmgandhi 18:6a4db94011d3 560 {
sahilmgandhi 18:6a4db94011d3 561 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 562 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
sahilmgandhi 18:6a4db94011d3 563 if (pAdvOBInit->PCROPState == OB_PCROP_STATE_ENABLE)
sahilmgandhi 18:6a4db94011d3 564 {
sahilmgandhi 18:6a4db94011d3 565 /*Enable of Write protection on the selected Sector*/
sahilmgandhi 18:6a4db94011d3 566 status = FLASH_OB_PCROPConfig(pAdvOBInit, ENABLE);
sahilmgandhi 18:6a4db94011d3 567 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 568 {
sahilmgandhi 18:6a4db94011d3 569 return status;
sahilmgandhi 18:6a4db94011d3 570 }
sahilmgandhi 18:6a4db94011d3 571 }
sahilmgandhi 18:6a4db94011d3 572 else
sahilmgandhi 18:6a4db94011d3 573 {
sahilmgandhi 18:6a4db94011d3 574 /* Disable of Write protection on the selected Sector*/
sahilmgandhi 18:6a4db94011d3 575 status = FLASH_OB_PCROPConfig(pAdvOBInit, DISABLE);
sahilmgandhi 18:6a4db94011d3 576 if (status != HAL_OK)
sahilmgandhi 18:6a4db94011d3 577 {
sahilmgandhi 18:6a4db94011d3 578 return status;
sahilmgandhi 18:6a4db94011d3 579 }
sahilmgandhi 18:6a4db94011d3 580 }
sahilmgandhi 18:6a4db94011d3 581 }
sahilmgandhi 18:6a4db94011d3 582
sahilmgandhi 18:6a4db94011d3 583 #endif /* FLASH_OBR_SPRMOD */
sahilmgandhi 18:6a4db94011d3 584
sahilmgandhi 18:6a4db94011d3 585 #if defined(FLASH_OBR_nRST_BFB2)
sahilmgandhi 18:6a4db94011d3 586
sahilmgandhi 18:6a4db94011d3 587 /* Program BOOT config option byte */
sahilmgandhi 18:6a4db94011d3 588 if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
sahilmgandhi 18:6a4db94011d3 589 {
sahilmgandhi 18:6a4db94011d3 590 status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
sahilmgandhi 18:6a4db94011d3 591 }
sahilmgandhi 18:6a4db94011d3 592
sahilmgandhi 18:6a4db94011d3 593 #endif /* FLASH_OBR_nRST_BFB2 */
sahilmgandhi 18:6a4db94011d3 594
sahilmgandhi 18:6a4db94011d3 595 return status;
sahilmgandhi 18:6a4db94011d3 596 }
sahilmgandhi 18:6a4db94011d3 597
sahilmgandhi 18:6a4db94011d3 598 /**
sahilmgandhi 18:6a4db94011d3 599 * @brief Get the OBEX byte configuration
sahilmgandhi 18:6a4db94011d3 600 * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
sahilmgandhi 18:6a4db94011d3 601 * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 602 * contains the configuration information for the programming.
sahilmgandhi 18:6a4db94011d3 603 *
sahilmgandhi 18:6a4db94011d3 604 * @retval None
sahilmgandhi 18:6a4db94011d3 605 */
sahilmgandhi 18:6a4db94011d3 606 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
sahilmgandhi 18:6a4db94011d3 607 {
sahilmgandhi 18:6a4db94011d3 608 pAdvOBInit->OptionType = 0;
sahilmgandhi 18:6a4db94011d3 609
sahilmgandhi 18:6a4db94011d3 610 #if defined(FLASH_OBR_SPRMOD)
sahilmgandhi 18:6a4db94011d3 611
sahilmgandhi 18:6a4db94011d3 612 pAdvOBInit->OptionType |= OPTIONBYTE_PCROP;
sahilmgandhi 18:6a4db94011d3 613
sahilmgandhi 18:6a4db94011d3 614 /*Get PCROP state */
sahilmgandhi 18:6a4db94011d3 615 pAdvOBInit->PCROPState = (FLASH->OBR & FLASH_OBR_SPRMOD) >> POSITION_VAL(FLASH_OBR_SPRMOD);
sahilmgandhi 18:6a4db94011d3 616
sahilmgandhi 18:6a4db94011d3 617 /*Get PCROP protected sector from 0 to 31 */
sahilmgandhi 18:6a4db94011d3 618 pAdvOBInit->PCROPSector0To31 = FLASH->WRPR1;
sahilmgandhi 18:6a4db94011d3 619
sahilmgandhi 18:6a4db94011d3 620 #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
sahilmgandhi 18:6a4db94011d3 621
sahilmgandhi 18:6a4db94011d3 622 /*Get PCROP protected sector from 32 to 63 */
sahilmgandhi 18:6a4db94011d3 623 pAdvOBInit->PCROPSector32To63 = FLASH->WRPR2;
sahilmgandhi 18:6a4db94011d3 624
sahilmgandhi 18:6a4db94011d3 625 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
sahilmgandhi 18:6a4db94011d3 626 #endif /* FLASH_OBR_SPRMOD */
sahilmgandhi 18:6a4db94011d3 627
sahilmgandhi 18:6a4db94011d3 628 #if defined(FLASH_OBR_nRST_BFB2)
sahilmgandhi 18:6a4db94011d3 629
sahilmgandhi 18:6a4db94011d3 630 pAdvOBInit->OptionType |= OPTIONBYTE_BOOTCONFIG;
sahilmgandhi 18:6a4db94011d3 631
sahilmgandhi 18:6a4db94011d3 632 /* Get Boot config OB */
sahilmgandhi 18:6a4db94011d3 633 pAdvOBInit->BootConfig = (FLASH->OBR & FLASH_OBR_nRST_BFB2) >> 16;
sahilmgandhi 18:6a4db94011d3 634
sahilmgandhi 18:6a4db94011d3 635 #endif /* FLASH_OBR_nRST_BFB2 */
sahilmgandhi 18:6a4db94011d3 636 }
sahilmgandhi 18:6a4db94011d3 637
sahilmgandhi 18:6a4db94011d3 638 #endif /* FLASH_OBR_SPRMOD || FLASH_OBR_nRST_BFB2 */
sahilmgandhi 18:6a4db94011d3 639
sahilmgandhi 18:6a4db94011d3 640 #if defined(FLASH_OBR_SPRMOD)
sahilmgandhi 18:6a4db94011d3 641
sahilmgandhi 18:6a4db94011d3 642 /**
sahilmgandhi 18:6a4db94011d3 643 * @brief Select the Protection Mode (SPRMOD).
sahilmgandhi 18:6a4db94011d3 644 * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
sahilmgandhi 18:6a4db94011d3 645 * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
sahilmgandhi 18:6a4db94011d3 646 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
sahilmgandhi 18:6a4db94011d3 647 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 648 */
sahilmgandhi 18:6a4db94011d3 649 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
sahilmgandhi 18:6a4db94011d3 650 {
sahilmgandhi 18:6a4db94011d3 651 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 652 uint16_t tmp1 = 0;
sahilmgandhi 18:6a4db94011d3 653 uint32_t tmp2 = 0;
sahilmgandhi 18:6a4db94011d3 654 uint8_t optiontmp = 0;
sahilmgandhi 18:6a4db94011d3 655 uint16_t optiontmp2 = 0;
sahilmgandhi 18:6a4db94011d3 656
sahilmgandhi 18:6a4db94011d3 657 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 658
sahilmgandhi 18:6a4db94011d3 659 /* Mask RDP Byte */
sahilmgandhi 18:6a4db94011d3 660 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
sahilmgandhi 18:6a4db94011d3 661
sahilmgandhi 18:6a4db94011d3 662 /* Update Option Byte */
sahilmgandhi 18:6a4db94011d3 663 optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
sahilmgandhi 18:6a4db94011d3 664
sahilmgandhi 18:6a4db94011d3 665 /* calculate the option byte to write */
sahilmgandhi 18:6a4db94011d3 666 tmp1 = (uint16_t)(~(optiontmp2 ));
sahilmgandhi 18:6a4db94011d3 667 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
sahilmgandhi 18:6a4db94011d3 668
sahilmgandhi 18:6a4db94011d3 669 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 670 {
sahilmgandhi 18:6a4db94011d3 671 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 672 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 673
sahilmgandhi 18:6a4db94011d3 674 /* program PCRop */
sahilmgandhi 18:6a4db94011d3 675 OB->RDP = tmp2;
sahilmgandhi 18:6a4db94011d3 676
sahilmgandhi 18:6a4db94011d3 677 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 678 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 679 }
sahilmgandhi 18:6a4db94011d3 680
sahilmgandhi 18:6a4db94011d3 681 /* Return the Read protection operation Status */
sahilmgandhi 18:6a4db94011d3 682 return status;
sahilmgandhi 18:6a4db94011d3 683 }
sahilmgandhi 18:6a4db94011d3 684
sahilmgandhi 18:6a4db94011d3 685 /**
sahilmgandhi 18:6a4db94011d3 686 * @brief Deselect the Protection Mode (SPRMOD).
sahilmgandhi 18:6a4db94011d3 687 * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
sahilmgandhi 18:6a4db94011d3 688 * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
sahilmgandhi 18:6a4db94011d3 689 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
sahilmgandhi 18:6a4db94011d3 690 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 691 */
sahilmgandhi 18:6a4db94011d3 692 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
sahilmgandhi 18:6a4db94011d3 693 {
sahilmgandhi 18:6a4db94011d3 694 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 695 uint16_t tmp1 = 0;
sahilmgandhi 18:6a4db94011d3 696 uint32_t tmp2 = 0;
sahilmgandhi 18:6a4db94011d3 697 uint8_t optiontmp = 0;
sahilmgandhi 18:6a4db94011d3 698 uint16_t optiontmp2 = 0;
sahilmgandhi 18:6a4db94011d3 699
sahilmgandhi 18:6a4db94011d3 700 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 701
sahilmgandhi 18:6a4db94011d3 702 /* Mask RDP Byte */
sahilmgandhi 18:6a4db94011d3 703 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
sahilmgandhi 18:6a4db94011d3 704
sahilmgandhi 18:6a4db94011d3 705 /* Update Option Byte */
sahilmgandhi 18:6a4db94011d3 706 optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
sahilmgandhi 18:6a4db94011d3 707
sahilmgandhi 18:6a4db94011d3 708 /* calculate the option byte to write */
sahilmgandhi 18:6a4db94011d3 709 tmp1 = (uint16_t)(~(optiontmp2 ));
sahilmgandhi 18:6a4db94011d3 710 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
sahilmgandhi 18:6a4db94011d3 711
sahilmgandhi 18:6a4db94011d3 712 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 713 {
sahilmgandhi 18:6a4db94011d3 714 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 715 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 716
sahilmgandhi 18:6a4db94011d3 717 /* program PCRop */
sahilmgandhi 18:6a4db94011d3 718 OB->RDP = tmp2;
sahilmgandhi 18:6a4db94011d3 719
sahilmgandhi 18:6a4db94011d3 720 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 721 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 722 }
sahilmgandhi 18:6a4db94011d3 723
sahilmgandhi 18:6a4db94011d3 724 /* Return the Read protection operation Status */
sahilmgandhi 18:6a4db94011d3 725 return status;
sahilmgandhi 18:6a4db94011d3 726 }
sahilmgandhi 18:6a4db94011d3 727
sahilmgandhi 18:6a4db94011d3 728 #endif /* FLASH_OBR_SPRMOD */
sahilmgandhi 18:6a4db94011d3 729
sahilmgandhi 18:6a4db94011d3 730 /**
sahilmgandhi 18:6a4db94011d3 731 * @}
sahilmgandhi 18:6a4db94011d3 732 */
sahilmgandhi 18:6a4db94011d3 733
sahilmgandhi 18:6a4db94011d3 734 /** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
sahilmgandhi 18:6a4db94011d3 735 * @brief DATA EEPROM Programming functions
sahilmgandhi 18:6a4db94011d3 736 *
sahilmgandhi 18:6a4db94011d3 737 @verbatim
sahilmgandhi 18:6a4db94011d3 738 ===============================================================================
sahilmgandhi 18:6a4db94011d3 739 ##### DATA EEPROM Programming functions #####
sahilmgandhi 18:6a4db94011d3 740 ===============================================================================
sahilmgandhi 18:6a4db94011d3 741
sahilmgandhi 18:6a4db94011d3 742 [..] Any operation of erase or program should follow these steps:
sahilmgandhi 18:6a4db94011d3 743 (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
sahilmgandhi 18:6a4db94011d3 744 and Flash program erase control register access.
sahilmgandhi 18:6a4db94011d3 745 (#) Call the desired function to erase or program data.
sahilmgandhi 18:6a4db94011d3 746 (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
sahilmgandhi 18:6a4db94011d3 747 and Flash program erase control register access(recommended
sahilmgandhi 18:6a4db94011d3 748 to protect the DATA_EEPROM against possible unwanted operation).
sahilmgandhi 18:6a4db94011d3 749
sahilmgandhi 18:6a4db94011d3 750 @endverbatim
sahilmgandhi 18:6a4db94011d3 751 * @{
sahilmgandhi 18:6a4db94011d3 752 */
sahilmgandhi 18:6a4db94011d3 753
sahilmgandhi 18:6a4db94011d3 754 /**
sahilmgandhi 18:6a4db94011d3 755 * @brief Unlocks the data memory and FLASH_PECR register access.
sahilmgandhi 18:6a4db94011d3 756 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 757 */
sahilmgandhi 18:6a4db94011d3 758 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
sahilmgandhi 18:6a4db94011d3 759 {
sahilmgandhi 18:6a4db94011d3 760 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
sahilmgandhi 18:6a4db94011d3 761 {
sahilmgandhi 18:6a4db94011d3 762 /* Unlocking the Data memory and FLASH_PECR register access*/
sahilmgandhi 18:6a4db94011d3 763 FLASH->PEKEYR = FLASH_PEKEY1;
sahilmgandhi 18:6a4db94011d3 764 FLASH->PEKEYR = FLASH_PEKEY2;
sahilmgandhi 18:6a4db94011d3 765 }
sahilmgandhi 18:6a4db94011d3 766 else
sahilmgandhi 18:6a4db94011d3 767 {
sahilmgandhi 18:6a4db94011d3 768 return HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 769 }
sahilmgandhi 18:6a4db94011d3 770 return HAL_OK;
sahilmgandhi 18:6a4db94011d3 771 }
sahilmgandhi 18:6a4db94011d3 772
sahilmgandhi 18:6a4db94011d3 773 /**
sahilmgandhi 18:6a4db94011d3 774 * @brief Locks the Data memory and FLASH_PECR register access.
sahilmgandhi 18:6a4db94011d3 775 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 776 */
sahilmgandhi 18:6a4db94011d3 777 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
sahilmgandhi 18:6a4db94011d3 778 {
sahilmgandhi 18:6a4db94011d3 779 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
sahilmgandhi 18:6a4db94011d3 780 SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
sahilmgandhi 18:6a4db94011d3 781
sahilmgandhi 18:6a4db94011d3 782 return HAL_OK;
sahilmgandhi 18:6a4db94011d3 783 }
sahilmgandhi 18:6a4db94011d3 784
sahilmgandhi 18:6a4db94011d3 785 /**
sahilmgandhi 18:6a4db94011d3 786 * @brief Erase a word in data memory.
sahilmgandhi 18:6a4db94011d3 787 * @param Address specifies the address to be erased.
sahilmgandhi 18:6a4db94011d3 788 * @param TypeErase Indicate the way to erase at a specified address.
sahilmgandhi 18:6a4db94011d3 789 * This parameter can be a value of @ref FLASH_Type_Program
sahilmgandhi 18:6a4db94011d3 790 * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
sahilmgandhi 18:6a4db94011d3 791 * must be called before.
sahilmgandhi 18:6a4db94011d3 792 * Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
sahilmgandhi 18:6a4db94011d3 793 * and Flash program erase control register access(recommended to protect
sahilmgandhi 18:6a4db94011d3 794 * the DATA_EEPROM against possible unwanted operation).
sahilmgandhi 18:6a4db94011d3 795 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 796 */
sahilmgandhi 18:6a4db94011d3 797 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
sahilmgandhi 18:6a4db94011d3 798 {
sahilmgandhi 18:6a4db94011d3 799 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 800
sahilmgandhi 18:6a4db94011d3 801 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 802 assert_param(IS_TYPEPROGRAMDATA(TypeErase));
sahilmgandhi 18:6a4db94011d3 803 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 804
sahilmgandhi 18:6a4db94011d3 805 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 806 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 807
sahilmgandhi 18:6a4db94011d3 808 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 809 {
sahilmgandhi 18:6a4db94011d3 810 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 811 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 812
sahilmgandhi 18:6a4db94011d3 813 if(TypeErase == FLASH_TYPEERASEDATA_WORD)
sahilmgandhi 18:6a4db94011d3 814 {
sahilmgandhi 18:6a4db94011d3 815 /* Write 00000000h to valid address in the data memory */
sahilmgandhi 18:6a4db94011d3 816 *(__IO uint32_t *) Address = 0x00000000;
sahilmgandhi 18:6a4db94011d3 817 }
sahilmgandhi 18:6a4db94011d3 818
sahilmgandhi 18:6a4db94011d3 819 if(TypeErase == FLASH_TYPEERASEDATA_HALFWORD)
sahilmgandhi 18:6a4db94011d3 820 {
sahilmgandhi 18:6a4db94011d3 821 /* Write 0000h to valid address in the data memory */
sahilmgandhi 18:6a4db94011d3 822 *(__IO uint16_t *) Address = (uint16_t)0x0000;
sahilmgandhi 18:6a4db94011d3 823 }
sahilmgandhi 18:6a4db94011d3 824
sahilmgandhi 18:6a4db94011d3 825 if(TypeErase == FLASH_TYPEERASEDATA_BYTE)
sahilmgandhi 18:6a4db94011d3 826 {
sahilmgandhi 18:6a4db94011d3 827 /* Write 00h to valid address in the data memory */
sahilmgandhi 18:6a4db94011d3 828 *(__IO uint8_t *) Address = (uint8_t)0x00;
sahilmgandhi 18:6a4db94011d3 829 }
sahilmgandhi 18:6a4db94011d3 830
sahilmgandhi 18:6a4db94011d3 831 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 832 }
sahilmgandhi 18:6a4db94011d3 833
sahilmgandhi 18:6a4db94011d3 834 /* Return the erase status */
sahilmgandhi 18:6a4db94011d3 835 return status;
sahilmgandhi 18:6a4db94011d3 836 }
sahilmgandhi 18:6a4db94011d3 837
sahilmgandhi 18:6a4db94011d3 838 /**
sahilmgandhi 18:6a4db94011d3 839 * @brief Program word at a specified address
sahilmgandhi 18:6a4db94011d3 840 * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
sahilmgandhi 18:6a4db94011d3 841 * must be called before.
sahilmgandhi 18:6a4db94011d3 842 * Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
sahilmgandhi 18:6a4db94011d3 843 * and Flash program erase control register access(recommended to protect
sahilmgandhi 18:6a4db94011d3 844 * the DATA_EEPROM against possible unwanted operation).
sahilmgandhi 18:6a4db94011d3 845 * @note The function @ref HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
sahilmgandhi 18:6a4db94011d3 846 * this function to configure the Fixed Time Programming.
sahilmgandhi 18:6a4db94011d3 847 * @param TypeProgram Indicate the way to program at a specified address.
sahilmgandhi 18:6a4db94011d3 848 * This parameter can be a value of @ref FLASHEx_Type_Program_Data
sahilmgandhi 18:6a4db94011d3 849 * @param Address specifies the address to be programmed.
sahilmgandhi 18:6a4db94011d3 850 * @param Data specifies the data to be programmed
sahilmgandhi 18:6a4db94011d3 851 *
sahilmgandhi 18:6a4db94011d3 852 * @retval HAL_StatusTypeDef HAL Status
sahilmgandhi 18:6a4db94011d3 853 */
sahilmgandhi 18:6a4db94011d3 854
sahilmgandhi 18:6a4db94011d3 855 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
sahilmgandhi 18:6a4db94011d3 856 {
sahilmgandhi 18:6a4db94011d3 857 HAL_StatusTypeDef status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 858
sahilmgandhi 18:6a4db94011d3 859 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 860 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 861
sahilmgandhi 18:6a4db94011d3 862 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 863 assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
sahilmgandhi 18:6a4db94011d3 864
sahilmgandhi 18:6a4db94011d3 865 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 866 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 867
sahilmgandhi 18:6a4db94011d3 868 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 869 {
sahilmgandhi 18:6a4db94011d3 870 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 871 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 872
sahilmgandhi 18:6a4db94011d3 873 if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTBYTE)
sahilmgandhi 18:6a4db94011d3 874 {
sahilmgandhi 18:6a4db94011d3 875 /*Program word (8-bit) at a specified address.*/
sahilmgandhi 18:6a4db94011d3 876 status = FLASH_DATAEEPROM_FastProgramByte(Address, (uint8_t) Data);
sahilmgandhi 18:6a4db94011d3 877 }
sahilmgandhi 18:6a4db94011d3 878
sahilmgandhi 18:6a4db94011d3 879 if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTHALFWORD)
sahilmgandhi 18:6a4db94011d3 880 {
sahilmgandhi 18:6a4db94011d3 881 /* Program halfword (16-bit) at a specified address.*/
sahilmgandhi 18:6a4db94011d3 882 status = FLASH_DATAEEPROM_FastProgramHalfWord(Address, (uint16_t) Data);
sahilmgandhi 18:6a4db94011d3 883 }
sahilmgandhi 18:6a4db94011d3 884
sahilmgandhi 18:6a4db94011d3 885 if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTWORD)
sahilmgandhi 18:6a4db94011d3 886 {
sahilmgandhi 18:6a4db94011d3 887 /* Program word (32-bit) at a specified address.*/
sahilmgandhi 18:6a4db94011d3 888 status = FLASH_DATAEEPROM_FastProgramWord(Address, (uint32_t) Data);
sahilmgandhi 18:6a4db94011d3 889 }
sahilmgandhi 18:6a4db94011d3 890
sahilmgandhi 18:6a4db94011d3 891 if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
sahilmgandhi 18:6a4db94011d3 892 {
sahilmgandhi 18:6a4db94011d3 893 /* Program word (32-bit) at a specified address.*/
sahilmgandhi 18:6a4db94011d3 894 status = FLASH_DATAEEPROM_ProgramWord(Address, (uint32_t) Data);
sahilmgandhi 18:6a4db94011d3 895 }
sahilmgandhi 18:6a4db94011d3 896
sahilmgandhi 18:6a4db94011d3 897 if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
sahilmgandhi 18:6a4db94011d3 898 {
sahilmgandhi 18:6a4db94011d3 899 /* Program halfword (16-bit) at a specified address.*/
sahilmgandhi 18:6a4db94011d3 900 status = FLASH_DATAEEPROM_ProgramHalfWord(Address, (uint16_t) Data);
sahilmgandhi 18:6a4db94011d3 901 }
sahilmgandhi 18:6a4db94011d3 902
sahilmgandhi 18:6a4db94011d3 903 if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
sahilmgandhi 18:6a4db94011d3 904 {
sahilmgandhi 18:6a4db94011d3 905 /* Program byte (8-bit) at a specified address.*/
sahilmgandhi 18:6a4db94011d3 906 status = FLASH_DATAEEPROM_ProgramByte(Address, (uint8_t) Data);
sahilmgandhi 18:6a4db94011d3 907 }
sahilmgandhi 18:6a4db94011d3 908 }
sahilmgandhi 18:6a4db94011d3 909
sahilmgandhi 18:6a4db94011d3 910 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 911 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 912
sahilmgandhi 18:6a4db94011d3 913 return status;
sahilmgandhi 18:6a4db94011d3 914 }
sahilmgandhi 18:6a4db94011d3 915
sahilmgandhi 18:6a4db94011d3 916 /**
sahilmgandhi 18:6a4db94011d3 917 * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
sahilmgandhi 18:6a4db94011d3 918 * @retval None
sahilmgandhi 18:6a4db94011d3 919 */
sahilmgandhi 18:6a4db94011d3 920 void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
sahilmgandhi 18:6a4db94011d3 921 {
sahilmgandhi 18:6a4db94011d3 922 SET_BIT(FLASH->PECR, FLASH_PECR_FTDW);
sahilmgandhi 18:6a4db94011d3 923 }
sahilmgandhi 18:6a4db94011d3 924
sahilmgandhi 18:6a4db94011d3 925 /**
sahilmgandhi 18:6a4db94011d3 926 * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
sahilmgandhi 18:6a4db94011d3 927 * @retval None
sahilmgandhi 18:6a4db94011d3 928 */
sahilmgandhi 18:6a4db94011d3 929 void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
sahilmgandhi 18:6a4db94011d3 930 {
sahilmgandhi 18:6a4db94011d3 931 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
sahilmgandhi 18:6a4db94011d3 932 }
sahilmgandhi 18:6a4db94011d3 933
sahilmgandhi 18:6a4db94011d3 934 /**
sahilmgandhi 18:6a4db94011d3 935 * @}
sahilmgandhi 18:6a4db94011d3 936 */
sahilmgandhi 18:6a4db94011d3 937
sahilmgandhi 18:6a4db94011d3 938 /**
sahilmgandhi 18:6a4db94011d3 939 * @}
sahilmgandhi 18:6a4db94011d3 940 */
sahilmgandhi 18:6a4db94011d3 941
sahilmgandhi 18:6a4db94011d3 942 /** @addtogroup FLASHEx_Private_Functions
sahilmgandhi 18:6a4db94011d3 943 * @{
sahilmgandhi 18:6a4db94011d3 944 */
sahilmgandhi 18:6a4db94011d3 945
sahilmgandhi 18:6a4db94011d3 946 /*
sahilmgandhi 18:6a4db94011d3 947 ==============================================================================
sahilmgandhi 18:6a4db94011d3 948 OPTIONS BYTES
sahilmgandhi 18:6a4db94011d3 949 ==============================================================================
sahilmgandhi 18:6a4db94011d3 950 */
sahilmgandhi 18:6a4db94011d3 951 /**
sahilmgandhi 18:6a4db94011d3 952 * @brief Enables or disables the read out protection.
sahilmgandhi 18:6a4db94011d3 953 * @note To correctly run this function, the @ref HAL_FLASH_OB_Unlock() function
sahilmgandhi 18:6a4db94011d3 954 * must be called before.
sahilmgandhi 18:6a4db94011d3 955 * @param OB_RDP specifies the read protection level.
sahilmgandhi 18:6a4db94011d3 956 * This parameter can be:
sahilmgandhi 18:6a4db94011d3 957 * @arg @ref OB_RDP_LEVEL_0 No protection
sahilmgandhi 18:6a4db94011d3 958 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
sahilmgandhi 18:6a4db94011d3 959 * @arg @ref OB_RDP_LEVEL_2 Chip protection
sahilmgandhi 18:6a4db94011d3 960 *
sahilmgandhi 18:6a4db94011d3 961 * !!!Warning!!! When enabling OB_RDP_LEVEL_2 it's no more possible to go back to level 1 or 0
sahilmgandhi 18:6a4db94011d3 962 *
sahilmgandhi 18:6a4db94011d3 963 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 964 */
sahilmgandhi 18:6a4db94011d3 965 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
sahilmgandhi 18:6a4db94011d3 966 {
sahilmgandhi 18:6a4db94011d3 967 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 968 uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
sahilmgandhi 18:6a4db94011d3 969
sahilmgandhi 18:6a4db94011d3 970 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 971 assert_param(IS_OB_RDP(OB_RDP));
sahilmgandhi 18:6a4db94011d3 972
sahilmgandhi 18:6a4db94011d3 973 tmp1 = (uint32_t)(OB->RDP & FLASH_OBR_RDPRT);
sahilmgandhi 18:6a4db94011d3 974
sahilmgandhi 18:6a4db94011d3 975 /* According to errata sheet, DocID022054 Rev 5, par2.1.5
sahilmgandhi 18:6a4db94011d3 976 Before setting Level0 in the RDP register, check that the current level is not equal to Level0.
sahilmgandhi 18:6a4db94011d3 977 If the current level is not equal to Level0, Level0 can be activated.
sahilmgandhi 18:6a4db94011d3 978 If the current level is Level0 then the RDP register must not be written again with Level0. */
sahilmgandhi 18:6a4db94011d3 979
sahilmgandhi 18:6a4db94011d3 980 if ((tmp1 == OB_RDP_LEVEL_0) && (OB_RDP == OB_RDP_LEVEL_0))
sahilmgandhi 18:6a4db94011d3 981 {
sahilmgandhi 18:6a4db94011d3 982 /*current level is Level0 then the RDP register must not be written again with Level0. */
sahilmgandhi 18:6a4db94011d3 983 status = HAL_ERROR;
sahilmgandhi 18:6a4db94011d3 984 }
sahilmgandhi 18:6a4db94011d3 985 else
sahilmgandhi 18:6a4db94011d3 986 {
sahilmgandhi 18:6a4db94011d3 987 #if defined(FLASH_OBR_SPRMOD)
sahilmgandhi 18:6a4db94011d3 988 /* Mask SPRMOD bit */
sahilmgandhi 18:6a4db94011d3 989 tmp3 = (uint32_t)(OB->RDP & FLASH_OBR_SPRMOD);
sahilmgandhi 18:6a4db94011d3 990 #endif
sahilmgandhi 18:6a4db94011d3 991
sahilmgandhi 18:6a4db94011d3 992 /* calculate the option byte to write */
sahilmgandhi 18:6a4db94011d3 993 tmp1 = (~((uint32_t)(OB_RDP | tmp3)));
sahilmgandhi 18:6a4db94011d3 994 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)(OB_RDP | tmp3)));
sahilmgandhi 18:6a4db94011d3 995
sahilmgandhi 18:6a4db94011d3 996 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 997 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 998
sahilmgandhi 18:6a4db94011d3 999 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1000 {
sahilmgandhi 18:6a4db94011d3 1001 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1002 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1003
sahilmgandhi 18:6a4db94011d3 1004 /* program read protection level */
sahilmgandhi 18:6a4db94011d3 1005 OB->RDP = tmp2;
sahilmgandhi 18:6a4db94011d3 1006
sahilmgandhi 18:6a4db94011d3 1007 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1008 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1009 }
sahilmgandhi 18:6a4db94011d3 1010 }
sahilmgandhi 18:6a4db94011d3 1011
sahilmgandhi 18:6a4db94011d3 1012 /* Return the Read protection operation Status */
sahilmgandhi 18:6a4db94011d3 1013 return status;
sahilmgandhi 18:6a4db94011d3 1014 }
sahilmgandhi 18:6a4db94011d3 1015
sahilmgandhi 18:6a4db94011d3 1016 /**
sahilmgandhi 18:6a4db94011d3 1017 * @brief Programs the FLASH brownout reset threshold level Option Byte.
sahilmgandhi 18:6a4db94011d3 1018 * @param OB_BOR Selects the brownout reset threshold level.
sahilmgandhi 18:6a4db94011d3 1019 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1020 * @arg @ref OB_BOR_OFF BOR is disabled at power down, the reset is asserted when the VDD
sahilmgandhi 18:6a4db94011d3 1021 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
sahilmgandhi 18:6a4db94011d3 1022 * @arg @ref OB_BOR_LEVEL1 BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
sahilmgandhi 18:6a4db94011d3 1023 * @arg @ref OB_BOR_LEVEL2 BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
sahilmgandhi 18:6a4db94011d3 1024 * @arg @ref OB_BOR_LEVEL3 BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
sahilmgandhi 18:6a4db94011d3 1025 * @arg @ref OB_BOR_LEVEL4 BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
sahilmgandhi 18:6a4db94011d3 1026 * @arg @ref OB_BOR_LEVEL5 BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
sahilmgandhi 18:6a4db94011d3 1027 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1028 */
sahilmgandhi 18:6a4db94011d3 1029 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
sahilmgandhi 18:6a4db94011d3 1030 {
sahilmgandhi 18:6a4db94011d3 1031 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1032 uint32_t tmp = 0, tmp1 = 0;
sahilmgandhi 18:6a4db94011d3 1033
sahilmgandhi 18:6a4db94011d3 1034 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1035 assert_param(IS_OB_BOR_LEVEL(OB_BOR));
sahilmgandhi 18:6a4db94011d3 1036
sahilmgandhi 18:6a4db94011d3 1037 /* Get the User Option byte register */
sahilmgandhi 18:6a4db94011d3 1038 tmp1 = OB->USER & ((~FLASH_OBR_BOR_LEV) >> 16);
sahilmgandhi 18:6a4db94011d3 1039
sahilmgandhi 18:6a4db94011d3 1040 /* Calculate the option byte to write - [0xFF | nUSER | 0x00 | USER]*/
sahilmgandhi 18:6a4db94011d3 1041 tmp = (uint32_t)~((OB_BOR | tmp1)) << 16;
sahilmgandhi 18:6a4db94011d3 1042 tmp |= (OB_BOR | tmp1);
sahilmgandhi 18:6a4db94011d3 1043
sahilmgandhi 18:6a4db94011d3 1044 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1045 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1046
sahilmgandhi 18:6a4db94011d3 1047 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1048 {
sahilmgandhi 18:6a4db94011d3 1049 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1050 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1051
sahilmgandhi 18:6a4db94011d3 1052 /* Write the BOR Option Byte */
sahilmgandhi 18:6a4db94011d3 1053 OB->USER = tmp;
sahilmgandhi 18:6a4db94011d3 1054
sahilmgandhi 18:6a4db94011d3 1055 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1056 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1057 }
sahilmgandhi 18:6a4db94011d3 1058
sahilmgandhi 18:6a4db94011d3 1059 /* Return the Option Byte BOR programmation Status */
sahilmgandhi 18:6a4db94011d3 1060 return status;
sahilmgandhi 18:6a4db94011d3 1061 }
sahilmgandhi 18:6a4db94011d3 1062
sahilmgandhi 18:6a4db94011d3 1063 /**
sahilmgandhi 18:6a4db94011d3 1064 * @brief Returns the FLASH User Option Bytes values.
sahilmgandhi 18:6a4db94011d3 1065 * @retval The FLASH User Option Bytes.
sahilmgandhi 18:6a4db94011d3 1066 */
sahilmgandhi 18:6a4db94011d3 1067 static uint8_t FLASH_OB_GetUser(void)
sahilmgandhi 18:6a4db94011d3 1068 {
sahilmgandhi 18:6a4db94011d3 1069 /* Return the User Option Byte */
sahilmgandhi 18:6a4db94011d3 1070 return (uint8_t)((FLASH->OBR & FLASH_OBR_USER) >> 16);
sahilmgandhi 18:6a4db94011d3 1071 }
sahilmgandhi 18:6a4db94011d3 1072
sahilmgandhi 18:6a4db94011d3 1073 /**
sahilmgandhi 18:6a4db94011d3 1074 * @brief Returns the FLASH Read Protection level.
sahilmgandhi 18:6a4db94011d3 1075 * @retval FLASH RDP level
sahilmgandhi 18:6a4db94011d3 1076 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1077 * @arg @ref OB_RDP_LEVEL_0 No protection
sahilmgandhi 18:6a4db94011d3 1078 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
sahilmgandhi 18:6a4db94011d3 1079 * @arg @ref OB_RDP_LEVEL_2 Full chip protection
sahilmgandhi 18:6a4db94011d3 1080 */
sahilmgandhi 18:6a4db94011d3 1081 static uint8_t FLASH_OB_GetRDP(void)
sahilmgandhi 18:6a4db94011d3 1082 {
sahilmgandhi 18:6a4db94011d3 1083 return (uint8_t)(FLASH->OBR & FLASH_OBR_RDPRT);
sahilmgandhi 18:6a4db94011d3 1084 }
sahilmgandhi 18:6a4db94011d3 1085
sahilmgandhi 18:6a4db94011d3 1086 /**
sahilmgandhi 18:6a4db94011d3 1087 * @brief Returns the FLASH BOR level.
sahilmgandhi 18:6a4db94011d3 1088 * @retval The BOR level Option Bytes.
sahilmgandhi 18:6a4db94011d3 1089 */
sahilmgandhi 18:6a4db94011d3 1090 static uint8_t FLASH_OB_GetBOR(void)
sahilmgandhi 18:6a4db94011d3 1091 {
sahilmgandhi 18:6a4db94011d3 1092 /* Return the BOR level */
sahilmgandhi 18:6a4db94011d3 1093 return (uint8_t)((FLASH->OBR & (uint32_t)FLASH_OBR_BOR_LEV) >> 16);
sahilmgandhi 18:6a4db94011d3 1094 }
sahilmgandhi 18:6a4db94011d3 1095
sahilmgandhi 18:6a4db94011d3 1096 /**
sahilmgandhi 18:6a4db94011d3 1097 * @brief Write protects the desired pages of the first 64KB of the Flash.
sahilmgandhi 18:6a4db94011d3 1098 * @param pOBInit pointer to an FLASH_OBInitStruct structure that
sahilmgandhi 18:6a4db94011d3 1099 * contains WRP parameters.
sahilmgandhi 18:6a4db94011d3 1100 * @param NewState new state of the specified FLASH Pages Wtite protection.
sahilmgandhi 18:6a4db94011d3 1101 * This parameter can be: ENABLE or DISABLE.
sahilmgandhi 18:6a4db94011d3 1102 * @retval HAL_StatusTypeDef
sahilmgandhi 18:6a4db94011d3 1103 */
sahilmgandhi 18:6a4db94011d3 1104 static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState)
sahilmgandhi 18:6a4db94011d3 1105 {
sahilmgandhi 18:6a4db94011d3 1106 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1107
sahilmgandhi 18:6a4db94011d3 1108 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1109 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1110
sahilmgandhi 18:6a4db94011d3 1111 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1112 {
sahilmgandhi 18:6a4db94011d3 1113 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1114 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1115
sahilmgandhi 18:6a4db94011d3 1116 /* WRP for sector between 0 to 31 */
sahilmgandhi 18:6a4db94011d3 1117 if (pOBInit->WRPSector0To31 != 0)
sahilmgandhi 18:6a4db94011d3 1118 {
sahilmgandhi 18:6a4db94011d3 1119 FLASH_OB_WRPConfigWRP1OrPCROP1(pOBInit->WRPSector0To31, NewState);
sahilmgandhi 18:6a4db94011d3 1120 }
sahilmgandhi 18:6a4db94011d3 1121
sahilmgandhi 18:6a4db94011d3 1122 #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
sahilmgandhi 18:6a4db94011d3 1123 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
sahilmgandhi 18:6a4db94011d3 1124 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
sahilmgandhi 18:6a4db94011d3 1125 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 1126
sahilmgandhi 18:6a4db94011d3 1127 /* Pages for Cat3, Cat4 & Cat5 devices*/
sahilmgandhi 18:6a4db94011d3 1128 /* WRP for sector between 32 to 63 */
sahilmgandhi 18:6a4db94011d3 1129 if (pOBInit->WRPSector32To63 != 0)
sahilmgandhi 18:6a4db94011d3 1130 {
sahilmgandhi 18:6a4db94011d3 1131 FLASH_OB_WRPConfigWRP2OrPCROP2(pOBInit->WRPSector32To63, NewState);
sahilmgandhi 18:6a4db94011d3 1132 }
sahilmgandhi 18:6a4db94011d3 1133
sahilmgandhi 18:6a4db94011d3 1134 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 1135
sahilmgandhi 18:6a4db94011d3 1136 #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
sahilmgandhi 18:6a4db94011d3 1137 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
sahilmgandhi 18:6a4db94011d3 1138 || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 1139
sahilmgandhi 18:6a4db94011d3 1140 /* Pages for devices with FLASH >= 256KB*/
sahilmgandhi 18:6a4db94011d3 1141 /* WRP for sector between 64 to 95 */
sahilmgandhi 18:6a4db94011d3 1142 if (pOBInit->WRPSector64To95 != 0)
sahilmgandhi 18:6a4db94011d3 1143 {
sahilmgandhi 18:6a4db94011d3 1144 FLASH_OB_WRPConfigWRP3(pOBInit->WRPSector64To95, NewState);
sahilmgandhi 18:6a4db94011d3 1145 }
sahilmgandhi 18:6a4db94011d3 1146
sahilmgandhi 18:6a4db94011d3 1147 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 1148
sahilmgandhi 18:6a4db94011d3 1149 #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
sahilmgandhi 18:6a4db94011d3 1150 || defined(STM32L152xDX) || defined(STM32L162xDX)
sahilmgandhi 18:6a4db94011d3 1151
sahilmgandhi 18:6a4db94011d3 1152 /* Pages for Cat5 devices*/
sahilmgandhi 18:6a4db94011d3 1153 /* WRP for sector between 96 to 127 */
sahilmgandhi 18:6a4db94011d3 1154 if (pOBInit->WRPSector96To127 != 0)
sahilmgandhi 18:6a4db94011d3 1155 {
sahilmgandhi 18:6a4db94011d3 1156 FLASH_OB_WRPConfigWRP4(pOBInit->WRPSector96To127, NewState);
sahilmgandhi 18:6a4db94011d3 1157 }
sahilmgandhi 18:6a4db94011d3 1158
sahilmgandhi 18:6a4db94011d3 1159 #endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
sahilmgandhi 18:6a4db94011d3 1160
sahilmgandhi 18:6a4db94011d3 1161 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1162 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1163 }
sahilmgandhi 18:6a4db94011d3 1164
sahilmgandhi 18:6a4db94011d3 1165 /* Return the write protection operation Status */
sahilmgandhi 18:6a4db94011d3 1166 return status;
sahilmgandhi 18:6a4db94011d3 1167 }
sahilmgandhi 18:6a4db94011d3 1168
sahilmgandhi 18:6a4db94011d3 1169 #if defined(STM32L151xBA) || defined(STM32L152xBA) || defined(STM32L151xC) || defined(STM32L152xC) \
sahilmgandhi 18:6a4db94011d3 1170 || defined(STM32L162xC)
sahilmgandhi 18:6a4db94011d3 1171 /**
sahilmgandhi 18:6a4db94011d3 1172 * @brief Enables the read/write protection (PCROP) of the desired
sahilmgandhi 18:6a4db94011d3 1173 * sectors.
sahilmgandhi 18:6a4db94011d3 1174 * @note This function can be used only for Cat2 & Cat3 devices
sahilmgandhi 18:6a4db94011d3 1175 * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
sahilmgandhi 18:6a4db94011d3 1176 * contains PCROP parameters.
sahilmgandhi 18:6a4db94011d3 1177 * @param NewState new state of the specified FLASH Pages read/Write protection.
sahilmgandhi 18:6a4db94011d3 1178 * This parameter can be: ENABLE or DISABLE.
sahilmgandhi 18:6a4db94011d3 1179 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1180 */
sahilmgandhi 18:6a4db94011d3 1181 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState)
sahilmgandhi 18:6a4db94011d3 1182 {
sahilmgandhi 18:6a4db94011d3 1183 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1184 FunctionalState pcropstate = DISABLE;
sahilmgandhi 18:6a4db94011d3 1185
sahilmgandhi 18:6a4db94011d3 1186 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1187 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1188
sahilmgandhi 18:6a4db94011d3 1189 /* Invert state to use same function of WRP */
sahilmgandhi 18:6a4db94011d3 1190 if (NewState == DISABLE)
sahilmgandhi 18:6a4db94011d3 1191 {
sahilmgandhi 18:6a4db94011d3 1192 pcropstate = ENABLE;
sahilmgandhi 18:6a4db94011d3 1193 }
sahilmgandhi 18:6a4db94011d3 1194
sahilmgandhi 18:6a4db94011d3 1195 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1196 {
sahilmgandhi 18:6a4db94011d3 1197 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1198 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1199
sahilmgandhi 18:6a4db94011d3 1200 /* Pages for Cat2 devices*/
sahilmgandhi 18:6a4db94011d3 1201 /* PCROP for sector between 0 to 31 */
sahilmgandhi 18:6a4db94011d3 1202 if (pAdvOBInit->PCROPSector0To31 != 0)
sahilmgandhi 18:6a4db94011d3 1203 {
sahilmgandhi 18:6a4db94011d3 1204 FLASH_OB_WRPConfigWRP1OrPCROP1(pAdvOBInit->PCROPSector0To31, pcropstate);
sahilmgandhi 18:6a4db94011d3 1205 }
sahilmgandhi 18:6a4db94011d3 1206
sahilmgandhi 18:6a4db94011d3 1207 #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
sahilmgandhi 18:6a4db94011d3 1208
sahilmgandhi 18:6a4db94011d3 1209 /* Pages for Cat3 devices*/
sahilmgandhi 18:6a4db94011d3 1210 /* WRP for sector between 32 to 63 */
sahilmgandhi 18:6a4db94011d3 1211 if (pAdvOBInit->PCROPSector32To63 != 0)
sahilmgandhi 18:6a4db94011d3 1212 {
sahilmgandhi 18:6a4db94011d3 1213 FLASH_OB_WRPConfigWRP2OrPCROP2(pAdvOBInit->PCROPSector32To63, pcropstate);
sahilmgandhi 18:6a4db94011d3 1214 }
sahilmgandhi 18:6a4db94011d3 1215
sahilmgandhi 18:6a4db94011d3 1216 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
sahilmgandhi 18:6a4db94011d3 1217
sahilmgandhi 18:6a4db94011d3 1218 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1219 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1220 }
sahilmgandhi 18:6a4db94011d3 1221
sahilmgandhi 18:6a4db94011d3 1222 /* Return the write protection operation Status */
sahilmgandhi 18:6a4db94011d3 1223 return status;
sahilmgandhi 18:6a4db94011d3 1224 }
sahilmgandhi 18:6a4db94011d3 1225 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
sahilmgandhi 18:6a4db94011d3 1226
sahilmgandhi 18:6a4db94011d3 1227 /**
sahilmgandhi 18:6a4db94011d3 1228 * @brief Write protects the desired pages of the first 128KB of the Flash.
sahilmgandhi 18:6a4db94011d3 1229 * @param WRP1OrPCROP1 specifies the address of the pages to be write protected.
sahilmgandhi 18:6a4db94011d3 1230 * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection1
sahilmgandhi 18:6a4db94011d3 1231 * @param NewState new state of the specified FLASH Pages Write protection.
sahilmgandhi 18:6a4db94011d3 1232 * This parameter can be: ENABLE or DISABLE.
sahilmgandhi 18:6a4db94011d3 1233 * @retval None
sahilmgandhi 18:6a4db94011d3 1234 */
sahilmgandhi 18:6a4db94011d3 1235 static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState)
sahilmgandhi 18:6a4db94011d3 1236 {
sahilmgandhi 18:6a4db94011d3 1237 uint32_t wrp01data = 0, wrp23data = 0;
sahilmgandhi 18:6a4db94011d3 1238
sahilmgandhi 18:6a4db94011d3 1239 uint32_t tmp1 = 0, tmp2 = 0;
sahilmgandhi 18:6a4db94011d3 1240
sahilmgandhi 18:6a4db94011d3 1241 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1242 assert_param(IS_OB_WRP(WRP1OrPCROP1));
sahilmgandhi 18:6a4db94011d3 1243 assert_param(IS_FUNCTIONAL_STATE(NewState));
sahilmgandhi 18:6a4db94011d3 1244
sahilmgandhi 18:6a4db94011d3 1245 if (NewState != DISABLE)
sahilmgandhi 18:6a4db94011d3 1246 {
sahilmgandhi 18:6a4db94011d3 1247 wrp01data = (uint16_t)(((WRP1OrPCROP1 & WRP_MASK_LOW) | OB->WRP01));
sahilmgandhi 18:6a4db94011d3 1248 wrp23data = (uint16_t)((((WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 | OB->WRP23)));
sahilmgandhi 18:6a4db94011d3 1249 tmp1 = (uint32_t)(~(wrp01data) << 16)|(wrp01data);
sahilmgandhi 18:6a4db94011d3 1250 OB->WRP01 = tmp1;
sahilmgandhi 18:6a4db94011d3 1251
sahilmgandhi 18:6a4db94011d3 1252 tmp2 = (uint32_t)(~(wrp23data) << 16)|(wrp23data);
sahilmgandhi 18:6a4db94011d3 1253 OB->WRP23 = tmp2;
sahilmgandhi 18:6a4db94011d3 1254 }
sahilmgandhi 18:6a4db94011d3 1255 else
sahilmgandhi 18:6a4db94011d3 1256 {
sahilmgandhi 18:6a4db94011d3 1257 wrp01data = (uint16_t)(~WRP1OrPCROP1 & (WRP_MASK_LOW & OB->WRP01));
sahilmgandhi 18:6a4db94011d3 1258 wrp23data = (uint16_t)((((~WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 & OB->WRP23)));
sahilmgandhi 18:6a4db94011d3 1259
sahilmgandhi 18:6a4db94011d3 1260 tmp1 = (uint32_t)((~wrp01data) << 16)|(wrp01data);
sahilmgandhi 18:6a4db94011d3 1261 OB->WRP01 = tmp1;
sahilmgandhi 18:6a4db94011d3 1262
sahilmgandhi 18:6a4db94011d3 1263 tmp2 = (uint32_t)((~wrp23data) << 16)|(wrp23data);
sahilmgandhi 18:6a4db94011d3 1264 OB->WRP23 = tmp2;
sahilmgandhi 18:6a4db94011d3 1265 }
sahilmgandhi 18:6a4db94011d3 1266 }
sahilmgandhi 18:6a4db94011d3 1267
sahilmgandhi 18:6a4db94011d3 1268 #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
sahilmgandhi 18:6a4db94011d3 1269 || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
sahilmgandhi 18:6a4db94011d3 1270 || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
sahilmgandhi 18:6a4db94011d3 1271 || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 1272 /**
sahilmgandhi 18:6a4db94011d3 1273 * @brief Enable Write protects the desired pages of the second 128KB of the Flash.
sahilmgandhi 18:6a4db94011d3 1274 * @note This function can be used only for Cat3, Cat4 & Cat5 devices.
sahilmgandhi 18:6a4db94011d3 1275 * @param WRP2OrPCROP2 specifies the address of the pages to be write protected.
sahilmgandhi 18:6a4db94011d3 1276 * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection2
sahilmgandhi 18:6a4db94011d3 1277 * @param NewState new state of the specified FLASH Pages Wtite protection.
sahilmgandhi 18:6a4db94011d3 1278 * This parameter can be: ENABLE or DISABLE.
sahilmgandhi 18:6a4db94011d3 1279 * @retval None
sahilmgandhi 18:6a4db94011d3 1280 */
sahilmgandhi 18:6a4db94011d3 1281 static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState)
sahilmgandhi 18:6a4db94011d3 1282 {
sahilmgandhi 18:6a4db94011d3 1283 uint32_t wrp45data = 0, wrp67data = 0;
sahilmgandhi 18:6a4db94011d3 1284
sahilmgandhi 18:6a4db94011d3 1285 uint32_t tmp1 = 0, tmp2 = 0;
sahilmgandhi 18:6a4db94011d3 1286
sahilmgandhi 18:6a4db94011d3 1287 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1288 assert_param(IS_OB_WRP(WRP2OrPCROP2));
sahilmgandhi 18:6a4db94011d3 1289 assert_param(IS_FUNCTIONAL_STATE(NewState));
sahilmgandhi 18:6a4db94011d3 1290
sahilmgandhi 18:6a4db94011d3 1291 if (NewState != DISABLE)
sahilmgandhi 18:6a4db94011d3 1292 {
sahilmgandhi 18:6a4db94011d3 1293 wrp45data = (uint16_t)(((WRP2OrPCROP2 & WRP_MASK_LOW) | OB->WRP45));
sahilmgandhi 18:6a4db94011d3 1294 wrp67data = (uint16_t)((((WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 | OB->WRP67)));
sahilmgandhi 18:6a4db94011d3 1295 tmp1 = (uint32_t)(~(wrp45data) << 16)|(wrp45data);
sahilmgandhi 18:6a4db94011d3 1296 OB->WRP45 = tmp1;
sahilmgandhi 18:6a4db94011d3 1297
sahilmgandhi 18:6a4db94011d3 1298 tmp2 = (uint32_t)(~(wrp67data) << 16)|(wrp67data);
sahilmgandhi 18:6a4db94011d3 1299 OB->WRP67 = tmp2;
sahilmgandhi 18:6a4db94011d3 1300 }
sahilmgandhi 18:6a4db94011d3 1301 else
sahilmgandhi 18:6a4db94011d3 1302 {
sahilmgandhi 18:6a4db94011d3 1303 wrp45data = (uint16_t)(~WRP2OrPCROP2 & (WRP_MASK_LOW & OB->WRP45));
sahilmgandhi 18:6a4db94011d3 1304 wrp67data = (uint16_t)((((~WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 & OB->WRP67)));
sahilmgandhi 18:6a4db94011d3 1305
sahilmgandhi 18:6a4db94011d3 1306 tmp1 = (uint32_t)((~wrp45data) << 16)|(wrp45data);
sahilmgandhi 18:6a4db94011d3 1307 OB->WRP45 = tmp1;
sahilmgandhi 18:6a4db94011d3 1308
sahilmgandhi 18:6a4db94011d3 1309 tmp2 = (uint32_t)((~wrp67data) << 16)|(wrp67data);
sahilmgandhi 18:6a4db94011d3 1310 OB->WRP67 = tmp2;
sahilmgandhi 18:6a4db94011d3 1311 }
sahilmgandhi 18:6a4db94011d3 1312 }
sahilmgandhi 18:6a4db94011d3 1313 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 1314
sahilmgandhi 18:6a4db94011d3 1315 #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
sahilmgandhi 18:6a4db94011d3 1316 || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
sahilmgandhi 18:6a4db94011d3 1317 || defined(STM32L162xE)
sahilmgandhi 18:6a4db94011d3 1318 /**
sahilmgandhi 18:6a4db94011d3 1319 * @brief Enable Write protects the desired pages of the third 128KB of the Flash.
sahilmgandhi 18:6a4db94011d3 1320 * @note This function can be used only for STM32L151xD, STM32L152xD, STM32L162xD & Cat5 devices.
sahilmgandhi 18:6a4db94011d3 1321 * @param WRP3 specifies the address of the pages to be write protected.
sahilmgandhi 18:6a4db94011d3 1322 * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection3
sahilmgandhi 18:6a4db94011d3 1323 * @param NewState new state of the specified FLASH Pages Wtite protection.
sahilmgandhi 18:6a4db94011d3 1324 * This parameter can be: ENABLE or DISABLE.
sahilmgandhi 18:6a4db94011d3 1325 * @retval None
sahilmgandhi 18:6a4db94011d3 1326 */
sahilmgandhi 18:6a4db94011d3 1327 static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState)
sahilmgandhi 18:6a4db94011d3 1328 {
sahilmgandhi 18:6a4db94011d3 1329 uint32_t wrp89data = 0, wrp1011data = 0;
sahilmgandhi 18:6a4db94011d3 1330
sahilmgandhi 18:6a4db94011d3 1331 uint32_t tmp1 = 0, tmp2 = 0;
sahilmgandhi 18:6a4db94011d3 1332
sahilmgandhi 18:6a4db94011d3 1333 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1334 assert_param(IS_OB_WRP(WRP3));
sahilmgandhi 18:6a4db94011d3 1335 assert_param(IS_FUNCTIONAL_STATE(NewState));
sahilmgandhi 18:6a4db94011d3 1336
sahilmgandhi 18:6a4db94011d3 1337 if (NewState != DISABLE)
sahilmgandhi 18:6a4db94011d3 1338 {
sahilmgandhi 18:6a4db94011d3 1339 wrp89data = (uint16_t)(((WRP3 & WRP_MASK_LOW) | OB->WRP89));
sahilmgandhi 18:6a4db94011d3 1340 wrp1011data = (uint16_t)((((WRP3 & WRP_MASK_HIGH)>>16 | OB->WRP1011)));
sahilmgandhi 18:6a4db94011d3 1341 tmp1 = (uint32_t)(~(wrp89data) << 16)|(wrp89data);
sahilmgandhi 18:6a4db94011d3 1342 OB->WRP89 = tmp1;
sahilmgandhi 18:6a4db94011d3 1343
sahilmgandhi 18:6a4db94011d3 1344 tmp2 = (uint32_t)(~(wrp1011data) << 16)|(wrp1011data);
sahilmgandhi 18:6a4db94011d3 1345 OB->WRP1011 = tmp2;
sahilmgandhi 18:6a4db94011d3 1346 }
sahilmgandhi 18:6a4db94011d3 1347 else
sahilmgandhi 18:6a4db94011d3 1348 {
sahilmgandhi 18:6a4db94011d3 1349 wrp89data = (uint16_t)(~WRP3 & (WRP_MASK_LOW & OB->WRP89));
sahilmgandhi 18:6a4db94011d3 1350 wrp1011data = (uint16_t)((((~WRP3 & WRP_MASK_HIGH)>>16 & OB->WRP1011)));
sahilmgandhi 18:6a4db94011d3 1351
sahilmgandhi 18:6a4db94011d3 1352 tmp1 = (uint32_t)((~wrp89data) << 16)|(wrp89data);
sahilmgandhi 18:6a4db94011d3 1353 OB->WRP89 = tmp1;
sahilmgandhi 18:6a4db94011d3 1354
sahilmgandhi 18:6a4db94011d3 1355 tmp2 = (uint32_t)((~wrp1011data) << 16)|(wrp1011data);
sahilmgandhi 18:6a4db94011d3 1356 OB->WRP1011 = tmp2;
sahilmgandhi 18:6a4db94011d3 1357 }
sahilmgandhi 18:6a4db94011d3 1358 }
sahilmgandhi 18:6a4db94011d3 1359 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
sahilmgandhi 18:6a4db94011d3 1360
sahilmgandhi 18:6a4db94011d3 1361 #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
sahilmgandhi 18:6a4db94011d3 1362 || defined(STM32L152xDX) || defined(STM32L162xDX)
sahilmgandhi 18:6a4db94011d3 1363 /**
sahilmgandhi 18:6a4db94011d3 1364 * @brief Enable Write protects the desired pages of the Fourth 128KB of the Flash.
sahilmgandhi 18:6a4db94011d3 1365 * @note This function can be used only for Cat5 & STM32L1xxDX devices.
sahilmgandhi 18:6a4db94011d3 1366 * @param WRP4 specifies the address of the pages to be write protected.
sahilmgandhi 18:6a4db94011d3 1367 * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection4
sahilmgandhi 18:6a4db94011d3 1368 * @param NewState new state of the specified FLASH Pages Wtite protection.
sahilmgandhi 18:6a4db94011d3 1369 * This parameter can be: ENABLE or DISABLE.
sahilmgandhi 18:6a4db94011d3 1370 * @retval None
sahilmgandhi 18:6a4db94011d3 1371 */
sahilmgandhi 18:6a4db94011d3 1372 static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState)
sahilmgandhi 18:6a4db94011d3 1373 {
sahilmgandhi 18:6a4db94011d3 1374 uint32_t wrp1213data = 0, wrp1415data = 0;
sahilmgandhi 18:6a4db94011d3 1375
sahilmgandhi 18:6a4db94011d3 1376 uint32_t tmp1 = 0, tmp2 = 0;
sahilmgandhi 18:6a4db94011d3 1377
sahilmgandhi 18:6a4db94011d3 1378 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1379 assert_param(IS_OB_WRP(WRP4));
sahilmgandhi 18:6a4db94011d3 1380 assert_param(IS_FUNCTIONAL_STATE(NewState));
sahilmgandhi 18:6a4db94011d3 1381
sahilmgandhi 18:6a4db94011d3 1382 if (NewState != DISABLE)
sahilmgandhi 18:6a4db94011d3 1383 {
sahilmgandhi 18:6a4db94011d3 1384 wrp1213data = (uint16_t)(((WRP4 & WRP_MASK_LOW) | OB->WRP1213));
sahilmgandhi 18:6a4db94011d3 1385 wrp1415data = (uint16_t)((((WRP4 & WRP_MASK_HIGH)>>16 | OB->WRP1415)));
sahilmgandhi 18:6a4db94011d3 1386 tmp1 = (uint32_t)(~(wrp1213data) << 16)|(wrp1213data);
sahilmgandhi 18:6a4db94011d3 1387 OB->WRP1213 = tmp1;
sahilmgandhi 18:6a4db94011d3 1388
sahilmgandhi 18:6a4db94011d3 1389 tmp2 = (uint32_t)(~(wrp1415data) << 16)|(wrp1415data);
sahilmgandhi 18:6a4db94011d3 1390 OB->WRP1415 = tmp2;
sahilmgandhi 18:6a4db94011d3 1391 }
sahilmgandhi 18:6a4db94011d3 1392 else
sahilmgandhi 18:6a4db94011d3 1393 {
sahilmgandhi 18:6a4db94011d3 1394 wrp1213data = (uint16_t)(~WRP4 & (WRP_MASK_LOW & OB->WRP1213));
sahilmgandhi 18:6a4db94011d3 1395 wrp1415data = (uint16_t)((((~WRP4 & WRP_MASK_HIGH)>>16 & OB->WRP1415)));
sahilmgandhi 18:6a4db94011d3 1396
sahilmgandhi 18:6a4db94011d3 1397 tmp1 = (uint32_t)((~wrp1213data) << 16)|(wrp1213data);
sahilmgandhi 18:6a4db94011d3 1398 OB->WRP1213 = tmp1;
sahilmgandhi 18:6a4db94011d3 1399
sahilmgandhi 18:6a4db94011d3 1400 tmp2 = (uint32_t)((~wrp1415data) << 16)|(wrp1415data);
sahilmgandhi 18:6a4db94011d3 1401 OB->WRP1415 = tmp2;
sahilmgandhi 18:6a4db94011d3 1402 }
sahilmgandhi 18:6a4db94011d3 1403 }
sahilmgandhi 18:6a4db94011d3 1404 #endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
sahilmgandhi 18:6a4db94011d3 1405
sahilmgandhi 18:6a4db94011d3 1406 /**
sahilmgandhi 18:6a4db94011d3 1407 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
sahilmgandhi 18:6a4db94011d3 1408 * @param OB_IWDG Selects the WDG mode.
sahilmgandhi 18:6a4db94011d3 1409 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1410 * @arg @ref OB_IWDG_SW Software WDG selected
sahilmgandhi 18:6a4db94011d3 1411 * @arg @ref OB_IWDG_HW Hardware WDG selected
sahilmgandhi 18:6a4db94011d3 1412 * @param OB_STOP Reset event when entering STOP mode.
sahilmgandhi 18:6a4db94011d3 1413 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1414 * @arg @ref OB_STOP_NORST No reset generated when entering in STOP
sahilmgandhi 18:6a4db94011d3 1415 * @arg @ref OB_STOP_RST Reset generated when entering in STOP
sahilmgandhi 18:6a4db94011d3 1416 * @param OB_STDBY Reset event when entering Standby mode.
sahilmgandhi 18:6a4db94011d3 1417 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1418 * @arg @ref OB_STDBY_NORST No reset generated when entering in STANDBY
sahilmgandhi 18:6a4db94011d3 1419 * @arg @ref OB_STDBY_RST Reset generated when entering in STANDBY
sahilmgandhi 18:6a4db94011d3 1420 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1421 */
sahilmgandhi 18:6a4db94011d3 1422 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
sahilmgandhi 18:6a4db94011d3 1423 {
sahilmgandhi 18:6a4db94011d3 1424 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1425 uint32_t tmp = 0, tmp1 = 0;
sahilmgandhi 18:6a4db94011d3 1426
sahilmgandhi 18:6a4db94011d3 1427 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1428 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
sahilmgandhi 18:6a4db94011d3 1429 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
sahilmgandhi 18:6a4db94011d3 1430 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
sahilmgandhi 18:6a4db94011d3 1431
sahilmgandhi 18:6a4db94011d3 1432 /* Get the User Option byte register */
sahilmgandhi 18:6a4db94011d3 1433 tmp1 = OB->USER & ((~FLASH_OBR_USER) >> 16);
sahilmgandhi 18:6a4db94011d3 1434
sahilmgandhi 18:6a4db94011d3 1435 /* Calculate the user option byte to write */
sahilmgandhi 18:6a4db94011d3 1436 tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16);
sahilmgandhi 18:6a4db94011d3 1437 tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
sahilmgandhi 18:6a4db94011d3 1438
sahilmgandhi 18:6a4db94011d3 1439 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1440 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1441
sahilmgandhi 18:6a4db94011d3 1442 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1443 {
sahilmgandhi 18:6a4db94011d3 1444 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1445 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1446
sahilmgandhi 18:6a4db94011d3 1447 /* Write the User Option Byte */
sahilmgandhi 18:6a4db94011d3 1448 OB->USER = tmp;
sahilmgandhi 18:6a4db94011d3 1449
sahilmgandhi 18:6a4db94011d3 1450 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1451 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1452 }
sahilmgandhi 18:6a4db94011d3 1453
sahilmgandhi 18:6a4db94011d3 1454 /* Return the Option Byte program Status */
sahilmgandhi 18:6a4db94011d3 1455 return status;
sahilmgandhi 18:6a4db94011d3 1456 }
sahilmgandhi 18:6a4db94011d3 1457
sahilmgandhi 18:6a4db94011d3 1458 #if defined(FLASH_OBR_nRST_BFB2)
sahilmgandhi 18:6a4db94011d3 1459 /**
sahilmgandhi 18:6a4db94011d3 1460 * @brief Configures to boot from Bank1 or Bank2.
sahilmgandhi 18:6a4db94011d3 1461 * @param OB_BOOT select the FLASH Bank to boot from.
sahilmgandhi 18:6a4db94011d3 1462 * This parameter can be one of the following values:
sahilmgandhi 18:6a4db94011d3 1463 * @arg @ref OB_BOOT_BANK2 At startup, if boot pins are set in boot from user Flash
sahilmgandhi 18:6a4db94011d3 1464 * position and this parameter is selected the device will boot from Bank2 or Bank1,
sahilmgandhi 18:6a4db94011d3 1465 * depending on the activation of the bank. The active banks are checked in
sahilmgandhi 18:6a4db94011d3 1466 * the following order: Bank2, followed by Bank1.
sahilmgandhi 18:6a4db94011d3 1467 * The active bank is recognized by the value programmed at the base address
sahilmgandhi 18:6a4db94011d3 1468 * of the respective bank (corresponding to the initial stack pointer value
sahilmgandhi 18:6a4db94011d3 1469 * in the interrupt vector table).
sahilmgandhi 18:6a4db94011d3 1470 * @arg @ref OB_BOOT_BANK1 At startup, if boot pins are set in boot from user Flash
sahilmgandhi 18:6a4db94011d3 1471 * position and this parameter is selected the device will boot from Bank1(Default).
sahilmgandhi 18:6a4db94011d3 1472 * For more information, please refer to AN2606 from www.st.com.
sahilmgandhi 18:6a4db94011d3 1473 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1474 */
sahilmgandhi 18:6a4db94011d3 1475 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
sahilmgandhi 18:6a4db94011d3 1476 {
sahilmgandhi 18:6a4db94011d3 1477 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1478 uint32_t tmp = 0, tmp1 = 0;
sahilmgandhi 18:6a4db94011d3 1479
sahilmgandhi 18:6a4db94011d3 1480 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1481 assert_param(IS_OB_BOOT_BANK(OB_BOOT));
sahilmgandhi 18:6a4db94011d3 1482
sahilmgandhi 18:6a4db94011d3 1483 /* Get the User Option byte register and BOR Level*/
sahilmgandhi 18:6a4db94011d3 1484 tmp1 = OB->USER & ((~FLASH_OBR_nRST_BFB2) >> 16);
sahilmgandhi 18:6a4db94011d3 1485
sahilmgandhi 18:6a4db94011d3 1486 /* Calculate the option byte to write */
sahilmgandhi 18:6a4db94011d3 1487 tmp = (uint32_t)~(OB_BOOT | tmp1) << 16;
sahilmgandhi 18:6a4db94011d3 1488 tmp |= (OB_BOOT | tmp1);
sahilmgandhi 18:6a4db94011d3 1489
sahilmgandhi 18:6a4db94011d3 1490 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1491 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1492
sahilmgandhi 18:6a4db94011d3 1493 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1494 {
sahilmgandhi 18:6a4db94011d3 1495 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1496 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1497
sahilmgandhi 18:6a4db94011d3 1498 /* Write the BOOT Option Byte */
sahilmgandhi 18:6a4db94011d3 1499 OB->USER = tmp;
sahilmgandhi 18:6a4db94011d3 1500
sahilmgandhi 18:6a4db94011d3 1501 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1502 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1503 }
sahilmgandhi 18:6a4db94011d3 1504
sahilmgandhi 18:6a4db94011d3 1505 /* Return the Option Byte program Status */
sahilmgandhi 18:6a4db94011d3 1506 return status;
sahilmgandhi 18:6a4db94011d3 1507 }
sahilmgandhi 18:6a4db94011d3 1508
sahilmgandhi 18:6a4db94011d3 1509 #endif /* FLASH_OBR_nRST_BFB2 */
sahilmgandhi 18:6a4db94011d3 1510
sahilmgandhi 18:6a4db94011d3 1511 /*
sahilmgandhi 18:6a4db94011d3 1512 ==============================================================================
sahilmgandhi 18:6a4db94011d3 1513 DATA
sahilmgandhi 18:6a4db94011d3 1514 ==============================================================================
sahilmgandhi 18:6a4db94011d3 1515 */
sahilmgandhi 18:6a4db94011d3 1516
sahilmgandhi 18:6a4db94011d3 1517 /**
sahilmgandhi 18:6a4db94011d3 1518 * @brief Write a Byte at a specified address in data memory.
sahilmgandhi 18:6a4db94011d3 1519 * @param Address specifies the address to be written.
sahilmgandhi 18:6a4db94011d3 1520 * @param Data specifies the data to be written.
sahilmgandhi 18:6a4db94011d3 1521 * @note This function assumes that the is data word is already erased.
sahilmgandhi 18:6a4db94011d3 1522 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1523 */
sahilmgandhi 18:6a4db94011d3 1524 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
sahilmgandhi 18:6a4db94011d3 1525 {
sahilmgandhi 18:6a4db94011d3 1526 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1527 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1528 uint32_t tmp = 0, tmpaddr = 0;
sahilmgandhi 18:6a4db94011d3 1529 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1530
sahilmgandhi 18:6a4db94011d3 1531 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1532 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 1533
sahilmgandhi 18:6a4db94011d3 1534 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1535 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1536
sahilmgandhi 18:6a4db94011d3 1537 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1538 {
sahilmgandhi 18:6a4db94011d3 1539 /* Clear the FTDW bit */
sahilmgandhi 18:6a4db94011d3 1540 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
sahilmgandhi 18:6a4db94011d3 1541
sahilmgandhi 18:6a4db94011d3 1542 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1543 /* Possible only on Cat1 devices */
sahilmgandhi 18:6a4db94011d3 1544 if(Data != (uint8_t)0x00)
sahilmgandhi 18:6a4db94011d3 1545 {
sahilmgandhi 18:6a4db94011d3 1546 /* If the previous operation is completed, proceed to write the new Data */
sahilmgandhi 18:6a4db94011d3 1547 *(__IO uint8_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1548
sahilmgandhi 18:6a4db94011d3 1549 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1550 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1551 }
sahilmgandhi 18:6a4db94011d3 1552 else
sahilmgandhi 18:6a4db94011d3 1553 {
sahilmgandhi 18:6a4db94011d3 1554 tmpaddr = Address & 0xFFFFFFFC;
sahilmgandhi 18:6a4db94011d3 1555 tmp = * (__IO uint32_t *) tmpaddr;
sahilmgandhi 18:6a4db94011d3 1556 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
sahilmgandhi 18:6a4db94011d3 1557 tmp &= ~tmpaddr;
sahilmgandhi 18:6a4db94011d3 1558 status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
sahilmgandhi 18:6a4db94011d3 1559 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 1560 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1561 status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
sahilmgandhi 18:6a4db94011d3 1562 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 1563 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1564 }
sahilmgandhi 18:6a4db94011d3 1565 #else /*!Cat1*/
sahilmgandhi 18:6a4db94011d3 1566 /* If the previous operation is completed, proceed to write the new Data */
sahilmgandhi 18:6a4db94011d3 1567 *(__IO uint8_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1568
sahilmgandhi 18:6a4db94011d3 1569 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1570 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1571 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1572 }
sahilmgandhi 18:6a4db94011d3 1573 /* Return the Write Status */
sahilmgandhi 18:6a4db94011d3 1574 return status;
sahilmgandhi 18:6a4db94011d3 1575 }
sahilmgandhi 18:6a4db94011d3 1576
sahilmgandhi 18:6a4db94011d3 1577 /**
sahilmgandhi 18:6a4db94011d3 1578 * @brief Writes a half word at a specified address in data memory.
sahilmgandhi 18:6a4db94011d3 1579 * @param Address specifies the address to be written.
sahilmgandhi 18:6a4db94011d3 1580 * @param Data specifies the data to be written.
sahilmgandhi 18:6a4db94011d3 1581 * @note This function assumes that the is data word is already erased.
sahilmgandhi 18:6a4db94011d3 1582 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1583 */
sahilmgandhi 18:6a4db94011d3 1584 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
sahilmgandhi 18:6a4db94011d3 1585 {
sahilmgandhi 18:6a4db94011d3 1586 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1587 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1588 uint32_t tmp = 0, tmpaddr = 0;
sahilmgandhi 18:6a4db94011d3 1589 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1590
sahilmgandhi 18:6a4db94011d3 1591 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1592 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 1593
sahilmgandhi 18:6a4db94011d3 1594 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1595 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1596
sahilmgandhi 18:6a4db94011d3 1597 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1598 {
sahilmgandhi 18:6a4db94011d3 1599 /* Clear the FTDW bit */
sahilmgandhi 18:6a4db94011d3 1600 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
sahilmgandhi 18:6a4db94011d3 1601
sahilmgandhi 18:6a4db94011d3 1602 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1603 /* Possible only on Cat1 devices */
sahilmgandhi 18:6a4db94011d3 1604 if(Data != (uint16_t)0x0000)
sahilmgandhi 18:6a4db94011d3 1605 {
sahilmgandhi 18:6a4db94011d3 1606 /* If the previous operation is completed, proceed to write the new data */
sahilmgandhi 18:6a4db94011d3 1607 *(__IO uint16_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1608
sahilmgandhi 18:6a4db94011d3 1609 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1610 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1611 }
sahilmgandhi 18:6a4db94011d3 1612 else
sahilmgandhi 18:6a4db94011d3 1613 {
sahilmgandhi 18:6a4db94011d3 1614 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 1615 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1616 if((Address & 0x3) != 0x3)
sahilmgandhi 18:6a4db94011d3 1617 {
sahilmgandhi 18:6a4db94011d3 1618 tmpaddr = Address & 0xFFFFFFFC;
sahilmgandhi 18:6a4db94011d3 1619 tmp = * (__IO uint32_t *) tmpaddr;
sahilmgandhi 18:6a4db94011d3 1620 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
sahilmgandhi 18:6a4db94011d3 1621 tmp &= ~tmpaddr;
sahilmgandhi 18:6a4db94011d3 1622 status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
sahilmgandhi 18:6a4db94011d3 1623 status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
sahilmgandhi 18:6a4db94011d3 1624 }
sahilmgandhi 18:6a4db94011d3 1625 else
sahilmgandhi 18:6a4db94011d3 1626 {
sahilmgandhi 18:6a4db94011d3 1627 HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
sahilmgandhi 18:6a4db94011d3 1628 HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
sahilmgandhi 18:6a4db94011d3 1629 }
sahilmgandhi 18:6a4db94011d3 1630 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 1631 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1632 }
sahilmgandhi 18:6a4db94011d3 1633 #else /* !Cat1 */
sahilmgandhi 18:6a4db94011d3 1634 /* If the previous operation is completed, proceed to write the new data */
sahilmgandhi 18:6a4db94011d3 1635 *(__IO uint16_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1636
sahilmgandhi 18:6a4db94011d3 1637 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1638 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1639 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1640 }
sahilmgandhi 18:6a4db94011d3 1641 /* Return the Write Status */
sahilmgandhi 18:6a4db94011d3 1642 return status;
sahilmgandhi 18:6a4db94011d3 1643 }
sahilmgandhi 18:6a4db94011d3 1644
sahilmgandhi 18:6a4db94011d3 1645 /**
sahilmgandhi 18:6a4db94011d3 1646 * @brief Programs a word at a specified address in data memory.
sahilmgandhi 18:6a4db94011d3 1647 * @param Address specifies the address to be written.
sahilmgandhi 18:6a4db94011d3 1648 * @param Data specifies the data to be written.
sahilmgandhi 18:6a4db94011d3 1649 * @note This function assumes that the is data word is already erased.
sahilmgandhi 18:6a4db94011d3 1650 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1651 */
sahilmgandhi 18:6a4db94011d3 1652 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data)
sahilmgandhi 18:6a4db94011d3 1653 {
sahilmgandhi 18:6a4db94011d3 1654 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1655
sahilmgandhi 18:6a4db94011d3 1656 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1657 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 1658
sahilmgandhi 18:6a4db94011d3 1659 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1660 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1661
sahilmgandhi 18:6a4db94011d3 1662 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1663 {
sahilmgandhi 18:6a4db94011d3 1664 /* Clear the FTDW bit */
sahilmgandhi 18:6a4db94011d3 1665 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
sahilmgandhi 18:6a4db94011d3 1666
sahilmgandhi 18:6a4db94011d3 1667 /* If the previous operation is completed, proceed to program the new data */
sahilmgandhi 18:6a4db94011d3 1668 *(__IO uint32_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1669
sahilmgandhi 18:6a4db94011d3 1670 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1671 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1672 }
sahilmgandhi 18:6a4db94011d3 1673 /* Return the Write Status */
sahilmgandhi 18:6a4db94011d3 1674 return status;
sahilmgandhi 18:6a4db94011d3 1675 }
sahilmgandhi 18:6a4db94011d3 1676
sahilmgandhi 18:6a4db94011d3 1677 /**
sahilmgandhi 18:6a4db94011d3 1678 * @brief Write a Byte at a specified address in data memory without erase.
sahilmgandhi 18:6a4db94011d3 1679 * @param Address specifies the address to be written.
sahilmgandhi 18:6a4db94011d3 1680 * @param Data specifies the data to be written.
sahilmgandhi 18:6a4db94011d3 1681 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1682 */
sahilmgandhi 18:6a4db94011d3 1683 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data)
sahilmgandhi 18:6a4db94011d3 1684 {
sahilmgandhi 18:6a4db94011d3 1685 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1686 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1687 uint32_t tmp = 0, tmpaddr = 0;
sahilmgandhi 18:6a4db94011d3 1688 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1689
sahilmgandhi 18:6a4db94011d3 1690 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1691 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 1692
sahilmgandhi 18:6a4db94011d3 1693 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1694 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1695
sahilmgandhi 18:6a4db94011d3 1696 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1697 {
sahilmgandhi 18:6a4db94011d3 1698 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1699 if(Data != (uint8_t) 0x00)
sahilmgandhi 18:6a4db94011d3 1700 {
sahilmgandhi 18:6a4db94011d3 1701 *(__IO uint8_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1702
sahilmgandhi 18:6a4db94011d3 1703 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1704 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1705
sahilmgandhi 18:6a4db94011d3 1706 }
sahilmgandhi 18:6a4db94011d3 1707 else
sahilmgandhi 18:6a4db94011d3 1708 {
sahilmgandhi 18:6a4db94011d3 1709 tmpaddr = Address & 0xFFFFFFFC;
sahilmgandhi 18:6a4db94011d3 1710 tmp = * (__IO uint32_t *) tmpaddr;
sahilmgandhi 18:6a4db94011d3 1711 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
sahilmgandhi 18:6a4db94011d3 1712 tmp &= ~tmpaddr;
sahilmgandhi 18:6a4db94011d3 1713 status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
sahilmgandhi 18:6a4db94011d3 1714 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 1715 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1716 status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
sahilmgandhi 18:6a4db94011d3 1717 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 1718 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1719 }
sahilmgandhi 18:6a4db94011d3 1720 #else /* Not Cat1*/
sahilmgandhi 18:6a4db94011d3 1721 *(__IO uint8_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1722
sahilmgandhi 18:6a4db94011d3 1723 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1724 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1725 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1726 }
sahilmgandhi 18:6a4db94011d3 1727 /* Return the Write Status */
sahilmgandhi 18:6a4db94011d3 1728 return status;
sahilmgandhi 18:6a4db94011d3 1729 }
sahilmgandhi 18:6a4db94011d3 1730
sahilmgandhi 18:6a4db94011d3 1731 /**
sahilmgandhi 18:6a4db94011d3 1732 * @brief Writes a half word at a specified address in data memory without erase.
sahilmgandhi 18:6a4db94011d3 1733 * @param Address specifies the address to be written.
sahilmgandhi 18:6a4db94011d3 1734 * @param Data specifies the data to be written.
sahilmgandhi 18:6a4db94011d3 1735 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1736 */
sahilmgandhi 18:6a4db94011d3 1737 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
sahilmgandhi 18:6a4db94011d3 1738 {
sahilmgandhi 18:6a4db94011d3 1739 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1740 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1741 uint32_t tmp = 0, tmpaddr = 0;
sahilmgandhi 18:6a4db94011d3 1742 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1743
sahilmgandhi 18:6a4db94011d3 1744 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1745 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 1746
sahilmgandhi 18:6a4db94011d3 1747 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1748 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1749
sahilmgandhi 18:6a4db94011d3 1750 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1751 {
sahilmgandhi 18:6a4db94011d3 1752 #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
sahilmgandhi 18:6a4db94011d3 1753 if(Data != (uint16_t)0x0000)
sahilmgandhi 18:6a4db94011d3 1754 {
sahilmgandhi 18:6a4db94011d3 1755 *(__IO uint16_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1756
sahilmgandhi 18:6a4db94011d3 1757 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1758 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1759 }
sahilmgandhi 18:6a4db94011d3 1760 else
sahilmgandhi 18:6a4db94011d3 1761 {
sahilmgandhi 18:6a4db94011d3 1762 /* Process Unlocked */
sahilmgandhi 18:6a4db94011d3 1763 __HAL_UNLOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1764 if((Address & 0x3) != 0x3)
sahilmgandhi 18:6a4db94011d3 1765 {
sahilmgandhi 18:6a4db94011d3 1766 tmpaddr = Address & 0xFFFFFFFC;
sahilmgandhi 18:6a4db94011d3 1767 tmp = * (__IO uint32_t *) tmpaddr;
sahilmgandhi 18:6a4db94011d3 1768 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
sahilmgandhi 18:6a4db94011d3 1769 tmp &= ~tmpaddr;
sahilmgandhi 18:6a4db94011d3 1770 status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
sahilmgandhi 18:6a4db94011d3 1771 status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
sahilmgandhi 18:6a4db94011d3 1772 }
sahilmgandhi 18:6a4db94011d3 1773 else
sahilmgandhi 18:6a4db94011d3 1774 {
sahilmgandhi 18:6a4db94011d3 1775 HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
sahilmgandhi 18:6a4db94011d3 1776 HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
sahilmgandhi 18:6a4db94011d3 1777 }
sahilmgandhi 18:6a4db94011d3 1778 /* Process Locked */
sahilmgandhi 18:6a4db94011d3 1779 __HAL_LOCK(&pFlash);
sahilmgandhi 18:6a4db94011d3 1780 }
sahilmgandhi 18:6a4db94011d3 1781 #else /* Not Cat1*/
sahilmgandhi 18:6a4db94011d3 1782 *(__IO uint16_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1783
sahilmgandhi 18:6a4db94011d3 1784 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1785 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1786 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
sahilmgandhi 18:6a4db94011d3 1787 }
sahilmgandhi 18:6a4db94011d3 1788 /* Return the Write Status */
sahilmgandhi 18:6a4db94011d3 1789 return status;
sahilmgandhi 18:6a4db94011d3 1790 }
sahilmgandhi 18:6a4db94011d3 1791
sahilmgandhi 18:6a4db94011d3 1792 /**
sahilmgandhi 18:6a4db94011d3 1793 * @brief Programs a word at a specified address in data memory without erase.
sahilmgandhi 18:6a4db94011d3 1794 * @param Address specifies the address to be written.
sahilmgandhi 18:6a4db94011d3 1795 * @param Data specifies the data to be written.
sahilmgandhi 18:6a4db94011d3 1796 * @retval HAL status
sahilmgandhi 18:6a4db94011d3 1797 */
sahilmgandhi 18:6a4db94011d3 1798 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data)
sahilmgandhi 18:6a4db94011d3 1799 {
sahilmgandhi 18:6a4db94011d3 1800 HAL_StatusTypeDef status = HAL_OK;
sahilmgandhi 18:6a4db94011d3 1801
sahilmgandhi 18:6a4db94011d3 1802 /* Check the parameters */
sahilmgandhi 18:6a4db94011d3 1803 assert_param(IS_FLASH_DATA_ADDRESS(Address));
sahilmgandhi 18:6a4db94011d3 1804
sahilmgandhi 18:6a4db94011d3 1805 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1806 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1807
sahilmgandhi 18:6a4db94011d3 1808 if(status == HAL_OK)
sahilmgandhi 18:6a4db94011d3 1809 {
sahilmgandhi 18:6a4db94011d3 1810 *(__IO uint32_t *)Address = Data;
sahilmgandhi 18:6a4db94011d3 1811
sahilmgandhi 18:6a4db94011d3 1812 /* Wait for last operation to be completed */
sahilmgandhi 18:6a4db94011d3 1813 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
sahilmgandhi 18:6a4db94011d3 1814 }
sahilmgandhi 18:6a4db94011d3 1815 /* Return the Write Status */
sahilmgandhi 18:6a4db94011d3 1816 return status;
sahilmgandhi 18:6a4db94011d3 1817 }
sahilmgandhi 18:6a4db94011d3 1818
sahilmgandhi 18:6a4db94011d3 1819 /**
sahilmgandhi 18:6a4db94011d3 1820 * @}
sahilmgandhi 18:6a4db94011d3 1821 */
sahilmgandhi 18:6a4db94011d3 1822
sahilmgandhi 18:6a4db94011d3 1823 /**
sahilmgandhi 18:6a4db94011d3 1824 * @}
sahilmgandhi 18:6a4db94011d3 1825 */
sahilmgandhi 18:6a4db94011d3 1826
sahilmgandhi 18:6a4db94011d3 1827 /** @addtogroup FLASH
sahilmgandhi 18:6a4db94011d3 1828 * @{
sahilmgandhi 18:6a4db94011d3 1829 */
sahilmgandhi 18:6a4db94011d3 1830
sahilmgandhi 18:6a4db94011d3 1831
sahilmgandhi 18:6a4db94011d3 1832 /** @addtogroup FLASH_Private_Functions
sahilmgandhi 18:6a4db94011d3 1833 * @{
sahilmgandhi 18:6a4db94011d3 1834 */
sahilmgandhi 18:6a4db94011d3 1835
sahilmgandhi 18:6a4db94011d3 1836 /**
sahilmgandhi 18:6a4db94011d3 1837 * @brief Erases a specified page in program memory.
sahilmgandhi 18:6a4db94011d3 1838 * @param PageAddress The page address in program memory to be erased.
sahilmgandhi 18:6a4db94011d3 1839 * @note A Page is erased in the Program memory only if the address to load
sahilmgandhi 18:6a4db94011d3 1840 * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
sahilmgandhi 18:6a4db94011d3 1841 * @retval None
sahilmgandhi 18:6a4db94011d3 1842 */
sahilmgandhi 18:6a4db94011d3 1843 void FLASH_PageErase(uint32_t PageAddress)
sahilmgandhi 18:6a4db94011d3 1844 {
sahilmgandhi 18:6a4db94011d3 1845 /* Clean the error context */
sahilmgandhi 18:6a4db94011d3 1846 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
sahilmgandhi 18:6a4db94011d3 1847
sahilmgandhi 18:6a4db94011d3 1848 /* Set the ERASE bit */
sahilmgandhi 18:6a4db94011d3 1849 SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
sahilmgandhi 18:6a4db94011d3 1850
sahilmgandhi 18:6a4db94011d3 1851 /* Set PROG bit */
sahilmgandhi 18:6a4db94011d3 1852 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
sahilmgandhi 18:6a4db94011d3 1853
sahilmgandhi 18:6a4db94011d3 1854 /* Write 00000000h to the first word of the program page to erase */
sahilmgandhi 18:6a4db94011d3 1855 *(__IO uint32_t *)(uint32_t)(PageAddress & ~(FLASH_PAGE_SIZE - 1)) = 0x00000000;
sahilmgandhi 18:6a4db94011d3 1856 }
sahilmgandhi 18:6a4db94011d3 1857
sahilmgandhi 18:6a4db94011d3 1858 /**
sahilmgandhi 18:6a4db94011d3 1859 * @}
sahilmgandhi 18:6a4db94011d3 1860 */
sahilmgandhi 18:6a4db94011d3 1861
sahilmgandhi 18:6a4db94011d3 1862 /**
sahilmgandhi 18:6a4db94011d3 1863 * @}
sahilmgandhi 18:6a4db94011d3 1864 */
sahilmgandhi 18:6a4db94011d3 1865
sahilmgandhi 18:6a4db94011d3 1866 #endif /* HAL_FLASH_MODULE_ENABLED */
sahilmgandhi 18:6a4db94011d3 1867 /**
sahilmgandhi 18:6a4db94011d3 1868 * @}
sahilmgandhi 18:6a4db94011d3 1869 */
sahilmgandhi 18:6a4db94011d3 1870
sahilmgandhi 18:6a4db94011d3 1871 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/