sdcard.c 89 KB

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