Skip to content

Commit d3747ad

Browse files
committed
spi: bit bang read ids, memory
1 parent 2efc679 commit d3747ad

File tree

1 file changed

+75
-18
lines changed

1 file changed

+75
-18
lines changed

src/main/scala/spi/SerialSpiTest.scala

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,101 @@ object SerialSpiTest extends App {
2525
}
2626
}
2727
// print("Received: " + ret)
28-
Thread.sleep(300)
28+
// Thread.sleep(100)
2929
ret
3030
}
3131

32-
def writeByte(v: Int) = {
32+
def setCmd(id: Int, v: Int): String = {
33+
val cmd = v.toHexString
34+
id match {
35+
case 0 => "w44" + cmd + "\r"
36+
case 1 => "w4" + cmd + "4\r"
37+
case 2 => "w" + cmd + "44\r"
38+
}
39+
}
40+
41+
def writeByte(id: Int, v: Int) = {
3342
for (i <- 0 until 8) {
3443
val bits = ((v >> (7-i)) & 1) << 1
3544
// clock off, set data
36-
writeRead(s"w44$bits\r")
45+
writeRead(setCmd(id, bits)) // clock off, set data
3746
// clock on, keep data
38-
writeRead(s"w44${bits + 1}\r")
47+
writeRead(setCmd(id, bits + 1)) // clock on, keep data
3948
}
4049
// not now, writeRead("w440\r") // clock off
4150
}
4251

43-
def readByte() = {
52+
def readByte(id: Int) = {
4453
var v = 0
4554
for (i <- 0 until 8) {
46-
writeRead("w440\r") // clock off
55+
writeRead(setCmd(id, 0)) // clock off
4756
// data changes after neg edge
48-
writeRead("w441\r") // clock on
57+
writeRead(setCmd(id, 1)) // clock on
4958
// sample on pos edge
5059
val rx = writeRead("r")
5160
// println("received: " + rx(8))
5261
// '8' is bit set
53-
val bit = if (rx(8) == '8') 1 else 0
62+
val bit = if (rx(8 - id) == '8') 1 else 0
5463
v = (v << 1) | bit
5564
}
56-
writeRead("w440\r") // clock off (maybe?), does not heart on multibyte read
65+
writeRead(setCmd(id, 0)) // clock off (maybe?), does not hurt on multibyte read
5766
v
5867
}
5968

6069
def readAdx(cmd: Int): Int = {
61-
writeRead("w444\r")
62-
writeRead("w440\r")
63-
writeByte(cmd)
64-
writeByte(0)
65-
val ret = readByte()
66-
writeRead("w444\r")
70+
writeRead(setCmd(0, 4))
71+
writeRead(setCmd(0, 0)) // CS low
72+
writeByte(0, cmd)
73+
writeByte(0, 0)
74+
val ret = readByte(0)
75+
writeRead(setCmd(0, 4)) // CS high
6776
ret
6877
}
6978

79+
def readJedecId(id: Int) = {
80+
writeRead(setCmd(id, 0)) // CS low
81+
writeByte(id, 0x9f)
82+
val v = readByte(id)
83+
println("Manufacturer is 0x" + v.toHexString)
84+
println("Device type is 0x" + readByte(id).toHexString)
85+
println("Device id is 0x" + readByte(id).toHexString)
86+
writeRead(setCmd(id, 4)) // CS high
87+
v
88+
}
89+
90+
def readStatusRegister(id: Int) = {
91+
writeRead(setCmd(id, 0)) // CS low
92+
writeByte(id, 0x05)
93+
val v = readByte(id)
94+
println("Status register is 0x" + v.toHexString)
95+
writeRead(setCmd(id, 4)) // CS high
96+
v
97+
}
98+
99+
def readMemory(id: Int, addr: Int) = {
100+
writeRead(setCmd(id, 0)) // CS low
101+
writeByte(id, 0x03)
102+
writeByte(id, (addr >> 16) & 0xff)
103+
writeByte(id, (addr >> 8) & 0xff)
104+
writeByte(id, addr & 0xff)
105+
val v = readByte(id)
106+
println("Memory of " + id + " at 0x" + addr.toHexString + " is 0x" + v.toHexString)
107+
writeRead(setCmd(id, 4)) // CS high
108+
v
109+
}
110+
111+
def readSram(id: Int, addr: Int) = {
112+
writeRead(setCmd(id, 0)) // CS low
113+
writeByte(id, 0x03)
114+
writeByte(id, (addr >> 8) & 0xff)
115+
writeByte(id, addr & 0xff)
116+
val v = readByte(id)
117+
println("SRAM of " + id + " at 0x" + addr.toHexString + " is 0x" + v.toHexString)
118+
writeRead(setCmd(id, 4)) // CS high
119+
v
120+
}
121+
122+
70123
// TODO: fix this hard coded thing
71124
val port = SerialPort.getCommPort("/dev/tty.usbserial-210292B408601")
72125
port.openPort()
@@ -75,12 +128,16 @@ object SerialSpiTest extends App {
75128
port.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 0, 0)
76129
val out = port.getOutputStream
77130

78-
print(writeRead("w444\r"))
79-
print(writeRead("w440\r"))
131+
print(writeRead(setCmd(0, 4))) // all CS high
132+
print(writeRead(setCmd(0, 0))) // CS low for ADX
80133
print(writeRead("r"))
81134
val v = readAdx(0x0b) // dev id
82135
println("device id is 0x" + v.toHexString)
83-
writeRead("w444\r")
136+
readJedecId(1) // Flash
137+
readStatusRegister(2) // SRAM
138+
readMemory(1, 0)
139+
readSram(2, 0)
140+
print(writeRead(setCmd(0, 4))) // all CS high
84141
out.close()
85142
port.closePort()
86143
}

0 commit comments

Comments
 (0)