|
1 | 1 | /* mbed Microcontroller Library
|
2 |
| - * Copyright (c) 2017 ARM Limited |
| 2 | + ******************************************************************************* |
| 3 | + * Copyright (c) 2017, STMicroelectronics |
| 4 | + * All rights reserved. |
3 | 5 | *
|
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
7 | 8 | *
|
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
9 | 17 | *
|
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + ******************************************************************************* |
15 | 29 | */
|
16 | 30 |
|
17 | 31 | #include "flash_api.h"
|
@@ -71,8 +85,186 @@ static const flash_target_config_t flash_target_config = {
|
71 | 85 |
|
72 | 86 | void flash_set_target_config(flash_t *obj)
|
73 | 87 | {
|
74 |
| - obj->flash_algo = &flash_algo_config; |
75 |
| - obj->target_config = &flash_target_config; |
| 88 | + |
| 89 | + if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
| 90 | + return -1; |
| 91 | + } |
| 92 | + |
| 93 | + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, |
| 94 | + you have to make sure that these data are rewritten before they are accessed during code |
| 95 | + execution. If this cannot be done safely, it is recommended to flush the caches by setting the |
| 96 | + DCRST and ICRST bits in the FLASH_CR register. */ |
| 97 | + __HAL_FLASH_DATA_CACHE_DISABLE(); |
| 98 | + __HAL_FLASH_INSTRUCTION_CACHE_DISABLE(); |
| 99 | + |
| 100 | + __HAL_FLASH_DATA_CACHE_RESET(); |
| 101 | + __HAL_FLASH_INSTRUCTION_CACHE_RESET(); |
| 102 | + |
| 103 | + __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); |
| 104 | + __HAL_FLASH_DATA_CACHE_ENABLE(); |
| 105 | + |
| 106 | + while (size > 0) { |
| 107 | + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, (uint64_t)*data) != HAL_OK) { |
| 108 | + return -1; |
| 109 | + } else { |
| 110 | + size--; |
| 111 | + address++; |
| 112 | + data++; |
| 113 | + } |
| 114 | + } |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) |
| 119 | +{ |
| 120 | + |
| 121 | + if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { |
| 122 | + return MBED_FLASH_INVALID_SIZE; |
| 123 | + } |
| 124 | + |
| 125 | + return (GetSectorSize(GetSector(address))); |
76 | 126 | }
|
77 | 127 |
|
78 |
| -#endif |
| 128 | +uint32_t flash_get_page_size(const flash_t *obj) |
| 129 | +{ |
| 130 | + // not applicable for STM32F4 |
| 131 | + return (0x4000); // minimum sector size |
| 132 | +} |
| 133 | +uint32_t flash_get_start_address(const flash_t *obj) |
| 134 | +{ |
| 135 | + return FLASH_BASE; |
| 136 | +} |
| 137 | +uint32_t flash_get_size(const flash_t *obj) |
| 138 | +{ |
| 139 | + return FLASH_SIZE; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * @brief Gets the sector of a given address |
| 144 | + * @param None |
| 145 | + * @retval The sector of a given address |
| 146 | + */ |
| 147 | +static uint32_t GetSector(uint32_t address) |
| 148 | +{ |
| 149 | + uint32_t sector = 0; |
| 150 | + |
| 151 | + if((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0)) |
| 152 | + { |
| 153 | + sector = FLASH_SECTOR_0; |
| 154 | + } |
| 155 | + else if((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1)) |
| 156 | + { |
| 157 | + sector = FLASH_SECTOR_1; |
| 158 | + } |
| 159 | + else if((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2)) |
| 160 | + { |
| 161 | + sector = FLASH_SECTOR_2; |
| 162 | + } |
| 163 | + else if((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3)) |
| 164 | + { |
| 165 | + sector = FLASH_SECTOR_3; |
| 166 | + } |
| 167 | + else if((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4)) |
| 168 | + { |
| 169 | + sector = FLASH_SECTOR_4; |
| 170 | + } |
| 171 | + else if((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5)) |
| 172 | + { |
| 173 | + sector = FLASH_SECTOR_5; |
| 174 | + } |
| 175 | + else if((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6)) |
| 176 | + { |
| 177 | + sector = FLASH_SECTOR_6; |
| 178 | + } |
| 179 | + else if((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7)) |
| 180 | + { |
| 181 | + sector = FLASH_SECTOR_7; |
| 182 | + } |
| 183 | + else if((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8)) |
| 184 | + { |
| 185 | + sector = FLASH_SECTOR_8; |
| 186 | + } |
| 187 | + else if((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9)) |
| 188 | + { |
| 189 | + sector = FLASH_SECTOR_9; |
| 190 | + } |
| 191 | + else if((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10)) |
| 192 | + { |
| 193 | + sector = FLASH_SECTOR_10; |
| 194 | + } |
| 195 | + else if((address < ADDR_FLASH_SECTOR_12) && (address >= ADDR_FLASH_SECTOR_11)) |
| 196 | + { |
| 197 | + sector = FLASH_SECTOR_11; |
| 198 | + } |
| 199 | + else if((address < ADDR_FLASH_SECTOR_13) && (address >= ADDR_FLASH_SECTOR_12)) |
| 200 | + { |
| 201 | + sector = FLASH_SECTOR_12; |
| 202 | + } |
| 203 | + else if((address < ADDR_FLASH_SECTOR_14) && (address >= ADDR_FLASH_SECTOR_13)) |
| 204 | + { |
| 205 | + sector = FLASH_SECTOR_13; |
| 206 | + } |
| 207 | + else if((address < ADDR_FLASH_SECTOR_15) && (address >= ADDR_FLASH_SECTOR_14)) |
| 208 | + { |
| 209 | + sector = FLASH_SECTOR_14; |
| 210 | + } |
| 211 | + else if((address < ADDR_FLASH_SECTOR_16) && (address >= ADDR_FLASH_SECTOR_15)) |
| 212 | + { |
| 213 | + sector = FLASH_SECTOR_15; |
| 214 | + } |
| 215 | + else if((address < ADDR_FLASH_SECTOR_17) && (address >= ADDR_FLASH_SECTOR_16)) |
| 216 | + { |
| 217 | + sector = FLASH_SECTOR_16; |
| 218 | + } |
| 219 | + else if((address < ADDR_FLASH_SECTOR_18) && (address >= ADDR_FLASH_SECTOR_17)) |
| 220 | + { |
| 221 | + sector = FLASH_SECTOR_17; |
| 222 | + } |
| 223 | + else if((address < ADDR_FLASH_SECTOR_19) && (address >= ADDR_FLASH_SECTOR_18)) |
| 224 | + { |
| 225 | + sector = FLASH_SECTOR_18; |
| 226 | + } |
| 227 | + else if((address < ADDR_FLASH_SECTOR_20) && (address >= ADDR_FLASH_SECTOR_19)) |
| 228 | + { |
| 229 | + sector = FLASH_SECTOR_19; |
| 230 | + } |
| 231 | + else if((address < ADDR_FLASH_SECTOR_21) && (address >= ADDR_FLASH_SECTOR_20)) |
| 232 | + { |
| 233 | + sector = FLASH_SECTOR_20; |
| 234 | + } |
| 235 | + else if((address < ADDR_FLASH_SECTOR_22) && (address >= ADDR_FLASH_SECTOR_21)) |
| 236 | + { |
| 237 | + sector = FLASH_SECTOR_21; |
| 238 | + } |
| 239 | + else if((address < ADDR_FLASH_SECTOR_23) && (address >= ADDR_FLASH_SECTOR_22)) |
| 240 | + { |
| 241 | + sector = FLASH_SECTOR_22; |
| 242 | + } |
| 243 | + else/*(address < FLASH_END_ADDR) && (address >= ADDR_FLASH_SECTOR_23))*/ |
| 244 | + { |
| 245 | + sector = FLASH_SECTOR_23; |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | + return sector; |
| 250 | +} |
| 251 | + |
| 252 | +/** |
| 253 | + * @brief Gets sector Size |
| 254 | + * @param None |
| 255 | + * @retval The size of a given sector |
| 256 | + */ |
| 257 | +static uint32_t GetSectorSize(uint32_t Sector) |
| 258 | +{ |
| 259 | + uint32_t sectorsize = 0x00; |
| 260 | + if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) ||\ |
| 261 | + (Sector == FLASH_SECTOR_3) || (Sector == FLASH_SECTOR_12) || (Sector == FLASH_SECTOR_13) ||\ |
| 262 | + (Sector == FLASH_SECTOR_14) || (Sector == FLASH_SECTOR_15)) { |
| 263 | + sectorsize = 16 * 1024; |
| 264 | + } else if((Sector == FLASH_SECTOR_4) || (Sector == FLASH_SECTOR_16)) { |
| 265 | + sectorsize = 64 * 1024; |
| 266 | + } else { |
| 267 | + sectorsize = 128 * 1024; |
| 268 | + } |
| 269 | + return sectorsize; |
| 270 | +} |
0 commit comments