Jump to content
inspect

Pfatfs library example write problem

Recommended Posts

Hello,

 

I am using example code of energia Pfatfs library named "PFatFsfileTest". This program reads "T_READ.txt" file from sd card properly.

But It can't write string "Hello world" 10 times to the file "T_WRITE.txt" into sd card. Instead, it just writes only "H".

 

I am posting the original code of file below:

/*----------------------------------------------------------------------*/
/* Petit FatFs sample project for generic uC  (C)ChaN, 2010             */
/*----------------------------------------------------------------------*/
/* ported to Energia */
/* copy the two files t_read.txt and t_write.txt from the example folder in the root of the sd card*/

 
#include "SPI.h" 
#include "pfatfs.h"

#define cs_pin      10             // chip select pin 
#define read_buffer 128             // size (in bytes) of read buffer 

  unsigned short int bw, br;//, i;
  char buffer[read_buffer];
  int rc;
  DIR dir;				/* Directory object */
  FILINFO fno;			/* File information object */

void setup() {

         pinMode(PUSH2, INPUT_PULLUP);
         
         Serial.begin(9600);            // initialize the serial terminal
         FatFs.begin(cs_pin);             // initialize FatFS library calls
         }
void die (		/* Stop with dying message */
         int pff_err	/* FatFs return value */
         )
         {
         Serial.println();Serial.print("Failed with rc=");Serial.print(pff_err,DEC);
         for (; ;
  }


/*-----------------------------------------------------------------------*/
/* Program Main                                                          */
/*-----------------------------------------------------------------------*/

void loop()
{
        Serial.println();
        Serial.println("Press button to start...");
        while(digitalRead(PUSH2)==1){}
        delay(100);
        while(digitalRead(PUSH2)==0){}
        
        Serial.println();
	Serial.println("Open a test file (t_read.txt).");
        delay(100);
	rc = FatFs.open("T_READ.TXT");
	if (rc) die(rc);

	Serial.println();
        Serial.println("Type the file content.");
        delay(100);
	for (; {
		rc = FatFs.read(buffer, sizeof(buffer), &br);	/* Read a chunk of file */
		if (rc || !br) break;			/* Error or end of file */
		for (uint16_t i = 0; i < br; i++)		/* Type the data */
			Serial.print(buffer[i]);
                        delay(100);
	}
	if (rc) die(rc);

        Serial.println();
	Serial.println("Open a file to write (t_write.txt).");
	delay(100);
        rc = FatFs.open("T_WRITE.TXT");
	if (rc) die(rc);

	Serial.println();
        Serial.println("Write a text data. (10 x Hello world!)");
        delay(100);
        bw=0;
        for (uint16_t i=0;i<10;i++) {
		rc = FatFs.write("Hello world!\r\n", 14, &bw);
                if (rc || !bw) break;
	}
	if (rc) die(rc);
    
	rc = FatFs.write(0, 0, &bw);  //Finalize write
	if (rc) die(rc);

     
        delay(100);
        Serial.println();
	Serial.println("Verify the write process (t_write.txt).");
        delay(100);
	rc = FatFs.open("T_WRITE.TXT");
	if (rc) die(rc);

	Serial.println();
        Serial.println("Type the file content.");
        delay(100);
	for (; {
		rc = FatFs.read(buffer, sizeof(buffer), &br);	/* Read a chunk of file */
		if (rc || !br) break;			/* Error or end of file */
		for (uint16_t i = 0; i < br; i++)		/* Type the data */
			Serial.print(buffer[i]);
                        delay(100);
        }
	if (rc) die(rc);
        
        Serial.println();
	Serial.println("Open root directory.");
	delay(100);
        rc = FatFs.opendir(&dir, "");
	if (rc) die(rc);

	Serial.println();
        Serial.println("Directory listing...");
        delay(100);
	for (; {
		rc = FatFs.readdir(&dir, &fno);	/* Read a directory item */
		if (rc || !fno.fname[0]) break;	/* Error or end of dir */
		if (fno.fattrib & AM_DIR) {Serial.print("<dir>\t"); Serial.println(fno.fname);delay(100);}
		else {Serial.print(fno.fsize);Serial.print("\t"); Serial.println(fno.fname);delay(100);}
	}
	if (rc) die(rc);

        Serial.println();
	Serial.print("Test completed.");
	//for (; ;
}

and i am also posting my terminal output below:


Press button to start...

Open a test file (t_read.txt).

Type the file content.
abc
Open a file to write (t_write.txt).

Write a text data. (10 x Hello world!)

Verify the write process (t_write.txt).

Type the file content.
H
Open root directory.

Directory listing...
3	T_READ.TXT
3858759679	m
3842012416	N
1409286144	EWTEX~1T.XT

Test completed.
Press button to start...

Plz tell me the solution as soon as possible.

Share this post


Link to post
Share on other sites

Did you check the lock tab on the card?

Actually, i am using old MMC with 64 mb.so, it does not have lock tab.

 

 

This Sketch does not seem to follow guide and take in account the limitations posted: http://elm-chan.org/fsw/ff/pf/write.html

Do you have both t_write.txt and t_read.txt on your SD card? As the guide states, it is not able to create files so t_write.txt must be present on the SD card before running the Sketch.

I've also copied T_READ.txt and T_WRITE.txt before running the sketch.

But oh!!! I see ,I created new blank T_WRITE.txt with one character  in MMC but didn't copy the provided blank T_WRITE.txt in example folder with 2KB size. Above link enlists one of the restriction as "cannot expand file size". so is it due to this restriction that i am able to write only one character?

 

Do we need to first define the size of file to be written?

Share this post


Link to post
Share on other sites

Would you please be so kind to add [sOLVED] to the title if you got it to work correctly with T_WRITE.txt that was provided with the library? This will help others when browsing the forum.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×