-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcycx3_uvc.c
1653 lines (1403 loc) · 52.8 KB
/
cycx3_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
/*
## Cypress CX3 Firmware Example Source (cycx3_uvc.c)
## ===========================
##
## Copyright Cypress Semiconductor Corporation, 2013-2014,
## All Rights Reserved
## UNPUBLISHED, LICENSED SOFTWARE.
##
## CONFIDENTIAL AND PROPRIETARY INFORMATION
## WHICH IS THE PROPERTY OF CYPRESS.
##
## Use of this file is governed
## by the license agreement included in the file
##
## <install>/license/license.txt
##
## where <install> is the Cypress software
## installation root directory path.
##
## ===========================
*/
/* This application example implemnets a USB UVC 1.1 compliant video camera on the CX3 using an
* Omnivision OV5640 image sensor. The example supports the following video formats:
* 1. Uncompressed 16 bit YUV2 2952x1944 @15 fps over USB SuperSpeed
* 2. Uncompressed 16 bit YUV2 1920x1080 @30 fps over USB SuperSpeed
* 3. Uncompressed 16 bit YUV2 1280x720 @60 fps over USB SuperSpeed
* 4. Uncompressed 16 bit YUV2 640x480 @60 fps over USB Hi-Speed
* 5. Uncompressed 16 bit YUV2 640x480 @30 fps over USB Hi-Speed
* 6. Uncompressed 16 bit YUV2 320x240 @5 fps over USB Full Speed
*/
#include "cyu3system.h"
#include "cyu3os.h"
#include "cyu3dma.h"
#include "cyu3error.h"
#include "cyu3usb.h"
#include "cyu3i2c.h"
#include "cyu3uart.h"
#include "cyu3gpio.h"
#include "cyu3utils.h"
#include "cyu3pib.h"
#include "cyu3socket.h"
#include "sock_regs.h"
#include "cycx3_uvc.h"
#include "cyu3mipicsi.h"
//#include "cyu3imagesensor.h"
#include "cy_ar0140.h"
static CyU3PThread uvcAppThread; /* Application thread used for streaming from the MIPI interface to USB */
static CyU3PEvent glCx3Event; /* Application Event Group */
#ifdef CX3_ERROR_THREAD_ENABLE
static CyU3PThread uvcMipiErrorThread; /* Thread used to poll the MIPI interface for Mipi bus errors */
static CyU3PEvent glMipiErrorEvent; /* Application Event Group */
#endif
static volatile uint32_t glDMATxCount = 0; /* Counter used to count the Dma Transfers */
static volatile uint32_t glDmaDone = 0;
static volatile uint8_t glActiveSocket = 0;
static volatile uint32_t my_status = 0;
static volatile uint32_t enter_dma_callback = 0;
static volatile uint32_t enter_dma_callback_count = 0;
static CyBool_t glHitFV = CyFalse; /* Flag used for state of FV signal. */
static CyBool_t glMipiActive = CyFalse; /* Flag set to true whin Mipi interface is active. Used for Suspend/Resume. */
static CyBool_t glIsClearFeature = CyFalse; /* Flag to signal when AppStop is called from the ClearFeature request. Needed
to clear endpoint data toggles. */
static volatile CyBool_t glLpmDisable = CyTrue;/* Flag used to Enable/Disable low USB 3.0 LPM */
/* UVC Header */
static uint8_t glUVCHeader[CX3_UVC_HEADER_LENGTH] =
{
0x0C, /* Header Length */
0x8C, /* Bit field header field */
0x00,0x00,0x00,0x00, /* Presentation time stamp field */
0x00,0x00,0x00,0x00,0x00,0x00 /* Source clock reference field */
};
/* Video Probe Commit Control */
static uint8_t glCommitCtrl[CX3_UVC_MAX_PROBE_SETTING_ALIGNED];
static uint8_t glCurrentFrameIndex = 1;
static CyU3PDmaMultiChannel glChHandleUVCStream; /* DMA Channel Handle for UVC Stream */
static CyBool_t glIsApplnActive = CyFalse; /* Whether the Mipi->USB application is active or not. */
static CyBool_t glIsConfigured = CyFalse; /* Whether Application is in configured state or not */
static CyBool_t glIsStreamingStarted = CyFalse; /* Whether streaming has started - Used for MAC OS support*/
#ifdef RESET_TIMER_ENABLE
/* Maximum frame transfer time in milli-seconds. */
#define TIMER_PERIOD (500)
/* Timer used to track frame transfer time. */
static CyU3PTimer UvcTimer;
#ifdef STILL_CAPTURE_ENABLE
static CyU3PEvent glStillImageEvent; /*Still image event group. */
static uint8_t glStillCommitCtrl[CX3_UVC_MAX_PROBE_SETTING_ALIGNED]; /*Still Commit Control Array*/
static uint8_t glStillReq = 0; /*Still Trigger Control Array*/
static uint8_t glStillFrameIndex = 1; /*Frame Index for Still Capture*/
static CyBool_t glStillFlag = CyFalse; /*Still Image Event Flag*/
/* Still Probe Control Setting */
uint8_t glStillProbeCtrl[CX3_UVC_MAX_STILL_PROBE_SETTING] =
{
0x01, /* Use 1st Video format index */
0x01, /* Use 1st Video frame index */
0x00, /* Compression quality */
0x00, 0xC6, 0x99, 0x00, /* Max video frame size in bytes (Highest resolution - 5MP frame size) */
0x00, 0x80, 0x00, 0x00 /* No. of bytes device can rx in single payload = 16KB */
};
#endif
#ifdef PRINT_FRAME_INFO
int32_t TxCount = 0;
int32_t RxCount = 0;
int32_t TxCountflag = 0;
int32_t RxCountflag = 0;
int32_t Printflag = 0;
int32_t FrameCount = 0;
int32_t PartialBufSize = 0;
uint32_t time0 = 0, time1 = 0;
uint16_t fpsflag = 0;
uint16_t gettimeflag = 0;
#endif
static void
CyCx3UvcAppProgressTimer (
uint32_t arg)
{
/* This frame has taken too long to complete. Notify the thread to abort the frame and restart streaming. */
CyU3PEventSet(&glCx3Event, CX3_DMA_RESET_EVENT,CYU3P_EVENT_OR);
}
#endif
/* Application critical error handler */
void
CyCx3UvcAppErrorHandler (
CyU3PReturnStatus_t status /* API return status */
)
{
/* Application failed with the error code status */
/* Add custom debug or recovery actions here */
/* Loop indefinitely */
for (;;)
{
/* Thread sleep : 100 ms */
CyU3PThreadSleep (100);
}
}
/* UVC header addition function */
static void
CyCx3UvcAppAddHeader (
uint8_t *buffer_p, /* Buffer pointer */
uint8_t frameInd /* EOF or normal frame indication */
)
{
/* Copy header to buffer */
CyU3PMemCopy (buffer_p, (uint8_t *)glUVCHeader, CX3_UVC_HEADER_LENGTH);
/* Check if last packet of the frame. */
if (frameInd == CX3_UVC_HEADER_EOF)
{
/* Modify UVC header to toggle Frame ID */
glUVCHeader[1] ^= CX3_UVC_HEADER_FRAME_ID;
/* Indicate End of Frame in the buffer */
buffer_p[1] |= CX3_UVC_HEADER_EOF;
}
}
/* This function starts the video streaming application. It is called
* when there is a SET_INTERFACE event for alternate interface 1
* (in case of UVC over Isochronous Endpoint usage) or when a
* COMMIT_CONTROL(SET_CUR) request is received (when using BULK only UVC).
*/
CyU3PReturnStatus_t
CyCx3UvcAppStart (
void)
{
#ifdef CX3_DEBUG_ENABLED
uint8_t SMState = 0;
#endif
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
glIsApplnActive = CyTrue;
glDmaDone = 0;
glDMATxCount = 0;
glHitFV = CyFalse;
glLpmDisable = CyTrue;
#ifdef RESET_TIMER_ENABLE
CyU3PTimerStop (&UvcTimer);
#endif
/* Place the EP in NAK mode before cleaning up the pipe. */
CyU3PUsbSetEpNak (CX3_EP_BULK_VIDEO, CyTrue);
CyU3PBusyWait (100);
/* Reset USB EP and DMA */
CyU3PUsbFlushEp(CX3_EP_BULK_VIDEO);
status = CyU3PDmaMultiChannelReset (&glChHandleUVCStream);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4,"\n\rAplnStrt:ChannelReset Err = 0x%x", status);
return status;
}
status = CyU3PDmaMultiChannelSetXfer (&glChHandleUVCStream, 0, 0);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rAplnStrt:SetXfer Err = 0x%x", status);
return status;
}
CyU3PUsbSetEpNak (CX3_EP_BULK_VIDEO, CyFalse);
CyU3PBusyWait (200);
/* Resume the Fixed Function GPIF State machine */
CyU3PGpifSMControl(CyFalse);
glActiveSocket = 0;
CyU3PGpifSMSwitch(CX3_INVALID_GPIF_STATE, CX3_START_SCK0,
CX3_INVALID_GPIF_STATE, ALPHA_CX3_START_SCK0, CX3_GPIF_SWITCH_TIMEOUT);
CyU3PThreadSleep (10);
#ifdef CX3_DEBUG_ENABLED
CyU3PGpifGetSMState(&SMState);
CyU3PDebugPrint (4, "\n\rAplnStrt:SMState = 0x%x",SMState);
#endif
/* Wake Mipi interface and Image Sensor */
CyU3PMipicsiWakeup();
CyCx3_ImageSensor_Wakeup();
glMipiActive = CyTrue;
//CyCx3_ImageSensor_Trigger_Autofocus();
return CY_U3P_SUCCESS;
}
/* This function stops the video streaming. It is called from the USB event
* handler, when there is a reset / disconnect or SET_INTERFACE for alternate
* interface 0 in case of ischronous implementation or when a Clear Feature (Halt)
* request is recieved (in case of bulk only implementation).
*/
void
CyCx3UvcAppStop (
void)
{
#ifdef CX3_DEBUG_ENABLED
uint8_t SMState = 0;
#endif
/* Stop the image sensor and CX3 mipi interface */
CyU3PMipicsiSleep();
//CyCx3_ImageSensor_Sleep();
glMipiActive = CyFalse;
#ifdef RESET_TIMER_ENABLE
CyU3PTimerStop (&UvcTimer);
#endif
#ifdef CX3_DEBUG_ENABLED
CyU3PGpifGetSMState(&SMState);
CyU3PDebugPrint (4, "\n\rAplnStop:SMState = 0x%x",SMState);
#endif
/* Pause the GPIF interface*/
CyU3PGpifSMControl(CyTrue);
/* Update the flag so that the application thread is notified of this. */
glIsApplnActive = CyFalse;
CyU3PUsbSetEpNak (CX3_EP_BULK_VIDEO, CyTrue);
CyU3PBusyWait (100);
/* Abort and destroy the video streaming channel */
/* Reset the channel: Set to DSCR chain starting point in PORD/CONS SCKT; set DSCR_SIZE field in DSCR memory*/
CyU3PDmaMultiChannelReset(&glChHandleUVCStream);
CyU3PThreadSleep(25);
/* Flush the endpoint memory */
CyU3PUsbFlushEp(CX3_EP_BULK_VIDEO);
CyU3PUsbSetEpNak (CX3_EP_BULK_VIDEO, CyFalse);
CyU3PBusyWait (200);
/* Clear the stall condition and sequence numbers if ClearFeature. */
if (glIsClearFeature)
{
CyU3PUsbStall (CX3_EP_BULK_VIDEO, CyFalse, CyTrue);
glIsClearFeature = CyFalse;
}
glDMATxCount = 0;
glDmaDone = 0;
/* Enable USB 3.0 LPM */
CyU3PUsbLPMEnable ();
}
/* GpifCB callback function is invoked when FV triggers GPIF interrupt */
void
CyCx3UvcAppGpifCB (
CyU3PGpifEventType event,
uint8_t currentState
)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
/* Handle interrupt from the State Machine */
if (event == CYU3P_GPIF_EVT_SM_INTERRUPT)
{
switch (currentState)
{
case CX3_PARTIAL_BUFFER_IN_SCK0:
{
/* Wrapup Socket 0*/
status = CyU3PDmaMultiChannelSetWrapUp (&glChHandleUVCStream, 0);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rGpifCB:WrapUp SCK0 Err = 0x%x", status);
}
}
break;
case CX3_PARTIAL_BUFFER_IN_SCK1:
{
/* Wrapup Socket 1 */
status = CyU3PDmaMultiChannelSetWrapUp (&glChHandleUVCStream, 1);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rGpifCB:WrapUp SCK1 Err = 0x%x", status);
}
}
break;
default:
break;
}
}
}
/* DMA callback function to handle the produce and consume events. */
void
CyCx3UvcAppDmaCallback (
CyU3PDmaMultiChannel *chHandle,
CyU3PDmaCbType_t type,
CyU3PDmaCBInput_t *input
)
{
CyU3PDmaBuffer_t dmaBuffer;
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
enter_dma_callback = 1;
enter_dma_callback_count++;
if (type == CY_U3P_DMA_CB_PROD_EVENT)
{
/* This is a produce event notification to the CPU. This notification is
* received upon reception of every buffer. The buffer will not be sent
* out unless it is explicitly committed. The call shall fail if there
* is a bus reset / usb disconnect or if there is any application error. */
/* Disable USB 3.0 LPM while Buffer is being transmitted out*/
if ((CyU3PUsbGetSpeed () == CY_U3P_SUPER_SPEED) && (glLpmDisable))
{
CyU3PUsbLPMDisable ();
CyU3PUsbSetLinkPowerState (CyU3PUsbLPM_U0);
CyU3PBusyWait (200);
glLpmDisable = CyFalse;
#ifdef RESET_TIMER_ENABLE
CyU3PTimerStart (&UvcTimer);
#endif
}
status = CyU3PDmaMultiChannelGetBuffer(chHandle, &dmaBuffer, CYU3P_NO_WAIT);
while (status == CY_U3P_SUCCESS)
{
/* Add Headers*/
if(dmaBuffer.count < CX3_UVC_DATA_BUF_SIZE)
{
CyCx3UvcAppAddHeader ((dmaBuffer.buffer - CX3_UVC_PROD_HEADER), CX3_UVC_HEADER_EOF);
glHitFV = CyTrue;
#ifdef PRINT_FRAME_INFO
FrameCount++;
PartialBufSize = dmaBuffer.count;
RxCountflag = RxCount;
TxCountflag = TxCount;
Printflag = 1;
#endif
}
else
{
CyCx3UvcAppAddHeader ((dmaBuffer.buffer - CX3_UVC_PROD_HEADER), CX3_UVC_HEADER_FRAME);
}
/* Commit Buffer to USB*/
status = CyU3PDmaMultiChannelCommitBuffer (chHandle, (dmaBuffer.count + 12), 0);
if (status != CY_U3P_SUCCESS)
{
CyU3PEventSet(&glCx3Event, CX3_DMA_RESET_EVENT,CYU3P_EVENT_OR);
break;
}
else
{
#ifdef PRINT_FRAME_INFO
TxCount++;
#endif
glDMATxCount++;
glDmaDone++;
my_status++;
}
glActiveSocket ^= 1; /* Toggle the Active Socket */
status = CyU3PDmaMultiChannelGetBuffer(chHandle, &dmaBuffer, CYU3P_NO_WAIT);
}
}
else if(type == CY_U3P_DMA_CB_CONS_EVENT)
{
#ifdef PRINT_FRAME_INFO
RxCount++;
#endif
glDmaDone--;
glIsStreamingStarted = CyTrue;
/* Check if Frame is completely transferred */
if ((glHitFV == CyTrue) && (glDmaDone == 0))
{
#ifdef PRINT_FRAME_INFO
TxCount = 0;
RxCount = 0;
#endif
glHitFV = CyFalse;
glDMATxCount=0;
#ifdef RESET_TIMER_ENABLE
CyU3PTimerStop (&UvcTimer);
#endif
if (glActiveSocket)
CyU3PGpifSMSwitch(CX3_INVALID_GPIF_STATE, CX3_START_SCK1,
CX3_INVALID_GPIF_STATE, ALPHA_CX3_START_SCK1, CX3_GPIF_SWITCH_TIMEOUT);
else
CyU3PGpifSMSwitch(CX3_INVALID_GPIF_STATE, CX3_START_SCK0,
CX3_INVALID_GPIF_STATE, ALPHA_CX3_START_SCK0, CX3_GPIF_SWITCH_TIMEOUT);
CyU3PUsbLPMEnable ();
glLpmDisable = CyTrue;
#ifdef RESET_TIMER_ENABLE
CyU3PTimerModify (&UvcTimer, TIMER_PERIOD, 0);
#endif
#ifdef STILL_CAPTURE_ENABLE
if(glStillFlag==CyTrue) //still image
{
CyU3PDebugPrint(4,"Frame complete\n\r");
CyU3PEventSet(&glStillImageEvent, CX3_STILL_IMAGE_EVENT, CYU3P_EVENT_OR);
}
#endif
}
}
}
/* This is the Callback function to handle the USB Events */
static void
CyCx3UvcAppUSBEventCB (
CyU3PUsbEventType_t evtype, /* Event type */
uint16_t evdata /* Event data */
)
{
uint8_t interface = 0, altSetting = 0;
switch (evtype)
{
case CY_U3P_USB_EVENT_SUSPEND:
/* Suspend the device with Wake On Bus Activity set */
glIsStreamingStarted = CyFalse;
CyU3PEventSet (&glCx3Event, CX3_USB_SUSP_EVENT_FLAG, CYU3P_EVENT_OR);
break;
case CY_U3P_USB_EVENT_SETINTF:
/* Start the video streamer application if the
* interface requested was 1. If not, stop the
* streamer. */
interface = CY_U3P_GET_MSB(evdata);
altSetting = CY_U3P_GET_LSB(evdata);
#if CX3_DEBUG_ENABLED
CyU3PDebugPrint(4,"\n\rUsbCB: IF = %d, ALT = %d", interface, altSetting);
#endif
glIsStreamingStarted = CyFalse;
if ((altSetting == CX3_UVC_STREAM_INTERFACE) && (interface == 1))
{
/* Stop the application before re-starting. */
if (glIsApplnActive)
{
#if CX3_DEBUG_ENABLED
CyU3PDebugPrint (4, "\n\rUsbCB:Call AppStop");
#endif
CyCx3UvcAppStop ();
}
CyCx3UvcAppStart ();
break;
}
/* Intentional Fall-through all cases */
case CY_U3P_USB_EVENT_SETCONF:
case CY_U3P_USB_EVENT_RESET:
case CY_U3P_USB_EVENT_DISCONNECT:
case CY_U3P_USB_EVENT_CONNECT:
glIsStreamingStarted = CyFalse;
if (evtype == CY_U3P_USB_EVENT_SETCONF)
glIsConfigured = CyTrue;
else
glIsConfigured = CyFalse;
/* Stop the video streamer application and enable LPM. */
CyU3PUsbLPMEnable ();
if (glIsApplnActive)
{
#if CX3_DEBUG_ENABLED
CyU3PDebugPrint (4, "\n\rUsbCB:Call AppStop");
#endif
CyCx3UvcAppStop ();
}
break;
default:
break;
}
}
/* Callback for LPM requests. Always return true to allow host to transition device
* into required LPM state U1/U2/U3. When data trasmission is active LPM management
* is explicitly desabled to prevent data transmission errors.
*/
static CyBool_t
CyCx3UvcAppLPMRqtCB (
CyU3PUsbLinkPowerMode link_mode /*USB 3.0 linkmode requested by Host */
)
{
return CyFalse;
}
#ifdef STILL_CAPTURE_ENABLE
/* Set the still image resolutions through this function. This function lists all the
* supported resolutions in SuperSpeed and HighSpeed. The frame index of resolutions
* supported in Still Capture can be different from the frame index of resolutions supported
* in Video streaming.
*/
static void
CyCx3UvcAppImageSensorSetStillResolution(
uint8_t resolution_index
)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
switch (CyU3PUsbGetSpeed ())
{
case CY_U3P_SUPER_SPEED:
switch (resolution_index)
{
case 0x01:
/*Write 720PSettings*/
status = CyU3PMipicsiSetIntfParams (&AR0140_UYVY_720P, CyFalse);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rUSBStpCB:SetIntfParams SS2 Err = 0x%x", status);
}
//CyCx3_ImageSensor_Set_720P();
CyU3PDebugPrint (4, "\n\CyCx3UvcAppImageSensorSetStillResolution: my_status = %d", my_status);
break;
}
break;
case CY_U3P_HIGH_SPEED:
switch (resolution_index)
{
}
break;
}
}
#endif
/* Set the video resolution through this function. This function lists all the
* supported resolutions in SuperSpeed and HighSpeed. The frame index of resolutions
* supported in Still Capture can be different from the frame index of resolutions supported
* in Video streaming.
*/
static void
CyCx3UvcAppImageSensorSetVideoResolution(
uint8_t resolution_index
)
{
static int dealytime = 0x00;
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
switch (CyU3PUsbGetSpeed ())
{
case CY_U3P_SUPER_SPEED:
switch (resolution_index)
{
case 0x01:
/* Write 720PSettings */
status = CyU3PMipicsiSetIntfParams (&AR0140_UYVY_720P, CyFalse);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rUSBStpCB:SetIntfParams SS1 Err = 0x%x", status);
}
status = CyU3PMipicsiSetPhyTimeDelay(1,dealytime);
CyU3PDebugPrint (4, "\n\SET CyU3PMipicsiSetPhyTimeDelay dealytime as %d", dealytime++);
//CyCx3_ImageSensor_Set_720P ();
CyU3PDebugPrint (4, "\n\CyCx3UvcAppImageSensorSetVideoResolution: my_status = %d", my_status);
CyU3PDebugPrint (4, "\n\CyCx3UvcAppImageSensorSetVideoResolution: enter_dma_callback = %d", enter_dma_callback);
if(enter_dma_callback == 1)
{
enter_dma_callback = 0;
CyU3PDebugPrint (4, "\n\CyCx3UvcAppImageSensorSetVideoResolution: dma call back function is called ");
CyU3PDebugPrint (4, "\n\CyCx3UvcAppImageSensorSetVideoResolution: dma call back function is called %d times", enter_dma_callback_count);
}
break;
}
break;
case CY_U3P_HIGH_SPEED:
switch (resolution_index)
{
}
break;
}
}
static void
CyCx3UvcAppHandleSetCurReq (
uint16_t wValue
)
{
CyU3PReturnStatus_t status;
uint16_t readCount = 0;
/* Get the UVC probe/commit control data from EP0 */
status = CyU3PUsbGetEP0Data(CX3_UVC_MAX_PROBE_SETTING_ALIGNED, glCommitCtrl, &readCount);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rUSB Setup CB:SET_CUR:GetEP0Data Err = 0x%x", status);
return;
}
/* Check the read count. Expecting a count of CX3_UVC_MAX_PROBE_SETTING bytes. */
if (readCount > (uint16_t)CX3_UVC_MAX_PROBE_SETTING)
{
CyU3PDebugPrint (4, "\n\rUSB Setup CB:Invalid SET_CUR Rqt Len");
return;
}
/* Set Probe Control */
if (wValue == CX3_UVC_VS_PROBE_CONTROL)
{
glCurrentFrameIndex = glCommitCtrl[3];
}
else
{
/* Set Commit Control and Start Streaming*/
if (wValue == CX3_UVC_VS_COMMIT_CONTROL)
{
status = CyU3PMipicsiReset(CY_U3P_CSI_HARD_RST);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\r CyU3PMipicsiReset Err = 0x%x", status);
return;
}
status = CyU3PMipicsiInit();
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\r CyU3PMipicsiInit Err = 0x%x", status);
return;
}
status = CyU3PMipicsiSetIntfParams(&AR0140_UYVY_720P, CyFalse);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\r CyU3PMipicsiSetIntfParams Err = 0x%x", status);
return;
}
status = CyU3PMipicsiSetSensorControl(CY_U3P_CSI_IO_XRES, CyTrue);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\r CyU3PMipicsiSetSensorControl Err = 0x%x", status);
return;
}
CyCx3UvcAppImageSensorSetVideoResolution (glCommitCtrl[3]);
if (glIsApplnActive)
{
#ifdef CX3_DEBUG_ENABLED
CyU3PDebugPrint (4, "\n\rUSB Setup CB:Call AppSTOP1");
#endif
CyCx3UvcAppStop();
}
CyCx3UvcAppStart();
}
}
}
/*Returns the pointer to the Probe Control structure for the corresponding frame index.*/
uint8_t *
CyCx3UvcAppGetProbeControlData (
CyU3PUSBSpeed_t usbConType,
uint8_t frameIndex
)
{
if (usbConType == CY_U3P_SUPER_SPEED)
{
if (frameIndex == 1)
{
/* 1280 x 720 @30.0 fps */
return ((uint8_t *) gl720PProbeCtrl);
}
}
else if (usbConType == CY_U3P_HIGH_SPEED)
{
}
else
{
}
return NULL;
}
/* Callback to handle the USB Setup Requests and UVC Class events */
static CyBool_t
CyCx3UvcAppUSBSetupCB (
uint32_t setupdat0, /* SETUP Data 0 */
uint32_t setupdat1 /* SETUP Data 1 */
)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
uint8_t bRequest, bType,bRType, bTarget;
uint16_t wValue, wIndex;
uint8_t ep0Buf[2];
uint8_t temp = 0;
CyBool_t isHandled = CyFalse;
uint8_t *ctrl_src = 0;
#ifdef STILL_CAPTURE_ENABLE
uint32_t eventFlag;
uint16_t readCount = 0;
#endif
#if CX3_DEBUG_ENABLED
uint16_t wLength;
#endif
/* Decode the fields from the setup request. */
bRType = (setupdat0 & CY_U3P_USB_REQUEST_TYPE_MASK);
bType = (bRType & CY_U3P_USB_TYPE_MASK);
bTarget = (bRType & CY_U3P_USB_TARGET_MASK);
bRequest = ((setupdat0 & CY_U3P_USB_REQUEST_MASK) >> CY_U3P_USB_REQUEST_POS);
wValue = ((setupdat0 & CY_U3P_USB_VALUE_MASK) >> CY_U3P_USB_VALUE_POS);
wIndex = ((setupdat1 & CY_U3P_USB_INDEX_MASK) >> CY_U3P_USB_INDEX_POS);
#if CX3_DEBUG_ENABLED
// wLength = ((setupdat1 & CY_U3P_USB_LENGTH_MASK) >> CY_U3P_USB_LENGTH_POS);
// CyU3PDebugPrint(4, "\n\rbRType = 0x%x, bRequest = 0x%x, wValue = 0x%x, wIndex = 0x%x, wLength= 0x%x",
// bRType, bRequest, wValue, wIndex, wLength);
#endif
/* ClearFeature(Endpoint_Halt) received on the Streaming Endpoint. Stop Streaming */
if((bTarget == CY_U3P_USB_TARGET_ENDPT) && (bRequest == CY_U3P_USB_SC_CLEAR_FEATURE)
&& (wIndex == CX3_EP_BULK_VIDEO) && (wValue == CY_U3P_USBX_FS_EP_HALT))
{
if ((glIsApplnActive) && (glIsStreamingStarted))
{
glIsClearFeature = CyTrue;
CyCx3UvcAppStop();
}
return CyFalse;
}
if(bRType == CY_U3P_USB_GS_DEVICE)
{
/* Make sure that we bring the link back to U0, so that the ERDY can be sent. */
if (CyU3PUsbGetSpeed () == CY_U3P_SUPER_SPEED)
CyU3PUsbSetLinkPowerState (CyU3PUsbLPM_U0);
}
if ((bTarget == CY_U3P_USB_TARGET_INTF) && ((bRequest == CY_U3P_USB_SC_SET_FEATURE)
|| (bRequest == CY_U3P_USB_SC_CLEAR_FEATURE)) && (wValue == 0))
{
#if CX3_DEBUG_ENABLED
CyU3PDebugPrint (4, "\n\rStpCB:In SET_FTR %d::%d", glIsApplnActive, glIsConfigured);
#endif
if (glIsConfigured)
{
CyU3PUsbAckSetup ();
}
else
{
CyU3PUsbStall (0, CyTrue, CyFalse);
}
return CyTrue;
}
if ((bRequest == CY_U3P_USB_SC_GET_STATUS) && (bTarget == CY_U3P_USB_TARGET_INTF))
{
/* We support only interface 0. */
if (wIndex == 0)
{
ep0Buf[0] = 0;
ep0Buf[1] = 0;
CyU3PUsbSendEP0Data (0x02, ep0Buf);
}
else
CyU3PUsbStall (0, CyTrue, CyFalse);
return CyTrue;
}
/* Check for UVC Class Requests */
if (bType == CY_U3P_USB_CLASS_RQT)
{
/* Requests to the Video Streaming Interface (IF 1) */
if ((CY_U3P_GET_LSB (wIndex)) == CX3_UVC_STREAM_INTERFACE)
{
if((wValue == CX3_UVC_VS_PROBE_CONTROL) || (wValue == CX3_UVC_VS_COMMIT_CONTROL))
{
switch (bRequest)
{
case CX3_USB_UVC_GET_INFO_REQ:
{
ep0Buf[0] = 3;
CyU3PUsbSendEP0Data (1, (uint8_t *)ep0Buf);
isHandled = CyTrue;
}
break;
case CX3_USB_UVC_GET_LEN_REQ:
{
ep0Buf[0] = CX3_UVC_MAX_PROBE_SETTING;
CyU3PUsbSendEP0Data (1, (uint8_t *)ep0Buf);
isHandled = CyTrue;
}
break;
case CX3_USB_UVC_GET_CUR_REQ:
case CX3_USB_UVC_GET_MIN_REQ:
case CX3_USB_UVC_GET_MAX_REQ:
case CX3_USB_UVC_GET_DEF_REQ:
{
/* Host requests for probe data of 34 bytes (UVC 1.1) or 26 Bytes (UVC1.0). Send it over EP0. */
ctrl_src = CyCx3UvcAppGetProbeControlData (CyU3PUsbGetSpeed (), glCurrentFrameIndex);
if (ctrl_src != 0)
{
CyU3PMemCopy (glProbeCtrl, (uint8_t *)ctrl_src, CX3_UVC_MAX_PROBE_SETTING);
status = CyU3PUsbSendEP0Data(CX3_UVC_MAX_PROBE_SETTING, glProbeCtrl);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rUSB Setup CB:GET_CUR:SendEP0Data Err = 0x%x", status);
}
}
else
{
CyU3PUsbStall (0, CyTrue, CyFalse);
}
isHandled = CyTrue;
}
break;
case CX3_USB_UVC_SET_CUR_REQ:
{
CyCx3UvcAppHandleSetCurReq (wValue);
isHandled = CyTrue;
}
break;
default:
isHandled = CyFalse;
break;
}
}
#ifdef STILL_CAPTURE_ENABLE
else if((wValue == CX3_UVC_VS_STILL_PROBE_CONTROL) || (wValue == CX3_UVC_VS_STILL_COMMIT_CONTROL))
{
switch (bRequest)
{
case CX3_USB_UVC_GET_CUR_REQ:
case CX3_USB_UVC_GET_MIN_REQ:
case CX3_USB_UVC_GET_MAX_REQ:
{
CyU3PDebugPrint(4,"Get cur Still probe index = %d\n\r", glStillFrameIndex);
glStillProbeCtrl[1] = glStillFrameIndex;
status = CyU3PUsbSendEP0Data(CX3_UVC_MAX_STILL_PROBE_SETTING, (uint8_t*)glStillProbeCtrl);
if(status != CY_U3P_SUCCESS)
CyU3PDebugPrint(4,"\rStill CyU3PUsbSendEP0Data Failed 0x%x\r\n",status);
}
break;
case CX3_USB_UVC_SET_CUR_REQ:
{
/* Get the UVC probe/commit control data from EP0 */
status = CyU3PUsbGetEP0Data(16, glStillCommitCtrl, &readCount);
if(status != CY_U3P_SUCCESS)
CyU3PDebugPrint(4,"\rStill CyU3PUsbGetEP0Data Failed 0x%x\r\n",status);
glStillFrameIndex = glStillCommitCtrl[1];
CyU3PDebugPrint(4,"Set cur Still probe index = %d\n\r", glStillFrameIndex);
}
break;
}
return CyTrue;
}
else if (wValue == CX3_UVC_VS_STILL_IMAGE_TRIGGER_CONTROL)
{
status = CyU3PUsbGetEP0Data(16, &glStillReq, &readCount);
if(status != CY_U3P_SUCCESS)
CyU3PDebugPrint(4,"\rStill CyU3PUsbGetEP0Data Failed 0x%x\r\n",status);
glStillFlag = CyTrue;
CyU3PEventGet (&glStillImageEvent,CX3_STILL_IMAGE_EVENT, CYU3P_EVENT_OR_CLEAR, &eventFlag, CYU3P_WAIT_FOREVER);
CyCx3UvcAppStop();
CyU3PDebugPrint(4,"Still trig Still probe index = %d\n\r", glStillFrameIndex);
CyCx3UvcAppImageSensorSetStillResolution (glStillFrameIndex);
glUVCHeader[1] ^= CX3_UVC_HEADER_STI;
CyCx3UvcAppStart ();
CyU3PDebugPrint(4,"Still Frame start\n\r");
CyU3PEventGet (&glStillImageEvent, CX3_STILL_IMAGE_EVENT, CYU3P_EVENT_OR_CLEAR, &eventFlag, CYU3P_WAIT_FOREVER);
CyU3PDebugPrint(4,"Still Frame end\n\r");
CyCx3UvcAppStop ();
glUVCHeader[1] ^= CX3_UVC_HEADER_STI;
glStillFlag = CyFalse;
CyCx3UvcAppImageSensorSetVideoResolution (glCurrentFrameIndex);
CyCx3UvcAppStart ();
return CyTrue;
}
#endif
}
/* Request addressed to the Video Control Interface */
else if (CY_U3P_GET_LSB(wIndex) == CX3_UVC_CONTROL_INTERFACE)
{
/* Respond to VC_REQUEST_ERROR_CODE_CONTROL and stall every other request as this example does
not support any of the Video Control features */
if ((wValue == CX3_UVC_VC_REQUEST_ERROR_CODE_CONTROL) && (wIndex == 0x00))
{
temp = CX3_UVC_ERROR_INVALID_CONTROL;
status = CyU3PUsbSendEP0Data(0x01, &temp);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "\n\rUSBStpCB:VCI SendEP0Data = %d", status);
}
isHandled = CyTrue;
}
}
}
return isHandled;
}
/* This function initialines the USB Module, creates event group,