mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
features/filesystem/bd/SlicingBlockDevice.cpp@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | /* mbed Microcontroller Library |
dkato | 0:f782d9c66c49 | 2 | * Copyright (c) 2017 ARM Limited |
dkato | 0:f782d9c66c49 | 3 | * |
dkato | 0:f782d9c66c49 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:f782d9c66c49 | 5 | * you may not use this file except in compliance with the License. |
dkato | 0:f782d9c66c49 | 6 | * You may obtain a copy of the License at |
dkato | 0:f782d9c66c49 | 7 | * |
dkato | 0:f782d9c66c49 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:f782d9c66c49 | 9 | * |
dkato | 0:f782d9c66c49 | 10 | * Unless required by applicable law or agreed to in writing, software |
dkato | 0:f782d9c66c49 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:f782d9c66c49 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:f782d9c66c49 | 13 | * See the License for the specific language governing permissions and |
dkato | 0:f782d9c66c49 | 14 | * limitations under the License. |
dkato | 0:f782d9c66c49 | 15 | */ |
dkato | 0:f782d9c66c49 | 16 | |
dkato | 0:f782d9c66c49 | 17 | #include "SlicingBlockDevice.h" |
dkato | 0:f782d9c66c49 | 18 | |
dkato | 0:f782d9c66c49 | 19 | |
dkato | 0:f782d9c66c49 | 20 | SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start) |
dkato | 0:f782d9c66c49 | 21 | : _bd(bd) |
dkato | 0:f782d9c66c49 | 22 | , _start_from_end(false), _start(start) |
dkato | 0:f782d9c66c49 | 23 | , _stop_from_end(true), _stop(0) |
dkato | 0:f782d9c66c49 | 24 | { |
dkato | 0:f782d9c66c49 | 25 | if ((int64_t)_start < 0) { |
dkato | 0:f782d9c66c49 | 26 | _start_from_end = true; |
dkato | 0:f782d9c66c49 | 27 | _start = -_start; |
dkato | 0:f782d9c66c49 | 28 | } |
dkato | 0:f782d9c66c49 | 29 | } |
dkato | 0:f782d9c66c49 | 30 | |
dkato | 0:f782d9c66c49 | 31 | SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t stop) |
dkato | 0:f782d9c66c49 | 32 | : _bd(bd) |
dkato | 0:f782d9c66c49 | 33 | , _start_from_end(false), _start(start) |
dkato | 0:f782d9c66c49 | 34 | , _stop_from_end(false), _stop(stop) |
dkato | 0:f782d9c66c49 | 35 | { |
dkato | 0:f782d9c66c49 | 36 | if ((int64_t)_start < 0) { |
dkato | 0:f782d9c66c49 | 37 | _start_from_end = true; |
dkato | 0:f782d9c66c49 | 38 | _start = -_start; |
dkato | 0:f782d9c66c49 | 39 | } |
dkato | 0:f782d9c66c49 | 40 | |
dkato | 0:f782d9c66c49 | 41 | if ((int64_t)_stop < 0) { |
dkato | 0:f782d9c66c49 | 42 | _stop_from_end = true; |
dkato | 0:f782d9c66c49 | 43 | _stop = -_stop; |
dkato | 0:f782d9c66c49 | 44 | } |
dkato | 0:f782d9c66c49 | 45 | } |
dkato | 0:f782d9c66c49 | 46 | |
dkato | 0:f782d9c66c49 | 47 | int SlicingBlockDevice::init() |
dkato | 0:f782d9c66c49 | 48 | { |
dkato | 0:f782d9c66c49 | 49 | int err = _bd->init(); |
dkato | 0:f782d9c66c49 | 50 | if (err) { |
dkato | 0:f782d9c66c49 | 51 | return err; |
dkato | 0:f782d9c66c49 | 52 | } |
dkato | 0:f782d9c66c49 | 53 | |
dkato | 0:f782d9c66c49 | 54 | bd_size_t size = _bd->size(); |
dkato | 0:f782d9c66c49 | 55 | |
dkato | 0:f782d9c66c49 | 56 | // Calculate from_end values |
dkato | 0:f782d9c66c49 | 57 | if (_start_from_end) { |
dkato | 0:f782d9c66c49 | 58 | _start_from_end = false; |
dkato | 0:f782d9c66c49 | 59 | _start = size - _start; |
dkato | 0:f782d9c66c49 | 60 | } |
dkato | 0:f782d9c66c49 | 61 | |
dkato | 0:f782d9c66c49 | 62 | if (_stop_from_end) { |
dkato | 0:f782d9c66c49 | 63 | _stop_from_end = false; |
dkato | 0:f782d9c66c49 | 64 | _stop = size - _stop; |
dkato | 0:f782d9c66c49 | 65 | } |
dkato | 0:f782d9c66c49 | 66 | |
dkato | 0:f782d9c66c49 | 67 | // Check that block addresses are valid |
dkato | 0:f782d9c66c49 | 68 | MBED_ASSERT(_bd->is_valid_erase(_start, _stop - _start)); |
dkato | 0:f782d9c66c49 | 69 | |
dkato | 0:f782d9c66c49 | 70 | return 0; |
dkato | 0:f782d9c66c49 | 71 | } |
dkato | 0:f782d9c66c49 | 72 | |
dkato | 0:f782d9c66c49 | 73 | int SlicingBlockDevice::deinit() |
dkato | 0:f782d9c66c49 | 74 | { |
dkato | 0:f782d9c66c49 | 75 | return _bd->deinit(); |
dkato | 0:f782d9c66c49 | 76 | } |
dkato | 0:f782d9c66c49 | 77 | |
dkato | 0:f782d9c66c49 | 78 | int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) |
dkato | 0:f782d9c66c49 | 79 | { |
dkato | 0:f782d9c66c49 | 80 | MBED_ASSERT(is_valid_read(addr, size)); |
dkato | 0:f782d9c66c49 | 81 | return _bd->read(b, addr + _start, size); |
dkato | 0:f782d9c66c49 | 82 | } |
dkato | 0:f782d9c66c49 | 83 | |
dkato | 0:f782d9c66c49 | 84 | int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) |
dkato | 0:f782d9c66c49 | 85 | { |
dkato | 0:f782d9c66c49 | 86 | MBED_ASSERT(is_valid_program(addr, size)); |
dkato | 0:f782d9c66c49 | 87 | return _bd->program(b, addr + _start, size); |
dkato | 0:f782d9c66c49 | 88 | } |
dkato | 0:f782d9c66c49 | 89 | |
dkato | 0:f782d9c66c49 | 90 | int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size) |
dkato | 0:f782d9c66c49 | 91 | { |
dkato | 0:f782d9c66c49 | 92 | MBED_ASSERT(is_valid_erase(addr, size)); |
dkato | 0:f782d9c66c49 | 93 | return _bd->erase(addr + _start, size); |
dkato | 0:f782d9c66c49 | 94 | } |
dkato | 0:f782d9c66c49 | 95 | |
dkato | 0:f782d9c66c49 | 96 | bd_size_t SlicingBlockDevice::get_read_size() const |
dkato | 0:f782d9c66c49 | 97 | { |
dkato | 0:f782d9c66c49 | 98 | return _bd->get_read_size(); |
dkato | 0:f782d9c66c49 | 99 | } |
dkato | 0:f782d9c66c49 | 100 | |
dkato | 0:f782d9c66c49 | 101 | bd_size_t SlicingBlockDevice::get_program_size() const |
dkato | 0:f782d9c66c49 | 102 | { |
dkato | 0:f782d9c66c49 | 103 | return _bd->get_program_size(); |
dkato | 0:f782d9c66c49 | 104 | } |
dkato | 0:f782d9c66c49 | 105 | |
dkato | 0:f782d9c66c49 | 106 | bd_size_t SlicingBlockDevice::get_erase_size() const |
dkato | 0:f782d9c66c49 | 107 | { |
dkato | 0:f782d9c66c49 | 108 | return _bd->get_erase_size(); |
dkato | 0:f782d9c66c49 | 109 | } |
dkato | 0:f782d9c66c49 | 110 | |
dkato | 0:f782d9c66c49 | 111 | bd_size_t SlicingBlockDevice::size() const |
dkato | 0:f782d9c66c49 | 112 | { |
dkato | 0:f782d9c66c49 | 113 | return _stop - _start; |
dkato | 0:f782d9c66c49 | 114 | } |