sdcard.c 103 KB

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