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 code for MultiMedia Cards
Goto page Previous  1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Mon Apr 30, 2007 7:22 am     Reply with quote

Thanks for the driver! I haven't tested it yet.. But I have a few questions:
1. Is it possible to get a list of all the files on the card? well, maby not the whole list at once but a few files to show on a lcd and then scroll?

2. How large cards can it handle? is it 2^32 bytes? 4Gb?

Thanks...
Tomi



Joined: 11 Aug 2005
Posts: 10

View user's profile Send private message

PostPosted: Fri May 04, 2007 12:46 am     Reply with quote

The answers are simple:
1. Take a look on FindDirEntry() function. This function steps over the entire directory structure to find a file. With minimal modification you can get a directory list (e.g. replace the strcmp() call to a printf() )
2. Theoretically the maximum card size is:
(0x0FFFFFFF sectors) * 512 Bytes/sector = 128 GB (This is the reason why I wrote FAT32 instead of FAT16; the PIC has not enough RAM space to use other than 512Bytes/cluster so the limit is 32MB for FAT16)
miky



Joined: 06 May 2007
Posts: 10
Location: Prague

View user's profile Send private message

PostPosted: Sun May 06, 2007 12:14 pm     Reply with quote

Hi Tomi,
I am trying to put your code into 18f452 - is there any chance to change
#locate FATTable = 0x0800
#locate gFiles = 0x0A00
#locate gFAT32Vars = 0x0E70
#locate DiskInfo = 0x0E90

so it fits into 18f452 (one open file is enough).

Thanks,
miky
Tomi



Joined: 11 Aug 2005
Posts: 10

View user's profile Send private message

PostPosted: Mon May 07, 2007 1:22 am     Reply with quote

Hi Miky,
Of course you can relocate the variables. When I started with this code the 18F6720 had some errata especially handling RAM space in the 0x100-0x200 range. In a case of "error-free" PIC you don't need to use #locate directives.
miky



Joined: 06 May 2007
Posts: 10
Location: Prague

View user's profile Send private message

PostPosted: Mon May 07, 2007 9:25 am     Reply with quote

Tomi,
the 18f452 ram ends 5FFh. When going from top to bottom (the difference is 900h):
#locate FATTable = 0x0800 -> -100 ??
#locate gFiles = 0x0A00 -> 100h ??
#locate gFAT32Vars = 0x0E70 -> 570h
#locate DiskInfo = 0x0E90 -> 590h

When compiling without #locate than I get an error (not enough ram memory).
So my Q to you - how to arrange the ram space (e.g. for MAXFILES =1)
in order to fit into 5FFh area (as your code is complex)??
Thanks,
Miky
Tomi



Joined: 11 Aug 2005
Posts: 10

View user's profile Send private message

PostPosted: Tue May 08, 2007 1:21 am     Reply with quote

Hi Miky,
Maybe you have to try 18F4515 (3.9kB RAM) instead of 18F452 (I think 452 has not enough RAM space). I think this chip is pin-compatible with 452.
miky



Joined: 06 May 2007
Posts: 10
Location: Prague

View user's profile Send private message

PostPosted: Tue May 08, 2007 4:42 am     Reply with quote

Hi Tomi,
I managed to get compiled the code on 452 (83%mem) by cutting off the #locate. However, the init of the card is now the issue (at least for me..)
I am using SD card 512MB. The init command (after the cmd0, instead of cmd1 you have to apply both) shall be:
result = mmc_send_cmd(MMC_CMD_SEND_OP_APPLSPEC,0,1);
result = mmc_send_cmd(MMC_CMD_SEND_OP_COND41,0,1);
as an example from mmc_spi (it works) or:
c=mmc_cmd(0x77,0x00000000,128,0x00,0x99);
//if (c==0x00) { goto ready;}
c=mmc_cmd(0x69,0x00000000,128,0x00,0x99);
if (c==0x00) { goto ready;}
as an example from mmcdriver.c (also works fine). When modifying your init code to:

char MMCInit()
{
char response,iii,errcnt;

//restart_wdt();
output_high(ChipSel);
output_high(ChipClk);
output_high(ChipDin);
//bit_clear(SSPCON1,5);
//SSPCON1 = 0x32; // modify this number to change SPI clock rate
//SSPSTAT = 0;
iii = 10;
errcnt = 100;
do {
MMCOut(0xFF);
} while (--iii);
delay_us(20000);
output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x40);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x95);
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
output_high(ChipClk);
do {
delay_us(2000);

output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x77);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x01); //01
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
output_high(ChipClk);
//MMC8Clock(); //

//output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x69);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x01); //01
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
output_high(ChipClk);


MMC8Clock();
errcnt--;
} while (response && errcnt);
return errcnt;
}

