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