Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with code for reading from MMC card using PIC18F8720

Status
Not open for further replies.

conkhicon

Advanced Member level 4
Joined
Aug 31, 2004
Messages
107
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Activity points
1,015
I can't read out from MMC card when use PIC18F8720
- I do init it OK
- I write into OK (belong to status it's return)
- I read at address 512
MMC return to me a 0 (R1 OK)
but I loop send 0xff to wating a begin of datatoken 0xfebut don't match.
Can you help me?

#define BLOCK_LEN 16
my call:
readBlockMMC(0,512,bufMMC);
my sourcecode:
unsigned char readBlockMMC(unsigned long int DWAddr,unsigned char *pdes)
{
unsigned char localRD;
unsigned char dataToken;

Nop();
lowerSS(); // Card Active
writeCMD(17,DWAddr,0xff);// READ COMMAND (None CRC)
if(MCCResponse()!=0) {
MMC_Err = 0x30;
return(1);
}
Nop();
while(1) {// Waiting DATA TOKEN
WriteSPI(0xff);
dataToken = ReadSPI();
if(dataToken==0xfe) break;// Success
if((dataToken&0xe0)==0) {// Error Token
Nop();
MMC_Err = 0x31;
return(1);
}
}
Nop();
for(localRD=0;localRD<BLOCK_LEN;localRD++) {
WriteSPI(0xff);
*pdes = ReadSPI();
pdes++;
}
WriteSPI(0xff); // CRC
WriteSPI(0xff);
raiseSS(); // Card InActive
Nop();
return(0);
}
 

Re: MMC help?

Try sending &HFF just before you lower ss.
If it helps, here is my AVR ASM code.
Code:
[read_sec]
'***************************************************************
'issue Read Sector command to card
'***************************************************************
'Sub Read_sec(lbasector As Long , Byval Sec2xfer As Byte)
'lbasector= Y+2,Y+3 Sec2xfer=Y+0,Y+1
'entry point if registers need to be loaded by soft stack
Read_sec:
ldd Xl, Y+0
ldd Xh, Y+1                                                 'X register pair points to Sec2xfer
ld r24, X                                                   'r24 contains Sec2xfer
ldd Xl, Y+2
ldd Xh, Y+3                                                 'X register pair points to lbasector
ld r16, X+                                                  'r16 contains bits 0 - 7 of LBA & post incr
ld r17, X+                                                  'r17 contains bits 8 - 15 of LBA & post incr
ld r18, X+                                                  'r18 contains bits 16 - 23 of LBA & post incr
ld r19, X                                                   'r19 contains bits 24 to 31 of LBA only 27 is used
'entry point if registers pre loaded
'r24 = sectors to transfer, r16 to r19 = lbasector
[Readsec]
$EXTERNAL Lba2add , Txrxspi , Txrxspi_wait4resp , Chk4error
Readsec:
rcall Txrxspi255                                            'send 255 without chip select to flush card buffer
Cbi Spiport , Sdmmccs                                   'make SD / MMC Chip Select active
rcall Lba2add                                               'convert LBA to address
ldi r23, Read_multiple_block
rcall Txrxspi
mov r23, r19                                                'send address hi
rcall Txrxspi
mov r23, r18
rcall Txrxspi
mov r23, r17
rcall Txrxspi
mov r23, r16
rcall Txrxspi_wait4resp                                     'send address lo & wait for response
rcall Chk4error                                             'check for any error
ret
[end]

[get_sec]
'***************************************************************
'Get da Sector Data from the card and Stick it in da RAM
'***************************************************************
'Sub Get_sec(buffer() , Byval Sec_expected As Byte )
'buffer()= Y+2,Y+3 Sec_expected=Y+0,Y+1
'entry point if registers need to be loaded by soft stack
Get_sec:
ldd Xl, Y+0
ldd Xh, Y+1                                                 'X register pair points to Sec_expected
ld r24, X                                                   'r24 contains Sec_expected
ldd Xl, Y+2                                                 'X register pair points to buffer
ldd Xh, Y+3
'entry point if registers pre loaded
'r24 = Sector Counter (Sec_expected)
'Xl & Xh = (X register pair) points to buffer
[Getsec]
$EXTERNAL Txrxspi_wait4token , Txrxspi , Txrxspi_wait4resp , Chk4error
Getsec:
rcall Txrxspi_wait4token                                    'wait for data token
ldi r25, 0                                                  'word counter
Getsec_loop:
cpi r25, &HFF                                               'have we got 2 bytes to go?
brne Getsec_loop1                                          'if not, keep looping
dec r24                                                     'decr sec_expected
brne Getsec_loop1                                          'have we finished?
ldi r23, Stop_transmission                                  'if so, then stop txing
rcall Txrxspi                                               'get a byte in r23
st X+, r23                                                  'store to buffer
rjmp Getsec_loop2
Getsec_loop1:
rcall Txrxspi255                                            'get a byte in r23
st X+, r23                                                  'store to buffer
Getsec_loop2:
rcall Txrxspi255
st X+, r23                                                  'again. 2 bytes = 1 word
inc r25                                                     'incr word counter
brne getsec_loop                                           'have we finished transfering 1 block
rcall Txrxspi255                                            'get crc16 hi & dump it
rcall Txrxspi255                                            'get crc16 lo & dump it
tst r24                                                     'is sector counter = 0
brne Getsec                                               'if not finished, get some more data
rcall Txrxspi_wait4resp                                     'wait for valid response
rcall Chk4error                                             'check for any error
sbi Spiport, Sdmmccs                                        'make SD / MMC Chip Select not active
ret
[end]
 

Re: MMC help?

for pic to access MMC there is a very nice library in mikroC, try it ....

demo is available at **broken link removed**

regards
 

MMC question?

Have a difference between MMC which support differently command classes.

I read sandisk MMC productManual and see in CSD Rergister [95:84] field
CCC=0x5=0b 000 000 0101 support comand classes 0,2 which class 0 support Initilize MMC card and class 2 support Read Command and so on I try to write MMC card with Write Block (in class 4) failure. I can read MM Card and not Write.
do I think true?
But this card insert to mobile. It very good for read and write. Why???
Do you help me?
 

Re: MMC question?

One possible reason is the address provided. Block writes can only write from the beginning of a block boundary, whilst reads can occur anywhere within the block. I havent touched MMCs for years, but I do recall this is one of the limitations.
 

Re: MMC question?

Can you help me?

#define BLOCK_LEN 16
my call:
readBlockMMC(0,512,bufMMC);
my sourcecode:
unsigned char readBlockMMC(unsigned long int DWAddr,unsigned char *pdes)
{
unsigned char localRD;
unsigned char dataToken;

Nop();
lowerSS(); // Card Active
writeCMD(17,DWAddr,0xff);// READ COMMAND (None CRC)
if(MCCResponse()!=0) {
MMC_Err = 0x30;
return(1);
}
Nop();
while(1) {// Waiting DATA TOKEN
WriteSPI(0xff);
dataToken = ReadSPI();
if(dataToken==0xfe) break;// Success
if((dataToken&0xe0)==0) {// Error Token
Nop();
MMC_Err = 0x31;
return(1);
}
}
Nop();
for(localRD=0;localRD<BLOCK_LEN;localRD++) {
WriteSPI(0xff);
*pdes = ReadSPI();
pdes++;
}
WriteSPI(0xff); // CRC
WriteSPI(0xff);
raiseSS(); // Card InActive
Nop();
return(0);
}

Do you think I'm true?
 

Re: MMC help?

I use Microchip C18 Compiler
Please post function for PIC which is runing OK.
Thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top