6 changed files with 751 additions and 92 deletions
-
106led_test.pds
-
288source/src/spi_reg_reader.v
-
259source/src/spi_reg_reader.v.bak
-
10source/src/top.v
-
90source/test/test_spi_reg_reader.v
-
90source/test/test_spi_reg_reader.v.bak
@ -0,0 +1,288 @@ |
|||
module spi_reg_reader ( |
|||
input clk, //clock input |
|||
input rst_n, //asynchronous reset input, low active |
|||
|
|||
//regbus interface |
|||
output reg [31:0] addr, |
|||
output reg [31:0] wr_data, |
|||
output reg wr_en, |
|||
input wire [31:0] rd_data, //received serial data |
|||
// |
|||
input wire spi_cs_pin, |
|||
input wire spi_clk_pin, |
|||
input wire spi_rx_pin, |
|||
output reg spi_tx_pin |
|||
); |
|||
|
|||
parameter STATE_IDLE = 0; |
|||
parameter STATE_RECEIVE_ADD = 1; |
|||
parameter STATE_READ_REG = 2; |
|||
parameter STATE_TRANSMIT_DATA = 3; |
|||
parameter STATE_RECEIVE_DATA = 4; |
|||
parameter STATE_WRITE_REG = 5; |
|||
parameter ADDRESS_WIDTH_BYTE_NUM = 2; |
|||
|
|||
|
|||
// |
|||
// 捕获SPI_CS的下降沿 和 SPI_CLK的上升沿 |
|||
// detect: |
|||
// spi_cs_negedge_tri |
|||
// spi_clk_posedge_tri |
|||
// |
|||
|
|||
reg spi_cs_last_state = 0; |
|||
reg spi_clk_last_state = 0; |
|||
assign spi_clk_posedge_tri = spi_clk_pin & ~spi_clk_last_state; |
|||
assign spi_clk_negedge_tri = ~spi_clk_pin & spi_clk_last_state; |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
spi_cs_last_state <= 1; |
|||
spi_clk_last_state <= 1; |
|||
end else begin |
|||
spi_cs_last_state <= spi_cs_pin; |
|||
spi_clk_last_state <= spi_clk_pin; |
|||
end |
|||
end |
|||
|
|||
|
|||
/******************************************************************************* |
|||
* SPI数据解析,及其部分状态更新 * |
|||
*******************************************************************************/ |
|||
|
|||
// |
|||
// |
|||
// |
|||
// cs : ----______________________________________________ |
|||
// clk : ----------____----____----____----____----____---- |
|||
// bitcnt : 0 1 2 ... 7 0 |
|||
// rx : . . . . . |
|||
// tx : <======><======><======><======><======> |
|||
// valid : . |
|||
// byte_cnt: 0 1 |
|||
// |
|||
|
|||
|
|||
reg [7:0] bit_cnt = 0; |
|||
reg first_edge = 1; |
|||
// first_edge 状态更新 |
|||
// 1:在spi_cs下降沿时候更新为0 |
|||
// 2:复位时候更新为1 |
|||
// bit_cnt 状态更新 |
|||
// 1:在spi_clk下降沿时候更新 |
|||
// 2:first_edge == 1时候更新 |
|||
// |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
bit_cnt <= 0; |
|||
first_edge <= 1; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
bit_cnt <= 0; |
|||
first_edge <= 1; |
|||
end else begin |
|||
if (spi_clk_negedge_tri) begin |
|||
|
|||
if (first_edge) begin |
|||
bit_cnt <= 0; |
|||
first_edge <= 0; |
|||
end else begin |
|||
if (bit_cnt == 7) bit_cnt <= 0; |
|||
else bit_cnt <= bit_cnt + 1; |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
|
|||
// |
|||
// byte_cnt |
|||
// 1:在spi_clk下降沿时候更新 |
|||
// 2:在bit_cnt == 7时候更新 |
|||
// |
|||
reg [7:0] spi_byte_cnt = 0; //byte_cnt |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
spi_byte_cnt <= 0; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
spi_byte_cnt <= 0; |
|||
end else begin |
|||
if (spi_clk_negedge_tri && bit_cnt == 7) begin |
|||
spi_byte_cnt <= spi_byte_cnt + 1; |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
// |
|||
// spi_tx_1byte_data 发送 |
|||
// |
|||
reg [7:0] spi_tx_1byte_data = 0; |
|||
always @(*) begin |
|||
spi_tx_pin <= spi_tx_1byte_data[bit_cnt]; |
|||
end |
|||
|
|||
|
|||
// |
|||
// spi_rx_1byte_data |
|||
// spi_rx_1byte_data_valid |
|||
// |
|||
reg [7:0] spi_rx_1byte_data = 0; |
|||
reg spi_rx_1byte_data_valid = 0; |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
spi_rx_1byte_data <= 0; |
|||
spi_rx_1byte_data_valid <= 0; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
spi_rx_1byte_data <= 0; |
|||
spi_rx_1byte_data_valid <= 0; |
|||
end else begin |
|||
|
|||
if (spi_clk_posedge_tri) begin |
|||
spi_rx_1byte_data[bit_cnt] <= spi_rx_pin; |
|||
end |
|||
|
|||
if (spi_clk_negedge_tri && bit_cnt == 7) spi_rx_1byte_data_valid <= 1; |
|||
else spi_rx_1byte_data_valid <= 0; |
|||
|
|||
end |
|||
end |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* 缓存接收到的数据 * |
|||
*******************************************************************************/ |
|||
|
|||
|
|||
reg [7:0] spi_rx_data_cache [0:4+4-1]; |
|||
reg [7:0] rx_byte_count = 0; |
|||
genvar i; |
|||
|
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
rx_byte_count <= 0; |
|||
spi_rx_data_cache[0] <= 0; |
|||
spi_rx_data_cache[1] <= 0; |
|||
spi_rx_data_cache[2] <= 0; |
|||
spi_rx_data_cache[3] <= 0; |
|||
spi_rx_data_cache[4] <= 0; |
|||
spi_rx_data_cache[5] <= 0; |
|||
spi_rx_data_cache[6] <= 0; |
|||
spi_rx_data_cache[7] <= 0; |
|||
|
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
// 失能状态 |
|||
rx_byte_count <= 0; |
|||
spi_rx_data_cache[0] <= 0; |
|||
spi_rx_data_cache[1] <= 0; |
|||
spi_rx_data_cache[2] <= 0; |
|||
spi_rx_data_cache[3] <= 0; |
|||
spi_rx_data_cache[4] <= 0; |
|||
spi_rx_data_cache[5] <= 0; |
|||
spi_rx_data_cache[6] <= 0; |
|||
spi_rx_data_cache[7] <= 0; |
|||
end else begin |
|||
// 选中状态 |
|||
if (spi_rx_1byte_data_valid) begin |
|||
rx_byte_count <= rx_byte_count + 1; |
|||
if (rx_byte_count < ADDRESS_WIDTH_BYTE_NUM + 4) begin |
|||
spi_rx_data_cache[rx_byte_count] <= spi_rx_1byte_data; |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
|
|||
|
|||
/******************************************************************************* |
|||
* 自动设置SPI需要发送的数据 spi_tx_1byte_data * |
|||
*******************************************************************************/ |
|||
always @(*) begin |
|||
case (spi_byte_cnt) |
|||
ADDRESS_WIDTH_BYTE_NUM + 0: spi_tx_1byte_data <= rd_data[7:0]; |
|||
ADDRESS_WIDTH_BYTE_NUM + 1: spi_tx_1byte_data <= rd_data[15:8]; |
|||
ADDRESS_WIDTH_BYTE_NUM + 2: spi_tx_1byte_data <= rd_data[23:16]; |
|||
ADDRESS_WIDTH_BYTE_NUM + 3: spi_tx_1byte_data <= rd_data[31:24]; |
|||
default: spi_tx_1byte_data <= 0; |
|||
endcase |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* 自动设置addr数值 * |
|||
*******************************************************************************/ |
|||
always @(*) begin |
|||
case (ADDRESS_WIDTH_BYTE_NUM) |
|||
0: begin |
|||
addr[7:0] <= 0; |
|||
addr[15:8] <= 0; |
|||
addr[23:16] <= 0; |
|||
addr[31:24] <= 0; |
|||
end |
|||
1: begin |
|||
addr[7:0] <= {1'b0, spi_rx_data_cache[0][6:0]}; |
|||
addr[15:8] <= 0; |
|||
addr[23:16] <= 0; |
|||
addr[31:24] <= 0; |
|||
end |
|||
2: begin |
|||
addr[7:0] <= spi_rx_data_cache[0][7:0]; |
|||
addr[15:8] <= {1'b0, spi_rx_data_cache[1][6:0]}; |
|||
addr[23:16] <= 0; |
|||
addr[31:24] <= 0; |
|||
end |
|||
3: begin |
|||
addr[7:0] <= spi_rx_data_cache[0][7:0]; |
|||
addr[15:8] <= spi_rx_data_cache[1][7:0]; |
|||
addr[23:16] <= {1'b0, spi_rx_data_cache[2][6:0]}; |
|||
addr[31:24] <= 0; |
|||
end |
|||
4: begin |
|||
addr[7:0] <= spi_rx_data_cache[0][7:0]; |
|||
addr[15:8] <= spi_rx_data_cache[1][7:0]; |
|||
addr[23:16] <= spi_rx_data_cache[2][7:0]; |
|||
addr[31:24] <= {1'b0, spi_rx_data_cache[3][6:0]}; |
|||
end |
|||
endcase |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* wr_data * |
|||
*******************************************************************************/ |
|||
always @(*) begin |
|||
wr_data[7:0] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM]; |
|||
wr_data[15:8] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM+1]; |
|||
wr_data[23:16] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM+2]; |
|||
wr_data[31:24] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM+3]; |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* wr_en * |
|||
*******************************************************************************/ |
|||
reg has_trigger_wr_en = 0; |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
wr_en <= 0; |
|||
has_trigger_wr_en <= 0; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
wr_en <= 0; |
|||
has_trigger_wr_en <= 0; |
|||
end else if (!spi_cs_pin && // |
|||
!has_trigger_wr_en && // |
|||
spi_byte_cnt == ADDRESS_WIDTH_BYTE_NUM + 4 && // |
|||
spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM-1][7]) begin |
|||
wr_en <= 1; |
|||
has_trigger_wr_en <= 1; |
|||
end else begin |
|||
wr_en <= 0; |
|||
end |
|||
end |
|||
end |
|||
|
|||
|
|||
|
|||
endmodule |
@ -0,0 +1,259 @@ |
|||
module spi_reg_reader ( |
|||
input clk, //clock input |
|||
input rst_n, //asynchronous reset input, low active |
|||
|
|||
//regbus interface |
|||
output reg [31:0] addr, |
|||
output reg [31:0] wr_data, |
|||
output reg wr_en, |
|||
input wire [31:0] rd_data, //received serial data |
|||
// |
|||
input wire spi_cs_pin, |
|||
input wire spi_clk_pin, |
|||
input wire spi_rx_pin, |
|||
output reg spi_tx_pin |
|||
); |
|||
|
|||
parameter STATE_IDLE = 0; |
|||
parameter STATE_RECEIVE_ADD = 1; |
|||
parameter STATE_READ_REG = 2; |
|||
parameter STATE_TRANSMIT_DATA = 3; |
|||
parameter STATE_RECEIVE_DATA = 4; |
|||
parameter STATE_WRITE_REG = 5; |
|||
parameter ADDRESS_WIDTH_BYTE_NUM = 2; |
|||
|
|||
|
|||
// |
|||
// 捕获SPI_CS的下降沿 和 SPI_CLK的上升沿 |
|||
// detect: |
|||
// spi_cs_negedge_tri |
|||
// spi_clk_posedge_tri |
|||
// |
|||
|
|||
reg spi_cs_last_state = 0; |
|||
reg spi_clk_last_state = 0; |
|||
assign spi_clk_posedge_tri = spi_clk_pin & ~spi_clk_last_state; |
|||
assign spi_clk_negedge_tri = ~spi_clk_pin & spi_clk_last_state; |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
spi_cs_last_state <= 1; |
|||
spi_clk_last_state <= 1; |
|||
end else begin |
|||
spi_cs_last_state <= spi_cs_pin; |
|||
spi_clk_last_state <= spi_clk_pin; |
|||
end |
|||
end |
|||
|
|||
|
|||
/******************************************************************************* |
|||
* SPI数据解析,及其部分状态更新 * |
|||
*******************************************************************************/ |
|||
|
|||
// |
|||
// |
|||
// |
|||
// cs : ----______________________________________________ |
|||
// clk : ----------____----____----____----____----____---- |
|||
// bitcnt : 0 1 2 ... 7 0 |
|||
// rx : . . . . . |
|||
// tx : <======><======><======><======><======> |
|||
// valid : . |
|||
// byte_cnt: 0 1 |
|||
// |
|||
|
|||
|
|||
reg [7:0] bit_cnt = 0; |
|||
reg first_edge = 1; |
|||
// first_edge 状态更新 |
|||
// 1:在spi_cs下降沿时候更新为0 |
|||
// 2:复位时候更新为1 |
|||
// bit_cnt 状态更新 |
|||
// 1:在spi_clk下降沿时候更新 |
|||
// 2:first_edge == 1时候更新 |
|||
// |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
bit_cnt <= 0; |
|||
first_edge <= 1; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
bit_cnt <= 0; |
|||
first_edge <= 1; |
|||
end else begin |
|||
if (first_edge) begin |
|||
bit_cnt <= 0; |
|||
first_edge <= 0; |
|||
end else begin |
|||
if (spi_clk_negedge_tri) begin |
|||
if (bit_cnt == 7) bit_cnt <= 0; |
|||
else bit_cnt <= bit_cnt + 1; |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
|
|||
// |
|||
// byte_cnt |
|||
// 1:在spi_clk下降沿时候更新 |
|||
// 2:在bit_cnt == 7时候更新 |
|||
// |
|||
reg [7:0] spi_byte_cnt = 0; //byte_cnt |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
spi_byte_cnt <= 0; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
spi_byte_cnt <= 0; |
|||
end else begin |
|||
if (spi_clk_negedge_tri && bit_cnt == 7) begin |
|||
spi_byte_cnt <= spi_byte_cnt + 1; |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
// |
|||
// spi_tx_1byte_data 发送 |
|||
// |
|||
reg [7:0] spi_tx_1byte_data = 0; |
|||
always @(*) begin |
|||
spi_tx_pin <= spi_tx_1byte_data[bit_cnt]; |
|||
end |
|||
|
|||
|
|||
// |
|||
// spi_rx_1byte_data |
|||
// spi_rx_1byte_data_valid |
|||
// |
|||
reg [7:0] spi_rx_1byte_data = 0; |
|||
reg spi_rx_1byte_data_valid = 0; |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
spi_rx_1byte_data <= 0; |
|||
spi_rx_1byte_data_valid <= 0; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
spi_rx_1byte_data <= 0; |
|||
spi_rx_1byte_data_valid <= 0; |
|||
end else begin |
|||
|
|||
if (spi_clk_posedge_tri) begin |
|||
spi_rx_1byte_data[bit_cnt] <= spi_rx_pin; |
|||
end |
|||
|
|||
if (spi_clk_negedge_tri && bit_cnt == 7) spi_rx_1byte_data_valid <= 1; |
|||
else spi_rx_1byte_data_valid <= 0; |
|||
|
|||
end |
|||
end |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* 缓存接收到的数据 * |
|||
*******************************************************************************/ |
|||
|
|||
|
|||
reg [7:0] spi_rx_data_cache [0:ADDRESS_WIDTH_BYTE_NUM+4-1] = 0; |
|||
reg [7:0] rx_byte_count = 0; |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
rx_byte_count <= 0; |
|||
spi_rx_data_cache[0] <= 0; |
|||
spi_rx_data_cache[1] <= 0; |
|||
spi_rx_data_cache[2] <= 0; |
|||
spi_rx_data_cache[3] <= 0; |
|||
spi_rx_data_cache[4] <= 0; |
|||
spi_rx_data_cache[5] <= 0; |
|||
end else begin |
|||
if (spi_cs_pin) begin |
|||
// 失能状态 |
|||
rx_byte_count <= 0; |
|||
spi_rx_data_cache[0] <= 0; |
|||
spi_rx_data_cache[1] <= 0; |
|||
spi_rx_data_cache[2] <= 0; |
|||
spi_rx_data_cache[3] <= 0; |
|||
spi_rx_data_cache[4] <= 0; |
|||
spi_rx_data_cache[5] <= 0; |
|||
end else begin |
|||
// 选中状态 |
|||
if (spi_rx_1byte_data_valid) begin |
|||
rx_byte_count <= rx_byte_count + 1; |
|||
if (rx_byte_count < ADDRESS_WIDTH_BYTE_NUM + 4) begin |
|||
spi_rx_data_cache[rx_byte_count] <= spi_rx_1byte_data; |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
|
|||
|
|||
/******************************************************************************* |
|||
* 自动设置SPI需要发送的数据 spi_tx_1byte_data * |
|||
*******************************************************************************/ |
|||
always @(*) begin |
|||
case (spi_byte_cnt) |
|||
ADDRESS_WIDTH_BYTE_NUM + 0: spi_tx_1byte_data <= rd_data[7:0]; |
|||
ADDRESS_WIDTH_BYTE_NUM + 1: spi_tx_1byte_data <= rd_data[15:8]; |
|||
ADDRESS_WIDTH_BYTE_NUM + 2: spi_tx_1byte_data <= rd_data[23:16]; |
|||
ADDRESS_WIDTH_BYTE_NUM + 3: spi_tx_1byte_data <= rd_data[31:24]; |
|||
default: spi_tx_1byte_data <= 0; |
|||
endcase |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* 自动设置addr数值 * |
|||
*******************************************************************************/ |
|||
always @(*) begin |
|||
case (ADDRESS_WIDTH_BYTE_NUM) |
|||
0: begin |
|||
addr[6:0] <= spi_rx_data_cache[0][6:0]; |
|||
end |
|||
1: begin |
|||
addr[7:0] <= spi_rx_data_cache[0][7:0]; |
|||
addr[14:8] <= spi_rx_data_cache[1][6:0]; |
|||
end |
|||
2: begin |
|||
addr[7:0] <= spi_rx_data_cache[0][7:0]; |
|||
addr[15:8] <= spi_rx_data_cache[1][7:0]; |
|||
addr[22:16] <= spi_rx_data_cache[2][6:0]; |
|||
end |
|||
3: begin |
|||
addr[7:0] <= spi_rx_data_cache[0][7:0]; |
|||
addr[15:8] <= spi_rx_data_cache[1][7:0]; |
|||
addr[23:16] <= spi_rx_data_cache[2][7:0]; |
|||
addr[30:24] <= spi_rx_data_cache[3][6:0]; |
|||
end |
|||
endcase |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* wr_data * |
|||
*******************************************************************************/ |
|||
always @(*) begin |
|||
wr_data[7:0] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM]; |
|||
wr_data[15:8] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM+1]; |
|||
wr_data[23:16] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM+2]; |
|||
wr_data[31:24] <= spi_rx_data_cache[ADDRESS_WIDTH_BYTE_NUM+3]; |
|||
end |
|||
|
|||
/******************************************************************************* |
|||
* wr_en * |
|||
*******************************************************************************/ |
|||
always @(posedge clk or negedge rst_n) begin |
|||
if (!rst_n) begin |
|||
wr_en <= 0; |
|||
end else begin |
|||
if (!spi_cs_pin && spi_clk_negedge_tri && spi_byte_cnt == ADDRESS_WIDTH_BYTE_NUM + 4) begin |
|||
wr_en <= 1; |
|||
end else begin |
|||
wr_en <= 0; |
|||
end |
|||
end |
|||
end |
|||
|
|||
|
|||
|
|||
endmodule |
@ -0,0 +1,90 @@ |
|||
`timescale 1ns / 1ns |
|||
module test_spi_reg_reader; |
|||
// input clk, |
|||
// input rst_n, |
|||
|
|||
// output reg [31:0] addr, |
|||
// output reg [31:0] wr_data, |
|||
// output reg wr_en, |
|||
// input wire [31:0] rd_data, |
|||
|
|||
// input wire spi_cs_pin, |
|||
// input wire spi_clk_pin, |
|||
// input wire spi_rx_pin, |
|||
// output reg spi_tx_pin |
|||
|
|||
reg clk_50m; |
|||
reg rst_n; |
|||
|
|||
wire [31:0] addr; |
|||
wire [31:0] wr_data; |
|||
wire wr_en; |
|||
reg [31:0] rd_data; |
|||
|
|||
reg spi_cs_pin; |
|||
reg spi_clk_pin; |
|||
reg spi_rx_pin; |
|||
wire spi_tx_pin; |
|||
|
|||
|
|||
|
|||
|
|||
spi_reg_reader spi_reg_reader_impl ( |
|||
.clk(clk_50m), |
|||
.rst_n(rst_n), |
|||
.addr(addr), |
|||
.wr_data(wr_data), |
|||
.wr_en(wr_en), |
|||
.rd_data(rd_data), |
|||
.spi_cs_pin(spi_cs_pin), |
|||
.spi_clk_pin(spi_clk_pin), |
|||
.spi_rx_pin(spi_rx_pin), |
|||
.spi_tx_pin(spi_tx_pin) |
|||
); |
|||
|
|||
// reg rst_n; |
|||
// reg [31:0] rd_data; |
|||
// reg spi_tx_pin; |
|||
// reg clk_50m; |
|||
// reg spi_cs_pin; |
|||
// reg spi_clk_pin; |
|||
// reg spi_rx_pin; |
|||
|
|||
|
|||
always #10 clk_50m = ~clk_50m; //20ns 50MHZ |
|||
integer i = 0; |
|||
reg [63:0] txdata = 64'h88_77_66_55_44_33_82_11; |
|||
initial begin |
|||
rst_n <= 0; |
|||
clk_50m <= 0; |
|||
spi_cs_pin <= 1; |
|||
spi_clk_pin <= 1; |
|||
spi_rx_pin <= 0; |
|||
rd_data <= 32'h11223344; |
|||
|
|||
#100; |
|||
rst_n <= 1; |
|||
|
|||
#100; |
|||
spi_cs_pin <= 0; |
|||
#100; |
|||
|
|||
repeat (64) begin |
|||
spi_clk_pin <= 0; |
|||
spi_rx_pin <= txdata[i]; |
|||
#200; |
|||
spi_clk_pin <= 1; |
|||
i = i + 1; |
|||
#200; |
|||
end |
|||
|
|||
spi_clk_pin <= 0; |
|||
#200; |
|||
spi_clk_pin <= 1; |
|||
#200; |
|||
spi_cs_pin <= 1; |
|||
|
|||
#1000; |
|||
$stop; |
|||
end |
|||
endmodule |
@ -0,0 +1,90 @@ |
|||
`timescale 1ns / 1ns |
|||
module test_spi_reg_reader; |
|||
// input clk, |
|||
// input rst_n, |
|||
|
|||
// output reg [31:0] addr, |
|||
// output reg [31:0] wr_data, |
|||
// output reg wr_en, |
|||
// input wire [31:0] rd_data, |
|||
|
|||
// input wire spi_cs_pin, |
|||
// input wire spi_clk_pin, |
|||
// input wire spi_rx_pin, |
|||
// output reg spi_tx_pin |
|||
|
|||
reg clk_50m; |
|||
reg rst_n; |
|||
|
|||
wire [31:0] addr; |
|||
wire [31:0] wr_data; |
|||
wire wr_en; |
|||
reg [31:0] rd_data; |
|||
|
|||
reg spi_cs_pin; |
|||
reg spi_clk_pin; |
|||
reg spi_rx_pin; |
|||
wire spi_tx_pin; |
|||
|
|||
|
|||
|
|||
|
|||
spi_reg_reader spi_reg_reader_impl ( |
|||
.clk(clk_50m), |
|||
.rst_n(rst_n), |
|||
.addr(addr), |
|||
.wr_data(wr_data), |
|||
.wr_en(wr_en), |
|||
.rd_data(rd_data), |
|||
.spi_cs_pin(spi_cs_pin), |
|||
.spi_clk_pin(spi_clk_pin), |
|||
.spi_rx_pin(spi_rx_pin), |
|||
.spi_tx_pin(spi_tx_pin) |
|||
); |
|||
|
|||
// reg rst_n; |
|||
// reg [31:0] rd_data; |
|||
// reg spi_tx_pin; |
|||
// reg clk_50m; |
|||
// reg spi_cs_pin; |
|||
// reg spi_clk_pin; |
|||
// reg spi_rx_pin; |
|||
|
|||
|
|||
always #10 clk_50m = ~clk_50m; //20ns 50MHZ |
|||
integer i = 0; |
|||
reg [63:0] txdata = 64'h11_22_33_44_55_66_77_88 |
|||
initial begin |
|||
rst_n <= 0; |
|||
clk_50m <= 0; |
|||
spi_cs_pin <= 1; |
|||
spi_clk_pin <= 1; |
|||
spi_rx_pin <= 0; |
|||
rd_data <= 32'h11223344; |
|||
|
|||
#100; |
|||
rst_n <= 1; |
|||
|
|||
#100; |
|||
spi_cs_pin <= 0; |
|||
#100; |
|||
|
|||
repeat (64) begin |
|||
spi_clk_pin <= 0; |
|||
spi_rx_pin <= txdata[i]; |
|||
#200; |
|||
spi_clk_pin <= 1; |
|||
i = i + 1; |
|||
#200; |
|||
end |
|||
|
|||
spi_clk_pin <= 0; |
|||
#200; |
|||
spi_clk_pin <= 1; |
|||
#200; |
|||
spi_cs_pin <= 1; |
|||
|
|||
#1000; |
|||
$stop; |
|||
end |
|||
endmodule |
Write
Preview
Loading…
Cancel
Save
Reference in new issue