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