mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/storage/blockdevice/SlicingBlockDevice.cpp@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* mbed Microcontroller Library |
kenjiArai | 0:5b88d5760320 | 2 | * Copyright (c) 2017 ARM Limited |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 0:5b88d5760320 | 5 | * you may not use this file except in compliance with the License. |
kenjiArai | 0:5b88d5760320 | 6 | * You may obtain a copy of the License at |
kenjiArai | 0:5b88d5760320 | 7 | * |
kenjiArai | 0:5b88d5760320 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:5b88d5760320 | 9 | * |
kenjiArai | 0:5b88d5760320 | 10 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:5b88d5760320 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 0:5b88d5760320 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:5b88d5760320 | 13 | * See the License for the specific language governing permissions and |
kenjiArai | 0:5b88d5760320 | 14 | * limitations under the License. |
kenjiArai | 0:5b88d5760320 | 15 | */ |
kenjiArai | 0:5b88d5760320 | 16 | |
kenjiArai | 0:5b88d5760320 | 17 | #include "SlicingBlockDevice.h" |
kenjiArai | 0:5b88d5760320 | 18 | #include "platform/mbed_assert.h" |
kenjiArai | 0:5b88d5760320 | 19 | #include "stddef.h" |
kenjiArai | 1:9db0e321a9f4 | 20 | #include <stdio.h> |
kenjiArai | 0:5b88d5760320 | 21 | |
kenjiArai | 0:5b88d5760320 | 22 | namespace mbed { |
kenjiArai | 0:5b88d5760320 | 23 | |
kenjiArai | 0:5b88d5760320 | 24 | |
kenjiArai | 0:5b88d5760320 | 25 | SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t stop) |
kenjiArai | 0:5b88d5760320 | 26 | : _bd(bd) |
kenjiArai | 0:5b88d5760320 | 27 | , _start_from_end(false), _start(start) |
kenjiArai | 0:5b88d5760320 | 28 | , _stop_from_end(false), _stop(stop) |
kenjiArai | 0:5b88d5760320 | 29 | { |
kenjiArai | 1:9db0e321a9f4 | 30 | MBED_ASSERT(bd); |
kenjiArai | 1:9db0e321a9f4 | 31 | // SlicingBlockDevice(bd, 0,0) would use the full block, which does not make sense, it must be a programming eror. |
kenjiArai | 1:9db0e321a9f4 | 32 | // SlicingBlockDevice(bd, 100,100) would have no size, which is also a programming error. |
kenjiArai | 1:9db0e321a9f4 | 33 | MBED_ASSERT(start != stop); |
kenjiArai | 1:9db0e321a9f4 | 34 | |
kenjiArai | 0:5b88d5760320 | 35 | if ((int64_t)_start < 0) { |
kenjiArai | 0:5b88d5760320 | 36 | _start_from_end = true; |
kenjiArai | 0:5b88d5760320 | 37 | _start = -_start; |
kenjiArai | 0:5b88d5760320 | 38 | } |
kenjiArai | 0:5b88d5760320 | 39 | |
kenjiArai | 0:5b88d5760320 | 40 | if ((int64_t)_stop <= 0) { |
kenjiArai | 0:5b88d5760320 | 41 | _stop_from_end = true; |
kenjiArai | 0:5b88d5760320 | 42 | _stop = -_stop; |
kenjiArai | 0:5b88d5760320 | 43 | } |
kenjiArai | 0:5b88d5760320 | 44 | } |
kenjiArai | 0:5b88d5760320 | 45 | |
kenjiArai | 0:5b88d5760320 | 46 | int SlicingBlockDevice::init() |
kenjiArai | 0:5b88d5760320 | 47 | { |
kenjiArai | 0:5b88d5760320 | 48 | int err = _bd->init(); |
kenjiArai | 0:5b88d5760320 | 49 | if (err) { |
kenjiArai | 0:5b88d5760320 | 50 | return err; |
kenjiArai | 0:5b88d5760320 | 51 | } |
kenjiArai | 0:5b88d5760320 | 52 | |
kenjiArai | 0:5b88d5760320 | 53 | bd_size_t size = _bd->size(); |
kenjiArai | 0:5b88d5760320 | 54 | |
kenjiArai | 0:5b88d5760320 | 55 | // Calculate from_end values |
kenjiArai | 0:5b88d5760320 | 56 | if (_start_from_end) { |
kenjiArai | 0:5b88d5760320 | 57 | _start_from_end = false; |
kenjiArai | 0:5b88d5760320 | 58 | _start = size - _start; |
kenjiArai | 0:5b88d5760320 | 59 | } |
kenjiArai | 0:5b88d5760320 | 60 | |
kenjiArai | 0:5b88d5760320 | 61 | if (_stop_from_end) { |
kenjiArai | 0:5b88d5760320 | 62 | _stop_from_end = false; |
kenjiArai | 0:5b88d5760320 | 63 | _stop = size - _stop; |
kenjiArai | 0:5b88d5760320 | 64 | } |
kenjiArai | 0:5b88d5760320 | 65 | |
kenjiArai | 0:5b88d5760320 | 66 | // Check that block addresses are valid |
kenjiArai | 1:9db0e321a9f4 | 67 | if (!is_valid_erase(0, _stop - _start)) { |
kenjiArai | 1:9db0e321a9f4 | 68 | return BD_ERROR_DEVICE_ERROR; |
kenjiArai | 1:9db0e321a9f4 | 69 | } |
kenjiArai | 0:5b88d5760320 | 70 | |
kenjiArai | 0:5b88d5760320 | 71 | return 0; |
kenjiArai | 0:5b88d5760320 | 72 | } |
kenjiArai | 0:5b88d5760320 | 73 | |
kenjiArai | 0:5b88d5760320 | 74 | int SlicingBlockDevice::deinit() |
kenjiArai | 0:5b88d5760320 | 75 | { |
kenjiArai | 0:5b88d5760320 | 76 | return _bd->deinit(); |
kenjiArai | 0:5b88d5760320 | 77 | } |
kenjiArai | 0:5b88d5760320 | 78 | |
kenjiArai | 0:5b88d5760320 | 79 | int SlicingBlockDevice::sync() |
kenjiArai | 0:5b88d5760320 | 80 | { |
kenjiArai | 0:5b88d5760320 | 81 | return _bd->sync(); |
kenjiArai | 0:5b88d5760320 | 82 | } |
kenjiArai | 0:5b88d5760320 | 83 | |
kenjiArai | 0:5b88d5760320 | 84 | int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) |
kenjiArai | 0:5b88d5760320 | 85 | { |
kenjiArai | 1:9db0e321a9f4 | 86 | if (!is_valid_read(addr, size)) { |
kenjiArai | 1:9db0e321a9f4 | 87 | return BD_ERROR_DEVICE_ERROR; |
kenjiArai | 1:9db0e321a9f4 | 88 | } |
kenjiArai | 0:5b88d5760320 | 89 | return _bd->read(b, addr + _start, size); |
kenjiArai | 0:5b88d5760320 | 90 | } |
kenjiArai | 0:5b88d5760320 | 91 | |
kenjiArai | 0:5b88d5760320 | 92 | int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) |
kenjiArai | 0:5b88d5760320 | 93 | { |
kenjiArai | 1:9db0e321a9f4 | 94 | if (!is_valid_program(addr, size)) { |
kenjiArai | 1:9db0e321a9f4 | 95 | return BD_ERROR_DEVICE_ERROR; |
kenjiArai | 1:9db0e321a9f4 | 96 | } |
kenjiArai | 0:5b88d5760320 | 97 | return _bd->program(b, addr + _start, size); |
kenjiArai | 0:5b88d5760320 | 98 | } |
kenjiArai | 0:5b88d5760320 | 99 | |
kenjiArai | 0:5b88d5760320 | 100 | int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size) |
kenjiArai | 0:5b88d5760320 | 101 | { |
kenjiArai | 1:9db0e321a9f4 | 102 | if (!is_valid_erase(addr, size)) { |
kenjiArai | 1:9db0e321a9f4 | 103 | return BD_ERROR_DEVICE_ERROR; |
kenjiArai | 1:9db0e321a9f4 | 104 | } |
kenjiArai | 0:5b88d5760320 | 105 | return _bd->erase(addr + _start, size); |
kenjiArai | 0:5b88d5760320 | 106 | } |
kenjiArai | 0:5b88d5760320 | 107 | |
kenjiArai | 0:5b88d5760320 | 108 | bool SlicingBlockDevice::is_valid_read(bd_addr_t addr, bd_size_t size) const |
kenjiArai | 0:5b88d5760320 | 109 | { |
kenjiArai | 1:9db0e321a9f4 | 110 | return _bd->is_valid_read(_start + addr, size) && _start + addr + size <= _stop; |
kenjiArai | 0:5b88d5760320 | 111 | } |
kenjiArai | 0:5b88d5760320 | 112 | |
kenjiArai | 0:5b88d5760320 | 113 | bool SlicingBlockDevice::is_valid_program(bd_addr_t addr, bd_size_t size) const |
kenjiArai | 0:5b88d5760320 | 114 | { |
kenjiArai | 1:9db0e321a9f4 | 115 | return _bd->is_valid_program(_start + addr, size) && _start + addr + size <= _stop; |
kenjiArai | 0:5b88d5760320 | 116 | } |
kenjiArai | 0:5b88d5760320 | 117 | |
kenjiArai | 0:5b88d5760320 | 118 | bool SlicingBlockDevice::is_valid_erase(bd_addr_t addr, bd_size_t size) const |
kenjiArai | 0:5b88d5760320 | 119 | { |
kenjiArai | 1:9db0e321a9f4 | 120 | return _bd->is_valid_erase(_start + addr, size) && _start + addr + size <= _stop; |
kenjiArai | 0:5b88d5760320 | 121 | } |
kenjiArai | 0:5b88d5760320 | 122 | |
kenjiArai | 0:5b88d5760320 | 123 | bd_size_t SlicingBlockDevice::get_read_size() const |
kenjiArai | 0:5b88d5760320 | 124 | { |
kenjiArai | 0:5b88d5760320 | 125 | return _bd->get_read_size(); |
kenjiArai | 0:5b88d5760320 | 126 | } |
kenjiArai | 0:5b88d5760320 | 127 | |
kenjiArai | 0:5b88d5760320 | 128 | bd_size_t SlicingBlockDevice::get_program_size() const |
kenjiArai | 0:5b88d5760320 | 129 | { |
kenjiArai | 0:5b88d5760320 | 130 | return _bd->get_program_size(); |
kenjiArai | 0:5b88d5760320 | 131 | } |
kenjiArai | 0:5b88d5760320 | 132 | |
kenjiArai | 0:5b88d5760320 | 133 | bd_size_t SlicingBlockDevice::get_erase_size() const |
kenjiArai | 0:5b88d5760320 | 134 | { |
kenjiArai | 1:9db0e321a9f4 | 135 | return _bd->get_erase_size(_start); |
kenjiArai | 0:5b88d5760320 | 136 | } |
kenjiArai | 0:5b88d5760320 | 137 | |
kenjiArai | 0:5b88d5760320 | 138 | bd_size_t SlicingBlockDevice::get_erase_size(bd_addr_t addr) const |
kenjiArai | 0:5b88d5760320 | 139 | { |
kenjiArai | 0:5b88d5760320 | 140 | return _bd->get_erase_size(_start + addr); |
kenjiArai | 0:5b88d5760320 | 141 | } |
kenjiArai | 0:5b88d5760320 | 142 | |
kenjiArai | 0:5b88d5760320 | 143 | int SlicingBlockDevice::get_erase_value() const |
kenjiArai | 0:5b88d5760320 | 144 | { |
kenjiArai | 0:5b88d5760320 | 145 | return _bd->get_erase_value(); |
kenjiArai | 0:5b88d5760320 | 146 | } |
kenjiArai | 0:5b88d5760320 | 147 | |
kenjiArai | 0:5b88d5760320 | 148 | bd_size_t SlicingBlockDevice::size() const |
kenjiArai | 0:5b88d5760320 | 149 | { |
kenjiArai | 0:5b88d5760320 | 150 | return _stop - _start; |
kenjiArai | 0:5b88d5760320 | 151 | } |
kenjiArai | 0:5b88d5760320 | 152 | |
kenjiArai | 0:5b88d5760320 | 153 | const char *SlicingBlockDevice::get_type() const |
kenjiArai | 0:5b88d5760320 | 154 | { |
kenjiArai | 1:9db0e321a9f4 | 155 | return _bd->get_type(); |
kenjiArai | 0:5b88d5760320 | 156 | } |
kenjiArai | 0:5b88d5760320 | 157 | |
kenjiArai | 0:5b88d5760320 | 158 | } // namespace mbed |