init
Embed:
(wiki syntax)
Show/hide line numbers
ExhaustibleBlockDevice.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 "ExhaustibleBlockDevice.h" 00018 #include "mbed.h" 00019 00020 00021 ExhaustibleBlockDevice::ExhaustibleBlockDevice(BlockDevice *bd, uint32_t erase_cycles) 00022 : _bd(bd), _erase_array(NULL), _erase_cycles(erase_cycles) 00023 { 00024 } 00025 00026 ExhaustibleBlockDevice::~ExhaustibleBlockDevice() 00027 { 00028 delete[] _erase_array; 00029 } 00030 00031 int ExhaustibleBlockDevice::init() 00032 { 00033 int err = _bd->init(); 00034 if (err) { 00035 return err; 00036 } 00037 00038 if (!_erase_array) { 00039 // can only be allocated after initialization 00040 _erase_array = new uint32_t[_bd->size() / _bd->get_erase_size()]; 00041 for (size_t i = 0; i < _bd->size() / _bd->get_erase_size(); i++) { 00042 _erase_array[i] = _erase_cycles; 00043 } 00044 } 00045 00046 return 0; 00047 } 00048 00049 int ExhaustibleBlockDevice::deinit() 00050 { 00051 // _erase_array is lazily cleaned up in destructor to allow 00052 // data to live across de/reinitialization 00053 return _bd->deinit(); 00054 } 00055 00056 int ExhaustibleBlockDevice::sync() 00057 { 00058 return _bd->sync(); 00059 } 00060 00061 int ExhaustibleBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) 00062 { 00063 return _bd->read(buffer, addr, size); 00064 } 00065 00066 int ExhaustibleBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size) 00067 { 00068 MBED_ASSERT(is_valid_program(addr, size)); 00069 00070 if (_erase_array[addr / get_erase_size()] == 0) { 00071 // TODO possibly something more destructive here 00072 return 0; 00073 } 00074 00075 return _bd->program(buffer, addr, size); 00076 } 00077 00078 int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size) 00079 { 00080 MBED_ASSERT(is_valid_erase(addr, size)); 00081 00082 // use an erase cycle 00083 if (_erase_array[addr / get_erase_size()] > 0) { 00084 _erase_array[addr / get_erase_size()] -= 1; 00085 } 00086 00087 if (_erase_array[addr / get_erase_size()] == 0) { 00088 // TODO possibly something more destructive here 00089 return 0; 00090 } 00091 00092 return _bd->erase(addr, size); 00093 } 00094 00095 bd_size_t ExhaustibleBlockDevice::get_read_size() const 00096 { 00097 return _bd->get_read_size(); 00098 } 00099 00100 bd_size_t ExhaustibleBlockDevice::get_program_size() const 00101 { 00102 return _bd->get_program_size(); 00103 } 00104 00105 bd_size_t ExhaustibleBlockDevice::get_erase_size() const 00106 { 00107 return _bd->get_erase_size(); 00108 } 00109 00110 int ExhaustibleBlockDevice::get_erase_value() const 00111 { 00112 return _bd->get_erase_value(); 00113 } 00114 00115 bd_size_t ExhaustibleBlockDevice::size() const 00116 { 00117 return _bd->size(); 00118 }
Generated on Tue Jul 12 2022 13:24:40 by
1.7.2