Skip to content

Commit 4b4a52c

Browse files
committed
1.7.4 I2C_EEPROM
1 parent 711ddef commit 4b4a52c

File tree

7 files changed

+174
-22
lines changed

7 files changed

+174
-22
lines changed

libraries/I2C_EEPROM/CHANGELOG.md

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

88

9+
## [1.7.4] - 2023-09-06
10+
- solve #57 add support for WriteProtectPin
11+
- add writeProtectPin as optional parameter in **begin()**
12+
- add **bool hasWriteProtectPin()**
13+
- add **void allowWrite()**
14+
- add **void preventWrite()**
15+
- add **void setAutoWriteProtect(bool b)**
16+
- add **bool getAutoWriteProtect()**
17+
- optimized **waitEEReady()**
18+
- update keywords.txt
19+
- update readme.md
20+
21+
922
## [1.7.3] - 2023-05-10
1023
- fix #55 ==> redo fix #53
1124
- add test to detect **MBED** and **RP2040**
1225
- adjust **I2C_BUFFERSIZE** for RP2040 to 128.
1326
- update readme.md
1427

15-
1628
## [1.7.2] - 2023-05-02
1729
- fix #53 support RP2040 (kudos to jotamachuca)
1830
- move code from .h to .cpp

libraries/I2C_EEPROM/I2C_eeprom.cpp

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: I2C_eeprom.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 1.7.3
4+
// VERSION: 1.7.4
55
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
66
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
77

@@ -55,7 +55,7 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize, T
5555

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

58-
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
58+
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
5959
{
6060
// if (_wire == 0) Serial.println("zero"); // test #48
6161
if ((sda < 255) && (scl < 255))
@@ -67,12 +67,18 @@ bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
6767
_wire->begin();
6868
}
6969
_lastWrite = 0;
70+
_writeProtectPin = writeProtectPin;
71+
if (_writeProtectPin >= 0)
72+
{
73+
pinMode(_writeProtectPin, OUTPUT);
74+
preventWrite();
75+
}
7076
return isConnected();
7177
}
7278

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

75-
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
81+
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
7682
{
7783
if ((sda < 255) && (scl < 255))
7884
{
@@ -81,17 +87,29 @@ bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
8187
_wire->begin();
8288
}
8389
_lastWrite = 0;
90+
_writeProtectPin = writeProtectPin;
91+
if (_writeProtectPin >= 0)
92+
{
93+
pinMode(_writeProtectPin, OUTPUT);
94+
preventWrite();
95+
}
8496
return isConnected();
8597
}
8698

8799
#endif
88100

89101

90-
bool I2C_eeprom::begin()
102+
bool I2C_eeprom::begin(int8_t writeProtectPin)
91103
{
92104
// if (_wire == 0) Serial.println("zero"); // test #48
93105
_wire->begin();
94106
_lastWrite = 0;
107+
_writeProtectPin = writeProtectPin;
108+
if (_writeProtectPin >= 0)
109+
{
110+
pinMode(_writeProtectPin, OUTPUT);
111+
preventWrite();
112+
}
95113
return isConnected();
96114
}
97115

@@ -399,6 +417,47 @@ uint8_t I2C_eeprom::getExtraWriteCycleTime()
399417
}
400418

401419

420+
//
421+
// WRITEPROTECT
422+
//
423+
bool I2C_eeprom::hasWriteProtectPin()
424+
{
425+
return (_writeProtectPin >= 0);
426+
}
427+
428+
429+
void I2C_eeprom::allowWrite()
430+
{
431+
if (hasWriteProtectPin())
432+
{
433+
digitalWrite(_writeProtectPin, LOW);
434+
}
435+
}
436+
437+
438+
void I2C_eeprom::preventWrite()
439+
{
440+
if (hasWriteProtectPin())
441+
{
442+
digitalWrite(_writeProtectPin, HIGH);
443+
}
444+
}
445+
446+
447+
void I2C_eeprom::setAutoWriteProtect(bool b)
448+
{
449+
if (hasWriteProtectPin())
450+
{
451+
_autoWriteProtect = b;
452+
}
453+
}
454+
455+
456+
bool I2C_eeprom::getAutoWriteProtect()
457+
{
458+
return _autoWriteProtect;
459+
}
460+
402461

