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

PIC+MMC Circuit?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21169

View user's profile Send private message

PostPosted: Thu Jun 09, 2005 1:53 pm     Reply with quote

I translated the code given in ckielstra's link, to CCS.
http://www.cc5x.de/MMC/
I also wired up my test board according to their schematic.
It works.

In the translation to CCS, I tried to make as few changes as possible.
I translated a few of the German comments.
Also, they had the read operation occur first, followed by the write.
That was confusing to me, so I changed it to write the data, and then
read it back and display it.
They had the data set to all "MMMMM". I wanted to know that it
could do something besides that, so I added "Begin" and "End"
to the data block.

We should thank those guys for posting their code and the schematic.
We had a thread on MMC a few weeks ago, and nothing worked.

Code:
#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#use fast_io(C)

#byte SSPBUF = 0x13
#byte SSPCON = 0x14
#byte SSPSTAT = 0x94
#bit  BF = SSPSTAT.0
#bit SMP = SSPSTAT.7
#bit CKE = SSPSTAT.6
#bit CKP = SSPCON.4
#bit SSPM1 = SSPCON.1
#bit SSPEN = SSPCON.5

#byte PORTC = 7
#bit CS = PORTC.2      

//****************************************
// This is the same as the CCS spi_read() function.

char SPI(char d)      
{            
SSPBUF=d;
while (!BF);         
return SSPBUF;         
}

//******************************************

char Command(char befF, int16 AdrH, int16 AdrL, char befH)
{            
SPI(0xFF);
SPI(befF);
SPI(AdrH >> 8);
SPI(AdrH);
SPI(AdrL >> 8);
SPI(AdrL);
SPI(befH);
SPI(0xFF);
return SPI(0xFF);      // Return with the response
}
//********************************************

char MMC_Init()
{
char i;   

// Init SPI
SMP=0;      
CKE=0;      
CKP=1;      
SSPM1=1;      
//SSPM0=1;   
SSPEN=1;    

CS=1;      // MMC-Disabled

// MMC in SPI Mode -- start and Reset.
for(i=0; i < 10; i++) SPI(0xFF);             // 10*8=80 clocks
CS=0;                                             // MMC-Enabled

// CMD0
if (Command(0x40,0,0,0x95) !=1) goto Error;    // Reset

st:
// CMD1            
if (Command(0x41,0,0,0xFF) !=0) goto st ;   // CMD1 

return 1;

Error:
return 0;
}
//*********************************************

void main(void)
{
int16 i;            
setup_port_a(NO_ANALOGS);
set_tris_c(0b11010011);    // sck rc3-0, sdo rc5-0, CS rc2-0.
set_tris_b(0b00000010);   
puts("Start\n\r");               
if(MMC_Init())
   puts("MMC ON\n\r");       // MMC Init OK

//*****************************************

// Write in 512 Byte-Mode
if (Command(0x58,0,512,0xFF) !=0) puts("Write error ");
SPI(0xFF);
SPI(0xFF);
SPI(0xFE);

SPI("Begin\n\r");   // 7 characters
   
for(i=0; i < 500; i++)   // Was 512, but used 12 for text
   {
   SPI('M');
   }
SPI("\n\rEnd");   // 5 characters


SPI(255);       // Send two bytes of 0xFF at the end
SPI(255);
i=SPI(0xFF);
i &=0b00011111;
if (i != 0b00000101) puts("Write Error ");
while(SPI(0xFF) !=0xFF); // Wait for end of Busy condition

//*************************************

// Read in 512 Byte-Mode
if (Command(0x51,0,512,0xFF) !=0) puts("Read Error ");

while(SPI(0xFF) != 0xFE);   

for(i=0; i < 512; i++)
   {
    putc(SPI(0xFF));      // Send data
   }
SPI(0xFF);     // Send two bytes of 0xFF at the end
SPI(0xFF);



//**********************************************
while(1);       // The program stops here.            
}
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Fri Jun 10, 2005 1:18 am     Reply with quote

Hi Ckielstra and PCM programmer

Thank you VERY MUCH for your help!

I am feeling really stupid though- I have tried both your suggestions (including the TRIS command, and the whole new code posted by PCM programmer), and I still get an error.
In both cases at the initiliazation stage.

Basically when I am reading from the Card.

In Ed Waugh's code, on the line
while(SPI_READ(0xFF) != response && --count > 0);

it returns count = 0;

and in the PCM programmers code at the initialization stage:

Command(0x40,0,0,0x95) !=1

it goes to "Error"

PLEASE can you suggest what I should do, and thank you once again.

All the best
arrow
PCM programmer



Joined: 06 Sep 2003
Posts: 21169

View user's profile Send private message

PostPosted: Fri Jun 10, 2005 1:51 am     Reply with quote

Well, it's not my code. It's the code from this website
http://www.cc5x.de/MMC/ that I translated to CCS.

You should go to that website and make sure that you follow
their schematic. I suspect you have a wiring error.
Make sure you know which pin on your MMC is pin 1.
Be certain that you don't have it wired backwards.
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Fri Jun 10, 2005 2:01 am     Reply with quote

