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 support@ccsinfo.com

FAT32 on SD Card
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

FAT32 on SD Card
PostPosted: Wed Jan 30, 2008 12:31 pm     Reply with quote

Hi people,

Im working on a project for a datalogger, that is using a flash memory to store data.

I already get a mmc card working very well using a code of fat32 based on Tommy´s code. But doesnt work when I use with SD card.

I´ve tested the routines (write_sector and read_sector) separately and it works fine.. So, I can write and read blocks from SD card using those functions.. But when I try fat32.. everything looks fine (initializes SD and FAT32), then i put it on PC and there is no files.

anyone has a clue ?


i didnt post the code because is the same of Tommy. with the bugs fixeds.

thanks.
_________________
Andre
DDDDaniel
Guest







PostPosted: Wed Jan 30, 2008 2:55 pm     Reply with quote

Are you sure the SD-Card is formatted with the FAT32 file system? Because every (micro)SD-Card I recently bought came formatted with FAT16. What's the capacity of your card?
For the File System itself it should not make a difference if you are using it on a MMC or SD - card...
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Jan 30, 2008 3:10 pm     Reply with quote

Daniel,

I found the problem and already fixed it.

it was in fat32 initialization.


bye the way, do you know some code to read all data from CID And CSD register ? im trying but im getting some strange data.

thanks for the replay.
_________________
Andre
DDDDaniel
Guest







PostPosted: Wed Jan 30, 2008 3:28 pm     Reply with quote

hope this helps:

Code:

int mmc_read_csd (int *Buffer)
{   
 int *buf;
   //Command sequence to read the CSD
   int cmd[] = {0x49,0x00,0x00,0x00,0x00,0xFF};
   
   //read 16 Bytes
   mmc_read_block(cmd,Buffer,16);
   buf = Buffer;

   printf("\r\nCSD_STRUCTURE: %X", (buf[0] & 0x0C) >> 2);
   printf("\r\nTAAC: %X", buf[1]);
   printf("\r\nNSAC: %X", buf[2]);
   printf("\r\nTRAN_SPEED: %X", buf[3]);
   printf("\r\nCCC: %lX", (make16(buf[4], buf[5]) & 0xFFF0) >> 4);
   printf("\r\nREAD_BL_LEN: %X", buf[5] & 0x0F);   
   printf("\r\nREAD_BL_PARTIAL: %X", (buf[6] & 0x80) >> 7);
   printf("\r\nWRITE_BLK_MISALIGN: %X", (buf[6] & 0x40) >> 6);
   printf("\r\nREAD_BLK_MISALIGN: %X", (buf[6] & 0x20) >> 5);
   printf("\r\nDSR_IMP: %X", (buf[6] & 0x10) >> 4);
   printf("\r\nC_SIZE: %lX", (((buf[6] & 0x03) << 10) | (buf[7] <<2>> 6)));
   printf("\r\nVDD_R_CURR_MIN: %X", (buf[8] & 0x38) >> 3);
   printf("\r\nVDD_R_CURR_MAX: %X", buf[8] & 0x07);
   printf("\r\nVDD_W_CURR_MIN: %X", (buf[9] & 0xE0) >> 5);
   printf("\r\nVDD_W_CURR_MAX: %X", (buf[9] & 0x1C) >> 2);
   printf("\r\nC_SIZE_MULT: %X", ((buf[9] & 0x03) <<1>> 7));
   printf("\r\nERASE_BLK_EN: %X", (buf[10] & 0x40) >> 6);
   printf("\r\nSECTOR_SIZE: %X", ((buf[10] & 0x3F) <<1>> 7));
   printf("\r\nWP_GRP_SIZE: %X", buf[11] & 0x7F);
   printf("\r\nWP_GRP_ENABLE: %X", (buf[12] & 0x80) >> 7);
   printf("\r\nR2W_FACTOR: %X", (buf[12] & 0x1C) >> 2);
   printf("\r\nWRITE_BL_LEN: %X", ((buf[12] & 0x03) <<2>> 6));
   printf("\r\nWRITE_BL_PARTIAL: %X", (buf[13] & 0x20) >> 5);
   printf("\r\nFILE_FORMAT_GRP: %X", (buf[14] & 0x80) >> 7);
   printf("\r\nCOPY: %X", (buf[14] & 0x40) >> 6);
   printf("\r\nPERM_WRITE_PROTECT: %X", (buf[14] & 0x20) >> 5);
   printf("\r\nTMP_WRITE_PROTECT: %X", (buf[14] & 0x10) >> 4);
   printf("\r\nFILE_FORMAT: %X", (buf[14] & 0x0C) >> 2);
   printf("\r\nCRC: %X", buf[15]);

   return(0);
}


