mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
features/filesystem/bd/BlockDevice.h@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | /* mbed Microcontroller Library |
dkato | 0:f782d9c66c49 | 2 | * Copyright (c) 2017 ARM Limited |
dkato | 0:f782d9c66c49 | 3 | * |
dkato | 0:f782d9c66c49 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:f782d9c66c49 | 5 | * you may not use this file except in compliance with the License. |
dkato | 0:f782d9c66c49 | 6 | * You may obtain a copy of the License at |
dkato | 0:f782d9c66c49 | 7 | * |
dkato | 0:f782d9c66c49 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:f782d9c66c49 | 9 | * |
dkato | 0:f782d9c66c49 | 10 | * Unless required by applicable law or agreed to in writing, software |
dkato | 0:f782d9c66c49 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:f782d9c66c49 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:f782d9c66c49 | 13 | * See the License for the specific language governing permissions and |
dkato | 0:f782d9c66c49 | 14 | * limitations under the License. |
dkato | 0:f782d9c66c49 | 15 | */ |
dkato | 0:f782d9c66c49 | 16 | |
dkato | 0:f782d9c66c49 | 17 | #ifndef MBED_BLOCK_DEVICE_H |
dkato | 0:f782d9c66c49 | 18 | #define MBED_BLOCK_DEVICE_H |
dkato | 0:f782d9c66c49 | 19 | |
dkato | 0:f782d9c66c49 | 20 | #include <stdint.h> |
dkato | 0:f782d9c66c49 | 21 | |
dkato | 0:f782d9c66c49 | 22 | |
dkato | 0:f782d9c66c49 | 23 | /** Enum of standard error codes |
dkato | 0:f782d9c66c49 | 24 | * |
dkato | 0:f782d9c66c49 | 25 | * @enum bd_error |
dkato | 0:f782d9c66c49 | 26 | */ |
dkato | 0:f782d9c66c49 | 27 | enum bd_error { |
dkato | 0:f782d9c66c49 | 28 | BD_ERROR_OK = 0, /*!< no error */ |
dkato | 0:f782d9c66c49 | 29 | BD_ERROR_DEVICE_ERROR = -4001, /*!< device specific error */ |
dkato | 0:f782d9c66c49 | 30 | }; |
dkato | 0:f782d9c66c49 | 31 | |
dkato | 0:f782d9c66c49 | 32 | /** Type representing the address of a specific block |
dkato | 0:f782d9c66c49 | 33 | */ |
dkato | 0:f782d9c66c49 | 34 | typedef uint64_t bd_addr_t; |
dkato | 0:f782d9c66c49 | 35 | |
dkato | 0:f782d9c66c49 | 36 | /** Type representing a quantity of 8-bit bytes |
dkato | 0:f782d9c66c49 | 37 | */ |
dkato | 0:f782d9c66c49 | 38 | typedef uint64_t bd_size_t; |
dkato | 0:f782d9c66c49 | 39 | |
dkato | 0:f782d9c66c49 | 40 | |
dkato | 0:f782d9c66c49 | 41 | /** A hardware device capable of writing and reading blocks |
dkato | 0:f782d9c66c49 | 42 | */ |
dkato | 0:f782d9c66c49 | 43 | class BlockDevice |
dkato | 0:f782d9c66c49 | 44 | { |
dkato | 0:f782d9c66c49 | 45 | public: |
dkato | 0:f782d9c66c49 | 46 | /** Lifetime of a block device |
dkato | 0:f782d9c66c49 | 47 | */ |
dkato | 0:f782d9c66c49 | 48 | virtual ~BlockDevice() {}; |
dkato | 0:f782d9c66c49 | 49 | |
dkato | 0:f782d9c66c49 | 50 | /** Initialize a block device |
dkato | 0:f782d9c66c49 | 51 | * |
dkato | 0:f782d9c66c49 | 52 | * @return 0 on success or a negative error code on failure |
dkato | 0:f782d9c66c49 | 53 | */ |
dkato | 0:f782d9c66c49 | 54 | virtual int init() = 0; |
dkato | 0:f782d9c66c49 | 55 | |
dkato | 0:f782d9c66c49 | 56 | /** Deinitialize a block device |
dkato | 0:f782d9c66c49 | 57 | * |
dkato | 0:f782d9c66c49 | 58 | * @return 0 on success or a negative error code on failure |
dkato | 0:f782d9c66c49 | 59 | */ |
dkato | 0:f782d9c66c49 | 60 | virtual int deinit() = 0; |
dkato | 0:f782d9c66c49 | 61 | |
dkato | 0:f782d9c66c49 | 62 | /** Read blocks from a block device |
dkato | 0:f782d9c66c49 | 63 | * |
dkato | 0:f782d9c66c49 | 64 | * If a failure occurs, it is not possible to determine how many bytes succeeded |
dkato | 0:f782d9c66c49 | 65 | * |
dkato | 0:f782d9c66c49 | 66 | * @param buffer Buffer to write blocks to |
dkato | 0:f782d9c66c49 | 67 | * @param addr Address of block to begin reading from |
dkato | 0:f782d9c66c49 | 68 | * @param size Size to read in bytes, must be a multiple of read block size |
dkato | 0:f782d9c66c49 | 69 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 70 | */ |
dkato | 0:f782d9c66c49 | 71 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size) = 0; |
dkato | 0:f782d9c66c49 | 72 | |
dkato | 0:f782d9c66c49 | 73 | /** Program blocks to a block device |
dkato | 0:f782d9c66c49 | 74 | * |
dkato | 0:f782d9c66c49 | 75 | * The blocks must have been erased prior to being programmed |
dkato | 0:f782d9c66c49 | 76 | * |
dkato | 0:f782d9c66c49 | 77 | * If a failure occurs, it is not possible to determine how many bytes succeeded |
dkato | 0:f782d9c66c49 | 78 | * |
dkato | 0:f782d9c66c49 | 79 | * @param buffer Buffer of data to write to blocks |
dkato | 0:f782d9c66c49 | 80 | * @param addr Address of block to begin writing to |
dkato | 0:f782d9c66c49 | 81 | * @param size Size to write in bytes, must be a multiple of program block size |
dkato | 0:f782d9c66c49 | 82 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 83 | */ |
dkato | 0:f782d9c66c49 | 84 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size) = 0; |
dkato | 0:f782d9c66c49 | 85 | |
dkato | 0:f782d9c66c49 | 86 | /** Erase blocks on a block device |
dkato | 0:f782d9c66c49 | 87 | * |
dkato | 0:f782d9c66c49 | 88 | * The state of an erased block is undefined until it has been programmed |
dkato | 0:f782d9c66c49 | 89 | * |
dkato | 0:f782d9c66c49 | 90 | * @param addr Address of block to begin erasing |
dkato | 0:f782d9c66c49 | 91 | * @param size Size to erase in bytes, must be a multiple of erase block size |
dkato | 0:f782d9c66c49 | 92 | * @return 0 on success, negative error code on failure |
dkato | 0:f782d9c66c49 | 93 | */ |
dkato | 0:f782d9c66c49 | 94 | virtual int erase(bd_addr_t addr, bd_size_t size) = 0; |
dkato | 0:f782d9c66c49 | 95 | |
dkato | 0:f782d9c66c49 | 96 | /** Get the size of a readable block |
dkato | 0:f782d9c66c49 | 97 | * |
dkato | 0:f782d9c66c49 | 98 | * @return Size of a readable block in bytes |
dkato | 0:f782d9c66c49 | 99 | */ |
dkato | 0:f782d9c66c49 | 100 | virtual bd_size_t get_read_size() const = 0; |
dkato | 0:f782d9c66c49 | 101 | |
dkato | 0:f782d9c66c49 | 102 | /** Get the size of a programable block |
dkato | 0:f782d9c66c49 | 103 | * |
dkato | 0:f782d9c66c49 | 104 | * @return Size of a programable block in bytes |
dkato | 0:f782d9c66c49 | 105 | * @note Must be a multiple of the read size |
dkato | 0:f782d9c66c49 | 106 | */ |
dkato | 0:f782d9c66c49 | 107 | virtual bd_size_t get_program_size() const = 0; |
dkato | 0:f782d9c66c49 | 108 | |
dkato | 0:f782d9c66c49 | 109 | /** Get the size of a eraseable block |
dkato | 0:f782d9c66c49 | 110 | * |
dkato | 0:f782d9c66c49 | 111 | * @return Size of a eraseable block in bytes |
dkato | 0:f782d9c66c49 | 112 | * @note Must be a multiple of the program size |
dkato | 0:f782d9c66c49 | 113 | */ |
dkato | 0:f782d9c66c49 | 114 | virtual bd_size_t get_erase_size() const = 0; |
dkato | 0:f782d9c66c49 | 115 | |
dkato | 0:f782d9c66c49 | 116 | /** Get the total size of the underlying device |
dkato | 0:f782d9c66c49 | 117 | * |
dkato | 0:f782d9c66c49 | 118 | * @return Size of the underlying device in bytes |
dkato | 0:f782d9c66c49 | 119 | */ |
dkato | 0:f782d9c66c49 | 120 | virtual bd_size_t size() const = 0; |
dkato | 0:f782d9c66c49 | 121 | |
dkato | 0:f782d9c66c49 | 122 | /** Convenience function for checking block read validity |
dkato | 0:f782d9c66c49 | 123 | * |
dkato | 0:f782d9c66c49 | 124 | * @param addr Address of block to begin reading from |
dkato | 0:f782d9c66c49 | 125 | * @param size Size to read in bytes |
dkato | 0:f782d9c66c49 | 126 | * @return True if read is valid for underlying block device |
dkato | 0:f782d9c66c49 | 127 | */ |
dkato | 0:f782d9c66c49 | 128 | bool is_valid_read(bd_addr_t addr, bd_size_t size) const |
dkato | 0:f782d9c66c49 | 129 | { |
dkato | 0:f782d9c66c49 | 130 | return ( |
dkato | 0:f782d9c66c49 | 131 | addr % get_read_size() == 0 && |
dkato | 0:f782d9c66c49 | 132 | size % get_read_size() == 0 && |
dkato | 0:f782d9c66c49 | 133 | addr + size <= this->size()); |
dkato | 0:f782d9c66c49 | 134 | } |
dkato | 0:f782d9c66c49 | 135 | |
dkato | 0:f782d9c66c49 | 136 | /** Convenience function for checking block program validity |
dkato | 0:f782d9c66c49 | 137 | * |
dkato | 0:f782d9c66c49 | 138 | * @param addr Address of block to begin writing to |
dkato | 0:f782d9c66c49 | 139 | * @param size Size to write in bytes |
dkato | 0:f782d9c66c49 | 140 | * @return True if program is valid for underlying block device |
dkato | 0:f782d9c66c49 | 141 | */ |
dkato | 0:f782d9c66c49 | 142 | bool is_valid_program(bd_addr_t addr, bd_size_t size) const |
dkato | 0:f782d9c66c49 | 143 | { |
dkato | 0:f782d9c66c49 | 144 | return ( |
dkato | 0:f782d9c66c49 | 145 | addr % get_program_size() == 0 && |
dkato | 0:f782d9c66c49 | 146 | size % get_program_size() == 0 && |
dkato | 0:f782d9c66c49 | 147 | addr + size <= this->size()); |
dkato | 0:f782d9c66c49 | 148 | } |
dkato | 0:f782d9c66c49 | 149 | |
dkato | 0:f782d9c66c49 | 150 | /** Convenience function for checking block erase validity |
dkato | 0:f782d9c66c49 | 151 | * |
dkato | 0:f782d9c66c49 | 152 | * @param addr Address of block to begin erasing |
dkato | 0:f782d9c66c49 | 153 | * @param size Size to erase in bytes |
dkato | 0:f782d9c66c49 | 154 | * @return True if erase is valid for underlying block device |
dkato | 0:f782d9c66c49 | 155 | */ |
dkato | 0:f782d9c66c49 | 156 | bool is_valid_erase(bd_addr_t addr, bd_size_t size) const |
dkato | 0:f782d9c66c49 | 157 | { |
dkato | 0:f782d9c66c49 | 158 | return ( |
dkato | 0:f782d9c66c49 | 159 | addr % get_erase_size() == 0 && |
dkato | 0:f782d9c66c49 | 160 | size % get_erase_size() == 0 && |
dkato | 0:f782d9c66c49 | 161 | addr + size <= this->size()); |
dkato | 0:f782d9c66c49 | 162 | } |
dkato | 0:f782d9c66c49 | 163 | }; |
dkato | 0:f782d9c66c49 | 164 | |
dkato | 0:f782d9c66c49 | 165 | |
dkato | 0:f782d9c66c49 | 166 | #endif |