OpenCores
URL https://opencores.org/ocsvn/i2c/i2c/trunk

Subversion Repositories i2c

[/] [i2c/] [trunk/] [rtl/] [verilog/] [i2c_master_bit_ctrl.v] - Diff between revs 27 and 29

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 27 Rev 29
Line 35... Line 35...
////                                                             ////
////                                                             ////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
 
 
//  CVS Log
//  CVS Log
//
//
//  $Id: i2c_master_bit_ctrl.v,v 1.5 2002-11-30 22:24:40 rherveille Exp $
//  $Id: i2c_master_bit_ctrl.v,v 1.6 2002-12-26 15:02:32 rherveille Exp $
//
//
//  $Date: 2002-11-30 22:24:40 $
//  $Date: 2002-12-26 15:02:32 $
//  $Revision: 1.5 $
//  $Revision: 1.6 $
//  $Author: rherveille $
//  $Author: rherveille $
//  $Locker:  $
//  $Locker:  $
//  $State: Exp $
//  $State: Exp $
//
//
// Change History:
// Change History:
//               $Log: not supported by cvs2svn $
//               $Log: not supported by cvs2svn $
 
//               Revision 1.5  2002/11/30 22:24:40  rherveille
 
//               Cleaned up code
 
//
//               Revision 1.4  2002/10/30 18:10:07  rherveille
//               Revision 1.4  2002/10/30 18:10:07  rherveille
//               Fixed some reported minor start/stop generation timing issuess.
//               Fixed some reported minor start/stop generation timing issuess.
//
//
//               Revision 1.3  2002/06/15 07:37:03  rherveille
//               Revision 1.3  2002/06/15 07:37:03  rherveille
//               Fixed a small timing bug in the bit controller.\nAdded verilog simulation environment.
//               Fixed a small timing bug in the bit controller.\nAdded verilog simulation environment.
Line 96... Line 99...
// Tsu:sta     4.7us            0.6us   setup time for a repeated start condition
// Tsu:sta     4.7us            0.6us   setup time for a repeated start condition
// Tsu:sto     4.0us            0.6us   setup time for a stop conditon
// Tsu:sto     4.0us            0.6us   setup time for a stop conditon
// Tbuf        4.7us            1.3us   Bus free time between a stop and start condition
// Tbuf        4.7us            1.3us   Bus free time between a stop and start condition
//
//
 
 
 
// synopsys translate_off
`include "timescale.v"
`include "timescale.v"
 
// synopsys translate_on
 
 
`include "i2c_master_defines.v"
`include "i2c_master_defines.v"
 
 
module i2c_master_bit_ctrl(clk, rst, nReset, clk_cnt, ena, cmd, cmd_ack, busy, din, dout, scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen);
module i2c_master_bit_ctrl(
 
        clk, rst, nReset,
 
        clk_cnt, ena, cmd, cmd_ack, busy, al, din, dout,
 
        scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen
 
        );
 
 
        //
        //
        // inputs & outputs
        // inputs & outputs
        //
        //
        input clk;
        input clk;
Line 112... Line 122...
        input ena;            // core enable signal
        input ena;            // core enable signal
 
 
        input [15:0] clk_cnt; // clock prescale value
        input [15:0] clk_cnt; // clock prescale value
 
 
        input  [3:0] cmd;
        input  [3:0] cmd;
        output       cmd_ack;
        output       cmd_ack; // command complete acknowledge
        reg cmd_ack;
        reg cmd_ack;
        output       busy;
        output       busy;    // i2c bus busy
        reg busy;
        reg busy;
 
        output       al;      // i2c bus arbitration lost
 
        reg al;
 
 
        input  din;
        input  din;
        output dout;
        output dout;
        reg dout;
        reg dout;
 
 
Line 138... Line 150...
        // variable declarations
        // variable declarations
        //
        //
 
 
        reg sSCL, sSDA;             // synchronized SCL and SDA inputs
        reg sSCL, sSDA;             // synchronized SCL and SDA inputs
        reg dscl_oen;               // delayed scl_oen
        reg dscl_oen;               // delayed scl_oen
 
        reg sda_chk;                // check SDA output (Multi-master arbitration)
        reg clk_en;                 // clock generation signals
        reg clk_en;                 // clock generation signals
        wire slave_wait;
        wire slave_wait;
