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  * 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 #ifndef MBED_I2CEEPROM_BLOCK_DEVICE_H
18 #define MBED_I2CEEPROM_BLOCK_DEVICE_H
19 
20 #include "blockdevice/BlockDevice.h"
21 #include "drivers/I2C.h"
22 
23 /** BlockDevice for I2C based flash device such as
24  * Microchip's 24LC or ATMEL's AT24C ranges
25  *
26  * @code
27  * // Here's an example using a 24LC256 on a GR PEACH
28  * #include "mbed.h"
29  * #include "I2CEEBlockDevice.h"
30  *
31  * // Create EEPROM device on I2C bus with 32kbytes of memory
32  * I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024);
33  *
34  * int main() {
35  * printf("i2cee test\n");
36  *
37  * // Initialize the device and print the memory layout
38  * i2cee.init();
39  * printf("i2cee size: %llu\n", i2cee.size());
40  * printf("i2cee read size: %llu\n", i2cee.get_read_size());
41  * printf("i2cee program size: %llu\n", i2cee.get_program_size());
42  * printf("i2cee erase size: %llu\n", i2cee.get_erase_size());
43  *
44  * // Write "Hello World!" to the first block
45  * char *buffer = (char*)malloc(i2cee.get_erase_size());
46  * sprintf(buffer, "Hello World!\n");
47  * i2cee.erase(0, i2cee.get_erase_size());
48  * i2cee.program(buffer, 0, i2cee.get_erase_size());
49  *
50  * // Read back what was stored
51  * i2cee.read(buffer, 0, i2cee.get_erase_size());
52  * printf("%s", buffer);
53  *
54  * // Deinitialize the device
55  * i2cee.deinit();
56  * }
57  * @endcode
58  */
59 class I2CEEBlockDevice : public BlockDevice {
60 public:
61  /** Constructor to create an I2CEEBlockDevice on I2C pins
62  *
63  * @param sda The pin name for the sda line of the I2C bus.
64  * @param scl The pin name for the scl line of the I2C bus.
65  * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
66  * @param size The size of the device in bytes
67  * @param block The page size of the device in bytes, defaults to 32bytes
68  * @param freq The frequency of the I2C bus, defaults to 400K.
69  * @param address_is_eight_bit Specifies whether the EEPROM device is using eight bit
70  * addresses instead of 16 bit addresses. This is used for example
71  * in AT24C series chips.
72  */
74  PinName sda, PinName scl, uint8_t address,
75  mbed::bd_size_t size, mbed::bd_size_t block = 32,
76  int bus_speed = 400000,
77  bool address_is_eight_bit = false);
78 
79  /** Constructor to create an I2CEEBlockDevice on I2C pins
80  *
81  * @param i2c The I2C instance pointer
82  * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
83  * @param size The size of the device in bytes
84  * @param block The page size of the device in bytes, defaults to 32bytes
85  * @param freq The frequency of the I2C bus, defaults to 400K.
86  * @param address_is_eight_bit Specifies whether the EEPROM device is using eight bit
87  * addresses instead of 16 bit addresses. This is used for example
88  * in AT24C series chips.
89  */
91  mbed::I2C *i2c_obj, uint8_t address,
92  mbed::bd_size_t size, mbed::bd_size_t block = 32,
93  bool address_is_eight_bit = false);
94 
95  /** Destructor of I2CEEBlockDevice
96  */
97 
98  virtual ~I2CEEBlockDevice();
99 
100  /** Initialize a block device
101  *
102  * @return 0 on success or a negative error code on failure
103  */
104  virtual int init();
105 
106  /** Deinitialize a block device
107  *
108  * @return 0 on success or a negative error code on failure
109  */
110  virtual int deinit();
111 
112  /** Read blocks from a block device
113  *
114  * @param buffer Buffer to write blocks to
115  * @param addr Address of block to begin reading from
116  * @param size Size to read in bytes, must be a multiple of read block size
117  * @return 0 on success, negative error code on failure
118  */
119  virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
120 
121  /** Program blocks to a block device
122  *
123  * The blocks must have been erased prior to being programmed
124  *
125  * @param buffer Buffer of data to write to blocks
126  * @param addr Address of block to begin writing to
127  * @param size Size to write in bytes, must be a multiple of program block size
128  * @return 0 on success, negative error code on failure
129  */
130  virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
131 
132  /** Erase blocks on a block device
133  *
134  * The state of an erased block is undefined until it has been programmed
135  *
136  * @param addr Address of block to begin erasing
137  * @param size Size to erase in bytes, must be a multiple of erase block size
138  * @return 0 on success, negative error code on failure
139  */
140  virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size);
141 
142  /** Get the size of a readable block
143  *
144  * @return Size of a readable block in bytes
145  */
146  virtual mbed::bd_size_t get_read_size() const;
147 
148  /** Get the size of a programable block
149  *
150  * @return Size of a programable block in bytes
151  * @note Must be a multiple of the read size
152  */
153  virtual mbed::bd_size_t get_program_size() const;
154 
155  /** Get the size of a eraseable block
156  *
157  * @return Size of a eraseable block in bytes
158  * @note Must be a multiple of the program size
159  */
160  virtual mbed::bd_size_t get_erase_size() const;
161 
162  /** Get the total size of the underlying device
163  *
164  * @return Size of the underlying device in bytes
165  */
166  virtual mbed::bd_size_t size() const;
167 
168  /** Get the BlockDevice class type.
169  *
170  * @return A string representation of the BlockDevice class type.
171  */
172  virtual const char *get_type() const;
173 
174 private:
175  mbed::I2C *_i2c;
176  uint32_t _i2c_buffer[sizeof(mbed::I2C) / sizeof(uint32_t)];
177  uint8_t _i2c_addr;
178  bool _address_is_eight_bit;
179  uint32_t _size;
180  uint32_t _block;
181 
182  int _sync();
183 
184  /**
185  * Gets the device's I2C address with respect to the requested page.
186  * When eight-bit mode is disabled, this function is a noop.
187  * When eight-bit mode is enabled, it sets the bits required
188  * in the devices address. Other bits remain unchanged.
189  * @param address An address in the requested page.
190  * @return The device's I2C address for that page
191  */
192  uint8_t get_paged_device_address(mbed::bd_addr_t address);
193 };
194 
195 
196 #endif /* MBED_SD_BLOCK_DEVICE_H */
virtual mbed::bd_size_t get_program_size() const
Get the size of a programable block.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:48
virtual int deinit()
Deinitialize a block device.
An I2C Master, used for communicating with I2C slave devices.
Definition: I2C.h:82
virtual mbed::bd_size_t get_read_size() const
Get the size of a readable block.
virtual mbed::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 erase(mbed::bd_addr_t addr, mbed::bd_size_t size)
Erase blocks on a block device.
virtual mbed::bd_size_t size() const
Get the total size of the underlying device.
virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Program blocks to a block device.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Read blocks from a block device.
I2CEEBlockDevice(PinName sda, PinName scl, uint8_t address, mbed::bd_size_t size, mbed::bd_size_t block=32, int bus_speed=400000, bool address_is_eight_bit=false)
Constructor to create an I2CEEBlockDevice on I2C pins.
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.