forked from talkkonnect/go-i2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.go
244 lines (219 loc) · 6.42 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
// Package i2c provides low level control over the linux i2c bus.
//
// Before usage you should load the i2c-dev kernel module
//
// sudo modprobe i2c-dev
//
// Each i2c bus can address 127 independent i2c devices, and most
// linux systems contain several buses.
package i2c
import (
"encoding/hex"
"fmt"
"os"
"syscall"
)
const (
I2C_SLAVE = 0x0703
)
// I2C represents a connection to I2C-device.
type I2C struct {
addr uint8
bus int
rc *os.File
}
// NewI2C opens a connection for I2C-device.
// SMBus (System Management Bus) protocol over I2C
// supported as well: you should preliminary specify
// register address to read from, either write register
// together with the data in case of write operations.
func NewI2C(addr uint8, bus int) (*I2C, error) {
f, err := os.OpenFile(fmt.Sprintf("/dev/i2c-%d", bus), os.O_RDWR, 0600)
if err != nil {
return nil, err
}
if err := ioctl(f.Fd(), I2C_SLAVE, uintptr(addr)); err != nil {
return nil, err
}
v := &I2C{rc: f, bus: bus, addr: addr}
return v, nil
}
// GetBus return bus line, where I2C-device is allocated.
func (v *I2C) GetBus() int {
return v.bus
}
// GetBus return device occupied address in the bus.
func (v *I2C) GetAddr() uint8 {
return v.addr
}
func (v *I2C) write(buf []byte) (int, error) {
return v.rc.Write(buf)
}
// Write sends bytes to the remote I2C-device. The interpretation of
// the message is implementation-dependant.
func (v *I2C) WriteBytes(buf []byte) (int, error) {
lg.Debugf("Write %d hex bytes: [%+v]", len(buf), hex.EncodeToString(buf))
return v.write(buf)
}
func (v *I2C) read(buf []byte) (int, error) {
return v.rc.Read(buf)
}
// ReadBytes reads bytes from I2C-device.
// Number of bytes read correspond to buf parameter length.
func (v *I2C) ReadBytes(buf []byte) (int, error) {
n, err := v.read(buf)
if err != nil {
return n, err
}
lg.Debugf("Read %d hex bytes: [%+v]", len(buf), hex.EncodeToString(buf))
return n, nil
}
// Close I2C-connection.
func (v *I2C) Close() error {
return v.rc.Close()
}
// ReadRegBytes read count of n byte's sequence from I2C-device
// starting from reg address.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) ReadRegBytes(reg byte, n int) ([]byte, int, error) {
lg.Debugf("Read %d bytes starting from reg 0x%0X...", n, reg)
_, err := v.WriteBytes([]byte{reg})
if err != nil {
return nil, 0, err
}
buf := make([]byte, n)
c, err := v.ReadBytes(buf)
if err != nil {
return nil, 0, err
}
return buf, c, nil
}
// ReadRegU8 reads byte from I2C-device register specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) ReadRegU8(reg byte) (byte, error) {
_, err := v.WriteBytes([]byte{reg})
if err != nil {
return 0, err
}
buf := make([]byte, 1)
_, err = v.ReadBytes(buf)
if err != nil {
return 0, err
}
lg.Debugf("Read U8 %d from reg 0x%0X", buf[0], reg)
return buf[0], nil
}
// WriteRegU8 writes byte to I2C-device register specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) WriteRegU8(reg byte, value byte) error {
buf := []byte{reg, value}
_, err := v.WriteBytes(buf)
if err != nil {
return err
}
lg.Debugf("Write U8 %d to reg 0x%0X", value, reg)
return nil
}
// ReadRegU16BE reads unsigned big endian word (16 bits)
// from I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) ReadRegU16BE(reg byte) (uint16, error) {
_, err := v.WriteBytes([]byte{reg})
if err != nil {
return 0, err
}
buf := make([]byte, 2)
_, err = v.ReadBytes(buf)
if err != nil {
return 0, err
}
w := uint16(buf[0])<<8 + uint16(buf[1])
lg.Debugf("Read U16 %d from reg 0x%0X", w, reg)
return w, nil
}
// ReadRegU16LE reads unsigned little endian word (16 bits)
// from I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) ReadRegU16LE(reg byte) (uint16, error) {
w, err := v.ReadRegU16BE(reg)
if err != nil {
return 0, err
}
// exchange bytes
w = (w&0xFF)<<8 + w>>8
return w, nil
}
// ReadRegS16BE reads signed big endian word (16 bits)
// from I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) ReadRegS16BE(reg byte) (int16, error) {
_, err := v.WriteBytes([]byte{reg})
if err != nil {
return 0, err
}
buf := make([]byte, 2)
_, err = v.ReadBytes(buf)
if err != nil {
return 0, err
}
w := int16(buf[0])<<8 + int16(buf[1])
lg.Debugf("Read S16 %d from reg 0x%0X", w, reg)
return w, nil
}
// ReadRegS16LE reads signed little endian word (16 bits)
// from I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) ReadRegS16LE(reg byte) (int16, error) {
w, err := v.ReadRegS16BE(reg)
if err != nil {
return 0, err
}
// exchange bytes
w = (w&0xFF)<<8 + w>>8
return w, nil
}
// WriteRegU16BE writes unsigned big endian word (16 bits)
// value to I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) WriteRegU16BE(reg byte, value uint16) error {
buf := []byte{reg, byte((value & 0xFF00) >> 8), byte(value & 0xFF)}
_, err := v.WriteBytes(buf)
if err != nil {
return err
}
lg.Debugf("Write U16 %d to reg 0x%0X", value, reg)
return nil
}
// WriteRegU16LE writes unsigned little endian word (16 bits)
// value to I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) WriteRegU16LE(reg byte, value uint16) error {
w := (value*0xFF00)>>8 + value<<8
return v.WriteRegU16BE(reg, w)
}
// WriteRegS16BE writes signed big endian word (16 bits)
// value to I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) WriteRegS16BE(reg byte, value int16) error {
buf := []byte{reg, byte((uint16(value) & 0xFF00) >> 8), byte(value & 0xFF)}
_, err := v.WriteBytes(buf)
if err != nil {
return err
}
lg.Debugf("Write S16 %d to reg 0x%0X", value, reg)
return nil
}
// WriteRegS16LE writes signed little endian word (16 bits)
// value to I2C-device starting from address specified in reg.
// SMBus (System Management Bus) protocol over I2C.
func (v *I2C) WriteRegS16LE(reg byte, value int16) error {
w := int16((uint16(value)*0xFF00)>>8) + value<<8
return v.WriteRegS16BE(reg, w)
}
func ioctl(fd, cmd, arg uintptr) error {
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, cmd, arg, 0, 0, 0)
if err != 0 {
return err
}
return nil
}