-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuart_send.v
More file actions
135 lines (119 loc) · 3.96 KB
/
uart_send.v
File metadata and controls
135 lines (119 loc) · 3.96 KB
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
//**********************************************************************
// Project: TDPS
// File: UART_send.v
// Description: Send 8-bit data through UART
// Author: Ruiqi Tang
// Modified from: ppqppl (https://www.cnblogs.com/ppqppl/articles/17461611.html)
// Timestamp:
//----------------------------------------------------------------------
// Code Revision History:
// Ver: | Author | Mod. Date | Changes Made:
// v1.0.0 | R.T. | 2024/05/15 | Initial version
//**********************************************************************
module UART_send(
clk,
rstn,
tx_flag_i,
tx_data_i,
tx_done,
uart_tx
);
//**********************************************************************
// --- Parameter
//**********************************************************************
parameter CLK_FREQ = 27_000_000;
parameter BAUD_RATE = 115200;
localparam BAUD_CLK = CLK_FREQ/BAUD_RATE;
//**********************************************************************
// --- Input/Output Declaration
//**********************************************************************
input wire clk;
input wire rstn;
input wire [7:0] tx_data_i;
input wire tx_flag_i;
output reg uart_tx;
output reg tx_done;
//**********************************************************************
// --- Internal Signal Declaration
//**********************************************************************
reg tx_en;
reg flag_bit;
reg [8:0] cnt_baud;
reg [3:0] cnt_bit;
//**********************************************************************
// --- Main Core
//**********************************************************************
// enable tx
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
tx_en <= 1'b0;
end
else if (cnt_bit == 4'd9 && flag_bit == 1'b1) begin
tx_en <= 1'b0;
end
else if (tx_flag_i) begin
tx_en <= 1'b1;
end
end
// baud counter
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
cnt_baud <= 9'd0;
end
else if (cnt_baud == BAUD_CLK-1 || tx_en == 0) begin
cnt_baud <= 9'd0;
end
else begin
cnt_baud <= cnt_baud + 9'd1;
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
flag_bit <= 1'b0;
end
else if (cnt_baud == 9'd1) begin
flag_bit <= 1'b1;
end
else begin
flag_bit <= 1'b0;
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
cnt_bit <= 4'd0;
tx_done <= 1'd0;
end
else if (cnt_bit == 4'd9 && flag_bit == 1'b1) begin
cnt_bit <= 4'd0;
tx_done <= 1'b1;
end
else if (flag_bit == 1'b1 && tx_en == 1'b1) begin
cnt_bit <= cnt_bit + 4'd1;
tx_done <= 1'b0;
end
else begin
cnt_bit <= cnt_bit;
tx_done <= 1'b0;
end
end
always @(posedge clk or negedge rstn) begin
if(!rstn) begin
uart_tx <= 1'd1;
end
else if (flag_bit == 1'b1) begin
case (cnt_bit)
0: uart_tx <= 1'd0;
1: uart_tx <= tx_data_i[0];
2: uart_tx <= tx_data_i[1];
3: uart_tx <= tx_data_i[2];
4: uart_tx <= tx_data_i[3];
5: uart_tx <= tx_data_i[4];
6: uart_tx <= tx_data_i[5];
7: uart_tx <= tx_data_i[6];
8: uart_tx <= tx_data_i[7];
9: uart_tx <= 1'd1;
default : uart_tx <= 1'd1;
endcase
end
end
endmodule