mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Nov 23 11:57:25 2017 +0000
Revision:
178:79309dc6340a
Parent:
175:af195413fb11
mbed-dev library. Release version 156

Who changed what in which revision?

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