-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFT_TEST.scala
202 lines (174 loc) · 5.56 KB
/
TFT_TEST.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package TFT_Driver
import spinal.core._
import spinal.lib._
import spinal.lib.fsm._
import TFT_Driver._
import MySpinalHardware._
class TFT_TEST(val delay: BigInt) extends Component {
var io = new Bundle()
{
val lcd_rst = out Bool()
val lcd_dc = out Bool()
val lcd_sdo = out Bool()
val lcd_sck = out Bool()
}
val data_clk = False
val data = B"9'h000"
val lcd = TFT_ILI9341(delay)
io.lcd_sck := lcd.io.SPI_SCL
io.lcd_sdo := lcd.io.SPI_SDA
io.lcd_rst := lcd.io.SPI_RST
io.lcd_dc := lcd.io.SPI_DC
lcd.io.data := data
lcd.io.data_clk := data_clk
//draw area setting for 0x0 by 320x240
val initParamsList = List(
0x02A, 0x100, 0x100, 0x101, 0x13f, //CASET
0x02B, 0x100, 0x100, 0x100, 0x1ef, //RASET
0x02C, //RAMWR
0x1FF //END
)
val ParamsRom = Mem(UInt(9 bits), initParamsList.map(U(_, 9 bits)))
val ParamsPointer = new Counter(start = 0, end = initParamsList.length)
val paramData = ParamsRom(ParamsPointer)
val ScreenX = Counter(start = 0, end = 319)
val ScreenY = Counter(start = 0, end = 239)
val ScreenDone = RegNext(ScreenX.willOverflowIfInc && ScreenY.willOverflowIfInc)
val colorHighByte = False
val colorOut = B"16'h0000"
val ball = False
val grid = False
val ballX = Reg(UInt(9 bits)) init(0)
val ballY = Reg(UInt(8 bits)) init(10)
val ballXDir = Reg(Bool) init(True)
val ballYDir = Reg(Bool) init(True)
when(ScreenX(4) ^ ScreenY(4)) {
grid := True
}
when(ScreenX >= ballX && ScreenX <= ballX+10 && ScreenY >= ballY && ScreenY <= ballY+10){
ball := True
}
when(ball){
colorOut := B"16'hf800"
}elsewhen(grid){
colorOut := B"16'h39C7"
}
//mux between the parameters and pixel data in the color space of 565
when(paramData === 0x1FF){
when(colorHighByte){
data := B"1" ## colorOut(15 downto 8)
}otherwise{
data := B"1" ## colorOut(7 downto 0)
}
}otherwise(data := paramData.asBits(8 downto 0))
val fsm = new StateMachine {
val Init: State = new State with EntryPoint {
whenIsActive {
data_clk := False
goto(Wait)
}
}
//Wait for SPI reset
val Wait: State = new State {
whenIsActive{
when(lcd.io.displayReady){
goto(Done)
}
}
}
//Send the paramData till we hit 0x1ff then start sending pixel data
val StartFrame: State = new State{
whenIsActive {
when(paramData === 0x1ff){
goto(SendData)
}otherwise{
data_clk := True;
goto(SendData)
}
}
}
val SendData: State = new State{
whenIsActive {
data_clk := False;
when(!lcd.io.fifo_full){
when(paramData =/= 0x1ff){
ParamsPointer.increment()
goto(StartFrame)
}otherwise{
goto(LoadColorLowByte)
}
}
}
}
val LoadColorLowByte: State = new State {
onEntry{
data_clk := True;
colorHighByte := True
}
whenIsActive {
data_clk := False;
goto(LoadColorHighByte)
}
}
val LoadColorHighByte: State = new State {
onEntry{
data_clk := True;
colorHighByte := False
}
whenIsActive {
data_clk := False;
when(ScreenDone){
goto(Finish)
}otherwise{
ScreenX.increment()
when (ScreenX.willOverflowIfInc) {
ScreenY.increment()
}
goto(SendData)
}
}
}
val Finish: State = new State {
whenIsActive {
colorHighByte := False;
when(ballX < 1){
ballXDir := True
} elsewhen(ballX >= 310){
ballXDir := False
}
when(ballY < 1) {
ballYDir := True
} elsewhen(ballY >= 230){
ballYDir := False
}
goto(Done)
}
}
//Reset everything and wait for next frame
val Done: State = new StateDelay(delay) {
whenIsActive {
data_clk := False;
ScreenY.clear()
ScreenX.clear()
ParamsPointer.clear()
colorHighByte := False
}
whenCompleted{
when(ballXDir) {
ballX := ballX + 1
} otherwise (ballX := ballX - 1)
when(ballYDir) {
ballY := ballY + 1
} otherwise (ballY := ballY - 1)
goto(StartFrame)
}
}
}
}
object TFT_TESTConfig extends SpinalConfig(targetDirectory = "/home/winston/Projects/C/nodeUI/src/TestNodes/", defaultConfigForClockDomains = ClockDomainConfig(resetKind = SYNC))
//Generate the MyTopLevel's Verilog using the above custom configuration.
object TFT_TESTGen {
def main(args: Array[String]) {
TFT_TESTConfig.generateVerilog(new TFT_TEST(10))
}
}