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