Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ExhaustibleBlockDevice.cpp Source File

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         return 0;
00072     }
00073 
00074     return _bd->program(buffer, addr, size);
00075 }
00076 
00077 int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size)
00078 {
00079     MBED_ASSERT(is_valid_erase(addr, size));
00080 
00081     bd_size_t eu_size = get_erase_size();
00082     while (size) {
00083         // use an erase cycle
00084         if (_erase_array[addr / eu_size] > 0) {
00085             _erase_array[addr / eu_size] -= 1;
00086         }
00087 
00088         if (_erase_array[addr / eu_size] > 0) {
00089             int  err = _bd->erase(addr, eu_size);
00090             if (err) {
00091                 return err;
00092             }
00093         }
00094 
00095         addr += eu_size;
00096         size -= eu_size;
00097     }
00098 
00099     return 0;
00100 }
00101 
00102 bd_size_t ExhaustibleBlockDevice::get_read_size() const
00103 {
00104     return _bd->get_read_size();
00105 }
00106 
00107 bd_size_t ExhaustibleBlockDevice::get_program_size() const
00108 {
00109     return _bd->get_program_size();
00110 }
00111 
00112 bd_size_t ExhaustibleBlockDevice::get_erase_size() const
00113 {
00114     return _bd->get_erase_size();
00115 }
00116 
00117 bd_size_t ExhaustibleBlockDevice::get_erase_size(bd_addr_t addr) const
00118 {
00119     return _bd->get_erase_size(addr);
00120 }
00121 
00122 int ExhaustibleBlockDevice::get_erase_value() const
00123 {
00124     return _bd->get_erase_value();
00125 }
00126 
00127 bd_size_t ExhaustibleBlockDevice::size() const
00128 {
00129     return _bd->size();
00130 }