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  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



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

View user's profile Send private message

PostPosted: Thu Jan 31, 2008 7:36 pm     Reply with quote

I implemented Neutone's algorithm and added two small fixes. Result is a reduction in the 512-byte read loop from 10,250 cycles to 5,772 cycles. A 44% speed improvement on this part.

Another 100 instruction cycles were shaved off in sending the Block Read command by removing the cmd-array and unrolling the loop.

For easier reading I renamed all variable types to be int8, int16, etc. These are all default unsigned so I could omit the 'unsigned' keyword.

Here is the new version:
Code:
int8 mmc_read_sector (int32 addr, int8 *Buffer)
{   
  int8 a;

  // chip select MMC/SD-card
  output_low(CS_CARD);

  // send 8 clock pulses
  WRITE_SSP(0xFF);

  // Convert block number to a 32-bit start address.
  addr = addr * 512;   // The compiler is smart enough to implement this with a shift command.

  WAIT_FOR_SSP();  // This is the wait for the previous WRITE_SSP, allows to do some calculations in between

  // Send block read command for given start address
  WRITE_SSP(0x51);
  WAIT_FOR_SSP();
  WRITE_SSP( make8(addr,3) );
  WAIT_FOR_SSP();
  WRITE_SSP( make8(addr,2) );
  WAIT_FOR_SSP();
  WRITE_SSP( make8(addr,1) );
  WAIT_FOR_SSP();
  WRITE_SSP(0x00);
  WAIT_FOR_SSP();
  WRITE_SSP(0xFF);
  WAIT_FOR_SSP();

  // Wait for a valid response from the MMC/SD-card
  do
  {
    WRITE_SSP(0xFF);
    WAIT_FOR_SSP();
  }
  while(READ_SSP() == 0xFF);

  // Wait for Start Byte (FEh/Start Byte)
  do
  {
    WRITE_SSP(0xFF);
    WAIT_FOR_SSP();
  }
  while(READ_SSP() != 0xFE);


  // Read Sector (usually 512 Bytes) from MMC/SD-card
  FSR0 = Buffer;
  WRITE_SSP(0xFF);
  Delay_Cycles(1);
  for (a=128; a>0; a--)
  {
    Delay_Cycles(3);
    POSTINC0 = READ_SSP();
    WRITE_SSP(0xFF);
    Delay_Cycles(7);
    POSTINC0 = READ_SSP();
    WRITE_SSP(0xFF);
    Delay_Cycles(7);
    POSTINC0 = READ_SSP();
    WRITE_SSP(0xFF);
    Delay_Cycles(7);
    POSTINC0 = READ_SSP();
    WRITE_SSP(0xFF);
  }


  // Read CRC-Bytes
  // WRITE_SSP(0xFF);   // Do not send, the previous while-loop did already send one extra 0xFF
  WAIT_FOR_SSP();   
  WRITE_SSP(0xFF);
  WAIT_FOR_SSP();   // let's try to skip the wait cycle here

  // disable MMC/SD-card
  output_high(CS_CARD);

  return 0;
}


I'm curious to hear about the new speed readings.
DDDDaniel
Guest







PostPosted: Fri Feb 01, 2008 9:12 am     Reply with quote

Hey everybody,

thanks for your help again!
I spent several hours now figuring out, why the code as proposed by ckielstra an Neutone does not work on my hardware.

For some strange reason it takes more than 8 clocks to send a byte over the spi!
Take a look at the following code:

Code:
   
   FSR0 = Buffer;
   for (b=0;b<128;b++)
   {
     WRITE_SSP(0xFF);
     Delay_Cycles(10);
     POSTINC0 = READ_SSP();

     WRITE_SSP(0xFF);
     Delay_Cycles(10);
     POSTINC0 = READ_SSP();

     WRITE_SSP(0xFF);
     Delay_Cycles(10);
     POSTINC0 = READ_SSP();

     WRITE_SSP(0xFF);
     Delay_Cycles(10);
     POSTINC0 = READ_SSP();
   }


Using the above code I can achieve a transfere rate of about 480kB/s. That's not too bad Smile
But as soon as I decrease the delay_cycle value, the data gets corrupted.
So what's going on here?
It seems to me as if it really takes more than 8 cycles to send a byte over the spi... I checked the datasheet but I could not find a precise value.

Another reason could be a bug in the delay cycles code, but it seems okay to me:
Code:

....................      Delay_Cycles(10);
0A5AC:  MOVLW  03
0A5AE:  MOVWF  00
0A5B0:  DECFSZ 00,F
0A5B2:  BRA    A5B0


best regards,
Daniel
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 9:32 am     Reply with quote

It may have to do with the SPI mode you use. Still faster with delays than checking the GO_DONE bit though.

You can squeez a bit more by making this change
Code:
....................    WRITE_SSP(0xFF);
0A3C8:  MOVLW  FF
0A3CA:  MOVWF  FC9

Code:
Int8 One_Mask=0xFF;
WRITE_SSP(One_Mask);
DDDDaniel
Guest







PostPosted: Fri Feb 01, 2008 9:58 am     Reply with quote

Neutone wrote:
It may have to do with the SPI mode you use...


That's my setup:
Code:

// cke = 1
SSP1STAT = 0b01000000;
// sspen = 1, SPI_CLK_DIV_4, CKP = 0
SSP1CON1 = 0b00100000;
DDDDaniel
Guest







PostPosted: Fri Feb 01, 2008 10:39 am     Reply with quote

And another issue:


This piece of code for sending the command sequence works...
Code:

   // Setup Command sequence for Block Read
   unsigned int cmd[] = {0x51,0x00,0x00,0x00,0x00,0xFF};
   unsigned int res,b,mask = 0xFF;   
   long a;

   // insert address bytes into command sequence 
   addr = addr << 9; //addr = addr * 512
   cmd[1] = make8(addr,3);
   cmd[2] = make8(addr,2);
   cmd[3] = make8(addr,1);

   // chip select MMC/SD-card
   output_low(CS_CARD);

   // send 8 clock pulses
   WRITE_SSP(0xFF);
   WAIT_FOR_SSP();

   for (res = 0;res<0x06;res++)
   {
      WRITE_SSP(cmd[res]);
      WAIT_FOR_SSP();
   }



...but with this (improved) version as proposed by ckielstra, communication locks up:

Code:

   // Setup Command sequence for Block Read
   unsigned int res,b,mask = 0xFF;   
   long a;

   // insert address bytes into command sequence 
   addr = addr << 9; //addr = addr * 512

   // chip select MMC/SD-card
   output_low(CS_CARD);

   // send 8 clock pulses
   WRITE_SSP(0xFF);
   WAIT_FOR_SSP();


   WRITE_SSP(0x51);
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,3));
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,2));
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,1));
   WAIT_FOR_SSP();
   WRITE_SSP(0x00);
   WAIT_FOR_SSP();
   WRITE_SSP(0xFF);
   WAIT_FOR_SSP();



I'm confused Sad
ckielstra



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

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 8:20 pm     Reply with quote

Neutone wrote:
You can squeez a bit more by making this change
Code:
....................    WRITE_SSP(0xFF);
0A3C8:  MOVLW  FF
0A3CA:  MOVWF  FC9

Code:
Int8 One_Mask=0xFF;
WRITE_SSP(One_Mask);
this compiles as
Code:
.................... WRITE_SSP(One_Mask);
001AC:  MOVFF  427,FC9
1 instruction instead of 2... Seems faster but the MOVFF instruction takes 2 instruction cycles and 4 bytes of memory. Result is same speed and memory usage as the original code at the expense of an extra RAM location.
ckielstra



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

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 8:46 pm     Reply with quote

DDDDaniel wrote:
...but with this (improved) version as proposed by ckielstra, communication locks up:

Code:

   // Setup Command sequence for Block Read
   unsigned int res,b,mask = 0xFF;   
   long a;

   // insert address bytes into command sequence 
   addr = addr << 9; //addr = addr * 512

   // chip select MMC/SD-card
   output_low(CS_CARD);

   // send 8 clock pulses
   WRITE_SSP(0xFF);
   WAIT_FOR_SSP();


   WRITE_SSP(0x51);
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,3));
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,2));
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,1));
   WAIT_FOR_SSP();
   WRITE_SSP(0x00);
   WAIT_FOR_SSP();
   WRITE_SSP(0xFF);
   WAIT_FOR_SSP();



