forked from google/periph
/
serial.go
291 lines (251 loc) · 6.41 KB
/
serial.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
// 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 serial implements cross platform UART support exposed by the
// operating system.
//
// On POSIX, it is via devfs. On Windows, it is via Windows specific APIs.
package serial
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"syscall"
"periph.io/x/periph"
"periph.io/x/periph/conn"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/experimental/conn/uart"
)
// Enumerate returns the available serial buses as exposed by the OS.
//
// TODO(maruel): Port number are likely not useful, we need port names.
func Enumerate() ([]int, error) {
var out []int
if !isWindows {
// Do not use "/sys/class/tty/ttyS0/" as these are all owned by root.
prefix := "/dev/ttyS"
items, err := filepath.Glob(prefix + "*")
if err != nil {
return nil, err
}
out = make([]int, 0, len(items))
for _, item := range items {
i, err := strconv.Atoi(item[len(prefix):])
if err != nil {
continue
}
out = append(out, i)
}
}
return out, nil
}
func newPortDevFs(portNumber int) (*Port, error) {
// Use the devfs path for now.
name := fmt.Sprintf("ttyS%d", portNumber)
f, err := os.OpenFile("/dev/"+name, os.O_RDWR|syscall.O_NOCTTY, os.ModeExclusive)
if err != nil {
return nil, err
}
p := &Port{serialConn{name: name, f: f, portNumber: portNumber}}
return p, nil
}
// Port is an open serial port.
type Port struct {
conn serialConn
}
// Close implements uart.PortCloser.
func (p *Port) Close() error {
err := p.conn.f.Close()
p.conn.f = nil
return err
}
// String implements uart.Port.
func (p *Port) String() string {
return p.conn.String()
}
// Connect implements uart.Port.
func (p *Port) Connect(f physic.Frequency, stopBit uart.Stop, parity uart.Parity, flow uart.Flow, bits int) (conn.Conn, error) {
if f > physic.GigaHertz {
return nil, fmt.Errorf("sysfs-uart: invalid speed %s; maximum supported clock is 1GHz", f)
}
if f < 50*physic.Hertz {
return nil, fmt.Errorf("sysfs-uart: invalid speed %s; minimum supported clock is 50Hz; did you forget to multiply by physic.KiloHertz?", f)
}
if bits < 5 || bits > 8 {
return nil, fmt.Errorf("sysfs-uart: invalid bits %d; must be between 5 and 8", bits)
}
// Find the closest value in acceptedBauds.
baud := uint32(f / physic.Hertz)
//var op uint32
for _, line := range acceptedBauds {
if line[0] > baud {
break
}
//op = line[1]
}
p.conn.mu.Lock()
defer p.conn.mu.Unlock()
if p.conn.f == nil {
return nil, errors.New("sysfs-uart: already closed")
}
if p.conn.connected {
return nil, errors.New("sysfs-uart: already connected")
}
p.conn.freqConn = f
p.conn.bitsPerWord = uint8(bits)
if flow != uart.RTSCTS {
p.conn.muPins.Lock()
p.conn.rts = gpio.INVALID
p.conn.cts = gpio.INVALID
p.conn.muPins.Unlock()
}
// TODO(maruel): ioctl with flags and op.
return &p.conn, nil
}
// LimitSpeed implements uart.PortCloser.
func (p *Port) LimitSpeed(f physic.Frequency) error {
if f > physic.GigaHertz {
return fmt.Errorf("sysfs-uart: invalid speed %s; maximum supported clock is 1GHz", f)
}
if f < 50*physic.Hertz {
return fmt.Errorf("sysfs-uart: invalid speed %s; minimum supported clock is 50Hz; did you forget to multiply by physic.KiloHertz?", f)
}
p.conn.mu.Lock()
defer p.conn.mu.Unlock()
p.conn.freqPort = f
return nil
}
// RX implements uart.Pins.
func (p *Port) RX() gpio.PinIn {
return p.conn.RX()
}
// TX implements uart.Pins.
func (p *Port) TX() gpio.PinOut {
return p.conn.TX()
}
// RTS implements uart.Pins.
func (p *Port) RTS() gpio.PinOut {
return p.conn.RTS()
}
// CTS implements uart.Pins.
func (p *Port) CTS() gpio.PinIn {
return p.conn.CTS()
}
type serialConn struct {
// Immutable
name string
f *os.File
portNumber int
mu sync.Mutex
freqPort physic.Frequency // Frequency specified at LimitSpeed()
freqConn physic.Frequency // Frequency specified at Connect()
bitsPerWord uint8
connected bool
// Use a separate lock for the pins, so that they can be queried while a
// transaction is happening.
muPins sync.Mutex
rx gpio.PinIn
tx gpio.PinOut
rts gpio.PinOut
cts gpio.PinIn
}
// String implements conn.Conn.
func (s *serialConn) String() string {
return s.name
}
// Duplex implements conn.Conn.
func (s *serialConn) Duplex() conn.Duplex {
return conn.Full
}
// Read implements io.Reader.
func (s *serialConn) Read(b []byte) (int, error) {
return s.f.Read(b)
}
// Write implements io.Writer.
func (s *serialConn) Write(b []byte) (int, error) {
return s.f.Write(b)
}
// Tx implements conn.Conn.
func (s *serialConn) Tx(w, r []byte) error {
if len(w) != 0 {
if _, err := s.f.Write(w); err != nil {
return err
}
}
if len(r) != 0 {
_, err := s.f.Read(r)
return err
}
return nil
}
// RX implements uart.Pins.
func (s *serialConn) RX() gpio.PinIn {
s.initPins()
return s.rx
}
// TX implements uart.Pins.
func (s *serialConn) TX() gpio.PinOut {
s.initPins()
return s.tx
}
// RTS implements uart.Pins.
func (s *serialConn) RTS() gpio.PinOut {
s.initPins()
return s.rts
}
// CTS implements uart.Pins.
func (s *serialConn) CTS() gpio.PinIn {
s.initPins()
return s.cts
}
func (s *serialConn) initPins() {
s.muPins.Lock()
defer s.muPins.Unlock()
if s.rx != nil {
return
}
if s.rx = gpioreg.ByName(fmt.Sprintf("UART%d_RX", s.portNumber)); s.rx == nil {
s.rx = gpio.INVALID
}
if s.tx = gpioreg.ByName(fmt.Sprintf("UART%d_TX", s.portNumber)); s.tx == nil {
s.tx = gpio.INVALID
}
// s.rts is set to INVALID if no hardware RTS/CTS flow control is used.
if s.rts == nil {
if s.rts = gpioreg.ByName(fmt.Sprintf("UART%d_RTS", s.portNumber)); s.rts == nil {
s.rts = gpio.INVALID
}
if s.cts = gpioreg.ByName(fmt.Sprintf("UART%d_CTS", s.portNumber)); s.cts == nil {
s.cts = gpio.INVALID
}
}
}
//
// driverSerial implements periph.Driver.
type driverSerial struct {
}
func (d *driverSerial) String() string {
return "serial"
}
func (d *driverSerial) Prerequisites() []string {
return nil
}
func (d *driverSerial) After() []string {
return nil
}
func (d *driverSerial) Init() (bool, error) {
return true, nil
}
func init() {
periph.MustRegister(&drv)
}
var drv driverSerial
var _ uart.PortCloser = &Port{}
var _ uart.Pins = &Port{}
var _ conn.Conn = &serialConn{}
var _ uart.Pins = &serialConn{}