Mistake on this page?
Report an issue in GitHub or email us
SlicingBlockDevice.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_SLICING_BLOCK_DEVICE_H
22 #define MBED_SLICING_BLOCK_DEVICE_H
23 
24 #include "BlockDevice.h"
25 #include "platform/mbed_assert.h"
26 
27 namespace mbed {
28 
29 /** Block device for mapping to a slice of another block device
30  */
32 public:
33  /** Lifetime of the memory block device
34  *
35  * @param bd Block device to back the SlicingBlockDevice
36  * @param start Start block address to map to block 0, negative addresses
37  * are calculated from the end of the underlying block device
38  * @param end End block address to mark the end of the block device,
39  * this block is not mapped, negative addresses are
40  * calculated from the end of the underlying block device.
41  * The default stops at end of the underlying block device.
42  */
43  SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end = 0);
44 
45  /** Lifetime of a block device
46  */
47  virtual ~SlicingBlockDevice() {};
48 
49  /** Initialize a block device
50  *
51  * @return 0 on success or a negative error code on failure
52  */
53  virtual int init();
54 
55  /** Deinitialize a block device
56  *
57  * @return 0 on success or a negative error code on failure
58  */
59  virtual int deinit();
60 
61  /** Ensure data on storage is in sync with the driver
62  *
63  * @return 0 on success or a negative error code on failure
64  */
65  virtual int sync();
66 
67  /** Read blocks from a block device
68  *
69  * @param buffer Buffer to read blocks into
70  * @param addr Address of block to begin reading from
71  * @param size Size to read in bytes, must be a multiple of read block size
72  * @return 0 on success or a negative error code on failure
73  */
74  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
75 
76  /** Program blocks to a block device
77  *
78  * The blocks must have been erased prior to being programmed
79  *
80  * @param buffer Buffer of data to write to blocks
81  * @param addr Address of block to begin writing to
82  * @param size Size to write in bytes, must be a multiple of program block size
83  * @return 0 on success or a negative error code on failure
84  */
85  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
86 
87  /** Erase blocks on a block device
88  *
89  * The state of an erased block is undefined until it has been programmed,
90  * unless get_erase_value returns a non-negative byte value
91  *
92  * @param addr Address of block to begin erasing
93  * @param size Size to erase in bytes, must be a multiple of erase block size
94  * @return 0 on success or a negative error code on failure
95  */
96  virtual int erase(bd_addr_t addr, bd_size_t size);
97 
98  /** Get the size of a readable block
99  *
100  * @return Size of a readable block in bytes
101  */
102  virtual bd_size_t get_read_size() const;
103 
104  /** Get the size of a programmable block
105  *
106  * @return Size of a programmable block in bytes
107  * @note Must be a multiple of the read size
108  */
109  virtual bd_size_t get_program_size() const;
110 
111  /** Get the size of an erasable block
112  *
113  * @return Size of an erasable block in bytes
114  * @note Must be a multiple of the program size
115  */
116  virtual bd_size_t get_erase_size() const;
117 
118  /** Get the size of an erasable block given address
119  *
120  * @param addr Address within the erasable block
121  * @return Size of an erasable block in bytes
122  * @note Must be a multiple of the program size
123  */
124  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
125 
126  /** Get the value of storage when erased
127  *
128  * If get_erase_value returns a non-negative byte value, the underlying
129  * storage is set to that value when erased, and storage containing
130  * that value can be programmed without another erase.
131  *
132  * @return The value of storage when erased, or -1 if you can't
133  * rely on the value of erased storage
134  */
135  virtual int get_erase_value() const;
136 
137  /** Get the total size of the underlying device
138  *
139  * @return Size of the underlying device in bytes
140  */
141  virtual bd_size_t size() const;
142 
143  /** Get the BlockDevice class type.
144  *
145  * @return A string represent the BlockDevice class type.
146  */
147  virtual const char *get_type() const;
148 
149  /** Convenience function for checking block program validity
150  *
151  * @param addr Address of block to begin writing to
152  * @param size Size to write in bytes
153  * @return True if program is valid for underlying block device
154  */
155  virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const;
156 
157  /** Convenience function for checking block read validity
158  *
159  * @param addr Address of block to begin reading from
160  * @param size Size to read in bytes
161  * @return True if read is valid for underlying block device
162  */
163  virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const;
164 
165  /** Convenience function for checking block erase validity
166  *
167  * @param addr Address of block to begin erasing
168  * @param size Size to erase in bytes
169  * @return True if erase is valid for underlying block device
170  */
171  virtual bool is_valid_erase(bd_addr_t addr, bd_size_t size) const;
172 
173 
174 protected:
175  BlockDevice *_bd;
176  bool _start_from_end;
177  bd_size_t _start;
178  bool _stop_from_end;
179  bd_size_t _stop;
180 };
181 
182 } // namespace mbed
183 
184 // Added "using" for backwards compatibility
185 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
187 #endif
188 
189 #endif
190 
191 /** @}*/
virtual ~SlicingBlockDevice()
Lifetime of a block device.
virtual bd_size_t get_program_size() const
Get the size of a programmable block.
virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const
Convenience function for checking block read validity.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:48
virtual bool is_valid_erase(bd_addr_t addr, bd_size_t size) const
Convenience function for checking block erase validity.
virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const
Convenience function for checking block program validity.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
virtual int sync()
Ensure data on storage is in sync with the driver.
virtual int deinit()
Deinitialize a block device.
virtual bd_size_t size() const
Get the total size of the underlying device.
SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end=0)
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 program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
virtual int get_erase_value() const
Get the value of storage when erased.
Definition: ATHandler.h:46
virtual int init()
Initialize a block device.
Block device for mapping to a slice of another 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.