Mbed OS Reference
Loading...
Searching...
No Matches
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
19#ifndef MBED_MEM_BLOCK_DEVICE_H
20#define MBED_MEM_BLOCK_DEVICE_H
21
22#include "BlockDevice.h"
23#include "platform/mbed_assert.h"
24#include <string.h>
25#include <stdlib.h>
26
27namespace mbed {
28
29/** \addtogroup storage-blockdevice */
30/** @{*/
31
32/** Lazily allocated heap-backed block device.
33 *
34 * Useful for simulating a block device and tests.
35 *
36 * @note Each block is allocated when used, and freed when erased.
37 *
38 * @code
39 * #include "mbed.h"
40 * #include "HeapBlockDevice.h"
41 *
42 * #define BLOCK_SIZE 512
43 *
44 * HeapBlockDevice bd(2048, BLOCK_SIZE); // 2048 bytes with a block size of 512 bytes
45 * uint8_t block[BLOCK_SIZE] = "Hello World!\n";
46 *
47 * int main() {
48 * bd.init();
49 * bd.erase(0, BLOCK_SIZE);
50 * bd.program(block, 0, BLOCK_SIZE);
51 * bd.read(block, 0, BLOCK_SIZE);
52 * printf("%s", block);
53 * bd.deinit();
54 * }
55 * @endcode
56 */
58public:
59
60 /** Lifetime of the memory block device
61 *
62 * @param size Size of the Block Device in bytes
63 * @param block Block size in bytes. Minimum read, program, and erase sizes are
64 * configured to this value
65 */
67 /** Lifetime of the memory block device
68 *
69 * @param size Size of the Block Device in bytes
70 * @param read Minimum read size required in bytes
71 * @param program Minimum program size required in bytes
72 * @param erase Minimum erase size required in bytes
73 */
75 virtual ~HeapBlockDevice();
76
77 /** Initialize a block device
78 *
79 * @return 0 on success or a negative error code on failure
80 */
81 virtual int init();
82
83 /** Deinitialize a block device
84 *
85 * @return 0 on success or a negative error code on failure
86 */
87 virtual int deinit();
88
89 /** Read blocks from a block device
90 *
91 * @param buffer Buffer to read blocks into
92 * @param addr Address of block to begin reading from
93 * @param size Size to read in bytes, must be a multiple of read block size
94 * @return 0 on success, negative error code on failure
95 */
96 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
97
98 /** Program blocks to a block device
99 *
100 * The blocks must have been erased prior to being programmed
101 *
102 * @param buffer Buffer of data to write to blocks
103 * @param addr Address of block to begin writing to
104 * @param size Size to write in bytes, must be a multiple of program block size
105 * @return 0 on success, negative error code on failure
106 */
107 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
108
109 /** Erase blocks on a block device
110 *
111 * The state of an erased block is undefined until it has been programmed
112 *
113 * @param addr Address of block to begin erasing
114 * @param size Size to erase in bytes, must be a multiple of erase block size
115 * @return 0 on success, negative error code on failure
116 */
117 virtual int erase(bd_addr_t addr, bd_size_t size);
118
119 /** Get the size of a readable block
120 *
121 * @return Size of a readable block in bytes
122 */
123 virtual bd_size_t get_read_size() const;
124
125 /** Get the size of a programmable block
126 *
127 * @return Size of a programmable block in bytes
128 */
130
131 /** Get the size of an erasable block
132 *
133 * @return Size of an erasable block in bytes
134 */
135 virtual bd_size_t get_erase_size() const;
136
137 /** Get the size of an erasable block given address
138 *
139 * @param addr Address within the erasable block
140 * @return Size of an erasable block in bytes
141 * @note Must be a multiple of the program size
142 */
143 virtual bd_size_t get_erase_size(bd_addr_t addr) const;
144
145 /** Get the total size of the underlying device
146 *
147 * @return Size of the underlying device in bytes
148 */
149 virtual bd_size_t size() const;
150
151 /** Get the BlockDevice class type.
152 *
153 * @return A string represent the BlockDevice class type.
154 */
155 virtual const char *get_type() const;
156
157private:
158 bd_size_t _read_size;
159 bd_size_t _program_size;
160 bd_size_t _erase_size;
161 bd_size_t _count;
162 uint8_t **_blocks;
163 uint32_t _init_ref_count;
164 bool _is_initialized;
165};
166
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
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
Lazily allocated heap-backed block device.
virtual const char * get_type() const
Get the BlockDevice class type.
HeapBlockDevice(bd_size_t size, bd_size_t block=512)
Lifetime of the memory 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 int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
virtual int deinit()
Deinitialize a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
virtual bd_size_t size() const
Get the total size of the underlying device.
HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
Lifetime of the memory block device.
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from a block device.
virtual int init()
Initialize a block device.
virtual bd_size_t get_erase_size(bd_addr_t addr) const
Get the size of an erasable block given address.
uint64_t bd_size_t
Type representing a quantity of 8-bit bytes.
Definition: BlockDevice.h:48
uint64_t bd_addr_t
Type representing the address of a specific block.
Definition: BlockDevice.h:44