本实验讲究实用性,故设计思想为:主机先向从机发送地址,若是向从机写入数据,则向从机发送数据,若是读取从机数据,则向从机发送时钟,然后在时钟下降沿读取数据即可。cs信号上升沿作为SPI通信的结束信号。rom程序只是做测试使用。

每次发送16个时钟信号,前八个是地址和命令,后八个是数据。其中:前8个时钟接受的数据的最高位决定着这次通信是读取数据还是写入数据,最高位为1,则是读取数据,为0则是写入数据。

程序:

/********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :spi_slave_2.v
** CreateDate :2015.004
** Funtions :spi通信试验。FPGA作为从机,与主机进行通信。先接收主机发来的地址,再根据地址最高位来判断是读数据还是些数据,
然后从机是接收数据还是送出数据。地址最高位为高则是读取数据,否则为写数据.上升沿接收数据,下降沿发送数据
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/
module spi_slave_2 (
clk,
rst_n, spi_cs,
spi_sck,
spi_miso,
spi_mosi, spi_over );
input clk;
input rst_n; input spi_cs;
input spi_sck;
input spi_mosi; output reg spi_miso;
output spi_over; //-----------------------------//
reg spi_cs_2,spi_cs_1;
reg spi_sck_2,spi_sck_1;
reg spi_mosi_2,spi_mosi_1;
wire spi_cs_pos;
wire spi_cs_flag;
wire spi_sck_neg;
wire spi_sck_pos;
wire spi_mosi_flag;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
{spi_cs_2,spi_cs_1} <= 'b11;
{spi_sck_2,spi_sck_1} <= 'b00;
{spi_mosi_2,spi_mosi_1} <= 'b00;
end
else
begin
{spi_cs_2,spi_cs_1} <= {spi_cs_1,spi_cs};
{spi_sck_2,spi_sck_1} <= {spi_sck_1,spi_sck};
{spi_mosi_2,spi_mosi_1} <= {spi_mosi_1,spi_mosi};
end
end assign spi_cs_pos = ~spi_cs_2 &spi_cs_1;
assign spi_cs_flag = spi_cs_2;
assign spi_sck_neg = ~spi_sck_1&spi_sck_2;
assign spi_sck_pos = ~spi_sck_2&spi_sck_1;
assign spi_mosi_flag = spi_mosi_2; assign spi_over = spi_cs_pos;
//----------------------------------------//
localparam idel = 'd0;
localparam rxd_addr = 'd1;
localparam jude_wr_rd = 'd2;
localparam rxd_data = 'd3;
localparam rxd_over = 'd4;
localparam txd_data = 'd5;
localparam txd_over = 'd6;
localparam end_sta = 'd7; reg [:] state;
reg [:] cnt;
reg [:] raddr;
reg [:] rdata;
reg [:] tdata;
reg rover_flag;
reg wover_flag;
reg rd_flag;
wire [:] data_out;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
state <= 'd0;
cnt <= ;
raddr <= 'd0;
rdata <= 'd0;
tdata <= 'd0;
rover_flag <= ;
wover_flag <= ;
rd_flag <= ;
spi_miso <= ;
end
else if(!spi_cs_flag)
begin
case(state)
idel:
begin
state <= rxd_addr;
cnt <= ;
raddr <= 'd0;
rdata <= 'd0;
tdata <= 'd0;
rover_flag <= ;
wover_flag <= ;
rd_flag <= ;
spi_miso <= ;
end
rxd_addr:
begin
if(cnt == )
begin
cnt <= ;
state <= jude_wr_rd;
end
else if(spi_sck_pos)
begin
cnt <= cnt + ;
raddr[ - cnt[:]] <= spi_mosi_flag;
end
end
jude_wr_rd:
begin
if(raddr[] == )
state <= rxd_data;
else
begin
state <= txd_data;
rd_flag <= ;
end
end
rxd_data:
begin
if(cnt == )
begin
cnt <= ;
state <= rxd_over;
end
else if(spi_sck_pos)
begin
cnt <= cnt + ;
rdata[ - cnt[:]] <= spi_mosi_flag;
end
end
rxd_over:
begin
rover_flag <= ;
state <= end_sta;
end
txd_data:
begin
tdata <= data_out;
if(cnt == )
begin
cnt <= ;
state <= txd_over;
end
else if(spi_sck_pos)
begin
cnt <= cnt + ;
spi_miso <= tdata[ - cnt[:]];
end
end
txd_over:
begin
wover_flag <= ;
state <= end_sta;
end
end_sta:
begin
rover_flag <= ;
wover_flag <= ;
state <= end_sta;
end
default:state <= 'd0;
endcase
end
else
begin
state <= 'd0;
cnt <= ;
raddr <= 'd0;
rdata <= 'd0;
tdata <= 'd0;
rover_flag <= ;
wover_flag <= ;
rd_flag <= ;
spi_miso <= ;
end
end data_rom data_rom_1 (
.clk(clk),
.rst_n(rst_n), .wr(rover_flag),
.rd(rd_flag), .addr(raddr[:]),
.data_in(rdata),
.data_out(data_out)
);
endmodule

ROM:

/********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :data_rom.v
** CreateDate :2015.04
** Funtions : 简单的数据读写存储程序,配合测试
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/
module data_rom (
clk,
rst_n, wr,
rd, addr,
data_in,
data_out
);
input clk;
input rst_n; input wr;
input rd;
input [:] addr;
input [:] data_in; output reg [:] data_out; reg [:] table_1 [:];
wire [:] table_2 [:];
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
table_1[] <= ;
table_1[] <= ;
table_1[] <= ;
table_1[] <= ;
table_1[] <= ;
table_1[] <= ;
table_1[] <= ;
table_1[] <= ;
data_out <= ;
end
else if(wr)
begin
table_1[addr] <= data_in;
end
else if(rd)
data_out <= table_1[addr];
else
begin
table_1[] <= table_1[];
table_1[] <= table_1[];
table_1[] <= table_1[];
table_1[] <= table_1[];
table_1[] <= table_1[];
table_1[] <= table_1[];
table_1[] <= table_1[];
table_1[] <= table_1[];
data_out <= data_out;
end
end assign table_2[] = table_1[];
assign table_2[] = table_1[];
assign table_2[] = table_1[];
assign table_2[] = table_1[];
assign table_2[] = table_1[];
assign table_2[] = table_1[];
assign table_2[] = table_1[];
assign table_2[] = table_1[]; endmodule

测试程序:

    /********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :spi_slave_tb.v
** CreateDate :2015.04
** Funtions :测试文件
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/ `timescale ns/ ns module spi_slave_tb ;
reg clk;
reg rst_n; reg spi_cs;
reg spi_sck;
wire spi_miso;
reg spi_mosi; wire spi_over; spi_slave_2 spi_slave_2_1(
.clk,
.rst_n, .spi_cs,
.spi_sck,
.spi_miso,
.spi_mosi, .spi_over
); parameter tck = ;
parameter t = /tck; always
#(t/) clk = ~clk; //-------------------------------
/* 模仿spi主机的发送程序,这个task很好,仿顺序操作,可以直观的显示过程 */
task spi_sd;
input [:] data_in;
begin
#(*t); spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[7]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[6]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[5]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[4]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[3]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[2]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[1]
spi_sck = ; spi_mosi= data_in[]; #(*t); spi_sck = ; #(*t); //send bit[0]
spi_sck = ; end
endtask initial
begin
clk = ;
rst_n = ;
spi_cs = ;
spi_sck = ;
spi_mosi = ; #(*t) rst_n = ;
#(*t);
spi_cs = ;
spi_sd('h81);
#(*t);
spi_sd('h04);
#(*t);
#(*t);
spi_cs = ; #(*t);
spi_cs = ;
spi_sd('h01);
#(*t);
spi_sd('h00);
#(*t);
spi_cs = ;
end endmodule

仿真图:

图中可以看出,第一次输入8‘h81,意味着向01的地址写入数据。第二个数8’h04,则是要写入的数据。然后写入数据8‘h01,则意味着要读取01地址的数据,然后发送8个时钟则是再读取数据。

SPI通信实验---verilog(FPGA作为从机,使用可读可写)-LMLPHP

04-29 02:35