diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e79464..6f997a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.7.1] - 2023-01-12 +- add setDeviceSize() +- add setPageSize() +- use setDeviceSize() in constructor to force power of 2. +- update unit test +- update GitHub actions +- update license +- update readme.md +- minor edits + + ## [1.7.0] - 2022-12-02 - fix #48 rewrote constructor. - ## [1.6.2] - 2022-10-30 - Add RP2040 support to build-CI. - Add CHANGELOG.md @@ -17,7 +27,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). as these are already handled on a lower level. See #46 - better explain **begin()** in readme.md - ## [1.6.1] - 2022-06-11 - update documentation - minor edits diff --git a/I2C_eeprom.cpp b/I2C_eeprom.cpp index 67662b4..4086cef 100644 --- a/I2C_eeprom.cpp +++ b/I2C_eeprom.cpp @@ -1,11 +1,9 @@ // // FILE: I2C_eeprom.cpp // AUTHOR: Rob Tillaart -// VERSION: 1.7.0 +// VERSION: 1.7.1 // PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al. // URL: https://github.com/RobTillaart/I2C_EEPROM.git -// -// HISTORY: see changelog.md #include "I2C_eeprom.h" @@ -46,11 +44,11 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, TwoWire * wire) : I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize, TwoWire * wire) { _deviceAddress = deviceAddress; - _deviceSize = deviceSize; + _deviceSize = setDeviceSize(deviceSize); _pageSize = getPageSize(_deviceSize); _wire = wire; - // Chips 16Kbit (2048 Bytes) or smaller only have one-word addresses. + // Chips 16 Kbit (2048 Bytes) or smaller only have one-word addresses. this->_isAddressSizeTwoWords = deviceSize > I2C_DEVICESIZE_24LC16; } @@ -326,6 +324,35 @@ uint8_t I2C_eeprom::getPageSize(uint32_t deviceSize) } +uint32_t I2C_eeprom::setDeviceSize(uint32_t deviceSize) +{ + uint32_t size = 128; + // force power of 2. + while ((size <= 65536) && ( size <= deviceSize)) + { + _deviceSize = size; + size *= 2; + } + // Chips 16 Kbit (2048 Bytes) or smaller only have one-word addresses. + this->_isAddressSizeTwoWords = _deviceSize > I2C_DEVICESIZE_24LC16; + return _deviceSize; +} + + +uint8_t I2C_eeprom::setPageSize(uint8_t pageSize) +{ + uint8_t size = 8; + // force power of 2. + while ((size <= 128) && ( size <= pageSize)) + { + _pageSize = size; + size *= 2; + } + return _pageSize; +} + + + //////////////////////////////////////////////////////////////////// // // PRIVATE diff --git a/I2C_eeprom.h b/I2C_eeprom.h index 562977c..8f9914c 100644 --- a/I2C_eeprom.h +++ b/I2C_eeprom.h @@ -2,18 +2,16 @@ // // FILE: I2C_eeprom.h // AUTHOR: Rob Tillaart -// VERSION: 1.7.0 +// VERSION: 1.7.1 // PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al. // URL: https://github.com/RobTillaart/I2C_EEPROM.git -// -// HISTORY: See I2C_eeprom.cpp #include "Arduino.h" #include "Wire.h" -#define I2C_EEPROM_VERSION (F("1.7.0")) +#define I2C_EEPROM_VERSION (F("1.7.1")) #define I2C_DEVICESIZE_24LC512 65536 @@ -105,6 +103,12 @@ public: uint8_t getPageSize(uint32_t deviceSize); uint32_t getLastWrite() { return _lastWrite; }; + // for overruling and debugging. + // forces a power of 2 + uint32_t setDeviceSize(uint32_t deviceSize); // returns set size + uint8_t setPageSize(uint8_t pageSize); // returns set size + + // TWR = WriteCycleTime // 5 ms is minimum, one can add extra ms here to adjust timing of both read() and write() void setExtraWriteCycleTime(uint8_t ms) { _extraTWR = ms; }; diff --git a/README.md b/README.md index d70c1ce..39bc81e 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ default address = 0x50 .. 0x57 depending on three address lines The interface is kept quite identical to the I2C_24LC1025 library. https://github.com/RobTillaart/I2C_24LC1025 -Most important change is 32 bit memory addresses. +Most important difference is 32 bit memory addresses. ### Constructor @@ -135,6 +135,14 @@ Test results The function cannot detect smaller than 128 bit EEPROMS. +Experimental since 1.7.1 can be used for debugging and overruling constructor. + +- **uint32_t setDeviceSize(uint32_t deviceSize)** overrules constructor setting. +returns set size == 128, 256, ... 32768, 65536 +- **uint8_t setPageSize(uint8_t pageSize)** overrules constructor setting. +returns set size == 8, 16, 32, 64, 128. + + #### UpdateBlock() (new since 1.4.2) @@ -179,6 +187,10 @@ See examples - investigate multi-EEPROM storage - wrapper class? - improve error handling, write functions should return bytes written or so. +- move code from .h to .cpp? +- make deviceSize explicit in examples? +- AT24C32 has a WriteCycle Time of max 20 ms + - make a define of the 5000 ? #### could - investigate smarter strategy for **updateBlock()** diff --git a/library.json b/library.json index d259baf..d0d6ff5 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/I2C_EEPROM.git" }, - "version": "1.7.0", + "version": "1.7.1", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index 471f007..18161ef 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=I2C_EEPROM -version=1.7.0 +version=1.7.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library for I2C EEPROMS diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index b7f4533..0201106 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -88,7 +88,6 @@ unittest(test_constants) assertEqual(I2C_DEVICESIZE_24LC04, 512); assertEqual(I2C_DEVICESIZE_24LC02, 256); assertEqual(I2C_DEVICESIZE_24LC01, 128); - } @@ -106,9 +105,62 @@ unittest(test_getPageSize) assertEqual(EE.getPageSize(I2C_DEVICESIZE_24LC04), 16); assertEqual(EE.getPageSize(I2C_DEVICESIZE_24LC02), 8); assertEqual(EE.getPageSize(I2C_DEVICESIZE_24LC01), 8); + + I2C_eeprom prom(0x50, 0x8123); + assertEqual(prom.getDeviceSize(), 0x8000); + assertEqual(prom.getPageSize(), 64); } +unittest(test_setDeviceSize) +{ + I2C_eeprom EE(0x50, 0x8000); + + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC512), I2C_DEVICESIZE_24LC512); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC256), I2C_DEVICESIZE_24LC256); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC128), I2C_DEVICESIZE_24LC128); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC64), I2C_DEVICESIZE_24LC64); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC32), I2C_DEVICESIZE_24LC32); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC16), I2C_DEVICESIZE_24LC16); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC08), I2C_DEVICESIZE_24LC08); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC04), I2C_DEVICESIZE_24LC04); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC02), I2C_DEVICESIZE_24LC02); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC01), I2C_DEVICESIZE_24LC01); + + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC512 + 1), I2C_DEVICESIZE_24LC512); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC256 + 1), I2C_DEVICESIZE_24LC256); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC128 + 1), I2C_DEVICESIZE_24LC128); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC64 + 1), I2C_DEVICESIZE_24LC64); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC32 + 1), I2C_DEVICESIZE_24LC32); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC16 + 1), I2C_DEVICESIZE_24LC16); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC08 + 1), I2C_DEVICESIZE_24LC08); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC04 + 1), I2C_DEVICESIZE_24LC04); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC02 + 1), I2C_DEVICESIZE_24LC02); + assertEqual(EE.setDeviceSize(I2C_DEVICESIZE_24LC01 + 1), I2C_DEVICESIZE_24LC01); + +} + +/* +unittest(test_setPageSize) +{ + I2C_eeprom EE(0x50, 0x8000); + + assertEqual(EE.setPageSize(129), 128); + assertEqual(EE.setPageSize(128), 128); + assertEqual(EE.setPageSize(127), 64); + assertEqual(EE.setPageSize(65), 64); + assertEqual(EE.setPageSize(64), 64); + assertEqual(EE.setPageSize(63), 32); + assertEqual(EE.setPageSize(33), 32); + assertEqual(EE.setPageSize(32), 32); + assertEqual(EE.setPageSize(31), 16); + assertEqual(EE.setPageSize(17), 16); + assertEqual(EE.setPageSize(16), 16); + assertEqual(EE.setPageSize(9), 8); + assertEqual(EE.setPageSize(8), 8); +} +*/ + unittest_main() // --------