calinp

[Energia Library] Petit FatFS SD Card Library

145 posts in this topic

Posted (edited)

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!

Share this post


Link to post
Share on other sites

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.

bluehash and hemangjoshi37a like this

Share this post


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

Share this post


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.

 

hemangjoshi37a likes this

Share this post


Link to post
Share on other sites

LE I've read the examples coming with PetitFS and they aren't using the mode specifier neither.

No idea why it doesn't work for you.

hemangjoshi37a likes this

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

I also tried with root user but i cant change the file permission from read-only to read and write.

But anyway thax for solving my problem. I LiviuM you have not solved my problem i would have always believed that we cant do data logging in SD card.

Thax Yo.....

Share this post


Link to post
Share on other sites

for datalogging in SD card which format type of SD card is allowed?? NTFS, FAT32 or FAT16 or any else??

Share this post


Link to post
Share on other sites

My code for writing "hello world" to the sd card is running successfully but the SD card is not showing anything inside the written file.

Please somebody hepl me.

Thank you.

Regards,

Hemang Joshi.

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[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 (;;) ;
}

OUTPUT on serial termianl :

Quote


Press button to start...

Open a test file (t_read.txt).

Type the file content.

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.

Open root directory.

Directory listing...
0    T_READ.TXT
0    T_WRITE.TXT

Test completed.

 

 

Share this post


Link to post
Share on other sites
On 2017-5-26 at 7:27 PM, LiviuM said:

Have you read the first post in the topic?

Is your card maybe hard locked? May you in Linux/Windows delete the file/create a new file on the card?

Actually I can successfully do data logging with same SD card and same SD card shield from Arduino, so I dont think the card is locked.

Share this post


Link to post
Share on other sites
1 hour ago, hemangjoshi37a said:

Actually I can successfully do data logging with same SD card and same SD card shield from Arduino, so I dont think the card is locked.

I found very weird solution for this problem. If I have some number of characters pre-written in the t_write.txt file than Hello world will get written on that file.

Is it not possible to write something in empty file?? 

Share this post


Link to post
Share on other sites
On 2013-3-25 at 0:45 AM, sirri said:

i will be very happy if someone helps. i want to do "datalogging" ^_^

Have you found anyway to perform datalogging bro???

Share this post


Link to post
Share on other sites

I am using micro SD card to log reading from four force sensors connected to the microcontroller (MSP430G2553). Controller logs reading to the SD card every 15 minutes and goes to sleep. I am using 1000 mAh battery, which is lasting over a week. Is it possible to reduce the power consumption as to make it last over a month.

(#) Even if the microcontroller is sleeping for 15 minute, SD card is continuously consuming power.

(#) It takes couple of mA to write.

  • Help with any suggestion to reduce to power consumption drastically. Schematic and code (attached).

Datalogger_Schematic.JPG

 

Code_Datalogger_msp430g2553.txt

agaelema likes this

Share this post


Link to post
Share on other sites
On 20/06/2017 at 10:39 PM, tapasxplore said:

Help with any suggestion to reduce to power consumption drastically. Schematic and code (attached).

why not try using a GPIO pin to switch the power on/off to the SD card when you aren't accessing it?

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