-
Notifications
You must be signed in to change notification settings - Fork 1
/
SPI.scala
176 lines (152 loc) · 4.96 KB
/
SPI.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package MySpinalHardware
import spinal.core._
import spinal.lib._
import spinal.lib.fsm._
import spinal.core.sim._
import scala.util.control.Breaks
class mySPI extends Component{
val io = new Bundle
{
val output = slave Stream(Bits(9 bits))
val output_full = out Bool()
val input = master Stream(Bits(8 bits))
val input_full = out Bool()
val holdOpen = in Bool()
val sending = out Bool()
val c = in Bits(16 bits)
val SPI = new Bundle
{
val SCLK = out Bool()
val CS = out Bool()
val MOSI = out Bool()
val MISO = in Bool()
}
}
val spi_data_out = Stream(Bits(9 bits))
val outFIFO = StreamFifo(
dataType = Bits(9 bits),
depth = 16
)
outFIFO.io.push << io.output
outFIFO.io.pop >> spi_data_out
val spi_data_in = Stream(Bits(8 bits))
val inFIFO = StreamFifo(
dataType = Bits(8 bits),
depth = 16
)
inFIFO.io.push << spi_data_in
inFIFO.io.pop >> io.input
io.output_full := outFIFO.io.availability < 2
io.input_full := inFIFO.io.availability < 2
val shiftReg = Reg(Bits(8 bits)) init(0)
val shiftRegShift = shiftReg(6 downto 0) ## io.SPI.MISO
val bitCount = Counter(8)
val dataInFlag = Reg(Bool) init(False)
val sending = Reg(Bool) init(False)
io.sending := sending
val SPI_SCLK = Reg(Bool) init(True)
val SPI_MOSI = Reg(Bool) init(False)
val SPI_CS = Reg(Bool) init(True)
io.SPI.SCLK := SPI_SCLK
io.SPI.MOSI := SPI_MOSI
io.SPI.CS := SPI_CS
spi_data_out.ready := False
spi_data_in.valid := False
spi_data_in.payload := shiftRegShift
val fsm = new StateMachine {
val Init: State = new State with EntryPoint {
whenIsActive {
goto(WaitForData)
}
}
val WaitForData: State = new State {
whenIsActive {
when(spi_data_out.valid) {
shiftReg := spi_data_out.payload(7 downto 0)
dataInFlag := spi_data_out.payload(8)
SPI_CS := False
bitCount.clear()
spi_data_out.ready := True
sending := True
goto(OutData)
}otherwise{
when(!io.holdOpen){
SPI_CS := True
}
sending := False
}
}
}
val OutData: State = new State {
whenIsActive {
SPI_MOSI := shiftReg(7)
SPI_SCLK := False
shiftReg := shiftRegShift
goto(ShiftData)
}
}
val ShiftData: State = new State {
when
whenIsActive {
bitCount.increment()
SPI_SCLK := True
when(bitCount === 7) {
when(spi_data_in.ready && dataInFlag){
spi_data_in.valid := True
}
when(spi_data_out.valid && (spi_data_in.ready || !dataInFlag)){
shiftReg := spi_data_out.payload(7 downto 0)
dataInFlag := spi_data_out.payload(8)
bitCount.clear()
spi_data_out.ready := True
goto(OutData)
} otherwise {
goto(WaitForData)
}
} otherwise(goto(OutData))
}
}
}
}
object SPI_Test {
def main(args: Array[String]) {
SimConfig.withWave.compile{
val dut = new mySPI()
dut
}.doSim { dut =>
//Fork a process to generate the reset and the clock on the dut
dut.clockDomain.forkStimulus(period = 10)
dut.clockDomain.waitRisingEdge()
var c = 0;
val loop = new Breaks;
loop.breakable {
while (true) {
dut.io.c #= c
dut.io.holdOpen #= true
if(c == 2){
dut.io.output.payload #= 0xe3
dut.io.output.valid #= true
}
if(c == 3){
dut.io.output.valid #= false
}
if(c == 4){
dut.io.output.payload #= 0x101
dut.io.output.valid #= true
}
if(c == 5){
dut.io.output.valid #= false
}
if(c >= 22 && c < 28 || c >= 34 && c < 38){
dut.io.SPI.MISO #= true
}else{
dut.io.SPI.MISO #= false
}
if(c == 50) loop.break()
c+=1
dut.clockDomain.waitRisingEdge()
}
}
}
}
}