-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFT_SPI.scala
133 lines (110 loc) · 3.81 KB
/
TFT_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
package TFT_Driver
import spinal.core._
import spinal.lib.{Counter, Stream, StreamFifo}
import spinal.lib.fsm.{EntryPoint, State, StateDelay, StateMachine}
object TFT_SPI {
def apply(cycles: BigInt) : TFT_SPI = new TFT_SPI(cycles)
def apply(time: TimeNumber) : TFT_SPI = new TFT_SPI((time * ClockDomain.current.frequency.getValue).toBigInt())
}
class TFT_SPI(val Delay: BigInt) extends Component {
val io = new Bundle {
val data = in Bits(9 bits) //Data to be placed into FIFO
val data_clk = in Bool()
val ready = out Bool()
val sending = out Bool()
val fifo_full = out Bool() //High when less than two byte are available in the fifo
val SPI_SCL = out Bool()
val SPI_SDA = out Bool()
val SPI_DC = out Bool()
val SPI_RST = out Bool()
val SPI_CS = out Bool()
}
val source,sink = Stream(Bits(9 bits))
val spiFiFo = StreamFifo(
dataType = Bits(9 bits),
depth = 16
)
spiFiFo.io.push << source
spiFiFo.io.pop >> sink
sink.ready := False
val validRegSource = Reg(Bool) init(False)
source.valid := io.data_clk
source.payload := io.data
val fifo_full = RegNext(spiFiFo.io.availability < 2) init(False)
io.fifo_full := fifo_full
val ready = Reg(Bool) init(False)
io.ready := ready
val sending = Reg(Bool) init(False)
io.sending := sending
val bitCount = Counter(8)
val shiftReg = Reg(Bits(8 bits)) init(0)
val SPI_SCL = Reg(Bool) init(True)
val SPI_SDA = Reg(Bool) init(False)
val SPI_DC = Reg(Bool) init(False)
val SPI_RST = Reg(Bool) init(False)
val SPI_CS = Reg(Bool) init(True)
io.SPI_SCL := SPI_SCL
io.SPI_SDA := SPI_SDA
io.SPI_DC := SPI_DC
io.SPI_RST := SPI_RST
io.SPI_CS := SPI_CS
val fsm = new StateMachine {
val Init: State = new StateDelay(Delay) with EntryPoint {
whenIsActive {
SPI_RST := False
}
whenCompleted {
goto(ResetDisplay)
}
}
val ResetDisplay: State = new StateDelay(Delay) {
whenIsActive {
SPI_RST := True
}
whenCompleted {
ready := True
goto(WaitForData)
}
}
val WaitForData: State = new State {
whenIsActive {
when(sink.valid) {
shiftReg := sink.payload(7 downto 0)
SPI_DC := sink.payload(8)
SPI_CS := False
bitCount.clear()
sink.ready := True
sending := True
goto(OutData)
}otherwise(sending := False)
}
}
val OutData: State = new State {
whenIsActive {
sink.ready := False
SPI_SDA := shiftReg(7)
SPI_SCL := False
goto(ShiftData)
}
}
val ShiftData: State = new State {
whenIsActive {
bitCount.increment()
shiftReg := shiftReg |<< 1
SPI_SCL := True
when(bitCount === 7) {
when(sink.valid){
shiftReg := sink.payload(7 downto 0)
SPI_DC := sink.payload(8)
bitCount.clear()
sink.ready := True
goto(OutData)
}otherwise {
SPI_CS := True
goto(WaitForData)
}
} otherwise(goto(OutData))
}
}
}
}