-
Notifications
You must be signed in to change notification settings - Fork 1
/
goMAX31856.go
208 lines (162 loc) · 5.5 KB
/
goMAX31856.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
/*
Copyright (c) 2018 Forrest Sibley <My^Name^Without^The^[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package max31856
import (
//"sync"
"errors"
"fmt"
"time"
"github.com/mvpninjas/go-bitflag"
"github.com/the-sibyl/piSPI"
"github.com/the-sibyl/sysfsGPIO"
)
type MAX31856 struct {
dev *spi.Device
spidevPath string
spiClockSpeed int64
drdyTimeoutPeriod time.Duration
}
// Add functionality for DRDY pin
func Setup(spidevPath string, spiClockSpeed int64, drdyPin int, drdyTimeoutPeriod time.Duration) (MAX31856, error) {
m := MAX31856{
spidevPath: spidevPath,
spiClockSpeed: spiClockSpeed,
drdyTimeoutPeriod: drdyTimeoutPeriod,
}
o := spi.Devfs{
Dev: m.spidevPath,
Mode: spi.Mode1,
MaxSpeed: m.spiClockSpeed,
}
// TOOD: implement closing for this
dev, err := spi.Open(&o)
if err != nil {
return m, err
}
m.dev = dev
var cr0 bitflag.Flag
cr0.Set(CMODE)
cr0.Set(OCFAULT0)
cr0.Set(OCFAULT1)
m.SetFlags(CR0_WR, cr0)
// Get default values from the register
mask, err := m.GetFlags(MASK_RD)
if err != nil {
return m, err
}
// Masks are active low. Unset the flags for the signals that need to be considered as faults.
mask.Unset(OPEN)
mask.Unset(OVUV)
m.SetFlags(MASK_WR, mask)
// TODO: Add DRDY interrupt code
drdy, err := sysfsGPIO.InitPin(drdyPin, "in")
if err != nil {
return m, err
}
drdy.SetTriggerEdge("rising")
drdy.AddPinInterrupt()
// Do SOMETHING with the ISR........need to think this out!
return m, nil
}
// TODO: Implement fault register polling. The board that I have has the FAULT pin hardwired to an LED. I need to be certain that waiting for data from the chip will not end in a deadlock. It might be prudent to add a timeout.
// Intended to be placed into a Goroutine
func (m *MAX31856) GetTempAuto() {
// Todo: Implement an output channel for data and an input channel to exit or perform flow control
// Step 1: Set up the chip for auto-capture
// Step 2: Wait for DRDY interrupt
// Step 3: Push new data onto the channel
// Step 4: Unset the auto-capture
}
// Intended to be called once per measurement
func (m *MAX31856) GetTempOnce() (float32, error) {
// Step 1: Request temperature
temperature, err := m.getTemp()
// Step 2: Wait for DRDY interrupt
// Step 3: Get data off SPI bus
// Step 4: Check for faults ?
// Step 5: Return data
return temperature, err
}
// Internal function to get temperature. Return a float32 containing the temperature in degrees centigrade.
func (m *MAX31856) getTemp() (float32, error) {
readValue := make([]byte, 4)
dataReady := make(chan bool, 1)
// Wait for the DRDY bit to go high
go func() {
dataReady <- true
}()
// TODO: Add code to actually check for DRDY. It is bypassed here.
select {
case <-time.After(m.drdyTimeoutPeriod):
return 0, errors.New("Timeout Error")
case <-dataReady:
fmt.Println("Data is ready")
}
m.dev.SetCSChange(false)
// Read 0xC, 0xD, 0xE. The address auto-increments on the chip.
m.dev.Tx([]byte{
0xC, 0x0, 0x0, 0x0,
}, readValue)
// Discard the first byte, save the rest, and shift them to their proper positions. The data are in two's
// complement, and the math here works out nicely.
temp := int16(readValue[1])<<8 | int16(readValue[2])
linearTempDegC := float32(temp) * 0.0625
return linearTempDegC, nil
}
// Read from the Fault Status Register and return a simple binary result
func (m *MAX31856) CheckForFaults() (bool, error) {
faultFlags, err := m.GetFlags(SR_RD)
fault := faultFlags != 0
return fault, err
}
// Reset the faults register
func (m *MAX31856) ResetFaults() error {
// Read the CR0 register
cr0, err := m.GetFlags(CR0_RD)
if err != nil {
return err
}
// Set the FAULTCLR bit in CR0
cr0.Set(FAULTCLR)
// Write back out to CR0
err = m.SetFlags(CR0_WR, cr0)
return err
}
// Write to a register using a bitflag.Flag type for convenience
func (m *MAX31856) SetFlags(address byte, value bitflag.Flag) error {
if address < 0x80 || address > 0x8B {
return errors.New("Invalid write address")
}
m.dev.SetCSChange(false)
m.dev.Tx([]byte{address, byte(value)}, nil)
fmt.Println([]byte{address, byte(value)})
fmt.Println("Writing config")
return nil
}
// Get register values and store them in a bitflag.Flag type for convenience
func (m *MAX31856) GetFlags(address byte) (bitflag.Flag, error) {
if address >= 0x80 && address <= 0x8B {
return 0, errors.New("Invalid read address")
}
readValue := make([]byte, 2)
m.dev.SetCSChange(false)
m.dev.Tx([]byte{address, byte(0)}, readValue)
return bitflag.Flag(readValue[1]), nil
}