Mistake on this page?
Report an issue in GitHub or email us
HeapBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2017-2020 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /** \addtogroup storage */
19 /** @{*/
20 
21 #ifndef MBED_MEM_BLOCK_DEVICE_H
22 #define MBED_MEM_BLOCK_DEVICE_H
23 
24 #include "BlockDevice.h"
25 #include "platform/mbed_assert.h"
26 #include <string.h>
27 #include <stdlib.h>
28 
29 namespace mbed {
30 
31 /** Lazily allocated heap-backed block device.
32  *
33  * Useful for simulating a block device and tests.
34  *
35  * @note Each block is allocated when used, and freed when erased.
36  *
37  * @code
38  * #include "mbed.h"
39  * #include "HeapBlockDevice.h"
40  *
41  * #define BLOCK_SIZE 512
42  *
43  * HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes
44  * uint8_t block[BLOCK_SIZE] = "Hello World!\n";
45  *
46  * int main() {
47  * bd.init();
48  * bd.erase(0, BLOCK_SIZE);
49  * bd.program(block, 0, BLOCK_SIZE);
50  * bd.read(block, 0, BLOCK_SIZE);
51  * printf("%s", block);
52  * bd.deinit();
53  * }
54  * @endcode
55  */
56 class HeapBlockDevice : public BlockDevice {
57 public:
58 
59  /** Lifetime of the memory block device
60  *
61  * @param size Size of the Block Device in bytes
62  * @param block Block size in bytes. Minimum read, program, and erase sizes are
63  * configured to this value
64  */
65  HeapBlockDevice(bd_size_t size, bd_size_t block = 512);
66  /** Lifetime of the memory block device
67  *
68  * @param size Size of the Block Device in bytes
69  * @param read Minimum read size required in bytes
70  * @param program Minimum program size required in bytes
71  * @param erase Minimum erase size required in bytes
72  */
73  HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase);
74  virtual ~HeapBlockDevice();
75 
76  /** Initialize a block device
77  *
78  * @return 0 on success or a negative error code on failure
79  */
80  virtual int init();
81 
82  /** Deinitialize a block device
83  *
84  * @return 0 on success or a negative error code on failure
85  */
86  virtual int deinit();
87 
88  /** Read blocks from a block device
89  *
90  * @param buffer Buffer to read blocks into
91  * @param addr Address of block to begin reading from
92  * @param size Size to read in bytes, must be a multiple of read block size
93  * @return 0 on success, negative error code on failure
94  */
95  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
96 
97  /** Program blocks to a block device
98  *
99  * The blocks must have been erased prior to being programmed
100  *
101  * @param buffer Buffer of data to write to blocks
102  * @param addr Address of block to begin writing to
103  * @param size Size to write in bytes, must be a multiple of program block size
104  * @return 0 on success, negative error code on failure
105  */
106  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
107 
108  /** Erase blocks on a block device
109  *
110  * The state of an erased block is undefined until it has been programmed
111  *
112  * @param addr Address of block to begin erasing
113  * @param size Size to erase in bytes, must be a multiple of erase block size
114  * @return 0 on success, negative error code on failure
115  */
116  virtual int erase(bd_addr_t addr, bd_size_t size);
117 
118  /** Get the size of a readable block
119  *
120  * @return Size of a readable block in bytes
121  */
122  virtual bd_size_t get_read_size() const;
123 
124  /** Get the size of a programmable block
125  *
126  * @return Size of a programmable block in bytes
127  */
128  virtual bd_size_t get_program_size() const;
129 
130  /** Get the size of an erasable block
131  *
132  * @return Size of an erasable block in bytes
133  */
134  virtual bd_size_t get_erase_size() const;
135 
136  /** Get the size of an erasable block given address
137  *
138  * @param addr Address within the erasable block
139  * @return Size of an erasable block in bytes
140  * @note Must be a multiple of the program size
141  */
142  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
143 
144  /** Get the total size of the underlying device
145  *
146  * @return Size of the underlying device in bytes
147  */
148  virtual bd_size_t size() const;
149 
150  /** Get the BlockDevice class type.
151  *
152  * @return A string represent the BlockDevice class type.
153  */
154  virtual const char *get_type() const;
155 
156 private:
157  bd_size_t _read_size;
158  bd_size_t _program_size;
159  bd_size_t _erase_size;
160  bd_size_t _count;
161  uint8_t **_blocks;
162  uint32_t _init_ref_count;
163  bool _is_initialized;
164 };
165 
166 } // namespace mbed
167 
168 // Added "using" for backwards compatibility
169 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
171 #endif
172 
173 #endif
174 
175 /** @}*/
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:48
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.
Definition: ATHandler.h:46
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.