SAM3S8/SD8 Software Package: libraries/memories/sdmmc/mci_cmd_pdc.c Source File

SAM3S8/SD8 Software Package 1.2

# mci_cmd_pdc.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2008, Atmel Corporation
    00005
    00006  * All rights reserved.
    00007  *
    00008  * Redistribution and use in source and binary forms, with or without
    00009  * modification, are permitted provided that the following conditions are met:
    00010  *
    00011  * - Redistributions of source code must retain the above copyright notice,
    00012  * this list of conditions and the disclaimer below.
    00013  *
    00014  * Atmel's name may not be used to endorse or promote products derived from
    00015  * this software without specific prior written permission.
    00016  *
    00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
    00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
    00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  * ----------------------------------------------------------------------------
    00028  */
    00029
    00030 /** \file
    00031  *
    00032  *  Implement for SD/MMC low level commands.
    00033  *
    00034  *  \sa hsmci_module
    00035  */
    00036
    00037 /** \addtogroup sdmmc_hal
    00038  *@{
    00039  */
    00040
    00041 /*----------------------------------------------------------------------------
    00042  *         Headers
    00043  *----------------------------------------------------------------------------*/
    00044
    00045 #include "memories.h"
    00046
    00047 #include <assert.h>
    00048
    00049 /*----------------------------------------------------------------------------
    00050  *         Local constants
    00051  *----------------------------------------------------------------------------*/
    00052
    00053 /** Return halfword(16-bit) count from byte count */
    00054 #define toHWCOUNT(byteCnt) (((byteCnt)&0x1) ? (((byteCnt)/2)+1) : ((byteCnt)/2))
    00055 /** Return word(32-bit) count from byte count */
    00056 #define toWCOUNT(byteCnt)  (((byteCnt)&0x3) ? (((byteCnt)/4)+1) : ((byteCnt)/4))
    00057
    00058
    00059 /** Bit mask for status register errors. */
    00060 #define STATUS_ERRORS ((uint32_t)(HSMCI_SR_UNRE  \
    00061                        | HSMCI_SR_OVRE \
    00062                        | HSMCI_SR_ACKRCVE \
    00063                        /*| HSMCI_SR_BLKOVRE*/ \
    00064                        | HSMCI_SR_CSTOE \
    00065                        | HSMCI_SR_DTOE \
    00066                        | HSMCI_SR_DCRCE \
    00067                        | HSMCI_SR_RTOE \
    00068                        | HSMCI_SR_RENDE \
    00069                        | HSMCI_SR_RCRCE \
    00070                        | HSMCI_SR_RDIRE \
    00071                        | HSMCI_SR_RINDE))
    00072
    00073 /** Bit mask for response errors */
    00074 #define STATUS_ERRORS_RESP ((uint32_t)(HSMCI_SR_CSTOE \
    00075                             | HSMCI_SR_RTOE \
    00076                             | HSMCI_SR_RENDE \
    00077                             | HSMCI_SR_RCRCE \
    00078                             | HSMCI_SR_RDIRE \
    00079                             | HSMCI_SR_RINDE))
    00080
    00081 /** Bit mask for data errors */
    00082 #define STATUS_ERRORS_DATA ((uint32_t)(HSMCI_SR_UNRE \
    00083                             | HSMCI_SR_OVRE \
    00084                             /*| HSMCI_SR_BLKOVRE*/ \
    00085                             /*| HSMCI_SR_CSTOE*/ \
    00086                             | HSMCI_SR_DTOE \
    00087                             | HSMCI_SR_DCRCE))
    00088
    00089
    00090 /*---------------------------------------------------------------------------
    00091  * Special command
    00092  *---------------------------------------------------------------------------*/
    00093
    00094 /** MCI Perform SD/MMC Initialize sequence */
    00095 #define SDMMC_POWER_ON_INIT (0 | HSMCI_CMDR_TRCMD_NO_DATA \
    00096                                | HSMCI_CMDR_SPCMD_INIT \
    00097                                | HSMCI_CMDR_OPDCMD )
    00098
    00099 /*---------------------------------------------------------------------------
    00100  * Command Classes
    00101  *---------------------------------------------------------------------------*/
    00102
    00103 /*
    00104  * Class 0, 2, 4, 5, 7 and 8 are mandatory and shall be supported by
    00105  * all SD Memory Cards.
    00106  */
    00107
    00108 /* Basic Commands (class 0) */
    00109
    00110 /** Cmd0 MCI + SPI, bc */
    00111 #define SDMMC_GO_IDLE_STATE     (0 | HSMCI_CMDR_TRCMD_NO_DATA \
    00112                                    | HSMCI_CMDR_SPCMD_STD )
    00113 /** Cmd1 SDSPI + MMC, bcr, R3 */
    00114 #define MMC_SEND_OP_COND        (1 | HSMCI_CMDR_TRCMD_NO_DATA \
    00115                                    | HSMCI_CMDR_SPCMD_STD \
    00116                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00117                                    | HSMCI_CMDR_OPDCMD )
    00118 /** Cmd2 MCI, bcr, R2 */
    00119 #define SDMMC_ALL_SEND_CID      (2 | HSMCI_CMDR_TRCMD_NO_DATA \
    00120                                    | HSMCI_CMDR_SPCMD_STD \
    00121                                    | HSMCI_CMDR_RSPTYP_136_BIT \
    00122                                    | HSMCI_CMDR_OPDCMD )
    00123 /** Cmd3 MCI SD, bcr, R6 */
    00124 #define SD_SEND_RELATIVE_ADDR   (3 | HSMCI_CMDR_TRCMD_NO_DATA \
    00125                                    | HSMCI_CMDR_SPCMD_STD \
    00126                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00127                                    | HSMCI_CMDR_OPDCMD \
    00128                                    | HSMCI_CMDR_MAXLAT )
    00129 /** Cmd3 MCI MMC, ac, R1 */
    00130 #define MMC_SET_RELATIVE_ADDR   (3 | HSMCI_CMDR_TRCMD_NO_DATA \
    00131                                    | HSMCI_CMDR_SPCMD_STD \
    00132                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00133                                    | HSMCI_CMDR_OPDCMD \
    00134                                    | HSMCI_CMDR_MAXLAT )
    00135
    00136 /** Cmd4 MCI, bc */
    00137 #define SDMMC_SET_DSR           (4 | HSMCI_CMDR_TRCMD_NO_DATA \
    00138                                    | HSMCI_CMDR_SPCMD_STD \
    00139                                    | HSMCI_CMDR_RSPTYP_NORESP \
    00140                                    | HSMCI_CMDR_MAXLAT )
    00141 /** Cmd5 MMC, ac, R1b */
    00142 #define MMC_SLEEP_AWAKE         (5 | HSMCI_CMDR_TRCMD_NO_DATA \
    00143                                    | HSMCI_CMDR_SPCMD_STD \
    00144                                    | HSMCI_CMDR_RSPTYP_R1B \
    00145                                    | HSMCI_CMDR_MAXLAT )
    00146 /** Cmd6 MMC, ac, R1b */
    00147 #define MMC_SWITCH              (6 | HSMCI_CMDR_TRCMD_NO_DATA \
    00148                                    | HSMCI_CMDR_SPCMD_STD \
    00149                                    | HSMCI_CMDR_RSPTYP_R1B \
    00150                                    | HSMCI_CMDR_MAXLAT )
    00151 /** Cmd6 SD, adtc, R1 */
    00152 #define SD_SWITCH_FUNC          (6 | HSMCI_CMDR_TRCMD_START_DATA \
    00153                                    | HSMCI_CMDR_TRTYP_SINGLE \
    00154                                    | HSMCI_CMDR_TRDIR \
    00155                                    | HSMCI_CMDR_SPCMD_STD \
    00156                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00157                                    | HSMCI_CMDR_MAXLAT )
    00158 /** Cmd7 MCI, ac, R1/R1b */
    00159 #define SDMMC_SELECT_CARD       (7 | HSMCI_CMDR_TRCMD_NO_DATA \
    00160                                    | HSMCI_CMDR_SPCMD_STD \
    00161                                    | HSMCI_CMDR_RSPTYP_R1B \
    00162                                    | HSMCI_CMDR_MAXLAT )
    00163 /** Cmd7 MCI, ac */
    00164 #define SDMMC_DESELECT_CARD     (7 | HSMCI_CMDR_TRCMD_NO_DATA \
    00165                                    | HSMCI_CMDR_SPCMD_STD \
    00166                                    | HSMCI_CMDR_RSPTYP_NORESP \
    00167                                    | HSMCI_CMDR_MAXLAT )
    00168 /** Cmd8 SD + SPI, bcr, R7 */
    00169 #define SD_SEND_IF_COND         (8 | HSMCI_CMDR_TRCMD_NO_DATA \
    00170                                    | HSMCI_CMDR_SPCMD_STD \
    00171                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00172                                    | HSMCI_CMDR_OPDCMD \
    00173                                    | HSMCI_CMDR_MAXLAT )
    00174 /** Cmd8 MMC, adtc, R1 */
    00175 #define MMC_SEND_EXT_CSD        (8 | HSMCI_CMDR_TRCMD_START_DATA \
    00176                                    | HSMCI_CMDR_TRTYP_SINGLE \
    00177                                    | HSMCI_CMDR_TRDIR \
    00178                                    | HSMCI_CMDR_SPCMD_STD \
    00179                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00180                                    | HSMCI_CMDR_MAXLAT )
    00181 /** Cmd9 MCI + SPI, ac, R2 */
    00182 #define SDMMC_SEND_CSD          (9 | HSMCI_CMDR_TRCMD_NO_DATA \
    00183                                    | HSMCI_CMDR_SPCMD_STD \
    00184                                    | HSMCI_CMDR_RSPTYP_136_BIT \
    00185                                    | HSMCI_CMDR_MAXLAT )
    00186 /** Cmd10 MCI + SPI, ac, R2 */
    00187 #define SDMMC_SEND_CID          (10| HSMCI_CMDR_TRCMD_NO_DATA \
    00188                                    | HSMCI_CMDR_SPCMD_STD \
    00189                                    | HSMCI_CMDR_RSPTYP_136_BIT \
    00190                                    | HSMCI_CMDR_MAXLAT )
    00191
    00192 /** Cmd12 MCI + SPI, ac, R1b (MMC read R1?) */
    00193 #define SDMMC_STOP_TRANSMISSION (12| HSMCI_CMDR_TRCMD_STOP_DATA \
    00194                                    | HSMCI_CMDR_SPCMD_STD \
    00195                                    /*| HSMCI_CMDR_SPCMD_SYNC*/ \
    00196                                    | HSMCI_CMDR_RSPTYP_R1B \
    00197                                    | HSMCI_CMDR_MAXLAT )
    00198 #define SDMMC_STOP_READ         (12| HSMCI_CMDR_RSPTYP_48_BIT \
    00199                                    | HSMCI_CMDR_SPCMD_STD \
    00200                                    | HSMCI_CMDR_TRCMD_STOP_DATA \
    00201                                    | HSMCI_CMDR_MAXLAT )
    00202 #define SDMMC_STOP_WRITE        (12| HSMCI_CMDR_RSPTYP_R1B \
    00203                                    | HSMCI_CMDR_SPCMD_STD \
    00204                                    | HSMCI_CMDR_TRCMD_STOP_DATA \
    00205                                    | HSMCI_CMDR_MAXLAT )
    00206 /** Cmd13 MCI + SPI, ac, R1 */
    00207 #define SDMMC_SEND_STATUS       (13| HSMCI_CMDR_TRCMD_NO_DATA \
    00208                                    | HSMCI_CMDR_SPCMD_STD \
    00209                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00210                                    | HSMCI_CMDR_MAXLAT )
    00211 /** Cmd14 MMC, adtc, R1 */
    00212 #define MMC_BUSTEST_R           (14| HSMCI_CMDR_TRCMD_START_DATA \
    00213                                    | HSMCI_CMDR_TRTYP_SINGLE \
    00214                                    | HSMCI_CMDR_TRDIR \
    00215                                    | HSMCI_CMDR_RSPTYP_48_BIT ) \
    00216                                    | HSMCI_CMDR_MAXLAT )
    00217 /** Cmd15 MCI, ac */
    00218 #define SDMMC_GO_INACTIVE_STATE (15| HSMCI_CMDR_RSPTYP_NORESP )
    00219
    00220 /** Cmd19 MMC, adtc, R1 */
    00221 #define MMC_BUSTEST_W           (19| HSMCI_CMDR_TRCMD_START_DATA \
    00222                                    | HSMCI_CMDR_TRTYP_SINGLE \
    00223                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00224                                    | HSMCI_CMDR_MAXLAT )
    00225
    00226 /*-------------------------------------------------
    00227  * Class 1 commands: Stream oriented Read commands
    00228  *-------------------------------------------------*/
    00229
    00230 /** Cmd11 MMC, adtc, R1 */
    00231 #define MMC_READ_DAT_UNTIL_STOP (11| HSMCI_CMDR_SPCMD_STD \
    00232                                    | HSMCI_CMDR_RSPTYP_48_BIT \
    00233                                    | HSMCI_CMDR_TRCMD_START_DATA \
    00234                                    | HSMCI_CMDR_TRTYP_STREAM \
    00235                                    | HSMCI_CMDR_TRDIR_READ \
    00236                                    | HSMCI_CMDR_MAXLAT )
    00237
    00238 /*-------------------------------------------------
    00239  * Class 2 commands: Block oriented Read commands
    00240  *-------------------------------------------------*/
    00241
    00242 /** Cmd16, ac, R1 */
    00243 #define SDMMC_SET_BLOCKLEN          (16| HSMCI_CMDR_TRCMD_NO_DATA \
    00244                                        | HSMCI_CMDR_SPCMD_STD \
    00245                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00246                                        | HSMCI_CMDR_MAXLAT_64 )
    00247 /** Cmd17, adtc, R1 */
    00248 #define SDMMC_READ_SINGLE_BLOCK     (17| HSMCI_CMDR_SPCMD_STD \
    00249                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00250                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00251                                        | HSMCI_CMDR_TRTYP_SINGLE \
    00252                                        | HSMCI_CMDR_TRDIR_READ \
    00253                                        | HSMCI_CMDR_MAXLAT)
    00254 /** Cmd18, adtc, R1 */
    00255 #define SDMMC_READ_MULTIPLE_BLOCK   (18| HSMCI_CMDR_SPCMD_STD \
    00256                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00257                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00258                                        | HSMCI_CMDR_TRTYP_MULTIPLE \
    00259                                        | HSMCI_CMDR_TRDIR_READ \
    00260                                        | HSMCI_CMDR_MAXLAT)
    00261
    00262 /*-------------------------------------------------
    00263  * Class 3 commands: Stream write commands
    00264  *-------------------------------------------------*/
    00265
    00266 #define MMC_WRITE_DAT_UNTIL_STOP    (20| HSMCI_CMDR_SPCMD_STD \
    00267                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00268                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00269                                        | HSMCI_CMDR_TRTYP_STREAM \
    00270                                        | HSMCI_CMDR_TRDIR_WRITE \
    00271                                        | HSMCI_CMDR_MAXLAT )
    00272
    00273 /*-------------------------------------------------
    00274  * Class 4 commands: Block oriented write commands
    00275  *-------------------------------------------------*/
    00276
    00277 /** Cmd23, MMC, ac, R1 */
    00278 #define MMC_SET_BLOCK_COUNT         (23| HSMCI_CMDR_TRCMD_NO_DATA \
    00279                                        | HSMCI_CMDR_SPCMD_STD \
    00280                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00281                                        | HSMCI_CMDR_MAXLAT )
    00282 /** Cmd24, adtc, R1 */
    00283 #define SDMMC_WRITE_BLOCK           (24| HSMCI_CMDR_SPCMD_STD \
    00284                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00285                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00286                                        | HSMCI_CMDR_TRTYP_MULTIPLE \
    00287                                        | HSMCI_CMDR_TRDIR_WRITE \
    00288                                        | HSMCI_CMDR_MAXLAT)
    00289 /** Cmd25, adtc, R1 */
    00290 #define SDMMC_WRITE_MULTIPLE_BLOCK  (25| HSMCI_CMDR_SPCMD_STD \
    00291                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00292                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00293                                        | HSMCI_CMDR_TRTYP_MULTIPLE \
    00294                                        | HSMCI_CMDR_TRDIR_WRITE \
    00295                                        | HSMCI_CMDR_MAXLAT)
    00296 /** Cmd26, MMC, adtc, R1 */
    00297 #define MMC_PROGRAM_CID             (26| HSMCI_CMDR_RSPTYP_48_BIT )
    00298 /** Cmd27, adtc, R1 */
    00299 #define SDMMC_PROGRAM_CSD           (27| HSMCI_CMDR_RSPTYP_48_BIT )
    00300
    00301 /*------------------------------------------------
    00302  * Class 8 commands: Application specific commands
    00303  *------------------------------------------------*/
    00304
    00305 /** Cmd55, SD, ac, R1 */
    00306 #define SDMMC_APP_CMD               (55| HSMCI_CMDR_SPCMD_STD \
    00307                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00308                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00309                                        | HSMCI_CMDR_MAXLAT)
    00310 /** Cmd 56, SD, adtc, R1 */
    00311 #define SDMMC_GEN_CMD               (56| HSMCI_CMDR_SPCMD_STD \
    00312                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00313                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00314                                        | HSMCI_CMDR_MAXLAT)
    00315
    00316 /*------------------------------------------------
    00317  * Class 9 commands: I/O mode commands
    00318  *------------------------------------------------*/
    00319
    00320 /** Cmd39, MMC, ac, R4 */
    00321 #define MMC_FAST_IO                 (39| HSMCI_CMDR_SPCMD_STD \
    00322                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00323                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00324                                        | HSMCI_CMDR_MAXLAT )
    00325 /** Cmd40, MMC, bcr, R5 */
    00326 #define MMC_GO_IRQ_STATE            (40| HSMCI_CMDR_SPCMD_IT_CMD \
    00327                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00328                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00329                                        | HSMCI_CMDR_OPDCMD_OPENDRAIN \
    00330                                        | HSMCI_CMDR_MAXLAT )
    00331
    00332 /*------------------------------------------------
    00333  * SD Application Specific commands:
    00334  *------------------------------------------------*/
    00335
    00336 /** ACMD6, ac, R1 */
    00337 #define SD_SET_BUS_WIDTH            (6 | HSMCI_CMDR_SPCMD_STD \
    00338                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00339                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00340                                        | HSMCI_CMDR_MAXLAT)
    00341 /** ACMD13, adtc, R1 */
    00342 #define SD_SD_STATUS                (13| HSMCI_CMDR_SPCMD_STD \
    00343                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00344                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00345                                        | HSMCI_CMDR_TRTYP_SINGLE \
    00346                                        | HSMCI_CMDR_TRDIR_READ \
    00347                                        | HSMCI_CMDR_MAXLAT)
    00348 /** ACMD22, adtc, R1 */
    00349 #define SD_SEND_NUM_WR_BLOCKS       (22| HSMCI_CMDR_SPCMD_STD \
    00350                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00351                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00352                                        | HSMCI_CMDR_MAXLAT)
    00353 /** ACMD23, ac, R1 */
    00354 #define SD_SET_WR_BLK_ERASE_COUNT   (23| HSMCI_CMDR_SPCMD_STD \
    00355                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00356                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00357                                        | HSMCI_CMDR_MAXLAT)
    00358 /** ACMD41, bcr, R3 */
    00359 #define SD_SD_SEND_OP_COND          (41| HSMCI_CMDR_SPCMD_STD \
    00360                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00361                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00362                                        /*| HSMCI_CMDR_OPDCMD_OPENDRAIN */ )
    00363 /** ACMD42, ac, R1 */
    00364 #define SD_SET_CLR_CARD_DETECT      (42| HSMCI_CMDR_SPCMD_STD \
    00365                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00366                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00367                                        | HSMCI_CMDR_MAXLAT)
    00368 /** ACMD51, adtc, R1 */
    00369 #define SD_SEND_SCR                 (51| HSMCI_CMDR_SPCMD_STD \
    00370                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00371                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00372                                        | HSMCI_CMDR_TRDIR_READ \
    00373                                        | HSMCI_CMDR_TRTYP_SINGLE \
    00374                                        | HSMCI_CMDR_MAXLAT)
    00375
    00376 /*------------------------------------------------
    00377  * SDIO commands:
    00378  *------------------------------------------------*/
    00379
    00380 /** SDIO CMD5, R4 */
    00381 #define SDIO_SEND_OP_COND           (5 | HSMCI_CMDR_SPCMD_STD \
    00382                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00383                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00384                                        | HSMCI_CMDR_OPDCMD )
    00385
    00386 /** SDIO CMD52, R5 */
    00387 #define SDIO_IO_RW_DIRECT           (52| HSMCI_CMDR_SPCMD_STD \
    00388                                        | HSMCI_CMDR_TRCMD_NO_DATA \
    00389                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00390                                        | HSMCI_CMDR_MAXLAT)
    00391
    00392 /** SDIO CMD53, R5 */
    00393 #define SDIO_IO_RW_EXTENDED         (53| HSMCI_CMDR_SPCMD_STD \
    00394                                        | HSMCI_CMDR_TRCMD_START_DATA \
    00395                                        | HSMCI_CMDR_RSPTYP_48_BIT \
    00396                                        | HSMCI_CMDR_MAXLAT )
    00397
    00398
    00399 /*---------------------------------------------------------------------------
    00400  *         Local types
    00401  *---------------------------------------------------------------------------*/
    00402
    00403 /** Transfer parameters */
    00404 typedef struct _MciTransfer {
    00405     uint8_t  *pData;        /** Pointer to data buffer */
    00406     uint32_t size;          /** Total number of bytes to transfer */
    00407     uint32_t transferred;   /** Number of bytes finished */
    00408     uint16_t fifoed;        /** bytes in FIFO */
    00409     uint8_t  isRx;          /** 1 for RX */
    00410     uint8_t  reserved;
    00411 } MciTransfer;
    00412
    00413 /*----------------------------------------------------------------------------
    00414  *         Local variable
    00415  *----------------------------------------------------------------------------*/
    00416
    00417 /** Tx/Rx parameters */
    00418 MciTransfer hsmciXfr;
    00419
    00420 static MciCmd mciCmd;
    00421
    00422
    00423 /*---------------------------------------------------------------------------
    00424  *         Internal functions
    00425  *---------------------------------------------------------------------------*/
    00426
    00427
    00428 #ifdef HSMCI_WRITE_FIFO
    00429 /**
    00430  * Write data to FIFO
    00431  * \param pHw  Pointer to HSMCI register base.
    00432  * \param pXfr Pointer to transfer instance.
    00433  * \param size Size of data in bytes.
    00434  */
    00435 static uint8_t FIFO_Write(Hsmci *pHw, MciTransfer *pXfr, uint32_t size)
    00436 {
    00437     volatile uint32_t *pFIFO = (volatile uint32_t*)((uint32_t)pHw + 0x200);
    00438     register uint32_t c4, c1;
    00439     uint32_t *pData = (uint32_t*)&pXfr->pData[pXfr->transferred];
    00440
    00441     if (size > pXfr->size - pXfr->transferred)
    00442         size = pXfr->size - pXfr->transferred;
    00443
    00444     if (size == 0)
    00445         return 0;
    00446
    00447     pXfr->fifoed += size;
    00448
    00449     /* convert bytes to DW */
    00450     if (size & 0x3) size += 4;
    00451     size /= 4;
    00452
    00453     c4 = size >> 2;
    00454     c1 = size & 0x3;
    00455     for (;c4; c4 --) {
    00456         *pFIFO ++ = *pData ++;
    00457         *pFIFO ++ = *pData ++;
    00458         *pFIFO ++ = *pData ++;
    00459         *pFIFO ++ = *pData ++;
    00460     }
    00461     for (;c1; c1 --) {
    00462         *pFIFO ++ = *pData ++;
    00463     }
    00464
    00465     return 0;
    00466 }
    00467 #endif
    00468 #ifdef HSMCI_READ_FIFO
    00469 /**
    00470  * Read data from FIFO
    00471  * \param pHw  Pointer to HSMCI register base.
    00472  * \param pXfr Pointer to transfer instance.
    00473  * \param size Size of data in DW.
    00474  */
    00475 static uint8_t FIFO_Read(Hsmci *pHw, MciTransfer *pXfr, uint32_t size)
    00476 {
    00477     volatile uint32_t *pFIFO = (volatile uint32_t*)((uint32_t)pHw + 0x200);
    00478     register uint32_t c4, c1;
    00479     uint32_t *pData = (uint32_t*)&pXfr->pData[pXfr->transferred];
    00480
    00481     if (size > pXfr->size - pXfr->transferred)
    00482         size = pXfr->size - pXfr->transferred;
    00483
    00484     if (size == 0)
    00485         return 0;
    00486
    00487     pXfr->fifoed -= size;
    00488     pXfr->transferred += size;
    00489
    00490     /* convert bytes to DW */
    00491     if (size & 0x3) size += 4;
    00492     size /= 4;
    00493
    00494     c4 = size >> 2;
    00495     c1 = size & 0x3;
    00496     for (;c4; c4 --) {
    00497         *pData ++ = *pFIFO ++;
    00498         *pData ++ = *pFIFO ++;
    00499         *pData ++ = *pFIFO ++;
    00500         *pData ++ = *pFIFO ++;
    00501     }
    00502     for (;c1; c1 --) {
    00503         *pData ++ = *pFIFO ++;
    00504     }
    00505
    00506     return 0;
    00507 }
    00508 #endif
    00509
    00510 /*----------------------------------------------------------------------------
    00511  *         Local functions
    00512  *----------------------------------------------------------------------------*/
    00513
    00514 /**
    00515  * Reset the MciCmd
    00516  */
    00517 static void ResetMciCommand(MciCmd *pCommand)
    00518 {
    00519     pCommand->cmd       = 0;
    00520     pCommand->arg       = 0;
    00521     pCommand->pData     = 0;
    00522     pCommand->blockSize = 0;
    00523     pCommand->nbBlock   = 0;
    00524     pCommand->pResp     = 0;
    00525     pCommand->callback  = 0;
    00526     pCommand->pArg      = 0;
    00527     pCommand->resType   = 0;
    00528     pCommand->tranType  = 0;
    00529     pCommand->busyCheck = 0;
    00530     pCommand->status    = 0;
    00531     pCommand->state     = 0;
    00532 }
    00533
    00534 /**
    00535  * Sends the current SD card driver command to the card.
    00536  * Returns 0 if successful; Otherwise, returns the transfer status code or
    00537  * SDMMC_ERROR if there was a problem with the SD transfer.
    00538  * \param pSd  Pointer to a SdCard driver instance.
    00539  * \param fCallback Pointer to optional callback invoked on command end.
    00540  *                  NULL:    Function return until command finished.
    00541  *                  Pointer: Return immediately and invoke callback at end.
    00542  *                  Callback argument is fixed to a pointer to SdCard instance.
    00543  */
    00544 static uint8_t SendMciCommand(SdCard *pSd, SdmmcCallback fCallback)
    00545 {
    00546     Mcid   *pMcid    = pSd->pSdDriver;
    00547     MciCmd *pCommand = &(mciCmd);
    00548     uint8_t error;
    00549
    00550     //TRACE_INFO("MCI Cmd: %x\n\r", pCommand->cmd);
    00551
    00552     /* Fill callback */
    00553     pCommand->callback = fCallback;
    00554     pCommand->pArg     = pSd;
    00555
    00556     /* Send command */
    00557     error = Sdmmc_SendCommand(pMcid, pCommand);
    00558     if (error) {
    00559         TRACE_ERROR("Sdmmc_SendCommand(%x): %d\n\r", (unsigned int)pCommand->cmd, (int)error);
    00560         return SDMMC_ERROR;
    00561     }
    00562
    00563     /* Wait for command to complete (if no callback defined) */
    00564     if (pCommand->callback == 0) {
    00565         while (!Sdmmc_IsCommandComplete(pMcid));
    00566         return pCommand->status;
    00567     }
    00568
    00569     return 0;
    00570 }
    00571
    00572
    00573 /*---------------------------------------------------------------------------
    00574  *      Exported functions
    00575  *---------------------------------------------------------------------------*/
    00576
    00577
    00578 /**
    00579  * Initialization delay: The maximum of 1 msec, 74 clock cycles and supply ramp
    00580  * up time.
    00581  * Returns the command transfer result (see SendMciCommand).
    00582  * \param pSd  Pointer to a SdCard driver instance.
    00583  * \param fCallback Pointer to optional callback invoked on command end.
    00584  *                  NULL:    Function return until command finished.
    00585  *                  Pointer: Return immediately and invoke callback at end.
    00586  *                  Callback argument is fixed to a pointer to SdCard instance.
    00587  */
    00588 uint8_t SdmmcPowerOn(SdCard *pSd, SdmmcCallback fCallback)
    00589 {
    00590     MciCmd *pCommand = &(mciCmd);
    00591     uint32_t response;
    00592     uint8_t error;
    00593
    00594     TRACE_DEBUG( "PowerON()\n\r" ) ;
    00595     ResetMciCommand(pCommand);
    00596
    00597     /* Fill command information */
    00598     pCommand->cmd = SDMMC_POWER_ON_INIT;
    00599     pCommand->pResp = &response;
    00600
    00601     /* Send command */
    00602     error =  SendMciCommand(pSd, fCallback);
    00603     return error;
    00604 }
    00605
    00606 /**
    00607  * Resets all cards to idle state
    00608  * \param pSd  Pointer to a SdCard driver instance.
    00609  * \param arg  Argument used.
    00610  * \return the command transfer result (see SendMciCommand).
    00611  * \param fCallback Pointer to optional callback invoked on command end.
    00612  *                  NULL:    Function return until command finished.
    00613  *                  Pointer: Return immediately and invoke callback at end.
    00614  *                  Callback argument is fixed to a pointer to SdCard instance.
    00615  */
    00616 uint8_t SdmmcCmd0(SdCard *pSd, uint32_t arg, SdmmcCallback fCallback)
    00617 {
    00618     MciCmd *pCommand = &(mciCmd);
    00619     uint8_t error;
    00620
    00621     TRACE_DEBUG("Cmd0()\n\r");
    00622     ResetMciCommand(pCommand);
    00623
    00624     /* Fill command information */
    00625     pCommand->cmd = SDMMC_GO_IDLE_STATE;
    00626     pCommand->arg = arg;
    00627
    00628     /* Send command */
    00629     error = SendMciCommand(pSd, fCallback);
    00630     return error;
    00631 }
    00632
    00633 /**
    00634  * MMC send operation condition command.
    00635  * Sends host capacity support information and activates the card's
    00636  * initialization process.
    00637  * Returns the command transfer result (see SendMciCommand).
    00638  * \param pSd  Pointer to a SdCard driver instance.
    00639  * \param pOCR Pointer to fill OCR value to send and get.
    00640  * \param fCallback Pointer to optional callback invoked on command end.
    00641  *                  NULL:    Function return until command finished.
    00642  *                  Pointer: Return immediately and invoke callback at end.
    00643  *                  Callback argument is fixed to a pointer to SdCard instance.
    00644  */
    00645 uint8_t MmcCmd1(SdCard *pSd, uint32_t* pOCR, SdmmcCallback fCallback)
    00646 {
    00647     MciCmd *pCommand = &(mciCmd);
    00648     uint8_t error;
    00649
    00650     TRACE_DEBUG("Cmd1()\n\r");
    00651     ResetMciCommand(pCommand);
    00652
    00653     /* Fill command information */
    00654     pCommand->cmd = MMC_SEND_OP_COND;
    00655     pCommand->arg = *pOCR;
    00656     pCommand->resType = 3;
    00657     pCommand->pResp = pOCR;
    00658
    00659     /* send command */
    00660     error = SendMciCommand(pSd, fCallback);
    00661     return error;
    00662 }
    00663
    00664 /**
    00665  * Asks any card to send the CID numbers
    00666  * on the CMD line (any card that is
    00667  * connected to the host will respond)
    00668  * Returns the command transfer result (see SendMciCommand).
    00669  * \param pSd  Pointer to a SD card driver instance.
    00670  * \param pCID Pointer to buffer for storing the CID numbers.
    00671  * \param fCallback Pointer to optional callback invoked on command end.
    00672  *                  NULL:    Function return until command finished.
    00673  *                  Pointer: Return immediately and invoke callback at end.
    00674  *                  Callback argument is fixed to a pointer to SdCard instance.
    00675  */
    00676 uint8_t SdmmcCmd2(SdCard *pSd, uint32_t *pCID, SdmmcCallback fCallback)
    00677 {
    00678     /* Stop warning */
    00679     pCID = pCID;
    00680
    00681     MciCmd *pCommand = &(mciCmd);
    00682
    00683     TRACE_DEBUG("Cmd2()\n\r");
    00684     ResetMciCommand(pCommand);
    00685
    00686     /* Fill the command information */
    00687     pCommand->cmd = SDMMC_ALL_SEND_CID;
    00688     pCommand->resType = 2;
    00689     pCommand->pResp = pSd->cid;
    00690
    00691     /* Send the command */
    00692     return SendMciCommand(pSd, fCallback);
    00693 }
    00694
    00695 /**
    00696  * Ask the card to publish a new relative address (RCA)
    00697  * Returns the command transfer result (see SendMciCommand).
    00698  * \param pSd   Pointer to a SD card driver instance.
    00699  * \param pRsp  Pointer to buffer to fill response (address on 31:16).
    00700  * \param fCallback Pointer to optional callback invoked on command end.
    00701  *                  NULL:    Function return until command finished.
    00702  *                  Pointer: Return immediately and invoke callback at end.
    00703  *                  Callback argument is fixed to a pointer to SdCard instance.
    00704  */
    00705 uint8_t SdCmd3(SdCard *pSd, uint32_t *pRsp, SdmmcCallback fCallback)
    00706 {
    00707     MciCmd *pCommand = &(mciCmd);
    00708     uint8_t error;
    00709
    00710     TRACE_DEBUG("Cmd3()\n\r");
    00711     ResetMciCommand(pCommand);
    00712
    00713     /* Fill command information */
    00714     pCommand->cmd = SD_SEND_RELATIVE_ADDR;
    00715     pCommand->resType = 6;
    00716     pCommand->pResp = pRsp;
    00717
    00718     /* Send command */
    00719     error = SendMciCommand(pSd, fCallback);
    00720     return error;
    00721 }
    00722
    00723 /**
    00724  * Assign relative address to the card
    00725  * Returns the command transfer result (see SendMciCommand).
    00726  * \param pSd       Pointer to a SD card driver instance.
    00727  * \param cardAddr  Card Relative Address value.
    00728  * \param fCallback Pointer to optional callback invoked on command end.
    00729  *                  NULL:    Function return until command finished.
    00730  *                  Pointer: Return immediately and invoke callback at end.
    00731  *                  Callback argument is fixed to a pointer to SdCard instance.
    00732  */
    00733 uint8_t MmcCmd3(SdCard *pSd, uint16_t cardAddr, SdmmcCallback fCallback)
    00734 {
    00735     MciCmd *pCommand = &(mciCmd);
    00736     uint32_t response;
    00737     uint8_t error;
    00738
    00739     TRACE_DEBUG("Cmd3()\n\r");
    00740     ResetMciCommand(pCommand);
    00741
    00742     /* Fill command information */
    00743     pCommand->cmd = MMC_SET_RELATIVE_ADDR;
    00744     pCommand->arg = (cardAddr << 16);
    00745     pCommand->resType = 6;
    00746     pCommand->pResp = &response;
    00747
    00748     /* Send command */
    00749     error = SendMciCommand(pSd, fCallback);
    00750     return error;
    00751 }
    00752
    00753 /**
    00754  * Command toggles a card between the
    00755  * stand-by and transfer states or between
    00756  * the programming and disconnect states.
    00757  * Returns the command transfer result (see SendMciCommand).
    00758  * \param pSd       Pointer to a SD card driver instance.
    00759  * \param cardAddr  Relative Card Address (0 deselects all).
    00760  * \param fCallback Pointer to optional callback invoked on command end.
    00761  *                  NULL:    Function return until command finished.
    00762  *                  Pointer: Return immediately and invoke callback at end.
    00763  *                  Callback argument is fixed to a pointer to SdCard instance.
    00764  */
    00765 uint8_t SdmmcCmd7(SdCard *pSd, uint16_t cardAddr, SdmmcCallback fCallback)
    00766 {
    00767     MciCmd *pCommand = &(mciCmd);
    00768
    00769     TRACE_DEBUG("Cmd7()\n\r");
    00770     ResetMciCommand(pCommand);
    00771
    00772     /* Fill command information */
    00773     pCommand->cmd = cardAddr ? SDMMC_SELECT_CARD : SDMMC_DESELECT_CARD;
    00774     pCommand->arg = cardAddr << 16;
    00775     pCommand->resType = 1;
    00776
    00777     /* Send command */
    00778     return SendMciCommand(pSd, fCallback);
    00779 }
    00780
    00781 /**
    00782  * SDIO SEND OPERATION CONDITION (OCR) command.
    00783  * Sends host capacity support information and acrivates the card's
    00784  * initialization process.
    00785  * \return The command transfer result (see SendMciCommand).
    00786  * \param pSd     Pointer to a SD/MMC card driver instance.
    00787  * \param pIoData Pointer to data sent as well as response buffer (32bit).
    00788  * \param fCallback Pointer to optional callback invoked on command end.
    00789  *                  NULL:    Function return until command finished.
    00790  *                  Pointer: Return immediately and invoke callback at end.
    00791  *                  Callback argument is fixed to a pointer to SdCard instance.
    00792  */
    00793 uint8_t SdioCmd5(SdCard *pSd, uint32_t *pIoData, SdmmcCallback fCallback)
    00794 {
    00795     MciCmd *pCommand = &(mciCmd);
    00796
    00797     TRACE_DEBUG("Cmd5()\n\r");
    00798     ResetMciCommand(pCommand);
    00799
    00800     /* Fill command information */
    00801     pCommand->cmd = SDIO_SEND_OP_COND;
    00802     pCommand->arg = *pIoData;
    00803     pCommand->resType = 4;
    00804     pCommand->pResp = pIoData;
    00805
    00806     /* Send command */
    00807     return SendMciCommand(pSd, fCallback);
    00808 }
    00809
    00810 /**
    00811  * Switches the mode of operation of the selected card.
    00812  * CMD6 is valid under the "trans" state.
    00813  * \return The command transfer result (see SendMciCommand).
    00814  * \param  pSd         Pointer to a SD/MMC card driver instance.
    00815  * \param  pSwitchArg  Pointer to a MmcCmd6Arg instance.
    00816  * \param  pStatus     Pointer to where the 512bit status is returned.
    00817  * \param  pResp       Pointer to where the response is returned.
    00818  * \param fCallback Pointer to optional callback invoked on command end.
    00819  *                  NULL:    Function return until command finished.
    00820  *                  Pointer: Return immediately and invoke callback at end.
    00821  *                  Callback argument is fixed to a pointer to SdCard instance.
    00822  */
    00823 uint8_t SdCmd6(SdCard *pSd,
    00824                const void * pSwitchArg,
    00825                uint32_t   * pStatus,
    00826                uint32_t   * pResp,
    00827                SdmmcCallback fCallback)
    00828 {
    00829     MciCmd *pCommand = &(mciCmd);
    00830     uint8_t error;
    00831     SdCmd6Arg * pSdSwitch;
    00832
    00833     assert(pSd);
    00834     assert(pSwitchArg);
    00835
    00836     TRACE_DEBUG("CMD6()\n\r");
    00837     ResetMciCommand(pCommand);
    00838
    00839     pSdSwitch = (SdCmd6Arg*)pSwitchArg;
    00840     pCommand->cmd = SD_SWITCH_FUNC;
    00841     pCommand->resType = 1;
    00842     pCommand->arg =   (pSdSwitch->mode << 31)
    00843                     | (pSdSwitch->reserved << 30)
    00844                     | (pSdSwitch->reserveFG6 << 20)
    00845                     | (pSdSwitch->reserveFG5 << 16)
    00846                     | (pSdSwitch->reserveFG4 << 12)
    00847                     | (pSdSwitch->reserveFG3 <<  8)
    00848                     | (pSdSwitch->command << 4)
    00849                     | (pSdSwitch->accessMode << 0);
    00850     if (pStatus) {
    00851         pCommand->blockSize = 512 / 8;
    00852         pCommand->nbBlock = 1;
    00853         pCommand->pData = (uint8_t*)pStatus;
    00854         pCommand->tranType = MCI_START_READ;
    00855     }
    00856     pCommand->pResp = pResp;
    00857
    00858     error = SendMciCommand(pSd, fCallback);
    00859     return error;
    00860 }
    00861
    00862 /**
    00863  * Switches the mode of operation of the selected card or
    00864  * Modifies the EXT_CSD registers.
    00865  * CMD6 is valid under the "trans" state.
    00866  * \return The command transfer result (see SendMciCommand).
    00867  * \param  pSd         Pointer to a SD/MMC card driver instance.
    00868  * \param  pSwitchArg  Pointer to a MmcCmd6Arg instance.
    00869  * \param  pResp       Pointer to where the response is returned.
    00870  * \param fCallback Pointer to optional callback invoked on command end.
    00871  *                  NULL:    Function return until command finished.
    00872  *                  Pointer: Return immediately and invoke callback at end.
    00873  *                  Callback argument is fixed to a pointer to SdCard instance.
    00874  */
    00875 uint8_t MmcCmd6(SdCard *pSd,
    00876                 const void * pSwitchArg,
    00877                 uint32_t   * pResp,
    00878                 SdmmcCallback fCallback)
    00879 {
    00880     MciCmd *pCommand = &(mciCmd);
    00881     uint8_t error;
    00882     MmcCmd6Arg * pMmcSwitch;
    00883
    00884     assert(pSd);
    00885     assert(pSwitchArg);
    00886
    00887     TRACE_DEBUG("CMD6()\n\r");
    00888     ResetMciCommand(pCommand);
    00889
    00890     pMmcSwitch = (MmcCmd6Arg*)pSwitchArg;
    00891     pCommand->cmd = MMC_SWITCH;
    00892     pCommand->resType = 1;
    00893     pCommand->arg =   (pMmcSwitch->access << 24)
    00894                     | (pMmcSwitch->index  << 16)
    00895                     | (pMmcSwitch->value  <<  8)
    00896                     | (pMmcSwitch->cmdSet <<  0);
    00897     pCommand->pResp = pResp;
    00898
    00899     error = SendMciCommand(pSd, fCallback);
    00900     return error;
    00901 }
    00902
    00903 /**
    00904  * Sends SD Memory Card interface condition, which includes host supply
    00905  * voltage information and asks the card whether card supports voltage.
    00906  * Should be performed at initialization time to detect the card type.
    00907  * \param pSd             Pointer to a SD card driver instance.
    00908  * \param supplyVoltage   Expected supply voltage(SD).
    00909  * \param fCallback Pointer to optional callback invoked on command end.
    00910  *                  NULL:    Function return until command finished.
    00911  *                  Pointer: Return immediately and invoke callback at end.
    00912  *                  Callback argument is fixed to a pointer to SdCard instance.
    00913  * \return 0 if successful;
    00914  *         otherwise returns SD_ERROR_NORESPONSE if the card did not answer
    00915  *         the command, or SDMMC_ERROR.
    00916  */
    00917 uint8_t SdCmd8(SdCard *pSd,
    00918                uint8_t supplyVoltage,
    00919                SdmmcCallback fCallback)
    00920 {
    00921     MciCmd *pCommand = &(mciCmd);
    00922     uint32_t response;
    00923     uint8_t error;
    00924
    00925     TRACE_DEBUG("Cmd8()\n\r");
    00926     ResetMciCommand(pCommand);
    00927
    00928     /* Fill command information */
    00929     pCommand->cmd = SD_SEND_IF_COND;
    00930     pCommand->arg = (supplyVoltage << 8) | (0xAA);
    00931     pCommand->resType = 7;
    00932     pCommand->pResp = &response;
    00933
    00934     /* Send command */
    00935     error = SendMciCommand(pSd, fCallback);
    00936
    00937     /* Return if there is callback */
    00938     if (fCallback)
    00939         return error;
    00940
    00941     /* Check result */
    00942     if (error == SDMMC_ERROR_NORESPONSE) {
    00943         return SDMMC_ERROR_NORESPONSE;
    00944     }
    00945     /* SD_R7
    00946      * Bit 0 - 7: check pattern (echo-back)
    00947      * Bit 8 -11: voltage accepted
    00948      */
    00949     else if (!error &&
    00950             ((response & 0x00000FFF) == (((uint32_t)supplyVoltage << 8) | 0xAAu))) {
    00951         return 0;
    00952     }
    00953     else {
    00954         return SDMMC_ERROR;
    00955     }
    00956 }
    00957
    00958 /**
    00959  * SEND_EXT_CSD, to get EXT_CSD register as a block of data.
    00960  * Valid under "trans" state.
    00961  * \param pSd   Pointer to a SD card driver instance.
    00962  * \param pEXT  512 byte buffer pointer for EXT_CSD data.
    00963  * \param fCallback Pointer to optional callback invoked on command end.
    00964  *                  NULL:    Function return until command finished.
    00965  *                  Pointer: Return immediately and invoke callback at end.
    00966  *                  Callback argument is fixed to a pointer to SdCard instance.
    00967  * \return 0 if successful;
    00968  *         otherwise returns SD_ERROR_NORESPONSE if the card did not answer
    00969  *         the command, or SDMMC_ERROR.
    00970  */
    00971 uint8_t MmcCmd8(SdCard *pSd,
    00972                 uint8_t* pEXT,
    00973                 SdmmcCallback fCallback)
    00974 {
    00975     MciCmd *pCommand = &(mciCmd);
    00976     uint8_t error;
    00977
    00978     TRACE_DEBUG("Cmd8()\n\r");
    00979     ResetMciCommand(pCommand);
    00980
    00981     pCommand->cmd = MMC_SEND_EXT_CSD;
    00982     pCommand->resType = 1;
    00983
    00984     pCommand->blockSize = 512;
    00985     pCommand->nbBlock = 1;
    00986     pCommand->pData = pEXT;
    00987
    00988     pCommand->tranType = MCI_START_READ;
    00989
    00990     /* Send command */
    00991     error = SendMciCommand(pSd, fCallback);
    00992     return error;
    00993 }
    00994
    00995 /**
    00996  * Addressed card sends its card-specific
    00997  * data (CSD) on the CMD line.
    00998  * Returns the command transfer result (see SendMciCommand).
    00999  * \param pSd       Pointer to a SD card driver instance.
    01000  * \param cardAddr  Card Relative Address.
    01001  * \param pCSD      Pointer to buffer for CSD data.
    01002  * \param fCallback Pointer to optional callback invoked on command end.
    01003  *                  NULL:    Function return until command finished.
    01004  *                  Pointer: Return immediately and invoke callback at end.
    01005  *                  Callback argument is fixed to a pointer to SdCard instance.
    01006  */
    01007 uint8_t SdmmcCmd9(SdCard *pSd,
    01008                   uint16_t cardAddr,
    01009                   uint32_t *pCSD,
    01010                   SdmmcCallback fCallback)
    01011 {
    01012     MciCmd *pCommand = &(mciCmd);
    01013     uint8_t error;
    01014
    01015     TRACE_DEBUG("Cmd9()\n\r");
    01016     ResetMciCommand(pCommand);
    01017
    01018     /* Fill command information */
    01019     pCommand->cmd = SDMMC_SEND_CSD;
    01020     pCommand->arg = cardAddr << 16;
    01021     pCommand->resType = 2;
    01022     pCommand->pResp = pCSD;
    01023
    01024     /* Send command */
    01025     error = SendMciCommand(pSd, fCallback);
    01026     return error;
    01027 }
    01028
    01029 /**
    01030  * Forces the card to stop transmission
    01031  * \param pSd      Pointer to a SD card driver instance.
    01032  * \param pStatus  Pointer to a status variable.
    01033  * \param fCallback Pointer to optional callback invoked on command end.
    01034  *                  NULL:    Function return until command finished.
    01035  *                  Pointer: Return immediately and invoke callback at end.
    01036  *                  Callback argument is fixed to a pointer to SdCard instance.
    01037  */
    01038 uint8_t SdmmcCmd12(SdCard *pSd,
    01039                    uint32_t *pStatus,
    01040                    SdmmcCallback fCallback)
    01041 {
    01042     MciCmd *pCommand = &(mciCmd);
    01043     uint8_t error;
    01044
    01045     TRACE_DEBUG("Cmd12()\n\r");
    01046     ResetMciCommand(pCommand);
    01047
    01048     /* Fill command information */
    01049     pCommand->cmd = SDMMC_STOP_TRANSMISSION;
    01050     pCommand->tranType = MCI_STOP_TRANSFER;
    01051     pCommand->resType = 1;
    01052     pCommand->busyCheck = 1;
    01053     pCommand->pResp = pStatus;
    01054
    01055     /* Send command */
    01056     error = SendMciCommand(pSd, fCallback);
    01057     return error;
    01058 }
    01059
    01060 /**
    01061  * Addressed card sends its status register.
    01062  * Returns the command transfer result (see SendMciCommand).
    01063  * \param pSd       Pointer to a SD card driver instance.
    01064  * \param cardAddr  Card Relative Address.
    01065  * \param pStatus   Pointer to a status variable.
    01066  * \param fCallback Pointer to optional callback invoked on command end.
    01067  *                  NULL:    Function return until command finished.
    01068  *                  Pointer: Return immediately and invoke callback at end.
    01069  *                  Callback argument is fixed to a pointer to SdCard instance.
    01070  */
    01071 uint8_t SdmmcCmd13(SdCard *pSd,
    01072                    uint16_t cardAddr,
    01073                    uint32_t *pStatus,
    01074                    SdmmcCallback fCallback)
    01075 {
    01076     MciCmd *pCommand = &(mciCmd);
    01077     uint8_t error;
    01078
    01079     TRACE_DEBUG("Cmd13()\n\r");
    01080     ResetMciCommand(pCommand);
    01081
    01082     /* Fill command information */
    01083     pCommand->cmd = SDMMC_SEND_STATUS;
    01084     pCommand->arg = cardAddr << 16;
    01085     pCommand->resType = 1;
    01086     pCommand->pResp = pStatus;
    01087
    01088     /* Send command */
    01089     error = SendMciCommand(pSd, fCallback);
    01090
    01091     return error;
    01092 }
    01093
    01094 /**
    01095  * In the case of a Standard Capacity SD Memory Card, this command sets the
    01096  * block length (in bytes) for all following block commands
    01097  * (read, write, lock).
    01098  * Default block length is fixed to 512 Bytes.
    01099  * Set length is valid for memory access commands only if partial block read
    01100  * operation are allowed in CSD.
    01101  * In the case of a High Capacity SD Memory Card, block length set by CMD16
    01102  * command does not affect the memory read and write commands. Always 512
    01103  * Bytes fixed block length is used. This command is effective for LOCK_UNLOCK
    01104  * command. In both cases, if block length is set larger than 512Bytes, the
    01105  * card sets the BLOCK_LEN_ERROR bit.
    01106  * \param pSd  Pointer to a SD card driver instance.
    01107  * \param blockLength  Block length in bytes.
    01108  * \param fCallback Pointer to optional callback invoked on command end.
    01109  *                  NULL:    Function return until command finished.
    01110  *                  Pointer: Return immediately and invoke callback at end.
    01111  *                  Callback argument is fixed to a pointer to SdCard instance.
    01112  */
    01113 uint8_t SdmmcCmd16(SdCard *pSd, uint16_t blockLength, SdmmcCallback fCallback)
    01114 {
    01115     MciCmd *pCommand = &(mciCmd);
    01116     uint8_t error;
    01117
    01118     TRACE_DEBUG("Cmd16()\n\r");
    01119     ResetMciCommand(pCommand);
    01120
    01121     /* Fill command information */
    01122     pCommand->cmd = SDMMC_SET_BLOCKLEN;
    01123     pCommand->arg = blockLength;
    01124     pCommand->resType = 1;
    01125
    01126     /* Send command */
    01127     error = SendMciCommand(pSd, fCallback);
    01128
    01129     return error;
    01130 }
    01131
    01132 /**
    01133  * Read single block command
    01134  * \param pSd  Pointer to a SD card driver instance.
    01135  * \param blockSize Block size (shall be set to 512 in case of high capacity).
    01136  * \param pData     Pointer to the DW aligned buffer to be filled.
    01137  * \param address   Data Address on SD/MMC card.
    01138  * \param pStatus   Pointer response buffer as status return.
    01139  * \param fCallback Pointer to optional callback invoked on command end.
    01140  *                  NULL:    Function return until command finished.
    01141  *                  Pointer: Return immediately and invoke callback at end.
    01142  *                  Callback argument is fixed to a pointer to SdCard instance.
    01143  */
    01144 uint8_t SdmmcCmd17(SdCard *pSd,
    01145                    uint16_t blockSize,
    01146                    uint8_t *pData,
    01147                    uint32_t address,
    01148                    uint32_t *pStatus,
    01149                    SdmmcCallback fCallback)
    01150 {
    01151     MciCmd *pCommand = &(mciCmd);
    01152     uint8_t error;
    01153
    01154     assert(pSd);
    01155
    01156     TRACE_DEBUG("Cmd17()\n\r");
    01157     ResetMciCommand(pCommand);
    01158
    01159     /* Fill command information */
    01160     pCommand->cmd = SDMMC_READ_SINGLE_BLOCK;
    01161     pCommand->arg = address;
    01162     pCommand->resType = 1;
    01163     pCommand->pResp = pStatus;
    01164
    01165     pCommand->blockSize = blockSize;
    01166     pCommand->nbBlock = 1;
    01167     pCommand->pData = pData;
    01168
    01169     pCommand->tranType = MCI_START_READ;
    01170
    01171     /* Send command */
    01172     error = SendMciCommand(pSd, fCallback);
    01173     return error;
    01174 }
    01175
    01176 /**
    01177  * Continously transfers datablocks from card to host until interrupted by a
    01178  * STOP_TRANSMISSION command.
    01179  * \param pSd       Pointer to a SD card driver instance.
    01180  * \param blockSize Block size (shall be set to 512 in case of high capacity).
    01181  * \param nbBlocks  Number of blocks to send.
    01182  * \param pData     Pointer to the DW aligned buffer to be filled.
    01183  * \param address   Data Address on SD/MMC card.
    01184  * \param pStatus   Pointer to the response status.
    01185  * \param fCallback Pointer to optional callback invoked on command end.
    01186  *                  NULL:    Function return until command finished.
    01187  *                  Pointer: Return immediately and invoke callback at end.
    01188  *                  Callback argument is fixed to a pointer to SdCard instance.
    01189  */
    01190 uint8_t SdmmcCmd18(SdCard *pSd,
    01191                    uint16_t blockSize,
    01192                    uint16_t nbBlocks,
    01193                    uint8_t *pData,
    01194                    uint32_t address,
    01195                    uint32_t *pStatus,
    01196                    SdmmcCallback fCallback)
    01197 {
    01198     MciCmd *pCommand = &(mciCmd);
    01199     uint8_t error;
    01200
    01201     TRACE_DEBUG("Cmd18()\n\r");
    01202     ResetMciCommand(pCommand);
    01203
    01204     /* Fill command information */
    01205     pCommand->cmd = SDMMC_READ_MULTIPLE_BLOCK;
    01206     pCommand->arg = address;
    01207     pCommand->resType = 1;
    01208     pCommand->pResp = pStatus;
    01209
    01210     pCommand->blockSize = blockSize;
    01211     pCommand->nbBlock = nbBlocks;
    01212     pCommand->pData = pData;
    01213
    01214     pCommand->tranType = MCI_START_READ;
    01215
    01216     /* Send command */
    01217     error = SendMciCommand(pSd, fCallback);
    01218     return error;
    01219 }
    01220
    01221 /**
    01222  * Write single block command
    01223  * \param pSd  Pointer to a SD card driver instance.
    01224  * \param blockSize Block size (shall be set to 512 in case of high capacity).
    01225  * \param pData     Pointer to the DW aligned buffer to be filled.
    01226  * \param address   Data Address on SD/MMC card.
    01227  * \param pStatus   Pointer to response buffer as status.
    01228  * \param fCallback Pointer to optional callback invoked on command end.
    01229  *                  NULL:    Function return until command finished.
    01230  *                  Pointer: Return immediately and invoke callback at end.
    01231  *                  Callback argument is fixed to a pointer to SdCard instance.
    01232  */
    01233 uint8_t SdmmcCmd24(SdCard *pSd,
    01234                    uint16_t blockSize,
    01235                    uint8_t *pData,
    01236                    uint32_t address,
    01237                    uint32_t *pStatus,
    01238                    SdmmcCallback fCallback)
    01239 {
    01240     MciCmd *pCommand = &(mciCmd);
    01241     uint8_t error;
    01242
    01243     assert(pSd);
    01244
    01245     TRACE_DEBUG("Cmd24()\n\r");
    01246     ResetMciCommand(pCommand);
    01247
    01248     /* Fill command information */
    01249     pCommand->cmd = SDMMC_WRITE_BLOCK;
    01250     pCommand->arg = address;
    01251     pCommand->resType = 1;
    01252     pCommand->pResp = pStatus;
    01253
    01254     pCommand->blockSize = blockSize;
    01255     pCommand->nbBlock = 1;
    01256     pCommand->pData = pData;
    01257
    01258     pCommand->tranType = MCI_START_WRITE;
    01259
    01260     /* Send command */
    01261     error = SendMciCommand(pSd, fCallback);
    01262     return error;
    01263 }
    01264
    01265 /**
    01266  * Write multiple block command
    01267  * \param pSd  Pointer to a SD card driver instance.
    01268  * \param blockSize Block size (shall be set to 512 in case of high capacity).
    01269  * \param nbBlock   Number of blocks to send.
    01270  * \param pData     Pointer to the DW aligned buffer to be filled.
    01271  * \param address   Data Address on SD/MMC card.
    01272  * \param pStatus   Pointer to the response buffer as status.
    01273  * \param fCallback Pointer to optional callback invoked on command end.
    01274  *                  NULL:    Function return until command finished.
    01275  *                  Pointer: Return immediately and invoke callback at end.
    01276  *                  Callback argument is fixed to a pointer to SdCard instance.
    01277  */
    01278 uint8_t SdmmcCmd25(SdCard *pSd,
    01279                    uint16_t blockSize,
    01280                    uint16_t nbBlock,
    01281                    uint8_t *pData,
    01282                    uint32_t address,
    01283                    uint32_t *pStatus,
    01284                    SdmmcCallback fCallback)
    01285 {
    01286     MciCmd *pCommand = &(mciCmd);
    01287     uint8_t error;
    01288
    01289     TRACE_DEBUG("Cmd25()\n\r");
    01290     ResetMciCommand(pCommand);
    01291
    01292     /* Fill command information */
    01293     pCommand->cmd = SDMMC_WRITE_MULTIPLE_BLOCK;
    01294     pCommand->arg = address;
    01295     pCommand->resType = 1;
    01296     pCommand->pResp = pStatus;
    01297
    01298     pCommand->blockSize = blockSize;
    01299     pCommand->nbBlock = nbBlock;
    01300     pCommand->pData = pData;
    01301
    01302     pCommand->tranType = MCI_START_WRITE;
    01303     pCommand->busyCheck = 1;
    01304
    01305     /* Send command */
    01306     error = SendMciCommand(pSd, fCallback);
    01307     return error;
    01308 }
    01309
    01310 /**
    01311  * SDIO IO_RW_DIRECT command, response R5.
    01312  * \return the command transfer result (see SendMciCommand).
    01313  * \param pSd       Pointer to a SD card driver instance.
    01314  * \param pIoData   Pointer to input argument (\ref SdioRwDirectArg) and
    01315  *                  response (\ref SdmmcR5) buffer.
    01316  * \param fCallback Pointer to optional callback invoked on command end.
    01317  *                  NULL:    Function return until command finished.
    01318  *                  Pointer: Return immediately and invoke callback at end.
    01319  *                  Callback argument is fixed to a pointer to SdCard instance.
    01320  */
    01321 uint8_t SdioCmd52(SdCard *pSd, uint32_t* pIoData,
    01322                   SdmmcCallback fCallback)
    01323 {
    01324     MciCmd *pCommand = &(mciCmd);
    01325     //SdioRwDirectArg *pArg = (SdioRwDirectArg*)pIoData;
    01326
    01327     TRACE_DEBUG("Cmd52()\n\r");
    01328     ResetMciCommand(pCommand);
    01329
    01330     /* Fill command information */
    01331     pCommand->cmd = SDIO_IO_RW_DIRECT;
    01332     #if 1
    01333     pCommand->arg = *pIoData;
    01334     #else
    01335     pCommand->arg = pArg->data               /* data            7: 0 */
    01336                   | (pArg->regAddress << 9)  /* address        25: 9 */
    01337                   | (pArg->rawFlag << 27)    /* ReadAfterWrite    27 */
    01338                   | (pArg->functionNum << 28)/* FunctionNumber 30:28 */
    01339                   | (pArg->rwFlag << 31);    /* Wr/Rd          31    */
    01340     #endif
    01341     pCommand->resType = 5;
    01342     pCommand->pResp = (uint32_t*)pIoData;
    01343
    01344     /* Send command */
    01345     return SendMciCommand(pSd, fCallback);
    01346 }
    01347
    01348 /**
    01349  * SDIO IO_RW_EXTENDED command, response R5.
    01350  * \param pSd       Pointer to a SD card driver instance.
    01351  * \param pArgResp  Pointer to input argument (\ref SdioRwExtArg)
    01352  *                  and response (\ref SdmmcR5) buffer.
    01353  * \param pData     Pointer to data buffer to transfer.
    01354  * \param size      Transfer data size.
    01355  * \param fCallback Pointer to optional callback invoked on command end.
    01356  *                  NULL:    Function return until command finished.
    01357  *                  Pointer: Return immediately and invoke callback at end.
    01358  *                  Callback argument is fixed to a pointer to SdCard instance.
    01359  * \param pArg Callback argument.
    01360  */
    01361 uint8_t SdioCmd53(SdCard *pSd,
    01362                   uint32_t* pArgResp,
    01363                   uint8_t * pData, uint32_t size,
    01364                   SdmmcCallback fCallback,
    01365                   void* pArg)
    01366 {
    01367     MciCmd *pCommand = &(mciCmd);
    01368     SdioRwExtArg *pCmdArg = (SdioRwExtArg*)pArgResp;
    01369     uint8_t error;
    01370
    01371     TRACE_DEBUG( "Cmd53()\n\r" ) ;
    01372     ResetMciCommand(pCommand);
    01373
    01374     /* Fill command information */
    01375     pCommand->cmd = SDIO_IO_RW_EXTENDED;
    01376     /* Write/Read operation */
    01377     if (pCmdArg->rwFlag) {
    01378         pCommand->tranType = MCI_START_WRITE;
    01379     }
    01380     else {
    01381         pCommand->cmd |= HSMCI_CMDR_TRDIR;
    01382         pCommand->tranType = MCI_START_READ;
    01383     }
    01384     /* Block/Byte mode */
    01385     if (pCmdArg->blockMode) {
    01386         pCommand->cmd |= HSMCI_CMDR_TRTYP_BLOCK
    01387                        | HSMCI_CMDR_TRCMD_START_DATA;
    01388         pCommand->blockSize = SDMMC_BLOCK_SIZE;
    01389     }
    01390     else {
    01391         pCommand->cmd |= HSMCI_CMDR_TRTYP_BYTE
    01392                        | HSMCI_CMDR_TRCMD_START_DATA;
    01393         pCommand->blockSize = 1;
    01394     }
    01395     #if 1
    01396     pCommand->arg = *pArgResp;
    01397     #else
    01398     pCommand->arg = pCmdArg->count              /* data count        8: 0 */
    01399                   | (pCmdArg->regAddress << 9)  /* address          27: 9 */
    01400                   | (pCmdArg->opCode << 26)     /* address Inc/Fixed   26 */
    01401                   | (pCmdArg->blockMode << 27)  /* block mode          27 */
    01402                   | (pCmdArg->functionNum << 28)/* FunctionNumber   30:28 */
    01403                   | (pCmdArg->rwFlag << 31);    /* Wr/Rd            31    */
    01404     #endif
    01405     pCommand->resType = 5;
    01406     pCommand->pResp = pArgResp;
    01407     pCommand->pData = pData;
    01408     pCommand->nbBlock = size;
    01409     /* Callback and its arguments */
    01410     pCommand->callback = fCallback;
    01411     pCommand->pArg     = pArg;
    01412
    01413     /* Send command */
    01414     error = Sdmmc_SendCommand(pSd->pSdDriver, pCommand);
    01415     if (fCallback == 0 && error == 0) {
    01416         while(!Sdmmc_IsCommandComplete(pSd->pSdDriver));
    01417     }
    01418     return error;
    01419 }
    01420
    01421 /**
    01422  * Indicates to the card that the next command is an application specific
    01423  * command rather than a standard command.
    01424  * \return the command transfer result (see SendMciCommand).
    01425  * \param pSd       Pointer to a SD card driver instance.
    01426  * \param cardAddr  Card Relative Address.
    01427  * \param fCallback Pointer to optional callback invoked on command end.
    01428  *                  NULL:    Function return until command finished.
    01429  *                  Pointer: Return immediately and invoke callback at end.
    01430  *                  Callback argument is fixed to a pointer to SdCard instance.
    01431  */
    01432 uint8_t SdmmcCmd55(SdCard *pSd, uint16_t cardAddr, SdmmcCallback fCallback)
    01433 {
    01434     MciCmd *pCommand = &(mciCmd);
    01435     uint8_t error;
    01436
    01437     TRACE_DEBUG( "Cmd55()\n\r" ) ;
    01438     ResetMciCommand(pCommand);
    01439
    01440     /* Fill command information */
    01441     pCommand->cmd = SDMMC_APP_CMD;
    01442     //if (addr == 0)  pCommand->cmd |= HSMCI_CMDR_OPDCMD;
    01443     pCommand->arg = cardAddr << 16;
    01444     pCommand->resType = 1;
    01445
    01446     /* Send command */
    01447     error = SendMciCommand(pSd, fCallback);
    01448     return error;
    01449 }
    01450
    01451 /**
    01452  * Defines the data bus width (00=1bit or 10=4 bits bus) to be used for data
    01453  * transfer.
    01454  * The allowed data bus widths are given in SCR register.
    01455  * Should be invoked after SdmmcCmd55().
    01456  * \param pSd     Pointer to a SD card driver instance.
    01457  * \param arg     Argument for this command.
    01458  * \param pStatus Pointer to buffer for command response as status.
    01459  * \param fCallback Pointer to optional callback invoked on command end.
    01460  *                  NULL:    Function return until command finished.
    01461  *                  Pointer: Return immediately and invoke callback at end.
    01462  *                  Callback argument is fixed to a pointer to SdCard instance.
    01463  * \return the command transfer result (see SendMciCommand).
    01464  */
    01465 uint8_t SdAcmd6(SdCard *pSd,
    01466                 uint32_t arg,
    01467                 uint32_t *pStatus,
    01468                 SdmmcCallback fCallback)
    01469 {
    01470     MciCmd *pCommand = &(mciCmd);
    01471
    01472     TRACE_DEBUG( "Acmd6()\n\r" ) ;
    01473     ResetMciCommand(pCommand);
    01474
    01475     /* Fill command information */
    01476     pCommand->cmd = SD_SET_BUS_WIDTH;
    01477     pCommand->arg = arg;
    01478     pCommand->resType = 1;
    01479     pCommand->pResp = pStatus;
    01480
    01481     /* Send command */
    01482     return SendMciCommand(pSd, fCallback);
    01483 }
    01484
    01485 /**
    01486  * The SD Status contains status bits that are related to the SD memory Card
    01487  * proprietary features and may be used for future application-specific usage.
    01488  * Can be sent to a card only in 'tran_state'.
    01489  * Should be invoked after SdmmcCmd55().
    01490  * \param pSd       Pointer to a SD card driver instance.
    01491  * \param pSdSTAT   Pointer to buffer for SD STATUS data.
    01492  * \param fCallback Pointer to optional callback invoked on command end.
    01493  *                  NULL:    Function return until command finished.
    01494  *                  Pointer: Return immediately and invoke callback at end.
    01495  *                  Callback argument is fixed to a pointer to SdCard instance.
    01496  * \return the command transfer result (see SendMciCommand).
    01497  */
    01498 uint8_t SdAcmd13(SdCard *pSd,
    01499                  uint32_t * pSdSTAT,
    01500                  SdmmcCallback fCallback)
    01501 {
    01502     MciCmd *pCommand = &(mciCmd);
    01503     uint8_t error;
    01504
    01505     TRACE_DEBUG( "Acmd13()\n\r" ) ;
    01506     ResetMciCommand(pCommand);
    01507
    01508     /* Fill command information */
    01509     pCommand->cmd = SD_SD_STATUS;
    01510     pCommand->resType = 1;
    01511
    01512     pCommand->blockSize = 512 / 8;
    01513     pCommand->nbBlock = 1;
    01514     pCommand->pData = (uint8_t*)pSdSTAT;
    01515
    01516     pCommand->tranType = MCI_START_READ;
    01517
    01518     /* Send command */
    01519     error = SendMciCommand(pSd, fCallback);
    01520     return error;
    01521 }
    01522
    01523 /**
    01524  * Asks to all cards to send their operations conditions.
    01525  * Returns the command transfer result (see SendMciCommand).
    01526  * Should be invoked after SdmmcCmd55().
    01527  * \param pSd   Pointer to a SD card driver instance.
    01528  * \param pOpIo Pointer to argument that should be sent and OCR content.
    01529  * \param fCallback Pointer to optional callback invoked on command end.
    01530  *                  NULL:    Function return until command finished.
    01531  *                  Pointer: Return immediately and invoke callback at end.
    01532  *                  Callback argument is fixed to a pointer to SdCard instance.
    01533  */
    01534 uint8_t SdAcmd41(SdCard *pSd, uint32_t* pOpIo, SdmmcCallback fCallback)
    01535 {
    01536     MciCmd *pCommand = &(mciCmd);
    01537
    01538     assert(pSd);
    01539     assert(pOpIo);
    01540
    01541     TRACE_DEBUG( "Acmd41()\n\r" ) ;
    01542     ResetMciCommand(pCommand);
    01543
    01544     /* Fill command information */
    01545     pCommand->cmd = SD_SD_SEND_OP_COND;
    01546     pCommand->arg = *pOpIo;
    01547     pCommand->resType = 3;
    01548     pCommand->pResp = pOpIo;
    01549
    01550     return SendMciCommand(pSd, fCallback);
    01551 }
    01552
    01553 /**
    01554  * SD Card Configuration Register (SCR) provides information on the SD Memory
    01555  * Card's special features that were configured into the given card. The size
    01556  * of SCR register is 64 bits.
    01557  * Should be invoked after SdmmcCmd55().
    01558  * \param pSd   Pointer to a SD card driver instance.
    01559  * \param pSCR  Pointer to buffer for SCR content.
    01560  * \param fCallback Pointer to optional callback invoked on command end.
    01561  *                  NULL:    Function return until command finished.
    01562  *                  Pointer: Return immediately and invoke callback at end.
    01563  *                  Callback argument is fixed to a pointer to SdCard instance.
    01564  */
    01565 uint8_t SdAcmd51(SdCard *pSd, uint32_t * pSCR, SdmmcCallback fCallback)
    01566 {
    01567     MciCmd *pCommand = &(mciCmd);
    01568     uint8_t error;
    01569
    01570     assert(pSd);
    01571
    01572     TRACE_DEBUG( "Acmd51()\n\r" ) ;
    01573     ResetMciCommand(pCommand);
    01574
    01575     /* Fill command information */
    01576     pCommand->cmd = SD_SEND_SCR;
    01577     pCommand->resType = 1;
    01578
    01579     pCommand->blockSize = 64 / 8;
    01580     pCommand->nbBlock = 1;
    01581     pCommand->pData = (uint8_t*)pSCR;
    01582
    01583     pCommand->tranType = MCI_START_READ;
    01584
    01585     /* Send command */
    01586     error = SendMciCommand(pSd, fCallback);
    01587     return error;
    01588 }
    01589
    01590 /**
    01591  * Continue to transfer datablocks from card to host until interrupted by a
    01592  * STOP_TRANSMISSION command.
    01593  * \param pSd  Pointer to a SD card driver instance.
    01594  * \param blockSize Block size (shall be set to 512 in case of high capacity).
    01595  * \param nbBlock   Number of blocks to send.
    01596  * \param pData     Pointer to the application buffer to be filled.
    01597  * \param fCallback Pointer to optional callback invoked on command end.
    01598  *                  NULL:    Function return until command finished.
    01599  *                  Pointer: Return immediately and invoke callback at end.
    01600  * \param pArg Callback argument.
    01601  */
    01602 uint8_t SdmmcRead(SdCard   *pSd,
    01603                   uint16_t blockSize,
    01604                   uint16_t nbBlock,
    01605                   uint8_t  *pData,
    01606                   SdmmcCallback fCallback,
    01607                   void* pArg)
    01608 {
    01609     MciCmd *pCommand = &(mciCmd);
    01610     uint8_t error;
    01611
    01612     TRACE_DEBUG( "Read()\n\r" ) ;
    01613     ResetMciCommand(pCommand);
    01614
    01615     /* Fill command information */
    01616     pCommand->blockSize = blockSize;
    01617     pCommand->nbBlock   = nbBlock;
    01618     pCommand->pData     = pData;
    01619
    01620     pCommand->tranType = MCI_READ;
    01621
    01622     pCommand->callback = fCallback;
    01623     pCommand->pArg     = pArg;
    01624
    01625     /* Send command */
    01626     error = Sdmmc_SendCommand(pSd->pSdDriver, pCommand);
    01627     if (fCallback == 0 && error == 0) {
    01628         while(!Sdmmc_IsCommandComplete(pSd->pSdDriver));
    01629     }
    01630     return error;
    01631 }
    01632
    01633 /**
    01634  * Continue to transfer datablocks from host to card until interrupted by a
    01635  * STOP_TRANSMISSION command.
    01636  * \param pSd  Pointer to a SD card driver instance.
    01637  * \param blockSize Block size (shall be set to 512 in case of high capacity).
    01638  * \param nbBlock   Number of blocks to send.
    01639  * \param pData  Pointer to the application buffer to be filled.
    01640  * \param fCallback Pointer to optional callback invoked on command end.
    01641  *                  NULL:    Function return until command finished.
    01642  *                  Pointer: Return immediately and invoke callback at end.
    01643  * \param pArg Callback argument.
    01644  */
    01645 uint8_t SdmmcWrite(SdCard   *pSd,
    01646                    uint16_t blockSize,
    01647                    uint16_t nbBlock,
    01648                    const uint8_t *pData,
    01649                    SdmmcCallback fCallback,
    01650                    void* pArg)
    01651 {
    01652     MciCmd *pCommand = &(mciCmd);
    01653     uint8_t error;
    01654
    01655     TRACE_DEBUG( "Write()\n\r" ) ;
    01656     ResetMciCommand(pCommand);
    01657
    01658     /* Fill command information */
    01659     pCommand->blockSize = blockSize;
    01660     pCommand->nbBlock   = nbBlock;
    01661     pCommand->pData     = (uint8_t*)pData;
    01662
    01663     pCommand->tranType = MCI_WRITE;
    01664     pCommand->busyCheck = 1;
    01665
    01666     pCommand->callback = fCallback;
    01667     pCommand->pArg     = pArg;
    01668
    01669     /* Send command */
    01670     error = Sdmmc_SendCommand(pSd->pSdDriver, pCommand);
    01671     if (fCallback == 0 && error == 0) {
    01672         while(!Sdmmc_IsCommandComplete(pSd->pSdDriver));
    01673     }
    01674     return error;
    01675 }
    01676
    01677 /**
    01678  * Return different properties of the SD/MMC low level driver.
    01679  * \param pSd       Pointer to \ref SdCard instance.
    01680  * \param property  Property code (\ref sdmmc_properties) that should return.
    01681  * \param pExtData  Pointer to additional data for the property.
    01682  */
    01683 uint32_t SdmmcGetProperty(SdCard * pSd,
    01684                           uint32_t property,
    01685                           void * pExtData)
    01686 {
    01687     /* Stop warning */
    01688     pSd = pSd;
    01689     pExtData = pExtData;
    01690
    01691     switch(property) {
    01692         case SDMMC_PROP_BUS_MODE:
    01693             return SDMMC_BUS_4_BIT;
    01694         case SDMMC_PROP_HS_MODE:
    01695             return 1;
    01696         case SDMMC_PROP_BOOT_MODE:
    01697             return 0;
    01698     }
    01699     return 0;
    01700 }
    01701
    01702 /**
    01703  * Setup HW to use the desired slot.
    01704  * \param pSd  Pointer to SdCard instance.
    01705  * \param slot Desired slot.
    01706  */
    01707 uint8_t SdmmcSetSlot(SdCard * pSd, uint8_t slot)
    01708 {
    01709     /* Stop warning */
    01710     pSd = pSd;
    01711     slot = slot;
    01712
    01713     return 0;
    01714 }
    01715
    01716 /**
    01717  * Setup HW to use the desired bus width.
    01718  * \param pSd Pointer to SdCard instance.
    01719  * \param busWidth Desired bus width.
    01720  */
    01721 uint32_t SdmmcSetBusWidth(SdCard * pSd, uint32_t busWidth)
    01722 {
    01723     switch(busWidth) {
    01724     case SDMMC_BUS_4_BIT: busWidth = HSMCI_SDCR_SDCBUS_4; break;
    01725     case SDMMC_BUS_8_BIT: busWidth = HSMCI_SDCR_SDCBUS_8; break;
    01726     default: busWidth = HSMCI_SDCR_SDCBUS_1;
    01727     }
    01728     return MCI_SetBusWidth(pSd->pSdDriver, busWidth);
    01729 }
    01730
    01731 /**
    01732  * Setup HW to enable High-Speed mode
    01733  * \param pSd Pointer to SdCard instance.
    01734  * \param enable 1 to enable, 0 to disable.
    01735  */
    01736 uint8_t SdmmcEnableHsMode(SdCard * pSd, uint8_t enable)
    01737 {
    01738     return MCI_EnableHsMode(pSd->pSdDriver, enable);
    01739 }
    01740
    01741 /**
    01742  * Setup HW to change to desired clock speed.
    01743  * \param pSd Pointer to SdCard instance.
    01744  * \param clk Clock frequence in Hz.
    01745  * \return Actually running clock.
    01746  */
    01747 uint32_t SdmmcSetSpeed(SdCard * pSd,uint32_t clk)
    01748 {
    01749     return MCI_SetSpeed(pSd->pSdDriver, clk, BOARD_MCK);
    01750 }
    01751
    01752 /**
    01753  * Starts a MCI  transfer. This is a non blocking function. It will return
    01754  * as soon as the transfer is started.
    01755  * Return 0 if successful; otherwise returns MCI_ERROR_LOCK if the driver is
    01756  * already in use.
    01757  * \param pMci  Pointer to an MCI driver instance.
    01758  * \param pCommand  Pointer to the command to execute.
    01759  */
    01760 uint8_t Sdmmc_SendCommand(Mcid*pMci, MciCmd *pCommand)
    01761 {
    01762     Hsmci* pMciHw = pMci->pMciHw;
    01763     uint32_t mciIer = 0, mciMr;
    01764     uint32_t transSize;
    01765
    01766     assert(pMci);
    01767     assert(pMciHw);
    01768     assert(pCommand);
    01769
    01770     /* Try to acquire the MCI semaphore */
    01771     if (pMci->semaphore == 0)
    01772     {
    01773         return SDMMC_ERROR_BUSY;
    01774     }
    01775     pMci->semaphore--;
    01776
    01777     /* Command is now being executed */
    01778     pMci->pCommand = pCommand;
    01779     pCommand->state = SDMMC_CMD_PENDING;
    01780
    01781     /* Enable the MCI peripheral clock */
    01782     PMC_EnablePeripheral(pMci->mciId);
    01783
    01784 #if 1
    01785     /* Wait for NOTBUSY (DATA bus IDLE) */
    01786     if (pCommand->tranType == MCI_STOP_TRANSFER)
    01787     {
    01788         volatile uint32_t busyWait = 0x00001000;
    01789
    01790         while ( busyWait-- )
    01791         {
    01792           uint32_t sr = pMciHw->HSMCI_SR;
    01793
    01794           if ((sr & HSMCI_SR_NOTBUSY) && ((sr & HSMCI_SR_DTIP) == 0))
    01795           {
    01796               break;
    01797           }
    01798         }
    01799     }
    01800 #endif
    01801
    01802     /* Disable MCI */
    01803     MCI_Disable(pMciHw);
    01804
    01805     /* Prepare Default Mode register value */
    01806     mciMr = pMciHw->HSMCI_MR & (~(HSMCI_MR_WRPROOF
    01807                                   |HSMCI_MR_RDPROOF
    01808                                   |HSMCI_MR_BLKLEN_Msk
    01809                                   |HSMCI_MR_PDCMODE
    01810                                   |HSMCI_MR_FBYTE));
    01811
    01812     /* Stop transfer: idle the bus */
    01813     if (pCommand->tranType == MCI_STOP_TRANSFER)
    01814     {
    01815         pMciHw->HSMCI_MR = mciMr;
    01816         mciIer = HSMCI_IER_XFRDONE | STATUS_ERRORS_RESP;
    01817     }
    01818     /* No data transfer: stop at the end of the command */
    01819     else if (pCommand->tranType == MCI_NO_TRANSFER)
    01820     {
    01821         pMciHw->HSMCI_MR = mciMr;
    01822         mciIer = HSMCI_IER_XFRDONE | STATUS_ERRORS_RESP;
    01823         /* R3 response, no CRC check */
    01824         if (pCommand->resType == 3)
    01825         {
    01826             mciIer &= ~((uint32_t)HSMCI_IER_RCRCE);
    01827         }
    01828     }
    01829     /* Data command but no following:
    01830        Start an infinite block transfer */
    01831     else
    01832         if (pCommand->nbBlock == 0)
    01833     {
    01834         /* Block size... Data length 0: no PDC */
    01835         pMciHw->HSMCI_MR = mciMr | HSMCI_MR_WRPROOF
    01836                                  | HSMCI_MR_RDPROOF;
    01837         pMciHw->HSMCI_BLKR = (pCommand->blockSize << 16);
    01838         mciIer = HSMCI_IER_CMDRDY | STATUS_ERRORS_RESP;
    01839     }
    01840     /* Command with DATA stage */
    01841     else
    01842     {
    01843         /* Check data size */
    01844         if (pCommand->nbBlock == 0)
    01845         {
    01846             TRACE_ERROR("MCICmd: At least one block required\n\r");
    01847             return SDMMC_ERROR_PARAM;
    01848         }
    01849
    01850         /* Number of blocks to transfer */
    01851         if (   pCommand->tranType == MCI_START_WRITE
    01852             || pCommand->tranType == MCI_START_READ)
    01853         {
    01854             /* Set number of blocks */
    01855             pMciHw->HSMCI_BLKR = (pCommand->blockSize << 16)
    01856                                | (pCommand->nbBlock   <<  0);
    01857         }
    01858
    01859         /* Force byte transfer */
    01860         transSize = (pCommand->nbBlock * pCommand->blockSize);
    01861         if (pCommand->blockSize & 0x3)
    01862         {
    01863             mciMr |= HSMCI_MR_FBYTE;
    01864         }
    01865         else
    01866         {
    01867             transSize = toWCOUNT(transSize);
    01868         }
    01869
    01870         /* Set block size & enable PDC */
    01871         pMciHw->HSMCI_MR = mciMr | HSMCI_MR_WRPROOF
    01872                                  | HSMCI_MR_RDPROOF
    01873                                  | HSMCI_MR_PDCMODE
    01874                                  | (pCommand->blockSize << 16);
    01875
    01876         /* Update PDC for write */
    01877         if (   pCommand->tranType == MCI_START_WRITE
    01878             || pCommand->tranType == MCI_WRITE )
    01879         {
    01880             pMciHw->HSMCI_TPR = (uint32_t)pCommand->pData;
    01881             pMciHw->HSMCI_TCR = transSize;
    01882             pMciHw->HSMCI_TNCR = 0;
    01883
    01884             if (pCommand->tranType == MCI_START_WRITE)
    01885             {
    01886                 mciIer = (uint32_t)(HSMCI_IER_XFRDONE | STATUS_ERRORS_DATA);
    01887             }
    01888             else
    01889             {
    01890                 mciIer = (uint32_t)(HSMCI_IER_TXBUFE | STATUS_ERRORS_DATA);
    01891             }
    01892         }
    01893         /* Update PDC for read */
    01894         else
    01895         {
    01896             pMciHw->HSMCI_RPR = (uint32_t)pCommand->pData;
    01897             pMciHw->HSMCI_RCR = transSize;
    01898             pMciHw->HSMCI_RNCR = 0;
    01899             pMciHw->HSMCI_PTCR = HSMCI_PTCR_RXTEN;
    01900             if (pCommand->tranType == MCI_START_READ)
    01901             {
    01902                 mciIer = (uint32_t)(HSMCI_IER_XFRDONE | STATUS_ERRORS_DATA);
    01903             }
    01904             else
    01905             {
    01906                 mciIer = (uint32_t)(HSMCI_IER_RXBUFF | STATUS_ERRORS_DATA);
    01907             }
    01908         }
    01909     }
    01910
    01911     /* Enable MCI */
    01912     MCI_Enable(pMciHw);
    01913
    01914     /* Send the command */
    01915     if ( (pCommand->tranType != MCI_WRITE && pCommand->tranType != MCI_READ) || (pCommand->blockSize == 0) )
    01916     {
    01917         pMciHw->HSMCI_ARGR = pCommand->arg;
    01918         pMciHw->HSMCI_CMDR = pCommand->cmd;
    01919     }
    01920
    01921     /* Start sending PDC */
    01922     if (pCommand->blockSize > 0
    01923         && (pCommand->tranType == MCI_START_WRITE
    01924             || (pCommand->tranType == MCI_WRITE)) )
    01925     {
    01926         pMciHw->HSMCI_PTCR = HSMCI_PTCR_TXTEN;
    01927     }
    01928
    01929     /* Ignore CRC error for R3 & R4 */
    01930     if (pCommand->resType == 3 || pCommand->resType == 4)
    01931     {
    01932         mciIer &= ~(uint32_t)(HSMCI_IER_RCRCE);
    01933     }
    01934
    01935     /* Ignore data error on stop transfer command :) */
    01936     if (pCommand->tranType == MCI_STOP_TRANSFER)
    01937     {
    01938         mciIer &= ~(STATUS_ERRORS_DATA);
    01939     }
    01940
    01941     /* Interrupt enable shall be done after PDC TXTEN and RXTEN */
    01942     pMciHw->HSMCI_IER = mciIer;
    01943
    01944     return 0;
    01945 }
    01946
    01947
    01948
    01949 /**
    01950  * Processes pending events on the given MCI driver.
    01951  * \param pMci  Pointer to a MCI driver instance.
    01952  */
    01953 void Sdmmc_Handler(Mcid*pMci)
    01954 {
    01955     Hsmci *pMciHw = pMci->pMciHw;
    01956     MciCmd *pCommand = pMci->pCommand;
    01957     uint32_t SR;
    01958     volatile uint32_t maskedSR;
    01959     uint32_t mask;
    01960     uint8_t i;
    01961
    01962     assert(pMci);
    01963     assert(pMciHw);
    01964     assert(pCommand);
    01965
    01966     /* Read the status register */
    01967     SR = pMciHw->HSMCI_SR;
    01968     mask = pMciHw->HSMCI_IMR;
    01969     maskedSR = SR & mask;
    01970
    01971     //TRACE_INFO_WP("i%dS %x\n\r", (pCommand->cmd & HSMCI_CMDR_CMDNB_Msk), SR);
    01972     //TRACE_INFO_WP("i%dM %x\n\r", (pCommand->cmd & HSMCI_CMDR_CMDNB_Msk), maskedSR);
    01973
    01974     /* Check if an error has occured */
    01975     if ((maskedSR & STATUS_ERRORS) != 0)
    01976     {
    01977         pCommand->state = SDMMC_CMD_ERROR;
    01978         /* Check error code */
    01979         if ((maskedSR & STATUS_ERRORS) == HSMCI_SR_RTOE)
    01980         {
    01981             pCommand->status = SDMMC_ERROR_NORESPONSE;
    01982         }
    01983         else
    01984         {
    01985             TRACE_INFO( "iERR %x: C%x(%d), R%d\n\r", maskedSR, pCommand->cmd, (pCommand->cmd & HSMCI_CMDR_CMDNB_Msk), pCommand->resType ) ;
    01986             /* If the command is SEND_OP_COND the CRC error flag is always
    01987                present (cf: R3,R4 response, no CRC) */
    01988             if (   ((pCommand->resType == 3)||(pCommand->resType == 4))
    01989                 && (maskedSR & STATUS_ERRORS) == HSMCI_SR_RCRCE)
    01990             {
    01991             }
    01992             else
    01993             {
    01994                 pCommand->status = SDMMC_ERROR;
    01995             }
    01996         }
    01997     }
    01998     mask &= ~STATUS_ERRORS;
    01999
    02000     /* Check if a command has been completed */
    02001     if (maskedSR & HSMCI_SR_CMDRDY)
    02002     {
    02003         /* Command OK, disable interrupt */
    02004         pMciHw->HSMCI_IDR = HSMCI_IDR_CMDRDY;
    02005
    02006         /* Check BUSY */
    02007         if (pCommand->busyCheck
    02008            && (pCommand->tranType == MCI_NO_TRANSFER
    02009               || (pCommand->tranType == MCI_STOP_TRANSFER)) )
    02010         {
    02011             /* Check XFRDONE to confirm no bus action */
    02012             if (SR & HSMCI_SR_NOTBUSY)
    02013             {
    02014                 mask &= ~(uint32_t)HSMCI_IMR_CMDRDY;
    02015             }
    02016             else
    02017             {
    02018                 pMciHw->HSMCI_IER = HSMCI_SR_NOTBUSY;
    02019             }
    02020         }
    02021         else
    02022         {
    02023             mask &= ~(uint32_t)HSMCI_IMR_CMDRDY;
    02024         }
    02025     }
    02026
    02027     /* Check if transfer stopped */
    02028     if (maskedSR & HSMCI_SR_XFRDONE)
    02029     {
    02030         mask &= ~(uint32_t)HSMCI_IMR_XFRDONE;
    02031     }
    02032
    02033     /* Check if not busy */
    02034     if (maskedSR & HSMCI_SR_NOTBUSY)
    02035     {
    02036         mask &= ~(uint32_t)HSMCI_IMR_NOTBUSY;
    02037     }
    02038
    02039     /* Check if data read done */
    02040     if (maskedSR & HSMCI_SR_RXBUFF)
    02041     {
    02042         mask &= ~(uint32_t)HSMCI_IMR_RXBUFF;
    02043     }
    02044
    02045     /* Check if TX ready */
    02046     if (maskedSR & HSMCI_SR_TXRDY)
    02047     {
    02048         mask &= ~(uint32_t)HSMCI_IMR_TXRDY;
    02049     }
    02050
    02051     /* Check if FIFO empty (TX & All data sent) */
    02052     if (maskedSR & HSMCI_SR_FIFOEMPTY)
    02053     {
    02054         /* Disable FIFO EMPTY */
    02055         pMciHw->HSMCI_IDR = HSMCI_SR_FIFOEMPTY;
    02056         /* Byte transfer no BLKE */
    02057         if (pMciHw->HSMCI_MR & HSMCI_MR_FBYTE)
    02058         {
    02059             mask &= ~(uint32_t)HSMCI_IMR_FIFOEMPTY;
    02060         }
    02061         /* End command until no data on bus */
    02062         else
    02063         {
    02064             if ((SR & HSMCI_SR_BLKE) == 0)
    02065             {
    02066                 pMciHw->HSMCI_IER = HSMCI_IER_BLKE;
    02067             }
    02068             else
    02069             {
    02070                 mask &= ~(uint32_t)HSMCI_IMR_FIFOEMPTY;
    02071             }
    02072         }
    02073     }
    02074
    02075     /* Check if BLKE (TX & no data on bus) */
    02076     if (maskedSR & HSMCI_SR_BLKE)
    02077     {
    02078         if (SR & HSMCI_SR_TXRDY )
    02079         {
    02080             mask &= ~(uint32_t)HSMCI_SR_BLKE;
    02081         }
    02082         else
    02083         {
    02084             pMciHw->HSMCI_IDR = HSMCI_SR_BLKE;
    02085             pMciHw->HSMCI_IER = HSMCI_SR_TXRDY;
    02086         }
    02087     }
    02088
    02089     /* Check if data write finished */
    02090     if (maskedSR & HSMCI_SR_TXBUFE)
    02091     {
    02092         /* Data OK, disable interrupt */
    02093         pMciHw->HSMCI_IDR = HSMCI_IDR_TXBUFE;
    02094
    02095         /* End command until no data on bus */
    02096         if ((SR & HSMCI_SR_FIFOEMPTY) == 0)
    02097         {
    02098             pMciHw->HSMCI_IER = HSMCI_IER_FIFOEMPTY;
    02099         }
    02100         else
    02101         {
    02102             mask &= ~(uint32_t)HSMCI_SR_TXBUFE;
    02103         }
    02104     }
    02105
    02106     /* All non-error mask done, complete the command */
    02107     if (0 == mask || pCommand->state != SDMMC_CMD_PENDING)
    02108     {
    02109         /* Store the card response in the provided buffer */
    02110         if (pCommand->pResp)
    02111         {
    02112             uint8_t resSize;
    02113             switch (pCommand->resType)
    02114             {
    02115                 case 1:
    02116                 case 3:
    02117                 case 4:
    02118                 case 5:
    02119                 case 6:
    02120                 case 7:
    02121                     resSize = 1;
    02122                 break;
    02123
    02124                 case 2:
    02125                     resSize = 4;
    02126                 break;
    02127
    02128                 default:
    02129                     resSize = 0;
    02130                 break;
    02131             }
    02132
    02133             for (i=0; i < resSize; i++)
    02134             {
    02135                 pCommand->pResp[i] = pMciHw->HSMCI_RSPR[0];
    02136             }
    02137         }
    02138
    02139         /* If no error occured, the transfer is successful */
    02140         if (pCommand->state == SDMMC_CMD_PENDING)
    02141         {
    02142             pCommand->state = 0;
    02143         }
    02144         /* Error ? Reset to remove transfer statuses */
    02145         else
    02146         {
    02147             MCI_Reset(pMci, 1);
    02148         }
    02149
    02150         /* Disable PDC */
    02151         pMciHw->HSMCI_PTCR = HSMCI_PTCR_RXTDIS | HSMCI_PTCR_TXTDIS;
    02152
    02153         /* Disable interrupts */
    02154         pMciHw->HSMCI_IDR = pMciHw->HSMCI_IMR;
    02155
    02156         /* Disable peripheral */
    02157         PMC_DisablePeripheral(pMci->mciId);
    02158
    02159         /* Release the semaphore */
    02160         pMci->semaphore++;
    02161
    02162         /* Invoke the callback associated with the current command (if any) */
    02163         if ( pCommand->callback )
    02164         {
    02165             (pCommand->callback)(pCommand->status, pCommand->pArg);
    02166         }
    02167     }
    02168 }
    02169
    02170
    02171 /**
    02172  * Returns 1 if the given MCI transfer is complete; otherwise returns 0.
    02173  * \param pMci  Pointer to a Mcid instance.
    02174  */
    02175 uint8_t Sdmmc_IsCommandComplete( Mcid *pMci )
    02176 {
    02177     MciCmd *pCommand = pMci->pCommand;
    02178
    02179     if ( pMci->semaphore == 0 )
    02180     {
    02181         return 0;
    02182     }
    02183
    02184     if ( pCommand )
    02185     {
    02186         if ( pCommand->state == SDMMC_CMD_PENDING )
    02187         {
    02188             return 0;
    02189         }
    02190     }
    02191     return 1;
    02192 }
    02193
    02194
    02195 /**@}*/

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines