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

Optimizations for SD-Card Access
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
DDDDaniel
Guest







PostPosted: Sun Feb 03, 2008 1:01 pm     Reply with quote

that's strange... in my last posting the for-loops got corrupted.

it has to be:
for(i=0 ; i<12 ; i++)
and
for (a = 0 ; a<0x06 ; a++)
of course...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 03, 2008 1:05 pm     Reply with quote

You have to disable HTML when you post code.

My advice:
1. Register and become a forum member.
2. Edit your profile and permanently disable HTML.
3. Keep BBcode enabled.
4. Log in and select the tick box to "log in automatically every time".

Then you'll never have to worry about disabling HTML. It will
always be done. Your code will always post correctly.
ckielstra



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

View user's profile Send private message

PostPosted: Sun Feb 03, 2008 6:00 pm     Reply with quote

Code:
   // send 6 Byte Command
   for (a = 0;a<0x06> 500)
         {
            break; //abort. MMC/SD-card does not response
         }
      }
There is more corrupted code here than just the for-loop. Looks like some lines are missing. Same for the other for-loop.
Too bad you are not a register member of this forum or you would have had the option to edit your previous posting.


I'm glad to hear your read speed increased from 260kB/s to 540kB/s, a nice improvement!

It is indeed fun to squeeze out the last bit of power from the PIC processor, but it also helps a lot to learn understand the processor better. For example I never expected one SPI mode to be faster than another.

I'm willing to help improve your write routine, but with all the new knowledge you should be able to make some huge improvements yourself. Then return to this forum for the final tweaking.
ColtFish



Joined: 04 Feb 2008
Posts: 3

View user's profile Send private message

PostPosted: Mon Feb 04, 2008 1:16 pm     Reply with quote

Allright,
here's the complete MMC/SD - code, this time with HTML disabled Wink

Code:

#byte SSP1CON1   =   0xFC6
#byte SSP1STAT   =   0xFC7
#byte SSP1BUF    =    0xFC9

// Sends a Command to the MMC/SD-card
int mmc_write_command (int *cmd)
{
   int tmp = 0xff;
   long Timeout = 0;
   int a;

   MMC_Enable();
 
   // send 8 Clock pulses
   spi_write(0xFF);

   // send 6 Byte Command
   for (a = 0;a<0x06;a++)
      {
         spi_write(*cmd++);
      }

   // wait for a valid reponse
   while (tmp == 0xff)   
      {
         tmp = spi_read(0xFF);
         if (Timeout++ > 500)
         {
            break; //abort. MMC/SD-card does not respond
         }
      }
   return(tmp);
}


int mmc_init()
{   
   int i;
   unsigned long Timeout = 0;
   unsigned int res;
   int cmd[] = {0x40,0x00,0x00,0x00,0x00,0x95};

   // setup SPI Mode 3 (CKP = 1, CKE = 0)
   SSP1STAT = 0b00000000;      // cke = 0
   SSP1CON1 = 0b00110010;      // sspen = 1, SPI-FREQ: FOSC/64, CKP = 0

   output_high(CS_CARD);
   
   delay_cycles(255);

   // initialise the MMC card into SPI mode by sending clock pulses
   for(i=0;i<12;i++)                  
   {                       
           SPI_WRITE(0xFF);
   }

   while(mmc_write_command (CMD) !=1)
   {
         delay_ms(1);     

         if (Timeout++ > 0x2FFF)
         {
            return(1); // Failure! (Return Code1)
         }
   }

   // Send Command CMD1 to MMC/SD-card
   Timeout = 0;
   CMD[0] = 0x41;//Command 1
   CMD[5] = 0xFF;
   res = mmc_write_command (CMD);
   while( res !=0)
   {
       res = mmc_write_command (CMD);
       delay_ms(1);     
       if (Timeout++ > 0x2FFF)
         {
            printf("\n\rInit failed. [%X]", res);
            return(2); //Failure! (Return Code2)
         }
   }

   printf("\n\rInit done.");

   // after successful mmc-init the SPI Frequency can be set to maximum speed (Fosc/4)
   // Mode 3 (CKP = 1, CKE = 0)
   SSP1STAT = 0b00000000;      // cke = 0
   SSP1CON1 = 0b00110000;      // sspen = 1, SPI_CLK_DIV_4, CKP = 0
   // disable MMC/SD-card chipselect
   MMC_Disable();
   return(0);
}


...an optimized version of my sector_write code will follow soon Wink

Best wishes,
Daniel
metalm



Joined: 22 Mar 2007
Posts: 23
Location: Buenos Aires, Argentina

View user's profile Send private message

PostPosted: Wed Feb 13, 2008 7:49 am     Reply with quote

Helo, me again

What if you enable the READ_BLK_MISALIGN parameter? this permits crossing block boundaries and then there is no need to bufferize an entire block?

also, i use the "multiread" comand, for reading continuous block at the max speed possible
ColtFish



Joined: 04 Feb 2008
Posts: 3

View user's profile Send private message

PostPosted: Wed Feb 13, 2008 9:29 am     Reply with quote

Hey metalm,

thanks for your input!
I need the sd card access functions for a USB mass storage device so the block length is fixed.

The "multiread" command might be a nice idea! I'll try to implement this...

