inspect 0 Posted September 19, 2013 Share Posted September 19, 2013 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. Quote Link to post Share on other sites
bluehash 1,581 Posted September 19, 2013 Share Posted September 19, 2013 Did you check the lock tab on the card? Quote Link to post Share on other sites
energia 485 Posted September 19, 2013 Share Posted September 19, 2013 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. Quote Link to post Share on other sites
inspect 0 Posted September 20, 2013 Author Share Posted September 20, 2013 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? Quote Link to post Share on other sites
calinp 24 Posted September 20, 2013 Share Posted September 20, 2013 For windows you can use the command described below: http://technet.microsoft.com/en-us/library/cc788058.aspx The file you are writing to, must exist on the card and be big enough for your needs. Calin Quote Link to post Share on other sites
energia 485 Posted September 21, 2013 Share Posted September 21, 2013 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. Quote Link to post Share on other sites
vissu401 0 Posted August 25, 2016 Share Posted August 25, 2016 refer this https://www.element14.com/community/groups/internet-of-things/blog/2014/07/26/tiva-launchpad-building-an-eventdata-logger--part-1-sd-card-capability Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.