it does not work (eer=0). I am initialising the SPI by:
SETUP_SPI(SPI_MASTER | SPI_CLK_DIV_64 | SPI_H_TO_L |SPI_XMIT_L_TO_H );

BTW why you toggle the (ChipClk)? Nobody does it in their codes.
Miky
[/code]
miky



Joined: 06 May 2007
Posts: 10
Location: Prague

View user's profile Send private message

PostPosted: Tue May 08, 2007 4:52 am     Reply with quote

Tomi,
so this is what I would say shall work (but does not):

char MMCInit()
{
char response,iii,errcnt;

//restart_wdt();
output_high(ChipSel);
//output_high(ChipClk);
//output_high(ChipDin);
//bit_clear(SSPCON1,5);
//SSPCON1 = 0x32; // modify this number to change SPI clock rate
//SSPSTAT = 0;
iii = 10;
errcnt = 100;
do {
MMCOut(0xFF);
} while (--iii);
delay_us(20000);
//output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x40);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x95);
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
//output_high(ChipClk);
do {
delay_us(2000);

//output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x77);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x01); //01, ff, 95 ??
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
//output_high(ChipClk);
//MMC8Clock(); //

//output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x69);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x01); //01, ff, 95 ??
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
//output_high(ChipClk);


// ??? MMC8Clock();
errcnt--;
} while (response && errcnt);
return errcnt;
}
miky



Joined: 06 May 2007
Posts: 10
Location: Prague

View user's profile Send private message

PostPosted: Sat May 12, 2007 1:57 pm     Reply with quote

Tomi,
I've fixed the SD init, so no it initialise the SD. However, I see some strange thing in this example(using 18f452, Maxfiles = 1, ram 83% full):


printf("\n\r Start MMC init \n\r");
v = 1;
if (!input(CardInserted)) { // if MMC card is in place
v = MMCInit(); // init the card
delay_ms(50);
crd = input(CardInserted);
printf("\n\rerr =%d Card inserted = %d \n\r",v,crd);
}
if (v != 0) { printf("\n\rerrInit\n\r");goto ERR_EX;}

printf("\n\r Start FAT init \n\r");
InitFAT(); // init the file system

printf("\n\r Start EVENT.LOG \n\r");

strcpy(gfilename,"EVENTS1.LOG");
f = fopen(gfilename,'a'); // open EVENTS1.LOG for append
if (f < MAXFILES) {
sprintf(msg,"%04lu.%02u.%02u. %02u:%02u:%02u ",myrec.tm_year,myrec.tm_mon,myrec.tm_mday,myrec.tm_hour,myrec.tm_min,myrec.tm_sec);
fputstring(msg,f);
strcpy(msg,"System started XXYY001\r\n");
fputstring(msg,f);
fclose(f);
}
printf("\n\r END EVENT.LOG \n\r"); delay_ms(500);
ERR_EX:
printf("\n\r END \n\r");
}

It creates EVENT1.LOG file, but the text in it (I can see it when I open it with Notepad) is coming from other sector (winhex F9000), while the by fputstring written text is at F82000. The card is 512MB, fresh formatted.
What could be wrong?
Miky
miky



Joined: 06 May 2007
Posts: 10
Location: Prague

View user's profile Send private message

PostPosted: Sat May 12, 2007 2:00 pm     Reply with quote

.. fputstring written text is at [b]F8200[/b]..
Andreas



Joined: 25 Oct 2004
Posts: 136

View user's profile Send private message

PostPosted: Mon May 21, 2007 12:16 am     Reply with quote

Hi miky !

Could You show us what You have done to get the SD Card initialized ??

best regards
Andreas
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

Software SPI?
PostPosted: Wed Jul 11, 2007 2:54 am     Reply with quote

Today I'm going to begin working with this code and the mmc card.
And I got some questions:
1. Is it possible to use software spi instead? (I guess it is, why would it not be?)
2. If so, All I need to change is these three functions
Code:

void MMCOut(char indata)
void MMC8Clock()
char MMCIn()

And these lines in MMCInit..
Code:

   bit_clear(SSPCON1,5);
   SSPCON1 = 0x30; // modify this number to change SPI clock rate
   SSPSTAT = 0;

And This in ReadSector
Code:

   do {
      SSPBUF=0xFF;
      while (!BF);
      *0xFEE = SSPBUF;
   } while (--cnt2);