//      reg [15:0] cnt = clk_cnt;   // clock divider counter (simulation)
//      reg [15:0] cnt = clk_cnt;   // clock divider counter (simulation)
        reg [15:0] cnt;             // clock divider counter (synthesis)
        reg [15:0] cnt;             // clock divider counter (synthesis)
 
 
        //
        //
        // module body
        // module body
        //
        //
 
 
        // synchronize SCL and SDA inputs
        // whenever the slave is not ready it can delay the cycle by pulling SCL low
        always @(posedge clk)
 
          begin
 
              sSCL <= #1 scl_i;
 
              sSDA <= #1 sda_i;
 
          end
 
 
 
        // delay scl_oen
        // delay scl_oen
        always @(posedge clk)
        always @(posedge clk)
          dscl_oen <= #1 scl_oen;
          dscl_oen <= #1 scl_oen;
 
 
        // whenever the slave is not ready it can delay the cycle by pulling SCL low
 
        assign slave_wait = dscl_oen && !sSCL;
        assign slave_wait = dscl_oen && !sSCL;
 
 
 
 
        // generate clk enable signal
        // generate clk enable signal
        always @(posedge clk or negedge nReset)
        always @(posedge clk or negedge nReset)
          if(~nReset)
          if(~nReset)
            begin
            begin
                cnt    <= #1 16'h0;
                cnt    <= #1 16'h0;
Line 173... Line 180...
          else if (rst)
          else if (rst)
            begin
            begin
                cnt    <= #1 16'h0;
                cnt    <= #1 16'h0;
                clk_en <= #1 1'b1;
                clk_en <= #1 1'b1;
            end
            end
          else if ( !(|cnt) || !ena)
          else if ( ~|cnt || ~ena)
 
            if (~slave_wait)
            begin
            begin
                cnt    <= #1 clk_cnt;
                cnt    <= #1 clk_cnt;
                clk_en <= #1 1'b1;
                clk_en <= #1 1'b1;
            end
            end
          else
          else
            begin
            begin
                if(!slave_wait)
                  cnt    <= #1 cnt;
 
                  clk_en <= #1 1'b0;
 
              end
 
          else
 
            begin
                  cnt <= #1 cnt - 16'h1;
                  cnt <= #1 cnt - 16'h1;
 
 
                clk_en <= #1 1'b0;
                clk_en <= #1 1'b0;
            end
            end
 
 
 
 
        // generate bus status controller
        // generate bus status controller
        reg dSDA;
        reg dSCL, dSDA;
        reg sta_condition;
        reg sta_condition;
        reg sto_condition;
        reg sto_condition;
 
 
 
        // synchronize SCL and SDA inputs
 
        // reduce metastability risc
 
        always @(posedge clk)
 
          begin
 
              sSCL <= #1 scl_i;
 
              sSDA <= #1 sda_i;
 
 
 
              dSCL <= #1 sSCL;
 
              dSDA <= #1 sSDA;
 
          end
 
 
        // detect start condition => detect falling edge on SDA while SCL is high
        // detect start condition => detect falling edge on SDA while SCL is high
        // detect stop condition => detect rising edge on SDA while SCL is high
        // detect stop condition => detect rising edge on SDA while SCL is high
        always @(posedge clk)
        always @(posedge clk)
          begin
          begin
              dSDA <= #1 sSDA; // generate a delayed version of sSDA
 
 
 
              sta_condition <= #1 ~sSDA &  dSDA & sSCL;
              sta_condition <= #1 ~sSDA &  dSDA & sSCL;
              sto_condition <= #1  sSDA & ~dSDA & sSCL;
              sto_condition <= #1  sSDA & ~dSDA & sSCL;
          end
          end
 
 
        // generate bus busy signal
        // generate bus busy signal
Line 211... Line 231...
          else if (rst)
          else if (rst)
            busy <= #1 1'b0;
            busy <= #1 1'b0;
          else
          else
            busy <= #1 (sta_condition | busy) & ~sto_condition;
            busy <= #1 (sta_condition | busy) & ~sto_condition;
 
 
 
        // generate arbitration lost signal
 
        // aribitration lost when:
 
        // 1) master drives SDA high, but the i2c bus is low
 
        // 2) stop detected while not requested
 
        reg cmd_stop, dcmd_stop;
 
        always @(posedge clk)
 
        begin
 
          cmd_stop  <= #1 cmd == `I2C_CMD_STOP;
 
          dcmd_stop <= #1 cmd_stop;
 
 
 
          al <= #1 (sda_chk & ~sSDA & sda_oen) | (sto_condition & ~dcmd_stop);
 
        end
 
 
 
        // generate dout signal (store SDA on rising edge of SCL)
 
        always @(posedge clk)
 
          if(sSCL & ~dSCL)
 
            dout <= #1 sSDA;
 
 
        // generate statemachine
        // generate statemachine
 
 
        // nxt_state decoder
        // nxt_state decoder
        parameter [16:0] idle    = 17'b0_0000_0000_0000_0000;
        parameter [16:0] idle    = 17'b0_0000_0000_0000_0000;
