-
Notifications
You must be signed in to change notification settings - Fork 24
/
i2c.cpp
125 lines (110 loc) · 3.58 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// **********************************************************************************
// I2C management source file for remora project
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend but please abide with the CC-BY-SA license:
// http://creativecommons.org/licenses/by-sa/4.0/
//
// Written by Charles-Henri Hallard (http://hallard.me)
//
// History : V1.00 2015-01-22 - First release
//
// All text above must be included in any redistribution.
//
// **********************************************************************************
#include "i2c.h"
/* ======================================================================
Function: i2c_init
Purpose : initialize the I2C function and setup
Input : -
Output : -
Comments: -
====================================================================== */
bool i2c_init(void)
{
// Set i2C speed
#if defined (SPARK)
Wire.setSpeed(CLOCK_SPEED_400KHZ);
#elif defined (ESP8266)
// Sepecific ESP8266 to set I2C Speed
Wire.setClock(400000);
#endif
Wire.begin();
}
/* ======================================================================
Function: i2c_detect
Purpose : check that a adressed device respond
Input : I2C device address
Output : true is seen (ACKed device) false otherwise
Comments: i2c_init should have been called before
====================================================================== */
bool i2c_detect(uint8_t _address)
{
Wire.beginTransmission(_address);
return (Wire.endTransmission() == 0 ? true : false);
}
/* ======================================================================
Function: i2c_scan
Purpose : scan I2C bus and display result
Input : address wanted to search (0xFF)
Output : true if I2C device found at address given
Comments: mostly used for debug purpose
====================================================================== */
uint8_t i2c_scan()
{
byte error, address;
uint8_t nDevices = 0;
unsigned long start = millis();
DebuglnF("Scanning I2C bus ...");
// slow down i2C speed in case of slow device
#if defined (SPARK)
Wire.setSpeed(CLOCK_SPEED_100KHZ);
#elif defined (ESP8266)
// Sepecific ESP8266 to set I2C Speed
Wire.setClock(100000);
#endif
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
DebugF("I2C device found at address 0x");
if (address<16)
DebugF("0");
DEBUG_SERIAL.print(address, HEX);
if (address>=0x20 && address<=0x27)
Debugln("-> MCP23017 !");
else if (address==0x3C || address==0x3D) {
DebugF("-> OLED ");
if (address==0x3C) {
config.oled_type = 1306;
DebuglnF("1306!");
} else if (address==0x3D) {
config.oled_type = 1106;
DebuglnF("1106!");
}
} else if (address==0x29 || address==0x39 || address==0x49)
Debugln("-> TSL2561 !");
else
Debugf("-> Unknown device at 0x%02X!\n");
nDevices++;
}
}
Debug(nDevices);
DebugF(" I2C devices found, scan took ");
Debug(millis()-start);
Debugln(" ms");
// Get back to full speed
// slow down i2C speed in case of slow device
#if defined (SPARK)
Wire.setSpeed(CLOCK_SPEED_400KHZ);
#elif defined (ESP8266)
// Sepecific ESP8266 to set I2C Speed
Wire.setClock(400000);
#endif
return (nDevices);
}