CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Unable to create text file on sd card

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
elephant2009



Joined: 04 Aug 2014
Posts: 7

View user's profile Send private message

Unable to create text file on sd card
PostPosted: Sat Oct 04, 2014 10:21 am     Reply with quote

Hi friends !
I would like to create a text file (ie "hello.txt") on my 1GB SD memory card, using a dspic33EP MCU.

I have used the corrected drivers by mr andrewg (fat.c and mmcsd.c).
I was able to init the card and i get the following message ("MMC/SDC Found").
However, It seems that the 'fatopen' and 'mk_file' does not create any file.
And Stream value is always equal to zero: "Stream = 0"

Here is my main code:

Code:




#include <33EP512MU810.h>
#device PASS_STRINGS=IN_RAM

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOJTAG                   //JTAG disabled

#device ICSP=1
#use delay(clock=106MHz,crystal=8MHz)


#include <string.h>
#include <flex_lcd_16x2.c>

#use rs232(baud=9600, xmit=PIN_C2, rcv=PIN_C3)
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_G6 //o
#define MMCSD_PIN_SDI     PIN_G7 //i
#define MMCSD_PIN_SDO     PIN_G8 //o
#define MMCSD_PIN_SELECT  PIN_G9 //o

#include "mmcsd2.c"
#include "fat2.c"


void MakeFile(char *fileName)
{
   FILE* stream = 0;
   //char *permission="w";
   
   lcd_gotoxy(1,1);
   printf(lcd_putc, "\r\nMaking file   ");
   delay_ms(1000);
   
   /*
   if(mk_file(fileName) != GOODEC)
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc, "Error        ");
      delay_ms(1000);
      return;
   }
   */
   
   fatopen(fileName, "w", stream);
   fatclose(stream);
   
   lcd_gotoxy(1,1);
   printf(lcd_putc, "Stream = %d  ", stream);
   delay_ms(1000);
}



void main(void)
{
   

   char fileN[]="hello.txt";
   int resp;

   lcd_init();
   delay_ms(1000);
   
   lcd_gotoxy(1,1);
   printf(lcd_putc, "Initializing...");
   delay_ms(1000);

   if (mmcsd_init())
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc, "Could not init ");
      lcd_gotoxy(1,2);
      printf(lcd_putc, "the MMC/SD!!!!");
      while(TRUE);
   }
   else
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc, "MMC/SDC Found  ");
   }

   resp = fat_init();
   MakeFile(fileN);
   
}



And here are the modified drivers:

"FAT2.C":
https://drive.google.com/file/d/0Byt-Yg0sUAM5TFE3SlM1UUg4emc/view?usp=sharing

"MMCSD2.C"
https://drive.google.com/file/d/0Byt-Yg0sUAM5WDlrSTFsUlE5Z0U/view?usp=sharing

Any help would be appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 04, 2014 11:45 am     Reply with quote

Read through this 3-page thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=52738
It goes through a lot of the problems that you may have.
We were successful in read/write of a text file. Try to
do it the same way.
elephant2009



Joined: 04 Aug 2014
Posts: 7

View user's profile Send private message

PostPosted: Sat Oct 04, 2014 1:05 pm     Reply with quote

PCM programmer wrote:
Read through this 3-page thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=52738
It goes through a lot of the problems that you may have.
We were successful in read/write of a text file. Try to
do it the same way.


Thank you mr PCM Programmer.
I will try to do the same way and keep you informed.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sun Oct 05, 2014 3:44 am     Reply with quote

Try actually writing some data to the file. Even a single byte, before closing.

I remember some years ago, having a problem like this on a Linux distribution. It turned out that the directory entry was not actually committed to the disk, until a write occurred.

You are not printing the stream, till after you have closed it. I think the close, sets it to 'null'.
elephant2009



Joined: 04 Aug 2014
Posts: 7

View user's profile Send private message

PostPosted: Mon Oct 06, 2014 1:27 am     Reply with quote

Thank you "Ttelmah" and "PCM Programmer" for your support.

PCM programmer wrote:
Read through this 3-page thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=52738
It goes through a lot of the problems that you may have.
We were successful in read/write of a text file. Try to
do it the same way.


