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