24 #include "I2CMaster.h"
97 , _size((type & 0xFFFF) * ((type >> 16) & 0x0FFF))
98 , _pageSize((type >> 16) & 0x0FFF)
99 , _mode((uint8_t)((type >> 28) & 0x0F))
105 i2cAddress += (bank & 0x07);
107 case EE_BSEL_8BIT_ADDR: {
108 uint8_t addrBits = 8;
109 unsigned long size = 0x0100;
110 while (size < _size) {
115 i2cAddress += ((bank << (addrBits - 8)) & 0x07);
117 case EE_BSEL_17BIT_ADDR:
118 i2cAddress += ((bank << 1) & 0x06);
120 case EE_BSEL_17BIT_ADDR_ALT:
121 i2cAddress += bank & 0x03;
169 if (address >= _size)
171 writeAddress(address);
189 if (address >= _size || !length)
191 if ((address + length) > _size)
192 length = (size_t)(_size - address);
193 writeAddress(address);
194 if (!_bus->
startRead(i2cAddress, length))
196 uint8_t *d = (uint8_t *)data;
197 unsigned int count = 0;
215 if (address >= _size)
217 writeAddress(address);
219 return waitForWrite();
237 if (address >= _size)
239 if ((address + length) > _size)
240 length = (size_t)(_size - address);
241 bool needAddress =
true;
244 const uint8_t *d = (
const uint8_t *)data;
247 writeAddress(address);
253 if ((address & (_pageSize - 1)) == 0) {
267 return result + page;
270 void EEPROM24::writeAddress(
unsigned long address)
275 _bus->
write((uint8_t)(address >> 8));
276 _bus->
write((uint8_t)address);
278 case EE_BSEL_8BIT_ADDR:
279 _bus->
startWrite(i2cAddress | (((uint8_t)(address >> 8)) & 0x07));
280 _bus->
write((uint8_t)address);
282 case EE_BSEL_17BIT_ADDR:
283 _bus->
startWrite(i2cAddress | (((uint8_t)(address >> 16)) & 0x01));
284 _bus->
write((uint8_t)(address >> 8));
285 _bus->
write((uint8_t)address);
287 case EE_BSEL_17BIT_ADDR_ALT:
288 _bus->
startWrite(i2cAddress | (((uint8_t)(address >> 14)) & 0x04));
289 _bus->
write((uint8_t)(address >> 8));
290 _bus->
write((uint8_t)address);
295 bool EEPROM24::waitForWrite()
302 unsigned count = 1000;
unsigned long size() const
Returns the size of the EEPROM in bytes.
uint8_t read(unsigned long address)
Reads a single byte from the EEPROM at address.
virtual void write(uint8_t value)=0
Writes a single byte value on the I2C bus.
virtual bool startRead(unsigned int address, unsigned int count)=0
Starts a read operation for count bytes by sending the start condition and the I2C control byte...
EEPROM24(I2CMaster &bus, unsigned long type, uint8_t bank=0)
Constructs a new EEPROM access object on bus for an EEPROM of the specified type. ...
bool write(unsigned long address, uint8_t value)
Writes a byte value to address in the EEPROM.
virtual unsigned int available()=0
Returns the number of bytes that are still available for reading.
virtual void startWrite(unsigned int address)
Starts a write operation by sending a start condition and the I2C control byte.
virtual bool endWrite()=0
Ends the current write operation.
Abstract base class for I2C master implementations.
bool available()
Returns true if the EEPROM is available on the I2C bus; false otherwise.
virtual uint8_t read()=0
Reads a single byte from the I2C bus.