summaryrefslogtreecommitdiff
path: root/drivers/misc/gpsip/cgdriver/CgCpu.h
blob: de064b001f9a22492e7c2c0ebf0e11c5842fb0d4 (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
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
/******************************************************************************
 *                                LEGAL NOTICE                                *
 *                                                                            *
 *  USE OF THIS SOFTWARE (including any copy or compiled version thereof) AND *
 *  DOCUMENTATION IS SUBJECT TO THE SOFTWARE LICENSE AND RESTRICTIONS AND THE *
 *  WARRANTY DISLCAIMER SET FORTH IN LEGAL_NOTICE.TXT FILE. IF YOU DO NOT     *
 *  FULLY ACCEPT THE TERMS, YOU MAY NOT INSTALL OR OTHERWISE USE THE SOFTWARE *
 *  OR DOCUMENTATION.                                                         *
 *  NOTWITHSTANDING ANYTHING TO THE CONTRARY IN THIS NOTICE, INSTALLING OR    *
 *  OTHERISE USING THE SOFTWARE OR DOCUMENTATION INDICATES YOUR ACCEPTANCE OF *
 *  THE LICENSE TERMS AS STATED.                                              *
 *                                                                            *
 ******************************************************************************/
/* Version: 1.8.9\3686 */
/* Build  : 13 */
/* Date   : 12/08/2012 */

/**
	\file
	\brief CPU API
	\attention This file should not be modified.
		If you think something is wrong here, please contact CellGuide

	CellGuide's API to access low-level CPU resources

	The API is used by CellGuide drivers and applications that require access to low-level CPU resources
	like DMA, SPI.

	CPU depended code is divided to the following API groups:
	\li \ref cg_cpu_clk
	\li \ref cg_cpu_dma
	\li \ref cg_cpu_gpio
	\li \ref cg_cpu_spi
	\li \ref cg_cpu_interrupt
	\li \ref cg_cpu_rtc
	\li \ref cg_cpu_miscellanies
*/

#ifndef CG_CPU_H
#define CG_CPU_H

#ifdef	__cplusplus
	extern "C" {
#endif /*__cplusplus*/

// ===========================================================================

#include "CgTypes.h"
#include "CgReturnCodes.h"
#include "CgCpuOs.h"

// ===========================================================================

/** helper macro to calculate minimum of 2 numbers */
#ifndef MIN
	#define MIN(__a__, __b__) 					((__a__) < (__b__) ? (__a__) : (__b__))
#endif

/** helper macro to align memory */
#ifndef ALLIGN
	#define ALLIGN(__var__,__base__)    ((__var__)%(__base__)) ? __var__ = (__base__) * ((U32)((__var__) / (__base__)) + 1) : (__var__)
#endif

/** helper macro to find the ceiling of a number */
#ifndef DIV_CEIL
	#define DIV_CEIL(__counter__,__devider__)			( ((__counter__) + (__devider__) -1) / (__devider__) )
#endif

/** Undefined addresss means the address needs to be filled during porting **/
#define CG_UNDEFINED_ADDRESS		(0xFFFFFFFF)

/** helper macro to get the 'controller' part of a channel code */
#define CG_CONTROLLER(ch)			(ch & 0xFFFF0000)

/** helper macro to get the 'channel' part of a channel code */
#define CG_CHANNEL(ch)				(ch & 0x0000FFFF)

/** helper macro to check NULL pointer in argument */
#define ASSERT_NOT_NULL(p)			if (p == NULL) return ECgNullPointer;

/** helper macro to calculate register index from it's offset */
#define REGIDX(offset)			(offset/4)


/** Bit field definition
  This structure is used to define a sub-register bit field, with arbitrary size.
  */
typedef struct {
	U32 start;								/**< start bit (0 based)  0 == LSB */
	U32 length;								/**< length of bit field (1-32) */
	U32 defaultValue;						/**< default value */
} TCgBitField;


#define CG_CPU_DEVICE_SPI			(1)

#define CG_CPU_SPI_MODE_DMA			(0)		/**< Use DMA to handle SPI communication */
#define CG_CPU_SPI_MODE_MANUAL		(1)		/**< Use polling to handle SPI communication */

/** \name  Controllers logical codes
\{
*/


/**
DMA Controller logical unit code
\note Do not change; this value is used internally in the driver
\ingroup cg_cpu_dma
*/
#define CG_CPU_CONTROLLER_DMA						(0x00020000)

/**
SPI Controller logical unit code
\note Do not change; this value is used internally in the driver
\ingroup cg_cpu_spi
*/
#define CG_CPU_CONTROLLER_SPI						(0x00030000)

/**
GPIO Controller logical unit code
\note Do not change; this value is used internally in the driver
\ingroup cg_cpu_gpio
*/
#define CG_CPU_CONTROLLER_GPIO						(0x00040000)



/** \} Controllers logical codes*/

/** Virtual GPIO groups definitions
	These values are used to define CellGuide 'virtual' GPIO codes, later converted to specific
	CPU GPIO register and pin.
 */


/** Maximum number of GPIOs in a group.
	the number is artificial, and should be the largest GPIO group there is.
*/
#define CG_CPU_GPIO_GROUP_SIZE		(128)

#define CG_CPU_GPIO_GROUP_INDEX_A  (1) /**< GPIO group A index */
#define CG_CPU_GPIO_GROUP_INDEX_B  (CG_CPU_GPIO_GROUP_INDEX_A+1) /**< GPIO group B index */
#define CG_CPU_GPIO_GROUP_INDEX_C  (CG_CPU_GPIO_GROUP_INDEX_B+1) /**< GPIO group C index */
#define CG_CPU_GPIO_GROUP_INDEX_D  (CG_CPU_GPIO_GROUP_INDEX_C+1) /**< GPIO group A index */
#define CG_CPU_GPIO_GROUP_INDEX_E  (CG_CPU_GPIO_GROUP_INDEX_D+1) /**< GPIO group A index */
#define CG_CPU_GPIO_GROUP_INDEX_F  (CG_CPU_GPIO_GROUP_INDEX_E+1) /**< GPIO group A index */
#define CG_CPU_GPIO_GROUP_INDEX_G  (CG_CPU_GPIO_GROUP_INDEX_F+1) /**< GPIO group G index */
#define CG_CPU_GPIO_GROUP_INDEX_H  (CG_CPU_GPIO_GROUP_INDEX_G+1) /**< GPIO group H index */
#define CG_CPU_GPIO_GROUP_INDEX_I  (CG_CPU_GPIO_GROUP_INDEX_H+1) /**< GPIO group I index */
#define CG_CPU_GPIO_GROUP_INDEX_J  (CG_CPU_GPIO_GROUP_INDEX_I+1) /**< GPIO group J index */
#define CG_CPU_GPIO_GROUP_INDEX_K  (CG_CPU_GPIO_GROUP_INDEX_J+1) /**< GPIO group K index */
#define CG_CPU_GPIO_GROUP_INDEX_L  (CG_CPU_GPIO_GROUP_INDEX_K+1) /**< GPIO group L index */
#define CG_CPU_GPIO_GROUP_INDEX_M  (CG_CPU_GPIO_GROUP_INDEX_L+1) /**< GPIO group M index */
#define CG_CPU_GPIO_GROUP_INDEX_N  (CG_CPU_GPIO_GROUP_INDEX_M+1) /**< GPIO group N index */
#define CG_CPU_GPIO_GROUP_INDEX_O  (CG_CPU_GPIO_GROUP_INDEX_N+1) /**< GPIO group O index */
#define CG_CPU_GPIO_GROUP_INDEX_P  (CG_CPU_GPIO_GROUP_INDEX_O+1) /**< GPIO group P index */
#define CG_CPU_GPIO_GROUP_INDEX_Q  (CG_CPU_GPIO_GROUP_INDEX_P+1) /**< GPIO group Q index */
#define CG_CPU_GPIO_GROUP_INDEX_R  (CG_CPU_GPIO_GROUP_INDEX_Q+1) /**< GPIO group R index */
#define CG_CPU_GPIO_GROUP_INDEX_S  (CG_CPU_GPIO_GROUP_INDEX_R+1) /**< GPIO group S index */
#define CG_CPU_GPIO_GROUP_INDEX_T  (CG_CPU_GPIO_GROUP_INDEX_S+1) /**< GPIO group T index */
#define CG_CPU_GPIO_GROUP_INDEX_U  (CG_CPU_GPIO_GROUP_INDEX_T+1) /**< GPIO group U index */
#define CG_CPU_GPIO_GROUP_INDEX_V  (CG_CPU_GPIO_GROUP_INDEX_U+1) /**< GPIO group V index */
#define CG_CPU_GPIO_GROUP_INDEX_W  (CG_CPU_GPIO_GROUP_INDEX_V+1) /**< GPIO group W index */
#define CG_CPU_GPIO_GROUP_INDEX_X  (CG_CPU_GPIO_GROUP_INDEX_W+1) /**< GPIO group X index */
#define CG_CPU_GPIO_GROUP_INDEX_Y  (CG_CPU_GPIO_GROUP_INDEX_X+1) /**< GPIO group Y index */
#define CG_CPU_GPIO_GROUP_INDEX_Z  (CG_CPU_GPIO_GROUP_INDEX_Y+1) /**< GPIO group Z index */
#define CG_CPU_GPIO_GROUP_INDEX___  (CG_CPU_GPIO_GROUP_INDEX_Z+1)


#define CG_CPU_GPIO_GROUP_A	 (CG_CPU_GPIO_GROUP_INDEX_A * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group A */
#define CG_CPU_GPIO_GROUP_B	 (CG_CPU_GPIO_GROUP_INDEX_B * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group B */
#define CG_CPU_GPIO_GROUP_C	 (CG_CPU_GPIO_GROUP_INDEX_C * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group C */
#define CG_CPU_GPIO_GROUP_D	 (CG_CPU_GPIO_GROUP_INDEX_D * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group D */
#define CG_CPU_GPIO_GROUP_E	 (CG_CPU_GPIO_GROUP_INDEX_E * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group E */
#define CG_CPU_GPIO_GROUP_F	 (CG_CPU_GPIO_GROUP_INDEX_F * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group F */
#define CG_CPU_GPIO_GROUP_G	 (CG_CPU_GPIO_GROUP_INDEX_G * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group G */
#define CG_CPU_GPIO_GROUP_H	 (CG_CPU_GPIO_GROUP_INDEX_H * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group H */
#define CG_CPU_GPIO_GROUP_I	 (CG_CPU_GPIO_GROUP_INDEX_I * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group I */
#define CG_CPU_GPIO_GROUP_J	 (CG_CPU_GPIO_GROUP_INDEX_J * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group J */
#define CG_CPU_GPIO_GROUP_K	 (CG_CPU_GPIO_GROUP_INDEX_K * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group K */
#define CG_CPU_GPIO_GROUP_L	 (CG_CPU_GPIO_GROUP_INDEX_L * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group L */
#define CG_CPU_GPIO_GROUP_M	 (CG_CPU_GPIO_GROUP_INDEX_M * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group M */
#define CG_CPU_GPIO_GROUP_N	 (CG_CPU_GPIO_GROUP_INDEX_N * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group N */
#define CG_CPU_GPIO_GROUP_O	 (CG_CPU_GPIO_GROUP_INDEX_O * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group O */
#define CG_CPU_GPIO_GROUP_P	 (CG_CPU_GPIO_GROUP_INDEX_P * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group P */
#define CG_CPU_GPIO_GROUP_Q	 (CG_CPU_GPIO_GROUP_INDEX_Q * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group Q */
#define CG_CPU_GPIO_GROUP_R	 (CG_CPU_GPIO_GROUP_INDEX_R * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group R */
#define CG_CPU_GPIO_GROUP_S	 (CG_CPU_GPIO_GROUP_INDEX_S * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group S */
#define CG_CPU_GPIO_GROUP_T	 (CG_CPU_GPIO_GROUP_INDEX_T * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group T */
#define CG_CPU_GPIO_GROUP_U	 (CG_CPU_GPIO_GROUP_INDEX_U * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group U */
#define CG_CPU_GPIO_GROUP_V	 (CG_CPU_GPIO_GROUP_INDEX_V * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group V */
#define CG_CPU_GPIO_GROUP_W	 (CG_CPU_GPIO_GROUP_INDEX_W * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group W */
#define CG_CPU_GPIO_GROUP_X	 (CG_CPU_GPIO_GROUP_INDEX_X * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group X */
#define CG_CPU_GPIO_GROUP_Y	 (CG_CPU_GPIO_GROUP_INDEX_Y * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group Y */
#define CG_CPU_GPIO_GROUP_Z	 (CG_CPU_GPIO_GROUP_INDEX_Z * CG_CPU_GPIO_GROUP_SIZE)	/**< 1st GPIO code in group Z */



#define REG32(r) *((volatile U32*)(r))

#define REG_GET1(aReg, aPin) \
	((aReg >> aPin) & 0x0001)

#define REG_GET2(aReg, aPin) \
	((aReg >> (aPin/2)) & 0x0003)

#define REG_GET4(aReg, aPin) \
	((aReg >> (aPin/4)) & 0x000F)

#define REG_GET8(aReg, aPin) \
	((aReg >> (aPin/8)) & 0x00FF)


#define REG_SET(aReg, aOffset, aMask, aVal)  {\
	U32 regval = ((aVal) << ((aOffset)));	\
	U32 mask = ~((U32)aMask << ((aOffset)));	\
	if ((aReg & ~mask) != regval) {\
 		aReg &= (mask); \
		aReg |= regval; \
		}\
	}

#define REG_SET1(aReg, aPin, aVal)  {\
	U32 regval = ((aVal) << ((aPin)));	\
	U32 mask = ~((U32)1 << ((aPin)));	\
 	if(aVal == 0) aReg &= (mask); \
	else aReg |= regval; \
	}


