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

18F4550 with MMC/SD
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
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

18F4550 with MMC/SD
PostPosted: Tue Oct 31, 2006 12:18 pm     Reply with quote

I am using a 18F4550 with a 48MHz external Oscillator. So far I have been unsuccessful reading and writing to a MMC or SD card using the mmc_spi.c library. Most of my testing has been done with the MMC card. The card seems to initialize, but i cannot read or write to it. Here is my code.
Code:
#include<18F4550.h>
#use delay (clock=48000000)


#fuses HS, NOLVP, NOWDT, PUT, BROWNOUT


#define MMC_CS    PIN_A1
#define MMC_CLK   PIN_B1
#define MMC_DI    PIN_B0
#define MMC_DO    PIN_C7

#include <mmc_spi.c>
#include "lcd.h"


main()
{
   int size=0;
   int address=0;
   int *ptr;
   int value = 10;
   int rd_val = -1;
   
   lcd_init();
   
   setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_64);

   ptr = &value;
   size = sizeof(value);
   disable_interrupts(GLOBAL);
   lcd_clear();
   lcd_gotoxy(1,1);
   enable_interrupts(GLOBAL);

   setup_oscillator(OSC_250KHZ);
   if(mmc_init()==TRUE)
   {
      delay_ms(100);
      disable_interrupts(GLOBAL);
       printf(lcd_putc, "mmc init ok");
      enable_interrupts(GLOBAL);

   }
   else
   {
      disable_interrupts(GLOBAL);
      printf(lcd_putc, "mmc init fault");
      enable_interrupts(GLOBAL);
   }

   setup_oscillator(FALSE);

   disable_interrupts(GLOBAL);
   lcd_gotoxy(1,2);
   enable_interrupts(GLOBAL);

    if(mmc_write_block(address, size, ptr)==TRUE)
    {
      delay_ms(100);

      disable_interrupts(GLOBAL);
      printf(lcd_putc, "write ok");
      enable_interrupts(GLOBAL);
   }
    else
   {
      disable_interrupts(GLOBAL);
        printf(lcd_putc, "write fault");
      enable_interrupts(GLOBAL);
   }

   disable_interrupts(GLOBAL);
   lcd_gotoxy(1,3);
   enable_interrupts(GLOBAL);
   
     if(mmc_read_block(address, size, &rd_val)==TRUE)
   {
      delay_ms(100);

      disable_interrupts(GLOBAL);
        printf(lcd_putc, "read ok");
      enable_interrupts(GLOBAL);

   }
     else
   {
      disable_interrupts(GLOBAL);
        printf(lcd_putc, "read fault");
      enable_interrupts(GLOBAL);
   }
   while(1){;}
}



output:
Code:
mmc init ok
write fault
read fault


any input is much appreciated.
Thanks
asmallri



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

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

PostPosted: Tue Oct 31, 2006 1:40 pm     Reply with quote

What hardware are you using?
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Tue Oct 31, 2006 1:45 pm     Reply with quote

the card slot has "ALPS" inscribed on it with the # 45008. The card buffer is a 74LCX244DT. There is also a RES-4-CTS being used for the pull-ups on pin 8, 9, card detect and write protect.
asmallri



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

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

PostPosted: Tue Oct 31, 2006 2:32 pm     Reply with quote

I use a different SPI setup

Code:
   setup_spi(spi_master | spi_l_to_h | spi_clk_div_64 | spi_xmit_l_to_h);

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Tue Oct 31, 2006 2:38 pm     Reply with quote

Still getting the same errors. Here is the output from the spi driver if it helps:
Code:
ERR: mmc spi is busy                       
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     
 ERR: mmc spi is busy                     

 sent cmd 0, arg 0
 err: no start bit waiting for response
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy

 sent cmd 0, arg 0
 i=7
 cmd data in = 00
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: mmc spi is busy
 ERR: after reset, device isn't idle ERR: mmc spi is busy
 set block size failed
 ERR: mmc spi is busy
 ERR: couldn't set read block size
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 31, 2006 2:39 pm     Reply with quote

1. What is your external oscillator or crystal frequency ?
You have your #use delay() statement set for 48 MHz, but you're
using the HS fuse. The HS setting will only work for a crystal,
which is from 4 MHz to 25 MHz. This is according to Table 28-8
in the data sheet. Please post the frequency of your crystal.

