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

Multimediacard issues
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
Chang-Huei Wu
Guest







Re: Multimediacard issues
PostPosted: Thu Jan 09, 2003 9:02 pm     Reply with quote

<font face="Courier New" size=-1>sorry, all format is gone ...
even some codes are missing ...

characters after "<" are missing ...

#include for (i= MMC_Addr_Offset_Index; i<block_length; i++)


send me an e-mail then I can mail you a
easy-to-read version ___________________________
This message was ported from CCS's old forum
Original Post ID: 10582
Chang-Huei Wu
Guest







Re: Multimediacard issues
PostPosted: Thu Jan 09, 2003 9:05 pm     Reply with quote

<font face="Courier New" size=-1>Since Tomi had posted his 300 lines of code, let me try 400 ...

main program: mmc_test.c
driver: yz_mmc.c

Best wishes

C-H Wu

--------------------
/* mmc_test.c MMC = Multi_Media_Card 16 ~ 128 MB by C-H Wu, 2002/12/31


Tested: PRETEC - High Speed 32 MB MMC, 512 bytes / block

PIC16F877 X'tal = 20 MHz


mmc_write( int8 ) executing time:


driver average single byte single byte
KB / sec MMC buffer Block programming
--------------------------------------------------
yz_mmc.c 32 28 usec 2500 usec
yz_mmc_1.c 24 30 usec 240 usec
--------------------------------------------------

Note: yz_mmc_1.c has FIFO buffer

Note: PIC18F452 + 40 MHz X'tal + yz_mmc_1.c
maybe 120 ~ 140 usec ... to be tested


global var. (int1) MMC_Programming '0' MMC buffer
'1' programming

I/O pin: hard coded in driver yz_mmc.c or yz_mmc_1.c



--------------------------------------------------------------------------- */

#include <16F877.H> // <18F452.H>

#fuses HS, NOWDT, PUT, NOBROWNOUT, NOLVP, NOCPD, WRT

#use delay(clock=20000000)
#use rs232(baud= 115200, xmit=PIN_C6, rcv=PIN_C7, errors)

#include <yz_mmc_1.c> // yz_mmc.c or yz_mmc_1.c

main() {

int16 i; int8 j, k, err, x;

printf("\n\n\n\n\n\r 16 ~ 128 MB MMC memory card test ... ");

err = mmc_init_spi(); if (!err) printf("mmc_init_spi() ... ok.");

printf("\n\n\n\r Writing 256 kB data to MMC ( 24 or 32 kB/sec ) ... ");


// --- clean up FAT (0x0 ~ 0x000139ff) ! ------------------------------
//
mmc_open_4_write(0x00, 0x00, 0x00); // 24 high bits of 32-bit addr


for(j=0;j< 8;j++) { x= 1; // just for testing
for(i=0;i<32768;i++) mmc_write( x+=2 ); printf("\%u ", j+1);
}

mmc_close(); // close before R/W mode change

printf("\n\n\r Reading data back ... ");
printf("press any key to continue ... "); getc(); printf("\n\n\r ");

mmc_open_4_read( 0x00, 0x02, 0x00); // 24 high bits of 32-bit addr

for (i=0; i< 5; i++) {

x = mmc_read(); printf("x(\%lu)=\%u ", i, x );

output_d ( x ); // to 8-bit DAC
}

mmc_close(); // close before R/W mode change

while ( 1 );
}

-----------------

/* yz_mmc.c MMC = Multi_Media_Card (16 ~ 128 MB) for YZ_PIC

by C-H Wu, 2002/12/29
*/

#define block_length 512 // WRITE_BLOCK_LEN, must be 512
#define BLOCK_LEN 0x02 // 0x02 for 512 bytes, 0x08 for 2048

// ---- I/O pin --------------------------------------------------------------
//
#if defined(__PCM__) // F877 PROTO MMC
// -----------------------------------------------
#bit MMC_SEL = 0x07.4 // RC4, CN2.13, pin_1, active low
#bit MMC_Din = 0x07.3 // RC3, CN2.11, pin_2, data / command, host to MMC
// GND, CN2.09, pin_3, GND
// Vdd, CN2.07, pin_4, Vdd, 2.7 ~ 3.6 V
#bit MMC_CLK = 0x06.3 // RB3, CN2.05, pin_5, pull up to +3 V
// GND, CN2.01, pin_6, GND
#bit MMC_Dout= 0x06.2 // RB2, CN2.03, pin_7, data / status, MMC to host
#bit TRIS_C4 = 0x87.4
#bit TRIS_C3 = 0x87.3
#bit TRIS_B3 = 0x86.3
#bit TRIS_B2 = 0x86.2

