mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c

Dependents:   Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay

Fork of mbed-dev by mbed official

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

Who changed what in which revision?

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