mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/storage/blockdevice/ChainingBlockDevice.h@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* mbed Microcontroller Library |
kenjiArai | 0:5b88d5760320 | 2 | * Copyright (c) 2017 ARM Limited |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kenjiArai | 0:5b88d5760320 | 5 | * of this software and associated documentation files (the "Software"), to deal |
kenjiArai | 0:5b88d5760320 | 6 | * in the Software without restriction, including without limitation the rights |
kenjiArai | 0:5b88d5760320 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenjiArai | 0:5b88d5760320 | 8 | * copies of the Software, and to permit persons to whom the Software is |
kenjiArai | 0:5b88d5760320 | 9 | * furnished to do so, subject to the following conditions: |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * The above copyright notice and this permission notice shall be included in |
kenjiArai | 0:5b88d5760320 | 12 | * all copies or substantial portions of the Software. |
kenjiArai | 0:5b88d5760320 | 13 | * |
kenjiArai | 0:5b88d5760320 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenjiArai | 0:5b88d5760320 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenjiArai | 0:5b88d5760320 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenjiArai | 0:5b88d5760320 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenjiArai | 0:5b88d5760320 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:5b88d5760320 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kenjiArai | 0:5b88d5760320 | 20 | * SOFTWARE. |
kenjiArai | 0:5b88d5760320 | 21 | */ |
kenjiArai | 0:5b88d5760320 | 22 | |
kenjiArai | 0:5b88d5760320 | 23 | /** \addtogroup storage */ |
kenjiArai | 0:5b88d5760320 | 24 | /** @{*/ |
kenjiArai | 0:5b88d5760320 | 25 | |
kenjiArai | 0:5b88d5760320 | 26 | #ifndef MBED_CHAINING_BLOCK_DEVICE_H |
kenjiArai | 0:5b88d5760320 | 27 | #define MBED_CHAINING_BLOCK_DEVICE_H |
kenjiArai | 0:5b88d5760320 | 28 | |
kenjiArai | 0:5b88d5760320 | 29 | #include "BlockDevice.h" |
kenjiArai | 0:5b88d5760320 | 30 | #include "platform/mbed_assert.h" |
kenjiArai | 0:5b88d5760320 | 31 | #include <stdlib.h> |
kenjiArai | 0:5b88d5760320 | 32 | |
kenjiArai | 0:5b88d5760320 | 33 | namespace mbed { |
kenjiArai | 0:5b88d5760320 | 34 | |
kenjiArai | 0:5b88d5760320 | 35 | /** Block device for chaining multiple block devices |
kenjiArai | 0:5b88d5760320 | 36 | * with the similar block sizes at sequential addresses |
kenjiArai | 0:5b88d5760320 | 37 | * |
kenjiArai | 0:5b88d5760320 | 38 | * @code |
kenjiArai | 0:5b88d5760320 | 39 | * #include "mbed.h" |
kenjiArai | 0:5b88d5760320 | 40 | * #include "HeapBlockDevice.h" |
kenjiArai | 0:5b88d5760320 | 41 | * #include "ChainingBlockDevice.h" |
kenjiArai | 0:5b88d5760320 | 42 | * |
kenjiArai | 0:5b88d5760320 | 43 | * // Create two smaller block devices with |
kenjiArai | 0:5b88d5760320 | 44 | * // 64 and 32 blocks of size 512 bytes |
kenjiArai | 0:5b88d5760320 | 45 | * HeapBlockDevice mem1(64*512, 512); |
kenjiArai | 0:5b88d5760320 | 46 | * HeapBlockDevice mem2(32*512, 512); |
kenjiArai | 0:5b88d5760320 | 47 | * |
kenjiArai | 0:5b88d5760320 | 48 | * // Create a block device backed by mem1 and mem2 |
kenjiArai | 0:5b88d5760320 | 49 | * // contains 96 blocks of size 512 bytes |
kenjiArai | 0:5b88d5760320 | 50 | * BlockDevice *bds[] = {&mem1, &mem2}; |
kenjiArai | 0:5b88d5760320 | 51 | * ChainingBlockDevice chainmem(bds); |
kenjiArai | 0:5b88d5760320 | 52 | * @endcode |
kenjiArai | 0:5b88d5760320 | 53 | */ |
kenjiArai | 0:5b88d5760320 | 54 | class ChainingBlockDevice : public BlockDevice { |
kenjiArai | 0:5b88d5760320 | 55 | public: |
kenjiArai | 0:5b88d5760320 | 56 | /** Lifetime of the memory block device |
kenjiArai | 0:5b88d5760320 | 57 | * |
kenjiArai | 0:5b88d5760320 | 58 | * @param bds Array of block devices to chain with sequential block addresses |
kenjiArai | 0:5b88d5760320 | 59 | * @param bd_count Number of block devices to chain |
kenjiArai | 0:5b88d5760320 | 60 | * @note All block devices must have the same block size |
kenjiArai | 0:5b88d5760320 | 61 | */ |
kenjiArai | 0:5b88d5760320 | 62 | ChainingBlockDevice(BlockDevice **bds, size_t bd_count); |
kenjiArai | 0:5b88d5760320 | 63 | |
kenjiArai | 0:5b88d5760320 | 64 | /** Lifetime of the memory block device |
kenjiArai | 0:5b88d5760320 | 65 | * |
kenjiArai | 0:5b88d5760320 | 66 | * @param bds Array of block devices to chain with sequential block addresses |
kenjiArai | 0:5b88d5760320 | 67 | * @note All block devices must have the same block size |
kenjiArai | 0:5b88d5760320 | 68 | */ |
kenjiArai | 0:5b88d5760320 | 69 | template <size_t Size> |
kenjiArai | 0:5b88d5760320 | 70 | ChainingBlockDevice(BlockDevice * (&bds)[Size]) |
kenjiArai | 0:5b88d5760320 | 71 | : _bds(bds), _bd_count(sizeof(bds) / sizeof(bds[0])) |
kenjiArai | 0:5b88d5760320 | 72 | , _read_size(0), _program_size(0), _erase_size(0), _size(0), _init_ref_count(0) |
kenjiArai | 0:5b88d5760320 | 73 | { |
kenjiArai | 0:5b88d5760320 | 74 | } |
kenjiArai | 0:5b88d5760320 | 75 | |
kenjiArai | 0:5b88d5760320 | 76 | /** Lifetime of the memory block device |
kenjiArai | 0:5b88d5760320 | 77 | * |
kenjiArai | 0:5b88d5760320 | 78 | */ |
kenjiArai | 0:5b88d5760320 | 79 | virtual ~ChainingBlockDevice() {} |
kenjiArai | 0:5b88d5760320 | 80 | |
kenjiArai | 0:5b88d5760320 | 81 | /** Initialize a block device |
kenjiArai | 0:5b88d5760320 | 82 | * |
kenjiArai | 0:5b88d5760320 | 83 | * @return 0 on success or a negative error code on failure |
kenjiArai | 0:5b88d5760320 | 84 | */ |
kenjiArai | 0:5b88d5760320 | 85 | virtual int init(); |
kenjiArai | 0:5b88d5760320 | 86 | |
kenjiArai | 0:5b88d5760320 | 87 | /** Deinitialize a block device |
kenjiArai | 0:5b88d5760320 | 88 | * |
kenjiArai | 0:5b88d5760320 | 89 | * @return 0 on success or a negative error code on failure |
kenjiArai | 0:5b88d5760320 | 90 | */ |
kenjiArai | 0:5b88d5760320 | 91 | virtual int deinit(); |
kenjiArai | 0:5b88d5760320 | 92 | |
kenjiArai | 0:5b88d5760320 | 93 | /** Ensure data on storage is in sync with the driver |
kenjiArai | 0:5b88d5760320 | 94 | * |
kenjiArai | 0:5b88d5760320 | 95 | * @return 0 on success or a negative error code on failure |
kenjiArai | 0:5b88d5760320 | 96 | */ |
kenjiArai | 0:5b88d5760320 | 97 | virtual int sync(); |
kenjiArai | 0:5b88d5760320 | 98 | |
kenjiArai | 0:5b88d5760320 | 99 | /** Read blocks from a block device |
kenjiArai | 0:5b88d5760320 | 100 | * |
kenjiArai | 0:5b88d5760320 | 101 | * @param buffer Buffer to write blocks to |
kenjiArai | 0:5b88d5760320 | 102 | * @param addr Address of block to begin reading from |
kenjiArai | 0:5b88d5760320 | 103 | * @param size Size to read in bytes, must be a multiple of read block size |
kenjiArai | 0:5b88d5760320 | 104 | * @return 0 on success, negative error code on failure |
kenjiArai | 0:5b88d5760320 | 105 | */ |
kenjiArai | 0:5b88d5760320 | 106 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
kenjiArai | 0:5b88d5760320 | 107 | |
kenjiArai | 0:5b88d5760320 | 108 | /** Program blocks to a block device |
kenjiArai | 0:5b88d5760320 | 109 | * |
kenjiArai | 0:5b88d5760320 | 110 | * The blocks must have been erased prior to being programmed |
kenjiArai | 0:5b88d5760320 | 111 | * |
kenjiArai | 0:5b88d5760320 | 112 | * @param buffer Buffer of data to write to blocks |
kenjiArai | 0:5b88d5760320 | 113 | * @param addr Address of block to begin writing to |
kenjiArai | 0:5b88d5760320 | 114 | * @param size Size to write in bytes, must be a multiple of program block size |
kenjiArai | 0:5b88d5760320 | 115 | * @return 0 on success, negative error code on failure |
kenjiArai | 0:5b88d5760320 | 116 | */ |
kenjiArai | 0:5b88d5760320 | 117 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
kenjiArai | 0:5b88d5760320 | 118 | |
kenjiArai | 0:5b88d5760320 | 119 | /** Erase blocks on a block device |
kenjiArai | 0:5b88d5760320 | 120 | * |
kenjiArai | 0:5b88d5760320 | 121 | * The state of an erased block is undefined until it has been programmed, |
kenjiArai | 0:5b88d5760320 | 122 | * unless get_erase_value returns a non-negative byte value |
kenjiArai | 0:5b88d5760320 | 123 | * |
kenjiArai | 0:5b88d5760320 | 124 | * @param addr Address of block to begin erasing |
kenjiArai | 0:5b88d5760320 | 125 | * @param size Size to erase in bytes, must be a multiple of erase block size |
kenjiArai | 0:5b88d5760320 | 126 | * @return 0 on success, negative error code on failure |
kenjiArai | 0:5b88d5760320 | 127 | */ |
kenjiArai | 0:5b88d5760320 | 128 | virtual int erase(bd_addr_t addr, bd_size_t size); |
kenjiArai | 0:5b88d5760320 | 129 | |
kenjiArai | 0:5b88d5760320 | 130 | /** Get the size of a readable block |
kenjiArai | 0:5b88d5760320 | 131 | * |
kenjiArai | 0:5b88d5760320 | 132 | * @return Size of a readable block in bytes |
kenjiArai | 0:5b88d5760320 | 133 | */ |
kenjiArai | 0:5b88d5760320 | 134 | virtual bd_size_t get_read_size() const; |
kenjiArai | 0:5b88d5760320 | 135 | |
kenjiArai | 0:5b88d5760320 | 136 | /** Get the size of a programmable block |
kenjiArai | 0:5b88d5760320 | 137 | * |
kenjiArai | 0:5b88d5760320 | 138 | * @return Size of a programmable block in bytes |
kenjiArai | 0:5b88d5760320 | 139 | * @note Must be a multiple of the read size |
kenjiArai | 0:5b88d5760320 | 140 | */ |
kenjiArai | 0:5b88d5760320 | 141 | virtual bd_size_t get_program_size() const; |
kenjiArai | 0:5b88d5760320 | 142 | |
kenjiArai | 0:5b88d5760320 | 143 | /** Get the size of an eraseable block |
kenjiArai | 0:5b88d5760320 | 144 | * |
kenjiArai | 0:5b88d5760320 | 145 | * @return Size of an erasable block in bytes |
kenjiArai | 0:5b88d5760320 | 146 | * @note Must be a multiple of the program size |
kenjiArai | 0:5b88d5760320 | 147 | */ |
kenjiArai | 0:5b88d5760320 | 148 | virtual bd_size_t get_erase_size() const; |
kenjiArai | 0:5b88d5760320 | 149 | |
kenjiArai | 0:5b88d5760320 | 150 | /** Get the size of an erasable block given address |
kenjiArai | 0:5b88d5760320 | 151 | * |
kenjiArai | 0:5b88d5760320 | 152 | * @param addr Address within the erasable block |
kenjiArai | 0:5b88d5760320 | 153 | * @return Size of an erasable block in bytes |
kenjiArai | 0:5b88d5760320 | 154 | * @note Must be a multiple of the program size |
kenjiArai | 0:5b88d5760320 | 155 | */ |
kenjiArai | 0:5b88d5760320 | 156 | virtual bd_size_t get_erase_size(bd_addr_t addr) const; |
kenjiArai | 0:5b88d5760320 | 157 | |
kenjiArai | 0:5b88d5760320 | 158 | /** Get the value of storage when erased |
kenjiArai | 0:5b88d5760320 | 159 | * |
kenjiArai | 0:5b88d5760320 | 160 | * If get_erase_value returns a non-negative byte value, the underlying |
kenjiArai | 0:5b88d5760320 | 161 | * storage is set to that value when erased, and storage containing |
kenjiArai | 0:5b88d5760320 | 162 | * that value can be programmed without another erase. |
kenjiArai | 0:5b88d5760320 | 163 | * |
kenjiArai | 0:5b88d5760320 | 164 | * @return The value of storage when erased, or -1 if you can't |
kenjiArai | 0:5b88d5760320 | 165 | * rely on the value of erased storage |
kenjiArai | 0:5b88d5760320 | 166 | */ |
kenjiArai | 0:5b88d5760320 | 167 | virtual int get_erase_value() const; |
kenjiArai | 0:5b88d5760320 | 168 | |
kenjiArai | 0:5b88d5760320 | 169 | /** Get the total size of the underlying device |
kenjiArai | 0:5b88d5760320 | 170 | * |
kenjiArai | 0:5b88d5760320 | 171 | * @return Size of the underlying device in bytes |
kenjiArai | 0:5b88d5760320 | 172 | */ |
kenjiArai | 0:5b88d5760320 | 173 | virtual bd_size_t size() const; |
kenjiArai | 0:5b88d5760320 | 174 | |
kenjiArai | 0:5b88d5760320 | 175 | /** Get the BlockDevice class type. |
kenjiArai | 0:5b88d5760320 | 176 | * |
kenjiArai | 0:5b88d5760320 | 177 | * @return A string represent the BlockDevice class type. |
kenjiArai | 0:5b88d5760320 | 178 | */ |
kenjiArai | 0:5b88d5760320 | 179 | virtual const char *get_type() const; |
kenjiArai | 0:5b88d5760320 | 180 | |
kenjiArai | 0:5b88d5760320 | 181 | protected: |
kenjiArai | 0:5b88d5760320 | 182 | BlockDevice **_bds; |
kenjiArai | 0:5b88d5760320 | 183 | size_t _bd_count; |
kenjiArai | 0:5b88d5760320 | 184 | bd_size_t _read_size; |
kenjiArai | 0:5b88d5760320 | 185 | bd_size_t _program_size; |
kenjiArai | 0:5b88d5760320 | 186 | bd_size_t _erase_size; |
kenjiArai | 0:5b88d5760320 | 187 | bd_size_t _size; |
kenjiArai | 0:5b88d5760320 | 188 | int _erase_value; |
kenjiArai | 0:5b88d5760320 | 189 | uint32_t _init_ref_count; |
kenjiArai | 0:5b88d5760320 | 190 | bool _is_initialized; |
kenjiArai | 0:5b88d5760320 | 191 | }; |
kenjiArai | 0:5b88d5760320 | 192 | |
kenjiArai | 0:5b88d5760320 | 193 | } // namespace mbed |
kenjiArai | 0:5b88d5760320 | 194 | |
kenjiArai | 0:5b88d5760320 | 195 | // Added "using" for backwards compatibility |
kenjiArai | 0:5b88d5760320 | 196 | #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE |
kenjiArai | 0:5b88d5760320 | 197 | using mbed::ChainingBlockDevice; |
kenjiArai | 0:5b88d5760320 | 198 | #endif |
kenjiArai | 0:5b88d5760320 | 199 | |
kenjiArai | 0:5b88d5760320 | 200 | #endif |
kenjiArai | 0:5b88d5760320 | 201 | |
kenjiArai | 0:5b88d5760320 | 202 | /** @}*/ |