Jump to content
tom 2007

Problem With Petit Fat File System

Recommended Posts

I'm trying to access an SD card using the Petit FAT File System but it's not really working properly:

 

First there was this problem in the pf_read() function:

				
clst = (fs->fptr == 0) ? /* On the top of the file? */
fs->org_clust : get_fat(fs->curr_clust);

 

This didn't work (fs->fptr == 0) equals true but clst remains 0 and fs->org_clust was 250.

After replacing this code with this:

if (fs->fptr==0){
clst = fs->org_clust;
}else{
clst = get_fat(fs->curr_clust);
}

this line of code finaly started working properly (clst became 250).

 

Now i still have the problem of the Fat file system structure which is declared as a variable in main() getting corrupt by calling the send_cmd() function:

 

BYTE send_cmd (BYTE cmd,DWORD arg){
BYTE n;
BYTE res;

if (cmd & 0x80) {	/* ACMD<n> is the command sequense of CMD55-CMD<n> */
	cmd &= 0x7F;
	res = send_cmd(CMD55, 0);
	if (res > 1) return res;
}

pff_select();
/* Send a command packet */
xmit_spi(cmd);						/* Start + Command index */
xmit_spi((BYTE)(arg >> 24));		/* Argument[31..24] */
xmit_spi((BYTE)(arg >> 16));		/* Argument[23..16] */
xmit_spi((BYTE)(arg >> 8));			/* Argument[15..8] */
xmit_spi((BYTE)arg);				/* Argument[7..0] */
n = 0x01;							/* Dummy CRC + Stop */
if (cmd == CMD0) n = 0x95;			/* Valid CRC for CMD0(0) */
if (cmd == CMD8) n = 0x87;			/* Valid CRC for CMD8(0x1AA) */
xmit_spi(n);
serial_send_string("fat c2:");
serial_send_long(fs->n_fatent);
serial_send_string("\r\n");
/* Receive a command response */
n = 10;								/* Wait for a valid response in timeout of 10 attempts */
do {
	res = rcv_spi();
} while ((res & 0x80) && --n);

//up to here there is no problem, nr of clusters = 15546
return res; //after this line the number of clusters (stored in the fat file system structure in main()) is corrupted (high byte is set to 0)!
}

 

the function is called from (disk_readp) like this:

//no problem with the file system structure (number of clusters is 15546)
BYTE tmp = send_cmd(CMD17, lba);
//here the number of clusters has changed to 186 high byte is 0

 

