sdio_sd.c 96 KB

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