2016-10-06 14:21:30 +03: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.
|
2021-10-01 17:34:20 +03:00
|
|
|
Modified Nov 2021 by Hristo Gochkov <Me-No-Dev> to support ESP-IDF API
|
2016-10-06 14:21:30 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TwoWire_h
|
|
|
|
#define TwoWire_h
|
|
|
|
|
|
|
|
#include <esp32-hal.h>
|
2021-10-01 17:34:20 +03:00
|
|
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
2016-10-06 14:21:30 +03:00
|
|
|
#include "freertos/FreeRTOS.h"
|
2021-10-01 17:34:20 +03:00
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#endif
|
2016-11-17 12:37:23 +02:00
|
|
|
#include "Stream.h"
|
2016-10-06 14:21:30 +03:00
|
|
|
|
2021-06-10 12:15:35 +02:00
|
|
|
#ifndef I2C_BUFFER_LENGTH
|
|
|
|
#define I2C_BUFFER_LENGTH 128
|
|
|
|
#endif
|
2018-06-27 09:01:06 +02:00
|
|
|
typedef void(*user_onRequest)(void);
|
|
|
|
typedef void(*user_onReceive)(uint8_t*, int);
|
2016-10-06 14:21:30 +03:00
|
|
|
|
2016-11-17 12:37:23 +02:00
|
|
|
class TwoWire: public Stream
|
2016-10-06 14:21:30 +03:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
uint8_t num;
|
|
|
|
int8_t sda;
|
|
|
|
int8_t scl;
|
|
|
|
|
|
|
|
uint8_t rxBuffer[I2C_BUFFER_LENGTH];
|
2021-10-01 17:34:20 +03:00
|
|
|
size_t rxIndex;
|
|
|
|
size_t rxLength;
|
2016-10-06 14:21:30 +03:00
|
|
|
|
|
|
|
uint8_t txBuffer[I2C_BUFFER_LENGTH];
|
2021-10-01 17:34:20 +03:00
|
|
|
size_t txLength;
|
2018-06-27 09:01:06 +02:00
|
|
|
uint16_t txAddress;
|
2021-10-01 17:34:20 +03:00
|
|
|
|
|
|
|
uint32_t _timeOutMillis;
|
|
|
|
bool nonStop;
|
|
|
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
|
|
|
TaskHandle_t nonStopTask;
|
|
|
|
SemaphoreHandle_t lock;
|
|
|
|
#endif
|
2021-10-09 14:30:20 +03:00
|
|
|
private:
|
|
|
|
bool is_slave;
|
|
|
|
void (*user_onRequest)(void);
|
|
|
|
void (*user_onReceive)(int);
|
|
|
|
static void onRequestService(uint8_t, void *);
|
|
|
|
static void onReceiveService(uint8_t, uint8_t*, size_t, bool, void *);
|
2021-10-11 14:46:31 +03:00
|
|
|
bool initPins(int sdaPin, int sclPin);
|
2016-10-06 14:21:30 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
TwoWire(uint8_t bus_num);
|
2018-06-27 09:01:06 +02:00
|
|
|
~TwoWire();
|
2020-07-19 02:21:15 +03: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 08:16:43 -07:00
|
|
|
bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus
|
2021-10-09 14:30:20 +03:00
|
|
|
bool begin(uint8_t slaveAddr, int sda=-1, int scl=-1, uint32_t frequency=0);
|
2021-10-01 17:34:20 +03:00
|
|
|
bool end();
|
2018-06-27 09:01:06 +02:00
|
|
|
|
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 08:16:43 -07:00
|
|
|
void setTimeOut(uint16_t timeOutMillis); // default timeout of i2c transactions is 50ms
|
2018-06-27 09:01:06 +02:00
|
|
|
uint16_t getTimeOut();
|
|
|
|
|
2021-10-01 17:34:20 +03:00
|
|
|
bool setClock(uint32_t);
|
|
|
|
uint32_t getClock();
|
2018-06-27 09:01:06 +02:00
|
|
|
|
|
|
|
void beginTransmission(uint16_t address);
|
|
|
|
void beginTransmission(uint8_t address);
|
|
|
|
void beginTransmission(int address);
|
|
|
|
|
|
|
|
uint8_t endTransmission(bool sendStop);
|
2016-10-06 14:21:30 +03: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 14:21:30 +03: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 19:33:42 +09:00
|
|
|
inline size_t write(const char * s)
|
|
|
|
{
|
|
|
|
return write((uint8_t*) s, strlen(s));
|
|
|
|
}
|
2016-10-06 14:21:30 +03: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) );
|
2021-10-09 14:30:20 +03:00
|
|
|
size_t slaveWrite(const uint8_t *, size_t);
|
2016-10-06 14:21:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
extern TwoWire Wire;
|
2018-07-03 20:41:03 +02:00
|
|
|
extern TwoWire Wire1;
|
2016-10-06 14:21:30 +03:00
|
|
|
|
|
|
|
#endif
|