2. You have various setup_oscillator() statements throughout
your code. Why are those statements in there ?

3. If you want Port B to come up as digital i/o upon power-on reset,
you must add the NOPBADEN setting to your #fuses statement.
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Tue Oct 31, 2006 5:19 pm     Reply with quote

1. Yes, you are absolutely correct. I should be using EC for my current config.

2.I inserted the oscillator statemetnts to control the speed during the card initialization. I am now realizing that the clock for the spi interface is set using the spi_setup() function.

3. Will I have to make any adjustments to the other ports(A and C) or is it only B that needs to be all digital?
Code:
#define MMC_CS    PIN_A1
#define MMC_CLK   PIN_B1
#define MMC_DI    PIN_B0
#define MMC_DO    PIN_C7
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Wed Nov 01, 2006 11:26 am     Reply with quote

After changing some config settings I now get different errors for the sd and mmc card.

SD Errors:
Code:
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is                 
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy
1 ERR: after reset, device isn't idle11 ERR: mmc spi is busy
 set block size failed
11 ERR: mmc spi is busy
4 ERR: couldn't set read block size


"11 ERR:" is comming from the mmc_send_command procedure during initialization. "1 ERR:" is right after the init procedure regains control and the rest are from a write and read operation(which would obviously fail due to the failure in the init.


MMC Errors:
Code:

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is               

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
 i=7   
 cmd data in = 00                 

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0                 
12 err: no start bit waiting for response                                         
11 ERR: mmc spi is busy                       
11 ERR: mmc spi is busy                       

 sent cmd 0, arg 0
12 err: no start bit waiting for response
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy

 sent cmd 0, arg 0
12 err: no start bit waiting for response
11 ERR: mmc spi is busy
11 ERR: mmc spi is busy

 sent cmd 0, arg 0
12 err: no start bit waiting for response
11 ERR: mmc spi is busy
1 ERR: after reset, device isn't idle
 sent cmd 16, arg 512
 i=7
 cmd data in = 00
11 ERR: mmc spi is busy
 write byte failed.  Nothing written!

 sent cmd 16, arg 1
12 err: no start bit waiting for response
4 ERR: couldn't set read block size


All output and errors come from the mmc_send_command procedure except "1 ERR:" which comes from mmc_init. The errors following the "1 ERR:" are generated from the mmc_init procedure.

I am not sure exactly what this is telling me other than the fact that I have some issues somewhere.

Any suggestions??
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 01, 2006 12:32 pm     Reply with quote

Did you change the setup_spi() function parameters to be the same
as asmallri's settings ? He's using SPI mode 0. Your original code
was set for Mode 2. Mode 0 samples data on the rising edge, which
is what the MMC spec says to use for SPI timing.
http://www.totalphase.com/support/articles/article03/#modes
I suggest that you change that line of code to match asmallri's settings.
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Wed Nov 01, 2006 12:37 pm     Reply with quote

Implemented that line of code before the last error posting.
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Wed Nov 01, 2006 12:49 pm     Reply with quote

Latest Version of my code:
Code:

#include<18F4550.h>


#use delay (clock=48000000)

#fuses EC ,NOLVP, PUT, BROWNOUT, NOWDT, NOPROTECT, DEBUG, NOPBADEN, CPUDIV1, VREGEN, NOUSBDIV



#define MMC_CS    PIN_A1
#define MMC_CLK   PIN_B1
#define MMC_DI    PIN_B0
#define MMC_DO    PIN_C7

#include <mmc_spi.c>
#include <usb_cdc.h>
#include "lcd.h"


void main(void)
{
   int size=0;
   int address=0;
   int *ptr;
   int value = 10;
   int rd_val = -1;
   char c = 'q';
   lcd_init();
   usb_init();
   
   c=usb_cdc_getc();

   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_64|SPI_XMIT_L_TO_H);

   ptr = &value;
   size = sizeof(value);
   
   disable_interrupts(GLOBAL);
   lcd_clear();
   lcd_gotoxy(1,1);
   enable_interrupts(GLOBAL);

   if(mmc_init()==TRUE)
   {   
      disable_interrupts(GLOBAL);
       printf(lcd_putc, "mmc init ok");
      enable_interrupts(GLOBAL);
   }
   else
   {
      disable_interrupts(GLOBAL);
      printf(lcd_putc, "mmc init fault");
      enable_interrupts(GLOBAL);
   }



   disable_interrupts(GLOBAL);
   lcd_gotoxy(1,2);
   enable_interrupts(GLOBAL);

    if(mmc_write_block(address, size, ptr)==TRUE)
    {
      disable_interrupts(GLOBAL);
      printf(lcd_putc, "write ok");
      enable_interrupts(GLOBAL);
   }
    else
   {
      disable_interrupts(GLOBAL);
        printf(lcd_putc, "write fault");
      enable_interrupts(GLOBAL);
   }

   disable_interrupts(GLOBAL);
   lcd_gotoxy(1,3);
   enable_interrupts(GLOBAL);
   
     if(mmc_read_block(address, size, &rd_val)==TRUE)
   {
      delay_ms(100);

      disable_interrupts(GLOBAL);
        printf(lcd_putc, "read ok");
      enable_interrupts(GLOBAL);

   }
     else
   {
      disable_interrupts(GLOBAL);
        printf(lcd_putc, "read fault");
      enable_interrupts(GLOBAL);
   }
   while(1){;}
}

