2016-03-08 21:59:08 -05:00
|
|
|
//#############################################################################
|
|
|
|
//# Purpose: SPI master (configurable) #
|
|
|
|
//#############################################################################
|
|
|
|
//# Author: Andreas Olofsson #
|
|
|
|
//# License: MIT (see below) #
|
|
|
|
//#############################################################################
|
2016-03-08 19:33:53 -05:00
|
|
|
`include "spi_regmap.vh"
|
|
|
|
module spi_master_regs (/*AUTOARG*/
|
|
|
|
// Outputs
|
2016-03-09 22:46:24 -05:00
|
|
|
cpol, cpha, lsbfirst, emode, spi_en, clkdiv_reg, wait_out,
|
|
|
|
access_out, packet_out,
|
2016-03-08 19:33:53 -05:00
|
|
|
// Inputs
|
|
|
|
clk, nreset, rx_data, rx_access, spi_state, access_in, packet_in,
|
|
|
|
wait_in
|
|
|
|
);
|
|
|
|
|
|
|
|
//parameters
|
2016-03-09 22:46:24 -05:00
|
|
|
parameter CLKDIV = 1; // default clkdiv
|
|
|
|
parameter PSIZE = 0; // default is 32 bits
|
2016-03-08 19:33:53 -05:00
|
|
|
parameter AW = 32; // addresss width
|
|
|
|
localparam PW = (2*AW+40); // packet width
|
|
|
|
|
|
|
|
//clk,reset, cfg
|
|
|
|
input clk; // core clock
|
|
|
|
input nreset; // async active low reset
|
|
|
|
|
|
|
|
//io interface
|
|
|
|
input [PW-1:0] rx_data; // rx data
|
|
|
|
input rx_access; // rx access pulse
|
|
|
|
|
|
|
|
//control
|
|
|
|
output cpol;
|
2016-03-09 22:46:24 -05:00
|
|
|
output cpha;
|
|
|
|
output lsbfirst; // send lsbfirst
|
|
|
|
output emode; // send emesh transaction
|
2016-03-08 19:33:53 -05:00
|
|
|
output spi_en; // enable transmitter
|
|
|
|
output [7:0] clkdiv_reg; // baud rate setting
|
2016-03-09 22:46:24 -05:00
|
|
|
input [1:0] spi_state; // transmit state
|
2016-03-08 19:33:53 -05:00
|
|
|
|
|
|
|
//packet to transmit
|
|
|
|
input access_in; // access from core
|
|
|
|
input [PW-1:0] packet_in; // data to core
|
|
|
|
output wait_out; // pushback from spi master
|
|
|
|
|
|
|
|
//return packet
|
|
|
|
output access_out; // writeback from spi
|
|
|
|
output [PW-1:0] packet_out; // writeback data from spi
|
|
|
|
input wait_in; // pushback by core
|
|
|
|
|
|
|
|
//########################################################
|
|
|
|
//# BODY
|
|
|
|
//########################################################
|
|
|
|
|
|
|
|
reg [7:0] config_reg;
|
|
|
|
reg [7:0] status_reg;
|
|
|
|
reg [7:0] cmd_reg;
|
2016-03-09 22:46:24 -05:00
|
|
|
reg [31:0] psize_reg;
|
2016-03-08 19:33:53 -05:00
|
|
|
reg [7:0] clkdiv_reg;
|
|
|
|
reg [63:0] tx_reg[3:0];
|
|
|
|
reg [7:0] rx_reg;
|
|
|
|
|
|
|
|
wire [63:0] reg_wdata;
|
|
|
|
wire [255:0] tx_vector;
|
|
|
|
wire [63:0] write_mask;
|
|
|
|
|
|
|
|
integer i;
|
|
|
|
|
|
|
|
/*AUTOWIRE*/
|
|
|
|
// Beginning of automatic wires (for undeclared instantiated-module outputs)
|
|
|
|
wire [4:0] ctrlmode_in; // From pe2 of packet2emesh.v
|
|
|
|
wire [AW-1:0] data_in; // From pe2 of packet2emesh.v
|
|
|
|
wire [1:0] datamode_in; // From pe2 of packet2emesh.v
|
|
|
|
wire [AW-1:0] dstaddr_in; // From pe2 of packet2emesh.v
|
|
|
|
wire [AW-1:0] srcaddr_in; // From pe2 of packet2emesh.v
|
|
|
|
wire write_in; // From pe2 of packet2emesh.v
|
|
|
|
// End of automatics
|
|
|
|
|
|
|
|
//####################################
|
|
|
|
//# DECODE
|
|
|
|
//####################################
|
|
|
|
|
|
|
|
packet2emesh #(.AW(AW))
|
|
|
|
pe2 (/*AUTOINST*/
|
|
|
|
// Outputs
|
|
|
|
.write_in (write_in),
|
|
|
|
.datamode_in (datamode_in[1:0]),
|
|
|
|
.ctrlmode_in (ctrlmode_in[4:0]),
|
|
|
|
.dstaddr_in (dstaddr_in[AW-1:0]),
|
|
|
|
.srcaddr_in (srcaddr_in[AW-1:0]),
|
|
|
|
.data_in (data_in[AW-1:0]),
|
|
|
|
// Inputs
|
|
|
|
.packet_in (packet_in[PW-1:0]));
|
|
|
|
|
|
|
|
assign reg_write = access_in & write_in;
|
|
|
|
assign reg_read = access_in & ~write_in;
|
|
|
|
assign reg_wdata[63:0] = {srcaddr_in[AW-1:0],data_in[AW-1:0]};
|
|
|
|
|
|
|
|
assign config_write = reg_write & (dstaddr_in[7:2]==`SPI_CONFIG);
|
|
|
|
assign status_write = reg_write & (dstaddr_in[7:2]==`SPI_STATUS);
|
|
|
|
assign clkdiv_write = reg_write & (dstaddr_in[7:2]==`SPI_CLKDIV);
|
|
|
|
|
|
|
|
//####################################
|
|
|
|
//# CONFIG
|
|
|
|
//####################################
|
|
|
|
|
|
|
|
always @ (posedge clk or negedge nreset)
|
|
|
|
if (~nreset)
|
|
|
|
config_reg[7:0] <= 'b0;
|
|
|
|
else if(config_write)
|
|
|
|
config_reg[7:0] <= data_in[7:0];
|
|
|
|
|
2016-03-09 22:46:24 -05:00
|
|
|
assign spi_en = config_reg[0]; // enable spi
|
|
|
|
assign irq_en = config_reg[1]; // enable interrupt
|
|
|
|
assign cpol = config_reg[2]; // cpol
|
|
|
|
assign cpha = config_reg[3]; // cpha
|
|
|
|
assign lsbfirst = config_reg[4]; // send lsb first
|
|
|
|
assign manual_ss = config_reg[5]; // manually control ss pin
|
|
|
|
assign rxauto_mode = config_reg[6]; // rx auto return mode
|
|
|
|
assign emode = config_reg[7]; // emesh transfer mode
|
|
|
|
|
2016-03-08 19:33:53 -05:00
|
|
|
//####################################
|
|
|
|
//# STATUS
|
|
|
|
//####################################
|
|
|
|
|
|
|
|
always @ (posedge clk or negedge nreset)
|
|
|
|
if (~nreset)
|
|
|
|
status_reg[7:0] <= 'b0;
|
|
|
|
else if(status_write)
|
|
|
|
status_reg[7:0] <= reg_wdata[7:0];
|
|
|
|
else
|
2016-03-09 22:46:24 -05:00
|
|
|
status_reg[7:0] <= {4'b0, //7:3
|
|
|
|
spi_state[1:0], //2:1
|
2016-03-08 19:33:53 -05:00
|
|
|
(rx_access | status_reg[0])};//0
|
2016-03-09 22:46:24 -05:00
|
|
|
|
2016-03-08 19:33:53 -05:00
|
|
|
//####################################
|
|
|
|
//# CLKDIV
|
|
|
|
//####################################
|
|
|
|
|
|
|
|
always @ (posedge clk or negedge nreset)
|
|
|
|
if (~nreset)
|
|
|
|
clkdiv_reg[7:0] <= CLKDIV;
|
2016-03-09 22:46:24 -05:00
|
|
|
else if(clkdiv_write)
|
2016-03-08 19:33:53 -05:00
|
|
|
clkdiv_reg[7:0] <= reg_wdata[7:0];
|
2016-03-09 22:46:24 -05:00
|
|
|
|
2016-03-08 19:33:53 -05:00
|
|
|
//####################################
|
|
|
|
//# RX REG
|
|
|
|
//####################################
|
|
|
|
always @ (posedge clk)
|
|
|
|
if(rx_access)
|
|
|
|
rx_reg[7:0] <= rx_data[7:0];
|
|
|
|
|
|
|
|
endmodule // spi_master_regs
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// verilog-library-directories:("." "../../emesh/hdl" "../../common/hdl")
|
|
|
|
// End:
|