Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HeapBlockDevice.h Source File

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 
00023 /** \addtogroup storage */
00024 /** @{*/
00025 
00026 #ifndef MBED_MEM_BLOCK_DEVICE_H
00027 #define MBED_MEM_BLOCK_DEVICE_H
00028 
00029 #include "BlockDevice.h"
00030 #include "platform/mbed_assert.h"
00031 #include <string.h>
00032 #include <stdlib.h>
00033 
00034 namespace mbed {
00035 
00036 /** Lazily allocated heap-backed block device
00037  *
00038  * Useful for simulating a block device and tests
00039  *
00040  * @code
00041  * #include "mbed.h"
00042  * #include "HeapBlockDevice.h"
00043  *
00044  * #define BLOCK_SIZE 512
00045  *
00046  * HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes
00047  * uint8_t block[BLOCK_SIZE] = "Hello World!\n";
00048  *
00049  * int main() {
00050  *     bd.init();
00051  *     bd.erase(0, BLOCK_SIZE);
00052  *     bd.program(block, 0, BLOCK_SIZE);
00053  *     bd.read(block, 0, BLOCK_SIZE);
00054  *     printf("%s", block);
00055  *     bd.deinit();
00056  * }
00057  * @endcode
00058  */
00059 class HeapBlockDevice : public BlockDevice {
00060 public:
00061 
00062     /** Lifetime of the memory block device
00063      *
00064      * @param size      Size of the Block Device in bytes
00065      * @param block     Block size in bytes. Minimum read, program, and erase sizes are
00066      *                  configured to this value
00067      */
00068     HeapBlockDevice(bd_size_t size, bd_size_t block = 512);
00069     /** Lifetime of the memory block device
00070      *
00071      * @param size      Size of the Block Device in bytes
00072      * @param read      Minimum read size required in bytes
00073      * @param program   Minimum program size required in bytes
00074      * @param erase     Minimum erase size required in bytes
00075      */
00076     HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase);
00077     virtual ~HeapBlockDevice();
00078 
00079     /** Initialize a block device
00080      *
00081      *  @return         0 on success or a negative error code on failure
00082      */
00083     virtual int init();
00084 
00085     /** Deinitialize a block device
00086      *
00087      *  @return         0 on success or a negative error code on failure
00088      */
00089     virtual int deinit();
00090 
00091     /** Read blocks from a block device
00092      *
00093      *  @param buffer   Buffer to read blocks into
00094      *  @param addr     Address of block to begin reading from
00095      *  @param size     Size to read in bytes, must be a multiple of read block size
00096      *  @return         0 on success, negative error code on failure
00097      */
00098     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00099 
00100     /** Program blocks to a block device
00101      *
00102      *  The blocks must have been erased prior to being programmed
00103      *
00104      *  @param buffer   Buffer of data to write to blocks
00105      *  @param addr     Address of block to begin writing to
00106      *  @param size     Size to write in bytes, must be a multiple of program block size
00107      *  @return         0 on success, negative error code on failure
00108      */
00109     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00110 
00111     /** Erase blocks on a block device
00112      *
00113      *  The state of an erased block is undefined until it has been programmed
00114      *
00115      *  @param addr     Address of block to begin erasing
00116      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00117      *  @return         0 on success, negative error code on failure
00118      */
00119     virtual int erase(bd_addr_t addr, bd_size_t size);
00120 
00121     /** Get the size of a readable block
00122      *
00123      *  @return         Size of a readable block in bytes
00124      */
00125     virtual bd_size_t get_read_size() const;
00126 
00127     /** Get the size of a programmable block
00128      *
00129      *  @return         Size of a programmable block in bytes
00130      */
00131     virtual bd_size_t get_program_size() const;
00132 
00133     /** Get the size of an erasable block
00134      *
00135      *  @return         Size of an erasable block in bytes
00136      */
00137     virtual bd_size_t get_erase_size() const;
00138 
00139     /** Get the size of an erasable block given address
00140      *
00141      *  @param addr     Address within the erasable block
00142      *  @return         Size of an erasable block in bytes
00143      *  @note Must be a multiple of the program size
00144      */
00145     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00146 
00147     /** Get the total size of the underlying device
00148      *
00149      *  @return         Size of the underlying device in bytes
00150      */
00151     virtual bd_size_t size() const;
00152 
00153     /** Get the BlockDevice class type.
00154      *
00155      *  @return         A string represent the BlockDevice class type.
00156      */
00157     virtual const char *get_type() const;
00158 
00159 private:
00160     bd_size_t _read_size;
00161     bd_size_t _program_size;
00162     bd_size_t _erase_size;
00163     bd_size_t _count;
00164     uint8_t **_blocks;
00165     uint32_t _init_ref_count;
00166     bool _is_initialized;
00167 };
00168 
00169 } // namespace mbed
00170 
00171 // Added "using" for backwards compatibility
00172 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
00173 using mbed::HeapBlockDevice;
00174 #endif
00175 
00176 #endif
00177 
00178 /** @}*/