⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmcdump.c

📁 Simple AVR MMC dumper
💻 C
📖 第 1 页 / 共 2 页
字号:
write_file(unsigned int ClustNumber)                             // write to file ClustNumber = first cluster nuber of file   
{       bit Feof;
        unsigned int dSector, i, NextClust;
        unsigned int *ptr;
        #ifdef MMC_DEBUG2
        printf("FileAllocationTable addr:%u\r\n", FatStart);
        #endif     
        Feof = 1;                                     
        while(Feof)
        {       
                dSector = ClusterOffset + (ClustNumber  - 2) * SectPerCluster;
                #ifdef MMC_DEBUG2
                printf("Write Cluster:0x%x ",ClustNumber); 
                #endif
                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                       
        #ifdef MMC_DEBUG2
        printf("Read MBR, DATA:");      
        dump();
        #endif
        
        BootSector =  block[0x1C6];            //  boot sector address have to be < 255
                                                                        
        // read Boot Sector
	mmc_read(BootSector,block);
        #ifdef MMC_DEBUG2
        printf("ReadBootSector:%u (0x%x)DATA:\r\n", BootSector,BootSector);      
        dump();
        #endif
	bootp=(struct BootSec *)block;              
	RootDirEntry = ( bootp->BPB_ResrvdSector + (bootp->BPB_NumOfFat * bootp->BPB_SectPerFat));
	RootDirEntry+= BootSector;   
	ClusterOffset = ((bootp->BPB_BytesPerSec * 32)/BlockSize);	
	ClusterOffset+= RootDirEntry;   
	FatStart = BootSector + bootp->BPB_ResrvdSector;
	SectPerCluster = bootp->BPB_SecPerClus;
	
	#ifdef MMC_DEBUG2
        printf("RootDirEntry:%u (0x%x)\r\n",RootDirEntry, RootDirEntry);
        printf("Fat Start:%u (0x%x)\r\n",FatStart, FatStart);
        printf("Bytes/Sector:%u\r\n", bootp->BPB_BytesPerSec);
        printf("Setor/Cluster:%u\r\n", SectPerCluster);
        printf("ReservedSect:%u\r\n", bootp->BPB_ResrvdSector);
        printf("ClusterOffset:%u (0x%x)\r\n",ClusterOffset, ClusterOffset);
        #endif                                                       
                                                                                                      
        #ifdef MMC_DEBUG3
        printf("DumpFatTable:%u", FatStart);                    // dump fat table (only for debugging)
        mmc_read(FatStart,block);                        // Read Fat table (first sector)
        dump();                             
        printf("DumpFatTable:%u", FatStart + 1);
        mmc_read((FatStart + 1),block);                  // Read Fat table (second sector, only 2 sectors dumped)
        dump();          
        #endif

        // read first DirEntry        
        mmc_read(RootDirEntry,block);   
	#ifdef MMC_DEBUG2   
        printf("FirstDirEntry\r\n");
        txtdump();
        #endif               
        /* 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<BlockSize;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); 
	        #ifdef MMC_DEBUG2
                printf("NextDirEntry:%i\r\n",DirBlkCnt + 1);
                txtdump();
                #endif               
        } 
        
}
                                                 
	


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;                         
#ifdef MMC_DEBUG3
printf("MMC_START\r\n");
#endif                  
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"); 
        }
}                                                 

#ifdef MMC_DEBUG3
printf("MMC READY\r\n");
#endif

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 + -