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