#define REG_SET2(aReg, aPin, aVal)  {\
	U32 regval = ((aVal) << ((aPin)*2));	\
	U32 mask = ~((U32)3 << ((aPin)*2));	\
	if ((aReg & ~mask) != regval) {\
 		aReg &= (mask); \
		aReg |= regval; \
		}\
	}

#define REG_SET4(aReg, aPin, aVal)  {\
	U32 regval = ((aVal) << ((aPin)*4));	\
	U32 mask = ~((U32)0xF << ((aPin)*4));	\
	if ((aReg & ~mask) != regval) {\
 		aReg &= (mask); \
		aReg |= regval; \
		}\
	}


#define REG_SET8(aReg, aPin, aVal)  {\
	U32 regval = ((aVal) << ((aPin)*8));	\
	U32 mask = ~((U32)0xFF << ((aPin)*8));	\
	if ((aReg & ~mask) != regval) {\
		aReg &= (mask); \
		aReg |= regval; \
		}\
}


/*********************************************************** CLOCK *************************/
/** \defgroup cg_cpu_clk Clock control
	Define the API to access the CPU clock configuration. This API is used to enable/disable generated clocks
	to required hardware units like SPI controller, UART controller etc.
*/




/**
    \ingroup cg_cpu_clk
    Create access to 'clock' registers.

	\if OS_WCE
		in windows CE, usually use VirtualAlloc/VirtualCopy to generate an access to the DMA registers.
	\endif

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuClockCreate(void);


/**
    \ingroup cg_cpu_clk
    Destroy access to 'clock' registers

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuClockDestroy(void);


/**
    \ingroup cg_cpu_clk
    Enable the clock to specific CPU unit
	Logical unit codes are defined per CPU. each unit may have sub-units.

	\attention Requires porting

	\param[in]	aUnit		Logical unit code to enable

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuClockEnable(U32 aUnit);


/**
    \ingroup cg_cpu_clk
    Disable the clock to specific CPU unit.
	Logical unit codes are defined per CPU. Each unit may have sub-units.

	\attention Requires porting

	\param[in]	aUnit		Logical unit code to disable

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuClockDisable(U32 aUnit);


/*********************************************************** DMA ***************************/

