The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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