forked from google/periph
/
spitest.go
378 lines (315 loc) · 7.8 KB
/
spitest.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
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// Package spitest is meant to be used to test drivers over a fake SPI port.
package spitest
import (
"io"
"log"
"sync"
"periph.io/x/periph/conn"
"periph.io/x/periph/conn/conntest"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/spi"
)
// RecordRaw implements spi.PortCloser.
//
// It sends everything written to it to W.
type RecordRaw struct {
conntest.RecordRaw
Initialized bool
}
// NewRecordRaw is a shortcut to create a RecordRaw
func NewRecordRaw(w io.Writer) *RecordRaw {
return &RecordRaw{RecordRaw: conntest.RecordRaw{W: w}}
}
// Close is a no-op.
func (r *RecordRaw) Close() error {
r.Lock()
defer r.Unlock()
return nil
}
// LimitSpeed is a no-op.
func (r *RecordRaw) LimitSpeed(f physic.Frequency) error {
return nil
}
// Connect is a no-op.
func (r *RecordRaw) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
r.Lock()
defer r.Unlock()
if r.Initialized {
return nil, conntest.Errorf("spitest: Connect cannot be called twice")
}
r.Initialized = true
return &recordRawConn{r}, nil
}
type recordRawConn struct {
r *RecordRaw
}
func (r *recordRawConn) String() string {
return r.r.String()
}
func (r *recordRawConn) Tx(w, read []byte) error {
return r.r.Tx(w, read)
}
func (r *recordRawConn) Duplex() conn.Duplex {
return r.r.Duplex()
}
func (r *recordRawConn) TxPackets(p []spi.Packet) error {
return conntest.Errorf("spitest: TxPackets is not implemented")
}
//
// Record implements spi.PortCloser that records everything written to it.
//
// This can then be used to feed to Playback to do "replay" based unit tests.
type Record struct {
sync.Mutex
Port spi.PortCloser // Port can be nil if only writes are being recorded.
Ops []conntest.IO
Initialized bool
}
func (r *Record) String() string {
return "record"
}
// Close implements spi.PortCloser.
func (r *Record) Close() error {
if r.Port != nil {
return r.Port.Close()
}
return nil
}
// LimitSpeed implements spi.PortCloser.
func (r *Record) LimitSpeed(f physic.Frequency) error {
if r.Port != nil {
return r.Port.LimitSpeed(f)
}
return nil
}
// Connect implements spi.PortCloser.
func (r *Record) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
r.Lock()
defer r.Unlock()
if r.Initialized {
return nil, conntest.Errorf("spitest: Connect cannot be called twice")
}
r.Initialized = true
if r.Port != nil {
c, err := r.Port.Connect(f, mode, bits)
if err != nil {
return nil, err
}
return &recordConn{r, c}, nil
}
return &recordConn{r, nil}, nil
}
// CLK implements spi.Pins.
func (r *Record) CLK() gpio.PinOut {
if p, ok := r.Port.(spi.Pins); ok {
return p.CLK()
}
return gpio.INVALID
}
// MOSI implements spi.Pins.
func (r *Record) MOSI() gpio.PinOut {
if p, ok := r.Port.(spi.Pins); ok {
return p.MOSI()
}
return gpio.INVALID
}
// MISO implements spi.Pins.
func (r *Record) MISO() gpio.PinIn {
if p, ok := r.Port.(spi.Pins); ok {
return p.MISO()
}
return gpio.INVALID
}
// CS implements spi.Pins.
func (r *Record) CS() gpio.PinOut {
if p, ok := r.Port.(spi.Pins); ok {
return p.CS()
}
return gpio.INVALID
}
func (r *Record) txInternal(c spi.Conn, w, read []byte) error {
io := conntest.IO{}
if len(w) != 0 {
io.W = make([]byte, len(w))
copy(io.W, w)
}
r.Lock()
defer r.Unlock()
if r.Port == nil {
if len(read) != 0 {
return conntest.Errorf("spitest: read unsupported when no port is connected")
}
} else {
if err := c.Tx(w, read); err != nil {
return err
}
}
if len(read) != 0 {
io.R = make([]byte, len(read))
copy(io.R, read)
}
r.Ops = append(r.Ops, io)
return nil
}
//
type recordConn struct {
r *Record
c spi.Conn
}
func (r *recordConn) String() string {
return r.r.String()
}
func (r *recordConn) Duplex() conn.Duplex {
if r.c != nil {
return r.c.Duplex()
}
return conn.DuplexUnknown
}
func (r *recordConn) Tx(w, read []byte) error {
return r.r.txInternal(r.c, w, read)
}
// TxPackets is not yet implemented.
func (r *recordConn) TxPackets(p []spi.Packet) error {
return conntest.Errorf("spitest: TxPackets is not implemented")
}
// CLK implements spi.Pins.
func (r *recordConn) CLK() gpio.PinOut {
return r.r.CLK()
}
// MOSI implements spi.Pins.
func (r *recordConn) MOSI() gpio.PinOut {
return r.r.MOSI()
}
// MISO implements spi.Pins.
func (r *recordConn) MISO() gpio.PinIn {
return r.r.MISO()
}
// CS implements spi.Pins.
func (r *recordConn) CS() gpio.PinOut {
return r.r.CS()
}
//
// Playback implements spi.PortCloser and plays back a recorded I/O flow.
//
// While "replay" type of unit tests are of limited value, they still present
// an easy way to do basic code coverage.
type Playback struct {
conntest.Playback
CLKPin gpio.PinIO
MOSIPin gpio.PinIO
MISOPin gpio.PinIO
CSPin gpio.PinIO
Initialized bool
}
// Close implements spi.PortCloser.
//
// Close() verifies that all the expected Ops have been consumed.
func (p *Playback) Close() error {
return p.Playback.Close()
}
// LimitSpeed implements spi.PortCloser.
func (p *Playback) LimitSpeed(f physic.Frequency) error {
return nil
}
// Connect implements spi.PortCloser.
func (p *Playback) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
p.Lock()
defer p.Unlock()
if p.Initialized {
return nil, conntest.Errorf("spitest: Connect cannot be called twice")
}
p.Initialized = true
return &playbackConn{p}, nil
}
// CLK implements spi.Pins.
func (p *Playback) CLK() gpio.PinOut {
return p.CLKPin
}
// MOSI implements spi.Pins.
func (p *Playback) MOSI() gpio.PinOut {
return p.MOSIPin
}
// MISO implements spi.Pins.
func (p *Playback) MISO() gpio.PinIn {
return p.MISOPin
}
// CS implements spi.Pins.
func (p *Playback) CS() gpio.PinOut {
return p.CSPin
}
type playbackConn struct {
p *Playback
}
func (p *playbackConn) String() string {
return p.p.String()
}
func (p *playbackConn) Duplex() conn.Duplex {
return p.p.Duplex()
}
func (p *playbackConn) Tx(w, r []byte) error {
return p.p.Tx(w, r)
}
func (p *playbackConn) TxPackets(packets []spi.Packet) error {
return conntest.Errorf("spitest: TxPackets is not implemented")
}
func (p *playbackConn) CLK() gpio.PinOut {
return p.p.CLK()
}
func (p *playbackConn) MOSI() gpio.PinOut {
return p.p.MOSI()
}
func (p *playbackConn) MISO() gpio.PinIn {
return p.p.MISO()
}
func (p *playbackConn) CS() gpio.PinOut {
return p.p.CS()
}
//
// Log logs all operations done on an spi.PortCloser.
type Log struct {
spi.PortCloser
}
// Close implements spi.PortCloser.
func (l *Log) Close() error {
err := l.PortCloser.Close()
log.Printf("%s.Close() = %v", l.PortCloser, err)
return err
}
// LimitSpeed implements spi.PortCloser.
func (l *Log) LimitSpeed(f physic.Frequency) error {
err := l.PortCloser.LimitSpeed(f)
log.Printf("%s.LimitSpeed(%s) = %v", l.PortCloser, f, err)
return err
}
// Connect implements spi.PortCloser.
func (l *Log) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
c, err := l.PortCloser.Connect(f, mode, bits)
log.Printf("%s.Connect(%s, %d, %d) = %v", l.PortCloser, f, mode, bits, err)
return &LogConn{c}, err
}
//
// LogConn logs all operations done on an spi.Conn.
type LogConn struct {
spi.Conn
}
// Tx implements spi.Conn.
func (l *LogConn) Tx(w, r []byte) error {
err := l.Conn.Tx(w, r)
log.Printf("%s.Tx(%#v, %#v) = %v", l.Conn, w, r, err)
return err
}
// TxPackets is not yet implemented.
func (l *LogConn) TxPackets(p []spi.Packet) error {
return conntest.Errorf("spitest: TxPackets is not implemented")
}
//
var _ spi.PortCloser = &RecordRaw{}
var _ spi.PortCloser = &Record{}
var _ spi.PortCloser = &Playback{}
var _ spi.PortCloser = &Log{}
var _ spi.Pins = &Record{}
var _ spi.Pins = &Playback{}