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

Subversion Repositories i2c_master_slave_core

[/] [i2c_master_slave_core/] [trunk/] [i2c_master_slave_core/] [verilog/] [rtl/] [ms_core.v] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 toomuch
///////////////////////////////////////ms_core.v////////////////////////////////////////////////////////////////////////
2
//                                                                                                                    //                        
3
//Design engineer:      Ravi Gupta                                                                                    //                
4
//Company Name   :      Toomuch Semiconductor   
5
//Email          :      ravi1.gupta@toomuchsemi.com                                                                   //        
6
//                                                                                                                    //        
7
//Purpose        :      This is the core which will be used to interface I2C bus.                                     //        
8
//Created        :      23-11-07                                                                                      //
9
                                                                                                                      //                
10
//Modification : Changes made in data_reg_ld                                                                          //        
11
                                                                                                                      //        
12
//Modification : Change byte_trans generation bit                                                                     //        
13
                                                                                                                      //                
14
//Modification : Implemented a halt bit that will be generated at the completion of 9th scl pulse in master_mode only //
15
                                                                                                                      //                                        
16
//Modification : After core reset(time out feature) core will go in idle escaping stop generation so need to clear    //
17
//all                                                                                                                 //
18
//of status register.                                                                                                 //                
19
                                                                                                                      //
20
//Modification : Remove the sm_state clause,so that even if there is no acknowledegement it will not stop the         //
21
//generation of SCL and SDA                                                                                           //                                
22
//untill it gets command to generate stop from processor.                                                             //        
23
                                                                                                                      //                
24
//Modification : Now also checking for detect_start in acknowledgement state for repeted start condition.             //        
25
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
 
27
 
28
 
29
module core(clk,rst,sda_oe,sda_in,sda_o,scl_oe,scl_in,scl_o,ack,mode,rep_start,master_rw,data_in,slave_add,bus_busy,byte_trans,slave_addressed,arb_lost,slave_rw,time_out,inter,ack_rec,i2c_up,time_out_reg,prescale_reg,inter_rst,inter_en,halt_rst,data_en,time_rst,h_rst);
30
 
31
////////////////////////////////////////////////signal defination////////////////////////////////////////////////////////////////
32
 
33
input   clk;                    //System Clock
34
input   rst;                    //Main Reset
35
output  sda_oe;                 //I2C serial data line output to be connected to control line of bidirectional buffer on physical SDA line
36
input   sda_in;                 //I2C serial data line input
37
output  sda_o;                  //I2C sda line always asssign to zero this is to be connected to input of bidirectional buffer on physical SDA line
38
output  scl_oe;                 //I2C serial clock line output to be connected to control line of bidirectiional buffer on physical scl line
39
input   scl_in;                 //I2C serial clock line input
40
output  scl_o;                  //SCL output line to be connected to input of bidirectional line
41
input   ack;                    //Acknowledgement signal from control register
42
input   mode;                   //master/slave mode select
43
input   rep_start;              //repeated start
44
input   master_rw;              //command to core in master mode
45
input   [7:0]data_in;            //data from processor to be outputed on I2C
46
input   [7:0]slave_add;          //I2C slave address
47
input data_en;
48
output time_rst;
49
input h_rst;
50
 
51
//status signal:
52
 
53
output  bus_busy;               //bus busy
54
inout   byte_trans;             //transfer of byte is in progress_reg_en
55
 
56
inout   slave_addressed;        //addressed as slave
57
inout   arb_lost;               //arbitration has lost
58
inout   slave_rw;               //indicates the operation by slave
59
inout   time_out;               //indicates that SCL LOW time has been exceeded
60
output  inter;                  //interrupt pending,will be used for interrupting processor
61
input   inter_rst;              //use to clear the interrupt
62
input   inter_en;               //processor wants to take interrupt or not
63
 
64
//signal for processor
65
input halt_rst;
66
output  ack_rec;                //indicates that ack has been recieved,will be used to inform if master reciever wants to terminate the transfer
67
output  [7:0]i2c_up;             //I2C data for micro processor
68
//timing control registers
69
input [7:0]time_out_reg;         //max SCL low period.
70
input [7:0]prescale_reg;         //clock divider for generating SCL frequency.
71
 
72
/////////////////////////////////////////End of port defination//////////////////////////////////////////////////////////////////
73
 
74
wire master_slave,arbitration_lost,bb,gen_start,rep_start,byte_trans_delay,byte_trans_fall;
75
 
76
//wire scl_out,sda_out,clk_cnt_enable,clk_cnt_rst,bit_cnt_enable,bit_cnt_rst,timer_cnt_enable,timer_cnt_rst,scl_in,sda_in,sda_out_reg,stop_scl_reg,master_sda,  gen_stop;
77
wire master_sda,scl_in,gen_stop,sm_stop,detect_stop,detect_start,addr_match,core_rst,stop_scl,scl_out,neg_scl_sig,sda_sig;
78
//reg [7:0]clk1_cnt,bit1_cnt,timer1_cnt;
79
reg posedge_mode,negedge_mode;
80
reg [2:0]scl_state;
81
reg [1:0]state;
82
reg [2:0]scl_main_state;
83
wire [7:0] add_reg,shift_reg;
84
wire [7:0]clk_cnt,bit_cnt;
85
reg  [7:0]time_cnt;
86
reg  [7:0]i2c_up;
87
wire bit_cnt_enable,bit_cnt_rst,clk_cnt_enable,clk_cnt_rst,data_reg_ld,data_reg_en,sda_in,serial_out,i2c_serial_out,add_reg_ld,add_reg_en,posedge_mode_sig,negedge_mode_sig,interrupt;
88
wire [7:0]zero;
89
wire [7:0]reg_clr;
90
 
