BA
/
BaBoRo_test2
Backup 1
mbed-os/drivers/FlashIAP.cpp@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* mbed Microcontroller Library |
borlanic | 0:02dd72d1d465 | 2 | * Copyright (c) 2017 ARM Limited |
borlanic | 0:02dd72d1d465 | 3 | * |
borlanic | 0:02dd72d1d465 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
borlanic | 0:02dd72d1d465 | 5 | * of this software and associated documentation files (the "Software"), to deal |
borlanic | 0:02dd72d1d465 | 6 | * in the Software without restriction, including without limitation the rights |
borlanic | 0:02dd72d1d465 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
borlanic | 0:02dd72d1d465 | 8 | * copies of the Software, and to permit persons to whom the Software is |
borlanic | 0:02dd72d1d465 | 9 | * furnished to do so, subject to the following conditions: |
borlanic | 0:02dd72d1d465 | 10 | * |
borlanic | 0:02dd72d1d465 | 11 | * The above copyright notice and this permission notice shall be included in |
borlanic | 0:02dd72d1d465 | 12 | * all copies or substantial portions of the Software. |
borlanic | 0:02dd72d1d465 | 13 | * |
borlanic | 0:02dd72d1d465 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
borlanic | 0:02dd72d1d465 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
borlanic | 0:02dd72d1d465 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
borlanic | 0:02dd72d1d465 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
borlanic | 0:02dd72d1d465 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
borlanic | 0:02dd72d1d465 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
borlanic | 0:02dd72d1d465 | 20 | * SOFTWARE. |
borlanic | 0:02dd72d1d465 | 21 | */ |
borlanic | 0:02dd72d1d465 | 22 | |
borlanic | 0:02dd72d1d465 | 23 | #include <stdio.h> |
borlanic | 0:02dd72d1d465 | 24 | #include <string.h> |
borlanic | 0:02dd72d1d465 | 25 | #include <algorithm> |
borlanic | 0:02dd72d1d465 | 26 | #include "FlashIAP.h" |
borlanic | 0:02dd72d1d465 | 27 | #include "mbed_assert.h" |
borlanic | 0:02dd72d1d465 | 28 | |
borlanic | 0:02dd72d1d465 | 29 | |
borlanic | 0:02dd72d1d465 | 30 | #ifdef DEVICE_FLASH |
borlanic | 0:02dd72d1d465 | 31 | |
borlanic | 0:02dd72d1d465 | 32 | namespace mbed { |
borlanic | 0:02dd72d1d465 | 33 | |
borlanic | 0:02dd72d1d465 | 34 | SingletonPtr<PlatformMutex> FlashIAP::_mutex; |
borlanic | 0:02dd72d1d465 | 35 | |
borlanic | 0:02dd72d1d465 | 36 | static inline bool is_aligned(uint32_t number, uint32_t alignment) |
borlanic | 0:02dd72d1d465 | 37 | { |
borlanic | 0:02dd72d1d465 | 38 | if ((number % alignment) != 0) { |
borlanic | 0:02dd72d1d465 | 39 | return false; |
borlanic | 0:02dd72d1d465 | 40 | } else { |
borlanic | 0:02dd72d1d465 | 41 | return true; |
borlanic | 0:02dd72d1d465 | 42 | } |
borlanic | 0:02dd72d1d465 | 43 | } |
borlanic | 0:02dd72d1d465 | 44 | |
borlanic | 0:02dd72d1d465 | 45 | FlashIAP::FlashIAP() |
borlanic | 0:02dd72d1d465 | 46 | { |
borlanic | 0:02dd72d1d465 | 47 | |
borlanic | 0:02dd72d1d465 | 48 | } |
borlanic | 0:02dd72d1d465 | 49 | |
borlanic | 0:02dd72d1d465 | 50 | FlashIAP::~FlashIAP() |
borlanic | 0:02dd72d1d465 | 51 | { |
borlanic | 0:02dd72d1d465 | 52 | |
borlanic | 0:02dd72d1d465 | 53 | } |
borlanic | 0:02dd72d1d465 | 54 | |
borlanic | 0:02dd72d1d465 | 55 | int FlashIAP::init() |
borlanic | 0:02dd72d1d465 | 56 | { |
borlanic | 0:02dd72d1d465 | 57 | int ret = 0; |
borlanic | 0:02dd72d1d465 | 58 | _mutex->lock(); |
borlanic | 0:02dd72d1d465 | 59 | if (flash_init(&_flash)) { |
borlanic | 0:02dd72d1d465 | 60 | ret = -1; |
borlanic | 0:02dd72d1d465 | 61 | } |
borlanic | 0:02dd72d1d465 | 62 | uint32_t page_size = get_page_size(); |
borlanic | 0:02dd72d1d465 | 63 | _page_buf = new uint8_t[page_size]; |
borlanic | 0:02dd72d1d465 | 64 | |
borlanic | 0:02dd72d1d465 | 65 | _mutex->unlock(); |
borlanic | 0:02dd72d1d465 | 66 | return ret; |
borlanic | 0:02dd72d1d465 | 67 | } |
borlanic | 0:02dd72d1d465 | 68 | |
borlanic | 0:02dd72d1d465 | 69 | int FlashIAP::deinit() |
borlanic | 0:02dd72d1d465 | 70 | { |
borlanic | 0:02dd72d1d465 | 71 | int ret = 0; |
borlanic | 0:02dd72d1d465 | 72 | _mutex->lock(); |
borlanic | 0:02dd72d1d465 | 73 | if (flash_free(&_flash)) { |
borlanic | 0:02dd72d1d465 | 74 | ret = -1; |
borlanic | 0:02dd72d1d465 | 75 | } |
borlanic | 0:02dd72d1d465 | 76 | delete[] _page_buf; |
borlanic | 0:02dd72d1d465 | 77 | _mutex->unlock(); |
borlanic | 0:02dd72d1d465 | 78 | return ret; |
borlanic | 0:02dd72d1d465 | 79 | } |
borlanic | 0:02dd72d1d465 | 80 | |
borlanic | 0:02dd72d1d465 | 81 | |
borlanic | 0:02dd72d1d465 | 82 | int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size) |
borlanic | 0:02dd72d1d465 | 83 | { |
borlanic | 0:02dd72d1d465 | 84 | int32_t ret = -1; |
borlanic | 0:02dd72d1d465 | 85 | _mutex->lock(); |
borlanic | 0:02dd72d1d465 | 86 | ret = flash_read(&_flash, addr, (uint8_t *) buffer, size); |
borlanic | 0:02dd72d1d465 | 87 | _mutex->unlock(); |
borlanic | 0:02dd72d1d465 | 88 | return ret; |
borlanic | 0:02dd72d1d465 | 89 | } |
borlanic | 0:02dd72d1d465 | 90 | |
borlanic | 0:02dd72d1d465 | 91 | int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size) |
borlanic | 0:02dd72d1d465 | 92 | { |
borlanic | 0:02dd72d1d465 | 93 | uint32_t page_size = get_page_size(); |
borlanic | 0:02dd72d1d465 | 94 | uint32_t flash_size = flash_get_size(&_flash); |
borlanic | 0:02dd72d1d465 | 95 | uint32_t flash_start_addr = flash_get_start_address(&_flash); |
borlanic | 0:02dd72d1d465 | 96 | uint32_t chunk, prog_size; |
borlanic | 0:02dd72d1d465 | 97 | const uint8_t *buf = (uint8_t *) buffer; |
borlanic | 0:02dd72d1d465 | 98 | const uint8_t *prog_buf; |
borlanic | 0:02dd72d1d465 | 99 | |
borlanic | 0:02dd72d1d465 | 100 | // addr should be aligned to page size |
borlanic | 0:02dd72d1d465 | 101 | if (!is_aligned(addr, page_size) || (!buffer) || |
borlanic | 0:02dd72d1d465 | 102 | ((addr + size) > (flash_start_addr + flash_size))) { |
borlanic | 0:02dd72d1d465 | 103 | return -1; |
borlanic | 0:02dd72d1d465 | 104 | } |
borlanic | 0:02dd72d1d465 | 105 | |
borlanic | 0:02dd72d1d465 | 106 | int ret = 0; |
borlanic | 0:02dd72d1d465 | 107 | _mutex->lock(); |
borlanic | 0:02dd72d1d465 | 108 | while (size) { |
borlanic | 0:02dd72d1d465 | 109 | uint32_t current_sector_size = flash_get_sector_size(&_flash, addr); |
borlanic | 0:02dd72d1d465 | 110 | chunk = std::min(current_sector_size - (addr % current_sector_size), size); |
borlanic | 0:02dd72d1d465 | 111 | if (chunk < page_size) { |
borlanic | 0:02dd72d1d465 | 112 | memcpy(_page_buf, buf, chunk); |
borlanic | 0:02dd72d1d465 | 113 | memset(_page_buf + chunk, 0xFF, page_size - chunk); |
borlanic | 0:02dd72d1d465 | 114 | prog_buf = _page_buf; |
borlanic | 0:02dd72d1d465 | 115 | prog_size = page_size; |
borlanic | 0:02dd72d1d465 | 116 | } else { |
borlanic | 0:02dd72d1d465 | 117 | chunk = chunk / page_size * page_size; |
borlanic | 0:02dd72d1d465 | 118 | prog_buf = buf; |
borlanic | 0:02dd72d1d465 | 119 | prog_size = chunk; |
borlanic | 0:02dd72d1d465 | 120 | } |
borlanic | 0:02dd72d1d465 | 121 | if (flash_program_page(&_flash, addr, prog_buf, prog_size)) { |
borlanic | 0:02dd72d1d465 | 122 | ret = -1; |
borlanic | 0:02dd72d1d465 | 123 | break; |
borlanic | 0:02dd72d1d465 | 124 | } |
borlanic | 0:02dd72d1d465 | 125 | size -= chunk; |
borlanic | 0:02dd72d1d465 | 126 | addr += chunk; |
borlanic | 0:02dd72d1d465 | 127 | buf += chunk; |
borlanic | 0:02dd72d1d465 | 128 | } |
borlanic | 0:02dd72d1d465 | 129 | _mutex->unlock(); |
borlanic | 0:02dd72d1d465 | 130 | |
borlanic | 0:02dd72d1d465 | 131 | return ret; |
borlanic | 0:02dd72d1d465 | 132 | } |
borlanic | 0:02dd72d1d465 | 133 | |
borlanic | 0:02dd72d1d465 | 134 | bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size) |
borlanic | 0:02dd72d1d465 | 135 | { |
borlanic | 0:02dd72d1d465 | 136 | uint32_t current_sector_size = flash_get_sector_size(&_flash, addr); |
borlanic | 0:02dd72d1d465 | 137 | if (!is_aligned(size, current_sector_size) || |
borlanic | 0:02dd72d1d465 | 138 | !is_aligned(addr, current_sector_size)) { |
borlanic | 0:02dd72d1d465 | 139 | return false; |
borlanic | 0:02dd72d1d465 | 140 | } else { |
borlanic | 0:02dd72d1d465 | 141 | return true; |
borlanic | 0:02dd72d1d465 | 142 | } |
borlanic | 0:02dd72d1d465 | 143 | } |
borlanic | 0:02dd72d1d465 | 144 | |
borlanic | 0:02dd72d1d465 | 145 | int FlashIAP::erase(uint32_t addr, uint32_t size) |
borlanic | 0:02dd72d1d465 | 146 | { |
borlanic | 0:02dd72d1d465 | 147 | uint32_t current_sector_size; |
borlanic | 0:02dd72d1d465 | 148 | uint32_t flash_size = flash_get_size(&_flash); |
borlanic | 0:02dd72d1d465 | 149 | uint32_t flash_start_addr = flash_get_start_address(&_flash); |
borlanic | 0:02dd72d1d465 | 150 | uint32_t flash_end_addr = flash_start_addr + flash_size; |
borlanic | 0:02dd72d1d465 | 151 | uint32_t erase_end_addr = addr + size; |
borlanic | 0:02dd72d1d465 | 152 | |
borlanic | 0:02dd72d1d465 | 153 | if (erase_end_addr > flash_end_addr) { |
borlanic | 0:02dd72d1d465 | 154 | return -1; |
borlanic | 0:02dd72d1d465 | 155 | } else if (erase_end_addr < flash_end_addr){ |
borlanic | 0:02dd72d1d465 | 156 | uint32_t following_sector_size = flash_get_sector_size(&_flash, erase_end_addr); |
borlanic | 0:02dd72d1d465 | 157 | if (!is_aligned(erase_end_addr, following_sector_size)) { |
borlanic | 0:02dd72d1d465 | 158 | return -1; |
borlanic | 0:02dd72d1d465 | 159 | } |
borlanic | 0:02dd72d1d465 | 160 | } |
borlanic | 0:02dd72d1d465 | 161 | |
borlanic | 0:02dd72d1d465 | 162 | int32_t ret = 0; |
borlanic | 0:02dd72d1d465 | 163 | _mutex->lock(); |
borlanic | 0:02dd72d1d465 | 164 | while (size) { |
borlanic | 0:02dd72d1d465 | 165 | ret = flash_erase_sector(&_flash, addr); |
borlanic | 0:02dd72d1d465 | 166 | if (ret != 0) { |
borlanic | 0:02dd72d1d465 | 167 | ret = -1; |
borlanic | 0:02dd72d1d465 | 168 | break; |
borlanic | 0:02dd72d1d465 | 169 | } |
borlanic | 0:02dd72d1d465 | 170 | current_sector_size = flash_get_sector_size(&_flash, addr); |
borlanic | 0:02dd72d1d465 | 171 | size -= current_sector_size; |
borlanic | 0:02dd72d1d465 | 172 | addr += current_sector_size; |
borlanic | 0:02dd72d1d465 | 173 | } |
borlanic | 0:02dd72d1d465 | 174 | _mutex->unlock(); |
borlanic | 0:02dd72d1d465 | 175 | return ret; |
borlanic | 0:02dd72d1d465 | 176 | } |
borlanic | 0:02dd72d1d465 | 177 | |
borlanic | 0:02dd72d1d465 | 178 | uint32_t FlashIAP::get_page_size() const |
borlanic | 0:02dd72d1d465 | 179 | { |
borlanic | 0:02dd72d1d465 | 180 | return flash_get_page_size(&_flash); |
borlanic | 0:02dd72d1d465 | 181 | } |
borlanic | 0:02dd72d1d465 | 182 | |
borlanic | 0:02dd72d1d465 | 183 | uint32_t FlashIAP::get_sector_size(uint32_t addr) const |
borlanic | 0:02dd72d1d465 | 184 | { |
borlanic | 0:02dd72d1d465 | 185 | return flash_get_sector_size(&_flash, addr); |
borlanic | 0:02dd72d1d465 | 186 | } |
borlanic | 0:02dd72d1d465 | 187 | |
borlanic | 0:02dd72d1d465 | 188 | uint32_t FlashIAP::get_flash_start() const |
borlanic | 0:02dd72d1d465 | 189 | { |
borlanic | 0:02dd72d1d465 | 190 | return flash_get_start_address(&_flash); |
borlanic | 0:02dd72d1d465 | 191 | } |
borlanic | 0:02dd72d1d465 | 192 | |
borlanic | 0:02dd72d1d465 | 193 | uint32_t FlashIAP::get_flash_size() const |
borlanic | 0:02dd72d1d465 | 194 | { |
borlanic | 0:02dd72d1d465 | 195 | return flash_get_size(&_flash); |
borlanic | 0:02dd72d1d465 | 196 | } |
borlanic | 0:02dd72d1d465 | 197 | |
borlanic | 0:02dd72d1d465 | 198 | } |
borlanic | 0:02dd72d1d465 | 199 | |
borlanic | 0:02dd72d1d465 | 200 | #endif |