Mbed OS Reference
Loading...
Searching...
No Matches
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 */
60public:
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 address 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 bus_speed The frequency of the I2C bus, defaults to 400kHz.
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,
76 int bus_speed = 400000,
77 bool address_is_eight_bit = false);
78
79 /** Constructor to create an I2CEEBlockDevice using the pins and frequency of an existing I2C bus
80 *
81 * @param i2c_obj The I2C instance pointer
82 * @param address 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 address_is_eight_bit Specifies whether the EEPROM device is using eight bit
86 * addresses instead of 16 bit addresses. This is used for example
87 * in AT24C series chips.
88 */
90 mbed::I2C *i2c_obj, uint8_t address,
92 bool address_is_eight_bit = false);
93
94 /** Destructor of I2CEEBlockDevice
95 */
96
98
99 /** Initialize a block device
100 *
101 * @return 0 on success or a negative error code on failure
102 */
103 virtual int init();
104
105 /** Deinitialize a block device
106 *
107 * @return 0 on success or a negative error code on failure
108 */
109 virtual int deinit();
110
111 /** Read blocks from a block device
112 *
113 * @param buffer Buffer to write blocks to
114 * @param addr Address of block to begin reading from
115 * @param size Size to read in bytes, must be a multiple of read block size
116 * @return 0 on success, negative error code on failure
117 */
118 virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
119
120 /** Program blocks to a block device
121 *
122 * The blocks must have been erased prior to being programmed
123 *
124 * @param buffer Buffer of data to write to blocks
125 * @param addr Address of block to begin writing to
126 * @param size Size to write in bytes, must be a multiple of program block size
127 * @return 0 on success, negative error code on failure
128 */
129 virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
130
131 /** Erase blocks on a block device
132 *
133 * The state of an erased block is undefined until it has been programmed
134 *
135 * @param addr Address of block to begin erasing
136 * @param size Size to erase in bytes, must be a multiple of erase block size
137 * @return 0 on success, negative error code on failure
138 */
140
141 /** Get the size of a readable block
142 *
143 * @return Size of a readable block in bytes
144 */
146
147 /** Get the size of a programable block
148 *
149 * @return Size of a programable block in bytes
150 * @note Must be a multiple of the read size
151 */
153
154 /** Get the size of a eraseable block
155 *
156 * @return Size of a eraseable block in bytes
157 * @note Must be a multiple of the program size
158 */
160
161 /** Get the total size of the underlying device
162 *
163 * @return Size of the underlying device in bytes
164 */
165 virtual mbed::bd_size_t size() const;
166
167 /** Get the BlockDevice class type.
168 *
169 * @return A string representation of the BlockDevice class type.
170 */
171 virtual const char *get_type() const;
172
173private:
174 mbed::I2C *_i2c;
175 uint32_t _i2c_buffer[sizeof(mbed::I2C) / sizeof(uint32_t)];
176 uint8_t _i2c_addr;
177 bool _address_is_eight_bit;
178 uint32_t _size;
179 uint32_t _block;
180
181 int _sync();
182
183 /**
184 * Gets the device's I2C address with respect to the requested page.
185 * When eight-bit mode is disabled, this function is a noop.
186 * When eight-bit mode is enabled, it sets the bits required
187 * in the devices address. Other bits remain unchanged.
188 * @param address An address in the requested page.
189 * @return The device's I2C address for that page
190 */
191 uint8_t get_paged_device_address(mbed::bd_addr_t address);
192};
193
194
195#endif /* MBED_SD_BLOCK_DEVICE_H */
BlockDevice for I2C based flash device such as Microchip's 24LC or ATMEL's AT24C ranges.
virtual const char * get_type() const
Get the BlockDevice class type.
virtual mbed::bd_size_t get_read_size() const
Get the size of a readable block.
virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Read blocks from a block device.
virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Program blocks to 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.
virtual mbed::bd_size_t get_program_size() const
Get the size of a programable block.
virtual mbed::bd_size_t size() const
Get the total size of the underlying device.
I2CEEBlockDevice(mbed::I2C *i2c_obj, uint8_t address, mbed::bd_size_t size, mbed::bd_size_t block=32, bool address_is_eight_bit=false)
Constructor to create an I2CEEBlockDevice using the pins and frequency of an existing I2C bus.
virtual mbed::bd_size_t get_erase_size() const
Get the size of a eraseable block.
virtual int deinit()
Deinitialize a block device.
virtual int erase(mbed::bd_addr_t addr, mbed::bd_size_t size)
Erase blocks on a block device.
virtual ~I2CEEBlockDevice()
Destructor of I2CEEBlockDevice.
virtual int init()
Initialize a block device.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
An I2C Master, used for communicating with I2C slave devices.
Definition: I2C.h:211
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