Subdirectory provided by Embedded Artists

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos mbed-src

Dependents:   lpc4088_displaymodule_hello_world_Sept_2018

Fork of DMSupport by Embedded Artists

Committer:
embeddedartists
Date:
Wed Apr 15 09:41:56 2015 +0200
Revision:
38:420cdc281467
Parent:
31:d47cffcb0a3e
Child:
40:6df4f63aa406
- Added a new class GPDMA to allow multiple users of DMA
- Modified the MCI file system to use the new GPDMA class
- Updated ethernet/rtos/mbed/usbdevice library versions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:6b68dac0d986 1 /*
embeddedartists 9:a33326afd686 2 * Copyright 2014 Embedded Artists AB
embeddedartists 0:6b68dac0d986 3 *
embeddedartists 0:6b68dac0d986 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 0:6b68dac0d986 5 * you may not use this file except in compliance with the License.
embeddedartists 0:6b68dac0d986 6 * You may obtain a copy of the License at
embeddedartists 0:6b68dac0d986 7 *
embeddedartists 0:6b68dac0d986 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 0:6b68dac0d986 9 *
embeddedartists 0:6b68dac0d986 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 0:6b68dac0d986 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 0:6b68dac0d986 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 0:6b68dac0d986 13 * See the License for the specific language governing permissions and
embeddedartists 0:6b68dac0d986 14 * limitations under the License.
embeddedartists 0:6b68dac0d986 15 */
embeddedartists 0:6b68dac0d986 16
embeddedartists 0:6b68dac0d986 17 /******************************************************************************
embeddedartists 0:6b68dac0d986 18 * Includes
embeddedartists 0:6b68dac0d986 19 *****************************************************************************/
embeddedartists 0:6b68dac0d986 20
embeddedartists 0:6b68dac0d986 21 #include "MCIFileSystem.h"
embeddedartists 0:6b68dac0d986 22 #include "mbed_debug.h"
embeddedartists 31:d47cffcb0a3e 23 #include "rtos.h"
embeddedartists 0:6b68dac0d986 24
embeddedartists 0:6b68dac0d986 25 #include "diskio.h" //STA_* defines
embeddedartists 0:6b68dac0d986 26
embeddedartists 0:6b68dac0d986 27 /******************************************************************************
embeddedartists 0:6b68dac0d986 28 * Defines and typedefs
embeddedartists 0:6b68dac0d986 29 *****************************************************************************/
embeddedartists 0:6b68dac0d986 30
embeddedartists 0:6b68dac0d986 31 #define MCI_DBG 0
embeddedartists 0:6b68dac0d986 32
embeddedartists 0:6b68dac0d986 33 #define CMD_TIMEOUT (0x10000)
embeddedartists 0:6b68dac0d986 34
embeddedartists 0:6b68dac0d986 35 #define DATA_TIMER_VALUE_R (SDC_TRAN_CLOCK_RATE / 4) // 250ms
embeddedartists 0:6b68dac0d986 36 #define DATA_TIMER_VALUE_W (SDC_TRAN_CLOCK_RATE) // 1000ms
embeddedartists 31:d47cffcb0a3e 37 #define ACQUIRE_DELAY (100) /*!< inter-command acquire oper condition delay in milliseconds */
embeddedartists 0:6b68dac0d986 38
embeddedartists 0:6b68dac0d986 39 #define SYSCTL_CLOCK_SDC (1<<28)
embeddedartists 0:6b68dac0d986 40
embeddedartists 0:6b68dac0d986 41 /**
embeddedartists 0:6b68dac0d986 42 * @brief SDC Clear Register bit definitions
embeddedartists 0:6b68dac0d986 43 */
embeddedartists 0:6b68dac0d986 44 /** Clear all status flag*/
embeddedartists 0:6b68dac0d986 45 #define SDC_CLEAR_ALL ((uint32_t) 0x7FF)
embeddedartists 0:6b68dac0d986 46
embeddedartists 0:6b68dac0d986 47 /*
embeddedartists 0:6b68dac0d986 48 * SDMMC Card bus clock rate definitions
embeddedartists 0:6b68dac0d986 49 */
embeddedartists 0:6b68dac0d986 50 /** Card bus clock in Card Identification Mode */
embeddedartists 0:6b68dac0d986 51 #define SDC_IDENT_CLOCK_RATE (400000) /* 400KHz */
embeddedartists 0:6b68dac0d986 52 /** Card bus clock in Data Transfer Mode */
embeddedartists 0:6b68dac0d986 53 #define SDC_TRAN_CLOCK_RATE (20000000) /* 20MHz */
embeddedartists 0:6b68dac0d986 54
embeddedartists 0:6b68dac0d986 55 /**
embeddedartists 0:6b68dac0d986 56 * @brief SDC Power Control Register bit definitions
embeddedartists 0:6b68dac0d986 57 */
embeddedartists 0:6b68dac0d986 58 /** SD_CMD Output Control */
embeddedartists 0:6b68dac0d986 59 #define SDC_PWR_OPENDRAIN (((uint32_t) 1) << 6)
embeddedartists 0:6b68dac0d986 60
embeddedartists 0:6b68dac0d986 61 /**
embeddedartists 0:6b68dac0d986 62 * @brief SDC Command Register bit definitions
embeddedartists 0:6b68dac0d986 63 */
embeddedartists 0:6b68dac0d986 64 /** SDC Command Register Bitmask */
embeddedartists 0:6b68dac0d986 65 #define SDC_COMMAND_BITMASK ((uint32_t) 0x7FF)
embeddedartists 0:6b68dac0d986 66 /** SDC Command Index Bitmask */
embeddedartists 0:6b68dac0d986 67 #define SDC_COMMAND_INDEX_BITMASK ((uint32_t) 0x3F)
embeddedartists 0:6b68dac0d986 68 /** Set SDC Command Index */
embeddedartists 0:6b68dac0d986 69 #define SDC_COMMAND_INDEX(n) ((uint32_t) n & 0x3F)
embeddedartists 0:6b68dac0d986 70 /** No response is expected */
embeddedartists 0:6b68dac0d986 71 #define SDC_COMMAND_NO_RSP (((uint32_t) 0 ) << 6)
embeddedartists 0:6b68dac0d986 72 /** Short response is expected */
embeddedartists 0:6b68dac0d986 73 #define SDC_COMMAND_SHORT_RSP (((uint32_t) 1 ) << 6)
embeddedartists 0:6b68dac0d986 74 /** Long response is expected */
embeddedartists 0:6b68dac0d986 75 #define SDC_COMMAND_LONG_RSP (((uint32_t) 3 ) << 6)
embeddedartists 0:6b68dac0d986 76 /** Response bit mask */
embeddedartists 0:6b68dac0d986 77 #define SDC_COMMAND_RSP_BITMASK (((uint32_t) 3 ) << 6)
embeddedartists 0:6b68dac0d986 78 /** Mark that command timer is disabled and CPSM waits for interrupt request */
embeddedartists 0:6b68dac0d986 79 #define SDC_COMMAND_INTERRUPT (((uint32_t) 1 ) << 8)
embeddedartists 0:6b68dac0d986 80 /** Mark that CPSM waits for CmdPend before starting sending a command*/
embeddedartists 0:6b68dac0d986 81 #define SDC_COMMAND_PENDING (((uint32_t) 1 ) << 9)
embeddedartists 0:6b68dac0d986 82 /** Enable CPSM */
embeddedartists 0:6b68dac0d986 83 #define SDC_COMMAND_ENABLE (((uint32_t) 1 ) << 10)
embeddedartists 0:6b68dac0d986 84
embeddedartists 0:6b68dac0d986 85 /**
embeddedartists 0:6b68dac0d986 86 * @brief SDC Command Response Register bit definitions
embeddedartists 0:6b68dac0d986 87 */
embeddedartists 0:6b68dac0d986 88 /** SDC Command Response value */
embeddedartists 0:6b68dac0d986 89 #define SDC_RESPCOMMAND_VAL(n) ((uint32_t) n & 0x3F)
embeddedartists 0:6b68dac0d986 90
embeddedartists 0:6b68dac0d986 91 /*
embeddedartists 0:6b68dac0d986 92 * SD/MMC Response type definitions
embeddedartists 0:6b68dac0d986 93 */
embeddedartists 0:6b68dac0d986 94 #define CMDRESP_NONE_TYPE (SDC_COMMAND_NO_RSP)
embeddedartists 0:6b68dac0d986 95 #define CMDRESP_R1_TYPE (SDC_COMMAND_SHORT_RSP)
embeddedartists 0:6b68dac0d986 96 #define CMDRESP_R1b_TYPE (SDC_COMMAND_SHORT_RSP)
embeddedartists 0:6b68dac0d986 97 #define CMDRESP_R2_TYPE (SDC_COMMAND_LONG_RSP)
embeddedartists 0:6b68dac0d986 98 #define CMDRESP_R3_TYPE (SDC_COMMAND_SHORT_RSP)
embeddedartists 0:6b68dac0d986 99 #define CMDRESP_R6_TYPE (SDC_COMMAND_SHORT_RSP)
embeddedartists 0:6b68dac0d986 100 #define CMDRESP_R7_TYPE (SDC_COMMAND_SHORT_RSP)
embeddedartists 0:6b68dac0d986 101
embeddedartists 0:6b68dac0d986 102 /*
embeddedartists 0:6b68dac0d986 103 * SD command values (Command Index, Response)
embeddedartists 0:6b68dac0d986 104 */
embeddedartists 0:6b68dac0d986 105 #define SD_GO_IDLE_STATE (SDC_COMMAND_INDEX(MMC_GO_IDLE_STATE) | CMDRESP_NONE_TYPE | SDC_COMMAND_INTERRUPT) /*!< GO_IDLE_STATE(MMC) or RESET(SD) */
embeddedartists 0:6b68dac0d986 106 #define SD_CMD1_SEND_OP_COND (SDC_COMMAND_INDEX(MMC_SEND_OP_COND) | CMDRESP_R3_TYPE | 0) /*!< SEND_OP_COND(MMC) or ACMD41(SD) */
embeddedartists 0:6b68dac0d986 107 #define SD_CMD2_ALL_SEND_CID (SDC_COMMAND_INDEX(MMC_ALL_SEND_CID) | CMDRESP_R2_TYPE | 0) /*!< ALL_SEND_CID */
embeddedartists 0:6b68dac0d986 108 #define SD_CMD3_SET_RELATIVE_ADDR (SDC_COMMAND_INDEX(MMC_SET_RELATIVE_ADDR) | CMDRESP_R1_TYPE | 0) /*!< SET_RELATE_ADDR */
embeddedartists 0:6b68dac0d986 109 #define SD_CMD3_SEND_RELATIVE_ADDR (SDC_COMMAND_INDEX(SD_SEND_RELATIVE_ADDR) | CMDRESP_R6_TYPE | 0) /*!< SEND_RELATE_ADDR */
embeddedartists 0:6b68dac0d986 110 #define SD_CMD7_SELECT_CARD (SDC_COMMAND_INDEX(MMC_SELECT_CARD) | CMDRESP_R1b_TYPE | 0) /*!< SELECT/DESELECT_CARD */
embeddedartists 0:6b68dac0d986 111 #define SD_CMD8_SEND_IF_COND (SDC_COMMAND_INDEX(SD_CMD8) | CMDRESP_R7_TYPE | 0) /*!< SEND_IF_COND */
embeddedartists 0:6b68dac0d986 112 #define SD_CMD9_SEND_CSD (SDC_COMMAND_INDEX(MMC_SEND_CSD) | CMDRESP_R2_TYPE | 0) /*!< SEND_CSD */
embeddedartists 0:6b68dac0d986 113 #define SD_CMD12_STOP_TRANSMISSION (SDC_COMMAND_INDEX(MMC_STOP_TRANSMISSION) | CMDRESP_R1_TYPE | 0) /*!< STOP_TRANSMISSION */
embeddedartists 0:6b68dac0d986 114 #define SD_CMD13_SEND_STATUS (SDC_COMMAND_INDEX(MMC_SEND_STATUS) | CMDRESP_R1_TYPE | 0) /*!< SEND_STATUS */
embeddedartists 0:6b68dac0d986 115
embeddedartists 0:6b68dac0d986 116 /* Block-Oriented Read Commands (class 2) */
embeddedartists 0:6b68dac0d986 117 #define SD_CMD16_SET_BLOCKLEN (SDC_COMMAND_INDEX(MMC_SET_BLOCKLEN) | CMDRESP_R1_TYPE | 0) /*!< SET_BLOCK_LEN */
embeddedartists 0:6b68dac0d986 118 #define SD_CMD17_READ_SINGLE_BLOCK (SDC_COMMAND_INDEX(MMC_READ_SINGLE_BLOCK) | CMDRESP_R1_TYPE | 0) /*!< READ_SINGLE_BLOCK */
embeddedartists 0:6b68dac0d986 119 #define SD_CMD18_READ_MULTIPLE_BLOCK (SDC_COMMAND_INDEX(MMC_READ_MULTIPLE_BLOCK) | CMDRESP_R1_TYPE | 0) /*!< READ_MULTIPLE_BLOCK */
embeddedartists 0:6b68dac0d986 120
embeddedartists 0:6b68dac0d986 121 /* Block-Oriented Write Commands (class 4) */
embeddedartists 0:6b68dac0d986 122 #define SD_CMD24_WRITE_BLOCK (SDC_COMMAND_INDEX(MMC_WRITE_BLOCK) | CMDRESP_R1_TYPE | 0) /*!< WRITE_BLOCK */
embeddedartists 0:6b68dac0d986 123 #define SD_CMD25_WRITE_MULTIPLE_BLOCK (SDC_COMMAND_INDEX(MMC_WRITE_MULTIPLE_BLOCK) | CMDRESP_R1_TYPE | 0) /*!< WRITE_MULTIPLE_BLOCK */
embeddedartists 0:6b68dac0d986 124
embeddedartists 0:6b68dac0d986 125 /* Erase Commands (class 5) */
embeddedartists 0:6b68dac0d986 126 #define SD_CMD32_ERASE_WR_BLK_START (SDC_COMMAND_INDEX(SD_ERASE_WR_BLK_START) | CMDRESP_R1_TYPE | 0) /*!< ERASE_WR_BLK_START */
embeddedartists 0:6b68dac0d986 127 #define SD_CMD33_ERASE_WR_BLK_END (SDC_COMMAND_INDEX(SD_ERASE_WR_BLK_END) | CMDRESP_R1_TYPE | 0) /*!< ERASE_WR_BLK_END */
embeddedartists 0:6b68dac0d986 128 #define SD_CMD38_ERASE (SDC_COMMAND_INDEX(SD_ERASE) | CMDRESP_R1b_TYPE | 0) /*!< ERASE */
embeddedartists 0:6b68dac0d986 129
embeddedartists 0:6b68dac0d986 130 /* Application-Specific Commands (class 8) */
embeddedartists 0:6b68dac0d986 131 #define SD_CMD55_APP_CMD (SDC_COMMAND_INDEX(MMC_APP_CMD) | CMDRESP_R1_TYPE | 0) /*!< APP_CMD */
embeddedartists 0:6b68dac0d986 132 #define SD_ACMD6_SET_BUS_WIDTH (SDC_COMMAND_INDEX(SD_APP_SET_BUS_WIDTH) | CMDRESP_R1_TYPE | 0) /*!< SET_BUS_WIDTH */
embeddedartists 0:6b68dac0d986 133 #define SD_ACMD13_SEND_SD_STATUS (SDC_COMMAND_INDEX(MMC_SEND_STATUS) | CMDRESP_R1_TYPE | 0) /*!< SEND_SD_STATUS */
embeddedartists 0:6b68dac0d986 134 #define SD_ACMD41_SD_SEND_OP_COND (SDC_COMMAND_INDEX(SD_APP_OP_COND) | CMDRESP_R3_TYPE | 0) /*!< SD_SEND_OP_COND */
embeddedartists 0:6b68dac0d986 135
embeddedartists 0:6b68dac0d986 136 /**
embeddedartists 0:6b68dac0d986 137 * @brief SDC Interrupt Mask Register bit definitions
embeddedartists 0:6b68dac0d986 138 */
embeddedartists 0:6b68dac0d986 139 /** Mask CmdCrcFail flag.*/
embeddedartists 0:6b68dac0d986 140 #define SDC_MASK0_CMDCRCFAIL (((uint32_t) 1 ) << 0)
embeddedartists 0:6b68dac0d986 141 /** Mask DataCrcFail flag. */
embeddedartists 0:6b68dac0d986 142 #define SDC_MASK0_DATACRCFAIL (((uint32_t) 1 ) << 1)
embeddedartists 0:6b68dac0d986 143 /** Mask CmdTimeOut flag. */
embeddedartists 0:6b68dac0d986 144 #define SDC_MASK0_CMDTIMEOUT (((uint32_t) 1 ) << 2)
embeddedartists 0:6b68dac0d986 145 /** Mask DataTimeOut flag. */
embeddedartists 0:6b68dac0d986 146 #define SDC_MASK0_DATATIMEOUT (((uint32_t) 1 ) << 3)
embeddedartists 0:6b68dac0d986 147 /** Mask TxUnderrun flag. */
embeddedartists 0:6b68dac0d986 148 #define SDC_MASK0_TXUNDERRUN (((uint32_t) 1 ) << 4)
embeddedartists 0:6b68dac0d986 149 /** Mask RxOverrun flag. */
embeddedartists 0:6b68dac0d986 150 #define SDC_MASK0_RXOVERRUN (((uint32_t) 1 ) << 5)
embeddedartists 0:6b68dac0d986 151 /** Mask CmdRespEnd flag. */
embeddedartists 0:6b68dac0d986 152 #define SDC_MASK0_CMDRESPEND (((uint32_t) 1 ) << 6)
embeddedartists 0:6b68dac0d986 153 /** Mask CmdSent flag.*/
embeddedartists 0:6b68dac0d986 154 #define SDC_MASK0_CMDSENT (((uint32_t) 1 ) << 7)
embeddedartists 0:6b68dac0d986 155 /** Mask DataEnd flag.*/
embeddedartists 0:6b68dac0d986 156 #define SDC_MASK0_DATAEND (((uint32_t) 1 ) << 8)
embeddedartists 0:6b68dac0d986 157 /** Mask StartBitErr flag.*/
embeddedartists 0:6b68dac0d986 158 #define SDC_MASK0_STARTBITERR (((uint32_t) 1 ) << 9)
embeddedartists 0:6b68dac0d986 159 /** Mask DataBlockEnd flag.*/
embeddedartists 0:6b68dac0d986 160 #define SDC_MASK0_DATABLOCKEND (((uint32_t) 1 ) << 10)
embeddedartists 0:6b68dac0d986 161 /** Mask CmdActive flag.*/
embeddedartists 0:6b68dac0d986 162 #define SDC_MASK0_CMDACTIVE (((uint32_t) 1 ) << 11)
embeddedartists 0:6b68dac0d986 163 /** Mask TxActive flag.*/
embeddedartists 0:6b68dac0d986 164 #define SDC_MASK0_TXACTIVE (((uint32_t) 1 ) << 12)
embeddedartists 0:6b68dac0d986 165 /** Mask RxActive flag.*/
embeddedartists 0:6b68dac0d986 166 #define SDC_MASK0_RXACTIVE (((uint32_t) 1 ) << 13)
embeddedartists 0:6b68dac0d986 167 /** Mask TxFifoHalfEmpty flag.*/
embeddedartists 0:6b68dac0d986 168 #define SDC_MASK0_TXFIFOHALFEMPTY (((uint32_t) 1 ) << 14)
embeddedartists 0:6b68dac0d986 169 /** Mask RxFifoHalfFull flag.*/
embeddedartists 0:6b68dac0d986 170 #define SDC_MASK0_RXFIFOHALFFULL (((uint32_t) 1 ) << 15)
embeddedartists 0:6b68dac0d986 171 /** Mask TxFifoFull flag.*/
embeddedartists 0:6b68dac0d986 172 #define SDC_MASK0_TXFIFOFULL (((uint32_t) 1 ) << 16)
embeddedartists 0:6b68dac0d986 173 /** Mask RxFifoFull flag.*/
embeddedartists 0:6b68dac0d986 174 #define SDC_MASK0_RXFIFOFULL (((uint32_t) 1 ) << 17)
embeddedartists 0:6b68dac0d986 175 /** Mask TxFifoEmpty flag.*/
embeddedartists 0:6b68dac0d986 176 #define SDC_MASK0_TXFIFOEMPTY (((uint32_t) 1 ) << 18)
embeddedartists 0:6b68dac0d986 177 /** Mask RxFifoEmpty flag.*/
embeddedartists 0:6b68dac0d986 178 #define SDC_MASK0_RXFIFOEMPTY (((uint32_t) 1 ) << 19)
embeddedartists 0:6b68dac0d986 179 /** Mask TxDataAvlbl flag.*/
embeddedartists 0:6b68dac0d986 180 #define SDC_MASK0_TXDATAAVLBL (((uint32_t) 1 ) << 20)
embeddedartists 0:6b68dac0d986 181 /** Mask RxDataAvlbl flag.*/
embeddedartists 0:6b68dac0d986 182 #define SDC_MASK0_RXDATAAVLBL (((uint32_t) 1 ) << 21)
embeddedartists 0:6b68dac0d986 183 /** CMD error interrupt mask */
embeddedartists 0:6b68dac0d986 184 #define SDC_MASK0_CMDERR (SDC_MASK0_CMDCRCFAIL | SDC_MASK0_CMDTIMEOUT | SDC_MASK0_STARTBITERR)
embeddedartists 0:6b68dac0d986 185 /** Data Transmit Error interrupt mask */
embeddedartists 0:6b68dac0d986 186 #define SDC_MASK0_TXDATAERR (SDC_MASK0_DATACRCFAIL | SDC_MASK0_DATATIMEOUT | SDC_MASK0_TXUNDERRUN | SDC_MASK0_STARTBITERR)
embeddedartists 0:6b68dac0d986 187 /** Data Receive Error interrupt mask */
embeddedartists 0:6b68dac0d986 188 #define SDC_MASK0_RXDATAERR (SDC_MASK0_DATACRCFAIL | SDC_MASK0_DATATIMEOUT | SDC_MASK0_RXOVERRUN | SDC_MASK0_STARTBITERR)
embeddedartists 0:6b68dac0d986 189 /** Data Transfer interrupt mask*/
embeddedartists 0:6b68dac0d986 190 #define SDC_MASK0_DATA (SDC_MASK0_DATAEND | SDC_MASK0_DATABLOCKEND )
embeddedartists 0:6b68dac0d986 191
embeddedartists 0:6b68dac0d986 192 /**
embeddedartists 0:6b68dac0d986 193 * @brief SDC Clock Control Register bit definitions
embeddedartists 0:6b68dac0d986 194 */
embeddedartists 0:6b68dac0d986 195 /** SDC Clock Control Register Bitmask */
embeddedartists 0:6b68dac0d986 196 #define SDC_CLOCK_BITMASK ((uint32_t) 0xFFF)
embeddedartists 0:6b68dac0d986 197 /** SDC Clock Divider Bitmask */
embeddedartists 0:6b68dac0d986 198 #define SDC_CLOCK_CLKDIV_BITMASK (((uint32_t) 0xFF ) << 0)
embeddedartists 0:6b68dac0d986 199 /** Set SDC Clock Divide value */
embeddedartists 0:6b68dac0d986 200 #define SDC_CLOCK_CLKDIV(n) (((uint32_t) (n & 0x0FF)) << 0)
embeddedartists 0:6b68dac0d986 201
embeddedartists 0:6b68dac0d986 202 /**
embeddedartists 0:6b68dac0d986 203 * @brief SDC Status Register bit definitions
embeddedartists 0:6b68dac0d986 204 */
embeddedartists 0:6b68dac0d986 205 /** Command Response received (CRC check failed) */
embeddedartists 0:6b68dac0d986 206 #define SDC_STATUS_CMDCRCFAIL (((uint32_t) 1 ) << 0)
embeddedartists 0:6b68dac0d986 207 /** Data block sent/received (CRC check failed). */
embeddedartists 0:6b68dac0d986 208 #define SDC_STATUS_DATACRCFAIL (((uint32_t) 1 ) << 1)
embeddedartists 0:6b68dac0d986 209 /** Command response timeout.. */
embeddedartists 0:6b68dac0d986 210 #define SDC_STATUS_CMDTIMEOUT (((uint32_t) 1 ) << 2)
embeddedartists 0:6b68dac0d986 211 /** Data timeout. */
embeddedartists 0:6b68dac0d986 212 #define SDC_STATUS_DATATIMEOUT (((uint32_t) 1 ) << 3)
embeddedartists 0:6b68dac0d986 213 /** Transmit FIFO underrun error. */
embeddedartists 0:6b68dac0d986 214 #define SDC_STATUS_TXUNDERRUN (((uint32_t) 1 ) << 4)
embeddedartists 0:6b68dac0d986 215 /** Receive FIFO overrun error. */
embeddedartists 0:6b68dac0d986 216 #define SDC_STATUS_RXOVERRUN (((uint32_t) 1 ) << 5)
embeddedartists 0:6b68dac0d986 217 /** Command response received (CRC check passed). */
embeddedartists 0:6b68dac0d986 218 #define SDC_STATUS_CMDRESPEND (((uint32_t) 1 ) << 6)
embeddedartists 0:6b68dac0d986 219 /** Command sent (no response required).*/
embeddedartists 0:6b68dac0d986 220 #define SDC_STATUS_CMDSENT (((uint32_t) 1 ) << 7)
embeddedartists 0:6b68dac0d986 221 /** Data end (data counter is zero).*/
embeddedartists 0:6b68dac0d986 222 #define SDC_STATUS_DATAEND (((uint32_t) 1 ) << 8)
embeddedartists 0:6b68dac0d986 223 /** Start bit not detected on all data signals in wide bus mode..*/
embeddedartists 0:6b68dac0d986 224 #define SDC_STATUS_STARTBITERR (((uint32_t) 1 ) << 9)
embeddedartists 0:6b68dac0d986 225 /** Data block sent/received (CRC check passed).*/
embeddedartists 0:6b68dac0d986 226 #define SDC_STATUS_DATABLOCKEND (((uint32_t) 1 ) << 10)
embeddedartists 0:6b68dac0d986 227 /** Command transfer in progress.*/
embeddedartists 0:6b68dac0d986 228 #define SDC_STATUS_CMDACTIVE (((uint32_t) 1 ) << 11)
embeddedartists 0:6b68dac0d986 229 /** Data transmit in progress.*/
embeddedartists 0:6b68dac0d986 230 #define SDC_STATUS_TXACTIVE (((uint32_t) 1 ) << 12)
embeddedartists 0:6b68dac0d986 231 /** Data receive in progress.*/
embeddedartists 0:6b68dac0d986 232 #define SDC_STATUS_RXACTIVE (((uint32_t) 1 ) << 13)
embeddedartists 0:6b68dac0d986 233 /** Transmit FIFO half empty.*/
embeddedartists 0:6b68dac0d986 234 #define SDC_STATUS_TXFIFOHALFEMPTY (((uint32_t) 1 ) << 14)
embeddedartists 0:6b68dac0d986 235 /** Receive FIFO half full.*/
embeddedartists 0:6b68dac0d986 236 #define SDC_STATUS_RXFIFOHALFFULL (((uint32_t) 1 ) << 15)
embeddedartists 0:6b68dac0d986 237 /** Transmit FIFO full.*/
embeddedartists 0:6b68dac0d986 238 #define SDC_STATUS_TXFIFOFULL (((uint32_t) 1 ) << 16)
embeddedartists 0:6b68dac0d986 239 /** Receive FIFO full.*/
embeddedartists 0:6b68dac0d986 240 #define SDC_STATUS_RXFIFOFULL (((uint32_t) 1 ) << 17)
embeddedartists 0:6b68dac0d986 241 /** Transmit FIFO empty.*/
embeddedartists 0:6b68dac0d986 242 #define SDC_STATUS_TXFIFOEMPTY (((uint32_t) 1 ) << 18)
embeddedartists 0:6b68dac0d986 243 /** Receive FIFO empty.*/
embeddedartists 0:6b68dac0d986 244 #define SDC_STATUS_RXFIFOEMPTY (((uint32_t) 1 ) << 19)
embeddedartists 0:6b68dac0d986 245 /** Data available in transmit FIFO.*/
embeddedartists 0:6b68dac0d986 246 #define SDC_STATUS_TXDATAAVLBL (((uint32_t) 1 ) << 20)
embeddedartists 0:6b68dac0d986 247 /** Data available in receive FIFO.*/
embeddedartists 0:6b68dac0d986 248 #define SDC_STATUS_RXDATAAVLBL (((uint32_t) 1 ) << 21)
embeddedartists 0:6b68dac0d986 249 /** Command Error Status */
embeddedartists 0:6b68dac0d986 250 #define SDC_STATUS_CMDERR (SDC_STATUS_CMDCRCFAIL | SDC_STATUS_CMDTIMEOUT | SDC_STATUS_STARTBITERR)
embeddedartists 0:6b68dac0d986 251 /** Data Error Status */
embeddedartists 0:6b68dac0d986 252 #define SDC_STATUS_DATAERR (SDC_STATUS_DATACRCFAIL | SDC_STATUS_DATATIMEOUT | SDC_STATUS_TXUNDERRUN \
embeddedartists 0:6b68dac0d986 253 | SDC_STATUS_RXOVERRUN | SDC_STATUS_STARTBITERR)
embeddedartists 0:6b68dac0d986 254 /** FIFO Status*/
embeddedartists 0:6b68dac0d986 255 #define SDC_STATUS_FIFO (SDC_STATUS_TXFIFOHALFEMPTY | SDC_STATUS_RXFIFOHALFFULL \
embeddedartists 0:6b68dac0d986 256 | SDC_STATUS_TXFIFOFULL | SDC_STATUS_RXFIFOFULL \
embeddedartists 0:6b68dac0d986 257 | SDC_STATUS_TXFIFOEMPTY | SDC_STATUS_RXFIFOEMPTY \
embeddedartists 0:6b68dac0d986 258 | SDC_STATUS_DATABLOCKEND)
embeddedartists 0:6b68dac0d986 259
embeddedartists 0:6b68dac0d986 260 /** Data Transfer Status*/
embeddedartists 0:6b68dac0d986 261 #define SDC_STATUS_DATA (SDC_STATUS_DATAEND )
embeddedartists 0:6b68dac0d986 262
embeddedartists 0:6b68dac0d986 263 /**
embeddedartists 0:6b68dac0d986 264 * @brief SDC Data Control Register bit definitions
embeddedartists 0:6b68dac0d986 265 */
embeddedartists 0:6b68dac0d986 266 /** SDC Data Control Register Bitmask */
embeddedartists 0:6b68dac0d986 267 #define SDC_DATACTRL_BITMASK ((uint32_t) 0xFF)
embeddedartists 0:6b68dac0d986 268 /** Enable Data Transfer */
embeddedartists 0:6b68dac0d986 269 #define SDC_DATACTRL_ENABLE (((uint32_t) 1 ) << 0)
embeddedartists 0:6b68dac0d986 270 /** Mark that Data is transfer from card to controller */
embeddedartists 0:6b68dac0d986 271 #define SDC_DATACTRL_DIR_FROMCARD (((uint32_t) 1 ) << 1)
embeddedartists 0:6b68dac0d986 272 /** Mark that Data is transfer from controller to card */
embeddedartists 0:6b68dac0d986 273 #define SDC_DATACTRL_DIR_TOCARD ((uint32_t) 0)
embeddedartists 0:6b68dac0d986 274 /** Mark that the transfer mode is Stream Data Transfer */
embeddedartists 0:6b68dac0d986 275 #define SDC_DATACTRL_XFER_MODE_STREAM (((uint32_t) 1 ) << 2)
embeddedartists 0:6b68dac0d986 276 /** Mark that the transfer mode is Block Data Transfer */
embeddedartists 0:6b68dac0d986 277 #define SDC_DATACTRL_XFER_MODE_BLOCK ((uint32_t) 0)
embeddedartists 0:6b68dac0d986 278 /** Enable DMA */
embeddedartists 0:6b68dac0d986 279 #define SDC_DATACTRL_DMA_ENABLE (((uint32_t) 1 ) << 3)
embeddedartists 0:6b68dac0d986 280 /** Set Data Block size */
embeddedartists 0:6b68dac0d986 281 #define SDC_DATACTRL_BLOCKSIZE(n) (((uint32_t) (n & 0x0F) ) << 4)
embeddedartists 0:6b68dac0d986 282 /** Get Data Block size value */
embeddedartists 0:6b68dac0d986 283 #define SDC_DATACTRL_BLOCKSIZE_VAL(n) (((uint32_t) 1) << n)
embeddedartists 0:6b68dac0d986 284
embeddedartists 0:6b68dac0d986 285 /**
embeddedartists 0:6b68dac0d986 286 * @brief OCR Register definitions
embeddedartists 0:6b68dac0d986 287 */
embeddedartists 0:6b68dac0d986 288 /** Support voltage range 2.7-3.6 */
embeddedartists 0:6b68dac0d986 289 #define SDC_OCR_27_36 ((uint32_t) 0x00FF8000)
embeddedartists 0:6b68dac0d986 290 /** Card power up status bit */
embeddedartists 0:6b68dac0d986 291 #define SDC_OCR_IDLE (((uint32_t) 1) << 31)
embeddedartists 0:6b68dac0d986 292 #define SDC_OCR_BUSY (((uint32_t) 0) << 31)
embeddedartists 0:6b68dac0d986 293
embeddedartists 0:6b68dac0d986 294
embeddedartists 0:6b68dac0d986 295 /* SD/MMC commands - this matrix shows the command, response types, and
embeddedartists 0:6b68dac0d986 296 supported card type for that command.
embeddedartists 0:6b68dac0d986 297 Command Number Resp SD MMC
embeddedartists 0:6b68dac0d986 298 ----------------------- ------ ----- --- ---
embeddedartists 0:6b68dac0d986 299 Reset (go idle) CMD0 NA x x
embeddedartists 0:6b68dac0d986 300 Send op condition CMD1 R3 x
embeddedartists 0:6b68dac0d986 301 All send CID CMD2 R2 x x
embeddedartists 0:6b68dac0d986 302 Send relative address CMD3 R1 x
embeddedartists 0:6b68dac0d986 303 Send relative address CMD3 R6 x
embeddedartists 0:6b68dac0d986 304 Program DSR CMD4 NA x
embeddedartists 0:6b68dac0d986 305 Select/deselect card CMD7 R1b x
embeddedartists 0:6b68dac0d986 306 Select/deselect card CMD7 R1 x
embeddedartists 0:6b68dac0d986 307 Send CSD CMD9 R2 x x
embeddedartists 0:6b68dac0d986 308 Send CID CMD10 R2 x x
embeddedartists 0:6b68dac0d986 309 Read data until stop CMD11 R1 x x
embeddedartists 0:6b68dac0d986 310 Stop transmission CMD12 R1/b x x
embeddedartists 0:6b68dac0d986 311 Send status CMD13 R1 x x
embeddedartists 0:6b68dac0d986 312 Go inactive state CMD15 NA x x
embeddedartists 0:6b68dac0d986 313 Set block length CMD16 R1 x x
embeddedartists 0:6b68dac0d986 314 Read single block CMD17 R1 x x
embeddedartists 0:6b68dac0d986 315 Read multiple blocks CMD18 R1 x x
embeddedartists 0:6b68dac0d986 316 Write data until stop CMD20 R1 x
embeddedartists 0:6b68dac0d986 317 Setblock count CMD23 R1 x
embeddedartists 0:6b68dac0d986 318 Write single block CMD24 R1 x x
embeddedartists 0:6b68dac0d986 319 Write multiple blocks CMD25 R1 x x
embeddedartists 0:6b68dac0d986 320 Program CID CMD26 R1 x
embeddedartists 0:6b68dac0d986 321 Program CSD CMD27 R1 x x
embeddedartists 0:6b68dac0d986 322 Set write protection CMD28 R1b x x
embeddedartists 0:6b68dac0d986 323 Clear write protection CMD29 R1b x x
embeddedartists 0:6b68dac0d986 324 Send write protection CMD30 R1 x x
embeddedartists 0:6b68dac0d986 325 Erase block start CMD32 R1 x
embeddedartists 0:6b68dac0d986 326 Erase block end CMD33 R1 x
embeddedartists 0:6b68dac0d986 327 Erase block start CMD35 R1 x
embeddedartists 0:6b68dac0d986 328 Erase block end CMD36 R1 x
embeddedartists 0:6b68dac0d986 329 Erase blocks CMD38 R1b x
embeddedartists 0:6b68dac0d986 330 Fast IO CMD39 R4 x
embeddedartists 0:6b68dac0d986 331 Go IRQ state CMD40 R5 x
embeddedartists 0:6b68dac0d986 332 Lock/unlock CMD42 R1b x
embeddedartists 0:6b68dac0d986 333 Application command CMD55 R1 x
embeddedartists 0:6b68dac0d986 334 General command CMD56 R1b x
embeddedartists 0:6b68dac0d986 335
embeddedartists 0:6b68dac0d986 336 *** SD card application commands - these must be preceded with ***
embeddedartists 0:6b68dac0d986 337 *** MMC CMD55 application specific command first ***
embeddedartists 0:6b68dac0d986 338 Set bus width ACMD6 R1 x
embeddedartists 0:6b68dac0d986 339 Send SD status ACMD13 R1 x
embeddedartists 0:6b68dac0d986 340 Send number WR blocks ACMD22 R1 x
embeddedartists 0:6b68dac0d986 341 Set WR block erase cnt ACMD23 R1 x
embeddedartists 0:6b68dac0d986 342 Send op condition ACMD41 R3 x
embeddedartists 0:6b68dac0d986 343 Set clear card detect ACMD42 R1 x
embeddedartists 0:6b68dac0d986 344 Send CSR ACMD51 R1 x */
embeddedartists 0:6b68dac0d986 345
embeddedartists 0:6b68dac0d986 346 /**
embeddedartists 0:6b68dac0d986 347 * @brief Possible SDMMC card state types
embeddedartists 0:6b68dac0d986 348 */
embeddedartists 0:6b68dac0d986 349 typedef enum {
embeddedartists 0:6b68dac0d986 350 SDMMC_IDLE_ST = 0, /*!< Idle state */
embeddedartists 0:6b68dac0d986 351 SDMMC_READY_ST, /*!< Ready state */
embeddedartists 0:6b68dac0d986 352 SDMMC_IDENT_ST, /*!< Identification State */
embeddedartists 0:6b68dac0d986 353 SDMMC_STBY_ST, /*!< standby state */
embeddedartists 0:6b68dac0d986 354 SDMMC_TRAN_ST, /*!< transfer state */
embeddedartists 0:6b68dac0d986 355 SDMMC_DATA_ST, /*!< Sending-data State */
embeddedartists 0:6b68dac0d986 356 SDMMC_RCV_ST, /*!< Receive-data State */
embeddedartists 0:6b68dac0d986 357 SDMMC_PRG_ST, /*!< Programming State */
embeddedartists 0:6b68dac0d986 358 SDMMC_DIS_ST /*!< Disconnect State */
embeddedartists 0:6b68dac0d986 359 } SDMMC_STATE_T;
embeddedartists 0:6b68dac0d986 360
embeddedartists 0:6b68dac0d986 361
embeddedartists 0:6b68dac0d986 362 /**
embeddedartists 0:6b68dac0d986 363 * @brief SD/MMC commands, arguments and responses
embeddedartists 0:6b68dac0d986 364 * Standard SD/MMC commands (3.1) type argument response
embeddedartists 0:6b68dac0d986 365 */
embeddedartists 0:6b68dac0d986 366 /* class 1 */
embeddedartists 0:6b68dac0d986 367 #define MMC_GO_IDLE_STATE 0 /* bc */
embeddedartists 0:6b68dac0d986 368 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
embeddedartists 0:6b68dac0d986 369 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
embeddedartists 0:6b68dac0d986 370 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
embeddedartists 0:6b68dac0d986 371 #define MMC_SET_DSR 4 /* bc [31:16] RCA */
embeddedartists 0:6b68dac0d986 372 #define MMC_SELECT_CARD 7 /* ac [31:16] RCA R1 */
embeddedartists 0:6b68dac0d986 373 #define MMC_SEND_EXT_CSD 8 /* bc R1 */
embeddedartists 0:6b68dac0d986 374 #define MMC_SEND_CSD 9 /* ac [31:16] RCA R2 */
embeddedartists 0:6b68dac0d986 375 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
embeddedartists 0:6b68dac0d986 376 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
embeddedartists 0:6b68dac0d986 377 #define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
embeddedartists 0:6b68dac0d986 378 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
embeddedartists 0:6b68dac0d986 379
embeddedartists 0:6b68dac0d986 380 /* class 2 */
embeddedartists 0:6b68dac0d986 381 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
embeddedartists 0:6b68dac0d986 382 #define MMC_READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 383 #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 384
embeddedartists 0:6b68dac0d986 385 /* class 3 */
embeddedartists 0:6b68dac0d986 386 #define MMC_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 387
embeddedartists 0:6b68dac0d986 388 /* class 4 */
embeddedartists 0:6b68dac0d986 389 #define MMC_SET_BLOCK_COUNT 23 /* adtc [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 390 #define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 391 #define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
embeddedartists 0:6b68dac0d986 392 #define MMC_PROGRAM_CID 26 /* adtc R1 */
embeddedartists 0:6b68dac0d986 393 #define MMC_PROGRAM_CSD 27 /* adtc R1 */
embeddedartists 0:6b68dac0d986 394
embeddedartists 0:6b68dac0d986 395 /* class 6 */
embeddedartists 0:6b68dac0d986 396 #define MMC_SET_WRITE_PROT 28 /* ac [31:0] data addr R1b */
embeddedartists 0:6b68dac0d986 397 #define MMC_CLR_WRITE_PROT 29 /* ac [31:0] data addr R1b */
embeddedartists 0:6b68dac0d986 398 #define MMC_SEND_WRITE_PROT 30 /* adtc [31:0] wpdata addr R1 */
embeddedartists 0:6b68dac0d986 399
embeddedartists 0:6b68dac0d986 400 /* class 5 */
embeddedartists 0:6b68dac0d986 401 #define MMC_ERASE_GROUP_START 35 /* ac [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 402 #define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 403 #define MMC_ERASE 37 /* ac R1b */
embeddedartists 0:6b68dac0d986 404 #define SD_ERASE_WR_BLK_START 32 /* ac [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 405 #define SD_ERASE_WR_BLK_END 33 /* ac [31:0] data addr R1 */
embeddedartists 0:6b68dac0d986 406 #define SD_ERASE 38 /* ac R1b */
embeddedartists 0:6b68dac0d986 407
embeddedartists 0:6b68dac0d986 408 /* class 9 */
embeddedartists 0:6b68dac0d986 409 #define MMC_FAST_IO 39 /* ac <Complex> R4 */
embeddedartists 0:6b68dac0d986 410 #define MMC_GO_IRQ_STATE 40 /* bcr R5 */
embeddedartists 0:6b68dac0d986 411
embeddedartists 0:6b68dac0d986 412 /* class 7 */
embeddedartists 0:6b68dac0d986 413 #define MMC_LOCK_UNLOCK 42 /* adtc R1b */
embeddedartists 0:6b68dac0d986 414
embeddedartists 0:6b68dac0d986 415 /* class 8 */
embeddedartists 0:6b68dac0d986 416 #define MMC_APP_CMD 55 /* ac [31:16] RCA R1 */
embeddedartists 0:6b68dac0d986 417 #define MMC_GEN_CMD 56 /* adtc [0] RD/WR R1b */
embeddedartists 0:6b68dac0d986 418
embeddedartists 0:6b68dac0d986 419 /* SD commands type argument response */
embeddedartists 0:6b68dac0d986 420 /* class 8 */
embeddedartists 0:6b68dac0d986 421 /* This is basically the same command as for MMC with some quirks. */
embeddedartists 0:6b68dac0d986 422 #define SD_SEND_RELATIVE_ADDR 3 /* ac R6 */
embeddedartists 0:6b68dac0d986 423 #define SD_CMD8 8 /* bcr [31:0] OCR R3 */
embeddedartists 0:6b68dac0d986 424
embeddedartists 0:6b68dac0d986 425 /* Application commands */
embeddedartists 0:6b68dac0d986 426 #define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
embeddedartists 0:6b68dac0d986 427 #define SD_APP_OP_COND 41 /* bcr [31:0] OCR R1 (R4) */
embeddedartists 0:6b68dac0d986 428 #define SD_APP_SEND_SCR 51 /* adtc R1 */
embeddedartists 0:6b68dac0d986 429
embeddedartists 0:6b68dac0d986 430
embeddedartists 0:6b68dac0d986 431 /**
embeddedartists 0:6b68dac0d986 432 * @brief MMC status in R1<br>
embeddedartists 0:6b68dac0d986 433 * Type<br>
embeddedartists 0:6b68dac0d986 434 * e : error bit<br>
embeddedartists 0:6b68dac0d986 435 * s : status bit<br>
embeddedartists 0:6b68dac0d986 436 * r : detected and set for the actual command response<br>
embeddedartists 0:6b68dac0d986 437 * x : detected and set during command execution. the host must poll
embeddedartists 0:6b68dac0d986 438 * the card by sending status command in order to read these bits.
embeddedartists 0:6b68dac0d986 439 * Clear condition<br>
embeddedartists 0:6b68dac0d986 440 * a : according to the card state<br>
embeddedartists 0:6b68dac0d986 441 * b : always related to the previous command. Reception of
embeddedartists 0:6b68dac0d986 442 * a valid command will clear it (with a delay of one command)<br>
embeddedartists 0:6b68dac0d986 443 * c : clear by read<br>
embeddedartists 0:6b68dac0d986 444 */
embeddedartists 0:6b68dac0d986 445
embeddedartists 0:6b68dac0d986 446 #define R1_OUT_OF_RANGE (1UL << 31) /* er, c */
embeddedartists 0:6b68dac0d986 447 #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
embeddedartists 0:6b68dac0d986 448 #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
embeddedartists 0:6b68dac0d986 449 #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
embeddedartists 0:6b68dac0d986 450 #define R1_ERASE_PARAM (1 << 27) /* ex, c */
embeddedartists 0:6b68dac0d986 451 #define R1_WP_VIOLATION (1 << 26) /* erx, c */
embeddedartists 0:6b68dac0d986 452 #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
embeddedartists 0:6b68dac0d986 453 #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
embeddedartists 0:6b68dac0d986 454 #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
embeddedartists 0:6b68dac0d986 455 #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
embeddedartists 0:6b68dac0d986 456 #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
embeddedartists 0:6b68dac0d986 457 #define R1_CC_ERROR (1 << 20) /* erx, c */
embeddedartists 0:6b68dac0d986 458 #define R1_ERROR (1 << 19) /* erx, c */
embeddedartists 0:6b68dac0d986 459 #define R1_UNDERRUN (1 << 18) /* ex, c */
embeddedartists 0:6b68dac0d986 460 #define R1_OVERRUN (1 << 17) /* ex, c */
embeddedartists 0:6b68dac0d986 461 #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
embeddedartists 0:6b68dac0d986 462 #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
embeddedartists 0:6b68dac0d986 463 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
embeddedartists 0:6b68dac0d986 464 #define R1_ERASE_RESET (1 << 13) /* sr, c */
embeddedartists 0:6b68dac0d986 465 #define R1_STATUS(x) (x & 0xFFFFE000)
embeddedartists 0:6b68dac0d986 466 #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
embeddedartists 0:6b68dac0d986 467 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
embeddedartists 0:6b68dac0d986 468 #define R1_APP_CMD (1 << 5) /* sr, c */
embeddedartists 0:6b68dac0d986 469
embeddedartists 0:6b68dac0d986 470
embeddedartists 0:6b68dac0d986 471 /**
embeddedartists 0:6b68dac0d986 472 * @brief SD/MMC card OCR register bits
embeddedartists 0:6b68dac0d986 473 */
embeddedartists 0:6b68dac0d986 474 #define OCR_ALL_READY (1UL << 31) /* Card Power up status bit */
embeddedartists 0:6b68dac0d986 475 #define OCR_HC_CCS (1 << 30) /* High capacity card */
embeddedartists 0:6b68dac0d986 476 #define OCR_VOLTAGE_RANGE_MSK (0x00FF8000)
embeddedartists 0:6b68dac0d986 477
embeddedartists 0:6b68dac0d986 478 #define SD_SEND_IF_ARG 0x000001AA
embeddedartists 0:6b68dac0d986 479 #define SD_SEND_IF_ECHO_MSK 0x000000FF
embeddedartists 0:6b68dac0d986 480 #define SD_SEND_IF_RESP 0x000000AA
embeddedartists 0:6b68dac0d986 481
embeddedartists 0:6b68dac0d986 482 /**
embeddedartists 0:6b68dac0d986 483 * @brief R3 response definitions
embeddedartists 0:6b68dac0d986 484 */
embeddedartists 0:6b68dac0d986 485 #define CMDRESP_R3_OCR_VAL(n) (((uint32_t) n) & 0xFFFFFF)
embeddedartists 0:6b68dac0d986 486 #define CMDRESP_R3_S18A (((uint32_t) 1 ) << 24)
embeddedartists 0:6b68dac0d986 487 #define CMDRESP_R3_HC_CCS (((uint32_t) 1 ) << 30)
embeddedartists 0:6b68dac0d986 488 #define CMDRESP_R3_INIT_COMPLETE (((uint32_t) 1 ) << 31)
embeddedartists 0:6b68dac0d986 489
embeddedartists 0:6b68dac0d986 490 /**
embeddedartists 0:6b68dac0d986 491 * @brief R6 response definitions
embeddedartists 0:6b68dac0d986 492 */
embeddedartists 0:6b68dac0d986 493 #define CMDRESP_R6_RCA_VAL(n) (((uint32_t) (n >> 16)) & 0xFFFF)
embeddedartists 0:6b68dac0d986 494 #define CMDRESP_R6_CARD_STATUS(n) (((uint32_t) (n & 0x1FFF)) | \
embeddedartists 0:6b68dac0d986 495 ((n & (1 << 13)) ? (1 << 19) : 0) | \
embeddedartists 0:6b68dac0d986 496 ((n & (1 << 14)) ? (1 << 22) : 0) | \
embeddedartists 0:6b68dac0d986 497 ((n & (1 << 15)) ? (1 << 23) : 0))
embeddedartists 0:6b68dac0d986 498
embeddedartists 0:6b68dac0d986 499 /**
embeddedartists 0:6b68dac0d986 500 * @brief R7 response definitions
embeddedartists 0:6b68dac0d986 501 */
embeddedartists 0:6b68dac0d986 502 /** Echo-back of check-pattern */
embeddedartists 0:6b68dac0d986 503 #define CMDRESP_R7_CHECK_PATTERN(n) (((uint32_t) n ) & 0xFF)
embeddedartists 0:6b68dac0d986 504 /** Voltage accepted */
embeddedartists 0:6b68dac0d986 505 #define CMDRESP_R7_VOLTAGE_ACCEPTED (((uint32_t) 1 ) << 8)
embeddedartists 0:6b68dac0d986 506
embeddedartists 0:6b68dac0d986 507 /**
embeddedartists 0:6b68dac0d986 508 * @brief CMD3 command definitions
embeddedartists 0:6b68dac0d986 509 */
embeddedartists 0:6b68dac0d986 510 /** Card Address */
embeddedartists 0:6b68dac0d986 511 #define CMD3_RCA(n) (((uint32_t) (n & 0xFFFF) ) << 16)
embeddedartists 0:6b68dac0d986 512
embeddedartists 0:6b68dac0d986 513 /**
embeddedartists 0:6b68dac0d986 514 * @brief CMD7 command definitions
embeddedartists 0:6b68dac0d986 515 */
embeddedartists 0:6b68dac0d986 516 /** Card Address */
embeddedartists 0:6b68dac0d986 517 #define CMD7_RCA(n) (((uint32_t) (n & 0xFFFF) ) << 16)
embeddedartists 0:6b68dac0d986 518
embeddedartists 0:6b68dac0d986 519 /**
embeddedartists 0:6b68dac0d986 520 * @brief CMD8 command definitions
embeddedartists 0:6b68dac0d986 521 */
embeddedartists 0:6b68dac0d986 522 /** Check pattern */
embeddedartists 0:6b68dac0d986 523 #define CMD8_CHECKPATTERN(n) (((uint32_t) (n & 0xFF) ) << 0)
embeddedartists 0:6b68dac0d986 524 /** Recommended pattern */
embeddedartists 0:6b68dac0d986 525 #define CMD8_DEF_PATTERN (0xAA)
embeddedartists 0:6b68dac0d986 526 /** Voltage supplied.*/
embeddedartists 0:6b68dac0d986 527 #define CMD8_VOLTAGESUPPLIED_27_36 (((uint32_t) 1 ) << 8)
embeddedartists 0:6b68dac0d986 528
embeddedartists 0:6b68dac0d986 529 /**
embeddedartists 0:6b68dac0d986 530 * @brief CMD9 command definitions
embeddedartists 0:6b68dac0d986 531 */
embeddedartists 0:6b68dac0d986 532 #define CMD9_RCA(n) (((uint32_t) (n & 0xFFFF) ) << 16)
embeddedartists 0:6b68dac0d986 533
embeddedartists 0:6b68dac0d986 534 /**
embeddedartists 0:6b68dac0d986 535 * @brief CMD13 command definitions
embeddedartists 0:6b68dac0d986 536 */
embeddedartists 0:6b68dac0d986 537 #define CMD13_RCA(n) (((uint32_t) (n & 0xFFFF) ) << 16)
embeddedartists 0:6b68dac0d986 538
embeddedartists 0:6b68dac0d986 539 /**
embeddedartists 0:6b68dac0d986 540 * @brief APP_CMD command definitions
embeddedartists 0:6b68dac0d986 541 */
embeddedartists 0:6b68dac0d986 542 #define CMD55_RCA(n) (((uint32_t) (n & 0xFFFF) ) << 16)
embeddedartists 0:6b68dac0d986 543
embeddedartists 0:6b68dac0d986 544 /**
embeddedartists 0:6b68dac0d986 545 * @brief ACMD41 command definitions
embeddedartists 0:6b68dac0d986 546 */
embeddedartists 0:6b68dac0d986 547 #define ACMD41_OCR(n) (((uint32_t) n) & 0xFFFFFF)
embeddedartists 0:6b68dac0d986 548 #define ACMD41_S18R (((uint32_t) 1 ) << 24)
embeddedartists 0:6b68dac0d986 549 #define ACMD41_XPC (((uint32_t) 1 ) << 28)
embeddedartists 0:6b68dac0d986 550 #define ACMD41_HCS (((uint32_t) 1 ) << 30)
embeddedartists 0:6b68dac0d986 551
embeddedartists 0:6b68dac0d986 552 /**
embeddedartists 0:6b68dac0d986 553 * @brief ACMD6 command definitions
embeddedartists 0:6b68dac0d986 554 */
embeddedartists 0:6b68dac0d986 555 #define ACMD6_BUS_WIDTH(n) ((uint32_t) n & 0x03)
embeddedartists 0:6b68dac0d986 556 #define ACMD6_BUS_WIDTH_1 (0)
embeddedartists 0:6b68dac0d986 557 #define ACMD6_BUS_WIDTH_4 (2)
embeddedartists 0:6b68dac0d986 558
embeddedartists 0:6b68dac0d986 559 /** @brief Card type defines
embeddedartists 0:6b68dac0d986 560 */
embeddedartists 0:6b68dac0d986 561 #define CARD_TYPE_SD (1 << 0)
embeddedartists 0:6b68dac0d986 562 #define CARD_TYPE_4BIT (1 << 1)
embeddedartists 0:6b68dac0d986 563 #define CARD_TYPE_8BIT (1 << 2)
embeddedartists 0:6b68dac0d986 564 #define CARD_TYPE_HC (OCR_HC_CCS)/*!< high capacity card > 2GB */
embeddedartists 0:6b68dac0d986 565
embeddedartists 0:6b68dac0d986 566 /**
embeddedartists 0:6b68dac0d986 567 * @brief SD/MMC sector size in bytes
embeddedartists 0:6b68dac0d986 568 */
embeddedartists 0:6b68dac0d986 569 #define MMC_SECTOR_SIZE 512
embeddedartists 0:6b68dac0d986 570
embeddedartists 0:6b68dac0d986 571 /******************************************************************************
embeddedartists 0:6b68dac0d986 572 * External global variables
embeddedartists 0:6b68dac0d986 573 *****************************************************************************/
embeddedartists 0:6b68dac0d986 574
embeddedartists 0:6b68dac0d986 575 /******************************************************************************
embeddedartists 0:6b68dac0d986 576 * Local variables
embeddedartists 0:6b68dac0d986 577 *****************************************************************************/
embeddedartists 0:6b68dac0d986 578
embeddedartists 0:6b68dac0d986 579 static MCIFileSystem* pUglyForIRQ = NULL;
embeddedartists 0:6b68dac0d986 580
embeddedartists 0:6b68dac0d986 581 /******************************************************************************
embeddedartists 0:6b68dac0d986 582 * Local Functions
embeddedartists 0:6b68dac0d986 583 *****************************************************************************/
embeddedartists 0:6b68dac0d986 584
embeddedartists 0:6b68dac0d986 585 static void mymciirq()
embeddedartists 0:6b68dac0d986 586 {
embeddedartists 0:6b68dac0d986 587 pUglyForIRQ->mci_MCIIRQHandler();
embeddedartists 0:6b68dac0d986 588 }
embeddedartists 0:6b68dac0d986 589
embeddedartists 0:6b68dac0d986 590 static void mydmairq()
embeddedartists 0:6b68dac0d986 591 {
embeddedartists 0:6b68dac0d986 592 pUglyForIRQ->mci_DMAIRQHandler();
embeddedartists 0:6b68dac0d986 593 }
embeddedartists 0:6b68dac0d986 594
embeddedartists 0:6b68dac0d986 595 /******************************************************************************
embeddedartists 0:6b68dac0d986 596 * Public Functions
embeddedartists 0:6b68dac0d986 597 *****************************************************************************/
embeddedartists 0:6b68dac0d986 598
embeddedartists 0:6b68dac0d986 599 MCIFileSystem::MCIFileSystem(const char* name, PinName cd) :
embeddedartists 0:6b68dac0d986 600 FATFileSystem(name)
embeddedartists 0:6b68dac0d986 601 {
embeddedartists 0:6b68dac0d986 602 pUglyForIRQ = this;
embeddedartists 0:6b68dac0d986 603
embeddedartists 0:6b68dac0d986 604 _Stat = STA_NOINIT;
embeddedartists 0:6b68dac0d986 605 memset(&_sdCardInfo, 0, sizeof(SDMMC_CARD_T));
embeddedartists 0:6b68dac0d986 606 _eventReceived = false;
embeddedartists 0:6b68dac0d986 607 _eventSuccess = false;
embeddedartists 0:6b68dac0d986 608
embeddedartists 0:6b68dac0d986 609 initMCI();
embeddedartists 0:6b68dac0d986 610
embeddedartists 0:6b68dac0d986 611 if (cd == NC)
embeddedartists 0:6b68dac0d986 612 {
embeddedartists 0:6b68dac0d986 613 _cardDetect = NULL;
embeddedartists 0:6b68dac0d986 614 }
embeddedartists 0:6b68dac0d986 615 else
embeddedartists 0:6b68dac0d986 616 {
embeddedartists 0:6b68dac0d986 617 _cardDetect = new DigitalIn(cd);
embeddedartists 0:6b68dac0d986 618 _cardDetect->mode(PullUp);
embeddedartists 0:6b68dac0d986 619 }
embeddedartists 0:6b68dac0d986 620 }
embeddedartists 0:6b68dac0d986 621
embeddedartists 0:6b68dac0d986 622 MCIFileSystem::~MCIFileSystem()
embeddedartists 0:6b68dac0d986 623 {
embeddedartists 0:6b68dac0d986 624 if (_cardDetect != NULL)
embeddedartists 0:6b68dac0d986 625 {
embeddedartists 0:6b68dac0d986 626 delete _cardDetect;
embeddedartists 0:6b68dac0d986 627 }
embeddedartists 0:6b68dac0d986 628 }
embeddedartists 0:6b68dac0d986 629 void MCIFileSystem::initMCI()
embeddedartists 0:6b68dac0d986 630 {
embeddedartists 0:6b68dac0d986 631 // Pinsel for MCI
embeddedartists 0:6b68dac0d986 632 LPC_IOCON->P1_2 = 2; /* SD_CLK @ P1.2 */
embeddedartists 0:6b68dac0d986 633 LPC_IOCON->P1_3 = 2; /* SD_CMD @ P1.3 */
embeddedartists 0:6b68dac0d986 634 LPC_IOCON->P1_5 = 2 | (1<<7); /* SD_PWR @ P1.5 - digital mode */
embeddedartists 0:6b68dac0d986 635 LPC_IOCON->P1_6 = 2 | (1<<7); /* SD_DAT[0] @ P1.6 - digital mode */
embeddedartists 0:6b68dac0d986 636 LPC_IOCON->P1_7 = 2 | (1<<7); /* SD_DAT[1] @ P1.7 - digital mode */
embeddedartists 0:6b68dac0d986 637 LPC_IOCON->P1_11 = 2; /* SD_DAT[2] @ P1.11 */
embeddedartists 0:6b68dac0d986 638 LPC_IOCON->P1_12 = 2; /* SD_DAT[3] @ P1.12 */
embeddedartists 0:6b68dac0d986 639
embeddedartists 0:6b68dac0d986 640 LPC_SC->PCONP |= SYSCTL_CLOCK_SDC;
embeddedartists 0:6b68dac0d986 641 LPC_SC->RSTCON0 = (1<<28);
embeddedartists 0:6b68dac0d986 642 LPC_SC->RSTCON0 &= ~(1<<28);
embeddedartists 0:6b68dac0d986 643
embeddedartists 0:6b68dac0d986 644 /* Initialize SDC peripheral */
embeddedartists 0:6b68dac0d986 645 LPC_SC->PCONP |= SYSCTL_CLOCK_SDC;
embeddedartists 0:6b68dac0d986 646
embeddedartists 0:6b68dac0d986 647 /* Disable SD_CLK */
embeddedartists 0:6b68dac0d986 648 mci_ClockControl(SDC_CLOCK_ENABLE, false);
embeddedartists 0:6b68dac0d986 649
embeddedartists 0:6b68dac0d986 650 /* Power-off */
embeddedartists 0:6b68dac0d986 651 mci_PowerControl(PowerOff, 0);
embeddedartists 0:6b68dac0d986 652 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 653
embeddedartists 0:6b68dac0d986 654 /* Disable all interrupts */
embeddedartists 0:6b68dac0d986 655 LPC_MCI->MASK0 = 0;
embeddedartists 0:6b68dac0d986 656
embeddedartists 0:6b68dac0d986 657 /*Setting for timeout problem */
embeddedartists 0:6b68dac0d986 658 LPC_MCI->DATATMR = 0x1FFFFFFF;
embeddedartists 0:6b68dac0d986 659
embeddedartists 0:6b68dac0d986 660 LPC_MCI->COMMAND = 0;
embeddedartists 0:6b68dac0d986 661 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 662
embeddedartists 0:6b68dac0d986 663 LPC_MCI->DATACTRL = 0;
embeddedartists 0:6b68dac0d986 664 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 665
embeddedartists 0:6b68dac0d986 666 /* clear all pending interrupts */
embeddedartists 0:6b68dac0d986 667 LPC_MCI->CLEAR = SDC_CLEAR_ALL;
embeddedartists 0:6b68dac0d986 668
embeddedartists 0:6b68dac0d986 669 /* Power-up SDC Peripheral */
embeddedartists 0:6b68dac0d986 670 mci_PowerControl(PowerUp, 0);
embeddedartists 0:6b68dac0d986 671
embeddedartists 0:6b68dac0d986 672 /* delays for the supply output is stable*/
embeddedartists 0:6b68dac0d986 673 for (uint32_t i = 0; i < 0x80000; i++ ) {}
embeddedartists 0:6b68dac0d986 674
embeddedartists 0:6b68dac0d986 675 mci_SetClock(SDC_IDENT_CLOCK_RATE);
embeddedartists 0:6b68dac0d986 676 mci_ClockControl(SDC_CLOCK_ENABLE, true);
embeddedartists 0:6b68dac0d986 677
embeddedartists 0:6b68dac0d986 678 /* Power-on SDC Interface */
embeddedartists 0:6b68dac0d986 679 mci_PowerControl(PowerOn, 0);
embeddedartists 0:6b68dac0d986 680
embeddedartists 0:6b68dac0d986 681 NVIC_SetVector(MCI_IRQn, (uint32_t) mymciirq);
embeddedartists 0:6b68dac0d986 682 NVIC_EnableIRQ(MCI_IRQn);
embeddedartists 0:6b68dac0d986 683
embeddedartists 38:420cdc281467 684 /* Initialize GPDMA controller */
embeddedartists 38:420cdc281467 685 _eventDmaChannel = GPDMA::instance().acquireChannel(mydmairq);
embeddedartists 0:6b68dac0d986 686 }
embeddedartists 0:6b68dac0d986 687
embeddedartists 0:6b68dac0d986 688 int MCIFileSystem::disk_initialize() {
embeddedartists 0:6b68dac0d986 689
embeddedartists 0:6b68dac0d986 690 debug_if(MCI_DBG, "mcifs:disk_initialize(), _Stat = %#x\n", _Stat);
embeddedartists 0:6b68dac0d986 691
embeddedartists 0:6b68dac0d986 692 if (!cardInserted()) {
embeddedartists 0:6b68dac0d986 693 /* No card in the socket */
embeddedartists 0:6b68dac0d986 694 _Stat = STA_NODISK | STA_NOINIT;
embeddedartists 0:6b68dac0d986 695 }
embeddedartists 0:6b68dac0d986 696
embeddedartists 0:6b68dac0d986 697 if (_Stat != STA_NOINIT) {
embeddedartists 0:6b68dac0d986 698 return _Stat; /* card is already enumerated */
embeddedartists 0:6b68dac0d986 699 }
embeddedartists 0:6b68dac0d986 700
embeddedartists 0:6b68dac0d986 701 //rtc_init();
embeddedartists 0:6b68dac0d986 702
embeddedartists 0:6b68dac0d986 703 /* Initialize the Card Data Strucutre */
embeddedartists 0:6b68dac0d986 704 memset(&_sdCardInfo, 0, sizeof(SDMMC_CARD_T));
embeddedartists 0:6b68dac0d986 705
embeddedartists 0:6b68dac0d986 706 /* Reset */
embeddedartists 0:6b68dac0d986 707 _Stat = STA_NOINIT;
embeddedartists 0:6b68dac0d986 708
embeddedartists 0:6b68dac0d986 709 /* Enumerate the card once detected. Note this function may block for a little while. */
embeddedartists 0:6b68dac0d986 710 int ret = mci_Acquire();
embeddedartists 0:6b68dac0d986 711 if (ret != 1) {
embeddedartists 0:6b68dac0d986 712 debug("Card Acquire failed... got %d, but expected 1\r\n", ret);
embeddedartists 0:6b68dac0d986 713 return 1;//Stat;
embeddedartists 0:6b68dac0d986 714 }
embeddedartists 0:6b68dac0d986 715
embeddedartists 0:6b68dac0d986 716 _Stat &= ~STA_NOINIT;
embeddedartists 0:6b68dac0d986 717 return _Stat;
embeddedartists 0:6b68dac0d986 718 }
embeddedartists 0:6b68dac0d986 719
embeddedartists 0:6b68dac0d986 720 int MCIFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number, uint8_t count) {
embeddedartists 0:6b68dac0d986 721 debug_if(MCI_DBG, "mcifs:disk_write(%#x, %llu, %u), _Stat = %#x\n", (uint32_t)buffer, block_number, count, _Stat);
embeddedartists 0:6b68dac0d986 722 if (_Stat & STA_NOINIT) {
embeddedartists 0:6b68dac0d986 723 // not ready
embeddedartists 0:6b68dac0d986 724 return 1;
embeddedartists 0:6b68dac0d986 725 }
embeddedartists 0:6b68dac0d986 726 if (mci_WriteBlocks((void*)buffer, block_number, count) == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 727 return 0;
embeddedartists 0:6b68dac0d986 728 }
embeddedartists 0:6b68dac0d986 729
embeddedartists 0:6b68dac0d986 730 return 1;
embeddedartists 0:6b68dac0d986 731 }
embeddedartists 0:6b68dac0d986 732
embeddedartists 0:6b68dac0d986 733 int MCIFileSystem::disk_read(uint8_t *buffer, uint64_t block_number, uint8_t count) {
embeddedartists 0:6b68dac0d986 734 debug_if(MCI_DBG, "mcifs:disk_read(%#x, %llu, %u), _Stat = %#x\n", (uint32_t)buffer, block_number, count, _Stat);
embeddedartists 0:6b68dac0d986 735 if (_Stat & STA_NOINIT) {
embeddedartists 0:6b68dac0d986 736 // not ready
embeddedartists 0:6b68dac0d986 737 return _Stat;
embeddedartists 0:6b68dac0d986 738 }
embeddedartists 0:6b68dac0d986 739 if (mci_ReadBlocks(buffer, block_number, count) == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 740 return 0;
embeddedartists 0:6b68dac0d986 741 }
embeddedartists 0:6b68dac0d986 742
embeddedartists 0:6b68dac0d986 743 return 1;
embeddedartists 0:6b68dac0d986 744 }
embeddedartists 0:6b68dac0d986 745
embeddedartists 0:6b68dac0d986 746 int MCIFileSystem::disk_status()
embeddedartists 0:6b68dac0d986 747 {
embeddedartists 0:6b68dac0d986 748 debug_if(MCI_DBG, "mcifs:disk_status(), _Stat = %#x\n", _Stat);
embeddedartists 0:6b68dac0d986 749 return _Stat;
embeddedartists 0:6b68dac0d986 750 }
embeddedartists 0:6b68dac0d986 751
embeddedartists 0:6b68dac0d986 752 int MCIFileSystem::disk_sync()
embeddedartists 0:6b68dac0d986 753 {
embeddedartists 0:6b68dac0d986 754 debug_if(MCI_DBG, "mcifs:disk_sync(), _Stat = %#x\n", _Stat);
embeddedartists 0:6b68dac0d986 755 uint32_t end = us_ticker_read() + 50*1000; // 50ms
embeddedartists 0:6b68dac0d986 756 while (us_ticker_read() < end)
embeddedartists 0:6b68dac0d986 757 {
embeddedartists 0:6b68dac0d986 758 if (mci_GetCardStatus() & R1_READY_FOR_DATA)
embeddedartists 0:6b68dac0d986 759 {
embeddedartists 0:6b68dac0d986 760 // card is ready
embeddedartists 0:6b68dac0d986 761 return 0;
embeddedartists 0:6b68dac0d986 762 }
embeddedartists 0:6b68dac0d986 763 }
embeddedartists 0:6b68dac0d986 764 // timeout while waiting for card to get ready
embeddedartists 0:6b68dac0d986 765 return 1;
embeddedartists 0:6b68dac0d986 766 }
embeddedartists 0:6b68dac0d986 767
embeddedartists 0:6b68dac0d986 768 uint64_t MCIFileSystem::disk_sectors()
embeddedartists 0:6b68dac0d986 769 {
embeddedartists 0:6b68dac0d986 770 debug_if(MCI_DBG, "mcifs:disk_sectors(), _Stat = %#x, returning %llu\n", _Stat, _sdCardInfo.blocknr);
embeddedartists 0:6b68dac0d986 771 return _sdCardInfo.blocknr;
embeddedartists 0:6b68dac0d986 772 }
embeddedartists 0:6b68dac0d986 773
embeddedartists 0:6b68dac0d986 774 void MCIFileSystem::mci_MCIIRQHandler()
embeddedartists 0:6b68dac0d986 775 {
embeddedartists 0:6b68dac0d986 776 int32_t Ret;
embeddedartists 0:6b68dac0d986 777
embeddedartists 0:6b68dac0d986 778 Ret = mci_IRQHandler(NULL, 0, NULL, 0);
embeddedartists 0:6b68dac0d986 779 if(Ret < 0) {
embeddedartists 0:6b68dac0d986 780 _eventSuccess = false;
embeddedartists 0:6b68dac0d986 781 _eventReceived = true;
embeddedartists 0:6b68dac0d986 782 }
embeddedartists 0:6b68dac0d986 783 }
embeddedartists 0:6b68dac0d986 784
embeddedartists 0:6b68dac0d986 785 void MCIFileSystem::mci_DMAIRQHandler()
embeddedartists 0:6b68dac0d986 786 {
embeddedartists 38:420cdc281467 787 _eventSuccess = LPC_GPDMA->IntTCStat & (1<<_eventDmaChannel);
embeddedartists 0:6b68dac0d986 788 _eventReceived = true;
embeddedartists 38:420cdc281467 789 LPC_GPDMA->IntTCClear = (1<<_eventDmaChannel);
embeddedartists 38:420cdc281467 790 LPC_GPDMA->IntErrClr = (1<<_eventDmaChannel);
embeddedartists 0:6b68dac0d986 791 }
embeddedartists 0:6b68dac0d986 792
embeddedartists 0:6b68dac0d986 793 /******************************************************************************
embeddedartists 0:6b68dac0d986 794 * Private Functions
embeddedartists 0:6b68dac0d986 795 *****************************************************************************/
embeddedartists 0:6b68dac0d986 796
embeddedartists 38:420cdc281467 797 static bool gpdma_transfer_to_mci(GPDMA::DMAChannels ChannelNum,
embeddedartists 38:420cdc281467 798 uint32_t src,
embeddedartists 38:420cdc281467 799 uint32_t Size)
embeddedartists 38:420cdc281467 800 {
embeddedartists 38:420cdc281467 801 GPDMA::GPDMA_Channel_CFG_T cfg;
embeddedartists 38:420cdc281467 802 cfg.ChannelNum = ChannelNum;
embeddedartists 38:420cdc281467 803 cfg.TransferType = GPDMA::FlowControl_M2P_Ctrl_Periph;
embeddedartists 38:420cdc281467 804 cfg.TransferSize = Size;
embeddedartists 38:420cdc281467 805 cfg.TransferWidth = 0;
embeddedartists 38:420cdc281467 806 cfg.SrcAddr = src;
embeddedartists 38:420cdc281467 807 cfg.DstAddr = (uint32_t) (&LPC_MCI->FIFO);
embeddedartists 38:420cdc281467 808
embeddedartists 38:420cdc281467 809 uint32_t ctrl_word =
embeddedartists 38:420cdc281467 810 GPDMA_DMACCxControl_TransferSize((uint32_t) cfg.TransferSize)
embeddedartists 38:420cdc281467 811 | GPDMA_DMACCxControl_SBSize(GPDMA_BSIZE_8)
embeddedartists 38:420cdc281467 812 | GPDMA_DMACCxControl_DBSize(GPDMA_BSIZE_8)
embeddedartists 38:420cdc281467 813 | GPDMA_DMACCxControl_SWidth(GPDMA_WIDTH_WORD)
embeddedartists 38:420cdc281467 814 | GPDMA_DMACCxControl_DWidth(GPDMA_WIDTH_WORD)
embeddedartists 38:420cdc281467 815 | GPDMA_DMACCxControl_DestTransUseAHBMaster1
embeddedartists 38:420cdc281467 816 | GPDMA_DMACCxControl_SI
embeddedartists 38:420cdc281467 817 | GPDMA_DMACCxControl_I;
embeddedartists 38:420cdc281467 818
embeddedartists 38:420cdc281467 819 /* Select SD card interface in the DMA MUX*/
embeddedartists 38:420cdc281467 820 LPC_SC->DMAREQSEL &= ~(1 << 1);
embeddedartists 38:420cdc281467 821
embeddedartists 38:420cdc281467 822 return GPDMA::instance().transfer(&cfg, ctrl_word, 0, GPDMA_CONN_MEMORY, GPDMA_CONN_SDC);
embeddedartists 38:420cdc281467 823 }
embeddedartists 38:420cdc281467 824
embeddedartists 38:420cdc281467 825 static bool gpdma_transfer_from_mci(GPDMA::DMAChannels ChannelNum,
embeddedartists 38:420cdc281467 826 uint32_t dst,
embeddedartists 38:420cdc281467 827 uint32_t Size)
embeddedartists 38:420cdc281467 828 {
embeddedartists 38:420cdc281467 829 GPDMA::GPDMA_Channel_CFG_T cfg;
embeddedartists 38:420cdc281467 830 cfg.ChannelNum = ChannelNum;
embeddedartists 38:420cdc281467 831 cfg.TransferType = GPDMA::FlowControl_P2M_Ctrl_Periph;
embeddedartists 38:420cdc281467 832 cfg.TransferSize = Size;
embeddedartists 38:420cdc281467 833 cfg.TransferWidth = 0;
embeddedartists 38:420cdc281467 834 cfg.SrcAddr = (uint32_t) (&LPC_MCI->FIFO);
embeddedartists 38:420cdc281467 835 cfg.DstAddr = dst;
embeddedartists 38:420cdc281467 836
embeddedartists 38:420cdc281467 837 uint32_t ctrl_word =
embeddedartists 38:420cdc281467 838 GPDMA_DMACCxControl_TransferSize((uint32_t) cfg.TransferSize)
embeddedartists 38:420cdc281467 839 | GPDMA_DMACCxControl_SBSize(GPDMA_BSIZE_8)
embeddedartists 38:420cdc281467 840 | GPDMA_DMACCxControl_DBSize(GPDMA_BSIZE_8)
embeddedartists 38:420cdc281467 841 | GPDMA_DMACCxControl_SWidth(GPDMA_WIDTH_WORD)
embeddedartists 38:420cdc281467 842 | GPDMA_DMACCxControl_DWidth(GPDMA_WIDTH_WORD)
embeddedartists 38:420cdc281467 843 | GPDMA_DMACCxControl_SrcTransUseAHBMaster1
embeddedartists 38:420cdc281467 844 | GPDMA_DMACCxControl_DI
embeddedartists 38:420cdc281467 845 | GPDMA_DMACCxControl_I;
embeddedartists 38:420cdc281467 846
embeddedartists 38:420cdc281467 847 /* Select SD card interface in the DMA MUX*/
embeddedartists 38:420cdc281467 848 LPC_SC->DMAREQSEL &= ~(1 << 1);
embeddedartists 38:420cdc281467 849
embeddedartists 38:420cdc281467 850 return GPDMA::instance().transfer(&cfg, ctrl_word, 0, GPDMA_CONN_SDC, GPDMA_CONN_MEMORY);
embeddedartists 38:420cdc281467 851 }
embeddedartists 38:420cdc281467 852
embeddedartists 0:6b68dac0d986 853 bool MCIFileSystem::cardInserted() const
embeddedartists 0:6b68dac0d986 854 {
embeddedartists 0:6b68dac0d986 855 // If no card detect pin is given, then assume that a card is inserted.
embeddedartists 0:6b68dac0d986 856 // If a pin is specified then use that to determing the presence of a card.
embeddedartists 0:6b68dac0d986 857 return ((_cardDetect == NULL) || (_cardDetect->read() == 0));
embeddedartists 0:6b68dac0d986 858 }
embeddedartists 0:6b68dac0d986 859
embeddedartists 0:6b68dac0d986 860
embeddedartists 0:6b68dac0d986 861
embeddedartists 0:6b68dac0d986 862 int32_t MCIFileSystem::mci_Acquire()
embeddedartists 0:6b68dac0d986 863 {
embeddedartists 0:6b68dac0d986 864 int32_t Ret;
embeddedartists 0:6b68dac0d986 865
embeddedartists 0:6b68dac0d986 866 /* Initialize card info */
embeddedartists 0:6b68dac0d986 867 _sdCardInfo.speed = SDC_TRAN_CLOCK_RATE;
embeddedartists 0:6b68dac0d986 868 _sdCardInfo.card_type = 0;
embeddedartists 0:6b68dac0d986 869
embeddedartists 0:6b68dac0d986 870 /* During identification phase, the clock should be less than
embeddedartists 0:6b68dac0d986 871 400Khz. Once we pass this phase, the normal clock can be set up
embeddedartists 0:6b68dac0d986 872 to 25Mhz on SD card and 20Mhz on MMC card. */
embeddedartists 0:6b68dac0d986 873 mci_SetClock(SDC_IDENT_CLOCK_RATE);
embeddedartists 0:6b68dac0d986 874
embeddedartists 0:6b68dac0d986 875 /* Clear Open Drain output control for SD */
embeddedartists 0:6b68dac0d986 876 mci_PowerControl(PowerOn, 0);
embeddedartists 0:6b68dac0d986 877
embeddedartists 0:6b68dac0d986 878 /* Card Reset */
embeddedartists 0:6b68dac0d986 879 Ret = mci_ExecuteCmd(SD_GO_IDLE_STATE, 0, NULL);
embeddedartists 0:6b68dac0d986 880 if (Ret != 0) {
embeddedartists 0:6b68dac0d986 881 return Ret;
embeddedartists 0:6b68dac0d986 882 }
embeddedartists 0:6b68dac0d986 883
embeddedartists 31:d47cffcb0a3e 884 Thread::wait(ACQUIRE_DELAY);
embeddedartists 0:6b68dac0d986 885
embeddedartists 0:6b68dac0d986 886 /* Send interface operation condiftion */
embeddedartists 0:6b68dac0d986 887 Ret = mci_SendIfCond();
embeddedartists 0:6b68dac0d986 888 if (Ret == SDC_RET_BAD_PARAMETERS) {
embeddedartists 0:6b68dac0d986 889 return Ret; /* Non-compatible voltage range or check pattern is not correct */
embeddedartists 0:6b68dac0d986 890
embeddedartists 0:6b68dac0d986 891 }
embeddedartists 0:6b68dac0d986 892 /* Get Card Type */
embeddedartists 0:6b68dac0d986 893 if (Ret == SDC_RET_OK) {/* Ver2.00 or later SD Memory Card*/
embeddedartists 0:6b68dac0d986 894 bool CCS;
embeddedartists 0:6b68dac0d986 895 uint32_t OCR = SDC_OCR_27_36;
embeddedartists 0:6b68dac0d986 896 _sdCardInfo.card_type |= CARD_TYPE_SD;
embeddedartists 0:6b68dac0d986 897 Ret = mci_SendAppOpCond(0, true, &OCR, &CCS);
embeddedartists 0:6b68dac0d986 898 if (CCS) { /* High Capacity or Extended Capacity SD Memory Card */
embeddedartists 0:6b68dac0d986 899 _sdCardInfo.card_type |= CARD_TYPE_HC;
embeddedartists 0:6b68dac0d986 900 }
embeddedartists 0:6b68dac0d986 901 }
embeddedartists 0:6b68dac0d986 902 else { /*Ver2.00 or later SD Memory Card(voltage mismatch) or Ver1.X SD Memory Card
embeddedartists 0:6b68dac0d986 903 or not SD Memory Card*/
embeddedartists 0:6b68dac0d986 904 bool CCS;
embeddedartists 0:6b68dac0d986 905 uint32_t OCR = SDC_OCR_27_36;
embeddedartists 0:6b68dac0d986 906 Ret = mci_SendAppOpCond(0, false, &OCR, &CCS);
embeddedartists 0:6b68dac0d986 907 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 908 _sdCardInfo.card_type |= CARD_TYPE_SD;
embeddedartists 0:6b68dac0d986 909 }
embeddedartists 0:6b68dac0d986 910 else if (Ret == SDC_RET_BAD_PARAMETERS) {
embeddedartists 0:6b68dac0d986 911 return Ret;
embeddedartists 0:6b68dac0d986 912 }
embeddedartists 0:6b68dac0d986 913 else { /* MMC Card setup */
embeddedartists 0:6b68dac0d986 914 uint32_t OCR;
embeddedartists 0:6b68dac0d986 915 /* Enter to Open Drain mode */
embeddedartists 0:6b68dac0d986 916 mci_PowerControl(PowerOn, SDC_PWR_OPENDRAIN);
embeddedartists 31:d47cffcb0a3e 917 Thread::wait(ACQUIRE_DELAY);
embeddedartists 0:6b68dac0d986 918 Ret = mci_SendOpCond(&OCR);
embeddedartists 0:6b68dac0d986 919 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 920 return Ret;
embeddedartists 0:6b68dac0d986 921 }
embeddedartists 0:6b68dac0d986 922
embeddedartists 0:6b68dac0d986 923 }
embeddedartists 0:6b68dac0d986 924 }
embeddedartists 0:6b68dac0d986 925
embeddedartists 0:6b68dac0d986 926 /* Read CID */
embeddedartists 0:6b68dac0d986 927 mci_GetCID(_sdCardInfo.cid);
embeddedartists 0:6b68dac0d986 928
embeddedartists 0:6b68dac0d986 929 /* RCA send, for SD get RCA */
embeddedartists 0:6b68dac0d986 930 if (_sdCardInfo.card_type & CARD_TYPE_SD) {
embeddedartists 0:6b68dac0d986 931 mci_GetAddr(&_sdCardInfo.rca);
embeddedartists 0:6b68dac0d986 932 }
embeddedartists 0:6b68dac0d986 933 else {
embeddedartists 0:6b68dac0d986 934 _sdCardInfo.rca = 1;
embeddedartists 0:6b68dac0d986 935 mci_SetAddr(_sdCardInfo.rca);
embeddedartists 0:6b68dac0d986 936 mci_PowerControl(PowerOn, 0); /* enter to push-pull mode */
embeddedartists 0:6b68dac0d986 937 }
embeddedartists 0:6b68dac0d986 938
embeddedartists 0:6b68dac0d986 939 /* Get CSD */
embeddedartists 0:6b68dac0d986 940 mci_GetCSD(_sdCardInfo.rca, _sdCardInfo.csd);
embeddedartists 0:6b68dac0d986 941
embeddedartists 0:6b68dac0d986 942 /* Compute card size, block size and no. of blocks based on CSD response recived. */
embeddedartists 0:6b68dac0d986 943 if (_sdCardInfo.cid[0]) {
embeddedartists 0:6b68dac0d986 944 mci_ProcessCSD();
embeddedartists 0:6b68dac0d986 945
embeddedartists 0:6b68dac0d986 946 if (mci_SetTranState(_sdCardInfo.rca) != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 947 return 0;
embeddedartists 0:6b68dac0d986 948 }
embeddedartists 0:6b68dac0d986 949
embeddedartists 0:6b68dac0d986 950 if (mci_GetCardState() != SDMMC_TRAN_ST) {
embeddedartists 0:6b68dac0d986 951 return 0;
embeddedartists 0:6b68dac0d986 952 }
embeddedartists 0:6b68dac0d986 953
embeddedartists 0:6b68dac0d986 954 if (mci_SetCardParams() != 0) {
embeddedartists 0:6b68dac0d986 955 return 0;
embeddedartists 0:6b68dac0d986 956 }
embeddedartists 0:6b68dac0d986 957 }
embeddedartists 0:6b68dac0d986 958
embeddedartists 0:6b68dac0d986 959 return (_sdCardInfo.cid[0]) ? 1 : 0;
embeddedartists 0:6b68dac0d986 960 }
embeddedartists 0:6b68dac0d986 961
embeddedartists 0:6b68dac0d986 962 uint32_t MCIFileSystem::mci_GetCardStatus() const
embeddedartists 0:6b68dac0d986 963 {
embeddedartists 0:6b68dac0d986 964 uint32_t Status;
embeddedartists 0:6b68dac0d986 965 mci_GetStatus(_sdCardInfo.rca, &Status);
embeddedartists 0:6b68dac0d986 966 return Status;
embeddedartists 0:6b68dac0d986 967 }
embeddedartists 0:6b68dac0d986 968
embeddedartists 0:6b68dac0d986 969 MCIFileSystem::CardState MCIFileSystem::mci_GetCardState() const
embeddedartists 0:6b68dac0d986 970 {
embeddedartists 0:6b68dac0d986 971 uint32_t Status;
embeddedartists 0:6b68dac0d986 972 volatile int32_t Ret;
embeddedartists 0:6b68dac0d986 973
embeddedartists 0:6b68dac0d986 974 /* get current state of the card */
embeddedartists 0:6b68dac0d986 975 Ret = mci_GetStatus(_sdCardInfo.rca, &Status);
embeddedartists 0:6b68dac0d986 976
embeddedartists 0:6b68dac0d986 977 /* check card state in response */
embeddedartists 0:6b68dac0d986 978 return (CardState) R1_CURRENT_STATE(Status);
embeddedartists 0:6b68dac0d986 979 }
embeddedartists 0:6b68dac0d986 980
embeddedartists 0:6b68dac0d986 981 MCIFileSystem::ReturnCode MCIFileSystem::mci_StopTransmission(uint32_t rca) const
embeddedartists 0:6b68dac0d986 982 {
embeddedartists 0:6b68dac0d986 983 uint32_t Status;
embeddedartists 0:6b68dac0d986 984 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 985 response_t Response;
embeddedartists 0:6b68dac0d986 986 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 987
embeddedartists 0:6b68dac0d986 988 Ret = mci_GetStatus(rca, &Status);
embeddedartists 0:6b68dac0d986 989 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 990 return SDC_RET_ERR_STATE;
embeddedartists 0:6b68dac0d986 991 }
embeddedartists 0:6b68dac0d986 992
embeddedartists 0:6b68dac0d986 993 if (R1_CURRENT_STATE(Status) == SDMMC_TRAN_ST) {
embeddedartists 0:6b68dac0d986 994 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 995 }
embeddedartists 0:6b68dac0d986 996
embeddedartists 0:6b68dac0d986 997 if ((R1_CURRENT_STATE(Status) != SDMMC_DATA_ST) &&
embeddedartists 0:6b68dac0d986 998 (R1_CURRENT_STATE(Status) != SDMMC_RCV_ST)) {
embeddedartists 0:6b68dac0d986 999 return SDC_RET_ERR_STATE;
embeddedartists 0:6b68dac0d986 1000 }
embeddedartists 0:6b68dac0d986 1001
embeddedartists 0:6b68dac0d986 1002 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1003 Ret = mci_ExecuteCmd(SD_CMD12_STOP_TRANSMISSION, 0, &Response);
embeddedartists 0:6b68dac0d986 1004 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1005 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1006 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1007 return Ret;
embeddedartists 0:6b68dac0d986 1008 }
embeddedartists 0:6b68dac0d986 1009 Ret = mci_GetStatus(rca, &Status);
embeddedartists 0:6b68dac0d986 1010 if ((R1_CURRENT_STATE(Status) == SDMMC_TRAN_ST) || (R1_CURRENT_STATE(Status) == SDMMC_PRG_ST)) {
embeddedartists 0:6b68dac0d986 1011 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1012 }
embeddedartists 0:6b68dac0d986 1013 return SDC_RET_ERR_STATE;
embeddedartists 0:6b68dac0d986 1014 }
embeddedartists 0:6b68dac0d986 1015 }
embeddedartists 0:6b68dac0d986 1016 RetryCnt--;
embeddedartists 0:6b68dac0d986 1017 }
embeddedartists 0:6b68dac0d986 1018 return Ret;
embeddedartists 0:6b68dac0d986 1019 }
embeddedartists 0:6b68dac0d986 1020
embeddedartists 0:6b68dac0d986 1021 MCIFileSystem::ReturnCode MCIFileSystem::mci_ReadBlocks(void *buffer, int32_t startBlock, int32_t blockNum)
embeddedartists 0:6b68dac0d986 1022 {
embeddedartists 0:6b68dac0d986 1023 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1024 int32_t ByteNum = blockNum * MMC_SECTOR_SIZE;
embeddedartists 0:6b68dac0d986 1025
embeddedartists 0:6b68dac0d986 1026 do
embeddedartists 0:6b68dac0d986 1027 {
embeddedartists 0:6b68dac0d986 1028 /* if card is not acquired return immediately */
embeddedartists 0:6b68dac0d986 1029 if (( startBlock < 0) || ( (startBlock + blockNum) > _sdCardInfo.blocknr) ) {
embeddedartists 0:6b68dac0d986 1030 Ret = SDC_RET_NOT_READY;
embeddedartists 0:6b68dac0d986 1031 break;
embeddedartists 0:6b68dac0d986 1032 }
embeddedartists 0:6b68dac0d986 1033
embeddedartists 0:6b68dac0d986 1034 /* Put to tran state */
embeddedartists 0:6b68dac0d986 1035 Ret = mci_SetTranState(_sdCardInfo.rca);
embeddedartists 0:6b68dac0d986 1036 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1037 break;
embeddedartists 0:6b68dac0d986 1038 }
embeddedartists 0:6b68dac0d986 1039
embeddedartists 0:6b68dac0d986 1040 LPC_MCI->MASK0 = SDC_MASK0_DATA | SDC_MASK0_RXDATAERR;
embeddedartists 0:6b68dac0d986 1041
embeddedartists 0:6b68dac0d986 1042 /* DMA Setup */
embeddedartists 38:420cdc281467 1043 mci_SetupEventWakeup();
embeddedartists 38:420cdc281467 1044 gpdma_transfer_from_mci(_eventDmaChannel, (uint32_t)buffer, ByteNum);
embeddedartists 0:6b68dac0d986 1045
embeddedartists 0:6b68dac0d986 1046 /* set transfer information */
embeddedartists 0:6b68dac0d986 1047 mci_SetDataTransfer(blockNum, true, DATA_TIMER_VALUE_R);
embeddedartists 0:6b68dac0d986 1048
embeddedartists 0:6b68dac0d986 1049 Ret = _readBlocks(_sdCardInfo.card_type, startBlock, blockNum);
embeddedartists 0:6b68dac0d986 1050 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1051 /* Wait for transfer Finish */
embeddedartists 0:6b68dac0d986 1052 if (mci_WaitForEvent() != 0) {
embeddedartists 0:6b68dac0d986 1053 Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1054 }
embeddedartists 0:6b68dac0d986 1055 } else {
embeddedartists 0:6b68dac0d986 1056 Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1057 }
embeddedartists 0:6b68dac0d986 1058
embeddedartists 38:420cdc281467 1059 GPDMA::instance().stopTransfer(_eventDmaChannel);
embeddedartists 0:6b68dac0d986 1060
embeddedartists 0:6b68dac0d986 1061 if ((blockNum > 1) || (mci_GetCardState() == SDMMC_DATA_ST)) {
embeddedartists 0:6b68dac0d986 1062 /* Send Stop transmission command */
embeddedartists 0:6b68dac0d986 1063 mci_StopTransmission(_sdCardInfo.rca);
embeddedartists 0:6b68dac0d986 1064 }
embeddedartists 0:6b68dac0d986 1065
embeddedartists 0:6b68dac0d986 1066 /* Wait for card to enter tran state */
embeddedartists 0:6b68dac0d986 1067 while (mci_GetCardState() != SDMMC_TRAN_ST) {}
embeddedartists 0:6b68dac0d986 1068
embeddedartists 0:6b68dac0d986 1069 } while(false);
embeddedartists 0:6b68dac0d986 1070
embeddedartists 0:6b68dac0d986 1071 return Ret;
embeddedartists 0:6b68dac0d986 1072 }
embeddedartists 0:6b68dac0d986 1073
embeddedartists 0:6b68dac0d986 1074 MCIFileSystem::ReturnCode MCIFileSystem::mci_WriteBlocks(void *buffer, int32_t startBlock, int32_t blockNum)
embeddedartists 0:6b68dac0d986 1075 {
embeddedartists 0:6b68dac0d986 1076 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1077
embeddedartists 0:6b68dac0d986 1078 do
embeddedartists 0:6b68dac0d986 1079 {
embeddedartists 0:6b68dac0d986 1080 /* if card is not acquired return immediately */
embeddedartists 0:6b68dac0d986 1081 if (( startBlock < 0) || ( (startBlock + blockNum) > _sdCardInfo.blocknr) ) {
embeddedartists 0:6b68dac0d986 1082 Ret = SDC_RET_NOT_READY;
embeddedartists 0:6b68dac0d986 1083 break;
embeddedartists 0:6b68dac0d986 1084 }
embeddedartists 0:6b68dac0d986 1085
embeddedartists 0:6b68dac0d986 1086 /* Put to tran state */
embeddedartists 0:6b68dac0d986 1087 Ret = mci_SetTranState(_sdCardInfo.rca);
embeddedartists 0:6b68dac0d986 1088 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1089 break;
embeddedartists 0:6b68dac0d986 1090 }
embeddedartists 0:6b68dac0d986 1091
embeddedartists 0:6b68dac0d986 1092 Ret = _writeBlocks(_sdCardInfo.card_type, startBlock, blockNum);
embeddedartists 0:6b68dac0d986 1093 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1094 break;
embeddedartists 0:6b68dac0d986 1095 }
embeddedartists 0:6b68dac0d986 1096
embeddedartists 0:6b68dac0d986 1097 /*Wait for card enter to rcv state*/
embeddedartists 0:6b68dac0d986 1098 while (mci_GetCardState() != SDMMC_RCV_ST) {}
embeddedartists 0:6b68dac0d986 1099
embeddedartists 0:6b68dac0d986 1100 LPC_MCI->MASK0 = SDC_MASK0_DATA | SDC_MASK0_TXDATAERR;
embeddedartists 0:6b68dac0d986 1101
embeddedartists 0:6b68dac0d986 1102 /* DMA Setup */
embeddedartists 38:420cdc281467 1103 mci_SetupEventWakeup();
embeddedartists 38:420cdc281467 1104 gpdma_transfer_to_mci(_eventDmaChannel, (uint32_t)buffer, blockNum*MMC_SECTOR_SIZE);
embeddedartists 0:6b68dac0d986 1105
embeddedartists 0:6b68dac0d986 1106 /* set transfer information */
embeddedartists 0:6b68dac0d986 1107 mci_SetDataTransfer(blockNum, false, DATA_TIMER_VALUE_W);
embeddedartists 0:6b68dac0d986 1108
embeddedartists 0:6b68dac0d986 1109 /* Wait for transfer done */
embeddedartists 0:6b68dac0d986 1110 if (mci_WaitForEvent() != 0) {
embeddedartists 0:6b68dac0d986 1111 Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1112 }
embeddedartists 0:6b68dac0d986 1113
embeddedartists 0:6b68dac0d986 1114 if ((blockNum > 1) || (mci_GetCardState() == SDMMC_RCV_ST)) {
embeddedartists 0:6b68dac0d986 1115 /* Send Stop transmission command */
embeddedartists 0:6b68dac0d986 1116 mci_StopTransmission(_sdCardInfo.rca);
embeddedartists 0:6b68dac0d986 1117 }
embeddedartists 0:6b68dac0d986 1118
embeddedartists 0:6b68dac0d986 1119 /* Wait for card to enter tran state */
embeddedartists 0:6b68dac0d986 1120 while (mci_GetCardState() != SDMMC_TRAN_ST) {}
embeddedartists 0:6b68dac0d986 1121
embeddedartists 0:6b68dac0d986 1122 } while (false);
embeddedartists 0:6b68dac0d986 1123
embeddedartists 0:6b68dac0d986 1124 return Ret;
embeddedartists 0:6b68dac0d986 1125 }
embeddedartists 0:6b68dac0d986 1126
embeddedartists 0:6b68dac0d986 1127 void MCIFileSystem::mci_SetClock(uint32_t freq) const
embeddedartists 0:6b68dac0d986 1128 {
embeddedartists 0:6b68dac0d986 1129 uint32_t PClk;
embeddedartists 0:6b68dac0d986 1130 uint32_t ClkValue = 0;
embeddedartists 0:6b68dac0d986 1131
embeddedartists 0:6b68dac0d986 1132 PClk = PeripheralClock;
embeddedartists 0:6b68dac0d986 1133
embeddedartists 0:6b68dac0d986 1134 ClkValue = (PClk + 2 * freq - 1) / (2 * freq);
embeddedartists 0:6b68dac0d986 1135 if (ClkValue > 0) {
embeddedartists 0:6b68dac0d986 1136 ClkValue -= 1;
embeddedartists 0:6b68dac0d986 1137 }
embeddedartists 0:6b68dac0d986 1138 uint32_t temp;
embeddedartists 0:6b68dac0d986 1139 temp = (LPC_MCI->CLOCK & (~SDC_CLOCK_CLKDIV_BITMASK));
embeddedartists 0:6b68dac0d986 1140 LPC_MCI->CLOCK = temp | (SDC_CLOCK_CLKDIV(ClkValue));
embeddedartists 0:6b68dac0d986 1141 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 1142 }
embeddedartists 0:6b68dac0d986 1143
embeddedartists 0:6b68dac0d986 1144 void MCIFileSystem::mci_ClockControl(ClockControl ctrlType, bool enable) const
embeddedartists 0:6b68dac0d986 1145 {
embeddedartists 0:6b68dac0d986 1146 if (enable) {
embeddedartists 0:6b68dac0d986 1147 LPC_MCI->CLOCK |= (1 << ctrlType);
embeddedartists 0:6b68dac0d986 1148 }
embeddedartists 0:6b68dac0d986 1149 else {
embeddedartists 0:6b68dac0d986 1150 LPC_MCI->CLOCK &= (~(1 << ctrlType));
embeddedartists 0:6b68dac0d986 1151 }
embeddedartists 0:6b68dac0d986 1152 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 1153 }
embeddedartists 0:6b68dac0d986 1154
embeddedartists 0:6b68dac0d986 1155 void MCIFileSystem::mci_PowerControl(power_ctrl_t powerMode, uint32_t flag) const
embeddedartists 0:6b68dac0d986 1156 {
embeddedartists 0:6b68dac0d986 1157 LPC_MCI->POWER = (powerMode & 0x3) | flag;
embeddedartists 0:6b68dac0d986 1158 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 1159 }
embeddedartists 0:6b68dac0d986 1160
embeddedartists 0:6b68dac0d986 1161 MCIFileSystem::ReturnCode MCIFileSystem::mci_ExecuteCmd(uint32_t Command, uint32_t Arg, response_t* pResp) const
embeddedartists 0:6b68dac0d986 1162 {
embeddedartists 0:6b68dac0d986 1163 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1164
embeddedartists 0:6b68dac0d986 1165 /* Send Command to card */
embeddedartists 0:6b68dac0d986 1166 Ret = mci_SendCmd(Command, Arg, CMD_TIMEOUT);
embeddedartists 0:6b68dac0d986 1167 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1168 return Ret;
embeddedartists 0:6b68dac0d986 1169 }
embeddedartists 0:6b68dac0d986 1170
embeddedartists 0:6b68dac0d986 1171 /* Get response (if any) */
embeddedartists 0:6b68dac0d986 1172 if ((Command & SDC_COMMAND_RSP_BITMASK) != SDC_COMMAND_NO_RSP) {
embeddedartists 0:6b68dac0d986 1173
embeddedartists 0:6b68dac0d986 1174 mci_GetResp(pResp);
embeddedartists 0:6b68dac0d986 1175
embeddedartists 0:6b68dac0d986 1176 /* If the response is not R1, in the response field, the Expected Cmd data
embeddedartists 0:6b68dac0d986 1177 won't be the same as the CMD data in SendCmd(). Below four cmds have
embeddedartists 0:6b68dac0d986 1178 R2 or R3 response. We don't need to check if MCI_RESP_CMD is the same
embeddedartists 0:6b68dac0d986 1179 as the Expected or not. */
embeddedartists 0:6b68dac0d986 1180 if ((SDC_COMMAND_INDEX(Command) != MMC_SEND_OP_COND) &&
embeddedartists 0:6b68dac0d986 1181 (SDC_COMMAND_INDEX(Command) != SD_APP_OP_COND) &&
embeddedartists 0:6b68dac0d986 1182 (SDC_COMMAND_INDEX(Command) != MMC_ALL_SEND_CID) &&
embeddedartists 0:6b68dac0d986 1183 (SDC_COMMAND_INDEX(Command) != MMC_SEND_CSD) &&
embeddedartists 0:6b68dac0d986 1184 (pResp->CmdIndex != SDC_COMMAND_INDEX(Command))) {
embeddedartists 0:6b68dac0d986 1185 return SDC_RET_CMD_FAILED;
embeddedartists 0:6b68dac0d986 1186 }
embeddedartists 0:6b68dac0d986 1187 }
embeddedartists 0:6b68dac0d986 1188
embeddedartists 0:6b68dac0d986 1189 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1190 }
embeddedartists 0:6b68dac0d986 1191
embeddedartists 0:6b68dac0d986 1192 MCIFileSystem::ReturnCode MCIFileSystem::mci_SendIfCond() const
embeddedartists 0:6b68dac0d986 1193 {
embeddedartists 0:6b68dac0d986 1194 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1195 response_t Response;
embeddedartists 0:6b68dac0d986 1196 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1197
embeddedartists 0:6b68dac0d986 1198 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1199 Ret = mci_ExecuteCmd(SD_CMD8_SEND_IF_COND, (CMD8_VOLTAGESUPPLIED_27_36 | CMD8_CHECKPATTERN(
embeddedartists 0:6b68dac0d986 1200 CMD8_DEF_PATTERN)), &Response);
embeddedartists 0:6b68dac0d986 1201 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1202 if ((Response.Data[0] & CMDRESP_R7_VOLTAGE_ACCEPTED) &&
embeddedartists 0:6b68dac0d986 1203 (CMDRESP_R7_CHECK_PATTERN(Response.Data[0]) == CMD8_DEF_PATTERN)) {
embeddedartists 0:6b68dac0d986 1204 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1205 }
embeddedartists 0:6b68dac0d986 1206 return SDC_RET_BAD_PARAMETERS;
embeddedartists 0:6b68dac0d986 1207 }
embeddedartists 0:6b68dac0d986 1208 RetryCnt--;
embeddedartists 0:6b68dac0d986 1209 }
embeddedartists 0:6b68dac0d986 1210 return Ret;
embeddedartists 0:6b68dac0d986 1211 }
embeddedartists 0:6b68dac0d986 1212
embeddedartists 0:6b68dac0d986 1213 MCIFileSystem::ReturnCode MCIFileSystem::mci_SendOpCond(uint32_t *pOCR) const
embeddedartists 0:6b68dac0d986 1214 {
embeddedartists 0:6b68dac0d986 1215 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1216 response_t Response;
embeddedartists 0:6b68dac0d986 1217 uint32_t RetryCnt = 0x200;
embeddedartists 0:6b68dac0d986 1218
embeddedartists 0:6b68dac0d986 1219 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1220 Ret = mci_ExecuteCmd(SD_CMD1_SEND_OP_COND, SDC_OCR_27_36, &Response);
embeddedartists 0:6b68dac0d986 1221 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1222 *pOCR = Response.Data[0];
embeddedartists 0:6b68dac0d986 1223 if (*pOCR & SDC_OCR_IDLE) {
embeddedartists 0:6b68dac0d986 1224 if ((Response.Data[0] & SDC_OCR_27_36) != SDC_OCR_27_36) {
embeddedartists 0:6b68dac0d986 1225 return SDC_RET_BAD_PARAMETERS;
embeddedartists 0:6b68dac0d986 1226 }
embeddedartists 0:6b68dac0d986 1227 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1228 }
embeddedartists 0:6b68dac0d986 1229 }
embeddedartists 0:6b68dac0d986 1230 RetryCnt--;
embeddedartists 0:6b68dac0d986 1231 }
embeddedartists 0:6b68dac0d986 1232 return SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1233 }
embeddedartists 0:6b68dac0d986 1234
embeddedartists 0:6b68dac0d986 1235 MCIFileSystem::ReturnCode MCIFileSystem::mci_SendAppOpCond(uint16_t rca, bool hcs, uint32_t *pOcr, bool *pCCS) const
embeddedartists 0:6b68dac0d986 1236 {
embeddedartists 0:6b68dac0d986 1237 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1238 response_t Response;
embeddedartists 0:6b68dac0d986 1239 uint32_t Argument;
embeddedartists 0:6b68dac0d986 1240 uint32_t RetryCnt = 0x2000; /* The host repeatedly issues ACMD41 for at least 1 second or
embeddedartists 0:6b68dac0d986 1241 until the busy bit are set to 1 */
embeddedartists 0:6b68dac0d986 1242
embeddedartists 0:6b68dac0d986 1243 Argument = ACMD41_OCR(*pOcr);
embeddedartists 0:6b68dac0d986 1244 if (hcs) {
embeddedartists 0:6b68dac0d986 1245 Argument |= ACMD41_HCS;
embeddedartists 0:6b68dac0d986 1246 }
embeddedartists 0:6b68dac0d986 1247
embeddedartists 0:6b68dac0d986 1248 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1249 Ret = mci_SendAppCmd(rca);
embeddedartists 0:6b68dac0d986 1250 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1251 Ret = mci_ExecuteCmd(SD_ACMD41_SD_SEND_OP_COND, Argument, &Response);
embeddedartists 0:6b68dac0d986 1252 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1253 if (Response.Data[0] & CMDRESP_R3_INIT_COMPLETE) {
embeddedartists 0:6b68dac0d986 1254 if (*pOcr == 0) {
embeddedartists 0:6b68dac0d986 1255 *pOcr = CMDRESP_R3_OCR_VAL(Response.Data[0]);
embeddedartists 0:6b68dac0d986 1256 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1257 }
embeddedartists 0:6b68dac0d986 1258 if ((CMDRESP_R3_OCR_VAL(Response.Data[0]) & *pOcr) != *pOcr) {
embeddedartists 0:6b68dac0d986 1259 return SDC_RET_BAD_PARAMETERS;
embeddedartists 0:6b68dac0d986 1260 }
embeddedartists 0:6b68dac0d986 1261 *pCCS = (Response.Data[0] & CMDRESP_R3_HC_CCS) ? true : false;
embeddedartists 0:6b68dac0d986 1262 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1263 }
embeddedartists 0:6b68dac0d986 1264 }
embeddedartists 0:6b68dac0d986 1265 }
embeddedartists 0:6b68dac0d986 1266 else {
embeddedartists 0:6b68dac0d986 1267 //If we abort here then some cards will go undetected, better to keep retrying
embeddedartists 0:6b68dac0d986 1268 //return Ret;
embeddedartists 0:6b68dac0d986 1269 }
embeddedartists 0:6b68dac0d986 1270 RetryCnt--;
embeddedartists 0:6b68dac0d986 1271 }
embeddedartists 0:6b68dac0d986 1272 return SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1273 }
embeddedartists 0:6b68dac0d986 1274
embeddedartists 0:6b68dac0d986 1275 MCIFileSystem::ReturnCode MCIFileSystem::mci_GetCID(uint32_t *pCID) const
embeddedartists 0:6b68dac0d986 1276 {
embeddedartists 0:6b68dac0d986 1277 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1278 response_t Response;
embeddedartists 0:6b68dac0d986 1279 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1280
embeddedartists 0:6b68dac0d986 1281 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1282 Ret = mci_ExecuteCmd(SD_CMD2_ALL_SEND_CID, 0, &Response);
embeddedartists 0:6b68dac0d986 1283 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1284 pCID[3] = Response.Data[0];
embeddedartists 0:6b68dac0d986 1285 pCID[2] = Response.Data[1];
embeddedartists 0:6b68dac0d986 1286 pCID[1] = Response.Data[2];
embeddedartists 0:6b68dac0d986 1287 pCID[0] = Response.Data[3];
embeddedartists 0:6b68dac0d986 1288 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1289 }
embeddedartists 0:6b68dac0d986 1290 RetryCnt--;
embeddedartists 0:6b68dac0d986 1291 }
embeddedartists 0:6b68dac0d986 1292 return Ret;
embeddedartists 0:6b68dac0d986 1293 }
embeddedartists 0:6b68dac0d986 1294
embeddedartists 0:6b68dac0d986 1295 MCIFileSystem::ReturnCode MCIFileSystem::mci_SetAddr(uint16_t addr) const
embeddedartists 0:6b68dac0d986 1296 {
embeddedartists 0:6b68dac0d986 1297 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1298 response_t Response;
embeddedartists 0:6b68dac0d986 1299 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1300
embeddedartists 0:6b68dac0d986 1301 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1302 Ret = mci_ExecuteCmd(SD_CMD3_SET_RELATIVE_ADDR, CMD3_RCA(addr), &Response);
embeddedartists 0:6b68dac0d986 1303 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1304 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1305 return Ret;
embeddedartists 0:6b68dac0d986 1306 }
embeddedartists 0:6b68dac0d986 1307 }
embeddedartists 0:6b68dac0d986 1308 RetryCnt--;
embeddedartists 0:6b68dac0d986 1309 }
embeddedartists 0:6b68dac0d986 1310 return Ret;
embeddedartists 0:6b68dac0d986 1311 }
embeddedartists 0:6b68dac0d986 1312
embeddedartists 0:6b68dac0d986 1313 MCIFileSystem::ReturnCode MCIFileSystem::mci_GetAddr(uint16_t *pRCA) const
embeddedartists 0:6b68dac0d986 1314 {
embeddedartists 0:6b68dac0d986 1315 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1316 response_t Response;
embeddedartists 0:6b68dac0d986 1317 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1318
embeddedartists 0:6b68dac0d986 1319 *pRCA = 0;
embeddedartists 0:6b68dac0d986 1320 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1321 Ret = mci_ExecuteCmd(SD_CMD3_SEND_RELATIVE_ADDR, 0, &Response);
embeddedartists 0:6b68dac0d986 1322 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1323 if (!(CMDRESP_R6_CARD_STATUS(Response.Data[0]) & R1_READY_FOR_DATA)) {
embeddedartists 0:6b68dac0d986 1324 Ret = SDC_RET_NOT_READY;
embeddedartists 0:6b68dac0d986 1325 }
embeddedartists 0:6b68dac0d986 1326 else if (R1_CURRENT_STATE(CMDRESP_R6_CARD_STATUS(Response.Data[0])) != SDMMC_STBY_ST) {
embeddedartists 0:6b68dac0d986 1327 Ret = SDC_RET_ERR_STATE;
embeddedartists 0:6b68dac0d986 1328 }
embeddedartists 0:6b68dac0d986 1329 else {
embeddedartists 0:6b68dac0d986 1330 *pRCA = CMDRESP_R6_RCA_VAL(Response.Data[0]);
embeddedartists 0:6b68dac0d986 1331 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1332 }
embeddedartists 0:6b68dac0d986 1333 }
embeddedartists 0:6b68dac0d986 1334 RetryCnt--;
embeddedartists 0:6b68dac0d986 1335 }
embeddedartists 0:6b68dac0d986 1336 return Ret;
embeddedartists 0:6b68dac0d986 1337 }
embeddedartists 0:6b68dac0d986 1338
embeddedartists 0:6b68dac0d986 1339 MCIFileSystem::ReturnCode MCIFileSystem::mci_GetCSD(uint16_t rca, uint32_t *pCSD) const
embeddedartists 0:6b68dac0d986 1340 {
embeddedartists 0:6b68dac0d986 1341 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1342 response_t Response;
embeddedartists 0:6b68dac0d986 1343 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1344
embeddedartists 0:6b68dac0d986 1345 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1346 Ret = mci_ExecuteCmd(SD_CMD9_SEND_CSD, CMD9_RCA(rca), &Response);
embeddedartists 0:6b68dac0d986 1347 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1348 pCSD[3] = Response.Data[0];
embeddedartists 0:6b68dac0d986 1349 pCSD[2] = Response.Data[1];
embeddedartists 0:6b68dac0d986 1350 pCSD[1] = Response.Data[2];
embeddedartists 0:6b68dac0d986 1351 pCSD[0] = Response.Data[3];
embeddedartists 0:6b68dac0d986 1352 return Ret;
embeddedartists 0:6b68dac0d986 1353 }
embeddedartists 0:6b68dac0d986 1354 RetryCnt--;
embeddedartists 0:6b68dac0d986 1355 }
embeddedartists 0:6b68dac0d986 1356 return Ret;
embeddedartists 0:6b68dac0d986 1357 }
embeddedartists 0:6b68dac0d986 1358
embeddedartists 0:6b68dac0d986 1359 MCIFileSystem::ReturnCode MCIFileSystem::mci_SelectCard(uint16_t addr) const
embeddedartists 0:6b68dac0d986 1360 {
embeddedartists 0:6b68dac0d986 1361 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1362 response_t Response;
embeddedartists 0:6b68dac0d986 1363 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1364
embeddedartists 0:6b68dac0d986 1365 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1366 Ret = mci_ExecuteCmd(SD_CMD7_SELECT_CARD, CMD7_RCA(addr), &Response);
embeddedartists 0:6b68dac0d986 1367 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1368 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1369 return Ret;
embeddedartists 0:6b68dac0d986 1370 }
embeddedartists 0:6b68dac0d986 1371 }
embeddedartists 0:6b68dac0d986 1372 RetryCnt--;
embeddedartists 0:6b68dac0d986 1373 }
embeddedartists 0:6b68dac0d986 1374 return Ret;
embeddedartists 0:6b68dac0d986 1375 }
embeddedartists 0:6b68dac0d986 1376
embeddedartists 0:6b68dac0d986 1377 MCIFileSystem::ReturnCode MCIFileSystem::mci_GetStatus(uint16_t rca, uint32_t *pStatus) const
embeddedartists 0:6b68dac0d986 1378 {
embeddedartists 0:6b68dac0d986 1379 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1380 response_t Response;
embeddedartists 0:6b68dac0d986 1381 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1382
embeddedartists 0:6b68dac0d986 1383 *pStatus = (uint32_t) -1;
embeddedartists 0:6b68dac0d986 1384 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1385 Ret = mci_ExecuteCmd(SD_CMD13_SEND_STATUS, CMD13_RCA(rca), &Response);
embeddedartists 0:6b68dac0d986 1386 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1387 mci_CheckR1Response(Response.Data[0], &Ret);
embeddedartists 0:6b68dac0d986 1388 *pStatus = Response.Data[0];
embeddedartists 0:6b68dac0d986 1389 return Ret;
embeddedartists 0:6b68dac0d986 1390 }
embeddedartists 0:6b68dac0d986 1391 RetryCnt--;
embeddedartists 0:6b68dac0d986 1392 }
embeddedartists 0:6b68dac0d986 1393 return Ret;
embeddedartists 0:6b68dac0d986 1394 }
embeddedartists 0:6b68dac0d986 1395
embeddedartists 0:6b68dac0d986 1396 void MCIFileSystem::mci_ProcessCSD()
embeddedartists 0:6b68dac0d986 1397 {
embeddedartists 0:6b68dac0d986 1398 int32_t CSize = 0;
embeddedartists 0:6b68dac0d986 1399 int32_t CSizeMult = 0;
embeddedartists 0:6b68dac0d986 1400 int32_t Mult = 0;
embeddedartists 0:6b68dac0d986 1401
embeddedartists 0:6b68dac0d986 1402 /* compute block length based on CSD response */
embeddedartists 0:6b68dac0d986 1403 _sdCardInfo.block_len = 1 << mci_GetBits(80, 83, _sdCardInfo.csd);
embeddedartists 0:6b68dac0d986 1404
embeddedartists 0:6b68dac0d986 1405 if ((_sdCardInfo.card_type & CARD_TYPE_HC) && (_sdCardInfo.card_type & CARD_TYPE_SD)) {
embeddedartists 0:6b68dac0d986 1406 /* See section 5.3.3 CSD Register (CSD Version 2.0) of SD2.0 spec an explanation for the calculation of these values */
embeddedartists 0:6b68dac0d986 1407 CSize = mci_GetBits(48, 63, (uint32_t *) _sdCardInfo.csd) + 1;
embeddedartists 0:6b68dac0d986 1408 _sdCardInfo.blocknr = CSize << 10; /* 512 byte blocks */
embeddedartists 0:6b68dac0d986 1409 }
embeddedartists 0:6b68dac0d986 1410 else {
embeddedartists 0:6b68dac0d986 1411 /* See section 5.3 of the 4.1 revision of the MMC specs for an explanation for the calculation of these values */
embeddedartists 0:6b68dac0d986 1412 CSize = mci_GetBits(62, 73, (uint32_t *) _sdCardInfo.csd);
embeddedartists 0:6b68dac0d986 1413 CSizeMult = mci_GetBits(47, 49, (uint32_t *) _sdCardInfo.csd);
embeddedartists 0:6b68dac0d986 1414 Mult = 1 << (CSizeMult + 2);
embeddedartists 0:6b68dac0d986 1415 _sdCardInfo.blocknr = (CSize + 1) * Mult;
embeddedartists 0:6b68dac0d986 1416
embeddedartists 0:6b68dac0d986 1417 /* adjust blocknr to 512/block */
embeddedartists 0:6b68dac0d986 1418 if (_sdCardInfo.block_len > MMC_SECTOR_SIZE) {
embeddedartists 0:6b68dac0d986 1419 _sdCardInfo.blocknr = _sdCardInfo.blocknr * (_sdCardInfo.block_len >> 9);
embeddedartists 0:6b68dac0d986 1420 }
embeddedartists 0:6b68dac0d986 1421 }
embeddedartists 0:6b68dac0d986 1422
embeddedartists 0:6b68dac0d986 1423 _sdCardInfo.device_size = _sdCardInfo.blocknr << 9; /* blocknr * 512 */
embeddedartists 0:6b68dac0d986 1424 }
embeddedartists 0:6b68dac0d986 1425
embeddedartists 0:6b68dac0d986 1426 MCIFileSystem::ReturnCode MCIFileSystem::mci_SetBusWidth(uint16_t rca, uint8_t width) const
embeddedartists 0:6b68dac0d986 1427 {
embeddedartists 0:6b68dac0d986 1428 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1429 response_t Response;
embeddedartists 0:6b68dac0d986 1430 uint8_t RetryCnt = 0x20;
embeddedartists 0:6b68dac0d986 1431
embeddedartists 0:6b68dac0d986 1432 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1433 Ret = mci_SendAppCmd(rca);
embeddedartists 0:6b68dac0d986 1434 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1435 Ret = mci_ExecuteCmd(SD_ACMD6_SET_BUS_WIDTH, ACMD6_BUS_WIDTH(width), &Response);
embeddedartists 0:6b68dac0d986 1436 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1437 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1438 return Ret;
embeddedartists 0:6b68dac0d986 1439 }
embeddedartists 0:6b68dac0d986 1440 }
embeddedartists 0:6b68dac0d986 1441 }
embeddedartists 0:6b68dac0d986 1442 RetryCnt--;
embeddedartists 0:6b68dac0d986 1443 }
embeddedartists 0:6b68dac0d986 1444 return SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1445 }
embeddedartists 0:6b68dac0d986 1446
embeddedartists 0:6b68dac0d986 1447 MCIFileSystem::ReturnCode MCIFileSystem::mci_SetTranState(uint16_t rca) const
embeddedartists 0:6b68dac0d986 1448 {
embeddedartists 0:6b68dac0d986 1449 ReturnCode Ret = SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1450 uint32_t status = 0;
embeddedartists 0:6b68dac0d986 1451 SDMMC_STATE_T state;
embeddedartists 0:6b68dac0d986 1452
embeddedartists 0:6b68dac0d986 1453 /* get current state of the card */
embeddedartists 0:6b68dac0d986 1454 Ret = mci_GetStatus(rca, &status);
embeddedartists 0:6b68dac0d986 1455 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1456 /* unable to get the card state. So return immediatly. */
embeddedartists 0:6b68dac0d986 1457 return Ret;
embeddedartists 0:6b68dac0d986 1458 }
embeddedartists 0:6b68dac0d986 1459
embeddedartists 0:6b68dac0d986 1460 /* check card state in response */
embeddedartists 0:6b68dac0d986 1461 state = (SDMMC_STATE_T) R1_CURRENT_STATE(status);
embeddedartists 0:6b68dac0d986 1462 switch (state) {
embeddedartists 0:6b68dac0d986 1463 case SDMMC_STBY_ST:
embeddedartists 0:6b68dac0d986 1464 /* put card in 'Trans' state */
embeddedartists 0:6b68dac0d986 1465 Ret = mci_SelectCard(rca);
embeddedartists 0:6b68dac0d986 1466 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1467 /* unable to put the card in Trans state. So return immediatly. */
embeddedartists 0:6b68dac0d986 1468 return Ret;
embeddedartists 0:6b68dac0d986 1469 }
embeddedartists 0:6b68dac0d986 1470 mci_GetStatus(rca, &status);
embeddedartists 0:6b68dac0d986 1471 if (((SDMMC_STATE_T) R1_CURRENT_STATE(status)) != SDMMC_TRAN_ST) {
embeddedartists 0:6b68dac0d986 1472 return SDC_RET_ERR_STATE;
embeddedartists 0:6b68dac0d986 1473 }
embeddedartists 0:6b68dac0d986 1474 break;
embeddedartists 0:6b68dac0d986 1475
embeddedartists 0:6b68dac0d986 1476 case SDMMC_TRAN_ST:
embeddedartists 0:6b68dac0d986 1477 /*do nothing */
embeddedartists 0:6b68dac0d986 1478 break;
embeddedartists 0:6b68dac0d986 1479
embeddedartists 0:6b68dac0d986 1480 default:
embeddedartists 0:6b68dac0d986 1481 /* card shouldn't be in other states so return */
embeddedartists 0:6b68dac0d986 1482 return SDC_RET_ERR_STATE;
embeddedartists 0:6b68dac0d986 1483 }
embeddedartists 0:6b68dac0d986 1484
embeddedartists 0:6b68dac0d986 1485 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1486 }
embeddedartists 0:6b68dac0d986 1487
embeddedartists 0:6b68dac0d986 1488 MCIFileSystem::ReturnCode MCIFileSystem::mci_SetBlockLength(uint32_t rca, uint32_t block_len) const
embeddedartists 0:6b68dac0d986 1489 {
embeddedartists 0:6b68dac0d986 1490 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1491 response_t Response;
embeddedartists 0:6b68dac0d986 1492 uint8_t RetryCnt = 0x20;
embeddedartists 0:6b68dac0d986 1493
embeddedartists 0:6b68dac0d986 1494 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1495 Ret = mci_ExecuteCmd(SD_CMD16_SET_BLOCKLEN, block_len, &Response);
embeddedartists 0:6b68dac0d986 1496 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1497 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1498 return Ret;
embeddedartists 0:6b68dac0d986 1499 }
embeddedartists 0:6b68dac0d986 1500 }
embeddedartists 0:6b68dac0d986 1501 RetryCnt--;
embeddedartists 0:6b68dac0d986 1502 }
embeddedartists 0:6b68dac0d986 1503 return SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1504 }
embeddedartists 0:6b68dac0d986 1505
embeddedartists 0:6b68dac0d986 1506 MCIFileSystem::ReturnCode MCIFileSystem::mci_SetCardParams() const
embeddedartists 0:6b68dac0d986 1507 {
embeddedartists 0:6b68dac0d986 1508 ReturnCode Ret;
embeddedartists 0:6b68dac0d986 1509
embeddedartists 0:6b68dac0d986 1510 mci_SetClock(SDC_TRAN_CLOCK_RATE);
embeddedartists 0:6b68dac0d986 1511 if (_sdCardInfo.card_type & CARD_TYPE_SD) {
embeddedartists 0:6b68dac0d986 1512 mci_ClockControl(SDC_CLOCK_WIDEBUS_MODE, true);
embeddedartists 0:6b68dac0d986 1513 Ret = mci_SetBusWidth(_sdCardInfo.rca, ACMD6_BUS_WIDTH_4);
embeddedartists 0:6b68dac0d986 1514 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1515 return Ret;
embeddedartists 0:6b68dac0d986 1516 }
embeddedartists 0:6b68dac0d986 1517 }
embeddedartists 0:6b68dac0d986 1518 else {
embeddedartists 0:6b68dac0d986 1519 mci_ClockControl(SDC_CLOCK_WIDEBUS_MODE, false);
embeddedartists 0:6b68dac0d986 1520 }
embeddedartists 0:6b68dac0d986 1521
embeddedartists 0:6b68dac0d986 1522 /* set block length */
embeddedartists 0:6b68dac0d986 1523 Ret = mci_SetBlockLength(_sdCardInfo.rca, MMC_SECTOR_SIZE);
embeddedartists 0:6b68dac0d986 1524 return Ret;
embeddedartists 0:6b68dac0d986 1525 }
embeddedartists 0:6b68dac0d986 1526
embeddedartists 0:6b68dac0d986 1527 bool MCIFileSystem::mci_CheckR1Response(uint32_t resp, ReturnCode* pCheckResult) const
embeddedartists 0:6b68dac0d986 1528 {
embeddedartists 0:6b68dac0d986 1529 bool Ret = true;
embeddedartists 0:6b68dac0d986 1530
embeddedartists 0:6b68dac0d986 1531 if (!(resp & R1_READY_FOR_DATA)) {
embeddedartists 0:6b68dac0d986 1532 *pCheckResult = SDC_RET_NOT_READY;
embeddedartists 0:6b68dac0d986 1533 Ret = false;
embeddedartists 0:6b68dac0d986 1534 }
embeddedartists 0:6b68dac0d986 1535 else if (R1_STATUS(resp)) {
embeddedartists 0:6b68dac0d986 1536 *pCheckResult = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1537 }
embeddedartists 0:6b68dac0d986 1538 else {
embeddedartists 0:6b68dac0d986 1539 *pCheckResult = SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1540 }
embeddedartists 0:6b68dac0d986 1541 return Ret;
embeddedartists 0:6b68dac0d986 1542 }
embeddedartists 0:6b68dac0d986 1543
embeddedartists 0:6b68dac0d986 1544 void MCIFileSystem::mci_WriteDelay() const
embeddedartists 0:6b68dac0d986 1545 {
embeddedartists 31:d47cffcb0a3e 1546 wait_us(10);
embeddedartists 0:6b68dac0d986 1547 }
embeddedartists 0:6b68dac0d986 1548
embeddedartists 0:6b68dac0d986 1549 MCIFileSystem::ReturnCode MCIFileSystem::mci_SendCmd(uint32_t Command, uint32_t Arg, uint32_t timeout) const
embeddedartists 0:6b68dac0d986 1550 {
embeddedartists 0:6b68dac0d986 1551 ReturnCode ret = SDC_RET_TIMEOUT;
embeddedartists 0:6b68dac0d986 1552 uint32_t Status;
embeddedartists 0:6b68dac0d986 1553
embeddedartists 0:6b68dac0d986 1554 /* Set Command Info */
embeddedartists 0:6b68dac0d986 1555 mci_SetCommand(Command, Arg);
embeddedartists 0:6b68dac0d986 1556
embeddedartists 0:6b68dac0d986 1557 while (timeout) {
embeddedartists 0:6b68dac0d986 1558
embeddedartists 0:6b68dac0d986 1559 Status = LPC_MCI->STATUS;
embeddedartists 0:6b68dac0d986 1560
embeddedartists 0:6b68dac0d986 1561 /* check if command was sent */
embeddedartists 0:6b68dac0d986 1562 if (((Command & SDC_COMMAND_RSP_BITMASK) == SDC_COMMAND_NO_RSP) && (Status & SDC_STATUS_CMDSENT)) {
embeddedartists 0:6b68dac0d986 1563 ret = SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1564 break;
embeddedartists 0:6b68dac0d986 1565 }
embeddedartists 0:6b68dac0d986 1566 /* check if response was received */
embeddedartists 0:6b68dac0d986 1567 if (Status & SDC_STATUS_CMDRESPEND) {
embeddedartists 0:6b68dac0d986 1568 ret = SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1569 break;
embeddedartists 0:6b68dac0d986 1570 }
embeddedartists 0:6b68dac0d986 1571
embeddedartists 0:6b68dac0d986 1572 /* check command sending status */
embeddedartists 0:6b68dac0d986 1573 if (Status & SDC_STATUS_CMDERR) {
embeddedartists 0:6b68dac0d986 1574 if (Status & SDC_STATUS_CMDCRCFAIL) {
embeddedartists 0:6b68dac0d986 1575 if ((SDC_COMMAND_INDEX(Command) == MMC_SEND_OP_COND) ||
embeddedartists 0:6b68dac0d986 1576 (SDC_COMMAND_INDEX(Command) == SD_APP_OP_COND) ||
embeddedartists 0:6b68dac0d986 1577 (SDC_COMMAND_INDEX(Command) == MMC_STOP_TRANSMISSION)) {
embeddedartists 0:6b68dac0d986 1578 ret = SDC_RET_OK; /* ignore CRC error if it's a resp for SEND_OP_COND or STOP_TRANSMISSION. */
embeddedartists 0:6b68dac0d986 1579 break;
embeddedartists 0:6b68dac0d986 1580 }
embeddedartists 0:6b68dac0d986 1581 }
embeddedartists 0:6b68dac0d986 1582 ret = SDC_RET_CMD_FAILED;
embeddedartists 0:6b68dac0d986 1583 break;
embeddedartists 0:6b68dac0d986 1584 }
embeddedartists 0:6b68dac0d986 1585
embeddedartists 0:6b68dac0d986 1586 timeout--;
embeddedartists 0:6b68dac0d986 1587 }
embeddedartists 0:6b68dac0d986 1588
embeddedartists 0:6b68dac0d986 1589 mci_ResetCommand();
embeddedartists 0:6b68dac0d986 1590
embeddedartists 0:6b68dac0d986 1591 return ret;
embeddedartists 0:6b68dac0d986 1592 }
embeddedartists 0:6b68dac0d986 1593
embeddedartists 0:6b68dac0d986 1594 MCIFileSystem::ReturnCode MCIFileSystem::mci_SendAppCmd(uint16_t rca) const
embeddedartists 0:6b68dac0d986 1595 {
embeddedartists 0:6b68dac0d986 1596 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1597 response_t Response;
embeddedartists 0:6b68dac0d986 1598 uint32_t RetryCnt = 20;
embeddedartists 0:6b68dac0d986 1599
embeddedartists 0:6b68dac0d986 1600 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1601 Ret = mci_ExecuteCmd(SD_CMD55_APP_CMD, CMD55_RCA(rca), &Response);
embeddedartists 0:6b68dac0d986 1602 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1603 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1604 if (Ret != SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1605 return Ret;
embeddedartists 0:6b68dac0d986 1606 }
embeddedartists 0:6b68dac0d986 1607 if (Response.Data[0] & R1_APP_CMD) {
embeddedartists 0:6b68dac0d986 1608 return SDC_RET_OK;
embeddedartists 0:6b68dac0d986 1609 }
embeddedartists 0:6b68dac0d986 1610 else {
embeddedartists 0:6b68dac0d986 1611 Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1612 }
embeddedartists 0:6b68dac0d986 1613 }
embeddedartists 0:6b68dac0d986 1614 }
embeddedartists 0:6b68dac0d986 1615 RetryCnt--;
embeddedartists 0:6b68dac0d986 1616 }
embeddedartists 0:6b68dac0d986 1617 return SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1618 }
embeddedartists 0:6b68dac0d986 1619
embeddedartists 0:6b68dac0d986 1620 void MCIFileSystem::mci_SetDataTransfer(uint16_t BlockNum, bool DirFromCard, uint32_t Timeout) const
embeddedartists 0:6b68dac0d986 1621 {
embeddedartists 0:6b68dac0d986 1622 uint32_t DataCtrl = 0;
embeddedartists 0:6b68dac0d986 1623 LPC_MCI->DATATMR = Timeout;
embeddedartists 0:6b68dac0d986 1624 LPC_MCI->DATALEN = BlockNum * 512;
embeddedartists 0:6b68dac0d986 1625
embeddedartists 0:6b68dac0d986 1626 DataCtrl = SDC_DATACTRL_ENABLE;
embeddedartists 0:6b68dac0d986 1627 // DataCtrl mode=block, block size=512byte
embeddedartists 0:6b68dac0d986 1628 DataCtrl |= (0x9 << 4);
embeddedartists 0:6b68dac0d986 1629 if (DirFromCard) {
embeddedartists 0:6b68dac0d986 1630 DataCtrl |= (0x1 << 1);
embeddedartists 0:6b68dac0d986 1631 }
embeddedartists 0:6b68dac0d986 1632 DataCtrl |= SDC_DATACTRL_DMA_ENABLE;
embeddedartists 0:6b68dac0d986 1633 LPC_MCI->DATACTRL = DataCtrl;
embeddedartists 0:6b68dac0d986 1634 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 1635 }
embeddedartists 0:6b68dac0d986 1636
embeddedartists 0:6b68dac0d986 1637 void MCIFileSystem::mci_GetResp(response_t* pResp) const
embeddedartists 0:6b68dac0d986 1638 {
embeddedartists 0:6b68dac0d986 1639 pResp->CmdIndex = SDC_RESPCOMMAND_VAL(LPC_MCI->RESP_CMD);
embeddedartists 0:6b68dac0d986 1640 pResp->Data[0] = LPC_MCI->RESP0;
embeddedartists 0:6b68dac0d986 1641 if (CardStatusNumBytes == 4) {
embeddedartists 0:6b68dac0d986 1642 pResp->Data[1] = LPC_MCI->RESP1;
embeddedartists 0:6b68dac0d986 1643 pResp->Data[2] = LPC_MCI->RESP2;
embeddedartists 0:6b68dac0d986 1644 pResp->Data[3] = LPC_MCI->RESP3;
embeddedartists 0:6b68dac0d986 1645 }
embeddedartists 0:6b68dac0d986 1646 }
embeddedartists 0:6b68dac0d986 1647
embeddedartists 0:6b68dac0d986 1648 uint32_t MCIFileSystem::mci_GetBits(int32_t start, int32_t end, uint32_t *data) const
embeddedartists 0:6b68dac0d986 1649 {
embeddedartists 0:6b68dac0d986 1650 uint32_t v;
embeddedartists 0:6b68dac0d986 1651 uint32_t i = end >> 5;
embeddedartists 0:6b68dac0d986 1652 uint32_t j = start & 0x1f;
embeddedartists 0:6b68dac0d986 1653
embeddedartists 0:6b68dac0d986 1654 if (i == (start >> 5)) {
embeddedartists 0:6b68dac0d986 1655 v = (data[i] >> j);
embeddedartists 0:6b68dac0d986 1656 }
embeddedartists 0:6b68dac0d986 1657 else {
embeddedartists 0:6b68dac0d986 1658 v = ((data[i] << (32 - j)) | (data[start >> 5] >> j));
embeddedartists 0:6b68dac0d986 1659 }
embeddedartists 0:6b68dac0d986 1660
embeddedartists 0:6b68dac0d986 1661 return v & ((1 << (end - start + 1)) - 1);
embeddedartists 0:6b68dac0d986 1662 }
embeddedartists 0:6b68dac0d986 1663
embeddedartists 0:6b68dac0d986 1664 void MCIFileSystem::mci_SetCommand(uint32_t Cmd, uint32_t Arg) const
embeddedartists 0:6b68dac0d986 1665 {
embeddedartists 0:6b68dac0d986 1666 /* Clear status register */
embeddedartists 0:6b68dac0d986 1667 LPC_MCI->CLEAR = SDC_CLEAR_ALL;
embeddedartists 0:6b68dac0d986 1668
embeddedartists 0:6b68dac0d986 1669 /* Set the argument first, finally command */
embeddedartists 0:6b68dac0d986 1670 LPC_MCI->ARGUMENT = Arg;
embeddedartists 0:6b68dac0d986 1671
embeddedartists 0:6b68dac0d986 1672 /* Write command value, enable the command */
embeddedartists 0:6b68dac0d986 1673 LPC_MCI->COMMAND = Cmd | SDC_COMMAND_ENABLE;
embeddedartists 0:6b68dac0d986 1674
embeddedartists 0:6b68dac0d986 1675 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 1676 }
embeddedartists 0:6b68dac0d986 1677
embeddedartists 0:6b68dac0d986 1678 void MCIFileSystem::mci_ResetCommand() const
embeddedartists 0:6b68dac0d986 1679 {
embeddedartists 0:6b68dac0d986 1680 LPC_MCI->CLEAR = SDC_CLEAR_ALL;
embeddedartists 0:6b68dac0d986 1681
embeddedartists 0:6b68dac0d986 1682 LPC_MCI->ARGUMENT = 0xFFFFFFFF;
embeddedartists 0:6b68dac0d986 1683
embeddedartists 0:6b68dac0d986 1684 LPC_MCI->COMMAND = 0;
embeddedartists 0:6b68dac0d986 1685
embeddedartists 0:6b68dac0d986 1686 mci_WriteDelay();
embeddedartists 0:6b68dac0d986 1687 }
embeddedartists 0:6b68dac0d986 1688
embeddedartists 0:6b68dac0d986 1689 int32_t MCIFileSystem::mci_IRQHandler(uint8_t *txBuf, uint32_t *txCnt, uint8_t *rxBuf, uint32_t *rxCnt)
embeddedartists 0:6b68dac0d986 1690 {
embeddedartists 0:6b68dac0d986 1691 uint32_t Status;
embeddedartists 0:6b68dac0d986 1692
embeddedartists 0:6b68dac0d986 1693 Status = LPC_MCI->STATUS;
embeddedartists 0:6b68dac0d986 1694
embeddedartists 0:6b68dac0d986 1695 if ( Status & SDC_STATUS_DATAERR) {
embeddedartists 0:6b68dac0d986 1696 LPC_MCI->CLEAR = SDC_STATUS_DATAERR;
embeddedartists 0:6b68dac0d986 1697 return -1; /* Data transfer error */
embeddedartists 0:6b68dac0d986 1698 }
embeddedartists 0:6b68dac0d986 1699
embeddedartists 0:6b68dac0d986 1700 if ( Status & SDC_STATUS_DATAEND) {
embeddedartists 0:6b68dac0d986 1701 LPC_MCI->CLEAR = SDC_STATUS_DATAEND;
embeddedartists 0:6b68dac0d986 1702 LPC_MCI->MASK0 = 0;
embeddedartists 0:6b68dac0d986 1703 return 0;
embeddedartists 0:6b68dac0d986 1704 }
embeddedartists 0:6b68dac0d986 1705
embeddedartists 0:6b68dac0d986 1706 if ( Status & SDC_STATUS_DATABLOCKEND) {
embeddedartists 0:6b68dac0d986 1707 LPC_MCI->CLEAR = SDC_STATUS_DATABLOCKEND;
embeddedartists 0:6b68dac0d986 1708 return 1;
embeddedartists 0:6b68dac0d986 1709 }
embeddedartists 0:6b68dac0d986 1710
embeddedartists 0:6b68dac0d986 1711 if (Status & SDC_STATUS_FIFO) {
embeddedartists 0:6b68dac0d986 1712 return mci_FIFOIRQHandler(txBuf, txCnt, rxBuf, rxCnt);
embeddedartists 0:6b68dac0d986 1713 }
embeddedartists 0:6b68dac0d986 1714
embeddedartists 0:6b68dac0d986 1715 return 1;
embeddedartists 0:6b68dac0d986 1716 }
embeddedartists 0:6b68dac0d986 1717
embeddedartists 0:6b68dac0d986 1718 int32_t MCIFileSystem::mci_FIFOIRQHandler(uint8_t *txBuf, uint32_t *txCnt, uint8_t *rxBuf, uint32_t *rxCnt)
embeddedartists 0:6b68dac0d986 1719 {
embeddedartists 0:6b68dac0d986 1720 uint32_t Status;
embeddedartists 0:6b68dac0d986 1721 Status = LPC_MCI->STATUS;
embeddedartists 0:6b68dac0d986 1722
embeddedartists 0:6b68dac0d986 1723 if (txBuf) {
embeddedartists 0:6b68dac0d986 1724 if (Status & SDC_STATUS_TXFIFOHALFEMPTY) {
embeddedartists 0:6b68dac0d986 1725 if (*txCnt % 64) {
embeddedartists 0:6b68dac0d986 1726 mci_WriteFIFO((uint32_t *) &txBuf[*txCnt], false);
embeddedartists 0:6b68dac0d986 1727 }
embeddedartists 0:6b68dac0d986 1728 else {
embeddedartists 0:6b68dac0d986 1729 mci_WriteFIFO((uint32_t *) &txBuf[*txCnt], true);
embeddedartists 0:6b68dac0d986 1730 }
embeddedartists 0:6b68dac0d986 1731 *txCnt += 32;
embeddedartists 0:6b68dac0d986 1732 }
embeddedartists 0:6b68dac0d986 1733 }
embeddedartists 0:6b68dac0d986 1734
embeddedartists 0:6b68dac0d986 1735 if (rxBuf) {
embeddedartists 0:6b68dac0d986 1736 if (Status & SDC_STATUS_RXFIFOHALFFULL) {
embeddedartists 0:6b68dac0d986 1737 if (*rxCnt % 64) {
embeddedartists 0:6b68dac0d986 1738 mci_ReadFIFO((uint32_t *) &rxBuf[*rxCnt], false);
embeddedartists 0:6b68dac0d986 1739 }
embeddedartists 0:6b68dac0d986 1740 else {
embeddedartists 0:6b68dac0d986 1741 mci_ReadFIFO((uint32_t *) &rxBuf[*rxCnt], true);
embeddedartists 0:6b68dac0d986 1742 }
embeddedartists 0:6b68dac0d986 1743 *rxCnt += 32;
embeddedartists 0:6b68dac0d986 1744 }
embeddedartists 0:6b68dac0d986 1745 }
embeddedartists 0:6b68dac0d986 1746
embeddedartists 0:6b68dac0d986 1747 LPC_MCI->CLEAR = SDC_STATUS_FIFO;
embeddedartists 0:6b68dac0d986 1748
embeddedartists 0:6b68dac0d986 1749 return 1;
embeddedartists 0:6b68dac0d986 1750 }
embeddedartists 0:6b68dac0d986 1751
embeddedartists 0:6b68dac0d986 1752 void MCIFileSystem::mci_ReadFIFO(uint32_t *pDst, bool bFirstHalf) const
embeddedartists 0:6b68dac0d986 1753 {
embeddedartists 0:6b68dac0d986 1754 uint8_t start = 0, end = 7;
embeddedartists 0:6b68dac0d986 1755
embeddedartists 0:6b68dac0d986 1756 if (!bFirstHalf) {
embeddedartists 0:6b68dac0d986 1757 start += 8;
embeddedartists 0:6b68dac0d986 1758 end += 8;
embeddedartists 0:6b68dac0d986 1759 }
embeddedartists 0:6b68dac0d986 1760 for (; start <= end; start++) {
embeddedartists 0:6b68dac0d986 1761 *pDst = LPC_MCI->FIFO[start];
embeddedartists 0:6b68dac0d986 1762 pDst++;
embeddedartists 0:6b68dac0d986 1763 }
embeddedartists 0:6b68dac0d986 1764 }
embeddedartists 0:6b68dac0d986 1765
embeddedartists 0:6b68dac0d986 1766 void MCIFileSystem::mci_WriteFIFO(uint32_t *pSrc, bool bFirstHalf) const
embeddedartists 0:6b68dac0d986 1767 {
embeddedartists 0:6b68dac0d986 1768 uint8_t start = 0, end = 7;
embeddedartists 0:6b68dac0d986 1769 if (!bFirstHalf) {
embeddedartists 0:6b68dac0d986 1770 start += 8;
embeddedartists 0:6b68dac0d986 1771 end += 8;
embeddedartists 0:6b68dac0d986 1772 }
embeddedartists 0:6b68dac0d986 1773 for (; start <= end; start++) {
embeddedartists 0:6b68dac0d986 1774 LPC_MCI->FIFO[start] = *pSrc;
embeddedartists 0:6b68dac0d986 1775 pSrc++;
embeddedartists 0:6b68dac0d986 1776 }
embeddedartists 0:6b68dac0d986 1777 }
embeddedartists 0:6b68dac0d986 1778
embeddedartists 38:420cdc281467 1779 void MCIFileSystem::mci_SetupEventWakeup()
embeddedartists 0:6b68dac0d986 1780 {
embeddedartists 0:6b68dac0d986 1781 /* Wait for IRQ - for an RTOS, you would pend on an event here with a IRQ based wakeup. */
embeddedartists 0:6b68dac0d986 1782
embeddedartists 0:6b68dac0d986 1783 _eventReceived = false;
embeddedartists 0:6b68dac0d986 1784 _eventSuccess = false;
embeddedartists 0:6b68dac0d986 1785 }
embeddedartists 0:6b68dac0d986 1786
embeddedartists 0:6b68dac0d986 1787 uint32_t MCIFileSystem::mci_WaitForEvent() const
embeddedartists 0:6b68dac0d986 1788 {
embeddedartists 0:6b68dac0d986 1789 /* Wait for the event (DMA or MCI interrupt) for a maximum of 2 seconds */
embeddedartists 0:6b68dac0d986 1790 uint32_t end = us_ticker_read() + 2*1000*1000;
embeddedartists 0:6b68dac0d986 1791 while ((us_ticker_read() < end) && (!_eventReceived))
embeddedartists 0:6b68dac0d986 1792 {
embeddedartists 0:6b68dac0d986 1793 // If the driver is having problems reading the card, adding a delay here
embeddedartists 0:6b68dac0d986 1794 // might help.
embeddedartists 0:6b68dac0d986 1795 //wait(0.01);
embeddedartists 0:6b68dac0d986 1796 }
embeddedartists 0:6b68dac0d986 1797
embeddedartists 0:6b68dac0d986 1798 if (_eventReceived && _eventSuccess) {
embeddedartists 0:6b68dac0d986 1799 return 0;
embeddedartists 0:6b68dac0d986 1800 }
embeddedartists 0:6b68dac0d986 1801
embeddedartists 0:6b68dac0d986 1802 return 1;
embeddedartists 0:6b68dac0d986 1803 }
embeddedartists 0:6b68dac0d986 1804
embeddedartists 0:6b68dac0d986 1805 MCIFileSystem::ReturnCode MCIFileSystem::_readBlocks(uint32_t card_type, uint32_t startBlock, uint32_t blockNum) const
embeddedartists 0:6b68dac0d986 1806 {
embeddedartists 0:6b68dac0d986 1807 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1808 response_t Response;
embeddedartists 0:6b68dac0d986 1809 uint32_t Command, Argument;
embeddedartists 0:6b68dac0d986 1810 uint8_t RetryCnt = 0x20;
embeddedartists 0:6b68dac0d986 1811
embeddedartists 0:6b68dac0d986 1812 if (blockNum == 1) {
embeddedartists 0:6b68dac0d986 1813 Command = SD_CMD17_READ_SINGLE_BLOCK;
embeddedartists 0:6b68dac0d986 1814 }
embeddedartists 0:6b68dac0d986 1815 else {
embeddedartists 0:6b68dac0d986 1816 Command = SD_CMD18_READ_MULTIPLE_BLOCK;
embeddedartists 0:6b68dac0d986 1817 }
embeddedartists 0:6b68dac0d986 1818
embeddedartists 0:6b68dac0d986 1819 /* Select single or multiple read based on number of blocks */
embeddedartists 0:6b68dac0d986 1820 /* if high capacity card use block indexing */
embeddedartists 0:6b68dac0d986 1821 if (card_type & CARD_TYPE_HC) {
embeddedartists 0:6b68dac0d986 1822 Argument = startBlock;
embeddedartists 0:6b68dac0d986 1823 }
embeddedartists 0:6b68dac0d986 1824 else { /*fix at 512 bytes*/
embeddedartists 0:6b68dac0d986 1825 Argument = startBlock << 9;
embeddedartists 0:6b68dac0d986 1826 }
embeddedartists 0:6b68dac0d986 1827
embeddedartists 0:6b68dac0d986 1828 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1829 Ret = mci_ExecuteCmd(Command, Argument, &Response);
embeddedartists 0:6b68dac0d986 1830 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1831 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1832 return Ret;
embeddedartists 0:6b68dac0d986 1833 }
embeddedartists 0:6b68dac0d986 1834 }
embeddedartists 0:6b68dac0d986 1835 RetryCnt--;
embeddedartists 0:6b68dac0d986 1836 }
embeddedartists 0:6b68dac0d986 1837 return Ret;
embeddedartists 0:6b68dac0d986 1838 }
embeddedartists 0:6b68dac0d986 1839
embeddedartists 0:6b68dac0d986 1840 MCIFileSystem::ReturnCode MCIFileSystem::_writeBlocks(uint32_t card_type, uint32_t startBlock, uint32_t blockNum) const
embeddedartists 0:6b68dac0d986 1841 {
embeddedartists 0:6b68dac0d986 1842 ReturnCode Ret = SDC_RET_FAILED;
embeddedartists 0:6b68dac0d986 1843 response_t Response;
embeddedartists 0:6b68dac0d986 1844 uint32_t Command, Argument;
embeddedartists 0:6b68dac0d986 1845 uint8_t RetryCnt = 0x20;
embeddedartists 0:6b68dac0d986 1846
embeddedartists 0:6b68dac0d986 1847 if (blockNum == 1) {
embeddedartists 0:6b68dac0d986 1848 Command = SD_CMD24_WRITE_BLOCK;
embeddedartists 0:6b68dac0d986 1849 }
embeddedartists 0:6b68dac0d986 1850 else {
embeddedartists 0:6b68dac0d986 1851 Command = SD_CMD25_WRITE_MULTIPLE_BLOCK;
embeddedartists 0:6b68dac0d986 1852 }
embeddedartists 0:6b68dac0d986 1853
embeddedartists 0:6b68dac0d986 1854 /* if high capacity card use block indexing */
embeddedartists 0:6b68dac0d986 1855 if (card_type & CARD_TYPE_HC) {
embeddedartists 0:6b68dac0d986 1856 Argument = startBlock;
embeddedartists 0:6b68dac0d986 1857 }
embeddedartists 0:6b68dac0d986 1858 else { /*fix at 512 bytes*/
embeddedartists 0:6b68dac0d986 1859 Argument = startBlock << 9;
embeddedartists 0:6b68dac0d986 1860
embeddedartists 0:6b68dac0d986 1861 }
embeddedartists 0:6b68dac0d986 1862
embeddedartists 0:6b68dac0d986 1863 while (RetryCnt > 0) {
embeddedartists 0:6b68dac0d986 1864 Ret = mci_ExecuteCmd(Command, Argument, &Response);
embeddedartists 0:6b68dac0d986 1865 if (Ret == SDC_RET_OK) {
embeddedartists 0:6b68dac0d986 1866 if (mci_CheckR1Response(Response.Data[0], &Ret)) {
embeddedartists 0:6b68dac0d986 1867 return Ret;
embeddedartists 0:6b68dac0d986 1868 }
embeddedartists 0:6b68dac0d986 1869 }
embeddedartists 0:6b68dac0d986 1870 RetryCnt--;
embeddedartists 0:6b68dac0d986 1871 }
embeddedartists 0:6b68dac0d986 1872 return Ret;
embeddedartists 0:6b68dac0d986 1873 }