📄 fat.i
字号:
// CodeVisionAVR C Compiler
// (C) 1998-2004 Pavel Haiduc, HP InfoTech S.R.L.
// I/O registers definitions for the ATmega32
#pragma used+
sfrb TWBR=0;
sfrb TWSR=1;
sfrb TWAR=2;
sfrb TWDR=3;
sfrb ADCL=4;
sfrb ADCH=5;
sfrw ADCW=4; // 16 bit access
sfrb ADCSRA=6;
sfrb ADCSR=6; // for compatibility with older code
sfrb ADMUX=7;
sfrb ACSR=8;
sfrb UBRRL=9;
sfrb UCSRB=0xa;
sfrb UCSRA=0xb;
sfrb UDR=0xc;
sfrb SPCR=0xd;
sfrb SPSR=0xe;
sfrb SPDR=0xf;
sfrb PIND=0x10;
sfrb DDRD=0x11;
sfrb PORTD=0x12;
sfrb PINC=0x13;
sfrb DDRC=0x14;
sfrb PORTC=0x15;
sfrb PINB=0x16;
sfrb DDRB=0x17;
sfrb PORTB=0x18;
sfrb PINA=0x19;
sfrb DDRA=0x1a;
sfrb PORTA=0x1b;
sfrb EECR=0x1c;
sfrb EEDR=0x1d;
sfrb EEARL=0x1e;
sfrb EEARH=0x1f;
sfrw EEAR=0x1e; // 16 bit access
sfrb UBRRH=0x20;
sfrb UCSRC=0X20;
sfrb WDTCR=0x21;
sfrb ASSR=0x22;
sfrb OCR2=0x23;
sfrb TCNT2=0x24;
sfrb TCCR2=0x25;
sfrb ICR1L=0x26;
sfrb ICR1H=0x27;
sfrb OCR1BL=0x28;
sfrb OCR1BH=0x29;
sfrw OCR1B=0x28; // 16 bit access
sfrb OCR1AL=0x2a;
sfrb OCR1AH=0x2b;
sfrw OCR1A=0x2a; // 16 bit access
sfrb TCNT1L=0x2c;
sfrb TCNT1H=0x2d;
sfrw TCNT1=0x2c; // 16 bit access
sfrb TCCR1B=0x2e;
sfrb TCCR1A=0x2f;
sfrb SFIOR=0x30;
sfrb OSCCAL=0x31;
sfrb TCNT0=0x32;
sfrb TCCR0=0x33;
sfrb MCUCSR=0x34;
sfrb MCUCR=0x35;
sfrb TWCR=0x36;
sfrb SPMCR=0x37;
sfrb TIFR=0x38;
sfrb TIMSK=0x39;
sfrb GIFR=0x3a;
sfrb GICR=0x3b;
sfrb OCR0=0X3c;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-
// Interrupt vectors definitions
// Needed by the power management functions (sleep.h)
#asm
#ifndef __SLEEP_DEFINED__
#define __SLEEP_DEFINED__
.EQU __se_bit=0x80
.EQU __sm_mask=0x70
.EQU __sm_powerdown=0x20
.EQU __sm_powersave=0x30
.EQU __sm_standby=0x60
.EQU __sm_ext_standby=0x70
.EQU __sm_adc_noise_red=0x10
.SET power_ctrl_reg=mcucr
#endif
#endasm
/*------------------------------------------------------------------*/
/* Redefine Data Size */
/*------------------------------------------------------------------*/
void TransmitByte( unsigned char data );
//-----------------------------------------------------
/* CodeVisionAVR C Compiler
Prototypes for standard library functions
(C) 1998-2003 Pavel Haiduc, HP InfoTech S.R.L.
*/
#pragma used+
int atoi(char *str);
long int atol(char *str);
float atof(char *str);
void itoa(int n,char *str);
void ltoa(long int n,char *str);
void ftoa(float n,unsigned char decimals,char *str);
void ftoe(float n,unsigned char decimals,char *str);
void srand(int seed);
int rand(void);
void *malloc(unsigned int size);
void *calloc(unsigned int num, unsigned int size);
void *realloc(void *ptr, unsigned int size);
void free(void *ptr);
#pragma used-
#pragma library stdlib.lib
//声明外部器件读写函数
extern unsigned char MMC_SD_ReadSingleBlock(unsigned long sector, unsigned char* buffer);
extern unsigned char FAT_Init();//初始化
//extern unsigned long FAT_NextCluster(unsigned long cluster);//查找下一簇号
//extern unsigned int FAT_FindItem(unsigned long cluster, BYTE *name, struct FileInfoStruct *FileInfo);//查找文件
//extern unsigned long FAT_OpenDir(BYTE * dir);//打开目录
//extern unsigned char Search(BYTE *dir,struct direntry *MusicInfo,WORD *Count,BYTE *type);//查找音乐文件
//void CopyDirentruyItem(struct direntry *Desti,struct direntry *Source);
extern unsigned char MMC_SD_SendCommand(unsigned char cmd, unsigned long arg);
extern void MMC_SD_Init (void);
extern unsigned char MMC_SD_Reset(void);
extern unsigned char MMC_SD_ReadSingleBlock(unsigned long sector, unsigned char* buffer);
extern void SPI_High(void);
//低速模式
void SPI_Low(void)
{
SPCR =0x52;//_BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
SPSR &= 0xfe; //关双速
}
//高速模式
void SPI_High(void)
{
SPCR =0x50;// _BV(SPE)|_BV(MSTR);
SPSR |=0x01;// _BV(SPI2X); //开双速
}
//端口初始化,模式初始化
void SPI_Init(void)
{
DDRB |=0xbf ; //端口设置,主要是SPI
SPI_Low(); //先低速
}
//sd卡初始化
void MMC_SD_Init(void)
{
SPI_Init();
PORTB.2=1;//cs=1
}
//写读一个字节 //read and write one byte
unsigned char SPI_WriteByte(unsigned char val)
{
SPDR = val;
while(SPSR.7==0);
return SPDR;
}
//sd卡写命令
unsigned char MMC_SD_SendCommand(unsigned char cmd, unsigned long arg)
{
unsigned char r1;
unsigned char retry=0;
SPI_WriteByte(0xff);
PORTB.2=0 ; //CS=0
SPI_WriteByte(cmd | 0x40);//分别写入命令
SPI_WriteByte(arg>>24);
SPI_WriteByte(arg>>16);
SPI_WriteByte(arg>>8);
SPI_WriteByte(arg);
SPI_WriteByte(0x95);
while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待响应,
if(retry++ > 200) break;//超时退出
PORTB.2=1; //cs=1
return r1;//返回状态值
}
//sd卡复位
unsigned char MMC_SD_Reset(void)
{
unsigned char i;
unsigned char retry;
unsigned char r1=0;
retry = 0;
SPI_Low();
PORTB.2=1;//
for(i=0; i<16; i++)
SPI_WriteByte(0xFF);
do
{
for(i=0;i<20;i++) SPI_WriteByte(0xff);
r1 = MMC_SD_SendCommand(0, 0);//发idle命令
retry++;
//TransmitByte(retry);
if(retry>200) return 1;//超时退出
} while(r1 != 0x01);
retry = 0;
do
{
r1 = MMC_SD_SendCommand(1, 0);//发active命令
retry++;
if(retry>100) return 1;//超时退出
} while(r1);
SPI_High();
r1 = MMC_SD_SendCommand(59, 0);//关crc
r1 = MMC_SD_SendCommand(16, 512);//设扇区大小512
return 0;//正常返回
}
//读一个扇区
unsigned char MMC_SD_ReadSingleBlock(unsigned long sector, unsigned char* buffer)
{
unsigned char r1;
unsigned int i;
unsigned int retry=0;
PORTB.2=1;//
for(i=0; i<16; i++)
SPI_WriteByte(0xFF);
r1 = MMC_SD_SendCommand(17, sector<<9);//读命令 /9
if(r1 != 0x00)
return r1;
PORTB.2=0 ; //cs=0
//等数据的开始
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 1000){PORTB.2=1;return 1;}
for(i=0; i<512; i++)//读512个数据
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);//伪crc
SPI_WriteByte(0xff);
PORTB.2=1; //cs=1
return 0;
}
/*
//读一个扇区
uint8 MMC_SD_ReadSingleBlock1(uint32 sector, uint8* buffer)
{
uint8 r1;
uint16 i;
uint16 retry=0;
MMC_SD_SendCommand(16, 256);
r1 = MMC_SD_SendCommand(17, sector<<8);//读命令 /9
if(r1 != 0x00)
return r1;
SPI_CS_Assert(); //cs=0
//等数据的开始
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 1000){SPI_CS_Deassert();return 1;}
for(i=0; i<256; i++)//读512个数据
{
*buffer++ = SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);//伪crc
SPI_WriteByte(0xff);
SPI_CS_Deassert(); //cs=1
return 0;
}
*/
unsigned char bf[512];
unsigned char secpclus; //每籁包函扇区数
unsigned int fr_fatsec; //FAT表首扇区
unsigned int rodir; // 根目录首扇区
unsigned char firstrodirclus; //根目录的首籁号
unsigned long filefirstclus; //文件首籁号
unsigned long filesize; //
unsigned char (* FAT_ReadSector)(unsigned long sector, unsigned char * buffer) = MMC_SD_ReadSingleBlock;//device read
///FAT初始化
unsigned char FAT_Init()
{ unsigned long x;
unsigned long *p;
unsigned int *rscnt;//保留扇区数
unsigned long *fatsz;//FAT大小
if(FAT_ReadSector(0,bf))return 1; //读出启动扇区
p=&bf[454]; //取出分区启动扇区地址
x=*p;
FAT_ReadSector(x,bf); //读分区的启动扇区
secpclus=bf[13]; //每籁的扇区数
rscnt=&bf[14]; //保留扇区
fatsz=&bf[36]; //FAT大小
firstrodirclus=bf[44];//根目录的首籁号0
fr_fatsec=x+(*rscnt); //FAT首扇区
rodir=fr_fatsec+(*fatsz)*2; //算出根目录
return 0;
}
//读一个簇中的一个扇区
unsigned char FAT_LoadPartCluster(unsigned long cluster,unsigned part,unsigned char * buffer)
{
unsigned long sector;
sector=rodir+(unsigned long)(cluster-2)*(unsigned long)secpclus;//算出扇区
if(MMC_SD_ReadSingleBlock(sector+part,buffer))return 1;
else return 0;
}
//读下一簇簇号
unsigned long FAT_NextCluster(unsigned long cluster)
{ unsigned long *x;
unsigned long sector;
unsigned long offset;
offset = cluster/128;
if(cluster<2){return 0x0ffffff8;}
sector=fr_fatsec+offset;
if(FAT_ReadSector(sector,bf))return 0x0ffffff8;//读FAT表
offset=cluster%128;//因为籁号大于128时就会跨扇区
x=&bf[offset*4];
return *x;
}
//----------------------------------------
//读下一簇簇号
unsigned long FAT_NextCluster1(unsigned long cluster)
{ unsigned long *x;
unsigned char r1,i;
unsigned int retry=0;
unsigned char buf[4];
unsigned long sector;
unsigned long offset;
offset = cluster/128;
//if(cluster<2){return 0x0ffffff8;}
sector=(fr_fatsec+offset)*512;
offset=cluster%128;//因为籁号大于128时就会跨扇区
sector=sector+offset*4;
MMC_SD_SendCommand(16, 4);
r1 = MMC_SD_SendCommand(17, sector);
if(r1 != 0x00)
return r1;
PORTB.2=0 ; //cs=0
//等数据的开始
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 1000){PORTB.2=1;return 1;}
for(i=0; i<4; i++)//读4个数据
{
buf[i] = SPI_WriteByte(0xff);
}
x=buf;
SPI_WriteByte(0xff);//伪crc
SPI_WriteByte(0xff);
MMC_SD_SendCommand(16,512);
PORTB.2=1; //cs=1
return *x;
}
//-------------------------
///////////////////////////////////////
//search the file , when *count = 0 it will bring the number whole songs, when *cout != 0 the *MusicInfo will bring the infomation of the file
unsigned char Search(unsigned int *Count)//当COUNT为零时,有它带回这个目录下总共有多少首音乐
{ unsigned long tempclust,sector;
unsigned char cnt;
unsigned int offset;
unsigned int i=0;
unsigned int *p;
tempclust=firstrodirclus;
while(1)
{ sector=rodir+(unsigned long)(tempclust-2)*(unsigned long)secpclus;//算出扇区
for(cnt=0;cnt<secpclus;cnt++)//读一个籁
{
if(FAT_ReadSector(sector+cnt,bf)){return 1;}
for(offset=0;offset<512;offset+=32)//扇区内遍历
{
if((bf[offset] !=0)&(bf[offset] !='.')&(bf[offset] !=0xe5)&(bf[offset+11]!=0x0f))
{
if((bf[offset+8] == 'W')&&(bf[offset+9] == 'A')&&(bf[offset+10] == 'V'))
{
p=&bf[offset+20];
filefirstclus=*p;
//TransmitByte(filefirstclus);
filefirstclus=filefirstclus<<16;
p=&bf[offset+26];
filefirstclus=(filefirstclus+*p);
p=&bf[offset+28];
filesize=*p;
i++;
if(i==*Count){return 0;}
}
}
}
}
tempclust=FAT_NextCluster(tempclust);//next cluster
if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 )break;
}
if(*Count==0)*Count=i;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -