Skip to content

Commit 2d4f6b0

Browse files
committed
spi: more experiments
1 parent d20374f commit 2d4f6b0

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

7series.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ puts [irscan xc7.tap 0x09]
1818
puts [drscan xc7.tap 32 0]
1919

2020
puts "Programming FPGA..."
21-
pld load 0 build/WildcatTop.bit
21+
pld load 0 build/BitBang.bit
2222
exit

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ synth:
3434

3535
cp-bit:
3636
-mkdir build
37-
scp [email protected]:~/source/wildcat/build/$(HW).bit build
37+
scp [email protected]:~/t-crest/soc-comm/build/$(HW).bit build
3838
# Configure the Basys3 or NexysA7 board with open source tools
3939
config:
4040
openocd -f 7series.txt
4141

42+
# serial port on Mac
43+
listen:
44+
ls /dev/tty.*
45+
screen /dev/tty.usbserial-210292B408601 115200
46+
47+
# stop with Ctrl+A and Ctrl+\
48+
4249
clean:
4350
git clean -fd

src/main/scala/spi/BitBang.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BitBang(frequ: Int) extends Module {
2626
io.tx := tx.io.txd
2727
rx.io.rxd := io.rx
2828

29-
tx.io.channel.bits := '0'.U + io.sw
29+
tx.io.channel.bits := '0'.U + io.miso
3030
tx.io.channel.valid := rx.io.channel.valid
3131
rx.io.channel.ready := true.B
3232
val regVal = RegEnable('0'.U + rx.io.channel.bits(3, 0), rx.io.channel.valid)

src/main/scala/spi/SpiMaster.scala

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SpiMaster extends Module {
1616
})
1717

1818
object State extends ChiselEnum {
19-
val idle, tx1, tx2, rx1, rx2 = Value
19+
val start, idle, tx1, tx2, rx1, rx2 = Value
2020
}
2121
import State._
2222
val state = RegInit(idle)
@@ -25,34 +25,49 @@ class SpiMaster extends Module {
2525
val mosiReg = RegInit(0.U(8.W))
2626
val misoReg = RegInit(0.U(8.W))
2727
val bitsReg = RegInit(0.U(8.W))
28-
val cntReg = RegInit(0.U(8.W))
28+
val cntReg = RegInit(0.U(32.W))
29+
val CNT_MAX = 9000.U
2930

3031

3132
spi.ncs := 1.U
3233
spi.sclk := x
3334
spi.mosi := mosiReg(7)
3435
io.dataOut := misoReg
3536

36-
val JTAG_ID = 0x9f.U
37+
// val JTAG_ID = 0x90.U
38+
val JTAG_ID = 0xab.U
39+
val RD_STATUS = 0x05.U
3740
val RDSR = 0x05.U
3841
val READ = 0x03.U
3942

4043
switch(state) {
44+
is(start) {
45+
spi.ncs := 1.U
46+
spi.sclk := 0.U
47+
cntReg := cntReg + 1.U
48+
when(cntReg === CNT_MAX) {
49+
state := idle
50+
cntReg := 0.U
51+
}
52+
}
4153
is(idle) {
4254
spi.ncs := 1.U
4355
spi.sclk := 0.U
44-
when(true.B) {
56+
cntReg := cntReg + 1.U
57+
when(cntReg === CNT_MAX) {
58+
// when data is available
4559
state := tx1
4660
bitsReg := 7.U
4761
cntReg := 0.U
4862
mosiReg := JTAG_ID
4963
}
64+
5065
}
5166
is(tx1) {
5267
spi.ncs := 0.U
5368
spi.sclk := 0.U
5469
cntReg := cntReg + 1.U
55-
when(cntReg === 3.U) {
70+
when(cntReg === CNT_MAX) {
5671
state := tx2
5772
cntReg := 0.U
5873
}
@@ -61,7 +76,7 @@ class SpiMaster extends Module {
6176
spi.ncs := 0.U
6277
spi.sclk := 1.U
6378
cntReg := cntReg + 1.U
64-
when(cntReg === 3.U) {
79+
when(cntReg === CNT_MAX) {
6580
state := tx1
6681
cntReg := 0.U
6782
mosiReg := mosiReg(6, 0) ## 0.U // io.dataIn(7))
@@ -75,7 +90,7 @@ class SpiMaster extends Module {
7590
spi.ncs := 0.U
7691
spi.sclk := 0.U
7792
cntReg := cntReg + 1.U
78-
when(cntReg === 3.U) {
93+
when(cntReg === CNT_MAX) {
7994
state := rx2
8095
cntReg := 0.U
8196
bitsReg := bitsReg - 1.U
@@ -88,7 +103,7 @@ class SpiMaster extends Module {
88103
spi.ncs := 0.U
89104
spi.sclk := 1.U
90105
cntReg := cntReg + 1.U
91-
when(cntReg === 3.U) {
106+
when(cntReg === CNT_MAX) {
92107
state := rx1
93108
cntReg := 0.U
94109
bitsReg := bitsReg - 1.U

src/test/scala/spi/TestSpiMaster.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,19 @@ class TopTest extends Module {
7474
}
7575
class TestSpiMaster extends AnyFlatSpec with ChiselScalatestTester {
7676

77+
/*
7778
"SpiMaster" should "work" in {
7879
test(new SpiMaster).withAnnotations(Seq(WriteVcdAnnotation)) { dut =>
7980
dut.clock.step(20)
8081
}
8182
}
8283
84+
*/
85+
8386
"TopTest" should "work" in {
8487
test(new TopTest).withAnnotations(Seq(WriteVcdAnnotation, IcarusBackendAnnotation)) { dut =>
85-
dut.clock.step(400)
88+
dut.clock.setTimeout(1500001)
89+
dut.clock.step(1500000)
8690
}
8791
}
8892
}

0 commit comments

Comments
 (0)