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