Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
SlicingBlockDevice.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "SlicingBlockDevice.h" 00018 #include "platform/mbed_assert.h" 00019 #include "stddef.h" 00020 #include <stdio.h> 00021 00022 namespace mbed { 00023 00024 00025 SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t stop) 00026 : _bd(bd) 00027 , _start_from_end(false), _start(start) 00028 , _stop_from_end(false), _stop(stop) 00029 { 00030 MBED_ASSERT(bd); 00031 // SlicingBlockDevice(bd, 0,0) would use the full block, which does not make sense, it must be a programming eror. 00032 // SlicingBlockDevice(bd, 100,100) would have no size, which is also a programming error. 00033 MBED_ASSERT(start != stop); 00034 00035 if ((int64_t)_start < 0) { 00036 _start_from_end = true; 00037 _start = -_start; 00038 } 00039 00040 if ((int64_t)_stop <= 0) { 00041 _stop_from_end = true; 00042 _stop = -_stop; 00043 } 00044 } 00045 00046 int SlicingBlockDevice::init() 00047 { 00048 int err = _bd->init(); 00049 if (err) { 00050 return err; 00051 } 00052 00053 bd_size_t size = _bd->size(); 00054 00055 // Calculate from_end values 00056 if (_start_from_end) { 00057 _start_from_end = false; 00058 _start = size - _start; 00059 } 00060 00061 if (_stop_from_end) { 00062 _stop_from_end = false; 00063 _stop = size - _stop; 00064 } 00065 00066 // Check that block addresses are valid 00067 if (!is_valid_erase(0, _stop - _start)) { 00068 return BD_ERROR_DEVICE_ERROR; 00069 } 00070 00071 return 0; 00072 } 00073 00074 int SlicingBlockDevice::deinit() 00075 { 00076 return _bd->deinit(); 00077 } 00078 00079 int SlicingBlockDevice::sync() 00080 { 00081 return _bd->sync(); 00082 } 00083 00084 int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) 00085 { 00086 if (!is_valid_read(addr, size)) { 00087 return BD_ERROR_DEVICE_ERROR; 00088 } 00089 return _bd->read(b, addr + _start, size); 00090 } 00091 00092 int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) 00093 { 00094 if (!is_valid_program(addr, size)) { 00095 return BD_ERROR_DEVICE_ERROR; 00096 } 00097 return _bd->program(b, addr + _start, size); 00098 } 00099 00100 int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size) 00101 { 00102 if (!is_valid_erase(addr, size)) { 00103 return BD_ERROR_DEVICE_ERROR; 00104 } 00105 return _bd->erase(addr + _start, size); 00106 } 00107 00108 bool SlicingBlockDevice::is_valid_read(bd_addr_t addr, bd_size_t size) const 00109 { 00110 return _bd->is_valid_read(_start + addr, size) && _start + addr + size <= _stop; 00111 } 00112 00113 bool SlicingBlockDevice::is_valid_program(bd_addr_t addr, bd_size_t size) const 00114 { 00115 return _bd->is_valid_program(_start + addr, size) && _start + addr + size <= _stop; 00116 } 00117 00118 bool SlicingBlockDevice::is_valid_erase(bd_addr_t addr, bd_size_t size) const 00119 { 00120 return _bd->is_valid_erase(_start + addr, size) && _start + addr + size <= _stop; 00121 } 00122 00123 bd_size_t SlicingBlockDevice::get_read_size() const 00124 { 00125 return _bd->get_read_size(); 00126 } 00127 00128 bd_size_t SlicingBlockDevice::get_program_size() const 00129 { 00130 return _bd->get_program_size(); 00131 } 00132 00133 bd_size_t SlicingBlockDevice::get_erase_size() const 00134 { 00135 return _bd->get_erase_size(_start); 00136 } 00137 00138 bd_size_t SlicingBlockDevice::get_erase_size(bd_addr_t addr) const 00139 { 00140 return _bd->get_erase_size(_start + addr); 00141 } 00142 00143 int SlicingBlockDevice::get_erase_value() const 00144 { 00145 return _bd->get_erase_value(); 00146 } 00147 00148 bd_size_t SlicingBlockDevice::size() const 00149 { 00150 return _stop - _start; 00151 } 00152 00153 const char *SlicingBlockDevice::get_type() const 00154 { 00155 return _bd->get_type(); 00156 } 00157 00158 } // namespace mbed
Generated on Tue Jul 12 2022 13:54:50 by
