00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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