Jump to content
43oh

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.

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?

Link to post
Share on other sites
  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...