summaryrefslogtreecommitdiff
path: root/bsp/lpc408x/Libraries/Drivers/source/lpc_mci.c
blob: 813d599be29311f765c27bbd68373df9af725044 (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
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
/**********************************************************************
* $Id$         lpc_mci.c            2011-06-02
*//**
* @file        lpc_mci.c
* @brief       Contains all functions support for MCI firmware library
*              on LPC
* @version     2.0
* @date        29. June. 2011
* @author      NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _MCI

#include "LPC407x_8x_177x_8x.h"
#include "lpc_types.h"
#include "lpc_mci.h"
#include "lpc_gpdma.h"
#include "lpc_clkpwr.h"
#include "lpc_pinsel.h"

#define DMA_MCI_SIZE                     BLOCK_LENGTH

#define MCI_DMA_WRITE_CHANNEL            (0)
#define MCI_DMA_READ_CHANNEL             (1)

#define _SHIFT(x)                        (1 << x)
#define _XSHIFT(x, y)                    (x << y)

#define SHIFT_(x)                        (1 >> x)
#define XSHIFT_(x, y)                    (x >> y)

#define MCI_ACMD41_HCS_POS                     (30)

#define MCI_PWRCTRL_BMASK                      (0xC3)

#define MCI_PWRCTRL_OPENDRAIN_POS              (6)
#define MCI_PWRCTRL_OPENDRAIN_NUMBIT           (1)
#define MCI_PWRCTRL_OPENDRAIN_BMASK            (0x01)


volatile uint32_t Mci_Data_Xfer_End = 0;

volatile uint32_t Mci_Data_Xfer_ERR = 0;

volatile uint8_t fifo_plane = 0;

volatile uint32_t CardRCA;

volatile uint8_t CCS;

volatile en_Mci_CardType MCI_CardType;

// Terminal Counter flag, Error Counter flag for Channel 0
uint32_t dmaWrCh_TermianalCnt, dmaWrCh_ErrorCnt;
uint32_t dmaRdCh_TermianalCnt, dmaRdCh_ErrorCnt;


uint32_t MCI_SettingDma(uint8_t* memBuf, uint32_t ChannelNum, uint32_t DMAMode );

int32_t MCI_ReadFifo(uint32_t * dest);
int32_t MCI_WriteFifo(uint32_t * src);

void MCI_TXEnable( void );
void MCI_RXEnable( void );
void MCI_TXDisable( void );
void MCI_RXDisable( void );

void MCI_CmdProcess( void );
void MCI_DataErrorProcess( void );
void MCI_DataErrorProcess( void );
void MCI_DATA_END_InterruptService( void );
void MCI_FIFOInterruptService( void );

int32_t MCI_CheckStatus(uint8_t expect_status);



volatile uint8_t* dataSrcBlock = (uint8_t *) MCI_DMA_SRC_ADDR;
volatile uint8_t* dataDestBlock = (uint8_t *) MCI_DMA_DST_ADDR;

volatile uint32_t txBlockCnt=0, rxBlockCnt=0;

/** @addtogroup MCI_Private_Functions MCI Private Function
 * @ingroup MCI
 * @{
 */

#if MCI_DMA_ENABLED
/*********************************************************************//**
 * @brief        Do setting GPDMA for MCI working
 *
 * @param[in]    DMAMode set for the Type of DMA Transfer. It may be memory
 *                        to peripheral (M2P) or peripheral to memory (P2M)
 *                        in MCI working
 *
 * @param[in]    ChannelNum    which channel is used for current transfer with 
 *                        DMA compent
 *
 * @param[in]    memBuf    point to a UINT8 buffer. In 2 cases of DMAMode
 *                        seperated:
 *                        - M2P: it is the source address that hold the
 *                        expected buffer to transfer
 *                        - P2M: it is the destination address that will store
 *                         data that retrieved from the peripheral (the Card in slot)
 *
 * @return       None
 *
 * @note        This is only required if DMA support is enabled
 **********************************************************************/
uint32_t MCI_SettingDma(uint8_t* memBuf, uint32_t ChannelNum, uint32_t DMAMode )
{
    GPDMA_Channel_CFG_Type GPDMACfg;

    // Transfer size
    GPDMACfg.TransferSize = DMA_MCI_SIZE;
    // Transfer width
    GPDMACfg.TransferWidth = GPDMA_WIDTH_WORD;
    // Transfer type
    GPDMACfg.TransferType = DMAMode;
    // Linker List Item - unused
    GPDMACfg.DMALLI = 0;
    
    /* USB RAM is used for test.
    Please note, Ethernet has its own SRAM, but GPDMA can't access
    that. GPDMA can access USB SRAM and IRAM. Ethernet DMA controller can 
    access both IRAM and Ethernet SRAM. */
    GPDMACfg.ChannelNum = ChannelNum;
    
    if ( DMAMode == GPDMA_TRANSFERTYPE_M2P_DEST_CTRL )
    {
        /* Ch0 set for M2P transfer from mempry to MCI FIFO. */
        // Source memory
        GPDMACfg.SrcMemAddr = (uint32_t)memBuf;
        // Destination memory
        GPDMACfg.DstMemAddr = (uint32_t)LPC_MCI->FIFO;

        // Source connection 
        GPDMACfg.SrcConn = 0;
        // Destination connection 
        GPDMACfg.DstConn = GPDMA_CONN_MCI;
            
    }
    else if ( DMAMode == GPDMA_TRANSFERTYPE_P2M_SRC_CTRL )
    {
        /* Ch0 set for P2M transfer from MCI FIFO to memory. */
        // Source memory
        GPDMACfg.SrcMemAddr = (uint32_t)LPC_MCI->FIFO;
        // Destination memory
        GPDMACfg.DstMemAddr = (uint32_t)memBuf;

        // Source connection 
        GPDMACfg.SrcConn = GPDMA_CONN_MCI;
        // Destination connection
        GPDMACfg.DstConn = 0;
    }
    else
    {
        return ( FALSE );
    }

    // Setup channel with given parameter
    GPDMA_Setup(&GPDMACfg);

    // Enable GPDMA channel 
    GPDMA_ChannelCmd(ChannelNum, ENABLE);

    /* Enable GPDMA interrupt */
    NVIC_EnableIRQ(DMA_IRQn);
    
    return (TRUE);
}


/*********************************************************************//**
 * @brief        GPDMA interrupt handler sub-routine
 *
 * @param        None
 *
 * @return       None
 *
 * @note         This is only executed if DMA support is enabled
 **********************************************************************/
void MCI_DMA_IRQHandler (void)
{
    // check GPDMA interrupt on channel 0
    if (GPDMA_IntGetStatus(GPDMA_STAT_INT, MCI_DMA_WRITE_CHANNEL))
    {
        //check interrupt status on channel 0
        // Check counter terminal status
        if(GPDMA_IntGetStatus(GPDMA_STAT_INTTC, MCI_DMA_WRITE_CHANNEL))
        {
            // Clear terminate counter Interrupt pending
            GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, MCI_DMA_WRITE_CHANNEL);

            dmaWrCh_TermianalCnt++;
        }
        if (GPDMA_IntGetStatus(GPDMA_STAT_INTERR, MCI_DMA_WRITE_CHANNEL))
        {
            // Clear error counter Interrupt pending
            GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, MCI_DMA_WRITE_CHANNEL);

            dmaWrCh_ErrorCnt++;
        }
    }
    else if (GPDMA_IntGetStatus(GPDMA_STAT_INT, MCI_DMA_READ_CHANNEL))
    {
        //check interrupt status on channel 0
        // Check counter terminal status
        if(GPDMA_IntGetStatus(GPDMA_STAT_INTTC, MCI_DMA_READ_CHANNEL))
        {
            // Clear terminate counter Interrupt pending
            GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, MCI_DMA_READ_CHANNEL);

            dmaRdCh_TermianalCnt++;
        }
        if (GPDMA_IntGetStatus(GPDMA_STAT_INTERR, MCI_DMA_READ_CHANNEL))
        {
            // Clear error counter Interrupt pending
            GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, MCI_DMA_READ_CHANNEL);

            dmaRdCh_ErrorCnt++;
        }
    }
}
#endif


/*********************************************************************//**
 * @brief        Read data from FIFO (after a transmission with card) to
 *                a destination buffer
 *
 * @param[in]    *dest The buffer to store the data that read from card
 *
 * @return       MCI_FUNC_OK
 *************************************************************************/
int32_t MCI_ReadFifo(uint32_t * dest) 
{
    uint8_t i;
    uint8_t start, end;

    if(fifo_plane == 0)
    {
        start = 0;
        end = 7;
    }
    else
    {
        start = 8;
        end = 15;
    }
    fifo_plane = (fifo_plane) ? 0:1;

    for (i = start; i <= end; i++) 
    {
        *dest = LPC_MCI->FIFO[i];
        
        dest++;
    }
    
    return MCI_FUNC_OK;
}


/*********************************************************************//**
 * @brief        Write data from a source buffer to FIFO for transmission
 *
 * @param[in]    *src The buffer hold the data need to write to card
 *
 * @return       MCI_FUNC_OK
 *************************************************************************/
int32_t MCI_WriteFifo(uint32_t * src)
{
    uint8_t i;
    uint8_t start, end;

    if(fifo_plane == 0)
    {
        start = 0;
        end = 7;
    }
    else
    {
        start = 8;
        end = 15;
    }
    fifo_plane = (fifo_plane) ? 0:1;

    for (i = start; i <= end; i++) 
    {
        LPC_MCI->FIFO[i] = *src;
        
        src++;
    }
    
    return MCI_FUNC_OK;
}


/*********************************************************************//**
 * @brief        Enable Transmit data interrupt
 *
 * @param        None
 *
 * @return       None
 *************************************************************************/
void MCI_TXEnable( void )
{
#if MCI_DMA_ENABLED
    LPC_MCI->MASK0 |= ((DATA_END_INT_MASK)|(ERR_TX_INT_MASK));    /* Enable TX interrupts only */
#else
    LPC_MCI->MASK0 |= ((FIFO_TX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_TX_INT_MASK));    /* FIFO TX interrupts only */
#endif

    return;
}


/*********************************************************************//**
 * @brief        Disable Transmit data interrupt
 *
 * @param        None
 *
 * @return       None
 *************************************************************************/
void MCI_TXDisable( void )
{
#if MCI_DMA_ENABLED
    LPC_MCI->MASK0 &= ~((DATA_END_INT_MASK)|(ERR_TX_INT_MASK));    /* Enable TX interrupts only */
#else
    LPC_MCI->MASK0 &= ~((FIFO_TX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_TX_INT_MASK));    /* FIFO TX interrupts only */
#endif

    return;
}


/*********************************************************************//**
 * @brief        Enable Receive data interrupt
 *
 * @param        None
 *
 * @return       None
 *************************************************************************/
void MCI_RXEnable( void )
{
#if MCI_DMA_ENABLED
    LPC_MCI->MASK0 |= ((DATA_END_INT_MASK)|(ERR_RX_INT_MASK));    /* Enable RX interrupts only */
#else
    LPC_MCI->MASK0 |= ((FIFO_RX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_RX_INT_MASK));    /* FIFO RX interrupts only */
#endif

    return;
}


/*********************************************************************//**
 * @brief        Disable Receive data interrupt
 *
 * @param        None
 *
 * @return       None
 *************************************************************************/
void MCI_RXDisable( void )
{
#if MCI_DMA_ENABLED
    LPC_MCI->MASK0 &= ~((DATA_END_INT_MASK)|(ERR_RX_INT_MASK));    /* Enable TX interrupts only */
#else
    LPC_MCI->MASK0 &= ~((FIFO_RX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_RX_INT_MASK));    /* FIFO TX interrupts only */
#endif

    return;
}

/*********************************************************************//**
 * @brief      Check if the card is in the given state.
 *@param       expect_status    expected status
 * @details    Continuously get the card status until the card is ready. if its status matches 
 *             with the given state, return with success. Else, return MCI_FUNC_ERR_STATE.
 *             If the card is still not ready, return MCI_FUNC_NOT_READY.
 *
 * @param      None
 *
 * @return     MCI_FUNC_OK if all success
 *************************************************************************/
int32_t MCI_CheckStatus(uint8_t expect_status)
{
    int32_t respValue, retval = MCI_FUNC_FAILED;
    uint32_t retryCnt = 0xFFFF, i;
    while (retryCnt > 0)
    {
        if (MCI_GetCardStatus(&respValue) != MCI_FUNC_OK)
        {
            break;
        }
        else
        {
            /* The only valid state is TRANS per MMC and SD state diagram.
            RCV state may be seen, but, it happens only when TX_ACTIVE or
            RX_ACTIVE occurs before the WRITE_BLOCK and READ_BLOCK cmds are
            being sent, which is not a valid sequence. */
            if(!(respValue & CARD_STATUS_READY_FOR_DATA ))
            {
                retval = MCI_FUNC_NOT_READY;
            }
            else if(CARDSTATEOF(respValue) != expect_status)
            {
              // If card is in prg state, wait until it changes to trans state
              // when "operation complete"
              if(CARDSTATEOF(respValue) != CARD_STATE_PRG)
              {
                return MCI_FUNC_ERR_STATE;
              }    
            }
            else
            {
                return MCI_FUNC_OK;
            }
        }
        retryCnt--;
        for(i = 0; i < 0x20; i++);
        
    }

    return retval;
}


/*********************************************************************//**
 * @brief        Called by MCI interrupt handler to simplify the command
 *                process.
 *
 * @param        None
 *
 * @return       None
 *
 * @note         In card initialization, the commnad interrupts are disabled
 *************************************************************************/
void MCI_CmdProcess( void )
{
    uint32_t MCIStatus;

    MCIStatus = LPC_MCI->STATUS;

    if ( MCIStatus &  MCI_CMD_CRC_FAIL )
    {
        LPC_MCI->CLEAR =  MCI_CMD_CRC_FAIL;
    }

    if ( MCIStatus &  MCI_CMD_TIMEOUT )
    {
        LPC_MCI->CLEAR =  MCI_CMD_TIMEOUT;
    }

    /* Cmd Resp End or Cmd Sent */
    if ( MCIStatus &  MCI_CMD_RESP_END )
    {
        LPC_MCI->CLEAR =  MCI_CMD_RESP_END;
    }

    if ( MCIStatus &  MCI_CMD_SENT )
    {
        LPC_MCI->CLEAR =  MCI_CMD_SENT;
    }

    if ( MCIStatus &  MCI_CMD_ACTIVE )
    {
        LPC_MCI->CLEAR =  MCI_CMD_ACTIVE;
    }

    return;
}


/*********************************************************************//**
 * @brief        Called by MCI interrupt handler to manage error on the bus
 *
 * @param        None
 *
 * @return       None
 *************************************************************************/
void MCI_DataErrorProcess( void )
{
    uint32_t MCIStatus;

    MCIStatus = LPC_MCI->STATUS;

    if ( MCIStatus &  MCI_DATA_CRC_FAIL )
    {
        LPC_MCI->CLEAR = MCI_DATA_CRC_FAIL;
    }

    if ( MCIStatus &  MCI_DATA_TIMEOUT )
    {
        LPC_MCI->CLEAR =  MCI_DATA_TIMEOUT;
    }

    /* Underrun or overrun */
    if ( MCIStatus &  MCI_TX_UNDERRUN )
    {
        LPC_MCI->CLEAR = MCI_TX_UNDERRUN;
    }

    if ( MCIStatus &  MCI_RX_OVERRUN )
    {
        LPC_MCI->CLEAR =  MCI_RX_OVERRUN;
    }

    /* Start bit error on data signal */
    if ( MCIStatus &  MCI_START_BIT_ERR )
    {
        LPC_MCI->CLEAR =  MCI_START_BIT_ERR;
    }

    Mci_Data_Xfer_End = 0;
    Mci_Data_Xfer_ERR = MCIStatus;
    return;
}


/*********************************************************************//**
 * @brief         Called by MCI interrupt handler. This is the last interrupt
 *                manipulates the process of the data-block write and read 
 *                to/with card
 *
 * @details       This service is also used with/without DMA support. It simply
 *                clears the flag messages the in-process data-block transfer 
 *                has been done/ended
 *
 * @param         None
 *
 * @return        None
 *************************************************************************/
void MCI_DATA_END_InterruptService( void )
{
    uint32_t MCIStatus;

    MCIStatus = LPC_MCI->STATUS;
    if ( MCIStatus &  MCI_DATA_END )        /* Data end, and Data block end  */
    {
        LPC_MCI->CLEAR = MCI_DATA_END;

        Mci_Data_Xfer_End = 0;

        Mci_Data_Xfer_ERR = 0;

        MCI_TXDisable();
        
        MCI_RXDisable();

        return;
    }

    if ( MCIStatus &  MCI_DATA_BLK_END )
    {
        LPC_MCI->CLEAR =  MCI_DATA_BLK_END;

        //MCI_TXDisable();

        return;
    }

    /* Tx active  */
    if ( MCIStatus & MCI_TX_ACTIVE )
    {

    }

    /* Rx active  */
    if ( MCIStatus & MCI_RX_ACTIVE )
    {

    }

    return;
}


/*********************************************************************//**
 * @brief        Called by MCI interrupt handler if requiring to using FIFO
 *                for data transferring. It copy data to/from FIFO register
 *                from/to a data buffer.
 *
 * @param        None
 *
 * @return       None
 *
 * @note         This function is done without DMA transfer support
 **********************************************************************/
void MCI_FIFOInterruptService( void )
{
#if !MCI_DMA_ENABLED
    uint32_t MCIStatus;

    MCIStatus = LPC_MCI->STATUS;

    if ( MCIStatus & (FIFO_TX_INT_MASK ) )
    {
        /* empty is multiple of 512 block size */
        if ( MCIStatus & MCI_TX_HALF_EMPTY )
        {
            //There's no data, return
            if(dataSrcBlock == NULL)
                return;

            /* write 8 words to fifo */
            MCI_WriteFifo((uint32_t *)&dataSrcBlock[txBlockCnt]);

            txBlockCnt += 32;
        }

        if (txBlockCnt == BLOCK_LENGTH)    /* block complete */
        {
            dataSrcBlock += BLOCK_LENGTH;

            txBlockCnt = 0;

            /* disable FIFO int until next block write */
            //LPC_MCI->MASK0 &= ~(FIFO_TX_INT_MASK);

            /* wait for SD card to complete sending data i.e MCI_DATA_BLK_END interrupt */
        }
    }
    else if ( MCIStatus & (FIFO_RX_INT_MASK) )
    {
        /* if using RX_HALF_FULL remove one ReadFIFO below */
        if ( MCIStatus & MCI_RX_HALF_FULL )
        {
            //There's no store data, return
            if(dataDestBlock == NULL)
                return;

            /* read 8 words from fifo */
            MCI_ReadFifo((uint32_t *)&dataDestBlock[rxBlockCnt]);

            rxBlockCnt += 32;
        }

        /* block complete */
        if (rxBlockCnt == BLOCK_LENGTH)
        {
            dataDestBlock += BLOCK_LENGTH;

            rxBlockCnt = 0;
        }
    }
#endif

    return;
}

/*********************************************************************//**
 * @brief        MCI_IRQHandler is to manage the reasons that cause the 
 *               interrupt.
 *
 * @details      It controls the data-block writing and reading by access 
 *               the FIFO register.
 *               It handle the state changes on the MCI bus...
 *
 * @param        None
 *
 * @return       None
 **********************************************************************/
void MCI_IRQHandler (void)
{
    uint32_t MCI_Status;

    MCI_Status = LPC_MCI->STATUS;

    /* handle MCI_STATUS interrupt */
    if ( MCI_Status & DATA_ERR_INT_MASK )
    {
        MCI_DataErrorProcess();

        return;
    }

    if ( MCI_Status & DATA_END_INT_MASK )
    {
        MCI_DATA_END_InterruptService();

        return;
    }
    else if ( MCI_Status & FIFO_INT_MASK )
    {
        MCI_FIFOInterruptService();

        return;
    }
    else if ( MCI_Status & CMD_INT_MASK )
    {
        MCI_CmdProcess();

        return;
    }
}


/**
 * @}
 */



/** @addtogroup MCI_Public_Functions
 * @{
 */

/*********************************************************************//**
 * @brief        Set MCI clock rate, during initialization phase < 400K
 *                during data phase < 20Mhz
 *
 * @param[in]    ClockRate Clock rate to be set (in Hz)
 *
 * @return       None
 **********************************************************************/
void MCI_Set_MCIClock( uint32_t ClockRate )
{
    volatile uint32_t i;
    uint32_t ClkValue = 0;
    uint32_t pclk;
    
    pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);

    ClkValue = (pclk + 2*ClockRate - 1) /(2*ClockRate); 
    if(ClkValue)
        ClkValue -= 1;

    LPC_MCI->CLOCK = (LPC_MCI->CLOCK & ~(0xFF)) | (1 << 8)  | ClkValue;

    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK before next write */

    return;
}


/**********************************************************************//**
 * @brief        Set the Width to 1-bit Bus or 4-bit Bus
 *
 * @param[in]    width buswidth expected to set
 *
 * @return       MCI_FUNC_OK in case of success
 *************************************************************************/
int32_t MCI_SetBusWidth( uint32_t width )
{
    volatile uint32_t i;
    uint8_t bus_width = BUS_WIDTH_1BIT;

    if ( width == SD_1_BIT )
    {
        LPC_MCI->CLOCK &=  ~(1 << 11);    /* 1 bit bus */
    }
    else if ( width == SD_4_BIT )
    {
        LPC_MCI->CLOCK |=  (1 << 11);/* 4 bit bus */
        bus_width = BUS_WIDTH_4BITS;
    }

    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
    
    if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
        (MCI_CardType == MCI_SDSC_V2_CARD) ||
        (MCI_CardType == MCI_SDHC_SDXC_CARD)) 
    {
        if ( MCI_Acmd_SendBusWidth( bus_width ) != MCI_FUNC_OK )
        {
            return(MCI_FUNC_FAILED);
        }
    }
    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief        Do initialization the MCI block as set its clock, registers,
 *                setup NVIC for interrupts, configure the pins used for MCI 
 *                function, do initialize the card in slot...
 *
 * @param[in]    powerActiveLevel the power level to activate the card in slot
 *
 * @return       MCI_FUNC_OK in case of success
 ***************************************************************************/
int32_t MCI_Init(uint8_t powerActiveLevel )
{
    volatile uint32_t i;

    MCI_CardType = MCI_CARD_UNKNOWN;

    // Following block of code added to ensure card VCC drops to zero
    // before card is initialized
  
    // Force all MCI control pins to basic I/O mode
    LPC_IOCON->P1_2  &= ~0x1F; /* SD_CLK @ P1.2 */
    LPC_IOCON->P1_3  &= ~0x1F; /* SD_CMD @ P1.3 */
    LPC_IOCON->P1_5  &= ~0x1F; /* SD_PWR @ P1.5 */
    LPC_IOCON->P1_6  &= ~0x1F; /* SD_DAT_0 @ P1.6 */
    LPC_IOCON->P1_7  &= ~0x1F; /* SD_DAT_1 @ P1.7 */
    LPC_IOCON->P1_11 &= ~0x1F; /* SD_DAT_2 @ P1.11 */
    LPC_IOCON->P1_12 &= 0x1F; /* SD_DAT_3 @ P1.12 */

    // Set all MCI pins to outputs
    LPC_GPIO1->DIR |= 0x18EC;
  
    // Force all pins low (except power control pin)
    LPC_GPIO1->CLR = 0x1000;
    LPC_GPIO1->CLR = 0x0800;
    LPC_GPIO1->CLR = 0x0080;
    LPC_GPIO1->CLR = 0x0040;
  
    LPC_GPIO1->SET = 0x0020;
  
    LPC_GPIO1->CLR = 0x0008;
    LPC_GPIO1->CLR = 0x0004;

    // Crude delay of 50ms at 120MHz
    for ( i = 0; i < 0x100000; i++ );

    LPC_SC->PCONP |= ( 1 << 28 );            /* Enable clock to the MCI block */

    if ( LPC_MCI->CLOCK & (1 << 8) )
    {
       LPC_MCI->CLOCK &= ~(1 << 8);
       for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
    }

    if ( LPC_MCI->POWER & 0x02 )
    {
       LPC_MCI->POWER = 0x00;
       for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
    }

    /* Disable all interrupts for now */
    LPC_MCI->MASK0 = 0;

    //SD_CLK
    PINSEL_ConfigPin(1, 2, 2);

    //SD_CMD
    PINSEL_ConfigPin(1, 3, 2);

    //SD_PWR
    PINSEL_ConfigPin(1, 5, 2);

    //SD_DAT_0
    PINSEL_ConfigPin(1, 6, 2);

    //SD_DAT_1
    PINSEL_ConfigPin(1, 7, 2);

    //SD_DAT_2
    PINSEL_ConfigPin(1, 11, 2);

    //SD_DAT_3
    PINSEL_ConfigPin(1, 12, 2);

    // SD_PWR is active high (follows the output of the SD Card interface block).
    if(powerActiveLevel == LOW_LVL)
    {
        LPC_SC->SCS &= ~ 0x08;//Becase on EA board SD_PWR is active low
    }
    else
    {
        LPC_SC->SCS |= 0x08;
    }

    //Setting for timeout problem
    LPC_MCI->DATATMR = 0x1FFFFFFF;

    /*set up clocking default mode, clear any registers as needed */
    LPC_MCI->COMMAND = 0;
    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
    LPC_MCI->DATACTRL = 0;
    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
    LPC_MCI->CLEAR = 0x7FF;        /* clear all pending interrupts */

    LPC_MCI->POWER = 0x02;        /* power up */
    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */

    
    /* delays for the supply output is stable*/
    for ( i = 0; i < 0x80000; i++ );
    
    /* During identification phase, the clock should be less than
    400Khz. Once we pass this phase, the normal clock can be set up
    to 25Mhz on SD card and 20Mhz on MMC card. */
    MCI_Set_MCIClock(MCI_SLOW_RATE );

    LPC_MCI->POWER |= 0x01;        /* bit 1 is set already, from power up to power on */
    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */

    NVIC_EnableIRQ(MCI_IRQn);

    MCI_CardInit();

    /* During the initialization phase, to simplify the process, the CMD related
    interrupts are disabled. The DATA related interrupts are enabled when
    the FIFOs are used and just before WRITE_BLOCK READ_BLOCK cmds are issues, and
    disabled after the data block has been written and read. Please also note,
    before WRITE_BLOCK only TX related data interrupts are enabled, and before
    READ_BLOCK only RX related data interrupts are enabled. */
    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief         Set output in open drain mode or pushpull mode
 *
 * @param[in]    mode the mode going to set
 *
 * @return         None
 ***************************************************************************/
void MCI_SetOutputMode(uint32_t mode)
{
    uint32_t i = 0;
    if(mode == MCI_OUTPUT_MODE_OPENDRAIN)
    {
        /* Set Open Drain output control for MMC */
        LPC_MCI->POWER |= (1 << MCI_PWRCTRL_OPENDRAIN_POS) & MCI_PWRCTRL_BMASK;
    }
    else
    {
        /* Clear Open Drain output control for SD */
        LPC_MCI->POWER &= (~(1 << MCI_PWRCTRL_OPENDRAIN_POS) & MCI_PWRCTRL_BMASK);
    }
    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
}


/************************************************************************//**
 * @brief        The routine is used to send a CMD to the card
 *
 * @param[in]    CmdIndex the command to be sent to cards
 *
 * @param[in]    Argument the argument follows the command
 *
 * @param[in]    ExpectResp the response type for the command. They may be:
 *                - EXPECT_NO_RESP: means no response required
 *                - EXPECT_SHORT_RESP: means a response in a word needed
 *                - EXPECT_LONG_RESP: means a response in 4 words needed
 *
 * @param[in]    AllowTimeout allow timeout the command or not
 *
 * @return       None
 ***************************************************************************/
void MCI_SendCmd(st_Mci_CmdInfo* pCmdIf)
{
    volatile uint32_t i;
    uint32_t CmdData = 0;
    uint32_t CmdStatus;

    uint32_t CmdIndex = pCmdIf->CmdIndex;
    uint32_t Argument = pCmdIf->Argument;
    uint32_t ExpectResp = pCmdIf->ExpectResp;
    uint32_t AllowTimeout = pCmdIf->AllowTimeout;
    
    /* the command engine must be disabled when we modify the argument
    or the peripheral resends */
    while ( (CmdStatus = LPC_MCI->STATUS) & MCI_CMD_ACTIVE )    /* Command in progress. */
    {
        LPC_MCI->COMMAND = 0;
        for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
        LPC_MCI->CLEAR = CmdStatus | MCI_CMD_ACTIVE;
    }


    /*set the command details, the CmdIndex should 0 through 0x3F only */
    CmdData |= (CmdIndex & 0x3F);    /* bit 0 through 5 only */

    if ( ExpectResp == EXPECT_NO_RESP )            /* no response */
    {
        CmdData &= ~((1 << 6) | (1 << 7));        /* Clear long response bit as well */
    }
    else if ( ExpectResp == EXPECT_SHORT_RESP )    /* expect short response */
    {
        CmdData |= (1 << 6);
    }
    else if ( ExpectResp == EXPECT_LONG_RESP )    /* expect long response */
    {
        CmdData |= (1 << 6) | (1 << 7);
    }

    if ( AllowTimeout == ALLOW_CMD_TIMER )            /* allow timeout or not */
    {
        CmdData &= ~ MCI_DISABLE_CMD_TIMER;
    }
    else
    {
        CmdData |= MCI_DISABLE_CMD_TIMER;
    }

    /*send the command*/
    CmdData |= (1 << 10);        /* This bit needs to be set last. */

    // clear status register
    LPC_MCI->CLEAR = 0x7FF;

    LPC_MCI->ARGUMENT = Argument;    /* Set the argument first, finally command */

    LPC_MCI->COMMAND = CmdData;

    for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */

    // Wait until command is processed
    while(!LPC_MCI->STATUS);
    
    // Wait until command sent
    while(LPC_MCI->STATUS & MCI_CMD_ACTIVE);
    
    return;
}


/************************************************************************//**
 * @brief        The routine is to get the reponse from card after commands.
 *               This function is always used in pair of MCI_SendCmd() func
 *
 * @param[in]    ExpectCmdData specify the command of which the data will be
 *                retrieved. This field should be the same with CmdIndex of
 *                MCI_SendCmd() function.
 *
 * @param[in]    ExpectResp the response type for the command. They may be:
 *                - EXPECT_NO_RESP: means no response required
 *                - EXPECT_SHORT_RESP: means a response in a word needed
 *                - EXPECT_LONG_RESP: means a response in 4 words needed
 *
 * @param[out]   CmdResp the buffer stored the data replied from cards
 *
 * @return       MCI_FUNC_OK in case of success
 ***************************************************************************/
int32_t MCI_GetCmdResp(uint32_t ExpectCmdData, uint32_t ExpectResp, uint32_t *CmdResp)
{
    uint32_t CmdRespStatus = 0;
    uint32_t LastCmdIndex;
    uint32_t i = 0;

    if ( ExpectResp == EXPECT_NO_RESP )
    {
        return MCI_FUNC_OK;
    }

    while (1)
    {
        // Get the status of the component
        CmdRespStatus = LPC_MCI->STATUS;

        if ( CmdRespStatus & (MCI_CMD_TIMEOUT) )
        {
            LPC_MCI->CLEAR = CmdRespStatus | MCI_CMD_TIMEOUT;

            LPC_MCI->COMMAND = 0;
            LPC_MCI->ARGUMENT = 0xFFFFFFFF;

            for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
            
            return (CmdRespStatus);
        }

        if (  CmdRespStatus & MCI_CMD_CRC_FAIL )
        {
            LPC_MCI->CLEAR = CmdRespStatus | MCI_CMD_CRC_FAIL;
            LastCmdIndex = LPC_MCI->COMMAND & 0x003F;

            if ( (LastCmdIndex == CMD1_SEND_OP_COND) || (LastCmdIndex == ACMD41_SEND_APP_OP_COND)
                                                    || (LastCmdIndex == CMD12_STOP_TRANSMISSION) )
            {
                LPC_MCI->COMMAND = 0;
                LPC_MCI->ARGUMENT = 0xFFFFFFFF;
                for ( i = 0; i < 0x10; i++ );    /* delay 3MCLK + 2PCLK  */
                break;            /* ignore CRC error if it's a resp for SEND_OP_COND
                                or STOP_TRANSMISSION. */
            }
            else
            {
                return (CmdRespStatus);
            }
        }
        else if (CmdRespStatus & MCI_CMD_RESP_END)
        {
            LPC_MCI->CLEAR = CmdRespStatus | MCI_CMD_RESP_END;
            break;    /* cmd response is received, expecting response */
        }
        
    }

    if ((LPC_MCI->RESP_CMD & 0x3F) != ExpectCmdData)
    {
        /* If the response is not R1, in the response field, the Expected Cmd data
        won't be the same as the CMD data in SendCmd(). Below four cmds have
        R2 or R3 response. We don't need to check if MCI_RESP_CMD is the same
        as the Expected or not. */
        if ((ExpectCmdData != CMD1_SEND_OP_COND) && (ExpectCmdData != ACMD41_SEND_APP_OP_COND)
                    && (ExpectCmdData != CMD2_ALL_SEND_CID) && (ExpectCmdData != CMD9_SEND_CSD))
        {
            CmdRespStatus = INVALID_RESPONSE;    /* Reuse error status */
            return (INVALID_RESPONSE);
        }
    }

    /* Read MCI_RESP0 register assuming it's not long response. */
    if (CmdResp != NULL)
    {
        if (ExpectResp == EXPECT_SHORT_RESP)
        {
            *(CmdResp + 0) = LPC_MCI->RESP0;
        }
        else if (ExpectResp == EXPECT_LONG_RESP)
        {
            *(CmdResp + 0) = LPC_MCI->RESP0;
            *(CmdResp + 1) = LPC_MCI->RESP1;
            *(CmdResp + 2) = LPC_MCI->RESP2;
            *(CmdResp + 3) = LPC_MCI->RESP3;
        }
        }

    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief        The routine is to send command to cards then get back the 
 *               reponses (if required).
 *
 * @param[in]    CmdIndex the command to be sent to cards
 *
 * @param[in]    Argument the argument follows the command
 *
 * @param[in]    ExpectResp the response type for the command. They may be:
 *                - EXPECT_NO_RESP: means no response required
 *                - EXPECT_SHORT_RESP: means a response in a word needed
 *                - EXPECT_LONG_RESP: means a response in 4 words needed
 *
 * @param[out]    CmdResp the buffer stored the data replied from cards
 *
 * @param[in]    AllowTimeout allow timeout the command or not
 *
 * @return       MCI_FUNC_OK in case of success
 ***************************************************************************/
int32_t MCI_CmdResp(st_Mci_CmdInfo* pCmdIf)
{
    int32_t respStatus;
    uint32_t CmdIndex = pCmdIf->CmdIndex;
    uint32_t ExpectResp = pCmdIf->ExpectResp;
    uint32_t *CmdResp = pCmdIf->CmdResp;
    

    MCI_SendCmd(pCmdIf);

    if((CmdResp != NULL) || (ExpectResp != EXPECT_NO_RESP))
    {
        respStatus = MCI_GetCmdResp(CmdIndex, ExpectResp, CmdResp);
    }
    else
    {
        respStatus = MCI_FUNC_BAD_PARAMETERS;
    }

    return respStatus;
}


/************************************************************************//**
 * @brief        To reset the card, the CMD0 is sent and then the card is put
 *               in idle state. This is the very first command to be sent to
 *               initialize either MMC or SD card.
 *
 * @param        None
 *
 * @return       Always MCI_FUNC_OK
 ***************************************************************************/
int32_t MCI_CardReset(void)
{
    st_Mci_CmdInfo cmdIf;
    /* Because CMD0 command to put the device to idle state does not need response 
    since, it's only sending commad */
    cmdIf.CmdIndex = CMD0_GO_IDLE_STATE;
    cmdIf.Argument = 0x00000000;
    cmdIf.ExpectResp = EXPECT_NO_RESP;
    cmdIf.AllowTimeout = 0;
    cmdIf.CmdResp = 0;
    MCI_SendCmd(&cmdIf);

    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief        Send CMD1 (SEND_OP_COND) to card.
 *
 * @param        None
 *
 * @return       MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_Cmd_SendOpCond( void )
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    retryCount = 0x200;            /* reset retry counter */

    cmdIf.CmdIndex = CMD1_SEND_OP_COND;
    cmdIf.Argument = OCR_INDEX;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = (uint32_t *)&respValue[0];
    /* continuously sends until the busy bit is cleared */
    while ( retryCount > 0 ) 
    {
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus & MCI_CMD_TIMEOUT)
        {
            retval = MCI_FUNC_TIMEOUT;
        }
        else if ((respValue[0] & 0x80000000) == 0)
        {
            //The card has not finished the power up routine
            retval = MCI_FUNC_BUS_NOT_IDLE;
        }
        else
        {
            retval = MCI_FUNC_OK;
            break;
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return(retval);
}


/************************************************************************//**
 * @brief        Send CMD8 (SEND_IF_COND) for interface condition to card.
 *
 * @param        None
 *
 * @return       MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_Cmd_SendIfCond(void)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t CmdArgument;
    uint32_t respStatus;
    uint32_t respValue[4];

    int32_t retval = MCI_FUNC_FAILED;

    uint8_t voltageSupplied = MCI_CMD8_VOLATAGESUPPLIED_27_36;//in range 2.7-3.6V
    uint8_t checkPattern = 0xAA;
    st_Mci_CmdInfo cmdIf;
   
    CmdArgument = (voltageSupplied << MCI_CMD8_VOLTAGESUPPLIED_POS) | (checkPattern << MCI_CMD8_CHECKPATTERN_POS);

    retryCount = 20;

    cmdIf.CmdIndex = CMD8_SEND_IF_COND;
    cmdIf.Argument = CmdArgument;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus & MCI_CMD_TIMEOUT)
        {
            //Consider as no response
            retval = MCI_FUNC_TIMEOUT;
        }
        else if (((respValue[0]>>MCI_CMD8_CHECKPATTERN_POS) & MCI_CMD8_CHECKPATTERN_BMASK) != checkPattern)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else if (((respValue[0] >> MCI_CMD8_VOLTAGESUPPLIED_POS) & MCI_CMD8_VOLTAGESUPPLIED_BMASK)
                        != voltageSupplied)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else
        {
            return MCI_FUNC_OK;
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief        Send CMD55 (APP_CMD) to indicate to the card that the next
 *               command is an application specific command rather than a 
 *               standard command. Before an ACMD, call this routine first
 *
 * @param        None
 *
 * @return       MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_Cmd_SendACMD( void )
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t CmdArgument;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
        (MCI_CardType == MCI_SDSC_V2_CARD) ||
        (MCI_CardType == MCI_SDHC_SDXC_CARD)) 
    {
        CmdArgument = CardRCA;    /* Use the address from SET_RELATIVE_ADDR cmd */
    }
    else            /* if MMC or unknown card type, use 0x0. */
    {
        CmdArgument = 0x00000000;
    }

    retryCount = 20;

    cmdIf.CmdIndex = CMD55_APP_CMD;
    cmdIf.Argument = CmdArgument;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus != 0)
        {
            retval = MCI_FUNC_FAILED;
        }
        else if (respValue[0] & CARD_STATUS_ACMD_ENABLE)
        {
            retval = MCI_FUNC_OK;
            break;
        }
        else
        {
            retval = MCI_FUNC_NOT_READY;
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief        Send ACMD41 (SEND_APP_OP_COND) to Host Capacity Support (HCS)
 *               information and asks the accessed card to send its operating
 *               condition (OCR).
 *
 * @param[in]    hcsVal input the Host Capacity Support
 *
 * @return       MCI_FUNC_OK if all success
 *
 * @note         If SEND_APP_OP_COND is timeout, the card in the slot is not MMC
 *                type, try this combination to see if we can communicate with
 *                a SD type.
 ****************************************************************************/
int32_t MCI_Acmd_SendOpCond(uint8_t hcsVal)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus, argument;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;

    int32_t retval = MCI_FUNC_FAILED;

    argument = OCR_INDEX | (hcsVal << MCI_ACMD41_HCS_POS);

    /* timeout on SEND_OP_COND command on MMC, now, try SEND_APP_OP_COND
    command to SD */
    retryCount = 0x2000;            /* reset retry counter */
    
    cmdIf.CmdIndex = ACMD41_SEND_APP_OP_COND;
    cmdIf.Argument = argument;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = (uint32_t *)&respValue[0];

    /* Clear Open Drain output control for SD */
    MCI_SetOutputMode(MCI_OUTPUT_MODE_PUSHPULL);

    /* The host repeatedly issues ACMD41 for at least 1 second or */
    /* until the busy bit are set to 1 */
    while ( retryCount > 0 )
    {
        if ((retval = MCI_Cmd_SendACMD()) == MCI_FUNC_OK)
        {   
            respStatus = MCI_CmdResp(&cmdIf);

            if(respStatus & MCI_CMD_TIMEOUT)
            {
                retval = MCI_FUNC_TIMEOUT;
            }
            else if (!(respValue[0] & 0x80000000))
            {
                retval = MCI_FUNC_BUS_NOT_IDLE;
            }
            else
            {
                CCS = (respValue[0]&(1<<30)) ? 1:0;
                retval = MCI_FUNC_OK;
                break;
            }
        }
        else    /* The command isn't accepted by the card.*/
        {
            return retval;
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}

/************************************************************************//**
 * @brief         Do initialization for the card in the slot
 *
 * @details        Try CMD1 first for MMC, if it's timeout, try CMD55
 *                and CMD41 for SD, if both failed, initialization faliure,
 *                bailout with unknown card type. Otherwise, return the
 *                card type, either MMC or SD. <<<KHOA_110708: checking>>>
 *
 *                This is followed Figure 4-2: Card Initialization and
 *                Identification Flow (SD mode) in Physical Layer Simplified
 *                Specification Version 2.00 document        
 *
 * @param         None
 *
 * @return        MCI_FUNC_OK if success
 ****************************************************************************/
int32_t MCI_CardInit( void )
{
    volatile uint32_t i;
    int32_t retval = MCI_FUNC_FAILED;

    MCI_CardType = MCI_CARD_UNKNOWN;

    if (MCI_CardReset() != MCI_FUNC_OK)
    {
        return MCI_FUNC_FAILED;
    }

    /* Clear Open Drain output control for SD */
    MCI_SetOutputMode(MCI_OUTPUT_MODE_PUSHPULL);

    for ( i = 0; i < 0x3000; i++ );

    retval = MCI_Cmd_SendIfCond();

    if(retval == MCI_FUNC_BAD_PARAMETERS)
    {
        //Unknow card is unusable
        return retval;
    }

    if(retval == MCI_FUNC_OK) /* Ver2.00 or later*/
    {
        //Check in case of High Capacity Supporting Host
        if ((retval = MCI_Acmd_SendOpCond(1)) == MCI_FUNC_OK)
        {
            MCI_CardType = MCI_SDSC_V2_CARD;//SDSC
            
            if(CCS )
            {
                MCI_CardType = MCI_SDHC_SDXC_CARD;//SDHC or SDXC 
            }

            return MCI_FUNC_OK;    /* Found the card, it's a hD */
        }
    }

    if(retval != MCI_FUNC_OK) /* voltage mismatch (ver2.00)or ver1.X SD Card or not SD Card*/
    {
        
        //Check in case of Standard Capacity Supporting Host
        if ((retval = MCI_Acmd_SendOpCond(0)) == MCI_FUNC_OK)
        {
            MCI_CardType = MCI_SDSC_V1_CARD;//Support Standard Capacity only

            return MCI_FUNC_OK;    /* Found the card, it's a SD */
        }
    }

    if(retval != MCI_FUNC_OK)
    {
        /* Set Open Drain output control for MMC */
        MCI_SetOutputMode(MCI_OUTPUT_MODE_OPENDRAIN);

        for ( i = 0; i < 0x3000; i++ );

        /* Try CMD1 first for MMC, if it's timeout, try CMD55 and CMD41 for SD,
        if both failed, initialization faIlure, bailout. */
        if (MCI_Cmd_SendOpCond() == MCI_FUNC_OK)
        {
            MCI_CardType = MCI_MMC_CARD;

            return MCI_FUNC_OK;    /* Found the card, it's a MMC */
        }
    }

    /* tried both MMC and SD card, give up */
    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief         Get the type of card that is currently used in the slot
 *
 * @param         None
 *
 * @return        Card Type: MMC Card or SD card
 ****************************************************************************/
en_Mci_CardType MCI_GetCardType(void)
{
    return MCI_CardType;
}


/************************************************************************//**
 * @brief         Get the all the Identifier (CID) of the card by sending the 
 *                CMD2 (ALL_SEND_CID) command. Then parse 4-byte data obtained
 *                from the card by the CID meaning.    
 *
 * @param[out]    cidValue the CID Result after parsing the data from the card
 *
 * @return        MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_GetCID(st_Mci_CardId* cidValue)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    
    /* This command is normally after CMD1(MMC) or ACMD41(SD). */
    retryCount = 0x200;// 0x20;            /* reset retry counter */
    
    cmdIf.CmdIndex = CMD2_ALL_SEND_CID;
    cmdIf.Argument = 0;
    cmdIf.ExpectResp = EXPECT_LONG_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
        respStatus = MCI_CmdResp(&cmdIf);

        /* bit 0 and bit 2 must be zero, or it's timeout or CRC error */
        //if ((!(respStatus & MCI_CMD_TIMEOUT)) && (!(respStatus & MCI_CMD_CRC_FAIL)))
        if (!(respStatus & MCI_CMD_TIMEOUT))
        {
            // Parsing the data retrieved
            if(cidValue != NULL)
            {
                cidValue->MID = (respValue[0] >> MCI_CID_MANUFACTURER_ID_WPOS) & MCI_CID_MANUFACTURER_ID_WBMASK;

                cidValue->OID = (respValue[0] >> MCI_CID_OEMAPPLICATION_ID_WPOS) & MCI_CID_OEMAPPLICATION_ID_WBMASK;

                cidValue->PNM_H = (respValue[0] >> MCI_CID_PRODUCTNAME_ID_H_WPOS) & MCI_CID_PRODUCTNAME_ID_H_WBMASK;

                cidValue->PNM_L = (respValue[1] >> MCI_CID_PRODUCTNAME_ID_L_WPOS) & MCI_CID_PRODUCTNAME_ID_L_WBMASK;

                cidValue->PRV = (respValue[2] >> MCI_CID_PRODUCTREVISION_ID_WPOS) & MCI_CID_PRODUCTREVISION_ID_WBMASK;

                cidValue->PSN = (((respValue[2] >> MCI_CID_PRODUCTSERIALNUM_ID_H_WPOS) & MCI_CID_PRODUCTSERIALNUM_ID_H_WBMASK) << 8)
                                            | ((respValue[3] >> MCI_CID_PRODUCTSERIALNUM_ID_L_WPOS) & MCI_CID_PRODUCTSERIALNUM_ID_L_WBMASK);

                cidValue->reserved = (respValue[3] >> MCI_CID_RESERVED_ID_WPOS) & MCI_CID_RESERVED_ID_WBMASK;

                cidValue->MDT = (respValue[3] >> MCI_CID_MANUFACTURINGDATE_ID_WPOS) & MCI_CID_MANUFACTURINGDATE_ID_WBMASK;

                cidValue->CRC = (respValue[3] >> MCI_CID_CHECKSUM_ID_WPOS) & MCI_CID_CHECKSUM_ID_WBMASK;

                cidValue->unused = (respValue[3] >> MCI_CID_UNUSED_ID_WPOS) & MCI_CID_UNUSED_ID_WBMASK;

            }

            return  MCI_FUNC_OK;    /* response is back and correct. */
        }


        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return MCI_FUNC_TIMEOUT;
}


/************************************************************************//**
 * @brief        Set the address for the card in the slot by sending CMD3 
 *                (SET_RELATIVE_ADDR) command. To get the address of the card
 *                currently in used, needs to call MCI_GetCardAddress()
 *
 * @param        None
 *
 * @return       MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_SetCardAddress( void )
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue;
    uint32_t CmdArgument;
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;
    
    /* If it's a SD card, SET_RELATIVE_ADDR is to get the address
    from the card and use this value in RCA, if it's a MMC, set default
    RCA addr. 0x00010000. */
    if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
        (MCI_CardType == MCI_SDSC_V2_CARD) ||
        (MCI_CardType == MCI_SDHC_SDXC_CARD))
    {
        CmdArgument = 0;
    }
    else            /* If it's unknown or MMC_CARD, fix the RCA address */
    {
        CmdArgument = 0x00010000;
    }

    retryCount = 0x20;            /* reset retry counter */
    cmdIf.CmdIndex = CMD3_SET_RELATIVE_ADDR;
    cmdIf.Argument = CmdArgument;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = &respValue;
    while ( retryCount > 0 )
    {
        /* Send CMD3 command repeatedly until the response is back correctly */
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus & MCI_CMD_TIMEOUT)
        {
            retval = MCI_FUNC_TIMEOUT;
        }
        else if(!((respValue >> RCA_RES_CARD_STATUS_POS)& CARD_STATUS_READY_FOR_DATA))
        {
            retval = MCI_FUNC_NOT_READY;
        }
        else if((CARDSTATEOF(respValue) != MCI_CARDSTATE_IDENDTIFIED))
        {
            retval = MCI_FUNC_ERR_STATE;
        }
        else
        {
            CardRCA = (respValue >> RCA_RES_NEW_PUBLISHED_RCA_POS) & RCA_RES_NEW_PUBLISHED_RCA_MASK;    /* Save the RCA value from SD card */

            CardRCA <<= RCA_ARGUMENT_POS;

            MCI_SetOutputMode(MCI_OUTPUT_MODE_PUSHPULL);
            
            return (MCI_FUNC_OK);    /* response is back and correct. */
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief        Get the address for the card in the slot
 *
 * @param        None
 *
 * @return       MCI_FUNC_OK if all success
 *
 * @note        This function must be called after MCI_SetCardAddress() executing
 ****************************************************************************/
uint32_t MCI_GetCardAddress(void)
{
    return CardRCA;
}


/************************************************************************//**
 * @brief       Get the Card-Specific Data of in-slot card by sending CMD9
 *                (SEND_CSD) command
 *
 * @param[out]  csdVal a buffer stored the value of CSD obtained from the card
 *
 * @return      MCI_FUNC_OK if all success
 *
 * @note        CMD9 (SEND_CSD) command should be sent only at standby state 
 *                (STBY) after CMD3
 ****************************************************************************/
int32_t MCI_GetCSD(uint32_t* csdVal)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    uint32_t CmdArgument;
    st_Mci_CmdInfo cmdIf;

    if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
        (MCI_CardType == MCI_SDSC_V2_CARD) ||
        (MCI_CardType == MCI_SDHC_SDXC_CARD))
    {
        CmdArgument = CardRCA;
    }
    else            /* if MMC or unknown card type, use default RCA addr. */
    {
        CmdArgument = 0x00010000;
    }

    retryCount = 0x20;
    cmdIf.CmdIndex = CMD9_SEND_CSD;
    cmdIf.Argument = CmdArgument;
    cmdIf.ExpectResp = EXPECT_LONG_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp = (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
         /* Check current status */
       if(((MCI_CheckStatus(CARD_STATE_STBY) != MCI_FUNC_OK)))
            return MCI_FUNC_ERR_STATE;

        respStatus = MCI_CmdResp(&cmdIf);

        if ( !respStatus )
        {
            if(csdVal != NULL)
            {
                csdVal[0] = respValue[0];
                csdVal[1] = respValue[1];
                csdVal[2] = respValue[2];
                csdVal[3] = respValue[3];
            }

            return (MCI_FUNC_OK);
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return (MCI_FUNC_FAILED);
}


/************************************************************************//**
 * @brief       Select the card by the specified address. This is done by sending
 *              out the CMD7 with the address argument to needed card
 *
 * @param       None
 *
 * @return      MCI_FUNC_OK if all success
 *
 * @note        CMD7 (SELECT_CARD) command should be sent after CMD9 ((SEND_CSD).
 *                The state will be inter-changed between STBY to TRANS by this 
 *                CMD7 command
 ****************************************************************************/
int32_t MCI_Cmd_SelectCard( void )
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    uint32_t CmdArgument;
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
            (MCI_CardType == MCI_SDSC_V2_CARD) ||
            (MCI_CardType == MCI_SDHC_SDXC_CARD)) 
    {
        CmdArgument = CardRCA;
    }
    else            /* if MMC or unknown card type, use default RCA addr. */
    {
        CmdArgument = 0x00010000;
    }

    retryCount = 0x20;
    cmdIf.CmdIndex = CMD7_SELECT_CARD;
    cmdIf.Argument = CmdArgument;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
         /* Check current status */
       if(((MCI_CheckStatus(CARD_STATE_STBY) != MCI_FUNC_OK)) &&
            (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK) &&
            (MCI_CheckStatus(CARD_STATE_DATA) != MCI_FUNC_OK) &&
            (MCI_CheckStatus(CARD_STATE_PRG) != MCI_FUNC_OK) &&
            (MCI_CheckStatus(CARD_STATE_DIS) != MCI_FUNC_OK))
            return MCI_FUNC_ERR_STATE;

        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus)
        {
            retval = MCI_FUNC_FAILED;
        }
        else if (respValue[0] & CARD_STATUS_ERR_MASK)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else
        {
            if(((MCI_CheckStatus(CARD_STATE_STBY) != MCI_FUNC_OK)) &&
                (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK) &&
                (MCI_CheckStatus(CARD_STATE_PRG) != MCI_FUNC_OK) &&
                (MCI_CheckStatus(CARD_STATE_DIS) != MCI_FUNC_OK))
                return MCI_FUNC_ERR_STATE;
            return MCI_FUNC_OK;
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief         Get the status of the card. The return is from the card.
 *                By sending CMD13 (SEND_STATUS), the status of the card
 *                will be responded from card addressed
 *
 * @param[out]    cardStatus the status returned from the card
 *
 * @return        MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_GetCardStatus(int32_t* cardStatus)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    uint32_t CmdArgument;
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    if(cardStatus == NULL)
        return MCI_FUNC_OK;
    
    if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
        (MCI_CardType == MCI_SDSC_V2_CARD) ||
        (MCI_CardType == MCI_SDHC_SDXC_CARD)) 
    {
        CmdArgument = CardRCA;
    }
    else            /* if MMC or unknown card type, use default RCA addr. */
    {
        CmdArgument = 0x00010000;
    }

    retryCount = 0x20;
    cmdIf.CmdIndex = CMD13_SEND_STATUS;
    cmdIf.Argument = CmdArgument;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus)  /* only retry if sending command failed */
        {
            retval = MCI_FUNC_FAILED;
        }
        else
        {
            *cardStatus = respValue[0];

            return MCI_FUNC_OK;
        }

        retryCount--;
        for ( i = 0; i < 0x10; i++ );
    }

    return retval;
}

/************************************************************************//**
 * @brief        Set the length for the blocks in the next action on data 
 *                manipulation (as read, write, erase). This function is to
 *                send CMD16 (SET_BLOCK_LEN) to cards.
 *
 * @param[in]    blockLength the value for the length of block will be handled
 *
 * @return         MCI_FUNC_OK if all success
 *
 * @note         CMD16 command should be sent after the card is selected by CMD7 
 *                (SELECT_CARD).
 *  In the case of SDHC and SDXC Cards, block length set by CMD16 command doen't 
 *  affect memory read and write commands. Always 512 Bytes fixed block length is 
 *  used. This command is effective for LOCK_UNLOCK command..
 ****************************************************************************/
int32_t MCI_SetBlockLen(uint32_t blockLength)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    retryCount = 0x20;
    cmdIf.CmdIndex = CMD16_SET_BLOCK_LEN;
    cmdIf.Argument = blockLength;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
         /* Check current status */
       if((MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK) )
            return MCI_FUNC_ERR_STATE;

        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus)
        {
            retval = MCI_FUNC_FAILED;
        }
        else if (respValue[0] & CARD_STATUS_ERR_MASK)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else
        {
            return MCI_CheckStatus(CARD_STATE_TRAN);
        }
        

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief         Set bus-width (1 bit or 4 bit) to work with the card by command
 *                CMD6 (SET_ACMD_BUS_WIDTH).
 *
 * @param[in]    buswidth The value represented for bus-width
 *                - 0b00: 1-bit bus-width
 *                - 0b10: 4-bit bus-width
 *
 * @return         MCI_FUNC_OK if all success
 *
 * @note        
 *                - If SD card is currently in used, it's possible to enable 4-bit
 *                bus-width instead of 1-bit to speed up.
 *                - This command can only be transferred during TRANS state.
 *                - Since, it's a ACMD, CMD55 (APP_CMD) needs to be sent out first
 ****************************************************************************/
