|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2016 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef MBED_BLOCK_DEVICE_H |
| 18 | +#define MBED_BLOCK_DEVICE_H |
| 19 | + |
| 20 | +#include <stdint.h> |
| 21 | + |
| 22 | + |
| 23 | +/** Enum of standard error codes |
| 24 | + * |
| 25 | + * @enum bd_error_t |
| 26 | + */ |
| 27 | +enum bd_error { |
| 28 | + BD_ERROR_OK = 0, /*!< no error */ |
| 29 | + BD_ERROR_WOULD_BLOCK = -4001, /*!< operation would block */ |
| 30 | + BD_ERROR_UNSUPPORTED = -4002, /*!< unsupported operation */ |
| 31 | + BD_ERROR_PARAMETER = -4003, /*!< invalid parameter */ |
| 32 | + BD_ERROR_NO_INIT = -4004, /*!< uninitialized */ |
| 33 | + BD_ERROR_NO_DEVICE = -4005, /*!< device is missing or not connected */ |
| 34 | + BD_ERROR_WRITE_PROTECTED = -4006, /*!< write protected */ |
| 35 | + BD_ERROR_DEVICE_ERROR = -4007, /*!< device specific error */ |
| 36 | +}; |
| 37 | + |
| 38 | +/** Type representing either 0 or a negative error code |
| 39 | + */ |
| 40 | +typedef int32_t bd_error_t; |
| 41 | + |
| 42 | +/** Type representing the address of a specific block |
| 43 | + */ |
| 44 | +typedef uint64_t bd_addr_t; |
| 45 | + |
| 46 | +/** Type representing a quantity of 8-bit bytes |
| 47 | + */ |
| 48 | +typedef uint64_t bd_size_t; |
| 49 | + |
| 50 | + |
| 51 | +/** A hardware device capable of writing and reading blocks |
| 52 | + */ |
| 53 | +class BlockDevice { |
| 54 | +public: |
| 55 | + /** Lifetime of a block device |
| 56 | + */ |
| 57 | + virtual ~BlockDevice() {}; |
| 58 | + |
| 59 | + /** Initialize a block device |
| 60 | + * |
| 61 | + * @return 0 on success or a negative error code on failure |
| 62 | + */ |
| 63 | + virtual bd_error_t init() = 0; |
| 64 | + |
| 65 | + /** Deinitialize a block device |
| 66 | + * |
| 67 | + * @return 0 on success or a negative error code on failure |
| 68 | + */ |
| 69 | + virtual bd_error_t deinit() = 0; |
| 70 | + |
| 71 | + /** Read blocks from a block device |
| 72 | + * |
| 73 | + * @param buffer Buffer to write blocks to |
| 74 | + * @param addr Address of block to begin reading from |
| 75 | + * @param size Size to read in bytes, must be a multiple of read block size |
| 76 | + * @return 0 on success, negative error code on failure |
| 77 | + */ |
| 78 | + virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size) = 0; |
| 79 | + |
| 80 | + /** Write blocks to a block device |
| 81 | + * |
| 82 | + * A write is equivalent to an erase followed by a program |
| 83 | + * |
| 84 | + * @param buffer Buffer of data to write to blocks |
| 85 | + * @param addr Address of block to begin writing to |
| 86 | + * @param size Size to write in bytes, must be a multiple of write block size |
| 87 | + * @return 0 on success, negative error code on failure |
| 88 | + */ |
| 89 | + virtual bd_error_t write(const void *buffer, bd_addr_t addr, bd_size_t size); |
| 90 | + |
| 91 | + /** Program blocks to a block device |
| 92 | + * |
| 93 | + * The blocks must have been erased prior to being programmed |
| 94 | + * |
| 95 | + * @param buffer Buffer of data to write to blocks |
| 96 | + * @param addr Address of block to begin writing to |
| 97 | + * @param size Size to write in bytes, must be a multiple of program block size |
| 98 | + * @return 0 on success, negative error code on failure |
| 99 | + */ |
| 100 | + virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size) = 0; |
| 101 | + |
| 102 | + /** Erase blocks on a block device |
| 103 | + * |
| 104 | + * The state of an erased block is undefined until it has been programmed |
| 105 | + * |
| 106 | + * @param addr Address of block to begin erasing |
| 107 | + * @param size Size to erase in bytes, must be a multiple of erase block size |
| 108 | + * @return 0 on success, negative error code on failure |
| 109 | + */ |
| 110 | + virtual bd_error_t erase(bd_addr_t addr, bd_size_t size) = 0; |
| 111 | + |
| 112 | + /** Get the size of a readable block |
| 113 | + * |
| 114 | + * @return Size of a readable block in bytes |
| 115 | + */ |
| 116 | + virtual bd_size_t read_size() = 0; |
| 117 | + |
| 118 | + /** Get the size of a writeable block |
| 119 | + * |
| 120 | + * @return Size of a writeable block in bytes |
| 121 | + * @note Must be a multiple of the read size, this is |
| 122 | + * equivalent to the erase size of the device |
| 123 | + */ |
| 124 | + virtual bd_size_t write_size(); |
| 125 | + |
| 126 | + /** Get the size of a programable block |
| 127 | + * |
| 128 | + * @return Size of a programable block in bytes |
| 129 | + * @note Must be a multiple of the read size |
| 130 | + */ |
| 131 | + virtual bd_size_t program_size() = 0; |
| 132 | + |
| 133 | + /** Get the size of a eraseable block |
| 134 | + * |
| 135 | + * @return Size of a eraseable block in bytes |
| 136 | + * @note Must be a multiple of the program size |
| 137 | + */ |
| 138 | + virtual bd_size_t erase_size() = 0; |
| 139 | + |
| 140 | + /** Get the total size of the underlying device |
| 141 | + * |
| 142 | + * @return Size of the underlying device in bytes |
| 143 | + */ |
| 144 | + virtual bd_size_t size() = 0; |
| 145 | + |
| 146 | + /** Convenience function for checking block read validity |
| 147 | + * |
| 148 | + * @param addr Address of block to begin reading from |
| 149 | + * @param size Size to read in bytes |
| 150 | + * @return True if read is valid for underlying block device |
| 151 | + */ |
| 152 | + bool is_valid_read(bd_addr_t addr, bd_size_t size); |
| 153 | + |
| 154 | + /** Convenience function for checking block write validity |
| 155 | + * |
| 156 | + * @param addr Address of block to begin writing to |
| 157 | + * @param size Size to write in bytes |
| 158 | + * @return True if write is valid for underlying block device |
| 159 | + */ |
| 160 | + bool is_valid_write(bd_addr_t addr, bd_size_t size); |
| 161 | + |
| 162 | + /** Convenience function for checking block program validity |
| 163 | + * |
| 164 | + * @param addr Address of block to begin writing to |
| 165 | + * @param size Size to write in bytes |
| 166 | + * @return True if program is valid for underlying block device |
| 167 | + */ |
| 168 | + bool is_valid_program(bd_addr_t addr, bd_size_t size); |
| 169 | + |
| 170 | + /** Convenience function for checking block erase validity |
| 171 | + * |
| 172 | + * @param addr Address of block to begin erasing |
| 173 | + * @param size Size to erase in bytes |
| 174 | + * @return True if erase is valid for underlying block device |
| 175 | + */ |
| 176 | + bool is_valid_erase(bd_addr_t addr, bd_size_t size); |
| 177 | +}; |
| 178 | + |
| 179 | + |
| 180 | +#endif |
0 commit comments