 |
 |
| View previous topic :: View next topic |
| Author |
Message |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jan 31, 2008 7:36 pm |
|
|
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
|
|
Posted: Fri Feb 01, 2008 9:12 am |
|
|
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
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
|
|
Posted: Fri Feb 01, 2008 9:32 am |
|
|
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
|
|
Posted: Fri Feb 01, 2008 9:58 am |
|
|
| 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
|
|
Posted: Fri Feb 01, 2008 10:39 am |
|
|
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  |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Feb 01, 2008 8:20 pm |
|
|
| 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
|
|
Posted: Fri Feb 01, 2008 8:46 pm |
|
|
| 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
|
|
Posted: Fri Feb 01, 2008 9:02 pm |
|
|
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
|
|
Posted: Sat Feb 02, 2008 7:58 am |
|
|
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
|
|
Posted: Sat Feb 02, 2008 8:12 am |
|
|
| 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
|
|
Posted: Sat Feb 02, 2008 10:00 am |
|
|
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
|
|
Posted: Sat Feb 02, 2008 11:15 am |
|
|
This is becoming quite a 'fun' thread. How to nibble away yet another clock cycle!...
What speed are you actually getting now in the KB/sec stakes?.
Best Wishes |
|
 |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Sun Feb 03, 2008 3:37 am |
|
|
| 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
|
|
Posted: Sun Feb 03, 2008 7:06 am |
|
|
Hi DDDaniel,
Would like to share Your Code for Init the SD Card ?
I would appreciate a PM
best regards
Andreas |
|
 |
DDDDaniel Guest
|
|
Posted: Sun Feb 03, 2008 12:56 pm |
|
|
| Ttelmah wrote: | This is becoming quite a 'fun' thread. How to nibble away yet another clock cycle!...  |
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
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 |
|
 |
|
|
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
|