Opencv 3.1 project on GR-PEACH board
Fork of gr-peach-opencv-project by
features/filesystem/bd/ChainingBlockDevice.cpp@170:54ff26da7eb6, 2017-07-04 (annotated)
- Committer:
- thedo
- Date:
- Tue Jul 04 06:23:13 2017 +0000
- Revision:
- 170:54ff26da7eb6
- Parent:
- 166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.
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 "ChainingBlockDevice.h" |
thedo | 166:3a9487d57a5c | 18 | |
thedo | 166:3a9487d57a5c | 19 | |
thedo | 166:3a9487d57a5c | 20 | ChainingBlockDevice::ChainingBlockDevice(BlockDevice **bds, size_t bd_count) |
thedo | 166:3a9487d57a5c | 21 | : _bds(bds), _bd_count(bd_count) |
thedo | 166:3a9487d57a5c | 22 | , _read_size(0), _program_size(0), _erase_size(0), _size(0) |
thedo | 166:3a9487d57a5c | 23 | { |
thedo | 166:3a9487d57a5c | 24 | } |
thedo | 166:3a9487d57a5c | 25 | |
thedo | 166:3a9487d57a5c | 26 | static bool is_aligned(uint64_t x, uint64_t alignment) |
thedo | 166:3a9487d57a5c | 27 | { |
thedo | 166:3a9487d57a5c | 28 | return (x / alignment) * alignment == x; |
thedo | 166:3a9487d57a5c | 29 | } |
thedo | 166:3a9487d57a5c | 30 | |
thedo | 166:3a9487d57a5c | 31 | int ChainingBlockDevice::init() |
thedo | 166:3a9487d57a5c | 32 | { |
thedo | 166:3a9487d57a5c | 33 | _read_size = 0; |
thedo | 166:3a9487d57a5c | 34 | _program_size = 0; |
thedo | 166:3a9487d57a5c | 35 | _erase_size = 0; |
thedo | 166:3a9487d57a5c | 36 | _size = 0; |
thedo | 166:3a9487d57a5c | 37 | |
thedo | 166:3a9487d57a5c | 38 | // Initialize children block devices, find all sizes and |
thedo | 166:3a9487d57a5c | 39 | // assert that block sizes are similar. We can't do this in |
thedo | 166:3a9487d57a5c | 40 | // the constructor since some block devices may need to be |
thedo | 166:3a9487d57a5c | 41 | // initialized before they know their block size/count |
thedo | 166:3a9487d57a5c | 42 | for (size_t i = 0; i < _bd_count; i++) { |
thedo | 166:3a9487d57a5c | 43 | int err = _bds[i]->init(); |
thedo | 166:3a9487d57a5c | 44 | if (err) { |
thedo | 166:3a9487d57a5c | 45 | return err; |
thedo | 166:3a9487d57a5c | 46 | } |
thedo | 166:3a9487d57a5c | 47 | |
thedo | 166:3a9487d57a5c | 48 | bd_size_t read = _bds[i]->get_read_size(); |
thedo | 166:3a9487d57a5c | 49 | if (i == 0 || (read >= _read_size && is_aligned(read, _read_size))) { |
thedo | 166:3a9487d57a5c | 50 | _read_size = read; |
thedo | 166:3a9487d57a5c | 51 | } else { |
thedo | 166:3a9487d57a5c | 52 | MBED_ASSERT(_read_size > read && is_aligned(_read_size, read)); |
thedo | 166:3a9487d57a5c | 53 | } |
thedo | 166:3a9487d57a5c | 54 | |
thedo | 166:3a9487d57a5c | 55 | bd_size_t program = _bds[i]->get_program_size(); |
thedo | 166:3a9487d57a5c | 56 | if (i == 0 || (program >= _program_size && is_aligned(program, _program_size))) { |
thedo | 166:3a9487d57a5c | 57 | _program_size = program; |
thedo | 166:3a9487d57a5c | 58 | } else { |
thedo | 166:3a9487d57a5c | 59 | MBED_ASSERT(_program_size > program && is_aligned(_program_size, program)); |
thedo | 166:3a9487d57a5c | 60 | } |
thedo | 166:3a9487d57a5c | 61 | |
thedo | 166:3a9487d57a5c | 62 | bd_size_t erase = _bds[i]->get_erase_size(); |
thedo | 166:3a9487d57a5c | 63 | if (i == 0 || (erase >= _erase_size && is_aligned(erase, _erase_size))) { |
thedo | 166:3a9487d57a5c | 64 | _erase_size = erase; |
thedo | 166:3a9487d57a5c | 65 | } else { |
thedo | 166:3a9487d57a5c | 66 | MBED_ASSERT(_erase_size > erase && is_aligned(_erase_size, erase)); |
thedo | 166:3a9487d57a5c | 67 | } |
thedo | 166:3a9487d57a5c | 68 | |
thedo | 166:3a9487d57a5c | 69 | _size += _bds[i]->size(); |
thedo | 166:3a9487d57a5c | 70 | } |
thedo | 166:3a9487d57a5c | 71 | |
thedo | 166:3a9487d57a5c | 72 | return 0; |
thedo | 166:3a9487d57a5c | 73 | } |
thedo | 166:3a9487d57a5c | 74 | |
thedo | 166:3a9487d57a5c | 75 | int ChainingBlockDevice::deinit() |
thedo | 166:3a9487d57a5c | 76 | { |
thedo | 166:3a9487d57a5c | 77 | for (size_t i = 0; i < _bd_count; i++) { |
thedo | 166:3a9487d57a5c | 78 | int err = _bds[i]->deinit(); |
thedo | 166:3a9487d57a5c | 79 | if (err) { |
thedo | 166:3a9487d57a5c | 80 | return err; |
thedo | 166:3a9487d57a5c | 81 | } |
thedo | 166:3a9487d57a5c | 82 | } |
thedo | 166:3a9487d57a5c | 83 | |
thedo | 166:3a9487d57a5c | 84 | return 0; |
thedo | 166:3a9487d57a5c | 85 | } |
thedo | 166:3a9487d57a5c | 86 | |
thedo | 166:3a9487d57a5c | 87 | int ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) |
thedo | 166:3a9487d57a5c | 88 | { |
thedo | 166:3a9487d57a5c | 89 | MBED_ASSERT(is_valid_read(addr, size)); |
thedo | 166:3a9487d57a5c | 90 | uint8_t *buffer = static_cast<uint8_t*>(b); |
thedo | 166:3a9487d57a5c | 91 | |
thedo | 166:3a9487d57a5c | 92 | // Find block devices containing blocks, may span multiple block devices |
thedo | 166:3a9487d57a5c | 93 | for (size_t i = 0; i < _bd_count && size > 0; i++) { |
thedo | 166:3a9487d57a5c | 94 | bd_size_t bdsize = _bds[i]->size(); |
thedo | 166:3a9487d57a5c | 95 | |
thedo | 166:3a9487d57a5c | 96 | if (addr < bdsize) { |
thedo | 166:3a9487d57a5c | 97 | bd_size_t read = size; |
thedo | 166:3a9487d57a5c | 98 | if (addr + read > bdsize) { |
thedo | 166:3a9487d57a5c | 99 | read = bdsize - addr; |
thedo | 166:3a9487d57a5c | 100 | } |
thedo | 166:3a9487d57a5c | 101 | |
thedo | 166:3a9487d57a5c | 102 | int err = _bds[i]->read(buffer, addr, read); |
thedo | 166:3a9487d57a5c | 103 | if (err) { |
thedo | 166:3a9487d57a5c | 104 | return err; |
thedo | 166:3a9487d57a5c | 105 | } |
thedo | 166:3a9487d57a5c | 106 | |
thedo | 166:3a9487d57a5c | 107 | buffer += read; |
thedo | 166:3a9487d57a5c | 108 | addr += read; |
thedo | 166:3a9487d57a5c | 109 | size -= read; |
thedo | 166:3a9487d57a5c | 110 | } |
thedo | 166:3a9487d57a5c | 111 | |
thedo | 166:3a9487d57a5c | 112 | addr -= size; |
thedo | 166:3a9487d57a5c | 113 | } |
thedo | 166:3a9487d57a5c | 114 | |
thedo | 166:3a9487d57a5c | 115 | return 0; |
thedo | 166:3a9487d57a5c | 116 | } |
thedo | 166:3a9487d57a5c | 117 | |
thedo | 166:3a9487d57a5c | 118 | int ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) |
thedo | 166:3a9487d57a5c | 119 | { |
thedo | 166:3a9487d57a5c | 120 | MBED_ASSERT(is_valid_program(addr, size)); |
thedo | 166:3a9487d57a5c | 121 | const uint8_t *buffer = static_cast<const uint8_t*>(b); |
thedo | 166:3a9487d57a5c | 122 | |
thedo | 166:3a9487d57a5c | 123 | // Find block devices containing blocks, may span multiple block devices |
thedo | 166:3a9487d57a5c | 124 | for (size_t i = 0; i < _bd_count && size > 0; i++) { |
thedo | 166:3a9487d57a5c | 125 | bd_size_t bdsize = _bds[i]->size(); |
thedo | 166:3a9487d57a5c | 126 | |
thedo | 166:3a9487d57a5c | 127 | if (addr < bdsize) { |
thedo | 166:3a9487d57a5c | 128 | bd_size_t program = size; |
thedo | 166:3a9487d57a5c | 129 | if (addr + program > bdsize) { |
thedo | 166:3a9487d57a5c | 130 | program = bdsize - addr; |
thedo | 166:3a9487d57a5c | 131 | } |
thedo | 166:3a9487d57a5c | 132 | |
thedo | 166:3a9487d57a5c | 133 | int err = _bds[i]->program(buffer, addr, program); |
thedo | 166:3a9487d57a5c | 134 | if (err) { |
thedo | 166:3a9487d57a5c | 135 | return err; |
thedo | 166:3a9487d57a5c | 136 | } |
thedo | 166:3a9487d57a5c | 137 | |
thedo | 166:3a9487d57a5c | 138 | buffer += program; |
thedo | 166:3a9487d57a5c | 139 | addr += program; |
thedo | 166:3a9487d57a5c | 140 | size -= program; |
thedo | 166:3a9487d57a5c | 141 | } |
thedo | 166:3a9487d57a5c | 142 | |
thedo | 166:3a9487d57a5c | 143 | addr -= size; |
thedo | 166:3a9487d57a5c | 144 | } |
thedo | 166:3a9487d57a5c | 145 | |
thedo | 166:3a9487d57a5c | 146 | return 0; |
thedo | 166:3a9487d57a5c | 147 | } |
thedo | 166:3a9487d57a5c | 148 | |
thedo | 166:3a9487d57a5c | 149 | int ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size) |
thedo | 166:3a9487d57a5c | 150 | { |
thedo | 166:3a9487d57a5c | 151 | MBED_ASSERT(is_valid_erase(addr, size)); |
thedo | 166:3a9487d57a5c | 152 | |
thedo | 166:3a9487d57a5c | 153 | // Find block devices containing blocks, may span multiple block devices |
thedo | 166:3a9487d57a5c | 154 | for (size_t i = 0; i < _bd_count && size > 0; i++) { |
thedo | 166:3a9487d57a5c | 155 | bd_size_t bdsize = _bds[i]->size(); |
thedo | 166:3a9487d57a5c | 156 | |
thedo | 166:3a9487d57a5c | 157 | if (addr < bdsize) { |
thedo | 166:3a9487d57a5c | 158 | bd_size_t erase = size; |
thedo | 166:3a9487d57a5c | 159 | if (addr + erase > bdsize) { |
thedo | 166:3a9487d57a5c | 160 | erase = bdsize - addr; |
thedo | 166:3a9487d57a5c | 161 | } |
thedo | 166:3a9487d57a5c | 162 | |
thedo | 166:3a9487d57a5c | 163 | int err = _bds[i]->erase(addr, erase); |
thedo | 166:3a9487d57a5c | 164 | if (err) { |
thedo | 166:3a9487d57a5c | 165 | return err; |
thedo | 166:3a9487d57a5c | 166 | } |
thedo | 166:3a9487d57a5c | 167 | |
thedo | 166:3a9487d57a5c | 168 | addr += erase; |
thedo | 166:3a9487d57a5c | 169 | size -= erase; |
thedo | 166:3a9487d57a5c | 170 | } |
thedo | 166:3a9487d57a5c | 171 | |
thedo | 166:3a9487d57a5c | 172 | addr -= size; |
thedo | 166:3a9487d57a5c | 173 | } |
thedo | 166:3a9487d57a5c | 174 | |
thedo | 166:3a9487d57a5c | 175 | return 0; |
thedo | 166:3a9487d57a5c | 176 | } |
thedo | 166:3a9487d57a5c | 177 | |
thedo | 166:3a9487d57a5c | 178 | bd_size_t ChainingBlockDevice::get_read_size() const |
thedo | 166:3a9487d57a5c | 179 | { |
thedo | 166:3a9487d57a5c | 180 | return _read_size; |
thedo | 166:3a9487d57a5c | 181 | } |
thedo | 166:3a9487d57a5c | 182 | |
thedo | 166:3a9487d57a5c | 183 | bd_size_t ChainingBlockDevice::get_program_size() const |
thedo | 166:3a9487d57a5c | 184 | { |
thedo | 166:3a9487d57a5c | 185 | return _program_size; |
thedo | 166:3a9487d57a5c | 186 | } |
thedo | 166:3a9487d57a5c | 187 | |
thedo | 166:3a9487d57a5c | 188 | bd_size_t ChainingBlockDevice::get_erase_size() const |
thedo | 166:3a9487d57a5c | 189 | { |
thedo | 166:3a9487d57a5c | 190 | return _erase_size; |
thedo | 166:3a9487d57a5c | 191 | } |
thedo | 166:3a9487d57a5c | 192 | |
thedo | 166:3a9487d57a5c | 193 | bd_size_t ChainingBlockDevice::size() const |
thedo | 166:3a9487d57a5c | 194 | { |
thedo | 166:3a9487d57a5c | 195 | return _size; |
thedo | 166:3a9487d57a5c | 196 | } |
thedo | 166:3a9487d57a5c | 197 |