forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_device.go
395 lines (333 loc) · 11.5 KB
/
i2c_device.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package system
import (
"fmt"
"log"
"os"
"sync"
"unsafe"
)
const (
i2cDeviceDebug = false
forceSetAddress = false // normally address will be written only when changed, this behavior can be overridden
)
const (
// From /usr/include/linux/i2c-dev.h:
// ioctl signals
I2C_SLAVE = 0x0703
I2C_FUNCS = 0x0705
I2C_SMBUS = 0x0720
// Read/write markers
I2C_SMBUS_READ = 1
I2C_SMBUS_WRITE = 0
// From /usr/include/linux/i2c.h:
// Adapter functionality
I2C_FUNC_SMBUS_READ_BYTE = 0x00020000
I2C_FUNC_SMBUS_WRITE_BYTE = 0x00040000
I2C_FUNC_SMBUS_READ_BYTE_DATA = 0x00080000
I2C_FUNC_SMBUS_WRITE_BYTE_DATA = 0x00100000
I2C_FUNC_SMBUS_READ_WORD_DATA = 0x00200000
I2C_FUNC_SMBUS_WRITE_WORD_DATA = 0x00400000
I2C_FUNC_SMBUS_READ_BLOCK_DATA = 0x01000000
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA = 0x02000000
I2C_FUNC_SMBUS_READ_I2C_BLOCK = 0x04000000 // I2C-like block transfer with 1-byte reg. addr.
I2C_FUNC_SMBUS_WRITE_I2C_BLOCK = 0x08000000 // I2C-like block transfer with 1-byte reg. addr.
// Transaction types
I2C_SMBUS_BYTE = 1
I2C_SMBUS_BYTE_DATA = 2
I2C_SMBUS_WORD_DATA = 3
I2C_SMBUS_PROC_CALL = 4
I2C_SMBUS_BLOCK_DATA = 5
I2C_SMBUS_I2C_BLOCK_BROKEN = 6
I2C_SMBUS_BLOCK_PROC_CALL = 7 /* SMBus 2.0 */
I2C_SMBUS_I2C_BLOCK_DATA = 8 /* SMBus 2.0 */
)
type i2cSmbusIoctlData struct {
readWrite byte
command byte
protocol uint32
data unsafe.Pointer
}
type i2cDevice struct {
location string
sys systemCaller
fs filesystem
file File
funcs uint64 // adapter functionality mask
lastAddress int
mutex sync.Mutex
}
// NewI2cDevice returns a Linux Kernel access by ioctrl to the given i2c bus location (character device).
// Important note for "os.ModeExclusive": this is undefined without create the file for character devices, this means
// a second open will not return an error e.g. due to a busy resource. If this is not wanted, e.g. to minimize count of
// open fd's this needs to be prevented at caller side by implementing a caching mechanism. Furthermore this behavior
// can lead to problems with multiple devices on the same bus because the cycle SetAddress()...Read()/Write() etc. can
// be interrupted when using multiple instances for the same location.
func (a *Accesser) NewI2cDevice(location string) (*i2cDevice, error) {
if location == "" {
return nil, fmt.Errorf("the given character device location is empty")
}
d := &i2cDevice{
location: location,
sys: a.sys,
fs: a.fs,
lastAddress: -1,
}
return d, nil
}
// Close closes the character device file and resets the lazy variables.
func (d *i2cDevice) Close() error {
d.mutex.Lock()
defer d.mutex.Unlock()
d.funcs = 0
d.lastAddress = -1
if d.file != nil {
return d.file.Close()
}
return nil
}
// ReadByte reads a byte from the current register of an i2c device.
func (d *i2cDevice) ReadByte(address int) (byte, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.queryFunctionality(I2C_FUNC_SMBUS_READ_BYTE, "read byte"); err != nil {
return 0, err
}
var data uint8 = 0xFC // set value for debugging purposes
err := d.smbusAccess(address, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, unsafe.Pointer(&data))
return data, err
}
// ReadByteData reads a byte from the given register of an i2c device.
func (d *i2cDevice) ReadByteData(address int, reg uint8) (val uint8, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.queryFunctionality(I2C_FUNC_SMBUS_READ_BYTE_DATA, "read byte data"); err != nil {
return 0, err
}
var data uint8 = 0xFD // set value for debugging purposes
err = d.smbusAccess(address, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, unsafe.Pointer(&data))
return data, err
}
// ReadWordData reads a 16 bit value starting from the given register of an i2c device.
func (d *i2cDevice) ReadWordData(address int, reg uint8) (val uint16, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.queryFunctionality(I2C_FUNC_SMBUS_READ_WORD_DATA, "read word data"); err != nil {
return 0, err
}
var data uint16 = 0xFFFE // set value for debugging purposes
err = d.smbusAccess(address, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, unsafe.Pointer(&data))
return data, err
}
// ReadBlockData fills the given buffer with reads starting from the given register of an i2c device.
func (d *i2cDevice) ReadBlockData(address int, reg uint8, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
dataLen := len(data)
if dataLen > 32 {
return fmt.Errorf("Reading blocks larger than 32 bytes (%v) not supported", len(data))
}
data[0] = 0xFF // set value for debugging purposes
if err := d.queryFunctionality(I2C_FUNC_SMBUS_READ_I2C_BLOCK, "read block data"); err != nil {
if i2cDeviceDebug {
log.Printf("%s, use fallback\n", err.Error())
}
return d.readBlockDataFallback(address, reg, data)
}
// set the first element with the data size
buf := make([]byte, dataLen+1)
buf[0] = byte(dataLen)
copy(buf[1:], data)
if err := d.smbusAccess(address, I2C_SMBUS_READ, reg, I2C_SMBUS_I2C_BLOCK_DATA, unsafe.Pointer(&buf[0])); err != nil {
return err
}
// get data from buffer without first size element
copy(data, buf[1:])
return nil
}
// WriteByte writes the given byte value to the current register of an i2c device.
func (d *i2cDevice) WriteByte(address int, val byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.queryFunctionality(I2C_FUNC_SMBUS_WRITE_BYTE, "write byte"); err != nil {
return err
}
return d.smbusAccess(address, I2C_SMBUS_WRITE, val, I2C_SMBUS_BYTE, nil)
}
// WriteByteData writes the given byte value to the given register of an i2c device.
func (d *i2cDevice) WriteByteData(address int, reg uint8, val uint8) error {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.queryFunctionality(I2C_FUNC_SMBUS_WRITE_BYTE_DATA, "write byte data"); err != nil {
return err
}
data := val
return d.smbusAccess(address, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, unsafe.Pointer(&data))
}
// WriteWordData writes the given 16 bit value starting from the given register of an i2c device.
func (d *i2cDevice) WriteWordData(address int, reg uint8, val uint16) error {
d.mutex.Lock()
defer d.mutex.Unlock()
if err := d.queryFunctionality(I2C_FUNC_SMBUS_WRITE_WORD_DATA, "write word data"); err != nil {
return err
}
data := val
return d.smbusAccess(address, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, unsafe.Pointer(&data))
}
// WriteBlockData writes the given buffer starting from the given register of an i2c device.
func (d *i2cDevice) WriteBlockData(address int, reg uint8, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
dataLen := len(data)
if dataLen > 32 {
return fmt.Errorf("Writing blocks larger than 32 bytes (%v) not supported", len(data))
}
if err := d.queryFunctionality(I2C_FUNC_SMBUS_WRITE_I2C_BLOCK, "write i2c block"); err != nil {
if i2cDeviceDebug {
log.Printf("%s, use fallback\n", err.Error())
}
return d.writeBlockDataFallback(address, reg, data)
}
// set the first element with the data size
buf := make([]byte, dataLen+1)
buf[0] = byte(dataLen)
copy(buf[1:], data)
return d.smbusAccess(address, I2C_SMBUS_WRITE, reg, I2C_SMBUS_I2C_BLOCK_DATA, unsafe.Pointer(&buf[0]))
}
// WriteBytes writes the given buffer starting from the current register of an i2c device.
func (d *i2cDevice) WriteBytes(address int, data []byte) error {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.writeBytes(address, data)
}
// Read implements direct I2C read operations.
func (d *i2cDevice) Read(address int, b []byte) (n int, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.read(address, b)
}
// Write implements the io.ReadWriteCloser method by direct I2C write operations.
func (d *i2cDevice) Write(address int, b []byte) (n int, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.write(address, b)
}
func (d *i2cDevice) readBlockDataFallback(address int, reg uint8, data []byte) error {
if err := d.writeBytes(address, []byte{reg}); err != nil {
return err
}
if err := d.readAndCheckCount(address, data); err != nil {
return err
}
return nil
}
func (d *i2cDevice) writeBlockDataFallback(address int, reg uint8, data []byte) error {
buf := make([]byte, len(data)+1)
copy(buf[1:], data)
buf[0] = reg
if err := d.writeBytes(address, buf); err != nil {
return err
}
return nil
}
func (d *i2cDevice) writeBytes(address int, data []byte) error {
n, err := d.write(address, data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("Write %v bytes to device by sysfs, expected %v", n, len(data))
}
return nil
}
func (d *i2cDevice) write(address int, b []byte) (n int, err error) {
if err = d.setAddress(address); err != nil {
return 0, err
}
if err := d.openFileLazy("Write"); err != nil {
return 0, err
}
return d.file.Write(b)
}
func (d *i2cDevice) readAndCheckCount(address int, data []byte) error {
n, err := d.read(address, data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("Read %v bytes from device by sysfs, expected %v", n, len(data))
}
return nil
}
func (d *i2cDevice) read(address int, b []byte) (n int, err error) {
if err = d.setAddress(address); err != nil {
return 0, err
}
if err := d.openFileLazy("Read"); err != nil {
return 0, err
}
return d.file.Read(b)
}
func (d *i2cDevice) queryFunctionality(requested uint64, sender string) error {
// lazy initialization
if d.funcs == 0 {
if err := d.syscallIoctl(I2C_FUNCS, unsafe.Pointer(&d.funcs), "Querying functionality"); err != nil {
return err
}
}
if d.funcs&requested == 0 {
return fmt.Errorf("SMBus %s not supported", sender)
}
return nil
}
func (d *i2cDevice) smbusAccess(address int, readWrite byte, command byte, protocol uint32, dataStart unsafe.Pointer) error {
if err := d.setAddress(address); err != nil {
return err
}
smbus := i2cSmbusIoctlData{
readWrite: readWrite,
command: command,
protocol: protocol,
data: dataStart, // the reflected value of unsafePointer equals uintptr(dataStart),
}
sender := fmt.Sprintf("SMBus access r/w: %d, command: %d, protocol: %d, address: %d", readWrite, command, protocol, d.lastAddress)
if err := d.syscallIoctl(I2C_SMBUS, unsafe.Pointer(&smbus), sender); err != nil {
return err
}
return nil
}
// setAddress sets the address of the i2c device to use.
func (d *i2cDevice) setAddress(address int) error {
if d.lastAddress == address && !forceSetAddress {
if i2cDeviceDebug {
log.Printf("I2C address %d was already sent - skip", address)
}
return nil
}
// for go vet false positives, see: https://github.com/golang/go/issues/41205
if err := d.syscallIoctl(I2C_SLAVE, unsafe.Pointer(uintptr(byte(address))), "Setting address"); err != nil {
return err
}
d.lastAddress = address
return nil
}
func (d *i2cDevice) syscallIoctl(signal uintptr, payload unsafe.Pointer, sender string) (err error) {
if err := d.openFileLazy(sender); err != nil {
return err
}
if _, _, errno := d.sys.syscall(Syscall_SYS_IOCTL, d.file, signal, payload); errno != 0 {
return fmt.Errorf("%s failed with syscall.Errno %v", sender, errno)
}
return nil
}
func (d *i2cDevice) openFileLazy(sender string) (err error) { //nolint:unparam // useful for debugging
// lazy initialization
// note: "os.ModeExclusive" is undefined without create the file. This means for the existing character device,
// a second open will not return an error e.g. due to a busy resource, so most likely "os.ModeExclusive" is not really
// helpful and we drop it to the default "0" used by normal Open().
if d.file == nil {
if d.file, err = d.fs.openFile(d.location, os.O_RDWR, 0); err != nil {
return err
}
}
return nil
}