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
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
#define STICKBREAKER V0.2.2
|
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();
|
|
|
|
void begin(int sda=-1, int scl=-1, uint32_t frequency=0);
|
|
|
|
|
|
|
|
void setClock(uint32_t frequency); // change bus clock without initing hardware
|
|
|
|
size_t getClock(); // current bus clock rate in hz
|
|
|
|
|
|
|
|
void setTimeOut(uint16_t timeOutMillis);
|
|
|
|
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);
|
|
|
|
uint8_t endTransmission(uint8_t 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) );
|
|
|
|
|
|
|
|
void dumpInts();
|
|
|
|
void dumpI2C();
|
2016-10-06 13:21:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern TwoWire Wire;
|
|
|
|
|
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
|