Skip to content

Commit

Permalink
0.2.5 I2C_24LC1025
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Sep 11, 2023
1 parent 2038ff5 commit 710c148
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 48 deletions.
14 changes: 13 additions & 1 deletion libraries/I2C_24LC1025/CHANGELOG..md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.5] - 2023-09-09
- Sync I2C_EEPROM
- add writeProtectPin as optional parameter in **begin()**
- add **bool hasWriteProtectPin()**
- add **void allowWrite()**
- add **void preventWrite()**
- add **void setAutoWriteProtect(bool b)**
- add **bool getAutoWriteProtect()**
- optimized **waitEEReady()**
- update keywords.txt
- update readme.md


## [0.2.4] - 2023-05-11
- redo support for RP2040
- see issue #53 / #55 I2C_EEPROM


## [0.2.3] - 2023-05-02
- improve support for RP2040
- move code from .h to .cpp
Expand Down
102 changes: 86 additions & 16 deletions libraries/I2C_24LC1025/I2C_24LC1025.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: I2C_24LC1025.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: I2C_24LC1025 library for Arduino with EEPROM I2C_24LC1025 et al.
// URL: https://github.com/RobTillaart/I2C_24LC1025

Expand Down Expand Up @@ -33,7 +33,7 @@ I2C_24LC1025::I2C_24LC1025(uint8_t deviceAddress, TwoWire * wire)

#if defined(ESP8266) || defined(ESP32)

bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
{
if ((sda < 255) && (scl < 255))
{
Expand All @@ -44,12 +44,18 @@ bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
_wire->begin();
}
_lastWrite = 0;
_writeProtectPin = writeProtectPin;
if (_writeProtectPin >= 0)
{
pinMode(_writeProtectPin, OUTPUT);
preventWrite();
}
return isConnected();
}

#elif defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__)

bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
{
if ((sda < 255) && (scl < 255))
{
Expand All @@ -58,15 +64,28 @@ bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
_wire->begin();
}
_lastWrite = 0;
_writeProtectPin = writeProtectPin;
if (_writeProtectPin >= 0)
{
pinMode(_writeProtectPin, OUTPUT);
preventWrite();
}
return isConnected();
}

#endif


bool I2C_24LC1025::begin()
bool I2C_24LC1025::begin(int8_t writeProtectPin)
{
_wire->begin();
_lastWrite = 0;
_writeProtectPin = writeProtectPin;
if (_writeProtectPin >= 0)
{
pinMode(_writeProtectPin, OUTPUT);
preventWrite();
}
return isConnected();
}

Expand Down Expand Up @@ -149,7 +168,6 @@ uint32_t I2C_24LC1025::readBlock(const uint32_t memoryAddress, uint8_t * buffer,
addr += cnt;
buffer += cnt;
len -= cnt;
yield(); // For OS scheduling
}
return rv;
}
Expand Down Expand Up @@ -188,7 +206,6 @@ uint32_t I2C_24LC1025::updateBlock(const uint32_t memoryAddress, const uint8_t *
addr += cnt;
buffer += cnt;
len -= cnt;
yield(); // For OS scheduling
}
return rv;
}
Expand Down Expand Up @@ -251,7 +268,7 @@ bool I2C_24LC1025::updateBlockVerify(const uint32_t memoryAddress, const uint8_t
}


////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//
// METADATA SECTION
//
Expand Down Expand Up @@ -285,10 +302,51 @@ uint8_t I2C_24LC1025::getExtraWriteCycleTime()
}


//
// WRITEPROTECT
//
bool I2C_24LC1025::hasWriteProtectPin()
{
return (_writeProtectPin >= 0);
}


void I2C_24LC1025::allowWrite()
{
if (hasWriteProtectPin())
{
digitalWrite(_writeProtectPin, LOW);
}
}


void I2C_24LC1025::preventWrite()
{
if (hasWriteProtectPin())
{
digitalWrite(_writeProtectPin, HIGH);
}
}


void I2C_24LC1025::setAutoWriteProtect(bool b)
{
if (hasWriteProtectPin())
{
_autoWriteProtect = b;
}
}


bool I2C_24LC1025::getAutoWriteProtect()
{
return _autoWriteProtect;
}


////////////////////////////////////////////////////////////////////
//
// PRIVATE
// PRIVATE
//

