Mbed OS Reference
Loading...
Searching...
No Matches
FlashSimBlockDevice.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
19#ifndef MBED_FLASH_SIM_BLOCK_DEVICE_H
20#define MBED_FLASH_SIM_BLOCK_DEVICE_H
21
22#include "BlockDevice.h"
23
24namespace mbed {
25
26/** \addtogroup storage-blockdevice */
27/** @{*/
28
29enum {
30 BD_ERROR_NOT_ERASED = -3201,
31};
32
33/** Flash simulating block device
34 *
35 * Flash simulation BD adaptor
36 *
37 */
39public:
40
41 /** Constructor
42 *
43 * @param bd Block device to back the FlashSimBlockDevice
44 * @param erase_value Value stored in a block after it's erased
45 */
46 FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value = 0xFF);
47 virtual ~FlashSimBlockDevice();
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 the 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 the 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, 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 the 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, 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 the block device
88 *
89 * Required before any write to these addresses
90 *
91 * @param addr Address of block to begin erasing
92 * @param size Size to erase in bytes, must be a multiple of erase block size
93 * @return 0 on success, negative error code on failure
94 */
95 virtual int erase(bd_addr_t addr, bd_size_t size);
96
97 /** Get the size of a readable block
98 *
99 * @return Size of a readable block in bytes
100 */
101 virtual bd_size_t get_read_size() const;
102
103 /** Get the size of a programmable block
104 *
105 * @return Size of a programmable block in bytes
106 */
108
109 /** Get the size of an erasable block
110 *
111 * @return Size of an erasable block in bytes
112 */
113 virtual bd_size_t get_erase_size() const;
114
115 /** Get the size of an erasable block given address
116 *
117 * @param addr Address within the erasable block
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(bd_addr_t addr) const;
122
123 /** Get the value of storage when erased
124 *
125 * @return The value of storage when erased
126 */
127 virtual int get_erase_value() const;
128
129 /** Get the total size of the underlying device
130 *
131 * @return Size of the underlying device in bytes
132 */
133 virtual bd_size_t size() const;
134
135 /** Get the BlockDevice class type.
136 *
137 * @return A string represent the BlockDevice class type.
138 */
139 virtual const char *get_type() const;
140
141private:
142 uint8_t _erase_value;
143 bd_size_t _blank_buf_size;
144 uint8_t *_blank_buf;
145 BlockDevice *_bd;
146 uint32_t _init_ref_count;
147 bool _is_initialized;
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
160
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
Flash simulating block device.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on the 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 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 the 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 the block device.
FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value=0xFF)
Constructor.
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