DRV8323RS Version

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Wed Jul 17 03:40:12 2019 +0000
Revision:
51:6cd89bd6fcaa
Parent:
23:2adf23ee0305
adding field-weakening to the user menu, switch "torque limit" to "current limit"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 23:2adf23ee0305 1 /**
benkatz 23:2adf23ee0305 2 ******************************************************************************
benkatz 23:2adf23ee0305 3 * @file stm32f4xx_flash.c
benkatz 23:2adf23ee0305 4 * @author MCD Application Team
benkatz 23:2adf23ee0305 5 * @version V1.7.1
benkatz 23:2adf23ee0305 6 * @date 20-May-2016
benkatz 23:2adf23ee0305 7 * @brief This file provides firmware functions to manage the following
benkatz 23:2adf23ee0305 8 * functionalities of the FLASH peripheral:
benkatz 23:2adf23ee0305 9 * + FLASH Interface configuration
benkatz 23:2adf23ee0305 10 * + FLASH Memory Programming
benkatz 23:2adf23ee0305 11 * + Option Bytes Programming
benkatz 23:2adf23ee0305 12 * + Interrupts and flags management
benkatz 23:2adf23ee0305 13 *
benkatz 23:2adf23ee0305 14 @verbatim
benkatz 23:2adf23ee0305 15 ===============================================================================
benkatz 23:2adf23ee0305 16 ##### How to use this driver #####
benkatz 23:2adf23ee0305 17 ===============================================================================
benkatz 23:2adf23ee0305 18 [..]
benkatz 23:2adf23ee0305 19 This driver provides functions to configure and program the FLASH memory
benkatz 23:2adf23ee0305 20 of all STM32F4xx devices. These functions are split in 4 groups:
benkatz 23:2adf23ee0305 21
benkatz 23:2adf23ee0305 22 (#) FLASH Interface configuration functions: this group includes the
benkatz 23:2adf23ee0305 23 management of the following features:
benkatz 23:2adf23ee0305 24 (++) Set the latency
benkatz 23:2adf23ee0305 25 (++) Enable/Disable the prefetch buffer
benkatz 23:2adf23ee0305 26 (++) Enable/Disable the Instruction cache and the Data cache
benkatz 23:2adf23ee0305 27 (++) Reset the Instruction cache and the Data cache
benkatz 23:2adf23ee0305 28
benkatz 23:2adf23ee0305 29 (#) FLASH Memory Programming functions: this group includes all needed
benkatz 23:2adf23ee0305 30 functions to erase and program the main memory:
benkatz 23:2adf23ee0305 31 (++) Lock and Unlock the FLASH interface
benkatz 23:2adf23ee0305 32 (++) Erase function: Erase sector, erase all sectors
benkatz 23:2adf23ee0305 33 (++) Program functions: byte, half word, word and double word
benkatz 23:2adf23ee0305 34
benkatz 23:2adf23ee0305 35 (#) Option Bytes Programming functions: this group includes all needed
benkatz 23:2adf23ee0305 36 functions to manage the Option Bytes:
benkatz 23:2adf23ee0305 37 (++) Set/Reset the write protection
benkatz 23:2adf23ee0305 38 (++) Set the Read protection Level
benkatz 23:2adf23ee0305 39 (++) Set the BOR level
benkatz 23:2adf23ee0305 40 (++) Program the user Option Bytes
benkatz 23:2adf23ee0305 41 (++) Launch the Option Bytes loader
benkatz 23:2adf23ee0305 42
benkatz 23:2adf23ee0305 43 (#) Interrupts and flags management functions: this group
benkatz 23:2adf23ee0305 44 includes all needed functions to:
benkatz 23:2adf23ee0305 45 (++) Enable/Disable the FLASH interrupt sources
benkatz 23:2adf23ee0305 46 (++) Get flags status
benkatz 23:2adf23ee0305 47 (++) Clear flags
benkatz 23:2adf23ee0305 48 (++) Get FLASH operation status
benkatz 23:2adf23ee0305 49 (++) Wait for last FLASH operation
benkatz 23:2adf23ee0305 50 @endverbatim
benkatz 23:2adf23ee0305 51 ******************************************************************************
benkatz 23:2adf23ee0305 52 * @attention
benkatz 23:2adf23ee0305 53 *
benkatz 23:2adf23ee0305 54 * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
benkatz 23:2adf23ee0305 55 *
benkatz 23:2adf23ee0305 56 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
benkatz 23:2adf23ee0305 57 * You may not use this file except in compliance with the License.
benkatz 23:2adf23ee0305 58 * You may obtain a copy of the License at:
benkatz 23:2adf23ee0305 59 *
benkatz 23:2adf23ee0305 60 * http://www.st.com/software_license_agreement_liberty_v2
benkatz 23:2adf23ee0305 61 *
benkatz 23:2adf23ee0305 62 * Unless required by applicable law or agreed to in writing, software
benkatz 23:2adf23ee0305 63 * distributed under the License is distributed on an "AS IS" BASIS,
benkatz 23:2adf23ee0305 64 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
benkatz 23:2adf23ee0305 65 * See the License for the specific language governing permissions and
benkatz 23:2adf23ee0305 66 * limitations under the License.
benkatz 23:2adf23ee0305 67 *
benkatz 23:2adf23ee0305 68 ******************************************************************************
benkatz 23:2adf23ee0305 69 */
benkatz 23:2adf23ee0305 70
benkatz 23:2adf23ee0305 71 /* Includes ------------------------------------------------------------------*/
benkatz 23:2adf23ee0305 72 #include "stm32f4xx_flash.h"
benkatz 23:2adf23ee0305 73
benkatz 23:2adf23ee0305 74 /** @addtogroup STM32F4xx_StdPeriph_Driver
benkatz 23:2adf23ee0305 75 * @{
benkatz 23:2adf23ee0305 76 */
benkatz 23:2adf23ee0305 77
benkatz 23:2adf23ee0305 78 /** @defgroup FLASH
benkatz 23:2adf23ee0305 79 * @brief FLASH driver modules
benkatz 23:2adf23ee0305 80 * @{
benkatz 23:2adf23ee0305 81 */
benkatz 23:2adf23ee0305 82
benkatz 23:2adf23ee0305 83 /* Private typedef -----------------------------------------------------------*/
benkatz 23:2adf23ee0305 84 /* Private define ------------------------------------------------------------*/
benkatz 23:2adf23ee0305 85 #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
benkatz 23:2adf23ee0305 86
benkatz 23:2adf23ee0305 87 /* Private macro -------------------------------------------------------------*/
benkatz 23:2adf23ee0305 88 /* Private variables ---------------------------------------------------------*/
benkatz 23:2adf23ee0305 89 /* Private function prototypes -----------------------------------------------*/
benkatz 23:2adf23ee0305 90 /* Private functions ---------------------------------------------------------*/
benkatz 23:2adf23ee0305 91
benkatz 23:2adf23ee0305 92 /** @defgroup FLASH_Private_Functions
benkatz 23:2adf23ee0305 93 * @{
benkatz 23:2adf23ee0305 94 */
benkatz 23:2adf23ee0305 95
benkatz 23:2adf23ee0305 96 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
benkatz 23:2adf23ee0305 97 * @brief FLASH Interface configuration functions
benkatz 23:2adf23ee0305 98 *
benkatz 23:2adf23ee0305 99
benkatz 23:2adf23ee0305 100 @verbatim
benkatz 23:2adf23ee0305 101 ===============================================================================
benkatz 23:2adf23ee0305 102 ##### FLASH Interface configuration functions #####
benkatz 23:2adf23ee0305 103 ===============================================================================
benkatz 23:2adf23ee0305 104 [..]
benkatz 23:2adf23ee0305 105 This group includes the following functions:
benkatz 23:2adf23ee0305 106 (+) void FLASH_SetLatency(uint32_t FLASH_Latency)
benkatz 23:2adf23ee0305 107 To correctly read data from FLASH memory, the number of wait states (LATENCY)
benkatz 23:2adf23ee0305 108 must be correctly programmed according to the frequency of the CPU clock
benkatz 23:2adf23ee0305 109 (HCLK) and the supply voltage of the device.
benkatz 23:2adf23ee0305 110 [..]
benkatz 23:2adf23ee0305 111 For STM32F405xx/07xx and STM32F415xx/17xx devices
benkatz 23:2adf23ee0305 112 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 113 | Latency | HCLK clock frequency (MHz) |
benkatz 23:2adf23ee0305 114 | |---------------------------------------------------------------------|
benkatz 23:2adf23ee0305 115 | | voltage range | voltage range | voltage range | voltage range |
benkatz 23:2adf23ee0305 116 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
benkatz 23:2adf23ee0305 117 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 118 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 22 |0 < HCLK <= 20 |
benkatz 23:2adf23ee0305 119 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 120 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |22 < HCLK <= 44 |20 < HCLK <= 40 |
benkatz 23:2adf23ee0305 121 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 122 |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |44 < HCLK <= 66 |40 < HCLK <= 60 |
benkatz 23:2adf23ee0305 123 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 124 |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |66 < HCLK <= 88 |60 < HCLK <= 80 |
benkatz 23:2adf23ee0305 125 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 126 |4WS(5CPU cycle)|120< HCLK <= 150|96 < HCLK <= 120|88 < HCLK <= 110 |80 < HCLK <= 100 |
benkatz 23:2adf23ee0305 127 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 128 |5WS(6CPU cycle)|150< HCLK <= 168|120< HCLK <= 144|110 < HCLK <= 132|100 < HCLK <= 120|
benkatz 23:2adf23ee0305 129 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 130 |6WS(7CPU cycle)| NA |144< HCLK <= 168|132 < HCLK <= 154|120 < HCLK <= 140|
benkatz 23:2adf23ee0305 131 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 132 |7WS(8CPU cycle)| NA | NA |154 < HCLK <= 168|140 < HCLK <= 160|
benkatz 23:2adf23ee0305 133 +---------------|----------------|----------------|-----------------|-----------------+
benkatz 23:2adf23ee0305 134
benkatz 23:2adf23ee0305 135 [..]
benkatz 23:2adf23ee0305 136 For STM32F42xxx/43xxx devices
benkatz 23:2adf23ee0305 137 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 138 | Latency | HCLK clock frequency (MHz) |
benkatz 23:2adf23ee0305 139 | |---------------------------------------------------------------------|
benkatz 23:2adf23ee0305 140 | | voltage range | voltage range | voltage range | voltage range |
benkatz 23:2adf23ee0305 141 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
benkatz 23:2adf23ee0305 142 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 143 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 22 |0 < HCLK <= 20 |
benkatz 23:2adf23ee0305 144 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 145 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |22 < HCLK <= 44 |20 < HCLK <= 40 |
benkatz 23:2adf23ee0305 146 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 147 |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |44 < HCLK <= 66 |40 < HCLK <= 60 |
benkatz 23:2adf23ee0305 148 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 149 |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |66 < HCLK <= 88 |60 < HCLK <= 80 |
benkatz 23:2adf23ee0305 150 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 151 |4WS(5CPU cycle)|120< HCLK <= 150|96 < HCLK <= 120|88 < HCLK <= 110 |80 < HCLK <= 100 |
benkatz 23:2adf23ee0305 152 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 153 |5WS(6CPU cycle)|120< HCLK <= 180|120< HCLK <= 144|110 < HCLK <= 132|100 < HCLK <= 120|
benkatz 23:2adf23ee0305 154 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 155 |6WS(7CPU cycle)| NA |144< HCLK <= 168|132 < HCLK <= 154|120 < HCLK <= 140|
benkatz 23:2adf23ee0305 156 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 157 |7WS(8CPU cycle)| NA |168< HCLK <= 180|154 < HCLK <= 176|140 < HCLK <= 160|
benkatz 23:2adf23ee0305 158 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 159 |8WS(9CPU cycle)| NA | NA |176 < HCLK <= 180|160 < HCLK <= 168|
benkatz 23:2adf23ee0305 160 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 161
benkatz 23:2adf23ee0305 162 [..]
benkatz 23:2adf23ee0305 163 For STM32F401x devices
benkatz 23:2adf23ee0305 164 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 165 | Latency | HCLK clock frequency (MHz) |
benkatz 23:2adf23ee0305 166 | |---------------------------------------------------------------------|
benkatz 23:2adf23ee0305 167 | | voltage range | voltage range | voltage range | voltage range |
benkatz 23:2adf23ee0305 168 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
benkatz 23:2adf23ee0305 169 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 170 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 22 |0 < HCLK <= 20 |
benkatz 23:2adf23ee0305 171 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 172 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |22 < HCLK <= 44 |20 < HCLK <= 40 |
benkatz 23:2adf23ee0305 173 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 174 |2WS(3CPU cycle)|60 < HCLK <= 84 |48 < HCLK <= 72 |44 < HCLK <= 66 |40 < HCLK <= 60 |
benkatz 23:2adf23ee0305 175 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 176 |3WS(4CPU cycle)| NA |72 < HCLK <= 84 |66 < HCLK <= 84 |60 < HCLK <= 80 |
benkatz 23:2adf23ee0305 177 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 178 |4WS(5CPU cycle)| NA | NA | NA |80 < HCLK <= 84 |
benkatz 23:2adf23ee0305 179 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 180
benkatz 23:2adf23ee0305 181 [..]
benkatz 23:2adf23ee0305 182 For STM32F410xx/STM32F411xE devices
benkatz 23:2adf23ee0305 183 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 184 | Latency | HCLK clock frequency (MHz) |
benkatz 23:2adf23ee0305 185 | |---------------------------------------------------------------------|
benkatz 23:2adf23ee0305 186 | | voltage range | voltage range | voltage range | voltage range |
benkatz 23:2adf23ee0305 187 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
benkatz 23:2adf23ee0305 188 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 189 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 18 |0 < HCLK <= 16 |
benkatz 23:2adf23ee0305 190 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 191 |1WS(2CPU cycle)|30 < HCLK <= 64 |24 < HCLK <= 48 |18 < HCLK <= 36 |16 < HCLK <= 32 |
benkatz 23:2adf23ee0305 192 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 193 |2WS(3CPU cycle)|64 < HCLK <= 90 |48 < HCLK <= 72 |36 < HCLK <= 54 |32 < HCLK <= 48 |
benkatz 23:2adf23ee0305 194 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 195 |3WS(4CPU cycle)|90 < HCLK <= 100|72 < HCLK <= 96 |54 < HCLK <= 72 |48 < HCLK <= 64 |
benkatz 23:2adf23ee0305 196 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 197 |4WS(5CPU cycle)| NA |96 < HCLK <= 100|72 < HCLK <= 90 |64 < HCLK <= 80 |
benkatz 23:2adf23ee0305 198 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 199 |5WS(6CPU cycle)| NA | NA |90 < HCLK <= 100 |80 < HCLK <= 96 |
benkatz 23:2adf23ee0305 200 |---------------|----------------|----------------|-----------------|-----------------|
benkatz 23:2adf23ee0305 201 |6WS(7CPU cycle)| NA | NA | NA |96 < HCLK <= 100 |
benkatz 23:2adf23ee0305 202 +-------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 203
benkatz 23:2adf23ee0305 204 [..]
benkatz 23:2adf23ee0305 205 +-------------------------------------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 206 | | voltage range | voltage range | voltage range | voltage range | voltage range 2.7 V - 3.6 V |
benkatz 23:2adf23ee0305 207 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V | with External Vpp = 9V |
benkatz 23:2adf23ee0305 208 |---------------|----------------|----------------|-----------------|-----------------|-----------------------------|
benkatz 23:2adf23ee0305 209 |Max Parallelism| x32 | x16 | x8 | x64 |
benkatz 23:2adf23ee0305 210 |---------------|----------------|----------------|-----------------|-----------------|-----------------------------|
benkatz 23:2adf23ee0305 211 |PSIZE[1:0] | 10 | 01 | 00 | 11 |
benkatz 23:2adf23ee0305 212 +-------------------------------------------------------------------------------------------------------------------+
benkatz 23:2adf23ee0305 213
benkatz 23:2adf23ee0305 214 -@- On STM32F405xx/407xx and STM32F415xx/417xx devices:
benkatz 23:2adf23ee0305 215 (++) when VOS = '0' Scale 2 mode, the maximum value of fHCLK = 144MHz.
benkatz 23:2adf23ee0305 216 (++) when VOS = '1' Scale 1 mode, the maximum value of fHCLK = 168MHz.
benkatz 23:2adf23ee0305 217 [..]
benkatz 23:2adf23ee0305 218 On STM32F42xxx/43xxx devices:
benkatz 23:2adf23ee0305 219 (++) when VOS[1:0] = '0x01' Scale 3 mode, the maximum value of fHCLK is 120MHz.
benkatz 23:2adf23ee0305 220 (++) when VOS[1:0] = '0x10' Scale 2 mode, the maximum value of fHCLK is 144MHz if OverDrive OFF and 168MHz if OverDrive ON.
benkatz 23:2adf23ee0305 221 (++) when VOS[1:0] = '0x11' Scale 1 mode, the maximum value of fHCLK is 168MHz if OverDrive OFF and 180MHz if OverDrive ON.
benkatz 23:2adf23ee0305 222 [..]
benkatz 23:2adf23ee0305 223 On STM32F401x devices:
benkatz 23:2adf23ee0305 224 (++) when VOS[1:0] = '0x01' Scale 3 mode, the maximum value of fHCLK is 60MHz.
benkatz 23:2adf23ee0305 225 (++) when VOS[1:0] = '0x10' Scale 2 mode, the maximum value of fHCLK is 84MHz.
benkatz 23:2adf23ee0305 226 [..]
benkatz 23:2adf23ee0305 227 On STM32F410xx/STM32F411xE devices:
benkatz 23:2adf23ee0305 228 (++) when VOS[1:0] = '0x01' Scale 3 mode, the maximum value of fHCLK is 64MHz.
benkatz 23:2adf23ee0305 229 (++) when VOS[1:0] = '0x10' Scale 2 mode, the maximum value of fHCLK is 84MHz.
benkatz 23:2adf23ee0305 230 (++) when VOS[1:0] = '0x11' Scale 1 mode, the maximum value of fHCLK is 100MHz.
benkatz 23:2adf23ee0305 231
benkatz 23:2adf23ee0305 232 For more details please refer product DataSheet
benkatz 23:2adf23ee0305 233 You can use PWR_MainRegulatorModeConfig() function to control VOS bits.
benkatz 23:2adf23ee0305 234
benkatz 23:2adf23ee0305 235 (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState)
benkatz 23:2adf23ee0305 236 (+) void FLASH_InstructionCacheCmd(FunctionalState NewState)
benkatz 23:2adf23ee0305 237 (+) void FLASH_DataCacheCmd(FunctionalState NewState)
benkatz 23:2adf23ee0305 238 (+) void FLASH_InstructionCacheReset(void)
benkatz 23:2adf23ee0305 239 (+) void FLASH_DataCacheReset(void)
benkatz 23:2adf23ee0305 240
benkatz 23:2adf23ee0305 241 [..]
benkatz 23:2adf23ee0305 242 The unlock sequence is not needed for these functions.
benkatz 23:2adf23ee0305 243
benkatz 23:2adf23ee0305 244 @endverbatim
benkatz 23:2adf23ee0305 245 * @{
benkatz 23:2adf23ee0305 246 */
benkatz 23:2adf23ee0305 247
benkatz 23:2adf23ee0305 248 /**
benkatz 23:2adf23ee0305 249 * @brief Sets the code latency value.
benkatz 23:2adf23ee0305 250 * @param FLASH_Latency: specifies the FLASH Latency value.
benkatz 23:2adf23ee0305 251 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 252 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
benkatz 23:2adf23ee0305 253 * @arg FLASH_Latency_1: FLASH One Latency cycle
benkatz 23:2adf23ee0305 254 * @arg FLASH_Latency_2: FLASH Two Latency cycles
benkatz 23:2adf23ee0305 255 * @arg FLASH_Latency_3: FLASH Three Latency cycles
benkatz 23:2adf23ee0305 256 * @arg FLASH_Latency_4: FLASH Four Latency cycles
benkatz 23:2adf23ee0305 257 * @arg FLASH_Latency_5: FLASH Five Latency cycles
benkatz 23:2adf23ee0305 258 * @arg FLASH_Latency_6: FLASH Six Latency cycles
benkatz 23:2adf23ee0305 259 * @arg FLASH_Latency_7: FLASH Seven Latency cycles
benkatz 23:2adf23ee0305 260 * @arg FLASH_Latency_8: FLASH Eight Latency cycles
benkatz 23:2adf23ee0305 261 * @arg FLASH_Latency_9: FLASH Nine Latency cycles
benkatz 23:2adf23ee0305 262 * @arg FLASH_Latency_10: FLASH Teen Latency cycles
benkatz 23:2adf23ee0305 263 * @arg FLASH_Latency_11: FLASH Eleven Latency cycles
benkatz 23:2adf23ee0305 264 * @arg FLASH_Latency_12: FLASH Twelve Latency cycles
benkatz 23:2adf23ee0305 265 * @arg FLASH_Latency_13: FLASH Thirteen Latency cycles
benkatz 23:2adf23ee0305 266 * @arg FLASH_Latency_14: FLASH Fourteen Latency cycles
benkatz 23:2adf23ee0305 267 * @arg FLASH_Latency_15: FLASH Fifteen Latency cycles
benkatz 23:2adf23ee0305 268 *
benkatz 23:2adf23ee0305 269 * @note For STM32F405xx/407xx, STM32F415xx/417xx, STM32F401xx/411xE and STM32F412xG devices
benkatz 23:2adf23ee0305 270 * this parameter can be a value between FLASH_Latency_0 and FLASH_Latency_7.
benkatz 23:2adf23ee0305 271 *
benkatz 23:2adf23ee0305 272 * @note For STM32F42xxx/43xxx devices this parameter can be a value between
benkatz 23:2adf23ee0305 273 * FLASH_Latency_0 and FLASH_Latency_15.
benkatz 23:2adf23ee0305 274 *
benkatz 23:2adf23ee0305 275 * @retval None
benkatz 23:2adf23ee0305 276 */
benkatz 23:2adf23ee0305 277 void FLASH_SetLatency(uint32_t FLASH_Latency)
benkatz 23:2adf23ee0305 278 {
benkatz 23:2adf23ee0305 279 /* Check the parameters */
benkatz 23:2adf23ee0305 280 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
benkatz 23:2adf23ee0305 281
benkatz 23:2adf23ee0305 282 /* Perform Byte access to FLASH_ACR[8:0] to set the Latency value */
benkatz 23:2adf23ee0305 283 *(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)FLASH_Latency;
benkatz 23:2adf23ee0305 284 }
benkatz 23:2adf23ee0305 285
benkatz 23:2adf23ee0305 286 /**
benkatz 23:2adf23ee0305 287 * @brief Enables or disables the Prefetch Buffer.
benkatz 23:2adf23ee0305 288 * @param NewState: new state of the Prefetch Buffer.
benkatz 23:2adf23ee0305 289 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 290 * @retval None
benkatz 23:2adf23ee0305 291 */
benkatz 23:2adf23ee0305 292 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
benkatz 23:2adf23ee0305 293 {
benkatz 23:2adf23ee0305 294 /* Check the parameters */
benkatz 23:2adf23ee0305 295 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 296
benkatz 23:2adf23ee0305 297 /* Enable or disable the Prefetch Buffer */
benkatz 23:2adf23ee0305 298 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 299 {
benkatz 23:2adf23ee0305 300 FLASH->ACR |= FLASH_ACR_PRFTEN;
benkatz 23:2adf23ee0305 301 }
benkatz 23:2adf23ee0305 302 else
benkatz 23:2adf23ee0305 303 {
benkatz 23:2adf23ee0305 304 FLASH->ACR &= (~FLASH_ACR_PRFTEN);
benkatz 23:2adf23ee0305 305 }
benkatz 23:2adf23ee0305 306 }
benkatz 23:2adf23ee0305 307
benkatz 23:2adf23ee0305 308 /**
benkatz 23:2adf23ee0305 309 * @brief Enables or disables the Instruction Cache feature.
benkatz 23:2adf23ee0305 310 * @param NewState: new state of the Instruction Cache.
benkatz 23:2adf23ee0305 311 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 312 * @retval None
benkatz 23:2adf23ee0305 313 */
benkatz 23:2adf23ee0305 314 void FLASH_InstructionCacheCmd(FunctionalState NewState)
benkatz 23:2adf23ee0305 315 {
benkatz 23:2adf23ee0305 316 /* Check the parameters */
benkatz 23:2adf23ee0305 317 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 318
benkatz 23:2adf23ee0305 319 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 320 {
benkatz 23:2adf23ee0305 321 FLASH->ACR |= FLASH_ACR_ICEN;
benkatz 23:2adf23ee0305 322 }
benkatz 23:2adf23ee0305 323 else
benkatz 23:2adf23ee0305 324 {
benkatz 23:2adf23ee0305 325 FLASH->ACR &= (~FLASH_ACR_ICEN);
benkatz 23:2adf23ee0305 326 }
benkatz 23:2adf23ee0305 327 }
benkatz 23:2adf23ee0305 328
benkatz 23:2adf23ee0305 329 /**
benkatz 23:2adf23ee0305 330 * @brief Enables or disables the Data Cache feature.
benkatz 23:2adf23ee0305 331 * @param NewState: new state of the Data Cache.
benkatz 23:2adf23ee0305 332 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 333 * @retval None
benkatz 23:2adf23ee0305 334 */
benkatz 23:2adf23ee0305 335 void FLASH_DataCacheCmd(FunctionalState NewState)
benkatz 23:2adf23ee0305 336 {
benkatz 23:2adf23ee0305 337 /* Check the parameters */
benkatz 23:2adf23ee0305 338 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 339
benkatz 23:2adf23ee0305 340 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 341 {
benkatz 23:2adf23ee0305 342 FLASH->ACR |= FLASH_ACR_DCEN;
benkatz 23:2adf23ee0305 343 }
benkatz 23:2adf23ee0305 344 else
benkatz 23:2adf23ee0305 345 {
benkatz 23:2adf23ee0305 346 FLASH->ACR &= (~FLASH_ACR_DCEN);
benkatz 23:2adf23ee0305 347 }
benkatz 23:2adf23ee0305 348 }
benkatz 23:2adf23ee0305 349
benkatz 23:2adf23ee0305 350 /**
benkatz 23:2adf23ee0305 351 * @brief Resets the Instruction Cache.
benkatz 23:2adf23ee0305 352 * @note This function must be used only when the Instruction Cache is disabled.
benkatz 23:2adf23ee0305 353 * @param None
benkatz 23:2adf23ee0305 354 * @retval None
benkatz 23:2adf23ee0305 355 */
benkatz 23:2adf23ee0305 356 void FLASH_InstructionCacheReset(void)
benkatz 23:2adf23ee0305 357 {
benkatz 23:2adf23ee0305 358 FLASH->ACR |= FLASH_ACR_ICRST;
benkatz 23:2adf23ee0305 359 }
benkatz 23:2adf23ee0305 360
benkatz 23:2adf23ee0305 361 /**
benkatz 23:2adf23ee0305 362 * @brief Resets the Data Cache.
benkatz 23:2adf23ee0305 363 * @note This function must be used only when the Data Cache is disabled.
benkatz 23:2adf23ee0305 364 * @param None
benkatz 23:2adf23ee0305 365 * @retval None
benkatz 23:2adf23ee0305 366 */
benkatz 23:2adf23ee0305 367 void FLASH_DataCacheReset(void)
benkatz 23:2adf23ee0305 368 {
benkatz 23:2adf23ee0305 369 FLASH->ACR |= FLASH_ACR_DCRST;
benkatz 23:2adf23ee0305 370 }
benkatz 23:2adf23ee0305 371
benkatz 23:2adf23ee0305 372 /**
benkatz 23:2adf23ee0305 373 * @}
benkatz 23:2adf23ee0305 374 */
benkatz 23:2adf23ee0305 375
benkatz 23:2adf23ee0305 376 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
benkatz 23:2adf23ee0305 377 * @brief FLASH Memory Programming functions
benkatz 23:2adf23ee0305 378 *
benkatz 23:2adf23ee0305 379 @verbatim
benkatz 23:2adf23ee0305 380 ===============================================================================
benkatz 23:2adf23ee0305 381 ##### FLASH Memory Programming functions #####
benkatz 23:2adf23ee0305 382 ===============================================================================
benkatz 23:2adf23ee0305 383 [..]
benkatz 23:2adf23ee0305 384 This group includes the following functions:
benkatz 23:2adf23ee0305 385 (+) void FLASH_Unlock(void)
benkatz 23:2adf23ee0305 386 (+) void FLASH_Lock(void)
benkatz 23:2adf23ee0305 387 (+) FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
benkatz 23:2adf23ee0305 388 (+) FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange)
benkatz 23:2adf23ee0305 389 (+) FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
benkatz 23:2adf23ee0305 390 (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
benkatz 23:2adf23ee0305 391 (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
benkatz 23:2adf23ee0305 392 (+) FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
benkatz 23:2adf23ee0305 393 The following functions can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 394 (+) FLASH_Status FLASH_EraseAllBank1Sectors(uint8_t VoltageRange)
benkatz 23:2adf23ee0305 395 (+) FLASH_Status FLASH_EraseAllBank2Sectors(uint8_t VoltageRange)
benkatz 23:2adf23ee0305 396 [..]
benkatz 23:2adf23ee0305 397 Any operation of erase or program should follow these steps:
benkatz 23:2adf23ee0305 398 (#) Call the FLASH_Unlock() function to enable the FLASH control register access
benkatz 23:2adf23ee0305 399
benkatz 23:2adf23ee0305 400 (#) Call the desired function to erase sector(s) or program data
benkatz 23:2adf23ee0305 401
benkatz 23:2adf23ee0305 402 (#) Call the FLASH_Lock() function to disable the FLASH control register access
benkatz 23:2adf23ee0305 403 (recommended to protect the FLASH memory against possible unwanted operation)
benkatz 23:2adf23ee0305 404
benkatz 23:2adf23ee0305 405 @endverbatim
benkatz 23:2adf23ee0305 406 * @{
benkatz 23:2adf23ee0305 407 */
benkatz 23:2adf23ee0305 408
benkatz 23:2adf23ee0305 409 /**
benkatz 23:2adf23ee0305 410 * @brief Unlocks the FLASH control register access
benkatz 23:2adf23ee0305 411 * @param None
benkatz 23:2adf23ee0305 412 * @retval None
benkatz 23:2adf23ee0305 413 */
benkatz 23:2adf23ee0305 414 void FLASH_Unlock(void)
benkatz 23:2adf23ee0305 415 {
benkatz 23:2adf23ee0305 416 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
benkatz 23:2adf23ee0305 417 {
benkatz 23:2adf23ee0305 418 /* Authorize the FLASH Registers access */
benkatz 23:2adf23ee0305 419 FLASH->KEYR = FLASH_KEY1;
benkatz 23:2adf23ee0305 420 FLASH->KEYR = FLASH_KEY2;
benkatz 23:2adf23ee0305 421 }
benkatz 23:2adf23ee0305 422 }
benkatz 23:2adf23ee0305 423
benkatz 23:2adf23ee0305 424 /**
benkatz 23:2adf23ee0305 425 * @brief Locks the FLASH control register access
benkatz 23:2adf23ee0305 426 * @param None
benkatz 23:2adf23ee0305 427 * @retval None
benkatz 23:2adf23ee0305 428 */
benkatz 23:2adf23ee0305 429 void FLASH_Lock(void)
benkatz 23:2adf23ee0305 430 {
benkatz 23:2adf23ee0305 431 /* Set the LOCK Bit to lock the FLASH Registers access */
benkatz 23:2adf23ee0305 432 FLASH->CR |= FLASH_CR_LOCK;
benkatz 23:2adf23ee0305 433 }
benkatz 23:2adf23ee0305 434
benkatz 23:2adf23ee0305 435 /**
benkatz 23:2adf23ee0305 436 * @brief Erases a specified FLASH Sector.
benkatz 23:2adf23ee0305 437 *
benkatz 23:2adf23ee0305 438 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 439 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 440 *
benkatz 23:2adf23ee0305 441 * @param FLASH_Sector: The Sector number to be erased.
benkatz 23:2adf23ee0305 442 *
benkatz 23:2adf23ee0305 443 * @note For STM32F405xx/407xx and STM32F415xx/417xx devices this parameter can
benkatz 23:2adf23ee0305 444 * be a value between FLASH_Sector_0 and FLASH_Sector_11.
benkatz 23:2adf23ee0305 445 *
benkatz 23:2adf23ee0305 446 * For STM32F42xxx/43xxx devices this parameter can be a value between
benkatz 23:2adf23ee0305 447 * FLASH_Sector_0 and FLASH_Sector_23.
benkatz 23:2adf23ee0305 448 *
benkatz 23:2adf23ee0305 449 * For STM32F401xx devices this parameter can be a value between
benkatz 23:2adf23ee0305 450 * FLASH_Sector_0 and FLASH_Sector_5.
benkatz 23:2adf23ee0305 451 *
benkatz 23:2adf23ee0305 452 * For STM32F411xE and STM32F412xG devices this parameter can be a value between
benkatz 23:2adf23ee0305 453 * FLASH_Sector_0 and FLASH_Sector_7.
benkatz 23:2adf23ee0305 454 *
benkatz 23:2adf23ee0305 455 * For STM32F410xx devices this parameter can be a value between
benkatz 23:2adf23ee0305 456 * FLASH_Sector_0 and FLASH_Sector_4.
benkatz 23:2adf23ee0305 457 *
benkatz 23:2adf23ee0305 458 * @param VoltageRange: The device voltage range which defines the erase parallelism.
benkatz 23:2adf23ee0305 459 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 460 * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
benkatz 23:2adf23ee0305 461 * the operation will be done by byte (8-bit)
benkatz 23:2adf23ee0305 462 * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
benkatz 23:2adf23ee0305 463 * the operation will be done by half word (16-bit)
benkatz 23:2adf23ee0305 464 * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
benkatz 23:2adf23ee0305 465 * the operation will be done by word (32-bit)
benkatz 23:2adf23ee0305 466 * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
benkatz 23:2adf23ee0305 467 * the operation will be done by double word (64-bit)
benkatz 23:2adf23ee0305 468 *
benkatz 23:2adf23ee0305 469 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 470 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 471 */
benkatz 23:2adf23ee0305 472 FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
benkatz 23:2adf23ee0305 473 {
benkatz 23:2adf23ee0305 474 uint32_t tmp_psize = 0x0;
benkatz 23:2adf23ee0305 475 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 476
benkatz 23:2adf23ee0305 477 /* Check the parameters */
benkatz 23:2adf23ee0305 478 assert_param(IS_FLASH_SECTOR(FLASH_Sector));
benkatz 23:2adf23ee0305 479 assert_param(IS_VOLTAGERANGE(VoltageRange));
benkatz 23:2adf23ee0305 480
benkatz 23:2adf23ee0305 481 if(VoltageRange == VoltageRange_1)
benkatz 23:2adf23ee0305 482 {
benkatz 23:2adf23ee0305 483 tmp_psize = FLASH_PSIZE_BYTE;
benkatz 23:2adf23ee0305 484 }
benkatz 23:2adf23ee0305 485 else if(VoltageRange == VoltageRange_2)
benkatz 23:2adf23ee0305 486 {
benkatz 23:2adf23ee0305 487 tmp_psize = FLASH_PSIZE_HALF_WORD;
benkatz 23:2adf23ee0305 488 }
benkatz 23:2adf23ee0305 489 else if(VoltageRange == VoltageRange_3)
benkatz 23:2adf23ee0305 490 {
benkatz 23:2adf23ee0305 491 tmp_psize = FLASH_PSIZE_WORD;
benkatz 23:2adf23ee0305 492 }
benkatz 23:2adf23ee0305 493 else
benkatz 23:2adf23ee0305 494 {
benkatz 23:2adf23ee0305 495 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
benkatz 23:2adf23ee0305 496 }
benkatz 23:2adf23ee0305 497 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 498 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 499
benkatz 23:2adf23ee0305 500 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 501 {
benkatz 23:2adf23ee0305 502 /* if the previous operation is completed, proceed to erase the sector */
benkatz 23:2adf23ee0305 503 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 504 FLASH->CR |= tmp_psize;
benkatz 23:2adf23ee0305 505 FLASH->CR &= SECTOR_MASK;
benkatz 23:2adf23ee0305 506 FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
benkatz 23:2adf23ee0305 507 FLASH->CR |= FLASH_CR_STRT;
benkatz 23:2adf23ee0305 508
benkatz 23:2adf23ee0305 509 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 510 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 511
benkatz 23:2adf23ee0305 512 /* if the erase operation is completed, disable the SER Bit */
benkatz 23:2adf23ee0305 513 FLASH->CR &= (~FLASH_CR_SER);
benkatz 23:2adf23ee0305 514 FLASH->CR &= SECTOR_MASK;
benkatz 23:2adf23ee0305 515 }
benkatz 23:2adf23ee0305 516 /* Return the Erase Status */
benkatz 23:2adf23ee0305 517 return status;
benkatz 23:2adf23ee0305 518 }
benkatz 23:2adf23ee0305 519
benkatz 23:2adf23ee0305 520 /**
benkatz 23:2adf23ee0305 521 * @brief Erases all FLASH Sectors.
benkatz 23:2adf23ee0305 522 *
benkatz 23:2adf23ee0305 523 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 524 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 525 *
benkatz 23:2adf23ee0305 526 * @param VoltageRange: The device voltage range which defines the erase parallelism.
benkatz 23:2adf23ee0305 527 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 528 * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
benkatz 23:2adf23ee0305 529 * the operation will be done by byte (8-bit)
benkatz 23:2adf23ee0305 530 * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
benkatz 23:2adf23ee0305 531 * the operation will be done by half word (16-bit)
benkatz 23:2adf23ee0305 532 * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
benkatz 23:2adf23ee0305 533 * the operation will be done by word (32-bit)
benkatz 23:2adf23ee0305 534 * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
benkatz 23:2adf23ee0305 535 * the operation will be done by double word (64-bit)
benkatz 23:2adf23ee0305 536 *
benkatz 23:2adf23ee0305 537 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 538 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 539 */
benkatz 23:2adf23ee0305 540 FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange)
benkatz 23:2adf23ee0305 541 {
benkatz 23:2adf23ee0305 542 uint32_t tmp_psize = 0x0;
benkatz 23:2adf23ee0305 543 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 544
benkatz 23:2adf23ee0305 545 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 546 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 547 assert_param(IS_VOLTAGERANGE(VoltageRange));
benkatz 23:2adf23ee0305 548
benkatz 23:2adf23ee0305 549 if(VoltageRange == VoltageRange_1)
benkatz 23:2adf23ee0305 550 {
benkatz 23:2adf23ee0305 551 tmp_psize = FLASH_PSIZE_BYTE;
benkatz 23:2adf23ee0305 552 }
benkatz 23:2adf23ee0305 553 else if(VoltageRange == VoltageRange_2)
benkatz 23:2adf23ee0305 554 {
benkatz 23:2adf23ee0305 555 tmp_psize = FLASH_PSIZE_HALF_WORD;
benkatz 23:2adf23ee0305 556 }
benkatz 23:2adf23ee0305 557 else if(VoltageRange == VoltageRange_3)
benkatz 23:2adf23ee0305 558 {
benkatz 23:2adf23ee0305 559 tmp_psize = FLASH_PSIZE_WORD;
benkatz 23:2adf23ee0305 560 }
benkatz 23:2adf23ee0305 561 else
benkatz 23:2adf23ee0305 562 {
benkatz 23:2adf23ee0305 563 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
benkatz 23:2adf23ee0305 564 }
benkatz 23:2adf23ee0305 565 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 566 {
benkatz 23:2adf23ee0305 567 /* if the previous operation is completed, proceed to erase all sectors */
benkatz 23:2adf23ee0305 568 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx)
benkatz 23:2adf23ee0305 569 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 570 FLASH->CR |= tmp_psize;
benkatz 23:2adf23ee0305 571 FLASH->CR |= (FLASH_CR_MER1 | FLASH_CR_MER2);
benkatz 23:2adf23ee0305 572 FLASH->CR |= FLASH_CR_STRT;
benkatz 23:2adf23ee0305 573
benkatz 23:2adf23ee0305 574 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 575 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 576
benkatz 23:2adf23ee0305 577 /* if the erase operation is completed, disable the MER Bit */
benkatz 23:2adf23ee0305 578 FLASH->CR &= ~(FLASH_CR_MER1 | FLASH_CR_MER2);
benkatz 23:2adf23ee0305 579 #endif /* STM32F427_437xx || STM32F429_439xx || STM32F469_479xx */
benkatz 23:2adf23ee0305 580
benkatz 23:2adf23ee0305 581 #if defined(STM32F40_41xxx) || defined(STM32F401xx) || defined(STM32F410xx) || defined(STM32F411xE) || defined(STM32F412xG) || defined(STM32F446xx)
benkatz 23:2adf23ee0305 582 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 583 FLASH->CR |= tmp_psize;
benkatz 23:2adf23ee0305 584 FLASH->CR |= FLASH_CR_MER;
benkatz 23:2adf23ee0305 585 FLASH->CR |= FLASH_CR_STRT;
benkatz 23:2adf23ee0305 586
benkatz 23:2adf23ee0305 587 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 588 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 589
benkatz 23:2adf23ee0305 590 /* if the erase operation is completed, disable the MER Bit */
benkatz 23:2adf23ee0305 591 FLASH->CR &= (~FLASH_CR_MER);
benkatz 23:2adf23ee0305 592 #endif /* STM32F40_41xxx || STM32F401xx || STM32F410xx || STM32F411xE || STM32F412xG || STM32F446xx */
benkatz 23:2adf23ee0305 593
benkatz 23:2adf23ee0305 594 }
benkatz 23:2adf23ee0305 595 /* Return the Erase Status */
benkatz 23:2adf23ee0305 596 return status;
benkatz 23:2adf23ee0305 597 }
benkatz 23:2adf23ee0305 598
benkatz 23:2adf23ee0305 599 /**
benkatz 23:2adf23ee0305 600 * @brief Erases all FLASH Sectors in Bank 1.
benkatz 23:2adf23ee0305 601 *
benkatz 23:2adf23ee0305 602 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 603 *
benkatz 23:2adf23ee0305 604 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 605 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 606 *
benkatz 23:2adf23ee0305 607 * @param VoltageRange: The device voltage range which defines the erase parallelism.
benkatz 23:2adf23ee0305 608 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 609 * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
benkatz 23:2adf23ee0305 610 * the operation will be done by byte (8-bit)
benkatz 23:2adf23ee0305 611 * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
benkatz 23:2adf23ee0305 612 * the operation will be done by half word (16-bit)
benkatz 23:2adf23ee0305 613 * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
benkatz 23:2adf23ee0305 614 * the operation will be done by word (32-bit)
benkatz 23:2adf23ee0305 615 * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
benkatz 23:2adf23ee0305 616 * the operation will be done by double word (64-bit)
benkatz 23:2adf23ee0305 617 *
benkatz 23:2adf23ee0305 618 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 619 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 620 */
benkatz 23:2adf23ee0305 621 FLASH_Status FLASH_EraseAllBank1Sectors(uint8_t VoltageRange)
benkatz 23:2adf23ee0305 622 {
benkatz 23:2adf23ee0305 623 uint32_t tmp_psize = 0x0;
benkatz 23:2adf23ee0305 624 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 625
benkatz 23:2adf23ee0305 626 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 627 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 628 assert_param(IS_VOLTAGERANGE(VoltageRange));
benkatz 23:2adf23ee0305 629
benkatz 23:2adf23ee0305 630 if(VoltageRange == VoltageRange_1)
benkatz 23:2adf23ee0305 631 {
benkatz 23:2adf23ee0305 632 tmp_psize = FLASH_PSIZE_BYTE;
benkatz 23:2adf23ee0305 633 }
benkatz 23:2adf23ee0305 634 else if(VoltageRange == VoltageRange_2)
benkatz 23:2adf23ee0305 635 {
benkatz 23:2adf23ee0305 636 tmp_psize = FLASH_PSIZE_HALF_WORD;
benkatz 23:2adf23ee0305 637 }
benkatz 23:2adf23ee0305 638 else if(VoltageRange == VoltageRange_3)
benkatz 23:2adf23ee0305 639 {
benkatz 23:2adf23ee0305 640 tmp_psize = FLASH_PSIZE_WORD;
benkatz 23:2adf23ee0305 641 }
benkatz 23:2adf23ee0305 642 else
benkatz 23:2adf23ee0305 643 {
benkatz 23:2adf23ee0305 644 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
benkatz 23:2adf23ee0305 645 }
benkatz 23:2adf23ee0305 646 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 647 {
benkatz 23:2adf23ee0305 648 /* if the previous operation is completed, proceed to erase all sectors */
benkatz 23:2adf23ee0305 649 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 650 FLASH->CR |= tmp_psize;
benkatz 23:2adf23ee0305 651 FLASH->CR |= FLASH_CR_MER1;
benkatz 23:2adf23ee0305 652 FLASH->CR |= FLASH_CR_STRT;
benkatz 23:2adf23ee0305 653
benkatz 23:2adf23ee0305 654 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 655 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 656
benkatz 23:2adf23ee0305 657 /* if the erase operation is completed, disable the MER Bit */
benkatz 23:2adf23ee0305 658 FLASH->CR &= (~FLASH_CR_MER1);
benkatz 23:2adf23ee0305 659
benkatz 23:2adf23ee0305 660 }
benkatz 23:2adf23ee0305 661 /* Return the Erase Status */
benkatz 23:2adf23ee0305 662 return status;
benkatz 23:2adf23ee0305 663 }
benkatz 23:2adf23ee0305 664
benkatz 23:2adf23ee0305 665
benkatz 23:2adf23ee0305 666 /**
benkatz 23:2adf23ee0305 667 * @brief Erases all FLASH Sectors in Bank 2.
benkatz 23:2adf23ee0305 668 *
benkatz 23:2adf23ee0305 669 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 670 *
benkatz 23:2adf23ee0305 671 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 672 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 673 *
benkatz 23:2adf23ee0305 674 * @param VoltageRange: The device voltage range which defines the erase parallelism.
benkatz 23:2adf23ee0305 675 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 676 * @arg VoltageRange_1: when the device voltage range is 1.8V to 2.1V,
benkatz 23:2adf23ee0305 677 * the operation will be done by byte (8-bit)
benkatz 23:2adf23ee0305 678 * @arg VoltageRange_2: when the device voltage range is 2.1V to 2.7V,
benkatz 23:2adf23ee0305 679 * the operation will be done by half word (16-bit)
benkatz 23:2adf23ee0305 680 * @arg VoltageRange_3: when the device voltage range is 2.7V to 3.6V,
benkatz 23:2adf23ee0305 681 * the operation will be done by word (32-bit)
benkatz 23:2adf23ee0305 682 * @arg VoltageRange_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
benkatz 23:2adf23ee0305 683 * the operation will be done by double word (64-bit)
benkatz 23:2adf23ee0305 684 *
benkatz 23:2adf23ee0305 685 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 686 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 687 */
benkatz 23:2adf23ee0305 688 FLASH_Status FLASH_EraseAllBank2Sectors(uint8_t VoltageRange)
benkatz 23:2adf23ee0305 689 {
benkatz 23:2adf23ee0305 690 uint32_t tmp_psize = 0x0;
benkatz 23:2adf23ee0305 691 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 692
benkatz 23:2adf23ee0305 693 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 694 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 695 assert_param(IS_VOLTAGERANGE(VoltageRange));
benkatz 23:2adf23ee0305 696
benkatz 23:2adf23ee0305 697 if(VoltageRange == VoltageRange_1)
benkatz 23:2adf23ee0305 698 {
benkatz 23:2adf23ee0305 699 tmp_psize = FLASH_PSIZE_BYTE;
benkatz 23:2adf23ee0305 700 }
benkatz 23:2adf23ee0305 701 else if(VoltageRange == VoltageRange_2)
benkatz 23:2adf23ee0305 702 {
benkatz 23:2adf23ee0305 703 tmp_psize = FLASH_PSIZE_HALF_WORD;
benkatz 23:2adf23ee0305 704 }
benkatz 23:2adf23ee0305 705 else if(VoltageRange == VoltageRange_3)
benkatz 23:2adf23ee0305 706 {
benkatz 23:2adf23ee0305 707 tmp_psize = FLASH_PSIZE_WORD;
benkatz 23:2adf23ee0305 708 }
benkatz 23:2adf23ee0305 709 else
benkatz 23:2adf23ee0305 710 {
benkatz 23:2adf23ee0305 711 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
benkatz 23:2adf23ee0305 712 }
benkatz 23:2adf23ee0305 713 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 714 {
benkatz 23:2adf23ee0305 715 /* if the previous operation is completed, proceed to erase all sectors */
benkatz 23:2adf23ee0305 716 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 717 FLASH->CR |= tmp_psize;
benkatz 23:2adf23ee0305 718 FLASH->CR |= FLASH_CR_MER2;
benkatz 23:2adf23ee0305 719 FLASH->CR |= FLASH_CR_STRT;
benkatz 23:2adf23ee0305 720
benkatz 23:2adf23ee0305 721 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 722 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 723
benkatz 23:2adf23ee0305 724 /* if the erase operation is completed, disable the MER Bit */
benkatz 23:2adf23ee0305 725 FLASH->CR &= (~FLASH_CR_MER2);
benkatz 23:2adf23ee0305 726
benkatz 23:2adf23ee0305 727 }
benkatz 23:2adf23ee0305 728 /* Return the Erase Status */
benkatz 23:2adf23ee0305 729 return status;
benkatz 23:2adf23ee0305 730 }
benkatz 23:2adf23ee0305 731
benkatz 23:2adf23ee0305 732 /**
benkatz 23:2adf23ee0305 733 * @brief Programs a double word (64-bit) at a specified address.
benkatz 23:2adf23ee0305 734 * @note This function must be used when the device voltage range is from
benkatz 23:2adf23ee0305 735 * 2.7V to 3.6V and an External Vpp is present.
benkatz 23:2adf23ee0305 736 *
benkatz 23:2adf23ee0305 737 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 738 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 739 *
benkatz 23:2adf23ee0305 740 * @param Address: specifies the address to be programmed.
benkatz 23:2adf23ee0305 741 * @param Data: specifies the data to be programmed.
benkatz 23:2adf23ee0305 742 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 743 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 744 */
benkatz 23:2adf23ee0305 745 FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data)
benkatz 23:2adf23ee0305 746 {
benkatz 23:2adf23ee0305 747 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 748
benkatz 23:2adf23ee0305 749 /* Check the parameters */
benkatz 23:2adf23ee0305 750 assert_param(IS_FLASH_ADDRESS(Address));
benkatz 23:2adf23ee0305 751
benkatz 23:2adf23ee0305 752 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 753 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 754
benkatz 23:2adf23ee0305 755 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 756 {
benkatz 23:2adf23ee0305 757 /* if the previous operation is completed, proceed to program the new data */
benkatz 23:2adf23ee0305 758 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 759 FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
benkatz 23:2adf23ee0305 760 FLASH->CR |= FLASH_CR_PG;
benkatz 23:2adf23ee0305 761
benkatz 23:2adf23ee0305 762 *(__IO uint64_t*)Address = Data;
benkatz 23:2adf23ee0305 763
benkatz 23:2adf23ee0305 764 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 765 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 766
benkatz 23:2adf23ee0305 767 /* if the program operation is completed, disable the PG Bit */
benkatz 23:2adf23ee0305 768 FLASH->CR &= (~FLASH_CR_PG);
benkatz 23:2adf23ee0305 769 }
benkatz 23:2adf23ee0305 770 /* Return the Program Status */
benkatz 23:2adf23ee0305 771 return status;
benkatz 23:2adf23ee0305 772 }
benkatz 23:2adf23ee0305 773
benkatz 23:2adf23ee0305 774 /**
benkatz 23:2adf23ee0305 775 * @brief Programs a word (32-bit) at a specified address.
benkatz 23:2adf23ee0305 776 *
benkatz 23:2adf23ee0305 777 * @note This function must be used when the device voltage range is from 2.7V to 3.6V.
benkatz 23:2adf23ee0305 778 *
benkatz 23:2adf23ee0305 779 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 780 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 781 *
benkatz 23:2adf23ee0305 782 * @param Address: specifies the address to be programmed.
benkatz 23:2adf23ee0305 783 * This parameter can be any address in Program memory zone or in OTP zone.
benkatz 23:2adf23ee0305 784 * @param Data: specifies the data to be programmed.
benkatz 23:2adf23ee0305 785 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 786 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 787 */
benkatz 23:2adf23ee0305 788 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
benkatz 23:2adf23ee0305 789 {
benkatz 23:2adf23ee0305 790 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 791
benkatz 23:2adf23ee0305 792 /* Check the parameters */
benkatz 23:2adf23ee0305 793 assert_param(IS_FLASH_ADDRESS(Address));
benkatz 23:2adf23ee0305 794
benkatz 23:2adf23ee0305 795 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 796 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 797
benkatz 23:2adf23ee0305 798 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 799 {
benkatz 23:2adf23ee0305 800 /* if the previous operation is completed, proceed to program the new data */
benkatz 23:2adf23ee0305 801 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 802 FLASH->CR |= FLASH_PSIZE_WORD;
benkatz 23:2adf23ee0305 803 FLASH->CR |= FLASH_CR_PG;
benkatz 23:2adf23ee0305 804
benkatz 23:2adf23ee0305 805 *(__IO uint32_t*)Address = Data;
benkatz 23:2adf23ee0305 806
benkatz 23:2adf23ee0305 807 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 808 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 809
benkatz 23:2adf23ee0305 810 /* if the program operation is completed, disable the PG Bit */
benkatz 23:2adf23ee0305 811 FLASH->CR &= (~FLASH_CR_PG);
benkatz 23:2adf23ee0305 812 }
benkatz 23:2adf23ee0305 813 /* Return the Program Status */
benkatz 23:2adf23ee0305 814 return status;
benkatz 23:2adf23ee0305 815 }
benkatz 23:2adf23ee0305 816
benkatz 23:2adf23ee0305 817 /**
benkatz 23:2adf23ee0305 818 * @brief Programs a half word (16-bit) at a specified address.
benkatz 23:2adf23ee0305 819 * @note This function must be used when the device voltage range is from 2.1V to 3.6V.
benkatz 23:2adf23ee0305 820 *
benkatz 23:2adf23ee0305 821 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 822 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 823 *
benkatz 23:2adf23ee0305 824 * @param Address: specifies the address to be programmed.
benkatz 23:2adf23ee0305 825 * This parameter can be any address in Program memory zone or in OTP zone.
benkatz 23:2adf23ee0305 826 * @param Data: specifies the data to be programmed.
benkatz 23:2adf23ee0305 827 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 828 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 829 */
benkatz 23:2adf23ee0305 830 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
benkatz 23:2adf23ee0305 831 {
benkatz 23:2adf23ee0305 832 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 833
benkatz 23:2adf23ee0305 834 /* Check the parameters */
benkatz 23:2adf23ee0305 835 assert_param(IS_FLASH_ADDRESS(Address));
benkatz 23:2adf23ee0305 836
benkatz 23:2adf23ee0305 837 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 838 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 839
benkatz 23:2adf23ee0305 840 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 841 {
benkatz 23:2adf23ee0305 842 /* if the previous operation is completed, proceed to program the new data */
benkatz 23:2adf23ee0305 843 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 844 FLASH->CR |= FLASH_PSIZE_HALF_WORD;
benkatz 23:2adf23ee0305 845 FLASH->CR |= FLASH_CR_PG;
benkatz 23:2adf23ee0305 846
benkatz 23:2adf23ee0305 847 *(__IO uint16_t*)Address = Data;
benkatz 23:2adf23ee0305 848
benkatz 23:2adf23ee0305 849 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 850 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 851
benkatz 23:2adf23ee0305 852 /* if the program operation is completed, disable the PG Bit */
benkatz 23:2adf23ee0305 853 FLASH->CR &= (~FLASH_CR_PG);
benkatz 23:2adf23ee0305 854 }
benkatz 23:2adf23ee0305 855 /* Return the Program Status */
benkatz 23:2adf23ee0305 856 return status;
benkatz 23:2adf23ee0305 857 }
benkatz 23:2adf23ee0305 858
benkatz 23:2adf23ee0305 859 /**
benkatz 23:2adf23ee0305 860 * @brief Programs a byte (8-bit) at a specified address.
benkatz 23:2adf23ee0305 861 * @note This function can be used within all the device supply voltage ranges.
benkatz 23:2adf23ee0305 862 *
benkatz 23:2adf23ee0305 863 * @note If an erase and a program operations are requested simultaneously,
benkatz 23:2adf23ee0305 864 * the erase operation is performed before the program one.
benkatz 23:2adf23ee0305 865 *
benkatz 23:2adf23ee0305 866 * @param Address: specifies the address to be programmed.
benkatz 23:2adf23ee0305 867 * This parameter can be any address in Program memory zone or in OTP zone.
benkatz 23:2adf23ee0305 868 * @param Data: specifies the data to be programmed.
benkatz 23:2adf23ee0305 869 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 870 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 871 */
benkatz 23:2adf23ee0305 872 FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
benkatz 23:2adf23ee0305 873 {
benkatz 23:2adf23ee0305 874 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 875
benkatz 23:2adf23ee0305 876 /* Check the parameters */
benkatz 23:2adf23ee0305 877 assert_param(IS_FLASH_ADDRESS(Address));
benkatz 23:2adf23ee0305 878
benkatz 23:2adf23ee0305 879 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 880 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 881
benkatz 23:2adf23ee0305 882 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 883 {
benkatz 23:2adf23ee0305 884 /* if the previous operation is completed, proceed to program the new data */
benkatz 23:2adf23ee0305 885 FLASH->CR &= CR_PSIZE_MASK;
benkatz 23:2adf23ee0305 886 FLASH->CR |= FLASH_PSIZE_BYTE;
benkatz 23:2adf23ee0305 887 FLASH->CR |= FLASH_CR_PG;
benkatz 23:2adf23ee0305 888
benkatz 23:2adf23ee0305 889 *(__IO uint8_t*)Address = Data;
benkatz 23:2adf23ee0305 890
benkatz 23:2adf23ee0305 891 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 892 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 893
benkatz 23:2adf23ee0305 894 /* if the program operation is completed, disable the PG Bit */
benkatz 23:2adf23ee0305 895 FLASH->CR &= (~FLASH_CR_PG);
benkatz 23:2adf23ee0305 896 }
benkatz 23:2adf23ee0305 897
benkatz 23:2adf23ee0305 898 /* Return the Program Status */
benkatz 23:2adf23ee0305 899 return status;
benkatz 23:2adf23ee0305 900 }
benkatz 23:2adf23ee0305 901
benkatz 23:2adf23ee0305 902 /**
benkatz 23:2adf23ee0305 903 * @}
benkatz 23:2adf23ee0305 904 */
benkatz 23:2adf23ee0305 905
benkatz 23:2adf23ee0305 906 /** @defgroup FLASH_Group3 Option Bytes Programming functions
benkatz 23:2adf23ee0305 907 * @brief Option Bytes Programming functions
benkatz 23:2adf23ee0305 908 *
benkatz 23:2adf23ee0305 909 @verbatim
benkatz 23:2adf23ee0305 910 ===============================================================================
benkatz 23:2adf23ee0305 911 ##### Option Bytes Programming functions #####
benkatz 23:2adf23ee0305 912 ===============================================================================
benkatz 23:2adf23ee0305 913 [..]
benkatz 23:2adf23ee0305 914 This group includes the following functions:
benkatz 23:2adf23ee0305 915 (+) void FLASH_OB_Unlock(void)
benkatz 23:2adf23ee0305 916 (+) void FLASH_OB_Lock(void)
benkatz 23:2adf23ee0305 917 (+) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
benkatz 23:2adf23ee0305 918 (+) void FLASH_OB_WRP1Config(uint32_t OB_WRP, FunctionalState NewState)
benkatz 23:2adf23ee0305 919 (+) void FLASH_OB_PCROPSelectionConfig(uint8_t OB_PCROPSelect)
benkatz 23:2adf23ee0305 920 (+) void FLASH_OB_PCROPConfig(uint32_t OB_PCROP, FunctionalState NewState)
benkatz 23:2adf23ee0305 921 (+) void FLASH_OB_PCROP1Config(uint32_t OB_PCROP, FunctionalState NewState)
benkatz 23:2adf23ee0305 922 (+) void FLASH_OB_RDPConfig(uint8_t OB_RDP)
benkatz 23:2adf23ee0305 923 (+) void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
benkatz 23:2adf23ee0305 924 (+) void FLASH_OB_BORConfig(uint8_t OB_BOR)
benkatz 23:2adf23ee0305 925 (+) FLASH_Status FLASH_ProgramOTP(uint32_t Address, uint32_t Data)
benkatz 23:2adf23ee0305 926 (+) FLASH_Status FLASH_OB_Launch(void)
benkatz 23:2adf23ee0305 927 (+) uint32_t FLASH_OB_GetUser(void)
benkatz 23:2adf23ee0305 928 (+) uint8_t FLASH_OB_GetWRP(void)
benkatz 23:2adf23ee0305 929 (+) uint8_t FLASH_OB_GetWRP1(void)
benkatz 23:2adf23ee0305 930 (+) uint8_t FLASH_OB_GetPCROP(void)
benkatz 23:2adf23ee0305 931 (+) uint8_t FLASH_OB_GetPCROP1(void)
benkatz 23:2adf23ee0305 932 (+) uint8_t FLASH_OB_GetRDP(void)
benkatz 23:2adf23ee0305 933 (+) uint8_t FLASH_OB_GetBOR(void)
benkatz 23:2adf23ee0305 934 [..]
benkatz 23:2adf23ee0305 935 The following function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 936 (+) void FLASH_OB_BootConfig(uint8_t OB_BOOT)
benkatz 23:2adf23ee0305 937 [..]
benkatz 23:2adf23ee0305 938 Any operation of erase or program should follow these steps:
benkatz 23:2adf23ee0305 939 (#) Call the FLASH_OB_Unlock() function to enable the FLASH option control
benkatz 23:2adf23ee0305 940 register access
benkatz 23:2adf23ee0305 941
benkatz 23:2adf23ee0305 942 (#) Call one or several functions to program the desired Option Bytes:
benkatz 23:2adf23ee0305 943 (++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
benkatz 23:2adf23ee0305 944 => to Enable/Disable the desired sector write protection
benkatz 23:2adf23ee0305 945 (++) void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read
benkatz 23:2adf23ee0305 946 Protection Level
benkatz 23:2adf23ee0305 947 (++) void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
benkatz 23:2adf23ee0305 948 => to configure the user Option Bytes.
benkatz 23:2adf23ee0305 949 (++) void FLASH_OB_BORConfig(uint8_t OB_BOR) => to set the BOR Level
benkatz 23:2adf23ee0305 950
benkatz 23:2adf23ee0305 951 (#) Once all needed Option Bytes to be programmed are correctly written,
benkatz 23:2adf23ee0305 952 call the FLASH_OB_Launch() function to launch the Option Bytes
benkatz 23:2adf23ee0305 953 programming process.
benkatz 23:2adf23ee0305 954
benkatz 23:2adf23ee0305 955 -@- When changing the IWDG mode from HW to SW or from SW to HW, a system
benkatz 23:2adf23ee0305 956 reset is needed to make the change effective.
benkatz 23:2adf23ee0305 957
benkatz 23:2adf23ee0305 958 (#) Call the FLASH_OB_Lock() function to disable the FLASH option control
benkatz 23:2adf23ee0305 959 register access (recommended to protect the Option Bytes against
benkatz 23:2adf23ee0305 960 possible unwanted operations)
benkatz 23:2adf23ee0305 961
benkatz 23:2adf23ee0305 962 @endverbatim
benkatz 23:2adf23ee0305 963 * @{
benkatz 23:2adf23ee0305 964 */
benkatz 23:2adf23ee0305 965
benkatz 23:2adf23ee0305 966 /**
benkatz 23:2adf23ee0305 967 * @brief Unlocks the FLASH Option Control Registers access.
benkatz 23:2adf23ee0305 968 * @param None
benkatz 23:2adf23ee0305 969 * @retval None
benkatz 23:2adf23ee0305 970 */
benkatz 23:2adf23ee0305 971 void FLASH_OB_Unlock(void)
benkatz 23:2adf23ee0305 972 {
benkatz 23:2adf23ee0305 973 if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
benkatz 23:2adf23ee0305 974 {
benkatz 23:2adf23ee0305 975 /* Authorizes the Option Byte register programming */
benkatz 23:2adf23ee0305 976 FLASH->OPTKEYR = FLASH_OPT_KEY1;
benkatz 23:2adf23ee0305 977 FLASH->OPTKEYR = FLASH_OPT_KEY2;
benkatz 23:2adf23ee0305 978 }
benkatz 23:2adf23ee0305 979 }
benkatz 23:2adf23ee0305 980
benkatz 23:2adf23ee0305 981 /**
benkatz 23:2adf23ee0305 982 * @brief Locks the FLASH Option Control Registers access.
benkatz 23:2adf23ee0305 983 * @param None
benkatz 23:2adf23ee0305 984 * @retval None
benkatz 23:2adf23ee0305 985 */
benkatz 23:2adf23ee0305 986 void FLASH_OB_Lock(void)
benkatz 23:2adf23ee0305 987 {
benkatz 23:2adf23ee0305 988 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
benkatz 23:2adf23ee0305 989 FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
benkatz 23:2adf23ee0305 990 }
benkatz 23:2adf23ee0305 991
benkatz 23:2adf23ee0305 992 /**
benkatz 23:2adf23ee0305 993 * @brief Enables or disables the write protection of the desired sectors, for the first
benkatz 23:2adf23ee0305 994 * 1 Mb of the Flash
benkatz 23:2adf23ee0305 995 *
benkatz 23:2adf23ee0305 996 * @note When the memory read protection level is selected (RDP level = 1),
benkatz 23:2adf23ee0305 997 * it is not possible to program or erase the flash sector i if CortexM4
benkatz 23:2adf23ee0305 998 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
benkatz 23:2adf23ee0305 999 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
benkatz 23:2adf23ee0305 1000 *
benkatz 23:2adf23ee0305 1001 * @param OB_WRP: specifies the sector(s) to be write protected or unprotected.
benkatz 23:2adf23ee0305 1002 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1003 * @arg OB_WRP: A value between OB_WRP_Sector0 and OB_WRP_Sector11
benkatz 23:2adf23ee0305 1004 * @arg OB_WRP_Sector_All
benkatz 23:2adf23ee0305 1005 * @param Newstate: new state of the Write Protection.
benkatz 23:2adf23ee0305 1006 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 1007 * @retval None
benkatz 23:2adf23ee0305 1008 */
benkatz 23:2adf23ee0305 1009 void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
benkatz 23:2adf23ee0305 1010 {
benkatz 23:2adf23ee0305 1011 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1012
benkatz 23:2adf23ee0305 1013 /* Check the parameters */
benkatz 23:2adf23ee0305 1014 assert_param(IS_OB_WRP(OB_WRP));
benkatz 23:2adf23ee0305 1015 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 1016
benkatz 23:2adf23ee0305 1017 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1018
benkatz 23:2adf23ee0305 1019 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 1020 {
benkatz 23:2adf23ee0305 1021 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 1022 {
benkatz 23:2adf23ee0305 1023 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~OB_WRP);
benkatz 23:2adf23ee0305 1024 }
benkatz 23:2adf23ee0305 1025 else
benkatz 23:2adf23ee0305 1026 {
benkatz 23:2adf23ee0305 1027 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)OB_WRP;
benkatz 23:2adf23ee0305 1028 }
benkatz 23:2adf23ee0305 1029 }
benkatz 23:2adf23ee0305 1030 }
benkatz 23:2adf23ee0305 1031
benkatz 23:2adf23ee0305 1032 /**
benkatz 23:2adf23ee0305 1033 * @brief Enables or disables the write protection of the desired sectors, for the second
benkatz 23:2adf23ee0305 1034 * 1 Mb of the Flash
benkatz 23:2adf23ee0305 1035 *
benkatz 23:2adf23ee0305 1036 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 1037 *
benkatz 23:2adf23ee0305 1038 * @note When the memory read out protection is selected (RDP level = 1),
benkatz 23:2adf23ee0305 1039 * it is not possible to program or erase the flash sector i if CortexM4
benkatz 23:2adf23ee0305 1040 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
benkatz 23:2adf23ee0305 1041 * @note Active value of nWRPi bits is inverted when PCROP mode is active (SPRMOD =1).
benkatz 23:2adf23ee0305 1042 *
benkatz 23:2adf23ee0305 1043 * @param OB_WRP: specifies the sector(s) to be write protected or unprotected.
benkatz 23:2adf23ee0305 1044 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1045 * @arg OB_WRP: A value between OB_WRP_Sector12 and OB_WRP_Sector23
benkatz 23:2adf23ee0305 1046 * @arg OB_WRP_Sector_All
benkatz 23:2adf23ee0305 1047 * @param Newstate: new state of the Write Protection.
benkatz 23:2adf23ee0305 1048 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 1049 * @retval None
benkatz 23:2adf23ee0305 1050 */
benkatz 23:2adf23ee0305 1051 void FLASH_OB_WRP1Config(uint32_t OB_WRP, FunctionalState NewState)
benkatz 23:2adf23ee0305 1052 {
benkatz 23:2adf23ee0305 1053 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1054
benkatz 23:2adf23ee0305 1055 /* Check the parameters */
benkatz 23:2adf23ee0305 1056 assert_param(IS_OB_WRP(OB_WRP));
benkatz 23:2adf23ee0305 1057 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 1058
benkatz 23:2adf23ee0305 1059 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1060
benkatz 23:2adf23ee0305 1061 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 1062 {
benkatz 23:2adf23ee0305 1063 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 1064 {
benkatz 23:2adf23ee0305 1065 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~OB_WRP);
benkatz 23:2adf23ee0305 1066 }
benkatz 23:2adf23ee0305 1067 else
benkatz 23:2adf23ee0305 1068 {
benkatz 23:2adf23ee0305 1069 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)OB_WRP;
benkatz 23:2adf23ee0305 1070 }
benkatz 23:2adf23ee0305 1071 }
benkatz 23:2adf23ee0305 1072 }
benkatz 23:2adf23ee0305 1073
benkatz 23:2adf23ee0305 1074 /**
benkatz 23:2adf23ee0305 1075 * @brief Select the Protection Mode (SPRMOD).
benkatz 23:2adf23ee0305 1076 *
benkatz 23:2adf23ee0305 1077 * @note This function can be used only for STM32F42xxx/43xxx and STM32F401xx/411xE devices.
benkatz 23:2adf23ee0305 1078 *
benkatz 23:2adf23ee0305 1079 * @note After PCROP activation, Option Byte modification is not possible.
benkatz 23:2adf23ee0305 1080 * Exception made for the global Read Out Protection modification level (level1 to level0)
benkatz 23:2adf23ee0305 1081 * @note Once SPRMOD bit is active unprotection of a protected sector is not possible
benkatz 23:2adf23ee0305 1082 *
benkatz 23:2adf23ee0305 1083 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
benkatz 23:2adf23ee0305 1084 *
benkatz 23:2adf23ee0305 1085 * @note Some Precautions should be taken when activating the PCROP feature :
benkatz 23:2adf23ee0305 1086 * The active value of nWRPi bits is inverted when PCROP mode is active, this means if SPRMOD = 1
benkatz 23:2adf23ee0305 1087 * and WRPi = 1 (default value), then the user sector i is read/write protected.
benkatz 23:2adf23ee0305 1088 * In order to avoid activation of PCROP Mode for undesired sectors, please follow the
benkatz 23:2adf23ee0305 1089 * below safety sequence :
benkatz 23:2adf23ee0305 1090 * - Disable PCROP for all Sectors using FLASH_OB_PCROPConfig(OB_PCROP_Sector_All, DISABLE) function
benkatz 23:2adf23ee0305 1091 * for Bank1 or FLASH_OB_PCROP1Config(OB_PCROP_Sector_All, DISABLE) function for Bank2
benkatz 23:2adf23ee0305 1092 * - Enable PCROP for the desired Sector i using FLASH_OB_PCROPConfig(Sector i, ENABLE) function
benkatz 23:2adf23ee0305 1093 * - Activate the PCROP Mode FLASH_OB_PCROPSelectionConfig() function.
benkatz 23:2adf23ee0305 1094 *
benkatz 23:2adf23ee0305 1095 * @param OB_PCROP: Select the Protection Mode of nWPRi bits
benkatz 23:2adf23ee0305 1096 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1097 * @arg OB_PcROP_Disable: nWRPi control the write protection of respective user sectors.
benkatz 23:2adf23ee0305 1098 * @arg OB_PcROP_Enable: nWRPi control the read&write protection (PCROP) of respective user sectors.
benkatz 23:2adf23ee0305 1099 * @retval None
benkatz 23:2adf23ee0305 1100 */
benkatz 23:2adf23ee0305 1101 void FLASH_OB_PCROPSelectionConfig(uint8_t OB_PcROP)
benkatz 23:2adf23ee0305 1102 {
benkatz 23:2adf23ee0305 1103 uint8_t optiontmp = 0xFF;
benkatz 23:2adf23ee0305 1104
benkatz 23:2adf23ee0305 1105 /* Check the parameters */
benkatz 23:2adf23ee0305 1106 assert_param(IS_OB_PCROP_SELECT(OB_PcROP));
benkatz 23:2adf23ee0305 1107
benkatz 23:2adf23ee0305 1108 /* Mask SPRMOD bit */
benkatz 23:2adf23ee0305 1109 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
benkatz 23:2adf23ee0305 1110 /* Update Option Byte */
benkatz 23:2adf23ee0305 1111 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PcROP | optiontmp);
benkatz 23:2adf23ee0305 1112
benkatz 23:2adf23ee0305 1113 }
benkatz 23:2adf23ee0305 1114
benkatz 23:2adf23ee0305 1115 /**
benkatz 23:2adf23ee0305 1116 * @brief Enables or disables the read/write protection (PCROP) of the desired
benkatz 23:2adf23ee0305 1117 * sectors, for the first 1 MB of the Flash.
benkatz 23:2adf23ee0305 1118 *
benkatz 23:2adf23ee0305 1119 * @note This function can be used only for STM32F42xxx/43xxx , STM32F401xx/411xE
benkatz 23:2adf23ee0305 1120 * and STM32F412xG devices.
benkatz 23:2adf23ee0305 1121 *
benkatz 23:2adf23ee0305 1122 * @param OB_PCROP: specifies the sector(s) to be read/write protected or unprotected.
benkatz 23:2adf23ee0305 1123 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1124 * @arg OB_PCROP: A value between OB_PCROP_Sector0 and OB_PCROP_Sector11 for
benkatz 23:2adf23ee0305 1125 * STM32F42xxx/43xxx devices and between OB_PCROP_Sector0 and
benkatz 23:2adf23ee0305 1126 * OB_PCROP_Sector5 for STM32F401xx/411xE devices.
benkatz 23:2adf23ee0305 1127 * @arg OB_PCROP_Sector_All
benkatz 23:2adf23ee0305 1128 * @param Newstate: new state of the Write Protection.
benkatz 23:2adf23ee0305 1129 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 1130 * @retval None
benkatz 23:2adf23ee0305 1131 */
benkatz 23:2adf23ee0305 1132 void FLASH_OB_PCROPConfig(uint32_t OB_PCROP, FunctionalState NewState)
benkatz 23:2adf23ee0305 1133 {
benkatz 23:2adf23ee0305 1134 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1135
benkatz 23:2adf23ee0305 1136 /* Check the parameters */
benkatz 23:2adf23ee0305 1137 assert_param(IS_OB_PCROP(OB_PCROP));
benkatz 23:2adf23ee0305 1138 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 1139
benkatz 23:2adf23ee0305 1140 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1141
benkatz 23:2adf23ee0305 1142 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 1143 {
benkatz 23:2adf23ee0305 1144 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 1145 {
benkatz 23:2adf23ee0305 1146 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)OB_PCROP;
benkatz 23:2adf23ee0305 1147 }
benkatz 23:2adf23ee0305 1148 else
benkatz 23:2adf23ee0305 1149 {
benkatz 23:2adf23ee0305 1150 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~OB_PCROP);
benkatz 23:2adf23ee0305 1151 }
benkatz 23:2adf23ee0305 1152 }
benkatz 23:2adf23ee0305 1153 }
benkatz 23:2adf23ee0305 1154
benkatz 23:2adf23ee0305 1155 /**
benkatz 23:2adf23ee0305 1156 * @brief Enables or disables the read/write protection (PCROP) of the desired
benkatz 23:2adf23ee0305 1157 * sectors
benkatz 23:2adf23ee0305 1158 *
benkatz 23:2adf23ee0305 1159 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 1160 *
benkatz 23:2adf23ee0305 1161 * @param OB_PCROP: specifies the sector(s) to be read/write protected or unprotected.
benkatz 23:2adf23ee0305 1162 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1163 * @arg OB_PCROP: A value between OB_PCROP_Sector12 and OB_PCROP_Sector23
benkatz 23:2adf23ee0305 1164 * @arg OB_PCROP_Sector_All
benkatz 23:2adf23ee0305 1165 * @param Newstate: new state of the Write Protection.
benkatz 23:2adf23ee0305 1166 * This parameter can be: ENABLE or DISABLE.
benkatz 23:2adf23ee0305 1167 * @retval None
benkatz 23:2adf23ee0305 1168 */
benkatz 23:2adf23ee0305 1169 void FLASH_OB_PCROP1Config(uint32_t OB_PCROP, FunctionalState NewState)
benkatz 23:2adf23ee0305 1170 {
benkatz 23:2adf23ee0305 1171 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1172
benkatz 23:2adf23ee0305 1173 /* Check the parameters */
benkatz 23:2adf23ee0305 1174 assert_param(IS_OB_PCROP(OB_PCROP));
benkatz 23:2adf23ee0305 1175 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 1176
benkatz 23:2adf23ee0305 1177 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1178
benkatz 23:2adf23ee0305 1179 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 1180 {
benkatz 23:2adf23ee0305 1181 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 1182 {
benkatz 23:2adf23ee0305 1183 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)OB_PCROP;
benkatz 23:2adf23ee0305 1184 }
benkatz 23:2adf23ee0305 1185 else
benkatz 23:2adf23ee0305 1186 {
benkatz 23:2adf23ee0305 1187 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~OB_PCROP);
benkatz 23:2adf23ee0305 1188 }
benkatz 23:2adf23ee0305 1189 }
benkatz 23:2adf23ee0305 1190 }
benkatz 23:2adf23ee0305 1191
benkatz 23:2adf23ee0305 1192
benkatz 23:2adf23ee0305 1193 /**
benkatz 23:2adf23ee0305 1194 * @brief Sets the read protection level.
benkatz 23:2adf23ee0305 1195 * @param OB_RDP: specifies the read protection level.
benkatz 23:2adf23ee0305 1196 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1197 * @arg OB_RDP_Level_0: No protection
benkatz 23:2adf23ee0305 1198 * @arg OB_RDP_Level_1: Read protection of the memory
benkatz 23:2adf23ee0305 1199 * @arg OB_RDP_Level_2: Full chip protection
benkatz 23:2adf23ee0305 1200 *
benkatz 23:2adf23ee0305 1201 * /!\ Warning /!\ When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
benkatz 23:2adf23ee0305 1202 *
benkatz 23:2adf23ee0305 1203 * @retval None
benkatz 23:2adf23ee0305 1204 */
benkatz 23:2adf23ee0305 1205 void FLASH_OB_RDPConfig(uint8_t OB_RDP)
benkatz 23:2adf23ee0305 1206 {
benkatz 23:2adf23ee0305 1207 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1208
benkatz 23:2adf23ee0305 1209 /* Check the parameters */
benkatz 23:2adf23ee0305 1210 assert_param(IS_OB_RDP(OB_RDP));
benkatz 23:2adf23ee0305 1211
benkatz 23:2adf23ee0305 1212 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1213
benkatz 23:2adf23ee0305 1214 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 1215 {
benkatz 23:2adf23ee0305 1216 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = OB_RDP;
benkatz 23:2adf23ee0305 1217
benkatz 23:2adf23ee0305 1218 }
benkatz 23:2adf23ee0305 1219 }
benkatz 23:2adf23ee0305 1220
benkatz 23:2adf23ee0305 1221 /**
benkatz 23:2adf23ee0305 1222 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
benkatz 23:2adf23ee0305 1223 * @param OB_IWDG: Selects the IWDG mode
benkatz 23:2adf23ee0305 1224 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1225 * @arg OB_IWDG_SW: Software IWDG selected
benkatz 23:2adf23ee0305 1226 * @arg OB_IWDG_HW: Hardware IWDG selected
benkatz 23:2adf23ee0305 1227 * @param OB_STOP: Reset event when entering STOP mode.
benkatz 23:2adf23ee0305 1228 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1229 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
benkatz 23:2adf23ee0305 1230 * @arg OB_STOP_RST: Reset generated when entering in STOP
benkatz 23:2adf23ee0305 1231 * @param OB_STDBY: Reset event when entering Standby mode.
benkatz 23:2adf23ee0305 1232 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1233 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
benkatz 23:2adf23ee0305 1234 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
benkatz 23:2adf23ee0305 1235 * @retval None
benkatz 23:2adf23ee0305 1236 */
benkatz 23:2adf23ee0305 1237 void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
benkatz 23:2adf23ee0305 1238 {
benkatz 23:2adf23ee0305 1239 uint8_t optiontmp = 0xFF;
benkatz 23:2adf23ee0305 1240 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1241
benkatz 23:2adf23ee0305 1242 /* Check the parameters */
benkatz 23:2adf23ee0305 1243 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
benkatz 23:2adf23ee0305 1244 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
benkatz 23:2adf23ee0305 1245 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
benkatz 23:2adf23ee0305 1246
benkatz 23:2adf23ee0305 1247 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 1248 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1249
benkatz 23:2adf23ee0305 1250 if(status == FLASH_COMPLETE2)
benkatz 23:2adf23ee0305 1251 {
benkatz 23:2adf23ee0305 1252 #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx)
benkatz 23:2adf23ee0305 1253 /* Mask OPTLOCK, OPTSTRT, BOR_LEV and BFB2 bits */
benkatz 23:2adf23ee0305 1254 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1F);
benkatz 23:2adf23ee0305 1255 #endif /* STM32F427_437xx || STM32F429_439xx || STM32F469_479xx */
benkatz 23:2adf23ee0305 1256
benkatz 23:2adf23ee0305 1257 #if defined(STM32F40_41xxx) || defined(STM32F401xx) || defined(STM32F410xx) || defined(STM32F411xE) || defined(STM32F446xx)
benkatz 23:2adf23ee0305 1258 /* Mask OPTLOCK, OPTSTRT and BOR_LEV bits */
benkatz 23:2adf23ee0305 1259 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0F);
benkatz 23:2adf23ee0305 1260 #endif /* STM32F40_41xxx || STM32F401xx || STM32F410xx || STM32F411xE || STM32F446xx */
benkatz 23:2adf23ee0305 1261
benkatz 23:2adf23ee0305 1262 /* Update User Option Byte */
benkatz 23:2adf23ee0305 1263 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = OB_IWDG | (uint8_t)(OB_STDBY | (uint8_t)(OB_STOP | ((uint8_t)optiontmp)));
benkatz 23:2adf23ee0305 1264 }
benkatz 23:2adf23ee0305 1265 }
benkatz 23:2adf23ee0305 1266
benkatz 23:2adf23ee0305 1267 /**
benkatz 23:2adf23ee0305 1268 * @brief Configure the Dual Bank Boot.
benkatz 23:2adf23ee0305 1269 *
benkatz 23:2adf23ee0305 1270 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 1271 *
benkatz 23:2adf23ee0305 1272 * @param OB_BOOT: specifies the Dual Bank Boot Option byte.
benkatz 23:2adf23ee0305 1273 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1274 * @arg OB_Dual_BootEnabled: Dual Bank Boot Enable
benkatz 23:2adf23ee0305 1275 * @arg OB_Dual_BootDisabled: Dual Bank Boot Disabled
benkatz 23:2adf23ee0305 1276 * @retval None
benkatz 23:2adf23ee0305 1277 */
benkatz 23:2adf23ee0305 1278 void FLASH_OB_BootConfig(uint8_t OB_BOOT)
benkatz 23:2adf23ee0305 1279 {
benkatz 23:2adf23ee0305 1280 /* Check the parameters */
benkatz 23:2adf23ee0305 1281 assert_param(IS_OB_BOOT(OB_BOOT));
benkatz 23:2adf23ee0305 1282
benkatz 23:2adf23ee0305 1283 /* Set Dual Bank Boot */
benkatz 23:2adf23ee0305 1284 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BFB2);
benkatz 23:2adf23ee0305 1285 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= OB_BOOT;
benkatz 23:2adf23ee0305 1286
benkatz 23:2adf23ee0305 1287 }
benkatz 23:2adf23ee0305 1288
benkatz 23:2adf23ee0305 1289 /**
benkatz 23:2adf23ee0305 1290 * @brief Sets the BOR Level.
benkatz 23:2adf23ee0305 1291 * @param OB_BOR: specifies the Option Bytes BOR Reset Level.
benkatz 23:2adf23ee0305 1292 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1293 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
benkatz 23:2adf23ee0305 1294 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
benkatz 23:2adf23ee0305 1295 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
benkatz 23:2adf23ee0305 1296 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
benkatz 23:2adf23ee0305 1297 * @retval None
benkatz 23:2adf23ee0305 1298 */
benkatz 23:2adf23ee0305 1299 void FLASH_OB_BORConfig(uint8_t OB_BOR)
benkatz 23:2adf23ee0305 1300 {
benkatz 23:2adf23ee0305 1301 /* Check the parameters */
benkatz 23:2adf23ee0305 1302 assert_param(IS_OB_BOR(OB_BOR));
benkatz 23:2adf23ee0305 1303
benkatz 23:2adf23ee0305 1304 /* Set the BOR Level */
benkatz 23:2adf23ee0305 1305 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
benkatz 23:2adf23ee0305 1306 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= OB_BOR;
benkatz 23:2adf23ee0305 1307
benkatz 23:2adf23ee0305 1308 }
benkatz 23:2adf23ee0305 1309
benkatz 23:2adf23ee0305 1310 /**
benkatz 23:2adf23ee0305 1311 * @brief Launch the option byte loading.
benkatz 23:2adf23ee0305 1312 * @param None
benkatz 23:2adf23ee0305 1313 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 1314 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 1315 */
benkatz 23:2adf23ee0305 1316 FLASH_Status FLASH_OB_Launch(void)
benkatz 23:2adf23ee0305 1317 {
benkatz 23:2adf23ee0305 1318 FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1319
benkatz 23:2adf23ee0305 1320 /* Set the OPTSTRT bit in OPTCR register */
benkatz 23:2adf23ee0305 1321 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
benkatz 23:2adf23ee0305 1322
benkatz 23:2adf23ee0305 1323 /* Wait for last operation to be completed */
benkatz 23:2adf23ee0305 1324 status = FLASH_WaitForLastOperation2();
benkatz 23:2adf23ee0305 1325
benkatz 23:2adf23ee0305 1326 return status;
benkatz 23:2adf23ee0305 1327 }
benkatz 23:2adf23ee0305 1328
benkatz 23:2adf23ee0305 1329 /**
benkatz 23:2adf23ee0305 1330 * @brief Returns the FLASH User Option Bytes values.
benkatz 23:2adf23ee0305 1331 * @param None
benkatz 23:2adf23ee0305 1332 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1)
benkatz 23:2adf23ee0305 1333 * and RST_STDBY(Bit2).
benkatz 23:2adf23ee0305 1334 */
benkatz 23:2adf23ee0305 1335 uint8_t FLASH_OB_GetUser(void)
benkatz 23:2adf23ee0305 1336 {
benkatz 23:2adf23ee0305 1337 /* Return the User Option Byte */
benkatz 23:2adf23ee0305 1338 return (uint8_t)(FLASH->OPTCR >> 5);
benkatz 23:2adf23ee0305 1339 }
benkatz 23:2adf23ee0305 1340
benkatz 23:2adf23ee0305 1341 /**
benkatz 23:2adf23ee0305 1342 * @brief Returns the FLASH Write Protection Option Bytes value.
benkatz 23:2adf23ee0305 1343 * @param None
benkatz 23:2adf23ee0305 1344 * @retval The FLASH Write Protection Option Bytes value
benkatz 23:2adf23ee0305 1345 */
benkatz 23:2adf23ee0305 1346 uint16_t FLASH_OB_GetWRP(void)
benkatz 23:2adf23ee0305 1347 {
benkatz 23:2adf23ee0305 1348 /* Return the FLASH write protection Register value */
benkatz 23:2adf23ee0305 1349 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
benkatz 23:2adf23ee0305 1350 }
benkatz 23:2adf23ee0305 1351
benkatz 23:2adf23ee0305 1352 /**
benkatz 23:2adf23ee0305 1353 * @brief Returns the FLASH Write Protection Option Bytes value.
benkatz 23:2adf23ee0305 1354 *
benkatz 23:2adf23ee0305 1355 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 1356 *
benkatz 23:2adf23ee0305 1357 * @param None
benkatz 23:2adf23ee0305 1358 * @retval The FLASH Write Protection Option Bytes value
benkatz 23:2adf23ee0305 1359 */
benkatz 23:2adf23ee0305 1360 uint16_t FLASH_OB_GetWRP1(void)
benkatz 23:2adf23ee0305 1361 {
benkatz 23:2adf23ee0305 1362 /* Return the FLASH write protection Register value */
benkatz 23:2adf23ee0305 1363 return (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
benkatz 23:2adf23ee0305 1364 }
benkatz 23:2adf23ee0305 1365
benkatz 23:2adf23ee0305 1366 /**
benkatz 23:2adf23ee0305 1367 * @brief Returns the FLASH PC Read/Write Protection Option Bytes value.
benkatz 23:2adf23ee0305 1368 *
benkatz 23:2adf23ee0305 1369 * @note This function can be used only for STM32F42xxx/43xxx devices and STM32F401xx/411xE devices.
benkatz 23:2adf23ee0305 1370 *
benkatz 23:2adf23ee0305 1371 * @param None
benkatz 23:2adf23ee0305 1372 * @retval The FLASH PC Read/Write Protection Option Bytes value
benkatz 23:2adf23ee0305 1373 */
benkatz 23:2adf23ee0305 1374 uint16_t FLASH_OB_GetPCROP(void)
benkatz 23:2adf23ee0305 1375 {
benkatz 23:2adf23ee0305 1376 /* Return the FLASH PC Read/write protection Register value */
benkatz 23:2adf23ee0305 1377 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
benkatz 23:2adf23ee0305 1378 }
benkatz 23:2adf23ee0305 1379
benkatz 23:2adf23ee0305 1380 /**
benkatz 23:2adf23ee0305 1381 * @brief Returns the FLASH PC Read/Write Protection Option Bytes value.
benkatz 23:2adf23ee0305 1382 *
benkatz 23:2adf23ee0305 1383 * @note This function can be used only for STM32F42xxx/43xxx devices.
benkatz 23:2adf23ee0305 1384 *
benkatz 23:2adf23ee0305 1385 * @param None
benkatz 23:2adf23ee0305 1386 * @retval The FLASH PC Read/Write Protection Option Bytes value
benkatz 23:2adf23ee0305 1387 */
benkatz 23:2adf23ee0305 1388 uint16_t FLASH_OB_GetPCROP1(void)
benkatz 23:2adf23ee0305 1389 {
benkatz 23:2adf23ee0305 1390 /* Return the FLASH write protection Register value */
benkatz 23:2adf23ee0305 1391 return (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
benkatz 23:2adf23ee0305 1392 }
benkatz 23:2adf23ee0305 1393
benkatz 23:2adf23ee0305 1394 /**
benkatz 23:2adf23ee0305 1395 * @brief Returns the FLASH Read Protection level.
benkatz 23:2adf23ee0305 1396 * @param None
benkatz 23:2adf23ee0305 1397 * @retval FLASH ReadOut Protection Status:
benkatz 23:2adf23ee0305 1398 * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
benkatz 23:2adf23ee0305 1399 * - RESET, when OB_RDP_Level_0 is set
benkatz 23:2adf23ee0305 1400 */
benkatz 23:2adf23ee0305 1401 FlagStatus FLASH_OB_GetRDP(void)
benkatz 23:2adf23ee0305 1402 {
benkatz 23:2adf23ee0305 1403 FlagStatus readstatus = RESET;
benkatz 23:2adf23ee0305 1404
benkatz 23:2adf23ee0305 1405 if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) != (uint8_t)OB_RDP_Level_0))
benkatz 23:2adf23ee0305 1406 {
benkatz 23:2adf23ee0305 1407 readstatus = SET;
benkatz 23:2adf23ee0305 1408 }
benkatz 23:2adf23ee0305 1409 else
benkatz 23:2adf23ee0305 1410 {
benkatz 23:2adf23ee0305 1411 readstatus = RESET;
benkatz 23:2adf23ee0305 1412 }
benkatz 23:2adf23ee0305 1413 return readstatus;
benkatz 23:2adf23ee0305 1414 }
benkatz 23:2adf23ee0305 1415
benkatz 23:2adf23ee0305 1416 /**
benkatz 23:2adf23ee0305 1417 * @brief Returns the FLASH BOR level.
benkatz 23:2adf23ee0305 1418 * @param None
benkatz 23:2adf23ee0305 1419 * @retval The FLASH BOR level:
benkatz 23:2adf23ee0305 1420 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
benkatz 23:2adf23ee0305 1421 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
benkatz 23:2adf23ee0305 1422 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
benkatz 23:2adf23ee0305 1423 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
benkatz 23:2adf23ee0305 1424 */
benkatz 23:2adf23ee0305 1425 uint8_t FLASH_OB_GetBOR(void)
benkatz 23:2adf23ee0305 1426 {
benkatz 23:2adf23ee0305 1427 /* Return the FLASH BOR level */
benkatz 23:2adf23ee0305 1428 return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
benkatz 23:2adf23ee0305 1429 }
benkatz 23:2adf23ee0305 1430
benkatz 23:2adf23ee0305 1431 /**
benkatz 23:2adf23ee0305 1432 * @}
benkatz 23:2adf23ee0305 1433 */
benkatz 23:2adf23ee0305 1434
benkatz 23:2adf23ee0305 1435 /** @defgroup FLASH_Group4 Interrupts and flags management functions
benkatz 23:2adf23ee0305 1436 * @brief Interrupts and flags management functions
benkatz 23:2adf23ee0305 1437 *
benkatz 23:2adf23ee0305 1438 @verbatim
benkatz 23:2adf23ee0305 1439 ===============================================================================
benkatz 23:2adf23ee0305 1440 ##### Interrupts and flags management functions #####
benkatz 23:2adf23ee0305 1441 ===============================================================================
benkatz 23:2adf23ee0305 1442 @endverbatim
benkatz 23:2adf23ee0305 1443 * @{
benkatz 23:2adf23ee0305 1444 */
benkatz 23:2adf23ee0305 1445
benkatz 23:2adf23ee0305 1446 /**
benkatz 23:2adf23ee0305 1447 * @brief Enables or disables the specified FLASH interrupts.
benkatz 23:2adf23ee0305 1448 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
benkatz 23:2adf23ee0305 1449 * This parameter can be any combination of the following values:
benkatz 23:2adf23ee0305 1450 * @arg FLASH_IT_ERR: FLASH Error Interrupt
benkatz 23:2adf23ee0305 1451 * @arg FLASH_IT_EOP: FLASH end of operation Interrupt
benkatz 23:2adf23ee0305 1452 * @retval None
benkatz 23:2adf23ee0305 1453 */
benkatz 23:2adf23ee0305 1454 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
benkatz 23:2adf23ee0305 1455 {
benkatz 23:2adf23ee0305 1456 /* Check the parameters */
benkatz 23:2adf23ee0305 1457 assert_param(IS_FLASH_IT(FLASH_IT));
benkatz 23:2adf23ee0305 1458 assert_param(IS_FUNCTIONAL_STATE(NewState));
benkatz 23:2adf23ee0305 1459
benkatz 23:2adf23ee0305 1460 if(NewState != DISABLE)
benkatz 23:2adf23ee0305 1461 {
benkatz 23:2adf23ee0305 1462 /* Enable the interrupt sources */
benkatz 23:2adf23ee0305 1463 FLASH->CR |= FLASH_IT;
benkatz 23:2adf23ee0305 1464 }
benkatz 23:2adf23ee0305 1465 else
benkatz 23:2adf23ee0305 1466 {
benkatz 23:2adf23ee0305 1467 /* Disable the interrupt sources */
benkatz 23:2adf23ee0305 1468 FLASH->CR &= ~(uint32_t)FLASH_IT;
benkatz 23:2adf23ee0305 1469 }
benkatz 23:2adf23ee0305 1470 }
benkatz 23:2adf23ee0305 1471
benkatz 23:2adf23ee0305 1472 /**
benkatz 23:2adf23ee0305 1473 * @brief Checks whether the specified FLASH flag is set or not.
benkatz 23:2adf23ee0305 1474 * @param FLASH_FLAG: specifies the FLASH flag to check.
benkatz 23:2adf23ee0305 1475 * This parameter can be one of the following values:
benkatz 23:2adf23ee0305 1476 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
benkatz 23:2adf23ee0305 1477 * @arg FLASH_FLAG_OPERR: FLASH operation Error flag
benkatz 23:2adf23ee0305 1478 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
benkatz 23:2adf23ee0305 1479 * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
benkatz 23:2adf23ee0305 1480 * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
benkatz 23:2adf23ee0305 1481 * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
benkatz 23:2adf23ee0305 1482 * @arg FLASH_FLAG_RDERR: FLASH (PCROP) Read Protection error flag (STM32F42xx/43xxx and STM32F401xx/411xE devices)
benkatz 23:2adf23ee0305 1483 * @arg FLASH_FLAG_BSY: FLASH Busy flag
benkatz 23:2adf23ee0305 1484 * @retval The new state of FLASH_FLAG (SET or RESET).
benkatz 23:2adf23ee0305 1485 */
benkatz 23:2adf23ee0305 1486 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
benkatz 23:2adf23ee0305 1487 {
benkatz 23:2adf23ee0305 1488 FlagStatus bitstatus = RESET;
benkatz 23:2adf23ee0305 1489 /* Check the parameters */
benkatz 23:2adf23ee0305 1490 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
benkatz 23:2adf23ee0305 1491
benkatz 23:2adf23ee0305 1492 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
benkatz 23:2adf23ee0305 1493 {
benkatz 23:2adf23ee0305 1494 bitstatus = SET;
benkatz 23:2adf23ee0305 1495 }
benkatz 23:2adf23ee0305 1496 else
benkatz 23:2adf23ee0305 1497 {
benkatz 23:2adf23ee0305 1498 bitstatus = RESET;
benkatz 23:2adf23ee0305 1499 }
benkatz 23:2adf23ee0305 1500 /* Return the new state of FLASH_FLAG (SET or RESET) */
benkatz 23:2adf23ee0305 1501 return bitstatus;
benkatz 23:2adf23ee0305 1502 }
benkatz 23:2adf23ee0305 1503
benkatz 23:2adf23ee0305 1504 /**
benkatz 23:2adf23ee0305 1505 * @brief Clears the FLASH's pending flags.
benkatz 23:2adf23ee0305 1506 * @param FLASH_FLAG: specifies the FLASH flags to clear.
benkatz 23:2adf23ee0305 1507 * This parameter can be any combination of the following values:
benkatz 23:2adf23ee0305 1508 * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
benkatz 23:2adf23ee0305 1509 * @arg FLASH_FLAG_OPERR: FLASH operation Error flag
benkatz 23:2adf23ee0305 1510 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
benkatz 23:2adf23ee0305 1511 * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
benkatz 23:2adf23ee0305 1512 * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
benkatz 23:2adf23ee0305 1513 * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
benkatz 23:2adf23ee0305 1514 * @arg FLASH_FLAG_RDERR: FLASH Read Protection error flag (STM32F42xx/43xxx and STM32F401xx/411xE devices)
benkatz 23:2adf23ee0305 1515 * @retval None
benkatz 23:2adf23ee0305 1516 */
benkatz 23:2adf23ee0305 1517 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
benkatz 23:2adf23ee0305 1518 {
benkatz 23:2adf23ee0305 1519 /* Check the parameters */
benkatz 23:2adf23ee0305 1520 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
benkatz 23:2adf23ee0305 1521
benkatz 23:2adf23ee0305 1522 /* Clear the flags */
benkatz 23:2adf23ee0305 1523 FLASH->SR = FLASH_FLAG;
benkatz 23:2adf23ee0305 1524 }
benkatz 23:2adf23ee0305 1525
benkatz 23:2adf23ee0305 1526 /**
benkatz 23:2adf23ee0305 1527 * @brief Returns the FLASH Status.
benkatz 23:2adf23ee0305 1528 * @param None
benkatz 23:2adf23ee0305 1529 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 1530 * FLASH_ERROR_WRP2, FLASH_ERROR_RD2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 1531 */
benkatz 23:2adf23ee0305 1532 FLASH_Status FLASH_GetStatus(void)
benkatz 23:2adf23ee0305 1533 {
benkatz 23:2adf23ee0305 1534 FLASH_Status flashstatus = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1535
benkatz 23:2adf23ee0305 1536 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
benkatz 23:2adf23ee0305 1537 {
benkatz 23:2adf23ee0305 1538 flashstatus = FLASH_BUSY2;
benkatz 23:2adf23ee0305 1539 }
benkatz 23:2adf23ee0305 1540 else
benkatz 23:2adf23ee0305 1541 {
benkatz 23:2adf23ee0305 1542 if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)
benkatz 23:2adf23ee0305 1543 {
benkatz 23:2adf23ee0305 1544 flashstatus = FLASH_ERROR_WRP2;
benkatz 23:2adf23ee0305 1545 }
benkatz 23:2adf23ee0305 1546 else
benkatz 23:2adf23ee0305 1547 {
benkatz 23:2adf23ee0305 1548 if((FLASH->SR & FLASH_FLAG_RDERR) != (uint32_t)0x00)
benkatz 23:2adf23ee0305 1549 {
benkatz 23:2adf23ee0305 1550 flashstatus = FLASH_ERROR_RD2;
benkatz 23:2adf23ee0305 1551 }
benkatz 23:2adf23ee0305 1552 else
benkatz 23:2adf23ee0305 1553 {
benkatz 23:2adf23ee0305 1554 if((FLASH->SR & (uint32_t)0xE0) != (uint32_t)0x00)
benkatz 23:2adf23ee0305 1555 {
benkatz 23:2adf23ee0305 1556 flashstatus = FLASH_ERROR_PROGRAM2;
benkatz 23:2adf23ee0305 1557 }
benkatz 23:2adf23ee0305 1558 else
benkatz 23:2adf23ee0305 1559 {
benkatz 23:2adf23ee0305 1560 if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)
benkatz 23:2adf23ee0305 1561 {
benkatz 23:2adf23ee0305 1562 flashstatus = FLASH_ERROR_OPERATION2;
benkatz 23:2adf23ee0305 1563 }
benkatz 23:2adf23ee0305 1564 else
benkatz 23:2adf23ee0305 1565 {
benkatz 23:2adf23ee0305 1566 flashstatus = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1567 }
benkatz 23:2adf23ee0305 1568 }
benkatz 23:2adf23ee0305 1569 }
benkatz 23:2adf23ee0305 1570 }
benkatz 23:2adf23ee0305 1571 }
benkatz 23:2adf23ee0305 1572 /* Return the FLASH Status */
benkatz 23:2adf23ee0305 1573 return flashstatus;
benkatz 23:2adf23ee0305 1574 }
benkatz 23:2adf23ee0305 1575
benkatz 23:2adf23ee0305 1576 /**
benkatz 23:2adf23ee0305 1577 * @brief Waits for a FLASH operation to complete.
benkatz 23:2adf23ee0305 1578 * @param None
benkatz 23:2adf23ee0305 1579 * @retval FLASH Status: The returned value can be: FLASH_BUSY2, FLASH_ERROR_PROGRAM2,
benkatz 23:2adf23ee0305 1580 * FLASH_ERROR_WRP2, FLASH_ERROR_OPERATION2 or FLASH_COMPLETE2.
benkatz 23:2adf23ee0305 1581 */
benkatz 23:2adf23ee0305 1582 FLASH_Status FLASH_WaitForLastOperation2(void)
benkatz 23:2adf23ee0305 1583 {
benkatz 23:2adf23ee0305 1584 __IO FLASH_Status status = FLASH_COMPLETE2;
benkatz 23:2adf23ee0305 1585
benkatz 23:2adf23ee0305 1586 /* Check for the FLASH Status */
benkatz 23:2adf23ee0305 1587 status = FLASH_GetStatus();
benkatz 23:2adf23ee0305 1588
benkatz 23:2adf23ee0305 1589 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
benkatz 23:2adf23ee0305 1590 Even if the FLASH operation fails, the BUSY flag will be reset and an error
benkatz 23:2adf23ee0305 1591 flag will be set */
benkatz 23:2adf23ee0305 1592 while(status == FLASH_BUSY2)
benkatz 23:2adf23ee0305 1593 {
benkatz 23:2adf23ee0305 1594 status = FLASH_GetStatus();
benkatz 23:2adf23ee0305 1595 }
benkatz 23:2adf23ee0305 1596 /* Return the operation status */
benkatz 23:2adf23ee0305 1597 return status;
benkatz 23:2adf23ee0305 1598 }
benkatz 23:2adf23ee0305 1599
benkatz 23:2adf23ee0305 1600 /**
benkatz 23:2adf23ee0305 1601 * @}
benkatz 23:2adf23ee0305 1602 */
benkatz 23:2adf23ee0305 1603
benkatz 23:2adf23ee0305 1604 /**
benkatz 23:2adf23ee0305 1605 * @}
benkatz 23:2adf23ee0305 1606 */
benkatz 23:2adf23ee0305 1607
benkatz 23:2adf23ee0305 1608 /**
benkatz 23:2adf23ee0305 1609 * @}
benkatz 23:2adf23ee0305 1610 */
benkatz 23:2adf23ee0305 1611
benkatz 23:2adf23ee0305 1612 /**
benkatz 23:2adf23ee0305 1613 * @}
benkatz 23:2adf23ee0305 1614 */
benkatz 23:2adf23ee0305 1615
benkatz 23:2adf23ee0305 1616 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/