Hi PCM Programmer

I actually use a flash card holder from:

http://www.sparkfun.com/shop/index.php?shop=1&itemid=331

so there is only one way to insert the card.

I use the 128MByte MMC card from "Super Talent". Together with the Low power version of the PIC (LF) series.

By mistake, I did apply a 5V to the MMC briefly, do you think that could have burn the card?
Can the MMC be pre-formated or write protected or something like that?

Also, would you know if writing to FLASH is faster than to EEPROM?
I would like to write in "blocks" of data to both. And if its faster, by how much?

Thank you
arrow
ckielstra



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

View user's profile Send private message

PostPosted: Fri Jun 10, 2005 3:24 am     Reply with quote

Quote:
By mistake, I did apply a 5V to the MMC briefly, do you think that could have burn the card?
Hard to give a specific answer, in general it is a bad thing to apply a too high voltage. Try a new card and see if things improve.

Quote:
Can the MMC be pre-formated or write protected or something like that?
No. SD cards however are pin compatible to MMC but slightly different protocol and do have a write-protect switch.

Quote:
Also, would you know if writing to FLASH is faster than to EEPROM?
I would like to write in "blocks" of data to both. And if its faster, by how much?
Technology improves continuously, you'll have to look at the datasheets of your flash and eeprom devices.
Very likely the limiting factor in writing to the MMC card will be the SPI bus. For SPI the maximum clock rate is Fosc/4. your processor is running at 4MHz I believe? The maximum SPI clock will then be 1MHz or 125kbyte/sec. This excludes the actual write to flash, timing for this depends on the card you are using. New higher speed cards exist for use in photo camera's.

Please note that for MMC cards you can only write data in blocks of 512 bytes. This makes modyfying data in MMC difficult in PIC processors because of the limited RAM capacity (read 512 bytes, modify, write back whole block). Reading smaller blocks up to a single byte is possible.

A trick used in some datalogging applications is the fact that writing the data block to the MMC has no maximum timing limit as long as the chip-select line stays active. Data will only be written to flash when the full 512 bytes are received by the MMC, so beware of power loss.
Guest








Some details please
PostPosted: Fri Sep 22, 2006 1:57 pm     Reply with quote

Hello,

Can someone explain me the details conserning the adresses in below code ?

Code:
//******************************************

char Command(char befF, int16 AdrH, int16 AdrL, char befH)
{             
SPI(0xFF);
SPI(befF);
SPI(AdrH >> 8);
SPI(AdrH);
SPI(AdrL >> 8);
SPI(AdrL);
SPI(befH);
SPI(0xFF);
return SPI(0xFF);      // Return with the response
}
//********************************************


It seems a bit unlogic for me when writing 512 bytes in the 1st sector, AdrH is zero and AdrL is 512.
Isn't H standing for High and L standing for Low ?

Thanks.
Bart



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

PostPosted: Fri Sep 22, 2006 2:07 pm     Reply with quote

Guestpost was from me.
PCM programmer



Joined: 06 Sep 2003
Posts: 21169

View user's profile Send private message

PostPosted: Fri Sep 22, 2006 3:36 pm     Reply with quote

Quote:

It seems a bit unlogic for me when writing 512 bytes in the 1st sector,
AdrH is zero and AdrL is 512.

They've skipped the first 512 bytes. Your question is, why did they
do this. Here's a possible answer:

Down at the bottom of the http://www.cc5x.de/MMC/ web page,
they have a comments section. It's all in German, but by using
the Altavista translator http://babelfish.altavista.com/ I can translate it.
The last section says this:
Quote:

One writes and one reads starting from address 512. Starting from
address 512 the reserved range begins when formatting with FAT16,
so that even the data and file system on your MMC map remain.


Regarding your question about the addresses, here's a short summary
of how to interface an MMC card to a microcontroller:
http://www.analog.com/UploadedFiles/Application_Notes/58264366383775EE264v01.pdf
It shows that the address field is 32-bits wide for the WRITE_BLOCK
command. So, the CC5X compiler code uses two 16-bit words to
pass the address to the Command() function. If we were to write
that function in CCS, we would use an 'int32' data type instead.
hippie
Guest







PostPosted: Fri Sep 22, 2006 5:39 pm     Reply with quote

Hi, just passing by:

this might interest you: h**p://elm-chan.org/fsw/ff/00index_e.html
Jerry I



Joined: 14 Sep 2003
Posts: 96
Location: Toronto, Ontario, Canada

View user's profile Send private message

PostPosted: Sun Sep 24, 2006 6:46 pm     Reply with quote

hippie wrote:
Hi, just passing by:

this might interest you: http://elm-chan.org/fsw/ff/00index_e.html


Interesting site.

Could anyone download the source?.

I keep getting corrupt files.

Thanks
asmallri



Joined: 12 Aug 2004
Posts: 1594
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Sep 26, 2006 6:36 am     Reply with quote

Yes I can download it without problem. I have completed a major port of this software to the PIC18F and the CCS compiler. It took me weeks!!
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
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 Previous  1, 2
Page 2 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