Line 241... Line 278...
        always @(posedge clk or negedge nReset)
        always @(posedge clk or negedge nReset)
          if (!nReset)
          if (!nReset)
            begin
            begin
                c_state <= #1 idle;
                c_state <= #1 idle;
                cmd_ack <= #1 1'b0;
                cmd_ack <= #1 1'b0;
                dout    <= #1 1'b0;
 
                scl_oen <= #1 1'b1;
                scl_oen <= #1 1'b1;
                sda_oen <= #1 1'b1;
                sda_oen <= #1 1'b1;
 
                sda_chk <= #1 1'b0;
            end
            end
          else if (rst)
          else if (rst | al)
            begin
            begin
                c_state <= #1 idle;
                c_state <= #1 idle;
                cmd_ack <= #1 1'b0;
                cmd_ack <= #1 1'b0;
                dout    <= #1 1'b0;
 
                scl_oen <= #1 1'b1;
                scl_oen <= #1 1'b1;
                sda_oen <= #1 1'b1;
                sda_oen <= #1 1'b1;
 
                sda_chk <= #1 1'b0;
            end
            end
          else
          else
            begin
            begin
                cmd_ack   <= #1 1'b0; // default no command acknowledge + assert cmd_ack only 1clk cycle
                cmd_ack   <= #1 1'b0; // default no command acknowledge + assert cmd_ack only 1clk cycle
 
 
