-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchpad_controller.v
241 lines (210 loc) · 5.46 KB
/
touchpad_controller.v
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
`timescale 1ns / 1ps
`default_nettype none
`define TOUCH_CLK_DIV_COUNT 25
// Maybe useful defines
`define TOUCH_X_ADJ_MIN 12'h090
`define TOUCH_X_POST_ADJ_MAX 12'h745
`define TOUCH_Y_ADJ_MIN 12'h060
`define TOUCH_Y_POST_ADJ_MAX 12'h6F0
// Important! The data_in pin here corresponds to the spi_data_out pin on the lab handout.
// Likewise, the spi_data_in pin on the handout corresponds to the data_out port here!
// The spi_* signals are named from the point of view of the touchpad itself, whereas the signals
// here are named from the point of view of the module. That is to say, data_out here is what the module
// is sending, data_in is what the module is receiving.
module touchpad_controller(
input wire cclk, rstb,
input wire touch_busy,data_in,
output reg touch_clk, data_out,
output reg touch_csb,
output wire [11:0] x,y,z,
wire zThreshold
);
reg [4:0] clk_div_counter;
reg [4:0] counter;
reg [4:0] counter2;
reg [4:0] xSampleCount;
reg [4:0] ySampleCount;
reg [4:0] zSampleCount;
reg [11:0] tmp_x;
reg [11:0] tmp_y;
reg [11:0] tmp_z;
reg [12:0] sampled_x, sampled_y, sampled_z;
//wire zThreshold;
reg enax, enay, enaz;
averager z_avg(.cclk(cclk), .rstb(rstb), .ena(enaz), .raw(sampled_z), .averaged(z));
averager y_avg(.cclk(cclk), .rstb(rstb), .ena(enay), .raw(sampled_y), .averaged(y));
averager x_avg(.cclk(cclk), .rstb(rstb), .ena(enax), .raw(sampled_x), .averaged(x));
assign zThreshold = (z[11] == 1) || (z[10] == 1) || (z[9] == 1) || (z[8] == 1);
always @(posedge cclk) begin
if(~rstb) begin
touch_clk <= 0;
clk_div_counter <= 0;
counter <= 0;
counter2 <= 0;
tmp_x <= 925;
tmp_y <= 887;
tmp_z <= 0;
sampled_z <= 0;
sampled_y <= 0;
sampled_x <= 0;
data_out <= 0;
enax <= 0;
enay <= 0;
enaz <= 0;
end
else begin
touch_csb <= 0;
if(clk_div_counter != (`TOUCH_CLK_DIV_COUNT-1)) begin
clk_div_counter <= clk_div_counter + 6'd1;
end
else begin
clk_div_counter <= 0;
touch_clk <= ~touch_clk;
if(touch_clk) begin //negative edge logic
// We send the z address 8 times before we sample
if (counter2 <= 6) begin
enay <= 0;
if (counter == 19) begin
data_out <= 0;
counter <= 0;
tmp_z[0] <= data_in;
counter2 <= counter2 + 1;
end
if (counter > 7) begin
data_out <= 0;
tmp_z[19-counter+1] <= data_in;
end
else if (counter == 0) begin
data_out <= 1;
end
else if (counter == 1) begin
data_out <= 0;
end
else if (counter == 2) begin
data_out <= 1;
end
else if (counter == 3) begin
data_out <= 1;
end
else if (counter == 4) begin
data_out <= 0;
end
else if (counter == 5) begin
data_out <= 0;
end
else if (counter == 6) begin
data_out <= 1;
end
else if (counter == 7) begin
data_out <= 1;
end
counter <= counter + 1;
end
// on 8th iteration sample z by adding it to our sum
else if (counter2 == 7) begin
//sampled_Z <= (tmp_z[11] == 1) || (tmp_z[10] == 1) || (tmp_z[9] == 1) || (tmp_z[8] == 1)|| (tmp_z[7] == 1);
sampled_z <= tmp_z;
enaz <=1;
counter2 <= counter2+1;
end
// send x address 8 times before sampling
else if (counter2 <= 14) begin
enaz <= 0;
if (counter == 19) begin
data_out <= 0;
counter <= 0;
tmp_x[0] <= data_in;
counter2 <= counter2 + 1;
end
if (counter > 7) begin
data_out <= 0;
tmp_x[19-counter+1] <= data_in;
end
else if (counter == 0) begin
data_out <= 1;
end
else if (counter == 1) begin
data_out <= 1;
end
else if (counter == 2) begin
data_out <= 0;
end
else if (counter == 3) begin
data_out <= 1;
end
else if (counter == 4) begin
data_out <= 0;
end
else if (counter == 5) begin
data_out <= 0;
end
else if (counter == 6) begin
data_out <= 1;
end
else if (counter == 7) begin
data_out <= 1;
end
counter <= counter + 1;
end
// on 8th iteration sample x
else if (counter2 == 15) begin
if (zThreshold > 0) begin
sampled_x <= ((tmp_x - 145) >> 2) + 25;
enax <=1;
// assign z here so it doesn't get assigned in same place where threshold changes
end
counter2 <= counter2 + 1;
end
// request y 7 times before you sample
else if (counter2 <= 22) begin
enax <= 0;
if (counter == 19) begin
data_out <= 0;
counter <= 0;
tmp_y[0] <= data_in;
counter2 <= counter2 + 1;
end
if (counter > 7) begin
data_out <= 0;
tmp_y[19-counter+1] <= data_in;
end
else if (counter == 0) begin
data_out <= 1;
end
else if (counter == 1) begin
data_out <= 0;
end
else if (counter == 2) begin
data_out <= 0;
end
else if (counter == 3) begin
data_out <= 1;
end
else if (counter == 4) begin
data_out <= 0;
end
else if (counter == 5) begin
data_out <= 0;
end
else if (counter == 6) begin
data_out <= 1;
end
else if (counter == 7) begin
data_out <= 1;
end
counter <= counter + 1;
end
// on 16th iteration set y
else if (counter2 == 23) begin
if (zThreshold > 0) begin
sampled_y <=((tmp_y - 95) >> 3) + 20;
enay <= 1;
end
counter2 <= 0;
end
end
end
end
end
endmodule
`default_nettype wire