-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuart_clock.v
255 lines (230 loc) · 5.39 KB
/
uart_clock.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* ------------------------------------------------ *
* Title : UART Clock Modules v1.2 *
* Project : Simple UART *
* ------------------------------------------------ *
* File : uart_clock.v *
* Author : Yigit Suoglu *
* Last Edit : 23/05/2021 *
* Licence : CERN-OHL-W *
* ------------------------------------------------ *
* Description : Clock generation for UART modules *
* ------------------------------------------------ */
//Generate a clock freqency for various baudrates, just clock divider
module uart_clk_gen#(parameter CLOCK_PERIOD = 10)(
input clk,
input rst,
input en,
output clk_uart,
input baseClock_freq, //0: 76,8kHz (13us) 1: 460,8kHz (2,17us)
input [2:0] divRatio); //Higher the value lower the freq
localparam sec6_5u = (6500 / CLOCK_PERIOD) - 1;
localparam sec1_08u = (1080 / CLOCK_PERIOD) - 1;
localparam counterWIDTH = $clog2(sec6_5u);
localparam CLKRST = 1'b0;
localparam CLKDEF = 1'b1;
wire en_rst;
reg baseClock;
wire countDONE;
reg [(counterWIDTH-1):0] counter;
wire [(counterWIDTH-1):0] countTO;
wire [7:0] clockArray;
reg [6:0] divClock;
assign en_rst = en | rst;
assign countTO = (baseClock_freq) ? sec1_08u : sec6_5u;
assign countDONE = (countTO == counter);
assign clockArray = {divClock, baseClock};
assign clk_uart = (en) ? clockArray[divRatio] : CLKDEF;
//Counter
always@(posedge clk)
begin
if(~en)
counter <= 0;
else
counter <= (countDONE) ? 0 : (counter + 1);
end
//Generate base clock with counter
always@(posedge clk)
begin
if(~en)
baseClock <= CLKRST;
else
baseClock <= (countDONE) ? ~baseClock : baseClock;
end
//Clock dividers
// 1/2
always@(posedge baseClock or negedge en_rst)
begin
if(~en_rst)
begin
divClock[0] <= CLKRST;
end
else
begin
divClock[0] <= ~divClock[0];
end
end
// 1/4
always@(posedge divClock[0] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[1] <= CLKRST;
end
else
begin
divClock[1] <= ~divClock[1];
end
end
// 1/8
always@(posedge divClock[1] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[2] <= CLKRST;
end
else
begin
divClock[2] <= ~divClock[2];
end
end
// 1/16
always@(posedge divClock[2] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[3] <= CLKRST;
end
else
begin
divClock[3] <= ~divClock[3];
end
end
// 1/32
always@(posedge divClock[3] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[4] <= CLKRST;
end
else
begin
divClock[4] <= ~divClock[4];
end
end
// 1/64
always@(posedge divClock[4] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[5] <= CLKRST;
end
else
begin
divClock[5] <= ~divClock[5];
end
end
// 1/128
always@(posedge divClock[5] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[6] <= CLKRST;
end
else
begin
divClock[6] <= ~divClock[6];
end
end
endmodule
//Generate a clock freqency for non standard higher frequencies
module uart_clk_gen_hs(
input clk,
input rst,
input en,
output clk_uart,
input [1:0] divRatio); //Higher the value lower the freq
localparam CLKRST = 1'b0;
localparam CLKDEF = 1'b1;
wire en_rst;
reg baseClock;
reg [3:0] divClock;
assign en_rst = en | rst;
assign clk_uart = (en) ? divClock[divRatio] : CLKDEF;
//Generate base clock with counter
always@(posedge clk)
begin
if(~en)
baseClock <= CLKRST;
else
baseClock <= ~baseClock;
end
//Clock dividers
// 1/2
always@(posedge baseClock or negedge en_rst)
begin
if(~en_rst)
begin
divClock[0] <= CLKRST;
end
else
begin
divClock[0] <= ~divClock[0];
end
end
// 1/4
always@(posedge divClock[0] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[1] <= CLKRST;
end
else
begin
divClock[1] <= ~divClock[1];
end
end
// 1/8
always@(posedge divClock[1] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[2] <= CLKRST;
end
else
begin
divClock[2] <= ~divClock[2];
end
end
// 1/16
always@(posedge divClock[2] or negedge en_rst)
begin
if(~en_rst)
begin
divClock[3] <= CLKRST;
end
else
begin
divClock[3] <= ~divClock[3];
end
end
endmodule
//Generate a clock freqency for various baudrates, just clock divider
module uart_clk_en(
input clk,
input rst,
input ext_uart_clk,
input en,
output clk_uart);
reg clk_enabled;
assign clk_uart = ext_uart_clk | clk_enabled;
always@(posedge clk or posedge rst)
begin
if(rst)
clk_enabled <= 1'b0;
else
case(clk_enabled)
1'b0: clk_enabled <= ext_uart_clk & en;
1'b1: clk_enabled <= ~ext_uart_clk | en;
endcase
end
endmodule