Mistake on this page?
Report an issue in GitHub or email us
I2CEEBlockDevice.h
1 /* Simple access class for I2C EEPROM chips like Microchip 24LC
2  * Copyright (c) 2015 Robin Hourahane
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_I2CEEPROM_BLOCK_DEVICE_H
17 #define MBED_I2CEEPROM_BLOCK_DEVICE_H
18 
19 #include "features/storage/blockdevice/BlockDevice.h"
20 #include "drivers/I2C.h"
21 
22 /** BlockDevice for I2C based flash device such as
23  * Microchip's 24LC or ATMEL's AT24C ranges
24  *
25  * @code
26  * // Here's an example using a 24LC256 on a GR PEACH
27  * #include "mbed.h"
28  * #include "I2CEEBlockDevice.h"
29  *
30  * // Create EEPROM device on I2C bus with 32kbytes of memory
31  * I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024);
32  *
33  * int main() {
34  * printf("i2cee test\n");
35  *
36  * // Initialize the device and print the memory layout
37  * i2cee.init();
38  * printf("i2cee size: %llu\n", i2cee.size());
39  * printf("i2cee read size: %llu\n", i2cee.get_read_size());
40  * printf("i2cee program size: %llu\n", i2cee.get_program_size());
41  * printf("i2cee erase size: %llu\n", i2cee.get_erase_size());
42  *
43  * // Write "Hello World!" to the first block
44  * char *buffer = (char*)malloc(i2cee.get_erase_size());
45  * sprintf(buffer, "Hello World!\n");
46  * i2cee.erase(0, i2cee.get_erase_size());
47  * i2cee.program(buffer, 0, i2cee.get_erase_size());
48  *
49  * // Read back what was stored
50  * i2cee.read(buffer, 0, i2cee.get_erase_size());
51  * printf("%s", buffer);
52  *
53  * // Deinitialize the device
54  * i2cee.deinit();
55  * }
56  * @endcode
57  */
58 class I2CEEBlockDevice : public BlockDevice {
59 public:
60  /** Constructor to create an I2CEEBlockDevice on I2C pins
61  *
62  * @param sda The pin name for the sda line of the I2C bus.
63  * @param scl The pin name for the scl line of the I2C bus.
64  * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
65  * @param size The size of the device in bytes
66  * @param block The page size of the device in bytes, defaults to 32bytes
67  * @param freq The frequency of the I2C bus, defaults to 400K.
68  */
70  PinName sda, PinName scl, uint8_t address,
71  bd_size_t size, bd_size_t block = 32,
72  int bus_speed = 400000);
73 
74  /** Constructor to create an I2CEEBlockDevice on I2C pins
75  *
76  * @param i2c The I2C instance pointer
77  * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
78  * @param size The size of the device in bytes
79  * @param block The page size of the device in bytes, defaults to 32bytes
80  * @param freq The frequency of the I2C bus, defaults to 400K.
81  */
83  mbed::I2C *i2c_obj, uint8_t address,
84  bd_size_t size, bd_size_t block = 32);
85 
86  /** Destructor of I2CEEBlockDevice
87  */
88 
89  virtual ~I2CEEBlockDevice();
90 
91  /** Initialize a block device
92  *
93  * @return 0 on success or a negative error code on failure
94  */
95  virtual int init();
96 
97  /** Deinitialize a block device
98  *
99  * @return 0 on success or a negative error code on failure
100  */
101  virtual int deinit();
102 
103  /** Read blocks from a block device
104  *
105  * @param buffer Buffer to write blocks to
106  * @param addr Address of block to begin reading from
107  * @param size Size to read in bytes, must be a multiple of read block size
108  * @return 0 on success, negative error code on failure
109  */
110  virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
111 
112  /** Program blocks to a block device
113  *
114  * The blocks must have been erased prior to being programmed
115  *
116  * @param buffer Buffer of data to write to blocks
117  * @param addr Address of block to begin writing to
118  * @param size Size to write in bytes, must be a multiple of program block size
119  * @return 0 on success, negative error code on failure
120  */
121  virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
122 
123  /** Erase blocks on a block device
124  *
125  * The state of an erased block is undefined until it has been programmed
126  *
127  * @param addr Address of block to begin erasing
128  * @param size Size to erase in bytes, must be a multiple of erase block size
129  * @return 0 on success, negative error code on failure
130  */
131  virtual int erase(bd_addr_t addr, bd_size_t size);
132 
133  /** Get the size of a readable block
134  *
135  * @return Size of a readable block in bytes
136  */
137  virtual bd_size_t get_read_size() const;
138 
139  /** Get the size of a programable block
140  *
141  * @return Size of a programable block in bytes
142  * @note Must be a multiple of the read size
143  */
144  virtual bd_size_t get_program_size() const;
145 
146  /** Get the size of a eraseable block
147  *
148  * @return Size of a eraseable block in bytes
149  * @note Must be a multiple of the program size
150  */
151  virtual bd_size_t get_erase_size() const;
152 
153  /** Get the total size of the underlying device
154  *
155  * @return Size of the underlying device in bytes
156  */
157  virtual bd_size_t size() const;
158 
159  /** Get the BlockDevice class type.
160  *
161  * @return A string representation of the BlockDevice class type.
162  */
163  virtual const char *get_type() const;
164 
165 private:
166  mbed::I2C *_i2c;
167  uint32_t _i2c_buffer[sizeof(mbed::I2C) / sizeof(uint32_t)];
168  uint8_t _i2c_addr;
169  uint32_t _size;
170  uint32_t _block;
171 
172  int _sync();
173 };
174 
175 
176 #endif /* MBED_SD_BLOCK_DEVICE_H */
I2CEEBlockDevice(PinName sda, PinName scl, uint8_t address, bd_size_t size, bd_size_t block=32, int bus_speed=400000)
Constructor to create an I2CEEBlockDevice on I2C pins.
virtual bd_size_t size() const
Get the total size of the underlying device.
virtual int erase(bd_addr_t addr, bd_size_t size)
Erase blocks on a block device.
virtual bd_size_t get_read_size() const
Get the size of a readable block.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:47
virtual int deinit()
Deinitialize a block device.
An I2C Master, used for communicating with I2C slave devices.
Definition: I2C.h:82
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size)
Program blocks to a block device.
virtual bd_size_t get_erase_size() const
Get the size of a eraseable block.
virtual int init()
Initialize a block device.
virtual ~I2CEEBlockDevice()
Destructor of I2CEEBlockDevice.
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size)
Read blocks from a block device.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual bd_size_t get_program_size() const
Get the size of a programable block.
BlockDevice for I2C based flash device such as Microchip's 24LC or ATMEL's AT24C ranges.
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.