Skip to content

Commit b6e9919

Browse files
committed
SPI controller reads the Flash
1 parent d6dd94b commit b6e9919

File tree

4 files changed

+69
-34
lines changed

4 files changed

+69
-34
lines changed

src/main/scala/spi/SerialSpiTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class SerialSpiTest(id: Int, portName: String = "/dev/tty.usbserial-210292B40860
3535
}
3636
// print("Received: " + ret)
3737

38-
Thread.sleep(10)
38+
// Thread.sleep(10)
3939
ret
4040
}
4141

src/main/scala/spi/SpiMaster.scala

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ class SpiMaster extends Module {
1414
val spi = IO(new SpiIO)
1515

1616
val io = IO(new Bundle {
17-
val dataOut = Output(UInt(8.W))
18-
val dataIn = Input(UInt(8.W))
19-
val dataValid = Input(Bool())
20-
val dataReady = Output(Bool())
17+
val readAddr = Flipped(Decoupled(UInt(24.W)))
18+
val readData = Decoupled(UInt(8.W))
2119
})
2220

2321
object State extends ChiselEnum {
@@ -26,19 +24,21 @@ class SpiMaster extends Module {
2624
import State._
2725
val state = RegInit(idle)
2826

29-
val mosiReg = RegInit(0.U(8.W))
27+
val mosiReg = RegInit(0.U(32.W))
3028
val misoReg = RegInit(0.U(8.W))
3129
val bitsReg = RegInit(0.U(8.W))
3230
val cntReg = RegInit(0.U(32.W))
3331
val CNT_MAX = 1.U
3432

3533

3634
// TODO: should those signals be in a register? Probably better timing
35+
// at least SCLK
3736
spi.ncs := 1.U
3837
spi.sclk := 0.U
39-
spi.mosi := mosiReg(7)
40-
io.dataOut := misoReg
41-
io.dataReady := false.B
38+
spi.mosi := mosiReg(31)
39+
io.readData.bits := misoReg
40+
io.readData.valid := false.B
41+
io.readAddr.ready := false.B
4242

4343
val JTAG_ID = 0x9f.U
4444
val RD_STATUS = 0x05.U
@@ -60,13 +60,14 @@ class SpiMaster extends Module {
6060
spi.sclk := 0.U
6161
cntReg := cntReg + 1.U
6262
when(cntReg === CNT_MAX) {
63-
// + when data is available
64-
state := tx1
65-
bitsReg := 7.U
66-
cntReg := 0.U
67-
mosiReg := JTAG_ID
63+
io.readAddr.ready := true.B
64+
when(io.readAddr.valid) {
65+
state := tx1
66+
bitsReg := 31.U
67+
cntReg := 0.U
68+
mosiReg := READ ## io.readAddr.bits
69+
}
6870
}
69-
7071
}
7172
is(tx1) {
7273
spi.ncs := 0.U
@@ -84,7 +85,7 @@ class SpiMaster extends Module {
8485
when(cntReg === CNT_MAX) {
8586
state := tx1
8687
cntReg := 0.U
87-
mosiReg := mosiReg(6, 0) ## 0.U // io.dataIn(7))
88+
mosiReg := mosiReg << 1
8889
bitsReg := bitsReg - 1.U
8990
when(bitsReg === 0.U) {
9091
state := rx1
@@ -99,7 +100,6 @@ class SpiMaster extends Module {
99100
cntReg := cntReg + 1.U
100101
when(cntReg === CNT_MAX) {
101102
misoReg := misoReg(6, 0) ## spi.miso
102-
printf(cf"HW Data is shifting in: $misoReg \n")
103103
state := rx2
104104
cntReg := 0.U
105105
}
@@ -118,12 +118,9 @@ class SpiMaster extends Module {
118118
}
119119
}
120120
is(done1) {
121-
printf("done\n")
122121
spi.ncs := 0.U
123122
spi.sclk := 0.U
124123
cntReg := cntReg + 1.U
125-
io.dataReady := true.B
126-
printf(cf"HW Data is $misoReg \n")
127124
when(cntReg === CNT_MAX) {
128125
state := done2
129126
cntReg := 0.U
@@ -133,10 +130,9 @@ class SpiMaster extends Module {
133130
spi.ncs := 1.U
134131
spi.sclk := 0.U
135132
cntReg := cntReg + 1.U
136-
io.dataReady := true.B
137-
when(cntReg === CNT_MAX) {
138-
state := done2
139-
cntReg := 0.U
133+
io.readData.valid := true.B
134+
when(io.readData.ready) {
135+
state := idle
140136
}
141137
}
142138
}

src/test/scala/spi/FlashTest.scala

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ import chiseltest._
66
import org.scalatest.flatspec.AnyFlatSpec
77
import spi.SerialSpiTest.spi
88

9-
class FlashTest(dontRun: String) extends AnyFlatSpec with ChiselScalatestTester {
9+
class FlashTest() extends AnyFlatSpec with ChiselScalatestTester {
1010
behavior of "FlashTest"
1111

1212
it should "test the flash" in {
13-
val spi = new SerialSpiTest(1)
13+
14+
var spi: SerialSpiTest = null
15+
try {
16+
spi = new SerialSpiTest(1)
17+
} catch {
18+
case e: Exception => {
19+
println("Serial port not available")
20+
cancel()
21+
}
22+
}
23+
1424
test(new SpiMaster) { c =>
15-
for(i <- 0 until 200) {
25+
26+
def echoPins() = {
1627
val sck = c.spi.sclk.peekInt()
1728
val mosi = c.spi.mosi.peekInt()
1829
val ncs = c.spi.ncs.peekInt()
@@ -26,13 +37,41 @@ class FlashTest(dontRun: String) extends AnyFlatSpec with ChiselScalatestTester
2637
// println("rx: " + rx)
2738
// println("bit: " + bit)
2839
c.spi.miso.poke(bit)
29-
println(s)
40+
}
41+
def readByte(addr: Int) = {
42+
c.io.readData.ready.poke(true.B)
43+
44+
c.io.readAddr.valid.poke(true.B)
45+
c.io.readAddr.bits.poke(addr.U)
46+
echoPins()
3047
c.clock.step()
31-
// println("dout: " + c.io.dataOut.peekInt())
32-
if (c.io.dataReady.peekBoolean()) {
33-
println("Data is " + c.io.dataOut.peekInt())
48+
/*
49+
while(!c.io.readAddr.ready.peekBoolean()) {
50+
c.io.readAddr.valid.poke(true.B)
51+
echoPins()
52+
c.clock.step()
53+
}
54+
c.io.readAddr.valid.poke(false.B)
55+
56+
*/
57+
var ready = false
58+
var ch = ' '
59+
while(!ready) {
60+
echoPins()
61+
c.clock.step()
62+
if (c.io.readData.valid.peekBoolean()) {
63+
ready = true
64+
ch = c.io.readData.bits.peekInt().toChar
65+
}
3466
}
67+
c.clock.step()
68+
ch
69+
}
70+
for (i <- 0 until 10) {
71+
val c = readByte(i)
72+
print(c)
3573
}
74+
println()
3675
}
3776
}
3877

src/test/scala/spi/TestSpiMaster.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class TopTest extends Module {
6767
flash.io.HOLDNeg := true.B
6868
ctrl.spi.miso := flash.io.SO
6969

70-
ctrl.io.dataIn := 0.U
71-
ctrl.io.dataValid := false.B
70+
ctrl.io.readAddr.bits := 0.U
71+
ctrl.io.readAddr.valid := false.B
7272

73-
io.dout := ctrl.io.dataOut
73+
io.dout := ctrl.io.readData.bits
7474
}
7575
class TestSpiMaster(doNotRun: String) extends AnyFlatSpec with ChiselScalatestTester {
7676

0 commit comments

Comments
 (0)