1
1
//
2
2
// FILE: I2C_24LC1025.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.2.4
4
+ // VERSION: 0.2.5
5
5
// PURPOSE: I2C_24LC1025 library for Arduino with EEPROM I2C_24LC1025 et al.
6
6
// URL: https://github.com/RobTillaart/I2C_24LC1025
7
7
@@ -33,7 +33,7 @@ I2C_24LC1025::I2C_24LC1025(uint8_t deviceAddress, TwoWire * wire)
33
33
34
34
#if defined(ESP8266) || defined(ESP32)
35
35
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 )
37
37
{
38
38
if ((sda < 255 ) && (scl < 255 ))
39
39
{
@@ -44,12 +44,18 @@ bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
44
44
_wire->begin ();
45
45
}
46
46
_lastWrite = 0 ;
47
+ _writeProtectPin = writeProtectPin;
48
+ if (_writeProtectPin >= 0 )
49
+ {
50
+ pinMode (_writeProtectPin, OUTPUT);
51
+ preventWrite ();
52
+ }
47
53
return isConnected ();
48
54
}
49
55
50
56
#elif defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__)
51
57
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 )
53
59
{
54
60
if ((sda < 255 ) && (scl < 255 ))
55
61
{
@@ -58,15 +64,28 @@ bool I2C_24LC1025::begin(uint8_t sda, uint8_t scl)
58
64
_wire->begin ();
59
65
}
60
66
_lastWrite = 0 ;
67
+ _writeProtectPin = writeProtectPin;
68
+ if (_writeProtectPin >= 0 )
69
+ {
70
+ pinMode (_writeProtectPin, OUTPUT);
71
+ preventWrite ();
72
+ }
61
73
return isConnected ();
62
74
}
75
+
63
76
#endif
64
77
65
78
66
- bool I2C_24LC1025::begin ()
79
+ bool I2C_24LC1025::begin (int8_t writeProtectPin )
67
80
{
68
81
_wire->begin ();
69
82
_lastWrite = 0 ;
83
+ _writeProtectPin = writeProtectPin;
84
+ if (_writeProtectPin >= 0 )
85
+ {
86
+ pinMode (_writeProtectPin, OUTPUT);
87
+ preventWrite ();
88
+ }
70
89
return isConnected ();
71
90
}
72
91
@@ -149,7 +168,6 @@ uint32_t I2C_24LC1025::readBlock(const uint32_t memoryAddress, uint8_t * buffer,
149
168
addr += cnt;
150
169
buffer += cnt;
151
170
len -= cnt;
152
- yield (); // For OS scheduling
153
171
}
154
172
return rv;
155
173
}
@@ -188,7 +206,6 @@ uint32_t I2C_24LC1025::updateBlock(const uint32_t memoryAddress, const uint8_t *
188
206
addr += cnt;
189
207
buffer += cnt;
190
208
len -= cnt;
191
- yield (); // For OS scheduling
192
209
}
193
210
return rv;
194
211
}
@@ -251,7 +268,7 @@ bool I2C_24LC1025::updateBlockVerify(const uint32_t memoryAddress, const uint8_t
251
268
}
252
269
253
270
254
- // //////////////////////////////////////////////////////////////////
271
+ // ///////////////////////////////////////////////////////////
255
272
//
256
273
// METADATA SECTION
257
274
//
@@ -285,10 +302,51 @@ uint8_t I2C_24LC1025::getExtraWriteCycleTime()
285
302
}
286
303
287
304
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
+
288
346
289
347
// //////////////////////////////////////////////////////////////////
290
348
//
291
- // PRIVATE
349
+ // PRIVATE
292
350
//
293
351
294
352
// _pageBlock aligns buffer to page boundaries for writing.
@@ -348,13 +406,23 @@ void I2C_24LC1025::_beginTransmission(uint32_t memoryAddress)
348
406
int I2C_24LC1025::_WriteBlock (uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length)
349
407
{
350
408
_waitEEReady ();
409
+ if (_autoWriteProtect)
410
+ {
411
+ digitalWrite (_writeProtectPin, LOW);
412
+ }
351
413
352
414
this ->_beginTransmission (memoryAddress);
353
415
_wire->write (buffer, length);
354
416
int rv = _wire->endTransmission ();
417
+
418
+ if (_autoWriteProtect)
419
+ {
420
+ digitalWrite (_writeProtectPin, HIGH);
421
+ }
422
+
355
423
_lastWrite = micros ();
356
424
357
- yield ();
425
+ yield (); // For OS scheduling
358
426
359
427
// if (rv != 0)
360
428
// {
@@ -389,16 +457,16 @@ uint8_t I2C_24LC1025::_ReadBlock(uint32_t memoryAddress, uint8_t * buffer, const
389
457
// Serial.print("\t");
390
458
// Serial.println(rv);
391
459
// }
392
- return 0 ; // error
460
+ return 0 ; // error
393
461
}
394
462
395
463
// readBytes will always be equal or smaller to length
396
464
uint8_t readBytes = _wire->requestFrom (_actualAddress, length);
465
+ yield (); // For OS scheduling
397
466
uint8_t cnt = 0 ;
398
467
while (cnt < readBytes)
399
468
{
400
469
buffer[cnt++] = _wire->read ();
401
- yield ();
402
470
}
403
471
return readBytes;
404
472
}
@@ -412,14 +480,16 @@ void I2C_24LC1025::_waitEEReady()
412
480
uint32_t waitTime = I2C_WRITEDELAY + _extraTWR * 1000UL ;
413
481
while ((micros () - _lastWrite) <= waitTime)
414
482
{
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
419
489
}
420
490
return ;
421
491
}
422
492
423
493
424
- // -- END OF FILE --
494
+ // -- END OF FILE --
425
495
0 commit comments