From efb7e89e734a84aee83a82be1133f7472b5214a8 Mon Sep 17 00:00:00 2001 From: Bernhard Nebel Date: Sat, 16 Dec 2023 18:06:41 +0100 Subject: [PATCH] V1.1.2 --- .github/workflows/LibraryBuild.yml | 7 +++ changelog.txt | 8 ++++ library.properties | 2 +- src/FlexWire.cpp | 74 ++++++++++++++++++++---------- src/FlexWire.h | 4 +- 5 files changed, 69 insertions(+), 26 deletions(-) diff --git a/.github/workflows/LibraryBuild.yml b/.github/workflows/LibraryBuild.yml index e641d03..1b185ce 100644 --- a/.github/workflows/LibraryBuild.yml +++ b/.github/workflows/LibraryBuild.yml @@ -38,10 +38,17 @@ jobs: arduino-boards-fqbn: - arduino:avr:uno - arduino:avr:leonardo + - arduino:sam:arduino_due_x_dbg + - arduino:samd:arduino_zero_edbg + - rp2040:rp2040:rpipico # Choose the right platform for the boards we want to test. (maybe in the future Arduino will automatically do this for you) # With sketches-exclude you may exclude specific examples for a board. Use a comma separated list. ############################################################################################################# + include: + - arduino-boards-fqbn: rp2040:rp2040:rpipico + - platform-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json + # Do not cancel all jobs / architectures if one job fails fail-fast: false diff --git a/changelog.txt b/changelog.txt index ae332a1..2189629 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,12 @@ CHANGELOG for FlexWire +V1.1.2 (16.12.2023) + - added state vars for sda and scl in order to speed up and to avoid + glitches when running on RP2040. + - tested on Zero, Due, and RP2040; for the latter one should use the + Arduino core by earlephilhower; the official core is very slow + when it comes to digital I/O. + - added the above to the LibraryBuild file + V1.1.1 (15.12.2023) - Fixed: setClock fixed so that the specified value is always an upper bound. diff --git a/library.properties b/library.properties index 6a0bc4c..1e5b606 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=FlexWire -version=1.1.1 +version=1.1.2 author=Bernhard Nebel maintainer=Bernhard Nebel sentence=This library implements the master side of the I2C protocol in a platform independent way. diff --git a/src/FlexWire.cpp b/src/FlexWire.cpp index caa6322..0ce5637 100644 --- a/src/FlexWire.cpp +++ b/src/FlexWire.cpp @@ -43,7 +43,9 @@ void FlexWire::setClock(uint32_t Hz) { void FlexWire::setPins(uint8_t sda, uint8_t scl) { _sda = sda; + _sdastate = -1; _scl = scl; + _sclstate = -1; #if AVR_OPTIMIZATION uint8_t port; @@ -171,8 +173,10 @@ void FlexWire::flush(void) { bool FlexWire::i2c_init(void) { pinMode(_sda, INPUT); digitalWrite(_sda, LOW); + _sdastate = 1; pinMode(_scl, INPUT); digitalWrite(_scl, LOW); + _sclstate = 1; delayMicroseconds(_i2cDelay); setSclHigh(); delayMicroseconds(_i2cDelay); @@ -263,36 +267,46 @@ uint8_t FlexWire::i2c_read(bool last) { #if AVR_OPTIMIZATION inline void FlexWire::setSdaLow(void) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) - { - *_sdaPortReg &= ~_sdaBitMask; - *_sdaDirReg |= _sdaBitMask; - + if (_sdastate != 0) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + *_sdaPortReg &= ~_sdaBitMask; + *_sdaDirReg |= _sdaBitMask; + _sdastate = 0; + } } } void FlexWire::setSdaHigh(void) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) - { - *_sdaDirReg &= ~_sdaBitMask; - if(_pullup) *_sdaPortReg |= _sdaBitMask; - + if (_sdastate != 1) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + *_sdaDirReg &= ~_sdaBitMask; + if(_pullup) *_sdaPortReg |= _sdaBitMask; + _sdastate = 1; + } } } void FlexWire::setSclLow(void) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) - { - *_sclPortReg &= ~_sclBitMask; - *_sclDirReg |= _sclBitMask; + if (_sclstate != 0) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + *_sclPortReg &= ~_sclBitMask; + *_sclDirReg |= _sclBitMask; + _sclstate = 0; + } } } void FlexWire::setSclHigh(void) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) - { - *_sclDirReg &= ~_sclBitMask; - if(_pullup) { *_sclPortReg |= _sclBitMask; } + if (_sclstate != 1) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + *_sclDirReg &= ~_sclBitMask; + if(_pullup) { *_sclPortReg |= _sclBitMask; } + _sclstate = 1; + } } } @@ -312,17 +326,23 @@ void FlexWire::setSdaLow(void) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) #endif { - if (_pullup) - digitalWrite(_sda, LOW); - pinMode(_sda, OUTPUT); + if (_sdastate != 0) { + if (_pullup) + digitalWrite(_sda, LOW); + pinMode(_sda, OUTPUT); + _sdastate = 0; + } } } void FlexWire::setSdaHigh(void) { + if (_sdastate != 1) { if (_pullup) pinMode(_sda, INPUT_PULLUP); else pinMode(_sda, INPUT); + _sdastate = 1; + } } void FlexWire::setSclLow(void) { @@ -330,17 +350,23 @@ void FlexWire::setSclLow(void) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) #endif { - if (_pullup) - digitalWrite(_scl, LOW); - pinMode(_scl, OUTPUT); + if (_sclstate != 0) { + if (_pullup) + digitalWrite(_scl, LOW); + pinMode(_scl, OUTPUT); + _sclstate = 0; + } } } void FlexWire::setSclHigh(void) { + if (_sclstate != 1) { if (_pullup) pinMode(_scl, INPUT_PULLUP); else pinMode(_scl, INPUT); + _sclstate = 1; + } } uint8_t FlexWire::getSda(void) { diff --git a/src/FlexWire.h b/src/FlexWire.h index 95da85c..37c837c 100644 --- a/src/FlexWire.h +++ b/src/FlexWire.h @@ -2,7 +2,7 @@ #ifndef FLEXWIRE_h #define FLEXWIRE_h -#define FLEXWIRE_VERSION 1.1.1 +#define FLEXWIRE_VERSION 1.1.2 // #define AVR_OPTIMIZATION 0 // without optimizations, less code, but much slower (55 kHz) @@ -36,6 +36,8 @@ class FlexWire { uint8_t _scl; bool _pullup; uint16_t _i2cDelay; + int8_t _sdastate; + int8_t _sclstate; #if AVR_OPTIMIZATION uint8_t _sdaBitMask; uint8_t _sclBitMask;