forked from google/periph
/
spi.go
198 lines (176 loc) · 4.46 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
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
// 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.
// Specification
//
// Motorola never published a proper specification.
// http://electronics.stackexchange.com/questions/30096/spi-specifications
// http://www.nxp.com/files/microcontrollers/doc/data_sheet/M68HC11E.pdf page 120
// http://www.st.com/content/ccc/resource/technical/document/technical_note/58/17/ad/50/fa/c9/48/07/DM00054618.pdf/files/DM00054618.pdf/jcr:content/translations/en.DM00054618.pdf
package bitbang
import (
"errors"
"fmt"
"sync"
"time"
"periph.io/x/periph/conn"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/host/cpu"
)
// SPI represents a SPI master implemented as bit-banging on 3 or 4 GPIO pins.
type SPI struct {
sck gpio.PinOut // Clock
sdi gpio.PinIn // MISO
sdo gpio.PinOut // MOSI
csn gpio.PinOut // CS
mu sync.Mutex
maxHzPort int64
maxHzDev int64
mode spi.Mode
bits int
halfCycle time.Duration
}
func (s *SPI) String() string {
return fmt.Sprintf("bitbang/spi(%s, %s, %s, %s)", s.sck, s.sdi, s.sdo, s.csn)
}
// Close implements spi.ConnCloser.
func (s *SPI) Close() error {
return nil
}
// Duplex implements spi.Conn.
func (s *SPI) Duplex() conn.Duplex {
// Maybe implement bitbanging SPI only in half mode?
return conn.Full
}
// LimitSpeed implements spi.ConnCloser.
func (s *SPI) LimitSpeed(maxHz int64) error {
if maxHz <= 0 {
return errors.New("bitbang-spi: invalid maxHz")
}
s.mu.Lock()
defer s.mu.Unlock()
s.maxHzPort = maxHz
if s.maxHzDev == 0 || s.maxHzPort < s.maxHzDev {
s.halfCycle = time.Second / time.Duration(maxHz) / time.Duration(2)
}
return nil
}
// Connect implements spi.Conn.
func (s *SPI) Connect(maxHz int64, mode spi.Mode, bits int) error {
if maxHz < 0 {
return errors.New("bitbang-spi: invalid maxHz")
}
if mode != spi.Mode3 {
return fmt.Errorf("bitbang-spi: mode %v is not implemented", mode)
}
s.mu.Lock()
defer s.mu.Unlock()
s.maxHzDev = maxHz
if s.maxHzDev != 0 && (s.maxHzPort == 0 || s.maxHzDev < s.maxHzPort) {
s.halfCycle = time.Second / time.Duration(maxHz) / time.Duration(2)
}
s.mode = mode
s.bits = bits
return nil
}
// Tx implements spi.Conn.
//
// BUG(maruel): Implement mode.
// BUG(maruel): Implement bits.
// BUG(maruel): Test if read works.
func (s *SPI) Tx(w, r []byte) error {
if len(r) != 0 && len(w) != len(r) {
return errors.New("bitbang-spi: write and read buffers must be the same length")
}
s.mu.Lock()
defer s.mu.Unlock()
if s.csn != nil {
s.csn.Out(gpio.Low)
s.sleepHalfCycle()
}
for i := uint(0); i < uint(len(w)*8); i++ {
s.sdo.Out(w[i/8]&(1<<(i%8)) != 0)
s.sleepHalfCycle()
s.sck.Out(gpio.Low)
s.sleepHalfCycle()
if len(r) != 0 {
if s.sdi.Read() == gpio.High {
r[i/8] |= 1 << (i % 8)
}
}
s.sck.Out(gpio.Low)
}
if s.csn != nil {
s.csn.Out(gpio.High)
}
return nil
}
// TxPackets implements spi.Conn.
func (s *SPI) TxPackets(p []spi.Packet) error {
return errors.New("bitbang-spi: not implemented")
}
// Write implements io.Writer.
func (s *SPI) Write(d []byte) (int, error) {
if err := s.Tx(d, nil); err != nil {
return 0, err
}
return len(d), nil
}
// CLK implements spi.Pins.
func (s *SPI) CLK() gpio.PinOut {
return s.sck
}
// MOSI implements spi.Pins.
func (s *SPI) MOSI() gpio.PinOut {
return s.sdo
}
// MISO implements spi.Pins.
func (s *SPI) MISO() gpio.PinIn {
return s.sdi
}
// CS implements spi.Pins.
func (s *SPI) CS() gpio.PinOut {
return s.csn
}
// NewSPI returns an object that communicates SPI over 3 or 4 pins.
//
// BUG(maruel): Completely untested.
//
// cs can be nil.
func NewSPI(clk, mosi gpio.PinOut, miso gpio.PinIn, cs gpio.PinOut, speedHz int64) (*SPI, error) {
if err := clk.Out(gpio.High); err != nil {
return nil, err
}
if err := mosi.Out(gpio.High); err != nil {
return nil, err
}
if miso != nil {
if err := miso.In(gpio.PullUp, gpio.NoEdge); err != nil {
return nil, err
}
}
if cs != nil {
// Low means active.
if err := cs.Out(gpio.High); err != nil {
return nil, err
}
}
s := &SPI{
sck: clk,
sdi: miso,
sdo: mosi,
csn: cs,
mode: spi.Mode3,
bits: 8,
halfCycle: time.Second / time.Duration(speedHz) / time.Duration(2),
}
return s, nil
}
//
// sleep does a busy loop to act as fast as possible.
func (s *SPI) sleepHalfCycle() {
cpu.Nanospin(s.halfCycle)
}
var _ spi.Conn = &SPI{}
var _ fmt.Stringer = &SPI{}