Has anyone ever had the same problem? I'm not sure if this is a bug in the compiler but because it happens when the send_cmd function returns I don't know what to do anymore :(

Is there somewhere a working example of boostc and FAT(32) / SD cards?

 

I'm using boostc 7.03 and PIC18F4550

 

Thanks a lot

Edited by tom 2007

Share this post


Link to post
Share on other sites

tom 2007,

 

I'm trying to access an SD card using the Petit FAT File System but it's not really working properly:

 

First there was this problem in the pf_read() function:

				
clst = (fs->fptr == 0) ? /* On the top of the file? */
fs->org_clust : get_fat(fs->curr_clust);

 

This didn't work (fs->fptr == 0) equals true but clst remains 0 and fs->org_clust was 250.

After replacing this code with this:

if (fs->fptr==0){
clst = fs->org_clust;
}else{
clst = get_fat(fs->curr_clust);
}

this line of code finaly started working properly (clst became 250).

 

Now i still have the problem of the Fat file system structure which is declared as a variable in main() getting corrupt by calling the send_cmd() function:

 

BYTE send_cmd (BYTE cmd,DWORD arg){
BYTE n;
BYTE res;

if (cmd & 0x80) {	/* ACMD<n> is the command sequense of CMD55-CMD<n> */
	cmd &= 0x7F;
	res = send_cmd(CMD55, 0);
	if (res > 1) return res;
}

pff_select();
/* Send a command packet */
xmit_spi(cmd);						/* Start + Command index */
xmit_spi((BYTE)(arg >> 24));		/* Argument[31..24] */
xmit_spi((BYTE)(arg >> 16));		/* Argument[23..16] */
xmit_spi((BYTE)(arg >> 8));			/* Argument[15..8] */
xmit_spi((BYTE)arg);				/* Argument[7..0] */
n = 0x01;							/* Dummy CRC + Stop */
if (cmd == CMD0) n = 0x95;			/* Valid CRC for CMD0(0) */
if (cmd == CMD8) n = 0x87;			/* Valid CRC for CMD8(0x1AA) */
xmit_spi(n);
serial_send_string("fat c2:");
serial_send_long(fs->n_fatent);
serial_send_string("\r\n");
/* Receive a command response */
n = 10;								/* Wait for a valid response in timeout of 10 attempts */
do {
	res = rcv_spi();
} while ((res & 0x80) && --n);

//up to here there is no problem, nr of clusters = 15546
return res; //after this line the number of clusters (stored in the fat file system structure in main()) is corrupted (high byte is set to 0)!
}

 

the function is called from (disk_readp) like this:

//no problem with the file system structure (number of clusters is 15546)
BYTE tmp = send_cmd(CMD17, lba);
//here the number of clusters has changed to 186 high byte is 0

 

Has anyone ever had the same problem? I'm not sure if this is a bug in the compiler but because it happens when the send_cmd function returns I don't know what to do anymore :(

Is there somewhere a working example of boostc and FAT(32) / SD cards?

 

I'm using boostc 7.03 and PIC18F4550

 

Thanks a lot

These do sound like compiler bugs.

Please try BoostC V7.04. If you still have the same issues it would be good if you can provide a simple project that demonstrates the issues and can be run under the SourceBoost IDE debugger/simulator, then we should be able to help.

 

Regards

Dave

Share this post


Link to post
Share on other sites

I can give my project but it will not work in the IDE because it needs to be able to send/receive data on the SPI port with the SD card.

I've tried changing the project so it runs in the IDE but this requires me to change a lot of stuff then the bug doesn't happen anymore :(

 

The hardware is very basic: PIC18F4550 + SD card holder connected to the SPI interface and 1 pin for sending debug information to the serial port of the computer...

Share this post


Link to post
Share on other sites

tom 2007,

I can give my project but it will not work in the IDE because it needs to be able to send/receive data on the SPI port with the SD card.

I've tried changing the project so it runs in the IDE but this requires me to change a lot of stuff then the bug doesn't happen anymore :(

 

The hardware is very basic: PIC18F4550 + SD card holder connected to the SPI interface and 1 pin for sending debug information to the serial port of the computer...

Send us the bad project and we can take a look, send project to support@sourceboost.com.

 

Regards

Dave

Share this post


Link to post
Share on other sites

tom 2007,

 

The problems you see seems to be that the software stack used for variables does handle recursion as intended, that is as you found other variables on the software stack can become corrupted. This is not meant to happen.

 

But even if that did work recursion as intended there is still a problem, that is a recursive function call will over right its arguments and local variables. With a correctly crafted function this isn't necessarily an issue.

 

The answer will be to separate out the code in send_cmd into two funtions, something along the lines of:

unsigned char send_cmd (BYTE cmd,DWORD arg){
unsigned char res;
FATFS * fs = pf_fatfs();

if (cmd & 0x80) {	/* ACMD<n> is the command sequense of CMD55-CMD<n> */
	cmd &= 0x7F;
	res = send_cmdxxx(CMD55, 0);
	if (res > 1) return res;
}

return send_cmdxxx( cmd );
}

unsigned char send_cmdxxx (BYTE cmd,DWORD arg){
BYTE n;
unsigned char res;
FATFS * fs = pf_fatfs();

pff_select();
/* Send a command packet */
xmit_spi(cmd);						/* Start + Command index */
xmit_spi((BYTE)(arg >> 24));		/* Argument[31..24] */
xmit_spi((BYTE)(arg >> 16));		/* Argument[23..16] */
xmit_spi((BYTE)(arg >> 8));			/* Argument[15..8] */
xmit_spi((BYTE)arg);				/* Argument[7..0] */
n = 0x01;							/* Dummy CRC + Stop */
if (cmd == CMD0) n = 0x95;			/* Valid CRC for CMD0(0) */
if (cmd == CMD8) n = 0x87;			/* Valid CRC for CMD8(0x1AA) */
xmit_spi(n);
/* Receive a command response */
n = 10;								/* Wait for a valid response in timeout of 10 attempts */
do {
	res = rcv_spi();
} while ((res & 0x80) && --n);
serial_send_string("fatent in send_cmd:");
serial_send_long(fs->n_fatent);
serial_send_string("\r\n");
return res;			/* Return with the response value */
}

 

I hope that helps.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...