Skip to content

Commit

Permalink
add determineSizeNoWrite() (#62)
Browse files Browse the repository at this point in the history
- add **uint32_t determineSizeNoWrite()**, kudos to roelandkluit
- add example
- minor edits
  • Loading branch information
RobTillaart authored Dec 14, 2023
1 parent 0248508 commit 42cdf1a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [1.8.1] - 2023-12-14
- add **uint32_t determineSizeNoWrite()**, kudos to roelandkluit
- add example
- minor edits


## [1.8.0] - 2023-11-24 (breaking change)
- simplify **begin()**, remove setting Wire pins from library.
- add **getAddress()**
- update readme.md
- update examples

----

## [1.7.4] - 2023-09-06
- solve #57 add support for WriteProtectPin
Expand Down
43 changes: 23 additions & 20 deletions I2C_eeprom.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: I2C_eeprom.cpp
// AUTHOR: Rob Tillaart
// VERSION: 1.8.0
// VERSION: 1.8.1
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
// URL: https://github.com/RobTillaart/I2C_EEPROM.git

Expand Down Expand Up @@ -304,33 +304,36 @@ uint32_t I2C_eeprom::determineSize(const bool debug)
return 0;
}


// new 1.8.1 #61
uint32_t I2C_eeprom::determineSizeNoWrite()
{
// try to read a byte to see if connected
if (!isConnected()) return 0;
// try to read a byte to see if connected
if (!isConnected()) return 0;

bool addressSize = _isAddressSizeTwoWords;
byte dummyVal = 0;
uint32_t lastOkSize = 0;
bool addressSize = _isAddressSizeTwoWords;
byte dummyVal = 0;
uint32_t lastOkSize = 0;

for (uint32_t size = 128; size <= 65536; size *= 2)
{
_isAddressSizeTwoWords = (size > I2C_DEVICESIZE_24LC16); // == 2048

for (uint32_t size = 128; size <= 65536; size *= 2)
// Try to read last byte of the block, should return length of 0 when fails
if (readBlock(size - 1, &dummyVal, 1) == 0)
{
_isAddressSizeTwoWords = size > I2C_DEVICESIZE_24LC16; // 2048

//Try to read last byte of the block, should return lenght of 0 when fails
if (readBlock(size - 1, &dummyVal, 1) == 0)
{
_isAddressSizeTwoWords = addressSize;
break;
}
else
{
lastOkSize = size;
}
_isAddressSizeTwoWords = addressSize;
break;
}
return lastOkSize;
else
{
lastOkSize = size;
}
}
return lastOkSize;
}


uint32_t I2C_eeprom::getDeviceSize()
{
return _deviceSize;
Expand Down
2 changes: 1 addition & 1 deletion I2C_eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: I2C_eeprom.h
// AUTHOR: Rob Tillaart
// VERSION: 1.8.0
// VERSION: 1.8.1
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
// URL: https://github.com/RobTillaart/I2C_EEPROM.git

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ Same as write and update functions above. Returns true if successful, false indi
- **uint8_t getPageSize()** idem
- **uint8_t getPageSize(uint32_t deviceSize)** idem
- **uint32_t getLastWrite()** idem
- **uint32_t determineSizeNoWrite()** function that determines the size of the EEPROM
by detecting when a selected memory address is not readable. (new in 1.8.1).
- **uint32_t determineSize(bool debug = false)**
function that determines the size of the EEPROM by detecting when a memory address
is folded upon memory address 0.
Expand All @@ -147,7 +149,7 @@ The debug flag prints some output to Serial.

**Warning**: this function has changed (again) in 1.4.0

Test results
Test results **determineSize()**

| Type | returns | Memory | Page Size | Notes |
|:--------|:--------|:---------|:---------:|:------|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// FILE: I2C_eeprom_determineSizeNoWrite.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test determineSizeNoWrite() function


#include "Wire.h"
#include "I2C_eeprom.h"


I2C_eeprom ee(0x50, I2C_DEVICESIZE_24LC256);

uint32_t start, diff;


void setup()
{
Serial.begin(115200);
while (!Serial); // wait for Serial port to connect. Needed for Leonardo only
Serial.println(__FILE__);
Serial.print("I2C_EEPROM_VERSION: ");
Serial.println(I2C_EEPROM_VERSION);

Wire.begin();

ee.begin();
if (! ee.isConnected())
{
Serial.println("ERROR: Can't find eeprom\nstopped...");
while (1);
}

Serial.println("\nDetermine size no write");
delay(100);

start = micros();
uint32_t size = ee.determineSizeNoWrite();
diff = micros() - start;
Serial.print("TIME: ");
Serial.print(diff);
Serial.println(" us.");
if (size == 0)
{
Serial.println("SIZE: could not determine size");
}
else if (size > 1024)
{
Serial.print("SIZE: ");
Serial.print(size / 1024);
Serial.println(" KB.");
}
else
{
Serial.print("SIZE: ");
Serial.print(size);
Serial.println(" bytes.");
}

Serial.print("PAGE: ");
uint8_t pageSize = ee.getPageSize(size);
Serial.print(pageSize);
Serial.println(" bytes.");

Serial.println("Done...");
}


void loop()
{
}


// -- END OF FILE --
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ updateByteVerify KEYWORD2
updateBlockVerify KEYWORD2

determineSize KEYWORD2
determineSizeNoWrite KEYWORD2
getDeviceSize KEYWORD2
getPageSize KEYWORD2
getLastWrite KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
},
"version": "1.8.0",
"version": "1.8.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down

0 comments on commit 42cdf1a

Please sign in to comment.