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

Subversion Repositories i2c

[/] [i2c/] [trunk/] [rtl/] [verilog/] [i2c_master_byte_ctrl.v] - Blame information for rev 29

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 rherveille
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  WISHBONE rev.B2 compliant I2C Master byte-controller       ////
4
////                                                             ////
5
////                                                             ////
6
////  Author: Richard Herveille                                  ////
7
////          richard@asics.ws                                   ////
8
////          www.asics.ws                                       ////
9
////                                                             ////
10
////  Downloaded from: http://www.opencores.org/projects/i2c/    ////
11
////                                                             ////
12
/////////////////////////////////////////////////////////////////////
13
////                                                             ////
14
//// Copyright (C) 2001 Richard Herveille                        ////
15
////                    richard@asics.ws                         ////
16
////                                                             ////
17
//// This source file may be used and distributed without        ////
18
//// restriction provided that this copyright statement is not   ////
19
//// removed from the file and that any derivative work contains ////
20
//// the original copyright notice and the associated disclaimer.////
21
////                                                             ////
22
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
23
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
24
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
25
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
26
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
27
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
28
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
29
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
30
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
31
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
32
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
33
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
34
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
35
////                                                             ////
36
/////////////////////////////////////////////////////////////////////
37
 
38
//  CVS Log
39 10 rherveille
//
40 29 rherveille
//  $Id: i2c_master_byte_ctrl.v,v 1.5 2002-12-26 15:02:32 rherveille Exp $
41 10 rherveille
//
42 29 rherveille
//  $Date: 2002-12-26 15:02:32 $
43
//  $Revision: 1.5 $
44 14 rherveille
//  $Author: rherveille $
45
//  $Locker:  $
46
//  $State: Exp $
47 10 rherveille
//
48 14 rherveille
// Change History:
49
//               $Log: not supported by cvs2svn $
50 29 rherveille
//               Revision 1.4  2002/11/30 22:24:40  rherveille
51
//               Cleaned up code
52
//
53 27 rherveille
//               Revision 1.3  2001/11/05 11:59:25  rherveille
54
//               Fixed wb_ack_o generation bug.
55
//               Fixed bug in the byte_controller statemachine.
56
//               Added headers.
57
//
58 10 rherveille
 
59 29 rherveille
// synopsys translate_off
60 10 rherveille
`include "timescale.v"
61 29 rherveille
// synopsys translate_on
62
 
63 10 rherveille
`include "i2c_master_defines.v"
64
 
65
module i2c_master_byte_ctrl (
66
        clk, rst, nReset, ena, clk_cnt, start, stop, read, write, ack_in, din,
67 29 rherveille
        cmd_ack, ack_out, dout, i2c_busy, i2c_al, scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen );
68 10 rherveille
 
69
        //
70
        // inputs & outputs
71
        //
72
        input clk;     // master clock
73
        input rst;     // synchronous active high reset
74
        input nReset;  // asynchronous active low reset
75
        input ena;     // core enable signal
76
 
77
        input [15:0] clk_cnt; // 4x SCL
78
 
79
        // control inputs
80
        input       start;
81
        input       stop;
82
        input       read;
83
        input       write;
84
        input       ack_in;
85
        input [7:0] din;
86
 
87
        // status outputs
88
        output       cmd_ack;
89
        reg cmd_ack;
90
        output       ack_out;
91
        reg ack_out;
92
        output       i2c_busy;
93 29 rherveille
        output       i2c_al;
94 10 rherveille
        output [7:0] dout;
95
 
96
        // I2C signals
97
        input  scl_i;
98
        output scl_o;
99
        output scl_oen;
100
        input  sda_i;
101
        output sda_o;
102
        output sda_oen;
103
 
104
 
105
        //
106
        // Variable declarations
107
        //
108
 
109
        // statemachine
110
        parameter [4:0] ST_IDLE  = 5'b0_0000;
111
        parameter [4:0] ST_START = 5'b0_0001;
112
        parameter [4:0] ST_READ  = 5'b0_0010;
113
        parameter [4:0] ST_WRITE = 5'b0_0100;
114
        parameter [4:0] ST_ACK   = 5'b0_1000;
115
        parameter [4:0] ST_STOP  = 5'b1_0000;
116
 
117
        // signals for bit_controller
118
        reg  [3:0] core_cmd;
119
        reg        core_txd;
120
        wire       core_ack, core_rxd;
121
 
122
        // signals for shift register
123
        reg [7:0] sr; //8bit shift register
124
        reg       shift, ld;
125
 
126
        // signals for state machine
127
        wire       go;
128 14 rherveille
        reg  [2:0] dcnt;
129 10 rherveille
        wire       cnt_done;
130
 
131
        //
132
        // Module body
133
        //
134
 
135
        // hookup bit_controller
136
        i2c_master_bit_ctrl bit_controller (
137 27 rherveille
                .clk     ( clk      ),
138
                .rst     ( rst      ),
139
                .nReset  ( nReset   ),
140
                .ena     ( ena      ),
141
                .clk_cnt ( clk_cnt  ),
142
                .cmd     ( core_cmd ),
143
                .cmd_ack ( core_ack ),
144
                .busy    ( i2c_busy ),
145 29 rherveille
                .al      ( i2c_al   ),
146 27 rherveille
                .din     ( core_txd ),
147
                .dout    ( core_rxd ),
148
                .scl_i   ( scl_i    ),
149
                .scl_o   ( scl_o    ),
150
                .scl_oen ( scl_oen  ),
151
                .sda_i   ( sda_i    ),
152
                .sda_o   ( sda_o    ),
153
                .sda_oen ( sda_oen  )
154 10 rherveille
        );
155
 
156
        // generate go-signal
157 27 rherveille
        assign go = (read | write | stop) & ~cmd_ack;
158 10 rherveille
 
159
        // assign dout output to shift-register
160
        assign dout = sr;
161
 
162
        // generate shift register
163 27 rherveille
        always @(posedge clk or negedge nReset)
164
          if (!nReset)
165
            sr <= #1 8'h0;
166
          else if (rst)
167
            sr <= #1 8'h0;
168
          else if (ld)
169
            sr <= #1 din;
170
          else if (shift)
171
            sr <= #1 {sr[6:0], core_rxd};
172 10 rherveille
 
173
        // generate counter
174 27 rherveille
        always @(posedge clk or negedge nReset)
175
          if (!nReset)
176
            dcnt <= #1 3'h0;
177
          else if (rst)
178
            dcnt <= #1 3'h0;
179
          else if (ld)
180
            dcnt <= #1 3'h7;
181
          else if (shift)
182
            dcnt <= #1 dcnt - 3'h1;
183 10 rherveille
 
184 29 rherveille
        assign cnt_done = ~(|dcnt);
185 10 rherveille
 
186
        //
187
        // state machine
188
        //
189
        reg [4:0] c_state; // synopsis enum_state
190
 
191 27 rherveille
        always @(posedge clk or negedge nReset)
192
          if (!nReset)
193
            begin
194
                core_cmd <= #1 `I2C_CMD_NOP;
195
                core_txd <= #1 1'b0;
196
                shift    <= #1 1'b0;
197
                ld       <= #1 1'b0;
198
                cmd_ack  <= #1 1'b0;
199
                c_state  <= #1 ST_IDLE;
200
                ack_out  <= #1 1'b0;
201
            end
202 29 rherveille
          else if (rst | i2c_al)
203 27 rherveille
           begin
204
               core_cmd <= #1 `I2C_CMD_NOP;
205
               core_txd <= #1 1'b0;
206
               shift    <= #1 1'b0;
207
               ld       <= #1 1'b0;
208
               cmd_ack  <= #1 1'b0;
209
               c_state  <= #1 ST_IDLE;
210
               ack_out  <= #1 1'b0;
211
           end
212 10 rherveille
        else
213 27 rherveille
          begin
214
              // initially reset all signals
215
              core_txd <= #1 sr[7];
216
              shift    <= #1 1'b0;
217
              ld       <= #1 1'b0;
218
              cmd_ack  <= #1 1'b0;
219 10 rherveille
 
220 27 rherveille
              case (c_state) // synopsis full_case parallel_case
221
                ST_IDLE:
222
                  if (go)
