-
Notifications
You must be signed in to change notification settings - Fork 9
/
spi.go
165 lines (130 loc) · 3.75 KB
/
spi.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
// MCP2515 Stand-Alone CAN Interface
package mcp2515
import (
"fmt"
"github.com/golang/glog"
"time"
)
func (d *MCP2515) writeRegister(register string, data ...uint8) error {
address, err := registerAddress(register)
if err != nil {
return err
}
glog.V(2).Infof("mcp2515: writeRegister %v=%v", register, data)
command := []uint8{commands["WRITE"], address}
buffer := append(command, data...)
return d.Bus.TransferAndReceiveData(buffer)
}
func (d *MCP2515) readRegister(register string, length int) ([]uint8, error) {
address, err := registerAddress(register)
if err != nil {
return nil, err
}
command := []uint8{commands["READ"], address}
buffer := make([]uint8, len(command) + length)
copy(buffer, command)
err = d.Bus.TransferAndReceiveData(buffer)
data := buffer[len(command):]
glog.V(2).Infof("mcp2515: readRegister %v=%v", register, data)
return data, err
}
func (d *MCP2515) readStatus() (uint8, error) {
command := []uint8{commands["READ_STATUS"]}
buffer := make([]uint8, len(command) + 1)
copy(buffer, command)
err := d.Bus.TransferAndReceiveData(buffer)
data := buffer[len(command)]
glog.V(4).Infof("mcp2515: readStatus=%v", data)
return data, err
}
func (d *MCP2515) reset() error {
glog.V(2).Infof("mcp2515: reset")
buffer := []uint8{commands["RESET"]}
return d.Bus.TransferAndReceiveData(buffer)
}
func (d *MCP2515) checkFreeBuffer() bool {
bufferFulls := uint8((1 << statusBits["TX0REQ"]) |
(1 << statusBits["TX1REQ"]) |
(1 << statusBits["TX2REQ"]))
status, err := d.readStatus()
return err == nil && status&bufferFulls != bufferFulls
}
func (d *MCP2515) receiveMessage(rxBuffer uint8) (*Message, error) {
glog.V(2).Infof("mcp2515: receive")
commandName := "READ_RX0"
if rxBuffer == 1 {
commandName = "READ_RX1"
}
command := []uint8{commands[commandName]}
// 4 bytes for id, 1 byte for length, 8 bytes for data
buffer := make([]uint8, len(command) + 13)
copy(buffer, command)
err := d.Bus.TransferAndReceiveData(buffer)
if err != nil {
return nil, err
}
data := buffer[len(command):]
message := Message{
Id: 0,
Extended: (data[1] & (1 << bits["IDE"])) != 0,
Length: (data[4] & 0xF),
Time: time.Now(),
}
if message.Extended {
// 29 bit extended identifier
message.Id = (uint32(data[0]) << 21) |
(uint32(data[1]&0xE0) << 13) |
(uint32(data[2]&0x03) << 16) |
uint32(data[3])
} else {
// standard 11 bit identifier
message.Id = (uint32(data[0]) << 3) |
(uint32(data[1]) >> 5)
}
copy(message.Data[:], data[5:])
return &message, nil
}
func (d *MCP2515) transmitMessage(txBuffer uint8, message *Message) error {
glog.V(2).Infof("mcp2515: transmit")
commandName := "WRITE_TX0"
if txBuffer == 1 {
commandName = "WRITE_TX1"
} else if txBuffer == 2 {
commandName = "WRITE_TX2"
}
command := []uint8{commands[commandName]}
// 4 bytes for id, 1 byte for length, 8 bytes for data
data := make([]uint8, 13)
if message.Extended {
data[0] = uint8(message.Id >> 21)
data[1] = (uint8(message.Id>>13) & 0xe0) |
(1 << bits["EXIDE"]) |
(uint8(message.Id>>16) & 0x03)
data[2] = uint8(message.Id >> 8)
data[3] = uint8(message.Id)
} else {
data[0] = uint8(message.Id >> 3)
data[1] = uint8(message.Id << 5)
}
data[4] = message.Length
copy(data[5:], message.Data[:])
buffer := append(command, data...)
err := d.Bus.TransferAndReceiveData(buffer)
if err != nil {
return err
}
// Initiate transmission
command = []uint8{commands["RTS"] | (1<<txBuffer)}
return d.Bus.TransferAndReceiveData(command)
}
func registerAddress(register string) (uint8, error) {
address, ok := registers[register]
if ok {
return address, nil
} else {
return 0, fmt.Errorf("Register doesn't exist %v", register)
}
}
func isBitSet(data uint8, bit uint8) bool {
return (data & (1 << bit)) != 0
}