Skip to content

Commit

Permalink
Change to fixed-width integers.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Feb 7, 2022
1 parent 8aa9d69 commit be39537
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=JC_EEPROM
version=1.0.3
version=1.0.4
author=Jack Christensen <[email protected]>
maintainer=Jack Christensen <[email protected]>
sentence=Arduino library to support external I2C EEPROMs.
Expand Down
19 changes: 9 additions & 10 deletions src/JC_EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// be identical).
// - pageSize is the EEPROM's page size in bytes.
// - eepromAddr is the EEPROM's I2C address and defaults to 0x50 which is common.
JC_EEPROM::JC_EEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, uint8_t eepromAddr)
JC_EEPROM::JC_EEPROM(eeprom_size_t deviceCapacity, uint8_t nDevice, uint16_t pageSize, uint8_t eepromAddr)
{
_dvcCapacity = deviceCapacity;
_nDevice = nDevice;
Expand Down Expand Up @@ -46,7 +46,7 @@ JC_EEPROM::JC_EEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pa
// when using a 400kHz bus 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 JC_EEPROM::begin(twiClockFreq_t twiFreq)
uint8_t JC_EEPROM::begin(twiClockFreq_t twiFreq)
{
Wire.begin();
Wire.setClock(twiFreq);
Expand All @@ -60,7 +60,7 @@ byte JC_EEPROM::begin(twiClockFreq_t twiFreq)
// If the I/O would extend past the top of the EEPROM address space,
// a status of EEPROM_ADDR_ERR is returned. For I2C errors, the status
// from the Arduino Wire library is passed back through to the caller.
byte JC_EEPROM::write(unsigned long addr, byte* values, unsigned int nBytes)
uint8_t JC_EEPROM::write(uint32_t addr, uint8_t* values, uint16_t nBytes)
{
uint8_t ctrlByte; // control byte (I2C device address & chip/block select bits)
uint8_t txStatus = 0; // transmit status
Expand Down Expand Up @@ -106,10 +106,10 @@ byte JC_EEPROM::write(unsigned long addr, byte* values, unsigned int nBytes)
// If the I/O would extend past the top of the EEPROM address space,
// a status of EEPROM_ADDR_ERR is returned. For I2C errors, the status
// from the Arduino Wire library is passed back through to the caller.
byte JC_EEPROM::read(unsigned long addr, byte* values, unsigned int nBytes)
uint8_t JC_EEPROM::read(uint32_t addr, uint8_t* values, uint16_t nBytes)
{
byte ctrlByte;
byte rxStatus;
uint8_t ctrlByte;
uint8_t rxStatus;
uint16_t nRead; // number of bytes to read
uint16_t nPage; // number of bytes remaining on current page, starting at addr

Expand Down Expand Up @@ -142,7 +142,7 @@ byte JC_EEPROM::read(unsigned long addr, byte* values, unsigned int nBytes)
// If the I/O would extend past the top of the EEPROM address space,
// a status of EEPROM_ADDR_ERR is returned. For I2C errors, the status
// from the Arduino Wire library is passed back through to the caller.
byte JC_EEPROM::write(unsigned long addr, byte value)
uint8_t JC_EEPROM::write(uint32_t addr, uint8_t value)
{
return write(addr, &value, 1);
}
Expand All @@ -152,11 +152,10 @@ byte JC_EEPROM::write(unsigned long addr, byte value)
// a status of EEPROM_ADDR_ERR is returned. For I2C errors, the status
// from the Arduino Wire library is passed back through to the caller.
// To distinguish error values from valid data, error values are returned as negative numbers.
int JC_EEPROM::read(unsigned long addr)
int16_t JC_EEPROM::read(uint32_t addr)
{
uint8_t data;
int ret;

ret = read(addr, &data, 1);
int16_t ret = read(addr, &data, 1);
return ret == 0 ? data : -ret;
}
18 changes: 9 additions & 9 deletions src/JC_EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ class JC_EEPROM
// upper address bound is exceeded
static const uint8_t EEPROM_ADDR_ERR {9};

JC_EEPROM(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, unsigned int nBytes);
byte write(unsigned long addr, byte value);
byte read(unsigned long addr, byte* values, unsigned int nBytes);
int read(unsigned long addr);
byte update(unsigned long addr, byte value)
JC_EEPROM(eeprom_size_t deviceCapacity, uint8_t nDevice,
uint16_t pageSize, uint8_t eepromAddr = 0x50);
uint8_t begin(twiClockFreq_t twiFreq = twiClock100kHz);
uint8_t write(uint32_t addr, uint8_t* values, uint16_t nBytes);
uint8_t write(uint32_t addr, uint8_t value);
uint8_t read(uint32_t addr, uint8_t* values, uint16_t nBytes);
int16_t read(uint32_t addr);
uint8_t update(uint32_t addr, uint8_t value)
{return (read(addr) == value) ? 0 : write(addr, &value, 1); }

private:
Expand All @@ -109,7 +109,7 @@ class JC_EEPROM
uint16_t _pageSize; // page size in bytes
uint8_t _csShift; // number of bits to shift address for chip select bits in control byte
uint16_t _nAddrBytes; // number of address bytes (1 or 2)
unsigned long _totalCapacity; // capacity of all EEPROM devices on the bus, in bytes
uint32_t _totalCapacity; // capacity of all EEPROM devices on the bus, in bytes
};

#endif

0 comments on commit be39537

Please sign in to comment.