/**
	\section DMA_CH_CODE DMA channel code
	code = DMA Controller code + channel number

	example: CG_CPU_DMA_CONTROLLER_0 + 4 ==> DMA controller 1, channel 4
*/
#define CG_CPU_DMA_CONTROLLER_0			(0x00000000)
#define CG_CPU_DMA_CONTROLLER_1			(0x00010000)
#define CG_CPU_DMA_CONTROLLER_2			(0x00020000)
#define CG_CPU_DMA_CONTROLLER_3			(0x00030000)
#define CG_CPU_DMA_CONTROLLER_4			(0x00040000)

#define CG_DMA_CONTROLLER(ch)			((ch & 0xFFFF0000) >> 16)
#define CG_DMA_CHANNEL(ch)				(ch & 0x0000FFFF)
#define CG_DMA_IS_VALID(ch)
/** DMA multi block mode
	Defines the method of executing consecutive DMA transfers.
	There are 3 different possible methods :
	LLI - means that the DMA needs to be configured only once, and no extra tx commands should be issued
	PENDING - means that after receiving the first GPS data interrupt, the DMA should issue the next task, so it is prepared for it.
	NONE - means that the DMA should prepared for the next task, only after receiving the DMA interrupt for tx-done.
*/

#define CG_DRIVER_DMA_MULTI_BLOCK_MODE_NONE		0
#define CG_DRIVER_DMA_MULTI_BLOCK_MODE_PENDING	1
#define CG_DRIVER_DMA_MULTI_BLOCK_MODE_LLI		2

typedef struct {
    U32 address;
	U32 length;
} TCgCpuDmaTask;


/** \defgroup cg_cpu_dma DMA control
	Define the API to access Direct Memory Access unit for specific CPU.
*/