91
wire slave_sda,sda_out,halt,arb_rst,interrupt_rst,d_detect_stop;
92
 
93
shift shift_data(neg_scl,rst,data_reg_ld,data_reg_en,sda_in,data_in,serial_out,shift_reg);      //shift register for transferring the data
94
shift shift_add(neg_scl,rst,add_reg_ld,add_reg_en,sda_in,reg_clr,i2c_serial_out,add_reg);       //shift register for transferring address
95
counter clock_counter(clk,rst,clk_cnt_enable,clk_cnt_rst,zero,clk_cnt);                         //This will count number of clock pulses for prescale
96
counter bit_counter(neg_scl,rst,bit_cnt_enable,bit_cnt_rst,zero,bit_cnt);                       //Implementation of bit counter
97
 
98
 
99
 
100
 
101
 
102
 
103
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104
reg clk_cnt_enable_sig;
105
assign clk_cnt_enable = clk_cnt_enable_sig;
106
 
107
reg clk_cnt_rst_sig;
108
assign clk_cnt_rst = clk_cnt_rst_sig;
109
 
110
 
111
//reg sda_in_sig;
112
//assign sda_in = sda_in_sig;
113
 
114
reg sm_stop_sig;
115
assign sm_stop = sm_stop_sig;
116
 
117
//reg scl_in_sig;
118
//assign scl_in = scl_in_sig;
119
 
120
reg gen_start_sig;
121
assign gen_start = gen_start_sig;
122
 
123
reg gen_stop_sig;
124
assign gen_stop = gen_stop_sig;
125
 
126
reg master_slave_sig;
127
assign master_slave = master_slave_sig;
128
 
129
reg detect_start_sig;
130
assign detect_start=detect_start_sig;
131
 
132
reg detect_stop_sig;
133
assign detect_stop=detect_stop_sig;
134
 
135
reg byte_trans_sig;
136
assign byte_trans= byte_trans_sig;
137
 
138
reg bb_sig;
139
assign bb=bb_sig;
140
 
141
reg slave_addressed_sig;
142
assign slave_addressed=slave_addressed_sig;
143
 
144
reg slave_rw_sig;
145
assign slave_rw=slave_rw_sig;
146
 
147
reg inter_sig;
148
assign inter=inter_sig;
149
assign interrupt=inter_sig;
150
 
151
reg time_out_sig;
152
assign time_out=time_out_sig;
153
 
154
reg ack_rec_sig;
155
assign ack_rec=ack_rec_sig;
156
 
157
reg add_reg_enable_sig;
158
assign add_reg_en=add_reg_enable_sig;
159
 
160
reg data_reg_en_sig;
161
assign data_reg_en=data_reg_en_sig;
162
 
163
reg data_reg_ld_sig;
164
assign data_reg_ld=data_reg_ld_sig;
165
 
166
reg stop_scl_sig;
167
assign stop_scl=stop_scl_sig;
168
 
169
reg core_rst_sig;
170
assign core_rst=core_rst_sig;
171
 
172
reg sda_out_sig;
173
assign sda_out=sda_out_sig;
174
 
175
reg scl_out_sig;
176
assign scl_out=scl_out_sig;
177
 
178
reg master_sda_sig;
179
assign master_sda=master_sda_sig;
180
 
181
reg slave_sda_sig;
182
assign slave_sda=slave_sda_sig;
183
 
184
reg arbitration_lost_sig;
185
assign arbitration_lost=arbitration_lost_sig;
186
 
187
reg arb_lost_sig;
188
assign arb_lost=arb_lost_sig;
189
 
190
reg byte_trans_delay_sig;
191
assign byte_trans_delay=byte_trans_delay_sig;
192
 
193
reg byte_trans_fall_sig;
194
assign byte_trans_fall=byte_trans_fall_sig;
195
 
196
reg halt_sig;
197
assign halt = halt_sig;
198
 
199
reg arb_rst_sig;
200
assign arb_rst=arb_rst_sig;
201
 
202
reg interrupt_rst_sig;
203
assign interrupt_rst=interrupt_rst_sig;
204
 
205
reg rep_start_sig;
206
assign time_rst = core_rst;
207
 
208
reg d_detect_stop_sig;
209
assign d_detect_stop = d_detect_stop_sig;
210
 
211
reg d1_detect_stop_sig;
212
 
213
 
214
assign bus_busy = bb;
215
assign reg_clr=8'b00000000;
216
assign neg_scl_sig=(~scl_in);
217
assign neg_scl=neg_scl_sig;
218
assign zero=8'b00000000;
219
assign posedge_mode_sig=posedge_mode;
220
assign negedge_mode_sig=negedge_mode;
221
assign sda_o = 1'b0;    //assign this to 0 always
222
assign scl_o = 1'b0;
223
 
224
 
225
 
226
parameter       scl_idle=3'b000,scl_start=3'b001,scl_low_edge=3'b010,scl_low=3'b011,scl_high_edge=3'b100,scl_high=3'b101;
227
parameter       scl_address_shift=3'b001,scl_ack_address=3'b010,scl_rx_data=3'b011,scl_tx_data=3'b100,scl_send_ack=3'b101,scl_wait_ack=3'b110,scl_main_idle=                    3'b000;
228
 
