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