Skip to content

Commit 58ca281

Browse files
ladyadaladyada
andcommitted
Fix NVM example on ESP8266
Co-authored-by: Limor Fried <ladyada@users.noreply.github.com>
1 parent f31a85e commit 58ca281

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,20 @@ and voltages above the selected range are rejected without an I2C write.
7979
Saving is never automatic. Do not call it repeatedly or from a loop; the
8080
datasheet does not specify the nonvolatile-memory endurance.
8181

82-
The I2C peripheral must be released before saving and restored afterward:
82+
When the Arduino core supports `Wire.end()`, release the I2C peripheral before
83+
saving. Restore `Wire` afterward on every platform:
8384

8485
```cpp
86+
#ifdef WIRE_HAS_END
8587
Wire.end();
88+
#endif
8689
bool saved = gp8403.saveToNVM(SDA, SCL);
8790
Wire.begin();
8891
```
8992

93+
Some software I2C cores, including ESP8266, do not provide `Wire.end()`.
94+
`saveToNVM()` takes control of SDA and SCL directly on those platforms.
95+
9096
If your application uses custom pins, speed, or other bus settings, restore the
9197
same configuration instead of using the parameterless `Wire.begin()` shown
9298
above.

examples/nvm_persistence/nvm_persistence.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* the NVM write endurance, so save only when the output really must change.
1010
*
1111
* WARNING: NVM programming uses a special open-drain waveform, not a normal
12-
* I2C transaction. Wire must be stopped before saveToNVM() and restarted
13-
* afterward.
12+
* I2C transaction. When the Arduino core supports Wire.end(), Wire must be
13+
* stopped before saveToNVM() and restarted afterward.
1414
*
1515
* WARNING: NVM programming always uses the GP8403's hardcoded 0x58 address.
1616
* It cannot save a device configured at another I2C address.
@@ -60,9 +60,12 @@ void setup() {
6060
waitForCommand('S');
6161

6262
// saveToNVM() temporarily controls SDA and SCL directly as open-drain GPIO.
63-
// Stop Wire first so the I2C peripheral releases those pins. If your project
64-
// uses custom pins or bus settings, restore that same configuration below.
63+
// Stop Wire first when the Arduino core supports it. Some software I2C cores,
64+
// including ESP8266, do not provide Wire.end(). If your project uses custom
65+
// pins or bus settings, restore that same configuration below.
66+
#ifdef WIRE_HAS_END
6567
Wire.end();
68+
#endif
6669
bool saved = gp8403.saveToNVM(SDA, SCL);
6770
Wire.begin();
6871

hw_tests/03_nvm_persistence/03_nvm_persistence.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ char waitForCommand() {
127127
}
128128

129129
bool saveNVM() {
130+
#ifdef WIRE_HAS_END
130131
Wire.end();
132+
#endif
131133
bool saved = gp8403.saveToNVM(SDA, SCL);
132134
Wire.begin();
133135
return saved;

0 commit comments

Comments
 (0)