Line 281... Line 318...
                            c_state <= #1 idle;
                            c_state <= #1 idle;
                        endcase
                        endcase
 
 
                        scl_oen <= #1 scl_oen; // keep SCL in same state
                        scl_oen <= #1 scl_oen; // keep SCL in same state
                        sda_oen <= #1 sda_oen; // keep SDA in same state
                        sda_oen <= #1 sda_oen; // keep SDA in same state
 
                        sda_chk <= #1 1'b0;    // don't check SDA output
                    end
                    end
 
 
                    // start
                    // start
                    start_a:
                    start_a:
                    begin
                    begin
                        c_state <= #1 start_b;
                        c_state <= #1 start_b;
                        scl_oen <= #1 scl_oen; // keep SCL in same state
                        scl_oen <= #1 scl_oen; // keep SCL in same state
                        sda_oen <= #1 1'b1;    // set SDA high
                        sda_oen <= #1 1'b1;    // set SDA high
 
                        sda_chk <= #1 1'b0;    // don't check SDA output
                    end
                    end
 
 
                    start_b:
                    start_b:
                    begin
                    begin
                        c_state <= #1 start_c;
                        c_state <= #1 start_c;
                        scl_oen <= #1 1'b1; // set SCL high
                        scl_oen <= #1 1'b1; // set SCL high
                        sda_oen <= #1 1'b1; // keep SDA high
                        sda_oen <= #1 1'b1; // keep SDA high
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    start_c:
                    start_c:
                    begin
                    begin
                        c_state <= #1 start_d;
                        c_state <= #1 start_d;
                        scl_oen <= #1 1'b1; // keep SCL high
                        scl_oen <= #1 1'b1; // keep SCL high
                        sda_oen <= #1 1'b0; // set SDA low
                        sda_oen <= #1 1'b0; // set SDA low
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    start_d:
                    start_d:
                    begin
                    begin
                        c_state <= #1 start_e;
                        c_state <= #1 start_e;
                        scl_oen <= #1 1'b1; // keep SCL high
                        scl_oen <= #1 1'b1; // keep SCL high
                        sda_oen <= #1 1'b0; // keep SDA low
                        sda_oen <= #1 1'b0; // keep SDA low
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    start_e:
                    start_e:
                    begin
                    begin
                        c_state <= #1 idle;
                        c_state <= #1 idle;
                        cmd_ack <= #1 1'b1;
                        cmd_ack <= #1 1'b1;
                        scl_oen <= #1 1'b0; // set SCL low
                        scl_oen <= #1 1'b0; // set SCL low
                        sda_oen <= #1 1'b0; // keep SDA low
                        sda_oen <= #1 1'b0; // keep SDA low
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    // stop
                    // stop
                    stop_a:
                    stop_a:
                    begin
                    begin
                        c_state <= #1 stop_b;
                        c_state <= #1 stop_b;
                        scl_oen <= #1 1'b0; // keep SCL low
                        scl_oen <= #1 1'b0; // keep SCL low
                        sda_oen <= #1 1'b0; // set SDA low
                        sda_oen <= #1 1'b0; // set SDA low
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    stop_b:
                    stop_b:
                    begin
                    begin
                        c_state <= #1 stop_c;
                        c_state <= #1 stop_c;
                        scl_oen <= #1 1'b1; // set SCL high
                        scl_oen <= #1 1'b1; // set SCL high
                        sda_oen <= #1 1'b0; // keep SDA low
                        sda_oen <= #1 1'b0; // keep SDA low
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    stop_c:
                    stop_c:
                    begin
                    begin
                        c_state <= #1 stop_d;
                        c_state <= #1 stop_d;
                        scl_oen <= #1 1'b1; // keep SCL high
                        scl_oen <= #1 1'b1; // keep SCL high
                        sda_oen <= #1 1'b0; // keep SDA low
                        sda_oen <= #1 1'b0; // keep SDA low
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    stop_d:
                    stop_d:
                    begin
                    begin
                        c_state <= #1 idle;
                        c_state <= #1 idle;
                        cmd_ack <= #1 clk_en;
                        cmd_ack <= #1 clk_en;
                        scl_oen <= #1 1'b1; // keep SCL high
                        scl_oen <= #1 1'b1; // keep SCL high
                        sda_oen <= #1 1'b1; // set SDA high
                        sda_oen <= #1 1'b1; // set SDA high
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    // read
                    // read
                    rd_a:
                    rd_a:
                    begin
                    begin
                        c_state <= #1 rd_b;
                        c_state <= #1 rd_b;
                        scl_oen <= #1 1'b0; // keep SCL low
                        scl_oen <= #1 1'b0; // keep SCL low
                        sda_oen <= #1 1'b1; // tri-state SDA
                        sda_oen <= #1 1'b1; // tri-state SDA
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    rd_b:
                    rd_b:
                    begin
                    begin
                        c_state <= #1 rd_c;
                        c_state <= #1 rd_c;
                        scl_oen <= #1 1'b1; // set SCL high
                        scl_oen <= #1 1'b1; // set SCL high
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    rd_c:
                    rd_c:
                    begin
                    begin
                        c_state <= #1 rd_d;
                        c_state <= #1 rd_d;
                        dout <= #1 sSDA;
 
                        scl_oen <= #1 1'b1; // keep SCL high
                        scl_oen <= #1 1'b1; // keep SCL high
                        sda_oen <= #1 1'b1;
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    rd_d:
                    rd_d:
                    begin
                    begin
                        c_state <= #1 idle;
                        c_state <= #1 idle;
                        cmd_ack <= #1 clk_en;
                        cmd_ack <= #1 clk_en;
                        scl_oen <= #1 1'b0; // set SCL low
                        scl_oen <= #1 1'b0; // set SCL low
                        sda_oen <= #1 1'b1;
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
 
                        sda_chk <= #1 1'b0; // don't check SDA output
                    end
                    end
 
 
                    // write
                    // write
                    wr_a:
                    wr_a:
                    begin
                    begin
                        c_state <= #1 wr_b;
                        c_state <= #1 wr_b;
                        scl_oen <= #1 1'b0; // keep SCL low
                        scl_oen <= #1 1'b0; // keep SCL low
                        sda_oen <= #1 din;  // set SDA
                        sda_oen <= #1 din;  // set SDA
 
                        sda_chk <= #1 1'b0; // don't check SDA output (SCL low)
                    end
                    end
 
 
                    wr_b:
                    wr_b:
                    begin
                    begin
                        c_state <= #1 wr_c;
                        c_state <= #1 wr_c;
                        scl_oen <= #1 1'b1; // set SCL high
                        scl_oen <= #1 1'b1; // set SCL high
                        sda_oen <= #1 din;  // keep SDA
                        sda_oen <= #1 din;  // keep SDA
 
                        sda_chk <= #1 1'b1; // check SDA output
                    end
                    end
 
 
                    wr_c:
                    wr_c:
                    begin
                    begin
                        c_state <= #1 wr_d;
                        c_state <= #1 wr_d;
                        scl_oen <= #1 1'b1; // keep SCL high
                        scl_oen <= #1 1'b1; // keep SCL high
                        sda_oen <= #1 din;
                        sda_oen <= #1 din;
 
                        sda_chk <= #1 1'b1; // check SDA output
                    end
                    end
 
 
                    wr_d:
                    wr_d:
                    begin
                    begin
                        c_state <= #1 idle;
                        c_state <= #1 idle;
                        cmd_ack <= #1 1'b1;
                        cmd_ack <= #1 1'b1;
                        scl_oen <= #1 1'b0; // set SCL low
                        scl_oen <= #1 1'b0; // set SCL low
                        sda_oen <= #1 din;
                        sda_oen <= #1 din;
 
                        sda_chk <= #1 1'b0; // don't check SDA output (SCL low)
                    end
                    end
 
 
                  endcase
                  endcase
            end
            end
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.