403462
////////////////////////////////////////////////////////////////////
404463
//
@@ -457,10 +516,20 @@ void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
457516
int I2C_eeprom::_WriteBlock(const uint16_t memoryAddress, const uint8_t * buffer, const uint8_t length)
458517
{
459518
_waitEEReady();
519+
if (_autoWriteProtect)
520+
{
521+
digitalWrite(_writeProtectPin, LOW);
522+
}
460523

461524
this->_beginTransmission(memoryAddress);
462525
_wire->write(buffer, length);
463526
int rv = _wire->endTransmission();
527+
528+
if (_autoWriteProtect)
529+
{
530+
digitalWrite(_writeProtectPin, HIGH);
531+
}
532+
464533
_lastWrite = micros();
465534

466535
yield(); // For OS scheduling
@@ -529,9 +598,11 @@ void I2C_eeprom::_waitEEReady()
529598
uint32_t waitTime = I2C_WRITEDELAY + _extraTWR * 1000UL;
530599
while ((micros() - _lastWrite) <= waitTime)
531600
{
532-
_wire->beginTransmission(_deviceAddress);
533-
int x = _wire->endTransmission();
534-
if (x == 0) return;
601+
if (isConnected()) return;
602+
// TODO remove pre 1.7.4 code
603+
// _wire->beginTransmission(_deviceAddress);
604+
// int x = _wire->endTransmission();
605+
// if (x == 0) return;
535606
yield(); // For OS scheduling
536607
}
537608
return;

libraries/I2C_EEPROM/I2C_eeprom.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: I2C_eeprom.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 1.7.3
5+
// VERSION: 1.7.4
66
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
77
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
88

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

1313

14-
#define I2C_EEPROM_VERSION (F("1.7.3"))
14+
#define I2C_EEPROM_VERSION (F("1.7.4"))
1515

1616

1717
#define I2C_DEVICESIZE_24LC512 65536
@@ -63,10 +63,10 @@ class I2C_eeprom
6363
// MBED test ==> see #55, #53
6464
#if defined(ESP8266) || defined(ESP32) || (defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__))
6565
// set the I2C pins explicitly (overrule)
66-
bool begin(uint8_t sda, uint8_t scl);
66+
bool begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin = -1);
6767
#endif
6868
// use default I2C pins.
69-
bool begin();
69+
bool begin(int8_t writeProtectPin = -1);
7070
bool isConnected();
7171

7272

@@ -127,13 +127,24 @@ class I2C_eeprom
127127
uint8_t getExtraWriteCycleTime();
128128

129129

130+
// WRITEPROTECT
131+
// works only if WP pin is defined in begin().
132+
// see readme.md
133+
inline bool hasWriteProtectPin();
134+
void allowWrite();
135+
void preventWrite();
136+
void setAutoWriteProtect(bool b);
137+
bool getAutoWriteProtect();
138+
139+
130140
private:
131141
uint8_t _deviceAddress;
132142
uint32_t _lastWrite = 0; // for waitEEReady
133143
uint32_t _deviceSize = 0;
134144
uint8_t _pageSize = 0;
135145
uint8_t _extraTWR = 0; // milliseconds
136146

147+
137148
// 24LC32..24LC512 use two bytes for memory address
138149
// 24LC01..24LC16 use one-byte addresses + part of device address
139150
bool _isAddressSizeTwoWords;
@@ -154,6 +165,9 @@ class I2C_eeprom
154165

155166
bool _debug = false;
156167

168+
int8_t _writeProtectPin = -1;
169+
bool _autoWriteProtect = false;
170+
157171
UNIT_TEST_FRIEND;
158172
};
159173

libraries/I2C_EEPROM/keywords.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ getLastWrite KEYWORD2
3737
setExtraWriteCycleTime KEYWORD2
3838
getExtraWriteCycleTime KEYWORD2
3939

40+
hasWriteProtectPin KEYWORD2
41+
allowWrite KEYWORD2
42+
preventWrite KEYWORD2
43+
setAutoWriteProtect KEYWORD2
44+
getAutoWriteProtect KEYWORD2
45+
46+
4047
# I2C_eeprom_cyclic_store
4148
format KEYWORD2
4249
read KEYWORD2

libraries/I2C_EEPROM/library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
1717
},
18-
"version": "1.7.3",
18+
"version": "1.7.4",
1919
"license": "MIT",
20-
"frameworks": "arduino",
20+
"frameworks": "*",
2121
"platforms": "*",
2222
"headers": "I2C_eeprom.h"
2323
}

libraries/I2C_EEPROM/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=I2C_EEPROM
2-
version=1.7.3
2+
version=1.7.4
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Library for I2C EEPROMS

