Serial loopback

Need some help on a project you are working on or got stuck on a tutorial? Post your questions here.
Post Reply
animan
Posts: 3
Joined: August 6th, 2013, 12:52 am

Serial loopback

Post by animan » August 18th, 2013, 4:36 pm

Hello,

I have created my first project to loopback serial data. After some trial and error I have kinda got it working, however, there seems to be a lag of a character, i.e, I need to type in the next character to see the previous character echoe'd back.
I would appreciate if any FPGA/Verilog gurus can please review and comment/suggest how to improve it and fix the lag. Enclosed is the code (modeled after Helloworld).
Thanks in advance for your help.

--------------serial_loopback.v-----------------
module serial_loopback(
input clk,
input rst,
output [7:0] tx_data,
output reg new_tx_data,
input tx_busy,
input [7:0] rx_data,
input new_rx_data
);

reg [7:0] tx_data_d, tx_data_q;
assign tx_data = tx_data_q;

always @(*) begin
new_tx_data = 1'b0;
if (new_rx_data) begin
if (!tx_busy) begin
tx_data_d = rx_data; // Loopback, send rx data
new_tx_data = 1'b1; // Clk it
end
end
end

always @(posedge clk) begin
tx_data_q <= tx_data_d;
end

endmodule

----------- mojo_top.v -----------
module mojo_top(
input clk,
input rst_n,
input cclk,
output[7:0]led,
output spi_miso,
input spi_ss,
input spi_mosi,
input spi_sck,
output [3:0] spi_channel,
input avr_tx,
output avr_rx,
input avr_rx_busy
);

wire rst = ~rst_n;

assign led = 8'b0;
wire [7:0] tx_data;
wire new_tx_data;
wire [7:0] rx_data;
wire new_rx_data;

avr_interface avr_interface (
.clk(clk),
.rst(rst),
.cclk(cclk),
.spi_miso(spi_miso),
.spi_mosi(spi_mosi),
.spi_sck(spi_sck),
.spi_ss(spi_ss),
.spi_channel(spi_channel),
.tx(avr_rx), // FPGA tx goes to AVR rx
.rx(avr_tx),
.channel(4'd15), // invalid channel disables the ADC
.new_sample(),
.sample(),
.sample_channel(),
.tx_data(tx_data),
.new_tx_data(new_tx_data),
.tx_busy(tx_busy),
.tx_block(avr_rx_busy),
.rx_data(rx_data),
.new_rx_data(new_rx_data)
);

serial_loopback sloopback(
.clk(clk),
.rst(rst),
.tx_data(tx_data),
.new_tx_data(new_tx_data),
.tx_busy(tx_busy),
.rx_data(rx_data),
.new_rx_data(new_rx_data)
);

endmodule

Post Reply