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)
348406int 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
0 commit comments