Mbed OS Reference
Loading...
Searching...
No Matches
ObservingBlockDevice.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
19#ifndef MBED_OBSERVING_BLOCK_DEVICE_H
20#define MBED_OBSERVING_BLOCK_DEVICE_H
21
22#include "BlockDevice.h"
23#include "rtos/Mutex.h"
24#include "platform/Callback.h"
25
26namespace mbed {
27
28/** \addtogroup storage-blockdevice */
29/** @{*/
30
32public:
33
34 /** Lifetime of the block device
35 *
36 * @param bd Block device to observe
37 */
39 virtual ~ObservingBlockDevice();
40
41 /** Attach a callback which is called on change
42 *
43 * @param cb Function to call on filesystem change (erase or program)
44 */
46
47 /** Initialize a block device
48 *
49 * @return 0 on success or a negative error code on failure
50 */
51 virtual int init();
52
53 /** Deinitialize a block device
54 *
55 * @return 0 on success or a negative error code on failure
56 */
57 virtual int deinit();
58
59 /** Ensure data on storage is in sync with the driver
60 *
61 * @return 0 on success or a negative error code on failure
62 */
63 virtual int sync();
64
65 /** Read blocks from a block device
66 *
67 * @param buffer Buffer to read blocks into
68 * @param addr Address of block to begin reading from
69 * @param size Size to read in bytes, must be a multiple of read block size
70 * @return 0 on success, negative error code on failure
71 */
72 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
73
74 /** Program blocks to a block device
75 *
76 * The blocks must have been erased prior to being programmed
77 *
78 * @param buffer Buffer of data to write to blocks
79 * @param addr Address of block to begin writing to
80 * @param size Size to write in bytes, must be a multiple of program block size
81 * @return 0 on success, negative error code on failure
82 */
83 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
84
85 /** Erase blocks on a block device
86 *
87 * The state of an erased block is undefined until it has been programmed,
88 * unless get_erase_value returns a non-negative byte value
89 *
90 * @param addr Address of block to begin erasing
91 * @param size Size to erase in bytes, must be a multiple of erase block size
92 * @return 0 on success, negative error code on failure
93 */
94 virtual int erase(bd_addr_t addr, bd_size_t size);
95
96 /** Get the size of a readable block
97 *
98 * @return Size of a readable block in bytes
99 */
100 virtual bd_size_t get_read_size() const;
101
102 /** Get the size of a programmable block
103 *
104 * @return Size of a programmable block in bytes
105 */
107
108 /** Get the size of an erasable block
109 *
110 * @return Size of an erasable block in bytes
111 */
112 virtual bd_size_t get_erase_size() const;
113
114 /** Get the size of an erasable block given address
115 *
116 * @param addr Address within the erasable block
117 * @return Size of an erasable block in bytes
118 * @note Must be a multiple of the program size
119 */
120 virtual bd_size_t get_erase_size(bd_addr_t addr) const;
121
122 /** Get the value of storage when erased
123 *
124 * If get_erase_value returns a non-negative byte value, the underlying
125 * storage is set to that value when erased, and storage containing
126 * that value can be programmed without another erase.
127 *
128 * @return The value of storage when erased, or -1 if you can't
129 * rely on the value of erased storage
130 */
131 virtual int get_erase_value() const;
132
133 /** Get the total size of the underlying device
134 *
135 * @return Size of the underlying device in bytes
136 */
137 virtual bd_size_t size() const;
138
139 /** Get the BlockDevice class type.
140 *
141 * @return A string represent the BlockDevice class type.
142 */
143 virtual const char *get_type() const;
144
145private:
146 BlockDevice *_bd;
147 mbed::Callback<void(BlockDevice *)> _change;
148};
149
150/** @}*/
151
152} // namespace mbed
153
154// Added "using" for backwards compatibility
155#ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
157#endif
158
159#endif
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
Callback class based on template specialization.
Definition: Callback.h:53
virtual const char * get_type() const
Get the BlockDevice class type.
void attach(mbed::Callback< void(BlockDevice *)> cb)
Attach a callback which is called on change.
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.
ObservingBlockDevice(BlockDevice *bd)
Lifetime of the 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.
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