Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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