test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Aug 16 18:27:13 2017 +0100
Revision:
172:89b338f31ef1
Child:
175:b96e65c34a4d
This updates the lib to the mbed lib v 149

Who changed what in which revision?

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