Skip to content

Commit 710c148

Browse files
committed
0.2.5 I2C_24LC1025
1 parent 2038ff5 commit 710c148

File tree

7 files changed

+225
-48
lines changed

7 files changed

+225
-48
lines changed

libraries/I2C_24LC1025/CHANGELOG..md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77

8+
## [0.2.5] - 2023-09-09
9+
- Sync I2C_EEPROM
10+
- add writeProtectPin as optional parameter in **begin()**
11+
- add **bool hasWriteProtectPin()**
12+
- add **void allowWrite()**
13+
- add **void preventWrite()**
14+
- add **void setAutoWriteProtect(bool b)**
15+
- add **bool getAutoWriteProtect()**
16+
- optimized **waitEEReady()**
17+
- update keywords.txt
18+
- update readme.md
19+
20+
821
## [0.2.4] - 2023-05-11
922
- redo support for RP2040
1023
- see issue #53 / #55 I2C_EEPROM
1124

12-
1325
## [0.2.3] - 2023-05-02
1426
- improve support for RP2040
1527
- move code from .h to .cpp

libraries/I2C_24LC1025/I2C_24LC1025.cpp

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: I2C_24LC1025.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.4
4+
// VERSION: 0.2.5
55
// PURPOSE: I2C_24LC1025 library for Arduino with EEPROM I2C_24LC1025 et al.
66
// URL: https://github.com/RobTillaart/I2C_24LC1025
77

@@ -33,7 +33,7 @@ I2C_24LC1025::I2C_24LC1025(uint8_t deviceAddress, TwoWire * wire)
3333

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

36-
bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
36+
bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
3737
{
3838
if ((sda < 255) && (scl < 255))
3939
{
@@ -44,12 +44,18 @@ bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
4444
_wire->begin();
4545
}
4646
_lastWrite = 0;
47+
_writeProtectPin = writeProtectPin;
48+
if (_writeProtectPin >= 0)
49+
{
50+
pinMode(_writeProtectPin, OUTPUT);
51+
preventWrite();
52+
}
4753
return isConnected();
4854
}
4955

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

52-
bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
58+
bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
5359
{
5460
if ((sda < 255) && (scl < 255))
5561
{
@@ -58,15 +64,28 @@ bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
5864
_wire->begin();
5965
}
6066
_lastWrite = 0;
67+
_writeProtectPin = writeProtectPin;
68+
if (_writeProtectPin >= 0)
69+
{
70+
pinMode(_writeProtectPin, OUTPUT);
71+
preventWrite();
72+
}
6173
return isConnected();
6274
}
75+
6376
#endif
6477

6578

66-
bool I2C_24LC1025::begin()
79+
bool I2C_24LC1025::begin(int8_t writeProtectPin)
6780
{
6881
_wire->begin();
6982
_lastWrite = 0;
83+
_writeProtectPin = writeProtectPin;
84+
if (_writeProtectPin >= 0)
85+
{
86+
pinMode(_writeProtectPin, OUTPUT);
87+
preventWrite();
88+
}
7089
return isConnected();
7190
}
7291

@@ -149,7 +168,6 @@ uint32_t I2C_24LC1025::readBlock(const uint32_t memoryAddress, uint8_t * buffer,
149168
addr += cnt;
150169
buffer += cnt;
151170
len -= cnt;
152-
yield(); // For OS scheduling
153171
}
154172
return rv;
155173
}
@@ -188,7 +206,6 @@ uint32_t I2C_24LC1025::updateBlock(const uint32_t memoryAddress, const uint8_t *
188206
addr += cnt;
189207
buffer += cnt;
190208
len -= cnt;
191-
yield(); // For OS scheduling
192209
}
193210
return rv;
194211
}
@@ -251,7 +268,7 @@ bool I2C_24LC1025::updateBlockVerify(const uint32_t memoryAddress, const uint8_t
251268
}
252269