int32_t MCI_Acmd_SendBusWidth( uint32_t buswidth )
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    retryCount = 0x20;            /* reset retry counter */
    cmdIf.CmdIndex = ACMD6_SET_BUS_WIDTH;
    cmdIf.Argument = buswidth;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
         /* The card must be in tran state in order to change the bus width */
        retval = MCI_CheckStatus(CARD_STATE_TRAN);
        if(retval!= MCI_FUNC_OK)
            return retval;
    
        if (MCI_Cmd_SendACMD() == MCI_FUNC_OK)
        {
            respStatus = MCI_CmdResp(&cmdIf);

            if(respStatus)
            {
                retval = MCI_FUNC_FAILED;
            }
            else if (respValue[0] & CARD_STATUS_ERR_MASK)
            {
                return MCI_FUNC_BAD_PARAMETERS;
            }
            else
            {
                return MCI_CheckStatus(CARD_STATE_TRAN);
            }
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief        Get the state of  data transfer to see if it is ended or not
 *
 * @param        None
 *
 * @return       Transfer state (stored by Mci_Data_Xfer_End variable)
 ****************************************************************************/
uint32_t MCI_GetDataXferEndState(void)
{
    return Mci_Data_Xfer_End;
}
/************************************************************************//**
 * @brief        Get the error state of  the lastest data transfer
 *
 * @param        None
 *
 * @return       Error state (stored by Mci_Data_Xfer_ERR variable)
 ****************************************************************************/
uint32_t MCI_GetXferErrState(void)
{
    return Mci_Data_Xfer_ERR;
}

/************************************************************************//**
 * @brief        Stop the current transmission on the bus by sending command CMD12
 *                (STOP_TRANSMISSION). In this case, the card may be in a unknown
 *                state. So that it need a warm reset for normal operation.
 *
 * @param[in]    None
 *
 * @return       MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_Cmd_StopTransmission( void )
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;

    /* do nothing when the card is in tran state */
    if(MCI_CheckStatus(CARD_STATE_TRAN) == MCI_FUNC_OK) 
    {
        return MCI_FUNC_OK;
    }
    
    retryCount = 0x20;
    cmdIf.CmdIndex = CMD12_STOP_TRANSMISSION;
    cmdIf.Argument = 0x00000000;
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
        /* Check current status */
         if((MCI_CheckStatus(CARD_STATE_DATA) != MCI_FUNC_OK) &&
              (MCI_CheckStatus(CARD_STATE_RCV) != MCI_FUNC_OK))
              return MCI_FUNC_ERR_STATE;
         
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus)
        {
            retval = MCI_FUNC_FAILED;
        }
        else if (respValue[0] & CARD_STATUS_ERR_MASK)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else
        {
            if((MCI_CheckStatus(CARD_STATE_PRG) != MCI_FUNC_OK) &&
              (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
              return MCI_FUNC_ERR_STATE;
            return MCI_FUNC_OK;
        }

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
    }

    return retval;
}


