This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CConnector.zig
285 lines (271 loc) · 11.6 KB
/
I2CConnector.zig
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const std = @import("std");
const Cxxrtl = @import("./Cxxrtl.zig");
const Tick = @import("./OLEDConnector.zig").Tick;
const RW = @import("./OLEDConnector.zig").RW;
const Sample = @import("./Sample.zig").Sample;
addr: u7,
byte_transmitter: ByteTransmitter = .{},
addressed: bool = false,
scl_o: Sample(bool),
scl_oe: Sample(bool),
sda_o: Sample(bool),
sda_oe: Sample(bool),
sda_i: Cxxrtl.Object(bool),
pub fn init(cxxrtl: Cxxrtl, addr: u7) @This() {
const scl_o = Sample(bool).init(cxxrtl, "hw_bus__scl_o", false);
const scl_oe = Sample(bool).init(cxxrtl, "hw_bus__scl_oe", false);
const sda_o = Sample(bool).init(cxxrtl, "hw_bus__sda_o", false);
const sda_oe = Sample(bool).init(cxxrtl, "hw_bus__sda_oe", false);
const sda_i = cxxrtl.get(bool, "hw_bus__sda_i");
return .{
.addr = addr,
.scl_o = scl_o,
.scl_oe = scl_oe,
.sda_o = sda_o,
.sda_oe = sda_oe,
.sda_i = sda_i,
};
}
pub fn tick(self: *@This()) Tick {
const scl_o = self.scl_o.tick();
const scl_oe = self.scl_oe.tick();
const sda_o = self.sda_o.tick();
const sda_oe = self.sda_oe.tick();
const result = self.byte_transmitter.process(scl_o, scl_oe, sda_o, sda_oe);
switch (result) {
.Pass => return .Pass,
.WriteAck => {
const byte = self.byte_transmitter.byte;
// XXX(Ch): This is probably buggy: if we get a START for not-us, we
// .Pass, but then imagine the first data byte looks like it
// addressses us.
if (!self.addressed) {
const addr: u7 = @as(u7, @truncate(byte >> 1));
const rw = @as(RW, @enumFromInt(@as(u1, @truncate(byte))));
if (addr == self.addr and rw == .W) {
self.sda_i.next(false);
self.addressed = true;
self.byte_transmitter.rw = .W;
return .AddressedWrite;
} else if (addr == self.addr and rw == .R) {
self.sda_i.next(false);
self.addressed = true;
self.byte_transmitter.rw = .W;
self.byte_transmitter.next_rw = .R;
return .{ .AddressedRead = &self.byte_transmitter.byte };
} else {
return .Pass;
}
} else {
self.sda_i.next(false);
return .{ .Byte = byte };
}
},
.ReadAck => {
return .{ .AddressedRead = &self.byte_transmitter.byte };
},
.SetSda => |v| {
self.sda_i.next(v);
return .Pass;
},
.Error => {
self.sda_i.next(true);
std.debug.print("got error, resetting\n", .{});
self.addressed = false;
self.byte_transmitter.rw = .W;
return .Error;
},
.Fish => {
self.sda_i.next(true);
if (!self.addressed) {
std.debug.print("command parser fish while unaddressed\n", .{});
}
self.addressed = false;
self.byte_transmitter.rw = .W;
return .Fish;
},
}
}
pub fn reset(self: *@This()) void {
self.addressed = false;
}
const ByteTransmitter = struct {
rw: RW = .W,
next_rw: RW = .W,
state: enum {
IDLE,
START_SDA_LOW,
WAIT_BIT_SCL_RISE,
WAIT_BIT_SCL_FALL,
WAIT_ACK_SCL_RISE,
WAIT_ACK_SCL_FALL,
} = .IDLE,
bits: u3 = 0,
byte: u8 = 0,
const Result = union(enum) {
Pass,
WriteAck,
ReadAck,
SetSda: bool,
Error,
Fish,
};
fn process(self: *ByteTransmitter, scl_o: *const Sample(bool), scl_oe: *const Sample(bool), sda_o: *const Sample(bool), sda_oe: *const Sample(bool)) Result {
const all_stable = scl_oe.stable() and scl_o.stable() and sda_oe.stable() and sda_o.stable();
switch (self.state) {
.IDLE => {
if (scl_oe.stable_high() and scl_o.stable_high() and sda_oe.stable_high() and sda_o.falling()) {
self.state = .START_SDA_LOW;
self.bits = 0;
self.byte = 0;
return .{ .SetSda = true };
}
return .Pass;
},
.START_SDA_LOW => {
if (scl_oe.stable_high() and scl_o.falling() and sda_oe.stable_high() and sda_o.stable_low()) {
self.state = .WAIT_BIT_SCL_RISE;
} else if (!all_stable) {
self.state = .IDLE;
}
return .Pass;
},
.WAIT_BIT_SCL_RISE => {
if (self.rw == .W) {
if (scl_oe.stable_high() and scl_o.rising() and sda_oe.stable_high() and sda_o.stable()) {
self.byte = (self.byte << 1) | @intFromBool(sda_o.curr);
self.state = .WAIT_BIT_SCL_FALL;
return .Pass;
} else if (!scl_oe.stable_high() or !sda_oe.stable_high()) {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_RISE(W): scl_oe({}), sda_oe({})\n", .{ scl_oe, sda_oe });
return .Error;
}
} else {
if (scl_oe.stable_high() and scl_o.rising() and sda_oe.stable_low()) {
self.state = .WAIT_BIT_SCL_FALL;
return .Pass;
} else if (!scl_oe.stable_high() or !sda_oe.stable_low()) {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_RISE(R): scl_oe({}), sda_oe({})\n", .{ scl_oe, sda_oe });
return .Error;
}
}
return .Pass;
},
.WAIT_BIT_SCL_FALL => {
if (self.rw == .W) {
if (scl_oe.stable_high() and scl_o.falling() and sda_oe.falling() and sda_o.stable()) {
if (self.bits != 7) {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_FALL(W): bits({}), byte({}) -- scl_oe({}), scl_o({}), sda_oe({}), sda_o({})\n", .{ self.bits, self.byte, scl_oe, scl_o, sda_oe, sda_o });
return .Error;
}
self.state = .WAIT_ACK_SCL_RISE;
return .WriteAck;
} else if (scl_oe.stable_high() and scl_o.falling() and sda_oe.stable_high() and sda_o.stable()) {
if (self.bits == 7) {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_FALL(W): bits({}), byte({}) -- scl_oe({}), scl_o({}), sda_oe({}), sda_o({})\n", .{ self.bits, self.byte, scl_oe, scl_o, sda_oe, sda_o });
return .Error;
}
self.bits += 1;
self.state = .WAIT_BIT_SCL_RISE;
return .Pass;
} else if (scl_oe.stable_high() and scl_o.stable_high() and sda_oe.stable_high() and sda_o.rising()) {
if (self.bits == 0 and self.byte == 0) {
self.state = .IDLE;
return .Fish;
} else {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_FALL(W): bits({}), byte({})\n", .{ self.bits, self.byte });
return .Error;
}
} else if (scl_oe.stable_high() and scl_o.stable_high() and sda_oe.stable_high() and sda_o.falling()) {
// repeated start
self.state = .START_SDA_LOW;
self.rw = .W;
self.bits = 0;
self.byte = 0;
return .Fish;
} else if (!all_stable) {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_FALL(W): scl_oe({}), scl_o({}), sda_oe({}), sda_o({})\n", .{ scl_oe, scl_o, sda_oe, sda_o });
return .Error;
}
return .Pass;
} else {
if (scl_oe.stable_high() and scl_o.falling() and sda_oe.stable_low()) {
if (self.bits == 7) {
self.state = .WAIT_ACK_SCL_RISE;
return .Pass;
} else {
self.bits += 1;
self.state = .WAIT_BIT_SCL_RISE;
return self.prepareSendBit();
}
} else if (!all_stable) {
self.state = .IDLE;
std.debug.print("WAIT_BIT_SCL_FALL(R): bits({}), byte({}) -- scl_oe({}), scl_o({}), sda_oe({}), sda_o({})\n", .{ self.bits, self.byte, scl_oe, scl_o, sda_oe, sda_o });
return .Error;
}
return .Pass;
}
},
.WAIT_ACK_SCL_RISE => {
if (self.rw == .W) {
if (sda_oe.falling()) {
return .Pass;
} else if (scl_oe.stable_high() and scl_o.rising() and sda_oe.stable_low()) {
self.state = .WAIT_ACK_SCL_FALL;
return .Pass;
} else if (!all_stable) {
self.state = .IDLE;
std.debug.print("WAIT_ACK_SCL_RISE(W): scl_oe({}), scl_o({}), sda_oe({}), sda_o({})\n", .{ scl_oe, scl_o, sda_oe, sda_o });
return .Error;
}
return .Pass;
} else {
if (sda_oe.rising()) {
return .Pass;
} else if (scl_oe.stable_high() and scl_o.rising() and sda_oe.stable_high()) {
self.state = .WAIT_ACK_SCL_FALL;
const ack = !sda_o.curr;
if (ack) {
return .ReadAck;
} else {
self.next_rw = .W;
return .Pass;
}
} else if (!all_stable) {
self.state = .IDLE;
std.debug.print("WAIT_ACK_SCL_RISE(R): scl_oe({}), scl_o({}), sda_oe({}), sda_o({})\n", .{ scl_oe, scl_o, sda_oe, sda_o });
return .Error;
}
return .Pass;
}
},
.WAIT_ACK_SCL_FALL => {
if (scl_oe.stable_high() and scl_o.falling()) {
self.state = .WAIT_BIT_SCL_RISE;
self.bits = 0;
self.rw = self.next_rw;
if (self.rw == .W) {
self.byte = 0;
return .{ .SetSda = true };
} else {
return self.prepareSendBit();
}
} else if (!(scl_oe.stable() and scl_o.stable())) {
self.state = .IDLE;
std.debug.print("WAIT_ACK_SCL_FALL: scl_oe({}), scl_o({})\n", .{ scl_oe, scl_o });
return .Error;
}
return .Pass;
},
}
}
fn prepareSendBit(self: *ByteTransmitter) Result {
return .{ .SetSda = (self.byte >> (7 - self.bits)) & 1 == 1 };
}
};