Jump to content
43oh

[Energia Library] Petit FatFS SD Card Library


Recommended Posts

From the description of the error codes (pff.h),

pff.h

/* File function return code (FRESULT) */

typedef enum {
	FR_OK = 0,			/* 0 */
	FR_DISK_ERR,		/* 1 */
	FR_NOT_READY,		/* 2 */
	FR_NO_FILE,			/* 3 */
	FR_NOT_OPENED,		/* 4 */
	FR_NOT_ENABLED,		/* 5 */
	FR_NO_FILESYSTEM	/* 6 */
} FRESULT;

I understand that rc=3 means the file you are trying to open doesn't exist and that rc=6 means bad connection.

In the same time, reading one of the original posts:

Quote

The write function has some limitations :

  • Cannot create file. Only existing file can be written.
  • Cannot expand file size.
  • Cannot update time stamp of the file.
  • Write operation can start/stop on the sector boundary.
  • Read-only attribute of the file cannot block write operation.

I understand that the files should exist before trying to access them.

With these considerations, I suppose your first set-up (the one with rc=3) is correct and the file you try to access doesn't exist.

Cheers,

Liviu

PS I'll PM you my bank data.

Link to post
Share on other sites
  • 2 weeks later...
  • Replies 148
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Hello,   This is a port of ChaN Petit FatFS SD library targeted to low RAM microcontrollers and offers a minimal FAT implementation, with limited write support. For more details see http://elm-chan.

A small update: - all config settings in one file - pffconf.h - a soft SPI driver ( shuftIn/shiftOut ) that can be enabled in pffconf.h. It defaults on HW SPI pins, but these can be changed using Fa

Hi, i just wanted to report that the library won't compile with Energia release 0101E0011 and the F5529 Launchpad because "DIR" is already defined in \energia-0101E0011\hardware\tools\msp430\msp430\in

Posted Images

Code:

 

