Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Add begin() function with ability to specify I2C clock frequency.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Jul 20, 2014
1 parent 45d42a1 commit 36156f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 8 additions & 4 deletions extEEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@
// - nDevice is the number of EEPROM devices on the I2C bus (all must
// be identical).
// - pageSize is the EEPROM's page size in bytes.
extEEPROM::extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize)
extEEPROM::extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, uint8_t eepromAddr)
{
_dvcCapacity = deviceCapacity;
_nDevice = nDevice;
_pageSize = pageSize;
_eepromAddr = eepromAddr;
_totalCapacity = _nDevice * _dvcCapacity * 1024UL / 8;
_nAddrBytes = deviceCapacity > kbits_16 ? 2 : 1; //two address bytes needed for eeproms > 16kbits

Expand All @@ -73,12 +74,15 @@ extEEPROM::extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pa
}
}

//initialize the I2C bus and try a dummy write (no data sent)
//initialize the I2C bus and do a dummy write (no data sent)
//to the device so that the caller can determine whether it is responding.
byte extEEPROM::begin(uint8_t eepromAddr)
//when using a 400kHz clock speed and there are multiple I2C devices on the
//bus (other than EEPROM), call extEEPROM::begin() after any initialization
//calls for the other devices to ensure the intended I2C clock speed is set.
byte extEEPROM::begin(twiClockFreq_t twiFreq)
{
_eepromAddr = eepromAddr;
Wire.begin(_eepromAddr);
TWBR = ( (F_CPU / twiFreq) - 16) / 2;
Wire.beginTransmission(_eepromAddr);
if (_nAddrBytes == 2) i2cWrite(0); //high addr byte
i2cWrite(0); //low addr byte
Expand Down
6 changes: 4 additions & 2 deletions extEEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ enum eeprom_size_t {
kbits_2048 = 2048
};

enum twiClockFreq_t { twiClock100kHz = 100000, twiClock400kHz = 400000 };

//EEPROM addressing error, returned by write() or read() if upper address bound is exceeded
const uint8_t EEPROM_ADDR_ERR = 9;

class extEEPROM
{
public:
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize);
byte begin(byte eepromAddr = 0x50);
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, byte eepromAddr = 0x50);
byte begin(twiClockFreq_t twiFreq = twiClock100kHz);
byte write(unsigned long addr, byte *values, byte nBytes);
byte read(unsigned long addr, byte *values, byte nBytes);

Expand Down

0 comments on commit 36156f5

Please sign in to comment.