打印
[FPGA]

fpga做从机的程序,求帮忙看下能否实现

[复制链接]
1085|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dgj111|  楼主 | 2014-7-18 13:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
FPGA, ck, os, npu, se
这是一个fpga做从机的程序,我是用dsp发送数据,已经可以发送了,可以从示波器看出来,然后fpga能否接收到数据,现在不知道怎么看,求高手帮忙看下这个程序能否实现fpga接受数据,是否可以通过modelsim仿真接受数据呢?求指导,,谢谢。。。上个帖子还没有结,不知道怎么给分,,不好意思!!!
程序如下,注释有的是自己注释的,
module spi_slave (clk,sck,mosi,miso,ssel,led);

input clk;
input sck,mosi;
input ssel;
output miso,led;

//sychronize  sck to the fpga clock using a 3-bits shift register
reg[2:0] sckr;
always @ (posedge clk)
        sckr <= {sckr[1:0],sck};//来一个时钟就把输入左移一位
wire sck_risingedge = (sckr[2:1]==2'b01);//now we can detect sck rising edges     ssel_active=1
wire sck_fallingedge = (sckr[2:1]==2'b10);//and falling edges   ssel_active=0

//same thing for ssel
//wire ssel_active = ssel;
reg[2:0] sselr;
always @ (posedge clk)
        sselr <={sselr[1:0],ssel};//来一个时钟就把输入左移一位
wire ssel_active =~sselr[1];//~按位取反
wire ssel_startmessage = (sselr[2:1]==2'b10);//message start at falling edge  ssel_active=0
wire ssel_endmessage = (sselr[2:1]==2'b01);//message stops at rising edge  ssel_active=1

//for MOSI
reg[1:0] mosir;
always @ (posedge clk)
        mosir <= {mosir[0],mosi};//不停采样
wire mosi_data = mosir[1];
//always @ (posedge clk)
//        wire mosi_data = mosi;

//now receiving data from the spi bus is easy
reg[2:0] bitcnt;// we handle spi in 8-bits format,so need a 3bits counter to count the bits as the come in
reg byte_received;//high when a byte has been received
reg [7:0] byte_data_received;
always @ (posedge clk)
        begin
                if(~ssel_active) //when上升沿,ssel_active=1
                 bitcnt <= 3'b000;
                else
                        if(sck_risingedge)//上升沿接收数据
                                begin
                                bitcnt <= bitcnt+3'b001;
                                byte_data_received <={byte_data_received[6:0],mosi_data};//implement a shift-left register(since we receive the data MSB first)
                        end
                end

always @ (posedge clk)
        begin
        byte_received <= ssel_active &&sck_risingedge&&(bitcnt==3'b111);       
        end
       
//we use the LSB of the data received to control an led
reg led;
always @ (posedge clk)
        begin
        if(byte_received)
                led <= byte_data_received[0];
        end
       
//finally the transmission part
reg[7:0] byte_data_sent;
reg[7:0] cnt;
always @ (posedge clk)
        if(ssel_startmessage)//下降沿发送数据,//message start at falling edge  ssel_active=0
        cnt <= cnt+8'h1;//count the messages
       
always @ (posedge clk)
        if(ssel_active)
        begin
                if(ssel_startmessage)
                byte_data_sent <= cnt;//first byte sent in a message is the message count
                else
                        if(sck_fallingedge)
                                begin
                                        if(bitcnt==3'b000)
                                                byte_data_sent <= 8'h00; //after that,we send 0s
                                        else
                                                byte_data_sent <={byte_data_sent[6:0],1'b0};
                                end
        end                                       
assign miso = byte_data_sent[7];

endmodule  

相关帖子

沙发
andous| | 2014-7-18 16:37 | 只看该作者
可以先仿真看看,写的逻辑是否正确,要做一个testbench,把master模拟出来。然后下到FPGA里面看看是否有数据出来。这接口我有调试稳定的程序。

使用特权

评论回复
板凳
andous| | 2014-7-18 16:38 | 只看该作者
SPI slave不困难,祝你成功

使用特权

评论回复
地板
dgj111|  楼主 | 2014-7-18 18:01 | 只看该作者
andous 发表于 2014-7-18 16:37
可以先仿真看看,写的逻辑是否正确,要做一个testbench,把master模拟出来。然后下到FPGA里面看看是否有数 ...

请问方便把你的调试稳定的程序发给我学习下吗?谢谢啦!!那个testbench文件,我还不怎么会写,这个程序已经调试好几天了,一直没出来,我觉得是没有理解那个时序,感觉有些乱。。

使用特权

评论回复
5
yulunna| | 2014-7-22 10:16 | 只看该作者
可以直接用软件带的逻辑分析工具查看,ALTERA的是signaltap

使用特权

评论回复
6
dgj111|  楼主 | 2014-7-23 22:35 | 只看该作者
yulunna 发表于 2014-7-22 10:16
可以直接用软件带的逻辑分析工具查看,ALTERA的是signaltap

这个好,要好好学习下,谢谢!!还有一个问题想请教,就是我现在dsp发送数据,然后fpga可以收到了,算是可以通信了,然后,我有很多数据要发送,要分地址的发送,请问,发送的时候怎样才能让fpga把接受到的地址和数据分开呢,然后dsp发送地址和数据的时候有什么要求吗?还有发送的这些地址和数据是存到fpga的什么地方了呢?谢谢!!!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

9

帖子

0

粉丝