mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Mon Oct 02 15:33:19 2017 +0100
Revision:
174:b96e65c34a4d
Child:
175:af195413fb11
This updates the lib to the mbed lib v 152

Who changed what in which revision?

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