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

Subversion Repositories i2cslave

[/] [i2cslave/] [trunk/] [rtl/] [i2cSlave.v] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sfielding
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// i2cSlave.v                                                   ////
4
////                                                              ////
5
//// This file is part of the i2cSlave opencores effort.
6
//// <http://www.opencores.org/cores//>                           ////
7
////                                                              ////
8
//// Module Description:                                          ////
9
//// You will need to modify this file to implement your 
10
//// interface.
11
////                                                              ////
12
//// To Do:                                                       ////
13
//// 
14
////                                                              ////
15
//// Author(s):                                                   ////
16
//// - Steve Fielding, sfielding@base2designs.com                 ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2008 Steve Fielding and OPENCORES.ORG          ////
21
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE. See the GNU Lesser General Public License for more  ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from <http://www.opencores.org/lgpl.shtml>                   ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44
//
45
`include "i2cSlave_define.v"
46
 
47
 
48
module i2cSlave (
49
  clk,
50
  rst,
51
  sda,
52
  scl,
53
  myReg0,
54
  myReg1,
55
  myReg2,
56
  myReg3,
57
  myReg4,
58
  myReg5,
59
  myReg6,
60
  myReg7
61
);
62
 
63
input clk;
64
input rst;
65
inout sda;
66
input scl;
67
output [7:0] myReg0;
68
output [7:0] myReg1;
69
output [7:0] myReg2;
70
output [7:0] myReg3;
71
input [7:0] myReg4;
72
input [7:0] myReg5;
73
input [7:0] myReg6;
74
input [7:0] myReg7;
75
 
76
 
77
// local wires and regs
78
reg sdaDeb;
79
reg sclDeb;
80
reg [`DEB_I2C_LEN-1:0] sdaPipe;
81
reg [`DEB_I2C_LEN-1:0] sclPipe;
82
 
83
reg [`SCL_DEL_LEN-1:0] sclDelayed;
84
reg [`SDA_DEL_LEN-1:0] sdaDelayed;
85
reg [1:0] startStopDetState;
86
wire clearStartStopDet;
87
wire sdaOut;
88
wire sdaIn;
89
wire [7:0] regAddr;
90
wire [7:0] dataToRegIF;
91
wire writeEn;
92
wire [7:0] dataFromRegIF;
93
reg [1:0] rstPipe;
94
wire rstSyncToClk;
95
reg startEdgeDet;
96
 
97
assign sda = (sdaOut == 1'b0) ? 1'b0 : 1'bz;
98
assign sdaIn = sda;
99
 
100
// sync rst rsing edge to clk
101
always @(posedge clk) begin
102
  if (rst == 1'b1)
103
    rstPipe <= 2'b11;
104
  else
105
    rstPipe <= {rstPipe[0], 1'b0};
106
end
107
 
108
assign rstSyncToClk = rstPipe[1];
109
 
110
// debounce sda and scl
111
always @(posedge clk) begin
112
  if (rstSyncToClk == 1'b1) begin
113
    sdaPipe <= {`DEB_I2C_LEN{1'b1}};
114
    sdaDeb <= 1'b1;
115
    sclPipe <= {`DEB_I2C_LEN{1'b1}};
116
    sclDeb <= 1'b1;
117
  end
118
  else begin
119
    sdaPipe <= {sdaPipe[`DEB_I2C_LEN-2:0], sdaIn};
120
    sclPipe <= {sclPipe[`DEB_I2C_LEN-2:0], scl};
121
    if (&sclPipe[`DEB_I2C_LEN-1:1] == 1'b1)
122
      sclDeb <= 1'b1;
123
    else if (|sclPipe[`DEB_I2C_LEN-1:1] == 1'b0)
124
      sclDeb <= 1'b0;
125
    if (&sdaPipe[`DEB_I2C_LEN-1:1] == 1'b1)
126
      sdaDeb <= 1'b1;
127
    else if (|sdaPipe[`DEB_I2C_LEN-1:1] == 1'b0)
128
      sdaDeb <= 1'b0;
129
  end
130
end
131
 
132
 
133
// delay scl and sda
134
// sclDelayed is used as a delayed sampling clock
135
// sdaDelayed is only used for start stop detection
136
// Because sda hold time from scl falling is 0nS
137
// sda must be delayed with respect to scl to avoid incorrect
138
// detection of start/stop at scl falling edge. 
139
always @(posedge clk) begin
140
  if (rstSyncToClk == 1'b1) begin
141
    sclDelayed <= {`SCL_DEL_LEN{1'b1}};
142
    sdaDelayed <= {`SDA_DEL_LEN{1'b1}};
143
  end
144
  else begin
145
    sclDelayed <= {sclDelayed[`SCL_DEL_LEN-2:0], sclDeb};
146
    sdaDelayed <= {sdaDelayed[`SDA_DEL_LEN-2:0], sdaDeb};
147
  end
148
end
149
 
150
// start stop detection
151
always @(posedge clk) begin
152
  if (rstSyncToClk == 1'b1) begin
153
    startStopDetState <= `NULL_DET;
154
    startEdgeDet <= 1'b0;
155
  end
156
  else begin
157
    if (sclDeb == 1'b1 && sdaDelayed[`SDA_DEL_LEN-2] == 1'b0 && sdaDelayed[`SDA_DEL_LEN-1] == 1'b1)
158
      startEdgeDet <= 1'b1;
159
    else
160
      startEdgeDet <= 1'b0;
161
    if (clearStartStopDet == 1'b1)
162
      startStopDetState <= `NULL_DET;
163
    else if (sclDeb == 1'b1) begin
164
      if (sdaDelayed[`SDA_DEL_LEN-2] == 1'b1 && sdaDelayed[`SDA_DEL_LEN-1] == 1'b0)
165
        startStopDetState <= `STOP_DET;
166
      else if (sdaDelayed[`SDA_DEL_LEN-2] == 1'b0 && sdaDelayed[`SDA_DEL_LEN-1] == 1'b1)
167
        startStopDetState <= `START_DET;
168
    end
169
  end
170
end
171
 
172
 
173
registerInterface u_registerInterface(
174
  .clk(clk),
175
  .addr(regAddr),
176
  .dataIn(dataToRegIF),
177
  .writeEn(writeEn),
178
  .dataOut(dataFromRegIF),
179
  .myReg0(myReg0),
180
  .myReg1(myReg1),
181
  .myReg2(myReg2),
182
  .myReg3(myReg3),
183
  .myReg4(myReg4),
184
  .myReg5(myReg5),
185
  .myReg6(myReg6),
186
  .myReg7(myReg7)
187
);
188
 
189
serialInterface u_serialInterface (
190
  .clk(clk),
191
  .rst(rstSyncToClk | startEdgeDet),
192
  .dataIn(dataFromRegIF),
193
  .dataOut(dataToRegIF),
194
  .writeEn(writeEn),
195
  .regAddr(regAddr),
196
  .scl(sclDelayed[`SCL_DEL_LEN-1]),
197
  .sdaIn(sdaDeb),
198
  .sdaOut(sdaOut),
199
  .startStopDetState(startStopDetState),
200
  .clearStartStopDet(clearStartStopDet)
201
);
202
 
203
 
204
endmodule
205
 
206
 
207
 

powered by: WebSVN 2.1.0

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