AceWire  0.1.0
Unified interface for selecting different I2C implementations on Arduino platforms
SimpleWireFastInterface.h
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_WIRE_SIMPLE_WIRE_FAST_INTERFACE_H
26 #define ACE_WIRE_SIMPLE_WIRE_FAST_INTERFACE_H
27 
28 // This header file requires the digitalWriteFast library on AVR, or the
29 // EpoxyMockDigitalWriteFast library on EpoxyDuino.
30 #if defined(ARDUINO_ARCH_AVR) || defined(EPOXY_DUINO)
31 
32 #include <stdint.h>
33 #include <Arduino.h> // delayMicroseconds()
34 
35 namespace ace_wire {
36 
52 template <
53  uint8_t T_DATA_PIN,
54  uint8_t T_CLOCK_PIN,
55  uint8_t T_DELAY_MICROS
56 >
57 class SimpleWireFastInterface {
58  public:
60  SimpleWireFastInterface() = default;
61 
71  void begin() const {
72  digitalWriteFast(T_CLOCK_PIN, LOW);
73  digitalWriteFast(T_DATA_PIN, LOW);
74 
75  // Begin with both lines in INPUT mode to passively go HIGH.
76  clockHigh();
77  dataHigh();
78  }
79 
81  void end() const {
82  clockHigh();
83  dataHigh();
84  }
85 
90  void beginTransmission(uint8_t addr) const {
91  clockHigh();
92  dataHigh();
93 
94  dataLow();
95  clockLow();
96 
97  // Send I2C addr (7 bits) and R/W bit set to "write" (0x00).
98  uint8_t effectiveAddr = (addr << 1) | 0x00;
99  write(effectiveAddr);
100  }
101 
103  void endTransmission() const {
104  // clock will always be LOW when this is called
105  dataLow();
106  clockHigh();
107  dataHigh();
108  }
109 
120  uint8_t write(uint8_t data) const {
121  for (uint8_t i = 0; i < 8; ++i) {
122  if (data & 0x80) {
123  dataHigh();
124  } else {
125  dataLow();
126  }
127  clockHigh();
128  clockLow();
129  data <<= 1;
130  }
131 
132  return readAck();
133  }
134 
135  private:
140  uint8_t readAck() const {
141  // Go into INPUT mode, reusing dataHigh(), saving 10 flash bytes on AVR.
142  dataHigh();
143  uint8_t ack = digitalReadFast(T_DATA_PIN);
144 
145  // Device releases SDA upon falling edge of the 9th CLK.
146  clockHigh();
147  clockLow();
148  return ack;
149  }
150 
151  // The following methods use compile-time constants from the template
152  // parameters. The compiler will optimize away the 'this' pointer so that
153  // these methods become identical to calling static functions.
154 
155  void bitDelay() const { delayMicroseconds(T_DELAY_MICROS); }
156 
157  void clockHigh() const { pinModeFast(T_CLOCK_PIN, INPUT); bitDelay(); }
158 
159  void clockLow() const { pinModeFast(T_CLOCK_PIN, OUTPUT); bitDelay(); }
160 
161  void dataHigh() const { pinModeFast(T_DATA_PIN, INPUT); bitDelay(); }
162 
163  void dataLow() const { pinModeFast(T_DATA_PIN, OUTPUT); bitDelay(); }
164 };
165 
166 }
167 
168 #endif // defined(ARDUINO_ARCH_AVR)
169 
170 #endif