Skip to content

Commit 1025c43

Browse files
committed
spi: work on Flash access
1 parent 04d67e9 commit 1025c43

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/main/scala/spi/SpiMaster.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class SpiMaster extends Module {
1717
val dataOut = Output(UInt(8.W))
1818
val dataIn = Input(UInt(8.W))
1919
val dataValid = Input(Bool())
20+
val dataReady = Output(Bool())
2021
})
2122

2223
object State extends ChiselEnum {
23-
val start, idle, tx1, tx2, rx1, rx2 = Value
24+
val start, idle, tx1, tx2, rx1, rx2, done = Value
2425
}
2526
import State._
2627
val state = RegInit(idle)
@@ -29,16 +30,17 @@ class SpiMaster extends Module {
2930
val misoReg = RegInit(0.U(8.W))
3031
val bitsReg = RegInit(0.U(8.W))
3132
val cntReg = RegInit(0.U(32.W))
32-
val CNT_MAX = 100.U
33+
val CNT_MAX = 1.U
3334

3435

36+
// TODO: should those signals be in a register? Probably better timing
3537
spi.ncs := 1.U
3638
spi.sclk := 0.U
3739
spi.mosi := mosiReg(7)
3840
io.dataOut := misoReg
41+
io.dataReady := false.B
3942

40-
// val JTAG_ID = 0x90.U
41-
val JTAG_ID = 0xab.U
43+
val JTAG_ID = 0x9f.U
4244
val RD_STATUS = 0x05.U
4345
val RDSR = 0x05.U
4446
val READ = 0x03.U
@@ -111,10 +113,20 @@ class SpiMaster extends Module {
111113
cntReg := 0.U
112114
bitsReg := bitsReg - 1.U
113115
when(bitsReg === 0.U) {
114-
state := idle
116+
state := done
115117
}
116118
}
117119
}
120+
is(done) {
121+
spi.ncs := 1.U
122+
spi.sclk := 0.U
123+
cntReg := cntReg + 1.U
124+
io.dataReady := true.B
125+
when(cntReg === CNT_MAX) {
126+
state := start
127+
cntReg := 0.U
128+
}
129+
}
118130
}
119131
}
120132

src/test/scala/spi/FlashTest.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package spi
2+
3+
import chisel3._
4+
import chisel3.util._
5+
import chiseltest._
6+
import org.scalatest.flatspec.AnyFlatSpec
7+
import spi.SerialSpiTest.spi
8+
9+
class FlashTest extends AnyFlatSpec with ChiselScalatestTester {
10+
behavior of "FlashTest"
11+
12+
it should "test the flash" in {
13+
val spi = new SerialSpiTest(1)
14+
spi.csLow()
15+
print(spi.writeReadSerial("r"))
16+
spi.csHigh()
17+
test(new SpiMaster) { c =>
18+
for(i <- 0 until 1000) {
19+
val sck = c.spi.sclk.peekInt()
20+
val mosi = c.spi.mosi.peekInt()
21+
val ncs = c.spi.ncs.peekInt()
22+
// println(s"sck: $sck, mosi: $mosi, ncs: $ncs")
23+
val bits = (ncs << 2) | (mosi << 1) | sck
24+
val s = "w4" + (bits + '0').toChar + "4\r"
25+
spi.writeReadSerial(s)
26+
println(s)
27+
c.clock.step()
28+
// println("dout: " + c.io.dataOut.peekInt())
29+
if (c.io.dataReady.peekBoolean()) {
30+
println("Data is " + c.io.dataOut.peekInt())
31+
}
32+
}
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)