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

Subversion Repositories i2cslave

[/] [i2cslave/] [trunk/] [model/] [i2c_master_bit_ctrl.v] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sfielding
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  WISHBONE rev.B2 compliant I2C Master bit-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
//
40
//  $Id: i2c_master_bit_ctrl.v,v 1.1 2008-11-08 13:15:10 sfielding Exp $
41
//
42
//  $Date: 2008-11-08 13:15:10 $
43
//  $Revision: 1.1 $
44
//  $Author: sfielding $
45
//  $Locker:  $
46
//  $State: Exp $
47
//
48
// Change History:
49
//               $Log: not supported by cvs2svn $
50
//               Revision 1.12  2006/09/04 09:08:13  rherveille
51
//               fixed short scl high pulse after clock stretch
52
//               fixed slave model not returning correct '(n)ack' signal
53
//
54
//               Revision 1.11  2004/05/07 11:02:26  rherveille
55
//               Fixed a bug where the core would signal an arbitration lost (AL bit set), when another master controls the bus and the other master generates a STOP bit.
56
//
57
//               Revision 1.10  2003/08/09 07:01:33  rherveille
58
//               Fixed a bug in the Arbitration Lost generation caused by delay on the (external) sda line.
59
//               Fixed a potential bug in the byte controller's host-acknowledge generation.
60
//
61
//               Revision 1.9  2003/03/10 14:26:37  rherveille
62
//               Fixed cmd_ack generation item (no bug).
63
//
64
//               Revision 1.8  2003/02/05 00:06:10  rherveille
65
//               Fixed a bug where the core would trigger an erroneous 'arbitration lost' interrupt after being reset, when the reset pulse width < 3 clk cycles.
66
//
67
//               Revision 1.7  2002/12/26 16:05:12  rherveille
68
//               Small code simplifications
69
//
70
//               Revision 1.6  2002/12/26 15:02:32  rherveille
71
//               Core is now a Multimaster I2C controller
72
//
73
//               Revision 1.5  2002/11/30 22:24:40  rherveille
74
//               Cleaned up code
75
//
76
//               Revision 1.4  2002/10/30 18:10:07  rherveille
77
//               Fixed some reported minor start/stop generation timing issuess.
78
//
79
//               Revision 1.3  2002/06/15 07:37:03  rherveille
80
//               Fixed a small timing bug in the bit controller.\nAdded verilog simulation environment.
81
//
82
//               Revision 1.2  2001/11/05 11:59:25  rherveille
83
//               Fixed wb_ack_o generation bug.
84
//               Fixed bug in the byte_controller statemachine.
85
//               Added headers.
86
//
87
 
88
//
89
/////////////////////////////////////
90
// Bit controller section
91
/////////////////////////////////////
92
//
93
// Translate simple commands into SCL/SDA transitions
94
// Each command has 5 states, A/B/C/D/idle
95
//
96
// start:       SCL     ~~~~~~~~~~\____
97
//      SDA     ~~~~~~~~\______
98
//               x | A | B | C | D | i
99
//
100
// repstart     SCL     ____/~~~~\___
101
//      SDA     __/~~~\______
102
//               x | A | B | C | D | i
103
//
104
// stop SCL     ____/~~~~~~~~
105
//      SDA     ==\____/~~~~~
106
//               x | A | B | C | D | i
107
//
108
//- write       SCL     ____/~~~~\____
109
//      SDA     ==X=========X=
110
//               x | A | B | C | D | i
111
//
112
//- read        SCL     ____/~~~~\____
113
//      SDA     XXXX=====XXXX
114
//               x | A | B | C | D | i
115
//
116
 
117
// Timing:     Normal mode      Fast mode
118
///////////////////////////////////////////////////////////////////////
119
// Fscl        100KHz           400KHz
120
// Th_scl      4.0us            0.6us   High period of SCL
121
// Tl_scl      4.7us            1.3us   Low period of SCL
122
// Tsu:sta     4.7us            0.6us   setup time for a repeated start condition
123
// Tsu:sto     4.0us            0.6us   setup time for a stop conditon
124
// Tbuf        4.7us            1.3us   Bus free time between a stop and start condition
125
//
126
 
