Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ChainingBlockDevice.h Source File

ChainingBlockDevice.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_CHAINING_BLOCK_DEVICE_H
00027 #define MBED_CHAINING_BLOCK_DEVICE_H
00028 
00029 #include "BlockDevice.h"
00030 #include "platform/mbed_assert.h"
00031 #include <stdlib.h>
00032 
00033 namespace mbed {
00034 
00035 /** Block device for chaining multiple block devices
00036  *  with the similar block sizes at sequential addresses
00037  *
00038  *  @code
00039  *  #include "mbed.h"
00040  *  #include "HeapBlockDevice.h"
00041  *  #include "ChainingBlockDevice.h"
00042  *
00043  *  // Create two smaller block devices with
00044  *  // 64 and 32 blocks of size 512 bytes
00045  *  HeapBlockDevice mem1(64*512, 512);
00046  *  HeapBlockDevice mem2(32*512, 512);
00047  *
00048  *  // Create a block device backed by mem1 and mem2
00049  *  // contains 96 blocks of size 512 bytes
00050  *  BlockDevice *bds[] = {&mem1, &mem2};
00051  *  ChainingBlockDevice chainmem(bds);
00052  *  @endcode
00053  */
00054 class ChainingBlockDevice : public BlockDevice {
00055 public:
00056     /** Lifetime of the memory block device
00057      *
00058      *  @param bds         Array of block devices to chain with sequential block addresses
00059      *  @param bd_count    Number of block devices to chain
00060      *  @note All block devices must have the same block size
00061      */
00062     ChainingBlockDevice(BlockDevice **bds, size_t bd_count);
00063 
00064     /** Lifetime of the memory block device
00065      *
00066      *  @param bds          Array of block devices to chain with sequential block addresses
00067      *  @note All block devices must have the same block size
00068      */
00069     template <size_t Size>
00070     ChainingBlockDevice(BlockDevice * (&bds)[Size])
00071         : _bds(bds), _bd_count(sizeof(bds) / sizeof(bds[0]))
00072         , _read_size(0), _program_size(0), _erase_size(0), _size(0),  _init_ref_count(0)
00073     {
00074     }
00075 
00076     /** Lifetime of the memory block device
00077      *
00078      */
00079     virtual ~ChainingBlockDevice() {}
00080 
00081     /** Initialize a block device
00082      *
00083      *  @return         0 on success or a negative error code on failure
00084      */
00085     virtual int init();
00086 
00087     /** Deinitialize a block device
00088      *
00089      *  @return         0 on success or a negative error code on failure
00090      */
00091     virtual int deinit();
00092 
00093     /** Ensure data on storage is in sync with the driver
00094      *
00095      *  @return         0 on success or a negative error code on failure
00096      */
00097     virtual int sync();
00098 
00099     /** Read blocks from a block device
00100      *
00101      *  @param buffer   Buffer to write blocks to
00102      *  @param addr     Address of block to begin reading from
00103      *  @param size     Size to read in bytes, must be a multiple of read block size
00104      *  @return         0 on success, negative error code on failure
00105      */
00106     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00107 
00108     /** Program blocks to a block device
00109      *
00110      *  The blocks must have been erased prior to being programmed
00111      *
00112      *  @param buffer   Buffer of data to write to blocks
00113      *  @param addr     Address of block to begin writing to
00114      *  @param size     Size to write in bytes, must be a multiple of program block size
00115      *  @return         0 on success, negative error code on failure
00116      */
00117     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00118 
00119     /** Erase blocks on a block device
00120      *
00121      *  The state of an erased block is undefined until it has been programmed,
00122      *  unless get_erase_value returns a non-negative byte value
00123      *
00124      *  @param addr     Address of block to begin erasing
00125      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00126      *  @return         0 on success, negative error code on failure
00127      */
00128     virtual int erase(bd_addr_t addr, bd_size_t size);
00129 
00130     /** Get the size of a readable block
00131      *
00132      *  @return         Size of a readable block in bytes
00133      */
00134     virtual bd_size_t get_read_size() const;
00135 
00136     /** Get the size of a programmable block
00137      *
00138      *  @return         Size of a programmable block in bytes
00139      *  @note Must be a multiple of the read size
00140      */
00141     virtual bd_size_t get_program_size() const;
00142 
00143     /** Get the size of an eraseable block
00144      *
00145      *  @return         Size of an erasable block in bytes
00146      *  @note Must be a multiple of the program size
00147      */
00148     virtual bd_size_t get_erase_size() const;
00149 
00150     /** Get the size of an erasable block given address
00151      *
00152      *  @param addr     Address within the erasable block
00153      *  @return         Size of an erasable block in bytes
00154      *  @note Must be a multiple of the program size
00155      */
00156     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00157 
00158     /** Get the value of storage when erased
00159      *
00160      *  If get_erase_value returns a non-negative byte value, the underlying
00161      *  storage is set to that value when erased, and storage containing
00162      *  that value can be programmed without another erase.
00163      *
00164      *  @return         The value of storage when erased, or -1 if you can't
00165      *                  rely on the value of erased storage
00166      */
00167     virtual int get_erase_value() const;
00168 
00169     /** Get the total size of the underlying device
00170      *
00171      *  @return         Size of the underlying device in bytes
00172      */
00173     virtual bd_size_t size() const;
00174 
00175     /** Get the BlockDevice class type.
00176      *
00177      *  @return         A string represent the BlockDevice class type.
00178      */
00179     virtual const char *get_type() const;
00180 
00181 protected:
00182     BlockDevice **_bds;
00183     size_t _bd_count;
00184     bd_size_t _read_size;
00185     bd_size_t _program_size;
00186     bd_size_t _erase_size;
00187     bd_size_t _size;
00188     int _erase_value;
00189     uint32_t _init_ref_count;
00190     bool _is_initialized;
00191 };
00192 
00193 } // namespace mbed
00194 
00195 // Added "using" for backwards compatibility
00196 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
00197 using mbed::ChainingBlockDevice;
00198 #endif
00199 
00200 #endif
00201 
00202 /** @}*/