253270

254-
////////////////////////////////////////////////////////////////////
271+
/////////////////////////////////////////////////////////////
255272
//
256273
// METADATA SECTION
257274
//
@@ -285,10 +302,51 @@ uint8_t I2C_24LC1025::getExtraWriteCycleTime()
285302
}
286303

287304

305+
//
306+
// WRITEPROTECT
307+
//
308+
bool I2C_24LC1025::hasWriteProtectPin()
309+
{
310+
return (_writeProtectPin >= 0);
311+
}
312+
313+
314+
void I2C_24LC1025::allowWrite()
315+
{
316+
if (hasWriteProtectPin())
317+
{
318+
digitalWrite(_writeProtectPin, LOW);
319+
}
320+
}
321+
322+
323+
void I2C_24LC1025::preventWrite()
324+
{
325+
if (hasWriteProtectPin())
326+
{
327+
digitalWrite(_writeProtectPin, HIGH);
328+
}
329+
}
330+
331+
332+
void I2C_24LC1025::setAutoWriteProtect(bool b)
333+
{
334+
if (hasWriteProtectPin())
335+
{
336+
_autoWriteProtect = b;
337+
}
338+
}
339+
340+
341+
bool I2C_24LC1025::getAutoWriteProtect()
342+
{
343+
return _autoWriteProtect;
344+
}
345+
288346

289347
////////////////////////////////////////////////////////////////////
290348
//
291-
// PRIVATE
349+
// PRIVATE
292350
//
293351

294352
// _pageBlock aligns buffer to page boundaries for writing.
@@ -348,13 +406,23 @@ void I2C_24LC1025::_beginTransmission(uint32_t memoryAddress)
348406
int I2C_24LC1025::_WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length)
349407
{
350408
_waitEEReady();
409+
if (_autoWriteProtect)
410+
{
411+
digitalWrite(_writeProtectPin, LOW);
412+
}
351413

352414
this->_beginTransmission(memoryAddress);
353415
_wire->write(buffer, length);
354416
int rv = _wire->endTransmission();
417+
418+
if (_autoWriteProtect)
419+
{
420+
digitalWrite(_writeProtectPin, HIGH);
421+
}
422+
355423
_lastWrite = micros();
356424

357-
yield();
425+
yield(); // For OS scheduling
358426

359427
// if (rv != 0)
360428
// {
@@ -389,16 +457,16 @@ uint8_t I2C_24LC1025::_ReadBlock(uint32_t memoryAddress, uint8_t * buffer, const
389457
// Serial.print("\t");
390458
// Serial.println(rv);
391459
// }
392-
return 0; // error
460+
return 0; // error
393461
}
394462

395463
// readBytes will always be equal or smaller to length
396464
uint8_t readBytes = _wire->requestFrom(_actualAddress, length);
465+
yield(); // For OS scheduling
397466
uint8_t cnt = 0;
398467
while (cnt < readBytes)
399468
{
400469
buffer[cnt++] = _wire->read();
401-
yield();
402470
}
403471
return readBytes;
404472
}
@@ -412,14 +480,16 @@ void I2C_24LC1025::_waitEEReady()
412480
uint32_t waitTime = I2C_WRITEDELAY + _extraTWR * 1000UL;
413481
while ((micros() - _lastWrite) <= waitTime)
414482
{
415-
_wire->beginTransmission(_deviceAddress);
416-
int x = _wire->endTransmission();
417-
if (x == 0) return;
418-
yield();
483+
if (isConnected()) return;
484+
// TODO remove previous code
485+
// _wire->beginTransmission(_deviceAddress);
486+
// int x = _wire->endTransmission();
487+
// if (x == 0) return;
488+
yield(); // For OS scheduling
419489
}
420490
return;
421491
}
422492

423493

424-
// -- END OF FILE --
494+
// -- END OF FILE --
425495

