From 5ec2a523d43f13dd47ffc216015f90cc1373cd41 Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 3 Jun 2016 20:49:01 +0200 Subject: [PATCH 1/4] fix and optimize signals flow --- SoftI2CMaster.cpp | 54 +++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) mode change 100644 => 100755 SoftI2CMaster.cpp diff --git a/SoftI2CMaster.cpp b/SoftI2CMaster.cpp old mode 100644 new mode 100755 index f836696..0e2dbc4 --- a/SoftI2CMaster.cpp +++ b/SoftI2CMaster.cpp @@ -11,18 +11,15 @@ * */ -#if (ARDUINO >= 100) + #include -#else -#include -#endif #include "SoftI2CMaster.h" #include #include -#define i2cbitdelay 50 +#define i2cbitdelay 100 #define I2C_ACK 1 #define I2C_NAK 0 @@ -75,6 +72,11 @@ SoftI2CMaster::SoftI2CMaster(uint8_t sclPin, uint8_t sdaPin, uint8_t pullups) i2c_init(); } +bool SoftI2CMaster::available() +{ + return true; +} + // // Turn Arduino pin numbers into PORTx, DDRx, and PINx // @@ -106,7 +108,7 @@ void SoftI2CMaster::setPins(uint8_t sclPin, uint8_t sdaPin, uint8_t pullups) uint8_t SoftI2CMaster::beginTransmission(uint8_t address) { i2c_start(); - uint8_t rc = i2c_write((address<<1) | 0); // clr read bit + uint8_t rc = i2c_write((address << 1) | 0); // clr read bit return rc; } @@ -114,7 +116,7 @@ uint8_t SoftI2CMaster::beginTransmission(uint8_t address) uint8_t SoftI2CMaster::requestFrom(uint8_t address) { i2c_start(); - uint8_t rc = i2c_write((address<<1) | 1); // set read bit + uint8_t rc = i2c_write((address << 1) | 1); // set read bit return rc; } // @@ -123,6 +125,11 @@ uint8_t SoftI2CMaster::requestFrom(int address) return requestFrom( (uint8_t) address); } +uint8_t SoftI2CMaster::requestFrom(uint8_t address, uint8_t l) +{ + return requestFrom( (uint8_t) address); +} + // uint8_t SoftI2CMaster::beginTransmission(int address) { @@ -144,33 +151,36 @@ uint8_t SoftI2CMaster::endTransmission(void) // or after beginTransmission(address) uint8_t SoftI2CMaster::write(uint8_t data) { - return i2c_write(data); + i2c_write(data); + return 1; } // must be called in: // slave tx event callback // or after beginTransmission(address) -void SoftI2CMaster::write(uint8_t* data, uint8_t quantity) +uint8_t SoftI2CMaster::write(uint8_t* data, uint8_t quantity) { + int c = 0; for(uint8_t i = 0; i < quantity; ++i){ - write(data[i]); + c+=write(data[i]); } + return c; } // must be called in: // slave tx event callback // or after beginTransmission(address) -void SoftI2CMaster::write(char* data) +uint8_t SoftI2CMaster::write(char* data) { - write((uint8_t*)data, strlen(data)); + return write((uint8_t*)data, strlen(data)); } // must be called in: // slave tx event callback // or after beginTransmission(address) -void SoftI2CMaster::write(int data) +uint8_t SoftI2CMaster::write(int data) { - write((uint8_t)data); + return write((uint8_t)data); } //-------------------------------------------------------------------- @@ -183,6 +193,7 @@ void SoftI2CMaster::i2c_writebit( uint8_t c ) } else { i2c_sda_lo(); } + _delay_us(i2cbitdelay); i2c_scl_hi(); _delay_us(i2cbitdelay); @@ -190,10 +201,10 @@ void SoftI2CMaster::i2c_writebit( uint8_t c ) i2c_scl_lo(); _delay_us(i2cbitdelay); - if ( c > 0 ) { - i2c_sda_lo(); - } - _delay_us(i2cbitdelay); + //if ( c > 0 ) { + // i2c_sda_lo(); + //} + //_delay_us(i2cbitdelay); } // @@ -205,6 +216,8 @@ uint8_t SoftI2CMaster::i2c_readbit(void) uint8_t port = digitalPinToPort(_sdaPin); volatile uint8_t* pinReg = portInputRegister(port); + i2c_sda_release(); // normalement on l'a deja! + uint8_t c = *pinReg; // I2C_PIN; i2c_scl_lo(); @@ -242,6 +255,8 @@ void SoftI2CMaster::i2c_start(void) i2c_scl_lo(); _delay_us(i2cbitdelay); + + } void SoftI2CMaster::i2c_repstart(void) @@ -269,6 +284,9 @@ void SoftI2CMaster::i2c_repstart(void) // void SoftI2CMaster::i2c_stop(void) { + i2c_sda_lo(); + _delay_us(i2cbitdelay); + i2c_scl_hi(); _delay_us(i2cbitdelay); From a4a996029632918aa00dcc7ac3a48008c5c438a8 Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 3 Jun 2016 20:54:21 +0200 Subject: [PATCH 2/4] fix and optimize signals flow --- SoftI2CMaster.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) mode change 100644 => 100755 SoftI2CMaster.h diff --git a/SoftI2CMaster.h b/SoftI2CMaster.h old mode 100644 new mode 100755 index 260ce90..a60109c --- a/SoftI2CMaster.h +++ b/SoftI2CMaster.h @@ -53,16 +53,19 @@ class SoftI2CMaster uint8_t beginTransmission(int address); uint8_t endTransmission(void); uint8_t write(uint8_t); - void write(uint8_t*, uint8_t); - void write(int); - void write(char*); + uint8_t write(uint8_t*, uint8_t); + uint8_t write(int); + uint8_t write(char*); void begin(void) {return;}; uint8_t requestFrom(int address); uint8_t requestFrom(uint8_t address); + uint8_t requestFrom(uint8_t address, uint8_t l); uint8_t read( uint8_t ack ); uint8_t read(); uint8_t readLast(); + bool available(); + }; #endif From 5b640cf9fbee2759c159f0d78325b5d1c5ad8f01 Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 3 Jun 2016 21:07:48 +0200 Subject: [PATCH 3/4] cleaning --- LICENSE.txt | 0 README.TXT | 0 SoftI2CMaster.cpp | 8 ++------ examples/BlinkMSoftI2CDemo/BlinkMSoftI2CDemo.ino | 0 examples/BlinkMSoftI2CDemo/BlinkM_funcs_soft.h | 0 .../MMA8452Q_Example_SoftI2C/MMA8452Q_Example_SoftI2C.ino | 0 6 files changed, 2 insertions(+), 6 deletions(-) mode change 100644 => 100755 LICENSE.txt mode change 100644 => 100755 README.TXT mode change 100644 => 100755 examples/BlinkMSoftI2CDemo/BlinkMSoftI2CDemo.ino mode change 100644 => 100755 examples/BlinkMSoftI2CDemo/BlinkM_funcs_soft.h mode change 100644 => 100755 examples/MMA8452Q_Example_SoftI2C/MMA8452Q_Example_SoftI2C.ino diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/README.TXT b/README.TXT old mode 100644 new mode 100755 diff --git a/SoftI2CMaster.cpp b/SoftI2CMaster.cpp index 0e2dbc4..cd62b34 100755 --- a/SoftI2CMaster.cpp +++ b/SoftI2CMaster.cpp @@ -201,22 +201,18 @@ void SoftI2CMaster::i2c_writebit( uint8_t c ) i2c_scl_lo(); _delay_us(i2cbitdelay); - //if ( c > 0 ) { - // i2c_sda_lo(); - //} - //_delay_us(i2cbitdelay); } // uint8_t SoftI2CMaster::i2c_readbit(void) { - i2c_sda_hi(); + i2c_sda_hi(); // also set sda as an input i2c_scl_hi(); _delay_us(i2cbitdelay); uint8_t port = digitalPinToPort(_sdaPin); volatile uint8_t* pinReg = portInputRegister(port); - i2c_sda_release(); // normalement on l'a deja! + //i2c_sda_release(); // normalement on l'a deja! uint8_t c = *pinReg; // I2C_PIN; diff --git a/examples/BlinkMSoftI2CDemo/BlinkMSoftI2CDemo.ino b/examples/BlinkMSoftI2CDemo/BlinkMSoftI2CDemo.ino old mode 100644 new mode 100755 diff --git a/examples/BlinkMSoftI2CDemo/BlinkM_funcs_soft.h b/examples/BlinkMSoftI2CDemo/BlinkM_funcs_soft.h old mode 100644 new mode 100755 diff --git a/examples/MMA8452Q_Example_SoftI2C/MMA8452Q_Example_SoftI2C.ino b/examples/MMA8452Q_Example_SoftI2C/MMA8452Q_Example_SoftI2C.ino old mode 100644 new mode 100755 From 98c8d6cea76488db677b4d8465fae180ec61cb39 Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 3 Jun 2016 23:36:20 +0200 Subject: [PATCH 4/4] ??? --- SoftI2CMaster.cpp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/SoftI2CMaster.cpp b/SoftI2CMaster.cpp index cd62b34..8163825 100755 --- a/SoftI2CMaster.cpp +++ b/SoftI2CMaster.cpp @@ -19,7 +19,7 @@ #include #include -#define i2cbitdelay 100 +#define i2cbitdelay 150 #define I2C_ACK 1 #define I2C_NAK 0 @@ -183,6 +183,24 @@ uint8_t SoftI2CMaster::write(int data) return write((uint8_t)data); } + +// FIXME: this isn't right, surely +uint8_t SoftI2CMaster::read( uint8_t ack ) +{ + return i2c_read( ack ); +} + +// +uint8_t SoftI2CMaster::read() +{ + return i2c_read( I2C_ACK ); +} + +// +uint8_t SoftI2CMaster::readLast() +{ + return i2c_read( I2C_NAK ); +} //-------------------------------------------------------------------- @@ -323,20 +341,4 @@ uint8_t SoftI2CMaster::i2c_read( uint8_t ack ) return res; } -// FIXME: this isn't right, surely -uint8_t SoftI2CMaster::read( uint8_t ack ) -{ - return i2c_read( ack ); -} -// -uint8_t SoftI2CMaster::read() -{ - return i2c_read( I2C_ACK ); -} - -// -uint8_t SoftI2CMaster::readLast() -{ - return i2c_read( I2C_NAK ); -}