To me the code looks equal in both cases, except for some timing improvements. If you follow the MMC specifications to the letter you should check the card being ready before sending a new command:
Code:

   // Setup Command sequence for Block Read
   unsigned int mask = 0xFF;   
   long a;

   // insert address bytes into command sequence 
   addr = addr << 9; //addr = addr * 512

   // chip select MMC/SD-card
   output_low(CS_CARD);

   // send minimum of 8 clock pulses between commands
   WRITE_SSP(0xFF);
   WAIT_FOR_SSP();

   // check for the card being in ready state.
   do
   {
     WRITE_SSP(0xFF);
     WAIT_FOR_SSP();
   } while(READ_SSP() != 0xFF);

   WRITE_SSP(0x51);
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,3));
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,2));
   WAIT_FOR_SSP();
   WRITE_SSP(make8(addr,1));
   WAIT_FOR_SSP();
   WRITE_SSP(0x00);
   WAIT_FOR_SSP();
   WRITE_SSP(0xFF);   // Note: CRC is not used, writing a 0 instead will save 1 instruction.
   WAIT_FOR_SSP();
ckielstra



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

View user's profile Send private message

PostPosted: Fri Feb 01, 2008 9:02 pm     Reply with quote

One more possible optimization:
Your 512 byte read loop has not the exact same structure as the one posted by me and Neutone. By shuffling around the write SPI command you can save a few cycles from the first delay_cycles() call.
Ttelmah
Guest







PostPosted: Sat Feb 02, 2008 7:58 am     Reply with quote

I'd suspect the comment about SPI mode is right.
The 'timing diagrams' from Microchip, don't (unfortunately), go 'down' to the individual cycle clock basis, but some things can be found from other parts of the data sheets. Generally, data 'loads' only occur on the last clock of the instruction. Reads occur on the first clock of the instruction. The actual transfer from the SPI shift register,to the holding register (which is what sets the BF bit), occurs on the _next_ instruction Q4 cycle, after the last SPI falling clock edge. The diagrams do show that on some SPI modes, the first clock, may be nearly one cycle of the SPI clock 'late'. So my guess would be:
Data loaded.
In selected SPI mode, clocking begins on the _next_ instruction
Eight instruction times to clock the data
half instruction time to get to next falling edge.
Data readable _one instruction latter_.

This would then mean that ten clock cycles would be needed, in some modes.

Best Wishes
ckielstra



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

View user's profile Send private message

PostPosted: Sat Feb 02, 2008 8:12 am     Reply with quote

Quote:
This would then mean that ten clock cycles would be needed, in some modes.
Would be interesting to test. MMC cards may be operated in either SPI mode 0 or 3.
Daniel tested in SPI mode 0 (CKP = 0, CKE = 1), maybe he can do the test again in SPI mode 3 (CKP = 1, CKE = 0) ?
DDDDaniel
Guest







PostPosted: Sat Feb 02, 2008 10:00 am     Reply with quote

Hey,

I just tried to operate the sd-card in SPI mode 3 as proposed.
This way I can reduce the number of cycles to delay to 9, what gives me another speed boost!

Thanks again!
Ttelmah
Guest







PostPosted: Sat Feb 02, 2008 11:15 am     Reply with quote

This is becoming quite a 'fun' thread. How to nibble away yet another clock cycle!... Smile
What speed are you actually getting now in the KB/sec stakes?.

Best Wishes
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Sun Feb 03, 2008 3:37 am     Reply with quote

ckielstra wrote:
Neutone wrote:
You can squeez a bit more by making this change
Code:
....................    WRITE_SSP(0xFF);
0A3C8:  MOVLW  FF
0A3CA:  MOVWF  FC9

Code:
Int8 One_Mask=0xFF;
WRITE_SSP(One_Mask);
this compiles as
Code:
.................... WRITE_SSP(One_Mask);
001AC:  MOVFF  427,FC9
1 instruction instead of 2... Seems faster but the MOVFF instruction takes 2 instruction cycles and 4 bytes of memory. Result is same speed and memory usage as the original code at the expense of an extra RAM location.


I never noticed that.
Andreas



Joined: 25 Oct 2004
Posts: 136

View user's profile Send private message

PostPosted: Sun Feb 03, 2008 7:06 am     Reply with quote

Hi DDDaniel,

Would like to share Your Code for Init the SD Card ?

I would appreciate a PM
best regards

Andreas
DDDDaniel
Guest







PostPosted: Sun Feb 03, 2008 12:56 pm     Reply with quote

Ttelmah wrote:
This is becoming quite a 'fun' thread. How to nibble away yet another clock cycle!... Smile


I'm pleased to read that you're having fun...
...because... if you like we can play the same game with my "write_sector"-code Laughing

The current sector-read-speed is almost 540 kB per second now!

Thanks a lot everybody!


@Andreas:
here's my init-code:

[code removed. new code posted below]

best wishes,
daniel
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  Next
Page 2 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