sdcard.c 89 KB

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