#else // F452
#bit MMC_SEL = 0xF82.4 // RC4
#bit MMC_Din = 0xF82.3 // RC4
#bit MMC_CLK = 0xF81.3 // RB3
#bit MMC_Dout= 0xF81.2 // RB2
#bit TRIS_C4 = 0xF94.4
#bit TRIS_C3 = 0xF94.3
#bit TRIS_B3 = 0xF93.3
#bit TRIS_B2 = 0xF93.2
#endif
#define set_tris_4_mmc {TRIS_C4=0;TRIS_C3=0;TRIS_B3=0;TRIS_B2=1;}

// ---- global variables ----mmc_error(-------------------------------------------------
//
int1 MMC_Programming = 0; // '1' for programming,2.5 msec !

int1 MMC_RW_Status = 0; // '1' indicate "reading" MMC
int1 MMC_working = 0; // '0' indicate "sleeping"

int8 MMC_Addr_4, MMC_Addr_3; // 32-bit MMC address
int8 MMC_Addr_2;//MMC_Addr_1; // MMC_Addr_1 always zero
int16 MMC_Addr_Offset_Index; // 0 ~ 511

// ---- function prototyping -------------------------------------------------
//
int8 mmc_init_spi(); // initialize MMC in SPI mode
int8 mmc_read(); // read a byte of data from MMC
void mmc_write(int8 data); // write a byte of data to MMC

int8 mmc_open_4_read( int8 Addr_4, int8 Addr_3, int8 Addr_2); // 24 high bits of 32-bit addr
int8 mmc_open_4_write(int8 Addr_4, int8 Addr_3, int8 Addr_2);
void mmc_close();

int8 mmc_in(); // get a byte from MMC, data or status
void mmc_out(int8 data); // send a byte to MMC, data or command

void mmc_write_flush(); // write x bytes to MMC, make block_length
int8 mmc_read_flush(); // read x bytes from MMC, make block_length

void mmc_send_8_clock(); // send 8 clocks to MMC, for internal operations

void mmc_error(int8 code) { printf(" MMC_Error_\%u ", code); }

// -------------------------------------------------------------------------

void mmc_close()
{
printf("\n\n\r mmc_close() ... ");

if ( MMC_RW_Status ) { mmc_read_flush( ); }
else mmc_write_flush();

MMC_SEL = 1; // close the chip
MMC_CLK = 1;

printf(" ... done.");

}