/**
    \ingroup cg_cpu_dma
    Create access to DMA controller.
	Depending on OS, CPU and platform implementation, this might require remapping physical
	address space into virtual address space. However, on some systems the virtual address is
	statically allocated, so this function should be left empty (for example, for Linux driver).

	In the CellGuide provided implementation, if \ref CG_DRIVER_DMA_BASE_VA is defined in platform.h, then
	a static virtual address should be used, otherwise, the physical address (CG_DRIVER_DMA_BASE_PA)
	is mapped to a virtual address.

	The function assigns gpDMA with the DMA controller virtual address.

	\if OS_WCE
		\note in windows CE, usually use VirtualAlloc/VirtualCopy to generate an access to the DMA registers.
		if a consist storage is needed, it should be global.
	\endif

	\attention DMA channel number is a 'logical' number, a combination of the controller and channel, and
		should be parsed before used directly with hardware registers
	\note On some implementations, the CGsnap read port address is required for proper DMA setup,
		and therefore it is passed as an argument. If it is not required, then it should be ignored,
		but not removed from the function prototype.

	\param[in]  dmaChannel : The DMA channel to create
	\param[in] ipDataSourceAddress : The address of the IP, from which the DMA should read data

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuDmaCreate(U32 dmaChannel, U32 ipDataSourceAddress);


/**
    \ingroup cg_cpu_dma
    Destroy DMA access

    \param[in]	aDmaChannel			Logical channel number
    \param[in]	aIpDataSourceAddress			Address

    \return System wide return code (see CgReturnCodes.h)
	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaDestroy(U32 aDmaChannel, U32 aIpDataSourceAddress);


/**
    \ingroup cg_cpu_dma
    Check if the DMA channel is ready for next operation.

    \param[in]	aDmaChannel			Logical channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
	\retval ECgBadArgument	On invalid channel
	\retval ECgBusy			if not ready
*/
TCgReturnCode CgCpuDmaIsReady(U32 aDmaChannel);



/**
    \ingroup cg_cpu_dma
    Get the DMA channel current bytes count

    \param[in]	aDmaChannel			Channel number
    \param[out]	apCount				Bytes count

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
	\retval ECgBadArgument	On invalid channel
	\retval ECgBusy			When channel is not ready
*/
TCgReturnCode CgCpuDmaCurCount(U32 aDmaChannel, U32 *apCount);

/**
    \ingroup cg_cpu_dma
    Get the DMA channel requested bytes count

    \param[in]	aDmaChannel			Logical channel number
    \param[out]	apCount				Bytes count

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
	\retval ECgBadArgument	On invalid channel
	\retval ECgBusy			When channel is not ready
*/
TCgReturnCode CgCpuDmaRequestedCount(U32 aDmaChannel, U32 *apCount);

/**
    \ingroup cg_cpu_dma
    Wait for the DMA channel to finish its current transfer

    \param[in]	aDmaChannel			Logical channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
	\retval ECgBadArgument	On invalid channel
	\retval ECgBusy			When channel is not ready
*/TCgReturnCode CgCpuDmaWaitFinish(U32 aDmaChannel);


/**
    \ingroup cg_cpu_dma
    Stop DMA channel

    \param[in]	aDmaChannel			Logical channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
	\retval ECgBadArgument	On invalid channel
*/
TCgReturnCode CgCpuDmaStop(U32 aDmaChannel);


/**
    \ingroup cg_cpu_dma
    Start the specified DMA channel.
	The channel initialization (setting source, destination, etc) is done in another function.

    \param[in]	aDmaChannelCode		DMA channel code.
									\ref DMA_CH_CODE

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
	\retval ECgBadArgument	On invalid channel
*/
TCgReturnCode CgCpuDmaStart(U32 aDmaChannelCode);


/**
    \ingroup cg_cpu_dma
    Configure DMA to write data to SPI

    \param pSource			Source memory address to write from
    \param aLength			Number of bytes to write
    \param aWriteDmaChannel DMA channel to use
    \param aDeviceChannel	SPI channel to use

	\note The function checks that the DMA channel selected is the right one for the SPI channel.

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaSetupToDevice(U32 pSource, U32 aLength, U32 aWriteDmaChannel, int aDeviceChannel);




/**
    \ingroup cg_cpu_dma
    Configure DMA to read data from SPI

    \param pTarget			Source memory address to write to
    \param aLength			Number of bytes to transfer
    \param aBlockLength		Number of bytes in a block
    \param aReadDmaChannel	DMA channel to use for read
    \param aWriteDmaChannel DMA channel to use for write
    \param aDeviceChannel	SPI channel to use

	\note if SPI has 'auto output' generator, there is no need to use the 'write' channel.

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaSetupFromDevice(U32 pTarget, U32 aLength, U32 aBlockLength, U32 aReadDmaChannel, U32 aWriteDmaChannel, int aDeviceChannel);



/**
    \ingroup cg_cpu_dma
    Configure DMA to read data from CGsnap

    \param apDmaTask		DMA tasks list
    \param aDmaTaskCount	Total number of DMA tasks
    \param aDmaChannelCode	DMA channel to use for read (\ref DMA_CH_CODE)
    \param aDeviceCode		Device code

	\note if SPI has 'auto output' generator, there is no need to use the 'write' channel.

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaSetupFromDeviceFirstTask(TCgCpuDmaTask *apDmaTask, U32 aDmaTaskCount, U32 aDmaChannelCode, U32 aDeviceCode);


/**
    \ingroup cg_cpu_dma
    Configure DMA to read data from SPI

    \param apDmaTask		DMA tasks list
    \param aDmaTaskCount	Total number of DMA tasks
    \param aActiveTaskIndex	Active DMA task number
    \param aDmaChannel		DMA channel to use for read
    \param aDeviceCode		Device code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaSetupFromDeviceNextTask(TCgCpuDmaTask *apDmaTask, U32 aDmaTaskCount, U32 aActiveTaskIndex, U32 aDmaChannel, U32 aDeviceCode);

/**
    \ingroup cg_cpu_dma
    Configure DMA to read data from GPS device.
	This handles the initial transfer setup, and is called by the application prior to CgCpuDmaStart.
    Configure DMA to read data from CGsnap.
	- DMA source
		- Device is CGsnap IP
		- Address is 'aDeviceAddress'
		- Non increment
		- Use handshake
	- Burst transfer (burst = 16 bytes)
	- Word (32bit) transfer mode
	- Enable interrupt at end of transfer

	If the FMA supports LLI (Linked List Interface), then a proper DMA tasks list should be generated from the generic
	tasks list.

    \param apDmaTask		DMA tasks list
	Since the destination buffer maybe smaller than the transfer size, it is required to split the DMA
	transfer into several DMA tasks. The destination of each task will take into consideration the buffer size.
	The application sets the buffer size on init.
	The application decides on transfer size, transfer task size and task destination address, for every transfer.

	For example, if the transfer size is 1500KB, and the buffer size is 500KB, and the task size is 100KB,
	then there will be 15 transfer tasks, each of 100KB, and the destination addresses in each task will be 0, 100KB, 200KB,
	300KB, 400KB, 0KB, 100KB, etc.

	In case the DMAC supports LLI, all required tasks shall be set in the DMAC.
	Otherwise, the tasks are stored temporarily, and will be used by CgCpuDmaSetupFromGpsNextTask.

    \param apDmaTask		DMA tasks list (pairs of addresses and lengths)
    \param aDmaTaskCount	Total number of DMA tasks
    \param aDmaChannel		DMA channel to use for read (\ref DMA_CH_CODE)
    \param aDeviceAddress	Device address

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaSetupFromGps(TCgCpuDmaTask *apDmaTask, U32 aDmaTaskCount, U32 aDmaChannel, U32 aDeviceAddress);




/**
    \ingroup cg_cpu_dma
    Configure DMA to read next DMA task from GPS device.
	This function is only required when the DMA controller does not support LLI
	This function is only required when the DMA controller does not support LLI
	This sets up the DMA to perform the 2nd and above transfer tasks, of the same transfer.

	In case the DMAC doesn't support LLI, this function is called by the the DMA interrupt handler, to set-up the
	next transfer task.
	Otherwise, this function is never called.


    \param apDmaTask		DMA tasks list (the same
    \param aDmaTaskCount	Total number of DMA tasks
    \param aActiveTaskIndex	Active DMA task number
    \param aDmaChannel		DMA channel to use for read
    \param aDeviceAddress	Device address

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuDmaSetupFromGpsNextTask(TCgCpuDmaTask *apDmaTask, U32 aDmaTaskCount, U32 aActiveTaskIndex, U32 aDmaChannel, U32 aDeviceAddress);

/*********************************************************** GPIO **************************/
/** \defgroup cg_cpu_gpio GPIO control
	Configure, set and read GPIO


	CellGuide GPIO code is an abstract number, combination of group constant and pin number.
	The number is translated to specific CPU values by specific implementation.
*/


