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

Subversion Repositories apbi2c

[/] [apbi2c/] [trunk/] [rtl/] [module_i2c.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 2 redbear
//////////////////////////////////////////////////////////////////
2
////
3
////
4
////    TOP I2C BLOCK to I2C Core
5
////
6
////
7
////
8
//// This file is part of the APB to I2C project
9
////
10
//// http://www.opencores.org/cores/apbi2c/
11
////
12
////
13
////
14
//// Description
15
////
16
//// Implementation of APB IP core according to
17
////
18
//// apbi2c_spec IP core specification document.
19
////
20
////
21
////
22
//// To Do: Things are right here but always all block can suffer changes
23
////
24
////
25
////
26
////
27
////
28
//// Author(s): - Felipe Fernandes Da Costa, fefe2560@gmail.com
29 4 redbear
////              Ronal Dario Celaya ,rcelaya.dario@gmail.com
30 2 redbear
////
31
///////////////////////////////////////////////////////////////// 
32
////
33
////
34
//// Copyright (C) 2009 Authors and OPENCORES.ORG
35
////
36
////
37
////
38
//// This source file may be used and distributed without
39
////
40
//// restriction provided that this copyright statement is not
41
////
42
//// removed from the file and that any derivative work contains
43
//// the original copyright notice and the associated disclaimer.
44
////
45
////
46
//// This source file is free software; you can redistribute it
47
////
48
//// and/or modify it under the terms of the GNU Lesser General
49
////
50
//// Public License as published by the Free Software Foundation;
51
//// either version 2.1 of the License, or (at your option) any
52
////
53
//// later version.
54
////
55
////
56
////
57
//// This source is distributed in the hope that it will be
58
////
59
//// useful, but WITHOUT ANY WARRANTY; without even the implied
60
////
61
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
62
////
63
//// PURPOSE. See the GNU Lesser General Public License for more
64
//// details.
65
////
66
////
67
////
68
//// You should have received a copy of the GNU Lesser General
69
////
70
//// Public License along with this source; if not, download it
71
////
72
//// from http://www.opencores.org/lgpl.shtml
73
////
74
////
75
///////////////////////////////////////////////////////////////////
76
 
77
 
78
`timescale 1ns/1ps //timescale 
79
 
80
module module_i2c#(
81
                        //THIS IS USED ONLY LIKE PARAMETER TO BEM CONFIGURABLE
82
                        parameter integer DWIDTH = 32,
83
                        parameter integer AWIDTH = 14
84
                )
85
                (
86
                //I2C INTERFACE WITH ANOTHER BLOCKS
87
                 input PCLK,
88
                 input PRESETn,
89
 
90
                //INTERFACE WITH FIFO TRANSMISSION
91
                 input fifo_tx_f_full,
92
                 input fifo_tx_f_empty,
93
                 input [DWIDTH-1:0] fifo_tx_data_out,
94
 
95
                //INTERFACE WITH FIFO RECEIVER
96
                 input fifo_rx_f_full,
97
                 input fifo_rx_f_empty,
98 6 redbear
                 output reg fifo_rx_wr_en,
99
                 output reg [DWIDTH-1:0] fifo_rx_data_in,
100 2 redbear
 
101
                //INTERFACE WITH REGISTER CONFIGURATION
102
                 input [AWIDTH-1:0] DATA_CONFIG_REG,
103
 
104
                //INTERFACE TO APB AND READ FOR FIFO TX
105
                 output reg fifo_tx_rd_en,
106
                 output TX_EMPTY,
107
                 output RX_EMPTY,
108
                 output ERROR,
109
 
110
                //I2C BI DIRETIONAL PORTS
111
                inout SDA,
112
                inout SCL
113
 
114
 
115
                 );
116
 
117
//THIS IS USED TO GENERATE INTERRUPTIONS
118
assign TX_EMPTY = (fifo_tx_f_empty == 1'b1)? 1'b1:1'b0;
119
assign RX_EMPTY = (fifo_rx_f_empty == 1'b1)? 1'b1:1'b0;
120
 
121
        //THIS COUNT IS USED TO CONTROL DATA ACCROSS FSM        
122 6 redbear
        reg [1:0] count_tx;
123 2 redbear
        //CONTROL CLOCK AND COUNTER
124
        reg [11:0] count_send_data;
125
        reg BR_CLK_O;
126
        reg SDA_OUT;
127
 
128
        //RESPONSE USED TO HOLD SIGNAL TO ACK OR NACK
129
        reg RESPONSE;
130
 
131
// TX PARAMETERS USED TO STATE MACHINE
132
 
133
localparam [5:0] TX_IDLE = 6'd0, //IDLE
134
 
135
           TX_START = 6'd1,//START BIT
136
 
137
           TX_CONTROLIN_1 = 6'd2, //START BYTE
138
           TX_CONTROLIN_2 = 6'd3,
139
           TX_CONTROLIN_3 = 6'd4,
140
           TX_CONTROLIN_4 = 6'd5,
141
           TX_CONTROLIN_5 = 6'd6,
142
           TX_CONTROLIN_6 = 6'd7,
143
           TX_CONTROLIN_7 = 6'd8,
144
           TX_CONTROLIN_8 = 6'd9, //END FIRST BYTE
145
 
146
           TX_RESPONSE_CIN =6'd10, //RESPONSE
147
 
148
           TX_ADRESS_1 = 6'd11,//START BYTE
149
           TX_ADRESS_2 = 6'd12,
150
           TX_ADRESS_3 = 6'd13,
151
           TX_ADRESS_4 = 6'd14,
152
           TX_ADRESS_5 = 6'd15,
153
           TX_ADRESS_6 = 6'd16,
154
           TX_ADRESS_7 = 6'd17,
155
           TX_ADRESS_8 = 6'd18,//END FIRST BYTE
156
 
157
           TX_RESPONSE_ADRESS =6'd19, //RESPONSE
158
 
159
           TX_DATA0_1 = 6'd20,//START BYTE
160
           TX_DATA0_2 = 6'd21,
161
           TX_DATA0_3 = 6'd22,
162
           TX_DATA0_4 = 6'd23,
163
           TX_DATA0_5 = 6'd24,
164
           TX_DATA0_6 = 6'd25,
165
           TX_DATA0_7 = 6'd26,
166
           TX_DATA0_8 = 6'd27,//END FIRST BYTE
167
 
168
           TX_RESPONSE_DATA0_1 = 6'd28,  //RESPONSE
169
 
170
           TX_DATA1_1 = 6'd29,//START BYTE
171
           TX_DATA1_2 = 6'd30,
172
           TX_DATA1_3 = 6'd31,
173
           TX_DATA1_4 = 6'd32,
174
           TX_DATA1_5 = 6'd33,
175
           TX_DATA1_6 = 6'd34,
176
           TX_DATA1_7 = 6'd35,
177
           TX_DATA1_8 = 6'd36,//END FIRST BYTE
178
 
179
           TX_RESPONSE_DATA1_1 = 6'd37,//RESPONSE
180
 
181
           TX_DELAY_BYTES = 6'd38,//USED ONLY IN ACK TO DELAY BETWEEN
182
           TX_NACK = 6'd39,//USED ONLY IN ACK TO DELAY BETWEEN BYTES
183
           TX_STOP = 6'd40;//USED TO SEND STOP BIT
184
 
185
        //STATE CONTROL 
186
        reg [5:0] state_tx;
187
        reg [5:0] next_state_tx;
188
 
189
//ASSIGN REGISTERS TO BIDIRETIONAL PORTS
190
assign SDA = SDA_OUT;
191
assign SCL = BR_CLK_O;
192
 
193 4 redbear
//STANDARD ERROR
194
assign ERROR = (DATA_CONFIG_REG[0] == 1'b1 & DATA_CONFIG_REG[1] == 1'b1)?1'b1:1'b0;
195 2 redbear
 
196
//COMBINATIONAL BLOCK TO TX
197
always@(*)
198
begin
199
 
200
        //THE FUN START HERE :-)
201
        //COMBINATIONAL UPDATE STATE BE CAREFUL WITH WHAT YOU MAKE HERE
202
        next_state_tx = state_tx;
203
 
204
        case(state_tx)//STATE_TX IS MORE SECURE CHANGE ONLY IF YOU KNOW WHAT ARE YOU DOING 
205
        TX_IDLE:
206
        begin
207
                //OBEYING SPEC
208
                if(DATA_CONFIG_REG[0] == 1'b0 && fifo_tx_f_full == 1'b0 && DATA_CONFIG_REG[1] == 1'b0)
209
                begin
210
                        next_state_tx = TX_IDLE;
211
                end
212 4 redbear
                else if(DATA_CONFIG_REG[0] == 1'b1 && fifo_tx_f_full == 1'b1 && DATA_CONFIG_REG[1] == 1'b1)
213 2 redbear
                begin
214 4 redbear
                        next_state_tx = TX_IDLE;
215
                end
216
                else if(DATA_CONFIG_REG[0] == 1'b1 && fifo_tx_f_full == 1'b1 && DATA_CONFIG_REG[1] == 1'b0)
217
                begin
218 2 redbear
                        next_state_tx = TX_START;
219
                end
220
 
221
 
222
        end
223
        TX_START://THIS IS USED TOO ALL STATE MACHINES THE COUNTER_SEND_DATA
224
        begin
225
                if(count_send_data != DATA_CONFIG_REG[13:2])
226
                begin
227
                        next_state_tx = TX_START;
228
                end
229
                else
230
                begin
231
                        next_state_tx = TX_CONTROLIN_1;
232
                end
233
 
234
        end
235
        TX_CONTROLIN_1:
236
        begin
237
                if(count_send_data != DATA_CONFIG_REG[13:2])
238
                begin
239
                        next_state_tx = TX_CONTROLIN_1;
240
                end
241
                else
242
                begin
243
                        next_state_tx = TX_CONTROLIN_2;
244
                end
245
 
246
        end
247
        TX_CONTROLIN_2:
248
        begin
249
 
250
                if(count_send_data != DATA_CONFIG_REG[13:2])
251
                begin
252
                        next_state_tx =TX_CONTROLIN_2;
253
                end
254
                else
255
                begin
256
                        next_state_tx = TX_CONTROLIN_3;
257
                end
258
 
259
        end
260
        TX_CONTROLIN_3:
261
        begin
262
 
263
                if(count_send_data != DATA_CONFIG_REG[13:2])
264
                begin
265
                        next_state_tx = TX_CONTROLIN_3;
266
                end
267
                else
268
                begin
269
                        next_state_tx = TX_CONTROLIN_4;
270
                end
271
        end
272
        TX_CONTROLIN_4:
273
        begin
274
 
275
                if(count_send_data != DATA_CONFIG_REG[13:2])
276
                begin
277
                        next_state_tx = TX_CONTROLIN_4;
278
                end
279
                else
280
                begin
281
                        next_state_tx = TX_CONTROLIN_5;
282
                end
283
        end
284
        TX_CONTROLIN_5:
285
        begin
286
 
287
                if(count_send_data != DATA_CONFIG_REG[13:2])
288
                begin
289
                        next_state_tx = TX_CONTROLIN_5;
290
                end
291
                else
292
                begin
293
                        next_state_tx = TX_CONTROLIN_6;
294
                end
295
        end
296
        TX_CONTROLIN_6:
297
        begin
298
 
299
                if(count_send_data != DATA_CONFIG_REG[13:2])
300
                begin
301
                        next_state_tx = TX_CONTROLIN_6;
302
                end
303
                else
304
                begin
305
                        next_state_tx = TX_CONTROLIN_7;
306
                end
307
        end
308
        TX_CONTROLIN_7:
309
        begin
310
 
311
                if(count_send_data != DATA_CONFIG_REG[13:2])
312
                begin
313
                        next_state_tx = TX_CONTROLIN_7;
314
                end
315
                else
316
                begin
317
                        next_state_tx = TX_CONTROLIN_8;
318
                end
319
        end
320
        TX_CONTROLIN_8:
321
        begin
322
 
323
                if(count_send_data != DATA_CONFIG_REG[13:2])
324
                begin
325
                        next_state_tx = TX_CONTROLIN_8;
326
                end
327
                else
328
                begin
329
                        next_state_tx = TX_RESPONSE_CIN;
330
                end
331
        end
332
        TX_RESPONSE_CIN:
333
        begin
334
 
335
                if(count_send_data != DATA_CONFIG_REG[13:2])
336
                begin
337
                        next_state_tx = TX_RESPONSE_CIN;
338
                end
339
                else if(RESPONSE == 1'b0)//ACK
340
                begin
341
                        next_state_tx = TX_DELAY_BYTES;
342
                end
343
                else if(RESPONSE == 1'b1)//NACK
344
                begin
345
                        next_state_tx = TX_NACK;
346
                end
347
 
348
        end
349
 
350
        //NOW SENDING ADDRESS
351
        TX_ADRESS_1:
352
        begin
353
                if(count_send_data != DATA_CONFIG_REG[13:2])
354
                begin
355
                        next_state_tx = TX_ADRESS_1;
356
                end
357
                else
358
                begin
359
                        next_state_tx = TX_ADRESS_2;
360
                end
361
        end
362
        TX_ADRESS_2:
363
        begin
364
                if(count_send_data != DATA_CONFIG_REG[13:2])
365
                begin
366
                        next_state_tx = TX_ADRESS_2;
367
                end
368
                else
369
                begin
370
                        next_state_tx = TX_ADRESS_3;
371
                end
372
        end
373
        TX_ADRESS_3:
374
        begin
375
                if(count_send_data != DATA_CONFIG_REG[13:2])
376
                begin
377
                        next_state_tx = TX_ADRESS_3;
378
                end
379
                else
380
                begin
381
                        next_state_tx = TX_ADRESS_4;
382
                end
383
        end
384
        TX_ADRESS_4:
385
        begin
386
                if(count_send_data != DATA_CONFIG_REG[13:2])
387
                begin
388
                        next_state_tx = TX_ADRESS_4;
389
                end
390
                else
391
                begin
392
                        next_state_tx = TX_ADRESS_5;
393
                end
394
        end
395
        TX_ADRESS_5:
396
        begin
397
                if(count_send_data != DATA_CONFIG_REG[13:2])
398
                begin
399
                        next_state_tx = TX_ADRESS_5;
400
                end
401
                else
402
                begin
403
                        next_state_tx = TX_ADRESS_6;
404
                end
405
        end
406
        TX_ADRESS_6:
407
        begin
408
                if(count_send_data != DATA_CONFIG_REG[13:2])
409
                begin
410
                        next_state_tx = TX_ADRESS_6;
411
                end
412
                else
413
                begin
414
                        next_state_tx = TX_ADRESS_7;
415
                end
416
        end
417
        TX_ADRESS_7:
418
        begin
419
                if(count_send_data != DATA_CONFIG_REG[13:2])
420
                begin
421
                        next_state_tx = TX_ADRESS_7;
422
                end
423
                else
424
                begin
425
                        next_state_tx = TX_ADRESS_8;
426
                end
427
        end
428
        TX_ADRESS_8:
429
        begin
430
                if(count_send_data != DATA_CONFIG_REG[13:2])
431
                begin
432
                        next_state_tx = TX_ADRESS_8;
433
                end
434
                else
435
                begin
436
                        next_state_tx = TX_RESPONSE_ADRESS;
437
                end
438
        end
439
        TX_RESPONSE_ADRESS:
440
        begin
441
                if(count_send_data != DATA_CONFIG_REG[13:2])
442
                begin
443
                        next_state_tx = TX_RESPONSE_ADRESS;
444
                end
445
                else if(RESPONSE == 1'b0)//ACK
446
                begin
447
                        next_state_tx = TX_DELAY_BYTES;
448
                end
449
                else if(RESPONSE == 1'b1)//NACK --> RESTART CONDITION AND BACK TO START BYTE AGAIN
450
                begin
451
                        next_state_tx = TX_NACK;
452
                end
453
        end
454
 
455
        //data in
456
        TX_DATA0_1:
457
        begin
458
                if(count_send_data != DATA_CONFIG_REG[13:2])
459
                begin
460
                        next_state_tx = TX_DATA0_1;
461
                end
462
                else
463
                begin
464
                        next_state_tx = TX_DATA0_2;
465
                end
466
        end
467
        TX_DATA0_2:
468
        begin
469
                if(count_send_data != DATA_CONFIG_REG[13:2])
470
                begin
471
                        next_state_tx = TX_DATA0_2;
472
                end
473
                else
474
                begin
475
                        next_state_tx = TX_DATA0_3;
476
                end
477
        end
478
        TX_DATA0_3:
479
        begin
480
                if(count_send_data != DATA_CONFIG_REG[13:2])
481
                begin
482
                        next_state_tx = TX_DATA0_3;
483
                end
484
                else
485
                begin
486
                        next_state_tx = TX_DATA0_4;
487
                end
488
        end
489
        TX_DATA0_4:
490
        begin
491
                if(count_send_data != DATA_CONFIG_REG[13:2])
492
                begin
493
                        next_state_tx = TX_DATA0_4;
494
                end
495
                else
496
                begin
497
                        next_state_tx = TX_DATA0_5;
498
                end
499
        end
500
        TX_DATA0_5:
501
        begin
502
                if(count_send_data != DATA_CONFIG_REG[13:2])
503
                begin
504
                        next_state_tx = TX_DATA0_5;
505
                end
506
                else
507
                begin
508
                        next_state_tx = TX_DATA0_6;
509
                end
510
        end
511
        TX_DATA0_6:
512
        begin
513
                if(count_send_data != DATA_CONFIG_REG[13:2])
514
                begin
515
                        next_state_tx = TX_DATA0_6;
516
                end
517
                else
518
                begin
519
                        next_state_tx = TX_DATA0_7;
520
                end
521
        end
522
        TX_DATA0_7:
523
        begin
524
                if(count_send_data != DATA_CONFIG_REG[13:2])
525
                begin
526
                        next_state_tx = TX_DATA0_7;
527
                end
528
                else
529
                begin
530
                        next_state_tx = TX_DATA0_8;
531
                end
532
        end
533
        TX_DATA0_8:
534
        begin
535
                if(count_send_data != DATA_CONFIG_REG[13:2])
536
                begin
537
                        next_state_tx = TX_DATA0_8;
538
                end
539
                else
540
                begin
541
                        next_state_tx = TX_RESPONSE_DATA0_1;
542
                end
543
        end
544
        TX_RESPONSE_DATA0_1:
545
        begin
546
                if(count_send_data != DATA_CONFIG_REG[13:2])
547
                begin
548
                        next_state_tx = TX_RESPONSE_DATA0_1;
549
                end
550
                else if(RESPONSE == 1'b0)//ACK
551
                begin
552
                        next_state_tx = TX_DELAY_BYTES;
553
                end
554
                else if(RESPONSE == 1'b1)//NACK
555
                begin
556
                        next_state_tx = TX_NACK;
557
                end
558
        end
559
 
560
        //second byte
561
        TX_DATA1_1:
562
        begin
563
                if(count_send_data != DATA_CONFIG_REG[13:2])
564
                begin
565
                        next_state_tx = TX_DATA1_1;
566
                end
567
                else
568
                begin
569
                        next_state_tx = TX_DATA1_2;
570
                end
571
        end
572
        TX_DATA1_2:
573
        begin
574
                if(count_send_data != DATA_CONFIG_REG[13:2])
575
                begin
576
                        next_state_tx = TX_DATA1_2;
577
                end
578
                else
579
                begin
580
                        next_state_tx = TX_DATA1_3;
581
                end
582
        end
583
        TX_DATA1_3:
584
        begin
585
                if(count_send_data != DATA_CONFIG_REG[13:2])
586
                begin
587
                        next_state_tx = TX_DATA1_3;
588
                end
589
                else
590
                begin
591
                        next_state_tx = TX_DATA1_4;
592
                end
593
        end
594
        TX_DATA1_4:
595
        begin
596
                if(count_send_data != DATA_CONFIG_REG[13:2])
597
                begin
598
                        next_state_tx = TX_DATA1_4;
599
                end
600
                else
601
                begin
602
                        next_state_tx = TX_DATA1_5;
603
                end
604
        end
605
        TX_DATA1_5:
606
        begin
607
                if(count_send_data != DATA_CONFIG_REG[13:2])
608
                begin
609
                        next_state_tx = TX_DATA1_5;
610
                end
611
                else
612
                begin
613
                        next_state_tx = TX_DATA1_6;
614
                end
615
        end
616
        TX_DATA1_6:
617
        begin
618
                if(count_send_data != DATA_CONFIG_REG[13:2])
619
                begin
620
                        next_state_tx = TX_DATA1_6;
621
                end
622
                else
623
                begin
624
                        next_state_tx = TX_DATA1_7;
625
                end
626
        end
627
        TX_DATA1_7:
628
        begin
629
                if(count_send_data != DATA_CONFIG_REG[13:2])
630
                begin
631
                        next_state_tx = TX_DATA1_7;
632
                end
633
                else
634
                begin
635
                        next_state_tx = TX_DATA1_8;
636
                end
637
        end
638
        TX_DATA1_8:
639
        begin
640
                if(count_send_data != DATA_CONFIG_REG[13:2])
641
                begin
642
                        next_state_tx = TX_DATA1_8;
643
                end
644
                else
645
                begin
646
                        next_state_tx = TX_RESPONSE_DATA1_1;
647
                end
648
        end
649
        TX_RESPONSE_DATA1_1:
650
        begin
651
                if(count_send_data != DATA_CONFIG_REG[13:2])
652
                begin
653
                        next_state_tx = TX_RESPONSE_DATA1_1;
654
                end
655
                else if(RESPONSE == 1'b0)//ACK
656
                begin
657
                        next_state_tx = TX_DELAY_BYTES;
658
                end
659
                else if(RESPONSE == 1'b1)//NACK
660
                begin
661
                        next_state_tx = TX_NACK;
662
                end
663
        end
664
        TX_DELAY_BYTES://THIS FORM WORKS 
665
        begin
666
 
667
 
668
                if(count_send_data != DATA_CONFIG_REG[13:2])
669
                begin
670
                        next_state_tx = TX_DELAY_BYTES;
671
                end
672
                else
673
                begin
674
 
675 6 redbear
                        if(count_tx == 2'd0)
676 2 redbear
                        begin
677
                                next_state_tx = TX_ADRESS_1;
678
                        end
679 6 redbear
                        else if(count_tx == 2'd1)
680 2 redbear
                        begin
681
                                next_state_tx = TX_DATA0_1;
682
                        end
683 6 redbear
                        else if(count_tx == 2'd2)
684 2 redbear
                        begin
685
                                next_state_tx = TX_DATA1_1;
686
                        end
687 6 redbear
                        else if(count_tx == 2'd3)
688 2 redbear
                        begin
689
                                next_state_tx = TX_STOP;
690
                        end
691
 
692
                end
693
 
694
        end
695
        TX_NACK://NOT TESTED YET !!!!
696
        begin
697
                if(count_send_data != DATA_CONFIG_REG[13:2]*2'd2)
698
                begin
699
                        next_state_tx = TX_NACK;
700
                end
701
                else
702
                begin
703 6 redbear
                        if(count_tx == 2'd0)
704 2 redbear
                        begin
705
                                next_state_tx = TX_CONTROLIN_1;
706
                        end
707 6 redbear
                        else if(count_tx == 2'd1)
708 2 redbear
                        begin
709
                                next_state_tx = TX_ADRESS_1;
710
                        end
711 6 redbear
                        else if(count_tx == 2'd2)
712 2 redbear
                        begin
713
                                next_state_tx = TX_DATA0_1;
714
                        end
715 6 redbear
                        else if(count_tx == 2'd3)
716 2 redbear
                        begin
717
                                next_state_tx = TX_DATA1_1;
718
                        end
719
                end
720
        end
721
        TX_STOP://THIS WORK
722
        begin
723
                if(count_send_data != DATA_CONFIG_REG[13:2])
724
                begin
725
                        next_state_tx = TX_STOP;
726
                end
727
                else
728
                begin
729
                        next_state_tx = TX_IDLE;
730
                end
731
        end
732
        default:
733
        begin
734
                next_state_tx = TX_IDLE;
735
        end
736
        endcase
737
 
738
 
739
end
740 6 redbear
//SEQUENTIAL TX
741 2 redbear
always@(posedge PCLK)
742
begin
743
 
744
        //RESET SYNC
745
        if(!PRESETn)
746
        begin
747
                //SIGNALS MUST BE RESETED
748
                count_send_data <= 12'd0;
749
                state_tx <= TX_IDLE;
750
                SDA_OUT<= 1'b1;
751
                fifo_tx_rd_en <= 1'b0;
752 6 redbear
                count_tx <= 2'd0;
753 2 redbear
                BR_CLK_O <= 1'b1;
754
                RESPONSE<= 1'b0;
755
        end
756
        else
757
        begin
758
 
759
                // SEQUENTIAL FUN START
760
                state_tx <= next_state_tx;
761
 
762
                case(state_tx)
763
                TX_IDLE:
764
                begin
765
 
766
                        fifo_tx_rd_en <= 1'b0;
767
 
768
 
769 4 redbear
                        if(DATA_CONFIG_REG[0] == 1'b0 && fifo_tx_f_full == 1'b0 && DATA_CONFIG_REG[1] == 1'b0)
770 2 redbear
                        begin
771
                                count_send_data <= 12'd0;
772
                                SDA_OUT<= 1'b1;
773
                                BR_CLK_O <= 1'b1;
774
                        end
775 4 redbear
                        else if(DATA_CONFIG_REG[0] == 1'b1 && fifo_tx_f_full == 1'b1 && DATA_CONFIG_REG[1] == 1'b0)
776 2 redbear
                        begin
777
                                count_send_data <= count_send_data + 12'd1;
778
                                SDA_OUT<=1'b0;
779 4 redbear
                        end
780
                        else if(DATA_CONFIG_REG[0] == 1'b1 && fifo_tx_f_full == 1'b1 && DATA_CONFIG_REG[1] == 1'b1)
781
                        begin
782
                                count_send_data <= 12'd0;
783
                                SDA_OUT<= 1'b1;
784
                                BR_CLK_O <= 1'b1;
785 2 redbear
                        end
786
 
787
                end
788
                TX_START:
789
                begin
790
 
791
                        if(count_send_data < DATA_CONFIG_REG[13:2])
792
                        begin
793
                                count_send_data <= count_send_data + 12'd1;
794
                                BR_CLK_O <= 1'b0;
795
                        end
796
                        else
797
                        begin
798
                                count_send_data <= 12'd0;
799
                                BR_CLK_O <= 1'b1;
800
                        end
801
 
802
                        if(count_send_data == DATA_CONFIG_REG[13:2]- 12'd1)
803
                        begin
804 6 redbear
                                SDA_OUT<=fifo_tx_data_out[0:0];
805
                                count_tx <= 2'd0;
806 2 redbear
                        end
807
 
808
                end
809
                TX_CONTROLIN_1:
810
                begin
811
 
812
 
813
 
814
                        if(count_send_data < DATA_CONFIG_REG[13:2])
815
                        begin
816
 
817
                                count_send_data <= count_send_data + 12'd1;
818
                                SDA_OUT<=fifo_tx_data_out[0:0];
819
 
820
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
821
                                begin
822
                                        BR_CLK_O <= 1'b1;
823
                                end
824
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
825
                                begin
826
                                        BR_CLK_O <= 1'b0;
827
                                end
828
                        end
829
                        else
830
                        begin
831
                                count_send_data <= 12'd0;
832
                                BR_CLK_O <= 1'b1;
833
                                SDA_OUT<=fifo_tx_data_out[1:1];
834
                        end
835
 
836
 
837
                end
838
 
839
                TX_CONTROLIN_2:
840
                begin
841
 
842
 
843
 
844
                        if(count_send_data < DATA_CONFIG_REG[13:2])
845
                        begin
846
                                count_send_data <= count_send_data + 12'd1;
847
                                SDA_OUT<=fifo_tx_data_out[1:1];
848
 
849
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
850
                                begin
851
                                        BR_CLK_O <= 1'b1;
852
                                end
853
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
854
                                begin
855
                                        BR_CLK_O <= 1'b0;
856
                                end
857
                        end
858
                        else
859
                        begin
860
                                count_send_data <= 12'd0;
861
                                BR_CLK_O <= 1'b1;
862
                                SDA_OUT<=fifo_tx_data_out[2:2];
863
                        end
864
 
865
                end
866
 
867
                TX_CONTROLIN_3:
868
                begin
869
 
870
 
871
 
872
                        if(count_send_data < DATA_CONFIG_REG[13:2])
873
                        begin
874
                                count_send_data <= count_send_data + 12'd1;
875
                                SDA_OUT<=fifo_tx_data_out[2:2];
876
 
877
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
878
                                begin
879
                                        BR_CLK_O <= 1'b1;
880
                                end
881
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
882
                                begin
883
                                        BR_CLK_O <= 1'b0;
884
                                end
885
                        end
886
                        else
887
                        begin
888
                                count_send_data <= 12'd0;
889
                                BR_CLK_O <= 1'b1;
890
                                SDA_OUT<=fifo_tx_data_out[3:3];
891
                        end
892
 
893
 
894
 
895
                end
896
                TX_CONTROLIN_4:
897
                begin
898
 
899
 
900
 
901
                        if(count_send_data < DATA_CONFIG_REG[13:2])
902
                        begin
903
                                count_send_data <= count_send_data + 12'd1;
904
                                SDA_OUT<=fifo_tx_data_out[3:3];
905
 
906
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
907
                                begin
908
                                        BR_CLK_O <= 1'b1;
909
                                end
910
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
911
                                begin
912
                                        BR_CLK_O <= 1'b0;
913
                                end
914
                        end
915
                        else
916
                        begin
917
                                count_send_data <= 12'd0;
918
                                BR_CLK_O <= 1'b1;
919
                                SDA_OUT<=fifo_tx_data_out[4:4];
920
                        end
921
 
922
                end
923
 
924
                TX_CONTROLIN_5:
925
                begin
926
 
927
 
928
 
929
                        if(count_send_data < DATA_CONFIG_REG[13:2])
930
                        begin
931
                                count_send_data <= count_send_data + 12'd1;
932
                                SDA_OUT<=fifo_tx_data_out[4:4];
933
 
934
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
935
                                begin
936
                                        BR_CLK_O <= 1'b1;
937
                                end
938
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
939
                                begin
940
                                        BR_CLK_O <= 1'b0;
941
                                end
942
                        end
943
                        else
944
                        begin
945
                                count_send_data <= 12'd0;
946
                                BR_CLK_O <= 1'b1;
947
                                SDA_OUT<=fifo_tx_data_out[5:5];
948
                        end
949
 
950
                end
951
 
952
 
953
                TX_CONTROLIN_6:
954
                begin
955
 
956
                        if(count_send_data < DATA_CONFIG_REG[13:2])
957
                        begin
958
                                count_send_data <= count_send_data + 12'd1;
959
                                SDA_OUT<=fifo_tx_data_out[5:5];
960
 
961
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
962
                                begin
963
                                        BR_CLK_O <= 1'b1;
964
                                end
965
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
966
                                begin
967
                                        BR_CLK_O <= 1'b0;
968
                                end
969
                        end
970
                        else
971
                        begin
972
                                count_send_data <= 12'd0;
973
                                BR_CLK_O <= 1'b1;
974
                                SDA_OUT<=fifo_tx_data_out[6:6];
975
                        end
976
 
977
 
978
                end
979
 
980
                TX_CONTROLIN_7:
981
                begin
982
 
983
                        if(count_send_data < DATA_CONFIG_REG[13:2])
984
                        begin
985
                                count_send_data <= count_send_data + 12'd1;
986
                                SDA_OUT<=fifo_tx_data_out[6:6];
987
 
988
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
989
                                begin
990
                                        BR_CLK_O <= 1'b1;
991
                                end
992
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
993
                                begin
994
                                        BR_CLK_O <= 1'b0;
995
                                end
996
                        end
997
                        else
998
                        begin
999
                                count_send_data <= 12'd0;
1000
                                BR_CLK_O <= 1'b1;
1001
                                SDA_OUT<=fifo_tx_data_out[7:7];
1002
                        end
1003
 
1004
 
1005
                end
1006
                TX_CONTROLIN_8:
1007
                begin
1008
 
1009
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1010
                        begin
1011
                                count_send_data <= count_send_data + 12'd1;
1012
                                SDA_OUT<=fifo_tx_data_out[7:7];
1013
 
1014
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1015
                                begin
1016
                                        BR_CLK_O <= 1'b1;
1017
                                end
1018
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1019
                                begin
1020
                                        BR_CLK_O <= 1'b0;
1021
                                end
1022
                        end
1023
                        else
1024
                        begin
1025
                                count_send_data <= 12'd0;
1026
                                BR_CLK_O <= 1'b1;
1027
                                SDA_OUT<= 1'b0;
1028
                        end
1029
 
1030
 
1031
                end
1032
                TX_RESPONSE_CIN:
1033
                begin
1034
 
1035
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1036
                        begin
1037
                                count_send_data <= count_send_data + 12'd1;
1038
 
1039
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1040
                                RESPONSE<= SDA;
1041
 
1042
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1043
                                begin
1044
                                        BR_CLK_O <= 1'b1;
1045
                                end
1046
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1047
                                begin
1048
                                        BR_CLK_O <= 1'b0;
1049
                                end
1050
                        end
1051
                        else
1052
                        begin
1053
                                count_send_data <= 12'd0;
1054
                        end
1055
 
1056
 
1057
                end
1058
                TX_ADRESS_1:
1059
                begin
1060
 
1061
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1062
                        begin
1063
                                count_send_data <= count_send_data + 12'd1;
1064
                                SDA_OUT<=fifo_tx_data_out[8:8];
1065
 
1066
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1067
                                begin
1068
                                        BR_CLK_O <= 1'b1;
1069
                                end
1070
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1071
                                begin
1072
                                        BR_CLK_O <= 1'b0;
1073
                                end
1074
                        end
1075
                        else
1076
                        begin
1077
                                count_send_data <= 12'd0;
1078
                                BR_CLK_O <= 1'b1;
1079
                                SDA_OUT<=fifo_tx_data_out[9:9];
1080
                        end
1081
 
1082
                end
1083
                TX_ADRESS_2:
1084
                begin
1085
 
1086
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1087
                        begin
1088
                                count_send_data <= count_send_data + 12'd1;
1089
                                SDA_OUT<=fifo_tx_data_out[9:9];
1090
 
1091
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1092
                                begin
1093
                                        BR_CLK_O <= 1'b1;
1094
                                end
1095
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1096
                                begin
1097
                                        BR_CLK_O <= 1'b0;
1098
                                end
1099
                        end
1100
                        else
1101
                        begin
1102
                                count_send_data <= 12'd0;
1103
                                BR_CLK_O <= 1'b1;
1104
                                SDA_OUT<=fifo_tx_data_out[10:10];
1105
                        end
1106
 
1107
                end
1108
                TX_ADRESS_3:
1109
                begin
1110
 
1111
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1112
                        begin
1113
                                count_send_data <= count_send_data + 12'd1;
1114
                                SDA_OUT<=fifo_tx_data_out[10:10];
1115
 
1116
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1117
                                begin
1118
                                        BR_CLK_O <= 1'b1;
1119
                                end
1120
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1121
                                begin
1122
                                        BR_CLK_O <= 1'b0;
1123
                                end
1124
                        end
1125
                        else
1126
                        begin
1127
                                count_send_data <= 12'd0;
1128
                                BR_CLK_O <= 1'b1;
1129
                                SDA_OUT<=fifo_tx_data_out[11:11];
1130
                        end
1131
 
1132
                end
1133
                TX_ADRESS_4:
1134
                begin
1135
 
1136
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1137
                        begin
1138
                                count_send_data <= count_send_data + 12'd1;
1139
                                SDA_OUT<=fifo_tx_data_out[11:11];
1140
 
1141
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1142
                                begin
1143
                                        BR_CLK_O <= 1'b1;
1144
                                end
1145
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1146
                                begin
1147
                                        BR_CLK_O <= 1'b0;
1148
                                end
1149
                        end
1150
                        else
1151
                        begin
1152
                                count_send_data <= 12'd0;
1153
                                BR_CLK_O <= 1'b1;
1154
                                SDA_OUT<=fifo_tx_data_out[12:12];
1155
                        end
1156
                end
1157
                TX_ADRESS_5:
1158
                begin
1159
 
1160
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1161
                        begin
1162
                                count_send_data <= count_send_data + 12'd1;
1163
                                SDA_OUT<=fifo_tx_data_out[12:12];
1164
 
1165
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1166
                                begin
1167
                                        BR_CLK_O <= 1'b1;
1168
                                end
1169
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1170
                                begin
1171
                                        BR_CLK_O <= 1'b0;
1172
                                end
1173
                        end
1174
                        else
1175
                        begin
1176
                                count_send_data <= 12'd0;
1177
                                BR_CLK_O <= 1'b1;
1178
                                SDA_OUT<=fifo_tx_data_out[13:13];
1179
                        end
1180
 
1181
 
1182
                end
1183
                TX_ADRESS_6:
1184
                begin
1185
 
1186
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1187
                        begin
1188
                                count_send_data <= count_send_data + 12'd1;
1189
                                SDA_OUT<=fifo_tx_data_out[13:13];
1190
 
1191
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1192
                                begin
1193
                                        BR_CLK_O <= 1'b1;
1194
                                end
1195
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1196
                                begin
1197
                                        BR_CLK_O <= 1'b0;
1198
                                end
1199
                        end
1200
                        else
1201
                        begin
1202
                                count_send_data <= 12'd0;
1203
                                BR_CLK_O <= 1'b1;
1204
                                SDA_OUT<=fifo_tx_data_out[14:14];
1205
                        end
1206
 
1207
                end
1208
                TX_ADRESS_7:
1209
                begin
1210
 
1211
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1212
                        begin
1213
                                count_send_data <= count_send_data + 12'd1;
1214
                                SDA_OUT<=fifo_tx_data_out[14:14];
1215
 
1216
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1217
                                begin
1218
                                        BR_CLK_O <= 1'b1;
1219
                                end
1220
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1221
                                begin
1222
                                        BR_CLK_O <= 1'b0;
1223
                                end
1224
                        end
1225
                        else
1226
                        begin
1227
                                count_send_data <= 12'd0;
1228
                                BR_CLK_O <= 1'b1;
1229
                                SDA_OUT<=fifo_tx_data_out[15:15];
1230
                        end
1231
 
1232
 
1233
                end
1234
                TX_ADRESS_8:
1235
                begin
1236
 
1237
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1238
                        begin
1239
                                count_send_data <= count_send_data + 12'd1;
1240
                                SDA_OUT<=fifo_tx_data_out[15:15];
1241
 
1242
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1243
                                begin
1244
                                        BR_CLK_O <= 1'b1;
1245
                                end
1246
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1247
                                begin
1248
                                        BR_CLK_O <= 1'b0;
1249
                                end
1250
                        end
1251
                        else
1252
                        begin
1253
                                count_send_data <= 12'd0;
1254
                                BR_CLK_O <= 1'b1;
1255
                        end
1256
 
1257
                end
1258
                TX_RESPONSE_ADRESS:
1259
                begin
1260
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1261
                        begin
1262
                                count_send_data <= count_send_data + 12'd1;
1263
 
1264
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1265
                                RESPONSE<= SDA;
1266
 
1267
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1268
                                begin
1269
                                        BR_CLK_O <= 1'b1;
1270
                                end
1271
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1272
                                begin
1273
                                        BR_CLK_O <= 1'b0;
1274
                                end
1275
                        end
1276
                        else
1277
                        begin
1278
                                count_send_data <= 12'd0;
1279
                        end
1280
 
1281
                end
1282
                TX_DATA0_1:
1283
                begin
1284
 
1285
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1286
                        begin
1287
                                count_send_data <= count_send_data + 12'd1;
1288
                                SDA_OUT<=fifo_tx_data_out[16:16];
1289
 
1290
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1291
                                begin
1292
                                        BR_CLK_O <= 1'b1;
1293
                                end
1294
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1295
                                begin
1296
                                        BR_CLK_O <= 1'b0;
1297
                                end
1298
                        end
1299
                        else
1300
                        begin
1301
                                count_send_data <= 12'd0;
1302
                                BR_CLK_O <= 1'b1;
1303
                                SDA_OUT<=fifo_tx_data_out[17:17];
1304
                        end
1305
 
1306
 
1307
                end
1308
                TX_DATA0_2:
1309
                begin
1310
 
1311
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1312
                        begin
1313
                                count_send_data <= count_send_data + 12'd1;
1314
                                SDA_OUT<=fifo_tx_data_out[17:17];
1315
 
1316
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1317
                                begin
1318
                                        BR_CLK_O <= 1'b1;
1319
                                end
1320
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1321
                                begin
1322
                                        BR_CLK_O <= 1'b0;
1323
                                end
1324
                        end
1325
                        else
1326
                        begin
1327
                                count_send_data <= 12'd0;
1328
                                BR_CLK_O <= 1'b1;
1329
                                SDA_OUT<=fifo_tx_data_out[18:18];
1330
                        end
1331
 
1332
 
1333
                end
1334
                TX_DATA0_3:
1335
                begin
1336
 
1337
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1338
                        begin
1339
                                count_send_data <= count_send_data + 12'd1;
1340
                                SDA_OUT<=fifo_tx_data_out[18:18];
1341
 
1342
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1343
                                begin
1344
                                        BR_CLK_O <= 1'b1;
1345
                                end
1346
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1347
                                begin
1348
                                        BR_CLK_O <= 1'b0;
1349
                                end
1350
                        end
1351
                        else
1352
                        begin
1353
                                count_send_data <= 12'd0;
1354
                                BR_CLK_O <= 1'b1;
1355
                                SDA_OUT<=fifo_tx_data_out[19:19];
1356
                        end
1357
 
1358
                end
1359
                TX_DATA0_4:
1360
                begin
1361
 
1362
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1363
                        begin
1364
                                count_send_data <= count_send_data + 12'd1;
1365
                                SDA_OUT<=fifo_tx_data_out[19:19];
1366
 
1367
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1368
                                begin
1369
                                        BR_CLK_O <= 1'b1;
1370
                                end
1371
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1372
                                begin
1373
                                        BR_CLK_O <= 1'b0;
1374
                                end
1375
                        end
1376
                        else
1377
                        begin
1378
                                count_send_data <= 12'd0;
1379
                                BR_CLK_O <= 1'b1;
1380
                                SDA_OUT<=fifo_tx_data_out[20:20];
1381
                        end
1382
 
1383
                end
1384
                TX_DATA0_5:
1385
                begin
1386
 
1387
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1388
                        begin
1389
                                count_send_data <= count_send_data + 12'd1;
1390
                                SDA_OUT<=fifo_tx_data_out[20:20];
1391
 
1392
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1393
                                begin
1394
                                        BR_CLK_O <= 1'b1;
1395
                                end
1396
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1397
                                begin
1398
                                        BR_CLK_O <= 1'b0;
1399
                                end
1400
                        end
1401
                        else
1402
                        begin
1403
                                count_send_data <= 12'd0;
1404
                                BR_CLK_O <= 1'b1;
1405
                                SDA_OUT<=fifo_tx_data_out[21:21];
1406
                        end
1407
 
1408
                end
1409
                TX_DATA0_6:
1410
                begin
1411
 
1412
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1413
                        begin
1414
                                count_send_data <= count_send_data + 12'd1;
1415
                                SDA_OUT<=fifo_tx_data_out[21:21];
1416
 
1417
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1418
                                begin
1419
                                        BR_CLK_O <= 1'b1;
1420
                                end
1421
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1422
                                begin
1423
                                        BR_CLK_O <= 1'b0;
1424
                                end
1425
                        end
1426
                        else
1427
                        begin
1428
                                count_send_data <= 12'd0;
1429
                                BR_CLK_O <= 1'b1;
1430
                                SDA_OUT<=fifo_tx_data_out[22:22];
1431
                        end
1432
 
1433
                end
1434
                TX_DATA0_7:
1435
                begin
1436
 
1437
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1438
                        begin
1439
                                count_send_data <= count_send_data + 12'd1;
1440
                                SDA_OUT<=fifo_tx_data_out[22:22];
1441
 
1442
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1443
                                begin
1444
                                        BR_CLK_O <= 1'b1;
1445
                                end
1446
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1447
                                begin
1448
                                        BR_CLK_O <= 1'b0;
1449
                                end
1450
                        end
1451
                        else
1452
                        begin
1453
                                count_send_data <= 12'd0;
1454
                                BR_CLK_O <= 1'b1;
1455
                                SDA_OUT<=fifo_tx_data_out[23:23];
1456
                        end
1457
 
1458
                end
1459
                TX_DATA0_8:
1460
                begin
1461
 
1462
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1463
                        begin
1464
                                count_send_data <= count_send_data + 12'd1;
1465
                                SDA_OUT<=fifo_tx_data_out[23:23];
1466
 
1467
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1468
                                begin
1469
                                        BR_CLK_O <= 1'b1;
1470
                                end
1471
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1472
                                begin
1473
                                        BR_CLK_O <= 1'b0;
1474
                                end
1475
 
1476
                        end
1477
                        else
1478
                        begin
1479
                                count_send_data <= 12'd0;
1480
                                BR_CLK_O <= 1'b1;
1481
                        end
1482
 
1483
                end
1484
                TX_RESPONSE_DATA0_1:
1485
                begin
1486
 
1487
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1488
                        begin
1489
                                count_send_data <= count_send_data + 12'd1;
1490
 
1491
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1492
                                RESPONSE<= SDA;
1493
 
1494
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1495
                                begin
1496
                                        BR_CLK_O <= 1'b1;
1497
                                end
1498
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1499
                                begin
1500
                                        BR_CLK_O <= 1'b0;
1501
                                end
1502
                        end
1503
                        else
1504
                        begin
1505
                                count_send_data <= 12'd0;
1506
                        end
1507
 
1508
                end
1509
                TX_DATA1_1:
1510
                begin
1511
 
1512
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1513
                        begin
1514
                                count_send_data <= count_send_data + 12'd1;
1515
                                SDA_OUT<=fifo_tx_data_out[24:24];
1516
 
1517
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1518
                                begin
1519
                                        BR_CLK_O <= 1'b1;
1520
                                end
1521
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1522
                                begin
1523
                                        BR_CLK_O <= 1'b0;
1524
                                end
1525
                        end
1526
                        else
1527
                        begin
1528
                                count_send_data <= 12'd0;
1529
                                BR_CLK_O <= 1'b1;
1530
                                SDA_OUT<=fifo_tx_data_out[25:25];
1531
 
1532
                        end
1533
 
1534
 
1535
                end
1536
                TX_DATA1_2:
1537
                begin
1538
 
1539
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1540
                        begin
1541
                                count_send_data <= count_send_data + 12'd1;
1542
                                SDA_OUT<=fifo_tx_data_out[25:25];
1543
 
1544
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1545
                                begin
1546
                                        BR_CLK_O <= 1'b1;
1547
                                end
1548
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1549
                                begin
1550
                                        BR_CLK_O <= 1'b0;
1551
                                end
1552
 
1553
                        end
1554
                        else
1555
                        begin
1556
                                count_send_data <= 12'd0;
1557
                                BR_CLK_O <= 1'b1;
1558
                                SDA_OUT<=fifo_tx_data_out[26:26];
1559
                        end
1560
 
1561
                end
1562
                TX_DATA1_3:
1563
                begin
1564
 
1565
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1566
                        begin
1567
                                count_send_data <= count_send_data + 12'd1;
1568
                                SDA_OUT<=fifo_tx_data_out[26:26];
1569
 
1570
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1571
                                begin
1572
                                        BR_CLK_O <= 1'b1;
1573
                                end
1574
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1575
                                begin
1576
                                        BR_CLK_O <= 1'b0;
1577
                                end
1578
 
1579
                        end
1580
                        else
1581
                        begin
1582
                                count_send_data <= 12'd0;
1583
                                BR_CLK_O <= 1'b1;
1584
                                SDA_OUT<=fifo_tx_data_out[27:27];
1585
                        end
1586
 
1587
                end
1588
                TX_DATA1_4:
1589
                begin
1590
 
1591
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1592
                        begin
1593
                                count_send_data <= count_send_data + 12'd1;
1594
                                SDA_OUT<=fifo_tx_data_out[27:27];
1595
 
1596
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1597
                                begin
1598
                                        BR_CLK_O <= 1'b1;
1599
                                end
1600
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1601
                                begin
1602
                                        BR_CLK_O <= 1'b0;
1603
                                end
1604
 
1605
                        end
1606
                        else
1607
                        begin
1608
                                count_send_data <= 12'd0;
1609
                                BR_CLK_O <= 1'b1;
1610
                                SDA_OUT<=fifo_tx_data_out[28:28];
1611
                        end
1612
 
1613
                end
1614
                TX_DATA1_5:
1615
                begin
1616
 
1617
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1618
                        begin
1619
                                count_send_data <= count_send_data + 12'd1;
1620
                                SDA_OUT<=fifo_tx_data_out[28:28];
1621
 
1622
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1623
                                begin
1624
                                        BR_CLK_O <= 1'b1;
1625
                                end
1626
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1627
                                begin
1628
                                        BR_CLK_O <= 1'b0;
1629
                                end
1630
 
1631
                        end
1632
                        else
1633
                        begin
1634
                                count_send_data <= 12'd0;
1635
                                BR_CLK_O <= 1'b1;
1636
                                SDA_OUT<=fifo_tx_data_out[29:29];
1637
                        end
1638
 
1639
                end
1640
                TX_DATA1_6:
1641
                begin
1642
 
1643
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1644
                        begin
1645
                                count_send_data <= count_send_data + 12'd1;
1646
                                SDA_OUT<=fifo_tx_data_out[29:29];
1647
 
1648
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1649
                                begin
1650
                                        BR_CLK_O <= 1'b1;
1651
                                end
1652
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1653
                                begin
1654
                                        BR_CLK_O <= 1'b0;
1655
                                end
1656
 
1657
                        end
1658
                        else
1659
                        begin
1660
                                count_send_data <= 12'd0;
1661
                                BR_CLK_O <= 1'b1;
1662
                                SDA_OUT<=fifo_tx_data_out[30:30];
1663
                        end
1664
 
1665
                end
1666
                TX_DATA1_7:
1667
                begin
1668
 
1669
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1670
                        begin
1671
                                count_send_data <= count_send_data + 12'd1;
1672
                                SDA_OUT<=fifo_tx_data_out[30:30];
1673
 
1674
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1675
                                begin
1676
                                        BR_CLK_O <= 1'b1;
1677
                                end
1678
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1679
                                begin
1680
                                        BR_CLK_O <= 1'b0;
1681
                                end
1682
 
1683
                        end
1684
                        else
1685
                        begin
1686
                                count_send_data <= 12'd0;
1687
                                BR_CLK_O <= 1'b1;
1688
                                SDA_OUT<=fifo_tx_data_out[31:31];
1689
                        end
1690
 
1691
 
1692
                end
1693
                TX_DATA1_8:
1694
                begin
1695
 
1696
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1697
                        begin
1698
                                count_send_data <= count_send_data + 12'd1;
1699
                                SDA_OUT<=fifo_tx_data_out[31:31];
1700
 
1701
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1702
                                begin
1703
                                        BR_CLK_O <= 1'b1;
1704
                                end
1705
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1706
                                begin
1707
                                        BR_CLK_O <= 1'b0;
1708
                                end
1709
 
1710
                        end
1711
                        else
1712
                        begin
1713
                                count_send_data <= 12'd0;
1714
                                BR_CLK_O <= 1'b1;
1715
                        end
1716
 
1717
                end
1718
                TX_RESPONSE_DATA1_1:
1719
                begin
1720
                        //fifo_tx_rd_en <= 1'b1;
1721
 
1722
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1723
                        begin
1724
                                count_send_data <= count_send_data + 12'd1;
1725
 
1726
                                //LETS TRY USE THIS BUT I DONT THINK IF WORKS  
1727
                                RESPONSE<= SDA;
1728
 
1729
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2-12'd2)
1730
                                begin
1731
                                        BR_CLK_O <= 1'b1;
1732
                                end
1733
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1734
                                begin
1735
                                        BR_CLK_O <= 1'b0;
1736
                                end
1737
                        end
1738
                        else
1739
                        begin
1740
                                count_send_data <= 12'd0;
1741
                                fifo_tx_rd_en <= 1'b1;
1742
                        end
1743
 
1744
                end
1745
                TX_DELAY_BYTES:
1746
                begin
1747
 
1748
                        fifo_tx_rd_en <= 1'b0;
1749
 
1750
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1751
                        begin
1752
 
1753
                                count_send_data <= count_send_data + 12'd1;
1754
                                BR_CLK_O <= 1'b0;
1755
                                SDA_OUT<=1'b0;
1756
                        end
1757
                        else
1758
                        begin
1759
 
1760
 
1761 6 redbear
                                if(count_tx == 2'd0)
1762 2 redbear
                                begin
1763 6 redbear
                                        count_tx <= count_tx + 2'd1;
1764 2 redbear
                                        BR_CLK_O <= 1'b1;
1765
                                        SDA_OUT<=fifo_tx_data_out[8:8];
1766
                                end
1767 6 redbear
                                else if(count_tx == 2'd1)
1768 2 redbear
                                begin
1769 6 redbear
                                        count_tx <= count_tx + 2'd1;
1770 2 redbear
                                        BR_CLK_O <= 1'b1;
1771
                                        SDA_OUT<=fifo_tx_data_out[16:16];
1772
                                end
1773 6 redbear
                                else if(count_tx == 2'd2)
1774 2 redbear
                                begin
1775 6 redbear
                                        count_tx <= count_tx + 2'd1;
1776 2 redbear
                                        BR_CLK_O <= 1'b1;
1777
                                        SDA_OUT<=fifo_tx_data_out[24:24];
1778
                                end
1779 6 redbear
                                else if(count_tx == 2'd3)
1780 2 redbear
                                begin
1781
                                        BR_CLK_O <= 1'b1;
1782 6 redbear
                                        count_tx <= 2'd0;
1783 2 redbear
                                end
1784
 
1785
                                count_send_data <= 12'd0;
1786
 
1787
                        end
1788
 
1789
                end
1790
                //THIS BLOCK MUST BE CHECKED WITH CARE
1791
                TX_NACK:// MORE A RESTART 
1792
                begin
1793
                        fifo_tx_rd_en <= 1'b0;
1794
 
1795
                        if(count_send_data < DATA_CONFIG_REG[13:2]*2'd2)
1796
                        begin
1797
                                count_send_data <= count_send_data + 12'd1;
1798
 
1799 6 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1800 2 redbear
                                begin
1801
                                        SDA_OUT<=1'b0;
1802
                                end
1803
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1804
                                begin
1805
                                        SDA_OUT<=1'b1;
1806
                                end
1807
                                else if(count_send_data  == DATA_CONFIG_REG[13:2]*2'd2)
1808
                                begin
1809
                                        SDA_OUT<=1'b0;
1810
                                end
1811
 
1812 6 redbear
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2)
1813 2 redbear
                                begin
1814
                                        BR_CLK_O <= 1'b1;
1815
                                end
1816
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1817
                                begin
1818
                                        BR_CLK_O <= 1'b0;
1819
                                end
1820
                                else if(count_send_data < DATA_CONFIG_REG[13:2]*2'd2)
1821
                                begin
1822
                                        BR_CLK_O <= 1'b1;
1823
                                end
1824
 
1825
                        end
1826
                        else
1827
                        begin
1828
                                count_send_data <= 12'd0;
1829
 
1830 6 redbear
                                if(count_tx == 2'd0)
1831 2 redbear
                                begin
1832 6 redbear
                                        count_tx <= 2'd0;
1833 2 redbear
                                        BR_CLK_O <= 1'b1;
1834
                                        SDA_OUT<=fifo_tx_data_out[0:0];
1835
                                end
1836 6 redbear
                                else if(count_tx == 2'd1)
1837 2 redbear
                                begin
1838 6 redbear
                                        count_tx <= 2'd1;
1839 2 redbear
                                        BR_CLK_O <= 1'b1;
1840
                                        SDA_OUT<=fifo_tx_data_out[8:8];
1841
                                end
1842 6 redbear
                                else if(count_tx == 2'd2)
1843 2 redbear
                                begin
1844 6 redbear
                                        count_tx <= 2'd2;
1845 2 redbear
                                        BR_CLK_O <= 1'b1;
1846
                                        SDA_OUT<=fifo_tx_data_out[16:16];
1847
                                end
1848 6 redbear
                                else if(count_tx == 2'd3)
1849 2 redbear
                                begin
1850
                                        BR_CLK_O <= 1'b1;
1851 6 redbear
                                        count_tx <= 2'd3;
1852 2 redbear
                                        SDA_OUT<=fifo_tx_data_out[24:24];
1853
                                end
1854
 
1855
 
1856
                        end
1857
                end
1858
                TX_STOP:
1859
                begin
1860
                        if(count_send_data < DATA_CONFIG_REG[13:2])
1861
                        begin
1862
                                count_send_data <= count_send_data + 12'd1;
1863
 
1864
                                if(count_send_data < DATA_CONFIG_REG[13:2]/12'd2-12'd2)
1865
                                begin
1866
                                        SDA_OUT<=1'b0;
1867
                                end
1868
                                else if(count_send_data > DATA_CONFIG_REG[13:2]/12'd2-12'd1 && count_send_data < DATA_CONFIG_REG[13:2])
1869
                                begin
1870
                                        SDA_OUT<=1'b1;
1871
                                end
1872
                        end
1873
                        else
1874
                        begin
1875
                                count_send_data <= 12'd0;
1876
                        end
1877
                end
1878
                default:
1879
                begin
1880
                        fifo_tx_rd_en <= 1'b0;
1881
                        count_send_data <= 12'd4095;
1882
                end
1883
                endcase
1884
 
1885
        end
1886
 
1887
 
1888
end
1889
 
1890
 
1891 6 redbear
// RX PARAMETERS USED TO STATE MACHINE
1892 2 redbear
 
1893 6 redbear
localparam [5:0] RX_IDLE = 6'd0, //IDLE
1894
 
1895
           RX_START = 6'd1,//START BIT
1896
 
1897
           RX_CONTROLIN_1 = 6'd2, //START BYTE
1898
           RX_CONTROLIN_2 = 6'd3,
1899
           RX_CONTROLIN_3 = 6'd4,
1900
           RX_CONTROLIN_4 = 6'd5,
1901
           RX_CONTROLIN_5 = 6'd6,
1902
           RX_CONTROLIN_6 = 6'd7,
1903
           RX_CONTROLIN_7 = 6'd8,
1904
           RX_CONTROLIN_8 = 6'd9, //END FIRST BYTE
1905
 
1906
           RX_RESPONSE_CIN =6'd10, //RESPONSE
1907
 
1908
           RX_ADRESS_1 = 6'd11,//START BYTE
1909
           RX_ADRESS_2 = 6'd12,
1910
           RX_ADRESS_3 = 6'd13,
1911
           RX_ADRESS_4 = 6'd14,
1912
           RX_ADRESS_5 = 6'd15,
1913
           RX_ADRESS_6 = 6'd16,
1914
           RX_ADRESS_7 = 6'd17,
1915
           RX_ADRESS_8 = 6'd18,//END FIRST BYTE
1916
 
1917
           RX_RESPONSE_ADRESS =6'd19, //RESPONSE
1918
 
1919
           RX_DATA0_1 = 6'd20,//START BYTE
1920
           RX_DATA0_2 = 6'd21,
1921
           RX_DATA0_3 = 6'd22,
1922
           RX_DATA0_4 = 6'd23,
1923
           RX_DATA0_5 = 6'd24,
1924
           RX_DATA0_6 = 6'd25,
1925
           RX_DATA0_7 = 6'd26,
1926
           RX_DATA0_8 = 6'd27,//END FIRST BYTE
1927
 
1928
           RX_RESPONSE_DATA0_1 = 6'd28,  //RESPONSE
1929
 
1930
           RX_DATA1_1 = 6'd29,//START BYTE
1931
           RX_DATA1_2 = 6'd30,
1932
           RX_DATA1_3 = 6'd31,
1933
           RX_DATA1_4 = 6'd32,
1934
           RX_DATA1_5 = 6'd33,
1935
           RX_DATA1_6 = 6'd34,
1936
           RX_DATA1_7 = 6'd35,
1937
           RX_DATA1_8 = 6'd36,//END FIRST BYTE
1938
 
1939
           RX_RESPONSE_DATA1_1 = 6'd37,//RESPONSE
1940
 
1941
           RX_DELAY_BYTES = 6'd38,//USED ONLY IN ACK TO DELAY BETWEEN
1942
           RX_NACK = 6'd39,//USED ONLY IN ACK TO DELAY BETWEEN BYTES
1943
           RX_STOP = 6'd40;//USED TO SEND STOP BIT
1944
 
1945
        //STATE CONTROL 
1946
        reg [5:0] state_rx;
1947
        reg [5:0] next_state_rx;
1948
 
1949
        reg [11:0] count_receive_data;
1950
 
1951
        reg [1:0] count_rx;
1952
 
1953
//COMBINATIONAL BLOCK RX
1954
 
1955
always@(*)
1956
begin
1957
 
1958
 
1959
        next_state_rx = state_rx;
1960
 
1961
        case(state_rx)//STATE_RX IS MORE SECURE CHANGE ONLY IF YOU KNOW WHAT ARE YOU DOING 
1962
        RX_IDLE:
1963
        begin
1964
                //OBEYING SPEC
1965
                if(DATA_CONFIG_REG[1] == 1'b0 && DATA_CONFIG_REG[0] == 1'b0)
1966
                begin
1967
                        next_state_rx = RX_IDLE;
1968
                end
1969
                else if(DATA_CONFIG_REG[1] == 1'b1 && DATA_CONFIG_REG[0] == 1'b1)
1970
                begin
1971
                        next_state_rx = RX_IDLE;
1972
                end
1973
                else if(DATA_CONFIG_REG[1] == 1'b1 && DATA_CONFIG_REG[0] == 1'b0 && SDA == 1'b0 && SCL == 1'b1)
1974
                begin
1975
                        next_state_rx = RX_START;
1976
                end
1977
        end
1978
        RX_START:
1979
        begin
1980
 
1981
                if(SDA == 1'b0 && SCL == 1'b1)
1982
                begin
1983
                        if(count_receive_data != DATA_CONFIG_REG[13:2])
1984
                        begin
1985
                                next_state_rx = RX_START;
1986
                        end
1987
                        else
1988
                        begin
1989
                                next_state_rx = RX_CONTROLIN_1;
1990
                        end
1991
                end
1992
                else
1993
                begin
1994
                        next_state_rx = RX_IDLE;
1995
                end
1996
        end
1997
        RX_CONTROLIN_1:
1998
        begin
1999
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2000
                begin
2001
                        next_state_rx = RX_CONTROLIN_1;
2002
                end
2003
                else
2004
                begin
2005
                        next_state_rx = RX_CONTROLIN_2;
2006
                end
2007
        end
2008
        RX_CONTROLIN_2:
2009
        begin
2010
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2011
                begin
2012
                        next_state_rx = RX_CONTROLIN_2;
2013
                end
2014
                else
2015
                begin
2016
                        next_state_rx = RX_CONTROLIN_3;
2017
                end
2018
        end
2019
        RX_CONTROLIN_3:
2020
        begin
2021
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2022
                begin
2023
                        next_state_rx = RX_CONTROLIN_3;
2024
                end
2025
                else
2026
                begin
2027
                        next_state_rx = RX_CONTROLIN_4;
2028
                end
2029
        end
2030
        RX_CONTROLIN_4:
2031
        begin
2032
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2033
                begin
2034
                        next_state_rx = RX_CONTROLIN_4;
2035
                end
2036
                else
2037
                begin
2038
                        next_state_rx = RX_CONTROLIN_5;
2039
                end
2040
        end
2041
        RX_CONTROLIN_5:
2042
        begin
2043
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2044
                begin
2045
                        next_state_rx = RX_CONTROLIN_5;
2046
                end
2047
                else
2048
                begin
2049
                        next_state_rx = RX_CONTROLIN_6;
2050
                end
2051
        end
2052
        RX_CONTROLIN_6:
2053
        begin
2054
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2055
                begin
2056
                        next_state_rx = RX_CONTROLIN_6;
2057
                end
2058
                else
2059
                begin
2060
                        next_state_rx = RX_CONTROLIN_7;
2061
                end
2062
        end
2063
        RX_CONTROLIN_7:
2064
        begin
2065
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2066
                begin
2067
                        next_state_rx = RX_CONTROLIN_7;
2068
                end
2069
                else
2070
                begin
2071
                        next_state_rx = RX_CONTROLIN_8;
2072
                end
2073
        end
2074
        RX_CONTROLIN_8:
2075
        begin
2076
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2077
                begin
2078
                        next_state_rx = RX_CONTROLIN_8;
2079
                end
2080
                else
2081
                begin
2082
                        next_state_rx = RX_RESPONSE_CIN;
2083
                end
2084
        end
2085
        RX_RESPONSE_CIN:
2086
        begin
2087
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2088
                begin
2089
                        next_state_rx = RX_CONTROLIN_8;
2090
                end
2091
                else
2092
                begin
2093
                        next_state_rx = RX_RESPONSE_CIN;
2094
                end
2095
        end
2096
 
2097
        RX_ADRESS_1:
2098
        begin
2099
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2100
                begin
2101
                        next_state_rx = RX_ADRESS_1;
2102
                end
2103
                else
2104
                begin
2105
                        next_state_rx = RX_ADRESS_2;
2106
                end
2107
        end
2108
        RX_ADRESS_2:
2109
        begin
2110
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2111
                begin
2112
                        next_state_rx = RX_ADRESS_2;
2113
                end
2114
                else
2115
                begin
2116
                        next_state_rx = RX_ADRESS_3;
2117
                end
2118
        end
2119
        RX_ADRESS_3:
2120
        begin
2121
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2122
                begin
2123
                        next_state_rx = RX_ADRESS_3;
2124
                end
2125
                else
2126
                begin
2127
                        next_state_rx = RX_ADRESS_4;
2128
                end
2129
        end
2130
        RX_ADRESS_4:
2131
        begin
2132
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2133
                begin
2134
                        next_state_rx = RX_ADRESS_4;
2135
                end
2136
                else
2137
                begin
2138
                        next_state_rx = RX_ADRESS_5;
2139
                end
2140
        end
2141
        RX_ADRESS_5:
2142
        begin
2143
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2144
                begin
2145
                        next_state_rx = RX_ADRESS_5;
2146
                end
2147
                else
2148
                begin
2149
                        next_state_rx = RX_ADRESS_6;
2150
                end
2151
        end
2152
        RX_ADRESS_6:
2153
        begin
2154
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2155
                begin
2156
                        next_state_rx = RX_ADRESS_6;
2157
                end
2158
                else
2159
                begin
2160
                        next_state_rx = RX_ADRESS_7;
2161
                end
2162
        end
2163
        RX_ADRESS_7:
2164
        begin
2165
 
2166
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2167
                begin
2168
                        next_state_rx = RX_ADRESS_7;
2169
                end
2170
                else
2171
                begin
2172
                        next_state_rx = RX_ADRESS_8;
2173
                end
2174
 
2175
        end
2176
        RX_ADRESS_8:
2177
        begin
2178
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2179
                begin
2180
                        next_state_rx = RX_ADRESS_8;
2181
                end
2182
                else
2183
                begin
2184
                        next_state_rx = RX_RESPONSE_ADRESS;
2185
                end
2186
        end
2187
        RX_RESPONSE_ADRESS:
2188
        begin
2189
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2190
                begin
2191
                        next_state_rx = RX_RESPONSE_ADRESS;
2192
                end
2193
                else
2194
                begin
2195
                        next_state_rx = RX_DATA0_1;
2196
                end
2197
        end
2198
 
2199
        RX_DATA0_1:
2200
        begin
2201
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2202
                begin
2203
                        next_state_rx = RX_DATA0_1;
2204
                end
2205
                else
2206
                begin
2207
                        next_state_rx = RX_DATA0_2;
2208
                end
2209
        end
2210
        RX_DATA0_2:
2211
        begin
2212
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2213
                begin
2214
                        next_state_rx = RX_DATA0_2;
2215
                end
2216
                else
2217
                begin
2218
                        next_state_rx = RX_DATA0_3;
2219
                end
2220
        end
2221
        RX_DATA0_3:
2222
        begin
2223
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2224
                begin
2225
                        next_state_rx = RX_DATA0_3;
2226
                end
2227
                else
2228
                begin
2229
                        next_state_rx = RX_DATA0_4;
2230
                end
2231
        end
2232
        RX_DATA0_4:
2233
        begin
2234
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2235
                begin
2236
                        next_state_rx = RX_DATA0_4;
2237
                end
2238
                else
2239
                begin
2240
                        next_state_rx = RX_DATA0_5;
2241
                end
2242
        end
2243
        RX_DATA0_5:
2244
        begin
2245
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2246
                begin
2247
                        next_state_rx = RX_DATA0_5;
2248
                end
2249
                else
2250
                begin
2251
                        next_state_rx = RX_DATA0_6;
2252
                end
2253
        end
2254
        RX_DATA0_6:
2255
        begin
2256
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2257
                begin
2258
                        next_state_rx = RX_DATA0_6;
2259
                end
2260
                else
2261
                begin
2262
                        next_state_rx = RX_DATA0_7;
2263
                end
2264
        end
2265
        RX_DATA0_7:
2266
        begin
2267
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2268
                begin
2269
                        next_state_rx = RX_DATA0_7;
2270
                end
2271
                else
2272
                begin
2273
                        next_state_rx = RX_DATA0_8;
2274
                end
2275
        end
2276
        RX_DATA0_8:
2277
        begin
2278
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2279
                begin
2280
                        next_state_rx = RX_DATA0_8;
2281
                end
2282
                else
2283
                begin
2284
                        next_state_rx = RX_RESPONSE_DATA0_1;
2285
                end
2286
        end
2287
        RX_RESPONSE_DATA0_1:
2288
        begin
2289
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2290
                begin
2291
                        next_state_rx = RX_RESPONSE_DATA0_1;
2292
                end
2293
                else
2294
                begin
2295
                        next_state_rx = RX_DATA1_1;
2296
                end
2297
        end
2298
        RX_DATA1_1:
2299
        begin
2300
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2301
                begin
2302
                        next_state_rx = RX_DATA1_1;
2303
                end
2304
                else
2305
                begin
2306
                        next_state_rx = RX_DATA1_2;
2307
                end
2308
        end
2309
        RX_DATA1_2:
2310
        begin
2311
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2312
                begin
2313
                        next_state_rx = RX_DATA1_1;
2314
                end
2315
                else
2316
                begin
2317
                        next_state_rx = RX_DATA1_3;
2318
                end
2319
        end
2320
        RX_DATA1_3:
2321
        begin
2322
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2323
                begin
2324
                        next_state_rx = RX_DATA1_3;
2325
                end
2326
                else
2327
                begin
2328
                        next_state_rx = RX_DATA1_4;
2329
                end
2330
        end
2331
        RX_DATA1_4:
2332
        begin
2333
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2334
                begin
2335
                        next_state_rx = RX_DATA1_4;
2336
                end
2337
                else
2338
                begin
2339
                        next_state_rx = RX_DATA1_5;
2340
                end
2341
        end
2342
        RX_DATA1_5:
2343
        begin
2344
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2345
                begin
2346
                        next_state_rx = RX_DATA1_5;
2347
                end
2348
                else
2349
                begin
2350
                        next_state_rx = RX_DATA1_6;
2351
                end
2352
        end
2353
        RX_DATA1_6:
2354
        begin
2355
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2356
                begin
2357
                        next_state_rx = RX_DATA1_6;
2358
                end
2359
                else
2360
                begin
2361
                        next_state_rx = RX_DATA1_7;
2362
                end
2363
        end
2364
        RX_DATA1_7:
2365
        begin
2366
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2367
                begin
2368
                        next_state_rx = RX_DATA1_7;
2369
                end
2370
                else
2371
                begin
2372
                        next_state_rx = RX_DATA1_8;
2373
                end
2374
        end
2375
        RX_DATA1_8:
2376
        begin
2377
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2378
                begin
2379
                        next_state_rx = RX_DATA1_8;
2380
                end
2381
                else
2382
                begin
2383
                        next_state_rx = RX_RESPONSE_DATA1_1;
2384
                end
2385
        end
2386
        RX_RESPONSE_DATA1_1:
2387
        begin
2388
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2389
                begin
2390
                        next_state_rx = RX_RESPONSE_DATA1_1;
2391
                end
2392
                else
2393
                begin
2394
                        next_state_rx = RX_DELAY_BYTES;
2395
                end
2396
        end
2397
        RX_DELAY_BYTES:
2398
        begin
2399
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2400
                begin
2401
                        next_state_rx = RX_DELAY_BYTES;
2402
                end
2403
                else
2404
                begin
2405
 
2406
                        if(count_rx == 2'd0)
2407
                        begin
2408
                                next_state_rx = RX_ADRESS_1;
2409
                        end
2410
                        else if(count_rx == 2'd1)
2411
                        begin
2412
                                next_state_rx = RX_DATA0_1;
2413
                        end
2414
                        else if(count_rx == 2'd2)
2415
                        begin
2416
                                next_state_rx = RX_DATA1_1;
2417
                        end
2418
                        else if(count_rx == 2'd3)
2419
                        begin
2420
                                next_state_rx = RX_STOP;
2421
                        end
2422
 
2423
                end
2424
        end
2425
        RX_NACK:
2426
        begin
2427
 
2428
                        if(count_receive_data != DATA_CONFIG_REG[13:2]*2'd2)
2429
                        begin
2430
                                next_state_rx = RX_NACK;
2431
                        end
2432
                        else
2433
                        begin
2434
                                if(count_rx == 2'd0)
2435
                                begin
2436
                                        next_state_rx = RX_CONTROLIN_1;
2437
                                end
2438
                                else if(count_rx == 2'd1)
2439
                                begin
2440
                                        next_state_rx = RX_ADRESS_1;
2441
                                end
2442
                                else if(count_rx == 2'd2)
2443
                                begin
2444
                                        next_state_rx = RX_DATA0_1;
2445
                                end
2446
                                else if(count_rx == 2'd3)
2447
                                begin
2448
                                        next_state_rx = RX_DATA1_1;
2449
                                end
2450
                        end
2451
 
2452
 
2453
        end
2454
        RX_STOP:
2455
        begin
2456
 
2457
                if(count_receive_data != DATA_CONFIG_REG[13:2])
2458
                begin
2459
                        next_state_rx = RX_STOP;
2460
                end
2461
                else
2462
                begin
2463
                        next_state_rx = RX_IDLE;
2464
                end
2465
 
2466
        end
2467
        default:
2468
        begin
2469
                        next_state_rx = RX_IDLE;
2470
        end
2471
        endcase
2472
end
2473
 
2474
 
2475
//SEQUENTIAL BLOCK RX
2476
 
2477
always@(posedge PCLK)
2478
begin
2479
 
2480
        if(!PRESETn)
2481
        begin
2482
                //SIGNALS MUST BE RESETED
2483
                count_receive_data <= 12'd0;
2484
                state_rx <= RX_IDLE;
2485
                fifo_rx_wr_en <= 1'b0;
2486
                count_rx <= 2'd0;
2487
        end
2488
        else
2489
        begin
2490
 
2491
                state_rx <= next_state_rx;
2492
 
2493
                case(state_rx)//STATE_RX IS MORE SECURE CHANGE ONLY IF YOU KNOW WHAT ARE YOU DOING 
2494
                RX_IDLE:
2495
                begin
2496
                        if(SDA == 1'b0 && SCL == 1'b1)
2497
                        begin
2498
                                count_receive_data <= count_receive_data +12'd1;
2499
                        end
2500
                        else
2501
                        begin
2502
                                count_receive_data <= 12'd0;
2503
                        end
2504
                end
2505
                RX_START:
2506
                begin
2507
                        if(SDA == 1'b0 && SCL == 1'b0)
2508
                        begin
2509
                                count_receive_data <= count_receive_data +12'd1;
2510
                        end
2511
                        else
2512
                        begin
2513
                                count_receive_data <= 12'd0;
2514
                        end
2515
                end
2516
                RX_CONTROLIN_1:
2517
                begin
2518
 
2519
                end
2520
                RX_CONTROLIN_2:
2521
                begin
2522
 
2523
                end
2524
                RX_CONTROLIN_3:
2525
                begin
2526
 
2527
                end
2528
                RX_CONTROLIN_4:
2529
                begin
2530
 
2531
                end
2532
                RX_CONTROLIN_5:
2533
                begin
2534
 
2535
                end
2536
                RX_CONTROLIN_6:
2537
                begin
2538
 
2539
                end
2540
                RX_CONTROLIN_7:
2541
                begin
2542
 
2543
                end
2544
                RX_CONTROLIN_8:
2545
                begin
2546
 
2547
                end
2548
                RX_RESPONSE_CIN:
2549
                begin
2550
 
2551
                end
2552
                RX_ADRESS_1:
2553
                begin
2554
                end
2555
                RX_ADRESS_2:
2556
                begin
2557
                end
2558
                RX_ADRESS_3:
2559
                begin
2560
                end
2561
                RX_ADRESS_4:
2562
                begin
2563
                end
2564
                RX_ADRESS_5:
2565
                begin
2566
                end
2567
                RX_ADRESS_6:
2568
                begin
2569
                end
2570
                RX_ADRESS_7:
2571
                begin
2572
                end
2573
                RX_ADRESS_8:
2574
                begin
2575
                end
2576
                RX_RESPONSE_ADRESS:
2577
                begin
2578
 
2579
                end
2580
                RX_DATA0_1:
2581
                begin
2582
 
2583
                end
2584
                RX_DATA0_2:
2585
                begin
2586
 
2587
                end
2588
                RX_DATA0_3:
2589
                begin
2590
 
2591
                end
2592
                RX_DATA0_4:
2593
                begin
2594
 
2595
                end
2596
                RX_DATA0_5:
2597
                begin
2598
 
2599
                end
2600
                RX_DATA0_6:
2601
                begin
2602
 
2603
                end
2604
                RX_DATA0_7:
2605
                begin
2606
 
2607
                end
2608
                RX_DATA0_8:
2609
                begin
2610
 
2611
                end
2612
                RX_RESPONSE_DATA0_1:
2613
                begin
2614
                end
2615
 
2616
                RX_DATA1_1:
2617
                begin
2618
 
2619
                end
2620
                RX_DATA1_2:
2621
                begin
2622
 
2623
                end
2624
                RX_DATA1_3:
2625
                begin
2626
 
2627
                end
2628
                RX_DATA1_4:
2629
                begin
2630
 
2631
                end
2632
                RX_DATA1_5:
2633
                begin
2634
 
2635
                end
2636
                RX_DATA1_6:
2637
                begin
2638
 
2639
                end
2640
                RX_DATA1_7:
2641
                begin
2642
 
2643
                end
2644
                RX_DATA1_8:
2645
                begin
2646
 
2647
                end
2648
                RX_RESPONSE_DATA1_1:
2649
                begin
2650
                end
2651
                RX_DELAY_BYTES:
2652
                begin
2653
 
2654
                end
2655
                RX_NACK:
2656
                begin
2657
 
2658
 
2659
                end
2660
                RX_STOP:
2661
                begin
2662
 
2663
 
2664
                end
2665
                default:
2666
                begin
2667
                        count_receive_data <= 12'd4095;
2668
                        fifo_rx_wr_en <= 1'b0;
2669
                        count_rx <= 2'd3;
2670
                end
2671
                endcase
2672
        end
2673
end
2674
 
2675 2 redbear
endmodule

powered by: WebSVN 2.1.0

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