...but I don't have code to read the CID...

good luck!
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Jan 30, 2008 5:21 pm     Reply with quote

Daniel,

very nice dude.. it will help me a lot.

Im writing the code based on mmc manual, but is a bit confuse to get these informations....

you helped me a lot.

thanks..
_________________
Andre
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jan 30, 2008 8:11 pm     Reply with quote

You are aware of the other FAT32 implementations for the PIC?
Of course you are free to write your own library but even then looking at the existing libraries may help solve some of your questions.

There are:
- FAT.c in the Driver directory of the v4 CCS compiler.
- At least two libraries in the code section of this forum.
- FatFs - I never used it but looks promising. Might need porting to CCS C-style.
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Jan 30, 2008 8:35 pm     Reply with quote

ckielstra, Im gonna check it...

thanks..
_________________
Andre
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

CID and CSD
PostPosted: Fri Feb 01, 2008 12:06 am     Reply with quote

Hi there, regarding you question about printing the CSD and CID. checkout the MMCSD.c driver in the CCS directory it contains the functions to print all the data. Used it before and works all right, but do you have the code to decode what they mean? eg. it will give you manufacturer's code, voltage code etc. but no "human readable" value/name. I must admit i haven't studied the SD card spec to see if they have explanation on the codes but maybe you have more info on this?

Kind Regards
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Feb 01, 2008 4:38 am     Reply with quote

Crystal_lattice

I have a pdf that explaing what each field in protocol means, but im not getting it to work. if you want this document, let your email, then im gonna send it to you.

Im reading the CID register, in the name of product it shows (MMC , MMCM and SD01G, these is the 3 cards that im testing), but the other fields is confused, and Im using the correct conversion of data types (decimal, hexa, binary and BCD codes).

the code that im using to read cid is:

Code:

       char Buf[17];
       char i;


   // wait for the start token (0xfe)
   while (RecebeMMC () != START_BYTE);

   // 16 bytes long
   for (i = 0; i < 16; i++)
   {
      Buf[i] = RecebeMMC ();
      printf (" %x", Buf[i]);
   }
   printf ("\r\n\r\n");

   printf("\r\n\r\nManufacture ID: 0x%x", Buf[0]);
   printf("\r\nAplication ID: 0x%x", make16 (Buf[1], Buf[2]));
   printf("\r\nProduct Name: %c%c%c%c%c%c", Buf[3], Buf[4], Buf[5], Buf[6], Buf[7], Buf[8]);
   printf("\r\nProduct Revision: %u.%u", (Buf[9] >> 4) & 0x0f, Buf[9] & 0x0f);
   printf("\r\nSerial Number: %lu", make16 (buf[9], Buf[10]) + make16 (Buf[11], Buf[12]));
   printf("\r\nManufacture Data Code: %u/%u", (Buf[13] >> 4) & 0x0f, Buf[13] & 0x0f);
   printf("\r\nCRC: 0x%x\r\n\r\n", (buf[14] >> 1) & 0xef);


and the answer for a MMCM kingston 2GB at RS232 is:

Code:

      70 00 00 4d 4d 43 4d 20 20 10 00 10 14 fd 6a bf



     Manufacture ID: 0x70
     Aplication ID: 0x00
     Product Name: MMCM 
     Product Revision: 1.0
     Serial Number: 8212
    Manufacture Data Code: 15/13
    CRC: 0x25



regards.
_________________
Andre
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 6:39 am     Reply with quote

Code:
printf("\r\nAplication ID: 0x%x", make16 (Buf[1], Buf[2]));
In order to print a 16 bit long int you have to use '%Lx' instead of '%x'.

