Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SlicingBlockDevice.cpp Source File

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 
00019 
00020 SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t stop)
00021     : _bd(bd)
00022     , _start_from_end(false), _start(start)
00023     , _stop_from_end(false), _stop(stop)
00024 {
00025     if ((int64_t)_start < 0) {
00026         _start_from_end = true;
00027         _start = -_start;
00028     }
00029 
00030     if ((int64_t)_stop <= 0) {
00031         _stop_from_end = true;
00032         _stop = -_stop;
00033     }
00034 }
00035 
00036 int SlicingBlockDevice::init()
00037 {
00038     int err = _bd->init();
00039     if (err) {
00040         return err;
00041     }
00042 
00043     bd_size_t size = _bd->size();
00044 
00045     // Calculate from_end values
00046     if (_start_from_end) {
00047         _start_from_end = false;
00048         _start = size - _start;
00049     }
00050 
00051     if (_stop_from_end) {
00052         _stop_from_end = false;
00053         _stop = size - _stop;
00054     }
00055 
00056     // Check that block addresses are valid
00057     MBED_ASSERT(_bd->is_valid_erase(_start, _stop - _start));
00058 
00059     return 0;
00060 }
00061 
00062 int SlicingBlockDevice::deinit()
00063 {
00064     return _bd->deinit();
00065 }
00066 
00067 int SlicingBlockDevice::sync()
00068 {
00069     return _bd->sync();
00070 }
00071 
00072 int SlicingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
00073 {
00074     MBED_ASSERT(is_valid_read(addr, size));
00075     return _bd->read(b, addr + _start, size);
00076 }
00077 
00078 int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
00079 {
00080     MBED_ASSERT(is_valid_program(addr, size));
00081     return _bd->program(b, addr + _start, size);
00082 }
00083 
00084 int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
00085 {
00086     MBED_ASSERT(is_valid_erase(addr, size));
00087     return _bd->erase(addr + _start, size);
00088 }
00089 
00090 bd_size_t SlicingBlockDevice::get_read_size() const
00091 {
00092     return _bd->get_read_size();
00093 }
00094 
00095 bd_size_t SlicingBlockDevice::get_program_size() const
00096 {
00097     return _bd->get_program_size();
00098 }
00099 
00100 bd_size_t SlicingBlockDevice::get_erase_size() const
00101 {
00102     return _bd->get_erase_size();
00103 }
00104 
00105 bd_size_t SlicingBlockDevice::get_erase_size(bd_addr_t addr) const
00106 {
00107     return _bd->get_erase_size(_start + addr);
00108 }
00109 
00110 int SlicingBlockDevice::get_erase_value() const
00111 {
00112     return _bd->get_erase_value();
00113 }
00114 
00115 bd_size_t SlicingBlockDevice::size() const
00116 {
00117     return _stop - _start;
00118 }