//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
//// ////
|
//// ////
|
//// OMS8051 I2C Master byte-controller Module ////
|
//// OMS8051 I2C Master byte-controller Module ////
|
//// WISHBONE rev.B2 compliant I2C Master byte-controller ////
|
//// WISHBONE rev.B2 compliant I2C Master byte-controller ////
|
//// ////
|
//// ////
|
//// This file is part of the OMS 8051 cores project ////
|
//// This file is part of the OMS 8051 cores project ////
|
//// http://www.opencores.org/cores/oms8051mini/ ////
|
//// http://www.opencores.org/cores/oms8051mini/ ////
|
//// ////
|
//// ////
|
//// Description ////
|
//// Description ////
|
//// OMS 8051 definitions. ////
|
//// OMS 8051 definitions. ////
|
//// ////
|
//// ////
|
//// To Do: ////
|
//// To Do: ////
|
//// nothing ////
|
//// nothing ////
|
//// ////
|
//// ////
|
//// Author(s): ////
|
//// Author(s): ////
|
//// -Richard Herveille , richard@asics.ws, www.asics.ws ////
|
//// -Richard Herveille , richard@asics.ws, www.asics.ws ////
|
//// -Dinesh Annayya, dinesha@opencores.org ////
|
//// -Dinesh Annayya, dinesha@opencores.org ////
|
//// ////
|
//// ////
|
//// Revision : Jan 6, 2017 ////
|
//// Revision : Jan 6, 2017 ////
|
//// ////
|
//// ////
|
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
// v0.0 - Dinesh A, 6th Jan 2017
|
//// v0.0 - Dinesh A, 6th Jan 2017
|
// 1. Initail version picked from
|
//// 1. Initail version picked from
|
// http://www.opencores.org/projects/i2c/
|
//// http://www.opencores.org/projects/i2c/
|
// 2. renaming of reset signal to aresetn and sresetn
|
//// 2. renaming of reset signal to aresetn and sresetn
|
//
|
//// v0.1 - Dinesh.A, 19th Jan 2017
|
|
//// 1. Lint Error fixes
|
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
//// ////
|
//// ////
|
//// Copyright (C) 2000 Authors and OPENCORES.ORG ////
|
//// Copyright (C) 2000 Authors and OPENCORES.ORG ////
|
//// ////
|
//// ////
|
//// This source file may be used and distributed without ////
|
//// This source file may be used and distributed without ////
|
//// restriction provided that this copyright statement is not ////
|
//// restriction provided that this copyright statement is not ////
|
//// removed from the file and that any derivative work contains ////
|
//// removed from the file and that any derivative work contains ////
|
//// the original copyright notice and the associated disclaimer. ////
|
//// the original copyright notice and the associated disclaimer. ////
|
//// ////
|
//// ////
|
//// This source file is free software; you can redistribute it ////
|
//// This source file is free software; you can redistribute it ////
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
//// Public License as published by the Free Software Foundation; ////
|
//// Public License as published by the Free Software Foundation; ////
|
//// either version 2.1 of the License, or (at your option) any ////
|
//// either version 2.1 of the License, or (at your option) any ////
|
//// later version. ////
|
//// later version. ////
|
//// ////
|
//// ////
|
//// This source is distributed in the hope that it will be ////
|
//// This source is distributed in the hope that it will be ////
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
//// details. ////
|
//// details. ////
|
//// ////
|
//// ////
|
//// You should have received a copy of the GNU Lesser General ////
|
//// You should have received a copy of the GNU Lesser General ////
|
//// Public License along with this source; if not, download it ////
|
//// Public License along with this source; if not, download it ////
|
//// from http://www.opencores.org/lgpl.shtml ////
|
//// from http://www.opencores.org/lgpl.shtml ////
|
//// ////
|
//// ////
|
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
|
|
`include "i2cm_defines.v"
|
`include "i2cm_defines.v"
|
|
|
module i2cm_byte_ctrl (
|
module i2cm_byte_ctrl (
|
//
|
//
|
// inputs & outputs
|
// inputs & outputs
|
//
|
//
|
input clk, // master clock
|
input clk, // master clock
|
input sresetn, // synchronous active high reset
|
input sresetn, // synchronous active high reset
|
input aresetn, // asynchronous active low reset
|
input aresetn, // asynchronous active low reset
|
input ena, // core enable signal
|
input ena, // core enable signal
|
|
|
input [15:0] clk_cnt, // 4x SCL
|
input [15:0] clk_cnt, // 4x SCL
|
|
|
// control inputs
|
// control inputs
|
input start,
|
input start,
|
input stop,
|
input stop,
|
input read,
|
input read,
|
input write,
|
input write,
|
input ack_in,
|
input ack_in,
|
input [7:0] din,
|
input [7:0] din,
|
|
|
// status outputs
|
// status outputs
|
output reg cmd_ack,
|
output reg cmd_ack,
|
output reg ack_out,
|
output reg ack_out,
|
output i2c_busy,
|
output i2c_busy,
|
output i2c_al,
|
output i2c_al,
|
output [7:0] dout,
|
output [7:0] dout,
|
|
|
// I2C signals
|
// I2C signals
|
input scl_i,
|
input scl_i,
|
output scl_o,
|
output scl_o,
|
output scl_oen,
|
output scl_oen,
|
input sda_i,
|
input sda_i,
|
output sda_o,
|
output sda_o,
|
output sda_oen
|
output sda_oen
|
|
|
);
|
);
|
|
|
|
|
|
|
//
|
//
|
// Variable declarations
|
// Variable declarations
|
//
|
//
|
|
|
// statemachine
|
// statemachine
|
parameter [4:0] ST_IDLE = 5'b0_0000;
|
parameter [4:0] ST_IDLE = 5'b0_0000;
|
parameter [4:0] ST_START = 5'b0_0001;
|
parameter [4:0] ST_START = 5'b0_0001;
|
parameter [4:0] ST_READ = 5'b0_0010;
|
parameter [4:0] ST_READ = 5'b0_0010;
|
parameter [4:0] ST_WRITE = 5'b0_0100;
|
parameter [4:0] ST_WRITE = 5'b0_0100;
|
parameter [4:0] ST_ACK = 5'b0_1000;
|
parameter [4:0] ST_ACK = 5'b0_1000;
|
parameter [4:0] ST_STOP = 5'b1_0000;
|
parameter [4:0] ST_STOP = 5'b1_0000;
|
|
|
// signals for bit_controller
|
// signals for bit_controller
|
reg [3:0] core_cmd;
|
reg [3:0] core_cmd;
|
reg core_txd;
|
reg core_txd;
|
wire core_ack, core_rxd;
|
wire core_ack, core_rxd;
|
|
|
// signals for shift register
|
// signals for shift register
|
reg [7:0] sr; //8bit shift register
|
reg [7:0] sr; //8bit shift register
|
reg shift, ld;
|
reg shift, ld;
|
|
|
// signals for state machine
|
// signals for state machine
|
wire go;
|
wire go;
|
reg [2:0] dcnt;
|
reg [2:0] dcnt;
|
wire cnt_done;
|
wire cnt_done;
|
|
|
//
|
//
|
// Module body
|
// Module body
|
//
|
//
|
|
|
// hookup bit_controller
|
// hookup bit_controller
|
i2cm_bit_ctrl u_bit_ctrl (
|
i2cm_bit_ctrl u_bit_ctrl (
|
.clk ( clk ),
|
.clk ( clk ),
|
.sresetn ( sresetn ),
|
.sresetn ( sresetn ),
|
.aresetn ( aresetn ),
|
.aresetn ( aresetn ),
|
.ena ( ena ),
|
.ena ( ena ),
|
.clk_cnt ( clk_cnt ),
|
.clk_cnt ( clk_cnt ),
|
.cmd ( core_cmd ),
|
.cmd ( core_cmd ),
|
.cmd_ack ( core_ack ),
|
.cmd_ack ( core_ack ),
|
.busy ( i2c_busy ),
|
.busy ( i2c_busy ),
|
.al ( i2c_al ),
|
.al ( i2c_al ),
|
.din ( core_txd ),
|
.din ( core_txd ),
|
.dout ( core_rxd ),
|
.dout ( core_rxd ),
|
.scl_i ( scl_i ),
|
.scl_i ( scl_i ),
|
.scl_o ( scl_o ),
|
.scl_o ( scl_o ),
|
.scl_oen ( scl_oen ),
|
.scl_oen ( scl_oen ),
|
.sda_i ( sda_i ),
|
.sda_i ( sda_i ),
|
.sda_o ( sda_o ),
|
.sda_o ( sda_o ),
|
.sda_oen ( sda_oen )
|
.sda_oen ( sda_oen )
|
);
|
);
|
|
|
// generate go-signal
|
// generate go-signal
|
assign go = (read | write | stop) & ~cmd_ack;
|
assign go = (read | write | stop) & ~cmd_ack;
|
|
|
// assign dout output to shift-register
|
// assign dout output to shift-register
|
assign dout = sr;
|
assign dout = sr;
|
|
|
// generate shift register
|
// generate shift register
|
always @(posedge clk or negedge aresetn)
|
always @(posedge clk or negedge aresetn)
|
if (!aresetn)
|
if (!aresetn)
|
sr <= #1 8'h0;
|
sr <= 8'h0;
|
else if (!sresetn)
|
else if (!sresetn)
|
sr <= #1 8'h0;
|
sr <= 8'h0;
|
else if (ld)
|
else if (ld)
|
sr <= #1 din;
|
sr <= din;
|
else if (shift)
|
else if (shift)
|
sr <= #1 {sr[6:0], core_rxd};
|
sr <= {sr[6:0], core_rxd};
|
|
|
// generate counter
|
// generate counter
|
always @(posedge clk or negedge aresetn)
|
always @(posedge clk or negedge aresetn)
|
if (!aresetn)
|
if (!aresetn)
|
dcnt <= #1 3'h0;
|
dcnt <= 3'h0;
|
else if (!sresetn)
|
else if (!sresetn)
|
dcnt <= #1 3'h0;
|
dcnt <= 3'h0;
|
else if (ld)
|
else if (ld)
|
dcnt <= #1 3'h7;
|
dcnt <= 3'h7;
|
else if (shift)
|
else if (shift)
|
dcnt <= #1 dcnt - 3'h1;
|
dcnt <= dcnt - 3'h1;
|
|
|
assign cnt_done = ~(|dcnt);
|
assign cnt_done = ~(|dcnt);
|
|
|
//
|
//
|
// state machine
|
// state machine
|
//
|
//
|
reg [4:0] c_state; // synopsys enum_state
|
reg [4:0] c_state; // synopsys enum_state
|
|
|
always @(posedge clk or negedge aresetn)
|
always @(posedge clk or negedge aresetn)
|
if (!aresetn)
|
if (!aresetn)
|
begin
|
begin
|
core_cmd <= #1 `I2C_CMD_NOP;
|
core_cmd <= `I2C_CMD_NOP;
|
core_txd <= #1 1'b0;
|
core_txd <= 1'b0;
|
shift <= #1 1'b0;
|
shift <= 1'b0;
|
ld <= #1 1'b0;
|
ld <= 1'b0;
|
cmd_ack <= #1 1'b0;
|
cmd_ack <= 1'b0;
|
c_state <= #1 ST_IDLE;
|
c_state <= ST_IDLE;
|
ack_out <= #1 1'b0;
|
ack_out <= 1'b0;
|
end
|
end
|
else if (!sresetn | i2c_al)
|
else if (!sresetn | i2c_al)
|
begin
|
begin
|
core_cmd <= #1 `I2C_CMD_NOP;
|
core_cmd <= `I2C_CMD_NOP;
|
core_txd <= #1 1'b0;
|
core_txd <= 1'b0;
|
shift <= #1 1'b0;
|
shift <= 1'b0;
|
ld <= #1 1'b0;
|
ld <= 1'b0;
|
cmd_ack <= #1 1'b0;
|
cmd_ack <= 1'b0;
|
c_state <= #1 ST_IDLE;
|
c_state <= ST_IDLE;
|
ack_out <= #1 1'b0;
|
ack_out <= 1'b0;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
// initially reset all signals
|
// initially reset all signals
|
core_txd <= #1 sr[7];
|
core_txd <= sr[7];
|
shift <= #1 1'b0;
|
shift <= 1'b0;
|
ld <= #1 1'b0;
|
ld <= 1'b0;
|
cmd_ack <= #1 1'b0;
|
cmd_ack <= 1'b0;
|
|
|
case (c_state) // synopsys full_case parallel_case
|
case (c_state) // synopsys full_case parallel_case
|
ST_IDLE:
|
ST_IDLE:
|
if (go)
|
if (go)
|
begin
|
begin
|
if (start)
|
if (start)
|
begin
|
begin
|
c_state <= #1 ST_START;
|
c_state <= ST_START;
|
core_cmd <= #1 `I2C_CMD_START;
|
core_cmd <= `I2C_CMD_START;
|
end
|
end
|
else if (read)
|
else if (read)
|
begin
|
begin
|
c_state <= #1 ST_READ;
|
c_state <= ST_READ;
|
core_cmd <= #1 `I2C_CMD_READ;
|
core_cmd <= `I2C_CMD_READ;
|
end
|
end
|
else if (write)
|
else if (write)
|
begin
|
begin
|
c_state <= #1 ST_WRITE;
|
c_state <= ST_WRITE;
|
core_cmd <= #1 `I2C_CMD_WRITE;
|
core_cmd <= `I2C_CMD_WRITE;
|
end
|
end
|
else // stop
|
else // stop
|
begin
|
begin
|
c_state <= #1 ST_STOP;
|
c_state <= ST_STOP;
|
core_cmd <= #1 `I2C_CMD_STOP;
|
core_cmd <= `I2C_CMD_STOP;
|
end
|
end
|
|
|
ld <= #1 1'b1;
|
ld <= 1'b1;
|
end
|
end
|
|
|
ST_START:
|
ST_START:
|
if (core_ack)
|
if (core_ack)
|
begin
|
begin
|
if (read)
|
if (read)
|
begin
|
begin
|
c_state <= #1 ST_READ;
|
c_state <= ST_READ;
|
core_cmd <= #1 `I2C_CMD_READ;
|
core_cmd <= `I2C_CMD_READ;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
c_state <= #1 ST_WRITE;
|
c_state <= ST_WRITE;
|
core_cmd <= #1 `I2C_CMD_WRITE;
|
core_cmd <= `I2C_CMD_WRITE;
|
end
|
end
|
|
|
ld <= #1 1'b1;
|
ld <= 1'b1;
|
end
|
end
|
|
|
ST_WRITE:
|
ST_WRITE:
|
if (core_ack)
|
if (core_ack)
|
if (cnt_done)
|
if (cnt_done)
|
begin
|
begin
|
c_state <= #1 ST_ACK;
|
c_state <= ST_ACK;
|
core_cmd <= #1 `I2C_CMD_READ;
|
core_cmd <= `I2C_CMD_READ;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
c_state <= #1 ST_WRITE; // stay in same state
|
c_state <= ST_WRITE; // stay in same state
|
core_cmd <= #1 `I2C_CMD_WRITE; // write next bit
|
core_cmd <= `I2C_CMD_WRITE; // write next bit
|
shift <= #1 1'b1;
|
shift <= 1'b1;
|
end
|
end
|
|
|
ST_READ:
|
ST_READ:
|
if (core_ack)
|
if (core_ack)
|
begin
|
begin
|
if (cnt_done)
|
if (cnt_done)
|
begin
|
begin
|
c_state <= #1 ST_ACK;
|
c_state <= ST_ACK;
|
core_cmd <= #1 `I2C_CMD_WRITE;
|
core_cmd <= `I2C_CMD_WRITE;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
c_state <= #1 ST_READ; // stay in same state
|
c_state <= ST_READ; // stay in same state
|
core_cmd <= #1 `I2C_CMD_READ; // read next bit
|
core_cmd <= `I2C_CMD_READ; // read next bit
|
end
|
end
|
|
|
shift <= #1 1'b1;
|
shift <= 1'b1;
|
core_txd <= #1 ack_in;
|
core_txd <= ack_in;
|
end
|
end
|
|
|
ST_ACK:
|
ST_ACK:
|
if (core_ack)
|
if (core_ack)
|
begin
|
begin
|
if (stop)
|
if (stop)
|
begin
|
begin
|
c_state <= #1 ST_STOP;
|
c_state <= ST_STOP;
|
core_cmd <= #1 `I2C_CMD_STOP;
|
core_cmd <= `I2C_CMD_STOP;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
c_state <= #1 ST_IDLE;
|
c_state <= ST_IDLE;
|
core_cmd <= #1 `I2C_CMD_NOP;
|
core_cmd <= `I2C_CMD_NOP;
|
|
|
// generate command acknowledge signal
|
// generate command acknowledge signal
|
cmd_ack <= #1 1'b1;
|
cmd_ack <= 1'b1;
|
end
|
end
|
|
|
// assign ack_out output to bit_controller_rxd (contains last received bit)
|
// assign ack_out output to bit_controller_rxd (contains last received bit)
|
ack_out <= #1 core_rxd;
|
ack_out <= core_rxd;
|
|
|
core_txd <= #1 1'b1;
|
core_txd <= 1'b1;
|
end
|
end
|
else
|
else
|
core_txd <= #1 ack_in;
|
core_txd <= ack_in;
|
|
|
ST_STOP:
|
ST_STOP:
|
if (core_ack)
|
if (core_ack)
|
begin
|
begin
|
c_state <= #1 ST_IDLE;
|
c_state <= ST_IDLE;
|
core_cmd <= #1 `I2C_CMD_NOP;
|
core_cmd <= `I2C_CMD_NOP;
|
|
|
// generate command acknowledge signal
|
// generate command acknowledge signal
|
cmd_ack <= #1 1'b1;
|
cmd_ack <= 1'b1;
|
end
|
end
|
|
default: c_state <= ST_IDLE;
|
|
|
endcase
|
endcase
|
end
|
end
|
endmodule
|
endmodule
|
|
|