Mistake on this page?
Report an issue in GitHub or email us
ChainingBlockDevice.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_CHAINING_BLOCK_DEVICE_H
22 #define MBED_CHAINING_BLOCK_DEVICE_H
23 
24 #include "BlockDevice.h"
25 #include "platform/mbed_assert.h"
26 #include <stdlib.h>
27 
28 namespace mbed {
29 
30 /** Block device for chaining multiple block devices
31  * with the similar block sizes at sequential addresses
32  *
33  * @code
34  * #include "mbed.h"
35  * #include "HeapBlockDevice.h"
36  * #include "ChainingBlockDevice.h"
37  *
38  * // Create two smaller block devices with
39  * // 64 and 32 blocks of size 512 bytes
40  * HeapBlockDevice mem1(64*512, 512);
41  * HeapBlockDevice mem2(32*512, 512);
42  *
43  * // Create a block device backed by mem1 and mem2
44  * // contains 96 blocks of size 512 bytes
45  * BlockDevice *bds[] = {&mem1, &mem2};
46  * ChainingBlockDevice chainmem(bds);
47  * @endcode
48  */
50 public:
51  /** Lifetime of the memory block device
52  *
53  * @param bds Array of block devices to chain with sequential block addresses
54  * @param bd_count Number of block devices to chain
55  * @note All block devices must have the same block size
56  */
57  ChainingBlockDevice(BlockDevice **bds, size_t bd_count);
58 
59  /** Lifetime of the memory block device
60  *
61  * @param bds Array of block devices to chain with sequential block addresses
62  * @note All block devices must have the same block size
63  */
64  template <size_t Size>
66  : _bds(bds), _bd_count(sizeof(bds) / sizeof(bds[0]))
67  , _read_size(0), _program_size(0), _erase_size(0), _size(0), _init_ref_count(0)
68  {
69  }
70 
71  /** Lifetime of the memory block device
72  *
73  */
74  virtual ~ChainingBlockDevice() {}
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  /** Ensure data on storage is in sync with the driver
89  *
90  * @return 0 on success or a negative error code on failure
91  */
92  virtual int sync();
93 
94  /** Read blocks from a block device
95  *
96  * @param buffer Buffer to write blocks to
97  * @param addr Address of block to begin reading from
98  * @param size Size to read in bytes, must be a multiple of read block size
99  * @return 0 on success, negative error code on failure
100  */
101  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
102 
103  /** Program blocks to a block device
104  *
105  * The blocks must have been erased prior to being programmed
106  *
107  * @param buffer Buffer of data to write to blocks
108  * @param addr Address of block to begin writing to
109  * @param size Size to write in bytes, must be a multiple of program block size
110  * @return 0 on success, negative error code on failure
111  */
112  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
113 
114  /** Erase blocks on a block device
115  *
116  * The state of an erased block is undefined until it has been programmed,
117  * unless get_erase_value returns a non-negative byte value
118  *
119  * @param addr Address of block to begin erasing
120  * @param size Size to erase in bytes, must be a multiple of erase block size
121  * @return 0 on success, negative error code on failure
122  */
123  virtual int erase(bd_addr_t addr, bd_size_t size);
124 
125  /** Get the size of a readable block
126  *
127  * @return Size of a readable block in bytes
128  */
129  virtual bd_size_t get_read_size() const;
130 
131  /** Get the size of a programmable block
132  *
133  * @return Size of a programmable block in bytes
134  * @note Must be a multiple of the read size
135  */
136  virtual bd_size_t get_program_size() const;
137 
138  /** Get the size of an eraseable block
139  *
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() const;
144 
145  /** Get the size of an erasable block given address
146  *
147  * @param addr Address within the erasable block
148  * @return Size of an erasable block in bytes
149  * @note Must be a multiple of the program size
150  */
151  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
152 
153  /** Get the value of storage when erased
154  *
155  * If get_erase_value returns a non-negative byte value, the underlying
156  * storage is set to that value when erased, and storage containing
157  * that value can be programmed without another erase.
158  *
159  * @return The value of storage when erased, or -1 if you can't
160  * rely on the value of erased storage
161  */
162  virtual int get_erase_value() const;
163 
164  /** Get the total size of the underlying device
165  *
166  * @return Size of the underlying device in bytes
167  */
168  virtual bd_size_t size() const;
169 
170  /** Get the BlockDevice class type.
171  *
172  * @return A string represent the BlockDevice class type.
173  */
174  virtual const char *get_type() const;
175 
176 protected:
177  BlockDevice **_bds;
178  size_t _bd_count;
179  bd_size_t _read_size;
180  bd_size_t _program_size;
181  bd_size_t _erase_size;
182  bd_size_t _size;
183  int _erase_value;
184  uint32_t _init_ref_count;
185  bool _is_initialized;
186 };
187 
188 } // namespace mbed
189 
190 // Added "using" for backwards compatibility
191 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
193 #endif
194 
195 #endif
196 
197 /** @}*/
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from a block device.
virtual bd_size_t get_program_size() const
Get the size of a programmable block.
ChainingBlockDevice(BlockDevice *(&bds)[Size])
Lifetime of the memory block device.
virtual ~ChainingBlockDevice()
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 get_erase_value() const
Get the value of storage when erased.
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
virtual bd_size_t size() const
Get the total size of the underlying device.
virtual int init()
Initialize a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
virtual int deinit()
Deinitialize a block device.
Block device for chaining multiple block devices with the similar block sizes at sequential addresses...
virtual int sync()
Ensure data on storage is in sync with the driver.
ChainingBlockDevice(BlockDevice **bds, size_t bd_count)
Lifetime of the memory block device.
virtual bd_size_t get_erase_size() const
Get the size of an eraseable 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.