Line 85... |
Line 85... |
//
|
//
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
//
|
//
|
|
`default_nettype none
|
|
//
|
`define LLSDSPI_IDLE 4'h0
|
`define LLSDSPI_IDLE 4'h0
|
`define LLSDSPI_HOTIDLE 4'h1
|
`define LLSDSPI_HOTIDLE 4'h1
|
`define LLSDSPI_WAIT 4'h2
|
`define LLSDSPI_WAIT 4'h2
|
`define LLSDSPI_START 4'h3
|
`define LLSDSPI_START 4'h3
|
//
|
//
|
module llsdspi(i_clk, i_speed, i_cs, i_stb, i_byte,
|
module llsdspi(i_clk, i_speed, i_cs, i_stb, i_byte,
|
o_cs_n, o_sclk, o_mosi, i_miso,
|
o_cs_n, o_sclk, o_mosi, i_miso,
|
o_stb, o_byte, o_idle, i_bus_grant);
|
o_stb, o_byte, o_idle, i_bus_grant);
|
parameter SPDBITS = 7;
|
parameter SPDBITS = 7;
|
//
|
//
|
input i_clk;
|
input wire i_clk;
|
// Parameters/setup
|
// Parameters/setup
|
input [(SPDBITS-1):0] i_speed;
|
input wire [(SPDBITS-1):0] i_speed;
|
// The incoming interface
|
// The incoming interface
|
input i_cs;
|
input wire i_cs;
|
input i_stb;
|
input wire i_stb;
|
input [7:0] i_byte;
|
input wire [7:0] i_byte;
|
// The actual SPI interface
|
// The actual SPI interface
|
output reg o_cs_n, o_sclk, o_mosi;
|
output reg o_cs_n, o_sclk, o_mosi;
|
input i_miso;
|
input wire i_miso;
|
// The outgoing interface
|
// The outgoing interface
|
output reg o_stb;
|
output reg o_stb;
|
output reg [7:0] o_byte;
|
output reg [7:0] o_byte;
|
output wire o_idle;
|
output wire o_idle;
|
// And whether or not we actually own the interface (yet)
|
// And whether or not we actually own the interface (yet)
|
input i_bus_grant;
|
input wire i_bus_grant;
|
|
|
reg r_z_counter;
|
reg r_z_counter;
|
reg [(SPDBITS-1):0] r_clk_counter;
|
reg [(SPDBITS-1):0] r_clk_counter;
|
reg r_idle;
|
reg r_idle;
|
reg [3:0] r_state;
|
reg [3:0] r_state;
|
Line 124... |
Line 126... |
assign byte_accepted = (i_stb)&&(o_idle);
|
assign byte_accepted = (i_stb)&&(o_idle);
|
|
|
initial r_clk_counter = 7'h0;
|
initial r_clk_counter = 7'h0;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
begin
|
begin
|
if ((~i_cs)||(~i_bus_grant))
|
if ((!i_cs)||(!i_bus_grant))
|
r_clk_counter <= 0;
|
r_clk_counter <= 0;
|
else if (byte_accepted)
|
else if (byte_accepted)
|
r_clk_counter <= i_speed;
|
r_clk_counter <= i_speed;
|
else if (~r_z_counter)
|
else if (!r_z_counter)
|
r_clk_counter <= (r_clk_counter - {{(SPDBITS-1){1'b0}},1'b1});
|
r_clk_counter <= (r_clk_counter - {{(SPDBITS-1){1'b0}},1'b1});
|
else if ((r_state != `LLSDSPI_IDLE)&&(r_state != `LLSDSPI_HOTIDLE))
|
else if ((r_state != `LLSDSPI_IDLE)&&(r_state != `LLSDSPI_HOTIDLE))
|
r_clk_counter <= (i_speed);
|
r_clk_counter <= (i_speed);
|
// else
|
// else
|
// r_clk_counter <= 16'h00;
|
// r_clk_counter <= 16'h00;
|
end
|
end
|
|
|
initial r_z_counter = 1'b1;
|
initial r_z_counter = 1'b1;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
begin
|
begin
|
if ((~i_cs)||(~i_bus_grant))
|
if ((!i_cs)||(!i_bus_grant))
|
r_z_counter <= 1'b1;
|
r_z_counter <= 1'b1;
|
else if (byte_accepted)
|
else if (byte_accepted)
|
r_z_counter <= 1'b0;
|
r_z_counter <= 1'b0;
|
else if (~r_z_counter)
|
else if (!r_z_counter)
|
r_z_counter <= (r_clk_counter == 1);
|
r_z_counter <= (r_clk_counter == 1);
|
else if ((r_state != `LLSDSPI_IDLE)&&(r_state != `LLSDSPI_HOTIDLE))
|
else if ((r_state != `LLSDSPI_IDLE)&&(r_state != `LLSDSPI_HOTIDLE))
|
r_z_counter <= 1'b0;
|
r_z_counter <= 1'b0;
|
end
|
end
|
|
|
initial r_state = `LLSDSPI_IDLE;
|
initial r_state = `LLSDSPI_IDLE;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
begin
|
begin
|
o_stb <= 1'b0;
|
o_stb <= 1'b0;
|
o_cs_n <= ~i_cs;
|
o_cs_n <= !i_cs;
|
if (~i_cs)
|
if (!i_cs)
|
begin
|
begin
|
r_state <= `LLSDSPI_IDLE;
|
r_state <= `LLSDSPI_IDLE;
|
r_idle <= 1'b0;
|
r_idle <= 1'b0;
|
o_sclk <= 1'b1;
|
o_sclk <= 1'b1;
|
end else if (~r_z_counter)
|
end else if (!r_z_counter)
|
begin
|
begin
|
r_idle <= 1'b0;
|
r_idle <= 1'b0;
|
if (byte_accepted)
|
if (byte_accepted)
|
begin // Will only happen within a hot idle state
|
begin // Will only happen within a hot idle state
|
r_byte <= { i_byte[6:0], 1'b1 };
|
r_byte <= { i_byte[6:0], 1'b1 };
|