229
parameter       a=2'b00,b=2'b01,c=2'b10;
230
 
231
////////////////////SCL Generator///////////////////////////////
232
//This machine will generate SCL and SDA when in master mode.It will
233
//also generate START and STOP condition.
234
 
235
//always@(scl_state or arbitration_lost or sm_stop or gen_stop or rep_start
236
//              or bb or gen_start or master_slave or clk_cnt or bit_cnt or
237
//              scl_in  or sda_out or master_sda or core_rst)
238
always@(posedge clk or posedge rst or posedge core_rst or posedge h_rst)
239
begin
240
 
241
//State machine initial conditions
242
 
243
if(rst || h_rst)
244
begin
245
        scl_state<=scl_idle;
246
        scl_out_sig<=1'b1;
247
        sda_out_sig<=1'b1;
248
        stop_scl_sig<=1'b0;
249
        clk_cnt_enable_sig<=1'b0;
250
        clk_cnt_rst_sig<=1'b1;
251
        //bit_cnt_rst_sig<=1'b0;
252
        //bit_cnt_enable_sig<=1'b0;
253
end
254
 
255
else if(core_rst)
256
begin
257
 
258
        scl_state<=scl_idle;
259
        scl_main_state <= scl_main_idle;
260
        scl_out_sig<=1'b1;
261
        sda_out_sig<=1'b1;
262
        stop_scl_sig<=1'b0;
263
        clk_cnt_enable_sig<=1'b0;
264
        clk_cnt_rst_sig<=1'b1;
265
        slave_addressed_sig<=1'b0;
266
end
267
 
268
 
269
else
270
begin
271
 
272
                case (scl_state)
273
 
274
                        scl_idle:
275
                        begin
276
                                arb_rst_sig <= 1'b1;
277
                                interrupt_rst_sig<=1'b1;
278
                                sda_out_sig<=1'b1;
279
                                stop_scl_sig<=1'b0;
280
 
281
                                        if(master_slave && !bb && gen_start)
282
                                        begin
283
                                                scl_state<=scl_start;
284
 
285
                                        end
286
                        end
287
 
288
 
289
                        scl_start:
290
                        begin
291
                                arb_rst_sig <= 1'b0;
292
                                interrupt_rst_sig<=1'b0;
293
                                clk_cnt_enable_sig<=1'b1;                               //enable the counter as soon as machine enters in this state.
294
                                clk_cnt_rst_sig<=1'b0;
295
                                //sda_out_sig<=1'b0;                                    //generating start condition
296
                                stop_scl_sig<=1'b0;
297
                        if(clk_cnt == prescale_reg / 3)
298
                                sda_out_sig<= 1'b0;
299
 
300
                                        if(clk_cnt == prescale_reg)             //wait for prescale value to over
301
                                                scl_state<=scl_low_edge;
302
                                        else
303
                                                scl_state<=scl_start;
304
                        end
305
 
306
                        scl_low_edge:
307
                        begin
308
                                clk_cnt_rst_sig<=1'b1;                  //This state will generate only SCL negative edge,and reset all the counters
309
                                //timer_cnt_enable_sig<=1'b1;                   //except timer counter which will be enabled at this state.
310
                                //timer_cnt_rst_sig<=1'b0;                      //also reseting the timer counter in this state.
311
                                scl_out_sig<=1'b0;
312
                                scl_state<=scl_low;
313
                                stop_scl_sig<=1'b0;
314
                        end
315
 
316
                        scl_low:
317
                        begin
318
                                clk_cnt_enable_sig<=1'b1;                       //enable the clock counter
319
                                clk_cnt_rst_sig<=1'b0;
320
                                scl_out_sig<=1'b0;
321
 
322
                                                        if(arbitration_lost)
323
                                                                stop_scl_sig<=1'b0;
324
                                                else if(rep_start_sig)
325
                                                        begin
326
                                                                sda_out_sig<=1'b1;
327
                                                                stop_scl_sig<=1'b0;
328
                                                        end
329
 
330
 
331
                                                 else if((gen_stop) && ((scl_main_state != scl_ack_address) && (scl_main_state != scl_send_ack)
332
                                                                        && (scl_main_state != scl_wait_ack)))           //Ravi remove sm_stop from oring with gen_stop
333
                                                        begin
334
                                                                sda_out_sig<=1'b0;
335
                                                                stop_scl_sig<=1'b1;
336
 
337
                                                        end
338
 
339
                                                        /*else if(rep_start)
340
                                                        begin
341
                                                                sda_out_sig<=1'b1;
342
                                                                stop_scl_sig<=1'b0;
343
                                                        end*/
344
                                                        else if(clk_cnt == prescale_reg / 3)
345
                                                        begin
346
                                                                                sda_out_sig<=master_sda;
347
                                                                                stop_scl_sig<=1'b0;
348
 
349
                                                        end
350
 
351
                                                        else
352
                                                                stop_scl_sig<=1'b0;
353
 
354
 
355
                                                                                        //determine next state. 
356
 
357
                                        if(clk_cnt == prescale_reg)
358
                                        begin
