test
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32F4/flash_api.c@178:d650f5d4c87a, 2017-11-08 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Nov 08 13:50:44 2017 +0000
- Revision:
- 178:d650f5d4c87a
- Parent:
- 176:af195413fb11
This updates the lib to the mbed lib v 155
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 172:89b338f31ef1 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 172:89b338f31ef1 | 2 | ******************************************************************************* |
AnnaBridge | 172:89b338f31ef1 | 3 | * Copyright (c) 2017, STMicroelectronics |
AnnaBridge | 172:89b338f31ef1 | 4 | * All rights reserved. |
AnnaBridge | 172:89b338f31ef1 | 5 | * |
AnnaBridge | 172:89b338f31ef1 | 6 | * Redistribution and use in source and binary forms, with or without |
AnnaBridge | 172:89b338f31ef1 | 7 | * modification, are permitted provided that the following conditions are met: |
AnnaBridge | 172:89b338f31ef1 | 8 | * |
AnnaBridge | 172:89b338f31ef1 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
AnnaBridge | 172:89b338f31ef1 | 10 | * this list of conditions and the following disclaimer. |
AnnaBridge | 172:89b338f31ef1 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
AnnaBridge | 172:89b338f31ef1 | 12 | * this list of conditions and the following disclaimer in the documentation |
AnnaBridge | 172:89b338f31ef1 | 13 | * and/or other materials provided with the distribution. |
AnnaBridge | 172:89b338f31ef1 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
AnnaBridge | 172:89b338f31ef1 | 15 | * may be used to endorse or promote products derived from this software |
AnnaBridge | 172:89b338f31ef1 | 16 | * without specific prior written permission. |
AnnaBridge | 172:89b338f31ef1 | 17 | * |
AnnaBridge | 172:89b338f31ef1 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
AnnaBridge | 172:89b338f31ef1 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
AnnaBridge | 172:89b338f31ef1 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
AnnaBridge | 172:89b338f31ef1 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
AnnaBridge | 172:89b338f31ef1 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
AnnaBridge | 172:89b338f31ef1 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
AnnaBridge | 172:89b338f31ef1 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
AnnaBridge | 172:89b338f31ef1 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
AnnaBridge | 172:89b338f31ef1 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
AnnaBridge | 172:89b338f31ef1 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
AnnaBridge | 172:89b338f31ef1 | 28 | ******************************************************************************* |
AnnaBridge | 172:89b338f31ef1 | 29 | */ |
AnnaBridge | 172:89b338f31ef1 | 30 | |
AnnaBridge | 172:89b338f31ef1 | 31 | #if DEVICE_FLASH |
AnnaBridge | 172:89b338f31ef1 | 32 | #include "flash_api.h" |
AnnaBridge | 172:89b338f31ef1 | 33 | #include "flash_data.h" |
AnnaBridge | 172:89b338f31ef1 | 34 | #include "platform/mbed_critical.h" |
AnnaBridge | 172:89b338f31ef1 | 35 | |
AnnaBridge | 172:89b338f31ef1 | 36 | // This file is automatically generated |
AnnaBridge | 172:89b338f31ef1 | 37 | |
AnnaBridge | 172:89b338f31ef1 | 38 | |
AnnaBridge | 172:89b338f31ef1 | 39 | static uint32_t GetSector(uint32_t Address); |
AnnaBridge | 172:89b338f31ef1 | 40 | static uint32_t GetSectorSize(uint32_t Sector); |
AnnaBridge | 172:89b338f31ef1 | 41 | |
AnnaBridge | 172:89b338f31ef1 | 42 | int32_t flash_init(flash_t *obj) |
AnnaBridge | 172:89b338f31ef1 | 43 | { |
AnnaBridge | 176:af195413fb11 | 44 | return 0; |
AnnaBridge | 176:af195413fb11 | 45 | } |
AnnaBridge | 176:af195413fb11 | 46 | |
AnnaBridge | 176:af195413fb11 | 47 | int32_t flash_free(flash_t *obj) |
AnnaBridge | 176:af195413fb11 | 48 | { |
AnnaBridge | 176:af195413fb11 | 49 | return 0; |
AnnaBridge | 176:af195413fb11 | 50 | } |
AnnaBridge | 176:af195413fb11 | 51 | |
AnnaBridge | 176:af195413fb11 | 52 | static int32_t flash_unlock(void) |
AnnaBridge | 176:af195413fb11 | 53 | { |
AnnaBridge | 172:89b338f31ef1 | 54 | /* Allow Access to Flash control registers and user Falsh */ |
AnnaBridge | 172:89b338f31ef1 | 55 | if (HAL_FLASH_Unlock()) { |
AnnaBridge | 172:89b338f31ef1 | 56 | return -1; |
AnnaBridge | 172:89b338f31ef1 | 57 | } else { |
AnnaBridge | 172:89b338f31ef1 | 58 | return 0; |
AnnaBridge | 172:89b338f31ef1 | 59 | } |
AnnaBridge | 172:89b338f31ef1 | 60 | } |
AnnaBridge | 176:af195413fb11 | 61 | |
AnnaBridge | 176:af195413fb11 | 62 | static int32_t flash_lock(void) |
AnnaBridge | 172:89b338f31ef1 | 63 | { |
AnnaBridge | 176:af195413fb11 | 64 | /* Disable the Flash option control register access (recommended to protect |
AnnaBridge | 172:89b338f31ef1 | 65 | the option Bytes against possible unwanted operations) */ |
AnnaBridge | 172:89b338f31ef1 | 66 | if (HAL_FLASH_Lock()) { |
AnnaBridge | 172:89b338f31ef1 | 67 | return -1; |
AnnaBridge | 172:89b338f31ef1 | 68 | } else { |
AnnaBridge | 172:89b338f31ef1 | 69 | return 0; |
AnnaBridge | 172:89b338f31ef1 | 70 | } |
AnnaBridge | 172:89b338f31ef1 | 71 | } |
AnnaBridge | 176:af195413fb11 | 72 | |
AnnaBridge | 172:89b338f31ef1 | 73 | int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
AnnaBridge | 172:89b338f31ef1 | 74 | { |
AnnaBridge | 172:89b338f31ef1 | 75 | /*Variable used for Erase procedure*/ |
AnnaBridge | 172:89b338f31ef1 | 76 | static FLASH_EraseInitTypeDef EraseInitStruct; |
AnnaBridge | 172:89b338f31ef1 | 77 | uint32_t FirstSector; |
AnnaBridge | 172:89b338f31ef1 | 78 | uint32_t SectorError = 0; |
AnnaBridge | 176:af195413fb11 | 79 | int32_t status = 0; |
AnnaBridge | 176:af195413fb11 | 80 | |
AnnaBridge | 172:89b338f31ef1 | 81 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 172:89b338f31ef1 | 82 | return -1; |
AnnaBridge | 172:89b338f31ef1 | 83 | } |
AnnaBridge | 176:af195413fb11 | 84 | |
AnnaBridge | 176:af195413fb11 | 85 | if (flash_unlock() != HAL_OK) { |
AnnaBridge | 176:af195413fb11 | 86 | return -1; |
AnnaBridge | 176:af195413fb11 | 87 | } |
AnnaBridge | 176:af195413fb11 | 88 | |
AnnaBridge | 172:89b338f31ef1 | 89 | /* Get the 1st sector to erase */ |
AnnaBridge | 172:89b338f31ef1 | 90 | FirstSector = GetSector(address); |
AnnaBridge | 172:89b338f31ef1 | 91 | |
AnnaBridge | 172:89b338f31ef1 | 92 | /* Fill EraseInit structure*/ |
AnnaBridge | 172:89b338f31ef1 | 93 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; |
AnnaBridge | 172:89b338f31ef1 | 94 | EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; |
AnnaBridge | 172:89b338f31ef1 | 95 | EraseInitStruct.Sector = FirstSector; |
AnnaBridge | 172:89b338f31ef1 | 96 | EraseInitStruct.NbSectors = 1; |
AnnaBridge | 172:89b338f31ef1 | 97 | if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){ |
AnnaBridge | 176:af195413fb11 | 98 | status = -1; |
AnnaBridge | 172:89b338f31ef1 | 99 | } |
AnnaBridge | 176:af195413fb11 | 100 | |
AnnaBridge | 176:af195413fb11 | 101 | flash_lock(); |
AnnaBridge | 176:af195413fb11 | 102 | |
AnnaBridge | 176:af195413fb11 | 103 | return status; |
AnnaBridge | 172:89b338f31ef1 | 104 | } |
AnnaBridge | 172:89b338f31ef1 | 105 | |
AnnaBridge | 172:89b338f31ef1 | 106 | int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) |
AnnaBridge | 172:89b338f31ef1 | 107 | { |
AnnaBridge | 176:af195413fb11 | 108 | int32_t status = 0; |
AnnaBridge | 172:89b338f31ef1 | 109 | |
AnnaBridge | 172:89b338f31ef1 | 110 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 172:89b338f31ef1 | 111 | return -1; |
AnnaBridge | 172:89b338f31ef1 | 112 | } |
AnnaBridge | 172:89b338f31ef1 | 113 | |
AnnaBridge | 176:af195413fb11 | 114 | if (flash_unlock() != HAL_OK) { |
AnnaBridge | 176:af195413fb11 | 115 | return -1; |
AnnaBridge | 176:af195413fb11 | 116 | } |
AnnaBridge | 176:af195413fb11 | 117 | |
AnnaBridge | 172:89b338f31ef1 | 118 | /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
AnnaBridge | 172:89b338f31ef1 | 119 | you have to make sure that these data are rewritten before they are accessed during code |
AnnaBridge | 172:89b338f31ef1 | 120 | execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
AnnaBridge | 172:89b338f31ef1 | 121 | DCRST and ICRST bits in the FLASH_CR register. */ |
AnnaBridge | 172:89b338f31ef1 | 122 | __HAL_FLASH_DATA_CACHE_DISABLE(); |
AnnaBridge | 172:89b338f31ef1 | 123 | __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); |
AnnaBridge | 172:89b338f31ef1 | 124 | |
AnnaBridge | 172:89b338f31ef1 | 125 | __HAL_FLASH_DATA_CACHE_RESET(); |
AnnaBridge | 172:89b338f31ef1 | 126 | __HAL_FLASH_INSTRUCTION_CACHE_RESET(); |
AnnaBridge | 172:89b338f31ef1 | 127 | |
AnnaBridge | 172:89b338f31ef1 | 128 | __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); |
AnnaBridge | 172:89b338f31ef1 | 129 | __HAL_FLASH_DATA_CACHE_ENABLE(); |
AnnaBridge | 172:89b338f31ef1 | 130 | |
AnnaBridge | 176:af195413fb11 | 131 | while ((size > 0) && (status == 0)) { |
AnnaBridge | 172:89b338f31ef1 | 132 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, (uint64_t)*data) != HAL_OK) { |
AnnaBridge | 176:af195413fb11 | 133 | status = -1; |
AnnaBridge | 172:89b338f31ef1 | 134 | } else { |
AnnaBridge | 172:89b338f31ef1 | 135 | size--; |
AnnaBridge | 172:89b338f31ef1 | 136 | address++; |
AnnaBridge | 172:89b338f31ef1 | 137 | data++; |
AnnaBridge | 172:89b338f31ef1 | 138 | } |
AnnaBridge | 172:89b338f31ef1 | 139 | } |
AnnaBridge | 176:af195413fb11 | 140 | |
AnnaBridge | 176:af195413fb11 | 141 | flash_lock(); |
AnnaBridge | 176:af195413fb11 | 142 | |
AnnaBridge | 176:af195413fb11 | 143 | return status; |
AnnaBridge | 172:89b338f31ef1 | 144 | } |
AnnaBridge | 172:89b338f31ef1 | 145 | |
AnnaBridge | 172:89b338f31ef1 | 146 | uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
AnnaBridge | 172:89b338f31ef1 | 147 | { |
AnnaBridge | 172:89b338f31ef1 | 148 | |
AnnaBridge | 172:89b338f31ef1 | 149 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 172:89b338f31ef1 | 150 | return MBED_FLASH_INVALID_SIZE; |
AnnaBridge | 172:89b338f31ef1 | 151 | } |
AnnaBridge | 172:89b338f31ef1 | 152 | |
AnnaBridge | 172:89b338f31ef1 | 153 | return (GetSectorSize(GetSector(address))); |
AnnaBridge | 172:89b338f31ef1 | 154 | } |
AnnaBridge | 172:89b338f31ef1 | 155 | |
AnnaBridge | 172:89b338f31ef1 | 156 | uint32_t flash_get_page_size(const flash_t *obj) |
AnnaBridge | 172:89b338f31ef1 | 157 | { |
AnnaBridge | 175:b96e65c34a4d | 158 | // Flash of STM32F4 devices can be programed 1 byte at a time |
AnnaBridge | 175:b96e65c34a4d | 159 | return (1); |
AnnaBridge | 172:89b338f31ef1 | 160 | } |
AnnaBridge | 175:b96e65c34a4d | 161 | |
AnnaBridge | 172:89b338f31ef1 | 162 | uint32_t flash_get_start_address(const flash_t *obj) |
AnnaBridge | 172:89b338f31ef1 | 163 | { |
AnnaBridge | 172:89b338f31ef1 | 164 | return FLASH_BASE; |
AnnaBridge | 172:89b338f31ef1 | 165 | } |
AnnaBridge | 172:89b338f31ef1 | 166 | uint32_t flash_get_size(const flash_t *obj) |
AnnaBridge | 172:89b338f31ef1 | 167 | { |
AnnaBridge | 172:89b338f31ef1 | 168 | return FLASH_SIZE; |
AnnaBridge | 172:89b338f31ef1 | 169 | } |
AnnaBridge | 172:89b338f31ef1 | 170 | |
AnnaBridge | 172:89b338f31ef1 | 171 | /** |
AnnaBridge | 172:89b338f31ef1 | 172 | * @brief Gets the sector of a given address |
AnnaBridge | 172:89b338f31ef1 | 173 | * @param None |
AnnaBridge | 172:89b338f31ef1 | 174 | * @retval The sector of a given address |
AnnaBridge | 172:89b338f31ef1 | 175 | */ |
AnnaBridge | 172:89b338f31ef1 | 176 | static uint32_t GetSector(uint32_t address) |
AnnaBridge | 172:89b338f31ef1 | 177 | { |
AnnaBridge | 172:89b338f31ef1 | 178 | uint32_t sector = 0; |
AnnaBridge | 172:89b338f31ef1 | 179 | uint32_t tmp = address - ADDR_FLASH_SECTOR_0; |
AnnaBridge | 172:89b338f31ef1 | 180 | /* This function supports 1Mb and 2Mb flash sizes */ |
AnnaBridge | 175:b96e65c34a4d | 181 | #if defined(ADDR_FLASH_SECTOR_16) |
AnnaBridge | 172:89b338f31ef1 | 182 | if (address & 0x100000) { // handle 2nd bank |
AnnaBridge | 172:89b338f31ef1 | 183 | sector = FLASH_SECTOR_12; |
AnnaBridge | 172:89b338f31ef1 | 184 | tmp = address - ADDR_FLASH_SECTOR_12; |
AnnaBridge | 172:89b338f31ef1 | 185 | } |
AnnaBridge | 172:89b338f31ef1 | 186 | #endif |
AnnaBridge | 172:89b338f31ef1 | 187 | if (address < ADDR_FLASH_SECTOR_4) { // 16k sectorsize |
AnnaBridge | 172:89b338f31ef1 | 188 | sector += tmp >>14; |
AnnaBridge | 175:b96e65c34a4d | 189 | } |
AnnaBridge | 175:b96e65c34a4d | 190 | #if defined(ADDR_FLASH_SECTOR_5) |
AnnaBridge | 175:b96e65c34a4d | 191 | else if (address < ADDR_FLASH_SECTOR_5) { //64k sector size |
AnnaBridge | 172:89b338f31ef1 | 192 | sector += FLASH_SECTOR_4; |
AnnaBridge | 172:89b338f31ef1 | 193 | } else { |
AnnaBridge | 172:89b338f31ef1 | 194 | sector += 4 + (tmp >>17); |
AnnaBridge | 172:89b338f31ef1 | 195 | } |
AnnaBridge | 175:b96e65c34a4d | 196 | #else |
AnnaBridge | 175:b96e65c34a4d | 197 | // In case ADDR_FLASH_SECTOR_5 is not defined, sector 4 is the last one. |
AnnaBridge | 175:b96e65c34a4d | 198 | else { //64k sector size |
AnnaBridge | 175:b96e65c34a4d | 199 | sector += FLASH_SECTOR_4; |
AnnaBridge | 175:b96e65c34a4d | 200 | } |
AnnaBridge | 175:b96e65c34a4d | 201 | #endif |
AnnaBridge | 172:89b338f31ef1 | 202 | return sector; |
AnnaBridge | 172:89b338f31ef1 | 203 | } |
AnnaBridge | 172:89b338f31ef1 | 204 | |
AnnaBridge | 172:89b338f31ef1 | 205 | /** |
AnnaBridge | 172:89b338f31ef1 | 206 | * @brief Gets sector Size |
AnnaBridge | 172:89b338f31ef1 | 207 | * @param None |
AnnaBridge | 172:89b338f31ef1 | 208 | * @retval The size of a given sector |
AnnaBridge | 172:89b338f31ef1 | 209 | */ |
AnnaBridge | 172:89b338f31ef1 | 210 | static uint32_t GetSectorSize(uint32_t Sector) |
AnnaBridge | 172:89b338f31ef1 | 211 | { |
AnnaBridge | 172:89b338f31ef1 | 212 | uint32_t sectorsize = 0x00; |
AnnaBridge | 175:b96e65c34a4d | 213 | #if defined(FLASH_SECTOR_16) |
AnnaBridge | 172:89b338f31ef1 | 214 | if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\ |
AnnaBridge | 172:89b338f31ef1 | 215 | (Sector == FLASH_SECTOR_3) || (Sector == FLASH_SECTOR_12) || (Sector == FLASH_SECTOR_13) ||\ |
AnnaBridge | 172:89b338f31ef1 | 216 | (Sector == FLASH_SECTOR_14) || (Sector == FLASH_SECTOR_15)) { |
AnnaBridge | 172:89b338f31ef1 | 217 | sectorsize = 16 * 1024; |
AnnaBridge | 172:89b338f31ef1 | 218 | } else if((Sector == FLASH_SECTOR_4) || (Sector == FLASH_SECTOR_16)) { |
AnnaBridge | 172:89b338f31ef1 | 219 | #else |
AnnaBridge | 172:89b338f31ef1 | 220 | if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\ |
AnnaBridge | 172:89b338f31ef1 | 221 | (Sector == FLASH_SECTOR_3)) { |
AnnaBridge | 172:89b338f31ef1 | 222 | sectorsize = 16 * 1024; |
AnnaBridge | 172:89b338f31ef1 | 223 | } else if(Sector == FLASH_SECTOR_4) { |
AnnaBridge | 172:89b338f31ef1 | 224 | #endif |
AnnaBridge | 172:89b338f31ef1 | 225 | sectorsize = 64 * 1024; |
AnnaBridge | 172:89b338f31ef1 | 226 | } else { |
AnnaBridge | 172:89b338f31ef1 | 227 | sectorsize = 128 * 1024; |
AnnaBridge | 172:89b338f31ef1 | 228 | } |
AnnaBridge | 172:89b338f31ef1 | 229 | return sectorsize; |
AnnaBridge | 172:89b338f31ef1 | 230 | } |
AnnaBridge | 172:89b338f31ef1 | 231 | |
AnnaBridge | 172:89b338f31ef1 | 232 | #endif |