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