-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcmctrl.v
More file actions
369 lines (324 loc) · 9.86 KB
/
dcmctrl.v
File metadata and controls
369 lines (324 loc) · 9.86 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//`define DCMCTRL_DUMMY
module dcmctrl #(
parameter integer N_CHANNELS = 6,
parameter integer TIMEOUT_EXP2 = 25, //around 1.4 sec timeout @24MHz, Formula=(2^TIMEOUT_EXP2)/24000000
parameter integer RESET_EXP = 18 //around 10ms
) (
input clk,
input reset,
input enable,
output irq,
input spi_ss,
input spi_clk,
input spi_mosi,
output reg spi_miso,
output reg [N_CHANNELS-1:0] motor_left,
output reg [N_CHANNELS-1:0] motor_right,
output reg [N_CHANNELS-1:0] motor_reset,
input [N_CHANNELS-1:0] motor_pulse,
input [N_CHANNELS-1:0] motor_fault,
input [N_CHANNELS-1:0] motor_otw
);
// ---- SPI Interface ----
reg [7:0] spi_shiftreg;
reg [2:0] spi_bitcnt;
reg spi_first_byte;
reg last_spi_clk;
reg spi_write;
reg spi_xstrobe;
reg spi_rstrobe;
reg spi_wstrobe;
reg [6:0] spi_raddr;
reg [6:0] spi_waddr;
reg [7:0] spi_rdata;
reg [7:0] spi_wdata;
reg spi_ss_q;
reg spi_clk_q;
reg spi_mosi_q;
always @(posedge clk) begin
spi_ss_q <= spi_ss;
spi_clk_q <= spi_clk;
spi_mosi_q <= spi_mosi;
end
always @(posedge clk) begin
spi_rstrobe <= 0;
spi_wstrobe <= spi_rstrobe && spi_write && !spi_first_byte;
spi_xstrobe <= spi_rstrobe;
spi_wdata = spi_shiftreg;
last_spi_clk <= spi_clk_q;
if (reset || spi_ss_q) begin
/* reset everything */
spi_first_byte <= 0;
spi_write <= 0;
spi_miso <= 0;
/* new transaction */
spi_shiftreg <= 0;
spi_bitcnt <= 0;
spi_first_byte <= 1;
end else
if (!last_spi_clk && spi_clk_q) begin
/* next bit in */
spi_shiftreg <= {spi_shiftreg, spi_mosi_q};
spi_bitcnt <= spi_bitcnt + 1;
if (spi_bitcnt == 7) begin
if (spi_first_byte)
{spi_write, spi_raddr} <= {spi_shiftreg, spi_mosi_q};
else
spi_raddr <= spi_raddr + 1;
spi_waddr <= spi_raddr;
spi_rstrobe <= 1;
end
end else
if (last_spi_clk && !spi_clk_q) begin
/* next bit out */
spi_miso <= spi_shiftreg[7];
end else
if (spi_xstrobe) begin
spi_shiftreg <= spi_rdata;
spi_first_byte <= 0;
end
end
// ---- Register File ----
reg [7:0] registers [0:128];
(* keep *) reg reg_wstrobe;
(* keep *) reg reg_rstrobe;
(* keep *) reg [6:0] reg_addr;
(* keep *) reg [7:0] reg_wdata;
wire reg_flowctrl = spi_wstrobe || spi_rstrobe;
reg reg_flowctrl_q;
(* keep *) wire [7:0] reg_rdata = reg_flowctrl_q ? reg_rdata_q : spi_rdata;
(* keep *) reg [7:0] reg_rdata_q;
always @(posedge clk) begin
if (spi_wstrobe || reg_wstrobe) begin
registers[spi_wstrobe ? spi_waddr : reg_addr] <= spi_wstrobe ? spi_wdata : reg_wdata;
end
spi_rdata <= registers[spi_rstrobe ? spi_raddr : reg_addr];
reg_flowctrl_q <= reg_flowctrl;
reg_rdata_q <= reg_rdata;
end
`ifdef DCMCTRL_DUMMY
// with this define set the core is a simple SPI memory and
// the motor ctrl outputs are constant low (for testing)
always @(posedge clk) begin
reg_wstrobe <= 0;
reg_rstrobe <= 0;
reg_addr <= 0;
reg_rdata <= 0;
reg_wdata <= 0;
motor_left <= 0;
motor_right <= 0;
motor_reset <= 0;
end
`else
// ---- Motor Controller ----
(* keep *) reg [9:0] mc_state;
(* keep *) reg [3:0] mc_channel;
(* keep *) reg [23:0] mc_current_position;
(* keep *) reg [23:0] mc_target_position;
(* keep *) reg [7:0] mc_flags [0:N_CHANNELS-1];
(* keep *) reg [7:0] mc_chan_speed [0:N_CHANNELS-1];
(* keep *) reg [N_CHANNELS-1:0] mc_chan_turn_left;
(* keep *) reg [N_CHANNELS-1:0] mc_chan_turn_right;
(* keep *) reg [N_CHANNELS-1:0] mc_chan_pulse;
(* keep *) reg [N_CHANNELS-1:0] mc_chan_last_pulse;
(* keep *) reg mc_write_flags;
(* keep *) reg mc_write_position;
(* keep *) reg [N_CHANNELS-1:0] mc_chan_live;
(* keep *) reg [TIMEOUT_EXP2-1:0] mc_timeout;
//(* keep *) reg [RESET_EXP-1:0] reset_timeout;
// "move left" := mc_position_delta < 0
// "move right" := mc_position_delta > 0
wire [23:0] mc_position_delta = mc_target_position - mc_current_position;
wire mc_timeout_check = &mc_timeout[TIMEOUT_EXP2-1 -: 3];
//wire reset_timeout_check = &reset_timeout[RESET_EXP-1 -: 3];
always @(posedge clk) begin
mc_chan_pulse <= mc_chan_pulse | (motor_pulse & ~mc_chan_last_pulse);
mc_chan_last_pulse <= motor_pulse;
mc_timeout <= mc_chan_speed[0] == 0 ? mc_timeout : (mc_timeout + 1);
mc_chan_live <= mc_timeout ? (mc_chan_live | mc_chan_pulse) : 0;
if (reset) begin
mc_state <= 0;
mc_channel <= 0;
mc_chan_pulse <= 0;
reg_wstrobe <= 0;
reg_rstrobe <= 0;
mc_chan_live <= 0;
mc_timeout <= 0;
motor_reset <= 1;
end else
if (!reg_flowctrl) begin
reg_wstrobe <= 0;
reg_rstrobe <= 0;
case (mc_state)
// --- Read state from register file ---
0: begin
mc_write_flags <= 0;
mc_write_position <= 0;
reg_addr <= mc_channel * 4;
reg_rstrobe <= 1;
mc_state <= 1;
end
1: begin
reg_addr <= reg_addr + 1;
reg_rstrobe <= 1;
mc_state <= 2;
end
2: begin
if (mc_flags[mc_channel] != 0 && reg_rdata == 0) begin
mc_chan_live[mc_channel] <= 0;
mc_timeout <= 0;
motor_reset[mc_channel] <= 0; //TODO - pulse
end
mc_flags[mc_channel] <= reg_rdata;
reg_addr <= reg_addr + 1;
reg_rstrobe <= 1;
mc_state <= 3;
end
3: begin
mc_current_position[23:16] <= reg_rdata;
reg_addr <= reg_addr + 1;
reg_rstrobe <= 1;
mc_state <= 4;
end
4: begin
mc_current_position[15:8] <= reg_rdata;
reg_addr <= reg_addr + 61;
reg_rstrobe <= 1;
mc_state <= 5;
end
5: begin
mc_current_position[7:0] <= reg_rdata;
reg_addr <= reg_addr + 1;
reg_rstrobe <= 1;
mc_state <= 6;
end
6: begin
//`ifndef SYNTHESIS
// if (mc_channel == 0)
// $display("curr pos = %d", mc_current_position);
//`endif
mc_chan_speed[mc_channel] <= reg_rdata;
reg_addr <= reg_addr + 1;
reg_rstrobe <= 1;
mc_state <= 7;
end
7: begin
mc_target_position[23:16] <= reg_rdata;
reg_addr <= reg_addr + 1;
reg_rstrobe <= 1;
mc_state <= 8;
end
8: begin
mc_target_position[15:8] <= reg_rdata;
mc_state <= 9;
end
9: begin
mc_target_position[7:0] <= reg_rdata;
mc_state <= 100;
end
// --- Update state ---
100: begin
if (mc_position_delta == 0) begin
mc_chan_live[mc_channel] <= 1;
end
mc_chan_turn_left[mc_channel] <= !mc_flags[mc_channel] && mc_position_delta[23];
mc_chan_turn_right[mc_channel] <= !mc_flags[mc_channel] && !mc_position_delta[23] && mc_position_delta[22:0];
mc_state <= 101;
end
101: begin
motor_reset[mc_channel] <= 0;
if (mc_timeout_check && !mc_chan_live[mc_channel] && !mc_flags[mc_channel][2:0]) begin
mc_flags[mc_channel][2] <= 1;
mc_write_flags <= 1;
mc_timeout <= 0;
end
if (motor_fault[mc_channel] && !mc_flags[mc_channel][1]) begin
motor_reset[mc_channel] <= 1;
mc_flags[mc_channel][1] <= 1;
mc_write_flags <= 1;
end
//Don't stop motor, the OTW does mean that the temperature is critical, but
//if the temp exceeds a limit, a FAULT will be raised
//if (motor_otw[mc_channel] && !mc_flags[mc_channel][0]) begin
// motor_reset[mc_channel] <= 1;
// mc_flags[mc_channel][0] <= 1;
// mc_write_flags <= 1;
//end
mc_state <= 1000;
end
// --- Write state back to register file ---
1000: begin
mc_channel <= mc_channel == N_CHANNELS-1 ? 0 : mc_channel+1;
mc_chan_last_pulse[mc_channel] <= motor_pulse[mc_channel];
if (mc_chan_pulse[mc_channel]) begin
if (mc_chan_turn_left[mc_channel])
mc_current_position <= mc_current_position - 1;
if (mc_chan_turn_right[mc_channel])
mc_current_position <= mc_current_position + 1;
mc_chan_pulse[mc_channel] <= 0;
mc_write_position <= 1;
end
reg_addr <= mc_channel * 4;
reg_wstrobe <= mc_write_flags;
reg_wdata <= mc_flags[mc_channel];
mc_state <= 1001;
end
1001: begin
reg_addr <= reg_addr + 1;
reg_wstrobe <= mc_write_position;
reg_wdata <= mc_current_position[23:16];
mc_state <= 1002;
end
1002: begin
reg_addr <= reg_addr + 1;
reg_wstrobe <= mc_write_position;
reg_wdata <= mc_current_position[15:8];
mc_state <= 1003;
end
1003: begin
reg_addr <= reg_addr + 1;
reg_wstrobe <= mc_write_position;
reg_wdata <= mc_current_position[7:0];
mc_state <= 0;
end
endcase
end
end
// ---- PWM Controller ----
(* keep *) reg [7:0] pwm_stage1_count;
(* keep *) reg [3:0] pwm_stage1_channel;
(* keep *) reg [7:0] pwm_stage2_count;
(* keep *) reg [3:0] pwm_stage2_channel;
(* keep *) reg [7:0] pwm_stage2_speed;
(* keep *) reg [0:N_CHANNELS-1] motor_left_prev ;
(* keep *) reg [0:N_CHANNELS-1] motor_right_prev ;
always @(posedge clk) begin
if (reset) begin
pwm_stage1_count <= 0;
pwm_stage1_channel <= 0;
pwm_left_prev <= 0;
pwm_right_prev <= 0;
end else begin
pwm_stage1_count <= pwm_stage1_count + 1;
if (pwm_stage1_channel == N_CHANNELS-1) begin
pwm_stage1_channel <= 0;
end else begin
pwm_stage1_channel <= pwm_stage1_channel + 1;
end
pwm_stage2_count <= pwm_stage1_count;
pwm_stage2_channel <= pwm_stage1_channel;
pwm_stage2_speed <= mc_chan_speed[pwm_stage1_channel];
motor_left[pwm_stage2_channel] <= mc_chan_turn_left[pwm_stage2_channel] && (pwm_stage2_count < pwm_stage2_speed);
motor_right[pwm_stage2_channel] <= mc_chan_turn_right[pwm_stage2_channel] && (pwm_stage2_count < pwm_stage2_speed);
if (mc_chan_turn_left[pwm_stage2_channel] && motor_left[pwm_stage2_channel] && !motor_left_prev[pwm_stage2_channel]) begin
motor_right[pwm_stage2_channel] <= 1;
end
if (mc_chan_turn_right[pwm_stage2_channel] && motor_right[pwm_stage2_channel] && !motor_right_prev[pwm_stage2_channel]) begin
motor_left[pwm_stage2_channel] <= 1;
end
motor_left_prev[pwm_stage2_channel] <= motor_left[pwm_stage2_channel];
motor_right_prev[pwm_stage2_channel] <= motor_right[pwm_stage2_channel];
end
end
`endif
endmodule