xmega128a3 + SD card over SPI - write speed issue

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello! I have a project based on xmega128a3 that has SD card connected over SPI. Everything seems to be working ok according to SD Association's simplified spec. I get all the correct responses. But block write times are way too long - 2 to 6ms, and about 1% of blocks takes 200..300ms to write. Setting blocks to be pre-erased does not seem to help. I have tried several SD and SDHC cards, they all work this way. Yet in SD USB adapter they show 5..6MB/s write speed. Of course adapter uses SD bus instead of SPI but I believe write times do no depend on interface mode.
What could be the problem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

They DO depend on the mode. SD is at least 4X faster for a given clk. speed, for example. I've NEVER heard of anyone getting 5-6 MB speeds using SPI mode ( not possible ), so only SD mode can do it. Being stuck with SPI mode your write times are about in the range you gave. Well... how big a block are you using ? How efficient is your SPI write code ? I don't know what's goin' on with 200ms - 300ms write times !

What's the MCU clk speed ?

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Thu. Apr 7, 2011 - 11:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, indianajones11!
The write time i am speaking about is the time the card is in the busy state (holds DataOut line low) AFTER the block is sent to the card and 'data accepted' token is received from the card. Therefore, it is internal write time that does not depend on MCU's speed, bus width or my code's efficiency.
The block size is 512 bytes - the only size for SDHC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When coding for the Mega chips, I found that the write time DID depend on the SPI clock speed! If I increased the SPI clock speed, the "busy time" decreased.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have similar problems using an MSP430 microcontroller. When writing logs to my Kingston SDHC cards (512B blocks) at 8MHz over SPI I incidentally (2-3 times a minute) have a 300ms write cycle.

edit:
I have solved the problem by making a cyclic sector buffer, instead of losing data or generating timing issues I now keep old data in the buffer and attempt to write that buffer (my write function will return if the SD card is still busy) until it succeeds. Of course this wouldn't work for continuous data, but I write log entries with an interval (once every 10 milliseconds).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The issue has been solved.
IF the whole AU (allocation unit) has been pre-erased then the block write time inside this AU is predictable (1..2ms for single block writes).
The problem is, AU's size on SDHC cards is 4MB or more so we get predictable times for large volumes of data only. If we do read-modify-write of a single block we still can get a long time response once in a while. I believe it's due to card's internal controller allocating blocks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

AU's size on SDHC cards is 4MB

No they aren't. Cluster are typically going to be 16K or 32K. That is 32 or 64 sectors. If they really were 4MB then each time you wrote a 1 byte file you'd waste 4,194,303 bytes!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AU is NOT a cluster.
See SD Physical Layer Simplified Specification 4.13.1.1
AU size is a field in SD Status structure that you get with ACMD13.

clawson wrote:
Quote:

AU's size on SDHC cards is 4MB

No they aren't. Cluster are typically going to be 16K or 32K. That is 32 or 64 sectors. If they really were 4MB then each time you wrote a 1 byte file you'd waste 4,194,303 bytes!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought you meant AU as used in the Microsoft sense (see fatgen103.doc). Microsoft ue the term "allocation unit" rather than "cluster" when talking about the entities pointed to by the FAT chain.