RTduino Source Code Reference Manual
Wire.h
1 /*
2  * Copyright (c) 2021-2022, RTduino Development Team
3  *
4  * SPDX-License-Identifier: LGPL-v2.1
5  *
6  * https://github.com/RTduino/RTduino
7  * https://gitee.com/rtduino/RTduino
8  *
9  * Change Logs:
10  * Date Author Notes
11  * 2022-03-26 Meco Man first version
12  */
13 /*
14  TwoWire.h - TWI/I2C library for Arduino & Wiring
15  Copyright (c) 2006 Nicholas Zambetti. All right reserved.
16 
17  This library is free software; you can redistribute it and/or
18  modify it under the terms of the GNU Lesser General Public
19  License as published by the Free Software Foundation; either
20  version 2.1 of the License, or (at your option) any later version.
21 
22  This library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  Lesser General Public License for more details.
26 
27  You should have received a copy of the GNU Lesser General Public
28  License along with this library; if not, write to the Free Software
29  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 
31  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
32  Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
33  Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
34 */
35 
36 #ifndef TwoWire_h
37 #define TwoWire_h
38 
39 #include <inttypes.h>
40 #include <Stream.h>
41 #include <Arduino.h>
42 
43 // DEPRECATED: Do not use BUFFER_LENGTH, prefer RTDUINO_WIRE_BUFFER_LENGTH
44 #ifndef RTDUINO_WIRE_BUFFER_LENGTH
45 #define RTDUINO_WIRE_BUFFER_LENGTH 32
46 #endif
47 
48 #ifndef RTDUINO_DEFAULT_IIC_BUS_NAME
49 #define RTDUINO_DEFAULT_IIC_BUS_NAME "i2c0" /* dummy name */
50 #warning "Please define this macro in arduino_pinout.h and make sure this board supports I2C!"
51 #endif
52 
53 class TwoWire : public Stream
54 {
55 private:
56  uint8_t rxBuffer[RTDUINO_WIRE_BUFFER_LENGTH];
57  size_t rxBufferIndex;
58  size_t rxBufferLength;
59 
60  uint8_t txAddress;
61  uint8_t txBuffer[RTDUINO_WIRE_BUFFER_LENGTH];
62  size_t txBufferIndex;
63  size_t txBufferLength;
64 
65  uint8_t transmitting;
66  void (*user_onRequest)(void);
67  void (*user_onReceive)(size_t);
68  void onRequestService(void);
69  void onReceiveService(uint8_t*, size_t);
70 
71  uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop);
72  uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop);
73  uint8_t twi_transmit(const uint8_t * buf, uint8_t len);
74  uint8_t twi_status(void);
75 
76 public:
77  struct rt_i2c_bus_device *_i2c_bus_dev;
78 
79  TwoWire();
80  void begin(const char *i2c_dev_name = RTDUINO_DEFAULT_IIC_BUS_NAME);
81  void begin(uint8_t);
82  void begin(int);
83  void end(void);
84  void setClock(uint32_t);
85  void setWireTimeout(uint32_t timeout = 25000, bool reset_with_timeout = false);
86  void beginTransmission(uint8_t);
87  void beginTransmission(int);
88  uint8_t endTransmission(void);
89  uint8_t endTransmission(uint8_t);
90  uint8_t requestFrom(uint8_t, uint8_t);
91  uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
92  uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t);
93  uint8_t requestFrom(int, int);
94  uint8_t requestFrom(int, int, int);
95  virtual size_t write(uint8_t);
96  virtual size_t write(const uint8_t *, size_t);
97  virtual int available(void);
98  virtual int read(void);
99  virtual int peek(void);
100  virtual void flush(void);
101  void onReceive(void (*)(int));
102  void onRequest(void (*)(void));
103 
104  inline size_t write(unsigned long n) { return write((uint8_t)n); }
105  inline size_t write(long n) { return write((uint8_t)n); }
106  inline size_t write(unsigned int n) { return write((uint8_t)n); }
107  inline size_t write(int n) { return write((uint8_t)n); }
108 
109  using Print::write;
110 };
111 
112 extern TwoWire Wire;
113 
114 #endif /* TwoWire_h */
Arduino Core Header File.
Definition: Stream.h:62
Definition: Wire.h:54