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

Create a new file on a mmc with hhd driver.c file

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



Joined: 02 Jan 2011
Posts: 1

View user's profile Send private message

Create a new file on a mmc with hhd driver.c file
PostPosted: Sun Jan 02, 2011 12:36 pm     Reply with quote

Hi,

I'm using Proteus v7.6 and ccs to generate a file (txt) on a mmc card with 18f452.
I have some examples and in one of them, it reads the text that is allready storaged on a mmc file called COUNTRY.TXT. Here is it:
Code:

#include "18f452.h"
#fuses H4,NOWDT,NOPROTECT,NOWRT,NOWRTD,NOLVP,NOOSCSEN,BORV27,PUT,STVREN,NODEBUG,NOWRTB
#use delay(clock=40000000,RESTART_WDT)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,ERRORS)

#include "HDD Driver.c"

void main(){

int r1,i,j,error,error0,error1;
int16 rec_no;
int16 index,rec_size;
int32 offset;
char fname[32],buff0[MMC_BUFF_SIZE+1],buff1[MMC_BUFF_SIZE+1];
char c;

setup_adc_ports(NO_ANALOGS);
set_tris_c(0b10010011); //c7=rx I, c6=tx O, c5 SDO O,c4 SDI I

output_high(_CS);


printf("\r\n**** SD / MMC FAT16  Read Demo for Sonsivri **** ");
printf("\r\n");
Delay_ms(1000);
printf("\r\Now Open COUNTRY.TXT File on SD Card  ");
printf("\r\n");
Delay_ms(3000);

SETUP_SPI (SPI_MASTER |  SPI_SS_DISABLED |SPI_H_TO_L| SPI_CLK_DIV_16 | SPI_XMIT_L_TO_H);

buff0[MMC_BUFF_SIZE]=0;
buff1[MMC_BUFF_SIZE]=0;
rec_no=0;

///////// init MMC ////////////////////////////////////////
error=init_MMC(10);
if (error>0) {
goto mmc_exit;
}

printf("\n\r MMC initialized \n\r");
rec_size=MMC_BUFF_SIZE;


//strcpy(fname,"HOME\\HOME.TXT");
strcpy(fname,"COUNTRY.TXT");
rec_size=MMC_BUFF_SIZE;
error0=open_file(0,fname,rec_size);

if (error0>0) {
printf("\n\r fopen as 0 failed error=%U\n\r",error);
goto mmc_exit;
}
else printf("\n\r opened as 0 file %s with rec size %lu \n\r",fname,rec_size);

do {

error0=file_read(0,buff0);

if (error0>0 && error0<255 ) {
printf("\n\r fread 0 failed error=%U\n\r",error0);
break;
}

printf("%s",buff0);


rec_no++;

} while (error0==0);



mmc_exit:
printf("\n\r done winhex adj= %lu \n\r",winhex_adj);

while(true);


}


I have modified it and now I could save some information in that file, but I want to generate a NEW FILE.

There are some functions on the hhd driver.c code but I don't know how to use it. If I use de open_file function to create/open a file that doesn't exist it doesn't works. Here is it:

