|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2012 ARM Limited |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | +#ifndef MBED_MEM_BLOCK_DEVICE_H |
| 23 | +#define MBED_MEM_BLOCK_DEVICE_H |
| 24 | + |
| 25 | +#include "BlockDevice.h" |
| 26 | +#include "mbed.h" |
| 27 | + |
| 28 | + |
| 29 | +/** Lazily allocated heap-backed block device |
| 30 | + * |
| 31 | + * Useful for simulating a block device and tests |
| 32 | + * |
| 33 | + * @code |
| 34 | + * #include "mbed.h" |
| 35 | + * #include "HeapBlockDevice.h" |
| 36 | + * |
| 37 | + * HeapBlockDevice bd(2048, 512); // 2048 bytes with a block size of 512 bytes |
| 38 | + * uint8_t block[512] = "Hello World!\n"; |
| 39 | + * |
| 40 | + * int main() { |
| 41 | + * bd.init(); |
| 42 | + * bd.write(block, 0); |
| 43 | + * bd.read(block, 0); |
| 44 | + * printf("%s", block); |
| 45 | + * bd.deinit(); |
| 46 | + * } |
| 47 | + */ |
| 48 | +class HeapBlockDevice : public BlockDevice { |
| 49 | +public: |
| 50 | + |
| 51 | + /** Lifetime of the memory block device |
| 52 | + */ |
| 53 | + HeapBlockDevice(bd_size_t size, bd_size_t block=512); |
| 54 | + HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase); |
| 55 | + virtual ~HeapBlockDevice(); |
| 56 | + |
| 57 | + /** Initialize a block device |
| 58 | + * |
| 59 | + * @return 0 on success or a negative error code on failure |
| 60 | + */ |
| 61 | + virtual bd_error_t init() = 0; |
| 62 | + |
| 63 | + /** Deinitialize a block device |
| 64 | + * |
| 65 | + * @return 0 on success or a negative error code on failure |
| 66 | + */ |
| 67 | + virtual bd_error_t deinit() = 0; |
| 68 | + |
| 69 | + /** Read blocks from a block device |
| 70 | + * |
| 71 | + * @param buffer Buffer to write blocks to |
| 72 | + * @param addr Address of block to begin reading from |
| 73 | + * @param size Size to read in bytes, must be a multiple of read block size |
| 74 | + * @return 0 on success, negative error code on failure |
| 75 | + */ |
| 76 | + virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size) = 0; |
| 77 | + |
| 78 | + /** Program blocks to a block device |
| 79 | + * |
| 80 | + * The blocks must have been erased prior to being programmed |
| 81 | + * |
| 82 | + * @param buffer Buffer of data to write to blocks |
| 83 | + * @param addr Address of block to begin writing to |
| 84 | + * @param size Size to write in bytes, must be a multiple of program block size |
| 85 | + * @return 0 on success, negative error code on failure |
| 86 | + */ |
| 87 | + virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size); |
| 88 | + |
| 89 | + /** Erase blocks on a block device |
| 90 | + * |
| 91 | + * The state of an erased block is undefined until it has been programmed |
| 92 | + * |
| 93 | + * @param addr Address of block to begin erasing |
| 94 | + * @param size Size to erase in bytes, must be a multiple of erase block size |
| 95 | + * @return 0 on success, negative error code on failure |
| 96 | + */ |
| 97 | + virtual bd_error_t erase(bd_addr_t addr, bd_size_t size); |
| 98 | + |
| 99 | + /** Get the size of a readable block |
| 100 | + * |
| 101 | + * @return Size of a readable block in bytes |
| 102 | + */ |
| 103 | + virtual bd_size_t read_size(); |
| 104 | + |
| 105 | + /** Get the size of a programable block |
| 106 | + * |
| 107 | + * @return Size of a programable block in bytes |
| 108 | + */ |
| 109 | + virtual bd_size_t program_size(); |
| 110 | + |
| 111 | + /** Get the size of a eraseable block |
| 112 | + * |
| 113 | + * @return Size of a eraseable block in bytes |
| 114 | + */ |
| 115 | + virtual bd_size_t erase_size(); |
| 116 | + |
| 117 | + /** Get the total size of the underlying device |
| 118 | + * |
| 119 | + * @return Size of the underlying device in bytes |
| 120 | + */ |
| 121 | + virtual bd_size_t size(); |
| 122 | + |
| 123 | +protected: |
| 124 | + bd_size_t _read_size; |
| 125 | + bd_size_t _program_size; |
| 126 | + bd_size_t _erase_size; |
| 127 | + bd_size_t _count; |
| 128 | + uint8_t **_blocks; |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +#endif |
0 commit comments