-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTCA9548.cpp
250 lines (194 loc) · 4.28 KB
/
TCA9548.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// FILE: TCA9548.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// DATE: 2021-03-16
// PURPOSE: Arduino Library for TCA9548 I2C multiplexer and compatibles.
#include "TCA9548.h"
TCA9548::TCA9548(uint8_t deviceAddress, TwoWire *wire)
{
_address = deviceAddress;
_wire = wire;
_mask = 0x00;
_resetPin = -1;
_forced = false;
_error = TCA9548_OK;
_channels = 8;
}
bool TCA9548::begin(uint8_t mask)
{
if (! isConnected()) return false;
setChannelMask(mask);
return true;
}
bool TCA9548::isConnected()
{
return isConnected(_address);
}
bool TCA9548::isConnected(uint8_t address)
{
_wire->beginTransmission(address);
return ( _wire->endTransmission() == 0);
}
bool TCA9548::isConnected(uint8_t address, uint8_t channel)
{
if (!selectChannel(channel)) return false;
return isConnected(address);
}
uint8_t TCA9548::find(uint8_t address)
{
uint8_t mask = 0x00;
for (uint8_t ch = 0; ch < _channels; ch++)
{
// will work partially if MP is off line (by choice).
selectChannel(ch);
if (isConnected(address)) mask |= (1 << ch);
}
return mask;
}
uint8_t TCA9548::channelCount()
{
return _channels;
}
bool TCA9548::enableChannel(uint8_t channel)
{
if (channel >= _channels) return false;
return setChannelMask(_mask | (0x01 << channel));
}
bool TCA9548::disableChannel(uint8_t channel)
{
if (channel >= _channels) return false;
return setChannelMask(_mask & ~(0x01 << channel));
}
bool TCA9548::selectChannel(uint8_t channel)
{
if (channel >= _channels) return false;
return setChannelMask(0x01 << channel);
}
bool TCA9548::isEnabled(uint8_t channel)
{
if (channel >= _channels) return false;
return (_mask & (0x01 << channel));
}
bool TCA9548::disableAllChannels()
{
return setChannelMask(0x00);
}
bool TCA9548::setChannelMask(uint8_t mask)
{
if ((_mask == mask) && (not _forced)) return true;
_mask = mask;
_wire->beginTransmission(_address);
_wire->write(_mask);
_error = _wire->endTransmission();
return (_error == 0);
}
uint8_t TCA9548::getChannelMask()
{
if (_forced) // read from device.
{
_wire->requestFrom(_address, (uint8_t)1);
_mask = _wire->read();
}
return _mask;
}
void TCA9548::setResetPin(uint8_t resetPin)
{
_resetPin = resetPin;
pinMode(_resetPin, OUTPUT);
digitalWrite(_resetPin, HIGH); // page 3 HIGH is normal operation
}
void TCA9548::reset()
{
digitalWrite(_resetPin, LOW);
delayMicroseconds(1); // datasheet page 6 & 7 - 500 ns
digitalWrite(_resetPin, HIGH);
}
void TCA9548::setForced(bool forced)
{
_forced = forced;
};
bool TCA9548::getForced()
{
return _forced;
};
int TCA9548::getError()
{
int error = _error;
_error = TCA9548_OK;
return error;
}
/////////////////////////////////////////////////////////////
//
// PCA9548
//
PCA9548::PCA9548(uint8_t deviceAddress, TwoWire *wire) : TCA9548(deviceAddress, wire)
{
_channels = 8;
}
/////////////////////////////////////////////////////////////
//
// PCA9546
//
PCA9546::PCA9546(uint8_t deviceAddress, TwoWire *wire) : TCA9548(deviceAddress, wire)
{
_channels = 4;
}
uint8_t PCA9546::getChannelMask()
{
if (_forced) // read from device.
{
_wire->requestFrom(_address, (uint8_t)1);
_mask = _wire->read();
}
return _mask &= 0x0F;
}
/////////////////////////////////////////////////////////////
//
// PCA9545
//
PCA9545::PCA9545(uint8_t deviceAddress, TwoWire *wire) : TCA9548(deviceAddress, wire)
{
_channels = 4;
}
uint8_t PCA9545::getChannelMask()
{
if (_forced) // read from device.
{
_wire->requestFrom(_address, (uint8_t)1);
_mask = _wire->read();
}
return _mask &= 0x0F;
}
uint8_t PCA9545::getInterruptMask()
{
_wire->requestFrom(_address, (uint8_t)1);
uint8_t mask = _wire->read();
mask >>= 4;
return mask;
}
/////////////////////////////////////////////////////////////
//
// PCA9548
//
PCA9543::PCA9543(uint8_t deviceAddress, TwoWire *wire) : TCA9548(deviceAddress, wire)
{
_channels = 2;
}
uint8_t PCA9543::getChannelMask()
{
if (_forced) // read from device.
{
_wire->requestFrom(_address, (uint8_t)1);
_mask = _wire->read();
}
return _mask &= 0x03;
}
uint8_t PCA9543::getInterruptMask()
{
_wire->requestFrom(_address, (uint8_t)1);
uint8_t mask = _wire->read();
mask >>= 4;
return mask;
}
// -- END OF FILE --