223
                    begin
224
                        if (start)
225
                          begin
226
                              c_state  <= #1 ST_START;
227
                              core_cmd <= #1 `I2C_CMD_START;
228
                          end
229
                        else if (read)
230
                          begin
231
                              c_state  <= #1 ST_READ;
232
                              core_cmd <= #1 `I2C_CMD_READ;
233
                          end
234
                        else if (write)
235
                          begin
236
                              c_state  <= #1 ST_WRITE;
237
                              core_cmd <= #1 `I2C_CMD_WRITE;
238
                          end
239
                        else // stop
240
                          begin
241
                              c_state  <= #1 ST_STOP;
242
                              core_cmd <= #1 `I2C_CMD_STOP;
243 10 rherveille
 
244 27 rherveille
                              // generate command acknowledge signal
245
                              cmd_ack  <= #1 1'b1;
246
                          end
247 10 rherveille
 
248 29 rherveille
                        ld <= #1 1'b1;
249 27 rherveille
                    end
250 10 rherveille
 
251 27 rherveille
                ST_START:
252
                  if (core_ack)
253
                    begin
254
                        if (read)
255
                          begin
256
                              c_state  <= #1 ST_READ;
257
                              core_cmd <= #1 `I2C_CMD_READ;
258
                          end
259
                        else
260
                          begin
261
                              c_state  <= #1 ST_WRITE;
262
                              core_cmd <= #1 `I2C_CMD_WRITE;
263
                          end
264 10 rherveille
 
265 29 rherveille
                        ld <= #1 1'b1;
266 27 rherveille
                    end
267 13 rherveille
 
268 27 rherveille
                ST_WRITE:
269
                  if (core_ack)
270
                    if (cnt_done)
271
                      begin
272
                          c_state  <= #1 ST_ACK;
273
                          core_cmd <= #1 `I2C_CMD_READ;
274
                      end
275
                    else
276
                      begin
277
                          c_state  <= #1 ST_WRITE;       // stay in same state
278
                          core_cmd <= #1 `I2C_CMD_WRITE; // write next bit
279
                          shift    <= #1 1'b1;
280
                      end
281 13 rherveille
 
282 27 rherveille
                ST_READ:
283
                  if (core_ack)
284
                    begin
285
                        if (cnt_done)
286
                          begin
287
                              c_state  <= #1 ST_ACK;
288
                              core_cmd <= #1 `I2C_CMD_WRITE;
289
                          end
290
                        else
291
                          begin
292
                              c_state  <= #1 ST_READ;       // stay in same state
293
                              core_cmd <= #1 `I2C_CMD_READ; // read next bit
294
                          end
295 13 rherveille
 
296 27 rherveille
                        shift    <= #1 1'b1;
297
                        core_txd <= #1 ack_in;
298
                    end
299 10 rherveille
 
300 27 rherveille
                ST_ACK:
301
                  if (core_ack)
302
                    begin
303
                       if (stop)
304
                         begin
305
                             c_state  <= #1 ST_STOP;
306
                             core_cmd <= #1 `I2C_CMD_STOP;
307
                         end
308
                       else
309
                         begin
310
                             c_state  <= #1 ST_IDLE;
311
                             core_cmd <= #1 `I2C_CMD_NOP;
312
                         end
313 10 rherveille
 
314 27 rherveille
                         // assign ack_out output to bit_controller_rxd (contains last received bit)
315
                         ack_out <= #1 core_rxd;
316 10 rherveille
 
317 27 rherveille
                         // generate command acknowledge signal
318
                         cmd_ack  <= #1 1'b1;
319 10 rherveille
 
320 27 rherveille
                         core_txd <= #1 1'b1;
321
                     end
322
                   else
323
                     core_txd <= #1 ack_in;
324 10 rherveille
 
325 27 rherveille
                ST_STOP:
326
                  if (core_ack)
327
                    begin
328
                        c_state  <= #1 ST_IDLE;
329
                        core_cmd <= #1 `I2C_CMD_NOP;
330
                    end
331 10 rherveille
 
332 27 rherveille
              endcase
333
          end
334 10 rherveille
endmodule

powered by: WebSVN 2.1.0

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