Thanks,
daniel
peter
Guest







PostPosted: Mon Mar 24, 2008 11:34 am     Reply with quote

Hi!

I read attentively the 3 pages of this thread and i have a question referred frequency clock SPI hardware SCK of MSSP module.

datasheet on 18F4550 said maximum speed SCK of 12Mhz (Fosc/4), but the crystal then must be 48Mhz???

if xtal=48Mhz then initializations MMC working above 400Khz (mode Fosc/64 more slow possible)

working xtal=24Mhz, i have 24Mhz / 64 = 375Khz necessary for init MMC, but speed clock up 24Mhz / 4 = 6Mhz maximum :(

i want working SCK to 12Mhz and using SPI hardware 18F4550

any idea?
ColtFish



Joined: 04 Feb 2008
Posts: 3

View user's profile Send private message

PostPosted: Mon Mar 24, 2008 12:34 pm     Reply with quote

All cards I'm working with initialize without any problems when I slow down the SPI Frequency to Fosc/64 = 750kHz.
Obviously it is not necessary to reduce SPI speed any further.
For the future, you should take a look at the PIC18FX7j50 device family which comes with a software switchable PLL which would enable you to reduce the SPI speed to <= 400kHz and the cpu could be running at 48MHz during normal operation.

best regards,
daniel
ckielstra



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

View user's profile Send private message

PostPosted: Mon Mar 24, 2008 3:21 pm     Reply with quote

It's very well possible the newer cards will accept a higher start-up clock rate. I haven't read the newer MMC specifications.

An alternative method to generate the slow SPI signal is to use a software based SPI driver for the start-up phase and then switch to the hardware SPI for fast transmissions.

In the v4.xxx compiler versions the software driver is easy to setup using the #USE SPI command, specifying the bitrate using the BAUD keyword.
In older compiler versions a software SPI driver example can be found in the CCS driver mmc_spi.c (included with the v3.249 compiler.)
peter



Joined: 25 Mar 2008
Posts: 2

View user's profile Send private message

PostPosted: Tue Mar 25, 2008 10:48 am     Reply with quote

ColtFish wrote:
All cards I'm working with initialize without any problems ...For the future, you should take a look at the PIC18FX7j50 device family which comes with a software switchable PLL which would enable you to reduce the SPI speed to <= 400kHz and the cpu could be running at 48MHz during normal operation.

best regards,
daniel


cpu running 48Mhz in fuses (XT/HS)PLL, but MSSP module configured SPI, SCK working directly with Crystal Frecuency

i thought that e.j. crystal 4Mhz i have 48Mhz using PLL and CPU microcontroller run this speed, but i read specification MSSP, it said SCLK working Fosc/4 maximun

then I would that using in Fosc a clock frecuency running to 48Mhz to have 12Mhz in SCK to increase speed in communication to MMC


ckielstra i will study your idea for when i change speed from init to stage read/write block. thanks you
Ttelmah
Guest







PostPosted: Tue Mar 25, 2008 11:19 am     Reply with quote

Designed to confuse you, but on these chips, 'Fosc', is _not_ always the external oscillator frequency. Look at the clock diagram. A series of clocks are generated. One for USB, one for the CPU, and one for the peripherals. This last, is 'Fosc' in MicroChip parlance, even if it does not come directly from the external oscillator. It is made (sort of) clear in several places, where Fosc/4, is used a a direct alternative to the instruction rate, and in the 'timing specification' table, where Fosc, is given as being the external oscillator frequency, with a big note at the bottom (paraphrased), that 'Tcy' (1/Fosc) is the external oscillator frequency, _except_ in PLL modes.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Tue Mar 25, 2008 9:39 pm     Reply with quote

ckielstra wrote:
It's very well possible the newer cards will accept a higher start-up clock rate. I haven't read the newer MMC specifications.
I just found out the new MMC specifications v4.3 is now available free of charge at http://www.mmca.org/compliance/buy_spec/.

According to chapter 12.7.1 the Identification Mode still has the slow clock speed of 400kHz, with a tolerance of +20kHz.
peter



Joined: 25 Mar 2008
Posts: 2

View user's profile Send private message

PostPosted: Sat May 17, 2008 1:43 pm     Reply with quote

Hi!

to picĀ“s series 18F with features switch clock "in hot", you can use OSCCON SFR to reducing speed Fosc durign mmc initializations.

code here:


Code:

mmc_init()
{
   // speed low to SCK=125Khz
   setup_oscillator(OSC_INTRC |OSC_8MHZ);
   // change system oscillator to INTRC = 8 MHz
   // IESO bit ignored     
   delay_cycles(10); // transition 7 cycles + extra
   while(!IOFS){}  // wait stabilization Fosc
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 | SPI_XMIT_L_TO_H);
   // spi for hardware, master, Fosc/64, clock idle high, falling edge
   ...
   output_high(chip_select);
   delay_ms(100); // power-up timer for MMC
   ...
   ...
   ...
   

   // increase speed SCK=12Mhz
   setup_oscillator(OSC_NORMAL);
   // change system Oscillator to primary oscillator = Xtal
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_XMIT_L_TO_H);
   // spi for hardware, master Fosc/4, clock idle high, falling edge
   ...
}

Code:
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, 3
Page 3 of 3

 
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