/** GPIO pin configuration modes */
typedef enum {
	ECG_CPU_GPIO_INPUT,				/**< GPIO to act as input */
	ECG_CPU_GPIO_OUTPUT,			/**< GPIO to act as output */
	ECG_CPU_GPIO_SPECIAL,			/**< GPIO to act as special function */
	ECG_CPU_GPIO_FALLING_INT,		/**< GPIO to act as falling edge interrupt*/
	ECG_CPU_GPIO_SPECIAL2,			/**< GPIO to act as special function */
	ECG_CPU_GPIO_SPECIAL3,			/**< GPIO to act as special function */
	ECG_CPU_GPIO_MODE___
} TCgCpuGpioMode;

/** GPIO pin configuration modes */
typedef enum {
	ECG_CPU_GPIO_PULL_NONE,			/**< GPIO with no pull */
	ECG_CPU_GPIO_PULL_DOWN,			/**< GPIO with pull down */
	ECG_CPU_GPIO_PULL_UP,			/**< GPIO with pull up */
	ECG_CPU_GPIO_PULL___
} TCgCpuGpioPull;

/**
    \ingroup cg_cpu_gpio
    Create access to GPIO controller.
	Depending on OS, CPU and platform implementation, this might require remapping physical
	address space into virtual address space. However, on some systems the virtual address is
	statically allocated, so this function should be left empty (for example, for Linux driver).

	In the 'standard' implementation, if CG_DRIVER_GPIO_BASE_VA is defined in platform.h, then
	a static virtual address should be used, otherwise, the physical address (CG_DRIVER_GPIO_BASE_PA)
	is mapped to a virtual address.

	The function assigns gpGpio with the GPIO controller virtual address.

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuGpioCreate(void);


/**
    \ingroup cg_cpu_gpio
    Destroy access to GPIO controller.
	The implementation depends on the method used for allocation (see CgCpuGpioCreate)

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioDestroy(void);


/**
    \ingroup cg_cpu_gpio
    Set a specific GPIO to 'output' mode and set it to '1' value

    \param[in]	aPinCode			GPIO pin code

	\note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioSet(U32 aPinCode);


/**
    \ingroup cg_cpu_gpio
    Set a specific GPIO to 'output' mode and set it to '0' value

	\note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

	\param[in]	aPinCode			GPIO pin code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioReset(U32 aPinCode);


/**
    \ingroup cg_cpu_gpio
    Set a specific GPIO to 'input' mode, read the value and return it

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

    \param[in]	aPinCode			GPIO pin code
    \param[out] aPinValue			current value

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioGet(U32 aPinCode, int *aPinValue);


/**
    \ingroup cg_cpu_gpio
    Return IRQ code for the specific GPIO pin

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

    \param[in]	aPin				GPIO pin code
    \param[out]	aCode				IRQ code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioIntCode(U32 aPin, U32 *aCode);


/**
    \ingroup cg_cpu_gpio
    Set a specific GPIO mode

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

    \param[in]	aPinCode			GPIO pin code
    \param[in]	aPinMode			Requested mode

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioModeSet(U32 aPinCode, TCgCpuGpioMode aPinMode);

/**
    \ingroup cg_cpu_gpio

    Set a specific GPIO strength

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

	\attention Not all CPUs support internal drive level. If the CPU does not support this feature, this function should be left empty.

    \param[in]	aPinCode			GPIO pin code
    \param[in]	aStrength			Requested strength

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioStrength(U32 aPinCode, U32 aStrength);

/**
    \ingroup cg_cpu_gpio
    Set a specific GPIO pull up/down

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

	\attention Not all CPUs support internal pull, if the CPU does not support this feature, this function should be left empty.

    \param[in]	aPinCode			GPIO pin code
    \param[in]	aPull				Requested

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioPull(U32 aPinCode, TCgCpuGpioPull aPull);

/**
    \ingroup cg_cpu_gpio
    Set a specific GPIO direction

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

    \param[in]	aPinCode			GPIO pin code
    \param[in]	aPinMode			Requested mode

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioDirection(U32 aPinCode, TCgCpuGpioMode aPinMode);


/**
    \ingroup cg_cpu_gpio
    Clear interrupt bit

    \note Pin code is CellGuide internal code for each GPIO, which is a combination of the
	GPIO group offset and the number inside the group.

    \param[in]	aPinCode			GPIO pin code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuGpioIntClear(U32 aPinCode);

/*********************************************************** SPI ***************************/
/** \defgroup cg_cpu_spi SPI control
	SPI programming
*/




