Wire.h
  1  /*
  2    TwoWire.h - TWI/I2C library for Arduino & Wiring
  3    Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  4  
  5    This library is free software; you can redistribute it and/or
  6    modify it under the terms of the GNU Lesser General Public
  7    License as published by the Free Software Foundation; either
  8    version 2.1 of the License, or (at your option) any later version.
  9  
 10    This library is distributed in the hope that it will be useful,
 11    but WITHOUT ANY WARRANTY; without even the implied warranty of
 12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13    Lesser General Public License for more details.
 14  
 15    You should have received a copy of the GNU Lesser General Public
 16    License along with this library; if not, write to the Free Software
 17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 18  
 19    Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
 20    Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
 21    Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
 22    Modified November 2017 by Chuck Todd <stickbreaker on GitHub> to use ISR and increase stability.
 23  */
 24  
 25  #ifndef TwoWire_h
 26  #define TwoWire_h
 27  
 28  #include "esp32-hal-i2c.h"
 29  #include "freertos/FreeRTOS.h"
 30  #include "freertos/queue.h"
 31  
 32  
 33  
 34  #define SDA (gpio_num_t)4
 35  #define SCL (gpio_num_t)5
 36  
 37  
 38  #define STICKBREAKER V1.0.1
 39  #define I2C_BUFFER_LENGTH 128
 40  typedef void(*user_onRequest)(void);
 41  typedef void(*user_onReceive)(uint8_t*, int);
 42  
 43  class TwoWire
 44  {
 45  protected:
 46      uint8_t num;
 47      int8_t sda;
 48      int8_t scl;
 49      i2c_t * i2c;
 50  
 51      uint8_t rxBuffer[I2C_BUFFER_LENGTH];
 52      uint16_t rxIndex;
 53      uint16_t rxLength;
 54      uint16_t rxQueued; //@stickBreaker
 55  
 56      uint8_t txBuffer[I2C_BUFFER_LENGTH];
 57      uint16_t txIndex;
 58      uint16_t txLength;
 59      uint16_t txAddress;
 60      uint16_t txQueued; //@stickbreaker
 61  
 62      uint8_t transmitting;
 63      /* slave Mode, not yet Stickbreaker
 64              static user_onRequest uReq[2];
 65              static user_onReceive uRcv[2];
 66          void onRequestService(void);
 67          void onReceiveService(uint8_t*, int);
 68      */
 69      i2c_err_t last_error; // @stickBreaker from esp32-hal-i2c.h
 70      uint16_t _timeOutMillis;
 71  
 72  public:
 73      TwoWire(uint8_t bus_num);
 74      ~TwoWire();
 75      bool begin(int sda=-1, int scl=-1, uint32_t frequency=0);
 76  
 77      void setClock(uint32_t frequency); // change bus clock without initing hardware
 78      size_t getClock(); // current bus clock rate in hz
 79  
 80      void setTimeOut(uint16_t timeOutMillis);
 81      uint16_t getTimeOut();
 82  
 83      uint8_t lastError();
 84      char * getErrorText(uint8_t err);
 85  
 86      //@stickBreaker for big blocks and ISR model
 87      i2c_err_t writeTransmission(uint16_t address, uint8_t* buff, uint16_t size, bool sendStop=true);
 88      i2c_err_t readTransmission(uint16_t address, uint8_t* buff, uint16_t size, bool sendStop=true, uint32_t *readCount=NULL);
 89  
 90      void beginTransmission(uint16_t address);
 91      void beginTransmission(uint8_t address);
 92      void beginTransmission(int address);
 93  
 94      uint8_t endTransmission(bool sendStop);
 95      uint8_t endTransmission(void);
 96  
 97      uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop);
 98      uint8_t requestFrom(uint16_t address, uint8_t size, uint8_t sendStop);
 99      uint8_t requestFrom(uint16_t address, uint8_t size);
100      uint8_t requestFrom(uint8_t address, uint8_t size, uint8_t sendStop);
101      uint8_t requestFrom(uint8_t address, uint8_t size);
102      uint8_t requestFrom(int address, int size, int sendStop);
103      uint8_t requestFrom(int address, int size);
104  
105      size_t write(uint8_t);
106      size_t write(const uint8_t *, size_t);
107      int available(void);
108      int read(void);
109      int peek(void);
110      void flush(void);
111  
112     
113  
114      void onReceive( void (*)(int) );
115      void onRequest( void (*)(void) );
116  
117      uint32_t setDebugFlags( uint32_t setBits, uint32_t resetBits);
118      bool busy();
119  };
120  
121  extern TwoWire Wire;
122  extern TwoWire Wire1;
123  
124  
125  /*
126  V1.0.1 02AUG2018 First Fix after release, Correct ReSTART handling, change Debug control, change begin()
127    to a function, this allow reporting if bus cannot be initialized, Wire.begin() can be used to recover
128    a hung bus busy condition.
129  V0.2.2 13APR2018 preserve custom SCL,SDA,Frequency when no parameters passed to begin()
130  V0.2.1 15MAR2018 Hardware reset, Glitch prevention, adding destructor for second i2c testing
131  */
132  #endif