mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
Kojto
Date:
Wed Jul 19 17:31:21 2017 +0100
Revision:
169:e3b6fe271b81
Parent:
167:e84263d55307
This updates the lib to the mbed lib v 147

Who changed what in which revision?

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