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