|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + #if DEVICE_FLASH |
| 18 | + |
| 19 | +#include "flash_api.h" |
| 20 | +#include "flash_data.h" |
| 21 | +#include "platform/mbed_critical.h" |
| 22 | + |
| 23 | +static uint32_t GetSector(uint32_t Address); |
| 24 | +static uint32_t GetSectorSize(uint32_t Sector); |
| 25 | + |
| 26 | +static int32_t flash_unlock(void) |
| 27 | +{ |
| 28 | + /* Allow Access to Flash control registers and user Falsh */ |
| 29 | + if (HAL_FLASH_Unlock()) { |
| 30 | + return -1; |
| 31 | + } else { |
| 32 | + return 0; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +static int32_t flash_lock(void) |
| 37 | +{ |
| 38 | + /* Disable the Flash option control register access (recommended to protect |
| 39 | + the option Bytes against possible unwanted operations) */ |
| 40 | + if (HAL_FLASH_Lock()) { |
| 41 | + return -1; |
| 42 | + } else { |
| 43 | + return 0; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +int32_t flash_init(flash_t *obj) |
| 48 | +{ |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +int32_t flash_free(flash_t *obj) |
| 53 | +{ |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
| 58 | +{ |
| 59 | + static FLASH_EraseInitTypeDef EraseInitStruct; |
| 60 | + uint32_t FirstSector; |
| 61 | + uint32_t SectorError = 0; |
| 62 | + int32_t status = 0; |
| 63 | + |
| 64 | + if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + if (flash_unlock() != HAL_OK) { |
| 69 | + return -1; |
| 70 | + } |
| 71 | + |
| 72 | + /* Get the 1st sector to erase */ |
| 73 | + FirstSector = GetSector(address); |
| 74 | + |
| 75 | + /* Fill EraseInit structure*/ |
| 76 | + EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; |
| 77 | + EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; |
| 78 | + EraseInitStruct.Sector = FirstSector; |
| 79 | + EraseInitStruct.NbSectors = 1; |
| 80 | + |
| 81 | + if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { |
| 82 | + status = -1; |
| 83 | + } |
| 84 | + |
| 85 | + flash_lock(); |
| 86 | + |
| 87 | + return status; |
| 88 | +} |
| 89 | + |
| 90 | +int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) |
| 91 | +{ |
| 92 | + int32_t status = 0; |
| 93 | + |
| 94 | + if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + if (flash_unlock() != HAL_OK) { |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
| 103 | + you have to make sure that these data are rewritten before they are accessed during code |
| 104 | + execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
| 105 | + DCRST and ICRST bits in the FLASH_CR register. */ |
| 106 | + __HAL_FLASH_DATA_CACHE_DISABLE(); |
| 107 | + __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); |
| 108 | + |
| 109 | + __HAL_FLASH_DATA_CACHE_RESET(); |
| 110 | + __HAL_FLASH_INSTRUCTION_CACHE_RESET(); |
| 111 | + |
| 112 | + __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); |
| 113 | + __HAL_FLASH_DATA_CACHE_ENABLE(); |
| 114 | + |
| 115 | + while ((size > 0) && (status == 0)) { |
| 116 | + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, (uint64_t)*data) != HAL_OK) { |
| 117 | + status = -1; |
| 118 | + } else { |
| 119 | + size--; |
| 120 | + address++; |
| 121 | + data++; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + flash_lock(); |
| 126 | + |
| 127 | + return status; |
| 128 | +} |
| 129 | + |
| 130 | +uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
| 131 | +{ |
| 132 | + if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
| 133 | + return MBED_FLASH_INVALID_SIZE; |
| 134 | + } else { |
| 135 | + return (GetSectorSize(GetSector(address))); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +uint32_t flash_get_page_size(const flash_t *obj) |
| 140 | +{ |
| 141 | + // Flash of STM32F2 devices can be programed 1 byte at a time |
| 142 | + return 1; |
| 143 | +} |
| 144 | + |
| 145 | +uint32_t flash_get_start_address(const flash_t *obj) |
| 146 | +{ |
| 147 | + return FLASH_BASE; |
| 148 | +} |
| 149 | + |
| 150 | +uint32_t flash_get_size(const flash_t *obj) |
| 151 | +{ |
| 152 | + return FLASH_SIZE; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * @brief Gets the sector of a given address |
| 157 | + * @param None |
| 158 | + * @retval The sector of a given address |
| 159 | + */ |
| 160 | +static uint32_t GetSector(uint32_t address) |
| 161 | +{ |
| 162 | + uint32_t sector = 0; |
| 163 | + uint32_t tmp = address - ADDR_FLASH_SECTOR_0; |
| 164 | + /* This function supports 1Mb and 2Mb flash sizes */ |
| 165 | +#if defined(ADDR_FLASH_SECTOR_16) |
| 166 | + if (address & 0x100000) { // handle 2nd bank |
| 167 | + /* Sector will be at least 12 */ |
| 168 | + sector = FLASH_SECTOR_12; |
| 169 | + tmp -= 0x100000; |
| 170 | + address -= 0x100000; |
| 171 | + } |
| 172 | +#endif |
| 173 | + if (address < ADDR_FLASH_SECTOR_4) { // 16k sectorsize |
| 174 | + sector += tmp >>14; |
| 175 | + } |
| 176 | +#if defined(ADDR_FLASH_SECTOR_5) |
| 177 | + else if (address < ADDR_FLASH_SECTOR_5) { //64k sector size |
| 178 | + sector += FLASH_SECTOR_4; |
| 179 | + } else { |
| 180 | + sector += 4 + (tmp >>17); |
| 181 | + } |
| 182 | +#else |
| 183 | + // In case ADDR_FLASH_SECTOR_5 is not defined, sector 4 is the last one. |
| 184 | + else { //64k sector size |
| 185 | + sector += FLASH_SECTOR_4; |
| 186 | + } |
| 187 | +#endif |
| 188 | + return sector; |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * @brief Gets sector Size |
| 193 | + * @param None |
| 194 | + * @retval The size of a given sector |
| 195 | + */ |
| 196 | +static uint32_t GetSectorSize(uint32_t Sector) |
| 197 | +{ |
| 198 | + uint32_t sectorsize = 0x00; |
| 199 | +#if defined(FLASH_SECTOR_16) |
| 200 | + if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\ |
| 201 | + (Sector == FLASH_SECTOR_3) || (Sector == FLASH_SECTOR_12) || (Sector == FLASH_SECTOR_13) ||\ |
| 202 | + (Sector == FLASH_SECTOR_14) || (Sector == FLASH_SECTOR_15)) { |
| 203 | + sectorsize = 16 * 1024; |
| 204 | + } else if((Sector == FLASH_SECTOR_4) || (Sector == FLASH_SECTOR_16)) { |
| 205 | +#else |
| 206 | +if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\ |
| 207 | + (Sector == FLASH_SECTOR_3)) { |
| 208 | + sectorsize = 16 * 1024; |
| 209 | + } else if(Sector == FLASH_SECTOR_4) { |
| 210 | +#endif |
| 211 | + sectorsize = 64 * 1024; |
| 212 | + } else { |
| 213 | + sectorsize = 128 * 1024; |
| 214 | + } |
| 215 | + return sectorsize; |
| 216 | +} |
| 217 | + |
| 218 | +#endif |
0 commit comments