Skip to content

Commit 726de70

Browse files
committed
spi: controller can read JTAD ID from Flash
1 parent 1025c43 commit 726de70

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/main/scala/spi/SerialSpiTest.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class SerialSpiTest(id: Int, portName: String = "/dev/tty.usbserial-210292B40860
3434
}
3535
}
3636
// print("Received: " + ret)
37-
// Thread.sleep(100)
37+
38+
Thread.sleep(10)
3839
ret
3940
}
4041

@@ -203,7 +204,6 @@ class SerialSpiTest(id: Int, portName: String = "/dev/tty.usbserial-210292B40860
203204

204205
object SerialSpiTest extends App {
205206

206-
207207
val spi = new SerialSpiTest(1) // Flash
208208

209209
spi.csLow()
@@ -232,7 +232,6 @@ object SerialSpiTest extends App {
232232
}
233233
println()
234234
*/
235-
236235
print(spi.writeReadSerial(spi.setCmd(4))) // all CS high
237236
spi.out.close()
238237
spi.port.closePort()

src/main/scala/spi/SpiMaster.scala

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SpiMaster extends Module {
2121
})
2222

2323
object State extends ChiselEnum {
24-
val start, idle, tx1, tx2, rx1, rx2, done = Value
24+
val start, idle, tx1, tx2, rx1, rx2, done1, done2 = Value
2525
}
2626
import State._
2727
val state = RegInit(idle)
@@ -98,10 +98,10 @@ class SpiMaster extends Module {
9898
spi.sclk := 0.U
9999
cntReg := cntReg + 1.U
100100
when(cntReg === CNT_MAX) {
101-
misoReg := misoReg(7, 1) ## spi.miso
101+
misoReg := misoReg(6, 0) ## spi.miso
102+
printf(cf"HW Data is shifting in: $misoReg \n")
102103
state := rx2
103104
cntReg := 0.U
104-
bitsReg := bitsReg - 1.U
105105
}
106106
}
107107
is(rx2) {
@@ -113,17 +113,29 @@ class SpiMaster extends Module {
113113
cntReg := 0.U
114114
bitsReg := bitsReg - 1.U
115115
when(bitsReg === 0.U) {
116-
state := done
116+
state := done1
117117
}
118118
}
119119
}
120-
is(done) {
120+
is(done1) {
121+
printf("done\n")
122+
spi.ncs := 0.U
123+
spi.sclk := 0.U
124+
cntReg := cntReg + 1.U
125+
io.dataReady := true.B
126+
printf(cf"HW Data is $misoReg \n")
127+
when(cntReg === CNT_MAX) {
128+
state := done2
129+
cntReg := 0.U
130+
}
131+
}
132+
is(done2) {
121133
spi.ncs := 1.U
122134
spi.sclk := 0.U
123135
cntReg := cntReg + 1.U
124136
io.dataReady := true.B
125137
when(cntReg === CNT_MAX) {
126-
state := start
138+
state := done2
127139
cntReg := 0.U
128140
}
129141
}

src/test/scala/spi/FlashTest.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ class FlashTest extends AnyFlatSpec with ChiselScalatestTester {
1111

1212
it should "test the flash" in {
1313
val spi = new SerialSpiTest(1)
14-
spi.csLow()
15-
print(spi.writeReadSerial("r"))
16-
spi.csHigh()
1714
test(new SpiMaster) { c =>
18-
for(i <- 0 until 1000) {
15+
for(i <- 0 until 200) {
1916
val sck = c.spi.sclk.peekInt()
2017
val mosi = c.spi.mosi.peekInt()
2118
val ncs = c.spi.ncs.peekInt()
22-
// println(s"sck: $sck, mosi: $mosi, ncs: $ncs")
19+
// println(s"ncs: $ncs, mosi: $mosi, sck: $sck")
2320
val bits = (ncs << 2) | (mosi << 1) | sck
2421
val s = "w4" + (bits + '0').toChar + "4\r"
2522
spi.writeReadSerial(s)
23+
val rx = spi.writeReadSerial("r")
24+
// '8' is MISO bit set
25+
val bit = if (rx(8 - 1) == '8') 1 else 0
26+
// println("rx: " + rx)
27+
// println("bit: " + bit)
28+
c.spi.miso.poke(bit)
2629
println(s)
2730
c.clock.step()
2831
// println("dout: " + c.io.dataOut.peekInt())

0 commit comments

Comments
 (0)