-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathPWM.v
185 lines (161 loc) · 4.01 KB
/
PWM.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
`timescale 1 ps / 1 ps
module Servo (
input wire clk, // clock_sink.clk
input wire reset_n, // reset_sink.reset_n
input wire [3:0] MMS_addr, // avalon_slave.address
input wire MMS_write, // .write
input wire [31:0] MMS_writedata, // .writedata
output reg [0:2] Udrive, // M0PWMout.udrive
output reg [0:2] Ldrive, // .ldrive
output reg irqout // Trigirq.irq
);
reg [15:0] compvalhightoset[2:0];
reg [15:0] compvallowtoset[2:0];
reg [15:0] maxctrvaltoset;
reg updateon0;
reg updateonmax;
reg trigon0;
reg trigonmax;
reg update;
reg updateAck;
integer i;
reg CPUDone;
reg En;
always @(posedge clk or negedge reset_n) begin : proc_MMSinterface
if(~reset_n) begin
for (i = 0; i < 3; i = i + 1) begin
compvalhightoset[i] <= 16'hFFFF;
compvallowtoset[i] <= 16'h0000;
end
maxctrvaltoset <= 16'hFFFF;
updateon0 <= 1'b0;
updateonmax <= 1'b0;
trigon0 <= 1'b0;
trigonmax <= 1'b0;
update <= 1'b0;
En <= 1'b0;
CPUDone <= 1'b0;
end else begin
if(updateAck) begin
update <= 1'b0;
CPUDone <= 1'b1;
end else begin
if(irqout) begin
if(!CPUDone) begin
En <= 1'b0;
end
CPUDone <= 1'b0;
end
end
if(MMS_write) begin
case (MMS_addr)
4'h0:
compvallowtoset[0] <= MMS_writedata[15:0];
4'h1:
compvalhightoset[0] <= MMS_writedata[15:0];
4'h2:
compvallowtoset[1] <= MMS_writedata[15:0];
4'h3:
compvalhightoset[1] <= MMS_writedata[15:0];
4'h4:
compvallowtoset[2] <= MMS_writedata[15:0];
4'h5:
compvalhightoset[2] <= MMS_writedata[15:0];
4'h8:
maxctrvaltoset <= MMS_writedata[15:0];
4'h9:
En <= MMS_writedata[0];
4'hA:
updateon0 <= MMS_writedata[0];
4'hB:
updateonmax <= MMS_writedata[0];
4'hC:
trigon0 <= MMS_writedata[0];
4'hD:
trigonmax <= MMS_writedata[0];
4'hF:
update <= MMS_writedata[0];
default : /* default */;
endcase
end
end
end
reg [15:0] compvalhigh[2:0];
reg [15:0] compvallow[2:0];
reg [15:0] ctr;
reg [15:0] maxctrval;
reg countup;
always @(posedge clk or negedge reset_n) begin : proc_modulator
if(~reset_n) begin
ctr <= 16'h0000;
countup <= 1'b1;
for (i = 0; i < 3; i = i + 1) begin
compvalhigh[i] <= 16'hFFFF;
compvallow[i] <= 16'h0000;
end
maxctrval <= 16'hFFFF;
updateAck <= 1'b0;
irqout <= 1'b0;
end else begin
irqout <= 1'b0;
if(!update) begin
updateAck <= 1'b0;
end
if(countup) begin
if(ctr == maxctrval) begin
countup <= 0;
if(update & !updateAck & updateonmax) begin
updateAck <= 1'b1;
for (i = 0; i < 3; i = i + 1) begin
compvalhigh[i] <= compvalhightoset[i];
compvallow[i] <= compvallowtoset[i];
end
end
if(trigonmax) begin
irqout <= 1'b1;
end
end else begin
ctr <= ctr + 1;
end
end else begin
if(ctr == 16'h0000) begin
countup <= 1;
if(update & !updateAck & updateon0) begin
updateAck <= 1'b1;
for (i = 0; i < 3; i = i + 1) begin
compvalhigh[i] <= compvalhightoset[i];
compvallow[i] <= compvallowtoset[i];
end
end
if(trigon0) begin
irqout <= 1'b1;
end
end else begin
ctr <= ctr - 1;
end
end
if(maxctrvaltoset != maxctrval) begin
maxctrval <= maxctrvaltoset;
ctr <= 16'h0000;
countup <= 1'b1;
end
end
end
always @(posedge clk or negedge reset_n) begin : proc_compare
if(~reset_n) begin
Udrive[0] <= 1'b0;
Ldrive[0] <= 1'b0;
Udrive[1] <= 1'b0;
Ldrive[1] <= 1'b0;
Udrive[2] <= 1'b0;
Ldrive[2] <= 1'b0;
end else begin
Udrive[0] <= (ctr > compvalhigh[0]) & En;
Ldrive[0] <= (ctr < compvallow[0] ) & En;
Udrive[1] <= (ctr > compvalhigh[1]) & En;
Ldrive[1] <= (ctr < compvallow[1] ) & En;
Udrive[2] <= (ctr > compvalhigh[2]) & En;
Ldrive[2] <= (ctr < compvallow[2] ) & En;
end
end
endmodule