Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BufferedBlockDevice.h Source File

BufferedBlockDevice.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 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_BUFFERED_BLOCK_DEVICE_H
00027 #define MBED_BUFFERED_BLOCK_DEVICE_H
00028 
00029 #include "BlockDevice.h"
00030 
00031 namespace mbed {
00032 
00033 /** Block device for allowing minimal read and program sizes (of 1) for the underlying BD,
00034  *  using a buffer on the heap.
00035  */
00036 class BufferedBlockDevice : public BlockDevice {
00037 public:
00038     /** Lifetime of a memory-buffered block device wrapping an underlying block device
00039      *
00040      *  @param bd        Block device to back the BufferedBlockDevice
00041      */
00042     BufferedBlockDevice(BlockDevice *bd);
00043 
00044     /** Lifetime of the memory-buffered block device
00045      */
00046     virtual ~BufferedBlockDevice();
00047 
00048     /** Initialize a buffered-memory block device and its underlying block device
00049      *
00050      *  @return         0 on success or a negative error code on failure
00051      */
00052     virtual int init();
00053 
00054     /** Deinitialize the buffered-memory block device and its underlying block device
00055      *
00056      *  @return         0 on success or a negative error code on failure
00057      */
00058     virtual int deinit();
00059 
00060     /** Ensure that data on the underlying storage block device is in sync with the
00061      *  memory-buffered block device
00062      *
00063      *  @return         0 on success or a negative error code on failure
00064      */
00065     virtual int sync();
00066 
00067     /** Read blocks from the memory-buffered block device
00068      *
00069      *  @param buffer   Buffer to read blocks into
00070      *  @param addr     Address of block to begin reading from
00071      *  @param size     Size to read in bytes, must be a multiple of read block size
00072      *  @return         0 on success, negative error code on failure
00073      */
00074     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00075 
00076     /** Program data to the memory-buffered block device
00077      *
00078      *  The write address blocks must be erased prior to being programmed.
00079      *
00080      *  @param buffer   Buffer of data to write to blocks
00081      *  @param addr     Address of block to begin writing to
00082      *  @param size     Size to write in bytes, must be a multiple of program block size
00083      *  @return         0 on success, negative error code on failure
00084      */
00085     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00086 
00087     /** Erase blocks from the memory-buffered block device
00088      *
00089      *  The state of an erased block is undefined until it has been programmed,
00090      *  unless get_erase_value returns a non-negative byte value.
00091      *
00092      *  @param addr     Address of block to begin erasing
00093      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00094      *  @return         0 on success, negative error code on failure
00095      */
00096     virtual int erase(bd_addr_t addr, bd_size_t size);
00097 
00098     /** Mark blocks as no longer in use
00099      *
00100      *  This function provides a hint to the underlying block device that a region of blocks
00101      *  is no longer in use and may be erased without side effects. Erase must still be called
00102      *  before programming, but trimming allows flash-translation-layers to schedule erases when
00103      *  the device is not busy.
00104      *
00105      *  @param addr     Address of block to mark as unused
00106      *  @param size     Size to mark as unused in bytes, must be a multiple of erase block size
00107      *  @return         0 on success, negative error code on failure
00108      */
00109     virtual int trim(bd_addr_t addr, bd_size_t size);
00110 
00111     /** Get the size of a readable block
00112      *
00113      *  @return         Size of a readable block in bytes
00114      */
00115     virtual bd_size_t get_read_size() const;
00116 
00117     /** Get the size of a programmable block
00118      *
00119      *  @return         Size of a programmable block in bytes
00120      *  @note Must be a multiple of the read size
00121      */
00122     virtual bd_size_t get_program_size() const;
00123 
00124     /** Get the size of an erasable block
00125      *
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() const;
00130 
00131     /** Get the size of an erasable block of a given address
00132      *
00133      *  @param addr     Address within the erasable block
00134      *  @return         Size of an erasable block in bytes
00135      *  @note Must be a multiple of the program size
00136      */
00137     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00138 
00139     /** Get the value of storage data after being erased
00140      *
00141      *  If get_erase_value returns a non-negative byte value, the underlying
00142      *  storage is set to that value when erased, and storage containing
00143      *  that value can be programmed without another erase.
00144      *
00145      *  @return         The value of storage when erased, or -1 if you can't
00146      *                  rely on the value of erased storage
00147      */
00148     virtual int get_erase_value() const;
00149 
00150     /** Get the total size of the underlying device
00151      *
00152      *  @return         Size of the underlying device in bytes
00153      */
00154     virtual bd_size_t size() const;
00155 
00156     /** Get the underlying BlockDevice class type
00157      *
00158      *  @return         A string representing the underlying BlockDevice class type
00159      */
00160     virtual const char *get_type() const;
00161 
00162 protected:
00163     BlockDevice *_bd;
00164     bd_size_t _bd_program_size;
00165     bd_size_t _bd_read_size;
00166     bd_size_t _bd_size;
00167     bd_size_t _write_cache_addr;
00168     bool _write_cache_valid;
00169     uint8_t *_write_cache;
00170     uint8_t *_read_buf;
00171     uint32_t _init_ref_count;
00172     bool _is_initialized;
00173 
00174 #if !(DOXYGEN_ONLY)
00175     /** Flush data in cache
00176      *
00177      *  @return         0 on success or a negative error code on failure
00178      */
00179     int flush();
00180 
00181     /** Invalidate write cache
00182      *
00183      *  @return         none
00184      */
00185     void invalidate_write_cache();
00186 #endif //#if !(DOXYGEN_ONLY)
00187 };
00188 } // namespace mbed
00189 
00190 // Added "using" for backwards compatibility
00191 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
00192 using mbed::BufferedBlockDevice;
00193 #endif
00194 
00195 #endif
00196 
00197 /** @}*/