lpc_mci.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  1. /**********************************************************************
  2. * $Id$ lpc_mci.c 2011-06-02
  3. *//**
  4. * @file lpc_mci.c
  5. * @brief Contains all functions support for MCI firmware library
  6. * on LPC
  7. * @version 2.0
  8. * @date 29. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. #ifdef __BUILD_WITH_EXAMPLE__
  33. #include "lpc_libcfg.h"
  34. #else
  35. #include "lpc_libcfg_default.h"
  36. #endif /* __BUILD_WITH_EXAMPLE__ */
  37. #ifdef _MCI
  38. #include "LPC407x_8x_177x_8x.h"
  39. #include "lpc_types.h"
  40. #include "lpc_mci.h"
  41. #include "lpc_gpdma.h"
  42. #include "lpc_clkpwr.h"
  43. #include "lpc_pinsel.h"
  44. #define DMA_MCI_SIZE BLOCK_LENGTH
  45. #define MCI_DMA_WRITE_CHANNEL (0)
  46. #define MCI_DMA_READ_CHANNEL (1)
  47. #define _SHIFT(x) (1 << x)
  48. #define _XSHIFT(x, y) (x << y)
  49. #define SHIFT_(x) (1 >> x)
  50. #define XSHIFT_(x, y) (x >> y)
  51. #define MCI_ACMD41_HCS_POS (30)
  52. #define MCI_PWRCTRL_BMASK (0xC3)
  53. #define MCI_PWRCTRL_OPENDRAIN_POS (6)
  54. #define MCI_PWRCTRL_OPENDRAIN_NUMBIT (1)
  55. #define MCI_PWRCTRL_OPENDRAIN_BMASK (0x01)
  56. volatile uint32_t Mci_Data_Xfer_End = 0;
  57. volatile uint32_t Mci_Data_Xfer_ERR = 0;
  58. volatile uint8_t fifo_plane = 0;
  59. volatile uint32_t CardRCA;
  60. volatile uint8_t CCS;
  61. volatile en_Mci_CardType MCI_CardType;
  62. // Terminal Counter flag, Error Counter flag for Channel 0
  63. uint32_t dmaWrCh_TermianalCnt, dmaWrCh_ErrorCnt;
  64. uint32_t dmaRdCh_TermianalCnt, dmaRdCh_ErrorCnt;
  65. uint32_t MCI_SettingDma(uint8_t* memBuf, uint32_t ChannelNum, uint32_t DMAMode );
  66. int32_t MCI_ReadFifo(uint32_t * dest);
  67. int32_t MCI_WriteFifo(uint32_t * src);
  68. void MCI_TXEnable( void );
  69. void MCI_RXEnable( void );
  70. void MCI_TXDisable( void );
  71. void MCI_RXDisable( void );
  72. void MCI_CmdProcess( void );
  73. void MCI_DataErrorProcess( void );
  74. void MCI_DataErrorProcess( void );
  75. void MCI_DATA_END_InterruptService( void );
  76. void MCI_FIFOInterruptService( void );
  77. int32_t MCI_CheckStatus(uint8_t expect_status);
  78. volatile uint8_t* dataSrcBlock = (uint8_t *) MCI_DMA_SRC_ADDR;
  79. volatile uint8_t* dataDestBlock = (uint8_t *) MCI_DMA_DST_ADDR;
  80. volatile uint32_t txBlockCnt=0, rxBlockCnt=0;
  81. /** @addtogroup MCI_Private_Functions MCI Private Function
  82. * @ingroup MCI
  83. * @{
  84. */
  85. #if MCI_DMA_ENABLED
  86. /*********************************************************************//**
  87. * @brief Do setting GPDMA for MCI working
  88. *
  89. * @param[in] DMAMode set for the Type of DMA Transfer. It may be memory
  90. * to peripheral (M2P) or peripheral to memory (P2M)
  91. * in MCI working
  92. *
  93. * @param[in] ChannelNum which channel is used for current transfer with
  94. * DMA compent
  95. *
  96. * @param[in] memBuf point to a UINT8 buffer. In 2 cases of DMAMode
  97. * seperated:
  98. * - M2P: it is the source address that hold the
  99. * expected buffer to transfer
  100. * - P2M: it is the destination address that will store
  101. * data that retrieved from the peripheral (the Card in slot)
  102. *
  103. * @return None
  104. *
  105. * @note This is only required if DMA support is enabled
  106. **********************************************************************/
  107. uint32_t MCI_SettingDma(uint8_t* memBuf, uint32_t ChannelNum, uint32_t DMAMode )
  108. {
  109. GPDMA_Channel_CFG_Type GPDMACfg;
  110. // Transfer size
  111. GPDMACfg.TransferSize = DMA_MCI_SIZE;
  112. // Transfer width
  113. GPDMACfg.TransferWidth = GPDMA_WIDTH_WORD;
  114. // Transfer type
  115. GPDMACfg.TransferType = DMAMode;
  116. // Linker List Item - unused
  117. GPDMACfg.DMALLI = 0;
  118. /* USB RAM is used for test.
  119. Please note, Ethernet has its own SRAM, but GPDMA can't access
  120. that. GPDMA can access USB SRAM and IRAM. Ethernet DMA controller can
  121. access both IRAM and Ethernet SRAM. */
  122. GPDMACfg.ChannelNum = ChannelNum;
  123. if ( DMAMode == GPDMA_TRANSFERTYPE_M2P_DEST_CTRL )
  124. {
  125. /* Ch0 set for M2P transfer from mempry to MCI FIFO. */
  126. // Source memory
  127. GPDMACfg.SrcMemAddr = (uint32_t)memBuf;
  128. // Destination memory
  129. GPDMACfg.DstMemAddr = (uint32_t)LPC_MCI->FIFO;
  130. // Source connection
  131. GPDMACfg.SrcConn = 0;
  132. // Destination connection
  133. GPDMACfg.DstConn = GPDMA_CONN_MCI;
  134. }
  135. else if ( DMAMode == GPDMA_TRANSFERTYPE_P2M_SRC_CTRL )
  136. {
  137. /* Ch0 set for P2M transfer from MCI FIFO to memory. */
  138. // Source memory
  139. GPDMACfg.SrcMemAddr = (uint32_t)LPC_MCI->FIFO;
  140. // Destination memory
  141. GPDMACfg.DstMemAddr = (uint32_t)memBuf;
  142. // Source connection
  143. GPDMACfg.SrcConn = GPDMA_CONN_MCI;
  144. // Destination connection
  145. GPDMACfg.DstConn = 0;
  146. }
  147. else
  148. {
  149. return ( FALSE );
  150. }
  151. // Setup channel with given parameter
  152. GPDMA_Setup(&GPDMACfg);
  153. // Enable GPDMA channel
  154. GPDMA_ChannelCmd(ChannelNum, ENABLE);
  155. /* Enable GPDMA interrupt */
  156. NVIC_EnableIRQ(DMA_IRQn);
  157. return (TRUE);
  158. }
  159. /*********************************************************************//**
  160. * @brief GPDMA interrupt handler sub-routine
  161. *
  162. * @param None
  163. *
  164. * @return None
  165. *
  166. * @note This is only executed if DMA support is enabled
  167. **********************************************************************/
  168. void MCI_DMA_IRQHandler (void)
  169. {
  170. // check GPDMA interrupt on channel 0
  171. if (GPDMA_IntGetStatus(GPDMA_STAT_INT, MCI_DMA_WRITE_CHANNEL))
  172. {
  173. //check interrupt status on channel 0
  174. // Check counter terminal status
  175. if(GPDMA_IntGetStatus(GPDMA_STAT_INTTC, MCI_DMA_WRITE_CHANNEL))
  176. {
  177. // Clear terminate counter Interrupt pending
  178. GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, MCI_DMA_WRITE_CHANNEL);
  179. dmaWrCh_TermianalCnt++;
  180. }
  181. if (GPDMA_IntGetStatus(GPDMA_STAT_INTERR, MCI_DMA_WRITE_CHANNEL))
  182. {
  183. // Clear error counter Interrupt pending
  184. GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, MCI_DMA_WRITE_CHANNEL);
  185. dmaWrCh_ErrorCnt++;
  186. }
  187. }
  188. else if (GPDMA_IntGetStatus(GPDMA_STAT_INT, MCI_DMA_READ_CHANNEL))
  189. {
  190. //check interrupt status on channel 0
  191. // Check counter terminal status
  192. if(GPDMA_IntGetStatus(GPDMA_STAT_INTTC, MCI_DMA_READ_CHANNEL))
  193. {
  194. // Clear terminate counter Interrupt pending
  195. GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, MCI_DMA_READ_CHANNEL);
  196. dmaRdCh_TermianalCnt++;
  197. }
  198. if (GPDMA_IntGetStatus(GPDMA_STAT_INTERR, MCI_DMA_READ_CHANNEL))
  199. {
  200. // Clear error counter Interrupt pending
  201. GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, MCI_DMA_READ_CHANNEL);
  202. dmaRdCh_ErrorCnt++;
  203. }
  204. }
  205. }
  206. #endif
  207. /*********************************************************************//**
  208. * @brief Read data from FIFO (after a transmission with card) to
  209. * a destination buffer
  210. *
  211. * @param[in] *dest The buffer to store the data that read from card
  212. *
  213. * @return MCI_FUNC_OK
  214. *************************************************************************/
  215. int32_t MCI_ReadFifo(uint32_t * dest)
  216. {
  217. uint8_t i;
  218. uint8_t start, end;
  219. if(fifo_plane == 0)
  220. {
  221. start = 0;
  222. end = 7;
  223. }
  224. else
  225. {
  226. start = 8;
  227. end = 15;
  228. }
  229. fifo_plane = (fifo_plane) ? 0:1;
  230. for (i = start; i <= end; i++)
  231. {
  232. *dest = LPC_MCI->FIFO[i];
  233. dest++;
  234. }
  235. return MCI_FUNC_OK;
  236. }
  237. /*********************************************************************//**
  238. * @brief Write data from a source buffer to FIFO for transmission
  239. *
  240. * @param[in] *src The buffer hold the data need to write to card
  241. *
  242. * @return MCI_FUNC_OK
  243. *************************************************************************/
  244. int32_t MCI_WriteFifo(uint32_t * src)
  245. {
  246. uint8_t i;
  247. uint8_t start, end;
  248. if(fifo_plane == 0)
  249. {
  250. start = 0;
  251. end = 7;
  252. }
  253. else
  254. {
  255. start = 8;
  256. end = 15;
  257. }
  258. fifo_plane = (fifo_plane) ? 0:1;
  259. for (i = start; i <= end; i++)
  260. {
  261. LPC_MCI->FIFO[i] = *src;
  262. src++;
  263. }
  264. return MCI_FUNC_OK;
  265. }
  266. /*********************************************************************//**
  267. * @brief Enable Transmit data interrupt
  268. *
  269. * @param None
  270. *
  271. * @return None
  272. *************************************************************************/
  273. void MCI_TXEnable( void )
  274. {
  275. #if MCI_DMA_ENABLED
  276. LPC_MCI->MASK0 |= ((DATA_END_INT_MASK)|(ERR_TX_INT_MASK)); /* Enable TX interrupts only */
  277. #else
  278. LPC_MCI->MASK0 |= ((FIFO_TX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_TX_INT_MASK)); /* FIFO TX interrupts only */
  279. #endif
  280. return;
  281. }
  282. /*********************************************************************//**
  283. * @brief Disable Transmit data interrupt
  284. *
  285. * @param None
  286. *
  287. * @return None
  288. *************************************************************************/
  289. void MCI_TXDisable( void )
  290. {
  291. #if MCI_DMA_ENABLED
  292. LPC_MCI->MASK0 &= ~((DATA_END_INT_MASK)|(ERR_TX_INT_MASK)); /* Enable TX interrupts only */
  293. #else
  294. LPC_MCI->MASK0 &= ~((FIFO_TX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_TX_INT_MASK)); /* FIFO TX interrupts only */
  295. #endif
  296. return;
  297. }
  298. /*********************************************************************//**
  299. * @brief Enable Receive data interrupt
  300. *
  301. * @param None
  302. *
  303. * @return None
  304. *************************************************************************/
  305. void MCI_RXEnable( void )
  306. {
  307. #if MCI_DMA_ENABLED
  308. LPC_MCI->MASK0 |= ((DATA_END_INT_MASK)|(ERR_RX_INT_MASK)); /* Enable RX interrupts only */
  309. #else
  310. LPC_MCI->MASK0 |= ((FIFO_RX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_RX_INT_MASK)); /* FIFO RX interrupts only */
  311. #endif
  312. return;
  313. }
  314. /*********************************************************************//**
  315. * @brief Disable Receive data interrupt
  316. *
  317. * @param None
  318. *
  319. * @return None
  320. *************************************************************************/
  321. void MCI_RXDisable( void )
  322. {
  323. #if MCI_DMA_ENABLED
  324. LPC_MCI->MASK0 &= ~((DATA_END_INT_MASK)|(ERR_RX_INT_MASK)); /* Enable TX interrupts only */
  325. #else
  326. LPC_MCI->MASK0 &= ~((FIFO_RX_INT_MASK)|(DATA_END_INT_MASK)|(ERR_RX_INT_MASK)); /* FIFO TX interrupts only */
  327. #endif
  328. return;
  329. }
  330. /*********************************************************************//**
  331. * @brief Check if the card is in the given state.
  332. *@param expect_status expected status
  333. * @details Continuously get the card status until the card is ready. if its status matches
  334. * with the given state, return with success. Else, return MCI_FUNC_ERR_STATE.
  335. * If the card is still not ready, return MCI_FUNC_NOT_READY.
  336. *
  337. * @param None
  338. *
  339. * @return MCI_FUNC_OK if all success
  340. *************************************************************************/
  341. int32_t MCI_CheckStatus(uint8_t expect_status)
  342. {
  343. int32_t respValue, retval = MCI_FUNC_FAILED;
  344. uint32_t retryCnt = 0xFFFF, i;
  345. while (retryCnt > 0)
  346. {
  347. if (MCI_GetCardStatus(&respValue) != MCI_FUNC_OK)
  348. {
  349. break;
  350. }
  351. else
  352. {
  353. /* The only valid state is TRANS per MMC and SD state diagram.
  354. RCV state may be seen, but, it happens only when TX_ACTIVE or
  355. RX_ACTIVE occurs before the WRITE_BLOCK and READ_BLOCK cmds are
  356. being sent, which is not a valid sequence. */
  357. if(!(respValue & CARD_STATUS_READY_FOR_DATA ))
  358. {
  359. retval = MCI_FUNC_NOT_READY;
  360. }
  361. else if(CARDSTATEOF(respValue) != expect_status)
  362. {
  363. // If card is in prg state, wait until it changes to trans state
  364. // when "operation complete"
  365. if(CARDSTATEOF(respValue) != CARD_STATE_PRG)
  366. {
  367. return MCI_FUNC_ERR_STATE;
  368. }
  369. }
  370. else
  371. {
  372. return MCI_FUNC_OK;
  373. }
  374. }
  375. retryCnt--;
  376. for(i = 0; i < 0x20; i++);
  377. }
  378. return retval;
  379. }
  380. /*********************************************************************//**
  381. * @brief Called by MCI interrupt handler to simplify the command
  382. * process.
  383. *
  384. * @param None
  385. *
  386. * @return None
  387. *
  388. * @note In card initialization, the commnad interrupts are disabled
  389. *************************************************************************/
  390. void MCI_CmdProcess( void )
  391. {
  392. uint32_t MCIStatus;
  393. MCIStatus = LPC_MCI->STATUS;
  394. if ( MCIStatus & MCI_CMD_CRC_FAIL )
  395. {
  396. LPC_MCI->CLEAR = MCI_CMD_CRC_FAIL;
  397. }
  398. if ( MCIStatus & MCI_CMD_TIMEOUT )
  399. {
  400. LPC_MCI->CLEAR = MCI_CMD_TIMEOUT;
  401. }
  402. /* Cmd Resp End or Cmd Sent */
  403. if ( MCIStatus & MCI_CMD_RESP_END )
  404. {
  405. LPC_MCI->CLEAR = MCI_CMD_RESP_END;
  406. }
  407. if ( MCIStatus & MCI_CMD_SENT )
  408. {
  409. LPC_MCI->CLEAR = MCI_CMD_SENT;
  410. }
  411. if ( MCIStatus & MCI_CMD_ACTIVE )
  412. {
  413. LPC_MCI->CLEAR = MCI_CMD_ACTIVE;
  414. }
  415. return;
  416. }
  417. /*********************************************************************//**
  418. * @brief Called by MCI interrupt handler to manage error on the bus
  419. *
  420. * @param None
  421. *
  422. * @return None
  423. *************************************************************************/
  424. void MCI_DataErrorProcess( void )
  425. {
  426. uint32_t MCIStatus;
  427. MCIStatus = LPC_MCI->STATUS;
  428. if ( MCIStatus & MCI_DATA_CRC_FAIL )
  429. {
  430. LPC_MCI->CLEAR = MCI_DATA_CRC_FAIL;
  431. }
  432. if ( MCIStatus & MCI_DATA_TIMEOUT )
  433. {
  434. LPC_MCI->CLEAR = MCI_DATA_TIMEOUT;
  435. }
  436. /* Underrun or overrun */
  437. if ( MCIStatus & MCI_TX_UNDERRUN )
  438. {
  439. LPC_MCI->CLEAR = MCI_TX_UNDERRUN;
  440. }
  441. if ( MCIStatus & MCI_RX_OVERRUN )
  442. {
  443. LPC_MCI->CLEAR = MCI_RX_OVERRUN;
  444. }
  445. /* Start bit error on data signal */
  446. if ( MCIStatus & MCI_START_BIT_ERR )
  447. {
  448. LPC_MCI->CLEAR = MCI_START_BIT_ERR;
  449. }
  450. Mci_Data_Xfer_End = 0;
  451. Mci_Data_Xfer_ERR = MCIStatus;
  452. return;
  453. }
  454. /*********************************************************************//**
  455. * @brief Called by MCI interrupt handler. This is the last interrupt
  456. * manipulates the process of the data-block write and read
  457. * to/with card
  458. *
  459. * @details This service is also used with/without DMA support. It simply
  460. * clears the flag messages the in-process data-block transfer
  461. * has been done/ended
  462. *
  463. * @param None
  464. *
  465. * @return None
  466. *************************************************************************/
  467. void MCI_DATA_END_InterruptService( void )
  468. {
  469. uint32_t MCIStatus;
  470. MCIStatus = LPC_MCI->STATUS;
  471. if ( MCIStatus & MCI_DATA_END ) /* Data end, and Data block end */
  472. {
  473. LPC_MCI->CLEAR = MCI_DATA_END;
  474. Mci_Data_Xfer_End = 0;
  475. Mci_Data_Xfer_ERR = 0;
  476. MCI_TXDisable();
  477. MCI_RXDisable();
  478. return;
  479. }
  480. if ( MCIStatus & MCI_DATA_BLK_END )
  481. {
  482. LPC_MCI->CLEAR = MCI_DATA_BLK_END;
  483. //MCI_TXDisable();
  484. return;
  485. }
  486. /* Tx active */
  487. if ( MCIStatus & MCI_TX_ACTIVE )
  488. {
  489. }
  490. /* Rx active */
  491. if ( MCIStatus & MCI_RX_ACTIVE )
  492. {
  493. }
  494. return;
  495. }
  496. /*********************************************************************//**
  497. * @brief Called by MCI interrupt handler if requiring to using FIFO
  498. * for data transferring. It copy data to/from FIFO register
  499. * from/to a data buffer.
  500. *
  501. * @param None
  502. *
  503. * @return None
  504. *
  505. * @note This function is done without DMA transfer support
  506. **********************************************************************/
  507. void MCI_FIFOInterruptService( void )
  508. {
  509. #if !MCI_DMA_ENABLED
  510. uint32_t MCIStatus;
  511. MCIStatus = LPC_MCI->STATUS;
  512. if ( MCIStatus & (FIFO_TX_INT_MASK ) )
  513. {
  514. /* empty is multiple of 512 block size */
  515. if ( MCIStatus & MCI_TX_HALF_EMPTY )
  516. {
  517. //There's no data, return
  518. if(dataSrcBlock == NULL)
  519. return;
  520. /* write 8 words to fifo */
  521. MCI_WriteFifo((uint32_t *)&dataSrcBlock[txBlockCnt]);
  522. txBlockCnt += 32;
  523. }
  524. if (txBlockCnt == BLOCK_LENGTH) /* block complete */
  525. {
  526. dataSrcBlock += BLOCK_LENGTH;
  527. txBlockCnt = 0;
  528. /* disable FIFO int until next block write */
  529. //LPC_MCI->MASK0 &= ~(FIFO_TX_INT_MASK);
  530. /* wait for SD card to complete sending data i.e MCI_DATA_BLK_END interrupt */
  531. }
  532. }
  533. else if ( MCIStatus & (FIFO_RX_INT_MASK) )
  534. {
  535. /* if using RX_HALF_FULL remove one ReadFIFO below */
  536. if ( MCIStatus & MCI_RX_HALF_FULL )
  537. {
  538. //There's no store data, return
  539. if(dataDestBlock == NULL)
  540. return;
  541. /* read 8 words from fifo */
  542. MCI_ReadFifo((uint32_t *)&dataDestBlock[rxBlockCnt]);
  543. rxBlockCnt += 32;
  544. }
  545. /* block complete */
  546. if (rxBlockCnt == BLOCK_LENGTH)
  547. {
  548. dataDestBlock += BLOCK_LENGTH;
  549. rxBlockCnt = 0;
  550. }
  551. }
  552. #endif
  553. return;
  554. }
  555. /*********************************************************************//**
  556. * @brief MCI_IRQHandler is to manage the reasons that cause the
  557. * interrupt.
  558. *
  559. * @details It controls the data-block writing and reading by access
  560. * the FIFO register.
  561. * It handle the state changes on the MCI bus...
  562. *
  563. * @param None
  564. *
  565. * @return None
  566. **********************************************************************/
  567. void MCI_IRQHandler (void)
  568. {
  569. uint32_t MCI_Status;
  570. MCI_Status = LPC_MCI->STATUS;
  571. /* handle MCI_STATUS interrupt */
  572. if ( MCI_Status & DATA_ERR_INT_MASK )
  573. {
  574. MCI_DataErrorProcess();
  575. return;
  576. }
  577. if ( MCI_Status & DATA_END_INT_MASK )
  578. {
  579. MCI_DATA_END_InterruptService();
  580. return;
  581. }
  582. else if ( MCI_Status & FIFO_INT_MASK )
  583. {
  584. MCI_FIFOInterruptService();
  585. return;
  586. }
  587. else if ( MCI_Status & CMD_INT_MASK )
  588. {
  589. MCI_CmdProcess();
  590. return;
  591. }
  592. }
  593. /**
  594. * @}
  595. */
  596. /** @addtogroup MCI_Public_Functions
  597. * @{
  598. */
  599. /*********************************************************************//**
  600. * @brief Set MCI clock rate, during initialization phase < 400K
  601. * during data phase < 20Mhz
  602. *
  603. * @param[in] ClockRate Clock rate to be set (in Hz)
  604. *
  605. * @return None
  606. **********************************************************************/
  607. void MCI_Set_MCIClock( uint32_t ClockRate )
  608. {
  609. volatile uint32_t i;
  610. uint32_t ClkValue = 0;
  611. uint32_t pclk;
  612. pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  613. ClkValue = (pclk + 2*ClockRate - 1) /(2*ClockRate);
  614. if(ClkValue)
  615. ClkValue -= 1;
  616. LPC_MCI->CLOCK = (LPC_MCI->CLOCK & ~(0xFF)) | (1 << 8) | ClkValue;
  617. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK before next write */
  618. return;
  619. }
  620. /**********************************************************************//**
  621. * @brief Set the Width to 1-bit Bus or 4-bit Bus
  622. *
  623. * @param[in] width buswidth expected to set
  624. *
  625. * @return MCI_FUNC_OK in case of success
  626. *************************************************************************/
  627. int32_t MCI_SetBusWidth( uint32_t width )
  628. {
  629. volatile uint32_t i;
  630. uint8_t bus_width = BUS_WIDTH_1BIT;
  631. if ( width == SD_1_BIT )
  632. {
  633. LPC_MCI->CLOCK &= ~(1 << 11); /* 1 bit bus */
  634. }
  635. else if ( width == SD_4_BIT )
  636. {
  637. LPC_MCI->CLOCK |= (1 << 11);/* 4 bit bus */
  638. bus_width = BUS_WIDTH_4BITS;
  639. }
  640. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  641. if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
  642. (MCI_CardType == MCI_SDSC_V2_CARD) ||
  643. (MCI_CardType == MCI_SDHC_SDXC_CARD))
  644. {
  645. if ( MCI_Acmd_SendBusWidth( bus_width ) != MCI_FUNC_OK )
  646. {
  647. return(MCI_FUNC_FAILED);
  648. }
  649. }
  650. return MCI_FUNC_OK;
  651. }
  652. /************************************************************************//**
  653. * @brief Do initialization the MCI block as set its clock, registers,
  654. * setup NVIC for interrupts, configure the pins used for MCI
  655. * function, do initialize the card in slot...
  656. *
  657. * @param[in] powerActiveLevel the power level to activate the card in slot
  658. *
  659. * @return MCI_FUNC_OK in case of success
  660. ***************************************************************************/
  661. int32_t MCI_Init(uint8_t powerActiveLevel )
  662. {
  663. volatile uint32_t i;
  664. MCI_CardType = MCI_CARD_UNKNOWN;
  665. // Following block of code added to ensure card VCC drops to zero
  666. // before card is initialized
  667. // Force all MCI control pins to basic I/O mode
  668. LPC_IOCON->P1_2 &= ~0x1F; /* SD_CLK @ P1.2 */
  669. LPC_IOCON->P1_3 &= ~0x1F; /* SD_CMD @ P1.3 */
  670. LPC_IOCON->P1_5 &= ~0x1F; /* SD_PWR @ P1.5 */
  671. LPC_IOCON->P1_6 &= ~0x1F; /* SD_DAT_0 @ P1.6 */
  672. LPC_IOCON->P1_7 &= ~0x1F; /* SD_DAT_1 @ P1.7 */
  673. LPC_IOCON->P1_11 &= ~0x1F; /* SD_DAT_2 @ P1.11 */
  674. LPC_IOCON->P1_12 &= 0x1F; /* SD_DAT_3 @ P1.12 */
  675. // Set all MCI pins to outputs
  676. LPC_GPIO1->DIR |= 0x18EC;
  677. // Force all pins low (except power control pin)
  678. LPC_GPIO1->CLR = 0x1000;
  679. LPC_GPIO1->CLR = 0x0800;
  680. LPC_GPIO1->CLR = 0x0080;
  681. LPC_GPIO1->CLR = 0x0040;
  682. LPC_GPIO1->SET = 0x0020;
  683. LPC_GPIO1->CLR = 0x0008;
  684. LPC_GPIO1->CLR = 0x0004;
  685. // Crude delay of 50ms at 120MHz
  686. for ( i = 0; i < 0x100000; i++ );
  687. LPC_SC->PCONP |= ( 1 << 28 ); /* Enable clock to the MCI block */
  688. if ( LPC_MCI->CLOCK & (1 << 8) )
  689. {
  690. LPC_MCI->CLOCK &= ~(1 << 8);
  691. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  692. }
  693. if ( LPC_MCI->POWER & 0x02 )
  694. {
  695. LPC_MCI->POWER = 0x00;
  696. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  697. }
  698. /* Disable all interrupts for now */
  699. LPC_MCI->MASK0 = 0;
  700. //SD_CLK
  701. PINSEL_ConfigPin(1, 2, 2);
  702. //SD_CMD
  703. PINSEL_ConfigPin(1, 3, 2);
  704. //SD_PWR
  705. PINSEL_ConfigPin(1, 5, 2);
  706. //SD_DAT_0
  707. PINSEL_ConfigPin(1, 6, 2);
  708. //SD_DAT_1
  709. PINSEL_ConfigPin(1, 7, 2);
  710. //SD_DAT_2
  711. PINSEL_ConfigPin(1, 11, 2);
  712. //SD_DAT_3
  713. PINSEL_ConfigPin(1, 12, 2);
  714. // SD_PWR is active high (follows the output of the SD Card interface block).
  715. if(powerActiveLevel == LOW_LVL)
  716. {
  717. LPC_SC->SCS &= ~ 0x08;//Becase on EA board SD_PWR is active low
  718. }
  719. else
  720. {
  721. LPC_SC->SCS |= 0x08;
  722. }
  723. //Setting for timeout problem
  724. LPC_MCI->DATATMR = 0x1FFFFFFF;
  725. /*set up clocking default mode, clear any registers as needed */
  726. LPC_MCI->COMMAND = 0;
  727. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  728. LPC_MCI->DATACTRL = 0;
  729. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  730. LPC_MCI->CLEAR = 0x7FF; /* clear all pending interrupts */
  731. LPC_MCI->POWER = 0x02; /* power up */
  732. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  733. /* delays for the supply output is stable*/
  734. for ( i = 0; i < 0x80000; i++ );
  735. /* During identification phase, the clock should be less than
  736. 400Khz. Once we pass this phase, the normal clock can be set up
  737. to 25Mhz on SD card and 20Mhz on MMC card. */
  738. MCI_Set_MCIClock(MCI_SLOW_RATE );
  739. LPC_MCI->POWER |= 0x01; /* bit 1 is set already, from power up to power on */
  740. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  741. NVIC_EnableIRQ(MCI_IRQn);
  742. MCI_CardInit();
  743. /* During the initialization phase, to simplify the process, the CMD related
  744. interrupts are disabled. The DATA related interrupts are enabled when
  745. the FIFOs are used and just before WRITE_BLOCK READ_BLOCK cmds are issues, and
  746. disabled after the data block has been written and read. Please also note,
  747. before WRITE_BLOCK only TX related data interrupts are enabled, and before
  748. READ_BLOCK only RX related data interrupts are enabled. */
  749. return MCI_FUNC_OK;
  750. }
  751. /************************************************************************//**
  752. * @brief Set output in open drain mode or pushpull mode
  753. *
  754. * @param[in] mode the mode going to set
  755. *
  756. * @return None
  757. ***************************************************************************/
  758. void MCI_SetOutputMode(uint32_t mode)
  759. {
  760. uint32_t i = 0;
  761. if(mode == MCI_OUTPUT_MODE_OPENDRAIN)
  762. {
  763. /* Set Open Drain output control for MMC */
  764. LPC_MCI->POWER |= (1 << MCI_PWRCTRL_OPENDRAIN_POS) & MCI_PWRCTRL_BMASK;
  765. }
  766. else
  767. {
  768. /* Clear Open Drain output control for SD */
  769. LPC_MCI->POWER &= (~(1 << MCI_PWRCTRL_OPENDRAIN_POS) & MCI_PWRCTRL_BMASK);
  770. }
  771. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  772. }
  773. /************************************************************************//**
  774. * @brief The routine is used to send a CMD to the card
  775. *
  776. * @param[in] CmdIndex the command to be sent to cards
  777. *
  778. * @param[in] Argument the argument follows the command
  779. *
  780. * @param[in] ExpectResp the response type for the command. They may be:
  781. * - EXPECT_NO_RESP: means no response required
  782. * - EXPECT_SHORT_RESP: means a response in a word needed
  783. * - EXPECT_LONG_RESP: means a response in 4 words needed
  784. *
  785. * @param[in] AllowTimeout allow timeout the command or not
  786. *
  787. * @return None
  788. ***************************************************************************/
  789. void MCI_SendCmd(st_Mci_CmdInfo* pCmdIf)
  790. {
  791. volatile uint32_t i;
  792. uint32_t CmdData = 0;
  793. uint32_t CmdStatus;
  794. uint32_t CmdIndex = pCmdIf->CmdIndex;
  795. uint32_t Argument = pCmdIf->Argument;
  796. uint32_t ExpectResp = pCmdIf->ExpectResp;
  797. uint32_t AllowTimeout = pCmdIf->AllowTimeout;
  798. /* the command engine must be disabled when we modify the argument
  799. or the peripheral resends */
  800. while ( (CmdStatus = LPC_MCI->STATUS) & MCI_CMD_ACTIVE ) /* Command in progress. */
  801. {
  802. LPC_MCI->COMMAND = 0;
  803. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  804. LPC_MCI->CLEAR = CmdStatus | MCI_CMD_ACTIVE;
  805. }
  806. /*set the command details, the CmdIndex should 0 through 0x3F only */
  807. CmdData |= (CmdIndex & 0x3F); /* bit 0 through 5 only */
  808. if ( ExpectResp == EXPECT_NO_RESP ) /* no response */
  809. {
  810. CmdData &= ~((1 << 6) | (1 << 7)); /* Clear long response bit as well */
  811. }
  812. else if ( ExpectResp == EXPECT_SHORT_RESP ) /* expect short response */
  813. {
  814. CmdData |= (1 << 6);
  815. }
  816. else if ( ExpectResp == EXPECT_LONG_RESP ) /* expect long response */
  817. {
  818. CmdData |= (1 << 6) | (1 << 7);
  819. }
  820. if ( AllowTimeout == ALLOW_CMD_TIMER ) /* allow timeout or not */
  821. {
  822. CmdData &= ~ MCI_DISABLE_CMD_TIMER;
  823. }
  824. else
  825. {
  826. CmdData |= MCI_DISABLE_CMD_TIMER;
  827. }
  828. /*send the command*/
  829. CmdData |= (1 << 10); /* This bit needs to be set last. */
  830. // clear status register
  831. LPC_MCI->CLEAR = 0x7FF;
  832. LPC_MCI->ARGUMENT = Argument; /* Set the argument first, finally command */
  833. LPC_MCI->COMMAND = CmdData;
  834. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  835. // Wait until command is processed
  836. while(!LPC_MCI->STATUS);
  837. // Wait until command sent
  838. while(LPC_MCI->STATUS & MCI_CMD_ACTIVE);
  839. return;
  840. }
  841. /************************************************************************//**
  842. * @brief The routine is to get the reponse from card after commands.
  843. * This function is always used in pair of MCI_SendCmd() func
  844. *
  845. * @param[in] ExpectCmdData specify the command of which the data will be
  846. * retrieved. This field should be the same with CmdIndex of
  847. * MCI_SendCmd() function.
  848. *
  849. * @param[in] ExpectResp the response type for the command. They may be:
  850. * - EXPECT_NO_RESP: means no response required
  851. * - EXPECT_SHORT_RESP: means a response in a word needed
  852. * - EXPECT_LONG_RESP: means a response in 4 words needed
  853. *
  854. * @param[out] CmdResp the buffer stored the data replied from cards
  855. *
  856. * @return MCI_FUNC_OK in case of success
  857. ***************************************************************************/
  858. int32_t MCI_GetCmdResp(uint32_t ExpectCmdData, uint32_t ExpectResp, uint32_t *CmdResp)
  859. {
  860. uint32_t CmdRespStatus = 0;
  861. uint32_t LastCmdIndex;
  862. uint32_t i = 0;
  863. if ( ExpectResp == EXPECT_NO_RESP )
  864. {
  865. return MCI_FUNC_OK;
  866. }
  867. while (1)
  868. {
  869. // Get the status of the component
  870. CmdRespStatus = LPC_MCI->STATUS;
  871. if ( CmdRespStatus & (MCI_CMD_TIMEOUT) )
  872. {
  873. LPC_MCI->CLEAR = CmdRespStatus | MCI_CMD_TIMEOUT;
  874. LPC_MCI->COMMAND = 0;
  875. LPC_MCI->ARGUMENT = 0xFFFFFFFF;
  876. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  877. return (CmdRespStatus);
  878. }
  879. if ( CmdRespStatus & MCI_CMD_CRC_FAIL )
  880. {
  881. LPC_MCI->CLEAR = CmdRespStatus | MCI_CMD_CRC_FAIL;
  882. LastCmdIndex = LPC_MCI->COMMAND & 0x003F;
  883. if ( (LastCmdIndex == CMD1_SEND_OP_COND) || (LastCmdIndex == ACMD41_SEND_APP_OP_COND)
  884. || (LastCmdIndex == CMD12_STOP_TRANSMISSION) )
  885. {
  886. LPC_MCI->COMMAND = 0;
  887. LPC_MCI->ARGUMENT = 0xFFFFFFFF;
  888. for ( i = 0; i < 0x10; i++ ); /* delay 3MCLK + 2PCLK */
  889. break; /* ignore CRC error if it's a resp for SEND_OP_COND
  890. or STOP_TRANSMISSION. */
  891. }
  892. else
  893. {
  894. return (CmdRespStatus);
  895. }
  896. }
  897. else if (CmdRespStatus & MCI_CMD_RESP_END)
  898. {
  899. LPC_MCI->CLEAR = CmdRespStatus | MCI_CMD_RESP_END;
  900. break; /* cmd response is received, expecting response */
  901. }
  902. }
  903. if ((LPC_MCI->RESP_CMD & 0x3F) != ExpectCmdData)
  904. {
  905. /* If the response is not R1, in the response field, the Expected Cmd data
  906. won't be the same as the CMD data in SendCmd(). Below four cmds have
  907. R2 or R3 response. We don't need to check if MCI_RESP_CMD is the same
  908. as the Expected or not. */
  909. if ((ExpectCmdData != CMD1_SEND_OP_COND) && (ExpectCmdData != ACMD41_SEND_APP_OP_COND)
  910. && (ExpectCmdData != CMD2_ALL_SEND_CID) && (ExpectCmdData != CMD9_SEND_CSD))
  911. {
  912. CmdRespStatus = INVALID_RESPONSE; /* Reuse error status */
  913. return (INVALID_RESPONSE);
  914. }
  915. }
  916. /* Read MCI_RESP0 register assuming it's not long response. */
  917. if (CmdResp != NULL)
  918. {
  919. if (ExpectResp == EXPECT_SHORT_RESP)
  920. {
  921. *(CmdResp + 0) = LPC_MCI->RESP0;
  922. }
  923. else if (ExpectResp == EXPECT_LONG_RESP)
  924. {
  925. *(CmdResp + 0) = LPC_MCI->RESP0;
  926. *(CmdResp + 1) = LPC_MCI->RESP1;
  927. *(CmdResp + 2) = LPC_MCI->RESP2;
  928. *(CmdResp + 3) = LPC_MCI->RESP3;
  929. }
  930. }
  931. return MCI_FUNC_OK;
  932. }
  933. /************************************************************************//**
  934. * @brief The routine is to send command to cards then get back the
  935. * reponses (if required).
  936. *
  937. * @param[in] CmdIndex the command to be sent to cards
  938. *
  939. * @param[in] Argument the argument follows the command
  940. *
  941. * @param[in] ExpectResp the response type for the command. They may be:
  942. * - EXPECT_NO_RESP: means no response required
  943. * - EXPECT_SHORT_RESP: means a response in a word needed
  944. * - EXPECT_LONG_RESP: means a response in 4 words needed
  945. *
  946. * @param[out] CmdResp the buffer stored the data replied from cards
  947. *
  948. * @param[in] AllowTimeout allow timeout the command or not
  949. *
  950. * @return MCI_FUNC_OK in case of success
  951. ***************************************************************************/
  952. int32_t MCI_CmdResp(st_Mci_CmdInfo* pCmdIf)
  953. {
  954. int32_t respStatus;
  955. uint32_t CmdIndex = pCmdIf->CmdIndex;
  956. uint32_t ExpectResp = pCmdIf->ExpectResp;
  957. uint32_t *CmdResp = pCmdIf->CmdResp;
  958. MCI_SendCmd(pCmdIf);
  959. if((CmdResp != NULL) || (ExpectResp != EXPECT_NO_RESP))
  960. {
  961. respStatus = MCI_GetCmdResp(CmdIndex, ExpectResp, CmdResp);
  962. }
  963. else
  964. {
  965. respStatus = MCI_FUNC_BAD_PARAMETERS;
  966. }
  967. return respStatus;
  968. }
  969. /************************************************************************//**
  970. * @brief To reset the card, the CMD0 is sent and then the card is put
  971. * in idle state. This is the very first command to be sent to
  972. * initialize either MMC or SD card.
  973. *
  974. * @param None
  975. *
  976. * @return Always MCI_FUNC_OK
  977. ***************************************************************************/
  978. int32_t MCI_CardReset(void)
  979. {
  980. st_Mci_CmdInfo cmdIf;
  981. /* Because CMD0 command to put the device to idle state does not need response
  982. since, it's only sending commad */
  983. cmdIf.CmdIndex = CMD0_GO_IDLE_STATE;
  984. cmdIf.Argument = 0x00000000;
  985. cmdIf.ExpectResp = EXPECT_NO_RESP;
  986. cmdIf.AllowTimeout = 0;
  987. cmdIf.CmdResp = 0;
  988. MCI_SendCmd(&cmdIf);
  989. return MCI_FUNC_OK;
  990. }
  991. /************************************************************************//**
  992. * @brief Send CMD1 (SEND_OP_COND) to card.
  993. *
  994. * @param None
  995. *
  996. * @return MCI_FUNC_OK if all success
  997. ****************************************************************************/
  998. int32_t MCI_Cmd_SendOpCond( void )
  999. {
  1000. volatile uint32_t i;
  1001. uint32_t retryCount;
  1002. uint32_t respStatus;
  1003. uint32_t respValue[4];
  1004. st_Mci_CmdInfo cmdIf;
  1005. int32_t retval = MCI_FUNC_FAILED;
  1006. retryCount = 0x200; /* reset retry counter */
  1007. cmdIf.CmdIndex = CMD1_SEND_OP_COND;
  1008. cmdIf.Argument = OCR_INDEX;
  1009. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1010. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1011. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1012. /* continuously sends until the busy bit is cleared */
  1013. while ( retryCount > 0 )
  1014. {
  1015. respStatus = MCI_CmdResp(&cmdIf);
  1016. if(respStatus & MCI_CMD_TIMEOUT)
  1017. {
  1018. retval = MCI_FUNC_TIMEOUT;
  1019. }
  1020. else if ((respValue[0] & 0x80000000) == 0)
  1021. {
  1022. //The card has not finished the power up routine
  1023. retval = MCI_FUNC_BUS_NOT_IDLE;
  1024. }
  1025. else
  1026. {
  1027. retval = MCI_FUNC_OK;
  1028. break;
  1029. }
  1030. for ( i = 0; i < 0x20; i++ );
  1031. retryCount--;
  1032. }
  1033. return(retval);
  1034. }
  1035. /************************************************************************//**
  1036. * @brief Send CMD8 (SEND_IF_COND) for interface condition to card.
  1037. *
  1038. * @param None
  1039. *
  1040. * @return MCI_FUNC_OK if all success
  1041. ****************************************************************************/
  1042. int32_t MCI_Cmd_SendIfCond(void)
  1043. {
  1044. volatile uint32_t i;
  1045. uint32_t retryCount;
  1046. uint32_t CmdArgument;
  1047. uint32_t respStatus;
  1048. uint32_t respValue[4];
  1049. int32_t retval = MCI_FUNC_FAILED;
  1050. uint8_t voltageSupplied = MCI_CMD8_VOLATAGESUPPLIED_27_36;//in range 2.7-3.6V
  1051. uint8_t checkPattern = 0xAA;
  1052. st_Mci_CmdInfo cmdIf;
  1053. CmdArgument = (voltageSupplied << MCI_CMD8_VOLTAGESUPPLIED_POS) | (checkPattern << MCI_CMD8_CHECKPATTERN_POS);
  1054. retryCount = 20;
  1055. cmdIf.CmdIndex = CMD8_SEND_IF_COND;
  1056. cmdIf.Argument = CmdArgument;
  1057. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1058. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1059. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1060. while ( retryCount > 0 )
  1061. {
  1062. respStatus = MCI_CmdResp(&cmdIf);
  1063. if(respStatus & MCI_CMD_TIMEOUT)
  1064. {
  1065. //Consider as no response
  1066. retval = MCI_FUNC_TIMEOUT;
  1067. }
  1068. else if (((respValue[0]>>MCI_CMD8_CHECKPATTERN_POS) & MCI_CMD8_CHECKPATTERN_BMASK) != checkPattern)
  1069. {
  1070. return MCI_FUNC_BAD_PARAMETERS;
  1071. }
  1072. else if (((respValue[0] >> MCI_CMD8_VOLTAGESUPPLIED_POS) & MCI_CMD8_VOLTAGESUPPLIED_BMASK)
  1073. != voltageSupplied)
  1074. {
  1075. return MCI_FUNC_BAD_PARAMETERS;
  1076. }
  1077. else
  1078. {
  1079. return MCI_FUNC_OK;
  1080. }
  1081. for ( i = 0; i < 0x20; i++ );
  1082. retryCount--;
  1083. }
  1084. return retval;
  1085. }
  1086. /************************************************************************//**
  1087. * @brief Send CMD55 (APP_CMD) to indicate to the card that the next
  1088. * command is an application specific command rather than a
  1089. * standard command. Before an ACMD, call this routine first
  1090. *
  1091. * @param None
  1092. *
  1093. * @return MCI_FUNC_OK if all success
  1094. ****************************************************************************/
  1095. int32_t MCI_Cmd_SendACMD( void )
  1096. {
  1097. volatile uint32_t i;
  1098. uint32_t retryCount;
  1099. uint32_t CmdArgument;
  1100. uint32_t respStatus;
  1101. uint32_t respValue[4];
  1102. st_Mci_CmdInfo cmdIf;
  1103. int32_t retval = MCI_FUNC_FAILED;
  1104. if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
  1105. (MCI_CardType == MCI_SDSC_V2_CARD) ||
  1106. (MCI_CardType == MCI_SDHC_SDXC_CARD))
  1107. {
  1108. CmdArgument = CardRCA; /* Use the address from SET_RELATIVE_ADDR cmd */
  1109. }
  1110. else /* if MMC or unknown card type, use 0x0. */
  1111. {
  1112. CmdArgument = 0x00000000;
  1113. }
  1114. retryCount = 20;
  1115. cmdIf.CmdIndex = CMD55_APP_CMD;
  1116. cmdIf.Argument = CmdArgument;
  1117. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1118. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1119. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1120. while ( retryCount > 0 )
  1121. {
  1122. respStatus = MCI_CmdResp(&cmdIf);
  1123. if(respStatus != 0)
  1124. {
  1125. retval = MCI_FUNC_FAILED;
  1126. }
  1127. else if (respValue[0] & CARD_STATUS_ACMD_ENABLE)
  1128. {
  1129. retval = MCI_FUNC_OK;
  1130. break;
  1131. }
  1132. else
  1133. {
  1134. retval = MCI_FUNC_NOT_READY;
  1135. }
  1136. for ( i = 0; i < 0x20; i++ );
  1137. retryCount--;
  1138. }
  1139. return retval;
  1140. }
  1141. /************************************************************************//**
  1142. * @brief Send ACMD41 (SEND_APP_OP_COND) to Host Capacity Support (HCS)
  1143. * information and asks the accessed card to send its operating
  1144. * condition (OCR).
  1145. *
  1146. * @param[in] hcsVal input the Host Capacity Support
  1147. *
  1148. * @return MCI_FUNC_OK if all success
  1149. *
  1150. * @note If SEND_APP_OP_COND is timeout, the card in the slot is not MMC
  1151. * type, try this combination to see if we can communicate with
  1152. * a SD type.
  1153. ****************************************************************************/
  1154. int32_t MCI_Acmd_SendOpCond(uint8_t hcsVal)
  1155. {
  1156. volatile uint32_t i;
  1157. uint32_t retryCount;
  1158. uint32_t respStatus, argument;
  1159. uint32_t respValue[4];
  1160. st_Mci_CmdInfo cmdIf;
  1161. int32_t retval = MCI_FUNC_FAILED;
  1162. argument = OCR_INDEX | (hcsVal << MCI_ACMD41_HCS_POS);
  1163. /* timeout on SEND_OP_COND command on MMC, now, try SEND_APP_OP_COND
  1164. command to SD */
  1165. retryCount = 0x2000; /* reset retry counter */
  1166. cmdIf.CmdIndex = ACMD41_SEND_APP_OP_COND;
  1167. cmdIf.Argument = argument;
  1168. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1169. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1170. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1171. /* Clear Open Drain output control for SD */
  1172. MCI_SetOutputMode(MCI_OUTPUT_MODE_PUSHPULL);
  1173. /* The host repeatedly issues ACMD41 for at least 1 second or */
  1174. /* until the busy bit are set to 1 */
  1175. while ( retryCount > 0 )
  1176. {
  1177. if ((retval = MCI_Cmd_SendACMD()) == MCI_FUNC_OK)
  1178. {
  1179. respStatus = MCI_CmdResp(&cmdIf);
  1180. if(respStatus & MCI_CMD_TIMEOUT)
  1181. {
  1182. retval = MCI_FUNC_TIMEOUT;
  1183. }
  1184. else if (!(respValue[0] & 0x80000000))
  1185. {
  1186. retval = MCI_FUNC_BUS_NOT_IDLE;
  1187. }
  1188. else
  1189. {
  1190. CCS = (respValue[0]&(1<<30)) ? 1:0;
  1191. retval = MCI_FUNC_OK;
  1192. break;
  1193. }
  1194. }
  1195. else /* The command isn't accepted by the card.*/
  1196. {
  1197. return retval;
  1198. }
  1199. for ( i = 0; i < 0x20; i++ );
  1200. retryCount--;
  1201. }
  1202. return retval;
  1203. }
  1204. /************************************************************************//**
  1205. * @brief Do initialization for the card in the slot
  1206. *
  1207. * @details Try CMD1 first for MMC, if it's timeout, try CMD55
  1208. * and CMD41 for SD, if both failed, initialization faliure,
  1209. * bailout with unknown card type. Otherwise, return the
  1210. * card type, either MMC or SD. <<<KHOA_110708: checking>>>
  1211. *
  1212. * This is followed Figure 4-2: Card Initialization and
  1213. * Identification Flow (SD mode) in Physical Layer Simplified
  1214. * Specification Version 2.00 document
  1215. *
  1216. * @param None
  1217. *
  1218. * @return MCI_FUNC_OK if success
  1219. ****************************************************************************/
  1220. int32_t MCI_CardInit( void )
  1221. {
  1222. volatile uint32_t i;
  1223. int32_t retval = MCI_FUNC_FAILED;
  1224. MCI_CardType = MCI_CARD_UNKNOWN;
  1225. if (MCI_CardReset() != MCI_FUNC_OK)
  1226. {
  1227. return MCI_FUNC_FAILED;
  1228. }
  1229. /* Clear Open Drain output control for SD */
  1230. MCI_SetOutputMode(MCI_OUTPUT_MODE_PUSHPULL);
  1231. for ( i = 0; i < 0x3000; i++ );
  1232. retval = MCI_Cmd_SendIfCond();
  1233. if(retval == MCI_FUNC_BAD_PARAMETERS)
  1234. {
  1235. //Unknow card is unusable
  1236. return retval;
  1237. }
  1238. if(retval == MCI_FUNC_OK) /* Ver2.00 or later*/
  1239. {
  1240. //Check in case of High Capacity Supporting Host
  1241. if ((retval = MCI_Acmd_SendOpCond(1)) == MCI_FUNC_OK)
  1242. {
  1243. MCI_CardType = MCI_SDSC_V2_CARD;//SDSC
  1244. if(CCS )
  1245. {
  1246. MCI_CardType = MCI_SDHC_SDXC_CARD;//SDHC or SDXC
  1247. }
  1248. return MCI_FUNC_OK; /* Found the card, it's a hD */
  1249. }
  1250. }
  1251. if(retval != MCI_FUNC_OK) /* voltage mismatch (ver2.00)or ver1.X SD Card or not SD Card*/
  1252. {
  1253. //Check in case of Standard Capacity Supporting Host
  1254. if ((retval = MCI_Acmd_SendOpCond(0)) == MCI_FUNC_OK)
  1255. {
  1256. MCI_CardType = MCI_SDSC_V1_CARD;//Support Standard Capacity only
  1257. return MCI_FUNC_OK; /* Found the card, it's a SD */
  1258. }
  1259. }
  1260. if(retval != MCI_FUNC_OK)
  1261. {
  1262. /* Set Open Drain output control for MMC */
  1263. MCI_SetOutputMode(MCI_OUTPUT_MODE_OPENDRAIN);
  1264. for ( i = 0; i < 0x3000; i++ );
  1265. /* Try CMD1 first for MMC, if it's timeout, try CMD55 and CMD41 for SD,
  1266. if both failed, initialization faIlure, bailout. */
  1267. if (MCI_Cmd_SendOpCond() == MCI_FUNC_OK)
  1268. {
  1269. MCI_CardType = MCI_MMC_CARD;
  1270. return MCI_FUNC_OK; /* Found the card, it's a MMC */
  1271. }
  1272. }
  1273. /* tried both MMC and SD card, give up */
  1274. return MCI_FUNC_OK;
  1275. }
  1276. /************************************************************************//**
  1277. * @brief Get the type of card that is currently used in the slot
  1278. *
  1279. * @param None
  1280. *
  1281. * @return Card Type: MMC Card or SD card
  1282. ****************************************************************************/
  1283. en_Mci_CardType MCI_GetCardType(void)
  1284. {
  1285. return MCI_CardType;
  1286. }
  1287. /************************************************************************//**
  1288. * @brief Get the all the Identifier (CID) of the card by sending the
  1289. * CMD2 (ALL_SEND_CID) command. Then parse 4-byte data obtained
  1290. * from the card by the CID meaning.
  1291. *
  1292. * @param[out] cidValue the CID Result after parsing the data from the card
  1293. *
  1294. * @return MCI_FUNC_OK if all success
  1295. ****************************************************************************/
  1296. int32_t MCI_GetCID(st_Mci_CardId* cidValue)
  1297. {
  1298. volatile uint32_t i;
  1299. uint32_t retryCount;
  1300. uint32_t respStatus;
  1301. uint32_t respValue[4];
  1302. st_Mci_CmdInfo cmdIf;
  1303. /* This command is normally after CMD1(MMC) or ACMD41(SD). */
  1304. retryCount = 0x200;// 0x20; /* reset retry counter */
  1305. cmdIf.CmdIndex = CMD2_ALL_SEND_CID;
  1306. cmdIf.Argument = 0;
  1307. cmdIf.ExpectResp = EXPECT_LONG_RESP;
  1308. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1309. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1310. while ( retryCount > 0 )
  1311. {
  1312. respStatus = MCI_CmdResp(&cmdIf);
  1313. /* bit 0 and bit 2 must be zero, or it's timeout or CRC error */
  1314. //if ((!(respStatus & MCI_CMD_TIMEOUT)) && (!(respStatus & MCI_CMD_CRC_FAIL)))
  1315. if (!(respStatus & MCI_CMD_TIMEOUT))
  1316. {
  1317. // Parsing the data retrieved
  1318. if(cidValue != NULL)
  1319. {
  1320. cidValue->MID = (respValue[0] >> MCI_CID_MANUFACTURER_ID_WPOS) & MCI_CID_MANUFACTURER_ID_WBMASK;
  1321. cidValue->OID = (respValue[0] >> MCI_CID_OEMAPPLICATION_ID_WPOS) & MCI_CID_OEMAPPLICATION_ID_WBMASK;
  1322. cidValue->PNM_H = (respValue[0] >> MCI_CID_PRODUCTNAME_ID_H_WPOS) & MCI_CID_PRODUCTNAME_ID_H_WBMASK;
  1323. cidValue->PNM_L = (respValue[1] >> MCI_CID_PRODUCTNAME_ID_L_WPOS) & MCI_CID_PRODUCTNAME_ID_L_WBMASK;
  1324. cidValue->PRV = (respValue[2] >> MCI_CID_PRODUCTREVISION_ID_WPOS) & MCI_CID_PRODUCTREVISION_ID_WBMASK;
  1325. cidValue->PSN = (((respValue[2] >> MCI_CID_PRODUCTSERIALNUM_ID_H_WPOS) & MCI_CID_PRODUCTSERIALNUM_ID_H_WBMASK) << 8)
  1326. | ((respValue[3] >> MCI_CID_PRODUCTSERIALNUM_ID_L_WPOS) & MCI_CID_PRODUCTSERIALNUM_ID_L_WBMASK);
  1327. cidValue->reserved = (respValue[3] >> MCI_CID_RESERVED_ID_WPOS) & MCI_CID_RESERVED_ID_WBMASK;
  1328. cidValue->MDT = (respValue[3] >> MCI_CID_MANUFACTURINGDATE_ID_WPOS) & MCI_CID_MANUFACTURINGDATE_ID_WBMASK;
  1329. cidValue->CRC = (respValue[3] >> MCI_CID_CHECKSUM_ID_WPOS) & MCI_CID_CHECKSUM_ID_WBMASK;
  1330. cidValue->unused = (respValue[3] >> MCI_CID_UNUSED_ID_WPOS) & MCI_CID_UNUSED_ID_WBMASK;
  1331. }
  1332. return MCI_FUNC_OK; /* response is back and correct. */
  1333. }
  1334. for ( i = 0; i < 0x20; i++ );
  1335. retryCount--;
  1336. }
  1337. return MCI_FUNC_TIMEOUT;
  1338. }
  1339. /************************************************************************//**
  1340. * @brief Set the address for the card in the slot by sending CMD3
  1341. * (SET_RELATIVE_ADDR) command. To get the address of the card
  1342. * currently in used, needs to call MCI_GetCardAddress()
  1343. *
  1344. * @param None
  1345. *
  1346. * @return MCI_FUNC_OK if all success
  1347. ****************************************************************************/
  1348. int32_t MCI_SetCardAddress( void )
  1349. {
  1350. volatile uint32_t i;
  1351. uint32_t retryCount;
  1352. uint32_t respStatus;
  1353. uint32_t respValue;
  1354. uint32_t CmdArgument;
  1355. st_Mci_CmdInfo cmdIf;
  1356. int32_t retval = MCI_FUNC_FAILED;
  1357. /* If it's a SD card, SET_RELATIVE_ADDR is to get the address
  1358. from the card and use this value in RCA, if it's a MMC, set default
  1359. RCA addr. 0x00010000. */
  1360. if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
  1361. (MCI_CardType == MCI_SDSC_V2_CARD) ||
  1362. (MCI_CardType == MCI_SDHC_SDXC_CARD))
  1363. {
  1364. CmdArgument = 0;
  1365. }
  1366. else /* If it's unknown or MMC_CARD, fix the RCA address */
  1367. {
  1368. CmdArgument = 0x00010000;
  1369. }
  1370. retryCount = 0x20; /* reset retry counter */
  1371. cmdIf.CmdIndex = CMD3_SET_RELATIVE_ADDR;
  1372. cmdIf.Argument = CmdArgument;
  1373. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1374. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1375. cmdIf.CmdResp = &respValue;
  1376. while ( retryCount > 0 )
  1377. {
  1378. /* Send CMD3 command repeatedly until the response is back correctly */
  1379. respStatus = MCI_CmdResp(&cmdIf);
  1380. if(respStatus & MCI_CMD_TIMEOUT)
  1381. {
  1382. retval = MCI_FUNC_TIMEOUT;
  1383. }
  1384. else if(!((respValue >> RCA_RES_CARD_STATUS_POS)& CARD_STATUS_READY_FOR_DATA))
  1385. {
  1386. retval = MCI_FUNC_NOT_READY;
  1387. }
  1388. else if((CARDSTATEOF(respValue) != MCI_CARDSTATE_IDENDTIFIED))
  1389. {
  1390. retval = MCI_FUNC_ERR_STATE;
  1391. }
  1392. else
  1393. {
  1394. CardRCA = (respValue >> RCA_RES_NEW_PUBLISHED_RCA_POS) & RCA_RES_NEW_PUBLISHED_RCA_MASK; /* Save the RCA value from SD card */
  1395. CardRCA <<= RCA_ARGUMENT_POS;
  1396. MCI_SetOutputMode(MCI_OUTPUT_MODE_PUSHPULL);
  1397. return (MCI_FUNC_OK); /* response is back and correct. */
  1398. }
  1399. for ( i = 0; i < 0x20; i++ );
  1400. retryCount--;
  1401. }
  1402. return retval;
  1403. }
  1404. /************************************************************************//**
  1405. * @brief Get the address for the card in the slot
  1406. *
  1407. * @param None
  1408. *
  1409. * @return MCI_FUNC_OK if all success
  1410. *
  1411. * @note This function must be called after MCI_SetCardAddress() executing
  1412. ****************************************************************************/
  1413. uint32_t MCI_GetCardAddress(void)
  1414. {
  1415. return CardRCA;
  1416. }
  1417. /************************************************************************//**
  1418. * @brief Get the Card-Specific Data of in-slot card by sending CMD9
  1419. * (SEND_CSD) command
  1420. *
  1421. * @param[out] csdVal a buffer stored the value of CSD obtained from the card
  1422. *
  1423. * @return MCI_FUNC_OK if all success
  1424. *
  1425. * @note CMD9 (SEND_CSD) command should be sent only at standby state
  1426. * (STBY) after CMD3
  1427. ****************************************************************************/
  1428. int32_t MCI_GetCSD(uint32_t* csdVal)
  1429. {
  1430. volatile uint32_t i;
  1431. uint32_t retryCount;
  1432. uint32_t respStatus;
  1433. uint32_t respValue[4];
  1434. uint32_t CmdArgument;
  1435. st_Mci_CmdInfo cmdIf;
  1436. if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
  1437. (MCI_CardType == MCI_SDSC_V2_CARD) ||
  1438. (MCI_CardType == MCI_SDHC_SDXC_CARD))
  1439. {
  1440. CmdArgument = CardRCA;
  1441. }
  1442. else /* if MMC or unknown card type, use default RCA addr. */
  1443. {
  1444. CmdArgument = 0x00010000;
  1445. }
  1446. retryCount = 0x20;
  1447. cmdIf.CmdIndex = CMD9_SEND_CSD;
  1448. cmdIf.Argument = CmdArgument;
  1449. cmdIf.ExpectResp = EXPECT_LONG_RESP;
  1450. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1451. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1452. while ( retryCount > 0 )
  1453. {
  1454. /* Check current status */
  1455. if(((MCI_CheckStatus(CARD_STATE_STBY) != MCI_FUNC_OK)))
  1456. return MCI_FUNC_ERR_STATE;
  1457. respStatus = MCI_CmdResp(&cmdIf);
  1458. if ( !respStatus )
  1459. {
  1460. if(csdVal != NULL)
  1461. {
  1462. csdVal[0] = respValue[0];
  1463. csdVal[1] = respValue[1];
  1464. csdVal[2] = respValue[2];
  1465. csdVal[3] = respValue[3];
  1466. }
  1467. return (MCI_FUNC_OK);
  1468. }
  1469. for ( i = 0; i < 0x20; i++ );
  1470. retryCount--;
  1471. }
  1472. return (MCI_FUNC_FAILED);
  1473. }
  1474. /************************************************************************//**
  1475. * @brief Select the card by the specified address. This is done by sending
  1476. * out the CMD7 with the address argument to needed card
  1477. *
  1478. * @param None
  1479. *
  1480. * @return MCI_FUNC_OK if all success
  1481. *
  1482. * @note CMD7 (SELECT_CARD) command should be sent after CMD9 ((SEND_CSD).
  1483. * The state will be inter-changed between STBY to TRANS by this
  1484. * CMD7 command
  1485. ****************************************************************************/
  1486. int32_t MCI_Cmd_SelectCard( void )
  1487. {
  1488. volatile uint32_t i;
  1489. uint32_t retryCount;
  1490. uint32_t respStatus;
  1491. uint32_t respValue[4];
  1492. uint32_t CmdArgument;
  1493. st_Mci_CmdInfo cmdIf;
  1494. int32_t retval = MCI_FUNC_FAILED;
  1495. if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
  1496. (MCI_CardType == MCI_SDSC_V2_CARD) ||
  1497. (MCI_CardType == MCI_SDHC_SDXC_CARD))
  1498. {
  1499. CmdArgument = CardRCA;
  1500. }
  1501. else /* if MMC or unknown card type, use default RCA addr. */
  1502. {
  1503. CmdArgument = 0x00010000;
  1504. }
  1505. retryCount = 0x20;
  1506. cmdIf.CmdIndex = CMD7_SELECT_CARD;
  1507. cmdIf.Argument = CmdArgument;
  1508. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1509. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1510. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1511. while ( retryCount > 0 )
  1512. {
  1513. /* Check current status */
  1514. if(((MCI_CheckStatus(CARD_STATE_STBY) != MCI_FUNC_OK)) &&
  1515. (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK) &&
  1516. (MCI_CheckStatus(CARD_STATE_DATA) != MCI_FUNC_OK) &&
  1517. (MCI_CheckStatus(CARD_STATE_PRG) != MCI_FUNC_OK) &&
  1518. (MCI_CheckStatus(CARD_STATE_DIS) != MCI_FUNC_OK))
  1519. return MCI_FUNC_ERR_STATE;
  1520. respStatus = MCI_CmdResp(&cmdIf);
  1521. if(respStatus)
  1522. {
  1523. retval = MCI_FUNC_FAILED;
  1524. }
  1525. else if (respValue[0] & CARD_STATUS_ERR_MASK)
  1526. {
  1527. return MCI_FUNC_BAD_PARAMETERS;
  1528. }
  1529. else
  1530. {
  1531. if(((MCI_CheckStatus(CARD_STATE_STBY) != MCI_FUNC_OK)) &&
  1532. (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK) &&
  1533. (MCI_CheckStatus(CARD_STATE_PRG) != MCI_FUNC_OK) &&
  1534. (MCI_CheckStatus(CARD_STATE_DIS) != MCI_FUNC_OK))
  1535. return MCI_FUNC_ERR_STATE;
  1536. return MCI_FUNC_OK;
  1537. }
  1538. for ( i = 0; i < 0x20; i++ );
  1539. retryCount--;
  1540. }
  1541. return retval;
  1542. }
  1543. /************************************************************************//**
  1544. * @brief Get the status of the card. The return is from the card.
  1545. * By sending CMD13 (SEND_STATUS), the status of the card
  1546. * will be responded from card addressed
  1547. *
  1548. * @param[out] cardStatus the status returned from the card
  1549. *
  1550. * @return MCI_FUNC_OK if all success
  1551. ****************************************************************************/
  1552. int32_t MCI_GetCardStatus(int32_t* cardStatus)
  1553. {
  1554. volatile uint32_t i;
  1555. uint32_t retryCount;
  1556. uint32_t respStatus;
  1557. uint32_t respValue[4];
  1558. uint32_t CmdArgument;
  1559. st_Mci_CmdInfo cmdIf;
  1560. int32_t retval = MCI_FUNC_FAILED;
  1561. if(cardStatus == NULL)
  1562. return MCI_FUNC_OK;
  1563. if ((MCI_CardType == MCI_SDSC_V1_CARD) ||
  1564. (MCI_CardType == MCI_SDSC_V2_CARD) ||
  1565. (MCI_CardType == MCI_SDHC_SDXC_CARD))
  1566. {
  1567. CmdArgument = CardRCA;
  1568. }
  1569. else /* if MMC or unknown card type, use default RCA addr. */
  1570. {
  1571. CmdArgument = 0x00010000;
  1572. }
  1573. retryCount = 0x20;
  1574. cmdIf.CmdIndex = CMD13_SEND_STATUS;
  1575. cmdIf.Argument = CmdArgument;
  1576. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1577. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1578. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1579. while ( retryCount > 0 )
  1580. {
  1581. respStatus = MCI_CmdResp(&cmdIf);
  1582. if(respStatus) /* only retry if sending command failed */
  1583. {
  1584. retval = MCI_FUNC_FAILED;
  1585. }
  1586. else
  1587. {
  1588. *cardStatus = respValue[0];
  1589. return MCI_FUNC_OK;
  1590. }
  1591. retryCount--;
  1592. for ( i = 0; i < 0x10; i++ );
  1593. }
  1594. return retval;
  1595. }
  1596. /************************************************************************//**
  1597. * @brief Set the length for the blocks in the next action on data
  1598. * manipulation (as read, write, erase). This function is to
  1599. * send CMD16 (SET_BLOCK_LEN) to cards.
  1600. *
  1601. * @param[in] blockLength the value for the length of block will be handled
  1602. *
  1603. * @return MCI_FUNC_OK if all success
  1604. *
  1605. * @note CMD16 command should be sent after the card is selected by CMD7
  1606. * (SELECT_CARD).
  1607. * In the case of SDHC and SDXC Cards, block length set by CMD16 command doen't
  1608. * affect memory read and write commands. Always 512 Bytes fixed block length is
  1609. * used. This command is effective for LOCK_UNLOCK command..
  1610. ****************************************************************************/
  1611. int32_t MCI_SetBlockLen(uint32_t blockLength)
  1612. {
  1613. volatile uint32_t i;
  1614. uint32_t retryCount;
  1615. uint32_t respStatus;
  1616. uint32_t respValue[4];
  1617. st_Mci_CmdInfo cmdIf;
  1618. int32_t retval = MCI_FUNC_FAILED;
  1619. retryCount = 0x20;
  1620. cmdIf.CmdIndex = CMD16_SET_BLOCK_LEN;
  1621. cmdIf.Argument = blockLength;
  1622. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1623. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1624. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1625. while ( retryCount > 0 )
  1626. {
  1627. /* Check current status */
  1628. if((MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK) )
  1629. return MCI_FUNC_ERR_STATE;
  1630. respStatus = MCI_CmdResp(&cmdIf);
  1631. if(respStatus)
  1632. {
  1633. retval = MCI_FUNC_FAILED;
  1634. }
  1635. else if (respValue[0] & CARD_STATUS_ERR_MASK)
  1636. {
  1637. return MCI_FUNC_BAD_PARAMETERS;
  1638. }
  1639. else
  1640. {
  1641. return MCI_CheckStatus(CARD_STATE_TRAN);
  1642. }
  1643. for ( i = 0; i < 0x20; i++ );
  1644. retryCount--;
  1645. }
  1646. return retval;
  1647. }
  1648. /************************************************************************//**
  1649. * @brief Set bus-width (1 bit or 4 bit) to work with the card by command
  1650. * CMD6 (SET_ACMD_BUS_WIDTH).
  1651. *
  1652. * @param[in] buswidth The value represented for bus-width
  1653. * - 0b00: 1-bit bus-width
  1654. * - 0b10: 4-bit bus-width
  1655. *
  1656. * @return MCI_FUNC_OK if all success
  1657. *
  1658. * @note
  1659. * - If SD card is currently in used, it's possible to enable 4-bit
  1660. * bus-width instead of 1-bit to speed up.
  1661. * - This command can only be transferred during TRANS state.
  1662. * - Since, it's a ACMD, CMD55 (APP_CMD) needs to be sent out first
  1663. ****************************************************************************/
  1664. int32_t MCI_Acmd_SendBusWidth( uint32_t buswidth )
  1665. {
  1666. volatile uint32_t i;
  1667. uint32_t retryCount;
  1668. uint32_t respStatus;
  1669. uint32_t respValue[4];
  1670. st_Mci_CmdInfo cmdIf;
  1671. int32_t retval = MCI_FUNC_FAILED;
  1672. retryCount = 0x20; /* reset retry counter */
  1673. cmdIf.CmdIndex = ACMD6_SET_BUS_WIDTH;
  1674. cmdIf.Argument = buswidth;
  1675. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1676. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1677. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1678. while ( retryCount > 0 )
  1679. {
  1680. /* The card must be in tran state in order to change the bus width */
  1681. retval = MCI_CheckStatus(CARD_STATE_TRAN);
  1682. if(retval!= MCI_FUNC_OK)
  1683. return retval;
  1684. if (MCI_Cmd_SendACMD() == MCI_FUNC_OK)
  1685. {
  1686. respStatus = MCI_CmdResp(&cmdIf);
  1687. if(respStatus)
  1688. {
  1689. retval = MCI_FUNC_FAILED;
  1690. }
  1691. else if (respValue[0] & CARD_STATUS_ERR_MASK)
  1692. {
  1693. return MCI_FUNC_BAD_PARAMETERS;
  1694. }
  1695. else
  1696. {
  1697. return MCI_CheckStatus(CARD_STATE_TRAN);
  1698. }
  1699. }
  1700. for ( i = 0; i < 0x20; i++ );
  1701. retryCount--;
  1702. }
  1703. return retval;
  1704. }
  1705. /************************************************************************//**
  1706. * @brief Get the state of data transfer to see if it is ended or not
  1707. *
  1708. * @param None
  1709. *
  1710. * @return Transfer state (stored by Mci_Data_Xfer_End variable)
  1711. ****************************************************************************/
  1712. uint32_t MCI_GetDataXferEndState(void)
  1713. {
  1714. return Mci_Data_Xfer_End;
  1715. }
  1716. /************************************************************************//**
  1717. * @brief Get the error state of the lastest data transfer
  1718. *
  1719. * @param None
  1720. *
  1721. * @return Error state (stored by Mci_Data_Xfer_ERR variable)
  1722. ****************************************************************************/
  1723. uint32_t MCI_GetXferErrState(void)
  1724. {
  1725. return Mci_Data_Xfer_ERR;
  1726. }
  1727. /************************************************************************//**
  1728. * @brief Stop the current transmission on the bus by sending command CMD12
  1729. * (STOP_TRANSMISSION). In this case, the card may be in a unknown
  1730. * state. So that it need a warm reset for normal operation.
  1731. *
  1732. * @param[in] None
  1733. *
  1734. * @return MCI_FUNC_OK if all success
  1735. ****************************************************************************/
  1736. int32_t MCI_Cmd_StopTransmission( void )
  1737. {
  1738. volatile uint32_t i;
  1739. uint32_t retryCount;
  1740. uint32_t respStatus;
  1741. uint32_t respValue[4];
  1742. st_Mci_CmdInfo cmdIf;
  1743. int32_t retval = MCI_FUNC_FAILED;
  1744. /* do nothing when the card is in tran state */
  1745. if(MCI_CheckStatus(CARD_STATE_TRAN) == MCI_FUNC_OK)
  1746. {
  1747. return MCI_FUNC_OK;
  1748. }
  1749. retryCount = 0x20;
  1750. cmdIf.CmdIndex = CMD12_STOP_TRANSMISSION;
  1751. cmdIf.Argument = 0x00000000;
  1752. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1753. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1754. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1755. while ( retryCount > 0 )
  1756. {
  1757. /* Check current status */
  1758. if((MCI_CheckStatus(CARD_STATE_DATA) != MCI_FUNC_OK) &&
  1759. (MCI_CheckStatus(CARD_STATE_RCV) != MCI_FUNC_OK))
  1760. return MCI_FUNC_ERR_STATE;
  1761. respStatus = MCI_CmdResp(&cmdIf);
  1762. if(respStatus)
  1763. {
  1764. retval = MCI_FUNC_FAILED;
  1765. }
  1766. else if (respValue[0] & CARD_STATUS_ERR_MASK)
  1767. {
  1768. return MCI_FUNC_BAD_PARAMETERS;
  1769. }
  1770. else
  1771. {
  1772. if((MCI_CheckStatus(CARD_STATE_PRG) != MCI_FUNC_OK) &&
  1773. (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
  1774. return MCI_FUNC_ERR_STATE;
  1775. return MCI_FUNC_OK;
  1776. }
  1777. for ( i = 0; i < 0x20; i++ );
  1778. retryCount--;
  1779. }
  1780. return retval;
  1781. }
  1782. /************************************************************************//**
  1783. * @brief Write blocks to card by sending command CMD24 (WRITE_BLOCK) or
  1784. * command CMD25 (WRITE_MULTIPLE_BLOCK) followed by the blocks of
  1785. * data to be written.
  1786. *
  1787. * @param[in] blockNum The block number to start writting
  1788. *
  1789. * @param[in] numOfBlock Determine how many blocks will be written (from the
  1790. * starting block)
  1791. *
  1792. * @return MCI_FUNC_OK if all success
  1793. *
  1794. * @note These commands should be sent in TRANS state.
  1795. ****************************************************************************/
  1796. int32_t MCI_Cmd_WriteBlock(uint32_t blockNum, uint32_t numOfBlock)
  1797. {
  1798. volatile uint32_t i;
  1799. uint32_t retryCount;
  1800. uint32_t respStatus;
  1801. uint32_t respValue[4];
  1802. st_Mci_CmdInfo cmdIf;
  1803. uint32_t commandID;
  1804. int32_t retval = MCI_FUNC_FAILED;
  1805. if (numOfBlock > 1)
  1806. {
  1807. commandID = CMD25_WRITE_MULTIPLE_BLOCK;
  1808. }
  1809. else
  1810. {
  1811. commandID = CMD24_WRITE_BLOCK;
  1812. }
  1813. retryCount = 0x20;
  1814. cmdIf.CmdIndex = commandID;
  1815. if (MCI_CardType == MCI_SDHC_SDXC_CARD)
  1816. {
  1817. cmdIf.Argument = blockNum; /* Block Address */
  1818. }
  1819. else
  1820. {
  1821. cmdIf.Argument = blockNum * BLOCK_LENGTH; /* Byte Address */
  1822. }
  1823. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1824. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1825. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1826. while ( retryCount > 0 )
  1827. {
  1828. /* Check current status */
  1829. if((MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
  1830. return MCI_FUNC_ERR_STATE;
  1831. respStatus = MCI_CmdResp(&cmdIf);
  1832. if(respStatus)
  1833. {
  1834. retval = MCI_FUNC_FAILED;
  1835. }
  1836. else if (respValue[0] & CARD_STATUS_ERR_MASK)
  1837. {
  1838. return MCI_FUNC_BAD_PARAMETERS;
  1839. }
  1840. else
  1841. {
  1842. if((MCI_CheckStatus(CARD_STATE_RCV) != MCI_FUNC_OK) &&
  1843. (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
  1844. return MCI_FUNC_ERR_STATE;
  1845. return MCI_FUNC_OK;
  1846. }
  1847. for ( i = 0; i < 0x20; i++ );
  1848. retryCount--;
  1849. }
  1850. return retval; /* Fatal error */
  1851. }
  1852. /************************************************************************//**
  1853. * @brief Read blocks to card by sending CMD17 (READ_SINGLE_BLOCK) or
  1854. * CMD18 (READ_MULTIPLE_BLOCK) commands followed by the blocks of
  1855. * data to be read.
  1856. *
  1857. * @param[in] blockNum The block number to start reading
  1858. *
  1859. * @param[in] numOfBlock Determine how many blocks will be read (from the
  1860. * starting block)
  1861. *
  1862. * @return MCI_FUNC_OK if all success
  1863. *
  1864. * @note These commands should be sent in TRANS state.
  1865. ****************************************************************************/
  1866. int32_t MCI_Cmd_ReadBlock(uint32_t blockNum, uint32_t numOfBlock)
  1867. {
  1868. volatile uint32_t i;
  1869. uint32_t retryCount;
  1870. uint32_t respStatus;
  1871. uint32_t respValue[4];
  1872. uint32_t commandID;
  1873. st_Mci_CmdInfo cmdIf;
  1874. int32_t retval = MCI_FUNC_FAILED;
  1875. // To Do: Read Multi-Block
  1876. if (numOfBlock > 1)
  1877. commandID = CMD18_READ_MULTIPLE_BLOCK;
  1878. else
  1879. commandID = CMD17_READ_SINGLE_BLOCK;
  1880. retryCount = 0x20;
  1881. cmdIf.CmdIndex = commandID;
  1882. if (MCI_CardType == MCI_SDHC_SDXC_CARD)
  1883. {
  1884. cmdIf.Argument = blockNum; /* Block Address */
  1885. }
  1886. else
  1887. {
  1888. cmdIf.Argument = blockNum * BLOCK_LENGTH; /* Byte Address */
  1889. }
  1890. cmdIf.ExpectResp = EXPECT_SHORT_RESP;
  1891. cmdIf.AllowTimeout = ALLOW_CMD_TIMER;
  1892. cmdIf.CmdResp = (uint32_t *)&respValue[0];
  1893. while ( retryCount > 0 )
  1894. {
  1895. /* Check current status */
  1896. if((MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
  1897. return MCI_FUNC_ERR_STATE;
  1898. respStatus = MCI_CmdResp(&cmdIf);
  1899. if(respStatus)
  1900. {
  1901. retval = MCI_FUNC_FAILED;
  1902. }
  1903. else if (respValue[0] & CARD_STATUS_ERR_MASK)
  1904. {
  1905. return MCI_FUNC_BAD_PARAMETERS;
  1906. }
  1907. else
  1908. {
  1909. if((MCI_CheckStatus(CARD_STATE_DATA) != MCI_FUNC_OK) &&
  1910. (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK))
  1911. return MCI_FUNC_ERR_STATE;
  1912. return MCI_FUNC_OK;
  1913. }
  1914. for ( i = 0; i < 0x20; i++ );
  1915. retryCount--;
  1916. }
  1917. return retval;
  1918. }
  1919. /************************************************************************//**
  1920. * @brief Write data at a specific address to starting block with number
  1921. * of blocks will be written from first block
  1922. * @details
  1923. * - At preparation
  1924. * - Set MCI data control register, data length and data timeout
  1925. * - Send CMD24 (WRITE_BLOCK) or CMD25 (WRITE_MULTIPLE_BLOCK)
  1926. * commands to card
  1927. * - Enable interupt for MCI component
  1928. * - At completion
  1929. * - TX_ACTIVE interrupt is occured
  1930. * - Write data to FIFO register continuously until the data block
  1931. * length is reached
  1932. *
  1933. * @param[in] *memblock The pointer to location stored required data to be
  1934. * written to card
  1935. *
  1936. * @param[in] blockNum The block number to start writting
  1937. *
  1938. * @param[in] numOfBlock Determine how many blocks will be written (from the
  1939. * starting block)
  1940. *
  1941. * @return MCI_FUNC_OK if all success
  1942. ****************************************************************************/
  1943. int32_t MCI_WriteBlock(volatile uint8_t* memblock, uint32_t blockNum, uint32_t numOfBlock)
  1944. {
  1945. volatile uint32_t i;
  1946. uint32_t DataCtrl = 0;
  1947. if(BLOCK_LENGTH*numOfBlock > DATA_RW_MAX_LEN)
  1948. return MCI_FUNC_BAD_PARAMETERS;
  1949. dataSrcBlock = memblock;
  1950. LPC_MCI->CLEAR = 0x7FF;
  1951. LPC_MCI->DATACTRL = 0;
  1952. for ( i = 0; i < 0x10; i++ );
  1953. /* Wait the SD Card enters to TRANS state. */
  1954. if (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK)
  1955. return MCI_FUNC_ERR_STATE;
  1956. LPC_MCI->DATATMR = DATA_TIMER_VALUE_W;
  1957. LPC_MCI->DATALEN = BLOCK_LENGTH*numOfBlock;
  1958. Mci_Data_Xfer_End = 1;
  1959. Mci_Data_Xfer_ERR = 0;
  1960. fifo_plane = 0;
  1961. txBlockCnt = 0;
  1962. MCI_TXEnable();
  1963. if (MCI_Cmd_WriteBlock(blockNum, numOfBlock) != MCI_FUNC_OK)
  1964. {
  1965. return ( MCI_FUNC_FAILED );
  1966. }
  1967. //for(blockCnt = 0; blockCnt < numOfBlock; blockCnt++)
  1968. {
  1969. #if MCI_DMA_ENABLED
  1970. MCI_SettingDma((uint8_t*) dataSrcBlock, MCI_DMA_WRITE_CHANNEL, GPDMA_TRANSFERTYPE_M2P_DEST_CTRL);
  1971. /* Write, block transfer, DMA, and data length */
  1972. DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_TO_CARD
  1973. | MCI_DATACTRL_DMA_ENABLE | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
  1974. #else
  1975. /* Write, block transfer, and data length */
  1976. DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_TO_CARD | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
  1977. #endif
  1978. }
  1979. LPC_MCI->DATACTRL = DataCtrl;
  1980. for ( i = 0; i < 0x10; i++ );
  1981. return MCI_FUNC_OK;
  1982. }
  1983. /************************************************************************//**
  1984. * @brief Read data at a specific address to starting block with number
  1985. * of blocks will be read from first block
  1986. *
  1987. * @details
  1988. * - At preparation
  1989. * - Set MCI data control register, data length and data timeout
  1990. * - Send CMD17 (READ_SINGLE_BLOCK) or CMD18 (READ_MULTIPLE_BLOCK)
  1991. * commands to card
  1992. * - Enable interupt for MCI component
  1993. * - At completion
  1994. * - RX_ACTIVE interrupt is occured
  1995. * - Read data from FIFO register continuously until the data block
  1996. * length is reached to retrieve needed data
  1997. *
  1998. * @param[in] *destBlock The pointer to location will captured data read
  1999. * from card
  2000. *
  2001. * @param[in] blockNum The block number to start reading
  2002. *
  2003. * @param[in] numOfBlock Determine how many blocks will be read (from the
  2004. * starting block)
  2005. *
  2006. * @return MCI_FUNC_OK if all success
  2007. ****************************************************************************/
  2008. int32_t MCI_ReadBlock(volatile uint8_t* destBlock, uint32_t blockNum, uint32_t numOfBlock)
  2009. {
  2010. volatile uint32_t i;
  2011. uint32_t DataCtrl = 0;
  2012. if(BLOCK_LENGTH*numOfBlock > DATA_RW_MAX_LEN)
  2013. return MCI_FUNC_BAD_PARAMETERS;
  2014. dataDestBlock = destBlock;
  2015. LPC_MCI->CLEAR = 0x7FF;
  2016. LPC_MCI->DATACTRL = 0;
  2017. for ( i = 0; i < 0x10; i++ );
  2018. /* Wait the SD Card enters to TRANS state. */
  2019. if (MCI_CheckStatus(CARD_STATE_TRAN) != MCI_FUNC_OK)
  2020. return MCI_FUNC_ERR_STATE;
  2021. MCI_RXEnable();
  2022. LPC_MCI->DATATMR = DATA_TIMER_VALUE_R;
  2023. LPC_MCI->DATALEN = BLOCK_LENGTH*numOfBlock;
  2024. Mci_Data_Xfer_End = 1;
  2025. Mci_Data_Xfer_ERR = 0;
  2026. rxBlockCnt = 0;
  2027. fifo_plane = 0;
  2028. // Start data engine on READ before command to avoid overflow of the FIFO.
  2029. {
  2030. #if MCI_DMA_ENABLED
  2031. MCI_SettingDma((uint8_t*) dataDestBlock, MCI_DMA_READ_CHANNEL, GPDMA_TRANSFERTYPE_P2M_SRC_CTRL);
  2032. /* Write, block transfer, DMA, and data length */
  2033. DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_FROM_CARD
  2034. | MCI_DATACTRL_DMA_ENABLE | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
  2035. #else
  2036. //Retrieving the result after reading the card is done by the FIFO handling for interrupt
  2037. /* Read, enable, block transfer, and data length */
  2038. DataCtrl |= MCI_DATACTRL_ENABLE | MCI_DATACTRL_DIR_FROM_CARD | MCI_DTATCTRL_BLOCKSIZE(DATA_BLOCK_LEN);
  2039. #endif
  2040. }
  2041. LPC_MCI->DATACTRL = DataCtrl;
  2042. for ( i = 0; i < 0x10; i++ );
  2043. if ( MCI_Cmd_ReadBlock(blockNum, numOfBlock) != MCI_FUNC_OK )
  2044. {
  2045. return MCI_FUNC_FAILED;
  2046. }
  2047. return MCI_FUNC_OK;
  2048. }
  2049. /************************************************************************//**
  2050. * @brief Turn off the MCI power by disabling the Power Register for MCI
  2051. *
  2052. * @param None
  2053. *
  2054. * @return None
  2055. ****************************************************************************/
  2056. void MCI_PowerOff(void)
  2057. {
  2058. volatile uint32_t i;
  2059. LPC_MCI->POWER = 0;
  2060. for (i = 0; i < 0x100; i++);
  2061. return;
  2062. }
  2063. /**
  2064. * @}
  2065. */
  2066. #endif /*_MCI*/
  2067. /*****************************************************************************
  2068. ** End Of File
  2069. ******************************************************************************/