Mistake on this page?
Report an issue in GitHub or email us
HeapBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2017 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 /** \addtogroup storage */
24 /** @{*/
25 
26 #ifndef MBED_MEM_BLOCK_DEVICE_H
27 #define MBED_MEM_BLOCK_DEVICE_H
28 
29 #include "BlockDevice.h"
30 #include "platform/mbed_assert.h"
31 #include <string.h>
32 #include <stdlib.h>
33 
34 namespace mbed {
35 
36 /** Lazily allocated heap-backed block device
37  *
38  * Useful for simulating a block device and tests
39  *
40  * @code
41  * #include "mbed.h"
42  * #include "HeapBlockDevice.h"
43  *
44  * #define BLOCK_SIZE 512
45  *
46  * HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes
47  * uint8_t block[BLOCK_SIZE] = "Hello World!\n";
48  *
49  * int main() {
50  * bd.init();
51  * bd.erase(0, BLOCK_SIZE);
52  * bd.program(block, 0, BLOCK_SIZE);
53  * bd.read(block, 0, BLOCK_SIZE);
54  * printf("%s", block);
55  * bd.deinit();
56  * }
57  * @endcode
58  */
59 class HeapBlockDevice : public BlockDevice {
60 public:
61 
62  /** Lifetime of the memory block device
63  *
64  * @param size Size of the Block Device in bytes
65  * @param block Block size in bytes. Minimum read, program, and erase sizes are
66  * configured to this value
67  */
68  HeapBlockDevice(bd_size_t size, bd_size_t block = 512);
69  /** Lifetime of the memory block device
70  *
71  * @param size Size of the Block Device in bytes
72  * @param read Minimum read size required in bytes
73  * @param program Minimum program size required in bytes
74  * @param erase Minimum erase size required in bytes
75  */
76  HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase);
77  virtual ~HeapBlockDevice();
78 
79  /** Initialize a block device
80  *
81  * @return 0 on success or a negative error code on failure
82  */
83  virtual int init();
84 
85  /** Deinitialize a block device
86  *
87  * @return 0 on success or a negative error code on failure
88  */
89  virtual int deinit();
90 
91  /** Read blocks from a block device
92  *
93  * @param buffer Buffer to read blocks into
94  * @param addr Address of block to begin reading from
95  * @param size Size to read in bytes, must be a multiple of read block size
96  * @return 0 on success, negative error code on failure
97  */
98  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
99 
100  /** Program blocks to a block device
101  *
102  * The blocks must have been erased prior to being programmed
103  *
104  * @param buffer Buffer of data to write to blocks
105  * @param addr Address of block to begin writing to
106  * @param size Size to write in bytes, must be a multiple of program block size
107  * @return 0 on success, negative error code on failure
108  */
109  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
110 
111  /** Erase blocks on a block device
112  *
113  * The state of an erased block is undefined until it has been programmed
114  *
115  * @param addr Address of block to begin erasing
116  * @param size Size to erase in bytes, must be a multiple of erase block size
117  * @return 0 on success, negative error code on failure
118  */
119  virtual int erase(bd_addr_t addr, bd_size_t size);
120 
121  /** Get the size of a readable block
122  *
123  * @return Size of a readable block in bytes
124  */
125  virtual bd_size_t get_read_size() const;
126 
127  /** Get the size of a programmable block
128  *
129  * @return Size of a programmable block in bytes
130  */
131  virtual bd_size_t get_program_size() const;
132 
133  /** Get the size of an erasable block
134  *
135  * @return Size of an erasable block in bytes
136  */
137  virtual bd_size_t get_erase_size() const;
138 
139  /** Get the size of an erasable block given address
140  *
141  * @param addr Address within the erasable block
142  * @return Size of an erasable block in bytes
143  * @note Must be a multiple of the program size
144  */
145  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
146 
147  /** Get the total size of the underlying device
148  *
149  * @return Size of the underlying device in bytes
150  */
151  virtual bd_size_t size() const;
152 
153  /** Get the BlockDevice class type.
154  *
155  * @return A string represent the BlockDevice class type.
156  */
157  virtual const char *get_type() const;
158 
159 private:
160  bd_size_t _read_size;
161  bd_size_t _program_size;
162  bd_size_t _erase_size;
163  bd_size_t _count;
164  uint8_t **_blocks;
165  uint32_t _init_ref_count;
166  bool _is_initialized;
167 };
168 
169 } // namespace mbed
170 
171 // Added "using" for backwards compatibility
172 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
174 #endif
175 
176 #endif
177 
178 /** @}*/
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from a block device.
Lazily allocated heap-backed block device.
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
HeapBlockDevice(bd_size_t size, bd_size_t block=512)
Lifetime of the memory block device.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:47
virtual const char * get_type() const
Get the BlockDevice class type.
virtual int deinit()
Deinitialize a block device.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
virtual bd_size_t get_program_size() const
Get the size of a programmable block.
virtual bd_size_t size() const
Get the total size of the underlying device.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
virtual int init()
Initialize a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.