Opencv 3.1 project on GR-PEACH board
Fork of gr-peach-opencv-project by
features/filesystem/bd/MBRBlockDevice.h@170:54ff26da7eb6, 2017-07-04 (annotated)
- Committer:
- thedo
- Date:
- Tue Jul 04 06:23:13 2017 +0000
- Revision:
- 170:54ff26da7eb6
- Parent:
- 166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thedo | 166:3a9487d57a5c | 1 | /* mbed Microcontroller Library |
thedo | 166:3a9487d57a5c | 2 | * Copyright (c) 2017 ARM Limited |
thedo | 166:3a9487d57a5c | 3 | * |
thedo | 166:3a9487d57a5c | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
thedo | 166:3a9487d57a5c | 5 | * of this software and associated documentation files (the "Software"), to deal |
thedo | 166:3a9487d57a5c | 6 | * in the Software without restriction, including without limitation the rights |
thedo | 166:3a9487d57a5c | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
thedo | 166:3a9487d57a5c | 8 | * copies of the Software, and to permit persons to whom the Software is |
thedo | 166:3a9487d57a5c | 9 | * furnished to do so, subject to the following conditions: |
thedo | 166:3a9487d57a5c | 10 | * |
thedo | 166:3a9487d57a5c | 11 | * The above copyright notice and this permission notice shall be included in |
thedo | 166:3a9487d57a5c | 12 | * all copies or substantial portions of the Software. |
thedo | 166:3a9487d57a5c | 13 | * |
thedo | 166:3a9487d57a5c | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
thedo | 166:3a9487d57a5c | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
thedo | 166:3a9487d57a5c | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
thedo | 166:3a9487d57a5c | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
thedo | 166:3a9487d57a5c | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
thedo | 166:3a9487d57a5c | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
thedo | 166:3a9487d57a5c | 20 | * SOFTWARE. |
thedo | 166:3a9487d57a5c | 21 | */ |
thedo | 166:3a9487d57a5c | 22 | #ifndef MBED_MBR_BLOCK_DEVICE_H |
thedo | 166:3a9487d57a5c | 23 | #define MBED_MBR_BLOCK_DEVICE_H |
thedo | 166:3a9487d57a5c | 24 | |
thedo | 166:3a9487d57a5c | 25 | #include "BlockDevice.h" |
thedo | 166:3a9487d57a5c | 26 | #include "mbed.h" |
thedo | 166:3a9487d57a5c | 27 | |
thedo | 166:3a9487d57a5c | 28 | |
thedo | 166:3a9487d57a5c | 29 | /** Additional error codes used for MBR records |
thedo | 166:3a9487d57a5c | 30 | */ |
thedo | 166:3a9487d57a5c | 31 | enum { |
thedo | 166:3a9487d57a5c | 32 | BD_ERROR_INVALID_MBR = -3101, |
thedo | 166:3a9487d57a5c | 33 | BD_ERROR_INVALID_PARTITION = -3102, |
thedo | 166:3a9487d57a5c | 34 | }; |
thedo | 166:3a9487d57a5c | 35 | |
thedo | 166:3a9487d57a5c | 36 | |
thedo | 166:3a9487d57a5c | 37 | /** Block device for managing a Master Boot Record |
thedo | 166:3a9487d57a5c | 38 | * https://en.wikipedia.org/wiki/Master_boot_record |
thedo | 166:3a9487d57a5c | 39 | * |
thedo | 166:3a9487d57a5c | 40 | * Here is an example of partitioning a heap backed block device |
thedo | 166:3a9487d57a5c | 41 | * @code |
thedo | 166:3a9487d57a5c | 42 | * #include "mbed.h" |
thedo | 166:3a9487d57a5c | 43 | * #include "HeapBlockDevice.h" |
thedo | 166:3a9487d57a5c | 44 | * #include "MBRBlockDevice.h" |
thedo | 166:3a9487d57a5c | 45 | * |
thedo | 166:3a9487d57a5c | 46 | * // Create a block device with 64 blocks of size 512 |
thedo | 166:3a9487d57a5c | 47 | * HeapBlockDevice mem(64*512, 512); |
thedo | 166:3a9487d57a5c | 48 | * |
thedo | 166:3a9487d57a5c | 49 | * // Partition into two partitions with ~half the blocks |
thedo | 166:3a9487d57a5c | 50 | * MBRBlockDevice::partition(&mem, 1, 0x83, 0*512, 32*512); |
thedo | 166:3a9487d57a5c | 51 | * MBRBlockDevice::partition(&mem, 2, 0x83, 32*512); |
thedo | 166:3a9487d57a5c | 52 | * |
thedo | 166:3a9487d57a5c | 53 | * // Create a block device that maps to the first 32 blocks (excluding MBR block) |
thedo | 166:3a9487d57a5c | 54 | * MBRBlockDevice part1(&mem, 1); |
thedo | 166:3a9487d57a5c | 55 | * |
thedo | 166:3a9487d57a5c | 56 | * // Create a block device that maps to the last 32 blocks |
thedo | 166:3a9487d57a5c | 57 | * MBRBlockDevice part2(&mem, 2); |
thedo | 166:3a9487d57a5c | 58 | * @endcode |
thedo | 166:3a9487d57a5c | 59 | * |
thedo | 166:3a9487d57a5c | 60 | * Here is a more realistic example where the MBRBlockDevice is used |
thedo | 166:3a9487d57a5c | 61 | * to partition a region of space on an SD card. When plugged into a computer, |
thedo | 166:3a9487d57a5c | 62 | * the partitions will be recognized appropriately. |
thedo | 166:3a9487d57a5c | 63 | * @code |
thedo | 166:3a9487d57a5c | 64 | * #include "mbed.h" |
thedo | 166:3a9487d57a5c | 65 | * #include "SDBlockDevice.h" |
thedo | 166:3a9487d57a5c | 66 | * #include "MBRBlockDevice.h" |
thedo | 166:3a9487d57a5c | 67 | * #include "FATFileSystem.h" |
thedo | 166:3a9487d57a5c | 68 | * |
thedo | 166:3a9487d57a5c | 69 | * // Create an SD card |
thedo | 166:3a9487d57a5c | 70 | * SDBlockDevice sd(s0, s1, s2, s3); |
thedo | 166:3a9487d57a5c | 71 | * |
thedo | 166:3a9487d57a5c | 72 | * // Create a partition with 1 GB of space |
thedo | 166:3a9487d57a5c | 73 | * MBRBlockDevice::partition(&sd, 1, 0x83, 0, 1024*1024); |
thedo | 166:3a9487d57a5c | 74 | * |
thedo | 166:3a9487d57a5c | 75 | * // Create the block device that represents the partition |
thedo | 166:3a9487d57a5c | 76 | * MBRBlockDevice part1(&sd, 1); |
thedo | 166:3a9487d57a5c | 77 | * |
thedo | 166:3a9487d57a5c | 78 | * // Format the partition with a FAT filesystem |
thedo | 166:3a9487d57a5c | 79 | * FATFileSystem::format(&part1); |
thedo | 166:3a9487d57a5c | 80 | * |
thedo | 166:3a9487d57a5c | 81 | * // Create the FAT filesystem instance, files can now be written to |
thedo | 166:3a9487d57a5c | 82 | * // the FAT filesystem in partition 1 |
thedo | 166:3a9487d57a5c | 83 | * FATFileSystem fat("fat", &part1); |
thedo | 166:3a9487d57a5c | 84 | * @endcode |
thedo | 166:3a9487d57a5c | 85 | */ |
thedo | 166:3a9487d57a5c | 86 | class MBRBlockDevice : public BlockDevice |
thedo | 166:3a9487d57a5c | 87 | { |
thedo | 166:3a9487d57a5c | 88 | public: |
thedo | 166:3a9487d57a5c | 89 | /** Format the MBR to contain the following partition |
thedo | 166:3a9487d57a5c | 90 | * |
thedo | 166:3a9487d57a5c | 91 | * @param bd Block device to partition |
thedo | 166:3a9487d57a5c | 92 | * @param part Partition to use, 1-4 |
thedo | 166:3a9487d57a5c | 93 | * @param type 8-bit partition type to identitfy partition's contents |
thedo | 166:3a9487d57a5c | 94 | * @param start Start block address to map to block 0 of partition, |
thedo | 166:3a9487d57a5c | 95 | * negative addresses are calculated from the end of the |
thedo | 166:3a9487d57a5c | 96 | * underlying block devices. Block 0 is implicitly ignored |
thedo | 166:3a9487d57a5c | 97 | * from the range to store the MBR. |
thedo | 166:3a9487d57a5c | 98 | * @return 0 on success or a negative error code on failure |
thedo | 166:3a9487d57a5c | 99 | * @note This is the same as partition(bd, part, type, start, bd->size()) |
thedo | 166:3a9487d57a5c | 100 | */ |
thedo | 166:3a9487d57a5c | 101 | static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start); |
thedo | 166:3a9487d57a5c | 102 | |
thedo | 166:3a9487d57a5c | 103 | /** Format the MBR to contain the following partition |
thedo | 166:3a9487d57a5c | 104 | * |
thedo | 166:3a9487d57a5c | 105 | * @param bd Block device to partition |
thedo | 166:3a9487d57a5c | 106 | * @param part Partition to use, 1-4 |
thedo | 166:3a9487d57a5c | 107 | * @param type 8-bit partition type to identitfy partition's contents |
thedo | 166:3a9487d57a5c | 108 | * @param start Start block address to map to block 0 of partition, |
thedo | 166:3a9487d57a5c | 109 | * negative addresses are calculated from the end of the |
thedo | 166:3a9487d57a5c | 110 | * underlying block devices. Block 0 is implicitly ignored |
thedo | 166:3a9487d57a5c | 111 | * from the range to store the MBR. |
thedo | 166:3a9487d57a5c | 112 | * @param stop End block address to mark the end of the partition, |
thedo | 166:3a9487d57a5c | 113 | * this block is not mapped, negative addresses are calculated |
thedo | 166:3a9487d57a5c | 114 | * from the end of the underlying block device. |
thedo | 166:3a9487d57a5c | 115 | * @return 0 on success or a negative error code on failure |
thedo | 166:3a9487d57a5c | 116 | */ |
thedo | 166:3a9487d57a5c | 117 | static int partition(BlockDevice *bd, int part, uint8_t type, bd_addr_t start, bd_addr_t stop); |
thedo | 166:3a9487d57a5c | 118 | |
thedo | 166:3a9487d57a5c | 119 | /** Lifetime of the block device |
thedo | 166:3a9487d57a5c | 120 | * |
thedo | 166:3a9487d57a5c | 121 | * @param bd Block device to back the MBRBlockDevice |
thedo | 166:3a9487d57a5c | 122 | * @param part Partition to use, 1-4 |
thedo | 166:3a9487d57a5c | 123 | */ |
thedo | 166:3a9487d57a5c | 124 | MBRBlockDevice(BlockDevice *bd, int part); |
thedo | 166:3a9487d57a5c | 125 | |
thedo | 166:3a9487d57a5c | 126 | /** Lifetime of the block device |
thedo | 166:3a9487d57a5c | 127 | */ |
thedo | 166:3a9487d57a5c | 128 | virtual ~MBRBlockDevice() {}; |
thedo | 166:3a9487d57a5c | 129 | |
thedo | 166:3a9487d57a5c | 130 | /** Initialize a block device |
thedo | 166:3a9487d57a5c | 131 | * |
thedo | 166:3a9487d57a5c | 132 | * @return 0 on success or a negative error code on failure |
thedo | 166:3a9487d57a5c | 133 | */ |
thedo | 166:3a9487d57a5c | 134 | virtual int init(); |
thedo | 166:3a9487d57a5c | 135 | |
thedo | 166:3a9487d57a5c | 136 | /** Deinitialize a block device |
thedo | 166:3a9487d57a5c | 137 | * |
thedo | 166:3a9487d57a5c | 138 | * @return 0 on success or a negative error code on failure |
thedo | 166:3a9487d57a5c | 139 | */ |
thedo | 166:3a9487d57a5c | 140 | virtual int deinit(); |
thedo | 166:3a9487d57a5c | 141 | |
thedo | 166:3a9487d57a5c | 142 | /** Read blocks from a block device |
thedo | 166:3a9487d57a5c | 143 | * |
thedo | 166:3a9487d57a5c | 144 | * @param buffer Buffer to read blocks into |
thedo | 166:3a9487d57a5c | 145 | * @param addr Address of block to begin reading from |
thedo | 166:3a9487d57a5c | 146 | * @param size Size to read in bytes, must be a multiple of read block size |
thedo | 166:3a9487d57a5c | 147 | * @return 0 on success, negative error code on failure |
thedo | 166:3a9487d57a5c | 148 | */ |
thedo | 166:3a9487d57a5c | 149 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
thedo | 166:3a9487d57a5c | 150 | |
thedo | 166:3a9487d57a5c | 151 | /** Program blocks to a block device |
thedo | 166:3a9487d57a5c | 152 | * |
thedo | 166:3a9487d57a5c | 153 | * The blocks must have been erased prior to being programmed |
thedo | 166:3a9487d57a5c | 154 | * |
thedo | 166:3a9487d57a5c | 155 | * @param buffer Buffer of data to write to blocks |
thedo | 166:3a9487d57a5c | 156 | * @param addr Address of block to begin writing to |
thedo | 166:3a9487d57a5c | 157 | * @param size Size to write in bytes, must be a multiple of program block size |
thedo | 166:3a9487d57a5c | 158 | * @return 0 on success, negative error code on failure |
thedo | 166:3a9487d57a5c | 159 | */ |
thedo | 166:3a9487d57a5c | 160 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
thedo | 166:3a9487d57a5c | 161 | |
thedo | 166:3a9487d57a5c | 162 | /** Erase blocks on a block device |
thedo | 166:3a9487d57a5c | 163 | * |
thedo | 166:3a9487d57a5c | 164 | * The state of an erased block is undefined until it has been programmed |
thedo | 166:3a9487d57a5c | 165 | * |
thedo | 166:3a9487d57a5c | 166 | * @param addr Address of block to begin erasing |
thedo | 166:3a9487d57a5c | 167 | * @param size Size to erase in bytes, must be a multiple of erase block size |
thedo | 166:3a9487d57a5c | 168 | * @return 0 on success, negative error code on failure |
thedo | 166:3a9487d57a5c | 169 | */ |
thedo | 166:3a9487d57a5c | 170 | virtual int erase(bd_addr_t addr, bd_size_t size); |
thedo | 166:3a9487d57a5c | 171 | |
thedo | 166:3a9487d57a5c | 172 | /** Get the size of a readable block |
thedo | 166:3a9487d57a5c | 173 | * |
thedo | 166:3a9487d57a5c | 174 | * @return Size of a readable block in bytes |
thedo | 166:3a9487d57a5c | 175 | */ |
thedo | 166:3a9487d57a5c | 176 | virtual bd_size_t get_read_size() const; |
thedo | 166:3a9487d57a5c | 177 | |
thedo | 166:3a9487d57a5c | 178 | /** Get the size of a programable block |
thedo | 166:3a9487d57a5c | 179 | * |
thedo | 166:3a9487d57a5c | 180 | * @return Size of a programable block in bytes |
thedo | 166:3a9487d57a5c | 181 | * @note Must be a multiple of the read size |
thedo | 166:3a9487d57a5c | 182 | */ |
thedo | 166:3a9487d57a5c | 183 | virtual bd_size_t get_program_size() const; |
thedo | 166:3a9487d57a5c | 184 | |
thedo | 166:3a9487d57a5c | 185 | /** Get the size of a eraseable block |
thedo | 166:3a9487d57a5c | 186 | * |
thedo | 166:3a9487d57a5c | 187 | * @return Size of a eraseable block in bytes |
thedo | 166:3a9487d57a5c | 188 | * @note Must be a multiple of the program size |
thedo | 166:3a9487d57a5c | 189 | */ |
thedo | 166:3a9487d57a5c | 190 | virtual bd_size_t get_erase_size() const; |
thedo | 166:3a9487d57a5c | 191 | |
thedo | 166:3a9487d57a5c | 192 | /** Get the total size of the underlying device |
thedo | 166:3a9487d57a5c | 193 | * |
thedo | 166:3a9487d57a5c | 194 | * @return Size of the underlying device in bytes |
thedo | 166:3a9487d57a5c | 195 | */ |
thedo | 166:3a9487d57a5c | 196 | virtual bd_size_t size() const; |
thedo | 166:3a9487d57a5c | 197 | |
thedo | 166:3a9487d57a5c | 198 | /** Get the offset of the partition on the underlying block device |
thedo | 166:3a9487d57a5c | 199 | * @return Offset of the partition on the underlying device |
thedo | 166:3a9487d57a5c | 200 | */ |
thedo | 166:3a9487d57a5c | 201 | virtual bd_addr_t get_partition_start() const; |
thedo | 166:3a9487d57a5c | 202 | |
thedo | 166:3a9487d57a5c | 203 | /** Get size of partition on underlying block device |
thedo | 166:3a9487d57a5c | 204 | * @return Size of the partition on the underlying device |
thedo | 166:3a9487d57a5c | 205 | */ |
thedo | 166:3a9487d57a5c | 206 | virtual bd_addr_t get_partition_stop() const; |
thedo | 166:3a9487d57a5c | 207 | |
thedo | 166:3a9487d57a5c | 208 | /** Get 8-bit type of the partition |
thedo | 166:3a9487d57a5c | 209 | * @return 8-bit type of partition assigned during format |
thedo | 166:3a9487d57a5c | 210 | */ |
thedo | 166:3a9487d57a5c | 211 | virtual uint8_t get_partition_type() const; |
thedo | 166:3a9487d57a5c | 212 | |
thedo | 166:3a9487d57a5c | 213 | /** Get the partition number |
thedo | 166:3a9487d57a5c | 214 | * @return The partition number, 1-4 |
thedo | 166:3a9487d57a5c | 215 | */ |
thedo | 166:3a9487d57a5c | 216 | virtual int get_partition_number() const; |
thedo | 166:3a9487d57a5c | 217 | |
thedo | 166:3a9487d57a5c | 218 | protected: |
thedo | 166:3a9487d57a5c | 219 | BlockDevice *_bd; |
thedo | 166:3a9487d57a5c | 220 | bd_size_t _offset; |
thedo | 166:3a9487d57a5c | 221 | bd_size_t _size; |
thedo | 166:3a9487d57a5c | 222 | uint8_t _type; |
thedo | 166:3a9487d57a5c | 223 | uint8_t _part; |
thedo | 166:3a9487d57a5c | 224 | }; |
thedo | 166:3a9487d57a5c | 225 | |
thedo | 166:3a9487d57a5c | 226 | |
thedo | 166:3a9487d57a5c | 227 | #endif |
thedo | 166:3a9487d57a5c | 228 |