1. 4x 10k Pull-up resistors have been used on data lines.
2. I have reinstalled CCS C Compiler (version 5.025) and used the provided "ex_fat.c" sample code.
3. In fact, when i run for the first time the "make hello.txt" command, the processor freeze and it didn't display
a result code.

Code:

ERROR INITIALIZING FAT
Formatting media (size=1048576): OK
Making file 'hello.txt':


But when I reset and re-started the Ex_fat program, i get the following error message:
Code:

ERROR INITIALIZING FAT
Formatting media (size=1048576): OK
Making file 'hello.txt'
Error creating file



Ttelmah wrote:
Try actually writing some data to the file. Even a single byte, before closing.

I remember some years ago, having a problem like this on a Linux distribution. It turned out that the directory entry was not actually committed to the disk, until a write occurred.

You are not printing the stream, till after you have closed it. I think the close, sets it to 'null'.


I have also pre-formatted my sd card using both fat16 and fat32 filesystems, and added some data before running test program, but again nothing happens.


Unfortunately, i still can not commit any format/write operation to my 1GB SD Card.

Whether the sd card is inserted or not, i always get the following error messages (when i press reset button):
Code:

ERROR INITIALIZING FAT
Formatting media (size=1048576): OK
Making file 'hello.txt'
Error creating file


In fact if the card is plugged in, I get the GOODEC response at a rapid rate. When I un-plug the card, the response occurs more slowly.

I have also tried to use andrewg's and Douglas Kennedy's fixes
http://www.ccsinfo.com/forum/viewtopic.php?t=51163&start=12
http://www.ccsinfo.com/forum/viewtopic.php?t=43417
http://www.ccsinfo.com/forum/viewtopic.php?t=43402

I only want to create a simple text file on the root directory.
I am very confused, and i don't know from where to start...

This is my simplified main code again (based on the "ex_fat.c" sample code):

Code:


#include <33EP512MU810.h>
#device PASS_STRINGS=IN_RAM

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOJTAG                   //JTAG disabled

#device ICSP=1
#use delay(clock=106MHz,crystal=8MHz)


#include <string.h>
#include <flex_lcd_16x2.c>

#use rs232(baud=9600, xmit=PIN_C2, rcv=PIN_C3)
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_G6 //o
#define MMCSD_PIN_SDI     PIN_G7 //i
#define MMCSD_PIN_SDO     PIN_G8 //o
#define MMCSD_PIN_SELECT  PIN_G9 //o


#include <stdlib.h> // for atoi32


#include <mmcsd.c>

//FAT library.
#include <fat.c>

void main(void)
{
   lcd_init();
   delay_ms(1000);
   
   int i;   // pointer to the buffer

   // initialize the FAT
   //  keep in mind that this will automagically initialize the media
   i = fat_init();
   if (i)
      printf(lcd_putc, "ERROR INITIALIZING FAT");
   
   FormatMedia("1048576");
   delay_ms(1000);
   char opt_buffer[255];
   
   strcpy(opt_buffer,"hello.txt");
   MakeFile(opt_buffer);

      }



And Here is the schematic:


Pin Mapping:
----------------
SD --- DSPIC
----------------
SS2 --> G9
SDI2 --> G7
SDO2 --> G8
SCK2 --> G6
VDD --> 3.3V
Could you please provide again the final fixes of mmcsd and fat drivers.
A 10-lines working code (to start with) would be also much appreciated Sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 06, 2014 12:04 pm     Reply with quote

Your code is doing things substantially different than ex_fat.
You're declaring variables in mid-code. Ex_fat declares them at the
top of main(). Ex_fat makes a big deal about the current working
directory. They have this whole section which you have ignored:
Quote:
/
/if option1 starts with a '/', that means the file in the option includes
//the full path to the file. if the file doesn't start with a '/', the
//current working directory must be added.

You should first try to make it work with Ex_fat.

Run through the numbered sequence that I do in this post
http://www.ccsinfo.com/forum/viewtopic.php?t=52738&start=27
and show us the output on the terminal window, just as I do in that post.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group