-
Notifications
You must be signed in to change notification settings - Fork 5
/
i2c.cpp
72 lines (63 loc) · 1.77 KB
/
i2c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <Wire.h>
#include <Arduino.h>
#define RTD_I2CADDR 0x4A // shouldn't change, i2c addr o
// uncomment for long/detailed i2c data xfer
//#define I2C_DEBUG_OUTPUT
bool WriteReg(uint8_t a, uint8_t d) {
Wire.beginTransmission(RTD_I2CADDR);
Wire.write(a);
Wire.write(d);
Wire.endTransmission();
#ifdef I2C_DEBUG_OUTPUT
Serial.print("$"); Serial.print(a, HEX); Serial.print(F(" <- 0x")); Serial.println(d, HEX);
#endif
return true;
}
bool WriteBytesToAddr(uint8_t reg, uint8_t* values, uint8_t len) {
Wire.beginTransmission(RTD_I2CADDR);
Wire.write(reg);
#ifdef I2C_DEBUG_OUTPUT
Serial.print("$"); Serial.print(reg, HEX); Serial.print(" <-");
#endif
for(uint8_t i=0; i< len; i++) {
Wire.write(values[i]);
#ifdef I2C_DEBUG_OUTPUT
Serial.print(" 0x"); Serial.print(values[i], HEX);
#endif
}
Wire.endTransmission();
#ifdef I2C_DEBUG_OUTPUT
Serial.println();
#endif
return true;
}
uint32_t ReadReg(uint8_t a) {
Wire.beginTransmission(RTD_I2CADDR);
Wire.write(a);
Wire.endTransmission();
Wire.requestFrom((uint8_t)RTD_I2CADDR, (uint8_t)1);
uint8_t d = Wire.read();
#ifdef I2C_DEBUG_OUTPUT
Serial.print("$"); Serial.print(a, HEX); Serial.print(F(" -> 0x")); Serial.println(d, HEX);
#endif
return d;
}
bool ReadBytesFromAddr(uint8_t reg, uint8_t* dest, uint8_t len) {
Wire.beginTransmission(RTD_I2CADDR);
Wire.write(reg);
Wire.endTransmission();
#ifdef I2C_DEBUG_OUTPUT
Serial.print("$"); Serial.print(reg, HEX); Serial.print(" ->");
#endif
Wire.requestFrom((uint8_t)RTD_I2CADDR, len);
for (uint8_t i=0; i<len; i++) {
dest[i] = Wire.read();
#ifdef I2C_DEBUG_OUTPUT
Serial.print(" 0x"); Serial.print(dest[i], HEX);
#endif
}
#ifdef I2C_DEBUG_OUTPUT
Serial.println();
#endif
return true;
}