forked from 6by9/userland
/
raspi_tc358743.c
1304 lines (1110 loc) · 40.6 KB
/
raspi_tc358743.c
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2015, Raspberry Pi Foundation
Copyright (c) 2015, Dave Stevenson
Copyright (c) 2017, Ben Kazemi
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#include <wiringPi.h>
//#include <wiringPiI2C.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
//#include "i2c-dev.h"
#include "interface/vcos/vcos.h"
#include "bcm_host.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_buffer.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_connection.h"
#include <sys/ioctl.h>
#include "tc358743_regs.h"
// Do the GPIO waggling from here, except that needs root access, plus
// there is variation on pin allocation between the various Pi platforms.
// Provided for reference, but needs tweaking to be useful.
//#define DO_PIN_CONFIG
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
u8 called_quit = 0;
struct sensor_regs {
uint16_t reg;
uint8_t data;
};
#define ENCODING MMAL_ENCODING_BGR24 //MMAL_ENCODING_I420// //MMAL_ENCODING_YUYV
#define UNPACK MMAL_CAMERA_RX_CONFIG_UNPACK_NONE
#define PACK MMAL_CAMERA_RX_CONFIG_PACK_NONE
#define I2C_ADDR 0x0F
#define CSI_IMAGE_ID 0x24
void signal_callback_handler(int);
static void i2c_rd(int fd, uint16_t reg, uint8_t *values, uint32_t n)
{
int err;
uint8_t buf[2] = { reg >> 8, reg & 0xff };
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg msgs[2] = {
{
.addr = I2C_ADDR,
.flags = 0,
.len = 2,
.buf = buf,
},
{
.addr = I2C_ADDR,
.flags = I2C_M_RD,
.len = n,
.buf = values,
},
};
msgset.msgs = msgs;
msgset.nmsgs = 2;
err = ioctl(fd, I2C_RDWR, &msgset);
if(err != msgset.nmsgs)
vcos_log_error("%s: reading register 0x%x from 0x%x failed, err %d\n",
__func__, reg, I2C_ADDR, err);
}
static void i2c_wr(int fd, uint16_t reg, uint8_t *values, uint32_t n)
{
uint8_t data[1024];
int err, i;
struct i2c_msg msg;
struct i2c_rdwr_ioctl_data msgset;
if ((2 + n) > sizeof(data))
vcos_log_error("i2c wr reg=%04x: len=%d is too big!\n",
reg, 2 + n);
msg.addr = I2C_ADDR;
msg.buf = data;
msg.len = 2 + n;
msg.flags = 0;
data[0] = reg >> 8;
data[1] = reg & 0xff;
for (i = 0; i < n; i++)
data[2 + i] = values[i];
msgset.msgs = &msg;
msgset.nmsgs = 1;
err = ioctl(fd, I2C_RDWR, &msgset);
if (err != 1) {
vcos_log_error("%s: writing register 0x%x from 0x%x failed\n",
__func__, reg, I2C_ADDR);
return;
}
}
static inline u8 i2c_rd8(int fd, u16 reg)
{
u8 val;
i2c_rd(fd, reg, &val, 1);
return val;
}
static inline void i2c_wr8(int fd, u16 reg, u8 val)
{
i2c_wr(fd, reg, &val, 1);
}
static inline void i2c_wr8_and_or(int fd, u16 reg,
u8 mask, u8 val)
{
i2c_wr8(fd, reg, (i2c_rd8(fd, reg) & mask) | val);
}
static inline u16 i2c_rd16(int fd, u16 reg)
{
u16 val;
i2c_rd(fd, reg, (u8 *)&val, 2);
return val;
}
static inline void i2c_wr16(int fd, u16 reg, u16 val)
{
i2c_wr(fd, reg, (u8 *)&val, 2);
}
static void i2c_wr16_and_or(int fd, u16 reg, u16 mask, u16 val)
{
i2c_wr16(fd, reg, (i2c_rd16(fd, reg) & mask) | val);
}
static inline u32 i2c_rd32(int fd, u16 reg)
{
u32 val;
i2c_rd(fd, reg, (u8 *)&val, 4);
return val;
}
static inline void i2c_wr32(int fd, u16 reg, u32 val)
{
i2c_wr(fd, reg, (u8 *)&val, 4);
}
static inline int no_signal(int fd)
{
int ret;
ret = i2c_rd8(fd, SYS_STATUS);
vcos_log_error("no_signal read %02X", ret);
return !(ret & MASK_S_TMDS);
}
static inline int no_sync(int fd)
{
int ret;
ret = i2c_rd8(fd, SYS_STATUS);
vcos_log_error("no_sync read %02X", ret);
return !(ret & MASK_S_SYNC);
}
#define CC_RGB_PASSTHROUGH 1
#define CC_RGB_YUV422 2
#define CC_RGB_YUV444 3
#define CC_YUV444_YUV422 4
#define CC_YUV422_YUV444 5
#define COLOR_CONVERSION CC_RGB_PASSTHROUGH
#if COLOR_CONVERSION == CC_RGB_PASSTHROUGH
// RGB through mode
#define r8576 0x00 // 0000 0000 -- RGB full
#define r8573 0x00 // 00000000 -- RGB through
#define r8574 0x00
#define r0004 0x0e24 // 0000 1110 0010 0111
#elif COLOR_CONVERSION == CC_RGB_YUV422
#define r8574 0x08
#define r8573 /* 11000001 */ 0xC1
#define r8576 0x60
#define r0004 0x0ee4
#elif COLOR_CONVERSION == CC_RGB_YUV444
#define r8574 0x08
#define r8573 /* 00000001 */ 0x01
#define r8576 0x60
#define r0004 0x0e24
#elif COLOR_CONVERSION == CC_YUV444_YUV422
#define r8574 0x08
#define r8573 /* 00000001 */ 0x80
#define r8576 0x00
#define r0004 0x0ee4
#elif COLOR_CONVERSION == CC_YUV422_YUV444
#define r8574 0x08
#define r8573 /* 00000001 */ 0x00
#define r8576 0x00
#define r0004 0x0e24
#endif
struct cmds_t {
uint16_t addr;
uint32_t value;
int num_bytes;
};
#define TOSHH2C_720P
#ifdef TOSHH2C_720P
// 720P with quantization flag
static unsigned char TOSHH2C_DEFAULT_EDID[] = //{
"\x00\xff\xff\xff\xff\xff\xff\x00\x52\x62\x88\x88\x00\x88\x88\x88"
"\x1c\x15\x01\x03\x80\x00\x00\x78\x0a\xEE\x91\xA3\x54\x4C\x99\x26"
"\x0F\x50\x54\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
"\x01\x01\x01\x01\x01\x01\x01\x1d\x00\x72\x51\xd0\x1e\x20\x6e\x28"
"\x55\x00\xc4\x8e\x21\x00\x00\x1e\x8c\x0a\xd0\x8a\x20\xe0\x2d\x10"
"\x10\x3e\x96\x00\x13\x8e\x21\x00\x00\x1e\x00\x00\x00\xfc\x00\x54"
"\x6f\x73\x68\x69\x62\x61\x2d\x48\x32\x43\x0a\x20\x00\x00\x00\xFD"
"\x00\x3b\x3d\x0f\x2e\x0f\x1e\x0a\x20\x20\x20\x20\x20\x20\x01\x67"
"\x02\x03\x1A\x42\x47\x84\x13\x03\x02\x07\x06\x01\x23\x09\x07\x07"
"\x66\x03\x0c\x00\x30\x00\x80\xE3\x00\x7F\x8c\x0a\xd0\x8a\x20\xe0"
"\x2d\x10\x10\x3e\x96\x00\xc4\x8e\x21\x00\x00\x18\x8c\x0a\xd0\x8a"
"\x20\xe0\x2d\x10\x10\x3e\x96\x00\x13\x8e\x21\x00\x00\x18\x8c\x0a"
"\xa0\x14\x51\xf0\x16\x00\x26\x7c\x43\x00\x13\x8e\x21\x00\x00\x98"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20";
#else
// 480P EDID
static unsigned char TOSHH2C_DEFAULT_EDID[] = //{
"\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00\x52\x62\x88\x88\x00\x88\x88\x88"
"\x1C\x15\x01\x03\x80\x00\x00\x78\x0A\xEE\x91\xA3\x54\x4C\x99\x26"
"\x0F\x50\x54\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
"\x01\x01\x01\x01\x01\x01\x8C\x0A\xD0\x8A\x20\xE0\x2D\x10\x10\x3E"
"\x96\x00\x13\x8E\x21\x00\x00\x1E\x00\x00\x00\xFC\x00\x54\x6F\x73"
"\x68\x69\x62\x61\x2D\x48\x32\x43\x0A\x20\x00\x00\x00\xFD\x00\x3B"
"\x3D\x0F\x2E\x0F\x00\x0A\x20\x20\x20\x20\x20\x20\x00\x00\x00\x10"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xC8";
#endif
struct cmds_t cmds2[] =
{
/* HDMI specification requires HPD to be pulsed low for 100ms when EDID changed */
{0x8544, 0x01, 1}, // DDC5V detection interlock -- disable
{0x8544, 0x00, 1}, // DDC5V detection interlock -- pulse low
{0x0000, 100, 0xFFFF}, // sleep
{0x8544, 0x10, 1}, // DDC5V detection interlock -- enable
{0x85D1, 0x01, 1}, // Key loading command
{0x8560, 0x24, 1}, // KSV Auto Clear Mode
{0x8563, 0x11, 1}, // EESS_Err auto-unAuth
{0x8564, 0x0F, 1}, // DI_Err (Data Island Error) auto-unAuth
// RGB888 to YUV422 conversion (must)
{0x8574, r8574, 1},
{0x8573, r8573, 1}, // OUT YUV444[7]
// 1010 0001
{0x8576, r8576, 1}, // [7:5] = YCbCr601 Limited ? 3'b011 : 3'b101 (YCbCr 709 Limited)
{0x8600, 0x00, 1}, // Forced Mute Off, Set Auto Mute On
{0x8602, 0xF3, 1}, // AUTO Mute (AB_sel, PCM/NLPCM_chg, FS_chg, PX_chg, PX_off, DVI)
{0x8603, 0x02, 1}, // AUTO Mute (AVMUTE)
{0x8604, 0x0C, 1}, // AUTO Play (mute-->BufInit-->play)
{0x8606, 0x05, 1}, // BufInit start time = 0.5sec
{0x8607, 0x00, 1}, // Disable mute
{0x8620, 0x00, 1}, // [5] = 0: LPCM/NLPCMinformation extraction from Cbit
{0x8640, 0x01, 1}, // CTS adjustment=ON
{0x8641, 0x65, 1}, // Adjustment level 1=1000ppm, Adjustment level 2=2000ppm
{0x8642, 0x07, 1}, // Adjustment level 3=4000ppm
{0x8652, 0x02, 1}, // Data Output Format: [6:4] = 0, 16-bit, [1:0] = 2, I2S Format
{0x8665, 0x10, 1}, // [7:4] 128 Fs Clock divider Delay 1 * 0.1 s, [0] = 0: 44.1/48 KHz Auto switch setting
{0x8709, 0xFF, 1}, // ""FF"": Updated secondary Pkts even if there are errors received
{0x870B, 0x2C, 1}, // [7:4]: ACP packet Intervals before interrupt, [3:0] AVI packet Intervals, [] * 80 ms
{0x870C, 0x53, 1}, // [6:0]: PKT receive interrupt is detected,storage register automatic clear, video signal with RGB and no repeat are set
{0x870D, 0x01, 1}, // InFo Pkt: [7]: Correctable Error is included, [6:0] # of Errors before assert interrupt
{0x870E, 0x30, 1}, // [7:4]: VS packet Intervals before interrupt, [3:0] SPD packet Intervals, [] * 80 ms
{0x9007, 0x10, 1}, // [5:0] Auto clear by not receiving 16V GBD
{0x854A, 0x01, 1}, // HDMIRx Initialization Completed, THIS MUST BE SET AT THE LAST!
};
#define NUM_REGS_CMD2 (sizeof(cmds2)/sizeof(cmds2[0]))
struct cmds_t cmds3[] =
{
{0x0004, r0004 | 0x03, 2}, // Enable tx buffer_size
};
#define NUM_REGS_CMD3 (sizeof(cmds3)/sizeof(cmds3[0]))
struct cmds_t stop_cmds[] =
{
{0x8544, 0x01, 1}, // regain manual control
{0x8544, 0x00, 1}, // disable HPD
//{0x0014, 0x8000, 0x12}, //power island enable (or'ed with register)
//{0x0000, 10, 0xFFFF}, //Sleep 10ms
//{0x0002, 0x0001, 2}, // put to sleep
};
#define NUM_REGS_STOP (sizeof(stop_cmds)/sizeof(stop_cmds[0]))
void write_regs(int fd, struct cmds_t *regs, int count)
{
int i;
for (i=0; i<count; i++)
{
switch(regs[i].num_bytes)
{
case 1:
i2c_wr8(fd, regs[i].addr, (uint8_t)regs[i].value);
break;
case 2:
i2c_wr16(fd, regs[i].addr, (uint16_t)regs[i].value);
break;
case 4:
i2c_wr32(fd, regs[i].addr, regs[i].value);
break;
case 0x11:
i2c_wr8_and_or(fd, regs[i].addr, 0xFF, (uint8_t)regs[i].value);
break;
case 0x12:
i2c_wr16_and_or(fd, regs[i].addr, 0xFFFF, (uint16_t)regs[i].value);
break;
case 0xFFFF:
vcos_sleep(regs[i].value);
break;
default:
vcos_log_error("%u bytes specified in entry %d - not supported", regs[i].num_bytes, i);
break;
}
}
}
void start_camera_streaming(int fd)
{
#ifdef DO_PIN_CONFIG
wiringPiSetupGpio();
pinModeAlt(0, INPUT);
pinModeAlt(1, INPUT);
//Toggle these pin modes to ensure they get changed.
pinModeAlt(28, INPUT);
pinModeAlt(28, 4); //Alt0
pinModeAlt(29, INPUT);
pinModeAlt(29, 4); //Alt0
digitalWrite(41, 1); //Shutdown pin on B+ and Pi2
digitalWrite(32, 1); //LED pin on B+ and Pi2
#endif
#define ENABLE_DATALANE_1 0x0
#define DISABLE_DATALANE_1 0x1
u8 _s_v_format = i2c_rd8(fd, VI_STATUS) & 0x0F;
vcos_log_error("VI_STATUS to select cfg.data_lanes: %u", _s_v_format);
u16 r0006;
u8 r0148;
u32 r0500;
if (_s_v_format < 12)
{
r0006 = 0x0080;
r0148 = DISABLE_DATALANE_1;
r0500 = 0xA3008080;
vcos_log_error("Selected Sub 720p registers");
}
else
{
r0006 = 0x0008;
r0148 = ENABLE_DATALANE_1;
r0500 = 0xA3008082;
vcos_log_error("Selected 720p+ registers");
}
struct cmds_t cmds[] =
{
{0x0004, 0x0000, 2}, // Disable video TX Buffer
// Turn off power and put in reset
// handle.first_boot = VC_FALSE;
{0x0002, 0x0F00, 2}, // Assert Reset, [0] = 0: Exit Sleep, wait
{0x0000, 1, 0xFFFF}, // V054 requires us to wait 1ms for PLL to lock
{0x0002, 0x0000, 2}, // Release Reset, Exit Sleep
{0x0006, r0006, 2}, // FIFO level
{0x0008, 0x005f, 2}, // Audio buffer level -- 96 bytes = 0x5F + 1
{0x0014, 0xFFFF, 2}, // Clear HDMI Rx, CSI Tx and System Interrupt Status
{0x0016, 0x051f, 2}, // Enable HDMI-Rx Interrupt (bit 9), Sys interrupt (bit 5). Disable others. 11-15, 6-7 reserved
{0x0020, 0x8111, 2}, // PRD[15:12], FBD[8:0]
{0x0022, 0x0213, 2}, // FRS[11:10], LBWS[9:8]= 2, Clock Enable[4] = 1, ResetB[1] = 1, PLL En[0]
{0x0004, r0004, 2}, // PwrIso[15], 422 output, send infoframe
{0x0140, 0x0, 4}, //Enable CSI-2 Clock lane
{0x0144, 0x0, 4}, //Enable CSI-2 Data lane 0
{0x0148, r0148, 4}, //Enable CSI-2 Data lane 1
{0x014C, 0x1, 4}, //Disable CSI-2 Data lane 2
{0x0150, 0x1, 4}, //Disable CSI-2 Data lane 3
{0x0210, 0x00002988, 4}, // LP11 = 100 us for D-PHY Rx Init
{0x0214, 0x00000005, 4}, // LP Tx Count[10:0]
{0x0218, 0x00001d04, 4}, // TxClk_Zero[15:8]
{0x021C, 0x00000002, 4}, // TClk_Trail =
{0x0220, 0x00000504, 4}, // HS_Zero[14:8] =
{0x0224, 0x00004600, 4}, // TWAKEUP Counter[15:0]
{0x0228, 0x0000000A, 4}, // TxCLk_PostCnt[10:0]
{0x022C, 0x00000004, 4}, // THS_Trail =
{0x0234, 0x0000001F, 4}, // Enable Voltage Regulator for CSI (4 Data + Clk) Lanes
{0x0204, 0x00000001, 4}, // Start PPI
{0x0518, 0x00000001, 4}, // Start CSI-2 Tx
{0x0500, r0500, 4}, // SetBit[31:29]
//
// 1010 0011 0000 0000 1000 0000 1010 0010
{0x8502, 0x01, 1}, // Enable HPD DDC Power Interrupt
{0x8512, 0xFE, 1}, // Disable HPD DDC Power Interrupt Mask
{0x8513, (uint8_t) ~0x20, 1}, // Receive interrupts for video format change (bit 5)
{0x8515, (uint8_t) ~0x02, 1}, // Receive interrupts for format change (bit 1)
{0x8531, 0x01, 1}, // [1] = 1: RefClk 42 MHz, [0] = 1, DDC5V Auto detection
{0x8540, 0x0A8C, 2}, // SysClk Freq count with RefClk = 27 MHz (0x1068 for 42 MHz, default)
{0x8630, 0x00041eb0, 4}, // Audio FS Lock Detect Control [19:0]: 041EB0 for 27 MHz, 0668A0 for 42 MHz (default)
{0x8670, 0x01, 1}, // SysClk 27/42 MHz: 00:= 42 MHz
{0x8532, 0x80, 1}, // PHY_AUTO_RST[7:4] = 1600 us, PHY_Range_Mode = 12.5 us
{0x8536, 0x40, 1}, // [7:4] Ibias: TBD, [3:0] BGR_CNT: Default
{0x853F, 0x0A, 1}, // [3:0] = 0x0a: PHY TMDS CLK line squelch level: 50 uA
{0x8543, 0x32, 1}, // [5:4] = 2'b11: 5V Comp, [1:0] = 10, DDC 5V active detect delay setting: 100 ms
{0x8544, 0x10, 1}, // DDC5V detection interlock -- enable
{0x8545, 0x31, 1}, // [5:4] = 2'b11: Audio PLL charge pump setting to Normal, [0] = 1: DAC/PLL Power On
{0x8546, 0x2D, 1}, // [7:0] = 0x2D: AVMUTE automatic clear setting (when in MUTE and no AVMUTE CMD received) 45 * 100 ms
{0x85C7, 0x01, 1}, // [6:4] EDID_SPEED: 100 KHz, [1:0] EDID_MODE: Internal EDID-RAM & DDC2B mode
{0x85CB, 0x01, 1}, // EDID Data size read from EEPROM EDID_LEN[10:8] = 0x01, 256-Byte
};
#define NUM_REGS_CMD (sizeof(cmds)/sizeof(cmds[0]))
write_regs(fd, cmds, NUM_REGS_CMD);
// default is 256 bytes, 256 / 16
// write in groups of 16
{
int i, j;
unsigned char checksum = 0;
for (i = 0; i < sizeof(TOSHH2C_DEFAULT_EDID); i += 16)
{
for (j = 0; j < 15; j++)
{
checksum -= TOSHH2C_DEFAULT_EDID[i + j];
}
// if checksum byte
if (i == (7 * 16) || i == (15 * 16))
{
TOSHH2C_DEFAULT_EDID[i + 15] = checksum;
checksum = 0;
}
else
{
checksum -= TOSHH2C_DEFAULT_EDID[i + 15];
}
i2c_wr(fd, 0x8C00 + i, &TOSHH2C_DEFAULT_EDID[i], 16);
}
}
write_regs(fd, cmds2, NUM_REGS_CMD2);
}
void stop_camera_streaming(int fd)
{
write_regs(fd, stop_cmds, NUM_REGS_STOP);
#ifdef DO_PIN_CONFIG
digitalWrite(41, 0); //Shutdown pin on B+ and Pi2
digitalWrite(32, 0); //LED pin on B+ and Pi2
#endif
}
int running = 0;
/**
* buffer header callback function for encoder
*
* Callback will dump buffer data to the specific file
*
* @param port Pointer to port from which callback originated
* @param buffer mmal buffer header pointer
*/
static void encoder_buffer_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
MMAL_STATUS_T status;
vcos_log_error("Buffer %p returned, filled %d, timestamp %llu, flags %04X", buffer, buffer->length, buffer->pts, buffer->flags);
//vcos_log_error("File handle: %p", port->userdata);
int bytes_written = buffer->length;
if (running)
{
FILE *file = (FILE*)port->userdata;
bytes_written = fwrite(buffer->data, 1, buffer->length, file);
fflush(file);
mmal_buffer_header_mem_unlock(buffer);
if (bytes_written != buffer->length)
{
vcos_log_error("Failed to write buffer data (%d from %d)- aborting", bytes_written, buffer->length);
}
mmal_buffer_header_release(buffer);
status = mmal_port_send_buffer(port, buffer);
if(status != MMAL_SUCCESS)
{
vcos_log_error("mmal_port_send_buffer failed on buffer %p, status %d", buffer, status);
}
}
}
/**
* Open a file based on the settings in state
*
* @param state Pointer to state
*/
static FILE *open_filename(const char *filename)
{
FILE *new_handle = NULL;
if (filename)
{
int network = 0, socktype;
if(!strncmp("tcp://", filename, 6))
{
network = 1;
socktype = SOCK_STREAM;
}
if(!strncmp("udp://", filename, 6))
{
network = 1;
socktype = SOCK_DGRAM;
}
if(network)
{
filename += 6;
char *colon;
colon = strchr(filename, ':');
int sfd = socket(AF_INET, socktype, 0);
if(sfd < 0)
{
perror("socket");
}
unsigned short port;
sscanf(colon + 1, "%hu", &port);
*colon = 0;
fprintf(stderr, "Connecting to %s:%hu\n", filename, port);
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
inet_aton(filename, &saddr.sin_addr);
if(connect(sfd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in)) < 0)
{
perror("connect");
}
new_handle = fdopen(sfd, "w");
}
else
{
new_handle = fopen(filename, "wb");
}
}
return new_handle;
}
/* Clean up after Ctrl+c */
void signal_callback_handler(int signum)
{
printf("Caught signal %d\nCleanly exiting\n", signum);
called_quit = 1;
// goto exit;
// exit(signum);
}
int main ( int argc, char *argv[] )
{
int8_t option_index = 0;
u32 sleep_duration = 0; //strtol(argv[1], &ptr, 10);
char *_filename = "test_encode.h264";
while ((option_index = getopt(argc, argv, "ht:o:")) != -1)
{
switch (option_index)
{
case 't':
sleep_duration = atoi(optarg); // not type safe
break;
case 'o':
if (strcmp(optarg, "") != 0)
{
_filename = (char*) malloc(1 + strlen(optarg) + strlen(".h264"));
strcpy(_filename, optarg);
strcat(_filename, ".h264");
}
break;
case 'h':
printf("Available options:\n\n");
printf("\t-t\tDefaults to 0 ms which playbacks until ctrl+c.\n");
printf("\t-o\tOutput filename, default is 'test_encode.h264'.\n");
printf("\t-h\tHelp\n");
return 1;
default:
printf("Incorrect option!\n");
return 1;
}
}
printf("Filename is %s\n", _filename);
if (sleep_duration != 0)
printf("Playing back for %u ms.\n", sleep_duration);
else
printf("Playing back indefinitely\n");
u8 playthroughs = 0;
MMAL_COMPONENT_T *rawcam, *render, *isp, *splitter, *encoder;
MMAL_STATUS_T status;
MMAL_PORT_T *output, *input, *isp_input, *isp_output, *encoder_input, *encoder_output;
MMAL_POOL_T *pool;
MMAL_CONNECTION_T *connection[4] = {0};
MMAL_PARAMETER_CAMERA_RX_CONFIG_T rx_cfg = {{MMAL_PARAMETER_CAMERA_RX_CONFIG, sizeof(rx_cfg)}};
MMAL_PARAMETER_CAMERA_RX_TIMING_T rx_timing = {{MMAL_PARAMETER_CAMERA_RX_TIMING, sizeof(rx_timing)}};
int i2c_fd;
unsigned int width, height, fps, frame_interval;
unsigned int frame_width, frame_height;
/* CALL THE SIGNAL HANDLER FUNCTION ON A Ctrl-C EXIT */
signal(SIGINT, signal_callback_handler);
i2c_fd = open("/dev/i2c-0", O_RDWR);
if (!i2c_fd)
{
vcos_log_error("Couldn't open I2C device");
return -1;
}
if(ioctl(i2c_fd, I2C_SLAVE, 0x0F) < 0)
{
vcos_log_error("Failed to set I2C address");
return -1;
}
bcm_host_init();
vcos_log_register("RaspiRaw", VCOS_LOG_CATEGORY);
status = mmal_component_create("vc.ril.rawcam", &rawcam);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create rawcam");
return -1;
}
status = mmal_component_create("vc.ril.video_render", &render);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create render");
return -1;
}
status = mmal_component_create("vc.ril.isp", &isp);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create isp");
return -1;
}
status = mmal_component_create("vc.ril.video_splitter", &splitter);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create isp");
return -1;
}
status = mmal_component_create("vc.ril.video_encode", &encoder);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create encoder");
return -1;
}
loop:
output = rawcam->output[0];
isp_input = isp->input[0];
isp_output = isp->output[0];
encoder_input = encoder->input[0];
encoder_output = encoder->output[0];
input = render->input[0];
// setup CSI configs on rawcam
status = mmal_port_parameter_get(output, &rx_cfg.hdr);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to get cfg");
goto component_destroy;
}
rx_cfg.image_id = CSI_IMAGE_ID;
u8 _s_v_format = i2c_rd8(i2c_fd, VI_STATUS) & 0x0F;
vcos_log_error("VI_STATUS to select cfg.data_lanes: %u", _s_v_format);
if (_s_v_format < 12)
{
rx_cfg.data_lanes = 1;
vcos_log_error("rx_cfg.data_lanes = 1");
}
else
{
rx_cfg.data_lanes = 2;
vcos_log_error("rx_cfg.data_lanes = 2");
}
rx_cfg.unpack = UNPACK;
rx_cfg.pack = PACK;
rx_cfg.embedded_data_lines = 128;
vcos_log_error("Set pack to %d, unpack to %d", rx_cfg.unpack, rx_cfg.pack);
status = mmal_port_parameter_set(output, &rx_cfg.hdr);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to set cfg");
goto component_destroy;
}
vcos_log_error("Enable rawcam....");
status = mmal_component_enable(rawcam);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to enable");
goto component_destroy;
}
status = mmal_port_parameter_set_boolean(output, MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to set zero copy");
goto component_disable;
}
// end setup rawcam
vcos_log_error("Enable isp....");
status = mmal_component_enable(isp);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to enable");
goto component_destroy;
}
status = mmal_port_parameter_set_boolean(isp_output, MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to set zero copy");
goto component_disable;
}
vcos_log_error("Enable splitter....");
status = mmal_component_enable(splitter);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to enable");
goto component_destroy;
}
status = mmal_port_parameter_set_boolean(splitter->output[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to set zero copy");
goto component_disable;
}
start_camera_streaming(i2c_fd);
vcos_sleep(500); //Give a chance to detect signal
vcos_log_error("Waiting to detect signal...");
int count=0;
while((count<20) && (no_sync(i2c_fd) || no_signal(i2c_fd)))
{
vcos_sleep(200);
count++;
}
vcos_log_error("Signal reported");
width = ((i2c_rd8(i2c_fd, DE_WIDTH_H_HI) & 0x1f) << 8) +
i2c_rd8(i2c_fd, DE_WIDTH_H_LO);
height = ((i2c_rd8(i2c_fd, DE_WIDTH_V_HI) & 0x1f) << 8) +
i2c_rd8(i2c_fd, DE_WIDTH_V_LO);
frame_width = ((i2c_rd8(i2c_fd, H_SIZE_HI) & 0x1f) << 8) +
i2c_rd8(i2c_fd, H_SIZE_LO);
frame_height = (((i2c_rd8(i2c_fd, V_SIZE_HI) & 0x3f) << 8) +
i2c_rd8(i2c_fd, V_SIZE_LO)) / 2;
if (playthroughs == 0)
{
playthroughs++;
vcos_log_error("First playthrough, Goto Loop");
goto loop;
}
/* frame interval in milliseconds * 10
* Require SYS_FREQ0 and SYS_FREQ1 are precisely set */
frame_interval = ((i2c_rd8(i2c_fd, FV_CNT_HI) & 0x3) << 8) +
i2c_rd8(i2c_fd, FV_CNT_LO);
fps = (frame_interval > 0) ?
(10000/frame_interval) : 0;
vcos_log_error("Signal is %u x %u, frm_interval %u, so %u fps", width, height, frame_interval, fps);
vcos_log_error("Frame w x h is %u x %u", frame_width, frame_height);
output->format->es->video.crop.width = width;
output->format->es->video.crop.height = height;
output->format->es->video.width = VCOS_ALIGN_UP(width, 32); //VCOS_ALIGN_UP(WIDTH, 32);
output->format->es->video.height = VCOS_ALIGN_UP(height, 16); //VCOS_ALIGN_UP(HEIGHT, 16);
output->format->es->video.frame_rate.num = 10000;
output->format->es->video.frame_rate.den = frame_interval ? frame_interval : 10000;
output->format->encoding = ENCODING;
status = mmal_port_format_commit(output);
if(status != MMAL_SUCCESS)
{
vcos_log_error("output: Failed port_format_commit");
mmal_log_dump_port(output);
goto component_disable;
}
output->buffer_size = output->buffer_size_recommended;
output->buffer_num = 3;
vcos_log_error("output: buffer size is %d bytes, num %d", output->buffer_size, output->buffer_num);
vcos_log_error("Create connection rawcam output to isp input....");
status = mmal_connection_create(&connection[0], output, isp_input, MMAL_CONNECTION_FLAG_TUNNELLING);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create connection status %d: rawcam->isp", status);
goto component_disable;
}
// ISP output port does not follow input, so do have to set that one up.
mmal_format_copy(isp_output->format, isp_input->format);
isp_output->format->encoding = MMAL_ENCODING_I420;
vcos_log_error("Setting isp output port format");
status = mmal_port_format_commit(isp_output);
isp_output->buffer_size = isp_output->buffer_size_recommended;
isp_output->buffer_num = isp_output->buffer_num_recommended;
if (status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create connection status %d: rawcam->isp", status);
goto component_disable;
}
// Encoder setup
vcos_log_error("Create connection isp output to splitter input....");
status = mmal_connection_create(&connection[1], isp_output, splitter->input[0], MMAL_CONNECTION_FLAG_TUNNELLING);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create connection status %d: isp->splitter", status);
goto component_disable;
}
vcos_log_error("Create connection splitter output to render input....");
status = mmal_connection_create(&connection[2], splitter->output[0], input, MMAL_CONNECTION_FLAG_TUNNELLING);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create connection status %d: splitter->render", status);
goto component_disable;
}
vcos_log_error("Create connection splitter output2 to encoder input....");
status = mmal_connection_create(&connection[3], splitter->output[1], encoder_input, MMAL_CONNECTION_FLAG_TUNNELLING);
if(status != MMAL_SUCCESS)
{
vcos_log_error("Failed to create connection status %d: splitter->encoder", status);
goto component_disable;
}
// Only supporting H264 at the moment
encoder_output->format->encoding = MMAL_ENCODING_H264;
encoder_output->format->bitrate = 17000000;
encoder_output->buffer_size = encoder_output->buffer_size_recommended;
if (encoder_output->buffer_size < encoder_output->buffer_size_min)
encoder_output->buffer_size = encoder_output->buffer_size_min;
encoder_output->buffer_num = 8; //encoder_output->buffer_num_recommended;
if (encoder_output->buffer_num < encoder_output->buffer_num_min)
encoder_output->buffer_num = encoder_output->buffer_num_min;
// We need to set the frame rate on output to 0, to ensure it gets
// updated correctly from the input framerate when port connected
encoder_output->format->es->video.frame_rate.num = fps;//0;
encoder_output->format->es->video.frame_rate.den = 1;
// Commit the port changes to the output port
status = mmal_port_format_commit(encoder_output);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Unable to set format on encoder output port");
}
{
MMAL_PARAMETER_VIDEO_PROFILE_T param;
param.hdr.id = MMAL_PARAMETER_PROFILE;
param.hdr.size = sizeof(param);
param.profile[0].profile = MMAL_VIDEO_PROFILE_H264_HIGH;//state->profile;
param.profile[0].level = MMAL_VIDEO_LEVEL_H264_4; // This is the only value supported
status = mmal_port_parameter_set(encoder_output, ¶m.hdr);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Unable to set H264 profile");
}
}
if (mmal_port_parameter_set_boolean(encoder_input, MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT, 1) != MMAL_SUCCESS)
{
vcos_log_error("Unable to set immutable input flag");
// Continue rather than abort..
}
//set INLINE HEADER flag to generate SPS and PPS for every IDR if requested
if (mmal_port_parameter_set_boolean(encoder_output, MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER, 0) != MMAL_SUCCESS)
{
vcos_log_error("failed to set INLINE HEADER FLAG parameters");
// Continue rather than abort..
}