Jump to content
calinp

[Energia Library] Petit FatFS SD Card Library

Recommended Posts

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

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

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

How can I delete the previous data... I'm doing data logger. while logging new value previous values are coming. anyone have the solution for it. everytime when im writing a new values old entries also coming someone give solution for it

 

Thanks

Share this post


Link to post
Share on other sites

Petit FatFS SD Card Library works well enough to read and copy the files. Kudos:biggrin:

I want to know a possible way to 'create and delete a file' in code run-time. I am expecting to use FIFO method to log data and send it sequentially later over a channel. So one solution i know is to create a temporary file, add the data from original file to temporary file except the one to be deleted and then delete the old file. 

Trying to create a file in run-time gives an error rc=3. 

Share this post


Link to post
Share on other sites

Hi ,
I testing this library with the MSP430FR4133 LP and my own code.. the Read and Write function is working fine, But the Thing is while writing to a file only Five characters is getting written to the file i.e if I write KAMALESH only KAMAL is written to the file. Can you Help me with this issue..

Source Code


#include <SPI.h>
#include <pfatfs.h>
#include <pffconf.h>

#define cs_pin      8             // chip select pin 
#define read_buffer 128             // size (in bytes) of read buffer 
#define LOG_DELAY   5000          // 5000ms -> 5sec

unsigned long int bw, br;//, i;
char buffer[read_buffer];
int rc;
DIR dir;        /* Directory object */
FILINFO fno;      /* File information object */

uint32_t ui32_ReadTemp = 0;
uint8_t StringLength = 0;
char buf[30];
uint32_t counter = 0;
uint32_t AccStringLength = 0;

void setup()
{
   Serial.begin(9600);                // initialize the serial terminal
   analogReference(INTERNAL1V5);
   FatFs.begin(cs_pin);              // initialize FatFS library calls
   Serial.print("\n\n\nMSP430 Logger \n\r"); 
   Serial.println("Type the String");
}
         
/* Stop with dying message */         
void die ( int pff_err  )
{
   Serial.println();
   Serial.print("Failed with rc=");
   Serial.print(pff_err,DEC);
   for (;;) ;
}

void printDec(uint32_t ui) 
{
   Serial.print(ui/10, DEC);
   Serial.print(".");
   Serial.print(ui%10, DEC);
}

/*-----------------------------------------------------------------------*/
/* Program Main                                                          */
/*-----------------------------------------------------------------------*/
void loop()
{
if(Serial.available())
{
  String src = Serial.readString();

  if(src.indexOf("R") != -1)
  {
      rc = FatFs.open("datalog.txt");
      if (rc) die(rc);
      delay(100);
      for (;;) {
      rc = FatFs.read(buffer, sizeof(buffer), &br);  /* Read a chunk of file */
     if (rc || !br) break;      /* Error or end of file */
     Serial.println(buffer);
       rc = FatFs.close();  //Close file
     if (rc) die(rc);
     }
  }
  else
  {
    rc = FatFs.open("datalog.txt");
    if (rc) die(rc);
   delay(100);
  char* msg = string2char(src);
      StringLength =  strlen(msg);
      Serial.println(msg);  
Serial.print("Size:");
Serial.println(StringLength);
      rc = FatFs.write(msg, StringLength,&bw);
      if (rc) die(rc);
      // rc = FatFs.write(0, 0, &bw);  //Finalize write
      // if (rc) die(rc);
       rc = FatFs.close();  //Close file
            if (rc) die(rc);
       Serial.println("ADDED");
    }
  }
}

char* string2char(String command)

  if(command.length()!=0)
  { 
    char *p = const_cast<char*>(command.c_str()); 
    return p;     
  }
}


 

Share this post


Link to post
Share on other sites

I used this library for an MSP432 but i got this error

In file included from C:\Users\Killer\Desktop\SDCardTutorialWithEnergia\SDCardTutorialWithEnergia.ino:1:0:

C:\Users\Killer\AppData\Local\Energia15\packages\energia\hardware\msp432r\5.23.1\libraries\SDCardTutorialWithEnergia/pfatfs.h:18:24: fatal error: ../SPI/SPI.h: No such file or directory

 #include "../SPI/SPI.h"

                        ^

compilation terminated.

exit status 1
Erreur de compilation pour la carte RED LaunchPad w/ msp432 EMT (48MHz)

 

Share this post


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