-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuartTx.sv
100 lines (99 loc) · 2.53 KB
/
uartTx.sv
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
module uartTx(
input var logic clock,
input var logic reset,
input var logic [7:0]buffer,
input var logic we,
output var logic uartTxPin,
output var logic busy2
);
logic clk;
logic [10:0]divCounter = 11'b0;
logic lwe;
logic busy;
always_ff @(posedge clock) begin
if (we==1'b1 && busy==1'b0) begin
lwe <= 1'b1;
txBuffer <= {1'b1, buffer, 1'b0};
end else if(we==1'b0 && busy==1'b0) begin
lwe <= 1'b0;
end
if (divCounter==11'd104-1) begin //57600 bps
divCounter <= 11'd0;
clk <= ~clk;
end else begin
divCounter <= divCounter+1;
end
end
logic [1:0] state, nextState;
// idle
// transmit
logic [3:0] transmissionCounter, nextTransmissionCounter;
logic [1:0] timingCounter, nextTimingCounter;
logic [9:0] txBuffer, register, nextRegister;
/*
logic lwe;
always_ff @(posedge we or negedge busy) begin
if(we==1'b1) begin
lwe <= 1;
txBuffer = {1'b1, buffer, 1'b0};
end else if(busy==1'b0) begin
lwe <= 0;
end
end
*/
always_comb begin
busy2 = 1'b0;
uartTxPin = 1'b1;
nextRegister = register;
nextState = state;
nextTransmissionCounter = transmissionCounter;
case (state)
2'd0: begin //idle
if(lwe==1'b1) begin
busy = 1'b1;
busy2 = 1'b1;
nextRegister = txBuffer;
nextState = 2'd1; //startbit
end else begin
busy = 1'b0;
busy2 = 1'b0;
end
end
2'd1: begin
busy2 = 1'b1;
busy = 1'b1;
nextState = 2'd2; //transmit
end
2'd2: begin //
busy = 1'b1;
busy2 = 1'b1;
if (transmissionCounter == 4'd9) begin
nextState = 2'd3; //fin
nextTransmissionCounter = 4'd0;
end else begin
uartTxPin = register[0];
nextRegister = {1'b1,register[9:1]};
nextTransmissionCounter = transmissionCounter+1;
end
end
2'd3: begin
busy2 = 1'b1;
busy = 1'b0;
nextState = 2'd0;
nextTransmissionCounter = 0;
end
default: begin
end
endcase
end
always_ff @(posedge clk or negedge reset) begin
if(!reset) begin
state <= 2'b0;
transmissionCounter <= 4'b0;
end else begin
state <= nextState;
register <= nextRegister;
transmissionCounter <= nextTransmissionCounter;
end
end
endmodule