inport from local
Dependents: Hobbyking_Cheetah_0511
targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_flash.c@0:85b3fd62ea1a, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:35:48 2020 +0000
- Revision:
- 0:85b3fd62ea1a
reinport to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:85b3fd62ea1a | 1 | /** |
NYX | 0:85b3fd62ea1a | 2 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 3 | * @file stm32f4xx_hal_flash.c |
NYX | 0:85b3fd62ea1a | 4 | * @author MCD Application Team |
NYX | 0:85b3fd62ea1a | 5 | * @version V1.7.1 |
NYX | 0:85b3fd62ea1a | 6 | * @date 14-April-2017 |
NYX | 0:85b3fd62ea1a | 7 | * @brief FLASH HAL module driver. |
NYX | 0:85b3fd62ea1a | 8 | * This file provides firmware functions to manage the following |
NYX | 0:85b3fd62ea1a | 9 | * functionalities of the internal FLASH memory: |
NYX | 0:85b3fd62ea1a | 10 | * + Program operations functions |
NYX | 0:85b3fd62ea1a | 11 | * + Memory Control functions |
NYX | 0:85b3fd62ea1a | 12 | * + Peripheral Errors functions |
NYX | 0:85b3fd62ea1a | 13 | * |
NYX | 0:85b3fd62ea1a | 14 | @verbatim |
NYX | 0:85b3fd62ea1a | 15 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 16 | ##### FLASH peripheral features ##### |
NYX | 0:85b3fd62ea1a | 17 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 18 | |
NYX | 0:85b3fd62ea1a | 19 | [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses |
NYX | 0:85b3fd62ea1a | 20 | to the Flash memory. It implements the erase and program Flash memory operations |
NYX | 0:85b3fd62ea1a | 21 | and the read and write protection mechanisms. |
NYX | 0:85b3fd62ea1a | 22 | |
NYX | 0:85b3fd62ea1a | 23 | [..] The Flash memory interface accelerates code execution with a system of instruction |
NYX | 0:85b3fd62ea1a | 24 | prefetch and cache lines. |
NYX | 0:85b3fd62ea1a | 25 | |
NYX | 0:85b3fd62ea1a | 26 | [..] The FLASH main features are: |
NYX | 0:85b3fd62ea1a | 27 | (+) Flash memory read operations |
NYX | 0:85b3fd62ea1a | 28 | (+) Flash memory program/erase operations |
NYX | 0:85b3fd62ea1a | 29 | (+) Read / write protections |
NYX | 0:85b3fd62ea1a | 30 | (+) Prefetch on I-Code |
NYX | 0:85b3fd62ea1a | 31 | (+) 64 cache lines of 128 bits on I-Code |
NYX | 0:85b3fd62ea1a | 32 | (+) 8 cache lines of 128 bits on D-Code |
NYX | 0:85b3fd62ea1a | 33 | |
NYX | 0:85b3fd62ea1a | 34 | |
NYX | 0:85b3fd62ea1a | 35 | ##### How to use this driver ##### |
NYX | 0:85b3fd62ea1a | 36 | ============================================================================== |
NYX | 0:85b3fd62ea1a | 37 | [..] |
NYX | 0:85b3fd62ea1a | 38 | This driver provides functions and macros to configure and program the FLASH |
NYX | 0:85b3fd62ea1a | 39 | memory of all STM32F4xx devices. |
NYX | 0:85b3fd62ea1a | 40 | |
NYX | 0:85b3fd62ea1a | 41 | (#) FLASH Memory IO Programming functions: |
NYX | 0:85b3fd62ea1a | 42 | (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and |
NYX | 0:85b3fd62ea1a | 43 | HAL_FLASH_Lock() functions |
NYX | 0:85b3fd62ea1a | 44 | (++) Program functions: byte, half word, word and double word |
NYX | 0:85b3fd62ea1a | 45 | (++) There Two modes of programming : |
NYX | 0:85b3fd62ea1a | 46 | (+++) Polling mode using HAL_FLASH_Program() function |
NYX | 0:85b3fd62ea1a | 47 | (+++) Interrupt mode using HAL_FLASH_Program_IT() function |
NYX | 0:85b3fd62ea1a | 48 | |
NYX | 0:85b3fd62ea1a | 49 | (#) Interrupts and flags management functions : |
NYX | 0:85b3fd62ea1a | 50 | (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler() |
NYX | 0:85b3fd62ea1a | 51 | (++) Wait for last FLASH operation according to its status |
NYX | 0:85b3fd62ea1a | 52 | (++) Get error flag status by calling HAL_SetErrorCode() |
NYX | 0:85b3fd62ea1a | 53 | |
NYX | 0:85b3fd62ea1a | 54 | [..] |
NYX | 0:85b3fd62ea1a | 55 | In addition to these functions, this driver includes a set of macros allowing |
NYX | 0:85b3fd62ea1a | 56 | to handle the following operations: |
NYX | 0:85b3fd62ea1a | 57 | (+) Set the latency |
NYX | 0:85b3fd62ea1a | 58 | (+) Enable/Disable the prefetch buffer |
NYX | 0:85b3fd62ea1a | 59 | (+) Enable/Disable the Instruction cache and the Data cache |
NYX | 0:85b3fd62ea1a | 60 | (+) Reset the Instruction cache and the Data cache |
NYX | 0:85b3fd62ea1a | 61 | (+) Enable/Disable the FLASH interrupts |
NYX | 0:85b3fd62ea1a | 62 | (+) Monitor the FLASH flags status |
NYX | 0:85b3fd62ea1a | 63 | |
NYX | 0:85b3fd62ea1a | 64 | @endverbatim |
NYX | 0:85b3fd62ea1a | 65 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 66 | * @attention |
NYX | 0:85b3fd62ea1a | 67 | * |
NYX | 0:85b3fd62ea1a | 68 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
NYX | 0:85b3fd62ea1a | 69 | * |
NYX | 0:85b3fd62ea1a | 70 | * Redistribution and use in source and binary forms, with or without modification, |
NYX | 0:85b3fd62ea1a | 71 | * are permitted provided that the following conditions are met: |
NYX | 0:85b3fd62ea1a | 72 | * 1. Redistributions of source code must retain the above copyright notice, |
NYX | 0:85b3fd62ea1a | 73 | * this list of conditions and the following disclaimer. |
NYX | 0:85b3fd62ea1a | 74 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
NYX | 0:85b3fd62ea1a | 75 | * this list of conditions and the following disclaimer in the documentation |
NYX | 0:85b3fd62ea1a | 76 | * and/or other materials provided with the distribution. |
NYX | 0:85b3fd62ea1a | 77 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
NYX | 0:85b3fd62ea1a | 78 | * may be used to endorse or promote products derived from this software |
NYX | 0:85b3fd62ea1a | 79 | * without specific prior written permission. |
NYX | 0:85b3fd62ea1a | 80 | * |
NYX | 0:85b3fd62ea1a | 81 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
NYX | 0:85b3fd62ea1a | 82 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
NYX | 0:85b3fd62ea1a | 83 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
NYX | 0:85b3fd62ea1a | 84 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
NYX | 0:85b3fd62ea1a | 85 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
NYX | 0:85b3fd62ea1a | 86 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
NYX | 0:85b3fd62ea1a | 87 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
NYX | 0:85b3fd62ea1a | 88 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
NYX | 0:85b3fd62ea1a | 89 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
NYX | 0:85b3fd62ea1a | 90 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
NYX | 0:85b3fd62ea1a | 91 | * |
NYX | 0:85b3fd62ea1a | 92 | ****************************************************************************** |
NYX | 0:85b3fd62ea1a | 93 | */ |
NYX | 0:85b3fd62ea1a | 94 | |
NYX | 0:85b3fd62ea1a | 95 | /* Includes ------------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 96 | #include "stm32f4xx_hal.h" |
NYX | 0:85b3fd62ea1a | 97 | |
NYX | 0:85b3fd62ea1a | 98 | /** @addtogroup STM32F4xx_HAL_Driver |
NYX | 0:85b3fd62ea1a | 99 | * @{ |
NYX | 0:85b3fd62ea1a | 100 | */ |
NYX | 0:85b3fd62ea1a | 101 | |
NYX | 0:85b3fd62ea1a | 102 | /** @defgroup FLASH FLASH |
NYX | 0:85b3fd62ea1a | 103 | * @brief FLASH HAL module driver |
NYX | 0:85b3fd62ea1a | 104 | * @{ |
NYX | 0:85b3fd62ea1a | 105 | */ |
NYX | 0:85b3fd62ea1a | 106 | |
NYX | 0:85b3fd62ea1a | 107 | #ifdef HAL_FLASH_MODULE_ENABLED |
NYX | 0:85b3fd62ea1a | 108 | |
NYX | 0:85b3fd62ea1a | 109 | /* Private typedef -----------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 110 | /* Private define ------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 111 | /** @addtogroup FLASH_Private_Constants |
NYX | 0:85b3fd62ea1a | 112 | * @{ |
NYX | 0:85b3fd62ea1a | 113 | */ |
NYX | 0:85b3fd62ea1a | 114 | #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */ |
NYX | 0:85b3fd62ea1a | 115 | /** |
NYX | 0:85b3fd62ea1a | 116 | * @} |
NYX | 0:85b3fd62ea1a | 117 | */ |
NYX | 0:85b3fd62ea1a | 118 | /* Private macro -------------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 119 | /* Private variables ---------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 120 | /** @addtogroup FLASH_Private_Variables |
NYX | 0:85b3fd62ea1a | 121 | * @{ |
NYX | 0:85b3fd62ea1a | 122 | */ |
NYX | 0:85b3fd62ea1a | 123 | /* Variable used for Erase sectors under interruption */ |
NYX | 0:85b3fd62ea1a | 124 | FLASH_ProcessTypeDef pFlash; |
NYX | 0:85b3fd62ea1a | 125 | /** |
NYX | 0:85b3fd62ea1a | 126 | * @} |
NYX | 0:85b3fd62ea1a | 127 | */ |
NYX | 0:85b3fd62ea1a | 128 | |
NYX | 0:85b3fd62ea1a | 129 | /* Private function prototypes -----------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 130 | /** @addtogroup FLASH_Private_Functions |
NYX | 0:85b3fd62ea1a | 131 | * @{ |
NYX | 0:85b3fd62ea1a | 132 | */ |
NYX | 0:85b3fd62ea1a | 133 | /* Program operations */ |
NYX | 0:85b3fd62ea1a | 134 | static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data); |
NYX | 0:85b3fd62ea1a | 135 | static void FLASH_Program_Word(uint32_t Address, uint32_t Data); |
NYX | 0:85b3fd62ea1a | 136 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data); |
NYX | 0:85b3fd62ea1a | 137 | static void FLASH_Program_Byte(uint32_t Address, uint8_t Data); |
NYX | 0:85b3fd62ea1a | 138 | static void FLASH_SetErrorCode(void); |
NYX | 0:85b3fd62ea1a | 139 | |
NYX | 0:85b3fd62ea1a | 140 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout); |
NYX | 0:85b3fd62ea1a | 141 | /** |
NYX | 0:85b3fd62ea1a | 142 | * @} |
NYX | 0:85b3fd62ea1a | 143 | */ |
NYX | 0:85b3fd62ea1a | 144 | |
NYX | 0:85b3fd62ea1a | 145 | /* Exported functions --------------------------------------------------------*/ |
NYX | 0:85b3fd62ea1a | 146 | /** @defgroup FLASH_Exported_Functions FLASH Exported Functions |
NYX | 0:85b3fd62ea1a | 147 | * @{ |
NYX | 0:85b3fd62ea1a | 148 | */ |
NYX | 0:85b3fd62ea1a | 149 | |
NYX | 0:85b3fd62ea1a | 150 | /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions |
NYX | 0:85b3fd62ea1a | 151 | * @brief Programming operation functions |
NYX | 0:85b3fd62ea1a | 152 | * |
NYX | 0:85b3fd62ea1a | 153 | @verbatim |
NYX | 0:85b3fd62ea1a | 154 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 155 | ##### Programming operation functions ##### |
NYX | 0:85b3fd62ea1a | 156 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 157 | [..] |
NYX | 0:85b3fd62ea1a | 158 | This subsection provides a set of functions allowing to manage the FLASH |
NYX | 0:85b3fd62ea1a | 159 | program operations. |
NYX | 0:85b3fd62ea1a | 160 | |
NYX | 0:85b3fd62ea1a | 161 | @endverbatim |
NYX | 0:85b3fd62ea1a | 162 | * @{ |
NYX | 0:85b3fd62ea1a | 163 | */ |
NYX | 0:85b3fd62ea1a | 164 | |
NYX | 0:85b3fd62ea1a | 165 | /** |
NYX | 0:85b3fd62ea1a | 166 | * @brief Program byte, halfword, word or double word at a specified address |
NYX | 0:85b3fd62ea1a | 167 | * @param TypeProgram: Indicate the way to program at a specified address. |
NYX | 0:85b3fd62ea1a | 168 | * This parameter can be a value of @ref FLASH_Type_Program |
NYX | 0:85b3fd62ea1a | 169 | * @param Address: specifies the address to be programmed. |
NYX | 0:85b3fd62ea1a | 170 | * @param Data: specifies the data to be programmed |
NYX | 0:85b3fd62ea1a | 171 | * |
NYX | 0:85b3fd62ea1a | 172 | * @retval HAL_StatusTypeDef HAL Status |
NYX | 0:85b3fd62ea1a | 173 | */ |
NYX | 0:85b3fd62ea1a | 174 | HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
NYX | 0:85b3fd62ea1a | 175 | { |
NYX | 0:85b3fd62ea1a | 176 | HAL_StatusTypeDef status = HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 177 | |
NYX | 0:85b3fd62ea1a | 178 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 179 | __HAL_LOCK(&pFlash); |
NYX | 0:85b3fd62ea1a | 180 | |
NYX | 0:85b3fd62ea1a | 181 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 182 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
NYX | 0:85b3fd62ea1a | 183 | |
NYX | 0:85b3fd62ea1a | 184 | /* Wait for last operation to be completed */ |
NYX | 0:85b3fd62ea1a | 185 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
NYX | 0:85b3fd62ea1a | 186 | |
NYX | 0:85b3fd62ea1a | 187 | if(status == HAL_OK) |
NYX | 0:85b3fd62ea1a | 188 | { |
NYX | 0:85b3fd62ea1a | 189 | if(TypeProgram == FLASH_TYPEPROGRAM_BYTE) |
NYX | 0:85b3fd62ea1a | 190 | { |
NYX | 0:85b3fd62ea1a | 191 | /*Program byte (8-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 192 | FLASH_Program_Byte(Address, (uint8_t) Data); |
NYX | 0:85b3fd62ea1a | 193 | } |
NYX | 0:85b3fd62ea1a | 194 | else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
NYX | 0:85b3fd62ea1a | 195 | { |
NYX | 0:85b3fd62ea1a | 196 | /*Program halfword (16-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 197 | FLASH_Program_HalfWord(Address, (uint16_t) Data); |
NYX | 0:85b3fd62ea1a | 198 | } |
NYX | 0:85b3fd62ea1a | 199 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
NYX | 0:85b3fd62ea1a | 200 | { |
NYX | 0:85b3fd62ea1a | 201 | /*Program word (32-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 202 | FLASH_Program_Word(Address, (uint32_t) Data); |
NYX | 0:85b3fd62ea1a | 203 | } |
NYX | 0:85b3fd62ea1a | 204 | else |
NYX | 0:85b3fd62ea1a | 205 | { |
NYX | 0:85b3fd62ea1a | 206 | /*Program double word (64-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 207 | FLASH_Program_DoubleWord(Address, Data); |
NYX | 0:85b3fd62ea1a | 208 | } |
NYX | 0:85b3fd62ea1a | 209 | |
NYX | 0:85b3fd62ea1a | 210 | /* Wait for last operation to be completed */ |
NYX | 0:85b3fd62ea1a | 211 | status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); |
NYX | 0:85b3fd62ea1a | 212 | |
NYX | 0:85b3fd62ea1a | 213 | /* If the program operation is completed, disable the PG Bit */ |
NYX | 0:85b3fd62ea1a | 214 | FLASH->CR &= (~FLASH_CR_PG); |
NYX | 0:85b3fd62ea1a | 215 | } |
NYX | 0:85b3fd62ea1a | 216 | |
NYX | 0:85b3fd62ea1a | 217 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 218 | __HAL_UNLOCK(&pFlash); |
NYX | 0:85b3fd62ea1a | 219 | |
NYX | 0:85b3fd62ea1a | 220 | return status; |
NYX | 0:85b3fd62ea1a | 221 | } |
NYX | 0:85b3fd62ea1a | 222 | |
NYX | 0:85b3fd62ea1a | 223 | /** |
NYX | 0:85b3fd62ea1a | 224 | * @brief Program byte, halfword, word or double word at a specified address with interrupt enabled. |
NYX | 0:85b3fd62ea1a | 225 | * @param TypeProgram: Indicate the way to program at a specified address. |
NYX | 0:85b3fd62ea1a | 226 | * This parameter can be a value of @ref FLASH_Type_Program |
NYX | 0:85b3fd62ea1a | 227 | * @param Address: specifies the address to be programmed. |
NYX | 0:85b3fd62ea1a | 228 | * @param Data: specifies the data to be programmed |
NYX | 0:85b3fd62ea1a | 229 | * |
NYX | 0:85b3fd62ea1a | 230 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 231 | */ |
NYX | 0:85b3fd62ea1a | 232 | HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data) |
NYX | 0:85b3fd62ea1a | 233 | { |
NYX | 0:85b3fd62ea1a | 234 | HAL_StatusTypeDef status = HAL_OK; |
NYX | 0:85b3fd62ea1a | 235 | |
NYX | 0:85b3fd62ea1a | 236 | /* Process Locked */ |
NYX | 0:85b3fd62ea1a | 237 | __HAL_LOCK(&pFlash); |
NYX | 0:85b3fd62ea1a | 238 | |
NYX | 0:85b3fd62ea1a | 239 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 240 | assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); |
NYX | 0:85b3fd62ea1a | 241 | |
NYX | 0:85b3fd62ea1a | 242 | /* Enable End of FLASH Operation interrupt */ |
NYX | 0:85b3fd62ea1a | 243 | __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP); |
NYX | 0:85b3fd62ea1a | 244 | |
NYX | 0:85b3fd62ea1a | 245 | /* Enable Error source interrupt */ |
NYX | 0:85b3fd62ea1a | 246 | __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR); |
NYX | 0:85b3fd62ea1a | 247 | |
NYX | 0:85b3fd62ea1a | 248 | pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM; |
NYX | 0:85b3fd62ea1a | 249 | pFlash.Address = Address; |
NYX | 0:85b3fd62ea1a | 250 | |
NYX | 0:85b3fd62ea1a | 251 | if(TypeProgram == FLASH_TYPEPROGRAM_BYTE) |
NYX | 0:85b3fd62ea1a | 252 | { |
NYX | 0:85b3fd62ea1a | 253 | /*Program byte (8-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 254 | FLASH_Program_Byte(Address, (uint8_t) Data); |
NYX | 0:85b3fd62ea1a | 255 | } |
NYX | 0:85b3fd62ea1a | 256 | else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) |
NYX | 0:85b3fd62ea1a | 257 | { |
NYX | 0:85b3fd62ea1a | 258 | /*Program halfword (16-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 259 | FLASH_Program_HalfWord(Address, (uint16_t) Data); |
NYX | 0:85b3fd62ea1a | 260 | } |
NYX | 0:85b3fd62ea1a | 261 | else if(TypeProgram == FLASH_TYPEPROGRAM_WORD) |
NYX | 0:85b3fd62ea1a | 262 | { |
NYX | 0:85b3fd62ea1a | 263 | /*Program word (32-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 264 | FLASH_Program_Word(Address, (uint32_t) Data); |
NYX | 0:85b3fd62ea1a | 265 | } |
NYX | 0:85b3fd62ea1a | 266 | else |
NYX | 0:85b3fd62ea1a | 267 | { |
NYX | 0:85b3fd62ea1a | 268 | /*Program double word (64-bit) at a specified address.*/ |
NYX | 0:85b3fd62ea1a | 269 | FLASH_Program_DoubleWord(Address, Data); |
NYX | 0:85b3fd62ea1a | 270 | } |
NYX | 0:85b3fd62ea1a | 271 | |
NYX | 0:85b3fd62ea1a | 272 | return status; |
NYX | 0:85b3fd62ea1a | 273 | } |
NYX | 0:85b3fd62ea1a | 274 | |
NYX | 0:85b3fd62ea1a | 275 | /** |
NYX | 0:85b3fd62ea1a | 276 | * @brief This function handles FLASH interrupt request. |
NYX | 0:85b3fd62ea1a | 277 | * @retval None |
NYX | 0:85b3fd62ea1a | 278 | */ |
NYX | 0:85b3fd62ea1a | 279 | void HAL_FLASH_IRQHandler(void) |
NYX | 0:85b3fd62ea1a | 280 | { |
NYX | 0:85b3fd62ea1a | 281 | uint32_t addresstmp = 0U; |
NYX | 0:85b3fd62ea1a | 282 | |
NYX | 0:85b3fd62ea1a | 283 | /* Check FLASH operation error flags */ |
NYX | 0:85b3fd62ea1a | 284 | #if defined(FLASH_SR_RDERR) |
NYX | 0:85b3fd62ea1a | 285 | if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \ |
NYX | 0:85b3fd62ea1a | 286 | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET) |
NYX | 0:85b3fd62ea1a | 287 | #else |
NYX | 0:85b3fd62ea1a | 288 | if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \ |
NYX | 0:85b3fd62ea1a | 289 | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET) |
NYX | 0:85b3fd62ea1a | 290 | #endif /* FLASH_SR_RDERR */ |
NYX | 0:85b3fd62ea1a | 291 | { |
NYX | 0:85b3fd62ea1a | 292 | if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE) |
NYX | 0:85b3fd62ea1a | 293 | { |
NYX | 0:85b3fd62ea1a | 294 | /*return the faulty sector*/ |
NYX | 0:85b3fd62ea1a | 295 | addresstmp = pFlash.Sector; |
NYX | 0:85b3fd62ea1a | 296 | pFlash.Sector = 0xFFFFFFFFU; |
NYX | 0:85b3fd62ea1a | 297 | } |
NYX | 0:85b3fd62ea1a | 298 | else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
NYX | 0:85b3fd62ea1a | 299 | { |
NYX | 0:85b3fd62ea1a | 300 | /*return the faulty bank*/ |
NYX | 0:85b3fd62ea1a | 301 | addresstmp = pFlash.Bank; |
NYX | 0:85b3fd62ea1a | 302 | } |
NYX | 0:85b3fd62ea1a | 303 | else |
NYX | 0:85b3fd62ea1a | 304 | { |
NYX | 0:85b3fd62ea1a | 305 | /*return the faulty address*/ |
NYX | 0:85b3fd62ea1a | 306 | addresstmp = pFlash.Address; |
NYX | 0:85b3fd62ea1a | 307 | } |
NYX | 0:85b3fd62ea1a | 308 | |
NYX | 0:85b3fd62ea1a | 309 | /*Save the Error code*/ |
NYX | 0:85b3fd62ea1a | 310 | FLASH_SetErrorCode(); |
NYX | 0:85b3fd62ea1a | 311 | |
NYX | 0:85b3fd62ea1a | 312 | /* FLASH error interrupt user callback */ |
NYX | 0:85b3fd62ea1a | 313 | HAL_FLASH_OperationErrorCallback(addresstmp); |
NYX | 0:85b3fd62ea1a | 314 | |
NYX | 0:85b3fd62ea1a | 315 | /*Stop the procedure ongoing*/ |
NYX | 0:85b3fd62ea1a | 316 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
NYX | 0:85b3fd62ea1a | 317 | } |
NYX | 0:85b3fd62ea1a | 318 | |
NYX | 0:85b3fd62ea1a | 319 | /* Check FLASH End of Operation flag */ |
NYX | 0:85b3fd62ea1a | 320 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET) |
NYX | 0:85b3fd62ea1a | 321 | { |
NYX | 0:85b3fd62ea1a | 322 | /* Clear FLASH End of Operation pending bit */ |
NYX | 0:85b3fd62ea1a | 323 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
NYX | 0:85b3fd62ea1a | 324 | |
NYX | 0:85b3fd62ea1a | 325 | if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE) |
NYX | 0:85b3fd62ea1a | 326 | { |
NYX | 0:85b3fd62ea1a | 327 | /*Nb of sector to erased can be decreased*/ |
NYX | 0:85b3fd62ea1a | 328 | pFlash.NbSectorsToErase--; |
NYX | 0:85b3fd62ea1a | 329 | |
NYX | 0:85b3fd62ea1a | 330 | /* Check if there are still sectors to erase*/ |
NYX | 0:85b3fd62ea1a | 331 | if(pFlash.NbSectorsToErase != 0U) |
NYX | 0:85b3fd62ea1a | 332 | { |
NYX | 0:85b3fd62ea1a | 333 | addresstmp = pFlash.Sector; |
NYX | 0:85b3fd62ea1a | 334 | /*Indicate user which sector has been erased*/ |
NYX | 0:85b3fd62ea1a | 335 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
NYX | 0:85b3fd62ea1a | 336 | |
NYX | 0:85b3fd62ea1a | 337 | /*Increment sector number*/ |
NYX | 0:85b3fd62ea1a | 338 | pFlash.Sector++; |
NYX | 0:85b3fd62ea1a | 339 | addresstmp = pFlash.Sector; |
NYX | 0:85b3fd62ea1a | 340 | FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase); |
NYX | 0:85b3fd62ea1a | 341 | } |
NYX | 0:85b3fd62ea1a | 342 | else |
NYX | 0:85b3fd62ea1a | 343 | { |
NYX | 0:85b3fd62ea1a | 344 | /*No more sectors to Erase, user callback can be called.*/ |
NYX | 0:85b3fd62ea1a | 345 | /*Reset Sector and stop Erase sectors procedure*/ |
NYX | 0:85b3fd62ea1a | 346 | pFlash.Sector = addresstmp = 0xFFFFFFFFU; |
NYX | 0:85b3fd62ea1a | 347 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
NYX | 0:85b3fd62ea1a | 348 | |
NYX | 0:85b3fd62ea1a | 349 | /* Flush the caches to be sure of the data consistency */ |
NYX | 0:85b3fd62ea1a | 350 | FLASH_FlushCaches() ; |
NYX | 0:85b3fd62ea1a | 351 | |
NYX | 0:85b3fd62ea1a | 352 | /* FLASH EOP interrupt user callback */ |
NYX | 0:85b3fd62ea1a | 353 | HAL_FLASH_EndOfOperationCallback(addresstmp); |
NYX | 0:85b3fd62ea1a | 354 | } |
NYX | 0:85b3fd62ea1a | 355 | } |
NYX | 0:85b3fd62ea1a | 356 | else |
NYX | 0:85b3fd62ea1a | 357 | { |
NYX | 0:85b3fd62ea1a | 358 | if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) |
NYX | 0:85b3fd62ea1a | 359 | { |
NYX | 0:85b3fd62ea1a | 360 | /* MassErase ended. Return the selected bank */ |
NYX | 0:85b3fd62ea1a | 361 | /* Flush the caches to be sure of the data consistency */ |
NYX | 0:85b3fd62ea1a | 362 | FLASH_FlushCaches() ; |
NYX | 0:85b3fd62ea1a | 363 | |
NYX | 0:85b3fd62ea1a | 364 | /* FLASH EOP interrupt user callback */ |
NYX | 0:85b3fd62ea1a | 365 | HAL_FLASH_EndOfOperationCallback(pFlash.Bank); |
NYX | 0:85b3fd62ea1a | 366 | } |
NYX | 0:85b3fd62ea1a | 367 | else |
NYX | 0:85b3fd62ea1a | 368 | { |
NYX | 0:85b3fd62ea1a | 369 | /*Program ended. Return the selected address*/ |
NYX | 0:85b3fd62ea1a | 370 | /* FLASH EOP interrupt user callback */ |
NYX | 0:85b3fd62ea1a | 371 | HAL_FLASH_EndOfOperationCallback(pFlash.Address); |
NYX | 0:85b3fd62ea1a | 372 | } |
NYX | 0:85b3fd62ea1a | 373 | pFlash.ProcedureOnGoing = FLASH_PROC_NONE; |
NYX | 0:85b3fd62ea1a | 374 | } |
NYX | 0:85b3fd62ea1a | 375 | } |
NYX | 0:85b3fd62ea1a | 376 | |
NYX | 0:85b3fd62ea1a | 377 | if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE) |
NYX | 0:85b3fd62ea1a | 378 | { |
NYX | 0:85b3fd62ea1a | 379 | /* Operation is completed, disable the PG, SER, SNB and MER Bits */ |
NYX | 0:85b3fd62ea1a | 380 | CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT)); |
NYX | 0:85b3fd62ea1a | 381 | |
NYX | 0:85b3fd62ea1a | 382 | /* Disable End of FLASH Operation interrupt */ |
NYX | 0:85b3fd62ea1a | 383 | __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP); |
NYX | 0:85b3fd62ea1a | 384 | |
NYX | 0:85b3fd62ea1a | 385 | /* Disable Error source interrupt */ |
NYX | 0:85b3fd62ea1a | 386 | __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR); |
NYX | 0:85b3fd62ea1a | 387 | |
NYX | 0:85b3fd62ea1a | 388 | /* Process Unlocked */ |
NYX | 0:85b3fd62ea1a | 389 | __HAL_UNLOCK(&pFlash); |
NYX | 0:85b3fd62ea1a | 390 | } |
NYX | 0:85b3fd62ea1a | 391 | } |
NYX | 0:85b3fd62ea1a | 392 | |
NYX | 0:85b3fd62ea1a | 393 | /** |
NYX | 0:85b3fd62ea1a | 394 | * @brief FLASH end of operation interrupt callback |
NYX | 0:85b3fd62ea1a | 395 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
NYX | 0:85b3fd62ea1a | 396 | * Mass Erase: Bank number which has been requested to erase |
NYX | 0:85b3fd62ea1a | 397 | * Sectors Erase: Sector which has been erased |
NYX | 0:85b3fd62ea1a | 398 | * (if 0xFFFFFFFFU, it means that all the selected sectors have been erased) |
NYX | 0:85b3fd62ea1a | 399 | * Program: Address which was selected for data program |
NYX | 0:85b3fd62ea1a | 400 | * @retval None |
NYX | 0:85b3fd62ea1a | 401 | */ |
NYX | 0:85b3fd62ea1a | 402 | __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) |
NYX | 0:85b3fd62ea1a | 403 | { |
NYX | 0:85b3fd62ea1a | 404 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 405 | UNUSED(ReturnValue); |
NYX | 0:85b3fd62ea1a | 406 | /* NOTE : This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 407 | the HAL_FLASH_EndOfOperationCallback could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 408 | */ |
NYX | 0:85b3fd62ea1a | 409 | } |
NYX | 0:85b3fd62ea1a | 410 | |
NYX | 0:85b3fd62ea1a | 411 | /** |
NYX | 0:85b3fd62ea1a | 412 | * @brief FLASH operation error interrupt callback |
NYX | 0:85b3fd62ea1a | 413 | * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure |
NYX | 0:85b3fd62ea1a | 414 | * Mass Erase: Bank number which has been requested to erase |
NYX | 0:85b3fd62ea1a | 415 | * Sectors Erase: Sector number which returned an error |
NYX | 0:85b3fd62ea1a | 416 | * Program: Address which was selected for data program |
NYX | 0:85b3fd62ea1a | 417 | * @retval None |
NYX | 0:85b3fd62ea1a | 418 | */ |
NYX | 0:85b3fd62ea1a | 419 | __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) |
NYX | 0:85b3fd62ea1a | 420 | { |
NYX | 0:85b3fd62ea1a | 421 | /* Prevent unused argument(s) compilation warning */ |
NYX | 0:85b3fd62ea1a | 422 | UNUSED(ReturnValue); |
NYX | 0:85b3fd62ea1a | 423 | /* NOTE : This function Should not be modified, when the callback is needed, |
NYX | 0:85b3fd62ea1a | 424 | the HAL_FLASH_OperationErrorCallback could be implemented in the user file |
NYX | 0:85b3fd62ea1a | 425 | */ |
NYX | 0:85b3fd62ea1a | 426 | } |
NYX | 0:85b3fd62ea1a | 427 | |
NYX | 0:85b3fd62ea1a | 428 | /** |
NYX | 0:85b3fd62ea1a | 429 | * @} |
NYX | 0:85b3fd62ea1a | 430 | */ |
NYX | 0:85b3fd62ea1a | 431 | |
NYX | 0:85b3fd62ea1a | 432 | /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions |
NYX | 0:85b3fd62ea1a | 433 | * @brief management functions |
NYX | 0:85b3fd62ea1a | 434 | * |
NYX | 0:85b3fd62ea1a | 435 | @verbatim |
NYX | 0:85b3fd62ea1a | 436 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 437 | ##### Peripheral Control functions ##### |
NYX | 0:85b3fd62ea1a | 438 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 439 | [..] |
NYX | 0:85b3fd62ea1a | 440 | This subsection provides a set of functions allowing to control the FLASH |
NYX | 0:85b3fd62ea1a | 441 | memory operations. |
NYX | 0:85b3fd62ea1a | 442 | |
NYX | 0:85b3fd62ea1a | 443 | @endverbatim |
NYX | 0:85b3fd62ea1a | 444 | * @{ |
NYX | 0:85b3fd62ea1a | 445 | */ |
NYX | 0:85b3fd62ea1a | 446 | |
NYX | 0:85b3fd62ea1a | 447 | /** |
NYX | 0:85b3fd62ea1a | 448 | * @brief Unlock the FLASH control register access |
NYX | 0:85b3fd62ea1a | 449 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 450 | */ |
NYX | 0:85b3fd62ea1a | 451 | HAL_StatusTypeDef HAL_FLASH_Unlock(void) |
NYX | 0:85b3fd62ea1a | 452 | { |
NYX | 0:85b3fd62ea1a | 453 | if((FLASH->CR & FLASH_CR_LOCK) != RESET) |
NYX | 0:85b3fd62ea1a | 454 | { |
NYX | 0:85b3fd62ea1a | 455 | /* Authorize the FLASH Registers access */ |
NYX | 0:85b3fd62ea1a | 456 | FLASH->KEYR = FLASH_KEY1; |
NYX | 0:85b3fd62ea1a | 457 | FLASH->KEYR = FLASH_KEY2; |
NYX | 0:85b3fd62ea1a | 458 | } |
NYX | 0:85b3fd62ea1a | 459 | else |
NYX | 0:85b3fd62ea1a | 460 | { |
NYX | 0:85b3fd62ea1a | 461 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 462 | } |
NYX | 0:85b3fd62ea1a | 463 | |
NYX | 0:85b3fd62ea1a | 464 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 465 | } |
NYX | 0:85b3fd62ea1a | 466 | |
NYX | 0:85b3fd62ea1a | 467 | /** |
NYX | 0:85b3fd62ea1a | 468 | * @brief Locks the FLASH control register access |
NYX | 0:85b3fd62ea1a | 469 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 470 | */ |
NYX | 0:85b3fd62ea1a | 471 | HAL_StatusTypeDef HAL_FLASH_Lock(void) |
NYX | 0:85b3fd62ea1a | 472 | { |
NYX | 0:85b3fd62ea1a | 473 | /* Set the LOCK Bit to lock the FLASH Registers access */ |
NYX | 0:85b3fd62ea1a | 474 | FLASH->CR |= FLASH_CR_LOCK; |
NYX | 0:85b3fd62ea1a | 475 | |
NYX | 0:85b3fd62ea1a | 476 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 477 | } |
NYX | 0:85b3fd62ea1a | 478 | |
NYX | 0:85b3fd62ea1a | 479 | /** |
NYX | 0:85b3fd62ea1a | 480 | * @brief Unlock the FLASH Option Control Registers access. |
NYX | 0:85b3fd62ea1a | 481 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 482 | */ |
NYX | 0:85b3fd62ea1a | 483 | HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void) |
NYX | 0:85b3fd62ea1a | 484 | { |
NYX | 0:85b3fd62ea1a | 485 | if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET) |
NYX | 0:85b3fd62ea1a | 486 | { |
NYX | 0:85b3fd62ea1a | 487 | /* Authorizes the Option Byte register programming */ |
NYX | 0:85b3fd62ea1a | 488 | FLASH->OPTKEYR = FLASH_OPT_KEY1; |
NYX | 0:85b3fd62ea1a | 489 | FLASH->OPTKEYR = FLASH_OPT_KEY2; |
NYX | 0:85b3fd62ea1a | 490 | } |
NYX | 0:85b3fd62ea1a | 491 | else |
NYX | 0:85b3fd62ea1a | 492 | { |
NYX | 0:85b3fd62ea1a | 493 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 494 | } |
NYX | 0:85b3fd62ea1a | 495 | |
NYX | 0:85b3fd62ea1a | 496 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 497 | } |
NYX | 0:85b3fd62ea1a | 498 | |
NYX | 0:85b3fd62ea1a | 499 | /** |
NYX | 0:85b3fd62ea1a | 500 | * @brief Lock the FLASH Option Control Registers access. |
NYX | 0:85b3fd62ea1a | 501 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 502 | */ |
NYX | 0:85b3fd62ea1a | 503 | HAL_StatusTypeDef HAL_FLASH_OB_Lock(void) |
NYX | 0:85b3fd62ea1a | 504 | { |
NYX | 0:85b3fd62ea1a | 505 | /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */ |
NYX | 0:85b3fd62ea1a | 506 | FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK; |
NYX | 0:85b3fd62ea1a | 507 | |
NYX | 0:85b3fd62ea1a | 508 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 509 | } |
NYX | 0:85b3fd62ea1a | 510 | |
NYX | 0:85b3fd62ea1a | 511 | /** |
NYX | 0:85b3fd62ea1a | 512 | * @brief Launch the option byte loading. |
NYX | 0:85b3fd62ea1a | 513 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 514 | */ |
NYX | 0:85b3fd62ea1a | 515 | HAL_StatusTypeDef HAL_FLASH_OB_Launch(void) |
NYX | 0:85b3fd62ea1a | 516 | { |
NYX | 0:85b3fd62ea1a | 517 | /* Set the OPTSTRT bit in OPTCR register */ |
NYX | 0:85b3fd62ea1a | 518 | *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT; |
NYX | 0:85b3fd62ea1a | 519 | |
NYX | 0:85b3fd62ea1a | 520 | /* Wait for last operation to be completed */ |
NYX | 0:85b3fd62ea1a | 521 | return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)); |
NYX | 0:85b3fd62ea1a | 522 | } |
NYX | 0:85b3fd62ea1a | 523 | |
NYX | 0:85b3fd62ea1a | 524 | /** |
NYX | 0:85b3fd62ea1a | 525 | * @} |
NYX | 0:85b3fd62ea1a | 526 | */ |
NYX | 0:85b3fd62ea1a | 527 | |
NYX | 0:85b3fd62ea1a | 528 | /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions |
NYX | 0:85b3fd62ea1a | 529 | * @brief Peripheral Errors functions |
NYX | 0:85b3fd62ea1a | 530 | * |
NYX | 0:85b3fd62ea1a | 531 | @verbatim |
NYX | 0:85b3fd62ea1a | 532 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 533 | ##### Peripheral Errors functions ##### |
NYX | 0:85b3fd62ea1a | 534 | =============================================================================== |
NYX | 0:85b3fd62ea1a | 535 | [..] |
NYX | 0:85b3fd62ea1a | 536 | This subsection permits to get in run-time Errors of the FLASH peripheral. |
NYX | 0:85b3fd62ea1a | 537 | |
NYX | 0:85b3fd62ea1a | 538 | @endverbatim |
NYX | 0:85b3fd62ea1a | 539 | * @{ |
NYX | 0:85b3fd62ea1a | 540 | */ |
NYX | 0:85b3fd62ea1a | 541 | |
NYX | 0:85b3fd62ea1a | 542 | /** |
NYX | 0:85b3fd62ea1a | 543 | * @brief Get the specific FLASH error flag. |
NYX | 0:85b3fd62ea1a | 544 | * @retval FLASH_ErrorCode: The returned value can be a combination of: |
NYX | 0:85b3fd62ea1a | 545 | * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP) |
NYX | 0:85b3fd62ea1a | 546 | * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag |
NYX | 0:85b3fd62ea1a | 547 | * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag |
NYX | 0:85b3fd62ea1a | 548 | * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag |
NYX | 0:85b3fd62ea1a | 549 | * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag |
NYX | 0:85b3fd62ea1a | 550 | * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag |
NYX | 0:85b3fd62ea1a | 551 | */ |
NYX | 0:85b3fd62ea1a | 552 | uint32_t HAL_FLASH_GetError(void) |
NYX | 0:85b3fd62ea1a | 553 | { |
NYX | 0:85b3fd62ea1a | 554 | return pFlash.ErrorCode; |
NYX | 0:85b3fd62ea1a | 555 | } |
NYX | 0:85b3fd62ea1a | 556 | |
NYX | 0:85b3fd62ea1a | 557 | /** |
NYX | 0:85b3fd62ea1a | 558 | * @} |
NYX | 0:85b3fd62ea1a | 559 | */ |
NYX | 0:85b3fd62ea1a | 560 | |
NYX | 0:85b3fd62ea1a | 561 | /** |
NYX | 0:85b3fd62ea1a | 562 | * @brief Wait for a FLASH operation to complete. |
NYX | 0:85b3fd62ea1a | 563 | * @param Timeout: maximum flash operationtimeout |
NYX | 0:85b3fd62ea1a | 564 | * @retval HAL Status |
NYX | 0:85b3fd62ea1a | 565 | */ |
NYX | 0:85b3fd62ea1a | 566 | HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout) |
NYX | 0:85b3fd62ea1a | 567 | { |
NYX | 0:85b3fd62ea1a | 568 | uint32_t tickstart = 0U; |
NYX | 0:85b3fd62ea1a | 569 | |
NYX | 0:85b3fd62ea1a | 570 | /* Clear Error Code */ |
NYX | 0:85b3fd62ea1a | 571 | pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; |
NYX | 0:85b3fd62ea1a | 572 | |
NYX | 0:85b3fd62ea1a | 573 | /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. |
NYX | 0:85b3fd62ea1a | 574 | Even if the FLASH operation fails, the BUSY flag will be reset and an error |
NYX | 0:85b3fd62ea1a | 575 | flag will be set */ |
NYX | 0:85b3fd62ea1a | 576 | /* Get tick */ |
NYX | 0:85b3fd62ea1a | 577 | tickstart = HAL_GetTick(); |
NYX | 0:85b3fd62ea1a | 578 | |
NYX | 0:85b3fd62ea1a | 579 | while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) |
NYX | 0:85b3fd62ea1a | 580 | { |
NYX | 0:85b3fd62ea1a | 581 | if(Timeout != HAL_MAX_DELAY) |
NYX | 0:85b3fd62ea1a | 582 | { |
NYX | 0:85b3fd62ea1a | 583 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
NYX | 0:85b3fd62ea1a | 584 | { |
NYX | 0:85b3fd62ea1a | 585 | return HAL_TIMEOUT; |
NYX | 0:85b3fd62ea1a | 586 | } |
NYX | 0:85b3fd62ea1a | 587 | } |
NYX | 0:85b3fd62ea1a | 588 | } |
NYX | 0:85b3fd62ea1a | 589 | |
NYX | 0:85b3fd62ea1a | 590 | /* Check FLASH End of Operation flag */ |
NYX | 0:85b3fd62ea1a | 591 | if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET) |
NYX | 0:85b3fd62ea1a | 592 | { |
NYX | 0:85b3fd62ea1a | 593 | /* Clear FLASH End of Operation pending bit */ |
NYX | 0:85b3fd62ea1a | 594 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); |
NYX | 0:85b3fd62ea1a | 595 | } |
NYX | 0:85b3fd62ea1a | 596 | #if defined(FLASH_SR_RDERR) |
NYX | 0:85b3fd62ea1a | 597 | if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \ |
NYX | 0:85b3fd62ea1a | 598 | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET) |
NYX | 0:85b3fd62ea1a | 599 | #else |
NYX | 0:85b3fd62ea1a | 600 | if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \ |
NYX | 0:85b3fd62ea1a | 601 | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET) |
NYX | 0:85b3fd62ea1a | 602 | #endif /* FLASH_SR_RDERR */ |
NYX | 0:85b3fd62ea1a | 603 | { |
NYX | 0:85b3fd62ea1a | 604 | /*Save the error code*/ |
NYX | 0:85b3fd62ea1a | 605 | FLASH_SetErrorCode(); |
NYX | 0:85b3fd62ea1a | 606 | return HAL_ERROR; |
NYX | 0:85b3fd62ea1a | 607 | } |
NYX | 0:85b3fd62ea1a | 608 | |
NYX | 0:85b3fd62ea1a | 609 | /* If there is no error flag set */ |
NYX | 0:85b3fd62ea1a | 610 | return HAL_OK; |
NYX | 0:85b3fd62ea1a | 611 | |
NYX | 0:85b3fd62ea1a | 612 | } |
NYX | 0:85b3fd62ea1a | 613 | |
NYX | 0:85b3fd62ea1a | 614 | /** |
NYX | 0:85b3fd62ea1a | 615 | * @brief Program a double word (64-bit) at a specified address. |
NYX | 0:85b3fd62ea1a | 616 | * @note This function must be used when the device voltage range is from |
NYX | 0:85b3fd62ea1a | 617 | * 2.7V to 3.6V and Vpp in the range 7V to 9V. |
NYX | 0:85b3fd62ea1a | 618 | * |
NYX | 0:85b3fd62ea1a | 619 | * @note If an erase and a program operations are requested simultaneously, |
NYX | 0:85b3fd62ea1a | 620 | * the erase operation is performed before the program one. |
NYX | 0:85b3fd62ea1a | 621 | * |
NYX | 0:85b3fd62ea1a | 622 | * @param Address: specifies the address to be programmed. |
NYX | 0:85b3fd62ea1a | 623 | * @param Data: specifies the data to be programmed. |
NYX | 0:85b3fd62ea1a | 624 | * @retval None |
NYX | 0:85b3fd62ea1a | 625 | */ |
NYX | 0:85b3fd62ea1a | 626 | static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data) |
NYX | 0:85b3fd62ea1a | 627 | { |
NYX | 0:85b3fd62ea1a | 628 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 629 | assert_param(IS_FLASH_ADDRESS(Address)); |
NYX | 0:85b3fd62ea1a | 630 | |
NYX | 0:85b3fd62ea1a | 631 | /* If the previous operation is completed, proceed to program the new data */ |
NYX | 0:85b3fd62ea1a | 632 | CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE); |
NYX | 0:85b3fd62ea1a | 633 | FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD; |
NYX | 0:85b3fd62ea1a | 634 | FLASH->CR |= FLASH_CR_PG; |
NYX | 0:85b3fd62ea1a | 635 | |
NYX | 0:85b3fd62ea1a | 636 | /* Program the double-word */ |
NYX | 0:85b3fd62ea1a | 637 | *(__IO uint32_t*)Address = (uint32_t)Data; |
NYX | 0:85b3fd62ea1a | 638 | *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32); |
NYX | 0:85b3fd62ea1a | 639 | } |
NYX | 0:85b3fd62ea1a | 640 | |
NYX | 0:85b3fd62ea1a | 641 | |
NYX | 0:85b3fd62ea1a | 642 | /** |
NYX | 0:85b3fd62ea1a | 643 | * @brief Program word (32-bit) at a specified address. |
NYX | 0:85b3fd62ea1a | 644 | * @note This function must be used when the device voltage range is from |
NYX | 0:85b3fd62ea1a | 645 | * 2.7V to 3.6V. |
NYX | 0:85b3fd62ea1a | 646 | * |
NYX | 0:85b3fd62ea1a | 647 | * @note If an erase and a program operations are requested simultaneously, |
NYX | 0:85b3fd62ea1a | 648 | * the erase operation is performed before the program one. |
NYX | 0:85b3fd62ea1a | 649 | * |
NYX | 0:85b3fd62ea1a | 650 | * @param Address: specifies the address to be programmed. |
NYX | 0:85b3fd62ea1a | 651 | * @param Data: specifies the data to be programmed. |
NYX | 0:85b3fd62ea1a | 652 | * @retval None |
NYX | 0:85b3fd62ea1a | 653 | */ |
NYX | 0:85b3fd62ea1a | 654 | static void FLASH_Program_Word(uint32_t Address, uint32_t Data) |
NYX | 0:85b3fd62ea1a | 655 | { |
NYX | 0:85b3fd62ea1a | 656 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 657 | assert_param(IS_FLASH_ADDRESS(Address)); |
NYX | 0:85b3fd62ea1a | 658 | |
NYX | 0:85b3fd62ea1a | 659 | /* If the previous operation is completed, proceed to program the new data */ |
NYX | 0:85b3fd62ea1a | 660 | CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE); |
NYX | 0:85b3fd62ea1a | 661 | FLASH->CR |= FLASH_PSIZE_WORD; |
NYX | 0:85b3fd62ea1a | 662 | FLASH->CR |= FLASH_CR_PG; |
NYX | 0:85b3fd62ea1a | 663 | |
NYX | 0:85b3fd62ea1a | 664 | *(__IO uint32_t*)Address = Data; |
NYX | 0:85b3fd62ea1a | 665 | } |
NYX | 0:85b3fd62ea1a | 666 | |
NYX | 0:85b3fd62ea1a | 667 | /** |
NYX | 0:85b3fd62ea1a | 668 | * @brief Program a half-word (16-bit) at a specified address. |
NYX | 0:85b3fd62ea1a | 669 | * @note This function must be used when the device voltage range is from |
NYX | 0:85b3fd62ea1a | 670 | * 2.1V to 3.6V. |
NYX | 0:85b3fd62ea1a | 671 | * |
NYX | 0:85b3fd62ea1a | 672 | * @note If an erase and a program operations are requested simultaneously, |
NYX | 0:85b3fd62ea1a | 673 | * the erase operation is performed before the program one. |
NYX | 0:85b3fd62ea1a | 674 | * |
NYX | 0:85b3fd62ea1a | 675 | * @param Address: specifies the address to be programmed. |
NYX | 0:85b3fd62ea1a | 676 | * @param Data: specifies the data to be programmed. |
NYX | 0:85b3fd62ea1a | 677 | * @retval None |
NYX | 0:85b3fd62ea1a | 678 | */ |
NYX | 0:85b3fd62ea1a | 679 | static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data) |
NYX | 0:85b3fd62ea1a | 680 | { |
NYX | 0:85b3fd62ea1a | 681 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 682 | assert_param(IS_FLASH_ADDRESS(Address)); |
NYX | 0:85b3fd62ea1a | 683 | |
NYX | 0:85b3fd62ea1a | 684 | /* If the previous operation is completed, proceed to program the new data */ |
NYX | 0:85b3fd62ea1a | 685 | CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE); |
NYX | 0:85b3fd62ea1a | 686 | FLASH->CR |= FLASH_PSIZE_HALF_WORD; |
NYX | 0:85b3fd62ea1a | 687 | FLASH->CR |= FLASH_CR_PG; |
NYX | 0:85b3fd62ea1a | 688 | |
NYX | 0:85b3fd62ea1a | 689 | *(__IO uint16_t*)Address = Data; |
NYX | 0:85b3fd62ea1a | 690 | } |
NYX | 0:85b3fd62ea1a | 691 | |
NYX | 0:85b3fd62ea1a | 692 | /** |
NYX | 0:85b3fd62ea1a | 693 | * @brief Program byte (8-bit) at a specified address. |
NYX | 0:85b3fd62ea1a | 694 | * @note This function must be used when the device voltage range is from |
NYX | 0:85b3fd62ea1a | 695 | * 1.8V to 3.6V. |
NYX | 0:85b3fd62ea1a | 696 | * |
NYX | 0:85b3fd62ea1a | 697 | * @note If an erase and a program operations are requested simultaneously, |
NYX | 0:85b3fd62ea1a | 698 | * the erase operation is performed before the program one. |
NYX | 0:85b3fd62ea1a | 699 | * |
NYX | 0:85b3fd62ea1a | 700 | * @param Address: specifies the address to be programmed. |
NYX | 0:85b3fd62ea1a | 701 | * @param Data: specifies the data to be programmed. |
NYX | 0:85b3fd62ea1a | 702 | * @retval None |
NYX | 0:85b3fd62ea1a | 703 | */ |
NYX | 0:85b3fd62ea1a | 704 | static void FLASH_Program_Byte(uint32_t Address, uint8_t Data) |
NYX | 0:85b3fd62ea1a | 705 | { |
NYX | 0:85b3fd62ea1a | 706 | /* Check the parameters */ |
NYX | 0:85b3fd62ea1a | 707 | assert_param(IS_FLASH_ADDRESS(Address)); |
NYX | 0:85b3fd62ea1a | 708 | |
NYX | 0:85b3fd62ea1a | 709 | /* If the previous operation is completed, proceed to program the new data */ |
NYX | 0:85b3fd62ea1a | 710 | CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE); |
NYX | 0:85b3fd62ea1a | 711 | FLASH->CR |= FLASH_PSIZE_BYTE; |
NYX | 0:85b3fd62ea1a | 712 | FLASH->CR |= FLASH_CR_PG; |
NYX | 0:85b3fd62ea1a | 713 | |
NYX | 0:85b3fd62ea1a | 714 | *(__IO uint8_t*)Address = Data; |
NYX | 0:85b3fd62ea1a | 715 | } |
NYX | 0:85b3fd62ea1a | 716 | |
NYX | 0:85b3fd62ea1a | 717 | /** |
NYX | 0:85b3fd62ea1a | 718 | * @brief Set the specific FLASH error flag. |
NYX | 0:85b3fd62ea1a | 719 | * @retval None |
NYX | 0:85b3fd62ea1a | 720 | */ |
NYX | 0:85b3fd62ea1a | 721 | static void FLASH_SetErrorCode(void) |
NYX | 0:85b3fd62ea1a | 722 | { |
NYX | 0:85b3fd62ea1a | 723 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) |
NYX | 0:85b3fd62ea1a | 724 | { |
NYX | 0:85b3fd62ea1a | 725 | pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; |
NYX | 0:85b3fd62ea1a | 726 | |
NYX | 0:85b3fd62ea1a | 727 | /* Clear FLASH write protection error pending bit */ |
NYX | 0:85b3fd62ea1a | 728 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR); |
NYX | 0:85b3fd62ea1a | 729 | } |
NYX | 0:85b3fd62ea1a | 730 | |
NYX | 0:85b3fd62ea1a | 731 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) |
NYX | 0:85b3fd62ea1a | 732 | { |
NYX | 0:85b3fd62ea1a | 733 | pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA; |
NYX | 0:85b3fd62ea1a | 734 | |
NYX | 0:85b3fd62ea1a | 735 | /* Clear FLASH Programming alignment error pending bit */ |
NYX | 0:85b3fd62ea1a | 736 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR); |
NYX | 0:85b3fd62ea1a | 737 | } |
NYX | 0:85b3fd62ea1a | 738 | |
NYX | 0:85b3fd62ea1a | 739 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET) |
NYX | 0:85b3fd62ea1a | 740 | { |
NYX | 0:85b3fd62ea1a | 741 | pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP; |
NYX | 0:85b3fd62ea1a | 742 | |
NYX | 0:85b3fd62ea1a | 743 | /* Clear FLASH Programming parallelism error pending bit */ |
NYX | 0:85b3fd62ea1a | 744 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR); |
NYX | 0:85b3fd62ea1a | 745 | } |
NYX | 0:85b3fd62ea1a | 746 | |
NYX | 0:85b3fd62ea1a | 747 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET) |
NYX | 0:85b3fd62ea1a | 748 | { |
NYX | 0:85b3fd62ea1a | 749 | pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS; |
NYX | 0:85b3fd62ea1a | 750 | |
NYX | 0:85b3fd62ea1a | 751 | /* Clear FLASH Programming sequence error pending bit */ |
NYX | 0:85b3fd62ea1a | 752 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR); |
NYX | 0:85b3fd62ea1a | 753 | } |
NYX | 0:85b3fd62ea1a | 754 | #if defined(FLASH_SR_RDERR) |
NYX | 0:85b3fd62ea1a | 755 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) |
NYX | 0:85b3fd62ea1a | 756 | { |
NYX | 0:85b3fd62ea1a | 757 | pFlash.ErrorCode |= HAL_FLASH_ERROR_RD; |
NYX | 0:85b3fd62ea1a | 758 | |
NYX | 0:85b3fd62ea1a | 759 | /* Clear FLASH Proprietary readout protection error pending bit */ |
NYX | 0:85b3fd62ea1a | 760 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR); |
NYX | 0:85b3fd62ea1a | 761 | } |
NYX | 0:85b3fd62ea1a | 762 | #endif /* FLASH_SR_RDERR */ |
NYX | 0:85b3fd62ea1a | 763 | if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET) |
NYX | 0:85b3fd62ea1a | 764 | { |
NYX | 0:85b3fd62ea1a | 765 | pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION; |
NYX | 0:85b3fd62ea1a | 766 | |
NYX | 0:85b3fd62ea1a | 767 | /* Clear FLASH Operation error pending bit */ |
NYX | 0:85b3fd62ea1a | 768 | __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR); |
NYX | 0:85b3fd62ea1a | 769 | } |
NYX | 0:85b3fd62ea1a | 770 | } |
NYX | 0:85b3fd62ea1a | 771 | |
NYX | 0:85b3fd62ea1a | 772 | /** |
NYX | 0:85b3fd62ea1a | 773 | * @} |
NYX | 0:85b3fd62ea1a | 774 | */ |
NYX | 0:85b3fd62ea1a | 775 | |
NYX | 0:85b3fd62ea1a | 776 | #endif /* HAL_FLASH_MODULE_ENABLED */ |
NYX | 0:85b3fd62ea1a | 777 | |
NYX | 0:85b3fd62ea1a | 778 | /** |
NYX | 0:85b3fd62ea1a | 779 | * @} |
NYX | 0:85b3fd62ea1a | 780 | */ |
NYX | 0:85b3fd62ea1a | 781 | |
NYX | 0:85b3fd62ea1a | 782 | /** |
NYX | 0:85b3fd62ea1a | 783 | * @} |
NYX | 0:85b3fd62ea1a | 784 | */ |
NYX | 0:85b3fd62ea1a | 785 | |
NYX | 0:85b3fd62ea1a | 786 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |