Arduino Forum

Using Arduino => Storage => Topic started by: scswift on Aug 27, 2016, 11:37 pm

Title: SDFat - What speed will be used on a Zero?
Post by: scswift on Aug 27, 2016, 11:37 pm
I've been looking through the source, but it's extremely hard to follow with all the different architectures and ways of doing SPI in it.

I'm trying to determine if the library defaults to 4MHz on the Zero, or if it runs at the maximum 12MHz, and if it is running at the slower speed, how to set it to use a divisor of 4 to get 12MHz.  I see a divisor parameter specified in a number of places but it's not clear what it's being set to.

Also this code in sdspi.h looks weird:
Code: [Select]

  void beginTransaction(uint8_t divisor) {
    SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
#ifndef SPI_CLOCK_DIV128

It appears that when you use transactions it sets the SPI speed to 4MHz.  But then it sets the SPI clock divider directly afterward to an unknown divisor.  Which I don't know is allowed when using transactions, and I don't know what it's being set to either.
Title: Re: SDFat - What speed will be used on a Zero?
Post by: fat16lib on Aug 28, 2016, 02:31 am
For the current version of SdFat,  Zero will use the standard Arduino library and the default divisor will be two.

It's likely the library will use 12 MHz if that is the fastest SPI speed for Zero.

I have decided to use SPISetting in future versions of SdFat.  The current SdFat-beta ( uses SPISettings.  The default speed is 50 MHz so the SPI library should choose the fastest supported speed less than or equal to 50 MHz.

Here is an example of the new begin call.
Code: [Select]

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
Title: Re: SDFat - What speed will be used on a Zero?
Post by: scswift on Aug 28, 2016, 11:24 pm

I tried the beta with 1.6.8 which I'm still running, and it works fine.  I don't actually know that it is running at the correct speed, but I did discover that the SPI lib has a define in it to set the maximum divider and as I am using the SAMD21E17 and not the SAMD21G18 that wasn't being set to 4 like it should but 2 instead. 

The weird thing is, it was working before as well even though it seems like it shouldn't have been. Though I am getting different behavior now when playing 44KHz files and I suppose that could be a result of bad data before.  Perhaps running at too high a speed isn't as much of a problem because the SPI library is not fully optimized.  But I notice you have a bunch of SPI implementations in there so I don't know if you are using the SD library or not.  And even if you have defined it to use it with the Zero, my Tau uses the SAMD21E17 so maybe it isn't triggering the right define to get the fast version?
Title: Re: SDFat - What speed will be used on a Zero?
Post by: fat16lib on Aug 29, 2016, 02:24 am
There are no divisors in the latest SdFat-beta.  Speed is set with SPISettings.

All SAM boards except Due use the standard library for all versions of SdFat.
Title: Re: SDFat - What speed will be used on a Zero?
Post by: rickyelqasem on May 18, 2018, 02:37 pm
Can also get some guidance on this? If you run my code on a Mega with the above parameters (50) it works. If I run the same code on Zero it wont read the SD card. For a Zero what should I use?

  if (!sd.begin(SD_CS_PIN, SD_SCK_HZ(F_CPU/4))) {
    SerialUSB.println("unable to open sd");

Ive tried:SD_SCK_MHZ(50) and SD_SCK_HZ(F_CPU/3) also...