-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_interface.v
726 lines (671 loc) · 25.3 KB
/
camera_interface.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
`timescale 1ns / 1ps
module camera_interface(
input wire clk,clk_100,rst_n,
input wire[3:0] key, //key[1:0] for brightness control , key[3:2] for contrast control
input wire pir,
input start_camera,
//asyn_fifo IO
input wire rd_en, rd_en_SD,
output wire[9:0] data_count_r,
output wire[15:0] dout,
output wire[15:0] dout_SD,
//camera pinouts
input wire cmos_pclk,cmos_href,cmos_vsync,
input wire[7:0] cmos_db,
inout cmos_sda,cmos_scl, //i2c comm wires
output wire cmos_rst_n, cmos_pwdn, cmos_xclk, empty,
//Debugging
output led,
output wire led_start
);
//FSM state declarations
localparam idle=0,
start_sccb=1,
write_address=2,
write_data=3,
digest_loop=4,
delay=5,
vsync_fedge=6,
byte1=7,
byte2=8,
fifo_write=9,
stopping=10;
localparam wait_init=0,
sccb_idle=1,
sccb_address=2,
sccb_data=3,
sccb_stop=4;
localparam rest = 0,
vsync_fedge_SD = 1,
byte1_SD = 2,
byte2_SD = 3;
localparam MSG_INDEX=77; //number of the last index to be digested by SCCB
reg[3:0] state_q=0,state_d;
reg[2:0] sccb_state_q=0,sccb_state_d;
reg[7:0] addr_q,addr_d;
reg[7:0] data_q,data_d;
reg[7:0] brightness_q,brightness_d;
reg[7:0] contrast_q,contrast_d;
reg start,stop;
reg[7:0] wr_data;
wire rd_tick;
wire[1:0] ack;
wire[7:0] rd_data;
wire[3:0] state;
reg[3:0] led_q=0,led_d;
reg[27:0] delay_q=0,delay_d;
reg start_delay_q=0,start_delay_d;
reg delay_finish;
reg[15:0] message[250:0];
reg[7:0] message_index_q=0,message_index_d;
reg[15:0] pixel_q,pixel_d;
reg wr_en, wr_en_SD;
reg mod2_q=0,mod2_d;
wire full;
wire key0_tick,key1_tick,key2_tick,key3_tick;
reg[2:0] lines_q,lines_d;
reg[18:0] count_q=0,count_d;
reg[3:0] state_q_SD=0, state_d_SD;
//buffer for all inputs coming from the camera
reg pclk_1,pclk_2,href_1,href_2,vsync_1,vsync_2;
initial begin //collection of all adddresses and values to be written in the camera
//{address,data}
/*message[0]=16'h12_80; //reset all register to default values
message[1]=16'h12_04; //set output format to RGB
message[2]=16'h15_20; //pclk will not toggle during horizontal blank
message[3]=16'h40_d0; //RGB565
// These are values scalped from https://github.com/jonlwowski012/OV7670_NEXYS4_Verilog/blob/master/ov7670_registers_verilog.v
message[4]= 16'h12_04; // COM7, set RGB color output
message[5]= 16'h11_80; // CLKRC internal PLL matches input clock
message[6]= 16'h0C_00; // COM3, default settings
message[7]= 16'h3E_00; // COM14, no scaling, normal pclock
message[8]= 16'h04_00; // COM1, disable CCIR656
message[9]= 16'h40_d0; //COM15, RGB565, full output range
message[10]= 16'h3a_04; //TSLB set correct output data sequence (magic)
message[11]= 16'h14_18; //COM9 MAX AGC value x4 0001_1000
message[12]= 16'h4F_B3; //MTX1 all of these are magical matrix coefficients
message[13]= 16'h50_B3; //MTX2write
message[14]= 16'h51_00; //MTX3
message[15]= 16'h52_3d; //MTX4
message[16]= 16'h53_A7; //MTX5
message[17]= 16'h54_E4; //MTX6
message[18]= 16'h58_9E; //MTXS
message[19]= 16'h3D_C0; //COM13 sets gamma enable
message[20]= 16'h17_14; //HSTART start high 8 bits
message[21]= 16'h18_02; //HSTOP stop high 8 bits //these kill the odd colored line
message[22]= 16'h32_80; //HREF edge offset
message[23]= 16'h19_03; //VSTART start high 8 bits
message[24]= 16'h1A_7B; //VSTOP stop high 8 bits
message[25]= 16'h03_0A; //VREF vsync edge offset
message[26]= 16'h0F_41; //COM6 reset timings
message[27]= 16'h1E_00; //MVFP disable mirror / flip //might have magic value of 03
message[28]= 16'h33_0B; //CHLF //magic value from the internet
message[29]= 16'h3C_78; //COM12 no HREF when VSYNC low
message[30]= 16'h69_00; //GFIX fix gain control
message[31]= 16'h74_00; //REG74 Digital gain control
message[32]= 16'hB0_84; //RSVD magic value from the internet *required* for good color
message[33]= 16'hB1_0c; //ABLC1
message[34]= 16'hB2_0e; //RSVD more magic internet values
message[35]= 16'hB3_80; //THL_ST
//begin mystery scaling numbers
message[36]= 16'h70_3a;
message[37]= 16'h71_35;
message[38]= 16'h72_11;
message[39]= 16'h73_f0;
message[40]= 16'ha2_02;
//gamma curve values
message[41]= 16'h7a_20;
message[42]= 16'h7b_10;
message[43]= 16'h7c_1e;
message[44]= 16'h7d_35;
message[45]= 16'h7e_5a;
message[46]= 16'h7f_69;
message[47]= 16'h80_76;
message[48]= 16'h81_80;
message[49]= 16'h82_88;
message[50]= 16'h83_8f;
message[51]= 16'h84_96;
message[52]= 16'h85_a3;
message[53]= 16'h86_af;
message[54]= 16'h87_c4;
message[55]= 16'h88_d7;
message[56]= 16'h89_e8;
//AGC and AEC
message[57]= 16'h13_e0; //COM8, disable AGC / AEC
message[58]= 16'h00_00; //set gain reg to 0 for AGC
message[59]= 16'h10_00; //set ARCJ reg to 0
message[60]= 16'h0d_40; //magic reserved bit for COM4
message[61]= 16'h14_18; //COM9, 4x gain + magic bit
message[62]= 16'ha5_05; // BD50MAX
message[63]= 16'hab_07; //DB60MAX
message[64]= 16'h24_95; //AGC upper limit
message[65]= 16'h25_33; //AGC lower limit
message[66]= 16'h26_e3; //AGC/AEC fast mode op region
message[67]= 16'h9f_78; //HAECC1
message[68]= 16'ha0_68; //HAECC2
message[69]= 16'ha1_03; //magic
message[70]= 16'ha6_d8; //HAECC3
message[71]= 16'ha7_d8; //HAECC4
message[72]= 16'ha8_f0; //HAECC5
message[73]= 16'ha9_90; //HAECC6
message[74]= 16'haa_94; //HAECC7
message[75]= 16'h13_e5; //COM8, enable AGC / AEC
message[76]= 16'h1E_23; //Mirror Image
message[77]= 16'h69_06; //gain of RGB(manually adjusted)*/
/*message[0]=16'h12_80; //reset all register to default values
message[1]=16'h12_04; //set output format to RGB
message[2]=16'h15_20; //pclk will not toggle during horizontal blank
message[3]=16'h40_d0; //RGB565
// These are values scalped from https://github.com/jonlwowski012/OV7670_NEXYS4_Verilog/blob/master/ov7670_registers_verilog.v
message[4]= 16'h12_04; // COM7, set RGB color output
// message[5]= 16'h11_82; // CLKRC internal PLL matches input clock
message[5]= 16'h11_82; // CLKRC internal PLL matches input clock
message[6]= 16'h0C_04; // COM3, default settings
message[7]= 16'h3E_13; // COM14, no scaling, normal pclock
message[8]= 16'h04_00; // COM1, disable CCIR656
message[9]= 16'h40_d0; //COM15, RGB565, full output range
message[10]= 16'h3a_04; //TSLB set correct output data sequence (magic)
message[11]= 16'h14_18; //COM9 MAX AGC value x4 0001_1000
message[12]= 16'h4F_B3; //MTX1 all of these are magical matrix coefficients
message[13]= 16'h50_B3; //MTX2
message[14]= 16'h51_00; //MTX3
message[15]= 16'h52_3d; //MTX4
message[16]= 16'h53_A7; //MTX5
message[17]= 16'h54_E4; //MTX6
message[18]= 16'h58_9E; //MTXS
message[19]= 16'h3D_C0; //COM13 sets gamma enable, does not preserve reserved bits, may be wrong?
message[20]= 16'h17_14; //HSTART start high 8 bits
message[21]= 16'h18_02; //HSTOP stop high 8 bits //these kill the odd colored line
message[22]= 16'h32_80; //HREF edge offset
message[23]= 16'h19_03; //VSTART start high 8 bits
message[24]= 16'h1A_7B; //VSTOP stop high 8 bits
message[25]= 16'h03_0A; //VREF vsync edge offset
message[26]= 16'h0F_4b; //COM6 reset timings CHANGED TO 4b from 41
message[27]= 16'h1E_00; //MVFP disable mirror / flip //might have magic value of 03
message[28]= 16'h33_0B; //CHLF //magic value from the internet
message[29]= 16'h3C_78; //COM12 no HREF when VSYNC low
message[30]= 16'h69_00; //GFIX fix gain control
message[31]= 16'h74_10; //REG74 Digital gain control CHANGING THIS
message[32]= 16'hB0_84; //RSVD magic value from the internet *required* for good color
message[33]= 16'hB1_0c; //ABLC1
message[34]= 16'hB2_0e; //RSVD more magic internet values
message[35]= 16'hB3_80; //THL_ST
//begin mystery scaling numbers we will change these
message[36]= 16'h70_38;
message[37]= 16'h71_29;
message[38]= 16'h72_33;
message[39]= 16'h73_f3;
message[40]= 16'ha2_02;
//gamma curve values same as C
message[41]= 16'h7a_20;
message[42]= 16'h7b_10;
message[43]= 16'h7c_1e;
message[44]= 16'h7d_35;
message[45]= 16'h7e_5a;
message[46]= 16'h7f_69;
message[47]= 16'h80_76;
message[48]= 16'h81_80;
message[49]= 16'h82_88;
message[50]= 16'h83_8f;
message[51]= 16'h84_96;
message[52]= 16'h85_a3;
message[53]= 16'h86_af;
message[54]= 16'h87_c4;
message[55]= 16'h88_d7;
message[56]= 16'h89_e8;
//AGC and AEC
message[57]= 16'h13_e0; //COM8, disable AGC / AEC CAN CHANGE THIS TO C0
message[58]= 16'h00_00; //set gain reg to 0 for AGC
message[59]= 16'h10_00; //set ARCJ reg to 0
message[60]= 16'h0d_40; //magic reserved bit for COM4
message[61]= 16'h14_18; //COM9, 4x gain + magic bit
message[62]= 16'ha5_05; // BD50MAX
message[63]= 16'hab_07; //DB60MAX
message[64]= 16'h24_95; //AGC upper limit
message[65]= 16'h25_33; //AGC lower limit
message[66]= 16'h26_e3; //AGC/AEC fast mode op region
message[67]= 16'h9f_78; //HAECC1
message[68]= 16'ha0_68; //HAECC2
message[69]= 16'ha1_03; //magic
message[70]= 16'ha6_d8; //HAECC3
message[71]= 16'ha7_d8; //HAECC4
message[72]= 16'ha8_f0; //HAECC5
message[73]= 16'ha9_90; //HAECC6
message[74]= 16'haa_94; //HAECC7
message[75]= 16'h13_e5; //COM8, enable AGC / AEC CAN CHANGE THIS TO C0
message[76]= 16'h1E_07; //Mirror Image CHANGED FROM 23 to 07
message[77]= 16'h69_06; //gain of RGB(manually adjusted)
message[78]= 16'h6b_42; //gain of RGB(manually adjusted)160
////added by Prathmesh
message[78] = 16'h0e_61;//COM5
message[79] = 16'h16_02;
message[80] = 16'h21_02;
message[81] = 16'h22_91;
message[82] = 16'h29_07;
message[83] = 16'h35_0b;
message[84] = 16'h37_1d;
message[85] = 16'h38_71;
message[86] = 16'h39_2a;
message[87] = 16'h4d_40;
message[88] = 16'h4e_20;
message[89] = 16'h8d_4f;
message[90] = 16'h8e_00;
message[91] = 16'h8f_00;
message[92] = 16'h90_00;
message[93] = 16'h91_00;
message[94] = 16'h96_00;
message[95] = 16'h9a_00;
message[96] = 16'hb0_84;
message[97] = 16'hb1_0c;
message[98] = 16'hb2_0e;
message[99] = 16'hb3_82;
message[100] = 16'hb8_0a;*/
message[0]=16'h12_80; //reset all register to default values
message[1]=16'h12_04; //set output format to RGB
message[2]=16'h15_20; //pclk will not toggle during horizontal blank
message[3]=16'h40_d0; //RGB565
// These are values scalped from https://github.com/jonlwowski012/OV7670_NEXYS4_Verilog/blob/master/ov7670_registers_verilog.v
message[4]= 16'h12_00; // COM7, set RGB color output
message[5]= 16'h11_20; // CLKRC internal PLL matches input clock
message[6]= 16'h0C_04; // COM3, default settings
message[7]= 16'h3E_1a; // COM14, no scaling, normal pclock
message[8]= 16'h04_00; // COM1, disable CCIR656
message[9]= 16'h40_c0; //COM15, RGB565, full output range
message[10]= 16'h3a_04; //TSLB set correct output data sequence (magic)
message[11]= 16'h14_6A; //COM9 MAX AGC value x4 0001_1000
message[12]= 16'h42_B0; //MTX1 all of these are magical matrix coefficients
message[13]= 16'h50_80; //MTX2write
message[14]= 16'h51_00; //MTX3
message[15]= 16'h52_22; //MTX4
message[16]= 16'h53_5e; //MTX5
message[17]= 16'h54_80; //MTX6
message[18]= 16'h58_9E; //MTXS
message[19]= 16'h3D_40; //COM13 sets gamma enable
message[20]= 16'h17_14; //HSTART start high 8 bits
message[21]= 16'h18_02; //HSTOP stop high 8 bits //these kill the odd colored line
message[22]= 16'h32_80; //HREF edge offset
message[23]= 16'h19_03; //VSTART start high 8 bits
message[24]= 16'h1A_7B; //VSTOP stop high 8 bits
message[25]= 16'h03_0A; //VREF vsync edge offset
message[26]= 16'h0F_41; //COM6 reset timings
message[27]= 16'h1E_20; //MVFP disable mirror / flip //might have magic value of 03
message[28]= 16'h33_0B; //CHLF //magic value from the internet
message[29]= 16'h3C_78; //COM12 no HREF when VSYNC low
message[30]= 16'h69_00; //GFIX fix gain control
message[31]= 16'h74_00; //REG74 Digital gain control
message[32]= 16'hB0_84; //RSVD magic value from the internet *required* for good color
message[33]= 16'hB1_0c; //ABLC1
message[34]= 16'hB2_0e; //RSVD more magic internet values
message[35]= 16'hB3_80; //THL_ST
//begin mystery scaling numbers
message[36]= 16'h70_3a;
message[37]= 16'h71_35;
message[38]= 16'h72_22;
message[39]= 16'h73_f2;
message[40]= 16'ha2_02;
//gamma curve values
message[41]= 16'h7a_20;
message[42]= 16'h7b_10;
message[43]= 16'h7c_1e;
message[44]= 16'h7d_35;
message[45]= 16'h7e_5a;
message[46]= 16'h7f_69;
message[47]= 16'h80_76;
message[48]= 16'h81_80;
message[49]= 16'h82_88;
message[50]= 16'h83_8f;
message[51]= 16'h84_96;
message[52]= 16'h85_a3;
message[53]= 16'h86_af;
message[54]= 16'h87_c4;
message[55]= 16'h88_d7;
message[56]= 16'h89_e8;
//AGC and AEC
message[57]= 16'h13_e0; //COM8, disable AGC / AEC
message[58]= 16'h00_00; //set gain reg to 0 for AGC
message[59]= 16'h10_00; //set ARCJ reg to 0
message[60]= 16'h0d_40; //magic reserved bit for COM4
message[61]= 16'h14_18; //COM9, 4x gain + magic bit
message[62]= 16'ha5_05; // BD50MAX
message[63]= 16'hab_07; //DB60MAX
message[64]= 16'h24_95; //AGC upper limit
message[65]= 16'h25_33; //AGC lower limit
message[66]= 16'h26_e3; //AGC/AEC fast mode op region
message[67]= 16'h9f_78; //HAECC1
message[68]= 16'ha0_68; //HAECC2
message[69]= 16'ha1_03; //magic
message[70]= 16'ha6_d8; //HAECC3
message[71]= 16'ha7_d8; //HAECC4
message[72]= 16'ha8_f0; //HAECC5
message[73]= 16'ha9_90; //HAECC6
message[74]= 16'haa_94; //HAECC7
message[75]= 16'h13_e5; //COM8, enable AGC / AEC
message[76]= 16'h1E_23; //Mirror Image
message[77]= 16'h69_06; //gain of RGB(manually adjusted)
end
//register operations
always @(posedge clk_100,negedge rst_n) begin
if(!rst_n) begin
state_q<=0;
state_q_SD<=0;
led_q<=0;
delay_q<=0;
start_delay_q<=0;
message_index_q<=0;
pixel_q<=0;
sccb_state_q<=0;
addr_q<=0;
data_q<=0;
brightness_q<=0;
contrast_q<=0;
end
else begin
state_q<=state_d;
state_q_SD<=state_d_SD;
delay_q<=delay_d;
start_delay_q<=start_delay_d;
message_index_q<=message_index_d;
pclk_1<=cmos_pclk;
pclk_2<=pclk_1;
href_1<=cmos_href;
href_2<=href_1;
vsync_1<=cmos_vsync;
vsync_2<=vsync_1;
pixel_q<=pixel_d;
sccb_state_q<=sccb_state_d;
addr_q<=addr_d;
data_q<=data_d;
brightness_q<=brightness_d;
contrast_q<=contrast_d;
end
end
//FSM next-state logics
always @* begin
state_d=state_q;
led_d=led_q;
start=0;
stop=0;
wr_data=0;
start_delay_d=start_delay_q;
delay_d=delay_q;
delay_finish=0;
message_index_d=message_index_q;
pixel_d=pixel_q;
wr_en=0;
sccb_state_d=sccb_state_q;
addr_d=addr_q;
data_d=data_q;
brightness_d=brightness_q;
contrast_d=contrast_q;
//delay logic
if(start_delay_q) delay_d=delay_q+1'b1;
if(delay_q[16] && message_index_q!=(MSG_INDEX+1) && (state_q!=start_sccb)) begin //delay between SCCB transmissions (0.66ms)
delay_finish=1;
start_delay_d=0;
delay_d=0;
end
else if((delay_q[26] && message_index_q==(MSG_INDEX+1)) || (delay_q[26] && state_q==start_sccb)) begin //delay BEFORE SCCB transmission, AFTER SCCB transmission, and BEFORE retrieving pixel data from camera (0.67s)
delay_finish=1;
start_delay_d=0;
delay_d=0;
end
if (!pir) begin
case(state_q)
////////Begin: Setting register values of the camera via SCCB///////////
idle: if(delay_finish) begin //idle for 0.6s to start-up the camera
state_d=start_sccb;
start_delay_d=0;
//led_start<=0;
end
else begin
start_delay_d=1;
led_start<=1;
end
start_sccb: begin //start of SCCB transmission
start=1;
wr_data=8'h42; //slave address of OV7670 for write
state_d=write_address;
end
write_address: if(ack==2'b11) begin
wr_data=message[message_index_q][15:8]; //write address
state_d=write_data;
led_start<=0;
end
write_data: if(ack==2'b11) begin
wr_data=message[message_index_q][7:0]; //write data
state_d=digest_loop;
end
digest_loop: if(ack==2'b11) begin //stop sccb transmission
stop=1;
start_delay_d=1;
message_index_d=message_index_q+1'b1;
state_d=delay;
end
delay: begin
if(message_index_q==(MSG_INDEX+1) && delay_finish) begin
state_d=vsync_fedge; //if all messages are already digested, proceed to retrieving camera pixel data
led_d=4'b0110;
led_start<=0;
end
else if(state==0 && delay_finish) state_d=start_sccb; //small delay before next SCCB transmission(if all messages are not yet digested)
end
///////////////Begin: Retrieving Pixel Data from Camera to be Stored to SDRAM/////////////////
vsync_fedge: if(vsync_1==0 && vsync_2==1) state_d=byte1; //vsync falling edge means new frame is incoming
byte1: if(pclk_1==1 && pclk_2==0 && href_1==1 && href_2==1) begin //rising edge of pclk means new pixel data(first byte of 16-bit pixel RGB565) is available at output
pixel_d[15:8]=cmos_db;
state_d=byte2;
end
else if(vsync_1==1 && vsync_2==1) begin
state_d=vsync_fedge;
end
byte2: if(pclk_1==1 && pclk_2==0 && href_1==1 && href_2==1) begin //rising edge of pclk means new pixel data(second byte of 16-bit pixel RGB565) is available at output
pixel_d[7:0]=cmos_db;
state_d=fifo_write;
end
else if(vsync_1==1 && vsync_2==1) begin
state_d=vsync_fedge;
end
fifo_write: begin //write the 16-bit data to asynchronous fifo to be retrieved later by SDRAM
wr_en=1;
state_d=byte1;
if(full) led_d=4'b1001; //debugging led
end
default: state_d=idle;
endcase
//Logic for increasing/decreasing brightness and contrast via the 4 keybuttons
case(sccb_state_q)
wait_init: if(state_q==byte1) begin //wait for initial SCCB transmission to finish
sccb_state_d=sccb_idle;
addr_d=0;
data_d=0;
brightness_d=8'h00;
contrast_d=8'h40;
end
sccb_idle: if(state==0) begin //wait for any pushbutton
if(key0_tick) begin//increase brightness
brightness_d=(brightness_q[7]==1)? brightness_q-1:brightness_q+1;
if(brightness_q==8'h80) brightness_d=0;
start=1;
wr_data=8'h42; //slave address of OV7670 for write
addr_d=8'h55; //brightness control address
data_d=brightness_d;
sccb_state_d=sccb_address;
led_d=0;
end
if(key1_tick) begin //decrease brightness
brightness_d=(brightness_q[7]==1)? brightness_q+1:brightness_q-1;
if(brightness_q==0) brightness_d=8'h80;
start=1;
wr_data=8'h42;
addr_d=8'h55;
data_d=brightness_d;
sccb_state_d=sccb_address;
led_d=0;
end
else if(key2_tick) begin //increase contrast
contrast_d=contrast_q+1;
start=1;
wr_data=8'h42; //slave address of OV7670 for write
addr_d=8'h56; //contrast control address
data_d=contrast_d;
sccb_state_d=sccb_address;
led_d=0;
end
else if(key3_tick) begin //change contrast
contrast_d=contrast_q-1;
start=1;
wr_data=8'h42;
addr_d=8'h56;
data_d=contrast_d;
sccb_state_d=sccb_address;
led_d=0;
end
end
sccb_address: if(ack==2'b11) begin
wr_data=addr_q; //write address
sccb_state_d=sccb_data;
end
sccb_data: if(ack==2'b11) begin
wr_data=data_q; //write databyte
sccb_state_d=sccb_stop;
end
sccb_stop: if(ack==2'b11) begin //stop
stop=1;
sccb_state_d=sccb_idle;
led_d=4'b1001;
end
default: sccb_state_d=wait_init;
endcase
end
else begin
case(state_q_SD)
///////////////Begin: Retrieving Pixel Data from Camera to be Stored to SDRAM/////////////////
rest: if(pir) begin
lines_d=0;
state_d_SD=vsync_fedge;
end
vsync_fedge_SD: begin
if(vsync_1==0 && vsync_2==1 && empty && lines_q<5) begin
lines_d=lines_q+1;
state_d_SD=byte1; //vsync falling edge means new frame is incoming
count_d=0;
end
else if(lines_q==5) begin
state_d_SD=rest;
led_d=4'b0110;
end
end
byte1_SD: if(pclk_1==1 && pclk_2==0 && href_1==1 && href_2==1) begin //rising edge of pclk means new pixel data(first byte of 16-bit pixel RGB565) is available at output
case(lines_q)
1:wr_en_SD=count_q>=0 && count_q<=65535;
2:wr_en_SD=count_q>=65536 && count_q<=131071;
3:wr_en_SD=count_q>=131072 && count_q<=196607;
4:wr_en_SD=count_q>=196608 && count_q<=262143;
5:wr_en_SD=count_q>=262144 && count_q<=327679;
endcase
state_d_SD=byte2;
led_d=4'b1001;
end
else if(vsync_1==1 && vsync_2==1) begin
state_d_SD=vsync_fedge;
end
byte2_SD: if(pclk_1==1 && pclk_2==0 && href_1==1 && href_2==1) begin //rising edge of pclk means new pixel data(second byte of 16-bit pixel RGB565) is available at output
case(lines_q)
1:wr_en_SD=count_q>=0 && count_q<=65535;
2:wr_en_SD=count_q>=65536 && count_q<=131071;
3:wr_en_SD=count_q>=131072 && count_q<=196607;
4:wr_en_SD=count_q>=196608 && count_q<=262143;
5:wr_en_SD=count_q>=262144 && count_q<=327679;
endcase
state_d_SD=byte1;
count_d=(count_q<307200)? (count_q+1'b1):count_q;
end
else if(vsync_1==1 && vsync_2==1) begin
state_d_SD=vsync_fedge;
end
//default: state_d_SD=idle_SD;
endcase
end
end
assign cmos_pwdn=0;
assign cmos_rst_n=1;
assign led=led_q;
//module instantiations
i2c_top #(.freq(100_000)) m0
(
.clk(clk_100),
.rst_n(rst_n),
.start(start),
.stop(stop),
.wr_data(wr_data),
.rd_tick(rd_tick), //ticks when read data from servant is ready,data will be taken from rd_data
.ack(ack), //ack[1] ticks at the ack bit[9th bit],ack[0] asserts when ack bit is ACK,else NACK
.rd_data(rd_data),
.scl(cmos_scl),
.sda(cmos_sda),
.state(state)
);
clock_24_0002 clock_24_inst (
.refclk (clk), // refclk.clk
.rst (rst), // reset.reset
.outclk_0 (cmos_xclk), // outclk0.clk
.locked () // (terminated)
);
asyn_fifo #(.DATA_WIDTH(16),.FIFO_DEPTH_WIDTH(10)) m2 //1024x16 FIFO mem
(
.rst_n(rst_n),
.clk_write(clk_100),
.clk_read(clk_100), //clock input from both domains
.write(wr_en),
.read(rd_en),
.data_write(pixel_q), //input FROM write clock domain
.data_read(dout), //output TO read clock domain
.full(full),
.empty(), //full=sync to write domain clk , empty=sync to read domain clk
.data_count_r(data_count_r) //asserted if fifo is equal or more than than half of its max capacity
);
asyn_fifo #(.DATA_WIDTH(16),.FIFO_DEPTH_WIDTH(10)) m3 //2048x8 FIFO mem
(
.rst_n(rst_n),
.clk_write(clk_100),
.clk_read(clk_100), //clock input from both domains
.write(wr_en_SD),
.read(rd_en_SD),
.data_write(cmos_db), //input FROM write clock domain
.data_read(dout_SD), //output TO read clock domain
.full(full),
.empty(empty), //full=sync to write domain clk , empty=sync to read domain clk
);
debounce_explicit m4
(
.clk(clk_100),
.rst_n(rst_n),
.sw({!key[0]}),
.db_level(),
.db_tick(key0_tick)
);
debounce_explicit m5
(
.clk(clk_100),
.rst_n(rst_n),
.sw({!key[1]}),
.db_level(),
.db_tick(key1_tick)
);
debounce_explicit m6
(
.clk(clk_100),
.rst_n(rst_n),
.sw({!key[2]}),
.db_level(),
.db_tick(key2_tick)
);
debounce_explicit m7
(
.clk(clk_100),
.rst_n(rst_n),
.sw({!key[3]}),
.db_level(),
.db_tick(key3_tick)
);
endmodule