// _pageBlock aligns buffer to page boundaries for writing.
Expand Down Expand Up @@ -348,13 +406,23 @@ void I2C_24LC1025::_beginTransmission(uint32_t memoryAddress)
int I2C_24LC1025::_WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length)
{
_waitEEReady();
if (_autoWriteProtect)
{
digitalWrite(_writeProtectPin, LOW);
}

this->_beginTransmission(memoryAddress);
_wire->write(buffer, length);
int rv = _wire->endTransmission();

if (_autoWriteProtect)
{
digitalWrite(_writeProtectPin, HIGH);
}

_lastWrite = micros();

yield();
yield(); // For OS scheduling

// if (rv != 0)
// {
Expand Down Expand Up @@ -389,16 +457,16 @@ uint8_t I2C_24LC1025::_ReadBlock(uint32_t memoryAddress, uint8_t * buffer, const
// Serial.print("\t");
// Serial.println(rv);
// }
return 0; // error
return 0; // error
}

// readBytes will always be equal or smaller to length
uint8_t readBytes = _wire->requestFrom(_actualAddress, length);
yield(); // For OS scheduling
uint8_t cnt = 0;
while (cnt < readBytes)
{
buffer[cnt++] = _wire->read();
yield();
}
return readBytes;
}
Expand All @@ -412,14 +480,16 @@ void I2C_24LC1025::_waitEEReady()
uint32_t waitTime = I2C_WRITEDELAY + _extraTWR * 1000UL;
while ((micros() - _lastWrite) <= waitTime)
{
_wire->beginTransmission(_deviceAddress);
int x = _wire->endTransmission();
if (x == 0) return;
yield();
if (isConnected()) return;
// TODO remove previous code
// _wire->beginTransmission(_deviceAddress);
// int x = _wire->endTransmission();
// if (x == 0) return;
yield(); // For OS scheduling
}
return;
}


// -- END OF FILE --
// -- END OF FILE --

45 changes: 31 additions & 14 deletions libraries/I2C_24LC1025/I2C_24LC1025.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: I2C_24LC1025.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: I2C_24LC1025 library for Arduino with EEPROM 24LC1025 et al.
// URL: https://github.com/RobTillaart/I2C_24LC1025

Expand All @@ -11,7 +11,7 @@
#include "Wire.h"


#define I2C_24LC1025_VERSION (F("0.2.4"))
#define I2C_24LC1025_VERSION (F("0.2.5"))


#define I2C_DEVICESIZE_24LC1025 131072
Expand All @@ -20,6 +20,7 @@

// to adjust low level timing (use with care)
// can also be done on command line.
// (see private _waitEEReady() function)
#ifndef I2C_WRITEDELAY
#define I2C_WRITEDELAY 5000
#endif
Expand All @@ -34,28 +35,30 @@ class I2C_24LC1025

// MBED test ==> see #55, #53 I2C_EEPROM
#if defined(ESP8266) || defined(ESP32) || (defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__))
bool begin(uint8_t sda, uint8_t scl);
// set the I2C pins explicitly (overrule)
bool begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin = -1);
#endif
bool begin();
bool isConnected();
// use default I2C pins.
bool begin(int8_t writeProtectPin = -1);
bool isConnected();


// writes a byte to memoryAddress
// returns I2C status, 0 = OK
int writeByte(const uint32_t memoryAddress, const uint8_t value);
int writeByte(const uint32_t memoryAddress, const uint8_t value);
// writes length bytes from buffer to EEPROM
// returns I2C status, 0 = OK
int writeBlock(const uint32_t memoryAddress, const uint8_t *buffer, const uint32_t length);
int writeBlock(const uint32_t memoryAddress, const uint8_t *buffer, const uint32_t length);
// set length bytes in the EEPROM to the same value.
// returns I2C status, 0 = OK
int setBlock(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);
int setBlock(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);


// returns the value stored in memoryAddress
uint8_t readByte(const uint32_t memoryAddress);
uint8_t readByte(const uint32_t memoryAddress);
// reads length bytes into buffer
// returns bytes read.
uint32_t readBlock(const uint32_t memoryAddress, uint8_t * buffer, const uint32_t length);
uint32_t readBlock(const uint32_t memoryAddress, uint8_t * buffer, const uint32_t length);


// updates a byte at memoryAddress, writes only if there is a new value.
Expand Down Expand Up @@ -88,6 +91,17 @@ class I2C_24LC1025
void setExtraWriteCycleTime(uint8_t ms);
uint8_t getExtraWriteCycleTime();


// WRITEPROTECT
// works only if WP pin is defined in begin().
// see readme.md
inline bool hasWriteProtectPin();
void allowWrite();
void preventWrite();
void setAutoWriteProtect(bool b);
bool getAutoWriteProtect();


private:
uint8_t _deviceAddress;
uint8_t _actualAddress; // a.k.a. controlByte
Expand All @@ -98,12 +112,12 @@ class I2C_24LC1025
int _error = 0; // TODO.


void _beginTransmission(uint32_t memoryAddress);
void _beginTransmission(uint32_t memoryAddress);

// returns I2C status, 0 = OK
int _pageBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint16_t length, const bool incrBuffer);
int _pageBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint16_t length, const bool incrBuffer);
// returns I2C status, 0 = OK
int _WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length);
int _WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length);
// returns bytes read.
uint8_t _ReadBlock(uint32_t memoryAddress, uint8_t * buffer, const uint8_t length);

Expand All @@ -112,7 +126,10 @@ class I2C_24LC1025

TwoWire * _wire;

bool _debug = false;
bool _debug = false;

int8_t _writeProtectPin = -1;
bool _autoWriteProtect = false;
};


Expand Down
Loading

0 comments on commit 710c148

Please sign in to comment.