libraries/I2C_EEPROM/readme.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
[![Arduino CI](https://github.com/RobTillaart/I2C_EEPROM/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
33
[![Arduino-lint](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/arduino-lint.yml)
44
[![JSON check](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/jsoncheck.yml)
5+
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/I2C_EEPROM.svg)](https://github.com/RobTillaart/I2C_EEPROM/issues)
6+
57
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/I2C_EEPROM/blob/master/LICENSE)
68
[![GitHub release](https://img.shields.io/github/release/RobTillaart/I2C_EEPROM.svg?maxAge=3600)](https://github.com/RobTillaart/I2C_EEPROM/releases)
9+
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/I2C_EEPROM.svg)](https://registry.platformio.org/libraries/robtillaart/I2C_EEPROM)
10+
711

812

913
# I2C_EEPROM
@@ -67,13 +71,17 @@ Most important difference is 32 bit memory addresses.
6771
optional Wire interface.
6872
- **I2C_eeprom(uint8_t deviceAddress, uint32_t deviceSize, TwoWire \*wire = &Wire)**
6973
constructor, with optional Wire interface.
70-
- **bool begin()** initializes the I2C bus with the default pins.
74+
- **bool begin(uint8_t writeProtectPin = -1)** initializes the I2C bus with the default pins.
7175
Furthermore it checks if the deviceAddress is available on the I2C bus.
7276
Returns true if deviceAddress is found on the bus, false otherwise.
73-
- **bool begin(uint8_t sda, uint8_t scl)** for ESP32 / ESP8266 / RP2040 and alike.
77+
Optionally one can set the **WP** writeProtect pin. (see section below).
78+
If the **WP** pin is defined the default will be to **not** allow writing.
79+
- **bool begin(uint8_t sda, uint8_t scl, uint8_t writeProtectPin = -1)** for ESP32 / ESP8266 / RP2040 and alike.
7480
Initializes the I2C bus with the specified pins, thereby overruling the default pins.
7581
Furthermore it checks if the deviceAddress is available on the I2C bus.
7682
Returns true if deviceAddress is found on the bus, false otherwise.
83+
Optionally one can set the **WP** writeProtect pin. (see section below).
84+
If the **WP** pin is defined the default will be to **not** allow writing.
7785
- **bool isConnected()** test to see if deviceAddress is found on the bus.
7886

7987

@@ -162,7 +170,7 @@ returns set size == 128, 256, ... 32768, 65536
162170
returns set size == 8, 16, 32, 64, 128.
163171

164172

165-
#### UpdateBlock()
173+
### UpdateBlock()
166174

167175
(new since 1.4.2)
168176

@@ -177,7 +185,7 @@ If data is changed often between writes, **updateBlock()** is slower than **writ
177185
So you should verify if your sketch can make use of the advantages of **updateBlock()**
178186

179187

180-
#### ExtraWriteCycleTime (experimental)
188+
### ExtraWriteCycleTime (experimental)
181189

182190
To improve support older I2C EEPROMs e.g. IS24C16 two functions were
183191
added to increase the waiting time before a read and/or write as some
@@ -191,6 +199,36 @@ Since 1.7.2 it is also possible to adjust the **I2C_WRITEDELAY** in the .h file
191199
or overrule the define on the command line.
192200

193201

202+
### WriteProtectPin WP (experimental)
203+
204+
(since 1.7.4)
205+
206+
The library can control the **WP** = WriteProtect pin of the EEPROM.
207+
To do this one should connect a GPIO pin of the MCU to the **WP** pin of the EEPROM.
208+
Furthermore the **WP** should be defined as a parameter in **begin()**.
209+
If the **WP** pin is defined the default will be to **not** allow writing.
210+
The user has to enable writing either by manual or automatic control.
211+
212+
In the automatic mode the library only allows writing to the EEPROM when it
213+
actually writes to the EEPROM.
214+
So it keeps the EEPROM in a read only mode as much as possible.
215+
This prevents accidental writes due to (noisy) signals on the I2C bus. (#57)
216+
217+
218+
Status
219+
- **bool hasWriteProtectPin()** returns true if **WP** has been set.
220+
221+
Automatic control
222+
- **void setAutoWriteProtect(bool b)** if set to true, the library enables writing
223+
only when the EEPROM is actually written. This setting **overrules** the manual control.
224+
If **setAutoWriteProtect()** is set to false (== default) the manual control is leading.
225+
- **bool getAutoWriteProtect()** get current setting.
226+
227+
Manual control
228+
- **void allowWrite()** allows writing by setting **WP** to LOW.
229+
- **void preventWrite()** disables writing by setting **WP** to HIGH.
230+
231+
194232
## Limitation
195233

196234
The library does not offer multiple EEPROMS as one continuous storage device.
@@ -220,10 +258,20 @@ See examples
220258
- investigate smarter strategy for **updateBlock()**
221259
=> find first and last changed position could possibly result in less writes.
222260
- can **setBlock()** use strategies from **updateBlock()**
223-
- **\_waitEEReady();** can return bool and could use isConnected() internally.
224-
- added value?
261+
262+
263+
#### Wont
264+
225265
- investigate the print interface?
226266
- circular buffer? (see FRAM library)
227267
- dump function?
228268

229-
#### Wont
269+
270+
## Support
271+
272+
If you appreciate my libraries, you can support the development and maintenance.
273+
Improve the quality of the libraries by providing issues and Pull Requests, or
274+
donate through PayPal or GitHub sponsors.
275+
276+
Thank you,
277+

0 commit comments

Comments
 (0)