Marco Mayer / Mbed OS Queue
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::read(void *b, bd_addr_t addr, bd_size_t size)
00068 {
00069     MBED_ASSERT(is_valid_read(addr, size));
00070     return _bd->read(b, addr + _start, size);
00071 }
00072 
00073 int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
00074 {
00075     MBED_ASSERT(is_valid_program(addr, size));
00076     return _bd->program(b, addr + _start, size);
00077 }
00078 
00079 int SlicingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
00080 {
00081     MBED_ASSERT(is_valid_erase(addr, size));
00082     return _bd->erase(addr + _start, size);
00083 }
00084 
00085 bd_size_t SlicingBlockDevice::get_read_size() const
00086 {
00087     return _bd->get_read_size();
00088 }
00089 
00090 bd_size_t SlicingBlockDevice::get_program_size() const
00091 {
00092     return _bd->get_program_size();
00093 }
00094 
00095 bd_size_t SlicingBlockDevice::get_erase_size() const
00096 {
00097     return _bd->get_erase_size();
00098 }
00099 
00100 bd_size_t SlicingBlockDevice::size() const
00101 {
00102     return _stop - _start;
00103 }