Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HeapBlockDevice.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #ifndef MBED_MEM_BLOCK_DEVICE_H 00023 #define MBED_MEM_BLOCK_DEVICE_H 00024 00025 #include "BlockDevice.h" 00026 #include "platform/mbed_assert.h" 00027 #include <string.h> 00028 #include <stdlib.h> 00029 00030 /** Lazily allocated heap-backed block device 00031 * 00032 * Useful for simulating a block device and tests 00033 * 00034 * @code 00035 * #include "mbed.h" 00036 * #include "HeapBlockDevice.h" 00037 * 00038 * #define BLOCK_SIZE 512 00039 * 00040 * HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes 00041 * uint8_t block[BLOCK_SIZE] = "Hello World!\n"; 00042 * 00043 * int main() { 00044 * bd.init(); 00045 * bd.erase(0, BLOCK_SIZE); 00046 * bd.program(block, 0, BLOCK_SIZE); 00047 * bd.read(block, 0, BLOCK_SIZE); 00048 * printf("%s", block); 00049 * bd.deinit(); 00050 * } 00051 * @endcode 00052 */ 00053 class HeapBlockDevice : public BlockDevice 00054 { 00055 public: 00056 00057 /** Lifetime of the memory block device 00058 * 00059 * @param size Size of the Block Device in bytes 00060 * @param block Block size in bytes. Minimum read, program, and erase sizes are 00061 * configured to this value 00062 */ 00063 HeapBlockDevice(bd_size_t size, bd_size_t block=512); 00064 /** Lifetime of the memory block device 00065 * 00066 * @param size Size of the Block Device in bytes 00067 * @param read Minimum read size required in bytes 00068 * @param program Minimum program size required in bytes 00069 * @param erase Minimum erase size required in bytes 00070 */ 00071 HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase); 00072 virtual ~HeapBlockDevice(); 00073 00074 /** Initialize a block device 00075 * 00076 * @return 0 on success or a negative error code on failure 00077 */ 00078 virtual int init(); 00079 00080 /** Deinitialize a block device 00081 * 00082 * @return 0 on success or a negative error code on failure 00083 */ 00084 virtual int deinit(); 00085 00086 /** Read blocks from a block device 00087 * 00088 * @param buffer Buffer to read blocks into 00089 * @param addr Address of block to begin reading from 00090 * @param size Size to read in bytes, must be a multiple of read block size 00091 * @return 0 on success, negative error code on failure 00092 */ 00093 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00094 00095 /** Program blocks to a block device 00096 * 00097 * The blocks must have been erased prior to being programmed 00098 * 00099 * @param buffer Buffer of data to write to blocks 00100 * @param addr Address of block to begin writing to 00101 * @param size Size to write in bytes, must be a multiple of program block size 00102 * @return 0 on success, negative error code on failure 00103 */ 00104 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00105 00106 /** Erase blocks on a block device 00107 * 00108 * The state of an erased block is undefined until it has been programmed 00109 * 00110 * @param addr Address of block to begin erasing 00111 * @param size Size to erase in bytes, must be a multiple of erase block size 00112 * @return 0 on success, negative error code on failure 00113 */ 00114 virtual int erase(bd_addr_t addr, bd_size_t size); 00115 00116 /** Get the size of a readable block 00117 * 00118 * @return Size of a readable block in bytes 00119 */ 00120 virtual bd_size_t get_read_size() const; 00121 00122 /** Get the size of a programmable block 00123 * 00124 * @return Size of a programmable block in bytes 00125 */ 00126 virtual bd_size_t get_program_size() const; 00127 00128 /** Get the size of an erasable block 00129 * 00130 * @return Size of an erasable block in bytes 00131 */ 00132 virtual bd_size_t get_erase_size() const; 00133 00134 /** Get the size of an erasable block given address 00135 * 00136 * @param addr Address within the erasable block 00137 * @return Size of an erasable block in bytes 00138 * @note Must be a multiple of the program size 00139 */ 00140 virtual bd_size_t get_erase_size(bd_addr_t addr) const; 00141 00142 /** Get the total size of the underlying device 00143 * 00144 * @return Size of the underlying device in bytes 00145 */ 00146 virtual bd_size_t size() const; 00147 00148 private: 00149 bd_size_t _read_size; 00150 bd_size_t _program_size; 00151 bd_size_t _erase_size; 00152 bd_size_t _count; 00153 uint8_t **_blocks; 00154 uint32_t _init_ref_count; 00155 bool _is_initialized; 00156 }; 00157 00158 00159 #endif
Generated on Tue Jul 12 2022 20:52:44 by
1.7.2