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

📄 fat_sys.c

📁 用at91sam7s64+ucos写的mp3播放程序
💻 C
📖 第 1 页 / 共 3 页
字号:
	chain[0].number = 1;  
	chain[0].cluster.w[1] = cluster;         /* store first cluster */
	cluster++;
	cluster2start++;
	fat_buf_count += 2;

	if (cluster != max_cluster)
	{
  		do                                    /* construct the list */
  		{
			fat_count_sector();
/*
		    if(fat_buf_count == fat_sector_size)
			{
	          //fat_read_sector(fat_ptr_fats+(cluster2start/(fat_sector_size/2)));
			  //phy_read_open(fat_ptr_fats+(cluster2start/(fat_sector_size/2)));
		      phy_read_sector();
			  fat_buf_count = 0;
			  //phy_read_close();
			}
*/    
	        cluster_free = FALSE;
	        tmp_cluster.b[1] = gl_buffer[fat_buf_count];       
	        tmp_cluster.b[0] = gl_buffer[fat_buf_count+1];      
	

	        if (tmp_cluster.w == 0x0000)
	           	cluster_free = TRUE;

    		if (cluster_free)
    		{ /* free cluster: add it to the list */
          		if (chain[index].number == MAX_CL_PER_FRAG) 
          		{
            		index++;
            		chain[index].number = 1;
            		chain[index].cluster.w[1] = cluster;
          		}
          		else
          		{
            		chain[index].number++;
          		}
		  		cluster++;
		  		cluster2start++;
		  		fat_buf_count += 2;
	      		//count=(cluster2start%(fat_sector_size/2))*2;
    		}
    		else
    		{ /* cluster already used */
          		do                                  /* search for next free fragment */
          		{
		            cluster++;
					cluster2start++;
					fat_buf_count += 2;
			        //count=(cluster2start%(fat_sector_size/2))*2;
	
					fat_count_sector();
	
			        tmp_cluster.b[1] = gl_buffer[fat_buf_count];        
			        tmp_cluster.b[0] = gl_buffer[fat_buf_count+1];      
		
			        if (tmp_cluster.w == 0x0000)
					{
			           	cluster_free = TRUE;
					   	break;
					}

          		}
          		while (cluster != max_cluster);

          		if (!cluster_free)
          		{
		            if (chain[index].number == 1)
		            { /* last cluster is the current one */
		              	fat_last_clust_index = index;
		              	chain[fat_last_clust_index].number = 0; /* end of list marker */
		            }
		            else
		            {
		              	fat_last_clust_index = index + 1;
		              	chain[index].number--;
		              	chain[fat_last_clust_index].cluster.w[1] = chain[index].cluster.w[1] + chain[index].number;
		              	chain[fat_last_clust_index].number = 0; /* end of list marker */
		            }
		            return OK;                        /* end of partition reached */
          		}
  
	          	index++;
	          	chain[index].number = 1;
	          	chain[index].cluster.w[1] = cluster;
	          	cluster++;
			  	cluster2start++;
			  	fat_buf_count +=2;
		      	//count=(cluster2start%(fat_sector_size/2))*2;
      		}
	  		nb_cluster++;
  		}
  		while ((index != (nb_frag - 1)) && (cluster < max_cluster) && (nb_cluster < fat_max_cluster_number));
	}	

	fat_last_clust_index = index;
	return OK;
}
#endif //COMPILE_FAT_16

#if COMPILE_FAT_12 == TRUE
byte fat12_set_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
	Byte     cluster_free;//,flag;
	Byte     fat12_parity;
	Byte    temp;
	Byte    index;
//	Uint16  count;
//word    sector_count; // 2004.2.25 modify
	Uint16  cluster;
	Uint16  max_cluster;
