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

Subversion Repositories i2c

[/] [i2c/] [trunk/] [rtl/] [verilog/] [i2c_master_bit_ctrl.v] - Diff between revs 68 and 74

Show entire file | Details | Blame | View Log

Rev 68 Rev 74
Line 44... Line 44...
//  $Author: rherveille $
//  $Author: rherveille $
//  $Locker:  $
//  $Locker:  $
//  $State: Exp $
//  $State: Exp $
//
//
// Change History:
// Change History:
//               $Log: not supported by cvs2svn $
//               $Log: $
 
//               Revision 1.14  2009/01/20 10:25:29  rherveille
 
//               Added clock synchronization logic
 
//               Fixed slave_wait signal
 
//
//               Revision 1.13  2009/01/19 20:29:26  rherveille
//               Revision 1.13  2009/01/19 20:29:26  rherveille
//               Fixed synopsys miss spell (synopsis)
//               Fixed synopsys miss spell (synopsis)
//               Fixed cr[0] register width
//               Fixed cr[0] register width
//               Fixed ! usage instead of ~
//               Fixed ! usage instead of ~
//               Fixed bit controller parameter width to 18bits
//               Fixed bit controller parameter width to 18bits
Line 135... Line 139...
// synopsys translate_on
// synopsys translate_on
 
 
`include "i2c_master_defines.v"
`include "i2c_master_defines.v"
 
 
module i2c_master_bit_ctrl(
module i2c_master_bit_ctrl(
        clk, rst, nReset,
    input             clk,      // system clock
        clk_cnt, ena, cmd, cmd_ack, busy, al, din, dout,
    input             rst,      // synchronous active high reset
        scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen
    input             nReset,   // asynchronous active low reset
 
    input             ena,      // core enable signal
 
 
 
    input      [15:0] clk_cnt,  // clock prescale value
 
 
 
    input      [ 3:0] cmd,      // command (from byte controller)
 
    output reg        cmd_ack,  // command complete acknowledge
 
    output reg        busy,     // i2c bus busy
 
    output reg        al,       // i2c bus arbitration lost
 
 
 
    input             din,
 
    output reg        dout,
 
 
 
    input             scl_i,    // i2c clock line input
 
    output            scl_o,    // i2c clock line output
 
    output reg        scl_oen,  // i2c clock line output enable (active low)
 
    input             sda_i,    // i2c data line input
 
    output            sda_o,    // i2c data line output
 
    output reg        sda_oen   // i2c data line output enable (active low)
        );
        );
 
 
        //
 
        // inputs & outputs
 
        //
 
        input clk;
 
        input rst;
 
        input nReset;
 
        input ena;            // core enable signal
 
 
 
        input [15:0] clk_cnt; // clock prescale value
 
 
 
        input  [3:0] cmd;
 
        output       cmd_ack; // command complete acknowledge
 
        reg cmd_ack;
 
        output       busy;    // i2c bus busy
 
        reg busy;
 
        output       al;      // i2c bus arbitration lost
 
        reg al;
 
 
 
        input  din;
 
        output dout;
 
        reg dout;
 
 
 
        // I2C lines
 
        input  scl_i;         // i2c clock line input
 
        output scl_o;         // i2c clock line output
 
        output scl_oen;       // i2c clock line output enable (active low)
 
        reg scl_oen;
 
        input  sda_i;         // i2c data line input
 
        output sda_o;         // i2c data line output
 
        output sda_oen;       // i2c data line output enable (active low)
 
        reg sda_oen;
 
 
 
 
 
        //
        //
        // variable declarations
        // variable declarations
        //
        //
 
 
        reg sSCL, sSDA;             // synchronized SCL and SDA inputs
    reg [ 1:0] cSCL, cSDA;      // capture SCL and SDA
 
    reg [ 2:0] fSCL, fSDA;      // SCL and SDA filter inputs
 
    reg        sSCL, sSDA;      // filtered and synchronized SCL and SDA inputs
        reg dSCL, dSDA;             // delayed versions of sSCL and sSDA
        reg dSCL, dSDA;             // delayed versions of sSCL and sSDA
        reg dscl_oen;               // delayed scl_oen
        reg dscl_oen;               // delayed scl_oen
        reg sda_chk;                // check SDA output (Multi-master arbitration)
        reg sda_chk;                // check SDA output (Multi-master arbitration)
        reg clk_en;                 // clock generation signals
        reg clk_en;                 // clock generation signals
        reg slave_wait;             // slave inserts wait states
        reg slave_wait;             // slave inserts wait states
//      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)
 
    reg [13:0] filter_cnt;      // clock divider for filter
 
 
 
 
        // state machine variable
        // state machine variable
        reg [17:0] c_state; // synopsys enum_state
        reg [17:0] c_state; // synopsys enum_state
 
 
        //
        //
Line 198... Line 191...
        // whenever the slave is not ready it can delay the cycle by pulling SCL low
        // whenever the slave is not ready it can delay the cycle by pulling SCL low
        // delay scl_oen
        // delay scl_oen
        always @(posedge clk)
        always @(posedge clk)
          dscl_oen <= #1 scl_oen;
          dscl_oen <= #1 scl_oen;
 
 
        // slave_wait is asserted when master wants to drive SCL high, but the slave (another master) pulls it low
    // slave_wait is asserted when master wants to drive SCL high, but the slave pulls it low
        // slave_wait remains asserted until the slave (other master) releases SCL
    // slave_wait remains asserted until the slave releases SCL
        always @(posedge clk or negedge nReset)
        always @(posedge clk or negedge nReset)
          if (!nReset) slave_wait <= 1'b0;
          if (!nReset) slave_wait <= 1'b0;
          else         slave_wait = (scl_oen & ~dscl_oen & ~sSCL) | (slave_wait & ~sSCL);
      else         slave_wait <= (scl_oen & ~dscl_oen & ~sSCL) | (slave_wait & ~sSCL);
 
 
        // master drives SCL high, but another master pulls it low
        // master drives SCL high, but another master pulls it low
        // master start counting down its low cycle now (clock synchronization)
        // master start counting down its low cycle now (clock synchronization)
        wire scl_sync   = dSCL & ~sSCL & scl_oen;
        wire scl_sync   = dSCL & ~sSCL & scl_oen;
 
 
 
 
        // 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;
                clk_en <= #1 1'b1;
                clk_en <= #1 1'b1;
            end
            end
          else if (rst)
      else if (rst || ~|cnt || !ena || scl_sync)
            begin
 
                cnt    <= #1 16'h0;
 
                clk_en <= #1 1'b1;
 
            end
 
          else if ( ~|cnt || !ena || scl_sync)
 
            begin
            begin
                cnt    <= #1 clk_cnt;
                cnt    <= #1 clk_cnt;
                clk_en <= #1 1'b1;
                clk_en <= #1 1'b1;
            end
            end
          else if (slave_wait)
          else if (slave_wait)
Line 238... Line 227...
                clk_en <= #1 1'b0;
                clk_en <= #1 1'b0;
            end
            end
 
 
 
 
        // generate bus status controller
        // generate bus status controller
        reg sta_condition;
 
        reg sto_condition;
 
 
 
        // synchronize SCL and SDA inputs
    // capture SDA and SCL
        // reduce metastability risc
    // reduce metastability risk
 
    always @(posedge clk or negedge nReset)
 
      if (!nReset)
 
      begin
 
          cSCL <= #1 2'b00;
 
          cSDA <= #1 2'b00;
 
      end
 
      else if (rst)
 
      begin
 
          cSCL <= #1 2'b00;
 
          cSDA <= #1 2'b00;
 
      end
 
      else
 
      begin
 
          cSCL <= {cSCL[0],scl_i};
 
          cSDA <= {cSDA[0],sda_i};
 
      end
 
 
 
 
 
    // filter SCL and SDA signals; (attempt to) remove glitches
 
    always @(posedge clk or negedge nReset)
 
      if      (!nReset     ) filter_cnt <= 14'h0;
 
      else if (rst || !ena ) filter_cnt <= 14'h0;
 
      else if (~|filter_cnt) filter_cnt <= clk_cnt >> 2; //16x I2C bus frequency
 
      else                   filter_cnt <= filter_cnt -1;
 
 
 
 
 
    always @(posedge clk or negedge nReset)
 
      if (!nReset)
 
      begin
 
          fSCL <= 3'b111;
 
          fSDA <= 3'b111;
 
      end
 
      else if (rst)
 
      begin
 
          fSCL <= 3'b111;
 
          fSDA <= 3'b111;
 
      end
 
      else if (~|filter_cnt)
 
      begin
 
          fSCL <= {fSCL[1:0],cSCL[1]};
 
          fSDA <= {fSDA[1:0],cSDA[1]};
 
      end
 
 
 
 
 
    // generate filtered SCL and SDA signals
        always @(posedge clk or negedge nReset)
        always @(posedge clk or negedge nReset)
          if (~nReset)
          if (~nReset)
            begin
            begin
                sSCL <= #1 1'b1;
                sSCL <= #1 1'b1;
                sSDA <= #1 1'b1;
                sSDA <= #1 1'b1;
Line 262... Line 294...
                dSCL <= #1 1'b1;
                dSCL <= #1 1'b1;
                dSDA <= #1 1'b1;
                dSDA <= #1 1'b1;
            end
            end
          else
          else
            begin
            begin
                sSCL <= #1 scl_i;
          sSCL <= #1 &fSCL[2:1] | &fSCL[1:0] | (fSCL[2] & fSCL[0]);
                sSDA <= #1 sda_i;
          sSDA <= #1 &fSDA[2:1] | &fSDA[1:0] | (fSDA[2] & fSDA[0]);
 
 
                dSCL <= #1 sSCL;
                dSCL <= #1 sSCL;
                dSDA <= #1 sSDA;
                dSDA <= #1 sSDA;
            end
            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
 
    reg sta_condition;
 
    reg sto_condition;
        always @(posedge clk or negedge nReset)
        always @(posedge clk or negedge nReset)
          if (~nReset)
          if (~nReset)
            begin
            begin
                sta_condition <= #1 1'b0;
                sta_condition <= #1 1'b0;
                sto_condition <= #1 1'b0;
                sto_condition <= #1 1'b0;
Line 288... Line 322...
            begin
            begin
                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 i2c bus busy signal
        // generate i2c bus busy signal
        always @(posedge clk or negedge nReset)
        always @(posedge clk or negedge nReset)
          if(!nReset)
      if      (!nReset) busy <= #1 1'b0;
            busy <= #1 1'b0;
      else if (rst    ) busy <= #1 1'b0;
          else if (rst)
      else              busy <= #1 (sta_condition | busy) & ~sto_condition;
            busy <= #1 1'b0;
 
          else
 
            busy <= #1 (sta_condition | busy) & ~sto_condition;
 
 
 
        // generate arbitration lost signal
        // generate arbitration lost signal
        // aribitration lost when:
        // aribitration lost when:
        // 1) master drives SDA high, but the i2c bus is low
        // 1) master drives SDA high, but the i2c bus is low
        // 2) stop detected while not requested
        // 2) stop detected while not requested
Line 321... Line 354...
            al <= #1 (sda_chk & ~sSDA & sda_oen) | (|c_state & sto_condition & ~cmd_stop);
            al <= #1 (sda_chk & ~sSDA & sda_oen) | (|c_state & sto_condition & ~cmd_stop);
 
 
 
 
        // generate dout signal (store SDA on rising edge of SCL)
        // generate dout signal (store SDA on rising edge of SCL)
        always @(posedge clk)
        always @(posedge clk)
          if(sSCL & ~dSCL)
      if (sSCL & ~dSCL) dout <= #1 sSDA;
            dout <= #1 sSDA;
 
 
 
        // generate statemachine
        // generate statemachine
 
 
        // nxt_state decoder
        // nxt_state decoder
        parameter [17:0] idle    = 18'b0_0000_0000_0000_0000;
        parameter [17:0] idle    = 18'b0_0000_0000_0000_0000;
Line 373... Line 406...
                  case (c_state) // synopsys full_case parallel_case
                  case (c_state) // synopsys full_case parallel_case
                    // idle state
                    // idle state
                    idle:
                    idle:
                    begin
                    begin
                        case (cmd) // synopsys full_case parallel_case
                        case (cmd) // synopsys full_case parallel_case
                          `I2C_CMD_START:
                             `I2C_CMD_START: c_state <= #1 start_a;
                             c_state <= #1 start_a;
                             `I2C_CMD_STOP:  c_state <= #1 stop_a;
 
                             `I2C_CMD_WRITE: c_state <= #1 wr_a;
                          `I2C_CMD_STOP:
                             `I2C_CMD_READ:  c_state <= #1 rd_a;
                             c_state <= #1 stop_a;
                             default:        c_state <= #1 idle;
 
 
                          `I2C_CMD_WRITE:
 
                             c_state <= #1 wr_a;
 
 
 
                          `I2C_CMD_READ:
 
                             c_state <= #1 rd_a;
 
 
 
                          default:
 
                            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
                        sda_chk <= #1 1'b0;    // don't check SDA output
Line 518... Line 542...
                    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
                        sda_chk <= #1 1'b0; // don't check SDA output yet
 
                                            // allow some time for SDA and SCL to settle
                    end
                    end
 
 
                    wr_c:
                    wr_c:
                    begin
                    begin
                        c_state <= #1 wr_d;
                        c_state <= #1 wr_d;

powered by: WebSVN 2.1.0

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