stm32l1xx_hal_sd.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_sd.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief SD card HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Secure Digital (SD) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. This driver implements a high level communication layer for read and write from/to
  21. this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
  22. the user in HAL_SD_MspInit() function (MSP layer).
  23. Basically, the MSP layer configuration should be the same as we provide in the
  24. examples.
  25. You can easily tailor this configuration according to hardware resources.
  26. [..]
  27. This driver is a generic layered driver for SDIO memories which uses the HAL
  28. SDIO driver functions to interface with SD and uSD cards devices.
  29. It is used as follows:
  30. (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
  31. (##) Enable the SDIO interface clock using __SDIO_CLK_ENABLE();
  32. (##) SDIO pins configuration for SD card
  33. (+++) Enable the clock for the SDIO GPIOs using the functions __GPIOx_CLK_ENABLE();
  34. (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
  35. and according to your pin assignment;
  36. (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
  37. and HAL_SD_WriteBlocks_DMA() APIs).
  38. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE();
  39. (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
  40. (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
  41. (+++) Configure the SDIO and DMA interrupt priorities using functions
  42. HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
  43. (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
  44. (+++) SDIO interrupts are managed using the macros __HAL_SD_SDIO_ENABLE_IT()
  45. and __HAL_SD_SDIO_DISABLE_IT() inside the communication process.
  46. (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_SDIO_GET_IT()
  47. and __HAL_SD_SDIO_CLEAR_IT()
  48. (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
  49. *** SD Card Initialization and configuration ***
  50. ================================================
  51. [..]
  52. To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
  53. the SD Card and put it into StandBy State (Ready for data transfer).
  54. This function provide the following operations:
  55. (#) Apply the SD Card initialization process at 400KHz and check the SD Card
  56. type (Standard Capacity or High Capacity). You can change or adapt this
  57. frequency by adjusting the "ClockDiv" field.
  58. The SD Card frequency (SDIO_CK) is computed as follows:
  59. SDIO_CK = SDIOCLK / (ClockDiv + 2)
  60. In initialization mode and according to the SD Card standard,
  61. make sure that the SDIO_CK frequency doesn't exceed 400KHz.
  62. (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo
  63. structure. This structure provide also ready computed SD Card capacity
  64. and Block size.
  65. -@- These information are stored in SD handle structure in case of future use.
  66. (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
  67. frequency is set to 48MHz / (SDIO_TRANSFER_CLK_DIV + 2) = 8MHz. You can change or adapt this frequency by adjusting
  68. the "ClockDiv" field.
  69. The SD Card frequency (SDIO_CK) is computed as follows:
  70. SDIO_CK = SDIOCLK / (ClockDiv + 2)
  71. In transfer mode and according to the SD Card standard, make sure that the
  72. SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
  73. To be able to use a frequency higher than 24MHz, you should use the SDIO
  74. peripheral in bypass mode. Refer to the corresponding reference manual
  75. for more details.
  76. (#) Select the corresponding SD Card according to the address read with the step 2.
  77. (#) Configure the SD Card in wide bus mode: 4-bits data.
  78. *** SD Card Read operation ***
  79. ==============================
  80. [..]
  81. (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
  82. This function support only 512-byte block length (the block size should be
  83. chosen as 512 byte).
  84. You can choose either one block read operation or multiple block read operation
  85. by adjusting the "NumberOfBlocks" parameter.
  86. (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
  87. This function support only 512-byte block length (the block size should be
  88. chosen as 512 byte).
  89. You can choose either one block read operation or multiple block read operation
  90. by adjusting the "NumberOfBlocks" parameter.
  91. After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
  92. that the read transfer is done correctly in both DMA and SD sides.
  93. *** SD Card Write operation ***
  94. ===============================
  95. [..]
  96. (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
  97. This function support only 512-byte block length (the block size should be
  98. chosen as 512 byte).
  99. You can choose either one block read operation or multiple block read operation
  100. by adjusting the "NumberOfBlocks" parameter.
  101. (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
  102. This function support only 512-byte block length (the block size should be
  103. chosen as 512 byte).
  104. You can choose either one block read operation or multiple block read operation
  105. by adjusting the "NumberOfBlocks" parameter.
  106. After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
  107. that the write transfer is done correctly in both DMA and SD sides.
  108. *** SD card status ***
  109. ======================
  110. [..]
  111. (+) At any time, you can check the SD Card status and get the SD card state
  112. by using the HAL_SD_GetStatus() function. This function checks first if the
  113. SD card is still connected and then get the internal SD Card transfer state.
  114. (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus()
  115. function.
  116. *** SD HAL driver macros list ***
  117. ==================================
  118. [..]
  119. Below the list of most used macros in SD HAL driver.
  120. (+) __HAL_SD_SDIO_ENABLE : Enable the SD device
  121. (+) __HAL_SD_SDIO_DISABLE : Disable the SD device
  122. (+) __HAL_SD_SDIO_DMA_ENABLE: Enable the SDIO DMA transfer
  123. (+) __HAL_SD_SDIO_DMA_DISABLE: Disable the SDIO DMA transfer
  124. (+) __HAL_SD_SDIO_ENABLE_IT: Enable the SD device interrupt
  125. (+) __HAL_SD_SDIO_DISABLE_IT: Disable the SD device interrupt
  126. (+) __HAL_SD_SDIO_GET_FLAG:Check whether the specified SD flag is set or not
  127. (+) __HAL_SD_SDIO_CLEAR_FLAG: Clear the SD's pending flags
  128. (@) You can refer to the SD HAL driver header file for more useful macros
  129. @endverbatim
  130. ******************************************************************************
  131. * @attention
  132. *
  133. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  134. *
  135. * Redistribution and use in source and binary forms, with or without modification,
  136. * are permitted provided that the following conditions are met:
  137. * 1. Redistributions of source code must retain the above copyright notice,
  138. * this list of conditions and the following disclaimer.
  139. * 2. Redistributions in binary form must reproduce the above copyright notice,
  140. * this list of conditions and the following disclaimer in the documentation
  141. * and/or other materials provided with the distribution.
  142. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  143. * may be used to endorse or promote products derived from this software
  144. * without specific prior written permission.
  145. *
  146. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  147. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  148. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  149. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  150. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  151. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  152. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  153. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  154. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  155. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  156. *
  157. ******************************************************************************
  158. */
  159. /* Includes ------------------------------------------------------------------*/
  160. #include "stm32l1xx_hal.h"
  161. /** @addtogroup STM32L1xx_HAL_Driver
  162. * @{
  163. */
  164. /** @defgroup SD SD
  165. * @brief SD HAL module driver
  166. * @{
  167. */
  168. #ifdef HAL_SD_MODULE_ENABLED
  169. #if defined(STM32L151xD) || defined(STM32L152xD) || defined(STM32L162xD)
  170. /* Private typedef -----------------------------------------------------------*/
  171. /* Private define ------------------------------------------------------------*/
  172. /** @defgroup SD_Private_Define SD Private Define
  173. * @{
  174. */
  175. /**
  176. * @brief SDIO Static flags, TimeOut, FIFO Address
  177. */
  178. #define SDIO_STATIC_FLAGS ((uint32_t)(SDIO_FLAG_CCRCFAIL | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_CTIMEOUT |\
  179. SDIO_FLAG_DTIMEOUT | SDIO_FLAG_TXUNDERR | SDIO_FLAG_RXOVERR |\
  180. SDIO_FLAG_CMDREND | SDIO_FLAG_CMDSENT | SDIO_FLAG_DATAEND |\
  181. SDIO_FLAG_DBCKEND))
  182. #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
  183. /**
  184. * @brief Mask for errors Card Status R1 (OCR Register)
  185. */
  186. #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
  187. #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
  188. #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
  189. #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
  190. #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
  191. #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
  192. #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
  193. #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
  194. #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
  195. #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
  196. #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
  197. #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
  198. #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
  199. #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
  200. #define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000)
  201. #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
  202. #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
  203. #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
  204. #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
  205. #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
  206. /**
  207. * @brief Masks for R6 Response
  208. */
  209. #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
  210. #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
  211. #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
  212. #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
  213. #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
  214. #define SD_STD_CAPACITY ((uint32_t)0x00000000)
  215. #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
  216. #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
  217. #define SD_ALLZERO ((uint32_t)0x00000000)
  218. #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
  219. #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
  220. #define SD_CARD_LOCKED ((uint32_t)0x02000000)
  221. #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
  222. #define SD_0TO7BITS ((uint32_t)0x000000FF)
  223. #define SD_8TO15BITS ((uint32_t)0x0000FF00)
  224. #define SD_16TO23BITS ((uint32_t)0x00FF0000)
  225. #define SD_24TO31BITS ((uint32_t)0xFF000000)
  226. #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
  227. #define SD_HALFFIFO ((uint32_t)0x00000008)
  228. #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
  229. /**
  230. * @brief Command Class Supported
  231. */
  232. #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
  233. #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
  234. #define SD_CCCC_ERASE ((uint32_t)0x00000020)
  235. /**
  236. * @brief Following commands are SD Card Specific commands.
  237. * SDIO_APP_CMD should be sent before sending these commands.
  238. */
  239. #define SD_SDIO_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
  240. /**
  241. * @}
  242. */
  243. /* Private macro -------------------------------------------------------------*/
  244. /* Private variables ---------------------------------------------------------*/
  245. /* Private function prototypes -----------------------------------------------*/
  246. /* Private functions ---------------------------------------------------------*/
  247. /** @defgroup SD_Private_Functions SD Private Functions
  248. * @{
  249. */
  250. static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd);
  251. static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr);
  252. static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd);
  253. static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd);
  254. static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
  255. static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd);
  256. static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus);
  257. static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd);
  258. static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD);
  259. static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd);
  260. static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd);
  261. static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd);
  262. static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA);
  263. static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd);
  264. static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd);
  265. static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
  266. static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma);
  267. static void SD_DMA_RxError(DMA_HandleTypeDef *hdma);
  268. static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma);
  269. static void SD_DMA_TxError(DMA_HandleTypeDef *hdma);
  270. /**
  271. * @}
  272. */
  273. /** @defgroup SD_Exported_Functions SD Exported Functions
  274. * @{
  275. */
  276. /** @defgroup SD_Exported_Functions_Group1 Initialization and de-initialization functions
  277. * @brief Initialization and Configuration functions
  278. *
  279. @verbatim
  280. ===============================================================================
  281. ##### Initialization and de-initialization functions #####
  282. ===============================================================================
  283. [..]
  284. This section provides functions allowing to initialize/de-initialize the SD
  285. card device to be ready for use.
  286. @endverbatim
  287. * @{
  288. */
  289. /**
  290. * @brief Initializes the SD card according to the specified parameters in the
  291. SD_HandleTypeDef and create the associated handle.
  292. * @param hsd: SD handle
  293. * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
  294. * @retval HAL SD error state
  295. */
  296. HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
  297. {
  298. __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
  299. SD_InitTypeDef tmpinit;
  300. /* Initialize the low level hardware (MSP) */
  301. HAL_SD_MspInit(hsd);
  302. /* Default SDIO peripheral configuration for SD card initialization */
  303. tmpinit.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  304. tmpinit.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  305. tmpinit.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  306. tmpinit.BusWide = SDIO_BUS_WIDE_1B;
  307. tmpinit.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  308. tmpinit.ClockDiv = SDIO_INIT_CLK_DIV;
  309. /* Initialize SDIO peripheral interface with default configuration */
  310. SDIO_Init(hsd->Instance, tmpinit);
  311. /* Identify card operating voltage */
  312. errorstate = SD_PowerON(hsd);
  313. if(errorstate != SD_OK)
  314. {
  315. return errorstate;
  316. }
  317. /* Initialize the present SDIO card(s) and put them in idle state */
  318. errorstate = SD_Initialize_Cards(hsd);
  319. if (errorstate != SD_OK)
  320. {
  321. return errorstate;
  322. }
  323. /* Read CSD/CID MSD registers */
  324. errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
  325. if (errorstate == SD_OK)
  326. {
  327. /* Select the Card */
  328. errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16));
  329. }
  330. /* Configure SDIO peripheral interface */
  331. SDIO_Init(hsd->Instance, hsd->Init);
  332. return errorstate;
  333. }
  334. /**
  335. * @brief De-Initializes the SD card.
  336. * @param hsd: SD handle
  337. * @retval HAL status
  338. */
  339. HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
  340. {
  341. /* Set SD power state to off */
  342. SD_PowerOFF(hsd);
  343. /* De-Initialize the MSP layer */
  344. HAL_SD_MspDeInit(hsd);
  345. return HAL_OK;
  346. }
  347. /**
  348. * @brief Initializes the SD MSP.
  349. * @param hsd: SD handle
  350. * @retval None
  351. */
  352. __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
  353. {
  354. /* NOTE : This function Should not be modified, when the callback is needed,
  355. the HAL_SD_MspInit could be implemented in the user file
  356. */
  357. }
  358. /**
  359. * @brief De-Initialize SD MSP.
  360. * @param hsd: SD handle
  361. * @retval None
  362. */
  363. __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
  364. {
  365. /* NOTE : This function Should not be modified, when the callback is needed,
  366. the HAL_SD_MspDeInit could be implemented in the user file
  367. */
  368. }
  369. /**
  370. * @}
  371. */
  372. /** @defgroup SD_Exported_Functions_Group2 IO operation functions
  373. * @brief Data transfer functions
  374. *
  375. @verbatim
  376. ===============================================================================
  377. ##### IO operation functions #####
  378. ===============================================================================
  379. [..]
  380. This subsection provides a set of functions allowing to manage the data
  381. transfer from/to SD card.
  382. @endverbatim
  383. * @{
  384. */
  385. /**
  386. * @brief Reads block(s) from a specified address in a card. The Data transfer
  387. * is managed by polling mode.
  388. * @param hsd: SD handle
  389. * @param pReadBuffer: pointer to the buffer that will contain the received data
  390. * @param ReadAddr: Address from where data is to be read
  391. * @param BlockSize: SD card Data block size
  392. * This parameter should be 512
  393. * @param NumberOfBlocks: Number of SD blocks to read
  394. * @retval SD Card error state
  395. */
  396. HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  397. {
  398. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  399. SDIO_DataInitTypeDef sdio_datainitstructure;
  400. HAL_SD_ErrorTypedef errorstate = SD_OK;
  401. uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer;
  402. /* Initialize data control register */
  403. hsd->Instance->DCTRL = 0;
  404. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  405. {
  406. BlockSize = 512;
  407. ReadAddr /= 512;
  408. }
  409. /* Set Block Size for Card */
  410. sdio_cmdinitstructure.Argument = (uint32_t) BlockSize;
  411. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  412. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  413. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  414. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  415. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  416. /* Check for error conditions */
  417. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  418. if (errorstate != SD_OK)
  419. {
  420. return errorstate;
  421. }
  422. /* Configure the SD DPSM (Data Path State Machine) */
  423. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  424. sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
  425. sdio_datainitstructure.DataBlockSize = (uint32_t)(9 << 4);
  426. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  427. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  428. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  429. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  430. if(NumberOfBlocks > 1)
  431. {
  432. /* Send CMD18 READ_MULT_BLOCK with argument data address */
  433. sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
  434. }
  435. else
  436. {
  437. /* Send CMD17 READ_SINGLE_BLOCK */
  438. sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
  439. }
  440. sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr;
  441. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  442. /* Read block(s) in polling mode */
  443. if(NumberOfBlocks > 1)
  444. {
  445. /* Check for error conditions */
  446. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
  447. if (errorstate != SD_OK)
  448. {
  449. return errorstate;
  450. }
  451. /* Poll on SDIO flags */
  452. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
  453. {
  454. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
  455. {
  456. /* Read data from SDIO Rx FIFO */
  457. for (count = 0; count < 8; count++)
  458. {
  459. *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
  460. }
  461. tempbuff += 8;
  462. }
  463. }
  464. }
  465. else
  466. {
  467. /* Check for error conditions */
  468. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
  469. if (errorstate != SD_OK)
  470. {
  471. return errorstate;
  472. }
  473. /* In case of single block transfer, no need of stop transfer at all */
  474. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
  475. {
  476. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
  477. {
  478. /* Read data from SDIO Rx FIFO */
  479. for (count = 0; count < 8; count++)
  480. {
  481. *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
  482. }
  483. tempbuff += 8;
  484. }
  485. }
  486. }
  487. /* Send stop transmission command in case of multiblock read */
  488. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1))
  489. {
  490. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\
  491. (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  492. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  493. {
  494. /* Send stop transmission command */
  495. errorstate = HAL_SD_StopTransfer(hsd);
  496. }
  497. }
  498. /* Get error state */
  499. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  500. {
  501. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  502. errorstate = SD_DATA_TIMEOUT;
  503. return errorstate;
  504. }
  505. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  506. {
  507. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  508. errorstate = SD_DATA_CRC_FAIL;
  509. return errorstate;
  510. }
  511. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  512. {
  513. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
  514. errorstate = SD_RX_OVERRUN;
  515. return errorstate;
  516. }
  517. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
  518. {
  519. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
  520. errorstate = SD_START_BIT_ERR;
  521. return errorstate;
  522. }
  523. else
  524. {
  525. /* No error flag set */
  526. }
  527. count = SD_DATATIMEOUT;
  528. /* Empty FIFO if there is still any data */
  529. while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
  530. {
  531. *tempbuff = SDIO_ReadFIFO(hsd->Instance);
  532. tempbuff++;
  533. count--;
  534. }
  535. /* Clear all the static flags */
  536. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  537. return errorstate;
  538. }
  539. /**
  540. * @brief Allows to write block(s) to a specified address in a card. The Data
  541. * transfer is managed by polling mode.
  542. * @param hsd: SD handle
  543. * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
  544. * @param WriteAddr: Address from where data is to be written
  545. * @param BlockSize: SD card Data block size
  546. * This parameter should be 512.
  547. * @param NumberOfBlocks: Number of SD blocks to write
  548. * @retval SD Card error state
  549. */
  550. HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  551. {
  552. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  553. SDIO_DataInitTypeDef sdio_datainitstructure;
  554. HAL_SD_ErrorTypedef errorstate = SD_OK;
  555. uint32_t totalnumberofbytes = 0, bytestransferred = 0, count = 0, restwords = 0;
  556. uint32_t *tempbuff = (uint32_t *)pWriteBuffer;
  557. uint8_t cardstate = 0;
  558. /* Initialize data control register */
  559. hsd->Instance->DCTRL = 0;
  560. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  561. {
  562. BlockSize = 512;
  563. WriteAddr /= 512;
  564. }
  565. /* Set Block Size for Card */
  566. sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
  567. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  568. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  569. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  570. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  571. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  572. /* Check for error conditions */
  573. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  574. if (errorstate != SD_OK)
  575. {
  576. return errorstate;
  577. }
  578. if(NumberOfBlocks > 1)
  579. {
  580. /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
  581. sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
  582. }
  583. else
  584. {
  585. /* Send CMD24 WRITE_SINGLE_BLOCK */
  586. sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
  587. }
  588. sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr;
  589. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  590. /* Check for error conditions */
  591. if(NumberOfBlocks > 1)
  592. {
  593. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
  594. }
  595. else
  596. {
  597. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
  598. }
  599. if (errorstate != SD_OK)
  600. {
  601. return errorstate;
  602. }
  603. /* Set total number of bytes to write */
  604. totalnumberofbytes = NumberOfBlocks * BlockSize;
  605. /* Configure the SD DPSM (Data Path State Machine) */
  606. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  607. sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize;
  608. sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  609. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  610. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  611. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  612. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  613. /* Write block(s) in polling mode */
  614. if(NumberOfBlocks > 1)
  615. {
  616. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
  617. {
  618. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
  619. {
  620. if ((totalnumberofbytes - bytestransferred) < 32)
  621. {
  622. restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
  623. /* Write data to SDIO Tx FIFO */
  624. for (count = 0; count < restwords; count++)
  625. {
  626. SDIO_WriteFIFO(hsd->Instance, tempbuff);
  627. tempbuff++;
  628. bytestransferred += 4;
  629. }
  630. }
  631. else
  632. {
  633. /* Write data to SDIO Tx FIFO */
  634. for (count = 0; count < 8; count++)
  635. {
  636. SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
  637. }
  638. tempbuff += 8;
  639. bytestransferred += 32;
  640. }
  641. }
  642. }
  643. }
  644. else
  645. {
  646. /* In case of single data block transfer no need of stop command at all */
  647. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
  648. {
  649. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
  650. {
  651. if ((totalnumberofbytes - bytestransferred) < 32)
  652. {
  653. restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1);
  654. /* Write data to SDIO Tx FIFO */
  655. for (count = 0; count < restwords; count++)
  656. {
  657. SDIO_WriteFIFO(hsd->Instance, tempbuff);
  658. tempbuff++;
  659. bytestransferred += 4;
  660. }
  661. }
  662. else
  663. {
  664. /* Write data to SDIO Tx FIFO */
  665. for (count = 0; count < 8; count++)
  666. {
  667. SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
  668. }
  669. tempbuff += 8;
  670. bytestransferred += 32;
  671. }
  672. }
  673. }
  674. }
  675. /* Send stop transmission command in case of multiblock write */
  676. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1))
  677. {
  678. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  679. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  680. {
  681. /* Send stop transmission command */
  682. errorstate = HAL_SD_StopTransfer(hsd);
  683. }
  684. }
  685. /* Get error state */
  686. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  687. {
  688. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  689. errorstate = SD_DATA_TIMEOUT;
  690. return errorstate;
  691. }
  692. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  693. {
  694. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  695. errorstate = SD_DATA_CRC_FAIL;
  696. return errorstate;
  697. }
  698. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
  699. {
  700. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
  701. errorstate = SD_TX_UNDERRUN;
  702. return errorstate;
  703. }
  704. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
  705. {
  706. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
  707. errorstate = SD_START_BIT_ERR;
  708. return errorstate;
  709. }
  710. else
  711. {
  712. /* No error flag set */
  713. }
  714. /* Clear all the static flags */
  715. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  716. /* Wait till the card is in programming state */
  717. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  718. while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
  719. {
  720. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  721. }
  722. return errorstate;
  723. }
  724. /**
  725. * @brief Reads block(s) from a specified address in a card. The Data transfer
  726. * is managed by DMA mode.
  727. * @note This API should be followed by the function HAL_SD_CheckReadOperation()
  728. * to check the completion of the read process
  729. * @param hsd: SD handle
  730. * @param pReadBuffer: Pointer to the buffer that will contain the received data
  731. * @param ReadAddr: Address from where data is to be read
  732. * @param BlockSize: SD card Data block size
  733. * This paramater should be 512.
  734. * @param NumberOfBlocks: Number of blocks to read.
  735. * @retval SD Card error state
  736. */
  737. HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  738. {
  739. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  740. SDIO_DataInitTypeDef sdio_datainitstructure;
  741. HAL_SD_ErrorTypedef errorstate = SD_OK;
  742. /* Initialize data control register */
  743. hsd->Instance->DCTRL = 0;
  744. /* Initialize handle flags */
  745. hsd->SdTransferCplt = 0;
  746. hsd->DmaTransferCplt = 0;
  747. hsd->SdTransferErr = SD_OK;
  748. /* Initialize SD Read operation */
  749. if(NumberOfBlocks > 1)
  750. {
  751. hsd->SdOperation = SD_READ_MULTIPLE_BLOCK;
  752. }
  753. else
  754. {
  755. hsd->SdOperation = SD_READ_SINGLE_BLOCK;
  756. }
  757. /* Enable transfer interrupts */
  758. __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
  759. SDIO_IT_DTIMEOUT |\
  760. SDIO_IT_DATAEND |\
  761. SDIO_IT_RXOVERR |\
  762. SDIO_IT_STBITERR));
  763. /* Enable SDIO DMA transfer */
  764. __HAL_SD_SDIO_DMA_ENABLE();
  765. /* Configure DMA user callbacks */
  766. hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt;
  767. hsd->hdmarx->XferErrorCallback = SD_DMA_RxError;
  768. /* Enable the DMA Stream */
  769. HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks));
  770. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  771. {
  772. BlockSize = 512;
  773. ReadAddr /= 512;
  774. }
  775. /* Set Block Size for Card */
  776. sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
  777. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  778. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  779. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  780. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  781. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  782. /* Check for error conditions */
  783. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  784. if (errorstate != SD_OK)
  785. {
  786. return errorstate;
  787. }
  788. /* Configure the SD DPSM (Data Path State Machine) */
  789. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  790. sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
  791. sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  792. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  793. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  794. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  795. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  796. /* Check number of blocks command */
  797. if(NumberOfBlocks > 1)
  798. {
  799. /* Send CMD18 READ_MULT_BLOCK with argument data address */
  800. sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
  801. }
  802. else
  803. {
  804. /* Send CMD17 READ_SINGLE_BLOCK */
  805. sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
  806. }
  807. sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr;
  808. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  809. /* Check for error conditions */
  810. if(NumberOfBlocks > 1)
  811. {
  812. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
  813. }
  814. else
  815. {
  816. errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
  817. }
  818. /* Update the SD transfer error in SD handle */
  819. hsd->SdTransferErr = errorstate;
  820. return errorstate;
  821. }
  822. /**
  823. * @brief Writes block(s) to a specified address in a card. The Data transfer
  824. * is managed by DMA mode.
  825. * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
  826. * to check the completion of the write process (by SD current status polling).
  827. * @param hsd: SD handle
  828. * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
  829. * @param WriteAddr: Address from where data is to be read
  830. * @param BlockSize: the SD card Data block size
  831. * This parameter should be 512.
  832. * @param NumberOfBlocks: Number of blocks to write
  833. * @retval SD Card error state
  834. */
  835. HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
  836. {
  837. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  838. SDIO_DataInitTypeDef sdio_datainitstructure;
  839. HAL_SD_ErrorTypedef errorstate = SD_OK;
  840. /* Initialize data control register */
  841. hsd->Instance->DCTRL = 0;
  842. /* Initialize handle flags */
  843. hsd->SdTransferCplt = 0;
  844. hsd->DmaTransferCplt = 0;
  845. hsd->SdTransferErr = SD_OK;
  846. /* Initialize SD Write operation */
  847. if(NumberOfBlocks > 1)
  848. {
  849. hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK;
  850. }
  851. else
  852. {
  853. hsd->SdOperation = SD_WRITE_SINGLE_BLOCK;
  854. }
  855. /* Enable transfer interrupts */
  856. __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
  857. SDIO_IT_DTIMEOUT |\
  858. SDIO_IT_DATAEND |\
  859. SDIO_IT_TXUNDERR |\
  860. SDIO_IT_STBITERR));
  861. /* Configure DMA user callbacks */
  862. hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt;
  863. hsd->hdmatx->XferErrorCallback = SD_DMA_TxError;
  864. /* Enable the DMA Stream */
  865. HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks));
  866. /* Enable SDIO DMA transfer */
  867. __HAL_SD_SDIO_DMA_ENABLE();
  868. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  869. {
  870. BlockSize = 512;
  871. WriteAddr /= 512;
  872. }
  873. /* Set Block Size for Card */
  874. sdio_cmdinitstructure.Argument = (uint32_t)BlockSize;
  875. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  876. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  877. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  878. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  879. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  880. /* Check for error conditions */
  881. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  882. if (errorstate != SD_OK)
  883. {
  884. return errorstate;
  885. }
  886. /* Check number of blocks command */
  887. if(NumberOfBlocks <= 1)
  888. {
  889. /* Send CMD24 WRITE_SINGLE_BLOCK */
  890. sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
  891. }
  892. else
  893. {
  894. /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
  895. sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
  896. }
  897. sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr;
  898. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  899. /* Check for error conditions */
  900. if(NumberOfBlocks > 1)
  901. {
  902. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
  903. }
  904. else
  905. {
  906. errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
  907. }
  908. if (errorstate != SD_OK)
  909. {
  910. return errorstate;
  911. }
  912. /* Configure the SD DPSM (Data Path State Machine) */
  913. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  914. sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks;
  915. sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  916. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  917. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  918. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  919. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  920. hsd->SdTransferErr = errorstate;
  921. return errorstate;
  922. }
  923. /**
  924. * @brief This function waits until the SD DMA data read transfer is finished.
  925. * This API should be called after HAL_SD_ReadBlocks_DMA() function
  926. * to insure that all data sent by the card is already transferred by the
  927. * DMA controller.
  928. * @param hsd: SD handle
  929. * @param Timeout: Timeout duration
  930. * @retval SD Card error state
  931. */
  932. HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
  933. {
  934. HAL_SD_ErrorTypedef errorstate = SD_OK;
  935. uint32_t timeout = Timeout;
  936. uint32_t tmp1, tmp2;
  937. HAL_SD_ErrorTypedef tmp3;
  938. /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
  939. tmp1 = hsd->DmaTransferCplt;
  940. tmp2 = hsd->SdTransferCplt;
  941. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  942. while ((tmp1 == 0) && (tmp2 == 0) && (tmp3 == SD_OK) && (timeout > 0))
  943. {
  944. tmp1 = hsd->DmaTransferCplt;
  945. tmp2 = hsd->SdTransferCplt;
  946. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  947. timeout--;
  948. }
  949. timeout = Timeout;
  950. /* Wait until the Rx transfer is no longer active */
  951. while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXACT)) && (timeout > 0))
  952. {
  953. timeout--;
  954. }
  955. /* Send stop command in multiblock read */
  956. if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK)
  957. {
  958. errorstate = HAL_SD_StopTransfer(hsd);
  959. }
  960. if ((timeout == 0) && (errorstate == SD_OK))
  961. {
  962. errorstate = SD_DATA_TIMEOUT;
  963. }
  964. /* Clear all the static flags */
  965. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  966. /* Return error state */
  967. if (hsd->SdTransferErr != SD_OK)
  968. {
  969. return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
  970. }
  971. return errorstate;
  972. }
  973. /**
  974. * @brief This function waits until the SD DMA data write transfer is finished.
  975. * This API should be called after HAL_SD_WriteBlocks_DMA() function
  976. * to insure that all data sent by the card is already transferred by the
  977. * DMA controller.
  978. * @param hsd: SD handle
  979. * @param Timeout: Timeout duration
  980. * @retval SD Card error state
  981. */
  982. HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
  983. {
  984. HAL_SD_ErrorTypedef errorstate = SD_OK;
  985. uint32_t timeout = Timeout;
  986. uint32_t tmp1, tmp2;
  987. HAL_SD_ErrorTypedef tmp3;
  988. /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
  989. tmp1 = hsd->DmaTransferCplt;
  990. tmp2 = hsd->SdTransferCplt;
  991. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  992. while ((tmp1 == 0) && (tmp2 == 0) && (tmp3 == SD_OK) && (timeout > 0))
  993. {
  994. tmp1 = hsd->DmaTransferCplt;
  995. tmp2 = hsd->SdTransferCplt;
  996. tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
  997. timeout--;
  998. }
  999. timeout = Timeout;
  1000. /* Wait until the Tx transfer is no longer active */
  1001. while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXACT)) && (timeout > 0))
  1002. {
  1003. timeout--;
  1004. }
  1005. /* Send stop command in multiblock write */
  1006. if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK)
  1007. {
  1008. errorstate = HAL_SD_StopTransfer(hsd);
  1009. }
  1010. if ((timeout == 0) && (errorstate == SD_OK))
  1011. {
  1012. errorstate = SD_DATA_TIMEOUT;
  1013. }
  1014. /* Clear all the static flags */
  1015. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1016. /* Return error state */
  1017. if (hsd->SdTransferErr != SD_OK)
  1018. {
  1019. return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
  1020. }
  1021. /* Wait until write is complete */
  1022. while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK)
  1023. {
  1024. }
  1025. return errorstate;
  1026. }
  1027. /**
  1028. * @brief Erases the specified memory area of the given SD card.
  1029. * @param hsd: SD handle
  1030. * @param startaddr: Start byte address
  1031. * @param endaddr: End byte address
  1032. * @retval SD Card error state
  1033. */
  1034. HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
  1035. {
  1036. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1037. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  1038. uint32_t delay = 0;
  1039. __IO uint32_t maxdelay = 0;
  1040. uint8_t cardstate = 0;
  1041. /* Check if the card command class supports erase command */
  1042. if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0)
  1043. {
  1044. errorstate = SD_REQUEST_NOT_APPLICABLE;
  1045. return errorstate;
  1046. }
  1047. /* Get max delay value */
  1048. maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2);
  1049. if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  1050. {
  1051. errorstate = SD_LOCK_UNLOCK_FAILED;
  1052. return errorstate;
  1053. }
  1054. /* Get start and end block for high capacity cards */
  1055. if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  1056. {
  1057. startaddr /= 512;
  1058. endaddr /= 512;
  1059. }
  1060. /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1061. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  1062. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  1063. {
  1064. /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
  1065. sdio_cmdinitstructure.Argument =(uint32_t)startaddr;
  1066. sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START;
  1067. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  1068. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  1069. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  1070. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1071. /* Check for error conditions */
  1072. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START);
  1073. if (errorstate != SD_OK)
  1074. {
  1075. return errorstate;
  1076. }
  1077. /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
  1078. sdio_cmdinitstructure.Argument = (uint32_t)endaddr;
  1079. sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END;
  1080. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1081. /* Check for error conditions */
  1082. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END);
  1083. if (errorstate != SD_OK)
  1084. {
  1085. return errorstate;
  1086. }
  1087. }
  1088. /* Send CMD38 ERASE */
  1089. sdio_cmdinitstructure.Argument = 0;
  1090. sdio_cmdinitstructure.CmdIndex = SD_CMD_ERASE;
  1091. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1092. /* Check for error conditions */
  1093. errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE);
  1094. if (errorstate != SD_OK)
  1095. {
  1096. return errorstate;
  1097. }
  1098. for (; delay < maxdelay; delay++)
  1099. {
  1100. }
  1101. /* Wait untill the card is in programming state */
  1102. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  1103. delay = SD_DATATIMEOUT;
  1104. while ((delay > 0) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
  1105. {
  1106. errorstate = SD_IsCardProgramming(hsd, &cardstate);
  1107. delay--;
  1108. }
  1109. return errorstate;
  1110. }
  1111. /**
  1112. * @brief This function handles SD card interrupt request.
  1113. * @param hsd: SD handle
  1114. * @retval None
  1115. */
  1116. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  1117. {
  1118. /* Check for SDIO interrupt flags */
  1119. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DATAEND))
  1120. {
  1121. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_IT_DATAEND);
  1122. /* SD transfer is complete */
  1123. hsd->SdTransferCplt = 1;
  1124. /* No transfer error */
  1125. hsd->SdTransferErr = SD_OK;
  1126. HAL_SD_XferCpltCallback(hsd);
  1127. }
  1128. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DCRCFAIL))
  1129. {
  1130. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  1131. hsd->SdTransferErr = SD_DATA_CRC_FAIL;
  1132. HAL_SD_XferErrorCallback(hsd);
  1133. }
  1134. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DTIMEOUT))
  1135. {
  1136. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  1137. hsd->SdTransferErr = SD_DATA_TIMEOUT;
  1138. HAL_SD_XferErrorCallback(hsd);
  1139. }
  1140. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_RXOVERR))
  1141. {
  1142. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
  1143. hsd->SdTransferErr = SD_RX_OVERRUN;
  1144. HAL_SD_XferErrorCallback(hsd);
  1145. }
  1146. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_TXUNDERR))
  1147. {
  1148. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
  1149. hsd->SdTransferErr = SD_TX_UNDERRUN;
  1150. HAL_SD_XferErrorCallback(hsd);
  1151. }
  1152. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_STBITERR))
  1153. {
  1154. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
  1155. hsd->SdTransferErr = SD_START_BIT_ERR;
  1156. HAL_SD_XferErrorCallback(hsd);
  1157. }
  1158. else
  1159. {
  1160. /* No error flag set */
  1161. }
  1162. /* Disable all SDIO peripheral interrupt sources */
  1163. __HAL_SD_SDIO_DISABLE_IT(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |\
  1164. SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |\
  1165. SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
  1166. }
  1167. /**
  1168. * @brief SD end of transfer callback.
  1169. * @param hsd: SD handle
  1170. * @retval None
  1171. */
  1172. __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd)
  1173. {
  1174. /* NOTE : This function Should not be modified, when the callback is needed,
  1175. the HAL_SD_XferCpltCallback could be implemented in the user file
  1176. */
  1177. }
  1178. /**
  1179. * @brief SD Transfer Error callback.
  1180. * @param hsd: SD handle
  1181. * @retval None
  1182. */
  1183. __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd)
  1184. {
  1185. /* NOTE : This function Should not be modified, when the callback is needed,
  1186. the HAL_SD_XferErrorCallback could be implemented in the user file
  1187. */
  1188. }
  1189. /**
  1190. * @brief SD Transfer complete Rx callback in non blocking mode.
  1191. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1192. * the configuration information for the specified DMA module.
  1193. * @retval None
  1194. */
  1195. __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma)
  1196. {
  1197. /* NOTE : This function Should not be modified, when the callback is needed,
  1198. the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
  1199. */
  1200. }
  1201. /**
  1202. * @brief SD DMA transfer complete Rx error callback.
  1203. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1204. * the configuration information for the specified DMA module.
  1205. * @retval None
  1206. */
  1207. __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma)
  1208. {
  1209. /* NOTE : This function Should not be modified, when the callback is needed,
  1210. the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
  1211. */
  1212. }
  1213. /**
  1214. * @brief SD Transfer complete Tx callback in non blocking mode.
  1215. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1216. * the configuration information for the specified DMA module.
  1217. * @retval None
  1218. */
  1219. __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
  1220. {
  1221. /* NOTE : This function Should not be modified, when the callback is needed,
  1222. the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
  1223. */
  1224. }
  1225. /**
  1226. * @brief SD DMA transfer complete error Tx callback.
  1227. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1228. * the configuration information for the specified DMA module.
  1229. * @retval None
  1230. */
  1231. __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma)
  1232. {
  1233. /* NOTE : This function Should not be modified, when the callback is needed,
  1234. the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
  1235. */
  1236. }
  1237. /**
  1238. * @}
  1239. */
  1240. /** @defgroup SD_Exported_Functions_Group3 Peripheral Control functions
  1241. * @brief management functions
  1242. *
  1243. @verbatim
  1244. ==============================================================================
  1245. ##### Peripheral Control functions #####
  1246. ==============================================================================
  1247. [..]
  1248. This subsection provides a set of functions allowing to control the SD card
  1249. operations.
  1250. @endverbatim
  1251. * @{
  1252. */
  1253. /**
  1254. * @brief Returns information about specific card.
  1255. * @param hsd: SD handle
  1256. * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
  1257. * contains all SD cardinformation
  1258. * @retval SD Card error state
  1259. */
  1260. HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
  1261. {
  1262. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1263. uint32_t tmp = 0;
  1264. pCardInfo->CardType = (uint8_t)(hsd->CardType);
  1265. pCardInfo->RCA = (uint16_t)(hsd->RCA);
  1266. /* Byte 0 */
  1267. tmp = (hsd->CSD[0] & 0xFF000000) >> 24;
  1268. pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
  1269. pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
  1270. pCardInfo->SD_csd.Reserved1 = tmp & 0x03;
  1271. /* Byte 1 */
  1272. tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
  1273. pCardInfo->SD_csd.TAAC = (uint8_t)tmp;
  1274. /* Byte 2 */
  1275. tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
  1276. pCardInfo->SD_csd.NSAC = (uint8_t)tmp;
  1277. /* Byte 3 */
  1278. tmp = hsd->CSD[0] & 0x000000FF;
  1279. pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp;
  1280. /* Byte 4 */
  1281. tmp = (hsd->CSD[1] & 0xFF000000) >> 24;
  1282. pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4);
  1283. /* Byte 5 */
  1284. tmp = (hsd->CSD[1] & 0x00FF0000) >> 16;
  1285. pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
  1286. pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F);
  1287. /* Byte 6 */
  1288. tmp = (hsd->CSD[1] & 0x0000FF00) >> 8;
  1289. pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
  1290. pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
  1291. pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
  1292. pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
  1293. pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */
  1294. if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0))
  1295. {
  1296. pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
  1297. /* Byte 7 */
  1298. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
  1299. pCardInfo->SD_csd.DeviceSize |= (tmp) << 2;
  1300. /* Byte 8 */
  1301. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
  1302. pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
  1303. pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
  1304. pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
  1305. /* Byte 9 */
  1306. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
  1307. pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
  1308. pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
  1309. pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
  1310. /* Byte 10 */
  1311. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
  1312. pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
  1313. pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ;
  1314. pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
  1315. pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
  1316. pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
  1317. }
  1318. else if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
  1319. {
  1320. /* Byte 7 */
  1321. tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
  1322. pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
  1323. /* Byte 8 */
  1324. tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
  1325. pCardInfo->SD_csd.DeviceSize |= (tmp << 8);
  1326. /* Byte 9 */
  1327. tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
  1328. pCardInfo->SD_csd.DeviceSize |= (tmp);
  1329. /* Byte 10 */
  1330. tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
  1331. pCardInfo->CardCapacity = ((pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024;
  1332. pCardInfo->CardBlockSize = 512;
  1333. }
  1334. else
  1335. {
  1336. /* Not supported card type */
  1337. errorstate = SD_ERROR;
  1338. }
  1339. pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
  1340. pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
  1341. /* Byte 11 */
  1342. tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
  1343. pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
  1344. pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
  1345. /* Byte 12 */
  1346. tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000) >> 24);
  1347. pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
  1348. pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
  1349. pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
  1350. pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
  1351. /* Byte 13 */
  1352. tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
  1353. pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
  1354. pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
  1355. pCardInfo->SD_csd.Reserved3 = 0;
  1356. pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
  1357. /* Byte 14 */
  1358. tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
  1359. pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
  1360. pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
  1361. pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
  1362. pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
  1363. pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
  1364. pCardInfo->SD_csd.ECC = (tmp & 0x03);
  1365. /* Byte 15 */
  1366. tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
  1367. pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
  1368. pCardInfo->SD_csd.Reserved4 = 1;
  1369. /* Byte 0 */
  1370. tmp = (uint8_t)((hsd->CID[0] & 0xFF000000) >> 24);
  1371. pCardInfo->SD_cid.ManufacturerID = tmp;
  1372. /* Byte 1 */
  1373. tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
  1374. pCardInfo->SD_cid.OEM_AppliID = tmp << 8;
  1375. /* Byte 2 */
  1376. tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
  1377. pCardInfo->SD_cid.OEM_AppliID |= tmp;
  1378. /* Byte 3 */
  1379. tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
  1380. pCardInfo->SD_cid.ProdName1 = tmp << 24;
  1381. /* Byte 4 */
  1382. tmp = (uint8_t)((hsd->CID[1] & 0xFF000000) >> 24);
  1383. pCardInfo->SD_cid.ProdName1 |= tmp << 16;
  1384. /* Byte 5 */
  1385. tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
  1386. pCardInfo->SD_cid.ProdName1 |= tmp << 8;
  1387. /* Byte 6 */
  1388. tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
  1389. pCardInfo->SD_cid.ProdName1 |= tmp;
  1390. /* Byte 7 */
  1391. tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
  1392. pCardInfo->SD_cid.ProdName2 = tmp;
  1393. /* Byte 8 */
  1394. tmp = (uint8_t)((hsd->CID[2] & 0xFF000000) >> 24);
  1395. pCardInfo->SD_cid.ProdRev = tmp;
  1396. /* Byte 9 */
  1397. tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
  1398. pCardInfo->SD_cid.ProdSN = tmp << 24;
  1399. /* Byte 10 */
  1400. tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
  1401. pCardInfo->SD_cid.ProdSN |= tmp << 16;
  1402. /* Byte 11 */
  1403. tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
  1404. pCardInfo->SD_cid.ProdSN |= tmp << 8;
  1405. /* Byte 12 */
  1406. tmp = (uint8_t)((hsd->CID[3] & 0xFF000000) >> 24);
  1407. pCardInfo->SD_cid.ProdSN |= tmp;
  1408. /* Byte 13 */
  1409. tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
  1410. pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
  1411. pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
  1412. /* Byte 14 */
  1413. tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
  1414. pCardInfo->SD_cid.ManufactDate |= tmp;
  1415. /* Byte 15 */
  1416. tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
  1417. pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
  1418. pCardInfo->SD_cid.Reserved2 = 1;
  1419. return errorstate;
  1420. }
  1421. /**
  1422. * @brief Enables wide bus operation for the requested card if supported by
  1423. * card.
  1424. * @param hsd: SD handle
  1425. * @param WideMode: Specifies the SD card wide bus mode
  1426. * This parameter can be one of the following values:
  1427. * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
  1428. * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
  1429. * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
  1430. * @retval SD Card error state
  1431. */
  1432. HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode)
  1433. {
  1434. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1435. SDIO_InitTypeDef init;
  1436. /* MMC Card does not support this feature */
  1437. if (hsd->CardType == MULTIMEDIA_CARD)
  1438. {
  1439. errorstate = SD_UNSUPPORTED_FEATURE;
  1440. }
  1441. else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  1442. (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  1443. {
  1444. if (WideMode == SDIO_BUS_WIDE_8B)
  1445. {
  1446. errorstate = SD_UNSUPPORTED_FEATURE;
  1447. }
  1448. else if (WideMode == SDIO_BUS_WIDE_4B)
  1449. {
  1450. errorstate = SD_WideBus_Enable(hsd);
  1451. }
  1452. else if (WideMode == SDIO_BUS_WIDE_1B)
  1453. {
  1454. errorstate = SD_WideBus_Disable(hsd);
  1455. }
  1456. else
  1457. {
  1458. /* WideMode is not a valid argument*/
  1459. errorstate = SD_INVALID_PARAMETER;
  1460. }
  1461. if (errorstate == SD_OK)
  1462. {
  1463. /* Configure the SDIO peripheral */
  1464. init.ClockEdge = hsd->Init.ClockEdge;
  1465. init.ClockBypass = hsd->Init.ClockBypass;
  1466. init.ClockPowerSave = hsd->Init.ClockPowerSave;
  1467. init.BusWide = WideMode;
  1468. init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
  1469. init.ClockDiv = hsd->Init.ClockDiv;
  1470. /* Configure SDIO peripheral interface */
  1471. SDIO_Init(hsd->Instance, init);
  1472. }
  1473. else
  1474. {
  1475. /* An error occured while enabling/disabling the wide bus*/
  1476. }
  1477. }
  1478. else
  1479. {
  1480. /* Not supported card type */
  1481. errorstate = SD_ERROR;
  1482. }
  1483. return errorstate;
  1484. }
  1485. /**
  1486. * @brief Aborts an ongoing data transfer.
  1487. * @param hsd: SD handle
  1488. * @retval SD Card error state
  1489. */
  1490. HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd)
  1491. {
  1492. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  1493. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1494. /* Send CMD12 STOP_TRANSMISSION */
  1495. sdio_cmdinitstructure.Argument = 0;
  1496. sdio_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION;
  1497. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  1498. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  1499. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  1500. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1501. /* Check for error conditions */
  1502. errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION);
  1503. return errorstate;
  1504. }
  1505. /**
  1506. * @brief Switches the SD card to High Speed mode.
  1507. * This API must be used after "Transfer State"
  1508. * @note This operation should be followed by the configuration
  1509. * of PLL to have SDIOCK clock between 67 and 75 MHz
  1510. * @param hsd: SD handle
  1511. * @retval SD Card error state
  1512. */
  1513. HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
  1514. {
  1515. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1516. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  1517. SDIO_DataInitTypeDef sdio_datainitstructure;
  1518. uint8_t SD_hs[64] = {0};
  1519. uint32_t SD_scr[2] = {0, 0};
  1520. uint32_t SD_SPEC = 0 ;
  1521. uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs;
  1522. /* Initialize the Data control register */
  1523. hsd->Instance->DCTRL = 0;
  1524. /* Get SCR Register */
  1525. errorstate = SD_FindSCR(hsd, SD_scr);
  1526. if (errorstate != SD_OK)
  1527. {
  1528. return errorstate;
  1529. }
  1530. /* Test the Version supported by the card*/
  1531. SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000);
  1532. if (SD_SPEC != SD_ALLZERO)
  1533. {
  1534. /* Set Block Size for Card */
  1535. sdio_cmdinitstructure.Argument = (uint32_t)64;
  1536. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  1537. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  1538. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  1539. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  1540. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1541. /* Check for error conditions */
  1542. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  1543. if (errorstate != SD_OK)
  1544. {
  1545. return errorstate;
  1546. }
  1547. /* Configure the SD DPSM (Data Path State Machine) */
  1548. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  1549. sdio_datainitstructure.DataLength = 64;
  1550. sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B ;
  1551. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  1552. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  1553. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  1554. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  1555. /* Send CMD6 switch mode */
  1556. sdio_cmdinitstructure.Argument = 0x80FFFF01;
  1557. sdio_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH;
  1558. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1559. /* Check for error conditions */
  1560. errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH);
  1561. if (errorstate != SD_OK)
  1562. {
  1563. return errorstate;
  1564. }
  1565. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
  1566. {
  1567. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
  1568. {
  1569. for (count = 0; count < 8; count++)
  1570. {
  1571. *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
  1572. }
  1573. tempbuff += 8;
  1574. }
  1575. }
  1576. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  1577. {
  1578. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  1579. errorstate = SD_DATA_TIMEOUT;
  1580. return errorstate;
  1581. }
  1582. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  1583. {
  1584. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  1585. errorstate = SD_DATA_CRC_FAIL;
  1586. return errorstate;
  1587. }
  1588. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  1589. {
  1590. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
  1591. errorstate = SD_RX_OVERRUN;
  1592. return errorstate;
  1593. }
  1594. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
  1595. {
  1596. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
  1597. errorstate = SD_START_BIT_ERR;
  1598. return errorstate;
  1599. }
  1600. else
  1601. {
  1602. /* No error flag set */
  1603. }
  1604. count = SD_DATATIMEOUT;
  1605. while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
  1606. {
  1607. *tempbuff = SDIO_ReadFIFO(hsd->Instance);
  1608. tempbuff++;
  1609. count--;
  1610. }
  1611. /* Clear all the static flags */
  1612. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1613. /* Test if the switch mode HS is ok */
  1614. if ((SD_hs[13]& 2) != 2)
  1615. {
  1616. errorstate = SD_UNSUPPORTED_FEATURE;
  1617. }
  1618. }
  1619. return errorstate;
  1620. }
  1621. /**
  1622. * @}
  1623. */
  1624. /** @defgroup SD_Exported_Functions_Group4 Peripheral State functions
  1625. * @brief Peripheral State functions
  1626. *
  1627. @verbatim
  1628. ==============================================================================
  1629. ##### Peripheral State functions #####
  1630. ==============================================================================
  1631. [..]
  1632. This subsection permits to get in runtime the status of the peripheral
  1633. and the data flow.
  1634. @endverbatim
  1635. * @{
  1636. */
  1637. /**
  1638. * @brief Returns the current SD card's status.
  1639. * @param hsd: SD handle
  1640. * @param pSDstatus: Pointer to the buffer that will contain the SD card status
  1641. * SD Status register)
  1642. * @retval SD Card error state
  1643. */
  1644. HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
  1645. {
  1646. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  1647. SDIO_DataInitTypeDef sdio_datainitstructure;
  1648. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1649. uint32_t count = 0;
  1650. /* Check SD response */
  1651. if ((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  1652. {
  1653. errorstate = SD_LOCK_UNLOCK_FAILED;
  1654. return errorstate;
  1655. }
  1656. /* Set block size for card if it is not equal to current block size for card */
  1657. sdio_cmdinitstructure.Argument = 64;
  1658. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  1659. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  1660. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  1661. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  1662. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1663. /* Check for error conditions */
  1664. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  1665. if (errorstate != SD_OK)
  1666. {
  1667. return errorstate;
  1668. }
  1669. /* Send CMD55 */
  1670. sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  1671. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  1672. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1673. /* Check for error conditions */
  1674. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  1675. if (errorstate != SD_OK)
  1676. {
  1677. return errorstate;
  1678. }
  1679. /* Configure the SD DPSM (Data Path State Machine) */
  1680. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  1681. sdio_datainitstructure.DataLength = 64;
  1682. sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
  1683. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  1684. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  1685. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  1686. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  1687. /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
  1688. sdio_cmdinitstructure.Argument = 0;
  1689. sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STAUS;
  1690. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1691. /* Check for error conditions */
  1692. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STAUS);
  1693. if (errorstate != SD_OK)
  1694. {
  1695. return errorstate;
  1696. }
  1697. /* Get status data */
  1698. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
  1699. {
  1700. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
  1701. {
  1702. for (count = 0; count < 8; count++)
  1703. {
  1704. *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance);
  1705. }
  1706. pSDstatus += 8;
  1707. }
  1708. }
  1709. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  1710. {
  1711. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  1712. errorstate = SD_DATA_TIMEOUT;
  1713. return errorstate;
  1714. }
  1715. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  1716. {
  1717. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  1718. errorstate = SD_DATA_CRC_FAIL;
  1719. return errorstate;
  1720. }
  1721. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  1722. {
  1723. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
  1724. errorstate = SD_RX_OVERRUN;
  1725. return errorstate;
  1726. }
  1727. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
  1728. {
  1729. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
  1730. errorstate = SD_START_BIT_ERR;
  1731. return errorstate;
  1732. }
  1733. else
  1734. {
  1735. /* No error flag set */
  1736. }
  1737. count = SD_DATATIMEOUT;
  1738. while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
  1739. {
  1740. *pSDstatus = SDIO_ReadFIFO(hsd->Instance);
  1741. pSDstatus++;
  1742. count--;
  1743. }
  1744. /* Clear all the static status flags*/
  1745. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1746. return errorstate;
  1747. }
  1748. /**
  1749. * @brief Gets the current sd card data status.
  1750. * @param hsd: SD handle
  1751. * @retval Data Transfer state
  1752. */
  1753. HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd)
  1754. {
  1755. HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER;
  1756. /* Get SD card state */
  1757. cardstate = SD_GetState(hsd);
  1758. /* Find SD status according to card state*/
  1759. if (cardstate == SD_CARD_TRANSFER)
  1760. {
  1761. return SD_TRANSFER_OK;
  1762. }
  1763. else if(cardstate == SD_CARD_ERROR)
  1764. {
  1765. return SD_TRANSFER_ERROR;
  1766. }
  1767. else
  1768. {
  1769. return SD_TRANSFER_BUSY;
  1770. }
  1771. }
  1772. /**
  1773. * @brief Gets the SD card status.
  1774. * @param hsd: SD handle
  1775. * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
  1776. * will contain the SD card status information
  1777. * @retval SD Card error state
  1778. */
  1779. HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
  1780. {
  1781. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1782. uint32_t tmp = 0;
  1783. uint32_t sd_status[16];
  1784. errorstate = HAL_SD_SendSDStatus(hsd, sd_status);
  1785. if (errorstate != SD_OK)
  1786. {
  1787. return errorstate;
  1788. }
  1789. /* Byte 0 */
  1790. tmp = (sd_status[0] & 0xC0) >> 6;
  1791. pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp;
  1792. /* Byte 0 */
  1793. tmp = (sd_status[0] & 0x20) >> 5;
  1794. pCardStatus->SECURED_MODE = (uint8_t)tmp;
  1795. /* Byte 2 */
  1796. tmp = (sd_status[2] & 0xFF);
  1797. pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8);
  1798. /* Byte 3 */
  1799. tmp = (sd_status[3] & 0xFF);
  1800. pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp;
  1801. /* Byte 4 */
  1802. tmp = (sd_status[4] & 0xFF);
  1803. pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24);
  1804. /* Byte 5 */
  1805. tmp = (sd_status[5] & 0xFF);
  1806. pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16);
  1807. /* Byte 6 */
  1808. tmp = (sd_status[6] & 0xFF);
  1809. pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8);
  1810. /* Byte 7 */
  1811. tmp = (sd_status[7] & 0xFF);
  1812. pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp;
  1813. /* Byte 8 */
  1814. tmp = (sd_status[8] & 0xFF);
  1815. pCardStatus->SPEED_CLASS = (uint8_t)tmp;
  1816. /* Byte 9 */
  1817. tmp = (sd_status[9] & 0xFF);
  1818. pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp;
  1819. /* Byte 10 */
  1820. tmp = (sd_status[10] & 0xF0) >> 4;
  1821. pCardStatus->AU_SIZE = (uint8_t)tmp;
  1822. /* Byte 11 */
  1823. tmp = (sd_status[11] & 0xFF);
  1824. pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8);
  1825. /* Byte 12 */
  1826. tmp = (sd_status[12] & 0xFF);
  1827. pCardStatus->ERASE_SIZE |= (uint8_t)tmp;
  1828. /* Byte 13 */
  1829. tmp = (sd_status[13] & 0xFC) >> 2;
  1830. pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp;
  1831. /* Byte 13 */
  1832. tmp = (sd_status[13] & 0x3);
  1833. pCardStatus->ERASE_OFFSET = (uint8_t)tmp;
  1834. return errorstate;
  1835. }
  1836. /**
  1837. * @}
  1838. */
  1839. /**
  1840. * @}
  1841. */
  1842. /** @addtogroup SD_Private_Functions
  1843. * @{
  1844. */
  1845. /**
  1846. * @brief SD DMA transfer complete Rx callback.
  1847. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1848. * the configuration information for the specified DMA module.
  1849. * @retval None
  1850. */
  1851. static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma)
  1852. {
  1853. SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1854. /* DMA transfer is complete */
  1855. hsd->DmaTransferCplt = 1;
  1856. /* Wait until SD transfer is complete */
  1857. while(hsd->SdTransferCplt == 0)
  1858. {
  1859. }
  1860. /* Transfer complete user callback */
  1861. HAL_SD_DMA_RxCpltCallback(hsd->hdmarx);
  1862. }
  1863. /**
  1864. * @brief SD DMA transfer Error Rx callback.
  1865. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1866. * the configuration information for the specified DMA module.
  1867. * @retval None
  1868. */
  1869. static void SD_DMA_RxError(DMA_HandleTypeDef *hdma)
  1870. {
  1871. SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1872. /* Transfer complete user callback */
  1873. HAL_SD_DMA_RxErrorCallback(hsd->hdmarx);
  1874. }
  1875. /**
  1876. * @brief SD DMA transfer complete Tx callback.
  1877. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1878. * the configuration information for the specified DMA module.
  1879. * @retval None
  1880. */
  1881. static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
  1882. {
  1883. SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1884. /* DMA transfer is complete */
  1885. hsd->DmaTransferCplt = 1;
  1886. /* Wait until SD transfer is complete */
  1887. while(hsd->SdTransferCplt == 0)
  1888. {
  1889. }
  1890. /* Transfer complete user callback */
  1891. HAL_SD_DMA_TxCpltCallback(hsd->hdmatx);
  1892. }
  1893. /**
  1894. * @brief SD DMA transfer Error Tx callback.
  1895. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1896. * the configuration information for the specified DMA module.
  1897. * @retval None
  1898. */
  1899. static void SD_DMA_TxError(DMA_HandleTypeDef *hdma)
  1900. {
  1901. SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1902. /* Transfer complete user callback */
  1903. HAL_SD_DMA_TxErrorCallback(hsd->hdmatx);
  1904. }
  1905. /**
  1906. * @brief Returns the SD current state.
  1907. * @param hsd: SD handle
  1908. * @retval SD card current state
  1909. */
  1910. static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd)
  1911. {
  1912. uint32_t resp1 = 0;
  1913. if (SD_SendStatus(hsd, &resp1) != SD_OK)
  1914. {
  1915. return SD_CARD_ERROR;
  1916. }
  1917. else
  1918. {
  1919. return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F);
  1920. }
  1921. }
  1922. /**
  1923. * @brief Initializes all cards or single card as the case may be Card(s) come
  1924. * into standby state.
  1925. * @param hsd: SD handle
  1926. * @retval SD Card error state
  1927. */
  1928. static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd)
  1929. {
  1930. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  1931. HAL_SD_ErrorTypedef errorstate = SD_OK;
  1932. uint16_t sd_rca = 1;
  1933. if(SDIO_GetPowerState(hsd->Instance) == 0) /* Power off */
  1934. {
  1935. errorstate = SD_REQUEST_NOT_APPLICABLE;
  1936. return errorstate;
  1937. }
  1938. if(hsd->CardType != SECURE_DIGITAL_IO_CARD)
  1939. {
  1940. /* Send CMD2 ALL_SEND_CID */
  1941. sdio_cmdinitstructure.Argument = 0;
  1942. sdio_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID;
  1943. sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG;
  1944. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  1945. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  1946. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1947. /* Check for error conditions */
  1948. errorstate = SD_CmdResp2Error(hsd);
  1949. if(errorstate != SD_OK)
  1950. {
  1951. return errorstate;
  1952. }
  1953. /* Get Card identification number data */
  1954. hsd->CID[0] = SDIO_GetResponse(SDIO_RESP1);
  1955. hsd->CID[1] = SDIO_GetResponse(SDIO_RESP2);
  1956. hsd->CID[2] = SDIO_GetResponse(SDIO_RESP3);
  1957. hsd->CID[3] = SDIO_GetResponse(SDIO_RESP4);
  1958. }
  1959. if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
  1960. (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD))
  1961. {
  1962. /* Send CMD3 SET_REL_ADDR with argument 0 */
  1963. /* SD Card publishes its RCA. */
  1964. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR;
  1965. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  1966. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1967. /* Check for error conditions */
  1968. errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca);
  1969. if(errorstate != SD_OK)
  1970. {
  1971. return errorstate;
  1972. }
  1973. }
  1974. if (hsd->CardType != SECURE_DIGITAL_IO_CARD)
  1975. {
  1976. /* Get the SD card RCA */
  1977. hsd->RCA = sd_rca;
  1978. /* Send CMD9 SEND_CSD with argument as card's RCA */
  1979. sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  1980. sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD;
  1981. sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG;
  1982. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  1983. /* Check for error conditions */
  1984. errorstate = SD_CmdResp2Error(hsd);
  1985. if(errorstate != SD_OK)
  1986. {
  1987. return errorstate;
  1988. }
  1989. /* Get Card Specific Data */
  1990. hsd->CSD[0] = SDIO_GetResponse(SDIO_RESP1);
  1991. hsd->CSD[1] = SDIO_GetResponse(SDIO_RESP2);
  1992. hsd->CSD[2] = SDIO_GetResponse(SDIO_RESP3);
  1993. hsd->CSD[3] = SDIO_GetResponse(SDIO_RESP4);
  1994. }
  1995. /* All cards are initialized */
  1996. return errorstate;
  1997. }
  1998. /**
  1999. * @brief Selects od Deselects the corresponding card.
  2000. * @param hsd: SD handle
  2001. * @param addr: Address of the card to be selected
  2002. * @retval SD Card error state
  2003. */
  2004. static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr)
  2005. {
  2006. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2007. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2008. /* Send CMD7 SDIO_SEL_DESEL_CARD */
  2009. sdio_cmdinitstructure.Argument = (uint32_t)addr;
  2010. sdio_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD;
  2011. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2012. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2013. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2014. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2015. /* Check for error conditions */
  2016. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD);
  2017. return errorstate;
  2018. }
  2019. /**
  2020. * @brief Enquires cards about their operating voltage and configures clock
  2021. * controls and stores SD information that will be needed in future
  2022. * in the SD handle.
  2023. * @param hsd: SD handle
  2024. * @retval SD Card error state
  2025. */
  2026. static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd)
  2027. {
  2028. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2029. __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
  2030. uint32_t response = 0, count = 0, validvoltage = 0;
  2031. uint32_t sdtype = SD_STD_CAPACITY;
  2032. /* Power ON Sequence -------------------------------------------------------*/
  2033. /* Disable SDIO Clock */
  2034. __HAL_SD_SDIO_DISABLE();
  2035. /* Set Power State to ON */
  2036. SDIO_PowerState_ON(hsd->Instance);
  2037. /* Enable SDIO Clock */
  2038. __HAL_SD_SDIO_ENABLE();
  2039. /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
  2040. /* No CMD response required */
  2041. sdio_cmdinitstructure.Argument = 0;
  2042. sdio_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE;
  2043. sdio_cmdinitstructure.Response = SDIO_RESPONSE_NO;
  2044. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2045. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2046. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2047. /* Check for error conditions */
  2048. errorstate = SD_CmdError(hsd);
  2049. if(errorstate != SD_OK)
  2050. {
  2051. /* CMD Response TimeOut (wait for CMDSENT flag) */
  2052. return errorstate;
  2053. }
  2054. /* CMD8: SEND_IF_COND ------------------------------------------------------*/
  2055. /* Send CMD8 to verify SD card interface operating condition */
  2056. /* Argument: - [31:12]: Reserved (shall be set to '0')
  2057. - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
  2058. - [7:0]: Check Pattern (recommended 0xAA) */
  2059. /* CMD Response: R7 */
  2060. sdio_cmdinitstructure.Argument = SD_CHECK_PATTERN;
  2061. sdio_cmdinitstructure.CmdIndex = SD_SDIO_SEND_IF_COND;
  2062. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2063. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2064. /* Check for error conditions */
  2065. errorstate = SD_CmdResp7Error(hsd);
  2066. if (errorstate == SD_OK)
  2067. {
  2068. /* SD Card 2.0 */
  2069. hsd->CardType = STD_CAPACITY_SD_CARD_V2_0;
  2070. sdtype = SD_HIGH_CAPACITY;
  2071. }
  2072. /* Send CMD55 */
  2073. sdio_cmdinitstructure.Argument = 0;
  2074. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2075. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2076. /* Check for error conditions */
  2077. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2078. /* If errorstate is Command TimeOut, it is a MMC card */
  2079. /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
  2080. or SD card 1.x */
  2081. if(errorstate == SD_OK)
  2082. {
  2083. /* SD CARD */
  2084. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2085. while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
  2086. {
  2087. /* SEND CMD55 APP_CMD with RCA as 0 */
  2088. sdio_cmdinitstructure.Argument = 0;
  2089. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2090. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2091. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2092. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2093. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2094. /* Check for error conditions */
  2095. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2096. if(errorstate != SD_OK)
  2097. {
  2098. return errorstate;
  2099. }
  2100. /* Send CMD41 */
  2101. sdio_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype;
  2102. sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND;
  2103. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2104. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2105. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2106. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2107. /* Check for error conditions */
  2108. errorstate = SD_CmdResp3Error(hsd);
  2109. if(errorstate != SD_OK)
  2110. {
  2111. return errorstate;
  2112. }
  2113. /* Get command response */
  2114. response = SDIO_GetResponse(SDIO_RESP1);
  2115. /* Get operating voltage*/
  2116. validvoltage = (((response >> 31) == 1) ? 1 : 0);
  2117. count++;
  2118. }
  2119. if(count >= SD_MAX_VOLT_TRIAL)
  2120. {
  2121. errorstate = SD_INVALID_VOLTRANGE;
  2122. return errorstate;
  2123. }
  2124. if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  2125. {
  2126. hsd->CardType = HIGH_CAPACITY_SD_CARD;
  2127. }
  2128. } /* else MMC Card */
  2129. return errorstate;
  2130. }
  2131. /**
  2132. * @brief Turns the SDIO output signals off.
  2133. * @param hsd: SD handle
  2134. * @retval SD Card error state
  2135. */
  2136. static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd)
  2137. {
  2138. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2139. /* Set Power State to OFF */
  2140. SDIO_PowerState_OFF(hsd->Instance);
  2141. return errorstate;
  2142. }
  2143. /**
  2144. * @brief Returns the current card's status.
  2145. * @param hsd: SD handle
  2146. * @param pCardStatus: pointer to the buffer that will contain the SD card
  2147. * status (Card Status register)
  2148. * @retval SD Card error state
  2149. */
  2150. static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
  2151. {
  2152. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2153. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2154. if(pCardStatus == NULL)
  2155. {
  2156. errorstate = SD_INVALID_PARAMETER;
  2157. return errorstate;
  2158. }
  2159. /* Send Status command */
  2160. sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2161. sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
  2162. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2163. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2164. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2165. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2166. /* Check for error conditions */
  2167. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS);
  2168. if(errorstate != SD_OK)
  2169. {
  2170. return errorstate;
  2171. }
  2172. /* Get SD card status */
  2173. *pCardStatus = SDIO_GetResponse(SDIO_RESP1);
  2174. return errorstate;
  2175. }
  2176. /**
  2177. * @brief Checks for error conditions for CMD0.
  2178. * @param hsd: SD handle
  2179. * @retval SD Card error state
  2180. */
  2181. static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd)
  2182. {
  2183. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2184. uint32_t timeout, tmp;
  2185. timeout = SDIO_CMD0TIMEOUT;
  2186. tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
  2187. while((timeout > 0) && (!tmp))
  2188. {
  2189. tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
  2190. timeout--;
  2191. }
  2192. if(timeout == 0)
  2193. {
  2194. errorstate = SD_CMD_RSP_TIMEOUT;
  2195. return errorstate;
  2196. }
  2197. /* Clear all the static flags */
  2198. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2199. return errorstate;
  2200. }
  2201. /**
  2202. * @brief Checks for error conditions for R7 response.
  2203. * @param hsd: SD handle
  2204. * @retval SD Card error state
  2205. */
  2206. static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd)
  2207. {
  2208. HAL_SD_ErrorTypedef errorstate = SD_ERROR;
  2209. uint32_t timeout = SDIO_CMD0TIMEOUT, tmp;
  2210. tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
  2211. while((!tmp) && (timeout > 0))
  2212. {
  2213. tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
  2214. timeout--;
  2215. }
  2216. tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2217. if((timeout == 0) || tmp)
  2218. {
  2219. /* Card is not V2.0 compliant or card does not support the set voltage range */
  2220. errorstate = SD_CMD_RSP_TIMEOUT;
  2221. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2222. return errorstate;
  2223. }
  2224. if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDREND))
  2225. {
  2226. /* Card is SD V2.0 compliant */
  2227. errorstate = SD_OK;
  2228. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CMDREND);
  2229. return errorstate;
  2230. }
  2231. return errorstate;
  2232. }
  2233. /**
  2234. * @brief Checks for error conditions for R1 response.
  2235. * @param hsd: SD handle
  2236. * @param SD_CMD: The sent command index
  2237. * @retval SD Card error state
  2238. */
  2239. static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD)
  2240. {
  2241. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2242. uint32_t response_r1;
  2243. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
  2244. {
  2245. }
  2246. if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
  2247. {
  2248. errorstate = SD_CMD_RSP_TIMEOUT;
  2249. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2250. return errorstate;
  2251. }
  2252. else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
  2253. {
  2254. errorstate = SD_CMD_CRC_FAIL;
  2255. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
  2256. return errorstate;
  2257. }
  2258. else
  2259. {
  2260. /* No error flag set */
  2261. }
  2262. /* Check response received is of desired command */
  2263. if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
  2264. {
  2265. errorstate = SD_ILLEGAL_CMD;
  2266. return errorstate;
  2267. }
  2268. /* Clear all the static flags */
  2269. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2270. /* We have received response, retrieve it for analysis */
  2271. response_r1 = SDIO_GetResponse(SDIO_RESP1);
  2272. if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
  2273. {
  2274. return errorstate;
  2275. }
  2276. if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
  2277. {
  2278. return(SD_ADDR_OUT_OF_RANGE);
  2279. }
  2280. if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
  2281. {
  2282. return(SD_ADDR_MISALIGNED);
  2283. }
  2284. if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
  2285. {
  2286. return(SD_BLOCK_LEN_ERR);
  2287. }
  2288. if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
  2289. {
  2290. return(SD_ERASE_SEQ_ERR);
  2291. }
  2292. if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
  2293. {
  2294. return(SD_BAD_ERASE_PARAM);
  2295. }
  2296. if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
  2297. {
  2298. return(SD_WRITE_PROT_VIOLATION);
  2299. }
  2300. if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
  2301. {
  2302. return(SD_LOCK_UNLOCK_FAILED);
  2303. }
  2304. if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
  2305. {
  2306. return(SD_COM_CRC_FAILED);
  2307. }
  2308. if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
  2309. {
  2310. return(SD_ILLEGAL_CMD);
  2311. }
  2312. if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
  2313. {
  2314. return(SD_CARD_ECC_FAILED);
  2315. }
  2316. if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
  2317. {
  2318. return(SD_CC_ERROR);
  2319. }
  2320. if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
  2321. {
  2322. return(SD_GENERAL_UNKNOWN_ERROR);
  2323. }
  2324. if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
  2325. {
  2326. return(SD_STREAM_READ_UNDERRUN);
  2327. }
  2328. if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
  2329. {
  2330. return(SD_STREAM_WRITE_OVERRUN);
  2331. }
  2332. if((response_r1 & SD_OCR_CID_CSD_OVERWRIETE) == SD_OCR_CID_CSD_OVERWRIETE)
  2333. {
  2334. return(SD_CID_CSD_OVERWRITE);
  2335. }
  2336. if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
  2337. {
  2338. return(SD_WP_ERASE_SKIP);
  2339. }
  2340. if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
  2341. {
  2342. return(SD_CARD_ECC_DISABLED);
  2343. }
  2344. if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
  2345. {
  2346. return(SD_ERASE_RESET);
  2347. }
  2348. if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
  2349. {
  2350. return(SD_AKE_SEQ_ERROR);
  2351. }
  2352. return errorstate;
  2353. }
  2354. /**
  2355. * @brief Checks for error conditions for R3 (OCR) response.
  2356. * @param hsd: SD handle
  2357. * @retval SD Card error state
  2358. */
  2359. static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd)
  2360. {
  2361. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2362. while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
  2363. {
  2364. }
  2365. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
  2366. {
  2367. errorstate = SD_CMD_RSP_TIMEOUT;
  2368. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2369. return errorstate;
  2370. }
  2371. /* Clear all the static flags */
  2372. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2373. return errorstate;
  2374. }
  2375. /**
  2376. * @brief Checks for error conditions for R2 (CID or CSD) response.
  2377. * @param hsd: SD handle
  2378. * @retval SD Card error state
  2379. */
  2380. static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd)
  2381. {
  2382. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2383. while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
  2384. {
  2385. }
  2386. if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
  2387. {
  2388. errorstate = SD_CMD_RSP_TIMEOUT;
  2389. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2390. return errorstate;
  2391. }
  2392. else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
  2393. {
  2394. errorstate = SD_CMD_CRC_FAIL;
  2395. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
  2396. return errorstate;
  2397. }
  2398. else
  2399. {
  2400. /* No error flag set */
  2401. }
  2402. /* Clear all the static flags */
  2403. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2404. return errorstate;
  2405. }
  2406. /**
  2407. * @brief Checks for error conditions for R6 (RCA) response.
  2408. * @param hsd: SD handle
  2409. * @param SD_CMD: The sent command index
  2410. * @param pRCA: Pointer to the variable that will contain the SD card relative
  2411. * address RCA
  2412. * @retval SD Card error state
  2413. */
  2414. static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
  2415. {
  2416. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2417. uint32_t response_r1;
  2418. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
  2419. {
  2420. }
  2421. if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
  2422. {
  2423. errorstate = SD_CMD_RSP_TIMEOUT;
  2424. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2425. return errorstate;
  2426. }
  2427. else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
  2428. {
  2429. errorstate = SD_CMD_CRC_FAIL;
  2430. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
  2431. return errorstate;
  2432. }
  2433. else
  2434. {
  2435. /* No error flag set */
  2436. }
  2437. /* Check response received is of desired command */
  2438. if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
  2439. {
  2440. errorstate = SD_ILLEGAL_CMD;
  2441. return errorstate;
  2442. }
  2443. /* Clear all the static flags */
  2444. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2445. /* We have received response, retrieve it. */
  2446. response_r1 = SDIO_GetResponse(SDIO_RESP1);
  2447. if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO)
  2448. {
  2449. *pRCA = (uint16_t) (response_r1 >> 16);
  2450. return errorstate;
  2451. }
  2452. if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR)
  2453. {
  2454. return(SD_GENERAL_UNKNOWN_ERROR);
  2455. }
  2456. if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD)
  2457. {
  2458. return(SD_ILLEGAL_CMD);
  2459. }
  2460. if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED)
  2461. {
  2462. return(SD_COM_CRC_FAILED);
  2463. }
  2464. return errorstate;
  2465. }
  2466. /**
  2467. * @brief Enables the SDIO wide bus mode.
  2468. * @param hsd: SD handle
  2469. * @retval SD Card error state
  2470. */
  2471. static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd)
  2472. {
  2473. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2474. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2475. uint32_t scr[2] = {0, 0};
  2476. if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  2477. {
  2478. errorstate = SD_LOCK_UNLOCK_FAILED;
  2479. return errorstate;
  2480. }
  2481. /* Get SCR Register */
  2482. errorstate = SD_FindSCR(hsd, scr);
  2483. if(errorstate != SD_OK)
  2484. {
  2485. return errorstate;
  2486. }
  2487. /* If requested card supports wide bus operation */
  2488. if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
  2489. {
  2490. /* Send CMD55 APP_CMD with argument as card's RCA.*/
  2491. sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2492. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2493. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2494. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2495. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2496. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2497. /* Check for error conditions */
  2498. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2499. if(errorstate != SD_OK)
  2500. {
  2501. return errorstate;
  2502. }
  2503. /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  2504. sdio_cmdinitstructure.Argument = 2;
  2505. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  2506. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2507. /* Check for error conditions */
  2508. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
  2509. if(errorstate != SD_OK)
  2510. {
  2511. return errorstate;
  2512. }
  2513. return errorstate;
  2514. }
  2515. else
  2516. {
  2517. errorstate = SD_REQUEST_NOT_APPLICABLE;
  2518. return errorstate;
  2519. }
  2520. }
  2521. /**
  2522. * @brief Disables the SDIO wide bus mode.
  2523. * @param hsd: SD handle
  2524. * @retval SD Card error state
  2525. */
  2526. static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd)
  2527. {
  2528. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2529. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2530. uint32_t scr[2] = {0, 0};
  2531. if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
  2532. {
  2533. errorstate = SD_LOCK_UNLOCK_FAILED;
  2534. return errorstate;
  2535. }
  2536. /* Get SCR Register */
  2537. errorstate = SD_FindSCR(hsd, scr);
  2538. if(errorstate != SD_OK)
  2539. {
  2540. return errorstate;
  2541. }
  2542. /* If requested card supports 1 bit mode operation */
  2543. if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
  2544. {
  2545. /* Send CMD55 APP_CMD with argument as card's RCA */
  2546. sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2547. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2548. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2549. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2550. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2551. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2552. /* Check for error conditions */
  2553. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2554. if(errorstate != SD_OK)
  2555. {
  2556. return errorstate;
  2557. }
  2558. /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
  2559. sdio_cmdinitstructure.Argument = 0;
  2560. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
  2561. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2562. /* Check for error conditions */
  2563. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
  2564. if(errorstate != SD_OK)
  2565. {
  2566. return errorstate;
  2567. }
  2568. return errorstate;
  2569. }
  2570. else
  2571. {
  2572. errorstate = SD_REQUEST_NOT_APPLICABLE;
  2573. return errorstate;
  2574. }
  2575. }
  2576. /**
  2577. * @brief Finds the SD card SCR register value.
  2578. * @param hsd: SD handle
  2579. * @param pSCR: pointer to the buffer that will contain the SCR value
  2580. * @retval SD Card error state
  2581. */
  2582. static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
  2583. {
  2584. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2585. SDIO_DataInitTypeDef sdio_datainitstructure;
  2586. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2587. uint32_t index = 0;
  2588. uint32_t tempscr[2] = {0, 0};
  2589. /* Set Block Size To 8 Bytes */
  2590. /* Send CMD55 APP_CMD with argument as card's RCA */
  2591. sdio_cmdinitstructure.Argument = (uint32_t)8;
  2592. sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
  2593. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2594. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2595. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2596. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2597. /* Check for error conditions */
  2598. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
  2599. if(errorstate != SD_OK)
  2600. {
  2601. return errorstate;
  2602. }
  2603. /* Send CMD55 APP_CMD with argument as card's RCA */
  2604. sdio_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16);
  2605. sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD;
  2606. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2607. /* Check for error conditions */
  2608. errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
  2609. if(errorstate != SD_OK)
  2610. {
  2611. return errorstate;
  2612. }
  2613. sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT;
  2614. sdio_datainitstructure.DataLength = 8;
  2615. sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
  2616. sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  2617. sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  2618. sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE;
  2619. SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure);
  2620. /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  2621. sdio_cmdinitstructure.Argument = 0;
  2622. sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR;
  2623. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2624. /* Check for error conditions */
  2625. errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR);
  2626. if(errorstate != SD_OK)
  2627. {
  2628. return errorstate;
  2629. }
  2630. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
  2631. {
  2632. if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
  2633. {
  2634. *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
  2635. index++;
  2636. }
  2637. }
  2638. if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  2639. {
  2640. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  2641. errorstate = SD_DATA_TIMEOUT;
  2642. return errorstate;
  2643. }
  2644. else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  2645. {
  2646. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  2647. errorstate = SD_DATA_CRC_FAIL;
  2648. return errorstate;
  2649. }
  2650. else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  2651. {
  2652. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
  2653. errorstate = SD_RX_OVERRUN;
  2654. return errorstate;
  2655. }
  2656. else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
  2657. {
  2658. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
  2659. errorstate = SD_START_BIT_ERR;
  2660. return errorstate;
  2661. }
  2662. else
  2663. {
  2664. /* No error flag set */
  2665. }
  2666. /* Clear all the static flags */
  2667. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2668. *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\
  2669. ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
  2670. *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\
  2671. ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
  2672. return errorstate;
  2673. }
  2674. /**
  2675. * @brief Checks if the SD card is in programming state.
  2676. * @param hsd: SD handle
  2677. * @param pStatus: pointer to the variable that will contain the SD card state
  2678. * @retval SD Card error state
  2679. */
  2680. static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus)
  2681. {
  2682. SDIO_CmdInitTypeDef sdio_cmdinitstructure;
  2683. HAL_SD_ErrorTypedef errorstate = SD_OK;
  2684. __IO uint32_t responseR1 = 0;
  2685. sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16);
  2686. sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS;
  2687. sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT;
  2688. sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO;
  2689. sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE;
  2690. SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure);
  2691. while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
  2692. {
  2693. }
  2694. if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
  2695. {
  2696. errorstate = SD_CMD_RSP_TIMEOUT;
  2697. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
  2698. return errorstate;
  2699. }
  2700. else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
  2701. {
  2702. errorstate = SD_CMD_CRC_FAIL;
  2703. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
  2704. return errorstate;
  2705. }
  2706. else
  2707. {
  2708. /* No error flag set */
  2709. }
  2710. /* Check response received is of desired command */
  2711. if((uint32_t)SDIO_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS)
  2712. {
  2713. errorstate = SD_ILLEGAL_CMD;
  2714. return errorstate;
  2715. }
  2716. /* Clear all the static flags */
  2717. __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2718. /* We have received response, retrieve it for analysis */
  2719. responseR1 = SDIO_GetResponse(SDIO_RESP1);
  2720. /* Find out card status */
  2721. *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F);
  2722. if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
  2723. {
  2724. return errorstate;
  2725. }
  2726. if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
  2727. {
  2728. return(SD_ADDR_OUT_OF_RANGE);
  2729. }
  2730. if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
  2731. {
  2732. return(SD_ADDR_MISALIGNED);
  2733. }
  2734. if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
  2735. {
  2736. return(SD_BLOCK_LEN_ERR);
  2737. }
  2738. if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
  2739. {
  2740. return(SD_ERASE_SEQ_ERR);
  2741. }
  2742. if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
  2743. {
  2744. return(SD_BAD_ERASE_PARAM);
  2745. }
  2746. if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
  2747. {
  2748. return(SD_WRITE_PROT_VIOLATION);
  2749. }
  2750. if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
  2751. {
  2752. return(SD_LOCK_UNLOCK_FAILED);
  2753. }
  2754. if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
  2755. {
  2756. return(SD_COM_CRC_FAILED);
  2757. }
  2758. if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
  2759. {
  2760. return(SD_ILLEGAL_CMD);
  2761. }
  2762. if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
  2763. {
  2764. return(SD_CARD_ECC_FAILED);
  2765. }
  2766. if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
  2767. {
  2768. return(SD_CC_ERROR);
  2769. }
  2770. if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
  2771. {
  2772. return(SD_GENERAL_UNKNOWN_ERROR);
  2773. }
  2774. if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
  2775. {
  2776. return(SD_STREAM_READ_UNDERRUN);
  2777. }
  2778. if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
  2779. {
  2780. return(SD_STREAM_WRITE_OVERRUN);
  2781. }
  2782. if((responseR1 & SD_OCR_CID_CSD_OVERWRIETE) == SD_OCR_CID_CSD_OVERWRIETE)
  2783. {
  2784. return(SD_CID_CSD_OVERWRITE);
  2785. }
  2786. if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
  2787. {
  2788. return(SD_WP_ERASE_SKIP);
  2789. }
  2790. if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
  2791. {
  2792. return(SD_CARD_ECC_DISABLED);
  2793. }
  2794. if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
  2795. {
  2796. return(SD_ERASE_RESET);
  2797. }
  2798. if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
  2799. {
  2800. return(SD_AKE_SEQ_ERROR);
  2801. }
  2802. return errorstate;
  2803. }
  2804. /**
  2805. * @}
  2806. */
  2807. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  2808. #endif /* HAL_SD_MODULE_ENABLED */
  2809. /**
  2810. * @}
  2811. */
  2812. /**
  2813. * @}
  2814. */
  2815. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/