lpc177x_8x_mci.c 75 KB

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