stm32l4xx_hal_sd.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_sd.c
  4. * @author MCD Application Team
  5. * @brief SD card HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Secure Digital (SD) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. This driver implements a high level communication layer for read and write from/to
  30. this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
  31. the user in HAL_SD_MspInit() function (MSP layer).
  32. Basically, the MSP layer configuration should be the same as we provide in the
  33. examples.
  34. You can easily tailor this configuration according to hardware resources.
  35. [..]
  36. This driver is a generic layered driver for SDMMC memories which uses the HAL
  37. SDMMC driver functions to interface with SD and uSD cards devices.
  38. It is used as follows:
  39. (#)Initialize the SDMMC low level resources by implementing the HAL_SD_MspInit() API:
  40. (##) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_SDMMC1 for
  41. PeriphClockSelection and select SDMMC1 clock source (MSI, main PLL or PLLSAI1)
  42. (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC1_CLK_ENABLE();
  43. (##) SDMMC pins configuration for SD card
  44. (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
  45. (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
  46. and according to your pin assignment;
  47. (##) On STM32L4Rx/STM32L4Sxx devices, no DMA configuration is need, an internal DMA for SDMMC Peripheral is used.
  48. (##) On other devices, perform DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
  49. and HAL_SD_WriteBlocks_DMA() APIs).
  50. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
  51. (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
  52. (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
  53. (+++) Configure the SDMMC and DMA interrupt priorities using functions
  54. HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
  55. (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  56. (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
  57. and __HAL_SD_DISABLE_IT() inside the communication process.
  58. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  59. and __HAL_SD_CLEAR_IT()
  60. (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
  61. and HAL_SD_WriteBlocks_IT() APIs).
  62. (+++) Configure the SDMMC interrupt priorities using function HAL_NVIC_SetPriority();
  63. (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
  64. (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
  65. and __HAL_SD_DISABLE_IT() inside the communication process.
  66. (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  67. and __HAL_SD_CLEAR_IT()
  68. (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
  69. *** SD Card Initialization and configuration ***
  70. ================================================
  71. [..]
  72. To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
  73. SDMMC Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
  74. This function provide the following operations:
  75. (#) Apply the SD Card initialization process at 400KHz and check the SD Card
  76. type (Standard Capacity or High Capacity). You can change or adapt this
  77. frequency by adjusting the "ClockDiv" field.
  78. The SD Card frequency (SDMMC_CK) is computed as follows:
  79. SDMMC_CK = SDMMCCLK / (2 * ClockDiv) on STM32L4Rx/STM32L4Sxx devices
  80. SDMMC_CK = SDMMCCLK / (ClockDiv + 2) on other devices
  81. In initialization mode and according to the SD Card standard,
  82. make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
  83. This phase of initialization is done through SDMMC_Init() and
  84. SDMMC_PowerState_ON() SDMMC low level APIs.
  85. (#) Initialize the SD card. The API used is HAL_SD_InitCard().
  86. This phase allows the card initialization and identification
  87. and check the SD Card type (Standard Capacity or High Capacity)
  88. The initialization flow is compatible with SD standard.
  89. This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
  90. of plug-off plug-in.
  91. (#) Configure the SD Card Data transfer frequency. You can change or adapt this
  92. frequency by adjusting the "ClockDiv" field.
  93. In transfer mode and according to the SD Card standard, make sure that the
  94. SDMMC_CK frequency doesn't exceed 25MHz and 100MHz in High-speed mode switch.
  95. (#) Select the corresponding SD Card according to the address read with the step 2.
  96. (#) Configure the SD Card in wide bus mode: 4-bits data.
  97. *** SD Card Read operation ***
  98. ==============================
  99. [..]
  100. (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
  101. This function support only 512-bytes block length (the block size should be
  102. chosen as 512 bytes).
  103. You can choose either one block read operation or multiple block read operation
  104. by adjusting the "NumberOfBlocks" parameter.
  105. After this, you have to ensure that the transfer is done correctly. The check is done
  106. through HAL_SD_GetCardState() function for SD card state.
  107. (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
  108. This function support only 512-bytes block length (the block size should be
  109. chosen as 512 bytes).
  110. You can choose either one block read operation or multiple block read operation
  111. by adjusting the "NumberOfBlocks" parameter.
  112. After this, you have to ensure that the transfer is done correctly. The check is done
  113. through HAL_SD_GetCardState() function for SD card state.
  114. You could also check the DMA transfer process through the SD Rx interrupt event.
  115. (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
  116. This function support only 512-bytes block length (the block size should be
  117. chosen as 512 bytes).
  118. You can choose either one block read operation or multiple block read operation
  119. by adjusting the "NumberOfBlocks" parameter.
  120. After this, you have to ensure that the transfer is done correctly. The check is done
  121. through HAL_SD_GetCardState() function for SD card state.
  122. You could also check the IT transfer process through the SD Rx interrupt event.
  123. *** SD Card Write operation ***
  124. ===============================
  125. [..]
  126. (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
  127. This function support only 512-bytes block length (the block size should be
  128. chosen as 512 bytes).
  129. You can choose either one block read operation or multiple block read operation
  130. by adjusting the "NumberOfBlocks" parameter.
  131. After this, you have to ensure that the transfer is done correctly. The check is done
  132. through HAL_SD_GetCardState() function for SD card state.
  133. (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
  134. This function support only 512-bytes block length (the block size should be
  135. chosen as 512 bytes).
  136. You can choose either one block read operation or multiple block read operation
  137. by adjusting the "NumberOfBlocks" parameter.
  138. After this, you have to ensure that the transfer is done correctly. The check is done
  139. through HAL_SD_GetCardState() function for SD card state.
  140. You could also check the DMA transfer process through the SD Tx interrupt event.
  141. (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
  142. This function support only 512-bytes block length (the block size should be
  143. chosen as 512 bytes).
  144. You can choose either one block read operation or multiple block read operation
  145. by adjusting the "NumberOfBlocks" parameter.
  146. After this, you have to ensure that the transfer is done correctly. The check is done
  147. through HAL_SD_GetCardState() function for SD card state.
  148. You could also check the IT transfer process through the SD Tx interrupt event.
  149. *** SD card status ***
  150. ======================
  151. [..]
  152. (+) The SD Status contains status bits that are related to the SD Memory
  153. Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
  154. *** SD card information ***
  155. ===========================
  156. [..]
  157. (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
  158. It returns useful information about the SD card such as block size, card type,
  159. block number ...
  160. *** SD card CSD register ***
  161. ============================
  162. (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
  163. Some of the CSD parameters are useful for card initialization and identification.
  164. *** SD card CID register ***
  165. ============================
  166. (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
  167. Some of the CSD parameters are useful for card initialization and identification.
  168. *** SD HAL driver macros list ***
  169. ==================================
  170. [..]
  171. Below the list of most used macros in SD HAL driver.
  172. (+) __HAL_SD_ENABLE : Enable the SD device
  173. (+) __HAL_SD_DISABLE : Disable the SD device
  174. (+) __HAL_SD_DMA_ENABLE: Enable the SDMMC DMA transfer
  175. (+) __HAL_SD_DMA_DISABLE: Disable the SDMMC DMA transfer
  176. (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
  177. (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
  178. (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
  179. (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
  180. (@) You can refer to the SD HAL driver header file for more useful macros
  181. *** Callback registration ***
  182. =============================================
  183. [..]
  184. The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
  185. allows the user to configure dynamically the driver callbacks.
  186. Use Functions HAL_SD_RegisterCallback() to register a user callback,
  187. it allows to register following callbacks:
  188. (+) TxCpltCallback : callback when a transmission transfer is completed.
  189. (+) RxCpltCallback : callback when a reception transfer is completed.
  190. (+) ErrorCallback : callback when error occurs.
  191. (+) AbortCpltCallback : callback when abort is completed.
  192. (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
  193. (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
  194. (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
  195. (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
  196. (+) MspInitCallback : SD MspInit.
  197. (+) MspDeInitCallback : SD MspDeInit.
  198. This function takes as parameters the HAL peripheral handle, the Callback ID
  199. and a pointer to the user callback function.
  200. For specific callbacks TransceiverCallback use dedicated register callbacks:
  201. respectively HAL_SD_RegisterTransceiverCallback().
  202. Use function HAL_SD_UnRegisterCallback() to reset a callback to the default
  203. weak (surcharged) function. It allows to reset following callbacks:
  204. (+) TxCpltCallback : callback when a transmission transfer is completed.
  205. (+) RxCpltCallback : callback when a reception transfer is completed.
  206. (+) ErrorCallback : callback when error occurs.
  207. (+) AbortCpltCallback : callback when abort is completed.
  208. (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
  209. (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
  210. (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
  211. (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
  212. (+) MspInitCallback : SD MspInit.
  213. (+) MspDeInitCallback : SD MspDeInit.
  214. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  215. For specific callbacks TransceiverCallback use dedicated unregister callbacks:
  216. respectively HAL_SD_UnRegisterTransceiverCallback().
  217. By default, after the HAL_SD_Init and if the state is HAL_SD_STATE_RESET
  218. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  219. Exception done for MspInit and MspDeInit callbacks that are respectively
  220. reset to the legacy weak (surcharged) functions in the HAL_SD_Init
  221. and HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
  222. If not, MspInit or MspDeInit are not null, the HAL_SD_Init and HAL_SD_DeInit
  223. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  224. Callbacks can be registered/unregistered in READY state only.
  225. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  226. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  227. during the Init/DeInit.
  228. In that case first register the MspInit/MspDeInit user callbacks
  229. using HAL_SD_RegisterCallback before calling HAL_SD_DeInit
  230. or HAL_SD_Init function.
  231. When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
  232. not defined, the callback registering feature is not available
  233. and weak (surcharged) callbacks are used.
  234. @endverbatim
  235. ******************************************************************************
  236. */
  237. /* Includes ------------------------------------------------------------------*/
  238. #include "stm32l4xx_hal.h"
  239. #if defined(SDMMC1)
  240. /** @addtogroup STM32L4xx_HAL_Driver
  241. * @{
  242. */
  243. /** @addtogroup SD
  244. * @{
  245. */
  246. #ifdef HAL_SD_MODULE_ENABLED
  247. /* Private typedef -----------------------------------------------------------*/
  248. /* Private define ------------------------------------------------------------*/
  249. /** @addtogroup SD_Private_Defines
  250. * @{
  251. */
  252. /* Frequencies used in the driver for clock divider calculation */
  253. #define SD_INIT_FREQ 400000U /* Initialization phase : 400 kHz max */
  254. #define SD_NORMAL_SPEED_FREQ 25000000U /* Normal speed phase : 25 MHz max */
  255. #define SD_HIGH_SPEED_FREQ 50000000U /* High speed phase : 50 MHz max */
  256. /**
  257. * @}
  258. */
  259. /* Private macro -------------------------------------------------------------*/
  260. /* Private variables ---------------------------------------------------------*/
  261. /* Private function prototypes -----------------------------------------------*/
  262. /* Private functions ---------------------------------------------------------*/
  263. /** @defgroup SD_Private_Functions SD Private Functions
  264. * @{
  265. */
  266. static uint32_t SD_InitCard (SD_HandleTypeDef *hsd);
  267. static uint32_t SD_PowerON (SD_HandleTypeDef *hsd);
  268. static uint32_t SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
  269. static uint32_t SD_SendStatus (SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
  270. static uint32_t SD_WideBus_Enable (SD_HandleTypeDef *hsd);
  271. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
  272. static uint32_t SD_FindSCR (SD_HandleTypeDef *hsd, uint32_t *pSCR);
  273. static void SD_PowerOFF (SD_HandleTypeDef *hsd);
  274. static void SD_Write_IT (SD_HandleTypeDef *hsd);
  275. static void SD_Read_IT (SD_HandleTypeDef *hsd);
  276. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  277. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  278. static void SD_DMAReceiveCplt (DMA_HandleTypeDef *hdma);
  279. static void SD_DMAError (DMA_HandleTypeDef *hdma);
  280. static void SD_DMATxAbort (DMA_HandleTypeDef *hdma);
  281. static void SD_DMARxAbort (DMA_HandleTypeDef *hdma);
  282. #else
  283. uint32_t SD_HighSpeed (SD_HandleTypeDef *hsd);
  284. static uint32_t SD_UltraHighSpeed (SD_HandleTypeDef *hsd);
  285. static uint32_t SD_DDR_Mode (SD_HandleTypeDef *hsd);
  286. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  287. /**
  288. * @}
  289. */
  290. /* Exported functions --------------------------------------------------------*/
  291. /** @addtogroup SD_Exported_Functions
  292. * @{
  293. */
  294. /** @addtogroup SD_Exported_Functions_Group1
  295. * @brief Initialization and de-initialization functions
  296. *
  297. @verbatim
  298. ==============================================================================
  299. ##### Initialization and de-initialization functions #####
  300. ==============================================================================
  301. [..]
  302. This section provides functions allowing to initialize/de-initialize the SD
  303. card device to be ready for use.
  304. @endverbatim
  305. * @{
  306. */
  307. /**
  308. * @brief Initializes the SD according to the specified parameters in the
  309. SD_HandleTypeDef and create the associated handle.
  310. * @param hsd Pointer to the SD handle
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
  314. {
  315. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  316. HAL_SD_CardStatusTypeDef CardStatus;
  317. uint32_t speedgrade, unitsize;
  318. uint32_t tickstart;
  319. #endif
  320. /* Check the SD handle allocation */
  321. if(hsd == NULL)
  322. {
  323. return HAL_ERROR;
  324. }
  325. /* Check the parameters */
  326. assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
  327. assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
  328. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  329. assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
  330. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  331. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
  332. assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
  333. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
  334. assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
  335. if(hsd->State == HAL_SD_STATE_RESET)
  336. {
  337. /* Allocate lock resource and initialize it */
  338. hsd->Lock = HAL_UNLOCKED;
  339. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  340. /* Reset Callback pointers in HAL_SD_STATE_RESET only */
  341. hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
  342. hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
  343. hsd->ErrorCallback = HAL_SD_ErrorCallback;
  344. hsd->AbortCpltCallback = HAL_SD_AbortCallback;
  345. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  346. hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuffer0CpltCallback;
  347. hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuffer1CpltCallback;
  348. hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuffer0CpltCallback;
  349. hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuffer1CpltCallback;
  350. hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
  351. #endif
  352. if(hsd->MspInitCallback == NULL)
  353. {
  354. hsd->MspInitCallback = HAL_SD_MspInit;
  355. }
  356. /* Init the low level hardware */
  357. hsd->MspInitCallback(hsd);
  358. #else
  359. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  360. HAL_SD_MspInit(hsd);
  361. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  362. }
  363. hsd->State = HAL_SD_STATE_BUSY;
  364. /* Initialize the Card parameters */
  365. if (HAL_SD_InitCard(hsd) != HAL_OK)
  366. {
  367. return HAL_ERROR;
  368. }
  369. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  370. if( HAL_SD_GetCardStatus(hsd, &CardStatus) != HAL_OK)
  371. {
  372. return HAL_ERROR;
  373. }
  374. /* Get Initial Card Speed from Card Status*/
  375. speedgrade = CardStatus.UhsSpeedGrade;
  376. unitsize = CardStatus.UhsAllocationUnitSize;
  377. if ((hsd->SdCard.CardType == CARD_SDHC_SDXC) && ((speedgrade != 0U) || (unitsize != 0U)))
  378. {
  379. hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
  380. }
  381. else
  382. {
  383. if (hsd->SdCard.CardType == CARD_SDHC_SDXC)
  384. {
  385. hsd->SdCard.CardSpeed = CARD_HIGH_SPEED;
  386. }
  387. else
  388. {
  389. hsd->SdCard.CardSpeed = CARD_NORMAL_SPEED;
  390. }
  391. }
  392. /* Configure the bus wide */
  393. if(HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
  394. {
  395. return HAL_ERROR;
  396. }
  397. /* Verify that SD card is ready to use after Initialization */
  398. tickstart = HAL_GetTick();
  399. while((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
  400. {
  401. if((HAL_GetTick()-tickstart) >= SDMMC_DATATIMEOUT)
  402. {
  403. hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
  404. hsd->State= HAL_SD_STATE_READY;
  405. hsd->Context = SD_CONTEXT_NONE;
  406. return HAL_TIMEOUT;
  407. }
  408. }
  409. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  410. /* Initialize the error code */
  411. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  412. /* Initialize the SD operation */
  413. hsd->Context = SD_CONTEXT_NONE;
  414. /* Initialize the SD state */
  415. hsd->State = HAL_SD_STATE_READY;
  416. return HAL_OK;
  417. }
  418. /**
  419. * @brief Initializes the SD Card.
  420. * @param hsd Pointer to SD handle
  421. * @note This function initializes the SD card. It could be used when a card
  422. re-initialization is needed.
  423. * @retval HAL status
  424. */
  425. HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  426. {
  427. uint32_t errorstate;
  428. SD_InitTypeDef Init;
  429. uint32_t sdmmc_clk;
  430. /* Default SDMMC peripheral configuration for SD card initialization */
  431. Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  432. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  433. Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
  434. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  435. Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  436. Init.BusWide = SDMMC_BUS_WIDE_1B;
  437. Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  438. /* Init Clock should be less or equal to 400Khz*/
  439. sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC1);
  440. if (sdmmc_clk == 0U)
  441. {
  442. hsd->State = HAL_SD_STATE_READY;
  443. hsd->ErrorCode = SDMMC_ERROR_INVALID_PARAMETER;
  444. return HAL_ERROR;
  445. }
  446. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  447. Init.ClockDiv = ((sdmmc_clk / SD_INIT_FREQ) - 2U);
  448. #else
  449. Init.ClockDiv = sdmmc_clk / (2U * SD_INIT_FREQ);
  450. #endif
  451. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  452. Init.Transceiver = hsd->Init.Transceiver;
  453. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  454. {
  455. /* Set Transceiver polarity */
  456. hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
  457. }
  458. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  459. /* Initialize SDMMC peripheral interface with default configuration */
  460. (void)SDMMC_Init(hsd->Instance, Init);
  461. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  462. /* Disable SDMMC Clock */
  463. __HAL_SD_DISABLE(hsd);
  464. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  465. /* Set Power State to ON */
  466. (void)SDMMC_PowerState_ON(hsd->Instance);
  467. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  468. /* Enable SDMMC Clock */
  469. __HAL_SD_ENABLE(hsd);
  470. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  471. /* wait 74 Cycles: required power up waiting time before starting
  472. the SD initialization sequence */
  473. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  474. sdmmc_clk = sdmmc_clk/(Init.ClockDiv + 2U);
  475. #else
  476. sdmmc_clk = sdmmc_clk/(2U*Init.ClockDiv);
  477. #endif
  478. HAL_Delay(1U+ (74U*1000U/(sdmmc_clk)));
  479. /* Identify card operating voltage */
  480. errorstate = SD_PowerON(hsd);
  481. if(errorstate != HAL_SD_ERROR_NONE)
  482. {
  483. hsd->State = HAL_SD_STATE_READY;
  484. hsd->ErrorCode |= errorstate;
  485. return HAL_ERROR;
  486. }
  487. /* Card initialization */
  488. errorstate = SD_InitCard(hsd);
  489. if(errorstate != HAL_SD_ERROR_NONE)
  490. {
  491. hsd->State = HAL_SD_STATE_READY;
  492. hsd->ErrorCode |= errorstate;
  493. return HAL_ERROR;
  494. }
  495. /* Set Block Size for Card */
  496. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  497. if(errorstate != HAL_SD_ERROR_NONE)
  498. {
  499. /* Clear all the static flags */
  500. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  501. hsd->ErrorCode |= errorstate;
  502. hsd->State = HAL_SD_STATE_READY;
  503. return HAL_ERROR;
  504. }
  505. return HAL_OK;
  506. }
  507. /**
  508. * @brief De-Initializes the SD card.
  509. * @param hsd Pointer to SD handle
  510. * @retval HAL status
  511. */
  512. HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
  513. {
  514. /* Check the SD handle allocation */
  515. if(hsd == NULL)
  516. {
  517. return HAL_ERROR;
  518. }
  519. /* Check the parameters */
  520. assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
  521. hsd->State = HAL_SD_STATE_BUSY;
  522. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  523. /* Deactivate the 1.8V Mode */
  524. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  525. {
  526. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  527. if(hsd->DriveTransceiver_1_8V_Callback == NULL)
  528. {
  529. hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
  530. }
  531. hsd->DriveTransceiver_1_8V_Callback(RESET);
  532. #else
  533. HAL_SDEx_DriveTransceiver_1_8V_Callback(RESET);
  534. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  535. }
  536. #endif
  537. /* Set SD power state to off */
  538. SD_PowerOFF(hsd);
  539. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  540. if(hsd->MspDeInitCallback == NULL)
  541. {
  542. hsd->MspDeInitCallback = HAL_SD_MspDeInit;
  543. }
  544. /* DeInit the low level hardware */
  545. hsd->MspDeInitCallback(hsd);
  546. #else
  547. /* De-Initialize the MSP layer */
  548. HAL_SD_MspDeInit(hsd);
  549. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  550. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  551. hsd->State = HAL_SD_STATE_RESET;
  552. return HAL_OK;
  553. }
  554. /**
  555. * @brief Initializes the SD MSP.
  556. * @param hsd Pointer to SD handle
  557. * @retval None
  558. */
  559. __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
  560. {
  561. /* Prevent unused argument(s) compilation warning */
  562. UNUSED(hsd);
  563. /* NOTE : This function should not be modified, when the callback is needed,
  564. the HAL_SD_MspInit could be implemented in the user file
  565. */
  566. }
  567. /**
  568. * @brief De-Initialize SD MSP.
  569. * @param hsd Pointer to SD handle
  570. * @retval None
  571. */
  572. __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
  573. {
  574. /* Prevent unused argument(s) compilation warning */
  575. UNUSED(hsd);
  576. /* NOTE : This function should not be modified, when the callback is needed,
  577. the HAL_SD_MspDeInit could be implemented in the user file
  578. */
  579. }
  580. /**
  581. * @}
  582. */
  583. /** @addtogroup SD_Exported_Functions_Group2
  584. * @brief Data transfer functions
  585. *
  586. @verbatim
  587. ==============================================================================
  588. ##### IO operation functions #####
  589. ==============================================================================
  590. [..]
  591. This subsection provides a set of functions allowing to manage the data
  592. transfer from/to SD card.
  593. @endverbatim
  594. * @{
  595. */
  596. /**
  597. * @brief Reads block(s) from a specified address in a card. The Data transfer
  598. * is managed by polling mode.
  599. * @note This API should be followed by a check on the card state through
  600. * HAL_SD_GetCardState().
  601. * @param hsd Pointer to SD handle
  602. * @param pData pointer to the buffer that will contain the received data
  603. * @param BlockAdd Block Address from where data is to be read
  604. * @param NumberOfBlocks Number of SD blocks to read
  605. * @param Timeout Specify timeout value
  606. * @retval HAL status
  607. */
  608. HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  609. {
  610. SDMMC_DataInitTypeDef config;
  611. uint32_t errorstate;
  612. uint32_t tickstart = HAL_GetTick();
  613. uint32_t count, data, dataremaining;
  614. uint32_t add = BlockAdd;
  615. uint8_t *tempbuff = pData;
  616. if(NULL == pData)
  617. {
  618. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  619. return HAL_ERROR;
  620. }
  621. if(hsd->State == HAL_SD_STATE_READY)
  622. {
  623. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  624. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  625. {
  626. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  627. return HAL_ERROR;
  628. }
  629. hsd->State = HAL_SD_STATE_BUSY;
  630. /* Initialize data control register */
  631. hsd->Instance->DCTRL = 0U;
  632. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  633. {
  634. add *= 512U;
  635. }
  636. /* Configure the SD DPSM (Data Path State Machine) */
  637. config.DataTimeOut = SDMMC_DATATIMEOUT;
  638. config.DataLength = NumberOfBlocks * BLOCKSIZE;
  639. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  640. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  641. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  642. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  643. config.DPSM = SDMMC_DPSM_DISABLE;
  644. #else
  645. config.DPSM = SDMMC_DPSM_ENABLE;
  646. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  647. (void)SDMMC_ConfigData(hsd->Instance, &config);
  648. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  649. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  650. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  651. /* Read block(s) in polling mode */
  652. if(NumberOfBlocks > 1U)
  653. {
  654. hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
  655. /* Read Multi Block command */
  656. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  657. }
  658. else
  659. {
  660. hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
  661. /* Read Single Block command */
  662. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
  663. }
  664. if(errorstate != HAL_SD_ERROR_NONE)
  665. {
  666. /* Clear all the static flags */
  667. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  668. hsd->ErrorCode |= errorstate;
  669. hsd->State = HAL_SD_STATE_READY;
  670. hsd->Context = SD_CONTEXT_NONE;
  671. return HAL_ERROR;
  672. }
  673. /* Poll on SDMMC flags */
  674. dataremaining = config.DataLength;
  675. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  676. {
  677. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
  678. {
  679. /* Read data from SDMMC Rx FIFO */
  680. for(count = 0U; count < 8U; count++)
  681. {
  682. data = SDMMC_ReadFIFO(hsd->Instance);
  683. *tempbuff = (uint8_t)(data & 0xFFU);
  684. tempbuff++;
  685. dataremaining--;
  686. *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  687. tempbuff++;
  688. dataremaining--;
  689. *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  690. tempbuff++;
  691. dataremaining--;
  692. *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  693. tempbuff++;
  694. dataremaining--;
  695. }
  696. }
  697. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  698. {
  699. /* Clear all the static flags */
  700. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  701. hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  702. hsd->State= HAL_SD_STATE_READY;
  703. hsd->Context = SD_CONTEXT_NONE;
  704. return HAL_TIMEOUT;
  705. }
  706. }
  707. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  708. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  709. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  710. /* Send stop transmission command in case of multiblock read */
  711. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  712. {
  713. if(hsd->SdCard.CardType != CARD_SECURED)
  714. {
  715. /* Send stop transmission command */
  716. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  717. if(errorstate != HAL_SD_ERROR_NONE)
  718. {
  719. /* Clear all the static flags */
  720. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  721. hsd->ErrorCode |= errorstate;
  722. hsd->State = HAL_SD_STATE_READY;
  723. hsd->Context = SD_CONTEXT_NONE;
  724. return HAL_ERROR;
  725. }
  726. }
  727. }
  728. /* Get error state */
  729. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  730. {
  731. /* Clear all the static flags */
  732. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  733. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  734. hsd->State = HAL_SD_STATE_READY;
  735. hsd->Context = SD_CONTEXT_NONE;
  736. return HAL_ERROR;
  737. }
  738. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  739. {
  740. /* Clear all the static flags */
  741. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  742. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  743. hsd->State = HAL_SD_STATE_READY;
  744. hsd->Context = SD_CONTEXT_NONE;
  745. return HAL_ERROR;
  746. }
  747. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  748. {
  749. /* Clear all the static flags */
  750. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  751. hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  752. hsd->State = HAL_SD_STATE_READY;
  753. hsd->Context = SD_CONTEXT_NONE;
  754. return HAL_ERROR;
  755. }
  756. else
  757. {
  758. /* Nothing to do */
  759. }
  760. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  761. /* Empty FIFO if there is still any data */
  762. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
  763. {
  764. data = SDMMC_ReadFIFO(hsd->Instance);
  765. *tempbuff = (uint8_t)(data & 0xFFU);
  766. tempbuff++;
  767. dataremaining--;
  768. *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  769. tempbuff++;
  770. dataremaining--;
  771. *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  772. tempbuff++;
  773. dataremaining--;
  774. *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  775. tempbuff++;
  776. dataremaining--;
  777. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  778. {
  779. /* Clear all the static flags */
  780. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  781. hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  782. hsd->State= HAL_SD_STATE_READY;
  783. hsd->Context = SD_CONTEXT_NONE;
  784. return HAL_ERROR;
  785. }
  786. }
  787. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  788. /* Clear all the static flags */
  789. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  790. hsd->State = HAL_SD_STATE_READY;
  791. return HAL_OK;
  792. }
  793. else
  794. {
  795. hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  796. return HAL_ERROR;
  797. }
  798. }
  799. /**
  800. * @brief Allows to write block(s) to a specified address in a card. The Data
  801. * transfer is managed by polling mode.
  802. * @note This API should be followed by a check on the card state through
  803. * HAL_SD_GetCardState().
  804. * @param hsd Pointer to SD handle
  805. * @param pData pointer to the buffer that will contain the data to transmit
  806. * @param BlockAdd Block Address where data will be written
  807. * @param NumberOfBlocks Number of SD blocks to write
  808. * @param Timeout Specify timeout value
  809. * @retval HAL status
  810. */
  811. HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  812. {
  813. SDMMC_DataInitTypeDef config;
  814. uint32_t errorstate;
  815. uint32_t tickstart = HAL_GetTick();
  816. uint32_t count, data, dataremaining;
  817. uint32_t add = BlockAdd;
  818. uint8_t *tempbuff = pData;
  819. if(NULL == pData)
  820. {
  821. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  822. return HAL_ERROR;
  823. }
  824. if(hsd->State == HAL_SD_STATE_READY)
  825. {
  826. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  827. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  828. {
  829. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  830. return HAL_ERROR;
  831. }
  832. hsd->State = HAL_SD_STATE_BUSY;
  833. /* Initialize data control register */
  834. hsd->Instance->DCTRL = 0U;
  835. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  836. {
  837. add *= 512U;
  838. }
  839. /* Configure the SD DPSM (Data Path State Machine) */
  840. config.DataTimeOut = SDMMC_DATATIMEOUT;
  841. config.DataLength = NumberOfBlocks * BLOCKSIZE;
  842. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  843. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  844. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  845. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  846. config.DPSM = SDMMC_DPSM_DISABLE;
  847. #else
  848. config.DPSM = SDMMC_DPSM_ENABLE;
  849. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  850. (void)SDMMC_ConfigData(hsd->Instance, &config);
  851. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  852. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  853. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  854. /* Write Blocks in Polling mode */
  855. if(NumberOfBlocks > 1U)
  856. {
  857. hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
  858. /* Write Multi Block command */
  859. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  860. }
  861. else
  862. {
  863. hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
  864. /* Write Single Block command */
  865. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
  866. }
  867. if(errorstate != HAL_SD_ERROR_NONE)
  868. {
  869. /* Clear all the static flags */
  870. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  871. hsd->ErrorCode |= errorstate;
  872. hsd->State = HAL_SD_STATE_READY;
  873. hsd->Context = SD_CONTEXT_NONE;
  874. return HAL_ERROR;
  875. }
  876. /* Write block(s) in polling mode */
  877. dataremaining = config.DataLength;
  878. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  879. {
  880. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
  881. {
  882. /* Write data to SDMMC Tx FIFO */
  883. for(count = 0U; count < 8U; count++)
  884. {
  885. data = (uint32_t)(*tempbuff);
  886. tempbuff++;
  887. dataremaining--;
  888. data |= ((uint32_t)(*tempbuff) << 8U);
  889. tempbuff++;
  890. dataremaining--;
  891. data |= ((uint32_t)(*tempbuff) << 16U);
  892. tempbuff++;
  893. dataremaining--;
  894. data |= ((uint32_t)(*tempbuff) << 24U);
  895. tempbuff++;
  896. dataremaining--;
  897. (void)SDMMC_WriteFIFO(hsd->Instance, &data);
  898. }
  899. }
  900. if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
  901. {
  902. /* Clear all the static flags */
  903. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  904. hsd->ErrorCode |= errorstate;
  905. hsd->State = HAL_SD_STATE_READY;
  906. hsd->Context = SD_CONTEXT_NONE;
  907. return HAL_TIMEOUT;
  908. }
  909. }
  910. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  911. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  912. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  913. /* Send stop transmission command in case of multiblock write */
  914. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  915. {
  916. if(hsd->SdCard.CardType != CARD_SECURED)
  917. {
  918. /* Send stop transmission command */
  919. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  920. if(errorstate != HAL_SD_ERROR_NONE)
  921. {
  922. /* Clear all the static flags */
  923. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  924. hsd->ErrorCode |= errorstate;
  925. hsd->State = HAL_SD_STATE_READY;
  926. hsd->Context = SD_CONTEXT_NONE;
  927. return HAL_ERROR;
  928. }
  929. }
  930. }
  931. /* Get error state */
  932. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  933. {
  934. /* Clear all the static flags */
  935. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  936. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  937. hsd->State = HAL_SD_STATE_READY;
  938. hsd->Context = SD_CONTEXT_NONE;
  939. return HAL_ERROR;
  940. }
  941. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  942. {
  943. /* Clear all the static flags */
  944. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  945. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  946. hsd->State = HAL_SD_STATE_READY;
  947. hsd->Context = SD_CONTEXT_NONE;
  948. return HAL_ERROR;
  949. }
  950. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
  951. {
  952. /* Clear all the static flags */
  953. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  954. hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  955. hsd->State = HAL_SD_STATE_READY;
  956. hsd->Context = SD_CONTEXT_NONE;
  957. return HAL_ERROR;
  958. }
  959. else
  960. {
  961. /* Nothing to do */
  962. }
  963. /* Clear all the static flags */
  964. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  965. hsd->State = HAL_SD_STATE_READY;
  966. return HAL_OK;
  967. }
  968. else
  969. {
  970. hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  971. return HAL_ERROR;
  972. }
  973. }
  974. /**
  975. * @brief Reads block(s) from a specified address in a card. The Data transfer
  976. * is managed in interrupt mode.
  977. * @note This API should be followed by a check on the card state through
  978. * HAL_SD_GetCardState().
  979. * @note You could also check the IT transfer process through the SD Rx
  980. * interrupt event.
  981. * @param hsd Pointer to SD handle
  982. * @param pData Pointer to the buffer that will contain the received data
  983. * @param BlockAdd Block Address from where data is to be read
  984. * @param NumberOfBlocks Number of blocks to read.
  985. * @retval HAL status
  986. */
  987. HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  988. {
  989. SDMMC_DataInitTypeDef config;
  990. uint32_t errorstate;
  991. uint32_t add = BlockAdd;
  992. if(NULL == pData)
  993. {
  994. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  995. return HAL_ERROR;
  996. }
  997. if(hsd->State == HAL_SD_STATE_READY)
  998. {
  999. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1000. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1001. {
  1002. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1003. return HAL_ERROR;
  1004. }
  1005. hsd->State = HAL_SD_STATE_BUSY;
  1006. /* Initialize data control register */
  1007. hsd->Instance->DCTRL = 0U;
  1008. hsd->pRxBuffPtr = pData;
  1009. hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
  1010. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1011. {
  1012. add *= 512U;
  1013. }
  1014. /* Configure the SD DPSM (Data Path State Machine) */
  1015. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1016. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1017. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1018. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1019. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1020. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1021. config.DPSM = SDMMC_DPSM_DISABLE;
  1022. #else
  1023. config.DPSM = SDMMC_DPSM_ENABLE;
  1024. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1025. (void)SDMMC_ConfigData(hsd->Instance, &config);
  1026. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1027. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  1028. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1029. /* Read Blocks in IT mode */
  1030. if(NumberOfBlocks > 1U)
  1031. {
  1032. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
  1033. /* Read Multi Block command */
  1034. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  1035. }
  1036. else
  1037. {
  1038. hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
  1039. /* Read Single Block command */
  1040. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
  1041. }
  1042. if(errorstate != HAL_SD_ERROR_NONE)
  1043. {
  1044. /* Clear all the static flags */
  1045. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1046. hsd->ErrorCode |= errorstate;
  1047. hsd->State = HAL_SD_STATE_READY;
  1048. hsd->Context = SD_CONTEXT_NONE;
  1049. return HAL_ERROR;
  1050. }
  1051. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
  1052. return HAL_OK;
  1053. }
  1054. else
  1055. {
  1056. return HAL_BUSY;
  1057. }
  1058. }
  1059. /**
  1060. * @brief Writes block(s) to a specified address in a card. The Data transfer
  1061. * is managed in interrupt mode.
  1062. * @note This API should be followed by a check on the card state through
  1063. * HAL_SD_GetCardState().
  1064. * @note You could also check the IT transfer process through the SD Tx
  1065. * interrupt event.
  1066. * @param hsd Pointer to SD handle
  1067. * @param pData Pointer to the buffer that will contain the data to transmit
  1068. * @param BlockAdd Block Address where data will be written
  1069. * @param NumberOfBlocks Number of blocks to write
  1070. * @retval HAL status
  1071. */
  1072. HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1073. {
  1074. SDMMC_DataInitTypeDef config;
  1075. uint32_t errorstate;
  1076. uint32_t add = BlockAdd;
  1077. if(NULL == pData)
  1078. {
  1079. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1080. return HAL_ERROR;
  1081. }
  1082. if(hsd->State == HAL_SD_STATE_READY)
  1083. {
  1084. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1085. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1086. {
  1087. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1088. return HAL_ERROR;
  1089. }
  1090. hsd->State = HAL_SD_STATE_BUSY;
  1091. /* Initialize data control register */
  1092. hsd->Instance->DCTRL = 0U;
  1093. hsd->pTxBuffPtr = pData;
  1094. hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
  1095. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1096. {
  1097. add *= 512U;
  1098. }
  1099. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1100. /* Configure the SD DPSM (Data Path State Machine) */
  1101. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1102. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1103. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1104. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1105. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1106. config.DPSM = SDMMC_DPSM_DISABLE;
  1107. (void)SDMMC_ConfigData(hsd->Instance, &config);
  1108. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  1109. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1110. /* Write Blocks in Polling mode */
  1111. if(NumberOfBlocks > 1U)
  1112. {
  1113. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
  1114. /* Write Multi Block command */
  1115. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  1116. }
  1117. else
  1118. {
  1119. hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
  1120. /* Write Single Block command */
  1121. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
  1122. }
  1123. if(errorstate != HAL_SD_ERROR_NONE)
  1124. {
  1125. /* Clear all the static flags */
  1126. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1127. hsd->ErrorCode |= errorstate;
  1128. hsd->State = HAL_SD_STATE_READY;
  1129. hsd->Context = SD_CONTEXT_NONE;
  1130. return HAL_ERROR;
  1131. }
  1132. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1133. /* Configure the SD DPSM (Data Path State Machine) */
  1134. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1135. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1136. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1137. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1138. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1139. config.DPSM = SDMMC_DPSM_ENABLE;
  1140. (void)SDMMC_ConfigData(hsd->Instance, &config);
  1141. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  1142. /* Enable transfer interrupts */
  1143. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
  1144. return HAL_OK;
  1145. }
  1146. else
  1147. {
  1148. return HAL_BUSY;
  1149. }
  1150. }
  1151. /**
  1152. * @brief Reads block(s) from a specified address in a card. The Data transfer
  1153. * is managed by DMA mode.
  1154. * @note This API should be followed by a check on the card state through
  1155. * HAL_SD_GetCardState().
  1156. * @note You could also check the DMA transfer process through the SD Rx
  1157. * interrupt event.
  1158. * @param hsd Pointer SD handle
  1159. * @param pData Pointer to the buffer that will contain the received data
  1160. * @param BlockAdd Block Address from where data is to be read
  1161. * @param NumberOfBlocks Number of blocks to read.
  1162. * @retval HAL status
  1163. */
  1164. HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1165. {
  1166. SDMMC_DataInitTypeDef config;
  1167. uint32_t errorstate;
  1168. uint32_t add = BlockAdd;
  1169. if(NULL == pData)
  1170. {
  1171. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1172. return HAL_ERROR;
  1173. }
  1174. if(hsd->State == HAL_SD_STATE_READY)
  1175. {
  1176. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1177. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1178. {
  1179. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1180. return HAL_ERROR;
  1181. }
  1182. hsd->State = HAL_SD_STATE_BUSY;
  1183. /* Initialize data control register */
  1184. hsd->Instance->DCTRL = 0U;
  1185. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1186. /* Set the DMA transfer complete callback */
  1187. hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
  1188. /* Set the DMA error callback */
  1189. hsd->hdmarx->XferErrorCallback = SD_DMAError;
  1190. /* Set the DMA Abort callback */
  1191. hsd->hdmarx->XferAbortCallback = NULL;
  1192. /* Enable the DMA Channel */
  1193. if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  1194. {
  1195. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1196. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  1197. hsd->State = HAL_SD_STATE_READY;
  1198. return HAL_ERROR;
  1199. }
  1200. else
  1201. {
  1202. /* Enable SD DMA transfer */
  1203. __HAL_SD_DMA_ENABLE(hsd);
  1204. #else
  1205. hsd->pRxBuffPtr = pData;
  1206. hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
  1207. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  1208. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1209. {
  1210. add *= 512U;
  1211. }
  1212. /* Configure the SD DPSM (Data Path State Machine) */
  1213. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1214. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1215. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1216. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  1217. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1218. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1219. config.DPSM = SDMMC_DPSM_DISABLE;
  1220. #else
  1221. config.DPSM = SDMMC_DPSM_ENABLE;
  1222. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1223. (void)SDMMC_ConfigData(hsd->Instance, &config);
  1224. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1225. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  1226. hsd->Instance->IDMABASE0 = (uint32_t) pData ;
  1227. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
  1228. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1229. /* Read Blocks in DMA mode */
  1230. if(NumberOfBlocks > 1U)
  1231. {
  1232. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1233. /* Read Multi Block command */
  1234. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  1235. }
  1236. else
  1237. {
  1238. hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1239. /* Read Single Block command */
  1240. errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
  1241. }
  1242. if(errorstate != HAL_SD_ERROR_NONE)
  1243. {
  1244. /* Clear all the static flags */
  1245. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1246. hsd->ErrorCode |= errorstate;
  1247. hsd->State = HAL_SD_STATE_READY;
  1248. hsd->Context = SD_CONTEXT_NONE;
  1249. return HAL_ERROR;
  1250. }
  1251. /* Enable transfer interrupts */
  1252. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
  1253. return HAL_OK;
  1254. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1255. }
  1256. #endif
  1257. }
  1258. else
  1259. {
  1260. return HAL_BUSY;
  1261. }
  1262. }
  1263. /**
  1264. * @brief Writes block(s) to a specified address in a card. The Data transfer
  1265. * is managed by DMA mode.
  1266. * @note This API should be followed by a check on the card state through
  1267. * HAL_SD_GetCardState().
  1268. * @note You could also check the DMA transfer process through the SD Tx
  1269. * interrupt event.
  1270. * @param hsd Pointer to SD handle
  1271. * @param pData Pointer to the buffer that will contain the data to transmit
  1272. * @param BlockAdd Block Address where data will be written
  1273. * @param NumberOfBlocks Number of blocks to write
  1274. * @retval HAL status
  1275. */
  1276. HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1277. {
  1278. SDMMC_DataInitTypeDef config;
  1279. uint32_t errorstate;
  1280. uint32_t add = BlockAdd;
  1281. if(NULL == pData)
  1282. {
  1283. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1284. return HAL_ERROR;
  1285. }
  1286. if(hsd->State == HAL_SD_STATE_READY)
  1287. {
  1288. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1289. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1290. {
  1291. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1292. return HAL_ERROR;
  1293. }
  1294. hsd->State = HAL_SD_STATE_BUSY;
  1295. /* Initialize data control register */
  1296. hsd->Instance->DCTRL = 0U;
  1297. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1298. hsd->pTxBuffPtr = pData;
  1299. hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
  1300. #else
  1301. /* Set the DMA transfer complete callback */
  1302. hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
  1303. /* Set the DMA error callback */
  1304. hsd->hdmatx->XferErrorCallback = SD_DMAError;
  1305. /* Set the DMA Abort callback */
  1306. hsd->hdmatx->XferAbortCallback = NULL;
  1307. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1308. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1309. {
  1310. add *= 512U;
  1311. }
  1312. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1313. /* Configure the SD DPSM (Data Path State Machine) */
  1314. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1315. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1316. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1317. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1318. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1319. config.DPSM = SDMMC_DPSM_DISABLE;
  1320. (void)SDMMC_ConfigData(hsd->Instance, &config);
  1321. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  1322. hsd->Instance->IDMABASE0 = (uint32_t) pData ;
  1323. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
  1324. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1325. /* Write Blocks in Polling mode */
  1326. if(NumberOfBlocks > 1U)
  1327. {
  1328. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1329. /* Write Multi Block command */
  1330. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  1331. }
  1332. else
  1333. {
  1334. hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1335. /* Write Single Block command */
  1336. errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
  1337. }
  1338. if(errorstate != HAL_SD_ERROR_NONE)
  1339. {
  1340. /* Clear all the static flags */
  1341. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1342. hsd->ErrorCode |= errorstate;
  1343. hsd->State = HAL_SD_STATE_READY;
  1344. hsd->Context = SD_CONTEXT_NONE;
  1345. return HAL_ERROR;
  1346. }
  1347. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1348. /* Enable SDMMC DMA transfer */
  1349. __HAL_SD_DMA_ENABLE(hsd);
  1350. /* Enable the DMA Channel */
  1351. if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  1352. {
  1353. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1354. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  1355. hsd->State = HAL_SD_STATE_READY;
  1356. hsd->Context = SD_CONTEXT_NONE;
  1357. return HAL_ERROR;
  1358. }
  1359. else
  1360. {
  1361. /* Configure the SD DPSM (Data Path State Machine) */
  1362. config.DataTimeOut = SDMMC_DATATIMEOUT;
  1363. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  1364. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  1365. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  1366. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  1367. config.DPSM = SDMMC_DPSM_ENABLE;
  1368. (void)SDMMC_ConfigData(hsd->Instance, &config);
  1369. /* Enable SD Error interrupts */
  1370. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
  1371. #else
  1372. /* Enable transfer interrupts */
  1373. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
  1374. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  1375. return HAL_OK;
  1376. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  1377. }
  1378. #endif
  1379. }
  1380. else
  1381. {
  1382. return HAL_BUSY;
  1383. }
  1384. }
  1385. /**
  1386. * @brief Erases the specified memory area of the given SD card.
  1387. * @note This API should be followed by a check on the card state through
  1388. * HAL_SD_GetCardState().
  1389. * @param hsd Pointer to SD handle
  1390. * @param BlockStartAdd Start Block address
  1391. * @param BlockEndAdd End Block address
  1392. * @retval HAL status
  1393. */
  1394. HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  1395. {
  1396. uint32_t errorstate;
  1397. uint32_t start_add = BlockStartAdd;
  1398. uint32_t end_add = BlockEndAdd;
  1399. if(hsd->State == HAL_SD_STATE_READY)
  1400. {
  1401. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1402. if(end_add < start_add)
  1403. {
  1404. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1405. return HAL_ERROR;
  1406. }
  1407. if(end_add > (hsd->SdCard.LogBlockNbr))
  1408. {
  1409. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1410. return HAL_ERROR;
  1411. }
  1412. hsd->State = HAL_SD_STATE_BUSY;
  1413. /* Check if the card command class supports erase command */
  1414. if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
  1415. {
  1416. /* Clear all the static flags */
  1417. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1418. hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  1419. hsd->State = HAL_SD_STATE_READY;
  1420. return HAL_ERROR;
  1421. }
  1422. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  1423. {
  1424. /* Clear all the static flags */
  1425. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1426. hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  1427. hsd->State = HAL_SD_STATE_READY;
  1428. return HAL_ERROR;
  1429. }
  1430. /* Get start and end block for high capacity cards */
  1431. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1432. {
  1433. start_add *= 512U;
  1434. end_add *= 512U;
  1435. }
  1436. /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1437. if(hsd->SdCard.CardType != CARD_SECURED)
  1438. {
  1439. /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
  1440. errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
  1441. if(errorstate != HAL_SD_ERROR_NONE)
  1442. {
  1443. /* Clear all the static flags */
  1444. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1445. hsd->ErrorCode |= errorstate;
  1446. hsd->State = HAL_SD_STATE_READY;
  1447. return HAL_ERROR;
  1448. }
  1449. /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
  1450. errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
  1451. if(errorstate != HAL_SD_ERROR_NONE)
  1452. {
  1453. /* Clear all the static flags */
  1454. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1455. hsd->ErrorCode |= errorstate;
  1456. hsd->State = HAL_SD_STATE_READY;
  1457. return HAL_ERROR;
  1458. }
  1459. }
  1460. /* Send CMD38 ERASE */
  1461. errorstate = SDMMC_CmdErase(hsd->Instance, 0UL);
  1462. if(errorstate != HAL_SD_ERROR_NONE)
  1463. {
  1464. /* Clear all the static flags */
  1465. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  1466. hsd->ErrorCode |= errorstate;
  1467. hsd->State = HAL_SD_STATE_READY;
  1468. return HAL_ERROR;
  1469. }
  1470. hsd->State = HAL_SD_STATE_READY;
  1471. return HAL_OK;
  1472. }
  1473. else
  1474. {
  1475. return HAL_BUSY;
  1476. }
  1477. }
  1478. /**
  1479. * @brief This function handles SD card interrupt request.
  1480. * @param hsd Pointer to SD handle
  1481. * @retval None
  1482. */
  1483. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  1484. {
  1485. uint32_t errorstate;
  1486. uint32_t context = hsd->Context;
  1487. /* Check for SDMMC interrupt flags */
  1488. if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
  1489. {
  1490. SD_Read_IT(hsd);
  1491. }
  1492. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
  1493. {
  1494. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
  1495. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
  1496. SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
  1497. SDMMC_IT_RXFIFOHF);
  1498. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1499. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
  1500. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  1501. #else
  1502. hsd->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
  1503. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1504. if((context & SD_CONTEXT_IT) != 0U)
  1505. {
  1506. if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1507. {
  1508. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1509. if(errorstate != HAL_SD_ERROR_NONE)
  1510. {
  1511. hsd->ErrorCode |= errorstate;
  1512. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1513. hsd->ErrorCallback(hsd);
  1514. #else
  1515. HAL_SD_ErrorCallback(hsd);
  1516. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1517. }
  1518. }
  1519. /* Clear all the static flags */
  1520. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  1521. hsd->State = HAL_SD_STATE_READY;
  1522. hsd->Context = SD_CONTEXT_NONE;
  1523. if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1524. {
  1525. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1526. hsd->RxCpltCallback(hsd);
  1527. #else
  1528. HAL_SD_RxCpltCallback(hsd);
  1529. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1530. }
  1531. else
  1532. {
  1533. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1534. hsd->TxCpltCallback(hsd);
  1535. #else
  1536. HAL_SD_TxCpltCallback(hsd);
  1537. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1538. }
  1539. }
  1540. else if((context & SD_CONTEXT_DMA) != 0U)
  1541. {
  1542. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1543. hsd->Instance->DLEN = 0;
  1544. hsd->Instance->DCTRL = 0;
  1545. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  1546. /* Stop Transfer for Write Multi blocks or Read Multi blocks */
  1547. if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1548. {
  1549. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1550. if(errorstate != HAL_SD_ERROR_NONE)
  1551. {
  1552. hsd->ErrorCode |= errorstate;
  1553. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1554. hsd->ErrorCallback(hsd);
  1555. #else
  1556. HAL_SD_ErrorCallback(hsd);
  1557. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1558. }
  1559. }
  1560. hsd->State = HAL_SD_STATE_READY;
  1561. hsd->Context = SD_CONTEXT_NONE;
  1562. if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1563. {
  1564. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1565. hsd->TxCpltCallback(hsd);
  1566. #else
  1567. HAL_SD_TxCpltCallback(hsd);
  1568. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1569. }
  1570. if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1571. {
  1572. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1573. hsd->RxCpltCallback(hsd);
  1574. #else
  1575. HAL_SD_RxCpltCallback(hsd);
  1576. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1577. }
  1578. #else
  1579. if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1580. {
  1581. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1582. if(errorstate != HAL_SD_ERROR_NONE)
  1583. {
  1584. hsd->ErrorCode |= errorstate;
  1585. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1586. hsd->ErrorCallback(hsd);
  1587. #else
  1588. HAL_SD_ErrorCallback(hsd);
  1589. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1590. }
  1591. }
  1592. if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  1593. {
  1594. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1595. in the SD DCTRL register */
  1596. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  1597. hsd->State = HAL_SD_STATE_READY;
  1598. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1599. hsd->TxCpltCallback(hsd);
  1600. #else
  1601. HAL_SD_TxCpltCallback(hsd);
  1602. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1603. }
  1604. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1605. }
  1606. else
  1607. {
  1608. /* Nothing to do */
  1609. }
  1610. }
  1611. else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
  1612. {
  1613. SD_Write_IT(hsd);
  1614. }
  1615. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
  1616. {
  1617. /* Set Error code */
  1618. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
  1619. {
  1620. hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  1621. }
  1622. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
  1623. {
  1624. hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  1625. }
  1626. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
  1627. {
  1628. hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  1629. }
  1630. if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
  1631. {
  1632. hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  1633. }
  1634. /* Clear All flags */
  1635. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  1636. /* Disable all interrupts */
  1637. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  1638. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  1639. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1640. __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
  1641. hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  1642. hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP;
  1643. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1644. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  1645. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1646. hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
  1647. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT);
  1648. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1649. if((context & SD_CONTEXT_IT) != 0U)
  1650. {
  1651. /* Set the SD state to ready to be able to start again the process */
  1652. hsd->State = HAL_SD_STATE_READY;
  1653. hsd->Context = SD_CONTEXT_NONE;
  1654. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1655. hsd->ErrorCallback(hsd);
  1656. #else
  1657. HAL_SD_ErrorCallback(hsd);
  1658. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1659. }
  1660. else if((context & SD_CONTEXT_DMA) != 0U)
  1661. {
  1662. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1663. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  1664. {
  1665. /* Disable Internal DMA */
  1666. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
  1667. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  1668. /* Set the SD state to ready to be able to start again the process */
  1669. hsd->State = HAL_SD_STATE_READY;
  1670. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1671. hsd->ErrorCallback(hsd);
  1672. #else
  1673. HAL_SD_ErrorCallback(hsd);
  1674. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1675. }
  1676. #else
  1677. /* Abort the SD DMA channel */
  1678. if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1679. {
  1680. /* Set the DMA Tx abort callback */
  1681. hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  1682. /* Abort DMA in IT mode */
  1683. if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  1684. {
  1685. SD_DMATxAbort(hsd->hdmatx);
  1686. }
  1687. }
  1688. else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1689. {
  1690. /* Set the DMA Rx abort callback */
  1691. hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  1692. /* Abort DMA in IT mode */
  1693. if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  1694. {
  1695. SD_DMARxAbort(hsd->hdmarx);
  1696. }
  1697. }
  1698. else
  1699. {
  1700. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1701. hsd->State = HAL_SD_STATE_READY;
  1702. hsd->Context = SD_CONTEXT_NONE;
  1703. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1704. hsd->AbortCpltCallback(hsd);
  1705. #else
  1706. HAL_SD_AbortCallback(hsd);
  1707. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1708. }
  1709. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1710. }
  1711. else
  1712. {
  1713. /* Nothing to do */
  1714. }
  1715. }
  1716. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1717. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET)
  1718. {
  1719. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
  1720. if(READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
  1721. {
  1722. /* Current buffer is buffer0, Transfer complete for buffer1 */
  1723. if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1724. {
  1725. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1726. hsd->Write_DMADblBuf1CpltCallback(hsd);
  1727. #else
  1728. HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(hsd);
  1729. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1730. }
  1731. else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
  1732. {
  1733. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1734. hsd->Read_DMADblBuf1CpltCallback(hsd);
  1735. #else
  1736. HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(hsd);
  1737. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1738. }
  1739. }
  1740. else /* SD_DMA_BUFFER1 */
  1741. {
  1742. /* Current buffer is buffer1, Transfer complete for buffer0 */
  1743. if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1744. {
  1745. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1746. hsd->Write_DMADblBuf0CpltCallback(hsd);
  1747. #else
  1748. HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(hsd);
  1749. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1750. }
  1751. else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
  1752. {
  1753. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1754. hsd->Read_DMADblBuf0CpltCallback(hsd);
  1755. #else
  1756. HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(hsd);
  1757. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1758. }
  1759. }
  1760. }
  1761. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1762. else
  1763. {
  1764. /* Nothing to do */
  1765. }
  1766. }
  1767. /**
  1768. * @brief return the SD state
  1769. * @param hsd Pointer to sd handle
  1770. * @retval HAL state
  1771. */
  1772. HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
  1773. {
  1774. return hsd->State;
  1775. }
  1776. /**
  1777. * @brief Return the SD error code
  1778. * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
  1779. * the configuration information.
  1780. * @retval SD Error Code
  1781. */
  1782. uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
  1783. {
  1784. return hsd->ErrorCode;
  1785. }
  1786. /**
  1787. * @brief Tx Transfer completed callbacks
  1788. * @param hsd Pointer to SD handle
  1789. * @retval None
  1790. */
  1791. __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
  1792. {
  1793. /* Prevent unused argument(s) compilation warning */
  1794. UNUSED(hsd);
  1795. /* NOTE : This function should not be modified, when the callback is needed,
  1796. the HAL_SD_TxCpltCallback can be implemented in the user file
  1797. */
  1798. }
  1799. /**
  1800. * @brief Rx Transfer completed callbacks
  1801. * @param hsd Pointer SD handle
  1802. * @retval None
  1803. */
  1804. __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
  1805. {
  1806. /* Prevent unused argument(s) compilation warning */
  1807. UNUSED(hsd);
  1808. /* NOTE : This function should not be modified, when the callback is needed,
  1809. the HAL_SD_RxCpltCallback can be implemented in the user file
  1810. */
  1811. }
  1812. /**
  1813. * @brief SD error callbacks
  1814. * @param hsd Pointer SD handle
  1815. * @retval None
  1816. */
  1817. __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
  1818. {
  1819. /* Prevent unused argument(s) compilation warning */
  1820. UNUSED(hsd);
  1821. /* NOTE : This function should not be modified, when the callback is needed,
  1822. the HAL_SD_ErrorCallback can be implemented in the user file
  1823. */
  1824. }
  1825. /**
  1826. * @brief SD Abort callbacks
  1827. * @param hsd Pointer SD handle
  1828. * @retval None
  1829. */
  1830. __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
  1831. {
  1832. /* Prevent unused argument(s) compilation warning */
  1833. UNUSED(hsd);
  1834. /* NOTE : This function should not be modified, when the callback is needed,
  1835. the HAL_SD_AbortCallback can be implemented in the user file
  1836. */
  1837. }
  1838. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1839. /**
  1840. * @brief Register a User SD Callback
  1841. * To be used instead of the weak (surcharged) predefined callback
  1842. * @param hsd : SD handle
  1843. * @param CallbackID : ID of the callback to be registered
  1844. * This parameter can be one of the following values:
  1845. * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
  1846. * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
  1847. * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
  1848. * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
  1849. * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
  1850. * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
  1851. * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
  1852. * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
  1853. * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
  1854. * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
  1855. * @param pCallback : pointer to the Callback function
  1856. * @retval status
  1857. */
  1858. HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
  1859. {
  1860. HAL_StatusTypeDef status = HAL_OK;
  1861. if(pCallback == NULL)
  1862. {
  1863. /* Update the error code */
  1864. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1865. return HAL_ERROR;
  1866. }
  1867. /* Process locked */
  1868. __HAL_LOCK(hsd);
  1869. if(hsd->State == HAL_SD_STATE_READY)
  1870. {
  1871. switch (CallbackID)
  1872. {
  1873. case HAL_SD_TX_CPLT_CB_ID :
  1874. hsd->TxCpltCallback = pCallback;
  1875. break;
  1876. case HAL_SD_RX_CPLT_CB_ID :
  1877. hsd->RxCpltCallback = pCallback;
  1878. break;
  1879. case HAL_SD_ERROR_CB_ID :
  1880. hsd->ErrorCallback = pCallback;
  1881. break;
  1882. case HAL_SD_ABORT_CB_ID :
  1883. hsd->AbortCpltCallback = pCallback;
  1884. break;
  1885. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1886. case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
  1887. hsd->Read_DMADblBuf0CpltCallback = pCallback;
  1888. break;
  1889. case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
  1890. hsd->Read_DMADblBuf1CpltCallback = pCallback;
  1891. break;
  1892. case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
  1893. hsd->Write_DMADblBuf0CpltCallback = pCallback;
  1894. break;
  1895. case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
  1896. hsd->Write_DMADblBuf1CpltCallback = pCallback;
  1897. break;
  1898. #endif
  1899. case HAL_SD_MSP_INIT_CB_ID :
  1900. hsd->MspInitCallback = pCallback;
  1901. break;
  1902. case HAL_SD_MSP_DEINIT_CB_ID :
  1903. hsd->MspDeInitCallback = pCallback;
  1904. break;
  1905. default :
  1906. /* Update the error code */
  1907. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1908. /* update return status */
  1909. status = HAL_ERROR;
  1910. break;
  1911. }
  1912. }
  1913. else if (hsd->State == HAL_SD_STATE_RESET)
  1914. {
  1915. switch (CallbackID)
  1916. {
  1917. case HAL_SD_MSP_INIT_CB_ID :
  1918. hsd->MspInitCallback = pCallback;
  1919. break;
  1920. case HAL_SD_MSP_DEINIT_CB_ID :
  1921. hsd->MspDeInitCallback = pCallback;
  1922. break;
  1923. default :
  1924. /* Update the error code */
  1925. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1926. /* update return status */
  1927. status = HAL_ERROR;
  1928. break;
  1929. }
  1930. }
  1931. else
  1932. {
  1933. /* Update the error code */
  1934. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1935. /* update return status */
  1936. status = HAL_ERROR;
  1937. }
  1938. /* Release Lock */
  1939. __HAL_UNLOCK(hsd);
  1940. return status;
  1941. }
  1942. /**
  1943. * @brief Unregister a User SD Callback
  1944. * SD Callback is redirected to the weak (surcharged) predefined callback
  1945. * @param hsd : SD handle
  1946. * @param CallbackID : ID of the callback to be unregistered
  1947. * This parameter can be one of the following values:
  1948. * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
  1949. * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
  1950. * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
  1951. * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
  1952. * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
  1953. * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
  1954. * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
  1955. * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
  1956. * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
  1957. * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
  1958. * @retval status
  1959. */
  1960. HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
  1961. {
  1962. HAL_StatusTypeDef status = HAL_OK;
  1963. /* Process locked */
  1964. __HAL_LOCK(hsd);
  1965. if(hsd->State == HAL_SD_STATE_READY)
  1966. {
  1967. switch (CallbackID)
  1968. {
  1969. case HAL_SD_TX_CPLT_CB_ID :
  1970. hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
  1971. break;
  1972. case HAL_SD_RX_CPLT_CB_ID :
  1973. hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
  1974. break;
  1975. case HAL_SD_ERROR_CB_ID :
  1976. hsd->ErrorCallback = HAL_SD_ErrorCallback;
  1977. break;
  1978. case HAL_SD_ABORT_CB_ID :
  1979. hsd->AbortCpltCallback = HAL_SD_AbortCallback;
  1980. break;
  1981. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1982. case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
  1983. hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuffer0CpltCallback;
  1984. break;
  1985. case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
  1986. hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuffer1CpltCallback;
  1987. break;
  1988. case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
  1989. hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuffer0CpltCallback;
  1990. break;
  1991. case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
  1992. hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuffer1CpltCallback;
  1993. break;
  1994. #endif
  1995. case HAL_SD_MSP_INIT_CB_ID :
  1996. hsd->MspInitCallback = HAL_SD_MspInit;
  1997. break;
  1998. case HAL_SD_MSP_DEINIT_CB_ID :
  1999. hsd->MspDeInitCallback = HAL_SD_MspDeInit;
  2000. break;
  2001. default :
  2002. /* Update the error code */
  2003. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  2004. /* update return status */
  2005. status = HAL_ERROR;
  2006. break;
  2007. }
  2008. }
  2009. else if (hsd->State == HAL_SD_STATE_RESET)
  2010. {
  2011. switch (CallbackID)
  2012. {
  2013. case HAL_SD_MSP_INIT_CB_ID :
  2014. hsd->MspInitCallback = HAL_SD_MspInit;
  2015. break;
  2016. case HAL_SD_MSP_DEINIT_CB_ID :
  2017. hsd->MspDeInitCallback = HAL_SD_MspDeInit;
  2018. break;
  2019. default :
  2020. /* Update the error code */
  2021. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  2022. /* update return status */
  2023. status = HAL_ERROR;
  2024. break;
  2025. }
  2026. }
  2027. else
  2028. {
  2029. /* Update the error code */
  2030. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  2031. /* update return status */
  2032. status = HAL_ERROR;
  2033. }
  2034. /* Release Lock */
  2035. __HAL_UNLOCK(hsd);
  2036. return status;
  2037. }
  2038. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2039. /**
  2040. * @brief Register a User SD Transceiver Callback
  2041. * To be used instead of the weak (surcharged) predefined callback
  2042. * @param hsd : SD handle
  2043. * @param pCallback : pointer to the Callback function
  2044. * @retval status
  2045. */
  2046. HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback)
  2047. {
  2048. HAL_StatusTypeDef status = HAL_OK;
  2049. if(pCallback == NULL)
  2050. {
  2051. /* Update the error code */
  2052. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  2053. return HAL_ERROR;
  2054. }
  2055. /* Process locked */
  2056. __HAL_LOCK(hsd);
  2057. if(hsd->State == HAL_SD_STATE_READY)
  2058. {
  2059. hsd->DriveTransceiver_1_8V_Callback = pCallback;
  2060. }
  2061. else
  2062. {
  2063. /* Update the error code */
  2064. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  2065. /* update return status */
  2066. status = HAL_ERROR;
  2067. }
  2068. /* Release Lock */
  2069. __HAL_UNLOCK(hsd);
  2070. return status;
  2071. }
  2072. /**
  2073. * @brief Unregister a User SD Transceiver Callback
  2074. * SD Callback is redirected to the weak (surcharged) predefined callback
  2075. * @param hsd : SD handle
  2076. * @retval status
  2077. */
  2078. HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef *hsd)
  2079. {
  2080. HAL_StatusTypeDef status = HAL_OK;
  2081. /* Process locked */
  2082. __HAL_LOCK(hsd);
  2083. if(hsd->State == HAL_SD_STATE_READY)
  2084. {
  2085. hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
  2086. }
  2087. else
  2088. {
  2089. /* Update the error code */
  2090. hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  2091. /* update return status */
  2092. status = HAL_ERROR;
  2093. }
  2094. /* Release Lock */
  2095. __HAL_UNLOCK(hsd);
  2096. return status;
  2097. }
  2098. #endif
  2099. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  2100. /**
  2101. * @}
  2102. */
  2103. /** @addtogroup SD_Exported_Functions_Group3
  2104. * @brief management functions
  2105. *
  2106. @verbatim
  2107. ==============================================================================
  2108. ##### Peripheral Control functions #####
  2109. ==============================================================================
  2110. [..]
  2111. This subsection provides a set of functions allowing to control the SD card
  2112. operations and get the related information
  2113. @endverbatim
  2114. * @{
  2115. */
  2116. /**
  2117. * @brief Returns information the information of the card which are stored on
  2118. * the CID register.
  2119. * @param hsd Pointer to SD handle
  2120. * @param pCID Pointer to a HAL_SD_CardCIDTypeDef structure that
  2121. * contains all CID register parameters
  2122. * @retval HAL status
  2123. */
  2124. HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
  2125. {
  2126. pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
  2127. pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
  2128. pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
  2129. pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
  2130. pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
  2131. pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
  2132. pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
  2133. pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
  2134. pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
  2135. pCID->Reserved2 = 1U;
  2136. return HAL_OK;
  2137. }
  2138. /**
  2139. * @brief Returns information the information of the card which are stored on
  2140. * the CSD register.
  2141. * @param hsd Pointer to SD handle
  2142. * @param pCSD Pointer to a HAL_SD_CardCSDTypeDef structure that
  2143. * contains all CSD register parameters
  2144. * @retval HAL status
  2145. */
  2146. HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
  2147. {
  2148. pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
  2149. pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
  2150. pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
  2151. pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
  2152. pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
  2153. pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
  2154. pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
  2155. pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
  2156. pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
  2157. pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
  2158. pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
  2159. pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
  2160. pCSD->Reserved2 = 0U; /*!< Reserved */
  2161. if(hsd->SdCard.CardType == CARD_SDSC)
  2162. {
  2163. pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
  2164. pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
  2165. pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
  2166. pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
  2167. pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
  2168. pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
  2169. hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
  2170. hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
  2171. hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
  2172. hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
  2173. hsd->SdCard.LogBlockSize = 512U;
  2174. }
  2175. else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
  2176. {
  2177. /* Byte 7 */
  2178. pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
  2179. hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
  2180. hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
  2181. hsd->SdCard.BlockSize = 512U;
  2182. hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
  2183. }
  2184. else
  2185. {
  2186. /* Clear all the static flags */
  2187. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2188. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2189. hsd->State = HAL_SD_STATE_READY;
  2190. return HAL_ERROR;
  2191. }
  2192. pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
  2193. pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
  2194. pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
  2195. pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
  2196. pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
  2197. pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
  2198. pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
  2199. pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
  2200. pCSD->Reserved3 = 0;
  2201. pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
  2202. pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
  2203. pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
  2204. pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
  2205. pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
  2206. pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
  2207. pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
  2208. pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
  2209. pCSD->Reserved4 = 1;
  2210. return HAL_OK;
  2211. }
  2212. /**
  2213. * @brief Gets the SD status info.
  2214. * @param hsd Pointer to SD handle
  2215. * @param pStatus Pointer to the HAL_SD_CardStatusTypeDef structure that
  2216. * will contain the SD card status information
  2217. * @retval HAL status
  2218. */
  2219. HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
  2220. {
  2221. uint32_t sd_status[16];
  2222. uint32_t errorstate;
  2223. HAL_StatusTypeDef status = HAL_OK;
  2224. errorstate = SD_SendSDStatus(hsd, sd_status);
  2225. if(errorstate != HAL_SD_ERROR_NONE)
  2226. {
  2227. /* Clear all the static flags */
  2228. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2229. hsd->ErrorCode |= errorstate;
  2230. hsd->State = HAL_SD_STATE_READY;
  2231. status = HAL_ERROR;
  2232. }
  2233. else
  2234. {
  2235. pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
  2236. pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
  2237. pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
  2238. pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
  2239. ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
  2240. pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
  2241. pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
  2242. pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
  2243. pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
  2244. pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
  2245. pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
  2246. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2247. pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U);
  2248. pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ;
  2249. pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U);
  2250. #endif
  2251. }
  2252. /* Set Block Size for Card */
  2253. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  2254. if(errorstate != HAL_SD_ERROR_NONE)
  2255. {
  2256. /* Clear all the static flags */
  2257. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2258. hsd->ErrorCode = errorstate;
  2259. hsd->State = HAL_SD_STATE_READY;
  2260. status = HAL_ERROR;
  2261. }
  2262. return status;
  2263. }
  2264. /**
  2265. * @brief Gets the SD card info.
  2266. * @param hsd Pointer to SD handle
  2267. * @param pCardInfo Pointer to the HAL_SD_CardInfoTypeDef structure that
  2268. * will contain the SD card status information
  2269. * @retval HAL status
  2270. */
  2271. HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
  2272. {
  2273. pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
  2274. pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
  2275. pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
  2276. pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
  2277. pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
  2278. pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
  2279. pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
  2280. pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
  2281. return HAL_OK;
  2282. }
  2283. /**
  2284. * @brief Enables wide bus operation for the requested card if supported by
  2285. * card.
  2286. * @param hsd Pointer to SD handle
  2287. * @param WideMode Specifies the SD card wide bus mode
  2288. * This parameter can be one of the following values:
  2289. * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
  2290. * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
  2291. * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
  2292. * @retval HAL status
  2293. */
  2294. HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
  2295. {
  2296. SDMMC_InitTypeDef Init;
  2297. uint32_t errorstate;
  2298. uint32_t sdmmc_clk;
  2299. HAL_StatusTypeDef status = HAL_OK;
  2300. /* Check the parameters */
  2301. assert_param(IS_SDMMC_BUS_WIDE(WideMode));
  2302. /* Change State */
  2303. hsd->State = HAL_SD_STATE_BUSY;
  2304. if(hsd->SdCard.CardType != CARD_SECURED)
  2305. {
  2306. if(WideMode == SDMMC_BUS_WIDE_8B)
  2307. {
  2308. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2309. }
  2310. else if(WideMode == SDMMC_BUS_WIDE_4B)
  2311. {
  2312. errorstate = SD_WideBus_Enable(hsd);
  2313. hsd->ErrorCode |= errorstate;
  2314. }
  2315. else if(WideMode == SDMMC_BUS_WIDE_1B)
  2316. {
  2317. errorstate = SD_WideBus_Disable(hsd);
  2318. hsd->ErrorCode |= errorstate;
  2319. }
  2320. else
  2321. {
  2322. /* WideMode is not a valid argument*/
  2323. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  2324. }
  2325. }
  2326. else
  2327. {
  2328. /* MMC Card does not support this feature */
  2329. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2330. }
  2331. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2332. {
  2333. /* Clear all the static flags */
  2334. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2335. status = HAL_ERROR;
  2336. }
  2337. else
  2338. {
  2339. sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC1);
  2340. if (sdmmc_clk != 0U)
  2341. {
  2342. /* Configure the SDMMC peripheral */
  2343. Init.ClockEdge = hsd->Init.ClockEdge;
  2344. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2345. Init.ClockBypass = hsd->Init.ClockBypass;
  2346. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  2347. Init.ClockPowerSave = hsd->Init.ClockPowerSave;
  2348. Init.BusWide = WideMode;
  2349. Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
  2350. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2351. /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */
  2352. if (hsd->Init.ClockDiv >= (sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ)))
  2353. {
  2354. Init.ClockDiv = hsd->Init.ClockDiv;
  2355. }
  2356. else if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
  2357. {
  2358. /* UltraHigh speed SD card,user Clock div */
  2359. Init.ClockDiv = hsd->Init.ClockDiv;
  2360. }
  2361. else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
  2362. {
  2363. /* High speed SD card, Max Frequency = 50Mhz */
  2364. if (hsd->Init.ClockDiv == 0U)
  2365. {
  2366. if (sdmmc_clk > SD_HIGH_SPEED_FREQ)
  2367. {
  2368. Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ);
  2369. }
  2370. else
  2371. {
  2372. Init.ClockDiv = hsd->Init.ClockDiv;
  2373. }
  2374. }
  2375. else
  2376. {
  2377. if ((sdmmc_clk/(2U * hsd->Init.ClockDiv)) > SD_HIGH_SPEED_FREQ)
  2378. {
  2379. Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ);
  2380. }
  2381. else
  2382. {
  2383. Init.ClockDiv = hsd->Init.ClockDiv;
  2384. }
  2385. }
  2386. }
  2387. else
  2388. {
  2389. /* No High speed SD card, Max Frequency = 25Mhz */
  2390. if (hsd->Init.ClockDiv == 0U)
  2391. {
  2392. if (sdmmc_clk > SD_NORMAL_SPEED_FREQ)
  2393. {
  2394. Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ);
  2395. }
  2396. else
  2397. {
  2398. Init.ClockDiv = hsd->Init.ClockDiv;
  2399. }
  2400. }
  2401. else
  2402. {
  2403. if ((sdmmc_clk/(2U * hsd->Init.ClockDiv)) > SD_NORMAL_SPEED_FREQ)
  2404. {
  2405. Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ);
  2406. }
  2407. else
  2408. {
  2409. Init.ClockDiv = hsd->Init.ClockDiv;
  2410. }
  2411. }
  2412. }
  2413. Init.Transceiver = hsd->Init.Transceiver;
  2414. #else
  2415. if ((sdmmc_clk / (hsd->Init.ClockDiv + 2U)) > SD_NORMAL_SPEED_FREQ)
  2416. {
  2417. Init.ClockDiv = ((sdmmc_clk / SD_NORMAL_SPEED_FREQ) - 2U);
  2418. }
  2419. else
  2420. {
  2421. Init.ClockDiv = hsd->Init.ClockDiv;
  2422. }
  2423. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2424. (void)SDMMC_Init(hsd->Instance, Init);
  2425. }
  2426. else
  2427. {
  2428. hsd->ErrorCode |= SDMMC_ERROR_INVALID_PARAMETER;
  2429. status = HAL_ERROR;
  2430. }
  2431. }
  2432. /* Set Block Size for Card */
  2433. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  2434. if(errorstate != HAL_SD_ERROR_NONE)
  2435. {
  2436. /* Clear all the static flags */
  2437. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2438. hsd->ErrorCode |= errorstate;
  2439. status = HAL_ERROR;
  2440. }
  2441. /* Change State */
  2442. hsd->State = HAL_SD_STATE_READY;
  2443. return status;
  2444. }
  2445. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2446. /**
  2447. * @brief Configure the speed bus mode
  2448. * @param hsd Pointer to the SD handle
  2449. * @param SpeedMode Specifies the SD card speed bus mode
  2450. * This parameter can be one of the following values:
  2451. * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card
  2452. * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed/SDR12 mode
  2453. * @arg SDMMC_SPEED_MODE_HIGH: High Speed/SDR25 mode
  2454. * @arg SDMMC_SPEED_MODE_ULTRA: Ultra high speed mode
  2455. * @retval HAL status
  2456. */
  2457. HAL_StatusTypeDef HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef *hsd, uint32_t SpeedMode)
  2458. {
  2459. uint32_t tickstart;
  2460. uint32_t errorstate;
  2461. HAL_StatusTypeDef status = HAL_OK;
  2462. /* Check the parameters */
  2463. assert_param(IS_SDMMC_SPEED_MODE(SpeedMode));
  2464. /* Change State */
  2465. hsd->State = HAL_SD_STATE_BUSY;
  2466. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  2467. {
  2468. switch (SpeedMode)
  2469. {
  2470. case SDMMC_SPEED_MODE_AUTO:
  2471. {
  2472. if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  2473. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  2474. {
  2475. hsd->Instance->CLKCR |= 0x00100000U;
  2476. /* Enable Ultra High Speed */
  2477. if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2478. {
  2479. if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2480. {
  2481. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2482. status = HAL_ERROR;
  2483. }
  2484. }
  2485. }
  2486. else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
  2487. {
  2488. /* Enable High Speed */
  2489. if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2490. {
  2491. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2492. status = HAL_ERROR;
  2493. }
  2494. }
  2495. else
  2496. {
  2497. /*Nothing to do, Use defaultSpeed */
  2498. }
  2499. break;
  2500. }
  2501. case SDMMC_SPEED_MODE_ULTRA:
  2502. {
  2503. if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  2504. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  2505. {
  2506. hsd->Instance->CLKCR |= 0x00100000U;
  2507. /* Enable UltraHigh Speed */
  2508. if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2509. {
  2510. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2511. status = HAL_ERROR;
  2512. }
  2513. }
  2514. else
  2515. {
  2516. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2517. status = HAL_ERROR;
  2518. }
  2519. break;
  2520. }
  2521. case SDMMC_SPEED_MODE_DDR:
  2522. {
  2523. if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  2524. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  2525. {
  2526. hsd->Instance->CLKCR |= 0x00100000U;
  2527. /* Enable DDR Mode*/
  2528. if (SD_DDR_Mode(hsd) != HAL_SD_ERROR_NONE)
  2529. {
  2530. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2531. status = HAL_ERROR;
  2532. }
  2533. }
  2534. else
  2535. {
  2536. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2537. status = HAL_ERROR;
  2538. }
  2539. break;
  2540. }
  2541. case SDMMC_SPEED_MODE_HIGH:
  2542. {
  2543. if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  2544. (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
  2545. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  2546. {
  2547. /* Enable High Speed */
  2548. if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2549. {
  2550. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2551. status = HAL_ERROR;
  2552. }
  2553. }
  2554. else
  2555. {
  2556. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2557. status = HAL_ERROR;
  2558. }
  2559. break;
  2560. }
  2561. case SDMMC_SPEED_MODE_DEFAULT:
  2562. break;
  2563. default:
  2564. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  2565. status = HAL_ERROR;
  2566. break;
  2567. }
  2568. }
  2569. else
  2570. {
  2571. switch (SpeedMode)
  2572. {
  2573. case SDMMC_SPEED_MODE_AUTO:
  2574. {
  2575. if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  2576. (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
  2577. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  2578. {
  2579. /* Enable High Speed */
  2580. if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2581. {
  2582. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2583. status = HAL_ERROR;
  2584. }
  2585. }
  2586. else
  2587. {
  2588. /*Nothing to do, Use defaultSpeed */
  2589. }
  2590. break;
  2591. }
  2592. case SDMMC_SPEED_MODE_HIGH:
  2593. {
  2594. if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
  2595. (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
  2596. (hsd->SdCard.CardType == CARD_SDHC_SDXC))
  2597. {
  2598. /* Enable High Speed */
  2599. if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
  2600. {
  2601. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2602. status = HAL_ERROR;
  2603. }
  2604. }
  2605. else
  2606. {
  2607. hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2608. status = HAL_ERROR;
  2609. }
  2610. break;
  2611. }
  2612. case SDMMC_SPEED_MODE_DEFAULT:
  2613. break;
  2614. case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
  2615. default:
  2616. hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  2617. status = HAL_ERROR;
  2618. break;
  2619. }
  2620. }
  2621. /* Verify that SD card is ready to use after Speed mode switch*/
  2622. tickstart = HAL_GetTick();
  2623. while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
  2624. {
  2625. if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  2626. {
  2627. hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
  2628. hsd->State = HAL_SD_STATE_READY;
  2629. return HAL_TIMEOUT;
  2630. }
  2631. }
  2632. /* Set Block Size for Card */
  2633. errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  2634. if(errorstate != HAL_SD_ERROR_NONE)
  2635. {
  2636. /* Clear all the static flags */
  2637. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2638. hsd->ErrorCode |= errorstate;
  2639. status = HAL_ERROR;
  2640. }
  2641. /* Change State */
  2642. hsd->State = HAL_SD_STATE_READY;
  2643. return status;
  2644. }
  2645. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2646. /**
  2647. * @brief Gets the current sd card data state.
  2648. * @param hsd pointer to SD handle
  2649. * @retval Card state
  2650. */
  2651. HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
  2652. {
  2653. uint32_t cardstate;
  2654. uint32_t errorstate;
  2655. uint32_t resp1 = 0;
  2656. errorstate = SD_SendStatus(hsd, &resp1);
  2657. if(errorstate != HAL_SD_ERROR_NONE)
  2658. {
  2659. hsd->ErrorCode |= errorstate;
  2660. }
  2661. cardstate = ((resp1 >> 9U) & 0x0FU);
  2662. return (HAL_SD_CardStateTypeDef)cardstate;
  2663. }
  2664. /**
  2665. * @brief Abort the current transfer and disable the SD.
  2666. * @param hsd pointer to a SD_HandleTypeDef structure that contains
  2667. * the configuration information for SD module.
  2668. * @retval HAL status
  2669. */
  2670. HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
  2671. {
  2672. HAL_SD_CardStateTypeDef CardState;
  2673. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2674. uint32_t context = hsd->Context;
  2675. #endif
  2676. /* DIsable All interrupts */
  2677. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2678. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2679. /* Clear All flags */
  2680. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2681. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2682. /* If IDMA Context, disable Internal DMA */
  2683. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  2684. #else
  2685. CLEAR_BIT(hsd->Instance->DCTRL, SDMMC_DCTRL_DTEN);
  2686. if ((context & SD_CONTEXT_DMA) != 0U)
  2687. {
  2688. /* Disable the SD DMA request */
  2689. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2690. /* Abort the SD DMA Tx channel */
  2691. if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  2692. {
  2693. if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
  2694. {
  2695. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2696. }
  2697. }
  2698. /* Abort the SD DMA Rx channel */
  2699. else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  2700. {
  2701. if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
  2702. {
  2703. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2704. }
  2705. }
  2706. else
  2707. {
  2708. /* Nothing to do */
  2709. }
  2710. }
  2711. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2712. hsd->State = HAL_SD_STATE_READY;
  2713. /* Initialize the SD operation */
  2714. hsd->Context = SD_CONTEXT_NONE;
  2715. CardState = HAL_SD_GetCardState(hsd);
  2716. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2717. {
  2718. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2719. }
  2720. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2721. {
  2722. return HAL_ERROR;
  2723. }
  2724. return HAL_OK;
  2725. }
  2726. /**
  2727. * @brief Abort the current transfer and disable the SD (IT mode).
  2728. * @param hsd pointer to a SD_HandleTypeDef structure that contains
  2729. * the configuration information for SD module.
  2730. * @retval HAL status
  2731. */
  2732. HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
  2733. {
  2734. HAL_SD_CardStateTypeDef CardState;
  2735. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2736. uint32_t context = hsd->Context;
  2737. #endif
  2738. /* Disable All interrupts */
  2739. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2740. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2741. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  2742. /* If IDMA Context, disable Internal DMA */
  2743. hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
  2744. /* Clear All flags */
  2745. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2746. CardState = HAL_SD_GetCardState(hsd);
  2747. hsd->State = HAL_SD_STATE_READY;
  2748. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2749. {
  2750. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2751. }
  2752. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2753. {
  2754. return HAL_ERROR;
  2755. }
  2756. else
  2757. {
  2758. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  2759. hsd->AbortCpltCallback(hsd);
  2760. #else
  2761. HAL_SD_AbortCallback(hsd);
  2762. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  2763. }
  2764. #else
  2765. CLEAR_BIT(hsd->Instance->DCTRL, SDMMC_DCTRL_DTEN);
  2766. if ((context & SD_CONTEXT_DMA) != 0U)
  2767. {
  2768. /* Disable the SD DMA request */
  2769. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2770. /* Abort the SD DMA Tx channel */
  2771. if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  2772. {
  2773. hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  2774. if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  2775. {
  2776. hsd->hdmatx = NULL;
  2777. }
  2778. }
  2779. /* Abort the SD DMA Rx channel */
  2780. else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  2781. {
  2782. hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  2783. if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  2784. {
  2785. hsd->hdmarx = NULL;
  2786. }
  2787. }
  2788. else
  2789. {
  2790. /* Nothing to do */
  2791. }
  2792. }
  2793. /* No transfer ongoing on both DMA channels*/
  2794. else
  2795. {
  2796. /* Clear All flags */
  2797. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2798. CardState = HAL_SD_GetCardState(hsd);
  2799. hsd->State = HAL_SD_STATE_READY;
  2800. hsd->Context = SD_CONTEXT_NONE;
  2801. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2802. {
  2803. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2804. }
  2805. if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2806. {
  2807. return HAL_ERROR;
  2808. }
  2809. else
  2810. {
  2811. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  2812. hsd->AbortCpltCallback(hsd);
  2813. #else
  2814. HAL_SD_AbortCallback(hsd);
  2815. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  2816. }
  2817. }
  2818. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2819. return HAL_OK;
  2820. }
  2821. /**
  2822. * @}
  2823. */
  2824. /**
  2825. * @}
  2826. */
  2827. /* Private function ----------------------------------------------------------*/
  2828. /** @addtogroup SD_Private_Functions
  2829. * @{
  2830. */
  2831. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  2832. /**
  2833. * @brief DMA SD transmit process complete callback
  2834. * @param hdma DMA handle
  2835. * @retval None
  2836. */
  2837. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2838. {
  2839. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2840. /* Enable DATAEND Interrupt */
  2841. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
  2842. }
  2843. /**
  2844. * @brief DMA SD receive process complete callback
  2845. * @param hdma DMA handle
  2846. * @retval None
  2847. */
  2848. static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2849. {
  2850. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2851. uint32_t errorstate;
  2852. /* Send stop command in multiblock write */
  2853. if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
  2854. {
  2855. errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  2856. if(errorstate != HAL_SD_ERROR_NONE)
  2857. {
  2858. hsd->ErrorCode |= errorstate;
  2859. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2860. hsd->ErrorCallback(hsd);
  2861. #else
  2862. HAL_SD_ErrorCallback(hsd);
  2863. #endif
  2864. }
  2865. }
  2866. /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  2867. in the SD DCTRL register */
  2868. hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
  2869. /* Clear all the static flags */
  2870. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2871. hsd->State = HAL_SD_STATE_READY;
  2872. hsd->Context = SD_CONTEXT_NONE;
  2873. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2874. hsd->RxCpltCallback(hsd);
  2875. #else
  2876. HAL_SD_RxCpltCallback(hsd);
  2877. #endif
  2878. }
  2879. /**
  2880. * @brief DMA SD communication error callback
  2881. * @param hdma DMA handle
  2882. * @retval None
  2883. */
  2884. static void SD_DMAError(DMA_HandleTypeDef *hdma)
  2885. {
  2886. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2887. HAL_SD_CardStateTypeDef CardState;
  2888. uint32_t RxErrorCode, TxErrorCode;
  2889. RxErrorCode = hsd->hdmarx->ErrorCode;
  2890. TxErrorCode = hsd->hdmatx->ErrorCode;
  2891. if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
  2892. {
  2893. /* Clear All flags */
  2894. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
  2895. /* Disable All interrupts */
  2896. __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
  2897. SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
  2898. hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2899. CardState = HAL_SD_GetCardState(hsd);
  2900. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2901. {
  2902. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2903. }
  2904. hsd->State= HAL_SD_STATE_READY;
  2905. hsd->Context = SD_CONTEXT_NONE;
  2906. }
  2907. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2908. hsd->ErrorCallback(hsd);
  2909. #else
  2910. HAL_SD_ErrorCallback(hsd);
  2911. #endif
  2912. }
  2913. /**
  2914. * @brief DMA SD Tx Abort callback
  2915. * @param hdma DMA handle
  2916. * @retval None
  2917. */
  2918. static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
  2919. {
  2920. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2921. HAL_SD_CardStateTypeDef CardState;
  2922. /* Clear All flags */
  2923. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2924. CardState = HAL_SD_GetCardState(hsd);
  2925. hsd->State = HAL_SD_STATE_READY;
  2926. hsd->Context = SD_CONTEXT_NONE;
  2927. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2928. {
  2929. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2930. }
  2931. if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
  2932. {
  2933. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2934. hsd->AbortCpltCallback(hsd);
  2935. #else
  2936. HAL_SD_AbortCallback(hsd);
  2937. #endif
  2938. }
  2939. else
  2940. {
  2941. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2942. hsd->ErrorCallback(hsd);
  2943. #else
  2944. HAL_SD_ErrorCallback(hsd);
  2945. #endif
  2946. }
  2947. }
  2948. /**
  2949. * @brief DMA SD Rx Abort callback
  2950. * @param hdma DMA handle
  2951. * @retval None
  2952. */
  2953. static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
  2954. {
  2955. SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2956. HAL_SD_CardStateTypeDef CardState;
  2957. /* Clear All flags */
  2958. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  2959. CardState = HAL_SD_GetCardState(hsd);
  2960. hsd->State = HAL_SD_STATE_READY;
  2961. hsd->Context = SD_CONTEXT_NONE;
  2962. if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2963. {
  2964. hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2965. }
  2966. if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
  2967. {
  2968. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2969. hsd->AbortCpltCallback(hsd);
  2970. #else
  2971. HAL_SD_AbortCallback(hsd);
  2972. #endif
  2973. }
  2974. else
  2975. {
  2976. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2977. hsd->ErrorCallback(hsd);
  2978. #else
  2979. HAL_SD_ErrorCallback(hsd);
  2980. #endif
  2981. }
  2982. }
  2983. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  2984. /**
  2985. * @brief Initializes the sd card.
  2986. * @param hsd Pointer to SD handle
  2987. * @retval SD Card error state
  2988. */
  2989. static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
  2990. {
  2991. HAL_SD_CardCSDTypeDef CSD;
  2992. uint32_t errorstate;
  2993. uint16_t sd_rca = 1U;
  2994. /* Check the power State */
  2995. if(SDMMC_GetPowerState(hsd->Instance) == 0U)
  2996. {
  2997. /* Power off */
  2998. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2999. }
  3000. if(hsd->SdCard.CardType != CARD_SECURED)
  3001. {
  3002. /* Send CMD2 ALL_SEND_CID */
  3003. errorstate = SDMMC_CmdSendCID(hsd->Instance);
  3004. if(errorstate != HAL_SD_ERROR_NONE)
  3005. {
  3006. return errorstate;
  3007. }
  3008. else
  3009. {
  3010. /* Get Card identification number data */
  3011. hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  3012. hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  3013. hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  3014. hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  3015. }
  3016. }
  3017. if(hsd->SdCard.CardType != CARD_SECURED)
  3018. {
  3019. /* Send CMD3 SET_REL_ADDR with argument 0 */
  3020. /* SD Card publishes its RCA. */
  3021. errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
  3022. if(errorstate != HAL_SD_ERROR_NONE)
  3023. {
  3024. return errorstate;
  3025. }
  3026. }
  3027. if(hsd->SdCard.CardType != CARD_SECURED)
  3028. {
  3029. /* Get the SD card RCA */
  3030. hsd->SdCard.RelCardAdd = sd_rca;
  3031. /* Send CMD9 SEND_CSD with argument as card's RCA */
  3032. errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3033. if(errorstate != HAL_SD_ERROR_NONE)
  3034. {
  3035. return errorstate;
  3036. }
  3037. else
  3038. {
  3039. /* Get Card Specific Data */
  3040. hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  3041. hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
  3042. hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
  3043. hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
  3044. }
  3045. }
  3046. /* Get the Card Class */
  3047. hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
  3048. /* Get CSD parameters */
  3049. if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
  3050. {
  3051. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  3052. }
  3053. /* Select the Card */
  3054. errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
  3055. if(errorstate != HAL_SD_ERROR_NONE)
  3056. {
  3057. return errorstate;
  3058. }
  3059. #if !defined(STM32L4P5xx) && !defined(STM32L4Q5xx) && !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
  3060. /* Configure SDMMC peripheral interface */
  3061. (void)SDMMC_Init(hsd->Instance, hsd->Init);
  3062. #endif /* !STM32L4P5xx && !STM32L4Q5xx && !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
  3063. /* All cards are initialized */
  3064. return HAL_SD_ERROR_NONE;
  3065. }
  3066. /**
  3067. * @brief Enquires cards about their operating voltage and configures clock
  3068. * controls and stores SD information that will be needed in future
  3069. * in the SD handle.
  3070. * @param hsd Pointer to SD handle
  3071. * @retval error state
  3072. */
  3073. static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
  3074. {
  3075. __IO uint32_t count = 0U;
  3076. uint32_t response = 0U, validvoltage = 0U;
  3077. uint32_t errorstate;
  3078. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3079. uint32_t tickstart = HAL_GetTick();
  3080. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3081. /* CMD0: GO_IDLE_STATE */
  3082. errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
  3083. if(errorstate != HAL_SD_ERROR_NONE)
  3084. {
  3085. return errorstate;
  3086. }
  3087. /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
  3088. errorstate = SDMMC_CmdOperCond(hsd->Instance);
  3089. if(errorstate != HAL_SD_ERROR_NONE)
  3090. {
  3091. hsd->SdCard.CardVersion = CARD_V1_X;
  3092. /* CMD0: GO_IDLE_STATE */
  3093. errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
  3094. if(errorstate != HAL_SD_ERROR_NONE)
  3095. {
  3096. return errorstate;
  3097. }
  3098. }
  3099. else
  3100. {
  3101. hsd->SdCard.CardVersion = CARD_V2_X;
  3102. }
  3103. if( hsd->SdCard.CardVersion == CARD_V2_X)
  3104. {
  3105. /* SEND CMD55 APP_CMD with RCA as 0 */
  3106. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  3107. if(errorstate != HAL_SD_ERROR_NONE)
  3108. {
  3109. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  3110. }
  3111. }
  3112. /* SD CARD */
  3113. /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  3114. while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
  3115. {
  3116. /* SEND CMD55 APP_CMD with RCA as 0 */
  3117. errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  3118. if(errorstate != HAL_SD_ERROR_NONE)
  3119. {
  3120. return errorstate;
  3121. }
  3122. /* Send CMD41 */
  3123. errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
  3124. if(errorstate != HAL_SD_ERROR_NONE)
  3125. {
  3126. return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  3127. }
  3128. /* Get command response */
  3129. response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  3130. /* Get operating voltage*/
  3131. validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
  3132. count++;
  3133. }
  3134. if(count >= SDMMC_MAX_VOLT_TRIAL)
  3135. {
  3136. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  3137. }
  3138. if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  3139. {
  3140. hsd->SdCard.CardType = CARD_SDHC_SDXC;
  3141. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3142. if(hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE)
  3143. {
  3144. if((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
  3145. {
  3146. hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
  3147. /* Start switching procedue */
  3148. hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
  3149. /* Send CMD11 to switch 1.8V mode */
  3150. errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
  3151. if(errorstate != HAL_SD_ERROR_NONE)
  3152. {
  3153. return errorstate;
  3154. }
  3155. /* Check to CKSTOP */
  3156. while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
  3157. {
  3158. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  3159. {
  3160. return HAL_SD_ERROR_TIMEOUT;
  3161. }
  3162. }
  3163. /* Clear CKSTOP Flag */
  3164. hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
  3165. /* Check to BusyD0 */
  3166. if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
  3167. {
  3168. /* Error when activate Voltage Switch in SDMMC Peripheral */
  3169. return SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3170. }
  3171. else
  3172. {
  3173. /* Enable Transceiver Switch PIN */
  3174. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  3175. hsd->DriveTransceiver_1_8V_Callback(SET);
  3176. #else
  3177. HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
  3178. #endif
  3179. /* Switch ready */
  3180. hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
  3181. /* Check VSWEND Flag */
  3182. while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
  3183. {
  3184. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  3185. {
  3186. return HAL_SD_ERROR_TIMEOUT;
  3187. }
  3188. }
  3189. /* Clear VSWEND Flag */
  3190. hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
  3191. /* Check BusyD0 status */
  3192. if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
  3193. {
  3194. /* Error when enabling 1.8V mode */
  3195. return HAL_SD_ERROR_INVALID_VOLTRANGE;
  3196. }
  3197. /* Switch to 1.8V OK */
  3198. /* Disable VSWITCH FLAG from SDMMC Peripheral */
  3199. hsd->Instance->POWER = 0x13U;
  3200. /* Clean Status flags */
  3201. hsd->Instance->ICR = 0xFFFFFFFFU;
  3202. }
  3203. }
  3204. }
  3205. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3206. }
  3207. else
  3208. {
  3209. hsd->SdCard.CardType = CARD_SDSC;
  3210. }
  3211. return HAL_SD_ERROR_NONE;
  3212. }
  3213. /**
  3214. * @brief Turns the SDMMC output signals off.
  3215. * @param hsd Pointer to SD handle
  3216. * @retval None
  3217. */
  3218. static void SD_PowerOFF(SD_HandleTypeDef *hsd)
  3219. {
  3220. /* Set Power State to OFF */
  3221. (void)SDMMC_PowerState_OFF(hsd->Instance);
  3222. }
  3223. /**
  3224. * @brief Send Status info command.
  3225. * @param hsd pointer to SD handle
  3226. * @param pSDstatus Pointer to the buffer that will contain the SD card status
  3227. * SD Status register)
  3228. * @retval error state
  3229. */
  3230. static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
  3231. {
  3232. SDMMC_DataInitTypeDef config;
  3233. uint32_t errorstate;
  3234. uint32_t tickstart = HAL_GetTick();
  3235. uint32_t count;
  3236. uint32_t *pData = pSDstatus;
  3237. /* Check SD response */
  3238. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  3239. {
  3240. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  3241. }
  3242. /* Set block size for card if it is not equal to current block size for card */
  3243. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
  3244. if(errorstate != HAL_SD_ERROR_NONE)
  3245. {
  3246. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  3247. return errorstate;
  3248. }
  3249. /* Send CMD55 */
  3250. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3251. if(errorstate != HAL_SD_ERROR_NONE)
  3252. {
  3253. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  3254. return errorstate;
  3255. }
  3256. /* Configure the SD DPSM (Data Path State Machine) */
  3257. config.DataTimeOut = SDMMC_DATATIMEOUT;
  3258. config.DataLength = 64U;
  3259. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
  3260. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  3261. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  3262. config.DPSM = SDMMC_DPSM_ENABLE;
  3263. (void)SDMMC_ConfigData(hsd->Instance, &config);
  3264. /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
  3265. errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
  3266. if(errorstate != HAL_SD_ERROR_NONE)
  3267. {
  3268. hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  3269. return errorstate;
  3270. }
  3271. /* Get status data */
  3272. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3273. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  3274. #else
  3275. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  3276. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3277. {
  3278. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  3279. {
  3280. for(count = 0U; count < 8U; count++)
  3281. {
  3282. *pData = SDMMC_ReadFIFO(hsd->Instance);
  3283. pData++;
  3284. }
  3285. }
  3286. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  3287. {
  3288. return HAL_SD_ERROR_TIMEOUT;
  3289. }
  3290. }
  3291. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  3292. {
  3293. return HAL_SD_ERROR_DATA_TIMEOUT;
  3294. }
  3295. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  3296. {
  3297. return HAL_SD_ERROR_DATA_CRC_FAIL;
  3298. }
  3299. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  3300. {
  3301. return HAL_SD_ERROR_RX_OVERRUN;
  3302. }
  3303. else
  3304. {
  3305. /* Nothing to do */
  3306. }
  3307. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3308. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
  3309. #else
  3310. while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
  3311. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3312. {
  3313. *pData = SDMMC_ReadFIFO(hsd->Instance);
  3314. pData++;
  3315. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  3316. {
  3317. return HAL_SD_ERROR_TIMEOUT;
  3318. }
  3319. }
  3320. /* Clear all the static status flags*/
  3321. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  3322. return HAL_SD_ERROR_NONE;
  3323. }
  3324. /**
  3325. * @brief Returns the current card's status.
  3326. * @param hsd Pointer to SD handle
  3327. * @param pCardStatus pointer to the buffer that will contain the SD card
  3328. * status (Card Status register)
  3329. * @retval error state
  3330. */
  3331. static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
  3332. {
  3333. uint32_t errorstate;
  3334. if(pCardStatus == NULL)
  3335. {
  3336. return HAL_SD_ERROR_PARAM;
  3337. }
  3338. /* Send Status command */
  3339. errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3340. if(errorstate != HAL_SD_ERROR_NONE)
  3341. {
  3342. return errorstate;
  3343. }
  3344. /* Get SD card status */
  3345. *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
  3346. return HAL_SD_ERROR_NONE;
  3347. }
  3348. /**
  3349. * @brief Enables the SDMMC wide bus mode.
  3350. * @param hsd pointer to SD handle
  3351. * @retval error state
  3352. */
  3353. static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
  3354. {
  3355. uint32_t scr[2U] = {0UL, 0UL};
  3356. uint32_t errorstate;
  3357. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  3358. {
  3359. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  3360. }
  3361. /* Get SCR Register */
  3362. errorstate = SD_FindSCR(hsd, scr);
  3363. if(errorstate != HAL_SD_ERROR_NONE)
  3364. {
  3365. return errorstate;
  3366. }
  3367. /* If requested card supports wide bus operation */
  3368. if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
  3369. {
  3370. /* Send CMD55 APP_CMD with argument as card's RCA.*/
  3371. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3372. if(errorstate != HAL_SD_ERROR_NONE)
  3373. {
  3374. return errorstate;
  3375. }
  3376. /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  3377. errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
  3378. if(errorstate != HAL_SD_ERROR_NONE)
  3379. {
  3380. return errorstate;
  3381. }
  3382. return HAL_SD_ERROR_NONE;
  3383. }
  3384. else
  3385. {
  3386. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3387. }
  3388. }
  3389. /**
  3390. * @brief Disables the SDMMC wide bus mode.
  3391. * @param hsd Pointer to SD handle
  3392. * @retval error state
  3393. */
  3394. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
  3395. {
  3396. uint32_t scr[2U] = {0UL, 0UL};
  3397. uint32_t errorstate;
  3398. if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  3399. {
  3400. return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  3401. }
  3402. /* Get SCR Register */
  3403. errorstate = SD_FindSCR(hsd, scr);
  3404. if(errorstate != HAL_SD_ERROR_NONE)
  3405. {
  3406. return errorstate;
  3407. }
  3408. /* If requested card supports 1 bit mode operation */
  3409. if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
  3410. {
  3411. /* Send CMD55 APP_CMD with argument as card's RCA */
  3412. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3413. if(errorstate != HAL_SD_ERROR_NONE)
  3414. {
  3415. return errorstate;
  3416. }
  3417. /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
  3418. errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
  3419. if(errorstate != HAL_SD_ERROR_NONE)
  3420. {
  3421. return errorstate;
  3422. }
  3423. return HAL_SD_ERROR_NONE;
  3424. }
  3425. else
  3426. {
  3427. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3428. }
  3429. }
  3430. /**
  3431. * @brief Finds the SD card SCR register value.
  3432. * @param hsd Pointer to SD handle
  3433. * @param pSCR pointer to the buffer that will contain the SCR value
  3434. * @retval error state
  3435. */
  3436. static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
  3437. {
  3438. SDMMC_DataInitTypeDef config;
  3439. uint32_t errorstate;
  3440. uint32_t tickstart = HAL_GetTick();
  3441. uint32_t index = 0U;
  3442. uint32_t tempscr[2U] = {0UL, 0UL};
  3443. uint32_t *scr = pSCR;
  3444. /* Set Block Size To 8 Bytes */
  3445. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
  3446. if(errorstate != HAL_SD_ERROR_NONE)
  3447. {
  3448. return errorstate;
  3449. }
  3450. /* Send CMD55 APP_CMD with argument as card's RCA */
  3451. errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
  3452. if(errorstate != HAL_SD_ERROR_NONE)
  3453. {
  3454. return errorstate;
  3455. }
  3456. config.DataTimeOut = SDMMC_DATATIMEOUT;
  3457. config.DataLength = 8U;
  3458. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
  3459. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  3460. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  3461. config.DPSM = SDMMC_DPSM_ENABLE;
  3462. (void)SDMMC_ConfigData(hsd->Instance, &config);
  3463. /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  3464. errorstate = SDMMC_CmdSendSCR(hsd->Instance);
  3465. if(errorstate != HAL_SD_ERROR_NONE)
  3466. {
  3467. return errorstate;
  3468. }
  3469. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3470. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | SDMMC_FLAG_DATAEND))
  3471. {
  3472. if((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U))
  3473. {
  3474. tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
  3475. tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
  3476. index++;
  3477. }
  3478. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  3479. {
  3480. return HAL_SD_ERROR_TIMEOUT;
  3481. }
  3482. }
  3483. #else
  3484. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
  3485. {
  3486. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
  3487. {
  3488. *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
  3489. index++;
  3490. }
  3491. if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
  3492. {
  3493. return HAL_SD_ERROR_TIMEOUT;
  3494. }
  3495. }
  3496. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3497. if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  3498. {
  3499. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  3500. return HAL_SD_ERROR_DATA_TIMEOUT;
  3501. }
  3502. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  3503. {
  3504. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  3505. return HAL_SD_ERROR_DATA_CRC_FAIL;
  3506. }
  3507. else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  3508. {
  3509. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  3510. return HAL_SD_ERROR_RX_OVERRUN;
  3511. }
  3512. else
  3513. {
  3514. /* No error flag set */
  3515. /* Clear all the static flags */
  3516. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  3517. *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
  3518. ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
  3519. scr++;
  3520. *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
  3521. ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
  3522. }
  3523. return HAL_SD_ERROR_NONE;
  3524. }
  3525. /**
  3526. * @brief Wrap up reading in non-blocking mode.
  3527. * @param hsd pointer to a SD_HandleTypeDef structure that contains
  3528. * the configuration information.
  3529. * @retval None
  3530. */
  3531. static void SD_Read_IT(SD_HandleTypeDef *hsd)
  3532. {
  3533. uint32_t count, data, dataremaining;
  3534. uint8_t* tmp;
  3535. tmp = hsd->pRxBuffPtr;
  3536. dataremaining = hsd->RxXferSize;
  3537. if (dataremaining > 0U)
  3538. {
  3539. /* Read data from SDMMC Rx FIFO */
  3540. for(count = 0U; count < 8U; count++)
  3541. {
  3542. data = SDMMC_ReadFIFO(hsd->Instance);
  3543. *tmp = (uint8_t)(data & 0xFFU);
  3544. tmp++;
  3545. dataremaining--;
  3546. *tmp = (uint8_t)((data >> 8U) & 0xFFU);
  3547. tmp++;
  3548. dataremaining--;
  3549. *tmp = (uint8_t)((data >> 16U) & 0xFFU);
  3550. tmp++;
  3551. dataremaining--;
  3552. *tmp = (uint8_t)((data >> 24U) & 0xFFU);
  3553. tmp++;
  3554. dataremaining--;
  3555. }
  3556. hsd->pRxBuffPtr = tmp;
  3557. hsd->RxXferSize = dataremaining;
  3558. }
  3559. }
  3560. /**
  3561. * @brief Wrap up writing in non-blocking mode.
  3562. * @param hsd pointer to a SD_HandleTypeDef structure that contains
  3563. * the configuration information.
  3564. * @retval None
  3565. */
  3566. static void SD_Write_IT(SD_HandleTypeDef *hsd)
  3567. {
  3568. uint32_t count, data, dataremaining;
  3569. uint8_t* tmp;
  3570. tmp = hsd->pTxBuffPtr;
  3571. dataremaining = hsd->TxXferSize;
  3572. if (dataremaining > 0U)
  3573. {
  3574. /* Write data to SDMMC Tx FIFO */
  3575. for(count = 0U; count < 8U; count++)
  3576. {
  3577. data = (uint32_t)(*tmp);
  3578. tmp++;
  3579. dataremaining--;
  3580. data |= ((uint32_t)(*tmp) << 8U);
  3581. tmp++;
  3582. dataremaining--;
  3583. data |= ((uint32_t)(*tmp) << 16U);
  3584. tmp++;
  3585. dataremaining--;
  3586. data |= ((uint32_t)(*tmp) << 24U);
  3587. tmp++;
  3588. dataremaining--;
  3589. (void)SDMMC_WriteFIFO(hsd->Instance, &data);
  3590. }
  3591. hsd->pTxBuffPtr = tmp;
  3592. hsd->TxXferSize = dataremaining;
  3593. }
  3594. }
  3595. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3596. /**
  3597. * @brief Switches the SD card to High Speed mode.
  3598. * This API must be used after "Transfer State"
  3599. * @note This operation should be followed by the configuration
  3600. * of PLL to have SDMMCCK clock between 50 and 120 MHz
  3601. * @param hsd SD handle
  3602. * @retval SD Card error state
  3603. */
  3604. uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd)
  3605. {
  3606. uint32_t errorstate = HAL_SD_ERROR_NONE;
  3607. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  3608. uint32_t SD_hs[16] = {0};
  3609. uint32_t count, loop = 0 ;
  3610. uint32_t Timeout = HAL_GetTick();
  3611. if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
  3612. {
  3613. /* Standard Speed Card <= 12.5Mhz */
  3614. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3615. }
  3616. if(hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
  3617. {
  3618. /* Initialize the Data control register */
  3619. hsd->Instance->DCTRL = 0;
  3620. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
  3621. if (errorstate != HAL_SD_ERROR_NONE)
  3622. {
  3623. return errorstate;
  3624. }
  3625. /* Configure the SD DPSM (Data Path State Machine) */
  3626. sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
  3627. sdmmc_datainitstructure.DataLength = 64U;
  3628. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
  3629. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  3630. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  3631. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  3632. (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
  3633. errorstate = SDMMC_CmdSwitch(hsd->Instance,SDMMC_SDR25_SWITCH_PATTERN);
  3634. if(errorstate != HAL_SD_ERROR_NONE)
  3635. {
  3636. return errorstate;
  3637. }
  3638. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
  3639. {
  3640. if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  3641. {
  3642. for (count = 0U; count < 8U; count++)
  3643. {
  3644. SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
  3645. }
  3646. loop ++;
  3647. }
  3648. if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
  3649. {
  3650. hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
  3651. hsd->State= HAL_SD_STATE_READY;
  3652. return HAL_SD_ERROR_TIMEOUT;
  3653. }
  3654. }
  3655. if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  3656. {
  3657. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  3658. return errorstate;
  3659. }
  3660. else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  3661. {
  3662. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  3663. errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
  3664. return errorstate;
  3665. }
  3666. else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  3667. {
  3668. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  3669. errorstate = SDMMC_ERROR_RX_OVERRUN;
  3670. return errorstate;
  3671. }
  3672. else
  3673. {
  3674. /* No error flag set */
  3675. }
  3676. /* Clear all the static flags */
  3677. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  3678. /* Test if the switch mode HS is ok */
  3679. if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
  3680. {
  3681. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3682. }
  3683. }
  3684. return errorstate;
  3685. }
  3686. /**
  3687. * @brief Switches the SD card to Ultra High Speed mode.
  3688. * This API must be used after "Transfer State"
  3689. * @note This operation should be followed by the configuration
  3690. * of PLL to have SDMMCCK clock between 50 and 120 MHz
  3691. * @param hsd SD handle
  3692. * @retval SD Card error state
  3693. */
  3694. static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd)
  3695. {
  3696. uint32_t errorstate = HAL_SD_ERROR_NONE;
  3697. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  3698. uint32_t SD_hs[16] = {0};
  3699. uint32_t count, loop = 0 ;
  3700. uint32_t Timeout = HAL_GetTick();
  3701. if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
  3702. {
  3703. /* Standard Speed Card <= 12.5Mhz */
  3704. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3705. }
  3706. if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) &&
  3707. (hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE))
  3708. {
  3709. /* Initialize the Data control register */
  3710. hsd->Instance->DCTRL = 0;
  3711. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
  3712. if (errorstate != HAL_SD_ERROR_NONE)
  3713. {
  3714. return errorstate;
  3715. }
  3716. /* Configure the SD DPSM (Data Path State Machine) */
  3717. sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
  3718. sdmmc_datainitstructure.DataLength = 64U;
  3719. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
  3720. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  3721. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  3722. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  3723. (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
  3724. errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR104_SWITCH_PATTERN);
  3725. if(errorstate != HAL_SD_ERROR_NONE)
  3726. {
  3727. return errorstate;
  3728. }
  3729. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
  3730. {
  3731. if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  3732. {
  3733. for (count = 0U; count < 8U; count++)
  3734. {
  3735. SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
  3736. }
  3737. loop ++;
  3738. }
  3739. if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
  3740. {
  3741. hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
  3742. hsd->State= HAL_SD_STATE_READY;
  3743. return HAL_SD_ERROR_TIMEOUT;
  3744. }
  3745. }
  3746. if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  3747. {
  3748. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  3749. return errorstate;
  3750. }
  3751. else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  3752. {
  3753. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  3754. errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
  3755. return errorstate;
  3756. }
  3757. else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  3758. {
  3759. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  3760. errorstate = SDMMC_ERROR_RX_OVERRUN;
  3761. return errorstate;
  3762. }
  3763. else
  3764. {
  3765. /* No error flag set */
  3766. }
  3767. /* Clear all the static flags */
  3768. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  3769. /* Test if the switch mode HS is ok */
  3770. if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
  3771. {
  3772. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3773. }
  3774. else
  3775. {
  3776. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  3777. hsd->DriveTransceiver_1_8V_Callback(SET);
  3778. #else
  3779. HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
  3780. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  3781. #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
  3782. /* Enable DelayBlock Peripheral */
  3783. /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
  3784. MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
  3785. if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
  3786. {
  3787. return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
  3788. }
  3789. #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
  3790. }
  3791. }
  3792. return errorstate;
  3793. }
  3794. /**
  3795. * @brief Switches the SD card to Double Data Rate (DDR) mode.
  3796. * This API must be used after "Transfer State"
  3797. * @note This operation should be followed by the configuration
  3798. * of PLL to have SDMMCCK clock less than 50MHz
  3799. * @param hsd SD handle
  3800. * @retval SD Card error state
  3801. */
  3802. static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd)
  3803. {
  3804. uint32_t errorstate = HAL_SD_ERROR_NONE;
  3805. SDMMC_DataInitTypeDef sdmmc_datainitstructure;
  3806. uint32_t SD_hs[16] = {0};
  3807. uint32_t count, loop = 0 ;
  3808. uint32_t Timeout = HAL_GetTick();
  3809. if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
  3810. {
  3811. /* Standard Speed Card <= 12.5Mhz */
  3812. return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3813. }
  3814. if((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) &&
  3815. (hsd->Init.Transceiver == SDMMC_TRANSCEIVER_ENABLE))
  3816. {
  3817. /* Initialize the Data control register */
  3818. hsd->Instance->DCTRL = 0;
  3819. errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
  3820. if (errorstate != HAL_SD_ERROR_NONE)
  3821. {
  3822. return errorstate;
  3823. }
  3824. /* Configure the SD DPSM (Data Path State Machine) */
  3825. sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
  3826. sdmmc_datainitstructure.DataLength = 64U;
  3827. sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
  3828. sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  3829. sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  3830. sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
  3831. (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
  3832. errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN);
  3833. if(errorstate != HAL_SD_ERROR_NONE)
  3834. {
  3835. return errorstate;
  3836. }
  3837. while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
  3838. {
  3839. if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
  3840. {
  3841. for (count = 0U; count < 8U; count++)
  3842. {
  3843. SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
  3844. }
  3845. loop ++;
  3846. }
  3847. if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
  3848. {
  3849. hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
  3850. hsd->State= HAL_SD_STATE_READY;
  3851. return HAL_SD_ERROR_TIMEOUT;
  3852. }
  3853. }
  3854. if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
  3855. {
  3856. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
  3857. return errorstate;
  3858. }
  3859. else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
  3860. {
  3861. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
  3862. errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
  3863. return errorstate;
  3864. }
  3865. else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
  3866. {
  3867. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
  3868. errorstate = SDMMC_ERROR_RX_OVERRUN;
  3869. return errorstate;
  3870. }
  3871. else
  3872. {
  3873. /* No error flag set */
  3874. }
  3875. /* Clear all the static flags */
  3876. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  3877. /* Test if the switch mode is ok */
  3878. if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
  3879. {
  3880. errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
  3881. }
  3882. else
  3883. {
  3884. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  3885. hsd->DriveTransceiver_1_8V_Callback(SET);
  3886. #else
  3887. HAL_SDEx_DriveTransceiver_1_8V_Callback(SET);
  3888. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  3889. #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
  3890. /* Enable DelayBlock Peripheral */
  3891. /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
  3892. MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
  3893. if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
  3894. {
  3895. return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
  3896. }
  3897. #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
  3898. }
  3899. }
  3900. return errorstate;
  3901. }
  3902. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3903. /**
  3904. * @}
  3905. */
  3906. #endif /* HAL_SD_MODULE_ENABLED */
  3907. /**
  3908. * @}
  3909. */
  3910. /**
  3911. * @}
  3912. */
  3913. #endif /* SDMMC1 */