Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 "platform/mbed_assert.h" 00027 00028 /** Block device for mapping to a slice of another block device 00029 * 00030 * @code 00031 * #include "mbed.h" 00032 * #include "HeapBlockDevice.h" 00033 * #include "SlicingBlockDevice.h" 00034 * 00035 * // Create a block device with 64 blocks of size 512 00036 * HeapBlockDevice mem(64*512, 512); 00037 * 00038 * // Create a block device that maps to the first 32 blocks 00039 * SlicingBlockDevice slice1(&mem, 0*512, 32*512); 00040 * 00041 * // Create a block device that maps to the last 32 blocks 00042 * SlicingBlockDevice slice2(&mem, 32*512); 00043 * 00044 * // Create a block device that maps to the middle 32 blocks 00045 * SlicingBlockDevice slice3(&mem, 16*512, -16*512); 00046 * @endcode 00047 */ 00048 class SlicingBlockDevice : public BlockDevice 00049 { 00050 public: 00051 /** Lifetime of the memory block device 00052 * 00053 * @param bd Block device to back the SlicingBlockDevice 00054 * @param start Start block address to map to block 0, negative addresses 00055 * are calculated from the end of the underlying block device 00056 * @param end End block address to mark the end of the block device, 00057 * this block is not mapped, negative addresses are 00058 * calculated from the end of the underlying block device. 00059 * The default stops at end of the underlying block device. 00060 */ 00061 SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end = 0); 00062 00063 /** Lifetime of a block device 00064 */ 00065 virtual ~SlicingBlockDevice() {}; 00066 00067 /** Initialize a block device 00068 * 00069 * @return 0 on success or a negative error code on failure 00070 */ 00071 virtual int init(); 00072 00073 /** Deinitialize a block device 00074 * 00075 * @return 0 on success or a negative error code on failure 00076 */ 00077 virtual int deinit(); 00078 00079 /** Ensure data on storage is in sync with the driver 00080 * 00081 * @return 0 on success or a negative error code on failure 00082 */ 00083 virtual int sync(); 00084 00085 /** Read blocks from a block device 00086 * 00087 * @param buffer Buffer to read blocks into 00088 * @param addr Address of block to begin reading from 00089 * @param size Size to read in bytes, must be a multiple of read block size 00090 * @return 0 on success, negative error code on failure 00091 */ 00092 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00093 00094 /** Program blocks to a block device 00095 * 00096 * The blocks must have been erased prior to being programmed 00097 * 00098 * @param buffer Buffer of data to write to blocks 00099 * @param addr Address of block to begin writing to 00100 * @param size Size to write in bytes, must be a multiple of program block size 00101 * @return 0 on success, negative error code on failure 00102 */ 00103 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00104 00105 /** Erase blocks on a block device 00106 * 00107 * The state of an erased block is undefined until it has been programmed, 00108 * unless get_erase_value returns a non-negative byte value 00109 * 00110 * @param addr Address of block to begin erasing 00111 * @param size Size to erase in bytes, must be a multiple of erase block size 00112 * @return 0 on success, negative error code on failure 00113 */ 00114 virtual int erase(bd_addr_t addr, bd_size_t size); 00115 00116 /** Get the size of a readable block 00117 * 00118 * @return Size of a readable block in bytes 00119 */ 00120 virtual bd_size_t get_read_size() const; 00121 00122 /** Get the size of a programmable block 00123 * 00124 * @return Size of a programmable block in bytes 00125 * @note Must be a multiple of the read size 00126 */ 00127 virtual bd_size_t get_program_size() const; 00128 00129 /** Get the size of an erasable block 00130 * 00131 * @return Size of an erasable block in bytes 00132 * @note Must be a multiple of the program size 00133 */ 00134 virtual bd_size_t get_erase_size() const; 00135 00136 /** Get the size of an erasable block given address 00137 * 00138 * @param addr Address within the erasable block 00139 * @return Size of an erasable block in bytes 00140 * @note Must be a multiple of the program size 00141 */ 00142 virtual bd_size_t get_erase_size(bd_addr_t addr) const; 00143 00144 /** Get the value of storage when erased 00145 * 00146 * If get_erase_value returns a non-negative byte value, the underlying 00147 * storage is set to that value when erased, and storage containing 00148 * that value can be programmed without another erase. 00149 * 00150 * @return The value of storage when erased, or -1 if you can't 00151 * rely on the value of erased storage 00152 */ 00153 virtual int get_erase_value() const; 00154 00155 /** Get the total size of the underlying device 00156 * 00157 * @return Size of the underlying device in bytes 00158 */ 00159 virtual bd_size_t size() const; 00160 00161 protected: 00162 BlockDevice *_bd; 00163 bool _start_from_end; 00164 bd_size_t _start; 00165 bool _stop_from_end; 00166 bd_size_t _stop; 00167 }; 00168 00169 00170 #endif
Generated on Tue Jul 12 2022 20:52:56 by
1.7.2