/**
    \ingroup cg_cpu_spi
    Delay for specific time

    \param aCount

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuDelay(U32 aCount);


typedef enum {
	ECG_CPU_SPI_INPUT_DMA,
	ECG_CPU_SPI_INPUT_POLLING,
	ECG_CPU_SPI_OUTPUT_DMA,
	ECG_CPU_SPI_OUTPUT_POLLING,
	ECG_CPU_SPI_INIT,
	ECG_CPU_SPI_SLAVE_INPUT_DMA,
	ECG_CPU_SPI_SLEEP,
	ECG_CPU_SPI_MODE___
} TCgCpuSpiMode;



/**
    \ingroup cg_cpu_spi
    Create access to SPI

	\param[in]	aChannelCode		SPI Channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiCreate(U32 aChannelCode);


/**
    \ingroup cg_cpu_spi
    Destroy access to SPI

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiDestroy(void);


/**
    \ingroup cg_cpu_spi
    Enable SPI controller for specific channel

	\param[in]	aDeviceChannel		SPI Channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiEnable(int aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Disable SPI controller for specific channel

	\param[in]	aDeviceChannel		SPI Channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiDisable(int aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Check if SPI channel number is valid for the specific system

    \param[in]	aDeviceChannel		SPI Channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiIsValid(int aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Setup SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[in]	aPrescale			SPI clock divider
	\param[in]	aMode				SPI mode
	\param[in]	aLengthBytes		Number of bytes

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiConfig(U32 aDeviceChannel, U32 aPrescale, TCgCpuSpiMode aMode, U32 aLengthBytes);

/**
    \ingroup cg_cpu_spi
    Setup SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[in]	aLengthBytes		Number of bytes

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiSlaveConfig(U32 aDeviceChannel, U32 aLengthBytes);

/**
    \ingroup cg_cpu_spi
    Setup SPI channel GPIO

	\param[in]	aDeviceChannel			SPI channel number
	\param[in]	aPrescale			SPI clock divider
	\param[in]	aMode				SPI mode

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiSetup(U32 aDeviceChannel, U32 aPrescale, TCgCpuSpiMode aMode);

/**
    \ingroup cg_cpu_spi
    Return number of words waiting in receive FIFO

	\param[in]	aDeviceChannel		SPI channel number
	\param[out]	pCount				number of words (word width is according to config)

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiReadCount(U32 aDeviceChannel, U32 *pCount);


/**
    \ingroup cg_cpu_spi
    Loop until SPI write is finished.

	\param[in]	aDeviceChannel			SPI channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiTxFinishWait(U32 aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Rx FIFO is empty

	\param[in]	aDeviceChannel			SPI channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
BOOL CgCpuSpiRxFifoEmpty(U32 aDeviceChannel);


/**
    \ingroup cg_cpu_spi
    Clear SPI receive FIFO

	\param[in]	aDeviceChannel			SPI channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiClearFifo(U32 aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Loop until SPI read is finished.

	\param[in]	aDeviceChannel		SPI channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiRxFinishWait(U32 aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Read one byte from SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[out]	aByte				Data read from SPI

    \return Value read from SPI
*/
TCgReturnCode CgCpuSpiReadByte(U32 aDeviceChannel, volatile unsigned char *aByte);

/**
    \ingroup cg_cpu_spi
    Read one Word from SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[out]	aValue				Data read from SPI

    \return Value read from SPI
*/
TCgReturnCode CgCpuSpiReadU16(U32 aDeviceChannel, volatile U16 *aValue);

/**
    \ingroup cg_cpu_spi
    Read one DWord from SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[out]	aValue				Data read from SPI

    \return Value read from SPI
*/
TCgReturnCode CgCpuSpiReadU32(U32 aDeviceChannel, volatile U32 *aValue);

/**
    \ingroup cg_cpu_spi
    Read one byte from SPI channel (read 2nd FIFO byte)

	\param[in]	aDeviceChannel		SPI channel number
	\param[out]	aByte				Data read from SPI

    \return Value read from SPI
*/
TCgReturnCode CgCpuSpiReadByte2(U32 aDeviceChannel, volatile unsigned char *aByte);


/**
    \ingroup cg_cpu_spi
    Write one byte to SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[in]	aValue				Value to write

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiWriteByte(U32 aDeviceChannel, unsigned char aValue);

/**
    \ingroup cg_cpu_spi
    Write one Word to SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[in]	aValue				Value to write

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiWriteU16(U32 aDeviceChannel, U16 aValue);

/**
    \ingroup cg_cpu_spi
    Write one DWord to SPI channel

	\param[in]	aDeviceChannel		SPI channel number
	\param[in]	aValue				Value to write

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiWriteU32(U32 aDeviceChannel, U32 aValue);


/**
    \ingroup cg_cpu_spi
    Return the GPIO code for selected SPI channel MOSI

    \param[in]	aDeviceChannel		channel number
    \param[out] aGpioCode			gpio code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiMosi(U32 aDeviceChannel, U32 *aGpioCode);


/**
    \ingroup cg_cpu_spi
    Return the GPIO number for selected SPI channel MISO

    \param[in]	aDeviceChannel		channel number
    \param[out] aGpio				gpio code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiMiso(U32 aDeviceChannel, U32 *aGpio);


/**
    \ingroup cg_cpu_spi
    Return the GPIO number for selected SPI channel CLK

    \param[in]	aDeviceChannel		channel number
    \param[out] aGpio				gpio code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiClk(U32 aDeviceChannel, U32 *aGpio);


/**
    \ingroup cg_cpu_spi
    Return the GPIO number for selected SPI channel CS (Chip Select)

    \param[in]	aDeviceChannel			channel number
    \param[out] aGpio				gpio code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiCs(U32 aDeviceChannel, U32 *aGpio);


/**
    \ingroup cg_cpu_spi
    Start DMA transaction

	\param[in]	aDeviceChannel		SPI Channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiDmaStart(int aDeviceChannel);

/**
    \ingroup cg_cpu_spi
    Stop DMA transaction

	\param[in]	aDeviceChannel		SPI Channel number

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuSpiDmaStop(int aDeviceChannel);


/*********************************************************** INTR **************************/
/** \defgroup cg_cpu_interrupt Interrupt control
	Access to interrupt controller
*/

