📄 mmcdump.i
字号:
}
unsigned char mmc_write(unsigned long sector, unsigned char* buffer) // write to MMC (from buffer or 'Z')
{ unsigned char resp;
unsigned int i;
PORTB.2 = 0; // set chip select
resp = mmc_command(24 , sector<<9);
if(resp != 0x00) return resp; // check for response
spi(0xFF); // send dummy
spi(0xFE ); // send data start token
for(i=0; i<0x200; i++)
{ spi('Z'); // write data
// spi(*buffer++);
}
spi(0xFF); // write 16-bit CRC (dummy values)
spi(0xFF);
resp = spi(0xFF);
if( (resp & 0x1F) != 0x05) return resp; // read data response
while(!spi(0xFF)); // wait until card not busy
PORTB.2 = 1; // release chip select
return 0; // return ok
}
unsigned char mmc_read(unsigned long sector, unsigned char* buffer) // Read buffer, returns zero if successful.
{
unsigned char resp;
unsigned int i;
PORTB.2 = 0; // set chip select
resp = mmc_command(17 , sector<<9); // send command:READ-BLOCK
if(resp != 0x00) return resp; // check response
while(spi(0xFF) != 0xFE ); // wait for block start
for(i=0; i<0x200; i++)
{ *buffer++ = spi(0xFF); // read in data
}
spi(0xFF); // read 16-bit CRC
spi(0xFF);
PORTB.2 = 1; // release chip select
return 0; // return OK
}
dump() // dump block (sector) buffer as hex to serial port
{ unsigned char i,j;
unsigned int m = 0;
for(i=0;i<32;i++)
{ printf("\r\n%3x:", m);
for(j=0;j<16;j++)
{ printf("%3x ", block[m]);
m++;
}
}
printf("\r\n");
}
txtdump() // dump block (sector) buffer as text to serial port
{ unsigned char i,j;
unsigned int m = 0;
for(i=0;i<32;i++)
{ printf("\r\n%3x:", m);
for(j=0;j<16;j++)
{ printf("%c ", block[m]);
m++;
}
}
printf("\r\n");
}
dump_file(unsigned int ClustNumber) // dump file ClustNumber = first cluster nuber of file
{ bit Feof;
unsigned int dSector, i, NextClust;
unsigned int *ptr;
Feof = 1;
while(Feof)
{
dSector = ClusterOffset + (ClustNumber - 2) * SectPerCluster; // calculate sector number
printf("Read Sectors:", dSector);
for(i=0;i<SectPerCluster;i++)
{ printf("%u, ", dSector);
mmc_read(dSector,block); //Read data
//txtdump();
dSector++;
}
// fetch next cluster number from FAT
printf("\r\nRead FAT table\r\n");
dSector = FatStart + ClustNumber / 256; // calculate current Sector number of FAT table
printf("FatSectorAddr:%u ", dSector);
mmc_read(dSector,block); //Read Fat table
// dump();
ptr = block;
ClustNumber&=0xFF; // Max offset = 0xFF
ptr+=ClustNumber; // pointer to next cluster number
NextClust = *ptr; // get next cluster number
printf("PrevCluster:%u, NextCluster:%u (0x%x)\r\n",ClustNumber, NextClust, (unsigned int *) *ptr);
if(NextClust > 0xFF00) Feof = 0; // only 0xFF00 checked
ClustNumber = NextClust;
}
}
write_file(unsigned int ClustNumber) // write to file ClustNumber = first cluster nuber of file
{ bit Feof;
unsigned int dSector, i, NextClust;
unsigned int *ptr;
Feof = 1;
while(Feof)
{
dSector = ClusterOffset + (ClustNumber - 2) * SectPerCluster;
printf("Write Sectors:", dSector);
for(i=0;i<SectPerCluster;i++)
{ printf("%u, ", dSector);
mmc_write(dSector,block); //Write data
//txtdump();
dSector++;
}
// fetch next cluster number from FAT table
printf("\r\nRead FAT table\r\n");
dSector = FatStart + ClustNumber / 256; // calculate current Sector number of FAT table
printf("FatSectorAddr:%u ", dSector);
mmc_read(dSector,block); //Read Fat table
// dump();
ptr = block;
ClustNumber&=0xFF; // max offset = 0xFF
ptr+=ClustNumber; // ptr type = integer
NextClust = *ptr; // get next cluster number
printf("PrevCluster:%u, NextCluster:%u (0x%x)\r\n",ClustNumber, NextClust, (unsigned int *) *ptr);
if(NextClust > 0xFF00) Feof = 0;
ClustNumber = NextClust;
}
}
read_mbr() /* read/write file */
{ unsigned long BootSector;
unsigned int FileCnt, DirBlkCnt;
struct BootSec *bootp;
struct DirEntry *dir;
unsigned char FileName[12], *ptr;
// find Boot Sector (Master Boot Record MBR, offset 0x1C6)
mmc_read(0,block); //Read Master Boot Record
BootSector = block[0x1C6]; // boot sector address have to be < 255
// read Boot Sector
mmc_read(BootSector,block);
bootp=(struct BootSec *)block;
RootDirEntry = ( bootp->BPB_ResrvdSector + (bootp->BPB_NumOfFat * bootp->BPB_SectPerFat));
RootDirEntry+= BootSector;
ClusterOffset = ((bootp->BPB_BytesPerSec * 32)/512);
ClusterOffset+= RootDirEntry;
FatStart = BootSector + bootp->BPB_ResrvdSector;
SectPerCluster = bootp->BPB_SecPerClus;
// read first DirEntry
mmc_read(RootDirEntry,block);
/* go thru Dir entries */
for(DirBlkCnt=0;DirBlkCnt<32;DirBlkCnt++)
{ if(block[0] == 0)
{ printf("\r\nNo more files\r\n");
DirBlkCnt = 32; // stop if DirEntry[0] = 0
break;
}
ptr = block;
for(FileCnt=0;FileCnt<512;FileCnt+=32)
{
dir = (struct DirEntry*) ptr;
if(dir->DIR_Name[0]==0) break;
if(dir->DIR_Name[0]>'/')
{ if(dir->DIR_Name[0]<'{')
{
strncpy(FileName,dir->DIR_Name,11); // print filename
FileSize = dir->DIR_FileSize; // print file size
printf("DirName:%s ",FileName); // Print num of first cluster
printf("FirstCluster:%x ", dir->DIR_FirstCluster);
printf("FileSize:%lu\r\n",FileSize);
/* select dump_file or write_file */
dump_file(dir->DIR_FirstCluster);
//write_file(dir->DIR_FirstCluster);
mmc_read(RootDirEntry,block); // read Dir sector again
}
ptr+=32;
}
}
// read next DirEntry
RootDirEntry++;
mmc_read(RootDirEntry,block);
}
}
void main(void)
{
unsigned char ch, mmc_stat;
int i, j, k, m;
unsigned long sector;
char mystring[16] = "MMC-DUMP V1.0"; // not in use
// Port B initialization
// Func7=Out Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In
// State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x2C;
// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0x02;
// Port D initialization
// Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x80;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 115200
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x03;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 250,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: High
// SPI Data Order: MSB First
// SPCR
// SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
// 0 1 0 1 0 0 0 0
// ! ! ! ! ! ! !....!..Clock rate select (f(osc)/2 = 8 Mhz)
// ! ! ! ! ! !............Clock phase (sample on leading edge)
// ! ! ! ! !.................Clock polarity (SCK low on IDLE)
// ! ! ! !......................Master/Slave select (Master)
// ! ! !...........................Data order (MSB first)
// ! !................................SPI enable (enabled)
// !....................................SPI interrupt enable (Not enabled)
//SPCR=0x53;
SPCR=0x50;
// SPSR
// SPIF WCOL SP2X
// ! ! !.. spi double rate
// ! !................................ Write collision (NOT enabled)
// !..................................... SPI interrupt enable (NOT enabled)
SPSR=0x01;
delay_ms(500); // delay 0.5 sec
// Global enable interrupts
#asm("sei")
ch = 0x20;
mmc_stat = 0xFF;
while(mmc_stat !=0)
{ mmc_stat = mmc_init();
PORTB.2 = 1; // MMC chip select OFF (init leaves it on)
if(mmc_stat!=0)
{ printf("MMC CARD ERROR:%d \r\n", mmc_stat, 0xa,0xd);
#asm("cli");
delay_ms(10000);
#asm("sei");
}
}
read_mbr();
printf("DUMP MMC\r\n");
sector = 0;
mmc_stat = mmc_read(sector, block);
while (1) // dump sectors (debug style)
{ PORTC.1 = !PORTC.1;
k = 0;
m = 0;
printf("\r\nSECTOR:%lu:",sector);
for(i=0;i<32;i++)
{
printf("\r\n%3u(%3x):",m,m);
for(j=0;j<16;j++)
{ printf("%2x ", block[m]); // dump Hex
m++;
}
for(j=0;j<16;j++)
{ printf("%c ", block[k]); // dump char
k++;
}
}
sector++;
mmc_stat = mmc_read(sector, block);
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -