Wire.h
Go to the documentation of this file.
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 */
23 
24 #pragma once
25 
26 #include <Stream.h>
27 #include <twi_arch.h>
28 
29 class TwoWire : public Stream
30 {
31 public:
32  static constexpr size_t BUFFER_LENGTH{32};
33 
34  enum Status {
35  I2C_OK = 0,
40  };
41 
42  enum Error {
47  };
48 
49  using UserRequest = void (*)();
50  using UserReceive = void (*)(int len);
51 
53  {
54  }
55 
59  void begin(uint8_t sda, uint8_t scl);
60 
67  void pins(uint8_t sda, uint8_t scl);
68 
74  void begin();
75 
79  void end();
80 
84  void setClock(uint32_t frequency);
85 
89  void setClockStretchLimit(uint32_t limit);
90 
95  void beginTransmission(uint8_t address);
96 
101  Error endTransmission(bool sendStop = true);
102 
109  uint8_t requestFrom(uint8_t address, uint8_t size, bool sendStop = true);
110 
116 
117  /* Stream methods */
118 
119  size_t write(uint8_t) override;
120  size_t write(const uint8_t*, size_t) override;
121  int available() override;
122  int read() override;
123  int peek() override;
124  void flush() override;
125 
126  using Print::write;
127 
128  // Slave mode not currently implemented
129  void onReceive(UserReceive callback)
130  {
131  // userReceiveCallback = callback;
132  }
133 
134  // Slave mode not currently implemented
135  void onRequest(UserRequest callback)
136  {
137  // userRequestCallback = callback;
138  }
139 
140 private:
141  uint8_t twi_sda{DEFAULT_SDA_PIN};
142  uint8_t twi_scl{DEFAULT_SCL_PIN};
143  uint8_t twi_dcount{18};
144  unsigned twi_clockStretchLimit{0};
145 
146  uint8_t rxBuffer[BUFFER_LENGTH]{};
147  uint8_t rxBufferIndex{0};
148  uint8_t rxBufferLength{0};
149 
150  uint8_t txAddress{0};
151  uint8_t txBuffer[BUFFER_LENGTH]{};
152  uint8_t txBufferIndex{0};
153  uint8_t txBufferLength{0};
154 
155  bool transmitting{false};
156  UserRequest userRequestCallback{nullptr};
157  UserReceive userReceiveCallback{nullptr};
158  void onRequestService();
159  void onReceiveService(uint8_t*, int);
160 
161  void twi_delay(uint8_t v);
162  bool twi_write_start();
163  bool twi_write_stop();
164  bool twi_write_bit(bool bit);
165  bool twi_read_bit();
166  bool twi_write_byte(uint8_t byte);
167  uint8_t twi_read_byte(bool nack);
168  Error twi_writeTo(uint8_t address, const uint8_t* buf, size_t len, bool sendStop);
169  Error twi_readFrom(uint8_t address, uint8_t* buf, size_t len, bool sendStop);
170 };
171 
172 #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TWOWIRE)
173 extern TwoWire Wire;
174 #endif
#define bit(x)
Definition: BitManipulations.h:12
#define DEFAULT_SCL_PIN
Definition: Esp32/Core/twi_arch.h:24
#define DEFAULT_SDA_PIN
Definition: Esp32/Core/twi_arch.h:23
TwoWire Wire
virtual size_t write(uint8_t c)=0
Writes a single character to output stream.
Base Stream class.
Definition: Wiring/Stream.h:33
Definition: Wire.h:30
void setClock(uint32_t frequency)
Set approximate clock frequency.
size_t write(const uint8_t *, size_t) override
Writes characters from a buffer to output stream.
void end()
End TwoWire operation.
void beginTransmission(uint8_t address)
Signal start of transaction.
int peek() override
Status
Definition: Wire.h:34
@ I2C_SCL_HELD_LOW_AFTER_READ
Definition: Wire.h:37
@ I2C_SDA_HELD_LOW_AFTER_INIT
Definition: Wire.h:39
@ I2C_OK
Definition: Wire.h:35
@ I2C_SDA_HELD_LOW
Definition: Wire.h:38
@ I2C_SCL_HELD_LOW
Definition: Wire.h:36
Status status()
Query bus status.
TwoWire()
Definition: Wire.h:52
static constexpr size_t BUFFER_LENGTH
Definition: Wire.h:32
size_t write(uint8_t) override
Writes a single character to output stream.
uint8_t requestFrom(uint8_t address, uint8_t size, bool sendStop=true)
Perform a complete 'read' transaction.
int available() override
void flush() override
int read() override
void begin(uint8_t sda, uint8_t scl)
Initialise using selected pins.
void(*)() UserRequest
Definition: Wire.h:49
void onReceive(UserReceive callback)
Definition: Wire.h:129
void pins(uint8_t sda, uint8_t scl)
Switch to selected pins.
void setClockStretchLimit(uint32_t limit)
Set approximate time in microseconds that clocks may be stretched by.
void(*)(int len) UserReceive
Definition: Wire.h:50
void begin()
Initialise using current pin values.
Error endTransmission(bool sendStop=true)
Perform actual transaction with device.
Error
Definition: Wire.h:42
@ I2C_ERR_LINE_BUSY
Definition: Wire.h:46
@ I2C_ERR_SUCCESS
Definition: Wire.h:43
@ I2C_ERR_ADDR_NACK
Definition: Wire.h:44
@ I2C_ERR_DATA_NACK
Definition: Wire.h:45
void onRequest(UserRequest callback)
Definition: Wire.h:135