/**
    \ingroup cg_cpu_interrupt
    Create access to INTC controller.
	Depending on OS, CPU and platform implementation, this might require remapping physical
	address space into virtual address space. However, on some systems the virtual address is
	statically allocated, so this function should be left empty (for example, for Linux driver).

	In the 'standard' implementation, if CG_DRIVER_INT_BASE_VA is defined in platform.h, then
	a static virtual address should be used, otherwise,  the physical address (CG_DRIVER_INT_BASE_PA)
	is mapped to a virtual address.

	The function assigns pINT with the INT controller virtual address.

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuIntrCreate(void);

/**
    \ingroup cg_cpu_interrupt
    Destroy access to INTC

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuIntrDestroy(void);


/**
    \ingroup cg_cpu_interrupt
	Register IRQ interrupt as OS interrupt

    \param[in]			aIRQ				requested IRQ interrupt code
	\param[in]			apOSIntCode			requested software interrupt code
    \param[out]			apOSIntCode			registered OS interrupt code

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuIntrRequestOsInterrupt(U32 aIRQ, U32 *apOSIntCode);
/**
    \ingroup cg_cpu_interrupt
    Initialize interrupt

    \param[in]		aRequestCode	requested interrupt code (os specific)
    \param[in]		apEvent			event to tie with interrupt

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuIntrAttachEvent(U32 aRequestCode, void *apEvent);

/**
    \ingroup cg_cpu_interrupt
    Disable registered interrupt

    \param[in]	aInterruptCode	interrupt code (os specific)

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuIntrDisable(U32 aInterruptCode);


/**
    \ingroup cg_cpu_interrupt
    Signals to the kernel that interrupt processing has been completed

    \param[in]	aInterruptCode	interrupt code (os specific)

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/

TCgReturnCode CgCpuIntrDone(U32 aInterruptCode);


/** Interrupt Service Thread information structure */
typedef struct {
    void *hThread;					/**< Handle to thread object */
    U32 intCode;					/**< Interrupt code */
    void *event;					/**< Handle to Interrupt event */
    S32 priority;					/**< Thread priority */
} TCgCpuIST;


/**
    \ingroup cg_cpu_interrupt
	If apSharedISRInfo == NULL	===>		Create and start interrupt service thread
	Else						===>		Initialize an event, and connect it to the EXISTING ISR

    \param[in]	apIstInfo				Thread info structure
    \param[in]	aFunc					Main function for the thread
    \param[in]	aPriority				Requested thread priority
    \param[in]	aIntCode				Required interrupt code
    \param[in]	aIrqCode				Required IRQ code (may be 0)
    \param[in]	aParam					IST data structure pointer
	\param[in]	aEventName				Event name to use for interrupt/IST communication
										only need to supply name if the event is created by BSP.
										for systems where the event is internally tied to specific GPI interrupt,
										NULL can be set here
	\param[in] apSharedISRInfo			if the interrupt is shared, this will contain the info on
										the shared interrupt
	\param[in] apSharedISRLibrary		if the interrupt is shared, this will indicate what library
										holds the shared interrupt handler
	\param[in] apSharedISR				if the interrupt is shared, this will indicate the name of the
										interrupt service routine
	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuISTStart(TCgCpuIST *apIstInfo, void *aFunc, S32 aPriority, U32 aIntCode, U32 aIrqCode,
							void *aParam, const char *aEventName, void * apSharedISRInfo,
							const void * apSharedISRLibrary, const void * apSharedISR);


/**
    \ingroup cg_cpu_interrupt
    Stop interrupt service thread

    \param[in]	apIstInfo	Thread info structure

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuISTStop(TCgCpuIST *apIstInfo);



/*********************************************************** RTC ***************************/
/** \defgroup cg_cpu_rtc RTC control
	Access to RTC
*/

/**
    \ingroup cg_cpu_rtc
    Create access to RTC controller by mapping it to virtual memory.

	\note On some operating systems (Linux) and platforms, this step is static, i.e.,
	the virtual address is pre defined.
	CellGuide standard implementation of this function uses CG_DRIVER_RTC_BASE_VA
	and CG_DRIVER_RTC_BASE_PA defines to select the proper method.

	\if OS_WCE
		in windows CE, usually use VirtualAlloc/VirtualCopy to generate an access to the DMA registers.
		if a consist storage is needed, it should be global.
	\endif

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuRtcCreate(void);



/**
    \ingroup cg_cpu_rtc
    Destroy access to RTC controller by un-mapping virtual memory.

	\note CellGuide standard implementation of this function uses CG_DRIVER_RTC_BASE_VA
	and CG_DRIVER_RTC_BASE_PA defines to select the proper method.

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuRtcDestroy(void);


/**
    \ingroup cg_cpu_rtc
    RTC time structure
*/
typedef struct  {
	U32 year;		/**< Year (1970-2100) */
	U32 month;		/**< Month in year (0-11) */
	U32 day;		/**< Day in month (0-30) */
	U32 hour;		/**< Hour in day (0-23) */
	U32 minute;		/**< Minute in Hour (0-59) */
	U32 second;		/**< Second in minute (0-59) */
	U32 dayInWeek;	/**< Day in week (0-6) */
	U32 tick;		/**< internal tick counter (units are CPU specific) */
} TCgCpuRtcTime;

/**
    \ingroup cg_cpu_rtc

    Read current time from RTC unit

	\\attention Implementation is required

    \param[out]	apTime			Pointer to time structure

	\return System wide return code (see CgReturnCodes.h)

	\retval ECgOk			On success
*/
TCgReturnCode CgCpuRtcRead(TCgCpuRtcTime *apTime);

/*********************************************************** RF control *************************/

/** \defgroup cg_cpu_rf RF functions*/

/**
	\ingroup cg_cpu_rf

	Read one byte from RF control via specific protocol
    \param[out]	aValue			Pointer to received byte

	\return system error code
*/
TCgReturnCode CgCpuRFControlReadbyte(volatile unsigned char *aValue);

/**
	\ingroup cg_cpu_rf

	Write one byte to RF control via specific protocol
    \param[in]	aValue			Byte to send

	\return system error code
*/
TCgReturnCode CgCpuRFControlWriteByte(unsigned char aValue);