/*----------------------------------------------------------------------*/
/* 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 

  uint16_t 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.print("\nMount a volume.\n");
    //rc = FatFs.mount(&fatfs);
    //if (rc) die(rc);

        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);
                        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);


//    Serial.println();
//        Serial.println("Terminate the file write process.");
//        delay(100);
//    rc = FatFs.write(0, 0, &bw);
//    //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);
                        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 (;;) ;
}

 

error :

Energia: 1.6.10E18 (Linux), Board: "MSP-EXP430F5529LP"

WARNING: Spurious .github folder in 'RTClib' library
<command-line>:0:12: warning: missing whitespace after the macro name [enabled by default]
In file included from /media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:17:0,
                 from /media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/examples/PetitFatFsTest/PetitFatFsTest.ino:9:
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:120:3: error: expected unqualified-id before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:120:3: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:158:25: error: expected primary-expression before ',' token
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:158:27: error: expected primary-expression before 'const'
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:158:38: error: expression list treated as compound expression in initializer [-fpermissive]
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:159:25: error: expected primary-expression before ',' token
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:159:34: error: expected primary-expression before '*' token
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:159:35: error: expected primary-expression before ')' token
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pff.h:159:35: error: expression list treated as compound expression in initializer [-fpermissive]
In file included from /media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/examples/PetitFatFsTest/PetitFatFsTest.ino:9:0:
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:31:11: error: expected unqualified-id before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:31:11: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:75:19: error: expected identifier before '(' token
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:75:19: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:75:19: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:75:19: error: expected ';' at end of member declaration
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:75:19: error: expected unqualified-id before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:78:19: error: expected identifier before '(' token
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:78:19: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:78:19: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:78:19: error: expected ';' at end of member declaration
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:78:19: error: expected unqualified-id before numeric constant
PetitFatFsTest:17: error: expected unqualified-id before numeric constant
PetitFatFsTest:17: error: expected ')' before numeric constant
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/examples/PetitFatFsTest/PetitFatFsTest.ino: In function 'void loop()':
PetitFatFsTest:62: error: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'WORD* {aka short unsigned int*}' [-fpermissive]
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:69:10: error:   initializing argument 3 of 'FRESULT PFFS::read(void*, WORD, WORD*)' [-fpermissive]
PetitFatFsTest:81: error: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'WORD* {aka short unsigned int*}' [-fpermissive]
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:83:10: error:   initializing argument 3 of 'FRESULT PFFS::write(const void*, WORD, WORD*)' [-fpermissive]
PetitFatFsTest:85: error: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'WORD* {aka short unsigned int*}' [-fpermissive]
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:83:10: error:   initializing argument 3 of 'FRESULT PFFS::write(const void*, WORD, WORD*)' [-fpermissive]
PetitFatFsTest:106: error: invalid conversion from 'uint16_t* {aka unsigned int*}' to 'WORD* {aka short unsigned int*}' [-fpermissive]
/media/hemang/586E740B6E73DFE4/msp/energia-1.6.10E18/libraries/PFatFs/pfatfs.h:69:10: error:   initializing argument 3 of 'FRESULT PFFS::read(void*, WORD, WORD*)' [-fpermissive]
PetitFatFsTest:117: error: 'dir' was not declared in this scope
exit status 1
expected unqualified-id before numeric constant

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 

Link to post
Share on other sites
21 hours ago, LiviuM said:

Have you read this post and the following ones (with a possible solution)?

LiviumM thank you very much for support.

But i still get error rc=6.

I  have connected my pins

MSP       SD card

P2.6       1 - SS

P3.1 MISO     2-MOSI

GND       3-GND

VCC        4-VCC

P3.2 CLK   5-SCK

GND         ---- 6

P3.0 MOSI         7 MISO

I am using 5529 Launchpad

Code :  

/*----------------------------------------------------------------------*/
/* 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      P2_6             // 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;
  FSDIR 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);
                        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);
                        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 (;;) ;



}

Edited by bluehash
[ADMIN] Please use code tags<> next time. Thank you!
Link to post
Share on other sites
22 hours ago, LiviuM said:

MISO should be connected with MISO, MOSI with MOSI, not crossed.

MISO means Master In Slave Out, and MOSI means Master Out Slave In. Master & slave are always the same, no crossing is needed.

YESSSSSSS............

It solved my problem yo... Thank you very much...  now i dont get any error in my serial terminal screen..

It shows that it is writing some Hello to t_write.txt file. But nothing is getting written on the SD card. When i remove my SD card from the SD card shield and play it in my PC, the file is empty, both - t_read.txt and t_write.

But still we are much closer for  SD card datalogging...

I also tried this code. which can continuously log the analog read signal to LOG.txt file. In teminal it shows that the  analog read is getting writtten to SD card but when i play to  my PC it is also empty file.. What could be the possible reason??

Link to post
Share on other sites

Hi,

nice to see that making the connection properly bring it to work. :D Carefully reading the replies you receive may even accelerate the success. ;)

For your last problem - writing without writing - maybe you should open the file in the correct mode. In this case the mode should be FA_WRITE.

Try to replace

On 5/25/2017 at 1:49 PM, hemangjoshi37a said:

rc = FatFs.open("T_WRITE.TXT");

with

rc = FatFs.open("T_WRITE.TXT", FA_WRITE);

or

rc = FatFs.open("T_WRITE.TXT", FA_WRITE | FA_READ);

for read & write access.

 

Link to post
Share on other sites
1 minute ago, LiviuM said:

Hi,

nice to see that making the connection properly bring it to work. :D Carefully reading the replies you receive may even accelerate the success. ;)

For your last problem - writing without writing - maybe you should open the file in the correct mode. In this case the mode should be FA_WRITE.

Try to replace

with


rc = FatFs.open("T_WRITE.TXT", FA_WRITE);

or


rc = FatFs.open("T_WRITE.TXT", FA_WRITE | FA_READ);

for read & write access.

 

It gives me error when i compile...:

petitfs:69: error: 'FA_WRITE' was not declared in this scope
petitfs:69: error: 'FA_READ' was not declared in this scope

Link to post
Share on other sites
Just now, LiviuM said:

Yes, I've seen, I've read the wrong documentation. Should work without mode qualifiers.

Yes I see. The file log.txt is read-only file by default. So I think if I change it to read and write accessible from linux, it will work fine.

I am trying to change read-only to read and write accessible  but it is not changing somehow...

Screenshot from 2017-05-26 18-05-02.png

Screencast 2017-05-26 18:08:02.mp4

Link to post
Share on other sites

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...