123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042 |
- /**
- ******************************************************************************
- * @file stm324xg_eval_sdio_sd.c
- * @author MCD Application Team
- * @version V1.0.2
- * @date 09-March-2012
- * @brief This file provides a set of functions needed to manage the SDIO SD
- * Card memory mounted on STM324xG-EVAL evaluation board.
- *
- *
- * @verbatim
- *
- * ===================================================================
- * How to use this driver
- * ===================================================================
- * It implements a high level communication layer for read and write
- * from/to this memory. The needed STM32 hardware resources (SDIO and
- * GPIO) are defined in stm324xg_eval.h file, and the initialization is
- * performed in SD_LowLevel_Init() function declared in stm324xg_eval.c
- * file.
- * You can easily tailor this driver to any other development board,
- * by just adapting the defines for hardware resources and
- * SD_LowLevel_Init() function.
- *
- * A - SD Card Initialization and configuration
- * ============================================
- * - To initialize the SD Card, use the SD_Init() function. It
- * Initializes the SD Card and put it into StandBy State (Ready
- * for data transfer). This function provide the following operations:
- *
- * 1 - Apply the SD Card initialization process at 400KHz and check
- * the SD Card type (Standard Capacity or High Capacity). You
- * can change or adapt this frequency by adjusting the
- * "SDIO_INIT_CLK_DIV" define inside the stm324xg_eval.h file.
- * The SD Card frequency (SDIO_CK) is computed as follows:
- *
- * +---------------------------------------------+
- * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
- * +---------------------------------------------+
- *
- * In initialization mode and according to the SD Card standard,
- * make sure that the SDIO_CK frequency don't exceed 400KHz.
- *
- * 2 - Get the SD CID and CSD data. All these information are
- * managed by the SDCardInfo structure. This structure provide
- * also ready computed SD Card capacity and Block size.
- *
- * 3 - Configure the SD Card Data transfer frequency. By Default,
- * the card transfer frequency is set to 24MHz. You can change
- * or adapt this frequency by adjusting the "SDIO_TRANSFER_CLK_DIV"
- * define inside the stm324xg_eval.h file.
- * The SD Card frequency (SDIO_CK) is computed as follows:
- *
- * +---------------------------------------------+
- * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
- * +---------------------------------------------+
- *
- * In transfer mode and according to the SD Card standard,
- * make sure that the SDIO_CK frequency don't exceed 25MHz
- * and 50MHz in High-speed mode switch.
- * To be able to use a frequency higher than 24MHz, you should
- * use the SDIO peripheral in bypass mode. Refer to the
- * corresponding reference manual for more details.
- *
- * 4 - Select the corresponding SD Card according to the address
- * read with the step 2.
- *
- * 5 - Configure the SD Card in wide bus mode: 4-bits data.
- *
- * B - SD Card Read operation
- * ==========================
- * - You can read SD card by using two function: SD_ReadBlock() and
- * SD_ReadMultiBlocks() functions. These functions support only
- * 512-byte block length.
- * - The SD_ReadBlock() function read only one block (512-byte). This
- * function can transfer the data using DMA controller or using
- * polling mode. To select between DMA or polling mode refer to
- * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm324xg_eval_sdio_sd.h
- * file and uncomment the corresponding line. By default the SD DMA
- * mode is selected
- * - The SD_ReadMultiBlocks() function read only mutli blocks (multiple
- * of 512-byte).
- * - Any read operation should be followed by two functions to check
- * if the DMA Controller and SD Card status.
- * - SD_ReadWaitOperation(): this function insure that the DMA
- * controller has finished all data transfer.
- * - SD_GetStatus(): to check that the SD Card has finished the
- * data transfer and it is ready for data.
- *
- * - The DMA transfer is finished by the SDIO Data End interrupt.
- * User has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler()
- * and SD_ProcessDMAIRQ() function inside the DMA2_Streamx_IRQHandler().
- * Don't forget to enable the SDIO_IRQn and DMA2_Stream3_IRQn or
- * DMA2_Stream6_IRQn interrupts using the NVIC controller.
- *
- * C - SD Card Write operation
- * ===========================
- * - You can write SD card by using two function: SD_WriteBlock() and
- * SD_WriteMultiBlocks() functions. These functions support only
- * 512-byte block length.
- * - The SD_WriteBlock() function write only one block (512-byte). This
- * function can transfer the data using DMA controller or using
- * polling mode. To select between DMA or polling mode refer to
- * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm324xg_eval_sdio_sd.h
- * file and uncomment the corresponding line. By default the SD DMA
- * mode is selected
- * - The SD_WriteMultiBlocks() function write only mutli blocks (multiple
- * of 512-byte).
- * - Any write operation should be followed by two functions to check
- * if the DMA Controller and SD Card status.
- * - SD_ReadWaitOperation(): this function insure that the DMA
- * controller has finished all data transfer.
- * - SD_GetStatus(): to check that the SD Card has finished the
- * data transfer and it is ready for data.
- *
- * - The DMA transfer is finished by the SDIO Data End interrupt.
- * User has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler()
- * and SD_ProcessDMAIRQ() function inside the DMA2_Streamx_IRQHandler().
- * Don't forget to enable the SDIO_IRQn and DMA2_Stream3_IRQn or
- * DMA2_Stream6_IRQn interrupts using the NVIC controller.
- *
- *
- * D - SD card status
- * ==================
- * - At any time, you can check the SD Card status and get the SD card
- * state by using the SD_GetStatus() function. This function checks
- * first if the SD card is still connected and then get the internal
- * SD Card transfer state.
- * - You can also get the SD card SD Status register by using the
- * SD_SendSDStatus() function.
- *
- * E - Programming Model (Selecting DMA for SDIO data Transfer)
- * ============================================================
- * Status = SD_Init(); // Initialization Step as described in section A
- *
- * // SDIO Interrupt ENABLE
- * NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
- * NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- * NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- * NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- * NVIC_Init(&NVIC_InitStructure);
- * // DMA2 STREAMx Interrupt ENABLE
- * NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
- * NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- * NVIC_Init(&NVIC_InitStructure);
- *
- * // Write operation as described in Section C
- * Status = SD_WriteBlock(buffer, address, 512);
- * Status = SD_WaitWriteOperation();
- * while(SD_GetStatus() != SD_TRANSFER_OK);
- *
- * Status = SD_WriteMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
- * Status = SD_WaitWriteOperation();
- * while(SD_GetStatus() != SD_TRANSFER_OK);
- *
- * // Read operation as described in Section B
- * Status = SD_ReadBlock(buffer, address, 512);
- * Status = SD_WaitReadOperation();
- * while(SD_GetStatus() != SD_TRANSFER_OK);
- *
- * Status = SD_ReadMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
- * Status = SD_WaitReadOperation();
- * while(SD_GetStatus() != SD_TRANSFER_OK);
- *
- * - Add the SDIO and DMA2 StreamX (3 or 6) IRQ Handlers:
- * void SDIO_IRQHandler(void)
- * {
- * SD_ProcessIRQ();
- * }
- * void SD_SDIO_DMA_IRQHANDLER(void)
- * {
- * SD_ProcessDMAIRQ();
- * }
- *
- * F - Programming Model (Selecting Polling for SDIO data Transfer)
- * ================================================================
- * //Only SD Card Single Block operation are managed.
- * Status = SD_Init(); // Initialization Step as described in section
- *
- * // Write operation as described in Section C
- * Status = SD_WriteBlock(buffer, address, 512);
- *
- * // Read operation as described in Section B
- * Status = SD_ReadBlock(buffer, address, 512);
- *
- * STM32 SDIO Pin assignment
- * =========================
- * +-----------------------------------------------------------+
- * | Pin assignment |
- * +-----------------------------+---------------+-------------+
- * | STM32 SDIO Pins | SD | Pin |
- * +-----------------------------+---------------+-------------+
- * | SDIO D2 | D2 | 1 |
- * | SDIO D3 | D3 | 2 |
- * | SDIO CMD | CMD | 3 |
- * | | VCC | 4 (3.3 V)|
- * | SDIO CLK | CLK | 5 |
- * | | GND | 6 (0 V) |
- * | SDIO D0 | D0 | 7 |
- * | SDIO D1 | D1 | 8 |
- * +-----------------------------+---------------+-------------+
- *
- * @endverbatim
- *
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "sdcard.h"
- /** @addtogroup Utilities
- * @{
- */
- /** @addtogroup STM32_EVAL
- * @{
- */
- /** @addtogroup STM324xG_EVAL
- * @{
- */
- /** @addtogroup STM324xG_EVAL_SDIO_SD
- * @brief This file provides all the SD Card driver firmware functions.
- * @{
- */
- /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Types
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Defines
- * @{
- */
- /**
- * @brief SDIO Static flags, TimeOut, FIFO Address
- */
- #define NULL 0
- #define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
- #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
- /**
- * @brief Mask for errors Card Status R1 (OCR Register)
- */
- #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
- #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
- #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
- #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
- #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
- #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
- #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
- #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
- #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
- #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
- #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
- #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
- #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
- #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
- #define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000)
- #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
- #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
- #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
- #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
- #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
- /**
- * @brief Masks for R6 Response
- */
- #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
- #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
- #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
- #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
- #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
- #define SD_STD_CAPACITY ((uint32_t)0x00000000)
- #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
- #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
- #define SD_ALLZERO ((uint32_t)0x00000000)
- #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
- #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
- #define SD_CARD_LOCKED ((uint32_t)0x02000000)
- #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
- #define SD_0TO7BITS ((uint32_t)0x000000FF)
- #define SD_8TO15BITS ((uint32_t)0x0000FF00)
- #define SD_16TO23BITS ((uint32_t)0x00FF0000)
- #define SD_24TO31BITS ((uint32_t)0xFF000000)
- #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
- #define SD_HALFFIFO ((uint32_t)0x00000008)
- #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
- /**
- * @brief Command Class Supported
- */
- #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
- #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
- #define SD_CCCC_ERASE ((uint32_t)0x00000020)
- /**
- * @brief Following commands are SD Card Specific commands.
- * SDIO_APP_CMD should be sent before sending these commands.
- */
- #define SDIO_SEND_IF_COND ((uint32_t)0x00000008)
- /**
- * @}
- */
- /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Macros
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Variables
- * @{
- */
- static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1;
- static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0;
- static uint8_t SDSTATUS_Tab[16];
- __IO uint32_t StopCondition = 0;
- __IO SD_Error TransferError = SD_OK;
- __IO uint32_t TransferEnd = 0, DMAEndOfTransfer = 0;
- SD_CardInfo SDCardInfo;
- SDIO_InitTypeDef SDIO_InitStructure;
- SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
- SDIO_DataInitTypeDef SDIO_DataInitStructure;
- /**
- * @}
- */
- /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Function_Prototypes
- * @{
- */
- static SD_Error CmdError(void);
- static SD_Error CmdResp1Error(uint8_t cmd);
- static SD_Error CmdResp7Error(void);
- static SD_Error CmdResp3Error(void);
- static SD_Error CmdResp2Error(void);
- static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca);
- static SD_Error SDEnWideBus(FunctionalState NewState);
- static SD_Error IsCardProgramming(uint8_t *pstatus);
- static SD_Error FindSCR(uint16_t rca, uint32_t *pscr);
- uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes);
- /**
- * @}
- */
- /** @defgroup STM324xG_EVAL_SDIO_SD_Private_Functions
- * @{
- */
- /**
- * @brief DeInitializes the SDIO interface.
- * @param None
- * @retval None
- */
- void SD_DeInit(void)
- {
- SD_LowLevel_DeInit();
- }
- /**
- * @brief Initializes the SD Card and put it into StandBy State (Ready for data
- * transfer).
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_Init(void)
- {
- __IO SD_Error errorstatus = SD_OK;
- /* 配置NVIC */
- SDIO_Interrupts_Config();
- /* SDIO Peripheral Low Level Init */
- SD_LowLevel_Init();
- SDIO_DeInit();
- errorstatus = SD_PowerON();
- if (errorstatus != SD_OK)
- {
- /*!< CMD Response TimeOut (wait for CMDSENT flag) */
- return (errorstatus);
- }
- errorstatus = SD_InitializeCards();
- if (errorstatus != SD_OK)
- {
- /*!< CMD Response TimeOut (wait for CMDSENT flag) */
- return (errorstatus);
- }
- /*!< Configure the SDIO peripheral */
- /*!< SDIO_CK = SDIOCLK / (SDIO_TRANSFER_CLK_DIV + 2) */
- /*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
- SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
- SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
- SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
- SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
- SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
- SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
- SDIO_Init(&SDIO_InitStructure);
- /*----------------- Read CSD/CID MSD registers ------------------*/
- errorstatus = SD_GetCardInfo(&SDCardInfo);
- if (errorstatus == SD_OK)
- {
- /*----------------- Select Card --------------------------------*/
- errorstatus = SD_SelectDeselect((uint32_t)(SDCardInfo.RCA << 16));
- }
- if (errorstatus == SD_OK)
- {
- errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b);
- }
- return (errorstatus);
- }
- /**
- * @brief Gets the cuurent sd card data transfer status.
- * @param None
- * @retval SDTransferState: Data Transfer state.
- * This value can be:
- * - SD_TRANSFER_OK: No data transfer is acting
- * - SD_TRANSFER_BUSY: Data transfer is acting
- */
- SDTransferState SD_GetStatus(void)
- {
- SDCardState cardstate = SD_CARD_TRANSFER;
- cardstate = SD_GetState();
- if (cardstate == SD_CARD_TRANSFER)
- {
- return (SD_TRANSFER_OK);
- }
- else if (cardstate == SD_CARD_ERROR)
- {
- return (SD_TRANSFER_ERROR);
- }
- else
- {
- return (SD_TRANSFER_BUSY);
- }
- }
- /**
- * @brief Returns the current card's state.
- * @param None
- * @retval SDCardState: SD Card Error or SD Card Current State.
- */
- SDCardState SD_GetState(void)
- {
- uint32_t resp1 = 0;
- if (SD_Detect() == SD_PRESENT)
- {
- if (SD_SendStatus(&resp1) != SD_OK)
- {
- return SD_CARD_ERROR;
- }
- else
- {
- return (SDCardState)((resp1 >> 9) & 0x0F);
- }
- }
- else
- {
- return SD_CARD_ERROR;
- }
- }
- /**
- * @brief Detect if SD card is correctly plugged in the memory slot.
- * @param None
- * @retval Return if SD is detected or not
- */
- uint8_t SD_Detect(void)
- {
- __IO uint8_t status = SD_PRESENT;
- /*!< Check GPIO to detect SD */
- if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET)
- {
- status = SD_NOT_PRESENT;
- }
- return status;
- }
- /**
- * @brief Enquires cards about their operating voltage and configures
- * clock controls.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_PowerON(void)
- {
- __IO SD_Error errorstatus = SD_OK;
- uint32_t response = 0, count = 0, validvoltage = 0;
- uint32_t SDType = SD_STD_CAPACITY;
- /*!< Power ON Sequence -----------------------------------------------------*/
- /*!< Configure the SDIO peripheral */
- /*!< SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) */
- /*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
- /*!< SDIO_CK for initialization should not exceed 400 KHz */
- SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV;
- SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
- SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
- SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
- SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
- SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
- SDIO_Init(&SDIO_InitStructure);
- /*!< Set Power State to ON */
- SDIO_SetPowerState(SDIO_PowerState_ON);
- /*!< Enable SDIO Clock */
- SDIO_ClockCmd(ENABLE);
- /*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/
- /*!< No CMD response required */
- SDIO_CmdInitStructure.SDIO_Argument = 0x0;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdError();
- if (errorstatus != SD_OK)
- {
- /*!< CMD Response TimeOut (wait for CMDSENT flag) */
- return (errorstatus);
- }
- /*!< CMD8: SEND_IF_COND ----------------------------------------------------*/
- /*!< Send CMD8 to verify SD card interface operating condition */
- /*!< Argument: - [31:12]: Reserved (shall be set to '0')
- - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
- - [7:0]: Check Pattern (recommended 0xAA) */
- /*!< CMD Response: R7 */
- SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp7Error();
- if (errorstatus == SD_OK)
- {
- CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */
- SDType = SD_HIGH_CAPACITY;
- }
- else
- {
- /*!< CMD55 */
- SDIO_CmdInitStructure.SDIO_Argument = 0x00;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- }
- /*!< CMD55 */
- SDIO_CmdInitStructure.SDIO_Argument = 0x00;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- /*!< If errorstatus is Command TimeOut, it is a MMC card */
- /*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
- or SD card 1.x */
- if (errorstatus == SD_OK)
- {
- /*!< SD CARD */
- /*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
- while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
- {
- /*!< SEND CMD55 APP_CMD with RCA as 0 */
- SDIO_CmdInitStructure.SDIO_Argument = 0x00;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp3Error();
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- response = SDIO_GetResponse(SDIO_RESP1);
- validvoltage = (((response >> 31) == 1) ? 1 : 0);
- count++;
- }
- if (count >= SD_MAX_VOLT_TRIAL)
- {
- errorstatus = SD_INVALID_VOLTRANGE;
- return (errorstatus);
- }
- if (response &= SD_HIGH_CAPACITY)
- {
- CardType = SDIO_HIGH_CAPACITY_SD_CARD;
- }
- } /*!< else MMC Card */
- return (errorstatus);
- }
- /**
- * @brief Turns the SDIO output signals off.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_PowerOFF(void)
- {
- SD_Error errorstatus = SD_OK;
- /*!< Set Power State to OFF */
- SDIO_SetPowerState(SDIO_PowerState_OFF);
- return (errorstatus);
- }
- /**
- * @brief Intialises all cards or single card as the case may be Card(s) come
- * into standby state.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_InitializeCards(void)
- {
- SD_Error errorstatus = SD_OK;
- uint16_t rca = 0x01;
- if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
- {
- errorstatus = SD_REQUEST_NOT_APPLICABLE;
- return (errorstatus);
- }
- if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
- {
- /*!< Send CMD2 ALL_SEND_CID */
- SDIO_CmdInitStructure.SDIO_Argument = 0x0;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp2Error();
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
- CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
- CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
- CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
- }
- if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
- {
- /*!< Send CMD3 SET_REL_ADDR with argument 0 */
- /*!< SD Card publishes its RCA. */
- SDIO_CmdInitStructure.SDIO_Argument = 0x00;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca);
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- }
- if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
- {
- RCA = rca;
- /*!< Send CMD9 SEND_CSD with argument as card's RCA */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp2Error();
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
- CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
- CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
- CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
- }
- errorstatus = SD_OK; /*!< All cards get intialized */
- return (errorstatus);
- }
- /**
- * @brief Returns information about specific card.
- * @param cardinfo: pointer to a SD_CardInfo structure that contains all SD card
- * information.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
- {
- SD_Error errorstatus = SD_OK;
- uint8_t tmp = 0;
- cardinfo->CardType = (uint8_t)CardType;
- cardinfo->RCA = (uint16_t)RCA;
- /*!< Byte 0 */
- tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24);
- cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6;
- cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2;
- cardinfo->SD_csd.Reserved1 = tmp & 0x03;
- /*!< Byte 1 */
- tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16);
- cardinfo->SD_csd.TAAC = tmp;
- /*!< Byte 2 */
- tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8);
- cardinfo->SD_csd.NSAC = tmp;
- /*!< Byte 3 */
- tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF);
- cardinfo->SD_csd.MaxBusClkFrec = tmp;
- /*!< Byte 4 */
- tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24);
- cardinfo->SD_csd.CardComdClasses = tmp << 4;
- /*!< Byte 5 */
- tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16);
- cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4;
- cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
- /*!< Byte 6 */
- tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8);
- cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7;
- cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6;
- cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5;
- cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4;
- cardinfo->SD_csd.Reserved2 = 0; /*!< Reserved */
- if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0))
- {
- cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
- /*!< Byte 7 */
- tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
- cardinfo->SD_csd.DeviceSize |= (tmp) << 2;
- /*!< Byte 8 */
- tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
- cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
- cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
- cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
- /*!< Byte 9 */
- tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
- cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
- cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
- cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
- /*!< Byte 10 */
- tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
- cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
- cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1);
- cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
- cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
- cardinfo->CardCapacity *= cardinfo->CardBlockSize;
- }
- else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
- {
- /*!< Byte 7 */
- tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF);
- cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
- /*!< Byte 8 */
- tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24);
- cardinfo->SD_csd.DeviceSize |= (tmp << 8);
- /*!< Byte 9 */
- tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16);
- cardinfo->SD_csd.DeviceSize |= (tmp);
- /*!< Byte 10 */
- tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8);
- cardinfo->CardCapacity = (int64_t)(cardinfo->SD_csd.DeviceSize + 1) * 512 * 1024;
- cardinfo->CardBlockSize = 512;
- }
- cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
- cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
- /*!< Byte 11 */
- tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF);
- cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
- cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
- /*!< Byte 12 */
- tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24);
- cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
- cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
- cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
- cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
- /*!< Byte 13 */
- tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16);
- cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
- cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
- cardinfo->SD_csd.Reserved3 = 0;
- cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
- /*!< Byte 14 */
- tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8);
- cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
- cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
- cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
- cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
- cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
- cardinfo->SD_csd.ECC = (tmp & 0x03);
- /*!< Byte 15 */
- tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF);
- cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
- cardinfo->SD_csd.Reserved4 = 1;
- /*!< Byte 0 */
- tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24);
- cardinfo->SD_cid.ManufacturerID = tmp;
- /*!< Byte 1 */
- tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16);
- cardinfo->SD_cid.OEM_AppliID = tmp << 8;
- /*!< Byte 2 */
- tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8);
- cardinfo->SD_cid.OEM_AppliID |= tmp;
- /*!< Byte 3 */
- tmp = (uint8_t)(CID_Tab[0] & 0x000000FF);
- cardinfo->SD_cid.ProdName1 = tmp << 24;
- /*!< Byte 4 */
- tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24);
- cardinfo->SD_cid.ProdName1 |= tmp << 16;
- /*!< Byte 5 */
- tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16);
- cardinfo->SD_cid.ProdName1 |= tmp << 8;
- /*!< Byte 6 */
- tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8);
- cardinfo->SD_cid.ProdName1 |= tmp;
- /*!< Byte 7 */
- tmp = (uint8_t)(CID_Tab[1] & 0x000000FF);
- cardinfo->SD_cid.ProdName2 = tmp;
- /*!< Byte 8 */
- tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24);
- cardinfo->SD_cid.ProdRev = tmp;
- /*!< Byte 9 */
- tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16);
- cardinfo->SD_cid.ProdSN = tmp << 24;
- /*!< Byte 10 */
- tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8);
- cardinfo->SD_cid.ProdSN |= tmp << 16;
- /*!< Byte 11 */
- tmp = (uint8_t)(CID_Tab[2] & 0x000000FF);
- cardinfo->SD_cid.ProdSN |= tmp << 8;
- /*!< Byte 12 */
- tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24);
- cardinfo->SD_cid.ProdSN |= tmp;
- /*!< Byte 13 */
- tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16);
- cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
- cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
- /*!< Byte 14 */
- tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8);
- cardinfo->SD_cid.ManufactDate |= tmp;
- /*!< Byte 15 */
- tmp = (uint8_t)(CID_Tab[3] & 0x000000FF);
- cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
- cardinfo->SD_cid.Reserved2 = 1;
- return (errorstatus);
- }
- /**
- * @brief Enables wide bus opeartion for the requeseted card if supported by
- * card.
- * @param WideMode: Specifies the SD card wide bus mode.
- * This parameter can be one of the following values:
- * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
- * @arg SDIO_BusWide_4b: 4-bit data transfer
- * @arg SDIO_BusWide_1b: 1-bit data transfer
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus)
- {
- SD_Error errorstatus = SD_OK;
- uint8_t tmp = 0;
- errorstatus = SD_SendSDStatus((uint32_t *)SDSTATUS_Tab);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< Byte 0 */
- tmp = (uint8_t)((SDSTATUS_Tab[0] & 0xC0) >> 6);
- cardstatus->DAT_BUS_WIDTH = tmp;
- /*!< Byte 0 */
- tmp = (uint8_t)((SDSTATUS_Tab[0] & 0x20) >> 5);
- cardstatus->SECURED_MODE = tmp;
- /*!< Byte 2 */
- tmp = (uint8_t)((SDSTATUS_Tab[2] & 0xFF));
- cardstatus->SD_CARD_TYPE = tmp << 8;
- /*!< Byte 3 */
- tmp = (uint8_t)((SDSTATUS_Tab[3] & 0xFF));
- cardstatus->SD_CARD_TYPE |= tmp;
- /*!< Byte 4 */
- tmp = (uint8_t)(SDSTATUS_Tab[4] & 0xFF);
- cardstatus->SIZE_OF_PROTECTED_AREA = tmp << 24;
- /*!< Byte 5 */
- tmp = (uint8_t)(SDSTATUS_Tab[5] & 0xFF);
- cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 16;
- /*!< Byte 6 */
- tmp = (uint8_t)(SDSTATUS_Tab[6] & 0xFF);
- cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 8;
- /*!< Byte 7 */
- tmp = (uint8_t)(SDSTATUS_Tab[7] & 0xFF);
- cardstatus->SIZE_OF_PROTECTED_AREA |= tmp;
- /*!< Byte 8 */
- tmp = (uint8_t)((SDSTATUS_Tab[8] & 0xFF));
- cardstatus->SPEED_CLASS = tmp;
- /*!< Byte 9 */
- tmp = (uint8_t)((SDSTATUS_Tab[9] & 0xFF));
- cardstatus->PERFORMANCE_MOVE = tmp;
- /*!< Byte 10 */
- tmp = (uint8_t)((SDSTATUS_Tab[10] & 0xF0) >> 4);
- cardstatus->AU_SIZE = tmp;
- /*!< Byte 11 */
- tmp = (uint8_t)(SDSTATUS_Tab[11] & 0xFF);
- cardstatus->ERASE_SIZE = tmp << 8;
- /*!< Byte 12 */
- tmp = (uint8_t)(SDSTATUS_Tab[12] & 0xFF);
- cardstatus->ERASE_SIZE |= tmp;
- /*!< Byte 13 */
- tmp = (uint8_t)((SDSTATUS_Tab[13] & 0xFC) >> 2);
- cardstatus->ERASE_TIMEOUT = tmp;
- /*!< Byte 13 */
- tmp = (uint8_t)((SDSTATUS_Tab[13] & 0x3));
- cardstatus->ERASE_OFFSET = tmp;
- return (errorstatus);
- }
- /**
- * @brief Enables wide bus opeartion for the requeseted card if supported by
- * card.
- * @param WideMode: Specifies the SD card wide bus mode.
- * This parameter can be one of the following values:
- * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
- * @arg SDIO_BusWide_4b: 4-bit data transfer
- * @arg SDIO_BusWide_1b: 1-bit data transfer
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
- {
- SD_Error errorstatus = SD_OK;
- /*!< MMC Card doesn't support this feature */
- if (SDIO_MULTIMEDIA_CARD == CardType)
- {
- errorstatus = SD_UNSUPPORTED_FEATURE;
- return (errorstatus);
- }
- else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
- {
- if (SDIO_BusWide_8b == WideMode)
- {
- errorstatus = SD_UNSUPPORTED_FEATURE;
- return (errorstatus);
- }
- else if (SDIO_BusWide_4b == WideMode)
- {
- errorstatus = SDEnWideBus(ENABLE);
- if (SD_OK == errorstatus)
- {
- /*!< Configure the SDIO peripheral */
- SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
- SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
- SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
- SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
- SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b;
- SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
- SDIO_Init(&SDIO_InitStructure);
- }
- }
- else
- {
- errorstatus = SDEnWideBus(DISABLE);
- if (SD_OK == errorstatus)
- {
- /*!< Configure the SDIO peripheral */
- SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV;
- SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising;
- SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable;
- SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
- SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b;
- SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
- SDIO_Init(&SDIO_InitStructure);
- }
- }
- }
- return (errorstatus);
- }
- /**
- * @brief Selects od Deselects the corresponding card.
- * @param addr: Address of the Card to be selected.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_SelectDeselect(uint32_t addr)
- {
- SD_Error errorstatus = SD_OK;
- /*!< Send CMD7 SDIO_SEL_DESEL_CARD */
- SDIO_CmdInitStructure.SDIO_Argument = addr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD);
- return (errorstatus);
- }
- /**
- * @brief Allows to read one block from a specified address in a card. The Data
- * transfer can be managed by DMA mode or Polling mode.
- * @note This operation should be followed by two functions to check if the
- * DMA Controller and SD Card status.
- * - SD_ReadWaitOperation(): this function insure that the DMA
- * controller has finished all data transfer.
- * - SD_GetStatus(): to check that the SD Card has finished the
- * data transfer and it is ready for data.
- * @param readbuff: pointer to the buffer that will contain the received data
- * @param ReadAddr: Address from where data are to be read.
- * @param BlockSize: the SD card Data block size. The Block size should be 512.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
- {
- SD_Error errorstatus = SD_OK;
- #if defined(SD_POLLING_MODE)
- uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
- #endif
- TransferError = SD_OK;
- TransferEnd = 0;
- StopCondition = 0;
- SDIO->DCTRL = 0x0;
- if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
- {
- BlockSize = 512;
- ReadAddr /= 512;
- }
- /* Set Block Size for Card */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
- SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
- SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
- SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
- SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
- SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
- SDIO_DataConfig(&SDIO_DataInitStructure);
- /*!< Send CMD17 READ_SINGLE_BLOCK */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- #if defined(SD_POLLING_MODE)
- /*!< In case of single block transfer, no need of stop transfer at all.*/
- /*!< Polling mode */
- while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
- {
- if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
- {
- for (count = 0; count < 8; count++)
- {
- *(tempbuff + count) = SDIO_ReadData();
- }
- tempbuff += 8;
- }
- }
- if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
- errorstatus = SD_DATA_TIMEOUT;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
- errorstatus = SD_DATA_CRC_FAIL;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
- errorstatus = SD_RX_OVERRUN;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_STBITERR);
- errorstatus = SD_START_BIT_ERR;
- return (errorstatus);
- }
- count = SD_DATATIMEOUT;
- while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
- {
- *tempbuff = SDIO_ReadData();
- tempbuff++;
- count--;
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- #elif defined(SD_DMA_MODE)
- SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
- SDIO_DMACmd(ENABLE);
- SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize);
- #endif
- return (errorstatus);
- }
- /**
- * @brief Allows to read blocks from a specified address in a card. The Data
- * transfer can be managed by DMA mode or Polling mode.
- * @note This operation should be followed by two functions to check if the
- * DMA Controller and SD Card status.
- * - SD_ReadWaitOperation(): this function insure that the DMA
- * controller has finished all data transfer.
- * - SD_GetStatus(): to check that the SD Card has finished the
- * data transfer and it is ready for data.
- * @param readbuff: pointer to the buffer that will contain the received data.
- * @param ReadAddr: Address from where data are to be read.
- * @param BlockSize: the SD card Data block size. The Block size should be 512.
- * @param NumberOfBlocks: number of blocks to be read.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
- {
- SD_Error errorstatus = SD_OK;
- TransferError = SD_OK;
- TransferEnd = 0;
- StopCondition = 1;
- SDIO->DCTRL = 0x0;
- if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
- {
- BlockSize = 512;
- ReadAddr /= 512;
- }
- /*!< Set Block Size for Card */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
- SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
- SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
- SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
- SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
- SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
- SDIO_DataConfig(&SDIO_DataInitStructure);
- /*!< Send CMD18 READ_MULT_BLOCK with argument data address */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
- SDIO_DMACmd(ENABLE);
- SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize));
- return (errorstatus);
- }
- /**
- * @brief This function waits until the SDIO DMA data transfer is finished.
- * This function should be called after SDIO_ReadMultiBlocks() function
- * to insure that all data sent by the card are already transferred by
- * the DMA controller.
- * @param None.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_WaitReadOperation(void)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t timeout;
- timeout = SD_DATATIMEOUT;
- while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
- {
- timeout--;
- }
- DMAEndOfTransfer = 0x00;
- timeout = SD_DATATIMEOUT;
- while (((SDIO->STA & SDIO_FLAG_RXACT)) && (timeout > 0))
- {
- timeout--;
- }
- if (StopCondition == 1)
- {
- errorstatus = SD_StopTransfer();
- StopCondition = 0;
- }
- if ((timeout == 0) && (errorstatus == SD_OK))
- {
- errorstatus = SD_DATA_TIMEOUT;
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- if (TransferError != SD_OK)
- {
- return (TransferError);
- }
- else
- {
- return (errorstatus);
- }
- }
- /**
- * @brief Allows to write one block starting from a specified address in a card.
- * The Data transfer can be managed by DMA mode or Polling mode.
- * @note This operation should be followed by two functions to check if the
- * DMA Controller and SD Card status.
- * - SD_ReadWaitOperation(): this function insure that the DMA
- * controller has finished all data transfer.
- * - SD_GetStatus(): to check that the SD Card has finished the
- * data transfer and it is ready for data.
- * @param writebuff: pointer to the buffer that contain the data to be transferred.
- * @param WriteAddr: Address from where data are to be read.
- * @param BlockSize: the SD card Data block size. The Block size should be 512.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
- {
- SD_Error errorstatus = SD_OK;
- #if defined(SD_POLLING_MODE)
- uint32_t bytestransferred = 0, count = 0, restwords = 0;
- uint32_t *tempbuff = (uint32_t *)writebuff;
- #endif
- TransferError = SD_OK;
- TransferEnd = 0;
- StopCondition = 0;
- SDIO->DCTRL = 0x0;
- if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
- {
- BlockSize = 512;
- WriteAddr /= 512;
- }
- /* Set Block Size for Card */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- /*!< Send CMD24 WRITE_SINGLE_BLOCK */
- SDIO_CmdInitStructure.SDIO_Argument = WriteAddr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
- SDIO_DataInitStructure.SDIO_DataLength = BlockSize;
- SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
- SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
- SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
- SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
- SDIO_DataConfig(&SDIO_DataInitStructure);
- /*!< In case of single data block transfer no need of stop command at all */
- #if defined(SD_POLLING_MODE)
- while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
- {
- if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
- {
- if ((512 - bytestransferred) < 32)
- {
- restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : ((512 - bytestransferred) / 4 + 1);
- for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4)
- {
- SDIO_WriteData(*tempbuff);
- }
- }
- else
- {
- for (count = 0; count < 8; count++)
- {
- SDIO_WriteData(*(tempbuff + count));
- }
- tempbuff += 8;
- bytestransferred += 32;
- }
- }
- }
- if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
- errorstatus = SD_DATA_TIMEOUT;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
- errorstatus = SD_DATA_CRC_FAIL;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_TXUNDERR);
- errorstatus = SD_TX_UNDERRUN;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_STBITERR);
- errorstatus = SD_START_BIT_ERR;
- return (errorstatus);
- }
- #elif defined(SD_DMA_MODE)
- SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
- SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize);
- SDIO_DMACmd(ENABLE);
- #endif
- return (errorstatus);
- }
- /**
- * @brief Allows to write blocks starting from a specified address in a card.
- * The Data transfer can be managed by DMA mode only.
- * @note This operation should be followed by two functions to check if the
- * DMA Controller and SD Card status.
- * - SD_ReadWaitOperation(): this function insure that the DMA
- * controller has finished all data transfer.
- * - SD_GetStatus(): to check that the SD Card has finished the
- * data transfer and it is ready for data.
- * @param WriteAddr: Address from where data are to be read.
- * @param writebuff: pointer to the buffer that contain the data to be transferred.
- * @param BlockSize: the SD card Data block size. The Block size should be 512.
- * @param NumberOfBlocks: number of blocks to be written.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
- {
- SD_Error errorstatus = SD_OK;
- TransferError = SD_OK;
- TransferEnd = 0;
- StopCondition = 1;
- SDIO->DCTRL = 0x0;
- if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
- {
- BlockSize = 512;
- WriteAddr /= 512;
- }
- /* Set Block Size for Card */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)BlockSize;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- /*!< To improve performance */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(RCA << 16);
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< To improve performance */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK);
- if (SD_OK != errorstatus)
- {
- return (errorstatus);
- }
- SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
- SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize;
- SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t)9 << 4;
- SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard;
- SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
- SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
- SDIO_DataConfig(&SDIO_DataInitStructure);
- SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE);
- SDIO_DMACmd(ENABLE);
- SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize));
- return (errorstatus);
- }
- /**
- * @brief This function waits until the SDIO DMA data transfer is finished.
- * This function should be called after SDIO_WriteBlock() and
- * SDIO_WriteMultiBlocks() function to insure that all data sent by the
- * card are already transferred by the DMA controller.
- * @param None.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_WaitWriteOperation(void)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t timeout;
- timeout = SD_DATATIMEOUT;
- while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0))
- {
- timeout--;
- }
- DMAEndOfTransfer = 0x00;
- timeout = SD_DATATIMEOUT;
- while (((SDIO->STA & SDIO_FLAG_TXACT)) && (timeout > 0))
- {
- timeout--;
- }
- if (StopCondition == 1)
- {
- errorstatus = SD_StopTransfer();
- StopCondition = 0;
- }
- if ((timeout == 0) && (errorstatus == SD_OK))
- {
- errorstatus = SD_DATA_TIMEOUT;
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- if (TransferError != SD_OK)
- {
- return (TransferError);
- }
- else
- {
- return (errorstatus);
- }
- }
- /**
- * @brief Gets the cuurent data transfer state.
- * @param None
- * @retval SDTransferState: Data Transfer state.
- * This value can be:
- * - SD_TRANSFER_OK: No data transfer is acting
- * - SD_TRANSFER_BUSY: Data transfer is acting
- */
- SDTransferState SD_GetTransferState(void)
- {
- if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
- {
- return (SD_TRANSFER_BUSY);
- }
- else
- {
- return (SD_TRANSFER_OK);
- }
- }
- /**
- * @brief Aborts an ongoing data transfer.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_StopTransfer(void)
- {
- SD_Error errorstatus = SD_OK;
- /*!< Send CMD12 STOP_TRANSMISSION */
- SDIO_CmdInitStructure.SDIO_Argument = 0x0;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION);
- return (errorstatus);
- }
- /**
- * @brief Allows to erase memory area specified for the given card.
- * @param startaddr: the start address.
- * @param endaddr: the end address.
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t delay = 0;
- __IO uint32_t maxdelay = 0;
- uint8_t cardstate = 0;
- /*!< Check if the card coomnd class supports erase command */
- if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0)
- {
- errorstatus = SD_REQUEST_NOT_APPLICABLE;
- return (errorstatus);
- }
- maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2);
- if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
- {
- errorstatus = SD_LOCK_UNLOCK_FAILED;
- return (errorstatus);
- }
- if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
- {
- startaddr /= 512;
- endaddr /= 512;
- }
- /*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
- if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
- {
- /*!< Send CMD32 SD_ERASE_GRP_START with argument as addr */
- SDIO_CmdInitStructure.SDIO_Argument = startaddr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_START);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< Send CMD33 SD_ERASE_GRP_END with argument as addr */
- SDIO_CmdInitStructure.SDIO_Argument = endaddr;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- }
- /*!< Send CMD38 ERASE */
- SDIO_CmdInitStructure.SDIO_Argument = 0;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_ERASE);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- for (delay = 0; delay < maxdelay; delay++)
- {
- }
- /*!< Wait till the card is in programming state */
- errorstatus = IsCardProgramming(&cardstate);
- delay = SD_DATATIMEOUT;
- while ((delay > 0) && (errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate)))
- {
- errorstatus = IsCardProgramming(&cardstate);
- delay--;
- }
- return (errorstatus);
- }
- /**
- * @brief Returns the current card's status.
- * @param pcardstatus: pointer to the buffer that will contain the SD card
- * status (Card Status register).
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_SendStatus(uint32_t *pcardstatus)
- {
- SD_Error errorstatus = SD_OK;
- if (pcardstatus == NULL)
- {
- errorstatus = SD_INVALID_PARAMETER;
- return (errorstatus);
- }
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- *pcardstatus = SDIO_GetResponse(SDIO_RESP1);
- return (errorstatus);
- }
- /**
- * @brief Returns the current SD card's status.
- * @param psdstatus: pointer to the buffer that will contain the SD card status
- * (SD Status register).
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_SendSDStatus(uint32_t *psdstatus)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t count = 0;
- if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
- {
- errorstatus = SD_LOCK_UNLOCK_FAILED;
- return (errorstatus);
- }
- /*!< Set block size for card if it is not equal to current block size for card. */
- SDIO_CmdInitStructure.SDIO_Argument = 64;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< CMD55 */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
- SDIO_DataInitStructure.SDIO_DataLength = 64;
- SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b;
- SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
- SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
- SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
- SDIO_DataConfig(&SDIO_DataInitStructure);
- /*!< Send ACMD13 SD_APP_STAUS with argument as card's RCA.*/
- SDIO_CmdInitStructure.SDIO_Argument = 0;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
- {
- if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
- {
- for (count = 0; count < 8; count++)
- {
- *(psdstatus + count) = SDIO_ReadData();
- }
- psdstatus += 8;
- }
- }
- if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
- errorstatus = SD_DATA_TIMEOUT;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
- errorstatus = SD_DATA_CRC_FAIL;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
- errorstatus = SD_RX_OVERRUN;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_STBITERR);
- errorstatus = SD_START_BIT_ERR;
- return (errorstatus);
- }
- count = SD_DATATIMEOUT;
- while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0))
- {
- *psdstatus = SDIO_ReadData();
- psdstatus++;
- count--;
- }
- /*!< Clear all the static status flags*/
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- return (errorstatus);
- }
- /**
- * @brief Allows to process all the interrupts that are high.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- SD_Error SD_ProcessIRQSrc(void)
- {
- if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET)
- {
- TransferError = SD_OK;
- SDIO_ClearITPendingBit(SDIO_IT_DATAEND);
- TransferEnd = 1;
- }
- else if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET)
- {
- SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL);
- TransferError = SD_DATA_CRC_FAIL;
- }
- else if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET)
- {
- SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT);
- TransferError = SD_DATA_TIMEOUT;
- }
- else if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET)
- {
- SDIO_ClearITPendingBit(SDIO_IT_RXOVERR);
- TransferError = SD_RX_OVERRUN;
- }
- else if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET)
- {
- SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR);
- TransferError = SD_TX_UNDERRUN;
- }
- else if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET)
- {
- SDIO_ClearITPendingBit(SDIO_IT_STBITERR);
- TransferError = SD_START_BIT_ERR;
- }
- SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |
- SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |
- SDIO_IT_RXOVERR | SDIO_IT_STBITERR,
- DISABLE);
- return (TransferError);
- }
- /**
- * @brief This function waits until the SDIO DMA data transfer is finished.
- * @param None.
- * @retval None.
- */
- void SD_ProcessDMAIRQ(void)
- {
- if (DMA2->LISR & SD_SDIO_DMA_FLAG_TCIF)
- {
- DMAEndOfTransfer = 0x01;
- DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_TCIF | SD_SDIO_DMA_FLAG_FEIF);
- }
- }
- /**
- * @brief Checks for error conditions for CMD0.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error CmdError(void)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t timeout;
- timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
- while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET))
- {
- timeout--;
- }
- if (timeout == 0)
- {
- errorstatus = SD_CMD_RSP_TIMEOUT;
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- return (errorstatus);
- }
- /**
- * @brief Checks for error conditions for R7 response.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error CmdResp7Error(void)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t status;
- uint32_t timeout = SDIO_CMD0TIMEOUT;
- status = SDIO->STA;
- while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0))
- {
- timeout--;
- status = SDIO->STA;
- }
- if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT))
- {
- /*!< Card is not V2.0 complient or card does not support the set voltage range */
- errorstatus = SD_CMD_RSP_TIMEOUT;
- SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
- return (errorstatus);
- }
- if (status & SDIO_FLAG_CMDREND)
- {
- /*!< Card is SD V2.0 compliant */
- errorstatus = SD_OK;
- SDIO_ClearFlag(SDIO_FLAG_CMDREND);
- return (errorstatus);
- }
- return (errorstatus);
- }
- /**
- * @brief Checks for error conditions for R1 response.
- * @param cmd: The sent command index.
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error CmdResp1Error(uint8_t cmd)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t status;
- uint32_t response_r1;
- status = SDIO->STA;
- while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
- {
- status = SDIO->STA;
- }
- if (status & SDIO_FLAG_CTIMEOUT)
- {
- errorstatus = SD_CMD_RSP_TIMEOUT;
- SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
- return (errorstatus);
- }
- else if (status & SDIO_FLAG_CCRCFAIL)
- {
- errorstatus = SD_CMD_CRC_FAIL;
- SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
- return (errorstatus);
- }
- /*!< Check response received is of desired command */
- if (SDIO_GetCommandResponse() != cmd)
- {
- errorstatus = SD_ILLEGAL_CMD;
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- /*!< We have received response, retrieve it for analysis */
- response_r1 = SDIO_GetResponse(SDIO_RESP1);
- if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
- {
- return (errorstatus);
- }
- if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE)
- {
- return (SD_ADDR_OUT_OF_RANGE);
- }
- if (response_r1 & SD_OCR_ADDR_MISALIGNED)
- {
- return (SD_ADDR_MISALIGNED);
- }
- if (response_r1 & SD_OCR_BLOCK_LEN_ERR)
- {
- return (SD_BLOCK_LEN_ERR);
- }
- if (response_r1 & SD_OCR_ERASE_SEQ_ERR)
- {
- return (SD_ERASE_SEQ_ERR);
- }
- if (response_r1 & SD_OCR_BAD_ERASE_PARAM)
- {
- return (SD_BAD_ERASE_PARAM);
- }
- if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION)
- {
- return (SD_WRITE_PROT_VIOLATION);
- }
- if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED)
- {
- return (SD_LOCK_UNLOCK_FAILED);
- }
- if (response_r1 & SD_OCR_COM_CRC_FAILED)
- {
- return (SD_COM_CRC_FAILED);
- }
- if (response_r1 & SD_OCR_ILLEGAL_CMD)
- {
- return (SD_ILLEGAL_CMD);
- }
- if (response_r1 & SD_OCR_CARD_ECC_FAILED)
- {
- return (SD_CARD_ECC_FAILED);
- }
- if (response_r1 & SD_OCR_CC_ERROR)
- {
- return (SD_CC_ERROR);
- }
- if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
- {
- return (SD_GENERAL_UNKNOWN_ERROR);
- }
- if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN)
- {
- return (SD_STREAM_READ_UNDERRUN);
- }
- if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN)
- {
- return (SD_STREAM_WRITE_OVERRUN);
- }
- if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE)
- {
- return (SD_CID_CSD_OVERWRITE);
- }
- if (response_r1 & SD_OCR_WP_ERASE_SKIP)
- {
- return (SD_WP_ERASE_SKIP);
- }
- if (response_r1 & SD_OCR_CARD_ECC_DISABLED)
- {
- return (SD_CARD_ECC_DISABLED);
- }
- if (response_r1 & SD_OCR_ERASE_RESET)
- {
- return (SD_ERASE_RESET);
- }
- if (response_r1 & SD_OCR_AKE_SEQ_ERROR)
- {
- return (SD_AKE_SEQ_ERROR);
- }
- return (errorstatus);
- }
- /**
- * @brief Checks for error conditions for R3 (OCR) response.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error CmdResp3Error(void)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t status;
- status = SDIO->STA;
- while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
- {
- status = SDIO->STA;
- }
- if (status & SDIO_FLAG_CTIMEOUT)
- {
- errorstatus = SD_CMD_RSP_TIMEOUT;
- SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- return (errorstatus);
- }
- /**
- * @brief Checks for error conditions for R2 (CID or CSD) response.
- * @param None
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error CmdResp2Error(void)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t status;
- status = SDIO->STA;
- while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
- {
- status = SDIO->STA;
- }
- if (status & SDIO_FLAG_CTIMEOUT)
- {
- errorstatus = SD_CMD_RSP_TIMEOUT;
- SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
- return (errorstatus);
- }
- else if (status & SDIO_FLAG_CCRCFAIL)
- {
- errorstatus = SD_CMD_CRC_FAIL;
- SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- return (errorstatus);
- }
- /**
- * @brief Checks for error conditions for R6 (RCA) response.
- * @param cmd: The sent command index.
- * @param prca: pointer to the variable that will contain the SD card relative
- * address RCA.
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t status;
- uint32_t response_r1;
- status = SDIO->STA;
- while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
- {
- status = SDIO->STA;
- }
- if (status & SDIO_FLAG_CTIMEOUT)
- {
- errorstatus = SD_CMD_RSP_TIMEOUT;
- SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
- return (errorstatus);
- }
- else if (status & SDIO_FLAG_CCRCFAIL)
- {
- errorstatus = SD_CMD_CRC_FAIL;
- SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
- return (errorstatus);
- }
- /*!< Check response received is of desired command */
- if (SDIO_GetCommandResponse() != cmd)
- {
- errorstatus = SD_ILLEGAL_CMD;
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- /*!< We have received response, retrieve it. */
- response_r1 = SDIO_GetResponse(SDIO_RESP1);
- if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)))
- {
- *prca = (uint16_t)(response_r1 >> 16);
- return (errorstatus);
- }
- if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
- {
- return (SD_GENERAL_UNKNOWN_ERROR);
- }
- if (response_r1 & SD_R6_ILLEGAL_CMD)
- {
- return (SD_ILLEGAL_CMD);
- }
- if (response_r1 & SD_R6_COM_CRC_FAILED)
- {
- return (SD_COM_CRC_FAILED);
- }
- return (errorstatus);
- }
- /**
- * @brief Enables or disables the SDIO wide bus mode.
- * @param NewState: new state of the SDIO wide bus mode.
- * This parameter can be: ENABLE or DISABLE.
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error SDEnWideBus(FunctionalState NewState)
- {
- SD_Error errorstatus = SD_OK;
- uint32_t scr[2] = {0, 0};
- if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
- {
- errorstatus = SD_LOCK_UNLOCK_FAILED;
- return (errorstatus);
- }
- /*!< Get SCR Register */
- errorstatus = FindSCR(RCA, scr);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< If wide bus operation to be enabled */
- if (NewState == ENABLE)
- {
- /*!< If requested card supports wide bus operation */
- if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
- {
- /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
- SDIO_CmdInitStructure.SDIO_Argument = 0x2;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- return (errorstatus);
- }
- else
- {
- errorstatus = SD_REQUEST_NOT_APPLICABLE;
- return (errorstatus);
- }
- } /*!< If wide bus operation to be disabled */
- else
- {
- /*!< If requested card supports 1 bit mode operation */
- if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
- {
- /*!< Send CMD55 APP_CMD with argument as card's RCA.*/
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
- SDIO_CmdInitStructure.SDIO_Argument = 0x00;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- return (errorstatus);
- }
- else
- {
- errorstatus = SD_REQUEST_NOT_APPLICABLE;
- return (errorstatus);
- }
- }
- }
- /**
- * @brief Checks if the SD card is in programming state.
- * @param pstatus: pointer to the variable that will contain the SD card state.
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error IsCardProgramming(uint8_t *pstatus)
- {
- SD_Error errorstatus = SD_OK;
- __IO uint32_t respR1 = 0, status = 0;
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- status = SDIO->STA;
- while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
- {
- status = SDIO->STA;
- }
- if (status & SDIO_FLAG_CTIMEOUT)
- {
- errorstatus = SD_CMD_RSP_TIMEOUT;
- SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT);
- return (errorstatus);
- }
- else if (status & SDIO_FLAG_CCRCFAIL)
- {
- errorstatus = SD_CMD_CRC_FAIL;
- SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL);
- return (errorstatus);
- }
- status = (uint32_t)SDIO_GetCommandResponse();
- /*!< Check response received is of desired command */
- if (status != SD_CMD_SEND_STATUS)
- {
- errorstatus = SD_ILLEGAL_CMD;
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- /*!< We have received response, retrieve it for analysis */
- respR1 = SDIO_GetResponse(SDIO_RESP1);
- /*!< Find out card status */
- *pstatus = (uint8_t)((respR1 >> 9) & 0x0000000F);
- if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
- {
- return (errorstatus);
- }
- if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE)
- {
- return (SD_ADDR_OUT_OF_RANGE);
- }
- if (respR1 & SD_OCR_ADDR_MISALIGNED)
- {
- return (SD_ADDR_MISALIGNED);
- }
- if (respR1 & SD_OCR_BLOCK_LEN_ERR)
- {
- return (SD_BLOCK_LEN_ERR);
- }
- if (respR1 & SD_OCR_ERASE_SEQ_ERR)
- {
- return (SD_ERASE_SEQ_ERR);
- }
- if (respR1 & SD_OCR_BAD_ERASE_PARAM)
- {
- return (SD_BAD_ERASE_PARAM);
- }
- if (respR1 & SD_OCR_WRITE_PROT_VIOLATION)
- {
- return (SD_WRITE_PROT_VIOLATION);
- }
- if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED)
- {
- return (SD_LOCK_UNLOCK_FAILED);
- }
- if (respR1 & SD_OCR_COM_CRC_FAILED)
- {
- return (SD_COM_CRC_FAILED);
- }
- if (respR1 & SD_OCR_ILLEGAL_CMD)
- {
- return (SD_ILLEGAL_CMD);
- }
- if (respR1 & SD_OCR_CARD_ECC_FAILED)
- {
- return (SD_CARD_ECC_FAILED);
- }
- if (respR1 & SD_OCR_CC_ERROR)
- {
- return (SD_CC_ERROR);
- }
- if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
- {
- return (SD_GENERAL_UNKNOWN_ERROR);
- }
- if (respR1 & SD_OCR_STREAM_READ_UNDERRUN)
- {
- return (SD_STREAM_READ_UNDERRUN);
- }
- if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN)
- {
- return (SD_STREAM_WRITE_OVERRUN);
- }
- if (respR1 & SD_OCR_CID_CSD_OVERWRIETE)
- {
- return (SD_CID_CSD_OVERWRITE);
- }
- if (respR1 & SD_OCR_WP_ERASE_SKIP)
- {
- return (SD_WP_ERASE_SKIP);
- }
- if (respR1 & SD_OCR_CARD_ECC_DISABLED)
- {
- return (SD_CARD_ECC_DISABLED);
- }
- if (respR1 & SD_OCR_ERASE_RESET)
- {
- return (SD_ERASE_RESET);
- }
- if (respR1 & SD_OCR_AKE_SEQ_ERROR)
- {
- return (SD_AKE_SEQ_ERROR);
- }
- return (errorstatus);
- }
- /**
- * @brief Find the SD card SCR register value.
- * @param rca: selected card address.
- * @param pscr: pointer to the buffer that will contain the SCR value.
- * @retval SD_Error: SD Card Error code.
- */
- static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
- {
- uint32_t index = 0;
- SD_Error errorstatus = SD_OK;
- uint32_t tempscr[2] = {0, 0};
- /*!< Set Block Size To 8 Bytes */
- /*!< Send CMD55 APP_CMD with argument as card's RCA */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- /*!< Send CMD55 APP_CMD with argument as card's RCA */
- SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)RCA << 16;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_APP_CMD);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;
- SDIO_DataInitStructure.SDIO_DataLength = 8;
- SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b;
- SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;
- SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;
- SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;
- SDIO_DataConfig(&SDIO_DataInitStructure);
- /*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
- SDIO_CmdInitStructure.SDIO_Argument = 0x0;
- SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR;
- SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
- SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
- SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
- SDIO_SendCommand(&SDIO_CmdInitStructure);
- errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR);
- if (errorstatus != SD_OK)
- {
- return (errorstatus);
- }
- while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
- {
- if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
- {
- *(tempscr + index) = SDIO_ReadData();
- index++;
- }
- }
- if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);
- errorstatus = SD_DATA_TIMEOUT;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);
- errorstatus = SD_DATA_CRC_FAIL;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_RXOVERR);
- errorstatus = SD_RX_OVERRUN;
- return (errorstatus);
- }
- else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)
- {
- SDIO_ClearFlag(SDIO_FLAG_STBITERR);
- errorstatus = SD_START_BIT_ERR;
- return (errorstatus);
- }
- /*!< Clear all the static flags */
- SDIO_ClearFlag(SDIO_STATIC_FLAGS);
- *(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
- *(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
- return (errorstatus);
- }
- /**
- * @brief Converts the number of bytes in power of two and returns the power.
- * @param NumberOfBytes: number of bytes.
- * @retval None
- */
- uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes)
- {
- uint8_t count = 0;
- while (NumberOfBytes != 1)
- {
- NumberOfBytes >>= 1;
- count++;
- }
- return (count);
- }
- /*************************************************************************************/
- /**
- * @brief DeInitializes the SDIO interface.
- * @param None
- * @retval None
- */
- void SD_LowLevel_DeInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*!< Disable SDIO Clock */
- SDIO_ClockCmd(DISABLE);
- /*!< Set Power State to OFF */
- SDIO_SetPowerState(SDIO_PowerState_OFF);
- /*!< DeInitializes the SDIO peripheral */
- SDIO_DeInit();
- /* Disable the SDIO APB2 Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, DISABLE);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_MCO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_MCO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_MCO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_MCO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_MCO);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_MCO);
- /* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* Configure PD.02 CMD line */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Configure PC.12 pin: CLK pin */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /**
- * @brief Initializes the SD Card and put it into StandBy State (Ready for
- * data transfer).
- * @param None
- * @retval None
- */
- void SD_LowLevel_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* GPIOC and GPIOD Periph clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | SD_DETECT_GPIO_CLK, ENABLE);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_SDIO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_SDIO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SDIO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SDIO);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SDIO);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_SDIO);
- /* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* Configure PD.02 CMD line */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Configure PC.12 pin: CLK pin */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */
- GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
- /* Enable the SDIO APB2 Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, ENABLE);
- /* Enable the DMA2 Clock */
- RCC_AHB1PeriphClockCmd(SD_SDIO_DMA_CLK, ENABLE);
- }
- /**
- * @brief Configures the DMA2 Channel4 for SDIO Tx request.
- * @param BufferSRC: pointer to the source buffer
- * @param BufferSize: buffer size
- * @retval None
- */
- void SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize)
- {
- DMA_InitTypeDef SDDMA_InitStructure;
- DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_FEIF | SD_SDIO_DMA_FLAG_DMEIF | SD_SDIO_DMA_FLAG_TEIF | SD_SDIO_DMA_FLAG_HTIF | SD_SDIO_DMA_FLAG_TCIF);
- /* DMA2 Stream3 or Stream6 disable */
- DMA_Cmd(SD_SDIO_DMA_STREAM, DISABLE);
- /* DMA2 Stream3 or Stream6 Config */
- DMA_DeInit(SD_SDIO_DMA_STREAM);
- SDDMA_InitStructure.DMA_Channel = SD_SDIO_DMA_CHANNEL;
- SDDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS;
- SDDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BufferSRC;
- SDDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
- SDDMA_InitStructure.DMA_BufferSize = 0;
- SDDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- SDDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- SDDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
- SDDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
- SDDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- SDDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
- SDDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
- SDDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
- SDDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;
- SDDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;
- DMA_Init(SD_SDIO_DMA_STREAM, &SDDMA_InitStructure);
- DMA_ITConfig(SD_SDIO_DMA_STREAM, DMA_IT_TC, ENABLE);
- DMA_FlowControllerConfig(SD_SDIO_DMA_STREAM, DMA_FlowCtrl_Peripheral);
- /* DMA2 Stream3 or Stream6 enable */
- DMA_Cmd(SD_SDIO_DMA_STREAM, ENABLE);
- }
- /**
- * @brief Configures the DMA2 Channel4 for SDIO Rx request.
- * @param BufferDST: pointer to the destination buffer
- * @param BufferSize: buffer size
- * @retval None
- */
- void SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize)
- {
- DMA_InitTypeDef SDDMA_InitStructure;
- DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_FEIF | SD_SDIO_DMA_FLAG_DMEIF | SD_SDIO_DMA_FLAG_TEIF | SD_SDIO_DMA_FLAG_HTIF | SD_SDIO_DMA_FLAG_TCIF);
- /* DMA2 Stream3 or Stream6 disable */
- DMA_Cmd(SD_SDIO_DMA_STREAM, DISABLE);
- /* DMA2 Stream3 or Stream6 Config */
- DMA_DeInit(SD_SDIO_DMA_STREAM);
- SDDMA_InitStructure.DMA_Channel = SD_SDIO_DMA_CHANNEL;
- SDDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS;
- SDDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BufferDST;
- SDDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
- SDDMA_InitStructure.DMA_BufferSize = 0;
- SDDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- SDDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- SDDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
- SDDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
- SDDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- SDDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
- SDDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
- SDDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
- SDDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4;
- SDDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4;
- DMA_Init(SD_SDIO_DMA_STREAM, &SDDMA_InitStructure);
- DMA_ITConfig(SD_SDIO_DMA_STREAM, DMA_IT_TC, ENABLE);
- DMA_FlowControllerConfig(SD_SDIO_DMA_STREAM, DMA_FlowCtrl_Peripheral);
- /* DMA2 Stream3 or Stream6 enable */
- DMA_Cmd(SD_SDIO_DMA_STREAM, ENABLE);
- }
- /*
- *********************************************************************************************************
- * 函 数 名: SDIO_Interrupts_Config
- * 功能说明: 配置SDIO中断
- * 形 参: 无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void SDIO_Interrupts_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #if 1
- NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- }
- /*
- *********************************************************************************************************
- * 函 数 名: SDIO_IRQHandler
- * 功能说明: SDIO中断
- * 形 参: 无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void SDIO_IRQHandler(void)
- {
- /* 处理所有的SDIO中断源 */
- SD_ProcessIRQSrc();
- }
- /**
- * @brief This function handles DMA2 Stream3 or DMA2 Stream6 global interrupts
- * requests.
- * @param None
- * @retval None
- */
- void DMA2_Stream3_IRQHandler(void)
- {
- /* Process DMA2 Stream3 or DMA2 Stream6 Interrupt Sources */
- SD_ProcessDMAIRQ();
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
|