127
// synopsys translate_off
128
`include "timescale.v"
129
// synopsys translate_on
130
 
131
`include "i2c_master_defines.v"
132
 
133
module i2c_master_bit_ctrl(
134
        clk, rst, nReset,
135
        clk_cnt, ena, cmd, cmd_ack, busy, al, din, dout,
136
        scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen
137
        );
138
 
139
        //
140
        // inputs & outputs
141
        //
142
        input clk;
143
        input rst;
144
        input nReset;
145
        input ena;            // core enable signal
146
 
147
        input [15:0] clk_cnt; // clock prescale value
148
 
149
        input  [3:0] cmd;
150
        output       cmd_ack; // command complete acknowledge
151
        reg cmd_ack;
152
        output       busy;    // i2c bus busy
153
        reg busy;
154
        output       al;      // i2c bus arbitration lost
155
        reg al;
156
 
157
        input  din;
158
        output dout;
159
        reg dout;
160
 
161
        // I2C lines
162
        input  scl_i;         // i2c clock line input
163
        output scl_o;         // i2c clock line output
164
        output scl_oen;       // i2c clock line output enable (active low)
165
        reg scl_oen;
166
        input  sda_i;         // i2c data line input
167
        output sda_o;         // i2c data line output
168
        output sda_oen;       // i2c data line output enable (active low)
169
        reg sda_oen;
170
 
171
 
172
        //
173
        // variable declarations
174
        //
175
 
176
        reg sSCL, sSDA;             // synchronized SCL and SDA inputs
177
        reg dscl_oen;               // delayed scl_oen
178
        reg sda_chk;                // check SDA output (Multi-master arbitration)
179
        reg clk_en;                 // clock generation signals
180
        wire slave_wait;
181
//      reg [15:0] cnt = clk_cnt;   // clock divider counter (simulation)
182
        reg [15:0] cnt;             // clock divider counter (synthesis)
183
 
184
        // state machine variable
185
        reg [16:0] c_state; // synopsys enum_state
186
 
187
        //
188
        // module body
189
        //
190
 
191
        // whenever the slave is not ready it can delay the cycle by pulling SCL low
192
        // delay scl_oen
193
        always @(posedge clk)
194
          dscl_oen <= #1 scl_oen;
195
 
196
        assign slave_wait = dscl_oen && !sSCL;
197
 
198
 
199
        // generate clk enable signal
200
        always @(posedge clk or negedge nReset)
201
          if(~nReset)
202
            begin
203
                cnt    <= #1 16'h0;
204
                clk_en <= #1 1'b1;
205
            end
206
          else if (rst)
207
            begin
208
                cnt    <= #1 16'h0;
209
                clk_en <= #1 1'b1;
210
            end
211
          else if ( ~|cnt || !ena)
212
            begin
213
                cnt    <= #1 clk_cnt;
214
                clk_en <= #1 1'b1;
215
            end
216
          else if (slave_wait)
217
            begin
218
                cnt    <= #1 cnt;
219
                clk_en <= #1 1'b0;
220
            end
221
          else
222
            begin
223
                cnt    <= #1 cnt - 16'h1;
224
                clk_en <= #1 1'b0;
225
            end
226
 
227
 
228
        // generate bus status controller
229
        reg dSCL, dSDA;
230
        reg sta_condition;
231
        reg sto_condition;
232
 
233
        // synchronize SCL and SDA inputs
234
        // reduce metastability risc
235
        always @(posedge clk or negedge nReset)
236
          if (~nReset)
237
            begin
238
                sSCL <= #1 1'b1;
239
                sSDA <= #1 1'b1;
240
 
241
                dSCL <= #1 1'b1;
242
                dSDA <= #1 1'b1;