359
                                                if(bit_cnt == 8'b0000_0111 && arbitration_lost )
360
                                                        scl_state<=scl_idle;
361
                                                else if(interrupt && inter_en)                                                                  //uncomenting out for cheking the core in interrupt mode 
362
                                                        scl_state<=scl_low;
363
                                                else if(halt)
364
                                                        scl_state<=scl_low;
365
                                                else
366
                                                        scl_state<=scl_high_edge;
367
                                        end
368
 
369
                                        else
370
                                                scl_state<=scl_low;
371
                        end
372
 
373
 
374
 
375
 
376
                        scl_high_edge:
377
                        begin
378
                                clk_cnt_rst_sig<=1'b1;
379
                                scl_out_sig<=1'b1;
380
                                if(gen_stop)                            //Ravi sm_stop from oring with gen_stop
381
                                        stop_scl_sig<=1'b1;
382
 
383
                                else
384
                                        stop_scl_sig<=1'b0;
385
                                if(!scl_in)
386
                                        scl_state<=scl_high_edge;
387
                                else
388
                                        scl_state<=scl_high;
389
                        end
390
 
391
 
392
 
393
                        scl_high:
394
                        begin
395
                                clk_cnt_enable_sig<=1'b1;
396
                                clk_cnt_rst_sig<=1'b0;
397
                                scl_out_sig<=1'b1;
398
                                if(clk_cnt == prescale_reg)
399
                                begin
400
                                        if(rep_start_sig)
401
                                                scl_state<=scl_start;
402
                                        else if(stop_scl)
403
                                                scl_state<=scl_idle;
404
 
405
                                        else
406
                                                scl_state<=scl_low_edge;
407
                                end
408
 
409
                                else
410
                                        scl_state<=scl_high;
411
                        end
412
 
413
 
414
                endcase
415
end
416
end
417
 
418
 
419
//Sample the incoming SDA and SCL line with System clock
420
 
421
/*always@(posedge clk or posedge rst)
422
begin
423
 
424
        if(rst)
425
        begin
426
                //sda_in_sig <= 1'b1;
427
                scl_in_sig <=1'b1;
428
        end
429
        else
430
        begin
431
                if(!scl)
432
                        scl_in_sig <= 1'b0;
433
                else
434
                        scl_in_sig <= 1'b1;
435
 
436
                if(!sda)
437
                        sda_in_sig <= 1'b0;
438
                else
439
                        sda_in_sig <= 1'b1;
440
 
441
        //sda_out_sig <= sda;
442
        end
443
end*/
444
 
445
//Generartion of control signal from the command based on processor.
446
//This will control generation of start and stop signal.
447
//This will also set the master_slave bit based on MODE signal
448
//if bus is not busy i.e bb = 0
449
 
450
always@(posedge clk or posedge rst or posedge h_rst)
451
begin
452
        if(rst || h_rst)
453
        begin
454
                gen_start_sig <= 1'b0;
455
                gen_stop_sig <= 1'b0;
456
                master_slave_sig <= 1'b0;
457
 
458
        end
459
 
460
        else
461
        begin
462
                if(posedge_mode_sig)
463
                        gen_start_sig <= 1'b1;
464
                else if(detect_start)
465
                        gen_start_sig <= 1'b0;
466
 
467
                if(!arbitration_lost && negedge_mode_sig)
468
                        gen_stop_sig <= 1'b1;
469
                else if(detect_stop)
470
                        gen_stop_sig <= 1'b0;
471
 
472
                if(!bb)
473
                        master_slave_sig <= mode;
474
                else
475
                        master_slave_sig <= master_slave;
476
        end
477
end
478
 
479
//State machine for detection of rising and falling edge of input mode for the generation of START and STOP.
480
always@(posedge clk or posedge rst or posedge h_rst)
481
begin
482
        if(rst || h_rst)
483
        begin
484
                posedge_mode<=1'b0;
485
                negedge_mode<=1'b0;
486
                state<=a;
487
        end
488
 
489
        else
490
        begin
491
                case(state)
492
 
493
                        a:
494
                                if(mode==1'b0)
495
                                begin
496
                                        state<=b;
497
                                        posedge_mode<=1'b0;
498
                                        negedge_mode<=1'b0;
499
                                end
500
 
501
                                else
502
                                begin
503
                                        state<=c;
504
                                        posedge_mode<=1'b1;
505
                                        negedge_mode<=1'b0;
506
                                end
507
 
508
                        b:
509
                                if(mode==1'b0)
510
                                begin
511
                                        state<=b;
512
                                        posedge_mode<=1'b0;
513
                                        negedge_mode<=1'b0;
514
                                end
515
 
516
                                else
517
                                begin
518
                                        state<=a;
519
                                        posedge_mode<=1'b1;
520
                                        negedge_mode<=1'b0;
521
                                end
522
 
523
                        c:
524
                                if(mode==1'b0)
525
                                begin
526
                                        state<=a;
527
                                        posedge_mode<=1'b0;
528
                                        negedge_mode<=1'b1;
529
                                end
530
 
531
                                else
532
                                begin
533
                                        state<=c;
534
                                        posedge_mode<=1'b0;
535
                                        negedge_mode<=1'b0;
536
                                end
537
 
538
                endcase
539
end
540
end
541
 
542
//This is the main state machine which will be used as both master as well as slave.
543
//This gets triggered at falling edge of SCL.
544
//If stop codition gets detected then it should work as asyn reset.
545
 
546
always@(posedge rst or negedge scl_in or posedge detect_stop or posedge core_rst or posedge h_rst)
547
begin
548
 
549
if(rst || core_rst || h_rst)
550
begin
551
        scl_main_state<=scl_main_idle;
552
        sm_stop_sig<=1'b0;
553
end
554
 
555
else
556
begin
557
        case(scl_main_state)
558
        scl_main_idle:
559
 
560
                if(detect_start)
561
                        scl_main_state<=scl_address_shift;
562
                else if(detect_stop)
563
                begin
564
                        scl_main_state<=scl_main_idle;
565
                        sm_stop_sig<=1'b0;
566
                end
567
 
568
        scl_address_shift:                                      //machine will remain in this state,unless all the bits of address has been transferred. 
569
 
570
                if(bit_cnt == 8'b0000_0111)
571
                        scl_main_state<=scl_ack_address;
572
                else if(detect_stop)
573
                begin
574
                        scl_main_state<=scl_main_idle;
575
                        sm_stop_sig<=1'b0;
576
                end
577
 
578
        scl_ack_address:
579
 
580
                //if(arbitration_lost)                                  //if arbitration lost then go to idle state releasing buses.remove this because its a 
581
                        //scl_main_state<=scl_main_idle;                //software problem if even after arb_lost it is giving wr/rd then it has to go to respective state.
582
                if(detect_stop)
583
                begin                                           //Go to idle state if there is stop command
584
                        scl_main_state<=scl_main_idle;
585
                        sm_stop_sig<=1'b0;
586
                end
587
 
588
                else if(detect_start)
589
                begin
590
                        scl_main_state<=scl_address_shift;
591
                        sm_stop_sig<=1'b0;
592
                end
593
                //else if(!sda_in)
594
                                                        //If ack has been received then,check for slave/master
595
 
596
                        else if(master_slave)
597
                        begin                           //if master then set the direction for master to either transmit 
598
                           if(!master_rw)                               //or receive the data.
599
                                scl_main_state<=scl_rx_data;
600
                           else
601
                                scl_main_state<=scl_tx_data;    //Ravi: if no detect_stop then check if master send to state depending upon 
602
                        end                                                                     //tx/rx bit of control register.
603
 
604
                        else
605
                        begin                                           //If slave then check if received address has matched
606
                           //if(addr_match)
607
                           //begin                                      //if address matches then set the direction of communication based 
608
                              if(add_reg[0])                             //last bit of shift register of address cycle.
609
                                   scl_main_state<=scl_tx_data;
610
                              else
611
                                    scl_main_state<=scl_rx_data;
612
                         end
613
                           //else
614
                                //scl_main_state<=scl_main_idle;
615
                        //end
616
 
617
 
618
                //else
619
                //begin
620
                //      scl_main_state<=scl_main_idle;                          //If no ack received go to idle state.  
621
                //if(master_slave)
622
                //      sm_stop_sig<=1'b1;
623
                //end
624
 
625
        scl_rx_data:
626
                if(bit_cnt == 8'b0000_0111)
627
                        scl_main_state<=scl_send_ack;
628
                else if(detect_stop)
629
                begin
630
                        scl_main_state<=scl_main_idle;
631
                        sm_stop_sig<=1'b0;
632
                end
633
                else if(detect_start)
634
                begin
635
                        scl_main_state<=scl_address_shift;
636
                        sm_stop_sig<=1'b0;
637
                end
638
 
639
 
640
 
641
 
642
        scl_tx_data:
643
                if(bit_cnt == 8'b0000_0111)
644
                        scl_main_state<=scl_wait_ack;
645
                else if(detect_stop)
646
                begin
647
                        scl_main_state<=scl_main_idle;
648
                        sm_stop_sig<=1'b0;
649
                end
650
                else if(detect_start)
651
                begin
652
                        scl_main_state<=scl_address_shift;
653
                        sm_stop_sig<=1'b0;
654
                end
655
 
656
 
657
        scl_send_ack:
658
                if(detect_stop)
659
                begin
660
                        scl_main_state<=scl_main_idle;
661
                        sm_stop_sig<=1'b0;
662
                end
663
 
664
                else
665
                        scl_main_state<=scl_rx_data;
666
 
667
        scl_wait_ack:                                                           //Ravi: Even in this state machine will goto Tx state,if no ack or arb_lost has occur  
668
                //if(arbitration_lost)                                  //This is software part to program the control register so that it will generate stop 
669
                        //scl_main_state<=scl_main_idle;                //and will go in idle state.So removing all clauses except detect stop.
670
                if(detect_stop)
671
                begin
672
                        scl_main_state<=scl_main_idle;
673
                        sm_stop_sig<=1'b0;
674
                end
675
 
676
 
677
                else
678
                        scl_main_state<=scl_tx_data;
679
                //else
680
                //begin
681
                        //if(master_slave)
682
                                //sm_stop_sig<=1'b1;
683
                //scl_main_state<=scl_main_idle;
684
                //end
685
        endcase
686
end
687
end
688
 
689
//Start and stop detect process
690
//////////////////////////////
691
 
692
always@(sda_in or scl_main_state)
693
begin
694
 
695
        if(rst || h_rst)
696
                detect_start_sig<=1'b0;
697
        else if(!sda_in && scl_in)
698
                detect_start_sig<=1'b1;
699
        else if(scl_address_shift)
700
                detect_start_sig<=1'b0;
701
        else
702
                detect_start_sig<=1'b0;
703
end
704
 
705
always@(posedge sda_in or posedge detect_start)
706
begin
707
 
708
        if(rst || h_rst)
709
                detect_stop_sig<=1'b0;
710
        else if(detect_start)
711
                detect_stop_sig<=1'b0;
712
        else if(scl_in)
713
                detect_stop_sig<=1'b1;
714
        //else if(detect_start)
715
                //detect_stop_sig<=1'b0;
716
        else
717
                detect_stop_sig<=1'b0;
718
end
719
 
720
//generate a delay version of byte_trans signal
721
//This will be used for detecting falling edge of byte_trans
722
 
723
always@(posedge clk or posedge rst or posedge h_rst)
724
begin
725
        if(rst || h_rst)
726
                byte_trans_delay_sig <= 1'b0;
727
        else
728
        begin
729
                byte_trans_delay_sig <= byte_trans;
730
                byte_trans_fall_sig <= byte_trans_delay && !byte_trans;
731
        end
732
end
733
 
734
 
735
//Processor status bits/////
736
//byte_trans bit
737
//This indicate data is being transferred,This bit will be one only after all 8 bits has
738
//been tranferred.i.e on rising pulse of SCL in ack cycle.
739
 
740
always@(negedge scl_in or posedge rst or posedge halt_rst or posedge core_rst or posedge h_rst)
741
begin
742
        if(rst || h_rst)
743
                byte_trans_sig<=1'b0;
744
        else if(halt_rst)
745
                byte_trans_sig <= 1'b0;
746
        else if(bit_cnt == 8'b0000_1000)
747
                byte_trans_sig<=1'b1;
748
        else if(halt_rst || core_rst)                   // after core_rst negate byte_trans bit
749
                byte_trans_sig<=1'b0;
750
end
751
 
752
//bus_busy
753
//This indicates that communication is in progress and bus in not free.
754
//This bit will be set on detection of start and will be cleared on STOP
755
 
756
always@(posedge clk or posedge rst or posedge h_rst)
757
begin
758
        if(rst || h_rst)
759
                bb_sig<=1'b0;
760
        else
761
        begin
762
                if(detect_start)
763
                        bb_sig<=1'b1;
764
                if(detect_stop || core_rst)
765
                        bb_sig<=1'b0;
766
        end
767
end
768
 
769
//slave_addressed bit
770
//This indicates that slave has been addressed,and after sending ack
771
//core will switch to slave mode.
772
//This bit will be set if adds matched in add ack state.
773
 
774
always@(posedge clk or posedge rst or posedge h_rst)
775
begin
776
        if(rst)                         //Removing h_rst
777
        slave_addressed_sig<=1'b0;
778
        //else if(scl_main_state == scl_ack_address)
779
        else if(byte_trans)
780
        slave_addressed_sig<=addr_match;
781
        else
782
        slave_addressed_sig<=slave_addressed;
783
        //slave_addressed_sig<= 1'b0;
784
end
785
 
786
//set address match bit if address reg matches with shift register output
787
/*always@(negedge scl or posedge rst)
788
begin
789
        if(rst)
790
                addr_match_sig<=1'b0;
791
        else if( slave_add[7:1] == add_reg[7:1])
792
                addr_match_sig <=1'b1;
793
        else
794
                addr_match_sig<=1'b0;
795
end*/
796
assign addr_match = slave_add[7:1] == add_reg[7:1]? 1'b1:1'b0;
797
assign add_reg_ld = 1'b0;
798
 
799
//Slave read write
800
//This bit indicates slave has been addressed,this indicates
801
//read or write bit sent by processor.
802
 
803
always@(posedge clk or posedge rst or posedge h_rst)
804
begin
805
        if(rst || h_rst)
806
                slave_rw_sig<=1'b0;
807
        else if(scl_main_state == scl_ack_address)
808
                slave_rw_sig<=add_reg[0];
809
end
810
 
811
//interrupt pending
812
//This will cause an interrupt to processor if interrupt enable is set
813
//This bit will be set in following circumstances:
814
//1):Byte transfer has been completed.
815
//2):Arbitration lost.
816
//3):slave has been addressed and and bytes have been transferred.
817
//4):Time out condition has been reached.
818
//5):Repeated start condition.
819
//Only processor can clear the interrupt.
820
 
821
always@(posedge clk or posedge rst or posedge h_rst)
822
begin
823
        if(rst || h_rst)
824
        inter_sig<=1'b0;
825
 
826
        else
827
        begin
828
                //if(interrupt_rst)
829
                //inter_sig<=1'b0;
830
 
831
                if(inter_rst)
832
                inter_sig<=1'b0;
833
 
834
        //in below else if condition anding byte_trans with master_slave also removing add_reg[]  condition in next clause              
835
                else if((byte_trans && master_slave) || arbitration_lost || (slave_addressed && !master_slave && byte_trans) || rep_start)
836
                inter_sig<=1'b1;
837
 
838
 
839
 
840
                //else                  //interrupt need to get cleared by processor,so do not reset in else condition
841
                //inter_sig<=1'b0;
842
 
843
 
844
        end
845
end
846
 
847
//generate delay version of detect_stop
848
always@(posedge clk or posedge rst or posedge h_rst)
849
begin
850
if(rst || h_rst)
851
d_detect_stop_sig <= 1'b0;
852
else
853
begin
854
d1_detect_stop_sig <= detect_stop;
855
d_detect_stop_sig <= d1_detect_stop_sig;
856
end
857
end
858
 
859
 
860
always@(posedge clk or posedge rst or posedge h_rst)
861
begin
862
        if(rst || h_rst)
863
        halt_sig <= 1'b0;
864
 
865
        else
866
        begin
867
                if(halt_rst)
868
                        halt_sig<=1'b0;
869
 
870
                else if(byte_trans && master_slave)
871
                        halt_sig<=1'b1;
872
        end
873
end
874
 
875
//acknoweldege recieve
876
//This bit indicates the data on SDA line during ack cycle.
877
 
878
always@(posedge clk or posedge rst or posedge h_rst)
879
begin
880
        if(rst || h_rst)
881
                ack_rec_sig<=1'b0;
882
        else if((scl_main_state == scl_wait_ack) || (scl_main_state == scl_ack_address) || (scl_main_state == scl_send_ack))
883
                ack_rec_sig<=sda_in;
884
end
885
 
886
//Setting control bits of shift registers and counters
887
//////////////////////////////////////////////////////
888
 
889
//Address shift register will just receive the data after start 
890
//condition detection.It wont be get loaded.While data shift register
891
//will receive as well as transmit the data.
892
 
893
//address shift register enable bit
894
always@(posedge clk or posedge rst or posedge h_rst)
895
begin
896
        if(rst || h_rst)
897
                add_reg_enable_sig<=1'b0;
898
        else if(detect_start || scl_main_state == scl_address_shift)
899
                add_reg_enable_sig<=1'b1;
900
        else
901
                add_reg_enable_sig<=1'b0;
902
end
903
 
904
 
905
//Data shift register.
906
//This register will be enabled every time when it is either transmitting or receiving the data. 
907
  always @(posedge clk or posedge rst or posedge h_rst)
908
  begin
909
    if (rst || h_rst)
910
    begin
911
      data_reg_en_sig <= 1'b0;
912
      data_reg_ld_sig <= 1'b0;
913
    end
914
    else
915
    begin
916
      if (((master_slave && scl_main_state == scl_address_shift) || (scl_main_state ==
917
          scl_rx_data) || (scl_main_state == scl_tx_data)))
918
        data_reg_en_sig <= 1'b1;
919
      else
920
        data_reg_en_sig <= 1'b0;
921
 
922
         /*if ((master_slave && scl_main_state == scl_idle) || (scl_main_state ==
923
          scl_wait_ack) || (scl_main_state == scl_ack_address &&
924
          !add_reg[0] && !master_slave) || (scl_main_state == scl_ack_address &&
925
          master_rw && master_slave))*/
926
                if(((scl_main_state == scl_main_idle) || byte_trans) && data_en)
927
 
928
                data_reg_ld_sig <= 1'b1;
929
         else
930
        data_reg_ld_sig <= 1'b0;
931
 
932
    end
933
 
934
  end
935
 
936
//logic for generating control bits for bit counter
937
////////////////////////////////////////////////////////////////////////////////////////////////
938
assign bit_cnt_enable = ((scl_main_state == scl_address_shift) || (scl_main_state == scl_rx_data) || (scl_main_state == scl_tx_data));
939
assign bit_cnt_rst = ((scl_main_state == scl_main_idle) || (scl_main_state == scl_send_ack) || (scl_main_state == scl_wait_ack) || (scl_main_state == scl_ack_address));
940
/////////////////////////////////////////////////////////////////////////////////////////////
941
//implementation of timer counter
942
 
943
always@(posedge clk or posedge rst or posedge h_rst)
944
begin
945
        if(rst || h_rst)
946
        time_cnt<=8'b0000_0000;
947
        else if(!scl_in)
948
        time_cnt<=time_cnt + 1'b1;
949
        else
950
        time_cnt<=8'b0000_0000;
951
end
952
 
953
always@(posedge clk or posedge rst or posedge h_rst)
954
begin
955
        if(rst || h_rst)
956
        begin
957
                core_rst_sig<=1'b0;
958
                time_out_sig<=1'b0;
959
        end
960
        else if((time_cnt == time_out_reg) & bb)
961
        begin
962
                core_rst_sig <= 1'b1;
963
                time_out_sig <= 1'b1;
964
        end
965
        /*else if((time_cnt == time_out_reg) && (scl_state == scl_idle))
966
        begin
967
                core_rst_sig <= 1'b0;
968
                time_out_sig <= 1'b1;
969
        end*/
970
        else
971
        begin
972
                core_rst_sig <= 1'b0;
973
                time_out_sig <= 1'b0;
974
        end
975
end
976
 
977
//Process for assigning Master and slave SDA.
978
always@(posedge clk or posedge rst or posedge h_rst)
979
begin
980
        if(rst || h_rst)
981
                master_sda_sig<=1'b1;
982
        else if((scl_main_state == scl_address_shift) || (scl_main_state == scl_tx_data))
983
                master_sda_sig<=serial_out;
984
        else if(scl_main_state == scl_send_ack)
985
                master_sda_sig<=ack;
986
        else
987
                master_sda_sig<=1'b1;
988
end
989
 
990
always@(posedge clk or posedge rst or posedge h_rst)
991
begin
992
        if(rst || h_rst)
993
                slave_sda_sig<=1'b1;
994
        else if(scl_main_state == scl_tx_data)
995
                slave_sda_sig<=serial_out;
996
        else if((addr_match && (scl_main_state == scl_ack_address)) || (scl_main_state == scl_send_ack))
997
                slave_sda_sig<=ack;
998
        else
999
                slave_sda_sig<=1'b1;
1000
end
1001
 
1002
//assigning SCL and SDA lines in output conditions.
1003
 
1004
 
1005
assign scl_oe = master_slave ? scl_out : 1'b1;
1006
assign sda_sig = (((master_slave == 1'b1 && sda_out == 1'b0) ||
1007
                 (master_slave == 1'b0 && slave_sda == 1'b0) || stop_scl) ? 1'b1 : 1'b0);
1008
assign sda_oe = (sda_sig ?1'b0 : 1'b1);
1009
 
1010
//Presenting data on data_register which is for processor
1011
always@(posedge clk or posedge rst or posedge h_rst)
1012
begin
1013
        if(rst || h_rst)
1014
                i2c_up<=8'b00000000;
1015
        else if(scl_main_state == scl_send_ack)
1016
                i2c_up<=shift_reg;
1017
        else
1018
                i2c_up<=i2c_up;
1019
end
1020
 
1021
 
1022
 
1023
//This process will set arbitration lost signal
1024
//////////////////////////////////////////////
1025
 //   This process checks the master's outgoing SDA with the incoming SDA to determine
1026
  //   if control of the bus has been lost. SDA is checked only when SCL is high
1027
  //   and during the states IDLE, ADD_SHIFT, and TX_DATA to insure that START and STOP
1028
  //   conditions are not set when the bus is busy. Note that this is only done when Master.
1029
   always @( posedge (clk) or posedge (rst) or posedge (h_rst) )
1030
  begin
1031
    if (rst || h_rst)
1032
    begin
1033
      arbitration_lost_sig <= 1'b0;
1034
     end
1035
    else
1036
    begin
1037
      if (scl_main_state == scl_idle)
1038
      begin
1039
        arbitration_lost_sig <= 1'b0;
1040
      end
1041
      else if ((master_slave))
1042
        //   only need to check arbitration in master mode
1043
        //   check for SCL high before comparing data 
1044
        if ((scl_in && scl_oe && (scl_main_state == scl_address_shift || scl_main_state
1045
            == scl_tx_data || scl_main_state == scl_idle)))
1046
          //   when master, will check bus in all states except ACK_ADDR and WAIT_ACK
1047
          //   this will insure that arb_lost is set if a start or stop condition
1048
          //   is set at the wrong time
1049
                //if(sda_in == 1'b0 && sda_oe == 1'b1) || (detect_stop
1050
 
1051
         if (sda_in == 1'b0 && sda_oe == 1'b1)
1052
          begin
1053
            arbitration_lost_sig <= 1'b1;
1054
 
1055
          end
1056
          else
1057
          begin
1058
            arbitration_lost_sig <= 1'b0;
1059
 
1060
          end
1061
 
1062
        else
1063
        begin
1064
          arbitration_lost_sig <= arbitration_lost;
1065
        end
1066
 
1067
 
1068
    end
1069
 
1070
  end
1071
 
1072
//setting the arbitration lost bit of status register
1073
////////////////////////////////////////////////////
1074
//this bit will be set when:
1075
        //arbiration has lost.
1076
        //core is in master mode and a generate strat condition has detected while bus is busy 
1077
        //or a stop conditioin has been detected when not requested
1078
        //or a repeate start has been detected when in slave mode.
1079
 
1080
always@(posedge clk or posedge rst or posedge core_rst or posedge h_rst)
1081
begin
1082
        if(rst || h_rst)
1083
                arb_lost_sig<=1'b0;
1084
        else
1085
        begin
1086
                if(arb_rst)
1087
                arb_lost_sig<=1'b0;
1088
                else if(master_slave)
1089
                begin
1090
                        if((arbitration_lost)||(bus_busy && gen_start))
1091
                                arb_lost_sig<=1'b1;
1092
                end
1093
 
1094
                else if(rep_start)
1095
                        arb_lost_sig<=1'b1;
1096
                //else if(core_rst && master_slave)
1097
                        //arb_lost_sig<=1'b0;
1098
                else
1099
                        arb_lost_sig<=1'b0;
1100
        end
1101
end
1102
 
1103
always@(posedge clk or posedge rst or posedge h_rst)
1104
begin
1105
if(rst || h_rst)
1106
        rep_start_sig<=1'b0;
1107
else if(scl_main_state == scl_address_shift || scl_main_state == scl_ack_address || scl_main_state == scl_send_ack || scl_main_state == scl_wait_ack)
1108
        rep_start_sig<=1'b0;
1109
else
1110
        rep_start_sig<=rep_start;
1111
end
1112
 
1113
 
1114
 
1115
endmodule
1116
 
1117
 
1118
 
1119
 
1120
 
1121
 
1122
 
1123
 
1124
 
1125
 
1126
 
1127
 
1128
 
1129
 
1130
 
1131
 
1132
 
1133
 
1134
 
1135
 
1136
 
1137
 
1138
 
1139
 
1140
 
1141
 
1142
 
1143
 
1144
 
1145
 
1146
 
1147
 
1148
 
1149
 
1150
 
1151
 
1152
 
1153
 
1154
 
1155
 
1156
 
1157
 

powered by: WebSVN 2.1.0

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