Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 
00023 /** \addtogroup storage */
00024 /** @{*/
00025 
00026 #ifndef MBED_SLICING_BLOCK_DEVICE_H
00027 #define MBED_SLICING_BLOCK_DEVICE_H
00028 
00029 #include "BlockDevice.h"
00030 #include "platform/mbed_assert.h"
00031 
00032 namespace mbed {
00033 
00034 /** Block device for mapping to a slice of another block device
00035  */
00036 class SlicingBlockDevice : public BlockDevice {
00037 public:
00038     /** Lifetime of the memory block device
00039      *
00040      *  @param bd       Block device to back the SlicingBlockDevice
00041      *  @param start    Start block address to map to block 0, negative addresses
00042      *                  are calculated from the end of the underlying block device
00043      *  @param end      End block address to mark the end of the block device,
00044      *                  this block is not mapped, negative addresses are
00045      *                  calculated from the end of the underlying block device.
00046      *                  The default stops at end of the underlying block device.
00047      */
00048     SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end = 0);
00049 
00050     /** Lifetime of a block device
00051      */
00052     virtual ~SlicingBlockDevice() {};
00053 
00054     /** Initialize a block device
00055      *
00056      *  @return         0 on success or a negative error code on failure
00057      */
00058     virtual int init();
00059 
00060     /** Deinitialize a block device
00061      *
00062      *  @return         0 on success or a negative error code on failure
00063      */
00064     virtual int deinit();
00065 
00066     /** Ensure data on storage is in sync with the driver
00067      *
00068      *  @return         0 on success or a negative error code on failure
00069      */
00070     virtual int sync();
00071 
00072     /** Read blocks from a block device
00073      *
00074      *  @param buffer   Buffer to read blocks into
00075      *  @param addr     Address of block to begin reading from
00076      *  @param size     Size to read in bytes, must be a multiple of read block size
00077      *  @return         0 on success or a negative error code on failure
00078      */
00079     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00080 
00081     /** Program blocks to a block device
00082      *
00083      *  The blocks must have been erased prior to being programmed
00084      *
00085      *  @param buffer   Buffer of data to write to blocks
00086      *  @param addr     Address of block to begin writing to
00087      *  @param size     Size to write in bytes, must be a multiple of program block size
00088      *  @return         0 on success or a negative error code on failure
00089      */
00090     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00091 
00092     /** Erase blocks on a block device
00093      *
00094      *  The state of an erased block is undefined until it has been programmed,
00095      *  unless get_erase_value returns a non-negative byte value
00096      *
00097      *  @param addr     Address of block to begin erasing
00098      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00099      *  @return         0 on success or a negative error code on failure
00100      */
00101     virtual int erase(bd_addr_t addr, bd_size_t size);
00102 
00103     /** Get the size of a readable block
00104      *
00105      *  @return         Size of a readable block in bytes
00106      */
00107     virtual bd_size_t get_read_size() const;
00108 
00109     /** Get the size of a programmable block
00110      *
00111      *  @return         Size of a programmable block in bytes
00112      *  @note Must be a multiple of the read size
00113      */
00114     virtual bd_size_t get_program_size() const;
00115 
00116     /** Get the size of an erasable block
00117      *
00118      *  @return         Size of an erasable block in bytes
00119      *  @note Must be a multiple of the program size
00120      */
00121     virtual bd_size_t get_erase_size() const;
00122 
00123     /** Get the size of an erasable block given address
00124      *
00125      *  @param addr     Address within the erasable block
00126      *  @return         Size of an erasable block in bytes
00127      *  @note Must be a multiple of the program size
00128      */
00129     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00130 
00131     /** Get the value of storage when erased
00132      *
00133      *  If get_erase_value returns a non-negative byte value, the underlying
00134      *  storage is set to that value when erased, and storage containing
00135      *  that value can be programmed without another erase.
00136      *
00137      *  @return         The value of storage when erased, or -1 if you can't
00138      *                  rely on the value of erased storage
00139      */
00140     virtual int get_erase_value() const;
00141 
00142     /** Get the total size of the underlying device
00143      *
00144      *  @return         Size of the underlying device in bytes
00145      */
00146     virtual bd_size_t size() const;
00147 
00148     /** Get the BlockDevice class type.
00149      *
00150      *  @return         A string represent the BlockDevice class type.
00151      */
00152     virtual const char *get_type() const;
00153 
00154     /** Convenience function for checking block program validity
00155      *
00156      *  @param addr     Address of block to begin writing to
00157      *  @param size     Size to write in bytes
00158      *  @return         True if program is valid for underlying block device
00159      */
00160     virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const;
00161 
00162     /** Convenience function for checking block read validity
00163      *
00164      *  @param addr     Address of block to begin reading from
00165      *  @param size     Size to read in bytes
00166      *  @return         True if read is valid for underlying block device
00167      */
00168     virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const;
00169 
00170     /** Convenience function for checking block erase validity
00171      *
00172      *  @param addr     Address of block to begin erasing
00173      *  @param size     Size to erase in bytes
00174      *  @return         True if erase is valid for underlying block device
00175      */
00176     virtual bool is_valid_erase(bd_addr_t addr, bd_size_t size) const;
00177 
00178 
00179 protected:
00180     BlockDevice *_bd;
00181     bool _start_from_end;
00182     bd_size_t _start;
00183     bool _stop_from_end;
00184     bd_size_t _stop;
00185 };
00186 
00187 } // namespace mbed
00188 
00189 // Added "using" for backwards compatibility
00190 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
00191 using mbed::SlicingBlockDevice;
00192 #endif
00193 
00194 #endif
00195 
00196 /** @}*/