Mistake on this page?
Report an issue in GitHub or email us
BufferedBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018-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_BUFFERED_BLOCK_DEVICE_H
22 #define MBED_BUFFERED_BLOCK_DEVICE_H
23 
24 #include "BlockDevice.h"
25 
26 namespace mbed {
27 
28 /** Block device for allowing minimal read and program sizes (of 1) for the underlying BD,
29  * using a buffer on the heap.
30  */
32 public:
33  /** Lifetime of a memory-buffered block device wrapping an underlying block device
34  *
35  * @param bd Block device to back the BufferedBlockDevice
36  */
38 
39  /** Lifetime of the memory-buffered block device
40  */
41  virtual ~BufferedBlockDevice();
42 
43  /** Initialize a buffered-memory block device and its underlying block device
44  *
45  * @return 0 on success or a negative error code on failure
46  */
47  virtual int init();
48 
49  /** Deinitialize the buffered-memory block device and its underlying block device
50  *
51  * @return 0 on success or a negative error code on failure
52  */
53  virtual int deinit();
54 
55  /** Ensure that data on the underlying storage block device is in sync with the
56  * memory-buffered block device
57  *
58  * @return 0 on success or a negative error code on failure
59  */
60  virtual int sync();
61 
62  /** Read blocks from the memory-buffered block device
63  *
64  * @param buffer Buffer to read blocks into
65  * @param addr Address of block to begin reading from
66  * @param size Size to read in bytes, must be a multiple of read block size
67  * @return 0 on success, negative error code on failure
68  */
69  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
70 
71  /** Program data to the memory-buffered block device
72  *
73  * The write address blocks must be erased prior to being programmed.
74  *
75  * @param buffer Buffer of data to write to blocks
76  * @param addr Address of block to begin writing to
77  * @param size Size to write in bytes, must be a multiple of program block size
78  * @return 0 on success, negative error code on failure
79  */
80  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
81 
82  /** Erase blocks from the memory-buffered block device
83  *
84  * The state of an erased block is undefined until it has been programmed,
85  * unless get_erase_value returns a non-negative byte value.
86  *
87  * @param addr Address of block to begin erasing
88  * @param size Size to erase in bytes, must be a multiple of erase block size
89  * @return 0 on success, negative error code on failure
90  */
91  virtual int erase(bd_addr_t addr, bd_size_t size);
92 
93  /** Mark blocks as no longer in use
94  *
95  * This function provides a hint to the underlying block device that a region of blocks
96  * is no longer in use and may be erased without side effects. Erase must still be called
97  * before programming, but trimming allows flash-translation-layers to schedule erases when
98  * the device is not busy.
99  *
100  * @param addr Address of block to mark as unused
101  * @param size Size to mark as unused in bytes, must be a multiple of erase block size
102  * @return 0 on success, negative error code on failure
103  */
104  virtual int trim(bd_addr_t addr, bd_size_t size);
105 
106  /** Get the size of a readable block
107  *
108  * @return Size of a readable block in bytes
109  */
110  virtual bd_size_t get_read_size() const;
111 
112  /** Get the size of a programmable block
113  *
114  * @return Size of a programmable block in bytes
115  * @note Must be a multiple of the read size
116  */
117  virtual bd_size_t get_program_size() const;
118 
119  /** Get the size of an erasable block
120  *
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() const;
125 
126  /** Get the size of an erasable block of a given address
127  *
128  * @param addr Address within the erasable block
129  * @return Size of an erasable block in bytes
130  * @note Must be a multiple of the program size
131  */
132  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
133 
134  /** Get the value of storage data after being erased
135  *
136  * If get_erase_value returns a non-negative byte value, the underlying
137  * storage is set to that value when erased, and storage containing
138  * that value can be programmed without another erase.
139  *
140  * @return The value of storage when erased, or -1 if you can't
141  * rely on the value of erased storage
142  */
143  virtual int get_erase_value() 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 underlying BlockDevice class type
152  *
153  * @return A string representing the underlying BlockDevice class type
154  */
155  virtual const char *get_type() const;
156 
157 protected:
158  BlockDevice *_bd;
159  bd_size_t _bd_program_size;
160  bd_size_t _bd_read_size;
161  bd_size_t _bd_size;
162  bd_size_t _write_cache_addr;
163  bool _write_cache_valid;
164  uint8_t *_write_cache;
165  uint8_t *_read_buf;
166  uint32_t _init_ref_count;
167  bool _is_initialized;
168 
169 #if !(DOXYGEN_ONLY)
170  /** Flush data in cache
171  *
172  * @return 0 on success or a negative error code on failure
173  */
174  int flush();
175 
176  /** Invalidate write cache
177  *
178  * @return none
179  */
180  void invalidate_write_cache();
181 #endif //#if !(DOXYGEN_ONLY)
182 };
183 } // namespace mbed
184 
185 // Added "using" for backwards compatibility
186 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
188 #endif
189 
190 #endif
191 
192 /** @}*/
virtual bd_size_t get_program_size() const
Get the size of a programmable block.
virtual int trim(bd_addr_t addr, bd_size_t size)
Mark blocks as no longer in use.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks from the memory-buffered block device.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
BufferedBlockDevice(BlockDevice *bd)
Lifetime of a memory-buffered block device wrapping an underlying block device.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:48
virtual int sync()
Ensure that data on the underlying storage block device is in sync with the memory-buffered block dev...
virtual int get_erase_value() const
Get the value of storage data after being erased.
virtual int deinit()
Deinitialize the buffered-memory block device and its underlying block device.
virtual int init()
Initialize a buffered-memory block device and its underlying block device.
Block device for allowing minimal read and program sizes (of 1) for the underlying BD...
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program data to the memory-buffered 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.
Definition: ATHandler.h:46
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from the memory-buffered block device.
virtual const char * get_type() const
Get the underlying BlockDevice class type.
virtual ~BufferedBlockDevice()
Lifetime of the memory-buffered block device.
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.