//	Union16 tmp_cluster;
//	Union32 cluster,cluster2start;
//	Union32 tmp_cluster;
//	Union32 max_cluster;	

	Uint16 nb_cluster;
	Uint16 max,min,mid;

    byte start_pos;
	Uint16 pos;
	byte sector_count ;
	byte max_sector;

   	max_cluster = fat_count_of_clusters;
    //max_cluster = fat_max_cluster_number;//2004.3.13 modify

  	index = 0;
  	nb_cluster = 0;
  	cluster_free = FALSE;

    phy_read_open(fat_ptr_fats);
	phy_read_sector();

	fat12_parity=0;    /*if is 0,then clusterNum is even,else is odd */

	max = fat_sector_size/3 - 2;
	min = 0;
	mid = (max - min)>>1 ;
	start_pos = 0;//(3 - (((Uint32)fat_sector_size) * sector_count) % 3)%3;
	pos = mid * 3 + start_pos;	
	max_sector = max_cluster / ((fat_sector_size / 3) << 1) + 1;
	sector_count = 0;

	do
	{
		while((max - min) > 1)
		{
			if((gl_buffer[pos] == 0x00) && (gl_buffer[pos + 1] == 0x00))
			{
				max = mid;
				mid = ((mid - min)>>1) + min;
				pos = mid * 3 + start_pos;			
			}
			else
			{
			    min = mid;
				mid = ((max - mid)>>1) + mid;
				pos = mid * 3 + start_pos;			
			}
		}
	
		if(max == (fat_sector_size/3 - 2))
		{
			phy_read_sector();
			sector_count++;
			min = 0;
            mid = (max - min)>>1 ;
			start_pos = (3 - (((Uint32)fat_sector_size) * sector_count) % 3)%3;
			pos = mid * 3 + start_pos;
		}
		else
		{
		    if(max == 1)
			{
			    if((gl_buffer[pos] == 0x00) && (gl_buffer[pos + 1] == 0x00))
				    max = 0;
			}
			fat_buf_count = start_pos + max * 3 + 2;
			//count += 2;
			temp = gl_buffer[fat_buf_count - 1];
			//cluster = 
			//cluster2start = ((((Uint32)fat_sector_size) * sector_count)/3) * 2 + (min + 1)*2;
			cluster = ((((Uint32)fat_sector_size) * sector_count)/3) * 2 + max*2 -2; //(max-1) * 2 ;//+ (((start_pos%3) == 0)?(0:1));
			if(start_pos%3 != 0)
				cluster += 2;
			cluster_free = TRUE;
			break;
		}
	}while(sector_count < max_sector);

	if((cluster_free==FALSE) || (cluster >= max_cluster))
	{
		return KO;     /* no free cluster found */
	}

	if(fat_buf_count >= fat_sector_size)
	{
		phy_read_sector();
		sector_count++;
		fat_buf_count = fat_buf_count - fat_sector_size;
	}

    /* At least we find one free cluster */
    chain[0].number = 1;
    chain[0].cluster.w[1] = cluster;        /* store the first cluster */
    cluster++;

	if(cluster != max_cluster)
	{
		do                                /* construct the list */
		{
			cluster_free = FALSE;
            if(fat12_parity == 0)
			{
				fat12_parity = 1;

				if(gl_buffer[fat_buf_count++] == 0x00)
				{
					fat_count_sector();
/*
					if(fat_buf_count == fat_sector_size)
					{
						phy_read_sector();
						fat_buf_count = 0;
					}
*/
					temp=gl_buffer[fat_buf_count++];

					if((temp & 0x0f)==0x00)
					{
						cluster_free=TRUE;
					}
				}
				else
				{
					fat_count_sector();
					temp=gl_buffer[fat_buf_count++];
				}
			}
			else
			{
				fat12_parity=0;

				if((temp & 0xf0)==0x00)
				{
					if(gl_buffer[fat_buf_count++]==0x00)
					{
						cluster_free=TRUE;
					}
				}
				else
				{
					fat_buf_count++;
				}
			}
            //2004.2.26 modify
			fat_count_sector();

			if(cluster_free)     /* free cluster: add it to the list */
			{
				if(chain[index].number == MAX_CL_PER_FRAG)
				{
					index++;
					chain[index].number=1;
					chain[index].cluster.w[1]=cluster;
					cluster++;
				}
				else
				{
					chain[index].number++;
					cluster++;
				}
			}
			else               /* cluster already used */
			{
				do             /* search for next free cluster */
				{
					if(fat12_parity==0)
					{
						fat12_parity=1;

						if(gl_buffer[fat_buf_count++]==0x00)
						{
							fat_count_sector();

							temp=gl_buffer[fat_buf_count++];

							//2004.2.26 modify
							fat_count_sector();

							if((temp & 0x0f)==0x00)
							{
								cluster_free=TRUE;
								break;
							}
							else
							{
								cluster++;
								fat_buf_count++;
							}
						}
						else
						{
							fat_count_sector();
							temp=gl_buffer[fat_buf_count++];
							cluster++;
						}
					}
					else
					{
						fat12_parity=0;

						if((temp & 0xf0)==0x00)
						{
							fat_count_sector();

							if(gl_buffer[fat_buf_count++]==0x00)
							{
								cluster_free=TRUE;
								break;
							}
							else
							{
								cluster++;
							}
						}
						else
						{
							fat_buf_count++;
							cluster++;
						}
					}
					fat_count_sector();
				}
				while((cluster+1)!=max_cluster);

			    if (!cluster_free)      /* In this new fragment, we don't find any free cluster */
				{
					if (chain[index].number == 1)
					{   /* last cluster is the current one */
						fat_last_clust_index = index;
						chain[fat_last_clust_index].number = 0; /* end of list marker */
					}
					else
					{
						fat_last_clust_index = index + 1;
						chain[index].number--;
						chain[fat_last_clust_index].cluster.w[1] = chain[index].cluster.w[1] + chain[index].number;
						chain[fat_last_clust_index].number = 0; /* end of list marker */
					}
					return OK;                        /* end of partition reached */
				}
			    cluster++;                          
			    index++;
			    chain[index].number = 1;
			    chain[index].cluster.w[1] = cluster;
			    cluster++;
			}   //else

			nb_cluster++ ;
		}
		while((index != (nb_frag - 1)) && ( (cluster + 1) < max_cluster) && (nb_cluster < fat_max_cluster_number));
    }
	fat_last_clust_index = index;
	return OK;
}
#endif //COMPILE_FAT_12


#if COMPILE_FAT_32 == TRUE
byte fat32_set_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
	Byte     cluster_free;//,flag;
	Byte    index;
//	Uint16  max_cluster;
	Union32 cluster,cluster2start;
	Union32 tmp_cluster;	
	Uint16 nb_cluster;
	Uint16 max,min;

//   	max_cluster = fat_count_of_clusters;
    //max_cluster = fat_max_cluster_number;//2004.3.13 modify
  	index = 0;
  	nb_cluster = 0;
  	cluster_free = FALSE;

    phy_read_open(fat_ptr_fats);
	phy_read_sector();

	cluster.l = 0;
    cluster2start.l=0;//2;
 
    max = fat_sector_size>>2;
	min = 0;
	fat_buf_count = fat_sector_size>>3;
	do
	{
		while((max - min) > 1)
		{
		   tmp_cluster.b[3] = gl_buffer[fat_buf_count<<2];  
		   tmp_cluster.b[2] = gl_buffer[(fat_buf_count<<2)+1];
		   tmp_cluster.b[1] = gl_buffer[(fat_buf_count<<2)+2];
		   tmp_cluster.b[0] = gl_buffer[(fat_buf_count<<2)+3];
	  
		   if(tmp_cluster.l == 0x00000000)
		   {
		       max = fat_buf_count;
		   	   fat_buf_count = ((fat_buf_count - min) >> 1) + min ;			   
		   }
		   else
		   {
		       min = fat_buf_count;
		   	   fat_buf_count = ((max - fat_buf_count) >> 1) + fat_buf_count;		  
		   }		   
		}
        if(max == 1)
        {
		    tmp_cluster.b[3] = gl_buffer[0];   
		    tmp_cluster.b[2] = gl_buffer[1];
		    tmp_cluster.b[1] = gl_buffer[2];   
		    tmp_cluster.b[0] = gl_buffer[3];

			if(tmp_cluster.l == 0x00000000)
			{
				max = max - 1;
			}
	    }			//min = 
	  		 	 	 
		if(max == (fat_sector_size>>2))
		{
		 	phy_read_sector();
            max = fat_sector_size>>2;
			min = 0;
		    fat_buf_count = fat_sector_size>>3;
			cluster.l += max;
            //cluster2start += n;
		}
	    else
		{
		    fat_buf_count = max<<2;
		    cluster.l += max; //count / 2;
			//cluster2start += count / 2;
			cluster_free = TRUE;
			break;
		}
	}while(cluster.l < fat_count_of_clusters);

	cluster2start.l = cluster.l;
	cluster.l -= 2;	
	
	if(!cluster_free)
	{
		return KO;
	}

	chain[0].number = 1;
    chain[0].cluster.l = cluster.l;         /* store first cluster */
    cluster.l++;
	cluster2start.l++;
	fat_buf_count += 4;

	do
	{
		if(fat_buf_count == fat_sector_size)
		{
			fat_buf_count = 0;
			phy_read_sector();
		}
		cluster_free = FALSE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -