inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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