bjf10



Joined: 26 Nov 2006
Posts: 1

View user's profile Send private message

PostPosted: Sun Nov 26, 2006 5:00 pm     Reply with quote

Code:
 if(mmc_init()==TRUE)
   {   
      disable_interrupts(GLOBAL);
       printf(lcd_putc, "mmc init ok");
      enable_interrupts(GLOBAL);
   }


Shouldn't that be:

Code:
f(mmc_init()==FALSE
   {   
      disable_interrupts(GLOBAL);
       printf(lcd_putc, "mmc init ok");
      enable_interrupts(GLOBAL);
   }


the mmc_init() function returns a ZERO if everything goes well...

-Brian.
krodkey



Joined: 12 Jul 2006
Posts: 9

View user's profile Send private message

PostPosted: Mon Nov 27, 2006 12:28 pm     Reply with quote

Thanks. I did notice that after the last post, but that was the least of my probs. I finally got a chance to put the board on a good scope and was able to see that timing was a major issue. The built in delay functions were giving inconsistent values. Once this was corrected I was able to initialize the card and received an R1 response. I have not tried to write to the card yet but that will be soon to come.
Picmouse



Joined: 20 Mar 2007
Posts: 7

View user's profile Send private message

PostPosted: Tue Mar 20, 2007 6:14 pm     Reply with quote

Confused Hello, you me this presenting the same problem that had krodkey, but I don't understand like it was the solution. In my case use an oscillator of 20MHz.

This is the code that I have:

Code:
#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_D1,force_sw)

#define MMC_CLK   PIN_B1
#define MMC_DI    PIN_B0
#define MMC_DO    PIN_C7
#define MMC_CS    PIN_B3
#include <mmc_spi.c>

#use standard_io(A)
#use standard_io(B)
#use standard_io(C)
#use standard_io(D)
#use standard_io(E)

void main (void){
set_tris_c(0b00010011);     
set_tris_b(0b00000001);
setup_spi(spi_master | spi_l_to_h | spi_clk_div_64 | spi_xmit_l_to_h);
if(mmc_init ()!=0) printf("\r\n ERROR\r\n");
else printf("\r\n OK\r\n");
   
   while(1){
   
   }
}



Always these errors:

Quote:
sent cmd 0, arg 0
err: no start bit waiting for response

sent cmd 0, arg 0
err: no start bit waiting for response

sent cmd 0, arg 0
err: no start bit waiting for response

sent cmd 0, arg 0
err: no start bit waiting for response

sent cmd 0, arg 0
err: no start bit waiting for response
ERR: after reset, device isn't idle


Please they can help me?


Last edited by Picmouse on Wed Mar 21, 2007 9:22 am; edited 1 time in total
ckielstra



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

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 6:50 am     Reply with quote

Check your hardware:
- Are you using an MMC or SD card?
- Are both the MMC card and the PIC running at 3.3V? If not, than you will have to add voltage level converters.
- Do you have pull-up resistors (100k) on Data_in and Data_out?
- Note that the data lines are crossed; MMC_DO of the PIC must be connected to Data_In (pin 2) of the MMC. MMC_DI of the PIC is connected to Data_Out (pin 7) of the MMC.
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