Mbed OS Reference
Loading...
Searching...
No Matches
ExhaustibleBlockDevice.h
1/*
2 * Copyright (c) 2018-2019, Arm Limited and affiliates.
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_EXHAUSTIBLE_BLOCK_DEVICE_H
20#define MBED_EXHAUSTIBLE_BLOCK_DEVICE_H
21
22#include "BlockDevice.h"
23
24namespace mbed {
25
26/** \addtogroup storage-blockdevice */
27/** @{*/
28
29/** Heap backed block device which simulates failures
30 *
31 * Similar to heap block device but sectors wear out and are no longer programmable
32 * after a configurable number of cycles.
33 *
34 */
36public:
37 /** Lifetime of the block device
38 *
39 * @param bd Block device to back the ExhaustibleBlockDevice
40 * @param erase_cycles Number of erase cycles before failure
41 */
42 ExhaustibleBlockDevice(BlockDevice *bd, uint32_t erase_cycles);
44
45 /**
46 * Get the number of erase cycles remaining on a block
47 *
48 * @param addr Any address in the block being queried for erase cycles
49 * @return Number of erase cycles remaining
50 */
51 uint32_t get_erase_cycles(bd_addr_t addr) const;
52
53 /**
54 * Set the number of erase cycles before failure
55 *
56 * @param addr Any address in the block being queried for erase cycles
57 * @param cycles Erase cycles before the block malfunctions
58 */
59 void set_erase_cycles(bd_addr_t addr, uint32_t cycles);
60
61 /** Initialize a block device
62 *
63 * @return 0 on success or a negative error code on failure
64 */
65 virtual int init();
66
67 /** Deinitialize a block device
68 *
69 * @return 0 on success or a negative error code on failure
70 */
71 virtual int deinit();
72
73 /** Ensure data on storage is in sync with the driver
74 *
75 * @return 0 on success or a negative error code on failure
76 */
77 virtual int sync();
78
79 /** Read blocks from a block device
80 *
81 * @param buffer Buffer to read blocks into
82 * @param addr Address of block to begin reading from
83 * @param size Size to read in bytes, must be a multiple of read block size
84 * @return 0 on success, negative error code on failure
85 */
86 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
87
88 /** Program blocks to a block device
89 *
90 * The blocks must have been erased prior to being programmed
91 *
92 * @param buffer Buffer of data to write to blocks
93 * @param addr Address of block to begin writing to
94 * @param size Size to write in bytes, must be a multiple of program block size
95 * @return 0 on success, negative error code on failure
96 */
97 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
98
99 /** Erase blocks on a block device
100 *
101 * The state of an erased block is undefined until it has been programmed,
102 * unless get_erase_value returns a non-negative byte value
103 *
104 * @param addr Address of block to begin erasing
105 * @param size Size to erase in bytes, must be a multiple of erase block size
106 * @return 0 on success, negative error code on failure
107 */
108 virtual int erase(bd_addr_t addr, bd_size_t size);
109
110 /** Get the size of a readable block
111 *
112 * @return Size of a readable block in bytes
113 */
114 virtual bd_size_t get_read_size() const;
115
116 /** Get the size of a programmable block
117 *
118 * @return Size of a programmable block in bytes
119 */
121
122 /** Get the size of an erasable block
123 *
124 * @return Size of an erasable block in bytes
125 */
126 virtual bd_size_t get_erase_size() const;
127
128 /** Get the size of an erasable block given address
129 *
130 * @param addr Address within the erasable block
131 * @return Size of an erasable block in bytes
132 * @note Must be a multiple of the program size
133 */
134 virtual bd_size_t get_erase_size(bd_addr_t addr) const;
135
136 /** Get the value of storage when erased
137 *
138 * If get_erase_value returns a non-negative byte value, the underlying
139 * storage is set to that value when erased, and storage containing
140 * that value can be programmed without another erase.
141 *
142 * @return The value of storage when erased, or -1 if you can't
143 * rely on the value of erased storage
144 */
145 virtual int get_erase_value() 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
159private:
160 BlockDevice *_bd;
161 uint32_t *_erase_array;
162 uint32_t _erase_cycles;
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
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
Heap backed block device which simulates failures.
virtual const char * get_type() const
Get the BlockDevice class type.
uint32_t get_erase_cycles(bd_addr_t addr) const
Get the number of erase cycles remaining on a block.
ExhaustibleBlockDevice(BlockDevice *bd, uint32_t erase_cycles)
Lifetime of the 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 int sync()
Ensure data on storage is in sync with the driver.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
void set_erase_cycles(bd_addr_t addr, uint32_t cycles)
Set the number of erase cycles before failure.
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.
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 int get_erase_value() const
Get the value of storage when erased.
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