Spi Protocol Verilog Code Explanation
Spi Protocol Verilog Code Explanation
Spi Protocol Verilog Code Explanation
//
//`define SPI_DIVIDER_LEN_8
`define SPI_DIVIDER_LEN_16
//`define SPI_DIVIDER_LEN_24
//`define SPI_DIVIDER_LEN_32
`ifdef SPI_DIVIDER_LEN_8
`endif
`ifdef SPI_DIVIDER_LEN_16
`endif
`ifdef SPI_DIVIDER_LEN_24
`endif
`ifdef SPI_DIVIDER_LEN_32
`endif
//
// Use SPI_MAX_CHAR for fine tuning the exact number, when using
//
`define SPI_MAX_CHAR_128
//`define SPI_MAX_CHAR_64
//`define SPI_MAX_CHAR_32
//`define SPI_MAX_CHAR_24
//`define SPI_MAX_CHAR_16
//`define SPI_MAX_CHAR_8
`ifdef SPI_MAX_CHAR_128
`define SPI_CHAR_LEN_BITS 7
`endif
`ifdef SPI_MAX_CHAR_64
`define SPI_CHAR_LEN_BITS 6
`endif
`ifdef SPI_MAX_CHAR_32
`define SPI_CHAR_LEN_BITS 5
`endif
`ifdef SPI_MAX_CHAR_24
`define SPI_CHAR_LEN_BITS 5
`endif
`ifdef SPI_MAX_CHAR_16
`define SPI_CHAR_LEN_BITS 4
`endif
`ifdef SPI_MAX_CHAR_8
`define SPI_CHAR_LEN_BITS 3
`endif
//
// Number of device select signals. Use SPI_SS_NB for fine tuning the
// exact number.
//
`define SPI_SS_NB_8
//`define SPI_SS_NB_16
//`define SPI_SS_NB_24
//`define SPI_SS_NB_32
`ifdef SPI_SS_NB_8
`endif
`ifdef SPI_SS_NB_16
`endif
`ifdef SPI_SS_NB_24
`endif
`ifdef SPI_SS_NB_32
`endif
//
//
//
// Register offset
//
`define SPI_RX_0 0
`define SPI_RX_1 1
`define SPI_RX_2 2
`define SPI_RX_3 3
`define SPI_TX_0 0
`define SPI_TX_1 1
`define SPI_TX_2 2
`define SPI_TX_3 3
`define SPI_CTRL 4
`define SPI_DEVIDE 5
`define SPI_SS 6
//
//
`define SPI_CTRL_BIT_NB 14
//
//
`define SPI_CTRL_ASS 13
`define SPI_CTRL_IE 12
`define SPI_CTRL_LSB 11
`define SPI_CTRL_TX_NEGEDGE 10
`define SPI_CTRL_RX_NEGEDGE 9
`define SPI_CTRL_GO 8
`define SPI_CTRL_RES_1 7
`define SPI_CTRL_CHAR_LEN 6:0
//////////////////////////////////////////////////////////////////////
//// ////
//// ////
//// ////
//// ////
//// ////
//////////////////////////////////////////////////////////////////////
//// ////
//// ////
//// This source file may be used and distributed without ////
//// removed from the file and that any derivative work contains ////
//// the original copyright notice and the associated disclaimer. ////
//// ////
//// This source file is free software; you can redistribute it ////
//// and/or modify it under the terms of the GNU Lesser General ////
//// either version 2.1 of the License, or (at your option) any ////
//// later version. ////
//// ////
`include "spi_defines.v"
`include "timescale.v"
module spi_clgen (clk_in, rst, go, enable, last_clk, divider, clk_out, pos_edge, neg_edge);
parameter Tp = 1;
input [`SPI_DIVIDER_LEN-1:0] divider; // clock divider (output clock is divided by this value)
reg clk_out;
reg pos_edge;
reg neg_edge;
begin
if(rst)
else
begin
if(!enable || cnt_zero)
else
end
end
begin
if(rst)
else
clk_out <= #Tp (enable && cnt_zero && (!last_clk || clk_out)) ? ~clk_out : clk_out;
end
if(rst)
begin
end
else
begin
pos_edge <= #Tp (enable && !clk_out && cnt_one) || (!(|divider) && clk_out) || (!(|divider) && go
&& !enable);
neg_edge <= #Tp (enable && clk_out && cnt_one) || (!(|divider) && !clk_out && enable);
end
end
endmodule
`include "spi_defines.v"
`include "timescale.v"
tip, last,
parameter Tp = 1;
input [3:0] latch; // latch signal for storing the data in shift register
input [3:0] byte_sel; // byte select signals for storing the data in shift register
input [`SPI_CHAR_LEN_BITS-1:0] len; // data len in bits (minus one)
reg s_out;
reg tip;
begin
if(rst)
else
begin
if(tip)
else
end
end
// Transfer in progress
begin
if(rst)
end
begin
if (rst)
else
end
begin
if (rst)
`ifdef SPI_MAX_CHAR_128
begin
if (byte_sel[3])
if (byte_sel[2])
if (byte_sel[1])
if (byte_sel[0])
end
else if (latch[1] && !tip)
begin
if (byte_sel[3])
if (byte_sel[2])
if (byte_sel[1])
if (byte_sel[0])
end
begin
if (byte_sel[3])
if (byte_sel[2])
if (byte_sel[1])
if (byte_sel[0])
end
begin
if (byte_sel[3])
if (byte_sel[2])
if (byte_sel[1])
data[111:104] <= #Tp p_in[15:8];
if (byte_sel[0])
end
`else
`ifdef SPI_MAX_CHAR_64
begin
if (byte_sel[3])
if (byte_sel[2])
if (byte_sel[1])
if (byte_sel[0])
end
begin
if (byte_sel[3])
if (byte_sel[2])
if (byte_sel[1])
if (byte_sel[0])
end
`else
else if (latch[0] && !tip)
begin
`ifdef SPI_MAX_CHAR_8
if (byte_sel[0])
`endif
`ifdef SPI_MAX_CHAR_16
if (byte_sel[0])
if (byte_sel[1])
`endif
`ifdef SPI_MAX_CHAR_24
if (byte_sel[0])
if (byte_sel[1])
if (byte_sel[2])
`endif
`ifdef SPI_MAX_CHAR_32
if (byte_sel[0])
if (byte_sel[1])
if (byte_sel[2])
if (byte_sel[3])
end
`endif
`endif
else
end
endmodule