Exemplo n.º 1
0
//FAT初始化,不含SD的初始化,用之前应先调用sd的初始化
//返回值:0,初始化成功
//    其他,初始化失败   
unsigned char FAT_Init(void)//Initialize of FAT  need initialize SD first
{  		   
	bootsector710 *bs  = 0;       //定义DBR结构体,包含BPB部分(12~90字节是BPB)
	bpb710        *bpb = 0;       //定义BPB结构体     
	partrecord    *pr  = 0;       //定义硬盘信息 结构休      (16字节)
	DWORD hidsec=0;             //隐藏的扇区数即为DBR在物理扇区的偏移量
	long long Capacity;   
	Capacity =(long long)SD_GetSectorCount()*512;   //得到SD容量,单位为字节            
	if(Capacity<0xff)return 1;       
	
	if(SD_ReadDisk(fat_buffer,0,1))return 2; //读物理扇区0。即MBR(其中MBR内的硬盘分区表中有DBR的地址信息),如没有MBR,则为DBR。经测试,我的SD卡是有MBR的
	if(fat_buffer[0]!=0XEB&&fat_buffer[0]!=0XE9)  //如物理0扇区第一字节不为0XEB或0XE9,则存在MBR,否则不存在
		{   				  
		//printf("此SD卡的物理扇区0为MBR\n");
		pr = (partrecord *)((partsector *)fat_buffer)->psPart;//first partition       第一分区
		hidsec=pr->prStartLBA;   //隐藏的扇区 即DBR的物理扇区偏移
		//printf("此SD卡的DBR的物理扇区为=%d\n",hidsec) ;
	}
	else 
	{
		hidsec=0;      //无MBR,物理扇区0即为DBR
		//printf("此SD卡不存在MBR,物理扇区0即为DBR,物理扇区0即为逻辑扇区0.\n"); 
 	} 
	if(SD_ReadDisk(fat_buffer,hidsec,1))return 3;
	//printf("DBR物理扇区偏移量:%d \n",hidsec);
	bs = (bootsector710 *)fat_buffer;
	bpb = (bpb710 *)bs->bsBPB;
	if(bpb->bpbFATsecs)//detemine thd FAT type  do not support FAT12
	{
		FAT32_Enable=0;   					//FAT16
		FATsectors     = bpb->bpbFATsecs;	//FAT表占用的扇区数 
		FirstDirClust = 2;
	}
	else
	{
		FAT32_Enable=1;   					//FAT32
		FATsectors     = bpb->bpbBigFATsecs;//FAT占用的扇区数 
		FirstDirClust = bpb->bpbRootClust;
	}   
	BytesPerSector   = bpb->bpbBytesPerSec;      				//每扇区字节数
	SectorsPerClust   = (BYTE)bpb->bpbSecPerClust;				//每簇扇区数
	FirstFATSector   = bpb->bpbResSectors+hidsec;				//第一个FAT表扇区
	RootDirCount   = bpb->bpbRootDirEnts;      					//根目录项数
	RootDirSectors   = (RootDirCount*32)>>9;      				//根目录占用的扇区数
	FirstDirSector   = FirstFATSector+bpb->bpbFATs*FATsectors;	//第一个目录扇区
	FirstDataSector   = FirstDirSector+RootDirSectors;			//第一个数据扇区
	//printf("每扇区字节数:%d \n",BytesPerSector);
	//printf("每簇扇区数:%d \n",SectorsPerClust);
	//printf("第一个FAT表扇区:%d \n",FirstFATSector);
	//printf("根目录项数:%d \n",RootDirCount);
	//printf("根目录占用的扇区数:%d \n",RootDirSectors);
	return 0; 	 
}   
Exemplo n.º 2
0
 int main(void)
 {  	
 	u32 sector_size;
	u8 t=0;	
	SystemInit();
	delay_init(72);	     //延时初始化
	NVIC_Configuration();
 	uart_init(9600);
 	LED_Init();
 	KEY_Init();
	LCD_Init();
	POINT_COLOR=RED;//设置字体为红色	   
	LCD_ShowString(60,50,"Mini STM32");	
	LCD_ShowString(60,70,"SD Card TEST");	
	LCD_ShowString(60,90,"ATOM@ALIENTEK");
	LCD_ShowString(60,110,"2011/1/1");				 
	while(SD_Initialize()!=0)//检测不到SD卡
	{
		LCD_ShowString(60,130,"SD Card Failed!");
		delay_ms(500);
		LCD_ShowString(60,130,"Please Check!      ");
		delay_ms(500);
		LED0=!LED0;//DS0闪烁
	}
	//检测SD卡成功 											    
	LCD_ShowString(60,130,"SD Card Checked OK ");
	sector_size=SD_GetSectorCount();
	printf(" SD Sector Size:%x\n",sector_size);
	LCD_ShowString(60,150,"SD Card Size:    Mb");
	LCD_ShowNum(164,150,sector_size>>12,4,16);
	while(1)
	{
		if(t==30)//每6s钟执行一次
		{
			if(SD_ReadDisk(buf,0,1)==0)//读取MBR扇区
			{	
				LCD_ShowString(60,170,"USART1 Sending Data...");
				printf("SECTOR 0 DATA:\n");
				for(sector_size=0;sector_size<512;sector_size++)printf("%x ",buf[sector_size]);//打印MBR扇区数据    	   
				printf("\nDATA ENDED\n");
				LCD_ShowString(60,170,"USART1 Send Data Over!");
			}
			t=0;
		}   
		t++;
		delay_ms(200);
		LED0=!LED0;
	}
}
Exemplo n.º 3
0
DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;
	int result;


	switch (cmd) {
	case CTRL_SYNC:		
        SD_CS = 0;
        if(0 == SD_WaitReady())
        {
            res = RES_OK; 
        }
        else 
        {
            res = RES_ERROR;   
        }
        SD_CS = 1;
		break;

    case GET_SECTOR_SIZE:
        *(DWORD*)buff = SD_SECTOR_SIZE;
        res = RES_OK;
    break;        

	case GET_SECTOR_COUNT:
        *(DWORD*)buff = SD_GetSectorCount();
        res = RES_OK;
		break;

	case GET_BLOCK_SIZE:
        *(DWORD*)buff = 8;
        res = RES_OK;
		break;

	default:
		res = RES_PARERR;
	}

	return (res);
}
DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;						  			     
	if(pdrv==SD_CARD)//SD卡
	{
	    switch(cmd)
	    {
		    case CTRL_SYNC:
			SD_CS_Clr();
		        if(SD_WaitReady()==0)res = RES_OK; 
		        else res = RES_ERROR;	  
			SD_CS_Set();
		        break;	 
		    case GET_SECTOR_SIZE:
		        *(WORD*)buff = 512;
		        res = RES_OK;
		        break;	 
		    case GET_BLOCK_SIZE:
		        *(WORD*)buff = 8;
		        res = RES_OK;
		        break;	 
		    case GET_SECTOR_COUNT:
		        *(DWORD*)buff = SD_GetSectorCount();
		        res = RES_OK;
		        break;
		    default:
		        res = RES_PARERR;
		        break;
	    }
	}else res=RES_ERROR;//其他的不支持
    return res;
}
 int main(void)
 { 
	u8 offline_cnt=0;
	u8 tct=0;
	u8 USB_STA;
	u8 Divece_STA; 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组设置	 
	delay_init();		//延时函数初始化 
	uart_init(9600);	//串口初始化为9600 	  
	LCD_Init();			//初始化液晶 
	LED_Init();         //LED初始化	 
	KEY_Init();			//按键初始化	   
	
	POINT_COLOR=RED;	//设置字体为红色	   
	LCD_ShowString(60,50,200,16,16,"Mini STM32");	
	LCD_ShowString(60,70,200,16,16,"USB Card Reader TEST");	
	LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
	LCD_ShowString(60,110,200,16,16,"2014/3/15");	
 	SPI_Flash_Init();
	if(SD_Initialize())LCD_ShowString(60,130,200,16,16,"SD Card Error!");	//检测SD卡错误
	else //SD 卡正常
	{   															  
		LCD_ShowString(60,130,200,16,16,"SD Card Size:     MB");
 	    Mass_Memory_Size[0]=(long long)SD_GetSectorCount()*512;//得到SD卡容量(字节),当SD卡容量超过4G的时候,需要用到两个u32来表示
	    Mass_Block_Size[0] =512;//因为我们在Init里面设置了SD卡的操作字节为512个,所以这里一定是512个字节.
	    Mass_Block_Count[0]=Mass_Memory_Size[0]/Mass_Block_Size[0];
 		LCD_ShowNum(164,130,Mass_Memory_Size[0]>>20,5,16);	//显示SD卡容量
 	}
	if(SPI_FLASH_TYPE!=W25Q64)LCD_ShowString(60,130,200,16,16,"W25Q64 Error!");	//检测SD卡错误
	else //SPI FLASH 正常
	{   															  
 	   	Mass_Memory_Size[1]=4916*1024;//前4.8M字节
	    Mass_Block_Size[1] =512;//因为我们在Init里面设置了SD卡的操作字节为512个,所以这里一定是512个字节.
	    Mass_Block_Count[1]=Mass_Memory_Size[1]/Mass_Block_Size[1];
		LCD_ShowString(60,150,200,16,16,"SPI FLASH Size:4916KB");	 
	}
  	delay_ms(1800);
 	USB_Port_Set(0); 	//USB先断开
	delay_ms(300);
   	USB_Port_Set(1);	//USB再次连接   
 	LCD_ShowString(60,170,200,16,16,"USB Connecting...");//提示SD卡已经准备了	
	Data_Buffer=mymalloc(BULK_MAX_PACKET_SIZE*2*4);	//为USB数据缓存区申请内存
	Bulk_Data_Buff=mymalloc(BULK_MAX_PACKET_SIZE);	//申请内存
 	//USB配置
 	USB_Interrupts_Config();    
 	Set_USBClock();   
 	USB_Init();	    
	delay_ms(1800);	   	    
	while(1)
	{	
		delay_ms(1);				  
		if(USB_STA!=USB_STATUS_REG)//状态改变了 
		{	 						   
			LCD_Fill(60,190,240,190+16,WHITE);//清除显示			  	   
			if(USB_STATUS_REG&0x01)//正在写		  
			{
				LCD_ShowString(60,190,200,16,16,"USB Writing...");//提示USB正在写入数据	 
			}
			if(USB_STATUS_REG&0x02)//正在读
			{
				LCD_ShowString(60,190,200,16,16,"USB Reading...");//提示USB正在读出数据  		 
			}	 										  
			if(USB_STATUS_REG&0x04)LCD_ShowString(60,210,200,16,16,"USB Write Err ");//提示写入错误
			else LCD_Fill(60,210,240,210+16,WHITE);//清除显示	  
			if(USB_STATUS_REG&0x08)LCD_ShowString(60,230,200,16,16,"USB Read  Err ");//提示读出错误
			else LCD_Fill(60,230,240,230+16,WHITE);//清除显示    
			USB_STA=USB_STATUS_REG;//记录最后的状态
		}
		if(Divece_STA!=bDeviceState) 
		{
			if(bDeviceState==CONFIGURED)LCD_ShowString(60,170,200,16,16,"USB Connected    ");//提示USB连接已经建立
			else LCD_ShowString(60,170,200,16,16,"USB DisConnected ");//提示USB被拔出了
			Divece_STA=bDeviceState;
		}
		tct++;
		if(tct==200)
		{
			tct=0;
			LED0=!LED0;//提示系统在运行
			if(USB_STATUS_REG&0x10)
			{
				offline_cnt=0;//USB连接了,则清除offline计数器
				bDeviceState=CONFIGURED;
			}else//没有得到轮询 
			{
				offline_cnt++;  
				if(offline_cnt>10)bDeviceState=UNCONNECTED;//2s内没收到在线标记,代表USB被拔出了
			}
			USB_STATUS_REG=0;
		}
	};  										    			    
}