243
            end
244
          else if (rst)
245
            begin
246
                sSCL <= #1 1'b1;
247
                sSDA <= #1 1'b1;
248
 
249
                dSCL <= #1 1'b1;
250
                dSDA <= #1 1'b1;
251
            end
252
          else
253
            begin
254
                sSCL <= #1 scl_i;
255
                sSDA <= #1 sda_i;
256
 
257
                dSCL <= #1 sSCL;
258
                dSDA <= #1 sSDA;
259
            end
260
 
261
        // detect start condition => detect falling edge on SDA while SCL is high
262
        // detect stop condition => detect rising edge on SDA while SCL is high
263
        always @(posedge clk or negedge nReset)
264
          if (~nReset)
265
            begin
266
                sta_condition <= #1 1'b0;
267
                sto_condition <= #1 1'b0;
268
            end
269
          else if (rst)
270
            begin
271
                sta_condition <= #1 1'b0;
272
                sto_condition <= #1 1'b0;
273
            end
274
          else
275
            begin
276
                sta_condition <= #1 ~sSDA &  dSDA & sSCL;
277
                sto_condition <= #1  sSDA & ~dSDA & sSCL;
278
            end
279
 
280
        // generate i2c bus busy signal
281
        always @(posedge clk or negedge nReset)
282
          if(!nReset)
283
            busy <= #1 1'b0;
284
          else if (rst)
285
            busy <= #1 1'b0;
286
          else
287
            busy <= #1 (sta_condition | busy) & ~sto_condition;
288
 
289
        // generate arbitration lost signal
290
        // aribitration lost when:
291
        // 1) master drives SDA high, but the i2c bus is low
292
        // 2) stop detected while not requested
293
        reg cmd_stop;
294
        always @(posedge clk or negedge nReset)
295
          if (~nReset)
296
            cmd_stop <= #1 1'b0;
297
          else if (rst)
298
            cmd_stop <= #1 1'b0;
299
          else if (clk_en)
300
            cmd_stop <= #1 cmd == `I2C_CMD_STOP;
301
 
302
        always @(posedge clk or negedge nReset)
303
          if (~nReset)
304
            al <= #1 1'b0;
305
          else if (rst)
306
            al <= #1 1'b0;
307
          else
308
            al <= #1 (sda_chk & ~sSDA & sda_oen) | (|c_state & sto_condition & ~cmd_stop);
309
 
310
 
311
        // generate dout signal (store SDA on rising edge of SCL)
312
        always @(posedge clk)
313
          if(sSCL & ~dSCL)
314
            dout <= #1 sSDA;
315
 
316
        // generate statemachine
317
 
318
        // nxt_state decoder
319
        parameter [16:0] idle    = 17'b0_0000_0000_0000_0000;
320
        parameter [16:0] start_a = 17'b0_0000_0000_0000_0001;
321
        parameter [16:0] start_b = 17'b0_0000_0000_0000_0010;
322
        parameter [16:0] start_c = 17'b0_0000_0000_0000_0100;
323
        parameter [16:0] start_d = 17'b0_0000_0000_0000_1000;
324
        parameter [16:0] start_e = 17'b0_0000_0000_0001_0000;
325
        parameter [16:0] stop_a  = 17'b0_0000_0000_0010_0000;
326
        parameter [16:0] stop_b  = 17'b0_0000_0000_0100_0000;
327
        parameter [16:0] stop_c  = 17'b0_0000_0000_1000_0000;
328
        parameter [16:0] stop_d  = 17'b0_0000_0001_0000_0000;
329
        parameter [16:0] rd_a    = 17'b0_0000_0010_0000_0000;
330
        parameter [16:0] rd_b    = 17'b0_0000_0100_0000_0000;
331
        parameter [16:0] rd_c    = 17'b0_0000_1000_0000_0000;
332
        parameter [16:0] rd_d    = 17'b0_0001_0000_0000_0000;
333
        parameter [16:0] wr_a    = 17'b0_0010_0000_0000_0000;
334
        parameter [16:0] wr_b    = 17'b0_0100_0000_0000_0000;
335
        parameter [16:0] wr_c    = 17'b0_1000_0000_0000_0000;
336
        parameter [16:0] wr_d    = 17'b1_0000_0000_0000_0000;
337
 
338
        always @(posedge clk or negedge nReset)
339
          if (!nReset)
340
            begin
341
                c_state <= #1 idle;
342
                cmd_ack <= #1 1'b0;
343
                scl_oen <= #1 1'b1;
344
                sda_oen <= #1 1'b1;
345
                sda_chk <= #1 1'b0;
346
            end
347
          else if (rst | al)
348
            begin
349
                c_state <= #1 idle;
350
                cmd_ack <= #1 1'b0;
351
                scl_oen <= #1 1'b1;
352
                sda_oen <= #1 1'b1;
353
                sda_chk <= #1 1'b0;
354
            end
355
          else
356
            begin
357
                cmd_ack   <= #1 1'b0; // default no command acknowledge + assert cmd_ack only 1clk cycle
358
 
359
                if (clk_en)
360
                  case (c_state) // synopsys full_case parallel_case
361
                    // idle state
362
                    idle:
363
                    begin
364
                        case (cmd) // synopsys full_case parallel_case
365
                          `I2C_CMD_START:
366
                             c_state <= #1 start_a;
367
 
368
                          `I2C_CMD_STOP:
369
                             c_state <= #1 stop_a;
370
 
371
                          `I2C_CMD_WRITE:
372
                             c_state <= #1 wr_a;
373
 
374
                          `I2C_CMD_READ:
375
                             c_state <= #1 rd_a;
376
 
377
                          default:
378
                            c_state <= #1 idle;
379
                        endcase
380
 
381
                        scl_oen <= #1 scl_oen; // keep SCL in same state
382
                        sda_oen <= #1 sda_oen; // keep SDA in same state
383
                        sda_chk <= #1 1'b0;    // don't check SDA output
384
                    end
385
 
386
                    // start
387
                    start_a:
388
                    begin
389
                        c_state <= #1 start_b;
390
                        scl_oen <= #1 scl_oen; // keep SCL in same state
391
                        sda_oen <= #1 1'b1;    // set SDA high
392
                        sda_chk <= #1 1'b0;    // don't check SDA output
393
                    end
394
 
395
                    start_b:
396
                    begin
397
                        c_state <= #1 start_c;
398
                        scl_oen <= #1 1'b1; // set SCL high
399
                        sda_oen <= #1 1'b1; // keep SDA high
400
                        sda_chk <= #1 1'b0; // don't check SDA output
401
                    end
402
 
403
                    start_c:
404
                    begin
405
                        c_state <= #1 start_d;
406
                        scl_oen <= #1 1'b1; // keep SCL high
407
                        sda_oen <= #1 1'b0; // set SDA low
408
                        sda_chk <= #1 1'b0; // don't check SDA output
409
                    end
410
 
411
                    start_d:
412
                    begin
413
                        c_state <= #1 start_e;
414
                        scl_oen <= #1 1'b1; // keep SCL high
415
                        sda_oen <= #1 1'b0; // keep SDA low
416
                        sda_chk <= #1 1'b0; // don't check SDA output
417
                    end
418
 
419
                    start_e:
420
                    begin
421
                        c_state <= #1 idle;
422
                        cmd_ack <= #1 1'b1;
423
                        scl_oen <= #1 1'b0; // set SCL low
424
                        sda_oen <= #1 1'b0; // keep SDA low
425
                        sda_chk <= #1 1'b0; // don't check SDA output
426
                    end
427
 
428
                    // stop
429
                    stop_a:
430
                    begin
431
                        c_state <= #1 stop_b;
432
                        scl_oen <= #1 1'b0; // keep SCL low