int8 mmc_write_flush() // send x bytes of 0xFF to MMC, make block_length
{ int16 i; int8 response= 0;

printf("write_flush() ... ");

for (i= MMC_Addr_Offset_Index; i
mmc_out(0xFF);

mmc_out(0x00); // two more bytes CRC
mmc_out(0x01); // printf("block_length reached, resp= \%02x", response);

response = mmc_in(); // return status, should be 0bxxx00101

response |= 0xE0; // do we need this ? 2002/12/27, Wu
response ^= 0xE5; // get the status byte

if ( !response ) while ( mmc_in() == 0 );

MMC_Addr_Offset_Index= 0;

MMC_working = 0;

printf(" block 0x\%02x\%02x\%02xxx ", MMC_Addr_4, MMC_Addr_3, MMC_Addr_2);

return ( response );
}


void mmc_write(int8 data) // "write" a byte of data to MMC
{ int8 i= 8, response= 0;

// restart_wdt(); // Wu

if ( (!MMC_working) || MMC_RW_Status ) {

// printf(" \n\rError ! MMC not opened for writing ! \n\r");
mmc_error(1);

} else {

mmc_out( data ); // send out a byte of data

if ( (++MMC_Addr_Offset_Index) == block_length ) { // block_length reached
MMC_Addr_Offset_Index = 0;

MMC_Programming = 1;

mmc_out(0x00); // two more bytes CRC
mmc_out(0x01);

response = mmc_in(); // return status, should be 0bxxx00101

// printf(" mmc_write() ... block_length reached, resp= \%02x", response);

response |= 0xE0; // do we need this ? 2002/12/27, Wu
response ^= 0xE5;

if ( !response ) { while ( mmc_in() == 0 ); // 2.5 msec ! programming
} else {
// printf(" \n\rError ! MMC_Programming_Fail ! \n\r");
mmc_error(2);
}

MMC_working = 0;

MMC_Addr_2 += BLOCK_LEN; if( MMC_Addr_2 ==0 )
if((++MMC_Addr_3)==0 ) ++MMC_Addr_4;

mmc_open_4_write(MMC_Addr_4, MMC_Addr_3, MMC_Addr_2);// open next page

MMC_Programming = 0;
}
}
}


//... mmc_open_4_write() ... block of block_length bytes, return 0 if success
//
int8 mmc_open_4_write(int8 Addr_4, int8 Addr_3, int8 Addr_2)
{ int8 errs, response;

if ( MMC_working ) {

// printf("\n\n\rMMC is still under reading or writing !");
mmc_error(3);

mmc_close();
}

MMC_Addr_4 = Addr_4;
MMC_Addr_3 = Addr_3;
MMC_Addr_2 = Addr_2 & (~(BLOCK_LEN-1)); // & 0xFE;

MMC_CLK = 0;
MMC_SEL = 0;

mmc_out( 0x58 ); // CMD_24, hex 0x58, WRITE_BLOCK
mmc_out( MMC_Addr_4 ); // bit 31..24 of 32 bit address
mmc_out( MMC_Addr_3 ); // bit 23..16
mmc_out( MMC_Addr_2 ); // bit 15...8, bit 8 must be zero
mmc_out( 0x00 ); // bit 7...0, always zero
mmc_out( 0x01 ); // CRC, write op. don't care ?

MMC_send_8_clock(); // 8-clock delay for internal oper.

errs = 8;
do { response = mmc_in(); // printf("\n\rresp= 0x\%02x", response);
} while (--errs && response); // return 0x00 if success

if ( !response ) { // init to write ... success

MMC_send_8_clock(); // require 8x clocks ?
MMC_send_8_clock();
MMC_send_8_clock();
mmc_out( 0xFE ); // the page is opened for write

MMC_working = 1;
MMC_RW_Status = 0;
MMC_Addr_Offset_Index = 0;

// printf("\n\rmmc_open_4_write(0x\%02x\%02x \%02x00) ... ok.", MMC_Addr_4, MMC_Addr_3, MMC_Addr_2);
}
return ( response );
}




int8 mmc_read_flush() // read x bytes from MMC, make block_length
{
int16 i; printf("read_flush() ... ");

for (i= MMC_Addr_Offset_Index; i
mmc_in(); mmc_in(); // two more bytes CRC

MMC_working = 0;
MMC_Addr_Offset_Index = 0;

printf(" block 0x\%02x\%02x\%02xxx ", MMC_Addr_4, MMC_Addr_3, MMC_Addr_2);
}


int8 mmc_read() // "read" a byte of data to MMC
{
int8 i= 8, retval= 0;

// restart_wdt(); // Wu

if ( (!MMC_working) || (!MMC_RW_Status)) {

// printf(" \n\rError ! MMC not opened for reading ! \n\r");
mmc_error(4);

} else {

retval = mmc_in(); // get a byte of data

if ( (++MMC_Addr_Offset_Index) == block_length ) { // block_length reached
MMC_Addr_Offset_Index = 0;

mmc_in(); // two more bytes CRC
mmc_in(); // printf(" mmc_read() ... block_length reached");

MMC_working = 0;

MMC_Addr_2 += BLOCK_LEN; if( MMC_Addr_2 ==0 )
if((++MMC_Addr_3)==0 ) ++MMC_Addr_4;

mmc_open_4_read(MMC_Addr_4, MMC_Addr_3, MMC_Addr_2);// open next page
}
}
return retval;
}


//... mmc_open_4_read() ... block_length bytes, return 0 if success
// return 1 if failed in phase 1
// return 2 if failed in phase 2
//
int8 mmc_open_4_read( int8 Addr_4, int8 Addr_3, int8 Addr_2)
{
int8 errs, response;

if ( MMC_working ) {

// printf("\n\n\rMMC is still under reading or writing !");
mmc_error(5);

mmc_close();
}

MMC_Addr_4 = Addr_4;
MMC_Addr_3 = Addr_3;
MMC_Addr_2 = Addr_2 & (~(BLOCK_LEN-1)); // & 0xFE;

MMC_CLK = 0;
MMC_SEL = 0;

mmc_out( 0x51 ); // CMD_17, hex 0x51
mmc_out( MMC_Addr_4 ); // bit 31..24 of 32 bit address
mmc_out( MMC_Addr_3 ); // bit 23..16
mmc_out( MMC_Addr_2 ); // bit 15...8, bit 8 must be zero
mmc_out( 0x00 ); // bit 7...0, always zero
mmc_out( 0x01 ); // CRC, write op. don't care ?

errs = 8;
do { response = mmc_in(); // printf("\n\rresp= 0x\%02x", response);
} while (--errs && response==0xFF); // wait until return 0x00

if (!errs) { // printf("\n\rmmc_open_4_read() ... init ... failed !");

mmc_error(6); return(1);
}
errs = 8; // min. 30 ! or, delay_us(120) below
do { response = mmc_in(); delay_us(200);
// printf("\n\rresp= 0x\%02x", response);
} while (--errs && response==0xFF); // wait until return 0xfe

if (!errs) { // printf("\n\rmmc_open_4_read() ... start byte ... failed !");

mmc_error(7); return(2);
}
MMC_working = 1;
MMC_RW_Status = 1;
MMC_Addr_Offset_Index = 0;

// printf("\n\rmmc_open_4_read( 0x\%02x\%02x \%02x00) ... ok.", MMC_Addr_4, MMC_Addr_3, MMC_Addr_2);

return 0 ;
}

// initialize MMC in SPI mode, return 0 if success, 1 if failed
//
int8 mmc_init_spi()
{ int1 flag;
int8 response, i, errcnt;

set_tris_4_mmc;

MMC_SEL = 1;
MMC_CLK = 1;
MMC_Din = 1;

flag = MMC_Dout;

i = 0;
do {
MMC_CLK = 1;
MMC_CLK = 0;
} while (--i);

delay_us(250);

MMC_CLK = 0;
MMC_SEL = 0;

mmc_out(0x40); // CMD_00, RESET sequence
mmc_out(0x00);
mmc_out(0x00);
mmc_out(0x00);
mmc_out(0x00);
mmc_out(0x95); // CRC, computed

MMC_send_8_clock();

response = mmc_in(); // reponse 0x01 if ok
// printf("RESET, resp= 0x\%02x", response);
MMC_SEL = 1;
MMC_CLK = 1;

errcnt = 100;
do {
delay_us(250);

MMC_CLK = 0;
MMC_SEL = 0;

mmc_out(0x41); // CMD_01, Init sequence
mmc_out(0x00);
mmc_out(0x00);
mmc_out(0x00);
mmc_out(0x00);
mmc_out(0x01); // CRC, don't care, 0xFF ? ?

MMC_send_8_clock();
response = mmc_in();

MMC_SEL = 1;
MMC_CLK = 1;

errcnt--;

// printf("\n\rresp= 0x\%02x, cnt= \%u", response, errcnt);

} while ( bit_test(response,0) && errcnt ); // wait until response 0x00

MMC_working = 0;

return ( !errcnt );
}


int8 mmc_in() // "get" a byte from MMC
{
int8 i= 8, retval= 0;

// restart_wdt(); // Wu

MMC_Din = 1;
do {
retval <<= 1;
if ( MMC_Dout ) bit_set(retval,0); MMC_CLK = 1; MMC_CLK = 0;
} while (--i);
return retval;
}


void mmc_out(int8 data) // "send" out a byte to MMC
{
int8 i= 8;

// restart_wdt(); // Wu

do { // write 8 bits, same as for(). but the code size is less
if ( bit_test(data,7) ) MMC_Din = 1;
else MMC_Din = 0;
MMC_CLK = 1; data <<= 1;
MMC_CLK = 0;
} while (--i);
}


void MMC_send_8_clock() // send 8 clocks to MMC for internal operations
{ int8 i= 8; MMC_Din = 1;
do { MMC_CLK = 1; MMC_CLK = 0; } while (--i);
} ___________________________
This message was ported from CCS's old forum
Original Post ID: 10583
jasperting



Joined: 25 Sep 2007
Posts: 1

View user's profile Send private message

send you an e-mail, Please help me! Thank you.
PostPosted: Tue Sep 25, 2007 2:45 am     Reply with quote

send you an e-mail then you mail me a easy-to-read version of yz_mmc.c.
My e-mail address:jasper_ting@compal.com
Thank you very much.
Jasper. Ting
ckielstra



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

View user's profile Send private message

PostPosted: Tue Sep 25, 2007 3:05 am     Reply with quote

This thread has been dead for more than 4 years. Small chance you are going to get a reply.

Many other and newer code examples can be found on this forum (use search on top of this page) or use the CCS provided driver mmc_spi.c in the drivers directory from your compiler.
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Sep 25, 2007 4:04 am     Reply with quote

wow that thread got resurrected from the CCS Graveyard Mr. Green

small chance that you might have a reply ... i would say give im a PM but its a guest user like many on this forum ^_^
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