-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.go
295 lines (231 loc) · 5.53 KB
/
i2c.go
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
286
287
288
289
290
291
292
293
294
295
package ch347
import "errors"
var (
ErrI2CRead = errors.New("i2c read failed")
ErrI2CWrite = errors.New("i2c write failed")
)
type I2CMode uint8
const (
I2CMode0 I2CMode = iota // Low rate 20KHz.
I2CMode1 // Standart rate 100KHz.
I2CMode2 // Fast rate 400KHz.
I2CMode3 // High rate 750KHz.
)
// SetI2C configures the interface with a specified mode.
// - I2CMode0 - Low rate 20KHz.
// - I2CMode1 - Standart rate 100KHz.
// - I2CMode2 - Fast rate 400KHz.
// - I2CMode3 - High rate 750KHz.
func (c *IO) SetI2C(mode I2CMode) error {
c.mu.Lock()
defer c.mu.Unlock()
p := []byte{0x03, 0x00, 0xaa, 0x60 | byte(mode), 0x00}
_, err := c.Dev.Write(p)
return err
}
// I2C performs write and read operations with device on given address.
//
// Example:
//
// // Read all 4096 bytes from 24C32B chip
// w := []byte{0x00, 0x00} // "Random read". Write page address first.
// r := make([]byte, 4096) // Allocate buffer for 4096 bytes to be read.
// err = c.I2C(0x57, w, r)
//
// if err != nil {
// return err
// }
//
// // Print result as a string
// fmt.Println(string(r))
func (c *IO) I2C(addr uint16, w, r []byte) error {
c.mu.Lock()
defer c.mu.Unlock()
const (
// The command package of the I2C interface, starting from the secondary byte, is the I2C command stream
CmdI2CStream = 0xAA
// Command flow of I2C interface: generate start bit
CmdI2CStart = 0x74
// Command flow of I2C interface: generate stop bit
CmdI2CStop = 0x75
// Command flow of I2C interface: output data, bit 5 - bit 0 is the length, subsequent bytes are data, and length 0 only sends one byte and returns an answer
CmdI2CWrite = 0x80
// I2C interface command flow: input data, bit 5 - bit 0 is the length, and 0 length only receives one byte and sends no response
CmdI2CRead = 0xc0 // Note: a reads must be completed with one byte reading (0xc0), otherwise next operation will fail.
)
const maxLen = 63 // Max data length with 6 bits.
p := make([]byte, 0, 512)
// Counters to confirm writes or reads of I2C bytes.
toWrite := 0
toRead := 0
rpos := 0
hasRead := false
write := func() error {
p = append(p, 0x00) // End packet with 0x00.
// Calucate and set length in the first 2 bytes.
plen := len(p) - 2
p[0] = byte(plen & 0xff)
p[1] = byte((plen >> 8) & 0xff)
_, err := c.Dev.Write(p)
if err != nil {
return err
}
// Confirm I2C operation.
if clen := (toWrite + toRead); clen > 0 {
if hasRead {
clen++
}
rlen := (2 + clen)
p = p[:rlen]
_, err = c.Dev.Read(p)
if err != nil {
return err
}
pos := 2 // Skip 2 bytes in begining.
// Confirm writes.
for toWrite > 0 {
if p[pos] == 0x00 {
// pos += toWrite
// toWrite = 0
// break
return ErrI2CWrite
}
toWrite--
pos++
}
// Confirm reads.
if toRead > 0 {
if hasRead { // Confirm read request.
hasRead = false
if p[pos] != 0x01 {
// pos += toRead
return ErrI2CRead
}
pos++
}
copy(r[rpos:rpos+toRead], p[pos:pos+toRead]) // Copy i2c bytes to reader buffer.
rpos += toRead
toRead = 0
}
}
p = p[:0] // Start new packet.
return nil
}
pack := func(elems ...byte) error {
// First make it work, then make it better.
if (len(p) + len(elems)) >= (maxPacketLen - 2) {
if err := write(); err != nil {
return err
}
// p = p[:0]
}
if len(p) == 0 {
p = append(p, 0x00, 0x00) // Every packet starts with length.
p = append(p, CmdI2CStream) // CMD byte.
}
p = append(p, elems...)
return nil
}
if wlen := len(w); wlen != 0 {
err := pack(CmdI2CStart)
if err != nil {
return err
}
pos := 0
d := []byte{CmdI2CWrite} // Start with length, will be calculated at the end.
var dlen int
for pos < wlen {
// Calc potential write part length.
dlen = (wlen - pos)
if dlen > maxLen {
dlen = maxLen
}
if pos == 0 && dlen == maxLen {
dlen--
}
if pos == 0 { // Write addr, yes.
d = append(d, byte(addr<<1))
}
// Write data.
d = append(d, w[pos:pos+dlen]...)
pos += dlen
dlen = len(d) - 1
if pos == 0 {
dlen++ // Oh.
}
d[0] = CmdI2CWrite | byte(dlen) // Length in the begining.
err = pack(d...)
if err != nil {
return err
}
d = d[:1] // Reset write part.
toWrite += dlen
}
}
if rlen := len(r); rlen != 0 {
// Read request.
d := []byte{
CmdI2CStart, CmdI2CWrite | 1, byte(addr<<1) | 1,
}
hasRead = true
// For some reason reading must end with one byte reading (0xc0).
maxRLen := 64
dlen := rlen
send := false
for rlen > 0 {
dlen = rlen
if dlen > maxLen {
dlen = maxLen
}
if nlen := (2 + toWrite + toRead + dlen); nlen >= maxPacketLen {
dlen -= (nlen - maxPacketLen)
send = true
if hasRead {
toRead--
}
}
if maxRLen == 63 {
d = append(d, CmdI2CRead|byte(dlen))
} else if dlen > 1 {
// Account for extra byte (0xc0) that needs to be send to finish reading.
d = append(d, CmdI2CRead|byte(dlen)-1)
}
if maxRLen == 64 {
maxRLen = 63
}
toRead += dlen
if send {
send = false
err := pack(d...)
if err != nil {
return err
}
err = write()
if err != nil {
return err
}
// p = p[:0]
d = d[:0]
}
rlen -= dlen
}
// I have no idea anymore.
if !hasRead {
toRead++
}
d = append(d, CmdI2CRead)
err := pack(d...)
if err != nil {
return err
}
}
err := pack(CmdI2CStop)
if err != nil {
return err
}
err = write()
if err != nil {
return err
}
return nil
}