summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/n9h30/Driver/Source/nu_cap.c
blob: 0fe3d87424d333f6187e186c33a53324f42c96aa (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
/**************************************************************************//**
* @file     cap.c
* @version  V1.00
* @brief    N9H30 CAP driver source file
*
* SPDX-License-Identifier: Apache-2.0
* @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/

#include <stdio.h>
#include <string.h>
#include "N9H30.h"
#include "nu_sys.h"
#include "nu_cap.h"

/** @addtogroup N9H30_Device_Driver N9H30 Device Driver
  @{
*/

/** @addtogroup N9H30_CAP_Driver CAP Driver
  @{
*/

/** @addtogroup N9H30_CAP_EXPORTED_FUNCTIONS CAP Exported Functions
  @{
*/

/// @cond HIDDEN_SYMBOLS
PFN_CAP_CALLBACK(pfnCAP_IntHandlerTable)[4] = {0};
/// @endcond HIDDEN_SYMBOLS

/**
 * @brief       CAP interrupt Handler
 *
 * @param       None
 *
 * @return      None
 *
 * @details     Driver internal use API to process the interrupt of CAP
 *              As interrupt occurrence, the register call back function will be executed
 */
static UINT32 u32EscapeFrame = 0;
//static UINT32 g_u32DeviceType = 0;
static void CAP_IntHandler(void)
{
    UINT32 u32CapInt;
    UINT32 uBuf = 0;

    if ((inp32(REG_CLK_HCLKEN) & (0x1 << 26)) == (0x1 << 26)) /* CMOS sensor interface controller clock enabled */
    {
        u32CapInt = inp32(REG_CAP_INT);
        if ((u32CapInt & (VIEN | VINTF)) == (VIEN | VINTF))
        {
            if (pfnCAP_IntHandlerTable[0] != 0)
                pfnCAP_IntHandlerTable[0](uBuf, uBuf, u32EscapeFrame);
            outp32(REG_CAP_INT, (u32CapInt & ~(MDINTF | ADDRMINTF | MEINTF)));    /* Clear Frame end interrupt */
            u32EscapeFrame = u32EscapeFrame + 1;
        }
        else if ((u32CapInt & (ADDRMIEN | ADDRMINTF)) == (ADDRMIEN | ADDRMINTF))
        {
            if (pfnCAP_IntHandlerTable[1] != 0)
                pfnCAP_IntHandlerTable[1](uBuf, uBuf, u32EscapeFrame);
            outp32(REG_CAP_INT, (u32CapInt & ~(MDINTF | VINTF | MEINTF)));      /* Clear Address match interrupt */
        }
        else if ((u32CapInt & (MEIEN | MEINTF)) == (MEIEN | MEINTF))
        {
            if (pfnCAP_IntHandlerTable[2] != 0)
                pfnCAP_IntHandlerTable[2](uBuf, uBuf, u32EscapeFrame);
            outp32(REG_CAP_INT, (u32CapInt & ~(MDINTF | VINTF | ADDRMINTF)));   /* Clear Memory error interrupt */
        }
        else if ((u32CapInt & (MDIEN | MDINTF)) == (MDIEN | MDINTF))
        {
            if (pfnCAP_IntHandlerTable[3] != 0)
                pfnCAP_IntHandlerTable[3](uBuf, uBuf, u32EscapeFrame);
            outp32(REG_CAP_INT, (u32CapInt & ~(VINTF | MEINTF | ADDRMINTF)));       /* Clear Memory error interrupt */
        }
    }
}

/**
 * @brief       Set Inital Frame
 *
 * @return      None
 *
 * @details     If enable interrupt, there is internal counter that records how many frames have pass.
 *              Set the internal counters to zero. The internal counter may be not a constant
 */
void CAP_SetInitFrame(void)
{
    u32EscapeFrame = 0;
}

/**
 * @brief       Get Inital Frame
 *
 * @retval      >0 Internal counters
 *
 * @details     If enable interrupt, there is internal counter that records how many frames have pass.
 *              Get the internal counters. The internal counter may be not a constant
 */
UINT32 CAP_GetSkipFrame(void)
{
    return u32EscapeFrame;
}

/**
 * @brief       CAP Initial
 *
 * @param[in]       bIsEnableSnrClock  Enable/Disable sensor clock
 *                  1 : Enable
 *                  0 : Disable
 * @param[in]       eSnrSrc            Set CAP clock source. Including :
 *                  - \ref   eCAP_SNR_APLL
 *                  - \ref   eCAP_SNR_UPLL
 * @param[in]       u32SensorFreqKHz   Specify the sensor clock
 *
 * @return      None
 *
 * @details     To Initial sensor source clock and frequency for CAP interface
 */
void CAP_Init(BOOL bIsEnableSnrClock, E_CAP_SNR_SRC eSnrSrc, UINT32 u32SensorFreqKHz/*KHz unit*/)
{
    UINT32 u32PllClock, u32SenDiv;// u32ExtFreq;
    UINT32 u32Div0, u32Div1;
    UINT32 u32SenSrc;
    volatile UINT32 u32Divider;

    /* MFP_GPI_L : I3=SEN_CLK0, I4=SEN_PCLK, I5=SEN_HSYNC, I6=SEN_VSYNC, I7=SEN_FIFLD*/
    outpw(REG_SYS_GPI_MFPL, (inpw(REG_SYS_GPI_MFPL) & (0x00000FFF)) | 0x33333000);

    /* MFP_GPI_H : SEN_PDATA[0~7]*/
    outpw(REG_SYS_GPI_MFPH, (inpw(REG_SYS_GPI_MFPH) & (0xFFFFFFFF)) | 0x33333333);

    u32SensorFreqKHz = u32SensorFreqKHz * 1000;
    switch (eSnrSrc)
    {
    case eCAP_SNR_APLL:
        u32PllClock = sysGetClock(SYS_APLL) * 1000000;
        u32SenSrc = 0x2 << 19; //APLL for sensor clock
        break;
    case eCAP_SNR_UPLL:
        u32PllClock = sysGetClock(SYS_UPLL) * 1000000;
        u32SenSrc = 0x3 << 19; //UPLL for sensor clock
        break;
    }


    u32SenDiv = u32PllClock / (u32SensorFreqKHz);
    if (u32PllClock % u32SensorFreqKHz != 0) u32SenDiv = u32SenDiv + 1;
    for (u32Div1 = 1; u32Div1 <= 16; u32Div1 = u32Div1 + 1)
    {
        for (u32Div0 = 1; u32Div0 <= 8; u32Div0 = u32Div0 + 1)
            if (u32SenDiv == u32Div0 * u32Div1)  break;
        if (u32Div0 >= 9)    continue;
        if (u32SenDiv == u32Div0 * u32Div1)  break;
    }
    //sysprintf("Div0 and Div1 = %d, %d ", u32Div0, u32Div1);
    u32Div0 = u32Div0 - 1;
    u32Div1 = u32Div1 - 1;

    if (bIsEnableSnrClock)
    {
        outp32(REG_CLK_HCLKEN, inp32(REG_CLK_HCLKEN) | (1 << 27));          /* CMOS Sensor Reference Clock Output Enable */
        outp32(REG_CLK_HCLKEN, inp32(REG_CLK_HCLKEN) | (1 << 26));          /* CMOS Sensor Interface Controller Clock Enable */
    }
    else
    {
        outp32(REG_CLK_HCLKEN, inp32(REG_CLK_HCLKEN) & ~(1 << 27));           /* CMOS Sensor Reference Clock Output Disabled */
        outp32(REG_CLK_HCLKEN, inp32(REG_CLK_HCLKEN) & ~(1 << 26));           /* CMOS Sensor Interface Controller Clock Disabled */
    }
    u32Divider  = u32SenSrc | ((u32Div0 << 16) | (u32Div1 << 24)) ;
    //sysprintf("Sensor Divider = 0x%08x\n", u32Divider);

    outp32(REG_CLK_DIVCTL3, (inp32(REG_CLK_DIVCTL3) & ~((0x3 << 19) | (0x7 << 16) | (0xF << 24))) | u32Divider);


}

/**
 * @brief       CAP Open
 *
 * @param[in]   u32SensorFreqKHz  Specify the sensor clock
 *
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     Initialize the CAP engine. Register a call back for driver internal using
 */
INT32 CAP_Open(UINT32 u32SensorFreqKHz)
{

    UINT32 u32PllClock;// u32ExtFreq;
    UINT32 u32SenDiv;
    UINT32 u32Div0, u32Div1;
    UINT32 u32SenSrc;
    volatile UINT32 u32Divider;

    u32SensorFreqKHz = u32SensorFreqKHz * 1000;

    outp32(REG_CLK_PMCON, inpw(REG_CLK_PMCON) | (0x1 << 4)) ; /* Sensor clock keep on high level */
    outp32(REG_CLK_HCLKEN, inpw(REG_CLK_HCLKEN) | (0x1 << 26)); /* CMOS sensor interface controller clock enable */
    outp32(REG_SYS_AHBIPRST, inp32(REG_SYS_AHBIPRST) | (1 << 10)); /* Video capture (CMOS sensor interface) reset enable. */
    outp32(REG_SYS_AHBIPRST, inp32(REG_SYS_AHBIPRST) & ~(1 << 10)); /* Video capture (CMOS sensor interface) reset disable */

    switch ((inpw(REG_CLK_DIVCTL3) >> 19) & 0x3)
    {
    case eCAP_SNR_APLL:
        u32PllClock = sysGetClock(SYS_APLL) * 1000000;
        u32SenSrc = 0x2 << 19; //APLL for sensor clock
        break;
    case eCAP_SNR_UPLL:
        u32PllClock = sysGetClock(SYS_UPLL) * 1000000;
        u32SenSrc = 0x3 << 19; //APLL for sensor clock
        break;
    }

    u32SenDiv = u32PllClock / (u32SensorFreqKHz);
    if (u32PllClock % u32SensorFreqKHz != 0)
        u32SenDiv = u32SenDiv + 1;
    for (u32Div1 = 1; u32Div1 <= 16; u32Div1 = u32Div1 + 1)
    {
        for (u32Div0 = 1; u32Div0 <= 8; u32Div0 = u32Div0 + 1)
        {
            if (u32SenDiv == u32Div0 * u32Div1)
                break;
        }
        if (u32Div0 >= 9)    continue;
        if (u32SenDiv == u32Div0 * u32Div1)
            break;
    }
    //sysprintf("Div0 and Div1 = %d, %d ", u32Div0, u32Div1);
    u32Div0 = u32Div0 - 1;
    u32Div1 = u32Div1 - 1;
    u32Divider  = u32SenSrc | ((u32Div0 << 16) | (u32Div1 << 24)) ;
    //sysprintf("Sensor Divider = 0x%08x\n", u32Divider);

    outp32(REG_CLK_DIVCTL3, (inp32(REG_CLK_DIVCTL3) & ~((0x3 << 19) | (0x7 << 16) | (0xF << 24))) | u32Divider);

    sysInstallISR(IRQ_LEVEL_1, CAP_IRQn, (PVOID)CAP_IntHandler);
    sysEnableInterrupt(CAP_IRQn);

    return Successful;
}


/**
 * @brief       videoIn Reset
 *
 * @return      None
 *
 * @details     Capture interface reset.
 */
void CAP_Reset(void)
{
    outp32(REG_CAP_CTL, inp32(REG_CAP_CTL) | (VPRST));
    outp32(REG_CAP_CTL, inp32(REG_CAP_CTL) & (~VPRST));
}

/**
 * @brief       videoIn Close
 *
 * @return      None
 *
 * @details     Disable pin function,engine clock and interrupt
 */
void CAP_Close(void)
{
    // 1. Disable IP's interrupt
    sysDisableInterrupt(CAP_IRQn);
    // 2. Disable IP's clock
    outp32(REG_CLK_HCLKEN, inp32(REG_CLK_HCLKEN) & ~(0x1 << 25));
    CAP_Reset();
    outp32(REG_CLK_HCLKEN, inp32(REG_CLK_HCLKEN) & ~(0x1 << 26));
    // 3. Disable Capture pin function
}

/**
 * @brief       Configure packet frame buffer.
 *
 * @param[in]   bFrameSwitch    Software mode buffer select
 *                              0: Packet buffer 0
 *                              1: Packet buffer 1
 * @return      None
 *
 * @details     This function set packet frame buffer control
 */
void CAP_SetPacketFrameBufferControl(BOOL bFrameSwitch)
{
    UINT32 u32Ctl;
    u32Ctl = inp32(REG_CAP_CTL) & ~(ADDRSW);
    outp32(REG_CAP_CTL, u32Ctl | (bFrameSwitch ? ADDRSW : 0));
}

/**
* @brief        Get packet frame buffer.
 *
 * @param       pbFrameSwitch   Software mode buffer select
 *                              0: Packet buffer 0
 *                              1: Packet buffer 1
 * @return      None
 *
 * @details     This function get packet frame buffer control
 */
void CAP_GetPacketFrameBufferControl(PBOOL pbFrameSwitch)
{
    UINT32 u32Ctl = inp32(REG_CAP_CTL);
    *pbFrameSwitch = (u32Ctl & ADDRSW) >> 3;
}

/**
* @brief        Configure callback function
 *
 * @param[in]   eIntType         Set interrupt type. Including :
 *                               - \ref eCAP_MDINTF
 *                               - \ref eCAP_ADDRMINTF
 *                               - \ref eCAP_MEINTF
 *                               - \ref eCAP_VINTF
 * @param[in]   pfnCallback      Set Callback function.
 *                                The callbakc function :
 *                                  void (*PFN_CAP_CALLBACK)(UINT8 u8PacketBufID,UINT8 u8PlanarBufID, UINT8 u8FrameRate);
 * @param[in]   pfnOldCallback   Set Old callback function
 *                                The callbakc function :
 *                                  void *(*PFN_CAP_CALLBACK)(UINT8 u8PacketBufID,UINT8 u8PlanarBufID, UINT8 u8FrameRate);
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function configure callback function and set trigger level
 */
INT32 CAP_InstallCallback(E_CAP_INT_TYPE eIntType, PFN_CAP_CALLBACK pfnCallback, PFN_CAP_CALLBACK *pfnOldCallback)
{
    if (eIntType == eCAP_VINTF)
    {
        *pfnOldCallback = pfnCAP_IntHandlerTable[0];
        pfnCAP_IntHandlerTable[0] = (PFN_CAP_CALLBACK)(pfnCallback);
    }
    else if (eIntType == eCAP_ADDRMINTF)
    {
        *pfnOldCallback = pfnCAP_IntHandlerTable[1];
        pfnCAP_IntHandlerTable[1] = (PFN_CAP_CALLBACK)(pfnCallback);
    }
    else if (eIntType == eCAP_MEINTF)
    {
        *pfnOldCallback = pfnCAP_IntHandlerTable[2];
        pfnCAP_IntHandlerTable[2] = (PFN_CAP_CALLBACK)(pfnCallback);
    }
    else if (eIntType == eCAP_MDINTF)
    {
        *pfnOldCallback = pfnCAP_IntHandlerTable[3];
        pfnCAP_IntHandlerTable[3] = (PFN_CAP_CALLBACK)(pfnCallback);
    }
    else
        return E_CAP_INVALID_INT;
    return Successful;
}

/**
 * @brief       Enable videoIn interrupt.
 *
 * @param[in]   eIntType     Interrupt type. Incuding:
 *                           - \ref eCAP_MDINTF
 *                           - \ref eCAP_ADDRMINTF
 *                           - \ref eCAP_MEINTF
 *                           - \ref eCAP_VINTF
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to enable videoIn interrupt.
 */
INT32 CAP_EnableInt(E_CAP_INT_TYPE eIntType)
{
    switch (eIntType)
    {
    case eCAP_MDINTF:
    case eCAP_ADDRMINTF:
    case eCAP_MEINTF:
    case eCAP_VINTF:
        outp32(REG_CAP_INT, inp32(REG_CAP_INT) | eIntType);
        break;
    default:
        return E_CAP_INVALID_INT;
    }
    return Successful;
}

/**
 * @brief       Disable videoIn interrupt
 *
 * @param[in]   eIntType     Interrupt type. Incuding:
 *                           - \ref eCAP_MDINTF
 *                           - \ref eCAP_ADDRMINTF
 *                           - \ref eCAP_MEINTF
 *                           - \ref eCAP_VINTF
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to disable videoIn interrupt.
 */
INT32 CAP_DisableInt(E_CAP_INT_TYPE eIntType)
{
    switch (eIntType)
    {
    case eCAP_MDINTF:
    case eCAP_ADDRMINTF:
    case eCAP_MEINTF:
    case eCAP_VINTF:
        outp32(REG_CAP_INT, inp32(REG_CAP_INT) & ~eIntType);
        break;
    default:
        return E_CAP_INVALID_INT;
    }
    return Successful;
}

/**
 * @brief       Check videoIn interrupt
 *
 * @param[in]   eIntType     Interrupt type. Incuding:
 *                           - \ref eCAP_MDINTF
 *                           - \ref eCAP_ADDRMINTF
 *                           - \ref eCAP_MEINTF
 *                           - \ref eCAP_VINTF
 * @retval      1 Enable
 * @retval      0 Disable
 *
 * @details     This function is used to check videoIn interrupt.
 */
BOOL CAP_IsIntEnabled(E_CAP_INT_TYPE eIntType)
{
    UINT32 u32IntEnable = inp32(REG_CAP_INT);
    switch (eIntType)
    {
    case eCAP_MDINTF:
        u32IntEnable = u32IntEnable & eCAP_MDINTF;
        break;
    case eCAP_ADDRMINTF:
        u32IntEnable = u32IntEnable & eCAP_ADDRMINTF;
        break;
    case eCAP_MEINTF:
        u32IntEnable = u32IntEnable & eCAP_MEINTF;
        break;
    case eCAP_VINTF:
        u32IntEnable = u32IntEnable & eCAP_VINTF;
        break;
    }
    return (u32IntEnable ? TRUE : FALSE);
}

/**
 * @brief       Clear videoIn interrupt flag.
 *
 * @param[in]   eIntType    Interrupt type. Incuding:
 *                          - \ref eCAP_MDINTF
 *                          - \ref eCAP_ADDRMINTF
 *                          - \ref eCAP_MEINTF
 *                          - \ref eCAP_VINTF
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to clear videoIn interrupt flag.
 */
INT32 CAP_ClearInt(E_CAP_INT_TYPE eIntType)
{
    UINT32 u32IntChannel = eIntType >> 16;
    switch (eIntType)
    {
    case eCAP_MDINTF:
        outp32(REG_CAP_INT, (inp32(REG_CAP_INT) & ~((eCAP_ADDRMINTF | eCAP_MEINTF | eCAP_VINTF) >> 16)) |
               u32IntChannel);
        break;
    case eCAP_ADDRMINTF:
        outp32(REG_CAP_INT, (inp32(REG_CAP_INT) & ~((eCAP_MDINTF | eCAP_MEINTF | eCAP_VINTF) >> 16)) |
               u32IntChannel);
        break;
    case eCAP_MEINTF:
        outp32(REG_CAP_INT, (inp32(REG_CAP_INT) & ~((eCAP_MDINTF | eCAP_ADDRMINTF | eCAP_VINTF) >> 16)) |
               u32IntChannel);
        break;
    case eCAP_VINTF:
        outp32(REG_CAP_INT, (inp32(REG_CAP_INT) & ~((eCAP_MDINTF | eCAP_MEINTF | eCAP_ADDRMINTF) >> 16)) |
               u32IntChannel);
        break;
    default:
        return E_CAP_INVALID_INT;
    }
    return Successful;


}

/**
 * @brief       Polling videoIn interrupt flag.
 *
 * @param[in]   eIntType    Interrupt type. Incuding:
 *                          - \ref eCAP_MDINTF
 *                          - \ref eCAP_ADDRMINTF
 *                          - \ref eCAP_MEINTF
 *                          - \ref eCAP_VINTF
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to poll videoIn interrupt flag.
 */
BOOL CAP_PollInt(E_CAP_INT_TYPE eIntType)
{
    UINT32 u32IntStatus = inp32(REG_CAP_INT);
    switch (eIntType)
    {
    case eCAP_MDINTF:
        u32IntStatus = u32IntStatus & (eCAP_MDINTF >> 16);
        break;
    case eCAP_ADDRMINTF:
        u32IntStatus = u32IntStatus & (eCAP_ADDRMINTF >> 16);
        break;
    case eCAP_MEINTF:
        u32IntStatus = u32IntStatus & (eCAP_MEINTF >> 16);
        break;
    case eCAP_VINTF:
        u32IntStatus = u32IntStatus & (eCAP_VINTF >> 16);
        break;
    }
    return (u32IntStatus ? TRUE : FALSE);
}

/**
 * @brief       Enable engine clock and turn on the pipe.
 *
 * @param[in]   bEngEnable      Enable engine clock.
 *                              1 : Enable engine clock.
 *                              0 : Disable engine clock.
 * @param[in]   ePipeEnable     Enable pipe type. Incuding:
 *                              - \ref eCAP_BOTH_PIPE_DISABLE
 *                              - \ref eCAP_PLANAR
 *                              - \ref eCAP_PACKET
 *                              - \ref eCAP_BOTH_PIPE_ENABLE
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to enable engine clock and pipe type.
 */
void CAP_SetPipeEnable(
    BOOL bEngEnable,
    E_CAP_PIPE ePipeEnable
)
{
    outp32(REG_CAP_CTL, (inp32(REG_CAP_CTL) & ~(CAPEN | PKTEN | PLNEN))
           | (((bEngEnable ? CAPEN : 0x0))
              // | ((ePipeEnable & ~(PKTEN | PLNEN))<<5)) );
              | ((ePipeEnable & 0x03) << 5)));
} // DrvVideoIn_SetPipeEnable

/**
 * @brief       Get engine clock and pipe type.
 *
 * @param[out]  pbEngEnable     Enable engine clock.
 *                              1 : Enable engine clock.
 *                              0 : Disable engine clock.
 * @param[out]  pePipeEnable    Pipe type. Incuding:
 *                              - \ref eCAP_BOTH_PIPE_DISABLE
 *                              - \ref eCAP_PLANAR
 *                              - \ref eCAP_PACKET
 *                              - \ref eCAP_BOTH_PIPE_ENABLE
 * @return      None
 *
 * @details     This function is used to get engin clock and pipe type.
 */
void CAP_GetPipeEnable(PBOOL pbEngEnable, E_CAP_PIPE *pePipeEnable)
{
    UINT32 u32Temp = inp32(REG_CAP_CTL);

    *pbEngEnable = (u32Temp & CAPEN) ? TRUE : FALSE;
    *pePipeEnable = (E_CAP_PIPE)((u32Temp & (PKTEN | PLNEN)) >> 5);
} // DrvVideoIn_GetPipeEnable


/**
 * @brief       Set Shadow(Update) Register
 *
 * @details     This function is used to reload frame buffer address after
 *              setting shoaw(update) register.
 */
void CAP_SetShadowRegister(void)
{
    outp32(REG_CAP_CTL,  inp32(REG_CAP_CTL) | UPDATE);
} // DrvVideoIn_SetShadowRegister


/**
 * @brief      Set sensor polarity.
 *
 * @param[in]   bVsync     Sensor Vsync Polarity.
 *                         1  : High Active
 *                         0  : Low Active
 * @param[in]   bHsync     Sensor Hsync Polarity.
 *                         1  : High Active
 *                         0  : Low Active
 * @param[in]   bPixelClk  Sensor Vsync Polarity.
 *                         1  : Falling Edge
 *                         0  : Rising Edig
 * @return       None
 *
 * @details     This function is used to set sensor polarity.
 */
void CAP_SetSensorPolarity(BOOL bVsync, BOOL bHsync, BOOL bPixelClk)
{
    UINT32 u32Polarity, u32RegVal;
    u32RegVal = inp32(REG_CAP_PAR);
    //sysprintf("Enter Register addr = 0x%x\n", (REG_CAP_PAR));
    //sysprintf("Enter Register value = 0x%x\n", u32RegVal);
    u32Polarity = (((bVsync ? VSP : 0x0) | (bHsync ? HSP : 0x0)) | (bPixelClk ? PCLKP : 0x0));
    u32RegVal = (inp32(REG_CAP_PAR) & ~(VSP | HSP | PCLKP)) ;
    //sysprintf("REG_VPEPAR = 0x%x", (u32RegVal | u32Polarity));
    outp32((REG_CAP_PAR), (u32RegVal | u32Polarity));
}

/**
 * @brief      Get sensor polarity.
 *
 * @param[out]   pbVsync     Sensor Vsync Polarity.
 *                           1  : High Active
 *                           0  : Low Active
 * @param[out]   pbHsync     Sensor Hsync Polarity.
 *                           1  : High Active
 *                           0  : Low Active
 * @param[out]   pbPixelClk  Sensor Vsync Polarity.
 *                           1  : Falling Edge
 *                           0  : Rising Edig
 * @return       None
 *
 * @details     This function is used to get sensor polarity.
 */
void CAP_GetSensorPolarity(PBOOL pbVsync, PBOOL pbHsync, PBOOL pbPixelClk)
{
    UINT32 u32Temp = inp32(REG_CAP_PAR);

    *pbVsync = (u32Temp & VSP)  ? TRUE : FALSE;
    *pbHsync = (u32Temp & HSP) ? TRUE : FALSE;
    *pbPixelClk = (u32Temp & PCLKP) ? TRUE : FALSE;
}

/**
 * @brief      Set data format and order.
 *
 * @param[in]   eInputOrder     Data order for input format.Including :
 *                              - \ref eCAP_IN_UYVY = Y0 U0 Y1 V0
 *                              - \ref eCAP_IN_YUYV = Y0 V0 Y1 U0
 *                              - \ref eCAP_IN_VYUY = U0 Y0 V0 Y1
 *                              - \ref eCAP_IN_YVYU = V0 Y0 U0 Y1
 * @param[in]   eInputFormat    Input data format.Including :
 *                              - \ref eCAP_IN_YUV422
 *                              - \ref eCAP_IN_RGB565
 * @param[in]   eOutputFormat   Sensor Vsync Polarity.Including :
 *                              - \ref eCAP_OUT_YUV422 = YCbCr422
 *                              - \ref eCAP_OUT_ONLY_Y = only output Y
 *                              - \ref eCAP_OUT_RGB555 = rgb555
 *                              - \ref eCAP_OUT_RGB565 = rgb565
 * @return      None
 *
 * @details     This function is used to set data format and order.
 */
void CAP_SetDataFormatAndOrder(E_CAP_ORDER eInputOrder, E_CAP_IN_FORMAT eInputFormat, E_CAP_OUT_FORMAT eOutputFormat)
{
    outp32((REG_CAP_PAR), (inp32(REG_CAP_PAR) & ~(OUTFMT | INDATORD | INFMT))
           | ((((eInputOrder << 2) & INDATORD)
               | (eInputFormat & INFMT))
              | ((eOutputFormat << 4) & OUTFMT)));
} // DrvVideoIn_SetDataFormatAndOrder

/**
 * @brief      Get data format and order.
 *
 * @param[out]   peInputOrder    Data order for input format.Including :
 *                               - \ref eCAP_IN_UYVY
 *                               - \ref eCAP_IN_YUYV
 *                               - \ref eCAP_IN_VYUY
 *                               - \ref eCAP_IN_YVYU
 * @param[out]   peInputFormat   Input data format.Including :
 *                               - \ref eCAP_IN_YUV422
 *                               - \ref eCAP_IN_RGB565
 * @param[out]   peOutputFormat  Sensor Vsync Polarity.Including :
 *                               - \ref eCAP_OUT_YUV422 = YCbCr422
 *                               - \ref eCAP_OUT_ONLY_Y = only output Y
 *                               - \ref eCAP_OUT_RGB555 = rgb555
 *                               - \ref eCAP_OUT_RGB565 = rgb565
 * @return      None
 *
 * @details     This function is used to get data format and order.
 */
void CAP_GetDataFormatAndOrder(E_CAP_ORDER *peInputOrder, E_CAP_IN_FORMAT *peInputFormat, E_CAP_OUT_FORMAT *peOutputFormat)
{
    UINT32 u32Temp = inp32(REG_CAP_PAR);

    *peInputOrder = (E_CAP_ORDER)((u32Temp & INDATORD) >> 2);
    *peInputFormat = (E_CAP_IN_FORMAT)(u32Temp & INFMT);
    *peOutputFormat = (E_CAP_OUT_FORMAT)((u32Temp & OUTFMT) >> 4);
}

/**
 * @brief      Set planar format.
 *
 * @param[in]   ePlanarFmt     Data order for input format.Including :
 *                              - \ref eCAP_PLANAR_YUV422
 *                              - \ref eCAP_PLANAR_YUV420
 * @return      None
 *
 * @details     This function is used to set planar format.
 */
void CAP_SetPlanarFormat(E_CAP_PLANAR_FORMAT ePlanarFmt)
{
    switch (ePlanarFmt)
    {
    case eCAP_PLANAR_YUV422:
        outp32((REG_CAP_PAR), (inp32(REG_CAP_PAR) & ~(PLNFMT)));
        break;
    case eCAP_PLANAR_YUV420:
        outp32((REG_CAP_PAR), ((inp32(REG_CAP_PAR) | (PLNFMT))));
        break;
    }
}

/**
 * @brief      Get planar format.
 *
 * @retval      - \ref eCAP_PLANAR_YUV422 : Planar format is YUV420.
 * @retval      - \ref eCAP_PLANAR_YUV420 : Planar format is YUV422.
 *
 * @details     This function is used to get planar format.
 */
BOOL CAP_GetPlanarFormat(void)
{
    return ((inp32(REG_CAP_PAR) & PLNFMT) >> 7);
}

/**
 * @brief      Set motion detection parameter.
 *
 * @param[in]   bEnable      Enable Motion Detection.Including :
 *                           0  : Disable motion detection.
 *                           1  : Enable motion detection.
 * @param[in]   bBlockSize   Motion Detection Block Size.Including :
 *                           0  : Block size is set to 16x16.
 *                           1  : Block size is set to 8x8.
 * @param[in]   bSaveMode    Motion Detection Save Mode.Including :
 *                           0  : 1 bit DIFF + 7 Y Differential.
 *                           1  : 1 bit DIFF only.
 * @return      None
 *
 * @details     This function is used to set motion detection parameter.
 */
void CAP_SetMotionDet(BOOL bEnable, BOOL bBlockSize, BOOL bSaveMode)
{
    outp32(REG_CAP_MD, (inp32(REG_CAP_MD) & ~(MDSM | MDBS | MDEN)) |
           (((bEnable ? MDEN : 0) | (bBlockSize ? MDBS : 0)) |
            (bSaveMode ? MDSM : 0)));
}

/**
 * @brief      Get motion detection parameter.
 *
 * @param[out]   pbEnable      Enable Motion Detection.Including :
 *                             0  : Disable motion detection.
 *                             1  : Enable motion detection.
 * @param[out]   pbBlockSize   Motion Detection Block Size.Including :
 *                             0  : Block size is set to 16x16.
 *                             1  : Block size is set to 8x8.
 * @param[out]   pbSaveMode    Motion Detection Save Mode.Including :
 *                             0  : 1 bit DIFF + 7 Y Differential.
 *                             1  : 1 bit DIFF only.
 * @return       None
 *
 * @details      This function is used to get motion detection parameter.
 */
void CAP_GetMotionDet(PBOOL pbEnable, PBOOL pbBlockSize, PBOOL pbSaveMode)
{
    UINT32 u32RegData = inp32(REG_CAP_MD);
    *pbEnable = (u32RegData & MDEN);
    *pbBlockSize = (u32RegData & MDBS) >> 8;
    *pbSaveMode = (u32RegData & MDSM) >> 9;
}

/**
 * @brief      Set motion detection parameter externtion.
 *
 * @param[in]   u32DetFreq        Motion Detection frequency.Including :
 *                                0 : Each frame
 *                                1 : Every 2 frame
 *                                2 : Every 3 frame
 *                                3 : Every 4 frame
 * @param[in]   u32Threshold      Motion detection threshold.It should be 0~31.
 *
 * @param[in]   u32OutBuffer      Motion Detection Output Address Register.(Word Alignment)
 *
 * @param[in]   u32LumBuffer      Motion Detection Temp Y Output Address Register.(Word Alignment)
 *
 * @return       None
 *
 * @details      This function is used to set motion detection parameter externtion.
 */
void CAP_SetMotionDetEx(UINT32 u32DetFreq, UINT32 u32Threshold, UINT32 u32OutBuffer, UINT32 u32LumBuffer)
{
    outp32(REG_CAP_MD, (inp32(REG_CAP_MD) & ~MDDF) | ((u32DetFreq << 10) & MDDF));
    outp32(REG_CAP_MD, (inp32(REG_CAP_MD) & ~MDTHR) | ((u32Threshold << 16) & MDTHR));
    outp32(REG_CAP_MDADDR, u32OutBuffer);
    outp32(REG_CAP_MDYADDR, u32LumBuffer);
}

/**
 * @brief      Get motion detection parameter externtion.
 *
 * @param[out]   pu32DetFreq        Motion Detection frequency.Including :
 *                                  0 : Each frame
 *                                  1 : Every 2 frame
 *                                  2 : Every 3 frame
 *                                  3 : Every 4 frame
 * @param[out]   pu32Threshold      Motion detection threshold.It should be 0~31.
 *
 * @param[out]   pu32OutBuffer      Motion Detection Output Address Register.(Word Alignment)
 *
 * @param[out]   pu32LumBuffer      Motion Detection Temp Y Output Address Register.(Word Alignment)
 *
 * @return       None
 *
 * @details      This function is used to get motion detection parameter externtion.
 */
void CAP_GetMotionDetEx(PUINT32 pu32DetFreq, PUINT32 pu32Threshold, PUINT32 pu32OutBuffer, PUINT32 pu32LumBuffer)
{
    UINT32 u32RegData;
    u32RegData = inp32(REG_CAP_MD);
    *pu32DetFreq = u32RegData & MDDF;
    *pu32Threshold = u32RegData & MDTHR;
    *pu32OutBuffer = inp32(REG_CAP_MDADDR);
    *pu32LumBuffer = inp32(REG_CAP_MDYADDR);
}

/**
 * @brief      Set motion detection frequency.
 *
 * @param[in]  u32DetFreq        Motion Detection frequency.Including :
 *                               0 : Each frame
 *                               1 : Every 2 frame
 *                               2 : Every 3 frame
 *                               3 : Every 4 frame
 * @return     None
 *
 * @details    This function is used to set motion detection frequency.
 */
void CAP_SetMotionDetFreq(UINT32 u32DetFreq)
{
    outp32(REG_CAP_MD, (inp32(REG_CAP_MD) & ~MDDF) |
           ((u32DetFreq << 10) & MDDF));
}

/**
 * @brief      Get motion detection frequency.
 *
 * @param[out]  pu32DetFreq      Motion Detection frequency.Including :
 *                               0 : Each frame
 *                               1 : Every 2 frame
 *                               2 : Every 3 frame
 *                               3 : Every 4 frame
 * @return     None
 *
 * @details    This function is used to get motion detection frequency.
 */
void CAP_GetMotionDetFreq(PUINT32 pu32DetFreq)
{
    UINT32 u32RegData;
    u32RegData = inp32(REG_CAP_MD);
    *pu32DetFreq = u32RegData & MDDF;
}

/**
 * @brief      Set One shutte or continuous mode.
 *
 * @param[in]  bIsOneSutterMode      Enable One shutte.Including :
 *                                   1 :  Enable  One shutte mode.
 *                                   0 : Disable One shutte mode.
 * @return     None
 *
 * @details    This function is used to set one shutte or continuous mode.
 *             Image capture interface automatically disable the capture
 *             inteface after a frame bad been captured.
 */
void CAP_SetOperationMode(BOOL bIsOneSutterMode)
{
    outp32(REG_CAP_CTL, (inp32(REG_CAP_CTL) & ~SHUTTER) |
           ((bIsOneSutterMode << 16) & SHUTTER));
} // DrvVideoIn_SetOperationMode

/**
 * @brief      Get One shutte or continuous mode.
 *
 * @retval     1 : Disable one shutte mode
 * @retval     0 : Enable one shutte mode
 *
 * @details    This function is used to get one shutte or continuous mode.
 *             Image capture interface automatically disable the capture
 *             inteface after a frame bad been captured.
 */
BOOL CAP_GetOperationMode(void)
{
    return ((inp32(REG_CAP_CTL) & SHUTTER) ? TRUE : FALSE);
} // DrvVideoIn_GetOperationMode


/**
 * @brief        Get packet/planar processed data count.
 *
 * @param[in]    ePipe  Pipe type. Including :
 *               - \ref eCAP_PACKET
 *               - \ref eCAP_PLANAR
 *
 * @return       Get current packet/planar processed data count.
 *
 * @details    This function is used to get packet/planar processed data count.
 */
UINT32 CAP_GetProcessedDataCount(E_CAP_PIPE ePipe)
{
    if (ePipe == eCAP_PACKET)
        return inp32(REG_CAP_CURADDRP);       /* Packet pipe */
    else if (ePipe == eCAP_PLANAR)
        return inp32(REG_CAP_CURADDRY);       /* Planar pipe */
    else
        return 0;
}


/**
 * @brief        Set cropping window vertical/horizontal starting address.
 *
 * @param[in]    u32VerticalStart   Cropping window vertical starting address.
 * @param[in]    u32HorizontalStart Cropping window horizontal starting address.
 *
 * @return       None.
 *
 * @details    This function is used to set cropping window vertical/horizontal starting address.
 */
void CAP_SetCropWinStartAddr(UINT32 u32VerticalStart, UINT32 u32HorizontalStart)
{
    outp32(REG_CAP_CWSP, (inp32(REG_CAP_CWSP) & ~(CWSADDRV | CWSADDRH)) //(Y|X)
           | ((u32VerticalStart << 16)
              | u32HorizontalStart));
}


/**
 * @brief        Get cropping window vertical/horizontal starting address.
 *
 * @param[out]   pu32VerticalStart   Cropping window vertical starting address.
 * @param[out]   pu32HorizontalStart Cropping window horizontal starting address.
 *
 * @return       None.
 *
 * @details    This function is used to get cropping window vertical/horizontal starting address.
 */
void CAP_GetCropWinStartAddr(PUINT32 pu32VerticalStart, PUINT32 pu32HorizontalStart)
{
    UINT32 u32Temp = inp32(REG_CAP_CWSP);

    *pu32VerticalStart = (u32Temp & CWSADDRV) >> 16;
    *pu32HorizontalStart = u32Temp & CWSADDRH;
}

/**
 * @brief        Set cropping window size.
 *
 * @param[in]    u32Width   Cropping window width.
 * @param[in]    u32Height  Cropping window heigh.
 *
 * @return       None.
 *
 * @details    This function is used to set cropping window size.
 */
void CAP_SetCropWinSize(UINT32 u32Height, UINT32 u32Width)
{
    outp32(REG_CAP_CWS, (inp32(REG_CAP_CWS) & ~(CWH | CWW))
           | ((u32Height << 16)
              | u32Width));
}


/**
 * @brief        Get cropping window size.
 *
 * @param[out]   pu32Width   Cropping window width.
 * @param[out]   pu32Height  Cropping window heigh.
 *
 * @return       None.
 *
 * @details    This function is used to get cropping window size.
 */
void CAP_GetCropWinSize(PUINT32 pu32Height, PUINT32 pu32Width)
{
    UINT32 u32Temp = inp32(REG_CAP_CWS);

    *pu32Height = (u32Temp & CWH) >> 16;
    *pu32Width = u32Temp & CWW;
}

/**
 * @brief       Set packet/planar scaling vertical factor.
 *
 * @param[in]   ePipe   Pipe type.Including.
 *                      - \ref eCAP_PACKET.
 *                      - \ref eCAP_PLANAR.
 * @param[in]   u16Numerator  Scaling Vertical Factor N.
 * @param[in]   u16Denominator  Scaling Vertical Factor M.
 *
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to set packet/planar scaling vertical factor.
 *              The output image width will be equal to the image width * N/M.
 *              Note: The value of N must be equal to or less than M
 */
INT32 CAP_SetVerticalScaleFactor(E_CAP_PIPE ePipe, UINT16 u16Numerator, UINT16 u16Denominator)
{
    UINT8 u8NumeratorL = u16Numerator & 0xFF, u8NumeratorH = u16Numerator >> 8;
    UINT8 u8DenominatorL = u16Denominator & 0xFF, u8DenominatorH = u16Denominator >> 8;
    if (ePipe == eCAP_PACKET)
    {
        outp32(REG_CAP_PKTSL, (inp32(REG_CAP_PKTSL) & ~(PKTSVNL | PKTSVML))
               | ((u8NumeratorL << 24)
                  | (u8DenominatorL << 16)));
        outp32(REG_CAP_PKTSM, (inp32(REG_CAP_PKTSM) & ~(PKTSHMH | PKTSVMH))
               | ((u8NumeratorH << 24)
                  | (u8DenominatorH << 16)));
    }
    else if (ePipe == eCAP_PLANAR)
    {
        outp32(REG_CAP_PLNSL, (inp32(REG_CAP_PLNSL) & ~(PKTSVNL | PKTSVML))
               | ((u8NumeratorL << 24)
                  | (u8DenominatorL << 16)));
        outp32(REG_CAP_PLNSM, (inp32(REG_CAP_PLNSM) & ~(PKTSHMH | PKTSVMH))
               | ((u8NumeratorH << 24)
                  | (u8DenominatorH << 16)));
    }
    else
        return E_CAP_INVALID_PIPE;
    return Successful;
}

/**
 * @brief       Get packet/planar scaling vertical factor.
 *
 * @param[in]   ePipe   Pipe type.Including.
 *                      - \ref eCAP_PACKET.
 *                      - \ref eCAP_PLANAR.
 * @param[out]  pu16Numerator  Scaling Vertical Factor N.
 * @param[out]  pu16Denominator  Scaling Vertical Factor M.
 *
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to get packet/planar scaling vertical factor.
 *              The output image width will be equal to the image width * N/M.
 *              Note: The value of N must be equal to or less than M
 */
INT32 DrvCAP_GetVerticalScaleFactor(E_CAP_PIPE ePipe, PUINT16 pu16Numerator, PUINT16 pu16Denominator)
{
    UINT32 u32Temp1, u32Temp2;
    if (ePipe == eCAP_PACKET)
    {
        u32Temp1 = inp32(REG_CAP_PKTSL);
        u32Temp2 = inp32(REG_CAP_PKTSM);
    }
    else if (ePipe == eCAP_PLANAR)
    {
        u32Temp1 = inp32(REG_CAP_PLNSL);
        u32Temp2 = inp32(REG_CAP_PLNSM);
    }
    else
        return E_CAP_INVALID_PIPE;
    *pu16Numerator = ((u32Temp1 & PKTSVNL) >> 24) | (((u32Temp2 & PKTSHMH) >> 24) << 8);
    *pu16Denominator = (u32Temp1 & PKTSVML) >> 16 | (((u32Temp2 & PKTSVMH) >> 16) << 8);
    return Successful;
}

/**
 * @brief       Set packet/planar scaling horizontal factor.
 *
 * @param[in]   bPipe   Pipe type.Including.
 *                      - \ref eCAP_PACKET.
 *                      - \ref eCAP_PLANAR.
 * @param[in]   u16Numerator  Scaling Horizontal Factor N.
 * @param[in]   u16Denominator  Scaling Horizontal Factor M.
 *
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to set packet/planar scaling horizontal factor.
 *              The output image width will be equal to the image width * N/M.
 *              Note: The value of N must be equal to or less than M
 */
INT32 CAP_SetHorizontalScaleFactor(E_CAP_PIPE bPipe, UINT16 u16Numerator, UINT16 u16Denominator)
{
    UINT8 u8NumeratorL = u16Numerator & 0xFF, u8NumeratorH = u16Numerator >> 8;
    UINT8 u8DenominatorL = u16Denominator & 0xFF, u8DenominatorH = u16Denominator >> 8;
    if (bPipe == eCAP_PACKET)
    {
        outp32(REG_CAP_PKTSL, (inp32(REG_CAP_PKTSL) & ~(PKTSHNL | PKTSHML))
               | ((u8NumeratorL << 8)
                  | u8DenominatorL));
        outp32(REG_CAP_PKTSM, (inp32(REG_CAP_PKTSM) & ~(PKTSHNH | PKTSHMH))
               | ((u8NumeratorH << 8)
                  | u8DenominatorH));
    }
    else  if (bPipe == eCAP_PLANAR)
    {
        outp32(REG_CAP_PLNSL, (inp32(REG_CAP_PLNSL) & ~(PKTSHNL | PKTSHML))
               | ((u8NumeratorL << 8)
                  | u8DenominatorL));
        outp32(REG_CAP_PLNSM, (inp32(REG_CAP_PLNSM) & ~(PKTSHNH | PKTSHMH))
               | ((u8NumeratorH << 8)
                  | u8DenominatorH));
    }
    else
        return E_CAP_INVALID_PIPE;
    return Successful;
}

/**
 * @brief       Get packet/planar scaling horizontal factor.
 *
 * @param[in]   bPipe   Pipe type.Including.
 *                      - \ref eCAP_PACKET.
 *                      - \ref eCAP_PLANAR.
 * @param[out]  pu16Numerator  Scaling Horizontal Factor N.
 * @param[out]  pu16Denominator  Scaling Horizontal Factor M.
 *
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details     This function is used to get packet/planar scaling horizontal factor.
 *              The output image width will be equal to the image width * N/M.
 *              Note: The value of N must be equal to or less than M.
 */
INT32 CAP_GetHorizontalScaleFactor(E_CAP_PIPE bPipe, PUINT16 pu16Numerator, PUINT16 pu16Denominator)
{
    UINT32 u32Temp1, u32Temp2;
    if (bPipe == eCAP_PACKET)
    {
        u32Temp1 = inp32(REG_CAP_PKTSL);
        u32Temp2 = inp32(REG_CAP_PKTSM);
    }
    else  if (bPipe == eCAP_PLANAR)
    {
        u32Temp1 = inp32(REG_CAP_PLNSL);
        u32Temp2 = inp32(REG_CAP_PLNSM);
    }
    else
        return E_CAP_INVALID_PIPE;
    *pu16Numerator = ((u32Temp1 & PKTSHNL) >> 8) | (u32Temp2 & PKTSHNH);
    *pu16Denominator = (u32Temp1 & PKTSHML) | ((u32Temp2 & PKTSHMH) << 8);
    return Successful;
}

/**
 * @brief      Set scaling frame rate factor.
 *
 * @param[in]  u8Numerator  Scaling Frame Rate Factor N.
 * @param[in]  u8Denominator  Scaling Frame Rate Factor M.
 *
 * @return     None.
 *
 * @details    This function is used to set scaling frame rate factor..
 *             The output image frame rate will be equal to input image frame rate * (N/M).
 *             Note: The value of N must be equal to or less than M.
 */
void DrvCAP_SetFrameRateScaleFactor(UINT8 u8Numerator, UINT8 u8Denominator)
{
    outp32(REG_CAP_FRCTL, (inp32(REG_CAP_FRCTL) & ~(FRN | FRM))
           | (((u8Numerator << 8) & FRN)
              | (u8Denominator & FRM)));
} // DrvVideoIn_SetFrameRateScaleFactor

/**
 * @brief      Get scaling frame rate factor.
 *
 * @param[out] pu8Numerator  Scaling Frame Rate Factor N.
 * @param[out] pu8Denominator  Scaling Frame Rate Factor M.
 *
 * @return     None.
 *
 * @details    This function is used to get scaling frame rate factor..
 *             The output image frame rate will be equal to input image frame rate * (N/M).
 *             Note: The value of N must be equal to or less than M.
 */
void DrvCAP_GetFrameRateScaleFactor(PUINT8 pu8Numerator, PUINT8 pu8Denominator)
{
    UINT32 u32Temp = inp32(REG_CAP_FRCTL);

    *pu8Numerator = (u32Temp & FRN) >> 8;
    *pu8Denominator = u32Temp & FRM;
}

/**
 * @brief      Set address match
 *
 * @param[in]  u32AddressMatch  Compare Memory Base Address.It should be 0~0xFFFFFFFF.
 *
 * @return     None.
 *
 * @details    This function is used to set compare memory base address.
 */
void DrvCAP_SetAddressMatch(UINT32 u32AddressMatch)
{
    outp32(REG_CAP_CMPADDR, u32AddressMatch);
}

/**
 * @brief      Get address match
 *
 * @param[out] pu32AddressMatch  Compare Memory Base Address.It should be 0~0xFFFFFFFF.
 *
 * @return     None.
 *
 * @details    This function is used to get compare memory base address.
 */
void CAP_GetAddressMatch(PUINT32 pu32AddressMatch)
{
    *pu32AddressMatch = inp32(REG_CAP_CMPADDR);
}

/**
 * @brief      Set frame output pixel stride width.
 *
 * @param[in] u32PacketStride  Packet frame output pixel stride width.It should be 0~0x3FFF.
 * @param[in] u32PlanarStride  Planar frame output pixel stride width.It should be 0~0x3FFF.
 *
 * @return     None.
 *
 * @details    This function is used to set frame output pixel stride width.
 */
void CAP_SetStride(UINT32 u32PacketStride, UINT32 u32PlanarStride)
{
    outp32(REG_CAP_STRIDE, ((u32PlanarStride << 16) & PLNSTRIDE) |
           (u32PacketStride & PKTSTRIDE));
}

/**
 * @brief      Get frame output pixel stride width.
 *
 * @param[out] pu32PacketStride  Packet frame output pixel stride width.It should be 0~0x3FFF.
 * @param[out] pu32PlanarStride  Planar frame output pixel stride width.It should be 0~0x3FFF.
 *
 * @return     None.
 *
 * @details    This function is used to get frame output pixel stride width.
 */
void CAP_GetStride(PUINT32  pu32PacketStride, PUINT32 pu32PlanarStride)
{
    UINT32 u32Tmp =  inp32(REG_CAP_STRIDE);
    *pu32PlanarStride = (u32Tmp & PLNSTRIDE) >> 16;
    *pu32PacketStride = u32Tmp & PKTSTRIDE;
}

/**
 * @brief      Set system memory packet/planar base address.
 *
 * @param[in] ePipe             Pipe type.Including:
 *                              - \ref eCAP_PACKET
 *                              - \ref eCAP_PLANAR
 *
 * @param[in] eBuf              Packet/Planar buffer address.
 *                              - \ref eCAP_BUF0 :
 *                                     Packet : Packet base address 0
 *                                     Planar : Planar Y base address
 *                              - \ref eCAP_BUF1
 *                                     Packet : Packet base address 1
 *                                     Planar : Planar U base address
 *                              - \ref eCAP_BUF2
 *                                     Packet : None.
 *                                     Planar : Planar V base address
 *
 * @param[in] u32BaseStartAddr  System Memory Base Address.It should be 0~0xFFFFFFFF.
 *
 * @retval      0 Success
 * @retval      <0 Error code
 *
 * @details    This function is used to set system memory packet/planar base address.
 */
INT32 CAP_SetBaseStartAddress(E_CAP_PIPE ePipe, E_CAP_BUFFER eBuf, UINT32 u32BaseStartAddr)
{
    if (ePipe == eCAP_PACKET)
    {
        if (eBuf > eCAP_BUF1)
            return E_CAP_INVALID_BUF;
        outp32(REG_CAP_PKTBA0 + eBuf * 4, u32BaseStartAddr);
    }
    else if (ePipe == eCAP_PLANAR)
    {
        if (eBuf > eCAP_BUF2)
            return E_CAP_INVALID_BUF;
        outp32(REG_CAP_YBA + eBuf * 4, u32BaseStartAddr);
    }
    else
        return E_CAP_INVALID_PIPE;
    return Successful;
}

/**
 * @brief      Get system memory packet/planar base address.
 *
 * @param[in]  ePipe              Pipe type.Including:
 *                                - \ref eCAP_PACKET
 *                                - \ref eCAP_PLANAR
 *
 * @param[in]  eBuf               Packet/Planar buffer address.
 *                                - \ref eCAP_BUF0 :
 *                                     Packet : Packet base address 0
 *                                     Planar : Planar Y base address
 *                                - \ref eCAP_BUF1
 *                                     Packet : Packet base address 1
 *                                     Planar : Planar U base address
 *                                - \ref eCAP_BUF2
 *                                     Packet : None.
 *                                     Planar : Planar V base address
 *
 * @param[out] pu32BaseStartAddr  System Memory Base Address.It should be 0~0xFFFFFFFF.
 *
 * @retval     0 Success
 * @retval     <0 Error code
 *
 * @details    This function is used to get system memory packet/planar base address.
 */
INT32 CAP_GetBaseStartAddress(E_CAP_PIPE ePipe, E_CAP_BUFFER eBuf, PUINT32 pu32BaseStartAddr)
{
    if (ePipe == eCAP_PACKET)
    {
        if (eBuf > eCAP_BUF1)
            return E_CAP_INVALID_BUF;
        *pu32BaseStartAddr = inp32(REG_CAP_PKTBA0 + eBuf * 4);
    }
    else if (ePipe == eCAP_PLANAR)
    {
        if (eBuf > eCAP_BUF2)
            return E_CAP_INVALID_BUF;
        *pu32BaseStartAddr = inp32(REG_CAP_YBA + eBuf * 4);
    }
    else
        return E_CAP_INVALID_PIPE;
    return Successful;
}

/**
 * @brief       Set standard CCIR656.
 *
 * @param[in]   bIsStandard      Standard CCIR656.
 *                               - 1 : Standard CCIR656 mode.
 *                               - 0 : Non-Standard CCIR656 mode. (OV7725 or Hynix 702)
 * @return      None.
 *
 * @details    This function is used to set standard CCIR65/non-standard CCIR65.
 */
void CAP_SetStandardCCIR656(BOOL bIsStandard)
{
    if (bIsStandard == TRUE)
        outp32(REG_CAP_PAR, inp32(REG_CAP_PAR) & ~FBB);   // Standard
    else
        outp32(REG_CAP_PAR, inp32(REG_CAP_PAR) | FBB);      // Non-Standard
}

/**
 * @brief      Set color effect
 *
 * @param[in]  eColorMode  Available as following.
 *                         - \ref eCAP_CEF_NORMAL : Normal Color.
 *                         - \ref eCAP_CEF_SEPIA : Sepia effect,
 *                         corresponding U,V component value is set at register - \ref REG_CAP_SEPIA.
 *                         - \ref eCAP_CEF_NEGATIVE  : Negative picture.
 *                         - \ref eCAP_CEF_POSTERIZE : Posterize image,
 *                         the Y, U, V components posterizing factor are set at register - \ref REG_CAP_POSTERIZE.
 *
 * @retval     0 Success
 * @retval     <0 Error code
 *
 * @details    This function is used to set color effect.
 */
INT32 CAP_SetColorEffect(E_CAP_CEF eColorMode)
{
    if (eColorMode > eCAP_CEF_POSTERIZE)
        return E_CAP_INVALID_COLOR_MODE;
    outp32(REG_CAP_PAR, (inp32(REG_CAP_PAR) & ~COLORCTL) |
           (eColorMode << 11));
    return Successful;
}

/**
 * @brief      Get color effect
 *
 * @param[out] peColorMode  Available as following.
 *                         - \ref eCAP_CEF_NORMAL : Normal Color.
 *                         - \ref eCAP_CEF_SEPIA : Sepia effect,
 *                         corresponding U,V component value is set at register - \ref REG_CAP_SEPIA.
 *                         - \ref eCAP_CEF_NEGATIVE  : Negative picture.
 *                         - \ref eCAP_CEF_POSTERIZE : Posterize image,
 *                         the Y, U, V components posterizing factor are set at register - \ref REG_CAP_POSTERIZE.
 *
 * @return     None.
 *
 * @details    This function is used to get color effect.
 */
void DrvCAP_GetColorEffect(E_CAP_CEF   *peColorMode)
{
    UINT32 u32Tmp = inp32(REG_CAP_PAR);
    *peColorMode = (E_CAP_CEF)((u32Tmp & COLORCTL) >> 11);
}

/**
 * @brief      Set color effect parameter
 *
 * @param[in]  u8YComp  The constant Y component.If eColorMode is set to
 *                      eCAP_CEF_SEPIA : the constant Y component in - \ref REG_CAP_SEPIA.
 *                      eCAP_CEF_POSTERIZE : the constant Y component in - \ref REG_CAP_POSTERIZE.
 * @param[in]  u8UComp  The constant U component.
 *                      eCAP_CEF_SEPIA : the constant U component in - \ref REG_CAP_SEPIA.
 *                      eCAP_CEF_POSTERIZE : the constant U component in - \ref REG_CAP_POSTERIZE.
 * @param[in]  u8VComp  The constant V component.
 *                      eCAP_CEF_SEPIA : the constant V component in - \ref REG_CAP_SEPIA.
 *                      eCAP_CEF_POSTERIZE : the constant V component in - \ref REG_CAP_POSTERIZE.
 * @retval     0 Success
 * @retval     <0 Error code
 *
 * @details    This function is used to set color effect parameter.
 */
INT32 CAP_SetColorEffectParameter(UINT8 u8YComp, UINT8 u8UComp, UINT8 u8VComp)
{
    UINT32 u32Tmp = inp32(REG_CAP_PAR);
    UINT32 u32ColorMode = (u32Tmp & COLORCTL) >> 11;
    if (u32ColorMode == eCAP_CEF_SEPIA)
    {
        outp32(REG_CAP_SEPIA, (((UINT32)u8UComp << 8) | u8VComp));
    }
    else if (u32ColorMode == eCAP_CEF_POSTERIZE)
    {
        outp32(REG_CAP_POSTERIZE, (((UINT32)u8YComp << 16) | ((UINT32)u8UComp << 8) | u8VComp));
    }
    else
    {
        return E_CAP_WRONG_COLOR_PARAMETER;
    }
    return Successful;
}

/**
 * @brief      Get color effect parameter
 *
 * @param[out] pu8YComp  The constant Y component.If eColorMode is set to
 *                       eCAP_CEF_SEPIA : the constant Y component in - \ref REG_CAP_SEPIA.
 *                       eCAP_CEF_POSTERIZE : the constant Y component in - \ref REG_CAP_POSTERIZE.
 * @param[out] pu8UComp  The constant U component.
 *                       eCAP_CEF_SEPIA : the constant U component in - \ref REG_CAP_SEPIA.
 *                       eCAP_CEF_POSTERIZE : the constant U component in - \ref REG_CAP_POSTERIZE.
 * @param[out] pu8VComp  The constant V component.
 *                       eCAP_CEF_SEPIA : the constant V component in - \ref REG_CAP_SEPIA.
 *                       eCAP_CEF_POSTERIZE : the constant V component in - \ref REG_CAP_POSTERIZE.
 * @retval     0 Success
 * @retval     <0 Error code
 *
 * @details    This function is used to get color effect parameter.
 */
INT32 CAP_GetColorEffectParameter(PUINT8 pu8YComp, PUINT8 pu8UComp, PUINT8 pu8VComp)
{
    UINT32 u32Tmp = inp32(REG_CAP_PAR);
    UINT32 u32ColorMode = (u32Tmp & COLORCTL) >> 11;
    if (u32ColorMode == eCAP_CEF_SEPIA)
    {
        u32Tmp = inp32(REG_CAP_SEPIA);
        *pu8UComp = (u32Tmp & 0xFF00) >> 8;
        *pu8VComp = u32Tmp & 0xFF;
    }
    else if (u32ColorMode == eCAP_CEF_POSTERIZE)
    {
        u32Tmp = inp32(REG_CAP_POSTERIZE);
        *pu8YComp = (u32Tmp & 0xFF0000) >> 16;
        *pu8UComp = (u32Tmp & 0xFF00) >> 8;
        *pu8VComp = u32Tmp & 0xFF;
    }
    else
    {
        return E_CAP_WRONG_COLOR_PARAMETER;
    }
    return Successful;
}

/// @cond HIDDEN_SYMBOLS
CAPDEV_T CAP =
{
    CAP_Init,                         // void  (*Init)(BOOL bIsEnableSnrClock, E_CAP_SNR_SRC eSnrSrc, UINT32 u32SensorFreqKHz, E_CAP_DEV_TYPE eDevType):
    CAP_Open,                         // INT32 (*Open)(UINT32 u32SensorFreqKHz);
    CAP_Close,                        // void  (*Close)(void);
    CAP_SetPipeEnable,                // void  (*SetPipeEnable)(BOOL bEngEnable, E_CAP_PIPE ePipeEnable);
    CAP_SetPlanarFormat,              // void  (*SetPlanarFormat)(E_CAP_PLANAR_FORMAT ePlanarFmt);
    CAP_SetCropWinSize,               // void  (*SetCropWinSize)(UINT32 u32height, UINT32 u32width);
    CAP_SetCropWinStartAddr,          // void  (*SetCropWinStartAddr)(UINT32 u32VerticalStart, UINT32 u32HorizontalStart);
    CAP_SetStride,                    // void  (*SetStride)(UINT32 u16packetstride, UINT32 u32planarstride);
    CAP_GetStride,                    // void  (*GetStride)(PUINT32 pu32PacketStride, PUINT32 pu32PlanarStride);
    CAP_EnableInt,                    // INT32 (*EnableInt)(E_CAP_INT_TYPE eIntType);
    CAP_DisableInt,                   // INT32 (*DisableInt)(E_CAP_INT_TYPE eIntType);
    CAP_InstallCallback,              // INT32 (*InstallCallback)(E_CAP_INT_TYPE eIntType, PFN_CAP_CALLBACK pfnCallback, PFN_CAP_CALLBACK *pfnOldCallback);
    CAP_SetBaseStartAddress,          // INT32 (*SetBaseStartAddress(E_CAP_PIPE ePipe, E_CAP_BUFFER eBuf, UINT32 u32BaseStartAddr);
    CAP_SetOperationMode,             // void  (*SetOperationMode(BOOL bIsOneSutterMode);
    CAP_GetOperationMode,             // BOOL  (*GetOperationMode)(void);
    CAP_SetPacketFrameBufferControl,  // void  (*videoIn1_SetPacketFrameBufferControl)(BOOL bFrameSwitch, BOOL bFrameBufferSel);
    CAP_SetSensorPolarity,            // void  (*videoIn1_SetSensorPolarity)(BOOL bVsync, BOOL bHsync,  BOOL bPixelClk);
    CAP_SetColorEffectParameter,      // INT32 (*SetColorEffectParameter)(UINT8 u8YComp, UINT8 u8UComp, UINT8 u8VComp);
    CAP_SetDataFormatAndOrder,        // void  (*SetDataFormatAndOrder)(E_CAP_ORDER eInputOrder, E_CAP_IN_FORMAT eInputFormat, E_CAP_OUT_FORMAT eOutputFormat)
    CAP_SetMotionDet,                 // void  (*SetMotionDet)(BOOL bEnable, BOOL bBlockSize,BOOL bSaveMode);
    CAP_SetMotionDetEx,               // void  (*SetMotionDetEx)(UINT32 u32Threshold, UINT32 u32OutBuffer, UINT32 u32LumBuffer);
    CAP_SetStandardCCIR656,           // void  (*SetStandardCcir656)(BOOL);
    CAP_SetShadowRegister             // void  (*SetShadowRegister)(void);
};
/// @endcond HIDDEN_SYMBOLS

/*@}*/ /* end of group N9H30_CAP_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group N9H30_CAP_Driver */

/*@}*/ /* end of group N9H30_Device_Driver */