fix #53 PICO_RP2040 support (#54)

- fix #53 support RP2040 (kudos to jotamachuca)
- move code from .h to .cpp
- make I2C_WRITEDELAY overridable
- minor edits
This commit is contained in:
Rob Tillaart
2023-05-02 13:02:17 +02:00
committed by GitHub
parent d26182c753
commit c4400ea101
6 changed files with 114 additions and 37 deletions
+9 -3
View File
@@ -6,16 +6,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [1.7.2] - 2023-05-02
- fix #53 support RP2040 (kudos to jotamachuca)
- move code from .h to .cpp
- make I2C_WRITEDELAY overridable
- minor edits
## [1.7.1] - 2023-01-12
- add setDeviceSize()
- add setDeviceSize()
- add setPageSize()
- use setDeviceSize() in constructor to force power of 2.
- update unit test
- update GitHub actions
- update license
- update readme.md
- minor edits
- minor edits
## [1.7.0] - 2022-12-02
- fix #48 rewrote constructor.
+57 -11
View File
@@ -1,7 +1,7 @@
//
// FILE: I2C_eeprom.cpp
// AUTHOR: Rob Tillaart
// VERSION: 1.7.1
// VERSION: 1.7.2
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
@@ -53,7 +53,7 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize, T
}
#if defined (ESP8266) || defined(ESP32)
#if defined(ESP8266) || defined(ESP32)
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
{
// if (_wire == 0) Serial.println("zero"); // test #48
@@ -71,6 +71,21 @@ bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
#endif
#if defined(PICO_RP2040)
bool I2C_eeprom::begin(uint8_t sda, uint8_t scl)
{
if ((sda < 255) && (scl < 255))
{
_wire->setSCL(scl);
_wire->setSDA(sda);
_wire->begin();
}
_lastWrite = 0;
return isConnected();
}
#endif
bool I2C_eeprom::begin()
{
// if (_wire == 0) Serial.println("zero"); // test #48
@@ -248,6 +263,7 @@ bool I2C_eeprom::updateBlockVerify(const uint16_t memoryAddress, const uint8_t *
return memcmp(data, buffer, length) == 0;
}
/////////////////////////////////////////////////////////////
//
// METADATA SECTION
@@ -311,6 +327,18 @@ uint32_t I2C_eeprom::determineSize(const bool debug)
}
uint32_t I2C_eeprom::getDeviceSize()
{
return _deviceSize;
}
uint8_t I2C_eeprom::getPageSize()
{
return _pageSize;
}
uint8_t I2C_eeprom::getPageSize(uint32_t deviceSize)
{
// determine page size from device size
@@ -324,6 +352,12 @@ uint8_t I2C_eeprom::getPageSize(uint32_t deviceSize)
}
uint32_t I2C_eeprom::getLastWrite()
{
return _lastWrite;
}
uint32_t I2C_eeprom::setDeviceSize(uint32_t deviceSize)
{
uint32_t size = 128;
@@ -352,6 +386,18 @@ uint8_t I2C_eeprom::setPageSize(uint8_t pageSize)
}
void I2C_eeprom::setExtraWriteCycleTime(uint8_t ms)
{
_extraTWR = ms;
}
uint8_t I2C_eeprom::getExtraWriteCycleTime()
{
return _extraTWR;
}
////////////////////////////////////////////////////////////////////
//
@@ -390,7 +436,7 @@ void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
if (this->_isAddressSizeTwoWords)
{
_wire->beginTransmission(_deviceAddress);
// Address High Byte
// Address High Byte
_wire->write((memoryAddress >> 8));
}
else
@@ -399,7 +445,8 @@ void I2C_eeprom::_beginTransmission(const uint16_t memoryAddress)
_wire->beginTransmission(addr);
}
// Address Low Byte (or single byte for chips 16K or smaller that have one-word addresses)
// Address Low Byte
// (or single byte for chips 16K or smaller that have one-word addresses)
_wire->write((memoryAddress & 0xFF));
}
@@ -449,10 +496,10 @@ uint8_t I2C_eeprom::_ReadBlock(const uint16_t memoryAddress, uint8_t * buffer, c
// Serial.print("\t");
// Serial.println(rv);
// }
return 0; // error
return 0; // error
}
// readBytes will always be equal or smaller to length
// readBytes will always be equal or smaller to length
uint8_t readBytes = 0;
if (this->_isAddressSizeTwoWords)
{
@@ -463,7 +510,7 @@ uint8_t I2C_eeprom::_ReadBlock(const uint16_t memoryAddress, uint8_t * buffer, c
uint8_t addr = _deviceAddress | ((memoryAddress >> 8) & 0x07);
readBytes = _wire->requestFrom(addr, length);
}
yield(); // For OS scheduling
yield(); // For OS scheduling
uint8_t cnt = 0;
while (cnt < readBytes)
{
@@ -475,21 +522,20 @@ uint8_t I2C_eeprom::_ReadBlock(const uint16_t memoryAddress, uint8_t * buffer, c
void I2C_eeprom::_waitEEReady()
{
#define I2C_WRITEDELAY 5000
// Wait until EEPROM gives ACK again.
// this is a bit faster than the hardcoded 5 milliSeconds
// TWR = WriteCycleTime
uint32_t waitTime = I2C_WRITEDELAY + _extraTWR * 1000UL; // do the math once.
uint32_t waitTime = I2C_WRITEDELAY + _extraTWR * 1000UL;
while ((micros() - _lastWrite) <= waitTime)
{
_wire->beginTransmission(_deviceAddress);
int x = _wire->endTransmission();
if (x == 0) return;
yield(); // For OS scheduling
yield(); // For OS scheduling
}
return;
}
// -- END OF FILE --
// -- END OF FILE --
+19 -9
View File
@@ -2,7 +2,7 @@
//
// FILE: I2C_eeprom.h
// AUTHOR: Rob Tillaart
// VERSION: 1.7.1
// VERSION: 1.7.2
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
// URL: https://github.com/RobTillaart/I2C_EEPROM.git
@@ -11,7 +11,7 @@
#include "Wire.h"
#define I2C_EEPROM_VERSION (F("1.7.1"))
#define I2C_EEPROM_VERSION (F("1.7.2"))
#define I2C_DEVICESIZE_24LC512 65536
@@ -26,6 +26,15 @@
#define I2C_DEVICESIZE_24LC01 128
// AT24C32 has a WriteCycle Time of max 20 ms
// so one need to set I2C_WRITEDELAY to 20000.
// can also be done on command line.
// (see private _waitEEReady() function)
#ifndef I2C_WRITEDELAY
#define I2C_WRITEDELAY 5000
#endif
#ifndef UNIT_TEST_FRIEND
#define UNIT_TEST_FRIEND
#endif
@@ -50,7 +59,7 @@ public:
*/
I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
#if defined(ESP8266) || defined(ESP32) || defined(PICO_RP2040)
// set the I2C pins explicitly (overrule)
bool begin(uint8_t sda, uint8_t scl);
#endif
@@ -98,10 +107,11 @@ public:
// Meta data functions
uint32_t determineSize(const bool debug = false);
uint32_t getDeviceSize() { return _deviceSize; };
uint8_t getPageSize() { return _pageSize; };
uint32_t getDeviceSize();
uint8_t getPageSize();
uint8_t getPageSize(uint32_t deviceSize);
uint32_t getLastWrite() { return _lastWrite; };
uint32_t getLastWrite();
// for overruling and debugging.
// forces a power of 2
@@ -111,8 +121,8 @@ public:
// TWR = WriteCycleTime
// 5 ms is minimum, one can add extra ms here to adjust timing of both read() and write()
void setExtraWriteCycleTime(uint8_t ms) { _extraTWR = ms; };
uint8_t getExtraWriteCycleTime() { return _extraTWR; };
void setExtraWriteCycleTime(uint8_t ms);
uint8_t getExtraWriteCycleTime();
private:
@@ -146,5 +156,5 @@ private:
};
// -- END OF FILE --
// -- END OF FILE --
+27 -12
View File
@@ -20,6 +20,11 @@ MicroChip 24LC512, 24LC256, 24LC64, 24LC32, 24LC16, 24LC08, 24LC04, 24LC02, 24LC
The **I2C_eeprom_cyclic_store** interface is documented [here](README_cyclic_store.md)
#### Links
- https://github.com/RobTillaart/I2C_24LC1025
## Schematic
```cpp
@@ -31,12 +36,15 @@ The **I2C_eeprom_cyclic_store** interface is documented [here](README_cyclic_sto
+-------+
default address = 0x50 .. 0x57 depending on three address lines
```
## Interface
```cpp
#include "I2C_eeprom.h"
```
The interface is kept quite identical to the I2C_24LC1025 library.
https://github.com/RobTillaart/I2C_24LC1025
@@ -52,8 +60,8 @@ constructor, with optional Wire interface.
- **bool begin()** initializes the I2C bus with the default pins.
Furthermore it checks if the deviceAddress is available on the I2C bus.
Returns true if deviceAddress is found on the bus, false otherwise.
- **bool begin(uint8_t sda, uint8_t scl)** (ESP32 / ESP8266 only)
initializes the I2C bus with the specified pins, therby overruling the default pins.
- **bool begin(uint8_t sda, uint8_t scl)** for ESP32 / ESP8266 / RP2040 and alike.
Initializes the I2C bus with the specified pins, thereby overruling the default pins.
Furthermore it checks if the deviceAddress is available on the I2C bus.
Returns true if deviceAddress is found on the bus, false otherwise.
- **bool isConnected()** test to see if deviceAddress is found on the bus.
@@ -110,7 +118,8 @@ Same as write and update functions above. Returns true if successful, false indi
- **uint8_t getPageSize(uint32_t deviceSize)** idem
- **uint32_t getLastWrite()** idem
- **uint32_t determineSize(bool debug = false)**
function that determines the size of the EEPROM by detecting when a memory address is folded upon memory address 0.
function that determines the size of the EEPROM by detecting when a memory address
is folded upon memory address 0.
It is based upon the observation that memory wraps around.
The debug flag prints some output to Serial.
@@ -168,10 +177,13 @@ than 5 milliseconds which is the minimum.
- **void setExtraWriteCycleTime(uint8_t ms)** idem
- **uint8_t getExtraWriteCycleTime()** idem
Since 1.7.2 it is also possible to adjust the **I2C_WRITEDELAY** in the .h file
or overrule the define on the command line.
## Limitation
The library does not offer multiple EEPROMS as one continuous storage device.
The library does not offer multiple EEPROMS as one continuous storage device.
## Operation
@@ -180,19 +192,21 @@ See examples
## Future
#### must
#### Must
- improve documentation
#### should
#### Should
- investigate multi-EEPROM storage
- wrapper class?
- improve error handling, write functions should return bytes written or so.
- move code from .h to .cpp?
- improve error handling,
- write functions should return bytes written or so.
- make deviceSize explicit in examples?
- AT24C32 has a WriteCycle Time of max 20 ms
- make a define of the 5000 ?
#### could
#### Could
- investigate smarter strategy for **updateBlock()**
=> find first and last changed position could possibly result in less writes.
- can **setBlock()** use strategies from **updateBlock()**
@@ -202,3 +216,4 @@ See examples
- circular buffer? (see FRAM library)
- dump function?
#### Wont
+1 -1
View File
@@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
},
"version": "1.7.1",
"version": "1.7.2",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
+1 -1
View File
@@ -1,5 +1,5 @@
name=I2C_EEPROM
version=1.7.1
version=1.7.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for I2C EEPROMS