This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuart-tx.v
151 lines (145 loc) · 4.13 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
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module uarttx(
input rst,
input clk,
input tx_start,
input [7 : 0] tx_byte,
output tx, output tx_ready);
localparam STATE_IDLE = 0;
localparam STATE_START_BYTE = 1;
localparam STATE_BYTE_1 = 2;
localparam STATE_BYTE_2 = 3;
localparam STATE_BYTE_3 = 4;
localparam STATE_BYTE_4 = 5;
localparam STATE_BYTE_5 = 6;
localparam STATE_BYTE_6 = 7;
localparam STATE_BYTE_7 = 8;
localparam STATE_BYTE_8 = 9;
localparam STATE_STOP_BYTE1 = 10;
localparam STATE_STOP_BYTE2 = 11;
localparam STATE_FINISH = 12;
localparam STATE_WIDTH = $clog2(STATE_FINISH);
parameter CLK_SPEED = 12000000; //Hz
`ifdef IVERILOG
parameter BAUD_RATE = CLK_SPEED / 4; //Divide clock by 4 for simulation
`else
parameter BAUD_RATE = 19200;
`endif
localparam BAUD_COUNT = CLK_SPEED / BAUD_RATE;
localparam BAUD_REG_SIZE = $clog2(BAUD_COUNT);
reg [BAUD_REG_SIZE-1 : 0] baud_counter;
reg [STATE_WIDTH-1 : 0] tx_state;
reg tx_val;
reg [7 : 0] value;
assign tx_ready = (tx_state != STATE_IDLE) || (tx_start) ? 0 : 1;
assign tx = (tx_state == STATE_IDLE) ? 1 : tx_val;
always @(posedge clk) begin
if (rst) begin
tx_state <= 0;
tx_val <= 0;
baud_counter <= 0;
value <= 0;
end else begin
if (baud_counter == BAUD_COUNT) begin
baud_counter <= 0;
end else if (tx_state != STATE_IDLE) begin
baud_counter <= baud_counter + 1;
end
case (tx_state)
STATE_IDLE: begin
if (tx_start) begin
tx_state <= STATE_START_BYTE;
tx_val <= 1;
value <= tx_byte;
end
end
STATE_START_BYTE: begin
if (baud_counter == 0) begin
tx_val <= 0;
tx_state <= STATE_BYTE_1;
end
end
STATE_BYTE_1: begin
if (baud_counter == 0) begin
tx_val <= value[0];
tx_state <= STATE_BYTE_2;
end
end
STATE_BYTE_2: begin
if (baud_counter == 0) begin
tx_val <= value[1];
tx_state <= STATE_BYTE_3;
end
end
STATE_BYTE_3: begin
if (baud_counter == 0) begin
tx_val <= value[2];
tx_state <= STATE_BYTE_4;
end
end
STATE_BYTE_4: begin
if (baud_counter == 0) begin
tx_val <= value[3];
tx_state <= STATE_BYTE_5;
end
end
STATE_BYTE_5: begin
if (baud_counter == 0) begin
tx_val <= value[4];
tx_state <= STATE_BYTE_6;
end
end
STATE_BYTE_6: begin
if (baud_counter == 0) begin
tx_val <= value[5];
tx_state <= STATE_BYTE_7;
end
end
STATE_BYTE_7: begin
if (baud_counter == 0) begin
tx_val <= value[6];
tx_state <= STATE_BYTE_8;
end
end
STATE_BYTE_8: begin
if (baud_counter == 0) begin
tx_val <= value[7];
tx_state <= STATE_STOP_BYTE1;
end
end
STATE_STOP_BYTE1: begin
if (baud_counter == 0) begin
tx_val <= 1;
tx_state <= STATE_STOP_BYTE2;
end
end
STATE_STOP_BYTE2: begin
if (baud_counter == 0) begin
tx_val <= 1;
tx_state <= STATE_FINISH;
end
end
STATE_FINISH : begin
tx_state <= STATE_IDLE;
end
default: begin
tx_state <= STATE_IDLE;
end
endcase
end
end
endmodule