Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Setting SPI clock for reading sd card

  1. #1

    Setting SPI clock for reading sd card

    I'm attempting to read an sd card attached to a Teensy 3.5 and also a Teensy 3.2. The Teensy 3.5 code works with no problem, but the Teensy 3.2 code fails.
    Code:
    #ifdef TEENSY_35	
    	
    	if (!(SD.begin(BUILTIN_SDCARD)))
    	{
    			Serial.println("Unable to access the SD card"); 
    			delay(200);
    	}
    #else // Teensy 3.2
    	{
    		if (!(SD.begin(PIN_SPI_CS)))  // 10
    	{
    			Serial.println("Unable to access the SD card"); 
    		  delay(200);
    	}
    #endif
    The sketch in this thread is able to read the card when I use the following:
    Code:
    if (!card.init(SPI_QUARTER_SPEED, chipSelect))
    What do I need to do to keep the Teensy 3.5 at full speed and also read the Teensy 3.2 card? Note: I'm only reading single lines from a text file on the card.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,514
    Looks like a problem with your hardware.
    Most common problem is a slow Arduino- breakout with levelshifters (don't use them) Or too long cables.

    Can you post a photo?

  3. #3
    I'm not sure what a photo will do. The Teensy 3.2 board is wired correctly. The Teensy_SDCARD_info sketch is able to access the card when the SPI clock speed is set to quarter speed. The card reader does have level shifters. How do I slow the SPI clock down enough to read the card attached to the Teensy 3.2 without slowing down the Teensy 3.5 card reads?

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,514
    Ok, I'd replace the board. It does not work with level shifters. They are too slow, and pointless.
    A photo would have shown problems with the wiring.

    Don't know what you mean with "without slowing down the 3.5" - are they connected somehow ????
    You alread have #defines in your program - just use them to set the speed, too? Hard to guess what you are doing.. Is the card really connected to BOTH Teensys?? This can not work.

  5. #5
    The same code runs on both Teensy 3.2s and Teensy 3.5s which are located on different boards. The Teensy 3.2 boards need the card reader, The Teensy 3.5 boards use the buildin card reader. How do I set the SPI clock speed for the Teensy 3.2? The Sd2Card library used in the Teensy_SDCARD_info sketch is not documented/supported. Will setting the speed using the Sd2Card SPI_QUARTER_SPEED also set the speed for SD.open calls?

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,514
    I'm out.. don't know what your problem is..

    "How do I set the SPI clock speed for the Teensy 3.2?" -

    The sketch in this thread is able to read the card when I use the following:
    Code:

    if (!card.init(SPI_QUARTER_SPEED, chipSelect))
    You answered your own question before(?!)
    And no, if you just just do this for T3.2 only it does not influence the 3.5...
    You know how #defines work? You use it in post #1...

    Good night. Too late here... obviously..

  7. #7
    Unfortunately, setting the SPI clock speed for the Sd2Card object doesn't set it for the SD object needed to read a specific text file. It does show the Teensy 3.2 is capable of communicating through the sd card reader when the SPI clock speed is lower. The question remains, how does one set the SPI clock speed for the SD object on the Teensy 3.2 without affecting the SPI clock speed on the Teensy 3.5.

  8. #8
    Senior Member
    Join Date
    Dec 2013
    Posts
    212
    On MS WIN 10 - zip Arduino and Teensyduino installation - in C:\Users\( >User name< )\Desktop\arduino-1.8.6\hardware\teensy\avr\libraries\SD\SD_t3.h, look for #define SD_SPI_SPEED SPISettings(25000000, MSBFIRST, SPI_MODE0). <---- 25 MHz -- line 54

  9. #9
    Senior Member
    Join Date
    Apr 2017
    Posts
    156
    What speed are you compiling your code to run? I've used the SD card boards (with level shifters) and if compiled above 72 MHZ, the SD card could not be recognized. At 72 MHZ, the card reader worked fine.

  10. #10

  11. #11
    Lowering the SPI clock speed in the SD_t3.h library to 23950000 was successful in getting the card reader to work when compiled for 96 MHz. It failed at 24000000 when compiled for 96 MHz and 48 MHz, but succeeded when compiled for 72 Mhz.
    In order to have a little safety margin, I'll set it to 23000000. This is only an 8% speed loss, so it probably isn't worth trying to have separate speeds for the Teensy 3.2s and 3.5s.

    It would be really nice to have this setting exposed publicly.

    Thanks everyone for the help.

    p.s. It also worked at 25000000 when compiled at 96 Mhz.

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,703
    Quote Originally Posted by Elf View Post
    In order to have a little safety margin, I'll set it to 23000000.
    The hardware can't create 23 MHz SPI clock. When you use 23000000, the next lower clock will be used.

    On Teensy 3.x, the SPI clocks are an integer division of F_BUS, where 2 is the smallest possible integer. When Teensy 3.2 runs at 72 MHz, F_BUS is 36 MHz. So no matter 23000000 or 25000000, the fastest possible speed for the SPI clock is only 18 MHz.

    When Teensy 3.2 runs at 96 MHz, F_BUS is 48 MHz. The fastest possible SPI clock is 24 MHz. If you use 23000000, the next slower clock will be F_BUS / 3, which is 16 MHz.

    This is the way SPI settings are designed to work. The number you give in the code is the maximum. The SPI hardware is configured with the fastest speed with is equal or less than the maximum you choose. In cases where you choose a number like 23000000, the actual speed will often be much less than 23 MHz.


    Any in case this isn't obvious, that cheap SD adaptor from Aliexpress is almost certainly causing these speed problems. When connected directly, without the unnecessary level shifting, all SD cards do work at 24 MHz.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •