|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本人从网上抄了一个spi slave的fpga代码,综合后下载到spartan-6中,上电后fpga的mosi口一直为低,无法通讯,请高手指点!
代码如下:
module spislave(nrst, clk, ncs, mosi, sck);
//====================================================================
// PORT declarations
//====================================================================
input clk, nrst;
input ncs, mosi, sck;
//====================================================================
//INTERNALS signal
wire sck_riseedge, sck_falledge;
reg[7:0] byte_received;
reg[3:0] bit_received_cnt;
reg rec_flag;
reg[1:0] rec_status;
reg[7:0] rec_data;
reg[2:0] rec_flag_width;
//====================================================================
reg[2:0] sck_edge;
always @ (posedge clk or negedge nrst)
begin
if(~nrst)
begin
sck_edge <= 3'b000;
end
else
begin
sck_edge <= {sck_edge[1:0], sck};
end
end
assign sck_riseedge = (sck_edge[2:1] == 2'b01);
assign sck_falledge = (sck_edge[2:1] == 2'b10);
always @ (posedge clk or negedge nrst)
begin
if(~nrst)
begin
byte_received <= 8'h00;
bit_received_cnt <= 4'h0;
rec_flag <= 1'b0;
rec_status <= 2'b00;
rec_flag_width <= 3'b000;
end
else
begin
if(~ncs)
begin
case (rec_status)
2'b00: begin
if(sck_riseedge)
begin
byte_received <= {byte_received[6:0], mosi};
if(bit_received_cnt == 4'h7)
begin
bit_received_cnt <= 4'b0000;
rec_status <= 2'b01;
end
else
begin
bit_received_cnt <= bit_received_cnt+1;
end
end
end
2'b01: begin
rec_data <= byte_received;
rec_flag <= 1'b1;
if(rec_flag_width==3'b100) begin
rec_flag_width <= 3'b000;
rec_status <= 2'b11;
end
else begin
rec_flag_width <= rec_flag_width+1;
end
end
2'b11: begin
rec_flag <= 1'b0;
rec_status <= 2'b00;
end
endcase
end
end
end
endmodule
我的ucf文件:
##
NET clk LOC = R7 | TNM_NET = sys_clk_pin | IOSTANDARD = "LVCMOS33";
TIMESPEC TS_sys_clk_pin = PERIOD sys_clk_pin 24576 kHz;
##
NET nrst LOC = N8 | IOSTANDARD = "LVCMOS33";
##
NET mosi LOC = D16 | IOSTANDARD = "LVCMOS33"; //数据接收端,一直为低,导致mcu无法发数据过来
NET mosi PULLUP;
NET "sck" CLOCK_DEDICATED_ROUTE = FALSE;
NET sck LOC = E16 | IOSTANDARD = "LVCMOS33";
NET ncs LOC = G16 | IOSTANDARD = "LVCMOS33";
#NET "spi_data" PULLUP; //配置中加了上拉也没有用
|
|