/*********************************************************** Miscellanies  *************************/
/** \defgroup cg_cpu_miscellanies Miscellanies functions*/

/**
    \ingroup cg_cpu_miscellanies
    Get last error code for this unit

	\return system error code
*/
U32 CgCpuLastErrorCode(void);



/**
    \ingroup cg_cpu_miscellanies
    Clear CPU cache

    \param[in]	pSource			Source memory address
    \param[in]	aDmaChannel		DMA channel


	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuClearCache(U32 pSource, U32 aDmaChannel);



/**
    \ingroup cg_cpu_miscellanies
    Set bits in bit-field register

    \param[in]	aReg			pointer to register
    \param[in]	aBitFieldDef	bit field definition
    \param[in]	aValue			value to set

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgSetRegisterBits(volatile U32 *aReg, TCgBitField aBitFieldDef, U32 aValue);

U32 CgGetU32Mask(U32 bitsInU32);


/**
    \ingroup cg_cpu_miscellanies
    Get bits from bit-field register

    \param[in]	aReg			register
    \param[in]	aBitFieldDef	bit field definition
    \param[in]	apValue			pointer to returned value

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgGetRegisterBits(volatile U32 aReg, TCgBitField aBitFieldDef, U32 *apValue);

/**
    \ingroup cg_cpu_miscellanies
    Global virtual memory allocation.
	This function is used on some platforms, where a global initialization of virtual memory is required.
	It is called as one of the first initialization steps in CgxDriverConstruct.

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuAllocateVa(void);

/**
    Reset a CPU sub-unit
    \param[in]	aUnitIndex		unit id (see specific CPU documentation)
	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuReset(U32 aUnitIndex);

/**
	Set the CGsnap IP Master reset to 'reset' state (device is not active)

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuIPMasterResetOn(void);

/**
	Set the CGsnap IP Master reset to 'clear' state (device is active)

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgCpuIPMasterResetClear(void);

/**
	Get CPU revision code.
	\note Used mainly for debug.

	\param[out]	apRevisionCode	CPU revision code

	\return CPU revision information as null terminated text.
*/
TCgReturnCode CgCpuRevision(char *apRevisionCode);


/**
	byte and word swap

	\param[in]	aSource			Pointer to source buffer
	\param[out]	aDest			Pointer to destination buffer
	\param[in]	aSizeInBytes	Size of memory, in bytes
	\param[in]	aFrom			Source byte order
	\param[in]	aTo				Destination byte order

	\return System wide return code (see CgReturnCodes.h)
	\retval ECgOk On success
*/
TCgReturnCode CgSwapBytesInBlock(void * aSource,void * aDest, long aSizeInBytes, TCgByteOrder aFrom, TCgByteOrder aTo);


#if defined(CGTEST) || defined(DEBUG)
	#define TRACE_ON
#endif
#define TRACE_ON

/**
	Debug facility - to show error messages
*/
#ifdef TRACE_ON
	TCgReturnCode CgxCpuMsg(const char *aFuncName, const char *aFileName, U32 aLineNum, char *aFormat, ...);
	TCgReturnCode CgxCpuPrint(char *aText);
	#define DBG_FUNC_NAME(name) const char *__funcname__ = name;
	#define DBGMSG(format) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format);
	#define DBGMSG1(format,a) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a);
	#define DBGMSG2(format,a,b) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b);
	#define DBGMSG3(format,a,b,c) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c);
	#define DBGMSG4(format,a,b,c,d) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c,d);
	#define DBGMSG5(format,a,b,c,d,e) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c,d,e);
	#define DBGMSG6(format,a,b,c,d,e,f) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c,d,e,f);
	#define DBGMSG7(format,a,b,c,d,e,f,g) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c,d,e,f,g);
	#define DBGMSG8(format,a,b,c,d,e,f,g,h) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c,d,e,f,g,h);
	#define DBGMSG9(format,a,b,c,d,e,f,g,h,i) CgxCpuMsg(__funcname__, __FILE__, __LINE__, format, a,b,c,d,e,f,g,h,i);
	#define DBGDONE {DBGMSG("Done");}
	#define DBGSTAT(title,s) {DbgStat##s(title);}

#else
	#define DBG_FUNC_NAME(name)
	#define DBGMSG(format) do {} while(0)
	#define DBGMSG1(format,a) do {} while(0)
	#define DBGMSG2(format,a,b) do {} while(0)
	#define DBGMSG3(format,a,b,c) do {} while(0)
	#define DBGMSG4(format,a,b,c,d) do {} while(0)
	#define DBGMSG5(format,a,b,c,d,e) do {} while(0)
	#define DBGMSG6(format,a,b,c,d,e,f) do {} while(0)
	#define DBGMSG7(format,a,b,c,d,e,f,g) do {} while(0)
	#define DBGMSG8(format,a,b,c,d,e,f,g,h) do {} while(0)
	#define DBGMSG9(format,a,b,c,d,e,f,g,h,i) do {} while(0)
	#define DBGDONE do {} while(0)
	#define DBGSTAT(title,s) do {} while(0)
#endif

#define CgSwapBytesU32( aSource )\
	(((0xFF00FF00 & aSource) >> 8) | ((0x00FF00FF & aSource) << 8))


#define CgSwapWordsU32( aSource )\
	(((0xFFFF0000 & aSource) >> 16) | ((0x0000FFFF & aSource) << 16))


#define CgSwapBytesAndWordsU32( aSource )\
	( CgSwapWordsU32(CgSwapBytesU32(aSource)) )

#ifdef TRACE_ON
	#define DBGERR(rc)	if (!OK(rc)) { DBGMSG3("Error : (%ld) [%s:%d]", rc, __FILE__, __LINE__); }
#else
	#define DBGERR(rc)do {} while(0)
#endif


#ifdef TRACE_ON
	void DbgStatSPI(const char *aTitle);
	void DbgStatDMA(const char *aTitle);
	void DumpGpioState(U32 aGpio);
#else
	#define DbgStatSPI(x) do {} while(0)
	#define DbgStatDMA(x) do {} while(0)
	#define DumpGpioState(x) do {} while(0)
#endif


// ===========================================================================
#ifdef	__cplusplus
}
#endif /*__cplusplus*/

#endif