00001
00002
00003
00004
00005
00006
00007
00008 #include "diskio.h"
00009 #include "Xmmc.h"
00010
00011
00012
00013
00014
00015
00016 #define ATA 0
00017 #define MMC 1
00018 #define USB 2
00019
00020
00021 static DSTATUS DecodeMMCResult(unsigned char r)
00022 {
00023 DSTATUS rV = 0;
00024
00025 if(r & MMC_R1_BUSY) rV = STA_NOINIT;
00026 else if (r & (MMC_R1_PARAMETER | MMC_R1_ADDRESS | MMC_R1_ERASE_SEQ | MMC_R1_COM_CRC))
00027 rV = STA_NOINIT;
00028 else if (r & (MMC_R1_ILLEGAL_COM | MMC_R1_ERASE_RESET | MMC_R1_IDLE_STATE))
00029 rV = STA_NOINIT;
00030 return rV;
00031 }
00032
00033 static DSTATUS DecodeMMCStatus(unsigned short r)
00034 {
00035 DSTATUS rV;
00036
00037 if(r & (MMC_R2_WP | MMC_R2_WP_ERASESKIPPED )) rV = STA_PROTECT;
00038 else if (r & ~(MMC_R2_WP | MMC_R2_WP_ERASESKIPPED) ) rV = STA_NOINIT;
00039 else rV = 0;
00040 return rV;
00041 }
00042
00043 static DRESULT DecodeMMCIO(unsigned char r)
00044 {
00045 DRESULT rV=0;
00046
00047 if(r & MMC_R1_ILLEGAL_COM ) rV = RES_PARERR;
00048 else if (r & (MMC_R1_PARAMETER | MMC_R1_ADDRESS | MMC_R1_ERASE_SEQ | MMC_R1_COM_CRC))
00049 rV = RES_ERROR;
00050 else if (r & ( MMC_R1_ERASE_RESET | MMC_R1_IDLE_STATE | MMC_R1_BUSY))
00051 rV = RES_NOTRDY;
00052 return rV;
00053 }
00054
00055
00056
00057
00058 DSTATUS disk_initialize (
00059 BYTE drv
00060 )
00061 {
00062 DSTATUS stat = STA_NOINIT;
00063 int result;
00064
00065 switch (drv) {
00066 case ATA :
00067
00068
00069
00070 return stat;
00071
00072 case MMC :
00073 result = mmcReset();
00074
00075 stat = DecodeMMCResult(result);
00076 return stat;
00077
00078 case USB :
00079
00080
00081
00082 return stat;
00083 }
00084 return STA_NOINIT;
00085 }
00086
00087
00088
00089
00090
00091
00092 DSTATUS disk_status (
00093 BYTE drv
00094 )
00095 {
00096 DSTATUS stat=0;
00097
00098
00099
00100 switch (drv) {
00101 case ATA :
00102
00103
00104
00105 return stat;
00106
00107 case MMC :
00108
00109
00110 stat = DecodeMMCStatus(mmcSendStatus());
00111
00112 return stat;
00113
00114 case USB :
00115
00116
00117
00118 return stat;
00119 }
00120 return STA_NOINIT;
00121 }
00122
00123
00124
00125
00126
00127
00128 DRESULT disk_read (
00129 BYTE drv,
00130 BYTE *buff,
00131 DWORD sector,
00132 BYTE count
00133 )
00134 {
00135 DRESULT res = 0;
00136 unsigned char result;
00137 char i;
00138
00139
00140 switch (drv) {
00141 case ATA :
00142
00143
00144
00145 return res;
00146
00147 case MMC :
00148
00149
00150 for(i=0;(i<count) && (res == 0);++i)
00151 {
00152 result = mmcRead(sector++,&buff[((unsigned)i) << 9]);
00153 res = DecodeMMCIO(result);
00154 }
00155
00156 return res;
00157
00158 case USB :
00159
00160
00161
00162 return res;
00163 }
00164 return RES_PARERR;
00165 }
00166
00167
00168
00169
00170
00171
00172 #if _READONLY == 0
00173 DRESULT disk_write (
00174 BYTE drv,
00175 const BYTE *buff,
00176 DWORD sector,
00177 BYTE count
00178 )
00179 {
00180 DRESULT res = 0;
00181 unsigned char result;
00182 char i;
00183
00184 switch (drv) {
00185 case ATA :
00186
00187
00188
00189 return res;
00190
00191 case MMC :
00192
00193
00194 for(i=0;(i<count) && (res == 0);++i)
00195 {
00196 result = mmcWrite(sector++,(unsigned char *)&buff[((unsigned)i) << 9]);
00197 res = DecodeMMCIO(result);
00198 }
00199 return res;
00200
00201 case USB :
00202
00203
00204
00205 return res;
00206 }
00207 return RES_PARERR;
00208 }
00209 #endif
00210
00211
00212
00213
00214
00215
00216 DRESULT disk_ioctl (
00217 BYTE drv,
00218 BYTE ctrl,
00219 void *buff
00220 )
00221 {
00222 DRESULT res=0;
00223
00224
00225 switch (drv) {
00226 case ATA :
00227
00228
00229
00230
00231
00232 return res;
00233
00234 case MMC :
00235
00236
00237
00238
00239
00240 return res;
00241
00242 case USB :
00243
00244
00245
00246
00247
00248 return res;
00249 }
00250 return RES_PARERR;
00251 }
00252