forked from hideakitai/PCA95x5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PI4IOE5V6416.h
229 lines (198 loc) · 6.22 KB
/
PI4IOE5V6416.h
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
#include <Arduino.h>
#include <Wire.h>
namespace PI4IOE5V64XX {
namespace Reg {
enum : uint8_t {
INPUT_PORT_0 = 0x00,
INPUT_PORT_1,
OUTPUT_PORT_0,
OUTPUT_PORT_1,
POLARITY_INVERSION_PORT_0,
POLARITY_INVERSION_PORT_1,
CONFIGURATION_PORT_0,
CONFIGURATION_PORT_1,
OUTPUT_DRIVE_STRENGTH_REGISTER_0_0 = 0x40,
OUTPUT_DRIVE_STRENGTH_REGISTER_0_1,
OUTPUT_DRIVE_STRENGTH_REGISTER_1_0,
OUTPUT_DRIVE_STRENGTH_REGISTER_1_1,
INPUT_LATCH_REGISTER_0,
INPUT_LATCH_REGISTER_1,
PULL_UP_DOWN_ENABLE_REGISTER_0,
PULL_UP_DOWN_ENABLE_REGISTER_1,
PULL_UP_DOWN_SELECTION_REGISTER_0,
PULL_UP_DOWN_SELECTION_REGISTER_1,
INTERRUPT_MASK_REGISTER_0,
INTERRUPT_MASK_REGISTER_1,
INTERRUPT_STATUS_REGISTER_0,
INTERRUPT_STATUS_REGISTER_1,
OUTPUT_PORT_CONFIGURATION_REGISTER
};
}
namespace Port {
enum Port : uint8_t {
P00,
P01,
P02,
P03,
P04,
P05,
P06,
P07,
P10,
P11,
P12,
P13,
P14,
P15,
P16,
P17,
};
} // namespace Port
namespace Level {
enum Level : uint8_t { L, H };
enum LevelAll : uint16_t { L_ALL = 0x0000, H_ALL = 0xFFFF };
} // namespace Level
namespace Polarity {
enum Polarity : uint8_t { ORIGINAL, INVERTED };
enum PolarityAll : uint16_t { ORIGINAL_ALL = 0x0000, INVERTED_ALL = 0xFFFF };
} // namespace Polarity
namespace Direction {
enum Direction : uint8_t { OUT, IN };
enum DirectionAll : uint16_t { OUT_ALL = 0x0000, IN_ALL = 0xFFFF };
} // namespace Direction
namespace PullUpDownEnable {
enum PullUpDownEnable : uint8_t { DISABLE, ENABLE };
enum PullUpDownEnableAll : uint16_t { DISABLE_ALL = 0x0000, ENABLE_ALL = 0xFFFF };
}
namespace PullUpDownSelection {
enum PullUpDownSelection : uint8_t { PULL_DOWN, PULL_UP };
enum PullUpDownSelectionAll : uint16_t { PULL_DOWN_ALL = 0x0000, PULL_UP_ALL = 0xFFFF };
}
template <typename WireType = TwoWire>
class PI4IOE5V64XX {
union Ports {
uint16_t w;
uint8_t b[2];
};
static constexpr uint8_t BASE_I2C_ADDR = 0x20;
WireType* wire {nullptr};
uint8_t addr {BASE_I2C_ADDR};
Ports input {0x0000};
Ports output {0xFFFF};
Ports pol {0x0000};
Ports dir {0xFFFF};
Ports pe {0x0000};
Ports pud {0x0000};
uint8_t status {0x00};
public:
void attach(WireType& wire, uint8_t i2c_addr = BASE_I2C_ADDR) {
this->wire = &wire;
this->addr = i2c_addr;
}
uint16_t read() {
read_bytes(this->addr, Reg::INPUT_PORT_0, this->input.b, 2);
return this->input.w;
}
Level::Level read(const Port::Port port) {
uint16_t v = read();
return (v & (1 << port)) ? Level::H : Level::L;
}
bool write(const uint16_t value) {
this->output.w = value;
return write_impl();
}
bool write(const Port::Port port, const Level::Level level) {
if (level == Level::H) {
this->output.w |= (1 << port);
} else {
this->output.w &= ~(1 << port);
}
return write_impl();
}
bool polarity(const uint16_t value) {
this->pol.w = value;
return polarity_impl();
}
bool polarity(const Port::Port port, const Polarity::Polarity pol) {
if (pol == Polarity::INVERTED) {
this->pol.w |= (1 << port);
} else {
this->pol.w &= ~(1 << port);
}
return polarity_impl();
}
bool direction(const uint16_t value) {
this->dir.w = value;
return direction_impl();
}
bool direction(const Port::Port port, const Direction::Direction dir) {
if (dir == Direction::IN) {
this->dir.w |= (1 << port);
} else {
this->dir.w &= ~(1 << port);
}
return direction_impl();
}
bool pullUpDownEnable(const uint16_t value) {
this->pe.w = value;
return pullup_down_enable_impl();
}
bool pullUpDownEnable(const Port::Port port, const PullUpDownEnable::PullUpDownEnable pe) {
if (pe == PullUpDownEnable::ENABLE) {
this->pe.w |= (1 << port);
} else {
this->pe.w &= ~(1 << port);
}
return pullup_down_enable_impl();
}
bool pullUpDownSelection(const uint16_t value) {
this->pud.w = value;
return pullup_down_selection_impl();
}
bool pullUpDownSelection(const Port::Port port, const PullUpDownSelection::PullUpDownSelection pud) {
if (pud == PullUpDownSelection::PULL_UP) {
this->pe.w |= (1 << port);
} else {
this->pe.w &= ~(1 << port);
}
return pullup_down_selection_impl();
}
uint8_t i2c_error() const {
return status;
}
private:
bool write_impl() {
return write_bytes(this->addr, Reg::OUTPUT_PORT_0, this->output.b, 2);
}
bool polarity_impl() {
return write_bytes(this->addr, Reg::POLARITY_INVERSION_PORT_0, this->pol.b, 2);
}
bool direction_impl() {
return write_bytes(this->addr, Reg::CONFIGURATION_PORT_0, this->dir.b, 2);
}
bool pullup_down_enable_impl() {
return write_bytes(this->addr, Reg::PULL_UP_DOWN_ENABLE_REGISTER_0, this->dir.b, 2);
}
bool pullup_down_selection_impl() {
return write_bytes(this->addr, Reg::PULL_UP_DOWN_SELECTION_REGISTER_0, this->dir.b, 2);
}
int8_t read_bytes(const uint8_t dev, const uint8_t reg, uint8_t* data, const uint8_t size) {
wire->beginTransmission(dev);
wire->write(reg);
wire->endTransmission();
wire->requestFrom(dev, size);
int8_t count = 0;
while (wire->available()) data[count++] = wire->read();
return count;
}
bool write_bytes(const uint8_t dev, const uint8_t reg, const uint8_t* data, const uint8_t size) {
wire->beginTransmission(dev);
wire->write(reg);
for (uint8_t i = 0; i < size; i++) wire->write(data[i]);
status = wire->endTransmission();
return (status == 0);
}
};
} // namespace PI4IOE5V64XX
// using PI4IOE5V6408 = PI4IOE5V64XX::PI4IOE5V64XX<>;
using PI4IOE5V6416 = PI4IOE5V64XX::PI4IOE5V64XX<>;