libraries/I2C_24LC1025/I2C_24LC1025.h

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: I2C_24LC1025.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.4
5+
// VERSION: 0.2.5
66
// PURPOSE: I2C_24LC1025 library for Arduino with EEPROM 24LC1025 et al.
77
// URL: https://github.com/RobTillaart/I2C_24LC1025
88

@@ -11,7 +11,7 @@
1111
#include "Wire.h"
1212

1313

14-
#define I2C_24LC1025_VERSION (F("0.2.4"))
14+
#define I2C_24LC1025_VERSION (F("0.2.5"))
1515

1616

1717
#define I2C_DEVICESIZE_24LC1025 131072
@@ -20,6 +20,7 @@
2020

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

3536
// MBED test ==> see #55, #53 I2C_EEPROM
3637
#if defined(ESP8266) || defined(ESP32) || (defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__))
37-
bool begin(uint8_t sda, uint8_t scl);
38+
// set the I2C pins explicitly (overrule)
39+
bool begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin = -1);
3840
#endif
39-
bool begin();
40-
bool isConnected();
41+
// use default I2C pins.
42+
bool begin(int8_t writeProtectPin = -1);
43+
bool isConnected();
4144

4245

4346
// writes a byte to memoryAddress
4447
// returns I2C status, 0 = OK
45-
int writeByte(const uint32_t memoryAddress, const uint8_t value);
48+
int writeByte(const uint32_t memoryAddress, const uint8_t value);
4649
// writes length bytes from buffer to EEPROM
4750
// returns I2C status, 0 = OK
48-
int writeBlock(const uint32_t memoryAddress, const uint8_t *buffer, const uint32_t length);
51+
int writeBlock(const uint32_t memoryAddress, const uint8_t *buffer, const uint32_t length);
4952
// set length bytes in the EEPROM to the same value.
5053
// returns I2C status, 0 = OK
51-
int setBlock(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);
54+
int setBlock(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);
5255

5356

5457
// returns the value stored in memoryAddress
55-
uint8_t readByte(const uint32_t memoryAddress);
58+
uint8_t readByte(const uint32_t memoryAddress);
5659
// reads length bytes into buffer
5760
// returns bytes read.
58-
uint32_t readBlock(const uint32_t memoryAddress, uint8_t * buffer, const uint32_t length);
61+
uint32_t readBlock(const uint32_t memoryAddress, uint8_t * buffer, const uint32_t length);
5962

6063

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

94+
95+
// WRITEPROTECT
96+
// works only if WP pin is defined in begin().
97+
// see readme.md
98+
inline bool hasWriteProtectPin();
99+
void allowWrite();
100+
void preventWrite();
101+
void setAutoWriteProtect(bool b);
102+
bool getAutoWriteProtect();
103+
104+
91105
private:
92106
uint8_t _deviceAddress;
93107
uint8_t _actualAddress; // a.k.a. controlByte
@@ -98,12 +112,12 @@ class I2C_24LC1025
98112
int _error = 0; // TODO.
99113

100114

101-
void _beginTransmission(uint32_t memoryAddress);
115+
void _beginTransmission(uint32_t memoryAddress);
102116

103117
// returns I2C status, 0 = OK
104-
int _pageBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint16_t length, const bool incrBuffer);
118+
int _pageBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint16_t length, const bool incrBuffer);
105119
// returns I2C status, 0 = OK
106-
int _WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length);
120+
int _WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length);
107121
// returns bytes read.
108122
uint8_t _ReadBlock(uint32_t memoryAddress, uint8_t * buffer, const uint8_t length);
109123

@@ -112,7 +126,10 @@ class I2C_24LC1025
112126

113127
TwoWire * _wire;
114128

115-
bool _debug = false;
129+
bool _debug = false;
130+
131+
int8_t _writeProtectPin = -1;
132+
bool _autoWriteProtect = false;
116133
};
117134

118135

0 commit comments

Comments
 (0)