Skip to content

Commit 332119f

Browse files
committed
spi: load words from Flash
1 parent b6e9919 commit 332119f

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/main/scala/spi/SerialSpiTest.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,20 @@ object SerialSpiTest extends App {
249249
val data = s.getBytes
250250
// spi.programFlash(1, 0, data)
251251
// Thread.sleep(1000)
252+
val prog = Array(0x11100093, 0x22200113, 0x002081b3)
253+
val by = new Array[Byte](12)
254+
for (i <- 0 until prog.length) {
255+
for (j <- 0 until 4) {
256+
val b = prog(i) >> (8 * j) & 0xff
257+
println(f"$b%02x")
258+
by(i * 4 + j) = b.toByte
259+
}
260+
}
261+
// spi.programFlash(1, 0, by)
252262

253263
val buf = new Array[Byte](20)
254-
// spi.readMemory(0, buf)
255-
// println(new String(buf))
264+
spi.readMemory(0, buf)
265+
println(new String(buf))
256266
/*
257267
for (i <- 0 until 20) {
258268
print(spi.readMemory(i).toChar)

src/main/scala/spi/SpiMaster.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SpiMaster extends Module {
1515

1616
val io = IO(new Bundle {
1717
val readAddr = Flipped(Decoupled(UInt(24.W)))
18-
val readData = Decoupled(UInt(8.W))
18+
val readData = Decoupled(UInt(32.W))
1919
})
2020

2121
object State extends ChiselEnum {
@@ -25,7 +25,7 @@ class SpiMaster extends Module {
2525
val state = RegInit(idle)
2626

2727
val mosiReg = RegInit(0.U(32.W))
28-
val misoReg = RegInit(0.U(8.W))
28+
val misoReg = RegInit(0.U(32.W))
2929
val bitsReg = RegInit(0.U(8.W))
3030
val cntReg = RegInit(0.U(32.W))
3131
val CNT_MAX = 1.U
@@ -36,7 +36,8 @@ class SpiMaster extends Module {
3636
spi.ncs := 1.U
3737
spi.sclk := 0.U
3838
spi.mosi := mosiReg(31)
39-
io.readData.bits := misoReg
39+
// little endian return
40+
io.readData.bits := misoReg(7, 0) ## misoReg(15, 8) ## misoReg(23, 16) ## misoReg(31, 24)
4041
io.readData.valid := false.B
4142
io.readAddr.ready := false.B
4243

@@ -89,7 +90,7 @@ class SpiMaster extends Module {
8990
bitsReg := bitsReg - 1.U
9091
when(bitsReg === 0.U) {
9192
state := rx1
92-
bitsReg := 7.U
93+
bitsReg := 31.U
9394
}
9495
}
9596
}
@@ -99,7 +100,7 @@ class SpiMaster extends Module {
99100
spi.sclk := 0.U
100101
cntReg := cntReg + 1.U
101102
when(cntReg === CNT_MAX) {
102-
misoReg := misoReg(6, 0) ## spi.miso
103+
misoReg := misoReg(30, 0) ## spi.miso
103104
state := rx2
104105
cntReg := 0.U
105106
}

src/test/scala/spi/FlashTest.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class FlashTest() extends AnyFlatSpec with ChiselScalatestTester {
3838
// println("bit: " + bit)
3939
c.spi.miso.poke(bit)
4040
}
41-
def readByte(addr: Int) = {
41+
def readWord(addr: Int) = {
4242
c.io.readData.ready.poke(true.B)
4343

4444
c.io.readAddr.valid.poke(true.B)
@@ -55,23 +55,22 @@ class FlashTest() extends AnyFlatSpec with ChiselScalatestTester {
5555
5656
*/
5757
var ready = false
58-
var ch = ' '
58+
var v = 0
5959
while(!ready) {
6060
echoPins()
6161
c.clock.step()
6262
if (c.io.readData.valid.peekBoolean()) {
6363
ready = true
64-
ch = c.io.readData.bits.peekInt().toChar
64+
v = c.io.readData.bits.peekInt().toInt
6565
}
6666
}
6767
c.clock.step()
68-
ch
68+
v
6969
}
70-
for (i <- 0 until 10) {
71-
val c = readByte(i)
72-
print(c)
70+
for (i <- 0 until 3) {
71+
val v = readWord(i*4)
72+
println(f"Read 0x$v%08x")
7373
}
74-
println()
7574
}
7675
}
7776

0 commit comments

Comments
 (0)