Lazily allocated heap-backed block device. When writing data of ROM address, heap memory is not used.

Dependents:   GR-PEACH-webcam GR-PEACH_DR_STRANGE_VR_GAME GR-Boards_WebCamera

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RomRamBlockDevice.h Source File

RomRamBlockDevice.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_ROM_RAM_BLOCK_DEVICE_H
00023 #define MBED_ROM_RAM_BLOCK_DEVICE_H
00024 
00025 #include "BlockDevice.h"
00026 #include "mbed.h"
00027 
00028 
00029 /** Lazily allocated heap-backed block device
00030  *
00031  * When writing data of ROM address, heap memory is not used
00032  * Useful for simulating a block device and tests
00033  *
00034  * @code
00035  * #include "mbed.h"
00036  * #include "RomRamBlockDevice.h"
00037  *
00038  * RomRamBlockDevice bd(2048, 512); // 2048 bytes with a block size of 512 bytes
00039  * uint8_t block[512] = "Hello World!\n";
00040  *
00041  * int main() {
00042  *     bd.SetRomAddr(0x18000000, 0x1FFFFFFF); // ROM Address 0x18000000 - 0x1FFFFFFF
00043  *     bd.init();
00044  *     bd.program(block, 0);
00045  *     bd.read(block, 0);
00046  *     printf("%s", block);
00047  *     bd.deinit();
00048  * }
00049  */
00050 class RomRamBlockDevice : public BlockDevice
00051 {
00052 public:
00053 
00054     /** Lifetime of the memory block device
00055      */
00056     RomRamBlockDevice(bd_size_t size, bd_size_t block=512);
00057     RomRamBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase);
00058     virtual ~RomRamBlockDevice();
00059 
00060     /** Set the ROM address range
00061      * 
00062      *  When writing data of ROM address, heap memory is not used
00063      * 
00064      *  @param rom_start_addr   Rom start address
00065      *  @param rom_end_addr     Rom end address
00066      */
00067     void SetRomAddr(uint32_t rom_start_addr, uint32_t rom_end_addr);
00068 
00069     /** Initialize a block device
00070      *
00071      *  @return         0 on success or a negative error code on failure
00072      */
00073     virtual int init();
00074 
00075     /** Deinitialize a block device
00076      *
00077      *  @return         0 on success or a negative error code on failure
00078      */
00079     virtual int deinit();
00080 
00081     /** Read blocks from a block device
00082      *
00083      *  @param buffer   Buffer to read blocks into
00084      *  @param addr     Address of block to begin reading from
00085      *  @param size     Size to read in bytes, must be a multiple of read block size
00086      *  @return         0 on success, negative error code on failure
00087      */
00088     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00089 
00090     /** Program blocks to a block device
00091      *
00092      *  The blocks must have been erased prior to being programmed
00093      *
00094      *  @param buffer   Buffer of data to write to blocks
00095      *  @param addr     Address of block to begin writing to
00096      *  @param size     Size to write in bytes, must be a multiple of program block size
00097      *  @return         0 on success, negative error code on failure
00098      */
00099     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00100 
00101     /** Erase blocks on a block device
00102      *
00103      *  The state of an erased block is undefined until it has been programmed
00104      *
00105      *  @param addr     Address of block to begin erasing
00106      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00107      *  @return         0 on success, negative error code on failure
00108      */
00109     virtual int erase(bd_addr_t addr, bd_size_t size);
00110 
00111     /** Get the size of a readable block
00112      *
00113      *  @return         Size of a readable block in bytes
00114      */
00115     virtual bd_size_t get_read_size() const;
00116 
00117     /** Get the size of a programable block
00118      *
00119      *  @return         Size of a programable block in bytes
00120      */
00121     virtual bd_size_t get_program_size() const;
00122 
00123     /** Get the size of a eraseable block
00124      *
00125      *  @return         Size of a eraseable block in bytes
00126      */
00127     virtual bd_size_t get_erase_size() const;
00128 
00129     /** Get the total size of the underlying device
00130      *
00131      *  @return         Size of the underlying device in bytes
00132      */
00133     virtual bd_size_t size() const;
00134 
00135 private:
00136     bd_size_t _read_size;
00137     bd_size_t _program_size;
00138     bd_size_t _erase_size;
00139     bd_size_t _count;
00140     uint8_t **_blocks;
00141     uint32_t _rom_start;
00142     uint32_t _rom_end;
00143 
00144     bool isRomAddress(const uint8_t *address);
00145 };
00146 
00147 
00148 #endif