mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
drivers/FlashIAP.cpp@189:f392fc9709a3, 2019-02-20 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Feb 20 22:31:08 2019 +0000
- Revision:
- 189:f392fc9709a3
- Parent:
- 188:bcfe06ba3d64
mbed library release version 165
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 160:d5399cc887bb | 1 | /* mbed Microcontroller Library |
<> | 160:d5399cc887bb | 2 | * Copyright (c) 2017 ARM Limited |
<> | 160:d5399cc887bb | 3 | * |
<> | 160:d5399cc887bb | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
<> | 160:d5399cc887bb | 5 | * of this software and associated documentation files (the "Software"), to deal |
<> | 160:d5399cc887bb | 6 | * in the Software without restriction, including without limitation the rights |
<> | 160:d5399cc887bb | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
<> | 160:d5399cc887bb | 8 | * copies of the Software, and to permit persons to whom the Software is |
<> | 160:d5399cc887bb | 9 | * furnished to do so, subject to the following conditions: |
<> | 160:d5399cc887bb | 10 | * |
<> | 160:d5399cc887bb | 11 | * The above copyright notice and this permission notice shall be included in |
<> | 160:d5399cc887bb | 12 | * all copies or substantial portions of the Software. |
<> | 160:d5399cc887bb | 13 | * |
<> | 160:d5399cc887bb | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
<> | 160:d5399cc887bb | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
<> | 160:d5399cc887bb | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
<> | 160:d5399cc887bb | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
<> | 160:d5399cc887bb | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
<> | 160:d5399cc887bb | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
<> | 160:d5399cc887bb | 20 | * SOFTWARE. |
<> | 160:d5399cc887bb | 21 | */ |
<> | 160:d5399cc887bb | 22 | |
AnnaBridge | 184:08ed48f1de7f | 23 | #include <stdio.h> |
<> | 160:d5399cc887bb | 24 | #include <string.h> |
AnnaBridge | 184:08ed48f1de7f | 25 | #include <algorithm> |
<> | 160:d5399cc887bb | 26 | #include "FlashIAP.h" |
AnnaBridge | 188:bcfe06ba3d64 | 27 | #include "platform/mbed_assert.h" |
AnnaBridge | 189:f392fc9709a3 | 28 | #include "platform/ScopedRamExecutionLock.h" |
AnnaBridge | 189:f392fc9709a3 | 29 | #include "platform/ScopedRomWriteLock.h" |
<> | 160:d5399cc887bb | 30 | |
<> | 160:d5399cc887bb | 31 | |
AnnaBridge | 189:f392fc9709a3 | 32 | #if DEVICE_FLASH |
<> | 160:d5399cc887bb | 33 | |
<> | 160:d5399cc887bb | 34 | namespace mbed { |
<> | 160:d5399cc887bb | 35 | |
AnnaBridge | 189:f392fc9709a3 | 36 | const unsigned int num_write_retries = 16; |
AnnaBridge | 189:f392fc9709a3 | 37 | |
<> | 160:d5399cc887bb | 38 | SingletonPtr<PlatformMutex> FlashIAP::_mutex; |
<> | 160:d5399cc887bb | 39 | |
<> | 160:d5399cc887bb | 40 | static inline bool is_aligned(uint32_t number, uint32_t alignment) |
<> | 160:d5399cc887bb | 41 | { |
<> | 160:d5399cc887bb | 42 | if ((number % alignment) != 0) { |
<> | 160:d5399cc887bb | 43 | return false; |
<> | 160:d5399cc887bb | 44 | } else { |
<> | 160:d5399cc887bb | 45 | return true; |
<> | 160:d5399cc887bb | 46 | } |
<> | 160:d5399cc887bb | 47 | } |
<> | 160:d5399cc887bb | 48 | |
<> | 160:d5399cc887bb | 49 | FlashIAP::FlashIAP() |
<> | 160:d5399cc887bb | 50 | { |
<> | 160:d5399cc887bb | 51 | |
<> | 160:d5399cc887bb | 52 | } |
<> | 160:d5399cc887bb | 53 | |
<> | 160:d5399cc887bb | 54 | FlashIAP::~FlashIAP() |
<> | 160:d5399cc887bb | 55 | { |
<> | 160:d5399cc887bb | 56 | |
<> | 160:d5399cc887bb | 57 | } |
<> | 160:d5399cc887bb | 58 | |
<> | 160:d5399cc887bb | 59 | int FlashIAP::init() |
<> | 160:d5399cc887bb | 60 | { |
<> | 160:d5399cc887bb | 61 | int ret = 0; |
<> | 160:d5399cc887bb | 62 | _mutex->lock(); |
AnnaBridge | 189:f392fc9709a3 | 63 | { |
AnnaBridge | 189:f392fc9709a3 | 64 | ScopedRamExecutionLock make_ram_executable; |
AnnaBridge | 189:f392fc9709a3 | 65 | ScopedRomWriteLock make_rom_writable; |
AnnaBridge | 189:f392fc9709a3 | 66 | if (flash_init(&_flash)) { |
AnnaBridge | 189:f392fc9709a3 | 67 | ret = -1; |
AnnaBridge | 189:f392fc9709a3 | 68 | } |
<> | 160:d5399cc887bb | 69 | } |
AnnaBridge | 184:08ed48f1de7f | 70 | uint32_t page_size = get_page_size(); |
AnnaBridge | 184:08ed48f1de7f | 71 | _page_buf = new uint8_t[page_size]; |
AnnaBridge | 184:08ed48f1de7f | 72 | |
<> | 160:d5399cc887bb | 73 | _mutex->unlock(); |
<> | 160:d5399cc887bb | 74 | return ret; |
<> | 160:d5399cc887bb | 75 | } |
<> | 160:d5399cc887bb | 76 | |
<> | 160:d5399cc887bb | 77 | int FlashIAP::deinit() |
<> | 160:d5399cc887bb | 78 | { |
<> | 160:d5399cc887bb | 79 | int ret = 0; |
<> | 160:d5399cc887bb | 80 | _mutex->lock(); |
AnnaBridge | 189:f392fc9709a3 | 81 | { |
AnnaBridge | 189:f392fc9709a3 | 82 | ScopedRamExecutionLock make_ram_executable; |
AnnaBridge | 189:f392fc9709a3 | 83 | ScopedRomWriteLock make_rom_writable; |
AnnaBridge | 189:f392fc9709a3 | 84 | if (flash_free(&_flash)) { |
AnnaBridge | 189:f392fc9709a3 | 85 | ret = -1; |
AnnaBridge | 189:f392fc9709a3 | 86 | } |
<> | 160:d5399cc887bb | 87 | } |
AnnaBridge | 184:08ed48f1de7f | 88 | delete[] _page_buf; |
<> | 160:d5399cc887bb | 89 | _mutex->unlock(); |
<> | 160:d5399cc887bb | 90 | return ret; |
<> | 160:d5399cc887bb | 91 | } |
<> | 160:d5399cc887bb | 92 | |
<> | 160:d5399cc887bb | 93 | |
<> | 160:d5399cc887bb | 94 | int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size) |
<> | 160:d5399cc887bb | 95 | { |
Kojto | 169:e3b6fe271b81 | 96 | int32_t ret = -1; |
<> | 160:d5399cc887bb | 97 | _mutex->lock(); |
AnnaBridge | 189:f392fc9709a3 | 98 | { |
AnnaBridge | 189:f392fc9709a3 | 99 | ScopedRamExecutionLock make_ram_executable; |
AnnaBridge | 189:f392fc9709a3 | 100 | ScopedRomWriteLock make_rom_writable; |
AnnaBridge | 189:f392fc9709a3 | 101 | ret = flash_read(&_flash, addr, (uint8_t *) buffer, size); |
AnnaBridge | 189:f392fc9709a3 | 102 | } |
<> | 160:d5399cc887bb | 103 | _mutex->unlock(); |
Kojto | 169:e3b6fe271b81 | 104 | return ret; |
<> | 160:d5399cc887bb | 105 | } |
<> | 160:d5399cc887bb | 106 | |
<> | 160:d5399cc887bb | 107 | int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size) |
<> | 160:d5399cc887bb | 108 | { |
<> | 160:d5399cc887bb | 109 | uint32_t page_size = get_page_size(); |
AnnaBridge | 184:08ed48f1de7f | 110 | uint32_t flash_size = flash_get_size(&_flash); |
AnnaBridge | 184:08ed48f1de7f | 111 | uint32_t flash_start_addr = flash_get_start_address(&_flash); |
AnnaBridge | 184:08ed48f1de7f | 112 | uint32_t chunk, prog_size; |
AnnaBridge | 184:08ed48f1de7f | 113 | const uint8_t *buf = (uint8_t *) buffer; |
AnnaBridge | 184:08ed48f1de7f | 114 | const uint8_t *prog_buf; |
AnnaBridge | 184:08ed48f1de7f | 115 | |
AnnaBridge | 184:08ed48f1de7f | 116 | // addr should be aligned to page size |
AnnaBridge | 184:08ed48f1de7f | 117 | if (!is_aligned(addr, page_size) || (!buffer) || |
AnnaBridge | 187:0387e8f68319 | 118 | ((addr + size) > (flash_start_addr + flash_size))) { |
<> | 160:d5399cc887bb | 119 | return -1; |
<> | 160:d5399cc887bb | 120 | } |
<> | 160:d5399cc887bb | 121 | |
<> | 160:d5399cc887bb | 122 | int ret = 0; |
<> | 160:d5399cc887bb | 123 | _mutex->lock(); |
AnnaBridge | 189:f392fc9709a3 | 124 | while (size && !ret) { |
AnnaBridge | 184:08ed48f1de7f | 125 | uint32_t current_sector_size = flash_get_sector_size(&_flash, addr); |
Anna Bridge |
186:707f6e361f3e | 126 | bool unaligned_src = (((size_t) buf / sizeof(uint32_t) * sizeof(uint32_t)) != (size_t) buf); |
AnnaBridge | 184:08ed48f1de7f | 127 | chunk = std::min(current_sector_size - (addr % current_sector_size), size); |
Anna Bridge |
186:707f6e361f3e | 128 | // Need to use the internal page buffer in any of these two cases: |
Anna Bridge |
186:707f6e361f3e | 129 | // 1. Size is not page aligned |
Anna Bridge |
186:707f6e361f3e | 130 | // 2. Source buffer is not aligned to uint32_t. This is not supported by many targets (although |
Anna Bridge |
186:707f6e361f3e | 131 | // the pointer they accept is of uint8_t). |
Anna Bridge |
186:707f6e361f3e | 132 | if (unaligned_src || (chunk < page_size)) { |
Anna Bridge |
186:707f6e361f3e | 133 | chunk = std::min(chunk, page_size); |
AnnaBridge | 184:08ed48f1de7f | 134 | memcpy(_page_buf, buf, chunk); |
Anna Bridge |
186:707f6e361f3e | 135 | if (chunk < page_size) { |
Anna Bridge |
186:707f6e361f3e | 136 | memset(_page_buf + chunk, 0xFF, page_size - chunk); |
Anna Bridge |
186:707f6e361f3e | 137 | } |
AnnaBridge | 184:08ed48f1de7f | 138 | prog_buf = _page_buf; |
AnnaBridge | 184:08ed48f1de7f | 139 | prog_size = page_size; |
AnnaBridge | 184:08ed48f1de7f | 140 | } else { |
AnnaBridge | 184:08ed48f1de7f | 141 | chunk = chunk / page_size * page_size; |
AnnaBridge | 184:08ed48f1de7f | 142 | prog_buf = buf; |
AnnaBridge | 184:08ed48f1de7f | 143 | prog_size = chunk; |
AnnaBridge | 184:08ed48f1de7f | 144 | } |
AnnaBridge | 189:f392fc9709a3 | 145 | { |
AnnaBridge | 189:f392fc9709a3 | 146 | // Few boards may fail the write actions due to HW limitations (like critical drivers that |
AnnaBridge | 189:f392fc9709a3 | 147 | // disable flash operations). Just retry a few times until success. |
AnnaBridge | 189:f392fc9709a3 | 148 | for (unsigned int retry = 0; retry < num_write_retries; retry++) { |
AnnaBridge | 189:f392fc9709a3 | 149 | ScopedRamExecutionLock make_ram_executable; |
AnnaBridge | 189:f392fc9709a3 | 150 | ScopedRomWriteLock make_rom_writable; |
AnnaBridge | 189:f392fc9709a3 | 151 | ret = flash_program_page(&_flash, addr, prog_buf, prog_size); |
AnnaBridge | 189:f392fc9709a3 | 152 | if (ret) { |
AnnaBridge | 189:f392fc9709a3 | 153 | ret = -1; |
AnnaBridge | 189:f392fc9709a3 | 154 | } else { |
AnnaBridge | 189:f392fc9709a3 | 155 | break; |
AnnaBridge | 189:f392fc9709a3 | 156 | } |
AnnaBridge | 189:f392fc9709a3 | 157 | } |
AnnaBridge | 184:08ed48f1de7f | 158 | } |
AnnaBridge | 184:08ed48f1de7f | 159 | size -= chunk; |
AnnaBridge | 184:08ed48f1de7f | 160 | addr += chunk; |
AnnaBridge | 184:08ed48f1de7f | 161 | buf += chunk; |
<> | 160:d5399cc887bb | 162 | } |
<> | 160:d5399cc887bb | 163 | _mutex->unlock(); |
AnnaBridge | 184:08ed48f1de7f | 164 | |
<> | 160:d5399cc887bb | 165 | return ret; |
<> | 160:d5399cc887bb | 166 | } |
<> | 160:d5399cc887bb | 167 | |
<> | 160:d5399cc887bb | 168 | bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size) |
<> | 160:d5399cc887bb | 169 | { |
<> | 160:d5399cc887bb | 170 | uint32_t current_sector_size = flash_get_sector_size(&_flash, addr); |
<> | 160:d5399cc887bb | 171 | if (!is_aligned(size, current_sector_size) || |
AnnaBridge | 187:0387e8f68319 | 172 | !is_aligned(addr, current_sector_size)) { |
<> | 160:d5399cc887bb | 173 | return false; |
<> | 160:d5399cc887bb | 174 | } else { |
<> | 160:d5399cc887bb | 175 | return true; |
<> | 160:d5399cc887bb | 176 | } |
<> | 160:d5399cc887bb | 177 | } |
<> | 160:d5399cc887bb | 178 | |
<> | 160:d5399cc887bb | 179 | int FlashIAP::erase(uint32_t addr, uint32_t size) |
<> | 160:d5399cc887bb | 180 | { |
AnnaBridge | 184:08ed48f1de7f | 181 | uint32_t current_sector_size; |
AnnaBridge | 184:08ed48f1de7f | 182 | uint32_t flash_size = flash_get_size(&_flash); |
AnnaBridge | 184:08ed48f1de7f | 183 | uint32_t flash_start_addr = flash_get_start_address(&_flash); |
AnnaBridge | 184:08ed48f1de7f | 184 | uint32_t flash_end_addr = flash_start_addr + flash_size; |
AnnaBridge | 184:08ed48f1de7f | 185 | uint32_t erase_end_addr = addr + size; |
<> | 160:d5399cc887bb | 186 | |
AnnaBridge | 184:08ed48f1de7f | 187 | if (erase_end_addr > flash_end_addr) { |
<> | 160:d5399cc887bb | 188 | return -1; |
AnnaBridge | 187:0387e8f68319 | 189 | } else if (erase_end_addr < flash_end_addr) { |
AnnaBridge | 184:08ed48f1de7f | 190 | uint32_t following_sector_size = flash_get_sector_size(&_flash, erase_end_addr); |
AnnaBridge | 184:08ed48f1de7f | 191 | if (!is_aligned(erase_end_addr, following_sector_size)) { |
AnnaBridge | 184:08ed48f1de7f | 192 | return -1; |
AnnaBridge | 184:08ed48f1de7f | 193 | } |
<> | 160:d5399cc887bb | 194 | } |
<> | 160:d5399cc887bb | 195 | |
<> | 160:d5399cc887bb | 196 | int32_t ret = 0; |
<> | 160:d5399cc887bb | 197 | _mutex->lock(); |
AnnaBridge | 189:f392fc9709a3 | 198 | while (size && !ret) { |
AnnaBridge | 189:f392fc9709a3 | 199 | // Few boards may fail the erase actions due to HW limitations (like critical drivers that |
AnnaBridge | 189:f392fc9709a3 | 200 | // disable flash operations). Just retry a few times until success. |
AnnaBridge | 189:f392fc9709a3 | 201 | for (unsigned int retry = 0; retry < num_write_retries; retry++) { |
AnnaBridge | 189:f392fc9709a3 | 202 | ScopedRamExecutionLock make_ram_executable; |
AnnaBridge | 189:f392fc9709a3 | 203 | ScopedRomWriteLock make_rom_writable; |
AnnaBridge | 189:f392fc9709a3 | 204 | ret = flash_erase_sector(&_flash, addr); |
AnnaBridge | 189:f392fc9709a3 | 205 | if (ret) { |
AnnaBridge | 189:f392fc9709a3 | 206 | ret = -1; |
AnnaBridge | 189:f392fc9709a3 | 207 | } else { |
AnnaBridge | 189:f392fc9709a3 | 208 | break; |
AnnaBridge | 189:f392fc9709a3 | 209 | } |
<> | 160:d5399cc887bb | 210 | } |
<> | 160:d5399cc887bb | 211 | current_sector_size = flash_get_sector_size(&_flash, addr); |
<> | 160:d5399cc887bb | 212 | size -= current_sector_size; |
<> | 160:d5399cc887bb | 213 | addr += current_sector_size; |
<> | 160:d5399cc887bb | 214 | } |
<> | 160:d5399cc887bb | 215 | _mutex->unlock(); |
<> | 160:d5399cc887bb | 216 | return ret; |
<> | 160:d5399cc887bb | 217 | } |
<> | 160:d5399cc887bb | 218 | |
<> | 160:d5399cc887bb | 219 | uint32_t FlashIAP::get_page_size() const |
<> | 160:d5399cc887bb | 220 | { |
<> | 160:d5399cc887bb | 221 | return flash_get_page_size(&_flash); |
<> | 160:d5399cc887bb | 222 | } |
<> | 160:d5399cc887bb | 223 | |
<> | 160:d5399cc887bb | 224 | uint32_t FlashIAP::get_sector_size(uint32_t addr) const |
<> | 160:d5399cc887bb | 225 | { |
<> | 160:d5399cc887bb | 226 | return flash_get_sector_size(&_flash, addr); |
<> | 160:d5399cc887bb | 227 | } |
<> | 160:d5399cc887bb | 228 | |
<> | 160:d5399cc887bb | 229 | uint32_t FlashIAP::get_flash_start() const |
<> | 160:d5399cc887bb | 230 | { |
<> | 160:d5399cc887bb | 231 | return flash_get_start_address(&_flash); |
<> | 160:d5399cc887bb | 232 | } |
<> | 160:d5399cc887bb | 233 | |
<> | 160:d5399cc887bb | 234 | uint32_t FlashIAP::get_flash_size() const |
<> | 160:d5399cc887bb | 235 | { |
<> | 160:d5399cc887bb | 236 | return flash_get_size(&_flash); |
<> | 160:d5399cc887bb | 237 | } |
<> | 160:d5399cc887bb | 238 | |
AnnaBridge | 189:f392fc9709a3 | 239 | uint8_t FlashIAP::get_erase_value() const |
AnnaBridge | 189:f392fc9709a3 | 240 | { |
AnnaBridge | 189:f392fc9709a3 | 241 | return flash_get_erase_value(&_flash); |
AnnaBridge | 189:f392fc9709a3 | 242 | } |
AnnaBridge | 189:f392fc9709a3 | 243 | |
<> | 160:d5399cc887bb | 244 | } |
<> | 160:d5399cc887bb | 245 | |
<> | 160:d5399cc887bb | 246 | #endif |