forked from google/periph
/
main.go
67 lines (61 loc) · 1.4 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
// 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.
// spi-list lists all SPI ports.
package main
import (
"fmt"
"os"
"periph.io/x/periph/conn/pin"
"periph.io/x/periph/conn/pin/pinreg"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spireg"
"periph.io/x/periph/host"
)
func printPin(fn string, p pin.Pin) {
name, pos := pinreg.Position(p)
if name != "" {
fmt.Printf(" %-4s: %-10s found on header %s, #%d\n", fn, p, name, pos)
} else {
fmt.Printf(" %-4s: %-10s\n", fn, p)
}
}
func mainImpl() error {
if _, err := host.Init(); err != nil {
return err
}
for _, ref := range spireg.All() {
fmt.Printf("%s", ref.Name)
if ref.Number != -1 {
fmt.Printf(" #%d", ref.Number)
}
fmt.Print(":\n")
if len(ref.Aliases) != 0 {
fmt.Printf(" Aliases:\n")
for _, a := range ref.Aliases {
fmt.Printf(" %s\n", a)
}
}
s, err := ref.Open()
if err != nil {
fmt.Printf(" Failed to open: %v\n", err)
continue
}
if p, ok := s.(spi.Pins); ok {
printPin("CLK", p.CLK())
printPin("MOSI", p.MOSI())
printPin("MISO", p.MISO())
printPin("CS", p.CS())
}
if err := s.Close(); err != nil {
return err
}
}
return nil
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "spi-list: %s.\n", err)
os.Exit(1)
}
}