And this in WriteSector
Code:

   do {/*
      SSPBUF=*0xFEE;
      while (!BF);
   } while (--cnt2);

Or is it any more I need to cahnge?
Just a last check here: The MMCOut function just writes a byte and clocks it out on the SPI(nor read), and MMC8Clock Just clocks 0xFF out and dummy byte in (no read) and the MMCIn function reads a byte with 0xFF out, is this right?

Thanks alot for any help (if I get any Rolling Eyes )
Mattias, Sweden
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Thu Jul 12, 2007 8:23 am     Reply with quote

I have tried this code all day. but it just don't work.. any one have gotten it to work? I do not know what kould be wrong, I have debugged it and I do not find what could be the issue... I'm using a 18F4550 and I have change MAXFILES to 1 and decreased all #loacte's by 0x700 to fit my ram. could that be the problem?
/Mattias
CGEngineering



Joined: 26 Jun 2007
Posts: 8
Location: Italy

View user's profile Send private message MSN Messenger

PostPosted: Sat Jul 14, 2007 6:31 am     Reply with quote

Miniman wrote:
I have tried this code all day. but it just don't work.. any one have gotten it to work? I do not know what kould be wrong, I have debugged it and I do not find what could be the issue... I'm using a 18F4550 and I have change MAXFILES to 1 and decreased all #loacte's by 0x700 to fit my ram. could that be the problem?
/Mattias


It doesn't work to me too.
Author says that it works only with MMC cards

Bye
CG
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

Yes!
PostPosted: Thu Jul 19, 2007 4:45 am     Reply with quote

I have got it to work!!!
Oh man how happy I am!

This is what I have changed(the rest are the same):
Code:

#use FAST_IO(B)
#USE SPI(MASTER, SAMPLE_RISE, MSB_FIRST, IDLE=1, DI=PIN_B4, DO=PIN_B6, CLK=PIN_B5, BAUD=102400, BITS=8, STREAM=MMC_SPI)


int32 FATTable[128];
int32 gFirstEmptyCluster;

FAT32Vars gFAT32Vars;
diskinforec DiskInfo;
FILE gFiles[MAXFILES];

#byte MMCAddressL = gFAT32Vars
#byte MMCAddressH = gFAT32Vars+1
#byte MMCAddressHL = gFAT32Vars+2
#byte MMCAddressHH = gFAT32Vars+3
#byte gStartSectorL = gFAT32Vars+8
#byte gStartSectorH = gFAT32Vars+9
#byte gStartSectorHL = gFAT32Vars+10

#define ChipSel pin_B7
#define ChipClk pin_B5
#define ChipDin pin_B6 // guess it should be Dataout instead becouse the pin_c5 is Data out pin

#define MMC_Xfer(a) SPI_Xfer(MMC_SPI,a)

#define CardInserted 0 // 0 = inserted these pins are already defined in my main C file
#define YELLOWLED_ON() ;
#define YELLOWLED_OFF() ;

#define MMC_Select()    output_low(ChipSel);delay_us(50)
#define MMC_Deselect()   output_high(ChipSel)


char IsSelfDir(char *be)
{
if (be[0] == '.' && be[1] == '.') return 0xFF;
else return 0;
}

void MMCOut(char indata)
{
  MMC_Xfer(indata);
}

void MMC8Clock()
{
  MMC_Xfer(0xFF);
}

char MMCIn()
{
  return MMC_Xfer(0xFF);
}

char MMCInit()
{
   char response,iii,errcnt;
   restart_wdt();
   set_tris_b(0x10);  // for MMC, B4 Data in, B5,B6,B7 out
   output_high(ChipSel);
   output_high(ChipClk);
   output_high(ChipDin);

   iii = 10;
   errcnt = 100;
   do {
      MMCOut(0xFF);
   } while (--iii);
   delay_us(1000);
   output_low(ChipClk);
   MMC_Select();
   MMCOut(0x40);
   MMCOut(0x00);
   MMCOut(0x00);
   MMCOut(0x00);
   MMCOut(0x00);
   MMCOut(0x95);
   MMC8Clock();
   response = MMCIn();
   MMC_Deselect();
   output_high(ChipClk);
   do {
      delay_us(1000);
      output_low(ChipClk);
      MMC_Select();
      MMCOut(0x41);
      MMCOut(0x00);
      MMCOut(0x00);
      MMCOut(0x00);
      MMCOut(0x00);
      MMCOut(0x01);
      MMC8Clock();
      response = MMCIn();
      MMC_Deselect();
      output_high(ChipClk);
      MMC8Clock();
      errcnt--;
   } while (response && errcnt);
   return errcnt;
}

I do not know what made it work, maby the delay after chipselect? (I do not think so)
Any way, I have deleted the #locate statments becuse they are not needed if you don't want the veriables to be placed at a special point in memory.
NOTES:
* I use software SPI, that is slower and you should use hardware if you can.
* I use a MMC card, I don't know anything about SDcards.
* I have a 100k pull-up resistor from data-out from card to 3.3v.
* I do not have a cardinserted pin, I just asume the card is inserted

If you have more questions just ask and I will try to help you as much as I can.
I made this post to point out the fact that I have both read and written files successfully!!
Now I will try to make som kind of function to list the files on the card.
/ Miniman
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5  Next
Page 2 of 5

 
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