mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
182:57724642e740
mbed-dev library. Release version 159.

Who changed what in which revision?

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