takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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