2016-10-06 13:21:30 +02:00
|
|
|
/*
|
|
|
|
TwoWire.h - TWI/I2C library for Arduino & Wiring
|
|
|
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
|
|
|
Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
|
|
|
|
Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
|
2018-06-27 09:01:06 +02:00
|
|
|
Modified November 2017 by Chuck Todd <stickbreaker on GitHub> to use ISR and increase stability.
|
2016-10-06 13:21:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TwoWire_h
|
|
|
|
#define TwoWire_h
|
|
|
|
|
|
|
|
#include <esp32-hal.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/queue.h"
|
2016-11-17 11:37:23 +01:00
|
|
|
#include "Stream.h"
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2019-01-09 10:07:54 +01:00
|
|
|
#define STICKBREAKER 'V1.1.0'
|
2016-10-06 13:21:30 +02:00
|
|
|
#define I2C_BUFFER_LENGTH 128
|
2018-06-27 09:01:06 +02:00
|
|
|
typedef void(*user_onRequest)(void);
|
|
|
|
typedef void(*user_onReceive)(uint8_t*, int);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2016-11-17 11:37:23 +01:00
|
|
|
class TwoWire: public Stream
|
2016-10-06 13:21:30 +02:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
uint8_t num;
|
|
|
|
int8_t sda;
|
|
|
|
int8_t scl;
|
|
|
|
i2c_t * i2c;
|
|
|
|
|
|
|
|
uint8_t rxBuffer[I2C_BUFFER_LENGTH];
|
|
|
|
uint16_t rxIndex;
|
|
|
|
uint16_t rxLength;
|
2018-06-27 09:01:06 +02:00
|
|
|
uint16_t rxQueued; //@stickBreaker
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
uint8_t txBuffer[I2C_BUFFER_LENGTH];
|
|
|
|
uint16_t txIndex;
|
|
|
|
uint16_t txLength;
|
2018-06-27 09:01:06 +02:00
|
|
|
uint16_t txAddress;
|
|
|
|
uint16_t txQueued; //@stickbreaker
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
uint8_t transmitting;
|
2018-06-27 09:01:06 +02:00
|
|
|
/* slave Mode, not yet Stickbreaker
|
|
|
|
static user_onRequest uReq[2];
|
|
|
|
static user_onReceive uRcv[2];
|
|
|
|
void onRequestService(void);
|
|
|
|
void onReceiveService(uint8_t*, int);
|
|
|
|
*/
|
|
|
|
i2c_err_t last_error; // @stickBreaker from esp32-hal-i2c.h
|
|
|
|
uint16_t _timeOutMillis;
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
TwoWire(uint8_t bus_num);
|
2018-06-27 09:01:06 +02:00
|
|
|
~TwoWire();
|
2020-07-19 01:21:15 +02:00
|
|
|
|
|
|
|
//call setPins() first, so that begin() can be called without arguments from libraries
|
|
|
|
bool setPins(int sda, int scl);
|
|
|
|
|
I2C ReSTART returns Success (#2141)
* Don't Return I2C_ERROR_CONTINUE on ReSTART
ReSTART operations on the ESP32 have to be handled differently than on AVR chips, so ReSTART operations(`Wire.endTransmission(false), Wire.requestFrom(id,size,false);` are queued until a STOP is send (`Wire.endTransmission(TRUE), Wire.endTransmission(), Wire.requestFrom(id,size), Wire.requestFrom(id,size,TRUE)). To indicate the queuing I had used `I2C_ERROR_CONTINUE`, this caused compatibility issues with the existing Arduino I2C Code base. So, back to Lying to the public(for their own good of course) about success! This update just returns `I2C_ERROR_OK` on ReSTART commands.
* add comments
add comments
* Change Return error for ReSTART operation to I2C_ERROR_OK
This change restores compatibility with pre-existing Arduino Libraries. The ReSTART queuing operations are hidden behind the scenes. Wire.endTransmission(id,len,FALSE); will know return I2C_ERROR_OK instead of I2C_ERROR_CONTINUE, Wire.lastError() will return the true condition of I2C_ERROR_CONTINUE.
2018-12-03 16:16:43 +01:00
|
|
|
bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus
|
|
|
|
// calling will attemp to recover hung bus
|
2018-06-27 09:01:06 +02:00
|
|
|
|
|
|
|
void setClock(uint32_t frequency); // change bus clock without initing hardware
|
|
|
|
size_t getClock(); // current bus clock rate in hz
|
|
|
|
|
I2C ReSTART returns Success (#2141)
* Don't Return I2C_ERROR_CONTINUE on ReSTART
ReSTART operations on the ESP32 have to be handled differently than on AVR chips, so ReSTART operations(`Wire.endTransmission(false), Wire.requestFrom(id,size,false);` are queued until a STOP is send (`Wire.endTransmission(TRUE), Wire.endTransmission(), Wire.requestFrom(id,size), Wire.requestFrom(id,size,TRUE)). To indicate the queuing I had used `I2C_ERROR_CONTINUE`, this caused compatibility issues with the existing Arduino I2C Code base. So, back to Lying to the public(for their own good of course) about success! This update just returns `I2C_ERROR_OK` on ReSTART commands.
* add comments
add comments
* Change Return error for ReSTART operation to I2C_ERROR_OK
This change restores compatibility with pre-existing Arduino Libraries. The ReSTART queuing operations are hidden behind the scenes. Wire.endTransmission(id,len,FALSE); will know return I2C_ERROR_OK instead of I2C_ERROR_CONTINUE, Wire.lastError() will return the true condition of I2C_ERROR_CONTINUE.
2018-12-03 16:16:43 +01:00
|
|
|
void setTimeOut(uint16_t timeOutMillis); // default timeout of i2c transactions is 50ms
|
2018-06-27 09:01:06 +02:00
|
|
|
uint16_t getTimeOut();
|
|
|
|
|
|
|
|
uint8_t lastError();
|
|
|
|
char * getErrorText(uint8_t err);
|
|
|
|
|
|
|
|
//@stickBreaker for big blocks and ISR model
|
|
|
|
i2c_err_t writeTransmission(uint16_t address, uint8_t* buff, uint16_t size, bool sendStop=true);
|
|
|
|
i2c_err_t readTransmission(uint16_t address, uint8_t* buff, uint16_t size, bool sendStop=true, uint32_t *readCount=NULL);
|
|
|
|
|
|
|
|
void beginTransmission(uint16_t address);
|
|
|
|
void beginTransmission(uint8_t address);
|
|
|
|
void beginTransmission(int address);
|
|
|
|
|
|
|
|
uint8_t endTransmission(bool sendStop);
|
2016-10-06 13:21:30 +02:00
|
|
|
uint8_t endTransmission(void);
|
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop);
|
|
|
|
uint8_t requestFrom(uint16_t address, uint8_t size, uint8_t sendStop);
|
|
|
|
uint8_t requestFrom(uint16_t address, uint8_t size);
|
|
|
|
uint8_t requestFrom(uint8_t address, uint8_t size, uint8_t sendStop);
|
|
|
|
uint8_t requestFrom(uint8_t address, uint8_t size);
|
|
|
|
uint8_t requestFrom(int address, int size, int sendStop);
|
|
|
|
uint8_t requestFrom(int address, int size);
|
2016-10-06 13:21:30 +02:00
|
|
|
|
|
|
|
size_t write(uint8_t);
|
|
|
|
size_t write(const uint8_t *, size_t);
|
|
|
|
int available(void);
|
|
|
|
int read(void);
|
|
|
|
int peek(void);
|
|
|
|
void flush(void);
|
|
|
|
|
2016-11-17 11:33:42 +01:00
|
|
|
inline size_t write(const char * s)
|
|
|
|
{
|
|
|
|
return write((uint8_t*) s, strlen(s));
|
|
|
|
}
|
2016-10-06 13:21:30 +02:00
|
|
|
inline size_t write(unsigned long n)
|
|
|
|
{
|
|
|
|
return write((uint8_t)n);
|
|
|
|
}
|
|
|
|
inline size_t write(long n)
|
|
|
|
{
|
|
|
|
return write((uint8_t)n);
|
|
|
|
}
|
|
|
|
inline size_t write(unsigned int n)
|
|
|
|
{
|
|
|
|
return write((uint8_t)n);
|
|
|
|
}
|
|
|
|
inline size_t write(int n)
|
|
|
|
{
|
|
|
|
return write((uint8_t)n);
|
|
|
|
}
|
2018-06-27 09:01:06 +02:00
|
|
|
|
|
|
|
void onReceive( void (*)(int) );
|
|
|
|
void onRequest( void (*)(void) );
|
|
|
|
|
Wire ReSTART fix, with others (#1717)
* ReSTART fix, Sequencing fix
pr #1665 introduce a problem with ReSTART, when solving this problem I found an interaction between the TxFifo refill, RxFifo empty and CMD[] fill. during certain sequences a dataqueue command would be skipped, this skipping resulted in a mismatch between the contents of the TxFifo and the i2c command sequence. The problem manifested as an ACK error.
In addition to this required bug fix I propose:
* `Wire.begin()` be changed from a `void` to a `bool` this will allow the reset functionality of `Wire.begin()` to be reported. Currently `Wire.begin()` attempts to reset the i2c Peripheral, but cannot report success/failure.
* `Wire.busy()` be added. this `bool` function returns the hardware status of the bus. This status can be use in multi-master environments for application level interleaving of commands, also in single master environment, it can be used to detect a 'hung' bus. With the functional change to `Wire.begin()` this allows app level recover of a hung bus.
* `Wire.lastError()` value updated for all errors, previously when interleaving `Wire.endTransmission(false)` and `Wire.readTransmission(false)`, the 128 byte `Wire.write()` buffer was exhausted without generating and error(very exotic). I discovered this error when I created a sequence of directed reads to a EEPROM. Each directed read used 2 bytes of the 128 byte `write()` buffer, so after 64 consecutive ReSTART writes with ReSTART reads, `Wire()` had no room to record the directed address bytes. It generated just a NAK check without setting the EEPROMs internal register address. The succeeding ReSTART read succeeded at incorrect address.
* Changes to the HAL layer:
** added `i2cGetStatus()` which returns the i2c peripheral status word, used to detect bus_busy currently
** added `i2cDebug()` programmatic control of debug buffer output
** changed `i2cAddQueue()` to allow data_only queue element this will allow a i2c transaction to use multiple data pointers.
** removed direct access to DumpInts(), DumpI2c() from app, use i2cDebug() to set trigger points
*
* Update esp32-hal-i2c.c
* Update Wire.cpp
* ReSTART, Sequencing
pr #1665 introduce a problem with ReSTART, when solving this problem I found an interaction between the TxFifo refill, RxFifo empty and CMD[] fill. during certain sequences a dataqueue command would be skipped, this skipping resulted in a mismatch between the contents of the TxFifo and the i2c command sequence. The problem manifested as an ACK error.
In addition to this required bug fix I propose:
* `Wire.begin()` be changed from a `void` to a `bool` this will allow the reset functionality of `Wire.begin()` to be reported. Currently `Wire.begin()` attempts to reset the i2c Peripheral, but cannot report success/failure.
* `Wire.busy()` be added. this `bool` function returns the hardware status of the bus. This status can be use in multi-master environments for application level interleaving of commands, also in single master environment, it can be used to detect a 'hung' bus. With the functional change to `Wire.begin()` this allows app level recover of a hung bus.
* `Wire.lastError()` value updated for all errors, previously when interleaving `Wire.endTransmission(false)` and `Wire.readTransmission(false)`, the 128 byte `Wire.write()` buffer was exhausted without generating and error(very exotic). I discovered this error when I created a sequence of directed reads to a EEPROM. Each directed read used 2 bytes of the 128 byte `write()` buffer, so after 64 consecutive ReSTART writes with ReSTART reads, `Wire()` had no room to record the directed address bytes. It generated just a NAK check without setting the EEPROMs internal register address. The succeeding ReSTART read succeeded at incorrect address.
* Changes to the HAL layer:
** added `i2cGetStatus()` which returns the i2c peripheral status word, used to detect bus_busy currently
** added `i2cDebug()` programmatic control of debug buffer output
** changed `i2cAddQueue()` to allow data_only queue element this will allow a i2c transaction to use multiple data pointers.
** removed direct access to DumpInts(), DumpI2c() from app, use i2cDebug() to set trigger points
*
* Forgot DebugFlags Return
@andriyadi found this, total brain fade on my part.
2018-08-14 11:51:15 +02:00
|
|
|
uint32_t setDebugFlags( uint32_t setBits, uint32_t resetBits);
|
|
|
|
bool busy();
|
2016-10-06 13:21:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern TwoWire Wire;
|
2018-07-03 20:41:03 +02:00
|
|
|
extern TwoWire Wire1;
|
2016-10-06 13:21:30 +02:00
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
|
|
|
|
/*
|
2019-01-09 10:07:54 +01:00
|
|
|
V1.1.0 08JAN2019 Support CPU Clock frequency changes
|
I2C ReSTART returns Success (#2141)
* Don't Return I2C_ERROR_CONTINUE on ReSTART
ReSTART operations on the ESP32 have to be handled differently than on AVR chips, so ReSTART operations(`Wire.endTransmission(false), Wire.requestFrom(id,size,false);` are queued until a STOP is send (`Wire.endTransmission(TRUE), Wire.endTransmission(), Wire.requestFrom(id,size), Wire.requestFrom(id,size,TRUE)). To indicate the queuing I had used `I2C_ERROR_CONTINUE`, this caused compatibility issues with the existing Arduino I2C Code base. So, back to Lying to the public(for their own good of course) about success! This update just returns `I2C_ERROR_OK` on ReSTART commands.
* add comments
add comments
* Change Return error for ReSTART operation to I2C_ERROR_OK
This change restores compatibility with pre-existing Arduino Libraries. The ReSTART queuing operations are hidden behind the scenes. Wire.endTransmission(id,len,FALSE); will know return I2C_ERROR_OK instead of I2C_ERROR_CONTINUE, Wire.lastError() will return the true condition of I2C_ERROR_CONTINUE.
2018-12-03 16:16:43 +01:00
|
|
|
V1.0.2 30NOV2018 stop returning I2C_ERROR_CONTINUE on ReSTART operations, regain compatibility with Arduino libs
|
Wire ReSTART fix, with others (#1717)
* ReSTART fix, Sequencing fix
pr #1665 introduce a problem with ReSTART, when solving this problem I found an interaction between the TxFifo refill, RxFifo empty and CMD[] fill. during certain sequences a dataqueue command would be skipped, this skipping resulted in a mismatch between the contents of the TxFifo and the i2c command sequence. The problem manifested as an ACK error.
In addition to this required bug fix I propose:
* `Wire.begin()` be changed from a `void` to a `bool` this will allow the reset functionality of `Wire.begin()` to be reported. Currently `Wire.begin()` attempts to reset the i2c Peripheral, but cannot report success/failure.
* `Wire.busy()` be added. this `bool` function returns the hardware status of the bus. This status can be use in multi-master environments for application level interleaving of commands, also in single master environment, it can be used to detect a 'hung' bus. With the functional change to `Wire.begin()` this allows app level recover of a hung bus.
* `Wire.lastError()` value updated for all errors, previously when interleaving `Wire.endTransmission(false)` and `Wire.readTransmission(false)`, the 128 byte `Wire.write()` buffer was exhausted without generating and error(very exotic). I discovered this error when I created a sequence of directed reads to a EEPROM. Each directed read used 2 bytes of the 128 byte `write()` buffer, so after 64 consecutive ReSTART writes with ReSTART reads, `Wire()` had no room to record the directed address bytes. It generated just a NAK check without setting the EEPROMs internal register address. The succeeding ReSTART read succeeded at incorrect address.
* Changes to the HAL layer:
** added `i2cGetStatus()` which returns the i2c peripheral status word, used to detect bus_busy currently
** added `i2cDebug()` programmatic control of debug buffer output
** changed `i2cAddQueue()` to allow data_only queue element this will allow a i2c transaction to use multiple data pointers.
** removed direct access to DumpInts(), DumpI2c() from app, use i2cDebug() to set trigger points
*
* Update esp32-hal-i2c.c
* Update Wire.cpp
* ReSTART, Sequencing
pr #1665 introduce a problem with ReSTART, when solving this problem I found an interaction between the TxFifo refill, RxFifo empty and CMD[] fill. during certain sequences a dataqueue command would be skipped, this skipping resulted in a mismatch between the contents of the TxFifo and the i2c command sequence. The problem manifested as an ACK error.
In addition to this required bug fix I propose:
* `Wire.begin()` be changed from a `void` to a `bool` this will allow the reset functionality of `Wire.begin()` to be reported. Currently `Wire.begin()` attempts to reset the i2c Peripheral, but cannot report success/failure.
* `Wire.busy()` be added. this `bool` function returns the hardware status of the bus. This status can be use in multi-master environments for application level interleaving of commands, also in single master environment, it can be used to detect a 'hung' bus. With the functional change to `Wire.begin()` this allows app level recover of a hung bus.
* `Wire.lastError()` value updated for all errors, previously when interleaving `Wire.endTransmission(false)` and `Wire.readTransmission(false)`, the 128 byte `Wire.write()` buffer was exhausted without generating and error(very exotic). I discovered this error when I created a sequence of directed reads to a EEPROM. Each directed read used 2 bytes of the 128 byte `write()` buffer, so after 64 consecutive ReSTART writes with ReSTART reads, `Wire()` had no room to record the directed address bytes. It generated just a NAK check without setting the EEPROMs internal register address. The succeeding ReSTART read succeeded at incorrect address.
* Changes to the HAL layer:
** added `i2cGetStatus()` which returns the i2c peripheral status word, used to detect bus_busy currently
** added `i2cDebug()` programmatic control of debug buffer output
** changed `i2cAddQueue()` to allow data_only queue element this will allow a i2c transaction to use multiple data pointers.
** removed direct access to DumpInts(), DumpI2c() from app, use i2cDebug() to set trigger points
*
* Forgot DebugFlags Return
@andriyadi found this, total brain fade on my part.
2018-08-14 11:51:15 +02:00
|
|
|
V1.0.1 02AUG2018 First Fix after release, Correct ReSTART handling, change Debug control, change begin()
|
|
|
|
to a function, this allow reporting if bus cannot be initialized, Wire.begin() can be used to recover
|
|
|
|
a hung bus busy condition.
|
2018-06-27 09:01:06 +02:00
|
|
|
V0.2.2 13APR2018 preserve custom SCL,SDA,Frequency when no parameters passed to begin()
|
|
|
|
V0.2.1 15MAR2018 Hardware reset, Glitch prevention, adding destructor for second i2c testing
|
|
|
|
*/
|
2016-10-06 13:21:30 +02:00
|
|
|
#endif
|