8
8
//
9
9
// spidev [OPTIONS] raw < tx.bin > rx.bin
10
10
// spidev [OPTIONS] sfdp
11
+ // spidev [OPTIONS] id
11
12
//
12
13
// Options:
13
14
//
19
20
// raw: The binary data from stdin is transmitted over the SPI bus.
20
21
// Received data is printed to stdout.
21
22
// sfdp: Parse and print the parameters in the SFDP.
23
+ // id: print the 3 byte hex id
22
24
package main
23
25
24
26
import (
@@ -58,13 +60,13 @@ func run(args []string, spiOpen spiOpenFunc, input io.Reader, output io.Writer)
58
60
speed := fs .Uint32P ("speed" , "s" , 500000 , "max speed in Hz" )
59
61
if err := fs .Parse (args ); err != nil {
60
62
if err == flag .ErrHelp {
61
- return fmt .Errorf ("%w:<raw|sfdp>" , errCommand )
63
+ return fmt .Errorf ("%w:<raw|sfdp|id >" , errCommand )
62
64
}
63
65
return fmt .Errorf ("%w:%v" , errFlag , err )
64
66
}
65
67
66
68
if fs .NArg () != 1 {
67
- return fmt .Errorf ("%w: %s <raw|sfdp>" , errCommand , fs .FlagUsages ())
69
+ return fmt .Errorf ("%w: %s <raw|sfdp|id >" , errCommand , fs .FlagUsages ())
68
70
}
69
71
70
72
// Open the spi device.
@@ -80,6 +82,35 @@ func run(args []string, spiOpen spiOpenFunc, input io.Reader, output io.Writer)
80
82
cmd := fs .Arg (0 )
81
83
// Currently, only the raw subcommand is supported.
82
84
switch cmd {
85
+ case "id" :
86
+ // Wake it up, then get the id.
87
+ // 0xab is not universally handled on all devices, but that's ok.
88
+ // but CE MUST drop, so we structure this as two separate
89
+ // transfers to ensure that happens.
90
+ transfers := []spidev.Transfer {
91
+ {
92
+ Tx : []byte {0xab },
93
+ Rx : make ([]byte , 1 ),
94
+ },
95
+ }
96
+
97
+ // Perform transfers.
98
+ if err := s .Transfer (transfers ); err != nil {
99
+ return err
100
+ }
101
+ var id = []byte {0x9f , 0 , 0 , 0 }
102
+ transfers = []spidev.Transfer {
103
+ {
104
+ Tx : []byte {0x9f , 0 , 0 , 0 },
105
+ Rx : id ,
106
+ },
107
+ }
108
+ if err := s .Transfer (transfers ); err != nil {
109
+ return err
110
+ }
111
+ fmt .Printf ("%02x\n " , id [1 :])
112
+ return nil
113
+
83
114
case "raw" :
84
115
// Create transfer from stdin.
85
116
tx , err := io .ReadAll (input )
0 commit comments