calinp

[Energia Library] Petit FatFS SD Card Library

136 posts in this topic

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

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