stevenmt / wavehc

Automatically exported from code.google.com/p/wavehc
0 stars 0 forks source link

Card init SPI clock rate causing issues on reset with some cards (with solution) #1

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Plug in Arduino, upload sketch - works
2. Press reset on shield - playback very choppy/distorted
3. Press reset on shield again - serial output shows ""No valid FAT partition!"
4. Press reset on shield 3rd time - serial output shows SD I/O error: 6
5. Powering off Arduino or reseating the SD card resets behavior to step 1

What version of the product are you using? On what operating system?
WaveShield 1.1, wavehc20101009, Arduino Uno, OS X. Included SD card, Transcend 
MicroSD (w/adapter) 2GB.

Please provide any additional information below.
I searched for issues related to the ACMD41 stage of SD card init, lots of hits 
but what set this apart was that it worked at first, then degraded.

I noticed that people described the spec requiring <400kHz SPI clock during the 
card init phase.  The current wavehc library sets the clock to 125kHz 
(f_osc/128), which is just under 1/3rd the max recommended init clock rate.  I 
tried the highest available option below 400kHz, 250kHz (f_osc/64), and 
success! I can now reset as many times as I want and no issues.

I changed the following lines in SdReader.cpp SdReader::init
  // Enable SPI, Master, clock rate f_osc/128
  SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
To:
  // Enable SPI, Master, clock rate f_osc/64
  SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1);

So, for at least this model MicroSD card, it appears 125kHz is too low a clock 
for the card's liking during init.

Original issue reported on code.google.com by andycarr@gmail.com on 7 May 2011 at 6:18

GoogleCodeExporter commented 9 years ago
The window for SPI initialization is 100 - 400 kHz.  

I choose 125 kHz because I had several cases where cards would not reinitialize 
at 250 kHz.

Looks like cards can fail to meet the spec at both 125 and 250 kHz.

Unfortunately the card doesn't see reset so reinitialization of cards has been 
a problem.  Only cycling power truly resets the SD card.

I have added a #define option in SdReader.h to select 250 kHz and will put a 
note in the readme.txt file.

Original comment by Bill.Gre...@gmail.com on 7 May 2011 at 11:16