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: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
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::read(void *buffer, bd_addr_t addr, bd_size_t size) 00057 { 00058 return _bd->read(buffer, addr, size); 00059 } 00060 00061 int ExhaustibleBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size) 00062 { 00063 MBED_ASSERT(is_valid_program(addr, size)); 00064 00065 if (_erase_array[addr / get_erase_size()] == 0) { 00066 // TODO possibly something more destructive here 00067 return 0; 00068 } 00069 00070 return _bd->program(buffer, addr, size); 00071 } 00072 00073 int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size) 00074 { 00075 MBED_ASSERT(is_valid_erase(addr, size)); 00076 00077 // use an erase cycle 00078 if (_erase_array[addr / get_erase_size()] > 0) { 00079 _erase_array[addr / get_erase_size()] -= 1; 00080 } 00081 00082 if (_erase_array[addr / get_erase_size()] == 0) { 00083 // TODO possibly something more destructive here 00084 return 0; 00085 } 00086 00087 return _bd->erase(addr, size); 00088 } 00089 00090 bd_size_t ExhaustibleBlockDevice::get_read_size() const 00091 { 00092 return _bd->get_read_size(); 00093 } 00094 00095 bd_size_t ExhaustibleBlockDevice::get_program_size() const 00096 { 00097 return _bd->get_program_size(); 00098 } 00099 00100 bd_size_t ExhaustibleBlockDevice::get_erase_size() const 00101 { 00102 return _bd->get_erase_size(); 00103 } 00104 00105 bd_size_t ExhaustibleBlockDevice::size() const 00106 { 00107 return _bd->size(); 00108 }
Generated on Tue Jul 12 2022 13:02:52 by