For the serial number you can not create an int32 by adding two int16 values. Use make32 instead. Second problem here is that you are starting with buf[9] instead of buf[10] and this offset by 1 error continues on the next lines.
Code:
   printf("\r\nSerial Number: %lu", make32 (buf[10], Buf[11], Buf[12], Buf[13]));
   printf("\r\nManufacture Data Code: %u/%u", (Buf[14] >> 4) & 0x0f, Buf[14] & 0x0f);
   printf("\r\nCRC: 0x%x\r\n\r\n", (buf[15] >> 1) & 0xef);
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Feb 01, 2008 7:30 am     Reply with quote

ckielstra,

alright... im gonna check it

thanks
_________________
Andre
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Feb 01, 2008 7:42 am     Reply with quote

ckielstra,

I´ve made the changes and still somthing wrong,

for MMCM kingston - serial wasnt displayed (after using make32)
the manufacture data seems to be ok, month 6, year 10 (offset of 1997), so de data is 06/2007

Code:



 70 00 00 4d 4d 43 4d 20 20 10 00 10 14 fd 6a bf


Manufacture ID: 0x70
Aplication ID: 0x0000
Product Name: MMCM 
Product Revision: 1.0
e¾òFe¾òFe¾òFe¾òF1053949
Manufacture Data Code: 6/10
CRC: 0x4f



with SD card ADATA with 1gb of storage, i´ve got:

the same problem as MMCM kingston
and the manufacture data is not correct too.
this value correspond for a 07/2008 (impossible)..


Code:


 27 50 48 53 44 30 31 47 20 b0 00 5e 3e 00 7b af



Manufacture ID: 0x27
Aplication ID: 0x5048
Product Name: SD01G
Product Revision: 11.0
e¾òFe¾òFe¾òFe¾òF6176256
Manufacture Data Code: 7/11
CRC: 0x47




the new code that im using is:

Code:


   printf("\r\n\r\nManufacture ID: 0x%x", Buf[0]);
   printf("\r\nAplication ID: 0x%lx", make16 (Buf[1], Buf[2]));
   printf("\r\nProduct Name: %c%c%c%c%c%c", Buf[3], Buf[4], Buf[5], Buf[6], Buf[7], Buf[8]);
   printf("\r\nProduct Revision: %u.%u", (Buf[9] >> 4) & 0x0f, Buf[9] & 0x0f);
   printf("\r\nSerial Number: %lu", make32 (Buf[10], Buf[11], Buf[12], Buf[13]));
   printf("\r\nManufacture Data Code: %u/%u", (Buf[14] >> 4) & 0x0f, Buf[14] & 0x0f);
   printf("\r\nCRC: 0x%x\r\n\r\n", (buf[15] >> 1) & 0xef);


is there another error that im not seeing ??

thanks for the help.

regards.
_________________
Andre
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 7:56 am     Reply with quote

The garbled text has nothing to do with your interpretation of the CID data. This is the RS232 going haywire, possibly caused by an interrupt messing with the timings. Are you using the hardware UART or a software emulated UART?

Code:
00 10 14 fd
equals the printed value 1053949, so your make32 is correct.

Quote:
and the manufacture data is not correct too.
this value correspond for a 07/2008 (impossible)..
The value 7B in the Kingston seems incorrect, either you are reading the wrong value or Kingston is not following the specs.
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Feb 01, 2008 10:25 am     Reply with quote

im using hardware UART and SPI... everything is alright but this register (and CSD too)...

but im gonna check all possiblities... probably kingston is not fallowing the specs... who knows ?? hehehe..

but im gonna work a little more.. if i get something, it will be posted.


thanks.


best wishes;
_________________
Andre
PuffDally



Joined: 09 Sep 2008
Posts: 1

View user's profile Send private message

FAT32 on SD card
PostPosted: Tue Sep 09, 2008 7:55 am     Reply with quote

Hi Andreluizeng

Any chance you could post the code that you got to work with an SD card, I have been unsuccessful in getting this to work with a PIC18F66J15.

I keep gettin a 'time out' error when trying to initalize the SD card. Do you have any error checking I can do to verify the SPI comms to the SD card is working correctly?

And lastly do you know what the maximum size SD card I can use is? The PIC I am using has 4kB of flash.

Thanks alot!
_________________
I want something good to die for, to make it beautiful to live
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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