8000 add and ID command to spidev · u-root/u-root@9a9baa5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a9baa5

Browse files
committed
add and ID command to spidev
Tested on an rpi4 with qspimux. Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
1 parent d9fb290 commit 9a9baa5

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

cmds/fwtools/spidev/spidev_linux.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
// spidev [OPTIONS] raw < tx.bin > rx.bin
1010
// spidev [OPTIONS] sfdp
11+
// spidev [OPTIONS] id
1112
//
1213
// Options:
1314
//
@@ -19,6 +20,7 @@
1920
// raw: The binary data from stdin is transmitted over the SPI bus.
2021
// Received data is printed to stdout.
2122
// sfdp: Parse and print the parameters in the SFDP.
23+
// id: print the 3 byte hex id
2224
package main
2325

2426
import (
@@ -58,13 +60,13 @@ func run(args []string, spiOpen spiOpenFunc, input io.Reader, output io.Writer)
5860
speed := fs.Uint32P("speed", "s", 500000, "max speed in Hz")
5961
if err := fs.Parse(args); err != nil {
6062
if err == flag.ErrHelp {
61-
return fmt.Errorf("%w:<raw|sfdp>", errCommand)
63+
return fmt.Errorf("%w:<raw|sfdp|id>", errCommand)
6264
}
6365
return fmt.Errorf("%w:%v", errFlag, err)
6466
}
6567

6668
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())
6870
}
6971

7072
// Open the spi device.
@@ -80,6 +82,35 @@ func run(args []string, spiOpen spiOpenFunc, input io.Reader, output io.Writer)
8082
cmd := fs.Arg(0)
8183
// Currently, only the raw subcommand is supported.
8284
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+
83114
case "raw":
84115
// Create transfer from stdin.
85116
tx, err := io.ReadAll(input)

0 commit comments

Comments
 (0)
0