forked from avivgr/teensy_uvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uvc.c
1147 lines (1046 loc) · 39 KB
/
uvc.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
/*
Author: Aviv Greenberg - www.linkedin.com/in/avivgr/
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdio.h>
#include "uart.h"
#include "uvc.h"
#define DBG(x) uart_print(x)
#define DBG_ONCE(x) { \
static uint8_t __cnt = 0;\
if(__cnt == 0) {uart_print(x);__cnt++;} \
}
char _buff[64];
#define DBGV(format, ...) \
snprintf_P(_buff, sizeof(_buff)-1, PSTR(format), __VA_ARGS__);\
uart_print_S(_buff);
#define STR_MANUFACTURER L"Acme"
#define STR_MANUFACTURER_I 1
#define STR_PRODUCT L"Teensy UVC Camera"
#define STR_PRODUCT_I 2
#define VENDOR_ID 0x046e
#define PRODUCT_ID 0x375d
// Endpoint config
#define ENDPOINT0_SIZE 16
#define UVC_INTERFACE 0
#define UVC_TX_ENDPOINT 1
#define UVC_TX_BUFFER EP_DOUBLE_BUFFER
#define UVC_TX_SIZE 256
#define BPP 12
#define HEIGHT 120L
#define WIDTH 160L
#define FRAME_SIZE (HEIGHT*WIDTH*BPP/8)
static const uint8_t PROGMEM endpoint_config_table[] = {
1, EP_TYPE_ISOCHRONOUS_IN, EP_SIZE(UVC_TX_SIZE) | UVC_TX_BUFFER,
0,
0,
0
};
static const uint8_t PROGMEM device_descriptor[] = {
18, // bLength
USB_DT_DEVICE, // bDescriptorType
W_TO_B(0x0200), // bcdUSB
0xef, // bDeviceClass = Miscellaneous Device Class
0x02, // bDeviceSubClass = Common Class
0x01, // bDeviceProtocol = Interface Association Descriptor
ENDPOINT0_SIZE, // bMaxPacketSize0
W_TO_B(VENDOR_ID), // idVendor
W_TO_B(PRODUCT_ID), // idProduct
W_TO_B(0x0100), // bcdDevice
STR_MANUFACTURER_I, // iManufacturer
STR_PRODUCT_I, // iProduct
0, // iSerialNumber
1 // bNumConfigurations
};
#define CONFIG1_DESC_SIZE (9+8+9+13+17+9+7+12+9+14+27+46+9+7)
#define VC_DESC_SIZE (13+17+9+7+12)
#define VS_DESC_SIZE (14+27+46)
/* Terminal IDs */
#define INPUT_TERMINAL_ID 0x01
#define OUTPUT_TERMINAL_ID 0x02
#define SU_TERMINAL_ID 0x03
#define PU_TERMINAL_ID 0x04
#define VIDEOC_IFACE 0x00
#define VIDEOS_IFACE 0x01
static const uint8_t PROGMEM config1_descriptor[] = {
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
9, // bLength;
USB_DT_CONFIG, // bDescriptorType;
W_TO_B(CONFIG1_DESC_SIZE), // wTotalLength
2, // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0x80, // bmAttributes = Bus-powered device, no remote wakeup capability
0xFA, // bMaxPower
// Interface Association Descriptor
8, // bLength
USB_DT_IAD, // bDescriptorType
0x00, // bFirstInterface = Interface number of the VideoControl interface for this function
0x02, // bInterfaceCount = Number of contiguous Video interfaces that are for this function
CC_VIDEO, // bFunctionClass = CC_VIDEO
UVC_SC_VIDEO_INTERFACE_COLLECTION,// bFunctionSubClass = SC_VIDEO_INTERFACE_COLLECTION
0x00, // bFunctionProtocol = Not used. Must be set to PC_PROTOCOL_UNDEFINED.
STR_PRODUCT_I, // iFunction = Index to product string descriptor
// Standard VideoControl Interface Descriptor
9, // bLength = Size of this descriptor, in bytes.
USB_DT_INTERFACE, // bDescriptorType = INTERFACE descriptor type
VIDEOC_IFACE, // bInterfaceNumber = Index of this interface
0x00, // bAlternateSetting = Index of this setting
0x00, // bNumEndpoints = 0 endpoints (NO interrupt endpoint)
CC_VIDEO, // bInterfaceClass = CC_VIDEO
UVC_SC_VIDEOCONTROL,// bInterfaceSubClass = SC_VIDEOCONTROL
0x00, // bInterfaceProtocol = Not used. Must be set to PC_PROTOCOL_UNDEFINED.
STR_PRODUCT_I, // iInterface = Index to string descriptor that contains the product string
// Class-specific VideoControl Interface Descriptor
13, // bLength = Size of this descriptor, in bytes.
UVC_DT_CS_INTERFACE,// bDescriptorType = UVC_DT_CS_INTERFACE
UVC_VC_HEADER, // bDescriptorSubType = VC_HEADER subtype
W_TO_B(0x0110), // bcdUVC = 0x0110 version 1.1.
W_TO_B(VC_DESC_SIZE),// wTotalLength = Total size of class-specific descriptors
DW_TO_B(0x005B8D80),// dwClockFrequency = deprecated. 0x005B8D80 This device will provide timestamps and a device clock reference based on a 6MHz clock.
0x01, // bInCollection = Number of streaming interfaces.
0x01, // baInterfaceNr(1) = VideoStreaming interface 1 belongs to this VideoControl interface.
// Input Terminal Descriptor (Camera)
17, // bLength = Size of this descriptor, in bytes.
UVC_DT_CS_INTERFACE,// bDescriptorType = UVC_DT_CS_INTERFACE
UVC_VC_INPUT_TERMINAL,// bDescriptorSubtype = VC_INPUT_TERMINAL subtype
INPUT_TERMINAL_ID, // bTerminalID = ID of this input terminal
W_TO_B(UVC_ITT_CAMERA),// wTerminalType = ITT_CAMERA type. This terminal is a camera terminal representing the CCD sensor.
0x00, // bAssocTerminal = No association
0x00, // iTerminal = Unused
W_TO_B(0x0000), // wObjectiveFocalLengthMin = No optical zoom supported
W_TO_B(0x0000), // wObjectiveFocalLengthMax = No optical zoom supported
W_TO_B(0x0000), // wOcularFocalLength = No optical zoom supported
0x02, // bControlSize = The size of the bmControls is 2 bytes (this terminal doesn’t implement any controls).
W_TO_B(0x0000), // bmControls = No controls are supported.
// Output Terminal Descriptor
9, // bLength = Size of this descriptor, in bytes.
UVC_DT_CS_INTERFACE,// bDescriptorType = UVC_DT_CS_INTERFACE
UVC_VC_OUTPUT_TERMINAL, // bDescriptorSubtype = VC_OUTPUT_TERMINAL
OUTPUT_TERMINAL_ID, // bTerminalID = ID of this terminal
W_TO_B(UVC_TT_STREAMING),// wTerminalType = TT_STREAMING type. This terminal is a USB streaming terminal.
0x00, // bAssocTerminal = No association
PU_TERMINAL_ID, // bSourceID = The input pin of this unit is connected to the output pin of this unit.
0x00, // iTerminal = Unused
// Selector Unit Terminal Descriptor
7, // bLength
UVC_DT_CS_INTERFACE,// bDescriptorType
UVC_VC_SELECTOR_UNIT,// bDescriptorSubtype (SELECTOR_UNIT)
SU_TERMINAL_ID, // bUnitID
1, // bNrInPins
INPUT_TERMINAL_ID, // baSource( 0)
0, // iSelector
// Processing Unit Descriptor
12, // bLength = Size of this descriptor, in bytes.
UVC_DT_CS_INTERFACE, // bDescriptorType = UVC_DT_CS_INTERFACE
UVC_VC_PROCESSING_UNIT,// bDescriptorSubtype = VC_PROCESSING_UNIT
PU_TERMINAL_ID, // bUnitID = ID of this unit
SU_TERMINAL_ID, // bSourceID = This input pin of this unit is connected to the output pin of other unit.
0x00, 0x00, // wMaxMultiplier = unused
0x02, // bControlSize = Size of the bmControls field, in bytes.
0x01, 0x00, // bmControls = Brightness control supported
0x00, // iProcessing = Unused
0x00, // bmVideoStandards
// Standard VideoStreaming Interface Descriptor - - Operational Alternate Setting 0
9, // bLength = Size of this descriptor, in bytes.
USB_DT_INTERFACE, // bDescriptorType = INTERFACE descriptor type
VIDEOS_IFACE, // bInterfaceNumber = Index of this interface
0x00, // bAlternateSetting = Index of this alternate setting
0x00, // bNumEndpoints = 0 endpoints – no bandwidth used
CC_VIDEO, // bInterfaceClass = CC_VIDEO
UVC_SC_VIDEOSTREAMING,// bInterfaceSubClass = SC_VIDEOSTREAMING
0x00, // bInterfaceProtocol = PC_PROTOCOL_UNDEFINED
0x00, // iInterface = Unused
// Class-specific VideoStreaming Header Descriptor (Input)
14, // bLength = Size of this descriptor, in bytes.
UVC_DT_CS_INTERFACE,// bDescriptorType = UVC_DT_CS_INTERFACE
UVC_VS_INPUT_HEADER,// bDescriptorSubtype = VS_INPUT_HEADER.
0x01, // bNumFormats = One format descriptor follows.
W_TO_B(VS_DESC_SIZE),// wTotalLength = Total size of class-specific VideoStreaming interface descriptors
UVC_TX_ENDPOINT|0x80, // bEndpointAddress = Address of the isochronous endpoint used for video data
0x00, // bmInfo = No dynamic format change supported
OUTPUT_TERMINAL_ID, // bTerminalLink = This VideoStreaming interface supplies terminal ID 2 (Output Terminal).
0x01, // bStillCaptureMethod = Device supports still image capture method 1.
0x00, // bTriggerSupport = Hardware trigger supported for still image capture
0x00, // bTriggerUsage = Hardware trigger should initiate a still image capture.
0x01, // bControlSize = Size of the bmaControls field
0x00, // bmaControls = No VideoStreaming specific controls are supported.
// Class-specific VideoStreaming Format Descriptor
27, // bLength = Size of this descriptor, in bytes.
UVC_DT_CS_INTERFACE, // bDescriptorType = UVC_DT_CS_INTERFACE
UVC_VS_FORMAT_UNCOMPRESSED, // bDescriptorSubtype = UVC_VS_FORMAT_UNCOMPRESSED
1, // bFormatIndex
1, // bNumFrameDescriptors
'Y', 'V', '1', '2', // guidFormat
0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xaa,
0x00, 0x38, 0x9b, 0x71,
BPP, // bBitsPerPixel
1, // bDefaultFrameIndex
0, // bAspectRatioX
0, // bAspectRatioY
0x00, // bmInterlaceFlags
0, // bCopyProtect
// Class-specific VideoStreaming Frame Descriptor
46, // bLength
UVC_DT_CS_INTERFACE,// bDescriptorType
UVC_VS_FRAME_UNCOMPRESSED,// bDescriptorSubtype
1, // bFrameIndex
0x01, // bmCapabilities
W_TO_B(WIDTH), // wWidth
W_TO_B(HEIGHT), // wHeight
DW_TO_B(((FRAME_SIZE)*8*1)),// dwMinBitRate
DW_TO_B(((FRAME_SIZE)*8*30)),// dwMaxBitRate
DW_TO_B((FRAME_SIZE)),// dwMaxVideoFrameBufferSize
DW_TO_B(1333333), // dwDefaultFrameInterval
5, // bFrameIntervalType
DW_TO_B(333333), // dwFrameInterval( 0)
DW_TO_B(500000), // dwFrameInterval( 1)
DW_TO_B(666666), // dwFrameInterval( 2)
DW_TO_B(1000000), // dwFrameInterval( 3)
DW_TO_B(1333333), // dwFrameInterval( 4)
// Standard VS Interface Descriptor - Operational Alternate Setting 1
9, // bLength = Size of this descriptor, in bytes.
USB_DT_INTERFACE, // bDescriptorType = INTERFACE descriptor type
VIDEOS_IFACE, // bInterfaceNumber = Index of this interface
0x01, // bAlternateSetting = Index of this alternate setting
0x01, // bNumEndpoints = 0 endpoints – no bandwidth used
CC_VIDEO, // bInterfaceClass = CC_VIDEO
UVC_SC_VIDEOSTREAMING,// bInterfaceSubClass = SC_VIDEOSTREAMING
0x00, // bInterfaceProtocol = PC_PROTOCOL_UNDEFINED
0x00, // iInterface = Unused
// Standard VS Isochronous Video Data Endpoint Descriptor
7, // bLength = Size of this descriptor, in bytes.
USB_DT_ENDPOINT, // bDescriptorType = ENDPOINT
UVC_TX_ENDPOINT|0x80,// bEndpointAddress = IN endpoint 2
0x01 | 12, // bmAttributes = Isochronous transfer type, Async sync type.
W_TO_B(UVC_TX_SIZE),// wMaxPacketSize = Max packet size
1, // bInterval = One frame interval
};
static const uint8_t PROGMEM device_qualifier_desc[] = {
10, // bLength
USB_DT_DEVICE_QUALIFIER, // bDescriptorType
W_TO_B(0x0200), // bcdUSB
239, // bDeviceClass 239 Miscellaneous Device
2, // bDeviceSubClass
1, // bDeviceProtocol Interface Association
ENDPOINT0_SIZE, // bMaxPacketSize0
1, // bNumConfigurations
0 // Device Status: 0x0000(Bus Powered)
};
static const uint8_t PROGMEM other_speed_descriptor[] = {
9, // bLength;
USB_DT_OTHER_SPEED_CONFIGURATION, // bDescriptorType;
W_TO_B(CONFIG1_DESC_SIZE), // wTotalLength
2, // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0x80, // bmAttributes = Bus-powered device, no remote wakeup capability
0xFA, // bMaxPower
};
struct usb_string_descriptor_struct {
uint8_t bLength;
uint8_t bDescriptorType;
int16_t wString[];
};
static const struct usb_string_descriptor_struct PROGMEM string0 = {
4,
USB_DT_STRING,
{0x0409}
};
static const struct usb_string_descriptor_struct PROGMEM string1 = {
sizeof(STR_MANUFACTURER),
USB_DT_STRING,
STR_MANUFACTURER
};
static const struct usb_string_descriptor_struct PROGMEM string2 = {
sizeof(STR_PRODUCT),
USB_DT_STRING,
STR_PRODUCT
};
// This table defines which descriptor data is sent for each specific
// request from the host (in wValue and wIndex).
static const struct descriptor_list_struct {
uint16_t wValue;
uint16_t wIndex;
const uint8_t *addr;
uint8_t length;
} PROGMEM descriptor_list[] = {
{MKWORD(USB_DT_DEVICE, 0x00), 0x0000, device_descriptor, sizeof(device_descriptor)},
{MKWORD(USB_DT_CONFIG, 0x00), 0x0000, config1_descriptor, sizeof(config1_descriptor)},
{MKWORD(USB_DT_DEVICE_QUALIFIER, 0x00), 0x0000, device_qualifier_desc, sizeof(device_qualifier_desc)},
{MKWORD(USB_DT_OTHER_SPEED_CONFIGURATION, 0x00), 0x0000, other_speed_descriptor, /* size ok */ sizeof(config1_descriptor)},
{MKWORD(USB_DT_STRING, 0x00), 0x0000, (const uint8_t *)&string0, 4},
{MKWORD(USB_DT_STRING, STR_MANUFACTURER_I), 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
{MKWORD(USB_DT_STRING, STR_PRODUCT_I), 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
};
#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
#define VSPC_HINT_dwFrameInterval (1<<0)
#define VSPC_HINT_wKeyFrameRate (1<<1)
#define VSPC_HINT_wPFrameRate (1<<2)
#define VSPC_HINT_wCompQuality (1<<3)
#define VSPC_HINT_wCompWindowSize (1<<4)
#define VSPC_FINFO_FID_REQUIRED (1<<0)
#define VSPC_FINFO_EOF (1<<1)
/* 34 bytes */
struct vs_probe_commit
{
uint16_t bmHint; // Bitfield indicating what fields shall be kept fixed
uint8_t bFormatIndex; // Video format index from a format descriptor.
uint8_t bFrameIndex; // Video frame index from a frame descriptor.
uint32_t dwFrameInterval; // Frame interval in 100 ns units.
uint16_t wKeyFrameRate; // Key frame rate in key-frame per video-frame units.
uint16_t wPFrameRate; // PFrame rate in PFrame/key frame units.
uint16_t wCompQuality; // Compression quality control in abstract units 0-10000 (highest).
uint16_t wCompWindowSize; // Window size for average bit rate control.
uint16_t wDelay; // Internal interface latency (ms) from data capture to presentation on USB.
uint32_t dwMaxVideoFrameSize;// Maximum video frame or codec-specific segment size in bytes.
uint32_t dwMaxPayloadTransferSize;// maximum number of i/o bytes the device handle in a single payload
uint32_t dwClockFrequency; // The device clock frequency in Hz
uint8_t bmFramingInfo; // Bitfield control
uint8_t bPreferedVersion; // The preferred payload format version
uint8_t bMinVersion; // The min payload format version
uint8_t bMaxVersion; // The max payload format version
};
#define PC_INIT(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16)\
{p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16}
// zero when we are not configured, non-zero when enumerated
static volatile uint8_t usb_configuration = 0;
static volatile uint8_t transmit_flush_timer = 0;
static uint8_t last_error = UVC_ERR_SUCCESS;
static volatile uint16_t videos_alt_setting = 0;
static volatile uint8_t streaming = 0;
static volatile uint8_t fid = 0;
/* Helper macros for UVC controls.
You can use DEFINE_UVC_CONTROL to define a uvc control and greatly simplify
access through needed GET_XXX / SET_CUR uvc requests. The read-only fields
(.e.g MIN/MAX etc) will be stored in program memory to save RAM.
Use CTL_CALL to dispatch uvc requests for the control.
*/
#define DEFINE_UVC_CONTROL(name, type, _min, _max, _res, _def, __len, _info_flags) \
type name = _def;\
const uint8_t PROGMEM name##_len = __len;\
static const struct name##_ctl_info_ {\
type min;\
type max;\
type res;\
type def;\
uint8_t info_flags;\
} PROGMEM name##_ctl_info = {\
_min, _max, _res, _def, _info_flags \
}
#define CTL_CALL(name, _bRequest, _wLength)\
ctl_req(_bRequest, (uint8_t *)&name, (const uint8_t *)& name##_ctl_info, \
name##_len, _wLength)
/* Control definitions */
DEFINE_UVC_CONTROL(brightness, int16_t, 0, 100, 1, 0, 2, GINFO_SUPPORT_GET | GINFO_SUPPORT_SET);
DEFINE_UVC_CONTROL(probe_commit, struct vs_probe_commit,
/* min */ PC_INIT(0,1,1, 333333,0,0,0,0,0,FRAME_SIZE,UVC_TX_SIZE,16000000L,0,0,0,0),
/* max */ PC_INIT(0,1,1,1333333,0,0,0,0,0,FRAME_SIZE,UVC_TX_SIZE,16000000L,0,0,0,0),
/* res */ PC_INIT(0,1,1, 333333,0,0,0,0,0, 0, 0, 0,0,0,0,0),
/* def */ PC_INIT(0,1,1, 333333,0,0,0,0,0,FRAME_SIZE,UVC_TX_SIZE,16000000L,0,0,0,0),
34,
GINFO_SUPPORT_GET | GINFO_SUPPORT_SET
);
// initialize USB
void usb_init(void)
{
HW_CONFIG();
USB_CONFIG(); // enable USB
PLL_CONFIG(); // config PLL
while (!(PLLCSR & (1<<PLOCK)))
; // wait for PLL lock
USB_CONFIG(); // start USB clock
UDCON = 0; // enable attach resistor
usb_configuration = 0;
UDIEN = (1<<EORSTE);
sei();
}
static inline uint8_t usb_configured(void)
{
return usb_configuration;
}
// Misc functions to wait for ready and send/receive packets
static inline void usb_wait_in_ready(void)
{
while (!(UEINTX & (1<<TXINI))) ;
}
static inline uint16_t usb_fnum(void)
{
return UDFNUM;
}
static inline int usb_wait_in_ready_timeo(uint8_t ms)
{
uint16_t last_fnum = usb_fnum();
uint16_t fnum;
while (1) {
if((UEINTX & (1<<TXINI)))
return 0;
fnum = usb_fnum();
/* frame advances every 1ms in full speed */
if(fnum != last_fnum) {
if(--ms == 0)
return -1;
last_fnum = fnum;
}
}
}
static inline void usb_ack_in(void)
{
UEINTX &= ~(1<<TXINI);
}
/* Cuases hw to switch banks */
static inline void usb_ack_bank(void)
{
//UEINTX = ~((int8_t)(1<<FIFOCON));
UEINTX &= (uint8_t)~((1 << FIFOCON));
}
static inline uint8_t usb_rw_allowed(void)
{
return UEINTX & (1<<RWAL);
}
static inline uint16_t usb_available(void)
{
uint16_t ret = UEBCHX;
ret = (ret << 8) | UEBCLX;
return UVC_TX_SIZE-ret;
}
static inline void usb_wait_receive_out(void)
{
while (!(UEINTX & (1<<RXOUTI))) ;
}
static inline void usb_ack_out(void)
{
UEINTX = ~(1<<RXOUTI);
}
static inline void usb_stall(void)
{
UECONX = (1<<STALLRQ)|(1<<EPEN);
}
#define NUM_COLORS 5
#define COLUMN_WIDTH (WIDTH/NUM_COLORS)
struct color_info {
uint8_t c[NUM_COLORS][3];
};
const struct color_info colors =
{{
/* Y U V */
/* white */ {235, 128, 128},
/* red */ { 81, 90, 240},
/* green */ {145, 54, 34},
/* blue */ { 41, 240, 110},
/* black */ { 16, 128, 128}
}};
uint8_t cur_col_offset = 0;
uint8_t cur_start_col = 0;
static inline uint8_t next_color(uint8_t col)
{
if(col >= NUM_COLORS-1)
return 0;
else
return col+1;
}
static inline void send_yv12_frame(void)
{
uint16_t h,w,lastw;
uint8_t write_hdr = 1;
uint8_t hdr;
uint8_t color, colcnt;
uint8_t br = LSB(brightness);
usb_wait_in_ready();
/* Y plane (h*w bytes) */
for(h=0; h < HEIGHT; h++) {
w = lastw = WIDTH;
color = cur_start_col;
colcnt = COLUMN_WIDTH - cur_col_offset;
do {
if(!usb_rw_allowed()) {
usb_ack_bank();
if(usb_wait_in_ready_timeo(10) < 0)
return;
usb_ack_in();
lastw = w;
write_hdr = 1;
} else {
if(write_hdr) {
/* Write header */
hdr = UVC_PHI_EOH | (fid&1);
UEDATX = 2; // write header len
UEDATX = hdr;
write_hdr = 0;
}
UEDATX=colors.c[color][0] + br; // Y
w--;
if(--colcnt == 0) {
color = next_color(color);
colcnt = COLUMN_WIDTH;
}
}
} while(w > 0);
}
/* U plane (h/2*w/2 bytes) */
for(h=0; h < HEIGHT/2; h++) {
w = lastw = WIDTH/2;
color = cur_start_col;
colcnt = COLUMN_WIDTH - cur_col_offset;
do {
if(!usb_rw_allowed()) {
usb_ack_bank();
if(usb_wait_in_ready_timeo(10) < 0)
return;
usb_ack_in();
lastw = w;
write_hdr = 1;
} else {
if(write_hdr) {
/* Write header */
hdr = UVC_PHI_EOH | (fid&1);
UEDATX = 2; // write header len
UEDATX = hdr;
write_hdr = 0;
}
UEDATX=colors.c[color][1]; // U
w--;
if(colcnt <= 2) {
color = next_color(color);
colcnt = COLUMN_WIDTH;
} else {
colcnt -= 2;
}
}
} while(w > 0);
}
/* V plane (h/2*w/2 bytes) */
for(h=0; h < HEIGHT/2; h++) {
w = lastw = WIDTH/2;
color = cur_start_col;
colcnt = COLUMN_WIDTH - cur_col_offset;
do {
if(!usb_rw_allowed()) {
usb_ack_bank();
if(usb_wait_in_ready_timeo(10) < 0)
return;
usb_ack_in();
lastw = w;
write_hdr = 1;
} else {
if(write_hdr) {
/* Write header */
hdr = UVC_PHI_EOH | (fid&1);
if(h==HEIGHT/2-1 && w < UVC_TX_SIZE)
hdr |= UVC_PHI_EOF;
UEDATX = 2; // write header len
UEDATX = hdr;
write_hdr = 0;
}
UEDATX=colors.c[color][2]; // V
w--;
if(colcnt <= 2) {
color = next_color(color);
colcnt = COLUMN_WIDTH;
} else {
colcnt-=2;
}
}
} while(w > 0);
}
if(lastw != w) {
usb_ack_bank();
}
fid = ~fid; // flip frame id bit
cur_col_offset += 10;
if(cur_col_offset >= COLUMN_WIDTH) {
cur_start_col = next_color(cur_start_col);
cur_col_offset = 0;
}
}
void send_frame(void)
{
uint8_t intr_state;
intr_state = SREG;
cli();
UENUM = UVC_TX_ENDPOINT;
send_yv12_frame();
SREG = intr_state;
sei();
}
int main(void)
{
CPU_PRESCALE(0); // run at 16 MHz
LED_CONFIG;
uart_init(BAUD_RATE);
// Initialize the USB, and then wait for the host to set configuration.
usb_init();
while (!usb_configured())
/* wait */ ;
// Wait an extra second for the PC's operating system to load drivers
// and do whatever it does to actually be ready for input
_delay_ms(1000);
while (1) {
if(streaming)
{
send_frame();
}
}
}
// USB Device Interrupt - handle all device events
ISR(USB_GEN_vect)
{
uint8_t intbits, t;
intbits = UDINT;
UDINT = 0;
if (intbits & (1<<EORSTI)) {
UENUM = 0;
UECONX = 1;
UECFG0X = EP_TYPE_CONTROL;
UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
UEIENX = (1<<RXSTPE);
usb_configuration = 0;
LED_OFF;
}
if ((intbits & (1<<SOFI))) {
if (usb_configuration) {
t = transmit_flush_timer;
if (t) {
transmit_flush_timer = --t;
if (!t) {
UENUM = UVC_TX_ENDPOINT;
UEINTX = 0x3A;
}
}
}
}
}
static uint8_t ctl_req(uint8_t bRequest, uint8_t *ctl, const uint8_t *ctl_info, uint8_t ctl_len, uint16_t wLength)
{
uint8_t *wptr;
const uint8_t *rptr;
uint8_t len, i, n;
uint8_t write = 0;
uint8_t pgmem = 0;
uint16_t ctl_len16 = ctl_len;
len = (uint8_t)wLength;
switch(bRequest) {
case UVC_REQ_SET_CUR:
wptr = ctl;
write = 1;
break;
case UVC_REQ_GET_CUR:
rptr = ctl;
break;
case UVC_REQ_GET_MIN:
rptr = ctl_info;
pgmem = 1;
break;
case UVC_REQ_GET_MAX:
rptr = ctl_info + (1 * ctl_len);
pgmem = 1;
break;
case UVC_REQ_GET_RES:
rptr = ctl_info + (2 * ctl_len);
pgmem = 1;
break;
case UVC_REQ_GET_DEF:
rptr = ctl_info + (3 * ctl_len);
pgmem = 1;
break;
case UVC_REQ_GET_LEN:
switch(len) {
case 1:
rptr = &ctl_len;
break;
case 2:
rptr = (const uint8_t *)&ctl_len16;
break;
default:
return UVC_ERR_INVALID_REQUEST;
}
break;
case UVC_REQ_GET_INFO:
len = 1;
rptr = ctl_info + (4 * ctl_len);
pgmem = 1;
break;
default:
return UVC_ERR_INVALID_REQUEST;
}
if(write) {
//DBGV("\r\nwp=%p l=%x pg=%x\r\n", wptr, len, pgmem);
do {
usb_wait_receive_out();
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
for(i = 0; i < n; i++) {
*wptr = UEDATX;
wptr++;
}
len -= n;
usb_ack_out();
usb_ack_in();
} while (len || n == ENDPOINT0_SIZE);
} else {
//DBGV("\r\nrp=%p l=%x pg=%x\r\n", rptr, len, pgmem);
do {
usb_wait_in_ready();
n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
for (i = n; i; i--) {
if(pgmem)
UEDATX = pgm_read_byte(rptr++);
else
UEDATX = *rptr++;
}
len -= n;
usb_ack_in();
} while (len || n == ENDPOINT0_SIZE);
}
return UVC_ERR_SUCCESS;
}
static inline uint8_t su_term_req(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
{
uint8_t cs = MSB(wValue);
uint8_t unit;
uint8_t ret = UVC_ERR_SUCCESS;
DBGV("su %x V=%x I=%x L=%x", bRequest, wValue, wIndex, wLength);
if(cs != UVC_SU_INPUT_SELECT_CONTROL || wLength != 1) {
return UVC_ERR_INVALID_CONTROL;
}
switch(bRequest) {
case UVC_REQ_SET_CUR:
usb_wait_receive_out();
unit = UEDATX;
if(unit != 1)
ret = UVC_ERR_OUT_OF_RANGE;
usb_ack_out();
usb_ack_in();
break;
case UVC_REQ_GET_CUR :
case UVC_REQ_GET_MIN :
case UVC_REQ_GET_MAX :
case UVC_REQ_GET_RES :
usb_wait_in_ready();
UEDATX = 1;
usb_ack_in();
break;
case UVC_REQ_GET_INFO:
usb_wait_in_ready();
UEDATX = GINFO_SUPPORT_GET | GINFO_SUPPORT_SET;
usb_ack_in();
break;
default:
ret = UVC_ERR_INVALID_REQUEST;
}
return ret;
}
static inline uint8_t pu_term_req(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
{
uint8_t cs = MSB(wValue);
uint8_t ret = UVC_ERR_SUCCESS;
DBGV("pu %x V=%x I=%x L=%x", bRequest, wValue, wIndex, wLength);
switch(cs) {
case UVC_PU_BRIGHTNESS_CONTROL:
ret = CTL_CALL(brightness, bRequest, wLength);
break;
default:
ret = UVC_ERR_INVALID_CONTROL;
}
return ret;
}
static inline uint8_t in_term_req(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
{
//uint8_t cs = MSB(wValue);
uint8_t ret = UVC_ERR_SUCCESS;
DBGV("in %x V=%x I=%x L=%x", bRequest, wValue, wIndex, wLength);
switch(bRequest) {
case UVC_REQ_SET_CUR:
case UVC_REQ_GET_CUR :
case UVC_REQ_GET_MIN :
case UVC_REQ_GET_MAX :
case UVC_REQ_GET_RES :
case UVC_REQ_GET_LEN:
case UVC_REQ_GET_INFO:
case UVC_REQ_GET_DEF:
default:
ret = UVC_ERR_INVALID_REQUEST;
}
return ret;
}
static inline uint8_t out_term_req(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
{
//uint8_t cs = MSB(wValue);
uint8_t ret = UVC_ERR_SUCCESS;
DBGV("out %x V=%x I=%x L=%x", bRequest, wValue, wIndex, wLength);
switch(bRequest) {
case UVC_REQ_SET_CUR:
case UVC_REQ_GET_CUR :
case UVC_REQ_GET_MIN :
case UVC_REQ_GET_MAX :
case UVC_REQ_GET_RES :
case UVC_REQ_GET_LEN:
case UVC_REQ_GET_INFO:
case UVC_REQ_GET_DEF:
default:
ret = UVC_ERR_INVALID_REQUEST;
}
return ret;
}
static inline uint8_t videoc_req(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
{
uint8_t cs = MSB(wValue);
uint8_t ret = UVC_ERR_SUCCESS;
DBGV("vc %x V=%x I=%x L=%x", bRequest, wValue, wIndex, wLength);
switch(cs) {
case UVC_VC_REQUEST_ERROR_CODE_CONTROL:
switch(bRequest) {
case UVC_REQ_GET_CUR:
usb_wait_in_ready();
UEDATX = last_error;
usb_ack_in();
break;
case UVC_REQ_GET_INFO:
usb_wait_in_ready();
UEDATX = GINFO_SUPPORT_GET;
usb_ack_in();
break;
default:
ret = UVC_ERR_INVALID_REQUEST;
}
break;
default:
ret = UVC_ERR_INVALID_CONTROL;
}
return ret;
}
static void adjust_after_probe(void)
{
DBG("after probe\r\n");
}
static int verify_after_commit(void)
{
DBG("commit!\r\n");
return UVC_ERR_SUCCESS;
}
static inline uint8_t videos_req(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength)
{
uint8_t cs = MSB(wValue);
uint8_t ret = UVC_ERR_SUCCESS;
//DBGV("vs %x V=%x I=%x L=%x", bRequest, wValue, wIndex, wLength);
switch(cs) {
case UVC_VS_PROBE_CONTROL:
ret = CTL_CALL(probe_commit, bRequest, wLength);
if(bRequest == UVC_REQ_SET_CUR && ret == UVC_ERR_SUCCESS)
adjust_after_probe();
break;
case UVC_VS_COMMIT_CONTROL:
ret = CTL_CALL(probe_commit, bRequest, wLength);
if(bRequest == UVC_REQ_SET_CUR && ret == UVC_ERR_SUCCESS)
ret = verify_after_commit();
break;
default:
ret = UVC_ERR_INVALID_REQUEST;
}
return ret;
}
// USB Endpoint Interrupt - endpoint 0 is handled here. The
// other endpoints are manipulated by the user-callable
// functions, and the start-of-frame interrupt.
ISR(USB_COM_vect)
{
uint8_t intbits;
const uint8_t *list;
const uint8_t *cfg;
uint8_t i, n, len, en;
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint16_t desc_val;
const uint8_t *desc_addr;
uint8_t desc_length;
UENUM = 0;
intbits = UEINTX;
if (intbits & (1<<RXSTPI)) {
bmRequestType = UEDATX;
bRequest = UEDATX;
wValue = UEDATX;
wValue |= (UEDATX << 8);
wIndex = UEDATX;
wIndex |= (UEDATX << 8);
wLength = UEDATX;
wLength |= (UEDATX << 8);
UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
if (bRequest == USB_REQ_GET_DESCRIPTOR) {
list = (const uint8_t *)descriptor_list;
for (i=0; ; i++) {
if (i >= NUM_DESC_LIST) {
UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
DBGV("Unk desc wV=%d wI=%d\r\n", wValue, wIndex);
return;
}
desc_val = pgm_read_word(list);
if (desc_val != wValue) {
list += sizeof(struct descriptor_list_struct);
continue;
}
list += 2;
desc_val = pgm_read_word(list);
if (desc_val != wIndex) {
list += sizeof(struct descriptor_list_struct)-2;
continue;
}
list += 2;
desc_addr = (const uint8_t *)pgm_read_word(list);
list += 2;
desc_length = pgm_read_byte(list);
break;
}
len = (wLength < 256) ? wLength : 255;
if (len > desc_length) len = desc_length;
do {
// wait for host ready for IN packet
do {
i = UEINTX;