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

Thread: Connecting the Adafruit micro SD breakout board to Teensy 3.6

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    4

    Connecting the Adafruit micro SD breakout board to Teensy 3.6

    I need to be able to locate an SD card with ejection capability near the edge of a 3D printed housing and thus cannot use the one on the Teensy 3.6. We also have multiple devices connected to the Teensy and are using a number of the pins that would naturally be optimal for an SD card. We tried the following per the Adafruit Micro-SD card (that we are using) configuration:
    5V - 5V
    GND - GND
    CLK - Pin 20 SCK1
    DO - Pin 5 MISO1
    DI - Pin 21 MOSI1
    CS - Pin 31 CS0

    After wiring up the card and confirming that the SD card is formatted for FAT32, I note that the LED flashes when I run the slightly customized 'Cardinfo' example - then stops and prints the error,

    "Initializing SD card...initialization failed. Things to check:
    * is a card inserted?
    * is your wiring correct?
    * did you change the chipSelect pin to match your shield or module?"


    Below is the customized software.

    Would appreciate some assistance.

    /*
    SD card test

    This example shows how use the utility libraries on which the'
    SD library is based in order to get info about your SD card.
    Very useful for testing a card when you're not sure whether its working or not.

    The circuit:
    * SD card attached to SPI bus as follows:
    ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila, pin 7 on Teensy with audio board
    ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
    ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila, pin 14 on Teensy with audio board
    ** CS - depends on your SD card shield or module - pin 10 on Teensy with audio board
    Pin 4 used here for consistency with other Arduino examples


    created 28 Mar 2011
    by Limor Fried
    modified 9 Apr 2012
    by Tom Igoe
    */
    // include the SD library:
    #include <SD.h>
    #include <SPI.h>

    // set up variables using the SD utility library functions:
    Sd2Card card;
    SdVolume volume;
    SdFile root;

    // change this to match your SD shield or module;
    // Arduino Ethernet shield: pin 4
    // Adafruit SD shields and modules: pin 10
    // Sparkfun SD shield: pin 8
    // Teensy audio board: pin 10
    // Teensy 3.5 & 3.6 on-board: BUILTIN_SDCARD
    // Wiz820+SD board: pin 4
    // Teensy 2.0: pin 0
    // Teensy++ 2.0: pin 20
    const int chipSelect = 31;

    void setup()
    {
    //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
    SPI.setMOSI(21); //
    SPI.setMISO(5); //
    SPI.setSCK(20); //
    SPI.setCS(31); //

    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }


    Serial.print("\nInitializing SD card...");


    // we'll use the initialization code from the utility libraries
    // since we're just testing if the card is working!
    if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
    } else {
    Serial.println("Wiring is correct and a card is present.");
    }

    // print the type of card
    Serial.print("\nCard type: ");
    switch(card.type()) {
    case SD_CARD_TYPE_SD1:
    Serial.println("SD1");
    break;
    case SD_CARD_TYPE_SD2:
    Serial.println("SD2");
    break;
    case SD_CARD_TYPE_SDHC:
    Serial.println("SDHC");
    break;
    default:
    Serial.println("Unknown");
    }

    // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
    }


    // print the type and size of the first FAT-type volume
    uint32_t volumesize;
    Serial.print("\nVolume type is FAT");
    Serial.println(volume.fatType(), DEC);
    Serial.println();

    volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
    volumesize *= volume.clusterCount(); // we'll have a lot of clusters
    if (volumesize < 8388608ul) {
    Serial.print("Volume size (bytes): ");
    Serial.println(volumesize * 512); // SD card blocks are always 512 bytes
    }
    Serial.print("Volume size (Kbytes): ");
    volumesize /= 2;
    Serial.println(volumesize);
    Serial.print("Volume size (Mbytes): ");
    volumesize /= 1024;
    Serial.println(volumesize);


    Serial.println("\nFiles found on the card (name, date and size in bytes): ");
    root.openRoot(volume);

    // list all files in the card with date and size
    root.ls(LS_R | LS_DATE | LS_SIZE);
    }


    void loop(void) {

    }

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,929
    More work required. I think the teensy SD library is hard-wired to use SPI0. So your SPI.setMOSI() doesn't really change the underlying SPI controller. You'll need to hack the SD library to use SPI1

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    4
    I appreciate the suggestion and have tried to hack: SD, SPI, Sd2Card all to no avail. I both hard coded the pins I need for CS, MOSI, etc and updated their assignment to what I needed: For example, this is my edit in SPI at begin() just prior to them being used and setup.

    SS = 31;
    SCK = 20 ;
    MOSI = 21 ;
    MISO = 5 ;


    These were the changes I applied to S2Card

    // SPI pin definitions
    //
    // hardware pin defs
    /**
    * SD Chip Select pin
    *
    * Warning if this pin is redefined the hardware SS will pin will be enabled
    * as an output by init(). An avr processor will not function as an SPI
    * master unless SS is set to output mode.
    */
    /** The default chip select pin for the SD card is SS. */
    uint8_t const SD_CHIP_SELECT_PIN = 31; //AB:05-18-19 was SS_PIN changed to 31
    // The following three pins must not be redefined for hardware SPI.
    /** SPI Master Out Slave In pin */
    uint8_t const SPI_MOSI_PIN = 21; //AB:05-18-19 was MOSI_PIN changed to 21
    /** SPI Master In Slave Out pin */
    uint8_t const SPI_MISO_PIN = 5; //AB:05-18-19 was MISO_PIN changed to 5
    /** SPI Clock pin */
    uint8_t const SPI_SCK_PIN = 20; //AB:05-18-19 was SCK_PIN changed to 20
    /** optimize loops for hardware SPI */
    #define OPTIMIZE_HARDWARE_SPI

    What I still notice is the Teensy on board LED flashes - implying what I am thinking is the on board SD card is being accessed and not the peripheral board.

    Do you have any other more specific suggestions as to where I should be looking to hack?

    Thank you.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,929
    This is a rather difficult hack to attempt to the SD library, even for experts. You really should use SPI and abandon attempting to use SPI1 or SPI2.

    Or you can use the built in SD socket on Teensy 3.6. That is easy. Just use SD.begin(BUILTIN_SDCARD);

    Also, something to consider is whether the SD adaptor you're using as a level shifter. Many of them are too slow for the 24 MHz SPI clock Teensy uses to access the SD card.

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    4
    I even noticed further down in the S2Card.h code there was a reference to 'BUILTIN_SDCARD' defined as 254 - so that was changed to my CS pin 31. After compiling - the code still routes to the on board SD card!

    Thanks

  6. #6
    Junior Member
    Join Date
    May 2019
    Posts
    4
    Hi Paul: Thank you very much for making the suggestion - and also in noting that it is not a simple hack! I cannot use the built in SD socket as I need it to be able to self eject and also close to the edge of my PCB. As I need the micro-USB port to be accessible, I forfeited the on board SD card. Yesterday I even questioned if the SD breakout board worked, so I stripped the balance of wiring from my breadboard (I am working on a display at the same time) and connected the SD breakout board to:

    CLK - SCK0 - Pin 13
    DO - MISO0 - Pin 12
    DI - MOSI0 - Pin 11
    CS - CS0 - Pin 10

    and confirmed it was operational. Thanks to the flexibility of the Teensy 3.6 pins, I moved what I needed for the SD card that conflicted with the display and both were operational. I can now place the SD breakout near the edge of my PCB.

    Nice to know there was a solution.

    Have a good weekend.

Posting Permissions

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