-
Notifications
You must be signed in to change notification settings - Fork 2
/
uart_tx.v
185 lines (166 loc) · 4.61 KB
/
uart_tx.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
module uart_tx (clk,baud_rate_select,start,Byte_To_Send,Tx_Active,Tx_Serial,Tx_Done,rst);
//#(parameter baud_rate)
input clk;
input rst;
input [2:0] baud_rate_select;
input start;
input [7:0] Byte_To_Send;
output Tx_Active;
output reg Tx_Serial;
output Tx_Done;
parameter IDLE = 3'b000;
parameter TX_START_BIT = 3'b001;
parameter TX_DATA_BITS = 3'b010;
parameter TX_STOP_BIT = 3'b011;
parameter RESET = 3'b100;
reg [10:0] baud_rate;
reg [2:0] State;
reg [10:0] clk_count;
reg [2:0] bit_index;
reg [7:0] Data_Byte;
reg Tx_Done;
reg Tx_Enable;
always @(*)
begin
case (baud_rate_select)
3'b000:
begin
baud_rate = 11'b10000010010; //9600(1042)
end
3'b001:
begin
baud_rate = 11'b01010110111; //14400(695)
end
3'b010:
begin
baud_rate = 11'b01000001001; //19200(521)
end
3'b011:
begin
baud_rate = 11'b00100000101; //38400(261)
end
3'b100:
begin
baud_rate = 11'b00010101110; //57600(174)
end
3'b101:
begin
baud_rate = 11'b00001010111; //115200(87)
$display("Baud rate = %d\n",baud_rate);
end
3'b110:
begin
baud_rate = 11'b00001001111; //128000(79)
end
3'b111:
begin
baud_rate = 11'b00000100111; //256000(39)
end
default:
baud_rate = 11'b10000010010; //9600
endcase
end
always @(posedge clk or posedge rst)
begin
if (rst)
begin
State <= IDLE;
clk_count <= 11'b00000000000;
bit_index <= 3'b000;
Data_Byte <= 8'b00000000;
Tx_Done <= 1'b0;
Tx_Enable <= 1'b0;
end
else begin
case (State)
IDLE :
begin
Tx_Serial <= 1'b1; // Drive Line High for Idle
Tx_Done <= 1'b0;
clk_count <= 11'b00000000000;
bit_index <= 3'b000;
if (start == 1'b1)
begin
Tx_Enable <= 1'b1;
Data_Byte <= Byte_To_Send;
State <= TX_START_BIT;
end
else
State <= IDLE;
end // case: IDLE
// Send out Start Bit. Start bit = 0
TX_START_BIT :
begin
Tx_Serial <= 1'b0;
// Wait baud_rate-1 clock cycles for start bit to finish
if (clk_count < (baud_rate - 1'b1))
begin
clk_count <= clk_count + 1'b1;
State <= TX_START_BIT;
end
else
begin
clk_count <= 11'b00000000000;
$display("start bit sent\n");
State <= TX_DATA_BITS;
end
end // case: TX_START_BIT
// Wait baud_rate-1 clock cycles for data bits to finish
TX_DATA_BITS :
begin
Tx_Serial <= Data_Byte[bit_index];
if (clk_count < (baud_rate-1'b1))
begin
clk_count <= clk_count + 1'b1;
State <= TX_DATA_BITS;
end
else
begin
clk_count <= 11'b00000000000;
$display("bit %b sent\n",bit_index);
// Check if we have sent out all bits
if (bit_index == 3'b111)
begin
bit_index <= 3'b000;
$display("all bits sent\n");
State <= TX_STOP_BIT;
end
else
begin
bit_index <= bit_index + 1'b1;
State <= TX_DATA_BITS;
end
end
end // case: TX_DATA_BITS
// Send out Stop bit. Stop bit = 1
TX_STOP_BIT :
begin
Tx_Serial <= 1'b1;
// Wait baud_rate-1 clock cycles for Stop bit to finish
if (clk_count < (baud_rate-1'b1))
begin
clk_count <= clk_count + 1'b1;
State <= TX_STOP_BIT;
end
else
begin
$display("stop bit sent\n");
Tx_Done <= 1'b1;
clk_count <= 11'b00000000000;
State <= RESET;
Tx_Enable <= 1'b0;
end
end // case: TX_STOP_BIT
// Stay here 1 clock
RESET :
begin
Tx_Done <= 1'b1;
State <= IDLE;
end
default :
State <= IDLE;
endcase
end
end
assign Tx_Active = Tx_Enable;
endmodule