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
targets/TARGET_STM/TARGET_STM32F7/flash_api.c@175:af195413fb11, 2017-10-11 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Oct 11 12:45:49 2017 +0100
- Revision:
- 175:af195413fb11
- Parent:
- 174:b96e65c34a4d
This updates the lib to the mbed lib v 153
Who changed what in which revision?
User | Revision | Line number | New 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 | 175:af195413fb11 | 41 | return 0; |
AnnaBridge | 175:af195413fb11 | 42 | } |
AnnaBridge | 175:af195413fb11 | 43 | int32_t flash_free(flash_t *obj) |
AnnaBridge | 175:af195413fb11 | 44 | { |
AnnaBridge | 175:af195413fb11 | 45 | return 0; |
AnnaBridge | 175:af195413fb11 | 46 | } |
AnnaBridge | 175:af195413fb11 | 47 | |
AnnaBridge | 175:af195413fb11 | 48 | static int32_t flash_unlock(void) |
AnnaBridge | 175:af195413fb11 | 49 | { |
AnnaBridge | 175:af195413fb11 | 50 | /* Allow Access to Flash control registers and user Falsh */ |
AnnaBridge | 174:b96e65c34a4d | 51 | if (HAL_FLASH_Unlock()) { |
AnnaBridge | 174:b96e65c34a4d | 52 | return -1; |
AnnaBridge | 174:b96e65c34a4d | 53 | } else { |
AnnaBridge | 174:b96e65c34a4d | 54 | return 0; |
AnnaBridge | 174:b96e65c34a4d | 55 | } |
AnnaBridge | 174:b96e65c34a4d | 56 | } |
AnnaBridge | 175:af195413fb11 | 57 | |
AnnaBridge | 175:af195413fb11 | 58 | static int32_t flash_lock(void) |
AnnaBridge | 174:b96e65c34a4d | 59 | { |
AnnaBridge | 174:b96e65c34a4d | 60 | /* Disable the Flash option control register access (recommended to protect |
AnnaBridge | 174:b96e65c34a4d | 61 | the option Bytes against possible unwanted operations) */ |
AnnaBridge | 174:b96e65c34a4d | 62 | if (HAL_FLASH_Lock()) { |
AnnaBridge | 174:b96e65c34a4d | 63 | return -1; |
AnnaBridge | 174:b96e65c34a4d | 64 | } else { |
AnnaBridge | 174:b96e65c34a4d | 65 | return 0; |
AnnaBridge | 174:b96e65c34a4d | 66 | } |
AnnaBridge | 174:b96e65c34a4d | 67 | } |
AnnaBridge | 175:af195413fb11 | 68 | |
AnnaBridge | 174:b96e65c34a4d | 69 | int32_t flash_erase_sector(flash_t *obj, uint32_t address) |
AnnaBridge | 174:b96e65c34a4d | 70 | { |
AnnaBridge | 174:b96e65c34a4d | 71 | /* Variable used for Erase procedure */ |
AnnaBridge | 174:b96e65c34a4d | 72 | FLASH_EraseInitTypeDef EraseInitStruct; |
AnnaBridge | 174:b96e65c34a4d | 73 | FLASH_OBProgramInitTypeDef OBInit; |
AnnaBridge | 174:b96e65c34a4d | 74 | uint32_t SectorId; |
AnnaBridge | 174:b96e65c34a4d | 75 | uint32_t SectorError = 0; |
AnnaBridge | 175:af195413fb11 | 76 | int32_t status = 0; |
AnnaBridge | 174:b96e65c34a4d | 77 | |
AnnaBridge | 174:b96e65c34a4d | 78 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 174:b96e65c34a4d | 79 | return -1; |
AnnaBridge | 174:b96e65c34a4d | 80 | } |
AnnaBridge | 174:b96e65c34a4d | 81 | |
AnnaBridge | 175:af195413fb11 | 82 | if (flash_unlock() != HAL_OK) { |
AnnaBridge | 175:af195413fb11 | 83 | return -1; |
AnnaBridge | 175:af195413fb11 | 84 | } |
AnnaBridge | 175:af195413fb11 | 85 | |
AnnaBridge | 174:b96e65c34a4d | 86 | /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
AnnaBridge | 174:b96e65c34a4d | 87 | you have to make sure that these data are rewritten before they are accessed during code |
AnnaBridge | 174:b96e65c34a4d | 88 | execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
AnnaBridge | 174:b96e65c34a4d | 89 | DCRST and ICRST bits in the FLASH_CR register. */ |
AnnaBridge | 174:b96e65c34a4d | 90 | __HAL_FLASH_ART_DISABLE(); |
AnnaBridge | 174:b96e65c34a4d | 91 | __HAL_FLASH_ART_RESET(); |
AnnaBridge | 174:b96e65c34a4d | 92 | __HAL_FLASH_ART_ENABLE(); |
AnnaBridge | 174:b96e65c34a4d | 93 | |
AnnaBridge | 174:b96e65c34a4d | 94 | /* Get the 1st sector to erase */ |
AnnaBridge | 174:b96e65c34a4d | 95 | SectorId = GetSector(address); |
AnnaBridge | 174:b96e65c34a4d | 96 | |
AnnaBridge | 174:b96e65c34a4d | 97 | /* Allow Access to option bytes sector */ |
AnnaBridge | 174:b96e65c34a4d | 98 | HAL_FLASH_OB_Unlock(); |
AnnaBridge | 174:b96e65c34a4d | 99 | /* Get the Dual bank configuration status */ |
AnnaBridge | 174:b96e65c34a4d | 100 | HAL_FLASHEx_OBGetConfig(&OBInit); |
AnnaBridge | 174:b96e65c34a4d | 101 | /* Allow Access to option bytes sector */ |
AnnaBridge | 174:b96e65c34a4d | 102 | HAL_FLASH_OB_Lock(); |
AnnaBridge | 174:b96e65c34a4d | 103 | |
AnnaBridge | 174:b96e65c34a4d | 104 | #if defined (FLASH_OPTCR_nDBANK) |
AnnaBridge | 174:b96e65c34a4d | 105 | /* On targets that support dynamic single or dual bank configuration |
AnnaBridge | 174:b96e65c34a4d | 106 | * Check that we're in SINGLE Bank mode, only supported mode now. |
AnnaBridge | 174:b96e65c34a4d | 107 | */ |
AnnaBridge | 174:b96e65c34a4d | 108 | if((OBInit.USERConfig & OB_NDBANK_SINGLE_BANK) != OB_NDBANK_SINGLE_BANK) { |
AnnaBridge | 174:b96e65c34a4d | 109 | /* We don't support the DUAL BANK MODE for now, so return error */ |
AnnaBridge | 174:b96e65c34a4d | 110 | return -1; |
AnnaBridge | 174:b96e65c34a4d | 111 | } |
AnnaBridge | 174:b96e65c34a4d | 112 | #endif |
AnnaBridge | 174:b96e65c34a4d | 113 | |
AnnaBridge | 174:b96e65c34a4d | 114 | /* Fill EraseInit structure*/ |
AnnaBridge | 174:b96e65c34a4d | 115 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; |
AnnaBridge | 174:b96e65c34a4d | 116 | EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; |
AnnaBridge | 174:b96e65c34a4d | 117 | EraseInitStruct.Sector = SectorId; |
AnnaBridge | 174:b96e65c34a4d | 118 | EraseInitStruct.NbSectors = 1; |
AnnaBridge | 174:b96e65c34a4d | 119 | |
AnnaBridge | 174:b96e65c34a4d | 120 | if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){ |
AnnaBridge | 175:af195413fb11 | 121 | status = -1; |
AnnaBridge | 174:b96e65c34a4d | 122 | } |
AnnaBridge | 175:af195413fb11 | 123 | |
AnnaBridge | 175:af195413fb11 | 124 | flash_lock(); |
AnnaBridge | 175:af195413fb11 | 125 | |
AnnaBridge | 175:af195413fb11 | 126 | return status; |
AnnaBridge | 174:b96e65c34a4d | 127 | } |
AnnaBridge | 174:b96e65c34a4d | 128 | |
AnnaBridge | 174:b96e65c34a4d | 129 | int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, |
AnnaBridge | 174:b96e65c34a4d | 130 | uint32_t size) |
AnnaBridge | 174:b96e65c34a4d | 131 | { |
AnnaBridge | 175:af195413fb11 | 132 | int32_t status = 0; |
AnnaBridge | 175:af195413fb11 | 133 | |
AnnaBridge | 174:b96e65c34a4d | 134 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 174:b96e65c34a4d | 135 | return -1; |
AnnaBridge | 174:b96e65c34a4d | 136 | } |
AnnaBridge | 174:b96e65c34a4d | 137 | |
AnnaBridge | 175:af195413fb11 | 138 | if (flash_unlock() != HAL_OK) { |
AnnaBridge | 175:af195413fb11 | 139 | return -1; |
AnnaBridge | 175:af195413fb11 | 140 | } |
AnnaBridge | 175:af195413fb11 | 141 | |
AnnaBridge | 174:b96e65c34a4d | 142 | /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
AnnaBridge | 174:b96e65c34a4d | 143 | you have to make sure that these data are rewritten before they are accessed during code |
AnnaBridge | 174:b96e65c34a4d | 144 | execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
AnnaBridge | 174:b96e65c34a4d | 145 | DCRST and ICRST bits in the FLASH_CR register. */ |
AnnaBridge | 174:b96e65c34a4d | 146 | __HAL_FLASH_ART_DISABLE(); |
AnnaBridge | 174:b96e65c34a4d | 147 | __HAL_FLASH_ART_RESET(); |
AnnaBridge | 174:b96e65c34a4d | 148 | __HAL_FLASH_ART_ENABLE(); |
AnnaBridge | 174:b96e65c34a4d | 149 | |
AnnaBridge | 175:af195413fb11 | 150 | while ((size > 0) && (status == 0)) { |
AnnaBridge | 174:b96e65c34a4d | 151 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, |
AnnaBridge | 174:b96e65c34a4d | 152 | address, (uint64_t)*data) != HAL_OK) { |
AnnaBridge | 175:af195413fb11 | 153 | status = -1; |
AnnaBridge | 174:b96e65c34a4d | 154 | } else { |
AnnaBridge | 174:b96e65c34a4d | 155 | size--; |
AnnaBridge | 174:b96e65c34a4d | 156 | address++; |
AnnaBridge | 174:b96e65c34a4d | 157 | data++; |
AnnaBridge | 174:b96e65c34a4d | 158 | } |
AnnaBridge | 174:b96e65c34a4d | 159 | } |
AnnaBridge | 175:af195413fb11 | 160 | |
AnnaBridge | 175:af195413fb11 | 161 | flash_lock(); |
AnnaBridge | 175:af195413fb11 | 162 | |
AnnaBridge | 175:af195413fb11 | 163 | return status; |
AnnaBridge | 174:b96e65c34a4d | 164 | } |
AnnaBridge | 174:b96e65c34a4d | 165 | |
AnnaBridge | 174:b96e65c34a4d | 166 | uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
AnnaBridge | 174:b96e65c34a4d | 167 | { |
AnnaBridge | 174:b96e65c34a4d | 168 | if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
AnnaBridge | 174:b96e65c34a4d | 169 | return MBED_FLASH_INVALID_SIZE; |
AnnaBridge | 174:b96e65c34a4d | 170 | } |
AnnaBridge | 174:b96e65c34a4d | 171 | |
AnnaBridge | 174:b96e65c34a4d | 172 | return (GetSectorSize(GetSector(address))); |
AnnaBridge | 174:b96e65c34a4d | 173 | } |
AnnaBridge | 174:b96e65c34a4d | 174 | |
AnnaBridge | 174:b96e65c34a4d | 175 | uint32_t flash_get_page_size(const flash_t *obj) |
AnnaBridge | 174:b96e65c34a4d | 176 | { |
AnnaBridge | 174:b96e65c34a4d | 177 | // Flash of STM32F7 devices can be programed 1 byte at a time |
AnnaBridge | 174:b96e65c34a4d | 178 | return (1); |
AnnaBridge | 174:b96e65c34a4d | 179 | } |
AnnaBridge | 174:b96e65c34a4d | 180 | |
AnnaBridge | 174:b96e65c34a4d | 181 | uint32_t flash_get_start_address(const flash_t *obj) |
AnnaBridge | 174:b96e65c34a4d | 182 | { |
AnnaBridge | 174:b96e65c34a4d | 183 | return FLASH_BASE; |
AnnaBridge | 174:b96e65c34a4d | 184 | } |
AnnaBridge | 174:b96e65c34a4d | 185 | uint32_t flash_get_size(const flash_t *obj) |
AnnaBridge | 174:b96e65c34a4d | 186 | { |
AnnaBridge | 174:b96e65c34a4d | 187 | return FLASH_SIZE; |
AnnaBridge | 174:b96e65c34a4d | 188 | } |
AnnaBridge | 174:b96e65c34a4d | 189 | |
AnnaBridge | 174:b96e65c34a4d | 190 | /** |
AnnaBridge | 174:b96e65c34a4d | 191 | * @brief Gets the sector of a given address |
AnnaBridge | 174:b96e65c34a4d | 192 | * @param None |
AnnaBridge | 174:b96e65c34a4d | 193 | * @retval The sector of a given address |
AnnaBridge | 174:b96e65c34a4d | 194 | */ |
AnnaBridge | 174:b96e65c34a4d | 195 | static uint32_t GetSector(uint32_t address) |
AnnaBridge | 174:b96e65c34a4d | 196 | { |
AnnaBridge | 174:b96e65c34a4d | 197 | uint32_t sector = 0; |
AnnaBridge | 174:b96e65c34a4d | 198 | uint32_t tmp = address - ADDR_FLASH_SECTOR_0; |
AnnaBridge | 174:b96e65c34a4d | 199 | |
AnnaBridge | 174:b96e65c34a4d | 200 | if (address < ADDR_FLASH_SECTOR_4) { |
AnnaBridge | 174:b96e65c34a4d | 201 | // 32k sectorsize |
AnnaBridge | 174:b96e65c34a4d | 202 | sector += tmp >>15; |
AnnaBridge | 174:b96e65c34a4d | 203 | } else if (address < ADDR_FLASH_SECTOR_5) { |
AnnaBridge | 174:b96e65c34a4d | 204 | //64k sector size |
AnnaBridge | 174:b96e65c34a4d | 205 | sector += FLASH_SECTOR_4; |
AnnaBridge | 174:b96e65c34a4d | 206 | } else { |
AnnaBridge | 174:b96e65c34a4d | 207 | sector += 4 + (tmp >>18); |
AnnaBridge | 174:b96e65c34a4d | 208 | } |
AnnaBridge | 174:b96e65c34a4d | 209 | |
AnnaBridge | 174:b96e65c34a4d | 210 | return sector; |
AnnaBridge | 174:b96e65c34a4d | 211 | } |
AnnaBridge | 174:b96e65c34a4d | 212 | |
AnnaBridge | 174:b96e65c34a4d | 213 | /** |
AnnaBridge | 174:b96e65c34a4d | 214 | * @brief Gets sector Size |
AnnaBridge | 174:b96e65c34a4d | 215 | * @param None |
AnnaBridge | 174:b96e65c34a4d | 216 | * @retval The size of a given sector |
AnnaBridge | 174:b96e65c34a4d | 217 | */ |
AnnaBridge | 174:b96e65c34a4d | 218 | static uint32_t GetSectorSize(uint32_t Sector) |
AnnaBridge | 174:b96e65c34a4d | 219 | { |
AnnaBridge | 174:b96e65c34a4d | 220 | uint32_t sectorsize = 0x00; |
AnnaBridge | 174:b96e65c34a4d | 221 | if ((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) ||\ |
AnnaBridge | 174:b96e65c34a4d | 222 | (Sector == FLASH_SECTOR_2) || (Sector == FLASH_SECTOR_3)) { |
AnnaBridge | 174:b96e65c34a4d | 223 | sectorsize = 32 * 1024; |
AnnaBridge | 174:b96e65c34a4d | 224 | } else if (Sector == FLASH_SECTOR_4) { |
AnnaBridge | 174:b96e65c34a4d | 225 | sectorsize = 128 * 1024; |
AnnaBridge | 174:b96e65c34a4d | 226 | } else { |
AnnaBridge | 174:b96e65c34a4d | 227 | sectorsize = 256 * 1024; |
AnnaBridge | 174:b96e65c34a4d | 228 | } |
AnnaBridge | 174:b96e65c34a4d | 229 | |
AnnaBridge | 174:b96e65c34a4d | 230 | return sectorsize; |
AnnaBridge | 174:b96e65c34a4d | 231 | } |
AnnaBridge | 174:b96e65c34a4d | 232 | |
AnnaBridge | 174:b96e65c34a4d | 233 | #endif |