Skip to content

Commit

Permalink
V1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
felias-fogg committed Dec 16, 2023
1 parent 3908104 commit efb7e89
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/LibraryBuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=FlexWire
version=1.1.1
version=1.1.2
author=Bernhard Nebel
maintainer=Bernhard Nebel <[email protected]>
sentence=This library implements the master side of the I2C protocol in a platform independent way.
Expand Down
74 changes: 50 additions & 24 deletions src/FlexWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -312,35 +326,47 @@ 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) {
#ifdef ATOMIC_BLOCK
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) {
Expand Down
4 changes: 3 additions & 1 deletion src/FlexWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit efb7e89

Please sign in to comment.