BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* mbed Microcontroller Library
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2017 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16
borlanic 0:fbdae7e6d805 17 #include "HeapBlockDevice.h"
borlanic 0:fbdae7e6d805 18
borlanic 0:fbdae7e6d805 19
borlanic 0:fbdae7e6d805 20 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block)
borlanic 0:fbdae7e6d805 21 : _read_size(block), _program_size(block), _erase_size(block)
borlanic 0:fbdae7e6d805 22 , _count(size / block), _blocks(0)
borlanic 0:fbdae7e6d805 23 {
borlanic 0:fbdae7e6d805 24 MBED_ASSERT(_count * _erase_size == size);
borlanic 0:fbdae7e6d805 25 }
borlanic 0:fbdae7e6d805 26
borlanic 0:fbdae7e6d805 27 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
borlanic 0:fbdae7e6d805 28 : _read_size(read), _program_size(program), _erase_size(erase)
borlanic 0:fbdae7e6d805 29 , _count(size / erase), _blocks(0)
borlanic 0:fbdae7e6d805 30 {
borlanic 0:fbdae7e6d805 31 MBED_ASSERT(_count * _erase_size == size);
borlanic 0:fbdae7e6d805 32 }
borlanic 0:fbdae7e6d805 33
borlanic 0:fbdae7e6d805 34 HeapBlockDevice::~HeapBlockDevice()
borlanic 0:fbdae7e6d805 35 {
borlanic 0:fbdae7e6d805 36 if (_blocks) {
borlanic 0:fbdae7e6d805 37 for (size_t i = 0; i < _count; i++) {
borlanic 0:fbdae7e6d805 38 free(_blocks[i]);
borlanic 0:fbdae7e6d805 39 }
borlanic 0:fbdae7e6d805 40
borlanic 0:fbdae7e6d805 41 delete[] _blocks;
borlanic 0:fbdae7e6d805 42 _blocks = 0;
borlanic 0:fbdae7e6d805 43 }
borlanic 0:fbdae7e6d805 44 }
borlanic 0:fbdae7e6d805 45
borlanic 0:fbdae7e6d805 46 int HeapBlockDevice::init()
borlanic 0:fbdae7e6d805 47 {
borlanic 0:fbdae7e6d805 48 if (!_blocks) {
borlanic 0:fbdae7e6d805 49 _blocks = new uint8_t*[_count];
borlanic 0:fbdae7e6d805 50 for (size_t i = 0; i < _count; i++) {
borlanic 0:fbdae7e6d805 51 _blocks[i] = 0;
borlanic 0:fbdae7e6d805 52 }
borlanic 0:fbdae7e6d805 53 }
borlanic 0:fbdae7e6d805 54
borlanic 0:fbdae7e6d805 55 return BD_ERROR_OK;
borlanic 0:fbdae7e6d805 56 }
borlanic 0:fbdae7e6d805 57
borlanic 0:fbdae7e6d805 58 int HeapBlockDevice::deinit()
borlanic 0:fbdae7e6d805 59 {
borlanic 0:fbdae7e6d805 60 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 61 // Memory is lazily cleaned up in destructor to allow
borlanic 0:fbdae7e6d805 62 // data to live across de/reinitialization
borlanic 0:fbdae7e6d805 63 return BD_ERROR_OK;
borlanic 0:fbdae7e6d805 64 }
borlanic 0:fbdae7e6d805 65
borlanic 0:fbdae7e6d805 66 bd_size_t HeapBlockDevice::get_read_size() const
borlanic 0:fbdae7e6d805 67 {
borlanic 0:fbdae7e6d805 68 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 69 return _read_size;
borlanic 0:fbdae7e6d805 70 }
borlanic 0:fbdae7e6d805 71
borlanic 0:fbdae7e6d805 72 bd_size_t HeapBlockDevice::get_program_size() const
borlanic 0:fbdae7e6d805 73 {
borlanic 0:fbdae7e6d805 74 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 75 return _program_size;
borlanic 0:fbdae7e6d805 76 }
borlanic 0:fbdae7e6d805 77
borlanic 0:fbdae7e6d805 78 bd_size_t HeapBlockDevice::get_erase_size() const
borlanic 0:fbdae7e6d805 79 {
borlanic 0:fbdae7e6d805 80 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 81 return _erase_size;
borlanic 0:fbdae7e6d805 82 }
borlanic 0:fbdae7e6d805 83
borlanic 0:fbdae7e6d805 84 bd_size_t HeapBlockDevice::get_erase_size(bd_addr_t addr) const
borlanic 0:fbdae7e6d805 85 {
borlanic 0:fbdae7e6d805 86 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 87 return _erase_size;
borlanic 0:fbdae7e6d805 88 }
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90 bd_size_t HeapBlockDevice::size() const
borlanic 0:fbdae7e6d805 91 {
borlanic 0:fbdae7e6d805 92 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 93 return _count * _erase_size;
borlanic 0:fbdae7e6d805 94 }
borlanic 0:fbdae7e6d805 95
borlanic 0:fbdae7e6d805 96 int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
borlanic 0:fbdae7e6d805 97 {
borlanic 0:fbdae7e6d805 98 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 99 MBED_ASSERT(is_valid_read(addr, size));
borlanic 0:fbdae7e6d805 100 uint8_t *buffer = static_cast<uint8_t*>(b);
borlanic 0:fbdae7e6d805 101
borlanic 0:fbdae7e6d805 102 while (size > 0) {
borlanic 0:fbdae7e6d805 103 bd_addr_t hi = addr / _erase_size;
borlanic 0:fbdae7e6d805 104 bd_addr_t lo = addr % _erase_size;
borlanic 0:fbdae7e6d805 105
borlanic 0:fbdae7e6d805 106 if (_blocks[hi]) {
borlanic 0:fbdae7e6d805 107 memcpy(buffer, &_blocks[hi][lo], _read_size);
borlanic 0:fbdae7e6d805 108 } else {
borlanic 0:fbdae7e6d805 109 memset(buffer, 0, _read_size);
borlanic 0:fbdae7e6d805 110 }
borlanic 0:fbdae7e6d805 111
borlanic 0:fbdae7e6d805 112 buffer += _read_size;
borlanic 0:fbdae7e6d805 113 addr += _read_size;
borlanic 0:fbdae7e6d805 114 size -= _read_size;
borlanic 0:fbdae7e6d805 115 }
borlanic 0:fbdae7e6d805 116
borlanic 0:fbdae7e6d805 117 return 0;
borlanic 0:fbdae7e6d805 118 }
borlanic 0:fbdae7e6d805 119
borlanic 0:fbdae7e6d805 120 int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
borlanic 0:fbdae7e6d805 121 {
borlanic 0:fbdae7e6d805 122 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 123 MBED_ASSERT(is_valid_program(addr, size));
borlanic 0:fbdae7e6d805 124 const uint8_t *buffer = static_cast<const uint8_t*>(b);
borlanic 0:fbdae7e6d805 125
borlanic 0:fbdae7e6d805 126 while (size > 0) {
borlanic 0:fbdae7e6d805 127 bd_addr_t hi = addr / _erase_size;
borlanic 0:fbdae7e6d805 128 bd_addr_t lo = addr % _erase_size;
borlanic 0:fbdae7e6d805 129
borlanic 0:fbdae7e6d805 130 if (!_blocks[hi]) {
borlanic 0:fbdae7e6d805 131 _blocks[hi] = (uint8_t*)malloc(_erase_size);
borlanic 0:fbdae7e6d805 132 if (!_blocks[hi]) {
borlanic 0:fbdae7e6d805 133 return BD_ERROR_DEVICE_ERROR;
borlanic 0:fbdae7e6d805 134 }
borlanic 0:fbdae7e6d805 135 }
borlanic 0:fbdae7e6d805 136
borlanic 0:fbdae7e6d805 137 memcpy(&_blocks[hi][lo], buffer, _program_size);
borlanic 0:fbdae7e6d805 138
borlanic 0:fbdae7e6d805 139 buffer += _program_size;
borlanic 0:fbdae7e6d805 140 addr += _program_size;
borlanic 0:fbdae7e6d805 141 size -= _program_size;
borlanic 0:fbdae7e6d805 142 }
borlanic 0:fbdae7e6d805 143
borlanic 0:fbdae7e6d805 144 return 0;
borlanic 0:fbdae7e6d805 145 }
borlanic 0:fbdae7e6d805 146
borlanic 0:fbdae7e6d805 147 int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
borlanic 0:fbdae7e6d805 148 {
borlanic 0:fbdae7e6d805 149 MBED_ASSERT(_blocks != NULL);
borlanic 0:fbdae7e6d805 150 MBED_ASSERT(is_valid_erase(addr, size));
borlanic 0:fbdae7e6d805 151 // TODO assert on programming unerased blocks
borlanic 0:fbdae7e6d805 152
borlanic 0:fbdae7e6d805 153 return 0;
borlanic 0:fbdae7e6d805 154 }
borlanic 0:fbdae7e6d805 155