433
                        sda_oen <= #1 1'b0; // set SDA low
434
                        sda_chk <= #1 1'b0; // don't check SDA output
435
                    end
436
 
437
                    stop_b:
438
                    begin
439
                        c_state <= #1 stop_c;
440
                        scl_oen <= #1 1'b1; // set SCL high
441
                        sda_oen <= #1 1'b0; // keep SDA low
442
                        sda_chk <= #1 1'b0; // don't check SDA output
443
                    end
444
 
445
                    stop_c:
446
                    begin
447
                        c_state <= #1 stop_d;
448
                        scl_oen <= #1 1'b1; // keep SCL high
449
                        sda_oen <= #1 1'b0; // keep SDA low
450
                        sda_chk <= #1 1'b0; // don't check SDA output
451
                    end
452
 
453
                    stop_d:
454
                    begin
455
                        c_state <= #1 idle;
456
                        cmd_ack <= #1 1'b1;
457
                        scl_oen <= #1 1'b1; // keep SCL high
458
                        sda_oen <= #1 1'b1; // set SDA high
459
                        sda_chk <= #1 1'b0; // don't check SDA output
460
                    end
461
 
462
                    // read
463
                    rd_a:
464
                    begin
465
                        c_state <= #1 rd_b;
466
                        scl_oen <= #1 1'b0; // keep SCL low
467
                        sda_oen <= #1 1'b1; // tri-state SDA
468
                        sda_chk <= #1 1'b0; // don't check SDA output
469
                    end
470
 
471
                    rd_b:
472
                    begin
473
                        c_state <= #1 rd_c;
474
                        scl_oen <= #1 1'b1; // set SCL high
475
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
476
                        sda_chk <= #1 1'b0; // don't check SDA output
477
                    end
478
 
479
                    rd_c:
480
                    begin
481
                        c_state <= #1 rd_d;
482
                        scl_oen <= #1 1'b1; // keep SCL high
483
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
484
                        sda_chk <= #1 1'b0; // don't check SDA output
485
                    end
486
 
487
                    rd_d:
488
                    begin
489
                        c_state <= #1 idle;
490
                        cmd_ack <= #1 1'b1;
491
                        scl_oen <= #1 1'b0; // set SCL low
492
                        sda_oen <= #1 1'b1; // keep SDA tri-stated
493
                        sda_chk <= #1 1'b0; // don't check SDA output
494
                    end
495
 
496
                    // write
497
                    wr_a:
498
                    begin
499
                        c_state <= #1 wr_b;
500
                        scl_oen <= #1 1'b0; // keep SCL low
501
                        sda_oen <= #1 din;  // set SDA
502
                        sda_chk <= #1 1'b0; // don't check SDA output (SCL low)
503
                    end
504
 
505
                    wr_b:
506
                    begin
507
                        c_state <= #1 wr_c;
508
                        scl_oen <= #1 1'b1; // set SCL high
509
                        sda_oen <= #1 din;  // keep SDA
510
                        sda_chk <= #1 1'b1; // check SDA output
511
                    end
512
 
513
                    wr_c:
514
                    begin
515
                        c_state <= #1 wr_d;
516
                        scl_oen <= #1 1'b1; // keep SCL high
517
                        sda_oen <= #1 din;
518
                        sda_chk <= #1 1'b1; // check SDA output
519
                    end
520
 
521
                    wr_d:
522
                    begin
523
                        c_state <= #1 idle;
524
                        cmd_ack <= #1 1'b1;
525
                        scl_oen <= #1 1'b0; // set SCL low
526
                        sda_oen <= #1 din;
527
                        sda_chk <= #1 1'b0; // don't check SDA output (SCL low)
528
                    end
529
 
530
                  endcase
531
            end
532
 
533
 
534
        // assign scl and sda output (always gnd)
535
        assign scl_o = 1'b0;
536
        assign sda_o = 1'b0;
537
 
538
endmodule

powered by: WebSVN 2.1.0

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