Mistake on this page?
Report an issue in GitHub or email us
FlashSimBlockDevice.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 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_FLASH_SIM_BLOCK_DEVICE_H
27 #define MBED_FLASH_SIM_BLOCK_DEVICE_H
28 
29 #include "BlockDevice.h"
30 
31 namespace mbed {
32 
33 enum {
34  BD_ERROR_NOT_ERASED = -3201,
35 };
36 
37 /** Flash simulating block device
38  *
39  * Flash simulation BD adaptor
40  *
41  */
43 public:
44 
45  /** Constructor
46  *
47  * @param bd Block device to back the FlashSimBlockDevice
48  * @param erase_value Value stored in a block after it's erased
49  */
50  FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value = 0xFF);
51  virtual ~FlashSimBlockDevice();
52 
53  /** Initialize a block device
54  *
55  * @return 0 on success or a negative error code on failure
56  */
57  virtual int init();
58 
59  /** Deinitialize the block device
60  *
61  * @return 0 on success or a negative error code on failure
62  */
63  virtual int deinit();
64 
65  /** Ensure data on storage is in sync with the driver
66  *
67  * @return 0 on success or a negative error code on failure
68  */
69  virtual int sync();
70 
71  /** Read blocks from the block device
72  *
73  * @param buffer Buffer to read blocks into
74  * @param addr Address of block to begin reading from
75  * @param size Size to read in bytes, must be a multiple of read block size
76  * @return 0 on success, negative error code on failure
77  */
78  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
79 
80  /** Program blocks to the block device
81  *
82  * The blocks must have been erased prior to being programmed
83  *
84  * @param buffer Buffer of data to write to blocks
85  * @param addr Address of block to begin writing to
86  * @param size Size to write in bytes, must be a multiple of program block size
87  * @return 0 on success, negative error code on failure
88  */
89  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
90 
91  /** Erase blocks on the block device
92  *
93  * Required before any write to these addresses
94  *
95  * @param addr Address of block to begin erasing
96  * @param size Size to erase in bytes, must be a multiple of erase block size
97  * @return 0 on success, negative error code on failure
98  */
99  virtual int erase(bd_addr_t addr, bd_size_t size);
100 
101  /** Get the size of a readable block
102  *
103  * @return Size of a readable block in bytes
104  */
105  virtual bd_size_t get_read_size() const;
106 
107  /** Get the size of a programmable block
108  *
109  * @return Size of a programmable block in bytes
110  */
111  virtual bd_size_t get_program_size() const;
112 
113  /** Get the size of an erasable block
114  *
115  * @return Size of an erasable block in bytes
116  */
117  virtual bd_size_t get_erase_size() const;
118 
119  /** Get the size of an erasable block given address
120  *
121  * @param addr Address within the erasable block
122  * @return Size of an erasable block in bytes
123  * @note Must be a multiple of the program size
124  */
125  virtual bd_size_t get_erase_size(bd_addr_t addr) const;
126 
127  /** Get the value of storage when erased
128  *
129  * @return The value of storage when erased
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 
145 private:
146  uint8_t _erase_value;
147  bd_size_t _blank_buf_size;
148  uint8_t *_blank_buf;
149  BlockDevice *_bd;
150  uint32_t _init_ref_count;
151  bool _is_initialized;
152 };
153 
154 } // namespace mbed
155 
156 // Added "using" for backwards compatibility
157 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
159 #endif
160 
161 #endif
162 
163 /** @}*/
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from the block device.
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to the block device.
virtual bd_size_t get_erase_size() const
Get the size of an erasable block.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:48
Flash simulating block device.
virtual bd_size_t size() const
Get the total size of the underlying device.
virtual int deinit()
Deinitialize the block device.
FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value=0xFF)
Constructor.
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 int get_erase_value() const
Get the value of storage when erased.
virtual int init()
Initialize a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
Definition: ATHandler.h:46
virtual int sync()
Ensure data on storage is in sync with the driver.
virtual bd_size_t get_program_size() const
Get the size of a programmable 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.