CORE-876. Take shared PEI files and add them to library to prevent copying

This commit is contained in:
David Steele 2023-01-20 16:22:49 +00:00
commit 091847d151
5 changed files with 1861 additions and 0 deletions

View file

@ -0,0 +1,242 @@
//----------------------------------------------------------------------------
// Module Description
//
//
//----------------------------------------------------------------------------
// Revision Date Author Notes
//
//
//----------------------------------------------------------------------------
`resetall
`timescale 1ns/1ps
module unbuffered_uart_tb ;
// Internal declarations
localparam ST_TX = 3'd0;
localparam ST_TX2 = 3'd1;
localparam ST_TX3 = 3'd2;
localparam ST_RX = 3'd3;
localparam ST_RX2 = 3'd4;
reg clk96 = 1'b0;
reg [7:0] data_word = 8'h5a;
wire mosi;
wire miso;
wire master_de;
wire [7:0] master_rx_d;
wire master_rx_dv;
wire master_rx_err;
reg [7:0] master_tx_d = 8'd0;
reg master_tx_dv = 1'b0;
reg master_tx_en = 1'b0;
wire master_tx_rdy;
wire master_tx_busy;
reg [2:0] master_state = ST_TX;
reg [7:0] master_rx_d_f1 = 8'd0;
reg [7:0] master_rx_d_f2 = 8'd0;
wire slave_de;
wire [7:0] slave_rx_d;
wire slave_rx_dv;
wire slave_rx_err;
reg [7:0] slave_tx_d = 8'd0;
reg slave_tx_dv = 1'b0;
reg slave_tx_en = 1'b0;
wire slave_tx_rdy;
wire slave_tx_busy;
reg [2:0] slave_state = ST_RX;
reg [7:0] slave_rx_d_f1 = 8'd0;
reg [7:0] slave_rx_d_f2 = 8'd0;
reg rs485_a = 1'bZ;
reg rs485_b = 1'bZ;
// ### Please start your Verilog code here ###
// Clock gen
always #5.208 clk96 <= ~clk96;
// Control loop
always @(posedge clk96)
begin
// Master State Machine
case (master_state)
ST_TX : begin
master_tx_en <= 1'b1;
master_tx_d <= data_word;
master_tx_dv <= master_tx_rdy;
if (master_tx_rdy)
begin
master_state <= ST_TX2;
data_word <= {data_word[6:0], data_word[7]};
end
end
ST_TX2 : begin
master_tx_d <= data_word;
master_tx_dv <= master_tx_rdy;
if (master_tx_rdy)
begin
master_state <= ST_TX3;
data_word <= {data_word[6:0], data_word[7]};
end
end
ST_TX3 : begin
master_tx_dv <= 1'b0;
if (~master_tx_busy)
begin
master_tx_en <= 1'b0;
master_state <= ST_RX;
end
end
ST_RX : begin
if (master_rx_dv)
begin
master_rx_d_f1 <= master_rx_d;
master_state <= ST_RX2;
end
end
default : begin
if (master_rx_dv)
begin
master_rx_d_f2 <= master_rx_d;
master_state <= ST_TX;
end
end
endcase
// Slave State Machine
case (slave_state)
ST_RX : begin
slave_tx_en <= 1'b0;
slave_tx_dv <= 1'b0;
if (slave_rx_dv)
begin
slave_rx_d_f1 <= slave_rx_d;
slave_state <= ST_RX2;
end
end
ST_RX2 : begin
if (slave_rx_dv)
begin
slave_rx_d_f2 <= slave_rx_d;
slave_state <= ST_TX;
end
end
ST_TX : begin
slave_tx_en <= 1'b1;
slave_tx_d <= slave_rx_d_f1;
slave_tx_dv <= slave_tx_rdy;
if (slave_tx_rdy)
begin
slave_state <= ST_TX2;
end
end
ST_TX2 : begin
slave_tx_d <= slave_rx_d_f2;
slave_tx_dv <= slave_tx_rdy;
if (slave_tx_rdy)
begin
slave_state <= ST_TX3;
end
end
default : begin
slave_tx_dv <= 1'b0;
if (~slave_tx_busy)
begin
slave_tx_en <= 1'b0;
slave_state <= ST_RX;
end
end
endcase
end
// Instantiate two UARTS
unbuffered_uart #(
.OVERSAMPLE_RATE ( 24 ),
.ENABLE_CLOCK_DIVIDER ( 1 )
) master_inst(
.clk ( clk96 ),
.uart_de_out ( master_de ),
.uart_rx_data_out ( master_rx_d ),
.uart_rx_dv_out ( master_rx_dv ),
.uart_rx_en_in ( 1'b1 ),
.uart_rx_in ( miso ),
.uart_rx_error_out ( master_rx_err ),
.uart_tx_data_in ( master_tx_d ),
.uart_tx_dv_in ( master_tx_dv ),
.uart_tx_en_in ( master_tx_en ),
.uart_tx_out ( mosi ),
.uart_tx_rdy_out ( master_tx_rdy ),
.uart_tx_busy_out ( master_tx_busy )
);
unbuffered_uart #(
.OVERSAMPLE_RATE ( 24 ),
.ENABLE_CLOCK_DIVIDER ( 1 )
) slave_inst(
.clk ( clk96 ),
.uart_de_out ( slave_de ),
.uart_rx_data_out ( slave_rx_d ),
.uart_rx_dv_out ( slave_rx_dv ),
.uart_rx_en_in ( 1'b1 ),
.uart_rx_in ( mosi ),
.uart_rx_error_out ( slave_rx_err ),
.uart_tx_data_in ( slave_tx_d ),
.uart_tx_dv_in ( slave_tx_dv ),
.uart_tx_en_in ( slave_tx_en ),
.uart_tx_out ( miso ),
.uart_tx_rdy_out ( slave_tx_rdy ),
.uart_tx_busy_out ( slave_tx_busy )
);
// RS485 transceivers
always @(*)
begin
if (master_de & slave_de)
begin
rs485_a <= 1'bX;
rs485_b <= 1'bX;
end
else if (master_de)
begin
rs485_a <= mosi;
rs485_b <= ~mosi;
end
else if (slave_de)
begin
rs485_a <= miso;
rs485_b <= ~miso;
end
else
begin
rs485_a <= 1'bZ;
rs485_b <= 1'bZ;
end
end
endmodule

View file

@ -0,0 +1,912 @@
//----------------------------------------------------------------------------
// Module Description
//
// This is a simple debug serial port, providing terminal access to an internal bus.
// The port does not require a FIFO, as long as the UART Baud rate is significantly slower than the system clock (10:1 should be sufficient)
// A full-duplex link is assumed.
//
// Supported commands are :
// R 0xAA
// W 0xAA 0xDDDDDDDD
//
// The serial port automatically inserts whitespaces, carriage returns, and "0x", so the text entered to perform a write
// is "WAADDDDDDDD"
//
// Address and data width are determined at instantiaton through parameters. Valid range is 1 to 32
//
// Interface:
// To UART
// rx_dv shall be valid for 1 clock pulse
// rx_data shall be valid when dv is high
// tx_dv is valid for 1 clock pulse
// tx_data is valid when tx_dv is high.
// tx_dv shall not be asserted unless tx_rdy is asserted.
// tx_busy is not used (this is a subset of tx_rdy).
//
// To System Bus
// wr_en is asserted for 1 clock cycle
// wr_data, add, src_id_out are valid when wr_en is asserted.
// rd_en is asserted for 1 clock cycle (this allows interfacing to systems that support posted reads).
// After rd_en is asserted, the unit waits for rd_dv to be valid. A timeout should probably be added to prevent hangs by reads to non-existent addresses.
// src_id is provided in the interface to allow posted reads to be received out-of-order and be correctly routed.
// Posted reads (and src_id) are not implemented in this version.
//
//----------------------------------------------------------------------------
// Revision Date Author Notes
//
// 01 22-02-2009 Gordon Leather First written
//
//----------------------------------------------------------------------------
`resetall
`timescale 1ns/1ps
module pei_debug_serial_port_0 #(
parameter ADDRESS_WIDTH = 8,
parameter DATA_WIDTH = 32
)(
input wire clk,
input wire tx_rdy_in,
input wire tx_busy_in,
output wire tx_en_out,
output wire tx_dv_out,
output wire [7:0] tx_data_out,
input wire rx_err_in,
input wire rx_dv_in,
input wire [7:0] rx_data_in,
output wire rx_en_out,
output wire [ADDRESS_WIDTH-1:0] add_out,
output wire [DATA_WIDTH-1:0] wr_data_out,
output wire [5:0] src_id_out,
output wire wr_en_out,
output wire rd_en_out,
input wire [DATA_WIDTH-1:0] rd_data_in, //Data fetch by FPGA after an a read request
input wire [5:0] src_id_in,
input wire rd_dv_in,
input wire rd_32b_in
);
// Address and Data character counts
localparam ADDRESS_CHARS = ((ADDRESS_WIDTH + 3) / 4); // Ensure character count is a full nibble
localparam DATA_CHARS = ((DATA_WIDTH + 3) / 4); // Ensure character count is a full nibble
// ASCII definitions
localparam ASCII_TAB = 8'h09;
localparam ASCII_CR = 8'h0d;
localparam ASCII_LF = 8'h0a;
localparam ASCII_SPACE = 8'h20;
localparam ASCII_DASH = 8'h2d;
localparam ASCII_DOT = 8'h2e;
localparam ASCII_0 = 8'h30;
localparam ASCII_1 = 8'h31;
localparam ASCII_2 = 8'h32;
localparam ASCII_3 = 8'h33;
localparam ASCII_4 = 8'h34;
localparam ASCII_5 = 8'h35;
localparam ASCII_6 = 8'h36;
localparam ASCII_7 = 8'h37;
localparam ASCII_8 = 8'h38;
localparam ASCII_9 = 8'h39;
localparam ASCII_GT = 8'h3e;
localparam ASCII_A = 8'h41;
localparam ASCII_B = 8'h42;
localparam ASCII_C = 8'h43;
localparam ASCII_D = 8'h44;
localparam ASCII_E = 8'h45;
localparam ASCII_F = 8'h46;
localparam ASCII_G = 8'h47;
localparam ASCII_H = 8'h48;
localparam ASCII_I = 8'h49;
localparam ASCII_J = 8'h4a;
localparam ASCII_K = 8'h4b;
localparam ASCII_L = 8'h4c;
localparam ASCII_M = 8'h4d;
localparam ASCII_N = 8'h4e;
localparam ASCII_O = 8'h4f;
localparam ASCII_P = 8'h50;
localparam ASCII_Q = 8'h51;
localparam ASCII_R = 8'h52;
localparam ASCII_S = 8'h53;
localparam ASCII_T = 8'h54;
localparam ASCII_U = 8'h55;
localparam ASCII_V = 8'h56;
localparam ASCII_W = 8'h57;
localparam ASCII_X = 8'h58;
localparam ASCII_Y = 8'h59;
localparam ASCII_Z = 8'h5a;
localparam ASCII_a = 8'h61;
localparam ASCII_b = 8'h62;
localparam ASCII_c = 8'h63;
localparam ASCII_d = 8'h64;
localparam ASCII_e = 8'h65;
localparam ASCII_f = 8'h66;
localparam ASCII_g = 8'h67;
localparam ASCII_h = 8'h68;
localparam ASCII_i = 8'h69;
localparam ASCII_j = 8'h6a;
localparam ASCII_k = 8'h6b;
localparam ASCII_l = 8'h6c;
localparam ASCII_m = 8'h6d;
localparam ASCII_n = 8'h6e;
localparam ASCII_o = 8'h6f;
localparam ASCII_p = 8'h70;
localparam ASCII_q = 8'h71;
localparam ASCII_r = 8'h72;
localparam ASCII_s = 8'h73;
localparam ASCII_t = 8'h74;
localparam ASCII_u = 8'h75;
localparam ASCII_v = 8'h76;
localparam ASCII_w = 8'h77;
localparam ASCII_x = 8'h78;
localparam ASCII_y = 8'h79;
localparam ASCII_z = 8'h7a;
// String counts
localparam WELCOME_LAST_CHAR = 17;
//localparam PROMPT_LAST_CHAR = 3;
localparam PROMPT_LAST_CHAR = 1;
localparam ERR_LAST_CHAR = 7;
localparam PRE_DATA_LAST_CHAR = 4;
// Timeouts
localparam TIMEOUT_10MS = 20'd960000;
// State machine states
localparam RESET = 6'd0;
localparam ERROR = 6'd1;
localparam PROMPT = 6'd2;
localparam IDLE = 6'd3;
localparam PRE_DATA = 6'd4;
localparam GET_ADD_7 = 6'd5;
localparam GET_ADD_6 = 6'd6;
localparam GET_ADD_5 = 6'd7;
localparam GET_ADD_4 = 6'd8;
localparam GET_ADD_3 = 6'd9;
localparam GET_ADD_2 = 6'd10;
localparam GET_ADD_1 = 6'd11;
localparam GET_ADD_0 = 6'd12; // GET ADD States must be sequential, descending from GET_ADD_7
localparam GET_DATA_7 = 6'd13;
localparam GET_DATA_6 = 6'd14;
localparam GET_DATA_5 = 6'd15;
localparam GET_DATA_4 = 6'd16;
localparam GET_DATA_3 = 6'd17;
localparam GET_DATA_2 = 6'd18;
localparam GET_DATA_1 = 6'd19;
localparam GET_DATA_0 = 6'd20; // GET DATA States must be sequential, descending from GET_DATA_7
localparam WRITE = 6'd21;
localparam WRITE_1 = 6'd22;
localparam READ = 6'd23;
localparam READ_1 = 6'd24;
localparam SEND_ADD_7 = 6'd25;
localparam SEND_ADD_6 = 6'd26;
localparam SEND_ADD_5 = 6'd27;
localparam SEND_ADD_4 = 6'd28;
localparam SEND_ADD_3 = 6'd29;
localparam SEND_ADD_2 = 6'd30;
localparam SEND_ADD_1 = 6'd31;
localparam SEND_ADD_0 = 6'd32; // SEND ADD States must be sequential, descending from GET_ADD_7
localparam SEND_DATA_7 = 6'd33;
localparam SEND_DATA_6 = 6'd34;
localparam SEND_DATA_5 = 6'd35;
localparam SEND_DATA_4 = 6'd36;
localparam SEND_DATA_3 = 6'd37;
localparam SEND_DATA_2 = 6'd38;
localparam SEND_DATA_1 = 6'd39;
localparam SEND_DATA_0 = 6'd40; // SEND DATA States must be sequential, descending from GET_DATA_7
// Derived states, based on Address and Data size
localparam GET_ADD = (GET_ADD_7 + 8 - ADDRESS_CHARS );
localparam GET_DATA = (GET_DATA_7 + 8 - DATA_CHARS );
localparam SEND_ADD = (SEND_ADD_7 + 8 - ADDRESS_CHARS );
localparam SEND_DATA = (SEND_DATA_7 + 8 - DATA_CHARS );
// Internal signals
reg [31:0] address = 32'd0;
reg [31:0] data = 32'd0;
reg [31:0] data_new = 32'd0;
reg mode_rnw = 1'b0;
reg rd_en = 1'b0;
reg wr_en = 1'b0;
reg [5:0] state = RESET;
reg [5:0] next_state = RESET;
reg [7:0] welcome_char; // Not assigned a startup value as combinatorial
reg [7:0] prompt_char; // Not assigned a startup value as combinatorial
reg [7:0] err_char; // Not assigned a startup value as combinatorial
reg [7:0] pre_data_char; // Not assigned a startup value as combinatorial
reg [7:0] Append;
reg [7:0] idx = 8'd0;
reg [7:0] tx_data = 0;
reg tx_dv = 1'b0;
wire char_A_F;
wire char_a_f;
wire char_0_9;
wire char_hex;
wire [7:0] hex_A_F;
wire [7:0] hex_a_f;
wire [7:0] hex_0_9;
// Rx "FIFO" Signals
wire [8:0] rx_fifo_data;
reg [8:0] rx_fifo_q = 9'd0;
reg rx_fifo_empty_int = 1'b1;
wire rx_fifo_empty;
reg rx_fifo_rd_ack;
wire [7:0] rx_data_byte;
// Output assignments
assign tx_data_out = tx_data;
assign tx_dv_out = tx_dv;
assign tx_en_out = 1'b1; // Permanently enable transmitter - no good for RS-485 link
assign rx_en_out = 1'b1; // Permanently enable receiver
assign add_out = address[ADDRESS_WIDTH-1 : 0];
assign wr_data_out = data_new[DATA_WIDTH-1 : 0];
assign wr_en_out = wr_en;
assign rd_en_out = rd_en;
assign src_id_out = 6'd0; // This is used to identify returns from posted reads. Not implemented in this instance.
// Main state machine
always @(posedge clk)
begin
case (state)
RESET : begin
if (rx_dv_in) state <= IDLE; // Change implemented by DR to FSM away from idle for simulation
else begin
tx_dv <= tx_rdy_in;
tx_data <= welcome_char;
rx_fifo_rd_ack <= 1'b0;
if (tx_rdy_in)
begin
if (idx == WELCOME_LAST_CHAR) idx <= 8'd0;
else idx <= idx + 8'd1;
if (idx == WELCOME_LAST_CHAR) state <= PROMPT;
end
end
end
ERROR : begin
tx_dv <= tx_rdy_in;
tx_data <= err_char;
rx_fifo_rd_ack <= 1'b0;
if (tx_rdy_in)
begin
if (idx == ERR_LAST_CHAR) idx <= 8'd0;
else idx <= idx + 8'd1;
if (idx == ERR_LAST_CHAR) state <= PROMPT;
end
end
PROMPT : begin
tx_dv <= tx_rdy_in;
tx_data <= Append;
rx_fifo_rd_ack <= 1'b0;
if (tx_rdy_in)
begin
if (idx == PROMPT_LAST_CHAR) idx <= 8'd0;
else idx <= idx + 8'd1;
if (idx == PROMPT_LAST_CHAR) state <= IDLE;
end
end
IDLE : begin
tx_dv <= 1'b0;
wr_en <= 1'b0;
// Set idx to send shortened string for Read and Writes
if ( (rx_data_byte == ASCII_R) | (rx_data_byte == ASCII_r)
| (rx_data_byte == ASCII_W) | (rx_data_byte == ASCII_w)) idx <= 8'd0; // updated by DR it was set to idx <= 2
else idx <= 8'd0;
// Select next state and mode
state <= IDLE; //modified by DR
// mode_rnw <= ~((rx_data_byte == ASCII_W) | (rx_data_byte == ASCII_w));
// Require data before changing state
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
address <= 32'd0; // reset address and data
data <=32'd0;
if (( (rx_data_byte == ASCII_W) | (rx_data_byte == ASCII_w)) |( (rx_data_byte == ASCII_R) | (rx_data_byte == ASCII_r) )) //state <= PRE_DATA; modified by DR
begin
mode_rnw <= ((rx_data_byte == ASCII_R) | (rx_data_byte == ASCII_r));
state <= GET_ADD;
end
else state <= ERROR;
end
end
PRE_DATA : begin
tx_dv <= tx_rdy_in;
tx_data <= pre_data_char;
rx_fifo_rd_ack <= 1'b0;
state <= next_state; // moified by DR
/* if (tx_rdy_in)
begin
if (idx == PRE_DATA_LAST_CHAR) idx <= 8'd0;
else idx <= idx + 8'd1;
if (idx == PRE_DATA_LAST_CHAR) state <= next_state;
end */
end
GET_ADD_7 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[31:28] <= hex_A_F[3:0];
else if (char_a_f) address[31:28] <= hex_a_f[3:0];
else address[31:28] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_6;
else state <= ERROR;
end
end
GET_ADD_6 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[27:24] <= hex_A_F[3:0];
else if (char_a_f) address[27:24] <= hex_a_f[3:0];
else address[27:24] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_5;
else state <= ERROR;
end
end
GET_ADD_5 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[23:20] <= hex_A_F[3:0];
else if (char_a_f) address[23:20] <= hex_a_f[3:0];
else address[23:20] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_4;
else state <= ERROR;
end
end
GET_ADD_4 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[19:16] <= hex_A_F[3:0];
else if (char_a_f) address[19:16] <= hex_a_f[3:0];
else address[19:16] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_3;
else state <= ERROR;
end
end
GET_ADD_3 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[15:12] <= hex_A_F[3:0];
else if (char_a_f) address[15:12] <= hex_a_f[3:0];
else address[15:12] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_2;
else state <= ERROR;
end
end
GET_ADD_2 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[11:8] <= hex_A_F[3:0];
else if (char_a_f) address[11:8] <= hex_a_f[3:0];
else address[11:8] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_1;
else state <= ERROR;
end
end
GET_ADD_1 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[7:4] <= hex_A_F[3:0];
else if (char_a_f) address[7:4] <= hex_a_f[3:0];
else address[7:4] <= hex_0_9[3:0];
if (char_hex) state <= GET_ADD_0;
else state <= ERROR;
end
end
GET_ADD_0 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) address[3:0] <= hex_A_F[3:0];
else if (char_a_f) address[3:0] <= hex_a_f[3:0];
else address[3:0] <= hex_0_9[3:0];
if (mode_rnw) state <= READ;
else if (char_hex) state <= GET_DATA; //state <= PRE_DATA; modified by DR
else state <= ERROR;
if (char_hex & ~mode_rnw) idx <= 8'd0; // Don't print extra tabs unless in read mode
else idx <= 8'd0;
end
end
GET_DATA_7 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[31:28] <= hex_A_F[3:0];
else if (char_a_f) data[31:28] <= hex_a_f[3:0];
else data[31:28] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_6;
else state <= ERROR;
end
end
GET_DATA_6 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[27:24] <= hex_A_F[3:0];
else if (char_a_f) data[27:24] <= hex_a_f[3:0];
else data[27:24] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_5;
else state <= ERROR;
end
end
GET_DATA_5 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[23:20] <= hex_A_F[3:0];
else if (char_a_f) data[23:20] <= hex_a_f[3:0];
else data[23:20] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_4;
else state <= ERROR;
end
end
GET_DATA_4 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[19:16] <= hex_A_F[3:0];
else if (char_a_f) data[19:16] <= hex_a_f[3:0];
else data[19:16] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_3;
else state <= ERROR;
end
end
GET_DATA_3 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[15:12] <= hex_A_F[3:0];
else if (char_a_f) data[15:12] <= hex_a_f[3:0];
else data[15:12] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_2;
else state <= ERROR;
end
end
GET_DATA_2 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[11:8] <= hex_A_F[3:0];
else if (char_a_f) data[11:8] <= hex_a_f[3:0];
else data[11:8] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_1;
else state <= ERROR;
end
end
GET_DATA_1 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[7:4] <= hex_A_F[3:0];
else if (char_a_f) data[7:4] <= hex_a_f[3:0];
else data[7:4] <= hex_0_9[3:0];
if (char_hex) state <= GET_DATA_0;
else state <= ERROR;
end
end
GET_DATA_0 : begin
tx_dv <= 1'b0;
rx_fifo_rd_ack <= ~rx_fifo_empty;
if (~rx_fifo_empty)
begin
if (char_A_F) data[3:0] <= hex_A_F[3:0];
else if (char_a_f) data[3:0] <= hex_a_f[3:0];
else data[3:0] <= hex_0_9[3:0];
if (char_hex)
begin
state <= WRITE; //state <= PRE_DATA; Modified by DR
//data_new <= data;
end
else state <= ERROR;
next_state <= WRITE;
if (char_hex) idx <= 8'd0; // Don't print extra tabs from here
else idx <= 8'd0;
end
end
WRITE : begin
rx_fifo_rd_ack <= 1'b0;
tx_dv <= 1'b0;
wr_en <= 1'b1;
state <= WRITE_1;
data_new <= data;
end
WRITE_1 : begin
wr_en <= 1'b1;
//state <= READ; // Go to read to readback the write value
state <= IDLE; // data ready
end
READ : begin
rx_fifo_rd_ack <= 1'b0;
tx_dv <= 1'b0;
rd_en <= 1'b1;
state <= READ_1;
end
READ_1 : begin
rd_en <= 1'b0;
if (rd_dv_in)
begin
data <= rd_data_in;
if (rd_32b_in)
state <= SEND_DATA_7;
else
state <= SEND_DATA_3;
end
end
SEND_ADD_7 : begin
tx_dv <= tx_rdy_in;
if (address[31:28] > 4'd9) tx_data <= {4'd0,address[31:28]} + 8'h37;
else tx_data <= {4'd0,address[31:28]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_6;
end
SEND_ADD_6 : begin
tx_dv <= tx_rdy_in;
if (address[27:24] > 4'd9) tx_data <= {4'd0,address[27:24]} + 8'h37;
else tx_data <= {4'd0,address[27:24]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_5;
end
SEND_ADD_5 : begin
tx_dv <= tx_rdy_in;
if (address[23:20] > 4'd9) tx_data <= {4'd0,address[23:20]} + 8'h37;
else tx_data <= {4'd0,address[23:20]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_4;
end
SEND_ADD_4 : begin
tx_dv <= tx_rdy_in;
if (address[19:16] > 4'd9) tx_data <= {4'd0,address[19:16]} + 8'h37;
else tx_data <= {4'd0,address[19:16]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_3;
end
SEND_ADD_3 : begin
tx_dv <= tx_rdy_in;
if (address[15:12] > 4'd9) tx_data <= {4'd0,address[15:12]} + 8'h37;
else tx_data <= {4'd0,address[15:12]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_2;
end
SEND_ADD_2 : begin
tx_dv <= tx_rdy_in;
if (address[11:8] > 4'd9) tx_data <= {4'd0,address[11:8]} + 8'h37;
else tx_data <= {4'd0,address[11:8]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_1;
end
SEND_ADD_1 : begin
tx_dv <= tx_rdy_in;
if (address[7:4] > 4'd9) tx_data <= {4'd0,address[7:4]} + 8'h37;
else tx_data <= {4'd0,address[7:4]} + ASCII_0;
if (tx_rdy_in) state <= SEND_ADD_0;
end
SEND_ADD_0 : begin
tx_dv <= tx_rdy_in;
if (address[3:0] > 4'd9) tx_data <= {4'd0,address[3:0]} + 8'h37;
else tx_data <= {4'd0,address[3:0]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA;
//next_state <= SEND_DATA; I am not using a next state for branching
idx <= 0; // Don't print extra tabs
end
SEND_DATA_7 : begin
tx_dv <= tx_rdy_in;
if (data[31:28] > 4'd9) tx_data <= {4'd0,data[31:28]} + 8'h37;
else tx_data <= {4'd0,data[31:28]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_6;
end
SEND_DATA_6 : begin
tx_dv <= tx_rdy_in;
if (data[27:24] > 4'd9) tx_data <= {4'd0,data[27:24]} + 8'h37;
else tx_data <= {4'd0,data[27:24]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_5;
end
SEND_DATA_5 : begin
tx_dv <= tx_rdy_in;
if (data[23:20] > 4'd9) tx_data <= {4'd0,data[23:20]} + 8'h37;
else tx_data <= {4'd0,data[23:20]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_4;
end
SEND_DATA_4 : begin
tx_dv <= tx_rdy_in;
if (data[19:16] > 4'd9) tx_data <= {4'd0,data[19:16]} + 8'h37;
else tx_data <= {4'd0,data[19:16]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_3;
end
SEND_DATA_3 : begin
tx_dv <= tx_rdy_in;
if (data[15:12] > 4'd9) tx_data <= {4'd0,data[15:12]} + 8'h37;
else tx_data <= {4'd0,data[15:12]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_2;
end
SEND_DATA_2 : begin
tx_dv <= tx_rdy_in;
if (data[11:8] > 4'd9) tx_data <= {4'd0,data[11:8]} + 8'h37;
else tx_data <= {4'd0,data[11:8]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_1;
end
SEND_DATA_1 : begin
tx_dv <= tx_rdy_in;
if (data[7:4] > 4'd9) tx_data <= {4'd0,data[7:4]} + 8'h37;
else tx_data <= {4'd0,data[7:4]} + ASCII_0;
if (tx_rdy_in) state <= SEND_DATA_0;
end
SEND_DATA_0 : begin
tx_dv <= tx_rdy_in;
if (data[3:0] > 4'd9) tx_data <= {4'd0,data[3:0]} + 8'h37;
else tx_data <= {4'd0,data[3:0]} + ASCII_0;
if (tx_rdy_in) state <= PROMPT;
end
default : begin
rx_fifo_rd_ack <= 1'b0;
tx_dv <= 1'b0;
wr_en <= 1'b0;
rd_en <= 1'b0;
idx <= 8'd0;
state <= RESET;
end
endcase
end
assign char_A_F = ((rx_data_byte >= 8'h41) & (rx_data_byte <= 8'h46));
assign char_a_f = ((rx_data_byte >= 8'h61) & (rx_data_byte <= 8'h66));
assign char_0_9 = ((rx_data_byte >= 8'h30) & (rx_data_byte <= 8'h39));
assign char_hex = char_0_9 | char_A_F | char_a_f;
assign hex_A_F = rx_data_byte - 8'h37;
assign hex_a_f = rx_data_byte - 8'h57;
assign hex_0_9 = rx_data_byte - ASCII_0;
// Generate message "Debug Terminal"
always @(*) begin
case(idx)
8'd0 : welcome_char = ASCII_LF;
8'd1 : welcome_char = ASCII_LF;
8'd2 : welcome_char = ASCII_LF;
8'd3 : welcome_char = ASCII_CR;
8'd4 : welcome_char = ASCII_D;
8'd5 : welcome_char = ASCII_e;
8'd6 : welcome_char = ASCII_b;
8'd7 : welcome_char = ASCII_u;
8'd8 : welcome_char = ASCII_g;
8'd9 : welcome_char = ASCII_SPACE;
8'd10 : welcome_char = ASCII_T;
8'd11 : welcome_char = ASCII_e;
8'd12 : welcome_char = ASCII_r;
8'd13 : welcome_char = ASCII_m;
8'd14 : welcome_char = ASCII_i;
8'd15 : welcome_char = ASCII_n;
8'd16 : welcome_char = ASCII_a;
default : welcome_char = ASCII_l;
endcase
end
// Generate message "\n->"
always @(*) begin
case (idx)
8'd0 : prompt_char = ASCII_LF;
8'd1 : prompt_char = ASCII_CR;
8'd2 : prompt_char = ASCII_DASH;
default : prompt_char = ASCII_GT;
endcase
end
// Generate message " BAD CMD"
always @(*) begin
case(idx)
8'd0 : err_char = ASCII_TAB;
8'd1 : err_char = ASCII_B;
8'd2 : err_char = ASCII_A;
8'd3 : err_char = ASCII_D;
8'd4 : err_char = ASCII_SPACE;
8'd5 : err_char = ASCII_C;
8'd6 : err_char = ASCII_M;
default : err_char = ASCII_D;
endcase
end
// Generate message " 0x"
always @(*) begin
case (idx)
8'd0 : pre_data_char = ASCII_TAB;
8'd1 : pre_data_char = ASCII_TAB;
8'd2 : pre_data_char = ASCII_TAB;
8'd3 : pre_data_char = ASCII_0;
default : pre_data_char = ASCII_x;
endcase
end
// Generate <CR/LF>
always @(*) begin
case (idx)
8'd0 : Append = ASCII_TAB;
default : Append = ASCII_CR;
endcase
end
// Instantiate FIFO to buffer RX data from UART.
assign rx_fifo_data = {rx_err_in, rx_data_in};
assign rx_data_byte = rx_fifo_q[7:0];
assign rx_fifo_empty = (rx_fifo_empty_int | rx_fifo_rd_ack); // Gate with rd_ack to prevent double reads
// For the debug link, a dummy FIFO is implemented, on the basis that even posted reads will return quicker than a new character arriving.
// Also, the user is expected to wait for the read to return before issuing further commands.
always @(posedge clk)
begin
if (rx_dv_in) rx_fifo_q <= rx_fifo_data;
if (rx_dv_in) rx_fifo_empty_int <= 1'b0;
else if (rx_fifo_rd_ack) rx_fifo_empty_int <= 1'b1;
end
endmodule

View file

@ -0,0 +1,187 @@
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_misc.all;
entity pei_debug_uart_0 is
generic(
--Paramaters for the debugger
ADDRESS_WIDTH : integer range 1 to 32 := 8;
DATA_WIDTH : integer range 1 to 32 :=32;
--Parameters for the uart serdes
BAUD_RATE : integer range 9600 to 115200 := 115200;
TEST_MODE : boolean := false
);
PORT(
clk : IN STD_LOGIC;
--external pei connector
dbg_ser_out : OUT STD_LOGIC;
dbg_ser_in : IN STD_LOGIC;
--write block. Data from printhead and stm8
add_out : OUT STD_LOGIC_VECTOR (ADDRESS_WIDTH-1 DOWNTO 0); -- Address signal
wr_data_out : OUT STD_LOGIC_VECTOR (DATA_WIDTH-1 DOWNTO 0); --write data
--src_id_out : OUT STD_LOGIC_VECTOR (5 DOWNTO 0); // This is used to identify returns from posted reads. Not implemented in this instance.
wr_en_out : OUT STD_LOGIC; -- write control signal
--read block--
rd_data_in : IN STD_LOGIC_VECTOR (DATA_WIDTH-1 DOWNTO 0); --data from fpga
--src_id_in : IN STD_LOGIC_VECTOR (5 DOWNTO 0); // This is used to identify returns from posted reads. Not implemented in this instance.
rd_en_out : OUT STD_LOGIC; -- read request signal
rd_dv_in : IN STD_LOGIC; -- read data valid signal
rd_32b_in : IN STD_LOGIC -- read data is 32b not 16b
);
end entity pei_debug_uart_0;
architecture struct of pei_debug_uart_0 is
function calc_oversample_rate (TEST_MODE : boolean) return integer is
begin
if TEST_MODE then
return 5;
else
return 16;
end if;
end function calc_oversample_rate;
function calc_clock_divider (BAUD_RATE : integer; TEST_MODE : boolean) return integer is
begin
if TEST_MODE then
return 6;--32;
else
return (100000000/BAUD_RATE)/16; -- 54 for 115200 (16 is oversample rate)
end if;
end function calc_clock_divider;
constant OVERSAMPLE_RATE : integer := calc_oversample_rate(TEST_MODE);
constant ENABLE_CLOCK_DIVIDER : integer := calc_clock_divider(BAUD_RATE, TEST_MODE);
component pei_debug_serial_port_0
generic(
ADDRESS_WIDTH : integer range 1 to 31 := 8;
DATA_WIDTH : integer range 1 to 32 :=32
);
PORT (
clk : IN STD_LOGIC;
tx_rdy_in : IN STD_LOGIC;
tx_busy_in : IN STD_LOGIC;
tx_en_out : OUT STD_LOGIC; --perm. ON
tx_dv_out : OUT STD_LOGIC;
tx_data_out : OUT STD_LOGIC_VECTOR (7 DOWNTO 0);
rx_err_in : IN STD_LOGIC;
rx_dv_in : IN STD_LOGIC;
rx_data_in : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
rx_en_out : OUT STD_LOGIC; --perm ON
add_out : OUT STD_LOGIC_VECTOR (ADDRESS_WIDTH-1 DOWNTO 0);
wr_data_out : OUT STD_LOGIC_VECTOR (DATA_WIDTH-1 DOWNTO 0);
src_id_out : OUT STD_LOGIC_VECTOR (5 DOWNTO 0);
wr_en_out : OUT STD_LOGIC;
rd_en_out : OUT STD_LOGIC;
rd_data_in : IN STD_LOGIC_VECTOR (DATA_WIDTH-1 DOWNTO 0);
src_id_in : IN STD_LOGIC_VECTOR (5 DOWNTO 0);
rd_dv_in : IN STD_LOGIC;
rd_32b_in : IN STD_LOGIC
);
end component;
-- the block unbuffered_uart deserialises the data stream coming from the connector
component pei_unbuffered_uart_0
generic(
OVERSAMPLE_RATE : integer range 1 to 16 :=3;
ENABLE_CLOCK_DIVIDER : integer range 1 to 63 :=10
);
PORT(
clk : IN STD_LOGIC;
uart_de_out : OUT STD_LOGIC; -- copy of uart_tx_en_in
uart_rx_data_out : OUT STD_LOGIC_VECTOR (7 DOWNTO 0);
uart_rx_dv_out : OUT STD_LOGIC;
uart_rx_en_in : IN STD_LOGIC;
uart_rx_in : IN STD_LOGIC;
uart_rx_error_out : OUT STD_LOGIC;
uart_tx_data_in : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
uart_tx_dv_in : IN STD_LOGIC;
uart_tx_en_in : IN STD_LOGIC;
uart_tx_out : OUT STD_LOGIC;
uart_tx_rdy_out : OUT STD_LOGIC;
uart_tx_busy_out : OUT STD_LOGIC
);
end component;
signal s_tx_rdy_out, s_tx_busy_out, s_tx_dv_in, s_rx_error_out : std_logic; -- PEI
signal s_de_out,s_rx_en, s_rx_dv_out, s_tx_to_decoder : std_logic;
signal s_tx_data_in, s_rx_data_out : std_logic_vector (7 downto 0);
--mapping pei_unbuffered_uart_0
begin
--Instantiate uart blocks
unbuffered_uart_inst: pei_unbuffered_uart_0
generic map (
OVERSAMPLE_RATE => OVERSAMPLE_RATE,
ENABLE_CLOCK_DIVIDER => ENABLE_CLOCK_DIVIDER
)
port map (
-- system clock
clk => clk,
--I/O pints
uart_tx_out => dbg_ser_out, -- connected to the RxD pin of the J4 connector
uart_rx_in => dbg_ser_in, -- connected to the TxD pin of the J4 connector
-- connection to the uart decoder.
uart_tx_rdy_out => s_tx_rdy_out, -- Control signal. Tells the decoder that deser is ready to transmit
uart_tx_busy_out => s_tx_busy_out, -- Control signal. Tell the decoder that serialiaser is busy transmitting
uart_tx_dv_in => s_tx_dv_in, -- Control signal. The decoder signals to deser that there is valid data to be transmitted
uart_tx_data_in => s_tx_data_in, -- data from decoder to transmit
uart_rx_dv_out => s_rx_dv_out,
uart_rx_en_in => s_rx_en,
uart_de_out => open, -- this signal shows that there is a transmission
uart_rx_error_out => s_rx_error_out,
uart_tx_en_in => s_tx_to_decoder,
uart_rx_data_out => s_rx_data_out
);
debug_serial_port_inst : pei_debug_serial_port_0
port map (
clk => clk,
tx_rdy_in => s_tx_rdy_out, -- control signal from external deser
tx_busy_in => s_tx_busy_out, -- no connected
tx_en_out => s_tx_to_decoder, -- it is always on
tx_dv_out => s_tx_dv_in, --validate data
tx_data_out => s_tx_data_in,
rx_err_in => s_rx_error_out, -- error from deser
rx_dv_in => s_rx_dv_out ,
rx_data_in => s_rx_data_out,
rx_en_out => s_rx_en,
add_out => add_out, -- address from ftdi to fpga
wr_data_out => wr_data_out, -- data from ftdi to fpga
src_id_out => open, -- not used in this version
wr_en_out => wr_en_out, -- write enable controlled by decoder
rd_en_out => rd_en_out, -- read enable controlled by
rd_data_in => rd_data_in, --data from fpga to ftdi
src_id_in => (others =>'0'), -- not used in this version
rd_dv_in => rd_dv_in, -- Enable transmission from FPGA to ftdi chip
rd_32b_in => rd_32b_in
);
end architecture struct;

View file

@ -0,0 +1,255 @@
-------------------------------------------------------------------------------
-- Title : uart_tx
-- Project : EMB-1
-------------------------------------------------------------------------------
-- File : uart_tx.vhd
-- Author : Gianmarco Girau
-- Company : Global Inkjet Systems
-- Created : 2016-10-24
-- Last update: 2021-06-07
-- SVN ID : Git
-- Standard : VHDL'08
-------------------------------------------------------------------------------
-- Description:
-- Simple UART trasmitter, used to trasmit data over the POF, 1 start, 1 stop
-- bit, no parity
-------------------------------------------------------------------------------
-- altera vhdl_input_version vhdl_2008
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity pei_uart_tx_rx_0 is
generic(
data_width : natural range 1 to 32);
port(
reset_n : in std_logic;
clk : in std_logic;
bit_div : in std_logic_vector(4 downto 0); -- number of clock cycles per tx bit
init : in std_logic;
ready : out std_logic;
data_in : in std_logic_vector(data_width-1 downto 0);
data_valid : in std_logic;
tx_data : out std_logic;
rx_data : in std_logic;
data_rx_vec : out std_logic_vector(data_width-1 downto 0);
data_ackr_in : in std_logic;
data_ackr_out : out std_logic
);
end entity pei_uart_tx_rx_0;
architecture rtl of pei_uart_tx_rx_0 is
type fsm_state_t is (idle, send_start_bit, send_data, send_stop_bit);
type rx_t is (idle,idlebit, shiftdata);
signal fsm_state_curr, fsm_state_next : fsm_state_t;
signal rx_sm, rx_sm_d1 : rx_t :=idle;
--
signal ld_data, rst_bit_cnt, inc_bit_cnt : std_logic;
signal rst_clk_cnt,shf_data, end_of_bit, end_of_tx : std_logic;
signal rst_tx_data, set_tx_data, ld_tx_data : std_logic;
signal ready_i : std_logic;
--
signal data : std_logic_vector(data_width-1 downto 0);
signal clk_cnt : std_logic_vector(4 downto 0);
signal bit_cnt : std_logic_vector(5 downto 0);
---
signal rx_data_d1, rx_data_d2 : std_logic;
signal clk_shift : std_logic;
signal shift_data : std_logic_vector(data_width downto 0);
signal data_rx_int : std_logic_vector(data_width-1 downto 0);
signal bit_pos_count : integer range 0 to 255;
signal rx_cnt : integer range 0 to 10;
signal clk_shift_d1 : std_logic;
signal data_reg_shift : std_logic_vector (47 downto 0);
begin
-----------------------------------------------------------------------------
-- UART registers and sequential logic
-----------------------------------------------------------------------------
pREGs : process(clk)
begin
if(clk ='1' and clk'event) then
if(ld_data = '1') then
data <= data_in;
elsif(shf_data = '1') then
data <= '0' & data(data'high downto 1);
end if;
if(rst_bit_cnt = '1') then
bit_cnt <= (others => '0');
elsif(inc_bit_cnt = '1') then
bit_cnt <= std_logic_vector(unsigned(bit_cnt)+1);
end if;
if(rst_clk_cnt = '1' or end_of_bit = '1') then
clk_cnt <= (others => '0');
else
clk_cnt <= std_logic_vector(unsigned(clk_cnt)+1);
end if;
if(rst_tx_data = '1') then
tx_data <= '0';
elsif(set_tx_data = '1') then
tx_data <= '1';
elsif(ld_tx_data = '1') then
tx_data <= data(0);
end if;
end if;
end process pREGs;
end_of_bit <= '1' when unsigned(clk_cnt) = (unsigned(bit_div)-1) else '0';
end_of_tx <= '1' when unsigned(bit_cnt) = data_width else '0';
--shf_data <= inc_bit_cnt;
shf_data <= ld_tx_data;
inc_bit_cnt <= ld_tx_data;
-----------------------------------------------------------------------------
-- FSM
-----------------------------------------------------------------------------
-- FSM state register
pFSM_SR : process(reset_n, clk)
begin
if(reset_n = '0') then
fsm_state_curr <= idle;
elsif(clk = '1' and clk'event) then
fsm_state_curr <= fsm_state_next;
end if;
end process pFSM_SR;
-- FSM Combinational Logic
pFSM_CL : process(fsm_state_curr, init, data_valid, end_of_bit, end_of_tx)
begin
--default assignments
ready_i <= '0';
rst_clk_cnt <= '0';
rst_tx_data <= '0';
set_tx_data <= '0';
ld_tx_data <= '0';
rst_bit_cnt <= '0';
ld_data <= '0';
--
fsm_state_next <= fsm_state_curr;
if(init = '1') then
set_tx_data <= '1';
fsm_state_next <= idle;
else
case fsm_state_curr is
when idle =>
ready_i <= '1';
ld_data <= '1';
rst_clk_cnt <= '1';
rst_bit_cnt <= '1';
set_tx_data <= '1';
if(data_valid = '1') then
rst_tx_data <= '1';
fsm_state_next <= send_start_bit;
end if;
when send_start_bit =>
if(end_of_bit = '1') then
ld_tx_data <= '1';
fsm_state_next <= send_data;
end if;
when send_data =>
if(end_of_bit = '1') then
ld_tx_data <= '1';
if(end_of_tx = '1') then
set_tx_data <= '1';
fsm_state_next <= send_stop_bit;
end if;
end if;
when send_stop_bit =>
if(end_of_bit = '1') then
fsm_state_next <= idle;
end if;
when others => null;
end case;
end if;
end process pFSM_CL;
ready <= ready_i;
rx_data_p:process (clk, reset_n)
begin
if (reset_n='0') then
rx_cnt <= 0;
shift_data <= (others =>'0');
data_rx_int <= (others => '0');
data_ackr_out <= '0';
elsif (clk'event and clk='1') then
rx_data_d1 <= rx_data;
rx_data_d2 <= rx_data_d1;
clk_shift_d1 <= clk_shift;
case rx_sm is
when idle =>
rx_cnt <= 0;
if (rx_data_d2='1' and rx_data_d1='0') then
rx_sm <= idlebit;
shift_data <= rx_data_d1 & shift_data(data_width downto 1);
else
rx_sm <= idle;
end if;
when idlebit => -- skip idle bit
if (clk_shift_d1 ='1') then
rx_sm <= shiftdata;
else
rx_sm <= idlebit;
end if;
when shiftdata =>
if (rx_cnt=9 ) then
rx_sm <= idle;
data_rx_int <= shift_data(data_width-1 downto 0);
data_ackr_out <= '1';
elsif (clk_shift_d1 ='1' ) then
rx_cnt <= rx_cnt+1;
shift_data <= rx_data_d1 & shift_data(data_width downto 1);
rx_sm <= shiftdata;
else
rx_sm <= shiftdata;
end if;
end case;
if data_ackr_in = '1' then
data_ackr_out <= '0';
end if;
end if;
end process;
clock_div_p: process(clk,reset_n)
begin
if (reset_n='0') then
bit_pos_count <= 0;
elsif (clk'event and clk='1') then
if ( bit_pos_count = to_integer(unsigned(bit_div)-1)) then
bit_pos_count <= 0;
clk_shift <='1';
else
bit_pos_count <= bit_pos_count+1;
clk_shift <= '0';
end if;
end if;
end process;
data_rx_vec <= data_rx_int;
end architecture rtl;

View file

@ -0,0 +1,265 @@
//----------------------------------------------------------------------------
// Module Description
//
// This is a simple UART, capable of full duplex operation.
// Data format is fixed as 8N1, LSB first.
// It is intended to be used with a FIFO if necessary.
// Timings are set by parameters at instantiation.
//
// Interface:
// rx_dv is valid for 1 clock pulse
// rx_data_out is valid when dv is high
// tx_dv shall be valid for 1 clock pulse, after which tx_rdy is de-asserted.
// tx_data shall be valid when tx_dv is high.
// If tx_dv is asserted while tx_rdy is de-asserted, tx_dv is ignored.
//
//----------------------------------------------------------------------------
// Revision Date Author Notes
//
// 01 22-02-2009 Gordon Leather First written
//
//----------------------------------------------------------------------------
`resetall
`timescale 1ns/1ps
module pei_unbuffered_uart_0 #(
parameter OVERSAMPLE_RATE = 5'd16,
parameter ENABLE_CLOCK_DIVIDER = 6'd52
)
(
// Port Declarations
input wire clk,
output wire uart_de_out,
output wire [7:0] uart_rx_data_out,
output wire uart_rx_dv_out,
input wire uart_rx_en_in,
input wire uart_rx_in,
output wire uart_rx_error_out,
input wire [7:0] uart_tx_data_in,
input wire uart_tx_dv_in,
input wire uart_tx_en_in,
output wire uart_tx_out,
output wire uart_tx_rdy_out,
output wire uart_tx_busy_out
);
// Parameters
// Internal Declarations
localparam DATA_BITS = 4'd8;
localparam EDGE_DETECT_POSITION = OVERSAMPLE_RATE/2;
// RX State machine constants
localparam RX_IDLE = 3'd0;
localparam RX_GET_START = 3'd1;
localparam RX_GET_DATA = 3'd2;
localparam RX_GET_STOP = 3'd3;
localparam RX_GET_STOP_1 = 3'd4;
// TX State machine constants
localparam TX_IDLE = 2'b0;
// Internal Declarations
reg [OVERSAMPLE_RATE-1:0] rx_sr = 0;
reg [5:0] en_cnt = 6'd0;
reg [4:0] rx_bit_tmr = (OVERSAMPLE_RATE-5'd1);
reg [4:0] tx_bit_tmr = (OVERSAMPLE_RATE-5'd1);
wire clk_en;
wire rx_neg_edge;
wire rx_bit_valid;
reg [2:0] rx_state = RX_IDLE;
reg [3:0] rx_bit_cnt = DATA_BITS - 4'd1;
reg rx_receiving = 1'b0;
reg rx_done = 1'b0;
reg rx_error = 1'b0;
reg [7:0] rx_data = 8'd0;
wire tx_new_word;
wire tx_next_bit;
wire tx_last_bit;
wire tx_done;
reg [3:0] tx_bit_cnt = DATA_BITS + 4'd2;
reg tx_ignore_idle = 1'b0;
reg [9:0] tx_data = 10'h3ff; // Extra bits allow easy addition of start bit to data.
reg tx_rdy = 1'b1;
reg tx_busy = 1'b0;
// Output assignments
assign uart_rx_data_out = rx_data;
assign uart_rx_dv_out = rx_done;
assign uart_rx_error_out = rx_error;
assign uart_tx_rdy_out = tx_rdy & ~uart_tx_dv_in & uart_tx_en_in; // Combinatorial output, often required for RDY interface to FIFOs
assign uart_tx_out = tx_data[0];
assign uart_de_out = uart_tx_en_in;
assign uart_tx_busy_out = tx_busy;
//----------------------------------------------------------------------------
// Common Functions
//----------------------------------------------------------------------------
//
// Clock enable generation
assign clk_en = (en_cnt == (ENABLE_CLOCK_DIVIDER-1));
always @(posedge clk)
begin
if (clk_en) en_cnt <= 6'd0;
else en_cnt <= en_cnt + 6'd1;
end
//----------------------------------------------------------------------------
// END Common Functions
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// UART Transmitter
//----------------------------------------------------------------------------
//
always @(posedge clk)
begin
if (uart_tx_en_in)
begin
if (tx_new_word) tx_ignore_idle <= ~tx_busy;
else if (tx_next_bit) tx_ignore_idle <= 1'b0;
if (tx_new_word) tx_data <= {uart_tx_data_in, 1'b0, 1'b1}; // Data, Start bit, Idle bit. Ignore writes when busy,
else if (tx_next_bit) tx_data <= {1'b1, tx_data[9:1]}; // Shift data along. Shifting '1's in gives us the stop bit.
if (~tx_busy | tx_next_bit) tx_bit_tmr <= (OVERSAMPLE_RATE-5'd1);
else if (clk_en) tx_bit_tmr <= tx_bit_tmr - 5'd1;
if (tx_new_word) tx_bit_cnt <= DATA_BITS + 4'd2;
else if (tx_next_bit) tx_bit_cnt <= tx_bit_cnt - 4'd1;
if (tx_new_word) tx_busy <= 1'b1; // This takes priority, to allow continuous transmission
else if (tx_done) tx_busy <= 1'b0;
if (tx_new_word) tx_rdy <= 1'b0;
else if (~tx_busy | tx_last_bit) tx_rdy <= 1'b1; // Ready for new data as soon as the last bit has been transmitted, or when not busy.
end
else
begin
tx_bit_tmr <= (OVERSAMPLE_RATE-5'd1);
tx_bit_cnt <= DATA_BITS + 4'd2;
tx_data <= 10'h3ff;
tx_busy <= 1'b0;
tx_rdy <= 1'b1;
end
end
assign tx_new_word = uart_tx_dv_in & tx_rdy; // Ignore writes when not ready
assign tx_next_bit = clk_en & ((tx_busy & (tx_bit_tmr == 0)) | tx_ignore_idle);
assign tx_last_bit = tx_next_bit & (tx_bit_cnt == 1);
assign tx_done = tx_next_bit & (tx_bit_cnt == 0);
//----------------------------------------------------------------------------
// END UART Transmitter
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// UART Receiver
//----------------------------------------------------------------------------
//
// Rx edge detect and bit timer
always @(posedge clk)
begin
if (clk_en)
begin
if (uart_rx_en_in) rx_sr <= {rx_sr[OVERSAMPLE_RATE-2:0], uart_rx_in}; // Oversample Rx input
else rx_sr <= 0; // Reset required to avoid false triggers on restart
if (rx_receiving && (rx_bit_tmr>0)) rx_bit_tmr <= rx_bit_tmr - 5'd1; // Timer for sampling bits
else rx_bit_tmr <= (OVERSAMPLE_RATE - 5'd1);
end
end
assign rx_neg_edge = (rx_sr[EDGE_DETECT_POSITION] & ~rx_sr[EDGE_DETECT_POSITION-1]);
assign rx_bit_valid = (rx_bit_tmr == 0);
// Rx State machine
// This state machine only supports 8N1 data format
always @(posedge clk)
begin
if (clk_en)
begin
if (uart_rx_en_in)
begin
case (rx_state)
RX_GET_START : if (rx_neg_edge)
begin
rx_bit_cnt <= DATA_BITS- 4'd1; // Ensure correct number of bits are counted
rx_receiving <= 1'b1;
rx_state <= RX_GET_DATA; // State transition
end
RX_GET_DATA : if (rx_bit_valid)
begin
rx_data <= {uart_rx_in, rx_data[7:1]}; // Register Data
rx_bit_cnt <= rx_bit_cnt - 4'd1; // and decrement bit counter
if (rx_bit_cnt == 0) rx_state <= RX_GET_STOP; // Change state on last bit
end
RX_GET_STOP : if (rx_bit_valid)
begin
rx_error <= ~uart_rx_in; // Stop bit should be a '1'
rx_state <= RX_GET_STOP_1; // State transition
end
RX_GET_STOP_1 : if (rx_bit_tmr == (OVERSAMPLE_RATE/2)) // Delay to ensure Stop bit is fully received
begin // This is necessary to prevent turnaround time problems
rx_done <= 1'b1;
rx_receiving <= 1'b0;
rx_state <= RX_IDLE; // State transition
end
default : begin
rx_receiving <= 1'b0;
rx_done <= 1'b0;
rx_error <= 1'b0;
if (uart_rx_en_in) rx_state <= RX_GET_START; // State transition
end
endcase
end
else
begin
rx_state <= RX_IDLE;
rx_done <= 1'b0;
end
end
else
begin
rx_done <= 1'b0; // Required so only one pulse given.
end
end
//----------------------------------------------------------------------------
// END UART Receiver
//----------------------------------------------------------------------------
endmodule