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