sdio_sd.c 84 KB

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