Jump to content
Sign in to follow this  
hemangjoshi37a

SD card not writing any data from microcontroller.

Recommended Posts

Hi frineds,

Issue :

I am data logging into my 1 gb sd card using given code.

In my serial terminal window it shows that the data is being written in the sd card, but when i take it out  from microcontroller interface and put it in the computer it shows nothing buy empty file (log.txt). Well i know one thing that can be causing this issue that is the log.txt file shows read only in my linux mint environment but still i can write anything in that file. I have attached a screen recording and screen shot file of the issue down below. Please help me...

Thank you in advance...

Code : 

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

/*----------------------------------------------------------------------*/
/* Petit FatFs sample project for generic uC  (C)ChaN, 2010             */
/*----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
   Ported to Energia by calinp
   Copy the file LOG.txt from this file's location to the root of the 
   SD-Card.

   12/21/2013
   Modified by bluehash(43oh) to log temperature data from the MSP430G2553
   internal temperature sensor and log it to a file on the SD-Card
   References:
  http://forum.43oh.com/topic/3209-energia-library-petit-fatfs-sd-card-library/
 */

 
#include "SPI.h" 
#include "pfatfs.h"

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

unsigned short 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()
{
   pinMode(PUSH2, INPUT_PULLUP);
   Serial.begin(9600);                // initialize the serial terminal
   analogReference(INTERNAL1V5);
   analogRead(TEMPSENSOR);           // first reading usually wrong
   FatFs.begin(cs_pin);              // initialize FatFS library calls
   Serial.print("\n\n\n MSP430 Temperature Logger \n\r"); 
   Serial.println("Press S2 button to start...");
   while(digitalRead(PUSH2)==1){}
   delay(100);
   while(digitalRead(PUSH2)==0){}
}
         
/* 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 0
   Serial.println();
   Serial.println("Press button to start...");
   while(digitalRead(PUSH2)==1){}
   delay(100);
   while(digitalRead(PUSH2)==0){}
   #endif

   ui32_ReadTemp = ((uint32_t)analogRead(TEMPSENSOR)*27069 - 18169625) *10 >> 16;

   #if DEBUG
   Serial.println();
   Serial.println("Opening log file to write temperature(LOG.txt).");
   delay(100);
   #endif
   
   rc = FatFs.open("LOG.TXT");
   if (rc) die(rc);

   delay(100);
   bw=0;
   ui32_ReadTemp = ((uint32_t)analogRead(TEMPSENSOR)*27069 - 18169625) *10 >> 16;
   sprintf( buf, "%lu Current temperature is %lu.%lu\r\n", counter, ui32_ReadTemp/10, ui32_ReadTemp%10 );
   counter++;
   StringLength =  strlen(buf);
   Serial.println(buf);        

   #if DEBUG
   Serial.print(StringLength, DEC);
   Serial.println();
   Serial.print(AccStringLength, DEC);
   #endif

   rc = FatFs.lseek(  AccStringLength );
   if (rc) die(rc);
   AccStringLength =  AccStringLength + 512;
   rc = FatFs.write(buf, 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);

       
   #if READ         
   delay(100);
   Serial.println();
   Serial.println("Read Temp data from the log file (LOG.txt).");
   delay(100);
   rc = FatFs.open("LOG.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 */
   for (uint16_t i = 0; i < br; i++)    /* Type the data */
   Serial.print(buffer);
   delay(100);
   }
   if (rc) die(rc);
   #endif

   // Log delay
   delay(LOG_DELAY);
}

 

Screenshot 2017-05-27 15:50:27.png

Screencast_2017-05-27_15:45:33.mp4

Share this post


Link to post
Share on other sites

Did you read the limitations of the Petit FS?

...
Petit FatFs Limitations:

Petitfs specifically uses as little flash and stack as possible. This, however, comes at the expense of some functionality. Files are not able be created or increased in size, and only one file can be accessed at a time
...

So if you create a zero length file (open a file in a text editor and save it without adding anything to it), it isn't going to do what you want.  If you search the forum or google this is expressed in thousands of posts.

The comment above comes from this document: http://www.atmel.com/Images/Atmel-42776-Using-the-Petit-FAT-File-System-Module-with-AVR_ApplicationNote_AVR42776.pdf

 

http://elm-chan.org/fsw/ff/pf/write.html ... 

Description

The write function has some restrictions listed below:

  • 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 only.
  • Read-only attribute of the file cannot block write operation.

File write operation must be done in following sequence.

  1. pf_lseek(ofs); read/write pointer must be moved to sector bundary prior to initiate the write operation, or it will be rounded-down to the sector boundary at first write operation.
  2. pf_write(buff, btw, &bw); Initiate write operation. Write first data to the file.
  3. pf_write(buff, btw, &bw); Write next data. Any other file function cannot be used while a write operation is in progress.
  4. pf_write(0, 0, &bw); Finalize the current write operation. If read/write pointer is not on the sector boundary, left bytes in the sector will be filled with zero.

The read/write pointer in the file system object advances in number of bytes written. After the function succeeded, *bw should be checked to detect end of file. In case of *bw is less than btw, it means the read/write pointer reached end of file during the write operation. Once a write operation is initiated, it must be finalized properly, or the written data can be lost.

 

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.

Sign in to follow this  

×
×
  • Create New...