Code:

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////   OPEN FILE  ///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
#separate
int open_file(int fnbr,char *fname,int16 rec_length)
{
int32 dir_addr_ptr;
int16 bytes_read;
int i,J,ptr1,ptr2,lnf_tiles;
char file_name[12];
int attribute,directory,archive;
char tmp;
char buff[32];  //// buffer for 32 byte tiles

int level;  /// level in the directory structure 0 is top
 ///////  directory is searched and if file is found

 //////
 //////   init_MMC(tries) must be called first
 //////   uses globals root_dir_address

 //start by searching the root directory for folder or file

 /// assign an inital next_cluster_ptr in the root directory
   file[fnbr].next_cluster_ptr=0;
   file[fnbr].this_cluster_ptr=0;

   dir_addr_ptr=root_dir_address;
   file_name[11]=0;
   level=0;
   ptr1=0;
   ptr2=0;
   bytes_read=0; //// byte read so far in this cluster

read_directory:
/// extract the directory levels(folders)
 while ((fname[ptr2]!='/') && (fname[ptr2]!='\\') && (fname[ptr2]!='\0') && (fname[ptr2]!='.') )

       {
        // a dos directory (folder) name can not exceed 8 chars
        if ((ptr2-ptr1)>7) return (20);
        ptr2++;
        }

      #IF MMC_OPEN_TRACE
              printf("\n\r fopen ptr1=%u ptr2=%u ",ptr1,ptr2);
      #ENDIF

      if (ptr2==0){ ptr2=1;ptr1=1;goto read_directory;}    /// skip a leading '/' or '\'
      if ((ptr2-ptr1)==0) return (21);

      // ptr1 is the chars processed so far
      // ptr2 is the position of '/' or '\' or '.' or '\0'
      // prepare the file or directory  name fomat is cccccccceee
      // c is a valid letter or blank eee is extension or blank
      // a directory name is 'cccccccc   ' a file 'cccccccceee' always 11 chars

       for(i=0;i<11;i++)file_name[i]=32;//blank
        file_name[11]=0;
         i=0;
         while(ptr1<ptr2)
              {
                // extract the name

                tmp=fname[ptr1];
                tmp=TOUPPER(tmp);
                file_name[i]=tmp;
                ptr1++;i++;
              }
         if(fname[ptr2]=='.')
              {
                // extract the extension
                i=8;
               while((fname[ptr1]!='\0') && (i<12))
                    {
                     ptr1++;
                     tmp=fname[ptr1];
                     file_name[i]=TOUPPER(tmp);
                     i++;
                    }
              }
      ptr1++;
      ptr2=ptr1; // advance over the '\' or '/' so next pass starts correctly
      if (block_size!=(int32)32)
         {
          if(set_BLOCKLEN((int32)32)==false) return(17); /// tiles are 32 bytes
         }
      if (read_BLOCK(dir_addr_ptr,buff)==false) return(10);

      // decode the FAT16 entries
      // a tile is 32 bytes
      // std dos files take one tile
      // a long file name has multiple tiles
      // starting with the last down to the first and
      // then a std dos tile is found
      // byte 11 is 0x0f for LNF tiles and 0x00 for std
      // we skip the LNF and goto STD tile

tile_decode:
      lnf_tiles=0;
      if (buff[0]==0xE5) goto next_tile; ///0xE5 is the deleted file flag
      if (buff[0]==0x00)
                        {
                        printf("\n\r file err [%s] not found \n\r",file_name);
                         return(11);     /// file not found
                        }
      if (buff[11]==0x0F)
                       {
                        /// get number of LNF tiles
                        lnf_tiles=buff[0] & 0b00111111;
                        bytes_read=bytes_read+lnf_tiles*32;
                        if(bytes_read>cluster_size_bytes)
                                {
                                 // compute next cluster address next_cluster_ptr must be valid
                                 // assigns this_cluster_ptr

                                 dir_addr_ptr=cluster_addr(fnbr,NEXT_CLUSTER);
                                 if (dir_addr_ptr==0xFFFFFF) return (22);
                                 bytes_read=bytes_read-cluster_size_bytes;
                                 dir_addr_ptr=dir_addr_ptr+bytes_read;
                                }
                        else
                                {
                                dir_addr_ptr=dir_addr_ptr+lnf_tiles*32;
                                }

                        //advance over the lnf tiles
                        /// test to see if we need next cluster in chain
                        if (read_BLOCK(dir_addr_ptr,buff)==false) return(31);
                        /// !!! may read into next sector
                       }


      /// check out the standard DOS tile
      #IF MMC_OPEN_TRACE
      printf("\n\r fname[%s] level=%u \n\r",file_name,level);
      for (j=0;j<11;j++)printf("%c",buff[j]);
      #ENDIF

      if(strncmp(buff,file_name, 11)==0) ///8.3 file name ex "FILE    EXT"
                                         ///                 "FOLDER     "
        {
         // we have a  file type or a sub directory(folder)
         // so we get the starting cluster number
         attribute=buff[11];

         file[fnbr].root_cluster_ptr=make16(buff[27],buff[26]);/// assign initial  cluster ptr
                                                     /// if it is not a directory
                                                     /// it points to the begining of the file
                                                     /// cluster chain



         if ((attribute & 0b00010000)>0)directory=true;
         else directory=false;
         if ((attribute & 0b00100000)>0 || attribute==0)
                       {
                        archive=true;   //// we have our file

                        file[fnbr].size=make32(buff[31],buff[30],buff[29],buff[28]);
                        file[fnbr].dir_addr_ptr=dir_addr_ptr; ///save address of this files tile
                        /// assign global value
                       }
         else archive=false;



           goto match_found;
      //   goto fill_table; // we have a match
        }
next_tile:

      bytes_read=bytes_read+32;
      if(bytes_read > cluster_size_bytes)
                                {
                                  /// requires a valid next=next_cluster_ptr
                                 // compute next cluster address and assign this cluster
                                 dir_addr_ptr=cluster_addr(fnbr,NEXT_CLUSTER);
                                 if (dir_addr_ptr==0xFFFFFF) return (23);
                                 bytes_read=bytes_read-cluster_size_bytes;
                                 dir_addr_ptr=dir_addr_ptr+bytes_read;
                                }
                        else
                                {
                                dir_addr_ptr=dir_addr_ptr+32;
                                }


      dir_addr_ptr=dir_addr_ptr+32;

      if (read_BLOCK(dir_addr_ptr,buff)==false) return(32);
      goto tile_decode;

match_found:
      ///// if we have a sub directory we need to cycle down a level
      if (directory==true) {
                            // compute the sub directory address
                           // compute this cluster address this_cluster_ptr must be valid
                    dir_addr_ptr=cluster_addr(fnbr,ROOT_CLUSTER); /// set physical addr of starting cluster
                    #IF MMC_OPEN_TRACE
                    printf("\n\r next_cluster_ptr=%lu \n\r ",file[fnbr].next_cluster_ptr);
                    #ENDIF
                   //printf("\n\r dir_addr_ptr=%lu",dir_addr_ptr);
                   // dir_addr_ptr=((int32)cluster_table[0]-(int32)2)*(int32)cluster_size_bytes+
                   // data_area_address;
                            level++;
                            goto read_directory;
                           }


      // note record length must divide into 512 to align properly
      if (rec_length<2) return(12);



      /// get the initial file_addr_ptr

       file[fnbr].addr_ptr=cluster_addr(fnbr,ROOT_CLUSTER);
       file[fnbr].offset=0; //init  bytes read from beginning of open file
       file[fnbr].cluster_offset=0; //init bytes read to beginning of the current cluster
       file[fnbr].rec_size=(int32)rec_length; /// assign file record size
      #IF MMC_OPEN_TRACE
      printf("root_cluster=%lu \n\r",file[fnbr].root_cluster_ptr);
       #ENDIF

       //printf("\n\r fopen %u rec size=%lu",fnbr,file[fnbr].rec_size);

       if(set_BLOCKLEN(file[fnbr].rec_size)==false) return(13);

return(0);
}



Can someone help me?

Thank you!
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 3:23 pm     Reply with quote

Read the comments.
It says it looks through the directory to find the file. Nothing to do with creating it....
If the library you are using, is like the standard fat library supplied by CCS, there will be a separate function (the CCS one, is called 'mk_file'), to create a new file...

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