BA / Mbed OS BaBoRo1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SlicingBlockDevice.h Source File

SlicingBlockDevice.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_SLICING_BLOCK_DEVICE_H
00023 #define MBED_SLICING_BLOCK_DEVICE_H
00024 
00025 #include "BlockDevice.h"
00026 #include "mbed.h"
00027 
00028 
00029 /** Block device for mapping to a slice of another block device
00030  *
00031  *  @code
00032  *  #include "mbed.h"
00033  *  #include "HeapBlockDevice.h"
00034  *  #include "SlicingBlockDevice.h"
00035  *
00036  *  // Create a block device with 64 blocks of size 512
00037  *  HeapBlockDevice mem(64*512, 512);
00038  *
00039  *  // Create a block device that maps to the first 32 blocks
00040  *  SlicingBlockDevice slice1(&mem, 0*512, 32*512);
00041  *
00042  *  // Create a block device that maps to the last 32 blocks
00043  *  SlicingBlockDevice slice2(&mem, 32*512);
00044  *
00045  *  // Create a block device that maps to the middle 32 blocks
00046  *  SlicingBlockDevice slice3(&mem, 16*512, -16*512);
00047  * @endcode
00048  */
00049 class SlicingBlockDevice : public BlockDevice
00050 {
00051 public:
00052     /** Lifetime of the memory block device
00053      *
00054      *  @param bd       Block device to back the SlicingBlockDevice
00055      *  @param start    Start block address to map to block 0, negative addresses
00056      *                  are calculated from the end of the underlying block device
00057      *  @param end      End block address to mark the end of the block device,
00058      *                  this block is not mapped, negative addresses are
00059      *                  calculated from the end of the underlying block device.
00060      *                  The default stops at end of the underlying block device.
00061      */
00062     SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end = 0);
00063 
00064     /** Lifetime of a block device
00065      */
00066     virtual ~SlicingBlockDevice() {};
00067 
00068     /** Initialize a block device
00069      *
00070      *  @return         0 on success or a negative error code on failure
00071      */
00072     virtual int init();
00073 
00074     /** Deinitialize a block device
00075      *
00076      *  @return         0 on success or a negative error code on failure
00077      */
00078     virtual int deinit();
00079 
00080     /** Ensure data on storage is in sync with the driver
00081      *
00082      *  @return         0 on success or a negative error code on failure
00083      */
00084     virtual int sync();
00085 
00086     /** Read blocks from a block device
00087      *
00088      *  @param buffer   Buffer to read blocks into
00089      *  @param addr     Address of block to begin reading from
00090      *  @param size     Size to read in bytes, must be a multiple of read block size
00091      *  @return         0 on success, negative error code on failure
00092      */
00093     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00094 
00095     /** Program blocks to a block device
00096      *
00097      *  The blocks must have been erased prior to being programmed
00098      *
00099      *  @param buffer   Buffer of data to write to blocks
00100      *  @param addr     Address of block to begin writing to
00101      *  @param size     Size to write in bytes, must be a multiple of program block size
00102      *  @return         0 on success, negative error code on failure
00103      */
00104     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00105 
00106     /** Erase blocks on a block device
00107      *
00108      *  The state of an erased block is undefined until it has been programmed,
00109      *  unless get_erase_value returns a non-negative byte value
00110      *
00111      *  @param addr     Address of block to begin erasing
00112      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00113      *  @return         0 on success, negative error code on failure
00114      */
00115     virtual int erase(bd_addr_t addr, bd_size_t size);
00116 
00117     /** Get the size of a readable block
00118      *
00119      *  @return         Size of a readable block in bytes
00120      */
00121     virtual bd_size_t get_read_size() const;
00122 
00123     /** Get the size of a programmable block
00124      *
00125      *  @return         Size of a programmable block in bytes
00126      *  @note Must be a multiple of the read size
00127      */
00128     virtual bd_size_t get_program_size() const;
00129 
00130     /** Get the size of an erasable block
00131      *
00132      *  @return         Size of an erasable block in bytes
00133      *  @note Must be a multiple of the program size
00134      */
00135     virtual bd_size_t get_erase_size() const;
00136 
00137     /** Get the size of an erasable block given address
00138      *
00139      *  @param addr     Address within the erasable block
00140      *  @return         Size of an erasable block in bytes
00141      *  @note Must be a multiple of the program size
00142      */
00143     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00144 
00145     /** Get the value of storage when erased
00146      *
00147      *  If get_erase_value returns a non-negative byte value, the underlying
00148      *  storage is set to that value when erased, and storage containing
00149      *  that value can be programmed without another erase.
00150      *
00151      *  @return         The value of storage when erased, or -1 if you can't
00152      *                  rely on the value of erased storage
00153      */
00154     virtual int get_erase_value() const;
00155 
00156     /** Get the total size of the underlying device
00157      *
00158      *  @return         Size of the underlying device in bytes
00159      */
00160     virtual bd_size_t size() const;
00161 
00162 protected:
00163     BlockDevice *_bd;
00164     bool _start_from_end;
00165     bd_size_t _start;
00166     bool _stop_from_end;
00167     bd_size_t _stop;
00168 };
00169 
00170 
00171 #endif