Skip to content

Commit 3760990

Browse files
committed
Added UART-like shifters for for simple synchronous messaging inside the FPGA or between FPGAs
1 parent 68922a3 commit 3760990

File tree

4 files changed

+393
-0
lines changed

4 files changed

+393
-0
lines changed

uart_rx_shifter.sv

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//------------------------------------------------------------------------------
2+
// uart_rx_shifter.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// UART-like shifter for simple synchronous messaging inside the FPGA or between FPGAs
8+
// See also `uart_tx_shifter.sv` for TX part
9+
//
10+
// TX and RX parts should share one clock source
11+
// Capable of continious stream transfer when tx_start is held constant 1'b1
12+
// Any reasonable start bit count,data bit count, stop bit count
13+
//
14+
15+
16+
/* --- INSTANTIATION TEMPLATE BEGIN ---
17+
18+
uart_rx_shifter #(
19+
.START_BITS( 1 ),
20+
.DATA_BITS( 8 ),
21+
.STOP_BITS( 2 ),
22+
.SYNCHRONIZE_RXD( 0 ) // 0 - disabled; 1 - enabled
23+
) rx1 (
24+
.clk( clk ),
25+
.nrst( 1'b1 ),
26+
.rx_data( ),
27+
.rx_valid( ),
28+
.rxd( )
29+
);
30+
31+
--- INSTANTIATION TEMPLATE END ---*/
32+
33+
34+
module uart_rx_shifter #(
35+
36+
bit [7:0] START_BITS = 1, // must be >=1
37+
bit [7:0] DATA_BITS = 4, // must be >=1
38+
bit [7:0] STOP_BITS = 2, // must be >=1
39+
40+
bit SYNCHRONIZE_RXD = 0 // its better to synchronize when rxd input
41+
// is actually an FPGA pin
42+
)(
43+
input clk, // transmitter and receiver should use
44+
input nrst, // the same clock
45+
46+
output logic [DATA_BITS-1:0] rx_data = '0, // output data
47+
output logic rx_valid = '0, // read strobe
48+
49+
input rxd
50+
);
51+
52+
localparam TOTAL_BITS = START_BITS + DATA_BITS + STOP_BITS;
53+
54+
logic [TOTAL_BITS-1:0] rx_data_buf = '1;
55+
56+
logic rxd_sync;
57+
delay #(
58+
.LENGTH( 2 ),
59+
.WIDTH( 1 )
60+
) rxd_SYNC_ATTR (
61+
.clk( clk ),
62+
.nrst( 1'b1 ),
63+
.ena( 1'b1 ),
64+
65+
.in( rxd ),
66+
.out( rxd_sync )
67+
);
68+
69+
logic start_detected;
70+
assign start_detected = ~|rx_data_buf[DATA_BITS+STOP_BITS+:START_BITS];
71+
logic stop_detected;
72+
assign stop_detected = &rx_data_buf[0+:STOP_BITS];
73+
logic data_valid;
74+
assign data_valid = start_detected && stop_detected;
75+
76+
always_ff @(posedge clk) begin
77+
if( ~nrst ) begin
78+
rx_data_buf[TOTAL_BITS-1:0] <= '1;
79+
80+
rx_data[DATA_BITS-1:0] <= '0;
81+
rx_valid <= 1'b0;
82+
end else begin
83+
if( data_valid ) begin
84+
// clear rx_data_buf if valid message is already detected
85+
rx_data_buf[TOTAL_BITS-1:0] <= { {(TOTAL_BITS-1){1'b1}},
86+
(SYNCHRONIZE_RXD ? rxd_sync : rxd) };
87+
end else begin
88+
// simple shifter, MSB first
89+
rx_data_buf[TOTAL_BITS-1:0] <= { rx_data_buf[TOTAL_BITS-2:0],
90+
(SYNCHRONIZE_RXD ? rxd_sync : rxd) };
91+
end
92+
93+
// buffering valid messages
94+
if( data_valid ) begin
95+
rx_data[DATA_BITS-1:0] <= rx_data_buf[STOP_BITS+:DATA_BITS];
96+
rx_valid <= 1'b1;
97+
end else begin
98+
rx_valid <= 1'b0;
99+
end
100+
end
101+
end
102+
103+
104+
endmodule
105+

uart_tx_rx_shifter_tb.png

129 KB
Loading

uart_tx_rx_shifter_tb.sv

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//------------------------------------------------------------------------------
2+
// uart_tx_rx_shifter_tb.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// testbench for uart_tx_rx_shifter_tb.sv module
8+
9+
`timescale 1ns / 1ps
10+
11+
module uart_tx_rx_shifter_tb();
12+
13+
logic clk200;
14+
initial begin
15+
#0 clk200 = 1'b0;
16+
forever
17+
#2.5 clk200 = ~clk200;
18+
end
19+
20+
logic clk400;
21+
initial begin
22+
#0 clk400 = 1'b0;
23+
forever
24+
#1.25 clk400 = ~clk400;
25+
end
26+
27+
logic clk33;
28+
initial begin
29+
#0 clk33 = 1'b0;
30+
forever
31+
#15.151 clk33 = ~clk33;
32+
end
33+
34+
logic rst;
35+
initial begin
36+
#0 rst = 1'b0;
37+
#10.2 rst = 1'b1;
38+
#5 rst = 1'b0;
39+
end
40+
41+
logic nrst;
42+
assign nrst = ~rst;
43+
44+
logic rst_once;
45+
initial begin
46+
#0 rst_once = 1'b0;
47+
#10.2 rst_once = 1'b1;
48+
#5 rst_once = 1'b0;
49+
end
50+
51+
logic nrst_once;
52+
assign nrst_once = ~rst_once;
53+
54+
logic [31:0] DerivedClocks;
55+
clk_divider #(
56+
.WIDTH( 32 )
57+
) cd1 (
58+
.clk( clk200 ),
59+
.nrst( nrst_once ),
60+
.ena( 1'b1 ),
61+
.out( DerivedClocks[31:0] )
62+
);
63+
64+
logic [31:0] E_DerivedClocks;
65+
edge_detect ed1[31:0] (
66+
.clk( {32{clk200}} ),
67+
.nrst( {32{nrst_once}} ),
68+
.in( DerivedClocks[31:0] ),
69+
.rising( E_DerivedClocks[31:0] ),
70+
.falling( ),
71+
.both( )
72+
);
73+
74+
logic [31:0] RandomNumber1;
75+
c_rand rng1 (
76+
.clk( clk200 ),
77+
.rst( 1'b0 ),
78+
.reseed( rst_once ),
79+
.seed_val( DerivedClocks[31:0] ^ (DerivedClocks[31:0] << 1) ),
80+
.out( RandomNumber1[15:0] )
81+
);
82+
83+
c_rand rng2 (
84+
.clk( clk200 ),
85+
.rst( 1'b0 ),
86+
.reseed( rst_once ),
87+
.seed_val( DerivedClocks[31:0] ^ (DerivedClocks[31:0] << 2) ),
88+
.out( RandomNumber1[31:16] )
89+
);
90+
91+
92+
// Module under test ==========================================================
93+
94+
`define STB 1
95+
`define DB 8
96+
`define SPB 2
97+
98+
logic tx_busy;
99+
logic serial_data;
100+
101+
logic start;
102+
103+
// continious transfer (no automatic data check implemented)
104+
//assign start = 1'b1;
105+
106+
// random transfer (features automatic data check)
107+
assign start = ~tx_busy && &RandomNumber1[11:8];
108+
109+
uart_tx_shifter #(
110+
.START_BITS( `STB ),
111+
.DATA_BITS( `DB ),
112+
.STOP_BITS( `SPB )
113+
) tx1 (
114+
.clk( clk200 ),
115+
.nrst( nrst_once ),
116+
.tx_data( RandomNumber1[`DB-1:0] ),
117+
.tx_start( start ),
118+
.tx_busy( tx_busy ),
119+
.txd( serial_data )
120+
);
121+
122+
logic data_valid;
123+
logic [`DB-1:0] data_rcvd;
124+
uart_rx_shifter #(
125+
.START_BITS( `STB ),
126+
.DATA_BITS( `DB ),
127+
.STOP_BITS( `SPB ),
128+
.SYNCHRONIZE_RXD( 1 ) // 0 - disabled; 1 - enabled
129+
) rx1 (
130+
.clk( clk200 ),
131+
.nrst( nrst_once ),
132+
.rx_data( data_rcvd[`DB-1:0] ),
133+
.rx_valid( data_valid ),
134+
.rxd( serial_data )
135+
);
136+
137+
138+
logic [`DB-1:0] data_sent;
139+
fifo #(
140+
.DEPTH( 8 ),
141+
.DATA_W( `DB )
142+
) data_check_fifo (
143+
.clk( clk200 ),
144+
.rst( 1'b0 ),
145+
146+
.w_req( start ),
147+
.w_data( RandomNumber1[`DB-1:0] ),
148+
149+
.r_req( data_valid ),
150+
.r_data( data_sent[`DB-1:0] ),
151+
152+
.cnt( ),
153+
.empty( ),
154+
.full( )
155+
);
156+
157+
logic success = 1'b1;
158+
always_ff @(posedge clk200) begin
159+
if( data_valid ) begin
160+
if( data_sent[`DB-1:0] != data_rcvd[`DB-1:0] ) begin
161+
success <= 1'b0;
162+
end
163+
end
164+
end
165+
166+
endmodule

uart_tx_shifter.sv

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//------------------------------------------------------------------------------
2+
// uart_tx_shifter.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// UART-like shifter for simple synchronous messaging inside the FPGA or between FPGAs
8+
// See also `uart_rx_shifter.sv` for RX part
9+
//
10+
// TX and RX parts should share one clock source
11+
// Capable of continious stream transfer when tx_start is held constant 1'b1
12+
// Any reasonable start bit count,data bit count, stop bit count
13+
//
14+
15+
16+
/* --- INSTANTIATION TEMPLATE BEGIN ---
17+
18+
uart_tx_shifter #(
19+
.START_BITS( 1 ),
20+
.DATA_BITS( 8 ),
21+
.STOP_BITS( 2 )
22+
) tx1 (
23+
.clk( clk ),
24+
.nrst( 1'b1 ),
25+
.tx_data( ),
26+
.tx_start( ),
27+
.tx_busy( ),
28+
.txd( )
29+
);
30+
31+
--- INSTANTIATION TEMPLATE END ---*/
32+
33+
34+
module uart_tx_shifter #(
35+
36+
bit [7:0] START_BITS = 1, // must be >=1
37+
bit [7:0] DATA_BITS = 4, // must be >=1
38+
bit [7:0] STOP_BITS = 2 // must be >=1
39+
)(
40+
input clk, // transmitter and receiver should use
41+
input nrst, // the same clock
42+
43+
input [DATA_BITS-1:0] tx_data, // input data get captured on write strobe
44+
input tx_start, // write strobe itself
45+
output tx_busy, // tx_busy fall on the last stop bit
46+
47+
output logic txd = 1'b1
48+
);
49+
50+
logic [DATA_BITS-1:0] tx_data_buf = '0;
51+
logic [7:0] state_cntr = '0;
52+
53+
enum int unsigned { STOP, START, DATA } tx_state = STOP;
54+
55+
always_ff @(posedge clk) begin
56+
if( ~nrst ) begin
57+
tx_state <= STOP;
58+
59+
tx_data_buf[DATA_BITS-1:0] <= '0;
60+
state_cntr[7:0] <= '0;
61+
62+
txd <= 1'b1;
63+
end else begin
64+
65+
case( tx_state )
66+
STOP: begin
67+
68+
txd <= 1'b1;
69+
if( state_cntr[7:0] != '0 ) begin
70+
// holding stop bits
71+
state_cntr[7:0]--;
72+
end else begin
73+
// idle state after stop bits
74+
75+
// no need for edge detector here because tx_state changes instantly
76+
// after the first active tx_start cycle
77+
if( tx_start ) begin
78+
// buffering input data
79+
tx_data_buf[DATA_BITS-1:0] <= tx_data[DATA_BITS-1:0];
80+
state_cntr[7:0] <= START_BITS-1;
81+
tx_state <= tx_state.next();
82+
end // tx_start
83+
end // state_cntr
84+
85+
end
86+
START: begin
87+
88+
txd <= 1'b0;
89+
if( state_cntr[7:0] != '0 ) begin
90+
// holding start bits
91+
state_cntr[7:0]--;
92+
end else begin
93+
// transition
94+
state_cntr[7:0] <= DATA_BITS-1;
95+
tx_state <= tx_state.next();
96+
end // state_cntr
97+
98+
end
99+
DATA: begin
100+
101+
// setting data, MSB first
102+
txd <= tx_data_buf[state_cntr[7:0]];
103+
104+
if( state_cntr[7:0] != '0 ) begin
105+
state_cntr[7:0]--;
106+
end else begin
107+
// transition
108+
state_cntr[7:0] <= STOP_BITS-1;
109+
tx_state <= tx_state.next();
110+
end // state_cntr
111+
112+
end
113+
endcase // tx_state
114+
115+
end
116+
end
117+
118+
assign tx_busy = ~( (tx_state == STOP) && (state_cntr[7:0] == '0) );
119+
120+
121+
endmodule
122+

0 commit comments

Comments
 (0)