i2c_top.v
上传用户:lcztgy
上传日期:2007-03-17
资源大小:70k
文件大小:9k
源码类别:

并行计算

开发平台:

VHDL

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //                __     ___ _               ___ ____                         //
  3. //                    / (_) |__   ___  ___|_ _/ ___|                        //
  4. //                   / /| | '_  / _ / __|| | |                            //
  5. //                   V / | | |_) |  __/__ | | |___                         //
  6. //                   _/  |_|_.__/ ___||___/_______|                        //
  7. //                                                                            //
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //     Copyright (C) 2003-2006 VibesIC, Inc.   All rights reserved.           //
  10. //----------------------------------------------------------------------------//
  11. // This source code is provided by VibesIC,and be verified on VibesIC FPGA    //
  12. // development kit. The source code may be used and distributed without       //
  13. // restriction provided that this copyright statement is not removed from the //
  14. // file and that any derivative work contains the original copyright notice   //
  15. // and the associated disclaimer.                                             //
  16. //----------------------------------------------------------------------------//
  17. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED     //
  18. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF       //
  19. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE//
  20. // AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     //
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,//
  22. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,OR PROFITS; //
  23. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,   //
  24. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    //
  25. // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF     //
  26. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                 //
  27. //----------------------------------------------------------------------------//
  28. // 本设计由威百仕( VibesIC )提供,并在其产品中验证通过,您可以在此基础上修改,//
  29. // 复制并分发,但请您保留版权声明部分。我们并不承诺本设计可以用做商业产品,同时//
  30. // 我们不保证设计的通用性。为了方便更新以及修改请保留设计的版本信息,并对自行 //
  31. // 修改部分添加足够的注释。对设计如有其他建议,请到网站进行讨论。              //
  32. //                                                                            //
  33. ////////////////////////////////////////////////////////////////////////////////
  34. //  Company:       www.richic.com                                             //
  35. //  Company bbs:   www.edacn.net                                              //
  36. //  Engineer:      alex_yang                                                  //
  37. //                                                                            //
  38. //  Target Device: XC3S400-PQ208                                              //
  39. //  Tool versions: Simulation:    ModelSim SE 6.2a                            //
  40. //                 Synthesis:     XST(ise8.1...sp3)                           //
  41. //                 Place&Routing: ISE8.1...sp3                                //
  42. //                 Others tools:  UltraEdit-32 12.10a                         //
  43. //  Create Date:   2006-12-21 14:19                                           //
  44. //  Description:                                                              //
  45. //                                                                            //
  46. //  LOG:                                                                      //
  47. //       1. Revision 1.0 (Initial version)  2006-12-21 14:19  alex_yang       //
  48. //                                                                            //
  49. //       2. Revision 1.1  2006-12-26 9:11   alex_yang                         //
  50. //          Modify for VX-SP306                                               //
  51. ////////////////////////////////////////////////////////////////////////////////
  52. `timescale 1ns/1ns
  53. module i2c_top(clk, rst_n, sda, scl, hc_cp, hc_si);
  54.   input  clk;
  55.   input  rst_n;
  56.   inout  sda;
  57.   output scl;
  58.   output hc_cp;
  59.   output hc_si;
  60.   reg  wr;  //write eeprom command
  61.   reg  rd;  //read eeprom command
  62.   reg  [10:0] addr;   //write or read address.
  63.   reg  [7:0]  data_w; //write data to eeprom
  64.   wire ack;           //i2c write or read complete
  65.   wire [7:0] data_r;  //read data from eeprom
  66.  
  67.   reg [15:0] data_rep; //存储显示在七段数码管上的数据
  68.   reg show_ok;   //正确显示后的标志
  69.   reg wr_flag;   //记录是否已经发送过write,read command
  70.   
  71.   reg [6:0]  cs,ns;
  72.   parameter  IDLE    = 7'b0000001,
  73.              WR_BYTE = 7'b0000010,
  74.              WR_ACK  = 7'b0000100,
  75.              DELAY   = 7'B0001000,
  76.              RD_BYTE = 7'b0010000,
  77.              RD_ACK  = 7'b0100000,
  78.              SHOW    = 7'b1000000;
  79.              
  80.   //-------------产生时钟clk分频-----------------------------------------------
  81.   reg [11:0] clk_cnt; 
  82.   reg [12:0] clk_div_cnt;
  83.   reg clk_div;
  84.   always @ (posedge clk or negedge rst_n)
  85.     if (!rst_n) 
  86.       clk_cnt <= 12'd0;
  87.     else if (clk_cnt == 12'd1250)  //get 20khz SCL clock  
  88.       clk_cnt <= 12'd0;
  89.     else 
  90.       clk_cnt <= clk_cnt + 1'b1;
  91.       
  92.   always @ (posedge clk or negedge rst_n)
  93.     if (!rst_n)
  94.       clk_div <= 1'b0;
  95.     else if (clk_cnt == 12'd1250)
  96.       clk_div <= ~clk_div;       //20khz
  97.       
  98.   always @ (posedge clk_div or negedge rst_n)
  99.     if (!rst_n)
  100.       clk_div_cnt <= 13'd0;
  101.     else
  102.       clk_div_cnt <= clk_div_cnt + 1'b1;
  103.       
  104.   //---------State machine------------------
  105.   always @ (posedge clk_div or negedge rst_n)
  106.     if (!rst_n)
  107.       cs <= IDLE;
  108.     else 
  109.       cs <= ns;
  110.       
  111.   always @ ( * )
  112.     case (cs)
  113.       IDLE:
  114.         if (clk_div_cnt == 13'd1000)
  115.           ns = WR_BYTE;
  116.         else
  117.           ns = IDLE;
  118.       WR_BYTE:
  119.         if (ack)
  120.           ns = WR_ACK;
  121.         else
  122.           ns = WR_BYTE;
  123.       WR_ACK:
  124.         if (!ack)
  125.             ns = DELAY;
  126.         else
  127.           ns = WR_ACK;
  128.       DELAY://eeprom写数据需要时间(请参考SPEC),故:等待数据写完后再进行读操作
  129.         if (clk_div_cnt == 13'd1300)
  130.           ns = RD_BYTE;
  131.         else
  132.           ns = DELAY;
  133.       RD_BYTE:
  134.         if (ack)
  135.           ns = RD_ACK;
  136.         else
  137.           ns = RD_BYTE;
  138.       RD_ACK:
  139.         if (!ack)
  140.           ns = SHOW;
  141.         else
  142.           ns = RD_ACK;
  143.       SHOW:
  144.         if (show_ok)
  145.           ns = IDLE;
  146.         else
  147.           ns = SHOW;
  148.       default:
  149.         ns = IDLE;
  150.     endcase
  151.     
  152.   always @ (posedge clk_div or negedge rst_n)
  153.     if (!rst_n)
  154.       begin
  155.         wr <= 1'b0;
  156.         rd <= 1'b0;
  157.         addr <= 11'd0;
  158.         data_w <= 8'd0;
  159. //        data_w <= 8'h58;
  160.         show_ok <= 1'b0;
  161.         wr_flag <= 1'b0;
  162.       end
  163.     else if (data_w == 8'd255) //计数到255就重新开始
  164.       begin
  165.         addr <= 11'd0;
  166.         data_w <= 8'd0;
  167. //        data_w <= 8'h58;
  168.       end
  169.     else
  170.       case (cs)
  171.         IDLE:
  172.           begin
  173.             wr <= 1'b0;
  174.             rd <= 1'b0;
  175.             show_ok <= 1'b0;
  176.             wr_flag <= 1'b0;
  177.           end
  178.         WR_BYTE:
  179.           if (wr_flag == 1'b0)
  180.             begin
  181.               wr <= 1'b1;
  182.               wr_flag <= 1'b1;
  183.             end
  184.           else
  185.             wr <= 1'b0;
  186.         WR_ACK:
  187.           wr_flag <= 1'b0;
  188.         DELAY:
  189.           begin
  190.             wr <= 1'b0;
  191.             rd <= 1'b0;
  192.             show_ok <= 1'b0;
  193.             wr_flag <= 1'b0;
  194.           end
  195.         RD_BYTE:
  196.           if (wr_flag == 1'b0)
  197.             begin
  198.               rd <= 1'b1;
  199.               wr_flag <= 1'b1;
  200.             end
  201.           else
  202.             rd <= 1'b0;
  203.         RD_ACK:
  204.           begin
  205.             wr_flag <= 1'b0;
  206.             show_ok <= 1'b1; 
  207.           end
  208.         SHOW:
  209.           begin
  210.             show_ok <= 1'b0;
  211.             addr <= addr + 1'b1;
  212.             data_w <= data_w + 1'b1;
  213.           end
  214.         default:
  215.           begin
  216.             wr <= 1'b0;
  217.             rd <= 1'b0;
  218.             show_ok <= 1'b0;
  219.             wr_flag <= 1'b0;
  220.           end
  221.       endcase
  222.       
  223.   always @ (posedge clk_div or negedge rst_n)
  224.     if (!rst_n)
  225.       data_rep <=16'h0000;
  226.     else if (show_ok)
  227.       data_rep <= {data_w,data_r}; //将写入eeprom和从eeprom中读出的数据进行对比输出
  228.     
  229.   // ---------------------------------------------------------------------------
  230.   // 例化EEPROM: 24C02的驱动程序
  231.   // ---------------------------------------------------------------------------
  232.   i2c_wr i2c_wr_inst(
  233.       .clk          (clk_div),
  234.       .rst_n        (rst_n),
  235.       .wr           (wr),
  236.     .rd           (rd),
  237.     .addr         (addr),
  238.     .data_w       (data_w),
  239.     .data_r       (data_r),
  240.     .ack          (ack),
  241.       .scl          (scl),
  242.       .sda          (sda)
  243.     );
  244.   // ---------------------------------------------------------------------------
  245.   // 例化hc164的驱动程序
  246.   // ---------------------------------------------------------------------------
  247.   hc164_driver hc164_driver_inst(
  248.       .clk         ( clk ),
  249.       .rst_n       ( rst_n ),
  250.       .led         ( {4{ ack }} ),
  251.       .dot         ( 4'b0000 ),
  252.       .seg_value   ( data_rep ),
  253.       .hc_cp       ( hc_cp ), 
  254.       .hc_si       ( hc_si )  
  255.       );
  256.       
  257.       
  258. endmodule