sdcard.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042
  1. /**
  2. ******************************************************************************
  3. * @file stm324xg_eval_sdio_sd.c
  4. * @author MCD Application Team
  5. * @version V1.0.2
  6. * @date 09-March-2012
  7. * @brief This file provides a set of functions needed to manage the SDIO SD
  8. * Card memory mounted on STM324xG-EVAL evaluation board.
  9. *
  10. *
  11. * @verbatim
  12. *
  13. * ===================================================================
  14. * How to use this driver
  15. * ===================================================================
  16. * It implements a high level communication layer for read and write
  17. * from/to this memory. The needed STM32 hardware resources (SDIO and
  18. * GPIO) are defined in stm324xg_eval.h file, and the initialization is
  19. * performed in SD_LowLevel_Init() function declared in stm324xg_eval.c
  20. * file.
  21. * You can easily tailor this driver to any other development board,
  22. * by just adapting the defines for hardware resources and
  23. * SD_LowLevel_Init() function.
  24. *
  25. * A - SD Card Initialization and configuration
  26. * ============================================
  27. * - To initialize the SD Card, use the SD_Init() function. It
  28. * Initializes the SD Card and put it into StandBy State (Ready
  29. * for data transfer). This function provide the following operations:
  30. *
  31. * 1 - Apply the SD Card initialization process at 400KHz and check
  32. * the SD Card type (Standard Capacity or High Capacity). You
  33. * can change or adapt this frequency by adjusting the
  34. * "SDIO_INIT_CLK_DIV" define inside the stm324xg_eval.h file.
  35. * The SD Card frequency (SDIO_CK) is computed as follows:
  36. *
  37. * +---------------------------------------------+
  38. * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
  39. * +---------------------------------------------+
  40. *
  41. * In initialization mode and according to the SD Card standard,
  42. * make sure that the SDIO_CK frequency don't exceed 400KHz.
  43. *
  44. * 2 - Get the SD CID and CSD data. All these information are
  45. * managed by the SDCardInfo structure. This structure provide
  46. * also ready computed SD Card capacity and Block size.
  47. *
  48. * 3 - Configure the SD Card Data transfer frequency. By Default,
  49. * the card transfer frequency is set to 24MHz. You can change
  50. * or adapt this frequency by adjusting the "SDIO_TRANSFER_CLK_DIV"
  51. * define inside the stm324xg_eval.h file.
  52. * The SD Card frequency (SDIO_CK) is computed as follows:
  53. *
  54. * +---------------------------------------------+
  55. * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
  56. * +---------------------------------------------+
  57. *
  58. * In transfer mode and according to the SD Card standard,
  59. * make sure that the SDIO_CK frequency don't exceed 25MHz
  60. * and 50MHz in High-speed mode switch.
  61. * To be able to use a frequency higher than 24MHz, you should
  62. * use the SDIO peripheral in bypass mode. Refer to the
  63. * corresponding reference manual for more details.
  64. *
  65. * 4 - Select the corresponding SD Card according to the address
  66. * read with the step 2.
  67. *
  68. * 5 - Configure the SD Card in wide bus mode: 4-bits data.
  69. *
  70. * B - SD Card Read operation
  71. * ==========================
  72. * - You can read SD card by using two function: SD_ReadBlock() and
  73. * SD_ReadMultiBlocks() functions. These functions support only
  74. * 512-byte block length.
  75. * - The SD_ReadBlock() function read only one block (512-byte). This
  76. * function can transfer the data using DMA controller or using
  77. * polling mode. To select between DMA or polling mode refer to
  78. * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm324xg_eval_sdio_sd.h
  79. * file and uncomment the corresponding line. By default the SD DMA
  80. * mode is selected
  81. * - The SD_ReadMultiBlocks() function read only mutli blocks (multiple
  82. * of 512-byte).
  83. * - Any read operation should be followed by two functions to check
  84. * if the DMA Controller and SD Card status.
  85. * - SD_ReadWaitOperation(): this function insure that the DMA
  86. * controller has finished all data transfer.
  87. * - SD_GetStatus(): to check that the SD Card has finished the
  88. * data transfer and it is ready for data.
  89. *
  90. * - The DMA transfer is finished by the SDIO Data End interrupt.
  91. * User has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler()
  92. * and SD_ProcessDMAIRQ() function inside the DMA2_Streamx_IRQHandler().
  93. * Don't forget to enable the SDIO_IRQn and DMA2_Stream3_IRQn or
  94. * DMA2_Stream6_IRQn interrupts using the NVIC controller.
  95. *
  96. * C - SD Card Write operation
  97. * ===========================
  98. * - You can write SD card by using two function: SD_WriteBlock() and
  99. * SD_WriteMultiBlocks() functions. These functions support only
  100. * 512-byte block length.
  101. * - The SD_WriteBlock() function write only one block (512-byte). This
  102. * function can transfer the data using DMA controller or using
  103. * polling mode. To select between DMA or polling mode refer to
  104. * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm324xg_eval_sdio_sd.h
  105. * file and uncomment the corresponding line. By default the SD DMA
  106. * mode is selected
  107. * - The SD_WriteMultiBlocks() function write only mutli blocks (multiple
  108. * of 512-byte).
  109. * - Any write operation should be followed by two functions to check
  110. * if the DMA Controller and SD Card status.
  111. * - SD_ReadWaitOperation(): this function insure that the DMA
  112. * controller has finished all data transfer.
  113. * - SD_GetStatus(): to check that the SD Card has finished the
  114. * data transfer and it is ready for data.
  115. *
  116. * - The DMA transfer is finished by the SDIO Data End interrupt.
  117. * User has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler()
  118. * and SD_ProcessDMAIRQ() function inside the DMA2_Streamx_IRQHandler().
  119. * Don't forget to enable the SDIO_IRQn and DMA2_Stream3_IRQn or
  120. * DMA2_Stream6_IRQn interrupts using the NVIC controller.
  121. *
  122. *
  123. * D - SD card status
  124. * ==================
  125. * - At any time, you can check the SD Card status and get the SD card
  126. * state by using the SD_GetStatus() function. This function checks
  127. * first if the SD card is still connected and then get the internal
  128. * SD Card transfer state.
  129. * - You can also get the SD card SD Status register by using the
  130. * SD_SendSDStatus() function.
  131. *
  132. * E - Programming Model (Selecting DMA for SDIO data Transfer)
  133. * ============================================================
  134. * Status = SD_Init(); // Initialization Step as described in section A
  135. *
  136. * // SDIO Interrupt ENABLE
  137. * NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  138. * NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  139. * NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  140. * NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  141. * NVIC_Init(&NVIC_InitStructure);
  142. * // DMA2 STREAMx Interrupt ENABLE
  143. * NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
  144. * NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  145. * NVIC_Init(&NVIC_InitStructure);
  146. *
  147. * // Write operation as described in Section C
  148. * Status = SD_WriteBlock(buffer, address, 512);
  149. * Status = SD_WaitWriteOperation();
  150. * while(SD_GetStatus() != SD_TRANSFER_OK);
  151. *
  152. * Status = SD_WriteMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
  153. * Status = SD_WaitWriteOperation();
  154. * while(SD_GetStatus() != SD_TRANSFER_OK);
  155. *
  156. * // Read operation as described in Section B
  157. * Status = SD_ReadBlock(buffer, address, 512);
  158. * Status = SD_WaitReadOperation();
  159. * while(SD_GetStatus() != SD_TRANSFER_OK);
  160. *
  161. * Status = SD_ReadMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
  162. * Status = SD_WaitReadOperation();
  163. * while(SD_GetStatus() != SD_TRANSFER_OK);
  164. *
  165. * - Add the SDIO and DMA2 StreamX (3 or 6) IRQ Handlers:
  166. * void SDIO_IRQHandler(void)
  167. * {
  168. * SD_ProcessIRQ();
  169. * }
  170. * void SD_SDIO_DMA_IRQHANDLER(void)
  171. * {
  172. * SD_ProcessDMAIRQ();
  173. * }
  174. *
  175. * F - Programming Model (Selecting Polling for SDIO data Transfer)
  176. * ================================================================
  177. * //Only SD Card Single Block operation are managed.
  178. * Status = SD_Init(); // Initialization Step as described in section
  179. *
  180. * // Write operation as described in Section C
  181. * Status = SD_WriteBlock(buffer, address, 512);
  182. *
  183. * // Read operation as described in Section B
  184. * Status = SD_ReadBlock(buffer, address, 512);
  185. *
  186. * STM32 SDIO Pin assignment
  187. * =========================
  188. * +-----------------------------------------------------------+
  189. * | Pin assignment |
  190. * +-----------------------------+---------------+-------------+
  191. * | STM32 SDIO Pins | SD | Pin |
  192. * +-----------------------------+---------------+-------------+
  193. * | SDIO D2 | D2 | 1 |
  194. * | SDIO D3 | D3 | 2 |
  195. * | SDIO CMD | CMD | 3 |
  196. * | | VCC | 4 (3.3 V)|
  197. * | SDIO CLK | CLK | 5 |
  198. * | | GND | 6 (0 V) |
  199. * | SDIO D0 | D0 | 7 |
  200. * | SDIO D1 | D1 | 8 |
  201. * +-----------------------------+---------------+-------------+
  202. *
  203. * @endverbatim
  204. *
  205. ******************************************************************************
  206. * @attention
  207. *
  208. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  209. *
  210. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  211. * You may not use this file except in compliance with the License.
  212. * You may obtain a copy of the License at:
  213. *
  214. * http://www.st.com/software_license_agreement_liberty_v2
  215. *
  216. * Unless required by applicable law or agreed to in writing, software
  217. * distributed under the License is distributed on an "AS IS" BASIS,
  218. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  219. * See the License for the specific language governing permissions and
  220. * limitations under the License.
  221. *
  222. ******************************************************************************
  223. */
  224. /* Includes ------------------------------------------------------------------*/
  225. #include "sdcard.h"
  226. /** @addtogroup Utilities
  227. * @{
  228. */
  229. /** @addtogroup STM32_EVAL
  230. * @{
  231. */
  232. /** @addtogroup STM324xG_EVAL
  233. * @{
  234. */
  235. /** @addtogroup STM324xG_EVAL_SDIO_SD
  236. * @brief This file provides all the SD Card driver firmware functions.
  237. * @{
  238. */
  239. /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Types
  240. * @{
  241. */
  242. /**
  243. * @}
  244. */
  245. /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Defines
  246. * @{
  247. */
  248. /**
  249. * @brief SDIO Static flags, TimeOut, FIFO Address
  250. */
  251. #define NULL 0
  252. #define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
  253. #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
  254. /**
  255. * @brief Mask for errors Card Status R1 (OCR Register)
  256. */
  257. #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
  258. #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
  259. #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
  260. #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
  261. #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
  262. #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
  263. #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
  264. #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
  265. #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
  266. #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
  267. #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
  268. #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
  269. #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
  270. #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
  271. #define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000)
  272. #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
  273. #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
  274. #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
  275. #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
  276. #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
  277. /**
  278. * @brief Masks for R6 Response
  279. */
  280. #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
  281. #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
  282. #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
  283. #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
  284. #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
  285. #define SD_STD_CAPACITY ((uint32_t)0x00000000)
  286. #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
  287. #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
  288. #define SD_ALLZERO ((uint32_t)0x00000000)
  289. #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
  290. #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
  291. #define SD_CARD_LOCKED ((uint32_t)0x02000000)
  292. #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
  293. #define SD_0TO7BITS ((uint32_t)0x000000FF)
  294. #define SD_8TO15BITS ((uint32_t)0x0000FF00)
  295. #define SD_16TO23BITS ((uint32_t)0x00FF0000)
  296. #define SD_24TO31BITS ((uint32_t)0xFF000000)
  297. #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
  298. #define SD_HALFFIFO ((uint32_t)0x00000008)
  299. #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
  300. /**
  301. * @brief Command Class Supported
  302. */
  303. #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
  304. #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
  305. #define SD_CCCC_ERASE ((uint32_t)0x00000020)
  306. /**
  307. * @brief Following commands are SD Card Specific commands.
  308. * SDIO_APP_CMD should be sent before sending these commands.
  309. */
  310. #define SDIO_SEND_IF_COND ((uint32_t)0x00000008)
  311. /**
  312. * @}
  313. */
  314. /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Macros
  315. * @{
  316. */
  317. /**
  318. * @}
  319. */
  320. /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Variables
  321. * @{
  322. */
  323. static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1;
  324. static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0;
  325. static uint8_t SDSTATUS_Tab[16];
  326. __IO uint32_t StopCondition = 0;
  327. __IO SD_Error TransferError = SD_OK;
  328. __IO uint32_t TransferEnd = 0, DMAEndOfTransfer = 0;
  329. SD_CardInfo SDCardInfo;
  330. SDIO_InitTypeDef SDIO_InitStructure;
  331. SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
  332. SDIO_DataInitTypeDef SDIO_DataInitStructure;
  333. /**
  334. * @}
  335. */
  336. /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Function_Prototypes
  337. * @{
  338. */
  339. static SD_Error CmdError(void);
  340. static SD_Error CmdResp1Error(uint8_t cmd);
  341. static SD_Error CmdResp7Error(void);
  342. static SD_Error CmdResp3Error(void);
  343. static SD_Error CmdResp2Error(void);
  344. static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca);
  345. static SD_Error SDEnWideBus(FunctionalState NewState);
  346. static SD_Error IsCardProgramming(uint8_t *pstatus);
  347. static SD_Error FindSCR(uint16_t rca, uint32_t *pscr);
  348. uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes);
  349. /**
  350. * @}
  351. */
  352. /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Functions
  353. * @{
  354. */
  355. /**
  356. * @brief DeInitializes the SDIO interface.
  357. * @param None
  358. * @retval None
  359. */
  360. void SD_DeInit(void)
  361. {
  362. SD_LowLevel_DeInit();
  363. }
  364. /**
  365. * @brief Initializes the SD Card and put it into StandBy State (Ready for data
  366. * transfer).
  367. * @param None
  368. * @retval SD_Error: SD Card Error code.
  369. */
  370. SD_Error SD_Init(void)
  371. {
  372. __IO SD_Error errorstatus = SD_OK;
  373. /* 配置NVIC */
  374. SDIO_Interrupts_Config();
  375. /* SDIO Peripheral Low Level Init */
  376. SD_LowLevel_Init();
  377. SDIO_DeInit();
  378. errorstatus = SD_PowerON();
  379. if (errorstatus != SD_OK)
  380. {
  381. /*!< CMD Response TimeOut (wait for CMDSENT flag) */
  382. return (errorstatus);
  383. }
  384. errorstatus = SD_InitializeCards();
  385. if (errorstatus != SD_OK)
  386. {
  387. /*!< CMD Response TimeOut (wait for CMDSENT flag) */
  388. return (errorstatus);
  389. }
  390. /*!< Configure the SDIO peripheral */
  391. /*!< SDIO_CK = SDIOCLK / (SDIO_TRANSFER_CLK_DIV + 2) */
  392. /*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
  393. SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
  394. SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  395. SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  396. SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  397. SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
  398. SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  399. SDIO_Init(&SDIO_InitStructure);
  400. /*----------------- Read CSD/CID MSD registers ------------------*/
  401. errorstatus = SD_GetCardInfo(&SDCardInfo);
  402. if (errorstatus == SD_OK)
  403. {
  404. /*----------------- Select Card --------------------------------*/
  405. errorstatus = SD_SelectDeselect((uint32_t)(SDCardInfo.RCA << 16));
  406. }
  407. if (errorstatus == SD_OK)
  408. {
  409. errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b);
  410. }
  411. return (errorstatus);
  412. }
  413. /**
  414. * @brief Gets the cuurent sd card data transfer status.
  415. * @param None
  416. * @retval SDTransferState: Data Transfer state.
  417. * This value can be:
  418. * - SD_TRANSFER_OK: No data transfer is acting
  419. * - SD_TRANSFER_BUSY: Data transfer is acting
  420. */
  421. SDTransferState SD_GetStatus(void)
  422. {
  423. SDCardState cardstate = SD_CARD_TRANSFER;
  424. cardstate = SD_GetState();
  425. if (cardstate == SD_CARD_TRANSFER)
  426. {
  427. return (SD_TRANSFER_OK);
  428. }
  429. else if (cardstate == SD_CARD_ERROR)
  430. {
  431. return (SD_TRANSFER_ERROR);
  432. }
  433. else
  434. {
  435. return (SD_TRANSFER_BUSY);
  436. }
  437. }
  438. /**
  439. * @brief Returns the current card's state.
  440. * @param None
  441. * @retval SDCardState: SD Card Error or SD Card Current State.
  442. */
  443. SDCardState SD_GetState(void)
  444. {
  445. uint32_t resp1 = 0;
  446. if (SD_Detect() == SD_PRESENT)
  447. {
  448. if (SD_SendStatus(&resp1) != SD_OK)
  449. {
  450. return SD_CARD_ERROR;
  451. }
  452. else
  453. {
  454. return (SDCardState)((resp1 >> 9) & 0x0F);
  455. }
  456. }
  457. else
  458. {
  459. return SD_CARD_ERROR;
  460. }
  461. }
  462. /**
  463. * @brief Detect if SD card is correctly plugged in the memory slot.
  464. * @param None
  465. * @retval Return if SD is detected or not
  466. */
  467. uint8_t SD_Detect(void)
  468. {
  469. __IO uint8_t status = SD_PRESENT;
  470. /*!< Check GPIO to detect SD */
  471. if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET)
  472. {
  473. status = SD_NOT_PRESENT;
  474. }
  475. return status;
  476. }
  477. /**
  478. * @brief Enquires cards about their operating voltage and configures
  479. * clock controls.
  480. * @param None
  481. * @retval SD_Error: SD Card Error code.
  482. */
  483. SD_Error SD_PowerON(void)
  484. {
  485. __IO SD_Error errorstatus = SD_OK;
  486. uint32_t response = 0, count = 0, validvoltage = 0;
  487. uint32_t SDType = SD_STD_CAPACITY;
  488. /*!< Power ON Sequence -----------------------------------------------------*/
  489. /*!< Configure the SDIO peripheral */
  490. /*!< SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) */
  491. /*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
  492. /*!< SDIO_CK for initialization should not exceed 400 KHz */
  493. SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV;
  494. SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  495. SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  496. SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  497. SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
  498. SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  499. SDIO_Init(&SDIO_InitStructure);
  500. /*!< Set Power State to ON */
  501. SDIO_SetPowerState(SDIO_PowerState_ON);
  502. /*!< Enable SDIO Clock */
  503. SDIO_ClockCmd(ENABLE);
  504. /*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/
  505. /*!< No CMD response required */
  506. SDIO_CmdInitStructure.SDIO_Argument = 0x0;
  507. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE;
  508. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No;
  509. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  510. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  511. SDIO_SendCommand(&SDIO_CmdInitStructure);
  512. errorstatus = CmdError();
  513. if (errorstatus != SD_OK)
  514. {
  515. /*!< CMD Response TimeOut (wait for CMDSENT flag) */
  516. return (errorstatus);
  517. }
  518. /*!< CMD8: SEND_IF_COND ----------------------------------------------------*/
  519. /*!< Send CMD8 to verify SD card interface operating condition */
  520. /*!< Argument: - [31:12]: Reserved (shall be set to '0')
  521. - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
  522. - [7:0]: Check Pattern (recommended 0xAA) */
  523. /*!< CMD Response: R7 */
  524. SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;
  525. SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND;
  526. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  527. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  528. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  529. SDIO_SendCommand(&SDIO_CmdInitStructure);
  530. errorstatus = CmdResp7Error();
  531. if (errorstatus == SD_OK)
  532. {
  533. CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */
  534. SDType = SD_HIGH_CAPACITY;
  535. }
  536. else
  537. {
  538. /*!< CMD55 */
  539. SDIO_CmdInitStructure.SDIO_Argument = 0x00;
  540. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  541. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  542. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  543. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  544. SDIO_SendCommand(&SDIO_CmdInitStructure);
  545. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  546. }
  547. /*!< CMD55 */
  548. SDIO_CmdInitStructure.SDIO_Argument = 0x00;
  549. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  550. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  551. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  552. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  553. SDIO_SendCommand(&SDIO_CmdInitStructure);
  554. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  555. /*!< If errorstatus is Command TimeOut, it is a MMC card */
  556. /*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
  557. or SD card 1.x */
  558. if (errorstatus == SD_OK)
  559. {
  560. /*!< SD CARD */
  561. /*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  562. while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
  563. {
  564. /*!< SEND CMD55 APP_CMD with RCA as 0 */
  565. SDIO_CmdInitStructure.SDIO_Argument = 0x00;
  566. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  567. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  568. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  569. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  570. SDIO_SendCommand(&SDIO_CmdInitStructure);
  571. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  572. if (errorstatus != SD_OK)
  573. {
  574. return (errorstatus);
  575. }
  576. SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
  577. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
  578. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  579. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  580. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  581. SDIO_SendCommand(&SDIO_CmdInitStructure);
  582. errorstatus = CmdResp3Error();
  583. if (errorstatus != SD_OK)
  584. {
  585. return (errorstatus);
  586. }
  587. response = SDIO_GetResponse(SDIO_RESP1);
  588. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  589. count++;
  590. }
  591. if (count >= SD_MAX_VOLT_TRIAL)
  592. {
  593. errorstatus = SD_INVALID_VOLTRANGE;
  594. return (errorstatus);
  595. }
  596. if (response &= SD_HIGH_CAPACITY)
  597. {
  598. CardType = SDIO_HIGH_CAPACITY_SD_CARD;
  599. }
  600. } /*!< else MMC Card */
  601. return (errorstatus);
  602. }
  603. /**
  604. * @brief Turns the SDIO output signals off.
  605. * @param None
  606. * @retval SD_Error: SD Card Error code.
  607. */
  608. SD_Error SD_PowerOFF(void)
  609. {
  610. SD_Error errorstatus = SD_OK;
  611. /*!< Set Power State to OFF */
  612. SDIO_SetPowerState(SDIO_PowerState_OFF);
  613. return (errorstatus);
  614. }
  615. /**
  616. * @brief Intialises all cards or single card as the case may be Card(s) come
  617. * into standby state.
  618. * @param None
  619. * @retval SD_Error: SD Card Error code.
  620. */
  621. SD_Error SD_InitializeCards(void)
  622. {
  623. SD_Error errorstatus = SD_OK;
  624. uint16_t rca = 0x01;
  625. if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
  626. {
  627. errorstatus = SD_REQUEST_NOT_APPLICABLE;
  628. return (errorstatus);
  629. }
  630. if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
  631. {
  632. /*!< Send CMD2 ALL_SEND_CID */
  633. SDIO_CmdInitStructure.SDIO_Argument = 0x0;
  634. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
  635. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
  636. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  637. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  638. SDIO_SendCommand(&SDIO_CmdInitStructure);
  639. errorstatus = CmdResp2Error();
  640. if (SD_OK != errorstatus)
  641. {
  642. return (errorstatus);
  643. }
  644. CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
  645. CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
  646. CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
  647. CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
  648. }
  649. if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
  650. {
  651. /*!< Send CMD3 SET_REL_ADDR with argument 0 */
  652. /*!< SD Card publishes its RCA. */
  653. SDIO_CmdInitStructure.SDIO_Argument = 0x00;
  654. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;
  655. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  656. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  657. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  658. SDIO_SendCommand(&SDIO_CmdInitStructure);
  659. errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca);
  660. if (SD_OK != errorstatus)
  661. {
  662. return (errorstatus);
  663. }
  664. }
  665. if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
  666. {
  667. RCA = rca;
  668. /*!< Send CMD9 SEND_CSD with argument as card's RCA */
  669. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
  670. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
  671. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
  672. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  673. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  674. SDIO_SendCommand(&SDIO_CmdInitStructure);
  675. errorstatus = CmdResp2Error();
  676. if (SD_OK != errorstatus)
  677. {
  678. return (errorstatus);
  679. }
  680. CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
  681. CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
  682. CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
  683. CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
  684. }
  685. errorstatus = SD_OK; /*!< All cards get intialized */
  686. return (errorstatus);
  687. }
  688. /**
  689. * @brief Returns information about specific card.
  690. * @param cardinfo: pointer to a SD_CardInfo structure that contains all SD card
  691. * information.
  692. * @retval SD_Error: SD Card Error code.
  693. */
  694. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
  695. {
  696. SD_Error errorstatus = SD_OK;
  697. uint8_t tmp = 0;
  698. cardinfo->CardType = (uint8_t)CardType;
  699. cardinfo->RCA = (uint16_t)RCA;
  700. /*!< Byte 0 */
  701. tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24);
  702. cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6;
  703. cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2;
  704. cardinfo->SD_csd.Reserved1 = tmp & 0x03;
  705. /*!< Byte 1 */
  706. tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16);
  707. cardinfo->SD_csd.TAAC = tmp;
  708. /*!< Byte 2 */
  709. tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8);
  710. cardinfo->SD_csd.NSAC = tmp;
  711. /*!< Byte 3 */
  712. tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF);
  713. cardinfo->SD_csd.MaxBusClkFrec = tmp;
  714. /*!< Byte 4 */
  715. tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24);
  716. cardinfo->SD_csd.CardComdClasses = tmp << 4;
  717. /*!< Byte 5 */
  718. tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16);
  719. cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4;
  720. cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
  721. /*!< Byte 6 */
  722. tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8);
  723. cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7;
  724. cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6;
  725. cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5;
  726. cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4;
  727. cardinfo->SD_csd.Reserved2 = 0; /*!< Reserved */
  728. if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0))
  729. {
  730. cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
  731. /*!< Byte 7 */
  732. tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
  733. cardinfo->SD_csd.DeviceSize |= (tmp) << 2;
  734. /*!< Byte 8 */
  735. tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
  736. cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
  737. cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
  738. cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
  739. /*!< Byte 9 */
  740. tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
  741. cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
  742. cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
  743. cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
  744. /*!< Byte 10 */
  745. tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
  746. cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
  747. cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1);
  748. cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
  749. cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
  750. cardinfo->CardCapacity *= cardinfo->CardBlockSize;
  751. }
  752. else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
  753. {
  754. /*!< Byte 7 */
  755. tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
  756. cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
  757. /*!< Byte 8 */
  758. tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
  759. cardinfo->SD_csd.DeviceSize |= (tmp << 8);
  760. /*!< Byte 9 */
  761. tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
  762. cardinfo->SD_csd.DeviceSize |= (tmp);
  763. /*!< Byte 10 */
  764. tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
  765. cardinfo->CardCapacity = (int64_t)(cardinfo->SD_csd.DeviceSize + 1) * 512 * 1024;
  766. cardinfo->CardBlockSize = 512;
  767. }
  768. cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
  769. cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
  770. /*!< Byte 11 */
  771. tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF);
  772. cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
  773. cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
  774. /*!< Byte 12 */
  775. tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24);
  776. cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
  777. cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
  778. cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
  779. cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
  780. /*!< Byte 13 */
  781. tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16);
  782. cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
  783. cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
  784. cardinfo->SD_csd.Reserved3 = 0;
  785. cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
  786. /*!< Byte 14 */
  787. tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8);
  788. cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
  789. cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
  790. cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
  791. cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
  792. cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
  793. cardinfo->SD_csd.ECC = (tmp & 0x03);
  794. /*!< Byte 15 */
  795. tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF);
  796. cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
  797. cardinfo->SD_csd.Reserved4 = 1;
  798. /*!< Byte 0 */
  799. tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24);
  800. cardinfo->SD_cid.ManufacturerID = tmp;
  801. /*!< Byte 1 */
  802. tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16);
  803. cardinfo->SD_cid.OEM_AppliID = tmp << 8;
  804. /*!< Byte 2 */
  805. tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8);
  806. cardinfo->SD_cid.OEM_AppliID |= tmp;
  807. /*!< Byte 3 */
  808. tmp = (uint8_t)(CID_Tab[0] & 0x000000FF);
  809. cardinfo->SD_cid.ProdName1 = tmp << 24;
  810. /*!< Byte 4 */
  811. tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24);
  812. cardinfo->SD_cid.ProdName1 |= tmp << 16;
  813. /*!< Byte 5 */
  814. tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16);
  815. cardinfo->SD_cid.ProdName1 |= tmp << 8;
  816. /*!< Byte 6 */
  817. tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8);
  818. cardinfo->SD_cid.ProdName1 |= tmp;
  819. /*!< Byte 7 */
  820. tmp = (uint8_t)(CID_Tab[1] & 0x000000FF);
  821. cardinfo->SD_cid.ProdName2 = tmp;
  822. /*!< Byte 8 */
  823. tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24);
  824. cardinfo->SD_cid.ProdRev = tmp;
  825. /*!< Byte 9 */
  826. tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16);
  827. cardinfo->SD_cid.ProdSN = tmp << 24;
  828. /*!< Byte 10 */
  829. tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8);
  830. cardinfo->SD_cid.ProdSN |= tmp << 16;
  831. /*!< Byte 11 */
  832. tmp = (uint8_t)(CID_Tab[2] & 0x000000FF);
  833. cardinfo->SD_cid.ProdSN |= tmp << 8;
  834. /*!< Byte 12 */
  835. tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24);
  836. cardinfo->SD_cid.ProdSN |= tmp;
  837. /*!< Byte 13 */
  838. tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16);
  839. cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
  840. cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
  841. /*!< Byte 14 */
  842. tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8);
  843. cardinfo->SD_cid.ManufactDate |= tmp;
  844. /*!< Byte 15 */
  845. tmp = (uint8_t)(CID_Tab[3] & 0x000000FF);
  846. cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
  847. cardinfo->SD_cid.Reserved2 = 1;
  848. return (errorstatus);
  849. }
  850. /**
  851. * @brief Enables wide bus opeartion for the requeseted card if supported by
  852. * card.
  853. * @param WideMode: Specifies the SD card wide bus mode.
  854. * This parameter can be one of the following values:
  855. * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
  856. * @arg SDIO_BusWide_4b: 4-bit data transfer
  857. * @arg SDIO_BusWide_1b: 1-bit data transfer
  858. * @retval SD_Error: SD Card Error code.
  859. */
  860. SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus)
  861. {
  862. SD_Error errorstatus = SD_OK;
  863. uint8_t tmp = 0;
  864. errorstatus = SD_SendSDStatus((uint32_t *)SDSTATUS_Tab);
  865. if (errorstatus != SD_OK)
  866. {
  867. return (errorstatus);
  868. }
  869. /*!< Byte 0 */
  870. tmp = (uint8_t)((SDSTATUS_Tab[0] & 0xC0) >> 6);
  871. cardstatus->DAT_BUS_WIDTH = tmp;
  872. /*!< Byte 0 */
  873. tmp = (uint8_t)((SDSTATUS_Tab[0] & 0x20) >> 5);
  874. cardstatus->SECURED_MODE = tmp;
  875. /*!< Byte 2 */
  876. tmp = (uint8_t)((SDSTATUS_Tab[2] & 0xFF));
  877. cardstatus->SD_CARD_TYPE = tmp << 8;
  878. /*!< Byte 3 */
  879. tmp = (uint8_t)((SDSTATUS_Tab[3] & 0xFF));
  880. cardstatus->SD_CARD_TYPE |= tmp;
  881. /*!< Byte 4 */
  882. tmp = (uint8_t)(SDSTATUS_Tab[4] & 0xFF);
  883. cardstatus->SIZE_OF_PROTECTED_AREA = tmp << 24;
  884. /*!< Byte 5 */
  885. tmp = (uint8_t)(SDSTATUS_Tab[5] & 0xFF);
  886. cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 16;
  887. /*!< Byte 6 */
  888. tmp = (uint8_t)(SDSTATUS_Tab[6] & 0xFF);
  889. cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 8;
  890. /*!< Byte 7 */
  891. tmp = (uint8_t)(SDSTATUS_Tab[7] & 0xFF);
  892. cardstatus->SIZE_OF_PROTECTED_AREA |= tmp;
  893. /*!< Byte 8 */
  894. tmp = (uint8_t)((SDSTATUS_Tab[8] & 0xFF));
  895. cardstatus->SPEED_CLASS = tmp;
  896. /*!< Byte 9 */
  897. tmp = (uint8_t)((SDSTATUS_Tab[9] & 0xFF));
  898. cardstatus->PERFORMANCE_MOVE = tmp;
  899. /*!< Byte 10 */
  900. tmp = (uint8_t)((SDSTATUS_Tab[10] & 0xF0) >> 4);
  901. cardstatus->AU_SIZE = tmp;
  902. /*!< Byte 11 */
  903. tmp = (uint8_t)(SDSTATUS_Tab[11] & 0xFF);
  904. cardstatus->ERASE_SIZE = tmp << 8;
  905. /*!< Byte 12 */
  906. tmp = (uint8_t)(SDSTATUS_Tab[12] & 0xFF);
  907. cardstatus->ERASE_SIZE |= tmp;
  908. /*!< Byte 13 */
  909. tmp = (uint8_t)((SDSTATUS_Tab[13] & 0xFC) >> 2);
  910. cardstatus->ERASE_TIMEOUT = tmp;
  911. /*!< Byte 13 */
  912. tmp = (uint8_t)((SDSTATUS_Tab[13] & 0x3));
  913. cardstatus->ERASE_OFFSET = tmp;
  914. return (errorstatus);
  915. }
  916. /**
  917. * @brief Enables wide bus opeartion for the requeseted card if supported by
  918. * card.
  919. * @param WideMode: Specifies the SD card wide bus mode.
  920. * This parameter can be one of the following values:
  921. * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
  922. * @arg SDIO_BusWide_4b: 4-bit data transfer
  923. * @arg SDIO_BusWide_1b: 1-bit data transfer
  924. * @retval SD_Error: SD Card Error code.
  925. */
  926. SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
  927. {
  928. SD_Error errorstatus = SD_OK;
  929. /*!< MMC Card doesn't support this feature */
  930. if (SDIO_MULTIMEDIA_CARD == CardType)
  931. {
  932. errorstatus = SD_UNSUPPORTED_FEATURE;
  933. return (errorstatus);
  934. }
  935. else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
  936. {
  937. if (SDIO_BusWide_8b == WideMode)
  938. {
  939. errorstatus = SD_UNSUPPORTED_FEATURE;
  940. return (errorstatus);
  941. }
  942. else if (SDIO_BusWide_4b == WideMode)
  943. {
  944. errorstatus = SDEnWideBus(ENABLE);
  945. if (SD_OK == errorstatus)
  946. {
  947. /*!< Configure the SDIO peripheral */
  948. SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
  949. SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  950. SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  951. SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  952. SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b;
  953. SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  954. SDIO_Init(&SDIO_InitStructure);
  955. }
  956. }
  957. else
  958. {
  959. errorstatus = SDEnWideBus(DISABLE);
  960. if (SD_OK == errorstatus)
  961. {
  962. /*!< Configure the SDIO peripheral */
  963. SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
  964. SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  965. SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  966. SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  967. SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
  968. SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  969. SDIO_Init(&SDIO_InitStructure);
  970. }
  971. }
  972. }
  973. return (errorstatus);
  974. }
  975. /**
  976. * @brief Selects od Deselects the corresponding card.
  977. * @param addr: Address of the Card to be selected.
  978. * @retval SD_Error: SD Card Error code.
  979. */
  980. SD_Error SD_SelectDeselect(uint32_t addr)
  981. {
  982. SD_Error errorstatus = SD_OK;
  983. /*!< Send CMD7 SDIO_SEL_DESEL_CARD */
  984. SDIO_CmdInitStructure.SDIO_Argument = addr;
  985. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
  986. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  987. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  988. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  989. SDIO_SendCommand(&SDIO_CmdInitStructure);
  990. errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
  991. return (errorstatus);
  992. }
  993. /**
  994. * @brief Allows to read one block from a specified address in a card. The Data
  995. * transfer can be managed by DMA mode or Polling mode.
  996. * @note This operation should be followed by two functions to check if the
  997. * DMA Controller and SD Card status.
  998. * - SD_ReadWaitOperation(): this function insure that the DMA
  999. * controller has finished all data transfer.
  1000. * - SD_GetStatus(): to check that the SD Card has finished the
  1001. * data transfer and it is ready for data.
  1002. * @param readbuff: pointer to the buffer that will contain the received data
  1003. * @param ReadAddr: Address from where data are to be read.
  1004. * @param BlockSize: the SD card Data block size. The Block size should be 512.
  1005. * @retval SD_Error: SD Card Error code.
  1006. */
  1007. SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
  1008. {
  1009. SD_Error errorstatus = SD_OK;
  1010. #if defined(SD_POLLING_MODE)
  1011. uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
  1012. #endif
  1013. TransferError = SD_OK;
  1014. TransferEnd = 0;
  1015. StopCondition = 0;
  1016. SDIO->DCTRL = 0x0;
  1017. if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
  1018. {
  1019. BlockSize = 512;
  1020. ReadAddr /= 512;
  1021. }
  1022. /* Set Block Size for Card */
  1023. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
  1024. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  1025. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1026. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1027. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1028. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1029. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  1030. if (SD_OK != errorstatus)
  1031. {
  1032. return (errorstatus);
  1033. }
  1034. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  1035. SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
  1036. SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
  1037. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
  1038. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  1039. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  1040. SDIO_DataConfig(&SDIO_DataInitStructure);
  1041. /*!< Send CMD17 READ_SINGLE_BLOCK */
  1042. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
  1043. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
  1044. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1045. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1046. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1047. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1048. errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);
  1049. if (errorstatus != SD_OK)
  1050. {
  1051. return (errorstatus);
  1052. }
  1053. #if defined(SD_POLLING_MODE)
  1054. /*!< In case of single block transfer, no need of stop transfer at all.*/
  1055. /*!< Polling mode */
  1056. while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
  1057. {
  1058. if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
  1059. {
  1060. for (count = 0; count < 8; count++)
  1061. {
  1062. *(tempbuff + count) = SDIO_ReadData();
  1063. }
  1064. tempbuff += 8;
  1065. }
  1066. }
  1067. if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
  1068. {
  1069. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
  1070. errorstatus = SD_DATA_TIMEOUT;
  1071. return (errorstatus);
  1072. }
  1073. else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
  1074. {
  1075. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
  1076. errorstatus = SD_DATA_CRC_FAIL;
  1077. return (errorstatus);
  1078. }
  1079. else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
  1080. {
  1081. SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
  1082. errorstatus = SD_RX_OVERRUN;
  1083. return (errorstatus);
  1084. }
  1085. else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
  1086. {
  1087. SDIO_ClearFlag(SDIO_FLAG_STBITERR);
  1088. errorstatus = SD_START_BIT_ERR;
  1089. return (errorstatus);
  1090. }
  1091. count = SD_DATATIMEOUT;
  1092. while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
  1093. {
  1094. *tempbuff = SDIO_ReadData();
  1095. tempbuff++;
  1096. count--;
  1097. }
  1098. /*!< Clear all the static flags */
  1099. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1100. #elif defined(SD_DMA_MODE)
  1101. SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
  1102. SDIO_DMACmd(ENABLE);
  1103. SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize);
  1104. #endif
  1105. return (errorstatus);
  1106. }
  1107. /**
  1108. * @brief Allows to read blocks from a specified address in a card. The Data
  1109. * transfer can be managed by DMA mode or Polling mode.
  1110. * @note This operation should be followed by two functions to check if the
  1111. * DMA Controller and SD Card status.
  1112. * - SD_ReadWaitOperation(): this function insure that the DMA
  1113. * controller has finished all data transfer.
  1114. * - SD_GetStatus(): to check that the SD Card has finished the
  1115. * data transfer and it is ready for data.
  1116. * @param readbuff: pointer to the buffer that will contain the received data.
  1117. * @param ReadAddr: Address from where data are to be read.
  1118. * @param BlockSize: the SD card Data block size. The Block size should be 512.
  1119. * @param NumberOfBlocks: number of blocks to be read.
  1120. * @retval SD_Error: SD Card Error code.
  1121. */
  1122. SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
  1123. {
  1124. SD_Error errorstatus = SD_OK;
  1125. TransferError = SD_OK;
  1126. TransferEnd = 0;
  1127. StopCondition = 1;
  1128. SDIO->DCTRL = 0x0;
  1129. if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
  1130. {
  1131. BlockSize = 512;
  1132. ReadAddr /= 512;
  1133. }
  1134. /*!< Set Block Size for Card */
  1135. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
  1136. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  1137. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1138. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1139. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1140. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1141. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  1142. if (SD_OK != errorstatus)
  1143. {
  1144. return (errorstatus);
  1145. }
  1146. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  1147. SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
  1148. SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
  1149. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
  1150. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  1151. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  1152. SDIO_DataConfig(&SDIO_DataInitStructure);
  1153. /*!< Send CMD18 READ_MULT_BLOCK with argument data address */
  1154. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
  1155. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
  1156. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1157. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1158. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1159. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1160. errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK);
  1161. if (errorstatus != SD_OK)
  1162. {
  1163. return (errorstatus);
  1164. }
  1165. SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
  1166. SDIO_DMACmd(ENABLE);
  1167. SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize));
  1168. return (errorstatus);
  1169. }
  1170. /**
  1171. * @brief This function waits until the SDIO DMA data transfer is finished.
  1172. * This function should be called after SDIO_ReadMultiBlocks() function
  1173. * to insure that all data sent by the card are already transferred by
  1174. * the DMA controller.
  1175. * @param None.
  1176. * @retval SD_Error: SD Card Error code.
  1177. */
  1178. SD_Error SD_WaitReadOperation(void)
  1179. {
  1180. SD_Error errorstatus = SD_OK;
  1181. uint32_t timeout;
  1182. timeout = SD_DATATIMEOUT;
  1183. while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
  1184. {
  1185. timeout--;
  1186. }
  1187. DMAEndOfTransfer = 0x00;
  1188. timeout = SD_DATATIMEOUT;
  1189. while (((SDIO->STA & SDIO_FLAG_RXACT)) && (timeout > 0))
  1190. {
  1191. timeout--;
  1192. }
  1193. if (StopCondition == 1)
  1194. {
  1195. errorstatus = SD_StopTransfer();
  1196. StopCondition = 0;
  1197. }
  1198. if ((timeout == 0) && (errorstatus == SD_OK))
  1199. {
  1200. errorstatus = SD_DATA_TIMEOUT;
  1201. }
  1202. /*!< Clear all the static flags */
  1203. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1204. if (TransferError != SD_OK)
  1205. {
  1206. return (TransferError);
  1207. }
  1208. else
  1209. {
  1210. return (errorstatus);
  1211. }
  1212. }
  1213. /**
  1214. * @brief Allows to write one block starting from a specified address in a card.
  1215. * The Data transfer can be managed by DMA mode or Polling mode.
  1216. * @note This operation should be followed by two functions to check if the
  1217. * DMA Controller and SD Card status.
  1218. * - SD_ReadWaitOperation(): this function insure that the DMA
  1219. * controller has finished all data transfer.
  1220. * - SD_GetStatus(): to check that the SD Card has finished the
  1221. * data transfer and it is ready for data.
  1222. * @param writebuff: pointer to the buffer that contain the data to be transferred.
  1223. * @param WriteAddr: Address from where data are to be read.
  1224. * @param BlockSize: the SD card Data block size. The Block size should be 512.
  1225. * @retval SD_Error: SD Card Error code.
  1226. */
  1227. SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
  1228. {
  1229. SD_Error errorstatus = SD_OK;
  1230. #if defined(SD_POLLING_MODE)
  1231. uint32_t bytestransferred = 0, count = 0, restwords = 0;
  1232. uint32_t *tempbuff = (uint32_t *)writebuff;
  1233. #endif
  1234. TransferError = SD_OK;
  1235. TransferEnd = 0;
  1236. StopCondition = 0;
  1237. SDIO->DCTRL = 0x0;
  1238. if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
  1239. {
  1240. BlockSize = 512;
  1241. WriteAddr /= 512;
  1242. }
  1243. /* Set Block Size for Card */
  1244. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
  1245. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  1246. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1247. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1248. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1249. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1250. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  1251. if (SD_OK != errorstatus)
  1252. {
  1253. return (errorstatus);
  1254. }
  1255. /*!< Send CMD24 WRITE_SINGLE_BLOCK */
  1256. SDIO_CmdInitStructure.SDIO_Argument = WriteAddr;
  1257. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
  1258. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1259. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1260. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1261. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1262. errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);
  1263. if (errorstatus != SD_OK)
  1264. {
  1265. return (errorstatus);
  1266. }
  1267. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  1268. SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
  1269. SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
  1270. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
  1271. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  1272. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  1273. SDIO_DataConfig(&SDIO_DataInitStructure);
  1274. /*!< In case of single data block transfer no need of stop command at all */
  1275. #if defined(SD_POLLING_MODE)
  1276. while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
  1277. {
  1278. if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
  1279. {
  1280. if ((512 - bytestransferred) < 32)
  1281. {
  1282. restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : ((512 - bytestransferred) / 4 + 1);
  1283. for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
  1284. {
  1285. SDIO_WriteData(*tempbuff);
  1286. }
  1287. }
  1288. else
  1289. {
  1290. for (count = 0; count < 8; count++)
  1291. {
  1292. SDIO_WriteData(*(tempbuff + count));
  1293. }
  1294. tempbuff += 8;
  1295. bytestransferred += 32;
  1296. }
  1297. }
  1298. }
  1299. if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
  1300. {
  1301. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
  1302. errorstatus = SD_DATA_TIMEOUT;
  1303. return (errorstatus);
  1304. }
  1305. else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
  1306. {
  1307. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
  1308. errorstatus = SD_DATA_CRC_FAIL;
  1309. return (errorstatus);
  1310. }
  1311. else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
  1312. {
  1313. SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
  1314. errorstatus = SD_TX_UNDERRUN;
  1315. return (errorstatus);
  1316. }
  1317. else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
  1318. {
  1319. SDIO_ClearFlag(SDIO_FLAG_STBITERR);
  1320. errorstatus = SD_START_BIT_ERR;
  1321. return (errorstatus);
  1322. }
  1323. #elif defined(SD_DMA_MODE)
  1324. SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
  1325. SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize);
  1326. SDIO_DMACmd(ENABLE);
  1327. #endif
  1328. return (errorstatus);
  1329. }
  1330. /**
  1331. * @brief Allows to write blocks starting from a specified address in a card.
  1332. * The Data transfer can be managed by DMA mode only.
  1333. * @note This operation should be followed by two functions to check if the
  1334. * DMA Controller and SD Card status.
  1335. * - SD_ReadWaitOperation(): this function insure that the DMA
  1336. * controller has finished all data transfer.
  1337. * - SD_GetStatus(): to check that the SD Card has finished the
  1338. * data transfer and it is ready for data.
  1339. * @param WriteAddr: Address from where data are to be read.
  1340. * @param writebuff: pointer to the buffer that contain the data to be transferred.
  1341. * @param BlockSize: the SD card Data block size. The Block size should be 512.
  1342. * @param NumberOfBlocks: number of blocks to be written.
  1343. * @retval SD_Error: SD Card Error code.
  1344. */
  1345. SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
  1346. {
  1347. SD_Error errorstatus = SD_OK;
  1348. TransferError = SD_OK;
  1349. TransferEnd = 0;
  1350. StopCondition = 1;
  1351. SDIO->DCTRL = 0x0;
  1352. if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
  1353. {
  1354. BlockSize = 512;
  1355. WriteAddr /= 512;
  1356. }
  1357. /* Set Block Size for Card */
  1358. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
  1359. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  1360. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1361. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1362. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1363. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1364. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  1365. if (SD_OK != errorstatus)
  1366. {
  1367. return (errorstatus);
  1368. }
  1369. /*!< To improve performance */
  1370. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(RCA << 16);
  1371. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  1372. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1373. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1374. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1375. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1376. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  1377. if (errorstatus != SD_OK)
  1378. {
  1379. return (errorstatus);
  1380. }
  1381. /*!< To improve performance */
  1382. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
  1383. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
  1384. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1385. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1386. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1387. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1388. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);
  1389. if (errorstatus != SD_OK)
  1390. {
  1391. return (errorstatus);
  1392. }
  1393. /*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */
  1394. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr;
  1395. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
  1396. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1397. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1398. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1399. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1400. errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK);
  1401. if (SD_OK != errorstatus)
  1402. {
  1403. return (errorstatus);
  1404. }
  1405. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  1406. SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
  1407. SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
  1408. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
  1409. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  1410. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  1411. SDIO_DataConfig(&SDIO_DataInitStructure);
  1412. SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
  1413. SDIO_DMACmd(ENABLE);
  1414. SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize));
  1415. return (errorstatus);
  1416. }
  1417. /**
  1418. * @brief This function waits until the SDIO DMA data transfer is finished.
  1419. * This function should be called after SDIO_WriteBlock() and
  1420. * SDIO_WriteMultiBlocks() function to insure that all data sent by the
  1421. * card are already transferred by the DMA controller.
  1422. * @param None.
  1423. * @retval SD_Error: SD Card Error code.
  1424. */
  1425. SD_Error SD_WaitWriteOperation(void)
  1426. {
  1427. SD_Error errorstatus = SD_OK;
  1428. uint32_t timeout;
  1429. timeout = SD_DATATIMEOUT;
  1430. while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
  1431. {
  1432. timeout--;
  1433. }
  1434. DMAEndOfTransfer = 0x00;
  1435. timeout = SD_DATATIMEOUT;
  1436. while (((SDIO->STA & SDIO_FLAG_TXACT)) && (timeout > 0))
  1437. {
  1438. timeout--;
  1439. }
  1440. if (StopCondition == 1)
  1441. {
  1442. errorstatus = SD_StopTransfer();
  1443. StopCondition = 0;
  1444. }
  1445. if ((timeout == 0) && (errorstatus == SD_OK))
  1446. {
  1447. errorstatus = SD_DATA_TIMEOUT;
  1448. }
  1449. /*!< Clear all the static flags */
  1450. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1451. if (TransferError != SD_OK)
  1452. {
  1453. return (TransferError);
  1454. }
  1455. else
  1456. {
  1457. return (errorstatus);
  1458. }
  1459. }
  1460. /**
  1461. * @brief Gets the cuurent data transfer state.
  1462. * @param None
  1463. * @retval SDTransferState: Data Transfer state.
  1464. * This value can be:
  1465. * - SD_TRANSFER_OK: No data transfer is acting
  1466. * - SD_TRANSFER_BUSY: Data transfer is acting
  1467. */
  1468. SDTransferState SD_GetTransferState(void)
  1469. {
  1470. if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
  1471. {
  1472. return (SD_TRANSFER_BUSY);
  1473. }
  1474. else
  1475. {
  1476. return (SD_TRANSFER_OK);
  1477. }
  1478. }
  1479. /**
  1480. * @brief Aborts an ongoing data transfer.
  1481. * @param None
  1482. * @retval SD_Error: SD Card Error code.
  1483. */
  1484. SD_Error SD_StopTransfer(void)
  1485. {
  1486. SD_Error errorstatus = SD_OK;
  1487. /*!< Send CMD12 STOP_TRANSMISSION */
  1488. SDIO_CmdInitStructure.SDIO_Argument = 0x0;
  1489. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
  1490. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1491. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1492. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1493. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1494. errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
  1495. return (errorstatus);
  1496. }
  1497. /**
  1498. * @brief Allows to erase memory area specified for the given card.
  1499. * @param startaddr: the start address.
  1500. * @param endaddr: the end address.
  1501. * @retval SD_Error: SD Card Error code.
  1502. */
  1503. SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
  1504. {
  1505. SD_Error errorstatus = SD_OK;
  1506. uint32_t delay = 0;
  1507. __IO uint32_t maxdelay = 0;
  1508. uint8_t cardstate = 0;
  1509. /*!< Check if the card coomnd class supports erase command */
  1510. if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0)
  1511. {
  1512. errorstatus = SD_REQUEST_NOT_APPLICABLE;
  1513. return (errorstatus);
  1514. }
  1515. maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2);
  1516. if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
  1517. {
  1518. errorstatus = SD_LOCK_UNLOCK_FAILED;
  1519. return (errorstatus);
  1520. }
  1521. if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
  1522. {
  1523. startaddr /= 512;
  1524. endaddr /= 512;
  1525. }
  1526. /*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1527. if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
  1528. {
  1529. /*!< Send CMD32 SD_ERASE_GRP_START with argument as addr */
  1530. SDIO_CmdInitStructure.SDIO_Argument = startaddr;
  1531. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START;
  1532. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1533. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1534. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1535. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1536. errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_START);
  1537. if (errorstatus != SD_OK)
  1538. {
  1539. return (errorstatus);
  1540. }
  1541. /*!< Send CMD33 SD_ERASE_GRP_END with argument as addr */
  1542. SDIO_CmdInitStructure.SDIO_Argument = endaddr;
  1543. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END;
  1544. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1545. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1546. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1547. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1548. errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END);
  1549. if (errorstatus != SD_OK)
  1550. {
  1551. return (errorstatus);
  1552. }
  1553. }
  1554. /*!< Send CMD38 ERASE */
  1555. SDIO_CmdInitStructure.SDIO_Argument = 0;
  1556. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE;
  1557. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1558. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1559. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1560. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1561. errorstatus = CmdResp1Error(SD_CMD_ERASE);
  1562. if (errorstatus != SD_OK)
  1563. {
  1564. return (errorstatus);
  1565. }
  1566. for (delay = 0; delay < maxdelay; delay++)
  1567. {
  1568. }
  1569. /*!< Wait till the card is in programming state */
  1570. errorstatus = IsCardProgramming(&cardstate);
  1571. delay = SD_DATATIMEOUT;
  1572. while ((delay > 0) && (errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate)))
  1573. {
  1574. errorstatus = IsCardProgramming(&cardstate);
  1575. delay--;
  1576. }
  1577. return (errorstatus);
  1578. }
  1579. /**
  1580. * @brief Returns the current card's status.
  1581. * @param pcardstatus: pointer to the buffer that will contain the SD card
  1582. * status (Card Status register).
  1583. * @retval SD_Error: SD Card Error code.
  1584. */
  1585. SD_Error SD_SendStatus(uint32_t *pcardstatus)
  1586. {
  1587. SD_Error errorstatus = SD_OK;
  1588. if (pcardstatus == NULL)
  1589. {
  1590. errorstatus = SD_INVALID_PARAMETER;
  1591. return (errorstatus);
  1592. }
  1593. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
  1594. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
  1595. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1596. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1597. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1598. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1599. errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
  1600. if (errorstatus != SD_OK)
  1601. {
  1602. return (errorstatus);
  1603. }
  1604. *pcardstatus = SDIO_GetResponse(SDIO_RESP1);
  1605. return (errorstatus);
  1606. }
  1607. /**
  1608. * @brief Returns the current SD card's status.
  1609. * @param psdstatus: pointer to the buffer that will contain the SD card status
  1610. * (SD Status register).
  1611. * @retval SD_Error: SD Card Error code.
  1612. */
  1613. SD_Error SD_SendSDStatus(uint32_t *psdstatus)
  1614. {
  1615. SD_Error errorstatus = SD_OK;
  1616. uint32_t count = 0;
  1617. if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
  1618. {
  1619. errorstatus = SD_LOCK_UNLOCK_FAILED;
  1620. return (errorstatus);
  1621. }
  1622. /*!< Set block size for card if it is not equal to current block size for card. */
  1623. SDIO_CmdInitStructure.SDIO_Argument = 64;
  1624. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  1625. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1626. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1627. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1628. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1629. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  1630. if (errorstatus != SD_OK)
  1631. {
  1632. return (errorstatus);
  1633. }
  1634. /*!< CMD55 */
  1635. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
  1636. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  1637. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1638. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1639. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1640. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1641. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  1642. if (errorstatus != SD_OK)
  1643. {
  1644. return (errorstatus);
  1645. }
  1646. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  1647. SDIO_DataInitStructure.SDIO_DataLength = 64;
  1648. SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b;
  1649. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
  1650. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  1651. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  1652. SDIO_DataConfig(&SDIO_DataInitStructure);
  1653. /*!< Send ACMD13 SD_APP_STAUS with argument as card's RCA.*/
  1654. SDIO_CmdInitStructure.SDIO_Argument = 0;
  1655. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS;
  1656. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  1657. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  1658. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  1659. SDIO_SendCommand(&SDIO_CmdInitStructure);
  1660. errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS);
  1661. if (errorstatus != SD_OK)
  1662. {
  1663. return (errorstatus);
  1664. }
  1665. while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
  1666. {
  1667. if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
  1668. {
  1669. for (count = 0; count < 8; count++)
  1670. {
  1671. *(psdstatus + count) = SDIO_ReadData();
  1672. }
  1673. psdstatus += 8;
  1674. }
  1675. }
  1676. if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
  1677. {
  1678. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
  1679. errorstatus = SD_DATA_TIMEOUT;
  1680. return (errorstatus);
  1681. }
  1682. else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
  1683. {
  1684. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
  1685. errorstatus = SD_DATA_CRC_FAIL;
  1686. return (errorstatus);
  1687. }
  1688. else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
  1689. {
  1690. SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
  1691. errorstatus = SD_RX_OVERRUN;
  1692. return (errorstatus);
  1693. }
  1694. else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
  1695. {
  1696. SDIO_ClearFlag(SDIO_FLAG_STBITERR);
  1697. errorstatus = SD_START_BIT_ERR;
  1698. return (errorstatus);
  1699. }
  1700. count = SD_DATATIMEOUT;
  1701. while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
  1702. {
  1703. *psdstatus = SDIO_ReadData();
  1704. psdstatus++;
  1705. count--;
  1706. }
  1707. /*!< Clear all the static status flags*/
  1708. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1709. return (errorstatus);
  1710. }
  1711. /**
  1712. * @brief Allows to process all the interrupts that are high.
  1713. * @param None
  1714. * @retval SD_Error: SD Card Error code.
  1715. */
  1716. SD_Error SD_ProcessIRQSrc(void)
  1717. {
  1718. if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET)
  1719. {
  1720. TransferError = SD_OK;
  1721. SDIO_ClearITPendingBit(SDIO_IT_DATAEND);
  1722. TransferEnd = 1;
  1723. }
  1724. else if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET)
  1725. {
  1726. SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL);
  1727. TransferError = SD_DATA_CRC_FAIL;
  1728. }
  1729. else if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET)
  1730. {
  1731. SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT);
  1732. TransferError = SD_DATA_TIMEOUT;
  1733. }
  1734. else if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET)
  1735. {
  1736. SDIO_ClearITPendingBit(SDIO_IT_RXOVERR);
  1737. TransferError = SD_RX_OVERRUN;
  1738. }
  1739. else if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET)
  1740. {
  1741. SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR);
  1742. TransferError = SD_TX_UNDERRUN;
  1743. }
  1744. else if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET)
  1745. {
  1746. SDIO_ClearITPendingBit(SDIO_IT_STBITERR);
  1747. TransferError = SD_START_BIT_ERR;
  1748. }
  1749. SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
  1750. SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
  1751. SDIO_IT_RXOVERR | SDIO_IT_STBITERR,
  1752. DISABLE);
  1753. return (TransferError);
  1754. }
  1755. /**
  1756. * @brief This function waits until the SDIO DMA data transfer is finished.
  1757. * @param None.
  1758. * @retval None.
  1759. */
  1760. void SD_ProcessDMAIRQ(void)
  1761. {
  1762. if (DMA2->LISR & SD_SDIO_DMA_FLAG_TCIF)
  1763. {
  1764. DMAEndOfTransfer = 0x01;
  1765. DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_TCIF | SD_SDIO_DMA_FLAG_FEIF);
  1766. }
  1767. }
  1768. /**
  1769. * @brief Checks for error conditions for CMD0.
  1770. * @param None
  1771. * @retval SD_Error: SD Card Error code.
  1772. */
  1773. static SD_Error CmdError(void)
  1774. {
  1775. SD_Error errorstatus = SD_OK;
  1776. uint32_t timeout;
  1777. timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
  1778. while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET))
  1779. {
  1780. timeout--;
  1781. }
  1782. if (timeout == 0)
  1783. {
  1784. errorstatus = SD_CMD_RSP_TIMEOUT;
  1785. return (errorstatus);
  1786. }
  1787. /*!< Clear all the static flags */
  1788. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1789. return (errorstatus);
  1790. }
  1791. /**
  1792. * @brief Checks for error conditions for R7 response.
  1793. * @param None
  1794. * @retval SD_Error: SD Card Error code.
  1795. */
  1796. static SD_Error CmdResp7Error(void)
  1797. {
  1798. SD_Error errorstatus = SD_OK;
  1799. uint32_t status;
  1800. uint32_t timeout = SDIO_CMD0TIMEOUT;
  1801. status = SDIO->STA;
  1802. while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0))
  1803. {
  1804. timeout--;
  1805. status = SDIO->STA;
  1806. }
  1807. if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT))
  1808. {
  1809. /*!< Card is not V2.0 complient or card does not support the set voltage range */
  1810. errorstatus = SD_CMD_RSP_TIMEOUT;
  1811. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
  1812. return (errorstatus);
  1813. }
  1814. if (status & SDIO_FLAG_CMDREND)
  1815. {
  1816. /*!< Card is SD V2.0 compliant */
  1817. errorstatus = SD_OK;
  1818. SDIO_ClearFlag(SDIO_FLAG_CMDREND);
  1819. return (errorstatus);
  1820. }
  1821. return (errorstatus);
  1822. }
  1823. /**
  1824. * @brief Checks for error conditions for R1 response.
  1825. * @param cmd: The sent command index.
  1826. * @retval SD_Error: SD Card Error code.
  1827. */
  1828. static SD_Error CmdResp1Error(uint8_t cmd)
  1829. {
  1830. SD_Error errorstatus = SD_OK;
  1831. uint32_t status;
  1832. uint32_t response_r1;
  1833. status = SDIO->STA;
  1834. while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
  1835. {
  1836. status = SDIO->STA;
  1837. }
  1838. if (status & SDIO_FLAG_CTIMEOUT)
  1839. {
  1840. errorstatus = SD_CMD_RSP_TIMEOUT;
  1841. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
  1842. return (errorstatus);
  1843. }
  1844. else if (status & SDIO_FLAG_CCRCFAIL)
  1845. {
  1846. errorstatus = SD_CMD_CRC_FAIL;
  1847. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
  1848. return (errorstatus);
  1849. }
  1850. /*!< Check response received is of desired command */
  1851. if (SDIO_GetCommandResponse() != cmd)
  1852. {
  1853. errorstatus = SD_ILLEGAL_CMD;
  1854. return (errorstatus);
  1855. }
  1856. /*!< Clear all the static flags */
  1857. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1858. /*!< We have received response, retrieve it for analysis */
  1859. response_r1 = SDIO_GetResponse(SDIO_RESP1);
  1860. if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
  1861. {
  1862. return (errorstatus);
  1863. }
  1864. if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE)
  1865. {
  1866. return (SD_ADDR_OUT_OF_RANGE);
  1867. }
  1868. if (response_r1 & SD_OCR_ADDR_MISALIGNED)
  1869. {
  1870. return (SD_ADDR_MISALIGNED);
  1871. }
  1872. if (response_r1 & SD_OCR_BLOCK_LEN_ERR)
  1873. {
  1874. return (SD_BLOCK_LEN_ERR);
  1875. }
  1876. if (response_r1 & SD_OCR_ERASE_SEQ_ERR)
  1877. {
  1878. return (SD_ERASE_SEQ_ERR);
  1879. }
  1880. if (response_r1 & SD_OCR_BAD_ERASE_PARAM)
  1881. {
  1882. return (SD_BAD_ERASE_PARAM);
  1883. }
  1884. if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION)
  1885. {
  1886. return (SD_WRITE_PROT_VIOLATION);
  1887. }
  1888. if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED)
  1889. {
  1890. return (SD_LOCK_UNLOCK_FAILED);
  1891. }
  1892. if (response_r1 & SD_OCR_COM_CRC_FAILED)
  1893. {
  1894. return (SD_COM_CRC_FAILED);
  1895. }
  1896. if (response_r1 & SD_OCR_ILLEGAL_CMD)
  1897. {
  1898. return (SD_ILLEGAL_CMD);
  1899. }
  1900. if (response_r1 & SD_OCR_CARD_ECC_FAILED)
  1901. {
  1902. return (SD_CARD_ECC_FAILED);
  1903. }
  1904. if (response_r1 & SD_OCR_CC_ERROR)
  1905. {
  1906. return (SD_CC_ERROR);
  1907. }
  1908. if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
  1909. {
  1910. return (SD_GENERAL_UNKNOWN_ERROR);
  1911. }
  1912. if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN)
  1913. {
  1914. return (SD_STREAM_READ_UNDERRUN);
  1915. }
  1916. if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN)
  1917. {
  1918. return (SD_STREAM_WRITE_OVERRUN);
  1919. }
  1920. if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE)
  1921. {
  1922. return (SD_CID_CSD_OVERWRITE);
  1923. }
  1924. if (response_r1 & SD_OCR_WP_ERASE_SKIP)
  1925. {
  1926. return (SD_WP_ERASE_SKIP);
  1927. }
  1928. if (response_r1 & SD_OCR_CARD_ECC_DISABLED)
  1929. {
  1930. return (SD_CARD_ECC_DISABLED);
  1931. }
  1932. if (response_r1 & SD_OCR_ERASE_RESET)
  1933. {
  1934. return (SD_ERASE_RESET);
  1935. }
  1936. if (response_r1 & SD_OCR_AKE_SEQ_ERROR)
  1937. {
  1938. return (SD_AKE_SEQ_ERROR);
  1939. }
  1940. return (errorstatus);
  1941. }
  1942. /**
  1943. * @brief Checks for error conditions for R3 (OCR) response.
  1944. * @param None
  1945. * @retval SD_Error: SD Card Error code.
  1946. */
  1947. static SD_Error CmdResp3Error(void)
  1948. {
  1949. SD_Error errorstatus = SD_OK;
  1950. uint32_t status;
  1951. status = SDIO->STA;
  1952. while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
  1953. {
  1954. status = SDIO->STA;
  1955. }
  1956. if (status & SDIO_FLAG_CTIMEOUT)
  1957. {
  1958. errorstatus = SD_CMD_RSP_TIMEOUT;
  1959. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
  1960. return (errorstatus);
  1961. }
  1962. /*!< Clear all the static flags */
  1963. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1964. return (errorstatus);
  1965. }
  1966. /**
  1967. * @brief Checks for error conditions for R2 (CID or CSD) response.
  1968. * @param None
  1969. * @retval SD_Error: SD Card Error code.
  1970. */
  1971. static SD_Error CmdResp2Error(void)
  1972. {
  1973. SD_Error errorstatus = SD_OK;
  1974. uint32_t status;
  1975. status = SDIO->STA;
  1976. while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
  1977. {
  1978. status = SDIO->STA;
  1979. }
  1980. if (status & SDIO_FLAG_CTIMEOUT)
  1981. {
  1982. errorstatus = SD_CMD_RSP_TIMEOUT;
  1983. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
  1984. return (errorstatus);
  1985. }
  1986. else if (status & SDIO_FLAG_CCRCFAIL)
  1987. {
  1988. errorstatus = SD_CMD_CRC_FAIL;
  1989. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
  1990. return (errorstatus);
  1991. }
  1992. /*!< Clear all the static flags */
  1993. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  1994. return (errorstatus);
  1995. }
  1996. /**
  1997. * @brief Checks for error conditions for R6 (RCA) response.
  1998. * @param cmd: The sent command index.
  1999. * @param prca: pointer to the variable that will contain the SD card relative
  2000. * address RCA.
  2001. * @retval SD_Error: SD Card Error code.
  2002. */
  2003. static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
  2004. {
  2005. SD_Error errorstatus = SD_OK;
  2006. uint32_t status;
  2007. uint32_t response_r1;
  2008. status = SDIO->STA;
  2009. while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
  2010. {
  2011. status = SDIO->STA;
  2012. }
  2013. if (status & SDIO_FLAG_CTIMEOUT)
  2014. {
  2015. errorstatus = SD_CMD_RSP_TIMEOUT;
  2016. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
  2017. return (errorstatus);
  2018. }
  2019. else if (status & SDIO_FLAG_CCRCFAIL)
  2020. {
  2021. errorstatus = SD_CMD_CRC_FAIL;
  2022. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
  2023. return (errorstatus);
  2024. }
  2025. /*!< Check response received is of desired command */
  2026. if (SDIO_GetCommandResponse() != cmd)
  2027. {
  2028. errorstatus = SD_ILLEGAL_CMD;
  2029. return (errorstatus);
  2030. }
  2031. /*!< Clear all the static flags */
  2032. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  2033. /*!< We have received response, retrieve it. */
  2034. response_r1 = SDIO_GetResponse(SDIO_RESP1);
  2035. if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)))
  2036. {
  2037. *prca = (uint16_t)(response_r1 >> 16);
  2038. return (errorstatus);
  2039. }
  2040. if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
  2041. {
  2042. return (SD_GENERAL_UNKNOWN_ERROR);
  2043. }
  2044. if (response_r1 & SD_R6_ILLEGAL_CMD)
  2045. {
  2046. return (SD_ILLEGAL_CMD);
  2047. }
  2048. if (response_r1 & SD_R6_COM_CRC_FAILED)
  2049. {
  2050. return (SD_COM_CRC_FAILED);
  2051. }
  2052. return (errorstatus);
  2053. }
  2054. /**
  2055. * @brief Enables or disables the SDIO wide bus mode.
  2056. * @param NewState: new state of the SDIO wide bus mode.
  2057. * This parameter can be: ENABLE or DISABLE.
  2058. * @retval SD_Error: SD Card Error code.
  2059. */
  2060. static SD_Error SDEnWideBus(FunctionalState NewState)
  2061. {
  2062. SD_Error errorstatus = SD_OK;
  2063. uint32_t scr[2] = {0, 0};
  2064. if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
  2065. {
  2066. errorstatus = SD_LOCK_UNLOCK_FAILED;
  2067. return (errorstatus);
  2068. }
  2069. /*!< Get SCR Register */
  2070. errorstatus = FindSCR(RCA, scr);
  2071. if (errorstatus != SD_OK)
  2072. {
  2073. return (errorstatus);
  2074. }
  2075. /*!< If wide bus operation to be enabled */
  2076. if (NewState == ENABLE)
  2077. {
  2078. /*!< If requested card supports wide bus operation */
  2079. if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
  2080. {
  2081. /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
  2082. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
  2083. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  2084. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2085. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2086. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2087. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2088. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  2089. if (errorstatus != SD_OK)
  2090. {
  2091. return (errorstatus);
  2092. }
  2093. /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  2094. SDIO_CmdInitStructure.SDIO_Argument = 0x2;
  2095. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  2096. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2097. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2098. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2099. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2100. errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
  2101. if (errorstatus != SD_OK)
  2102. {
  2103. return (errorstatus);
  2104. }
  2105. return (errorstatus);
  2106. }
  2107. else
  2108. {
  2109. errorstatus = SD_REQUEST_NOT_APPLICABLE;
  2110. return (errorstatus);
  2111. }
  2112. } /*!< If wide bus operation to be disabled */
  2113. else
  2114. {
  2115. /*!< If requested card supports 1 bit mode operation */
  2116. if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
  2117. {
  2118. /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
  2119. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
  2120. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  2121. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2122. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2123. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2124. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2125. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  2126. if (errorstatus != SD_OK)
  2127. {
  2128. return (errorstatus);
  2129. }
  2130. /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  2131. SDIO_CmdInitStructure.SDIO_Argument = 0x00;
  2132. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  2133. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2134. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2135. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2136. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2137. errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
  2138. if (errorstatus != SD_OK)
  2139. {
  2140. return (errorstatus);
  2141. }
  2142. return (errorstatus);
  2143. }
  2144. else
  2145. {
  2146. errorstatus = SD_REQUEST_NOT_APPLICABLE;
  2147. return (errorstatus);
  2148. }
  2149. }
  2150. }
  2151. /**
  2152. * @brief Checks if the SD card is in programming state.
  2153. * @param pstatus: pointer to the variable that will contain the SD card state.
  2154. * @retval SD_Error: SD Card Error code.
  2155. */
  2156. static SD_Error IsCardProgramming(uint8_t *pstatus)
  2157. {
  2158. SD_Error errorstatus = SD_OK;
  2159. __IO uint32_t respR1 = 0, status = 0;
  2160. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
  2161. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
  2162. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2163. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2164. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2165. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2166. status = SDIO->STA;
  2167. while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
  2168. {
  2169. status = SDIO->STA;
  2170. }
  2171. if (status & SDIO_FLAG_CTIMEOUT)
  2172. {
  2173. errorstatus = SD_CMD_RSP_TIMEOUT;
  2174. SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
  2175. return (errorstatus);
  2176. }
  2177. else if (status & SDIO_FLAG_CCRCFAIL)
  2178. {
  2179. errorstatus = SD_CMD_CRC_FAIL;
  2180. SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
  2181. return (errorstatus);
  2182. }
  2183. status = (uint32_t)SDIO_GetCommandResponse();
  2184. /*!< Check response received is of desired command */
  2185. if (status != SD_CMD_SEND_STATUS)
  2186. {
  2187. errorstatus = SD_ILLEGAL_CMD;
  2188. return (errorstatus);
  2189. }
  2190. /*!< Clear all the static flags */
  2191. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  2192. /*!< We have received response, retrieve it for analysis */
  2193. respR1 = SDIO_GetResponse(SDIO_RESP1);
  2194. /*!< Find out card status */
  2195. *pstatus = (uint8_t)((respR1 >> 9) & 0x0000000F);
  2196. if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
  2197. {
  2198. return (errorstatus);
  2199. }
  2200. if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE)
  2201. {
  2202. return (SD_ADDR_OUT_OF_RANGE);
  2203. }
  2204. if (respR1 & SD_OCR_ADDR_MISALIGNED)
  2205. {
  2206. return (SD_ADDR_MISALIGNED);
  2207. }
  2208. if (respR1 & SD_OCR_BLOCK_LEN_ERR)
  2209. {
  2210. return (SD_BLOCK_LEN_ERR);
  2211. }
  2212. if (respR1 & SD_OCR_ERASE_SEQ_ERR)
  2213. {
  2214. return (SD_ERASE_SEQ_ERR);
  2215. }
  2216. if (respR1 & SD_OCR_BAD_ERASE_PARAM)
  2217. {
  2218. return (SD_BAD_ERASE_PARAM);
  2219. }
  2220. if (respR1 & SD_OCR_WRITE_PROT_VIOLATION)
  2221. {
  2222. return (SD_WRITE_PROT_VIOLATION);
  2223. }
  2224. if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED)
  2225. {
  2226. return (SD_LOCK_UNLOCK_FAILED);
  2227. }
  2228. if (respR1 & SD_OCR_COM_CRC_FAILED)
  2229. {
  2230. return (SD_COM_CRC_FAILED);
  2231. }
  2232. if (respR1 & SD_OCR_ILLEGAL_CMD)
  2233. {
  2234. return (SD_ILLEGAL_CMD);
  2235. }
  2236. if (respR1 & SD_OCR_CARD_ECC_FAILED)
  2237. {
  2238. return (SD_CARD_ECC_FAILED);
  2239. }
  2240. if (respR1 & SD_OCR_CC_ERROR)
  2241. {
  2242. return (SD_CC_ERROR);
  2243. }
  2244. if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
  2245. {
  2246. return (SD_GENERAL_UNKNOWN_ERROR);
  2247. }
  2248. if (respR1 & SD_OCR_STREAM_READ_UNDERRUN)
  2249. {
  2250. return (SD_STREAM_READ_UNDERRUN);
  2251. }
  2252. if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN)
  2253. {
  2254. return (SD_STREAM_WRITE_OVERRUN);
  2255. }
  2256. if (respR1 & SD_OCR_CID_CSD_OVERWRIETE)
  2257. {
  2258. return (SD_CID_CSD_OVERWRITE);
  2259. }
  2260. if (respR1 & SD_OCR_WP_ERASE_SKIP)
  2261. {
  2262. return (SD_WP_ERASE_SKIP);
  2263. }
  2264. if (respR1 & SD_OCR_CARD_ECC_DISABLED)
  2265. {
  2266. return (SD_CARD_ECC_DISABLED);
  2267. }
  2268. if (respR1 & SD_OCR_ERASE_RESET)
  2269. {
  2270. return (SD_ERASE_RESET);
  2271. }
  2272. if (respR1 & SD_OCR_AKE_SEQ_ERROR)
  2273. {
  2274. return (SD_AKE_SEQ_ERROR);
  2275. }
  2276. return (errorstatus);
  2277. }
  2278. /**
  2279. * @brief Find the SD card SCR register value.
  2280. * @param rca: selected card address.
  2281. * @param pscr: pointer to the buffer that will contain the SCR value.
  2282. * @retval SD_Error: SD Card Error code.
  2283. */
  2284. static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
  2285. {
  2286. uint32_t index = 0;
  2287. SD_Error errorstatus = SD_OK;
  2288. uint32_t tempscr[2] = {0, 0};
  2289. /*!< Set Block Size To 8 Bytes */
  2290. /*!< Send CMD55 APP_CMD with argument as card's RCA */
  2291. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8;
  2292. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
  2293. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2294. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2295. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2296. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2297. errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
  2298. if (errorstatus != SD_OK)
  2299. {
  2300. return (errorstatus);
  2301. }
  2302. /*!< Send CMD55 APP_CMD with argument as card's RCA */
  2303. SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
  2304. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
  2305. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2306. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2307. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2308. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2309. errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
  2310. if (errorstatus != SD_OK)
  2311. {
  2312. return (errorstatus);
  2313. }
  2314. SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
  2315. SDIO_DataInitStructure.SDIO_DataLength = 8;
  2316. SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b;
  2317. SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
  2318. SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
  2319. SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
  2320. SDIO_DataConfig(&SDIO_DataInitStructure);
  2321. /*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  2322. SDIO_CmdInitStructure.SDIO_Argument = 0x0;
  2323. SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;
  2324. SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
  2325. SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
  2326. SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
  2327. SDIO_SendCommand(&SDIO_CmdInitStructure);
  2328. errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
  2329. if (errorstatus != SD_OK)
  2330. {
  2331. return (errorstatus);
  2332. }
  2333. while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
  2334. {
  2335. if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
  2336. {
  2337. *(tempscr + index) = SDIO_ReadData();
  2338. index++;
  2339. }
  2340. }
  2341. if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
  2342. {
  2343. SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
  2344. errorstatus = SD_DATA_TIMEOUT;
  2345. return (errorstatus);
  2346. }
  2347. else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
  2348. {
  2349. SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
  2350. errorstatus = SD_DATA_CRC_FAIL;
  2351. return (errorstatus);
  2352. }
  2353. else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
  2354. {
  2355. SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
  2356. errorstatus = SD_RX_OVERRUN;
  2357. return (errorstatus);
  2358. }
  2359. else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
  2360. {
  2361. SDIO_ClearFlag(SDIO_FLAG_STBITERR);
  2362. errorstatus = SD_START_BIT_ERR;
  2363. return (errorstatus);
  2364. }
  2365. /*!< Clear all the static flags */
  2366. SDIO_ClearFlag(SDIO_STATIC_FLAGS);
  2367. *(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
  2368. *(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
  2369. return (errorstatus);
  2370. }
  2371. /**
  2372. * @brief Converts the number of bytes in power of two and returns the power.
  2373. * @param NumberOfBytes: number of bytes.
  2374. * @retval None
  2375. */
  2376. uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
  2377. {
  2378. uint8_t count = 0;
  2379. while (NumberOfBytes != 1)
  2380. {
  2381. NumberOfBytes >>= 1;
  2382. count++;
  2383. }
  2384. return (count);
  2385. }
  2386. /*************************************************************************************/
  2387. /**
  2388. * @brief DeInitializes the SDIO interface.
  2389. * @param None
  2390. * @retval None
  2391. */
  2392. void SD_LowLevel_DeInit(void)
  2393. {
  2394. GPIO_InitTypeDef GPIO_InitStructure;
  2395. /*!< Disable SDIO Clock */
  2396. SDIO_ClockCmd(DISABLE);
  2397. /*!< Set Power State to OFF */
  2398. SDIO_SetPowerState(SDIO_PowerState_OFF);
  2399. /*!< DeInitializes the SDIO peripheral */
  2400. SDIO_DeInit();
  2401. /* Disable the SDIO APB2 Clock */
  2402. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, DISABLE);
  2403. GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_MCO);
  2404. GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_MCO);
  2405. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_MCO);
  2406. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_MCO);
  2407. GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_MCO);
  2408. GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_MCO);
  2409. /* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
  2410. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  2411. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  2412. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  2413. GPIO_Init(GPIOC, &GPIO_InitStructure);
  2414. /* Configure PD.02 CMD line */
  2415. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  2416. GPIO_Init(GPIOD, &GPIO_InitStructure);
  2417. /* Configure PC.12 pin: CLK pin */
  2418. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  2419. GPIO_Init(GPIOC, &GPIO_InitStructure);
  2420. }
  2421. /**
  2422. * @brief Initializes the SD Card and put it into StandBy State (Ready for
  2423. * data transfer).
  2424. * @param None
  2425. * @retval None
  2426. */
  2427. void SD_LowLevel_Init(void)
  2428. {
  2429. GPIO_InitTypeDef GPIO_InitStructure;
  2430. /* GPIOC and GPIOD Periph clock enable */
  2431. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | SD_DETECT_GPIO_CLK, ENABLE);
  2432. GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_SDIO);
  2433. GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_SDIO);
  2434. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SDIO);
  2435. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SDIO);
  2436. GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SDIO);
  2437. GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_SDIO);
  2438. /* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
  2439. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  2440. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
  2441. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  2442. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  2443. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  2444. GPIO_Init(GPIOC, &GPIO_InitStructure);
  2445. /* Configure PD.02 CMD line */
  2446. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  2447. GPIO_Init(GPIOD, &GPIO_InitStructure);
  2448. /* Configure PC.12 pin: CLK pin */
  2449. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  2450. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  2451. GPIO_Init(GPIOC, &GPIO_InitStructure);
  2452. /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */
  2453. GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
  2454. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  2455. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  2456. GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
  2457. /* Enable the SDIO APB2 Clock */
  2458. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, ENABLE);
  2459. /* Enable the DMA2 Clock */
  2460. RCC_AHB1PeriphClockCmd(SD_SDIO_DMA_CLK, ENABLE);
  2461. }
  2462. /**
  2463. * @brief Configures the DMA2 Channel4 for SDIO Tx request.
  2464. * @param BufferSRC: pointer to the source buffer
  2465. * @param BufferSize: buffer size
  2466. * @retval None
  2467. */
  2468. void SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize)
  2469. {
  2470. DMA_InitTypeDef SDDMA_InitStructure;
  2471. DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_FEIF | SD_SDIO_DMA_FLAG_DMEIF | SD_SDIO_DMA_FLAG_TEIF | SD_SDIO_DMA_FLAG_HTIF | SD_SDIO_DMA_FLAG_TCIF);
  2472. /* DMA2 Stream3 or Stream6 disable */
  2473. DMA_Cmd(SD_SDIO_DMA_STREAM, DISABLE);
  2474. /* DMA2 Stream3 or Stream6 Config */
  2475. DMA_DeInit(SD_SDIO_DMA_STREAM);
  2476. SDDMA_InitStructure.DMA_Channel = SD_SDIO_DMA_CHANNEL;
  2477. SDDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS;
  2478. SDDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BufferSRC;
  2479. SDDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  2480. SDDMA_InitStructure.DMA_BufferSize = 0;
  2481. SDDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  2482. SDDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  2483. SDDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  2484. SDDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  2485. SDDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  2486. SDDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  2487. SDDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  2488. SDDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  2489. SDDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;
  2490. SDDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;
  2491. DMA_Init(SD_SDIO_DMA_STREAM, &SDDMA_InitStructure);
  2492. DMA_ITConfig(SD_SDIO_DMA_STREAM, DMA_IT_TC, ENABLE);
  2493. DMA_FlowControllerConfig(SD_SDIO_DMA_STREAM, DMA_FlowCtrl_Peripheral);
  2494. /* DMA2 Stream3 or Stream6 enable */
  2495. DMA_Cmd(SD_SDIO_DMA_STREAM, ENABLE);
  2496. }
  2497. /**
  2498. * @brief Configures the DMA2 Channel4 for SDIO Rx request.
  2499. * @param BufferDST: pointer to the destination buffer
  2500. * @param BufferSize: buffer size
  2501. * @retval None
  2502. */
  2503. void SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize)
  2504. {
  2505. DMA_InitTypeDef SDDMA_InitStructure;
  2506. DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_FEIF | SD_SDIO_DMA_FLAG_DMEIF | SD_SDIO_DMA_FLAG_TEIF | SD_SDIO_DMA_FLAG_HTIF | SD_SDIO_DMA_FLAG_TCIF);
  2507. /* DMA2 Stream3 or Stream6 disable */
  2508. DMA_Cmd(SD_SDIO_DMA_STREAM, DISABLE);
  2509. /* DMA2 Stream3 or Stream6 Config */
  2510. DMA_DeInit(SD_SDIO_DMA_STREAM);
  2511. SDDMA_InitStructure.DMA_Channel = SD_SDIO_DMA_CHANNEL;
  2512. SDDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS;
  2513. SDDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BufferDST;
  2514. SDDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  2515. SDDMA_InitStructure.DMA_BufferSize = 0;
  2516. SDDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  2517. SDDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  2518. SDDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  2519. SDDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  2520. SDDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  2521. SDDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  2522. SDDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  2523. SDDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  2524. SDDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;
  2525. SDDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;
  2526. DMA_Init(SD_SDIO_DMA_STREAM, &SDDMA_InitStructure);
  2527. DMA_ITConfig(SD_SDIO_DMA_STREAM, DMA_IT_TC, ENABLE);
  2528. DMA_FlowControllerConfig(SD_SDIO_DMA_STREAM, DMA_FlowCtrl_Peripheral);
  2529. /* DMA2 Stream3 or Stream6 enable */
  2530. DMA_Cmd(SD_SDIO_DMA_STREAM, ENABLE);
  2531. }
  2532. /*
  2533. *********************************************************************************************************
  2534. * 函 数 名: SDIO_Interrupts_Config
  2535. * 功能说明: 配置SDIO中断
  2536. * 形 参: 无
  2537. * 返 回 值: 无
  2538. *********************************************************************************************************
  2539. */
  2540. void SDIO_Interrupts_Config(void)
  2541. {
  2542. NVIC_InitTypeDef NVIC_InitStructure;
  2543. /* Configure the NVIC Preemption Priority Bits */
  2544. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  2545. NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  2546. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  2547. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  2548. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  2549. NVIC_Init(&NVIC_InitStructure);
  2550. #if 1
  2551. NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
  2552. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  2553. NVIC_Init(&NVIC_InitStructure);
  2554. #endif
  2555. }
  2556. /*
  2557. *********************************************************************************************************
  2558. * 函 数 名: SDIO_IRQHandler
  2559. * 功能说明: SDIO中断
  2560. * 形 参: 无
  2561. * 返 回 值: 无
  2562. *********************************************************************************************************
  2563. */
  2564. void SDIO_IRQHandler(void)
  2565. {
  2566. /* 处理所有的SDIO中断源 */
  2567. SD_ProcessIRQSrc();
  2568. }
  2569. /**
  2570. * @brief This function handles DMA2 Stream3 or DMA2 Stream6 global interrupts
  2571. * requests.
  2572. * @param None
  2573. * @retval None
  2574. */
  2575. void DMA2_Stream3_IRQHandler(void)
  2576. {
  2577. /* Process DMA2 Stream3 or DMA2 Stream6 Interrupt Sources */
  2578. SD_ProcessDMAIRQ();
  2579. }
  2580. /**
  2581. * @}
  2582. */
  2583. /**
  2584. * @}
  2585. */
  2586. /**
  2587. * @}
  2588. */
  2589. /**
  2590. * @}
  2591. */
  2592. /**
  2593. * @}
  2594. */
  2595. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/