Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to access a FAT16 file system (MMC card)

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
751
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,054
I need to search *.wav files stored in an MMC which is formated with FAT16 filing system. I don't like to use any library functions...
But now my knowledge about a file system is 'zero'. I would like to know how the files are arranged in a FAT file system and also where(in memory card(i exactly mean the location)) the FILE NAMES and their SECTOR START/STOP ADDRESS (for each file) will be available?
 
Last edited:


Another thing I found useful when I built a MMC/SD interface, was having a MMC/SD card reader and the WinHex program:
WinHex: Hex Editor & Disk Editor, Computer Forensics & Data Recovery Software

When you use WinHex you'll need to use the "physical" drive sector access so that you can see the actual sector 0, etc. Logical sectors are offset.
But i couldn't write to sector. It is showing to register. And i tried HxD hex editor. In that i can open disk but cant edit. So how could i edit disk?
 

I used WinHex to view the important parts of the disk...Boot sector, partition table, FAT, so that I could write my routines for the PIC. I never had a need to write to the card from the computer.
 

yes you are right. Its really helpful in that case. It automatically shows the boot sector, data sector, directory etc. Now i had seen the file directory which starts from particular sector number. Then i could see some file names also. But i feel they are arranged some thing like scattered manner. Then where will be their corresponding start sector and end sector or such required date? If i am getting that then i am sure that i could do it..In PIC16F877A , what i am now thinking to do is, i may define a char buffer[102] then i may repeat a single sector read function 6 times in a loop. Now if it reads the sector for first time then the first 102 bytes will be send to the buffer. Then i can process the buffer. Then i will read the same sector second time. Then there i will send next 102 bytes to buffer. Similarly i repeat it 6 times.
always the root directory starts from a fixed sector?
Any start token for a file name start?
Any end token for a file name end?
Similarly any start and end token for corresponding file sector address details?
If i am getting atleast there informations then i hope some how i could manage to read some files copied from my PC using the PIC16F877A.
 

See my post in your other thread. I think what you want to do is store the sector *locations* in the buffer.

A file is composed of a number of sectors (512 bytes). The locations of these sectors are stored in the FAT. And they won't always be in sequencial numerical order (1,2,3,4 etc.)...They will often be like this: 12,223,394,401, etc. Or even: 12,223,52,9...

So, if your buffer contains the list of sector numbers for the file, then you can process the list by reading each sector and sending it to the PWM.

Getting the list of sectors will be the hard part. Go through this to understand how the root directory and the files are stored: Paul's 8051 Code Library: Understanding the FAT32 Filesystem

Nothing is in a fixed location, except for the Master Boot Record (sector 0).

Additionally, there may be one problem that I encountered with MMC. The card I had was formatted as a "superfloppy", which means that it had no MBR (and no partition table). Sector 0 was the partition's boot record. I don't know if there's a proper way to detect this, but what I used was read the first 8 characters of sector 0 and if chars 4-8 said "MSDOS" then I considered it a "superfloppy", otherwise it was a regular FAT16 disk.
 

BOSS :wink:
I got many information about FAT file system from ur first link. I understood clusters, FAT, root dir etc. Now i am little bit confident. But after learning these things i understood the difficulty of writing a FAT accessing program even if the PIC is having enough RAM!:-? But any way, at present i cant shift to 18f. So i may continue with PIC16F877A.

some doubts :

1>I formatted the MMC(64 MB) using a nokia6230i phone. Then i opened it via WINHEX.Then i observed it as FAT16. Then from Master Boot Record i got 1 cluster is 4 sectors.Then i could see FAT1 starts from second sector.
But my doubt is, how could i know FAT1 starts from second sector. Because when i formatted it via windows (FAT16), then i observed FAT1 starts from another sector and not from sector2.

2>What is the significance of FAT2? I observed same 'next cluster numbers' in both FAT1 and FAT2 . Then what is the difference between both?

3>Where these records are available, ie from which sector FAT1 starts, from which sector root dir starts, from which sector cluster 2 (first data sector) starts etc? In my case, i could see it as sector 279. But it is always like that only?

4>I copied a 270kb jpg to MMC. Then i checked the start cluster for that file from the root dir. Then i checked the FAT1 and i observed that cluster position and i see the next cluster to be read after that cluster. But what i observed is , the clusters are successive. But any way, as u told, it may not be successive if more files are copied to the MMC. Right?
 

1. The starting points for FAT1 and FAT2 is a calculation. The information is in the links provided.

2. The reason for two FATs is redundancy, I guess. I always used FAT1.

3. See #1.

4. Correct, clusters won't always be successive. It's a linked list. See the recent link. You get the first one, which tells you were the second one is, which tells you where the third one is... That's why I think you should read in the list of cluster numbers and then loop through the list.

I hope this helps.
 
access FAT16 with PIC16F877A

Now my work is in progress...
Now i wrote a program to display all the file names in root directory on the LCD. Then it worked fine. I checked it with 64MB,512MB and 1GB.

Here i made a 64 byte array. Then i made a function ,
load_buff(unsigned long int sector,char num);
It read 'sector' to 64byte array. I used 'num' to select which 64 bytes (1 out of 8 .) is to be loaded to the buffer. But for reading 64 bytes it will take 1 sector-reading time.

By above method, i could address and select any 64bytes in the memory card.
Using same function, i am supposed to read the FAT also.
I expect your suggestions and PITFAL warnings.:)(@ upand_at_them)
 
Last edited:

YES.....DONE!!!!!!!!
NOW I COULD STREAM OUT ANY FILE COPIED TO AN MMC CARD (FATI6) USING MY PIC16F877A.

I could see every files in the root directory on an LCD. If i press a push button then that file will be displayed on a 16x2 LCD. If that file is a ASCII text then i could read the text file on the LCD. I copied two big text file (470kb and 250kb) in the MMC and i read the file successfully.


But i had coded it in such a way that after streaming a cluster, it will read particular sector of the FAT to get the next cluster and then it will stream the next cluster and this repeats until the read cluster is 0xffff.

But i think it will be less efficient because after every cluster read ,i need to check the corresponding FAT sector.
But any way, i am satisfied with this...
Ones again,
Thanks for your help @Upand_at_them.
 

upand_at_them said:
Very nice, Vinod. It sounds very good.

Are you able to read the cluster addresses into an array before playing the song? I haven't tried this myself, but I thought it might be a practical way of doing it. That way you don't have to find the next cluster address while the song is playing, you can simply go to the next array element and then load that cluster from the MMC.

No friend i didn't saved the cluster numbers to any array, instead, i read each one after every cluster read...
But i couldn't get the idea, ie the advantage of saving few cluster numbers to an array...Then what i doubt is, even if i save a few cluster numbers,then also, i need to read the cluster numbers while it is playing the song. So, if the next cluster array filling will also consume time... That is why i couldn't understand the advantage of this....

Then friend, in that video, what i had done is , i send each byte to PWM just after reading.....Then i feel some vibration in sound ...i mean unwanted periodic vibration due to the time for the calculation of next cluster location while playing...

Now, i improved it little bit ... Now i used 2 - 64BYTE arrays and used timer interrupt to send bytes to PWM.
Then the quality increased!!! But still need to increase a lot:)

What i had done is, it fills first 64 byte while the second 64byte is sending to PWM .Then it will fill the second 64byte while reading first 64 byte. So, now at the last 64 byte reading in a cluster, which completes the cluster reading , there will be a 64 byte buffer data which plays while the pic search for next cluster number.

The part of my improved code(PIC16F877A) is below, and it improved the audio quality little more....

PHP:
void interrupt timer()
{	
	if(toggle==1)
	{
	{CCPR2L=play1[zz];}zz++;if(zz==64){zz=0;toggle=0;}
	}
	else if(toggle==0)
	{
	{CCPR2L=play2[zz];}zz++;if(zz==64){zz=0;toggle=1;}
	}
	

	TMR0=100;
	TMR0IF=0;
	
}



void read(unsigned long int c)
	{
	for(i=0;i<=sect_per_clst;i++)
		{	
		long1=512*c;
		command(17,long1,0xff);
		while(readdata!=0){spi_read();}
		while(readdata!=0xfe){spi_read();}
		for(g=0;g<4;g++)
			{
			while(toggle==0);
				{for(zx=0;zx<64;zx++){spi_read();play2[zx]=readdata;}}
			while(toggle==1);
		
				{for(zx=0;zx<64;zx++){spi_read();play1[zx]=readdata;}}
			
			}
		spi_write(0xff);
		spi_write(0xff);
		c+=1;
		}	
	}

I don't know if my buffer concept is correct...That is why i posted a part of my code so that if any one have any suggestions, then i could try that.....
 
Last edited:

Hello Vinod. Its a excellent work. I am developing the same application. I just wanted to know whether can i proceed with P89V51RD2 which has RAM of 1 KB and Flash Rom of 64 KB? Thanks in advance
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top