/************************************************************************//**
 * @brief        Write blocks to card by sending command CMD24 (WRITE_BLOCK) or
 *                command CMD25 (WRITE_MULTIPLE_BLOCK) followed by the blocks of
 *                data to be written.
 *
 * @param[in]    blockNum The block number to start writting
 *
 * @param[in]    numOfBlock Determine how many blocks will be written (from the
 *                starting block)
 *
 * @return       MCI_FUNC_OK if all success
 *
 * @note        These commands should be sent in TRANS state.
 ****************************************************************************/
int32_t MCI_Cmd_WriteBlock(uint32_t blockNum, uint32_t numOfBlock)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    st_Mci_CmdInfo cmdIf;
    uint32_t commandID;

    int32_t retval = MCI_FUNC_FAILED;

    if (numOfBlock > 1)
    {
        commandID = CMD25_WRITE_MULTIPLE_BLOCK;
    }
    else
    {
        commandID = CMD24_WRITE_BLOCK;
    }
    
    retryCount = 0x20;
    cmdIf.CmdIndex = commandID;
    if (MCI_CardType == MCI_SDHC_SDXC_CARD)
    {
        cmdIf.Argument = blockNum;                      /* Block Address */
    }
    else            
    {
        cmdIf.Argument = blockNum * BLOCK_LENGTH;       /* Byte Address */
    }     
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    
    while ( retryCount > 0 )
    {
         /* Check current status */
        if((MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
            return MCI_FUNC_ERR_STATE;
   
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus)
        {
            retval = MCI_FUNC_FAILED;
        }
        else if (respValue[0] & CARD_STATUS_ERR_MASK)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else 
        {
            if((MCI_CheckStatus(CARD_STATE_RCV) != MCI_FUNC_OK) &&
              (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
              return MCI_FUNC_ERR_STATE;
            return MCI_FUNC_OK;
        }
        

        for ( i = 0; i < 0x20; i++ );

        retryCount--;

    }

    return retval;                /* Fatal error */
}




/************************************************************************//**
 * @brief        Read blocks to card by sending CMD17 (READ_SINGLE_BLOCK) or
 *                CMD18 (READ_MULTIPLE_BLOCK) commands followed by the blocks of
 *                data to be read.
 *
 * @param[in]    blockNum The block number to start reading
 *
 * @param[in]    numOfBlock Determine how many blocks will be read (from the
 *                starting block)
 *
 * @return       MCI_FUNC_OK if all success
 *
 * @note        These commands should be sent in TRANS state.
 ****************************************************************************/
int32_t MCI_Cmd_ReadBlock(uint32_t blockNum, uint32_t numOfBlock)
{
    volatile uint32_t i;
    uint32_t retryCount;
    uint32_t respStatus;
    uint32_t respValue[4];
    uint32_t commandID;
    st_Mci_CmdInfo cmdIf;
    int32_t retval = MCI_FUNC_FAILED;
  
    // To Do: Read Multi-Block
    if (numOfBlock > 1)
        commandID = CMD18_READ_MULTIPLE_BLOCK;
    else
        commandID = CMD17_READ_SINGLE_BLOCK;

    retryCount = 0x20;
    cmdIf.CmdIndex = commandID;
    if (MCI_CardType == MCI_SDHC_SDXC_CARD)
    {
        cmdIf.Argument = blockNum;                      /* Block Address */
    }
    else            
    {
        cmdIf.Argument = blockNum * BLOCK_LENGTH;       /* Byte Address */
    }    
    cmdIf.ExpectResp = EXPECT_SHORT_RESP;
    cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
    cmdIf.CmdResp =  (uint32_t *)&respValue[0];
    while ( retryCount > 0 )
    {
         /* Check current status */
        if((MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
            return MCI_FUNC_ERR_STATE;
        
        respStatus = MCI_CmdResp(&cmdIf);

        if(respStatus)
        {
            retval = MCI_FUNC_FAILED;
        }
        else if (respValue[0] & CARD_STATUS_ERR_MASK)
        {
            return MCI_FUNC_BAD_PARAMETERS;
        }
        else
        {
            if((MCI_CheckStatus(CARD_STATE_DATA) != MCI_FUNC_OK) &&
              (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
              return MCI_FUNC_ERR_STATE;
            return MCI_FUNC_OK;
        }
       

        for ( i = 0; i < 0x20; i++ );

        retryCount--;
         
    }

    return retval;
}


/************************************************************************//**
 * @brief         Write data at a specific address to starting block with number
 *                of blocks will be written from first block
 * @details        
 *                - At preparation
 *                    - Set MCI data control register, data length and data timeout
 *                    - Send CMD24 (WRITE_BLOCK) or CMD25 (WRITE_MULTIPLE_BLOCK)
 *                    commands to card
 *                    - Enable interupt for MCI component
 *                - At completion
 *                    - TX_ACTIVE interrupt is occured
 *                    - Write data to FIFO register continuously until the data block
 *                    length is reached
 *
 * @param[in]    *memblock The pointer to location stored required data to be
 *                written to card
 *
 * @param[in]    blockNum The block number to start writting
 *
 * @param[in]    numOfBlock Determine how many blocks will be written (from the
 *                starting block)
 *
 * @return         MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_WriteBlock(volatile uint8_t* memblock, uint32_t blockNum, uint32_t numOfBlock)
{
    volatile uint32_t i;
    uint32_t DataCtrl = 0;

    if(BLOCK_LENGTH*numOfBlock > DATA_RW_MAX_LEN)
        return MCI_FUNC_BAD_PARAMETERS;
    
    dataSrcBlock = memblock;

    LPC_MCI->CLEAR = 0x7FF;

    LPC_MCI->DATACTRL = 0;

    for ( i = 0; i < 0x10; i++ );

    /* Wait the SD Card enters to TRANS state. */
    if (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK)
        return MCI_FUNC_ERR_STATE;
    
    LPC_MCI->DATATMR = DATA_TIMER_VALUE_W;

    LPC_MCI->DATALEN = BLOCK_LENGTH*numOfBlock;

    Mci_Data_Xfer_End = 1;
    Mci_Data_Xfer_ERR = 0;
    fifo_plane = 0;

    txBlockCnt = 0;

    MCI_TXEnable();

    if (MCI_Cmd_WriteBlock(blockNum, numOfBlock) != MCI_FUNC_OK)
    {
        return ( MCI_FUNC_FAILED );
    }

    //for(blockCnt = 0; blockCnt < numOfBlock; blockCnt++)
    {
#if MCI_DMA_ENABLED
        MCI_SettingDma((uint8_t*) dataSrcBlock, MCI_DMA_WRITE_CHANNEL, GPDMA_TRANSFERTYPE_M2P_DEST_CTRL);
        
        /* Write, block transfer, DMA, and data length */
        DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_TO_CARD 
                        | MCI_DATACTRL_DMA_ENABLE | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
#else
        /* Write, block transfer, and data length */
        DataCtrl |= MCI_DATACTRL_ENABLE  | MCI_DATACTRL_DIR_TO_CARD  | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
#endif
    }

    LPC_MCI->DATACTRL = DataCtrl;

    for ( i = 0; i < 0x10; i++ );

    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief         Read data at a specific address to starting block with number
 *                of blocks will be read from first block
 *
 * @details        
 *                - At preparation
 *                    - Set MCI data control register, data length and data timeout
 *                    - Send CMD17 (READ_SINGLE_BLOCK) or CMD18 (READ_MULTIPLE_BLOCK)
 *                    commands to card
 *                    - Enable interupt for MCI component
 *                - At completion
 *                    - RX_ACTIVE interrupt is occured
 *                    - Read data from FIFO register continuously until the data block
 *                    length is reached to retrieve needed data
 *
 * @param[in]    *destBlock The pointer to location will captured data read
 *                from card
 *
 * @param[in]    blockNum The block number to start reading
 *
 * @param[in]    numOfBlock Determine how many blocks will be read (from the
 *                starting block)
 *
 * @return         MCI_FUNC_OK if all success
 ****************************************************************************/
int32_t MCI_ReadBlock(volatile uint8_t* destBlock, uint32_t blockNum, uint32_t numOfBlock)
{
    volatile uint32_t i;
    uint32_t DataCtrl = 0;

    if(BLOCK_LENGTH*numOfBlock > DATA_RW_MAX_LEN)
        return MCI_FUNC_BAD_PARAMETERS;
    
    dataDestBlock = destBlock;

    LPC_MCI->CLEAR = 0x7FF;

    LPC_MCI->DATACTRL = 0;

    for ( i = 0; i < 0x10; i++ );

    /* Wait the SD Card enters to TRANS state. */
    if (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK)
        return MCI_FUNC_ERR_STATE;
    
    MCI_RXEnable();

    LPC_MCI->DATATMR = DATA_TIMER_VALUE_R;

    LPC_MCI->DATALEN = BLOCK_LENGTH*numOfBlock;

    Mci_Data_Xfer_End = 1;
    Mci_Data_Xfer_ERR = 0;
    rxBlockCnt = 0;
    fifo_plane = 0;


    // Start data engine on READ before command to avoid overflow of the FIFO.
    {        
#if MCI_DMA_ENABLED
        MCI_SettingDma((uint8_t*) dataDestBlock, MCI_DMA_READ_CHANNEL, GPDMA_TRANSFERTYPE_P2M_SRC_CTRL);

        /* Write, block transfer, DMA, and data length */
        DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_FROM_CARD
                        | MCI_DATACTRL_DMA_ENABLE | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
#else
        //Retrieving the result after reading the card is done by the FIFO handling for interrupt
        
        /* Read, enable, block transfer, and data length */
        DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_FROM_CARD | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);

#endif
    }
    
    LPC_MCI->DATACTRL = DataCtrl;

    for ( i = 0; i < 0x10; i++ );

    if ( MCI_Cmd_ReadBlock(blockNum, numOfBlock) != MCI_FUNC_OK )
    {
        return MCI_FUNC_FAILED;
    }

    return MCI_FUNC_OK;
}


/************************************************************************//**
 * @brief        Turn off the MCI power by disabling the Power Register for MCI
 *
 * @param        None
 *
 * @return       None
 ****************************************************************************/
void MCI_PowerOff(void) 
{
    volatile uint32_t i;

    LPC_MCI->POWER = 0;
    
    for (i = 0; i < 0x100; i++);
    
    return;
}


/**
 * @}
 */

#endif /*_MCI*/

/*****************************************************************************
**                            End Of File
******************************************************************************/