-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
150 lines (140 loc) · 3.08 KB
/
main.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
// Copyright 2017 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.
// spi-io writes to an SPI port data from stdin and outputs to stdout or writes
// arguments and outputs hex encoded output.
//
// Usage:
//
// echo -n -e '\x88\x00' | spi-io -b SPI0.0 | hexdump
// spi-io -b SPI0.0 0x88 0
//
// For "read only" operation, writes zeros.
// For "write only" operation, ignore stdout.
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/host/v3"
)
// runTx does the I/O.
//
// If you find yourself with the need to do a one-off complicated transaction
// using TxPackets, temporarily override this function.
func runTx(s spi.Conn, args []string) error {
hex := false
var write []byte
var err error
if len(args) == 0 {
write, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
} else {
hex = true
for _, b := range args {
i := uint64(0)
if i, err = strconv.ParseUint(b, 0, 8); err != nil {
return err
}
write = append(write, byte(i))
}
}
read := make([]byte, len(write))
if err = s.Tx(write, read); err != nil {
return err
}
if !hex {
_, err = os.Stdout.Write(read)
} else {
for i, b := range read {
if i != 0 {
if _, err = fmt.Print(", "); err != nil {
break
}
}
if _, err = fmt.Printf("0x%02X", b); err != nil {
break
}
}
_, err = fmt.Print("\n")
}
return err
// Sample custom testing:
/*
p := []spi.Packet{
{
W: []byte{0x01},
KeepCS: true,
},
{
R: make([]byte, 16),
},
}
return s.TxPackets(p)
*/
}
func mainImpl() error {
spiID := flag.String("b", "", "SPI port to use")
hz := physic.MegaHertz
flag.Var(&hz, "hz", "SPI port speed")
nocs := flag.Bool("nocs", false, "do not assert the CS line")
half := flag.Bool("half", false, "half duplex mode, sharing MOSI and MISO")
lsbfirst := flag.Bool("lsb", false, "lsb first (default is msb)")
mode := flag.Int("mode", 0, "CLK and data polarity, between 0 and 3")
bits := flag.Int("bits", 8, "bits per word")
verbose := flag.Bool("v", false, "verbose mode")
flag.Parse()
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.Lmicroseconds)
if *mode < 0 || *mode > 3 {
return errors.New("invalid mode")
}
if *bits < 1 || *bits > 255 {
return errors.New("invalid bits")
}
m := spi.Mode(*mode)
if *half {
m |= spi.HalfDuplex
}
if *nocs {
m |= spi.NoCS
}
if *lsbfirst {
m |= spi.LSBFirst
}
if _, err := host.Init(); err != nil {
return err
}
s, err := spireg.Open(*spiID)
if err != nil {
return err
}
defer s.Close()
c, err := s.Connect(hz, m, *bits)
if err != nil {
return err
}
if *verbose {
if p, ok := c.(spi.Pins); ok {
log.Printf("Using pins CLK: %s MOSI: %s MISO: %s", p.CLK(), p.MOSI(), p.MISO())
}
}
return runTx(c, flag.Args())
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "spi-io: %s.\n", err)
os.Exit(1)
}
}