-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialReceiver.v
311 lines (278 loc) · 7.7 KB
/
SerialReceiver.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
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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer: [email protected]
//
// Create Date: 00:47:11 06/16/2021
// Design Name:
// Module Name: SerialReceiver
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module SerialReceiver(
output [7:0] DataOutput,
output NoData,
input Read,
input SerialLine,
input clk
);
wire data_fetched_by_controller;
wire data_available;
wire [8:0] data_receive_control;
wire [7:0] data_controller_fifo;
wire write_to_fifo;
Receiver SR
(
SerialLine,
data_fetched_by_controller,
data_available,
data_receive_control,
clk
);
ReceiveController RC
(
data_available,
data_controller_fifo,
write_to_fifo,
data_fetched_by_controller,
data_receive_control,
clk
);
FIFO RF
(
.DataI(data_controller_fifo),
.DataO(DataOutput),
.EF(NoData),
.R(Read),
.W(write_to_fifo),
.clk(clk)
);
endmodule
module ReceiverFIFO
#(
parameter Depth = 9,
parameter Width = 8
)
(
input [Width-1 : 0] DataI,
output reg [Width-1 : 0] DataO,
output reg FF, //full flag
output reg EF, //empty falg
input R, //read
input W, //write
input clk
);
//pointers
reg [$clog2(Depth)-1 : 0] _write_pointer = 0;
reg [$clog2(Depth)-1 : 0] _read_pointer = 0;
//memory
reg [Width-1 : 0] _memory[Depth-1: 0];
always @(posedge clk)
begin
EF = 0;
FF = 0;
// ----------------------------------------------- read from FIFO
if (R)
begin
if(_read_pointer != _write_pointer)
begin
DataO <= _memory[_read_pointer];
_read_pointer <= _read_pointer + 1;
if (_read_pointer == Depth)
_read_pointer <= 0;
end
else
begin
EF = 1;
end
end
// END----------------------------------------------- read from FIFO
// ----------------------------------------------- write to FIFO
if (W)
begin
if(_write_pointer != Depth-1)
begin
if(_write_pointer+1 == _read_pointer)
FF = 1;
end
else if (_read_pointer == 0)
begin
FF = 1;
end
// if no error occur write data
if (!FF)
begin
_memory[_write_pointer] <= DataI;
_write_pointer <= _write_pointer + 1;
if (_write_pointer == Depth)
begin
_write_pointer <= 0;
end
end
end
// END----------------------------------------------- write to FIFO
end
endmodule
module ReceiveController
#(
parameter Parity = 1, // use parity ?
parameter ParityEven = 1, // if use parity,do use even parity?
parameter DataLen = 8
)
(
input DataAvilable,
output reg [DataLen - 1 : 0] DataOutput,
output reg WriteData = 0,
output reg DataFetched = 0,
input [DataLen + Parity -1:0] DataInput,
input clk
);
parameter pre_fetch_data = 0;
parameter fetch_data = 1;
parameter save_data = 2;
parameter cancel_data = 3;
reg [1:0] mode = pre_fetch_data;
always @(posedge clk)
begin
if(mode == pre_fetch_data)
begin
if(DataAvilable)
begin
mode <= fetch_data;
DataFetched <= 1;
end
end
else if(mode == fetch_data)
begin
if(!Parity)
begin
DataOutput <= DataInput;
WriteData <= 1;
mode <= save_data;
end
else if(ParityEven)
begin
if(^DataInput == 0)
begin
DataOutput <= DataInput[DataLen-2:0];
WriteData <= 1;
end
mode <= save_data;
end
else
begin
if(^DataInput == 1)
begin
DataOutput <= DataInput[DataLen-2:0];
WriteData <= 1;
end
mode <= save_data;
end
end
else if(mode == save_data)
begin
WriteData <= 0;
DataFetched <= 0;
mode <= pre_fetch_data;
end
end
endmodule
module Receiver
#(
parameter ClkDivider = 5,
parameter DataLen = 9,
parameter StartBit = 1,
parameter StopBit = 1
)
(
input SerialLine,
input DataFetched,
output reg DataAvilable = 0,
output reg [DataLen-1:0] Data = {0},
input clk
);
//data
reg [DataLen-1:0] _data_temp = {0};
reg [$clog2(DataLen)-1:0] _save_pointer = 0;
reg [$clog2(StartBit + DataLen + StopBit) - 1:0] _receive_pointer = 0;
//mode
parameter receive_edge = 0;
parameter receive_value = 1;
parameter save = 2;
parameter delay = 3;
parameter save_wait = 4;
reg [2:0] mode = receive_edge;
//clk counter
reg [$clog2(ClkDivider)-1:0] clk_divider = ClkDivider - 1;
always @(posedge clk)
begin
if(mode == receive_edge)
begin
clk_divider = clk_divider + 1;
if(clk_divider == ClkDivider)
begin
clk_divider = 0;
_receive_pointer <= _receive_pointer + 1;
if(_receive_pointer < StartBit)
begin
if(SerialLine != 0)
begin
_receive_pointer <= 0;
clk_divider = ClkDivider - 1;
end
end
else if(_receive_pointer < StartBit + DataLen)
begin
// mode <= receive_value;
_data_temp[_save_pointer] <= SerialLine;
_save_pointer <= _save_pointer + 1;
end
else if(_receive_pointer < StartBit + DataLen + StopBit)
begin
//none
clk_divider = ClkDivider - 1;
end
else
begin
_receive_pointer <= 0;
DataAvilable <= 1;
mode <= save;
end
end
end
// else if(mode == receive_value)
// begin
// _save_pointer <= _save_pointer + 1;
// _data_temp[_save_pointer] <= SerialLine;
// mode <= receive_edge;
// end
// else if(mode == delay)
// begin
// mode <= receive_edge;
// end
else if(mode == save)
begin
// DataAvilable <= 1;
Data <= _data_temp;
mode <= save_wait;
end
else if(mode == save_wait)
begin
if(DataFetched)
begin
DataAvilable <= 0;
_save_pointer <= 0;
mode <= receive_edge;
end
end
end
endmodule