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

Thread: Check for SD card on t_3.6 or audio-shield

  1. #1

    Check for SD card on t_3.6 or audio-shield

    Hello,

    I have a little problem with my T_3.6 + audio-shield: I want to check if a sd-card is placed into the T_3.6 and, if not, than try to check if there is one installed on the audio-shield. But if I run the following code with a sd-card inside the T_3.6, it is not found:

    Code:
    #include <SPI.h>
    #include <SD.h>
    
    #define DEBUG 1
    
    // Teensy Audio Shield
    #define SDCARD_AUDIO_CS_PIN    10
    #define SDCARD_AUDIO_MOSI_PIN  7
    #define SDCARD_AUDIO_SCK_PIN   14
    #ifndef TEENSY4
    // Teensy 3.5 & 3.6 SD card
    #define SDCARD_CS_PIN    4 //BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  11
    #define SDCARD_SCK_PIN   13
    #else
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  11
    #define SDCARD_SCK_PIN   13
    #endif
    
    int8_t check_sd_cards(void)
    {
      int8_t ret = -1;
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
    
    #ifdef TEENSY4
    #ifdef DEBUG
      Serial.println(F("Checking audio-board SD (T_4.0)"));
    #endif
      if (SD.begin(SDCARD_CS_PIN))
      {
    #ifdef DEBUG
        Serial.println(F("Using SD-card on audio-board (T_4.0)"));
    #endif
        ret = SDCARD_CS_PIN;
      }
    #else // not TEENSY4
    #ifdef DEBUG
      Serial.println(F("Checking on-board SD (T_3.6)"));
    #endif
    
      if (SD.begin(SDCARD_CS_PIN))
      {
    #ifdef DEBUG
        Serial.print(F("Using SD-card on Teensy (T_3.6): "));
        Serial.println(SDCARD_CS_PIN, DEC);
    #endif
        ret = SDCARD_CS_PIN;
      }
      else
      {
        // SD Card from audio-board
    #ifdef DEBUG
        Serial.println(F("Checking audio-board SD (T_3.6)"));
    #endif
        SPI.setMOSI(SDCARD_AUDIO_MOSI_PIN);
        SPI.setSCK(SDCARD_AUDIO_SCK_PIN);
        if (SD.begin(SDCARD_AUDIO_CS_PIN))
        {
    #ifdef DEBUG
          Serial.println(F("Using SD-card on audio-board (T_3.6):"));
          Serial.println(SDCARD_AUDIO_CS_PIN, DEC);
    #endif
          ret = SDCARD_AUDIO_CS_PIN;
        }
      }
    #endif
    #ifdef DEBUG
      if (ret < 0)
        Serial.println(F("Cannot find SD card!"));
    #endif
    
      return (ret);
    }
    
    void setup() {
      delay(500); // needed for serial console
    
      int8_t sd = check_sd_cards();
    
      Serial.println(sd, DEC);
    }
    
    void loop() {
      ;
    }
    This results in:

    Checking on-board SD (T_3.6)
    Checking audio-board SD (T_3.6)
    Cannot find SD card!
    -1


    What am I doing wrong?

    Regards, Holger

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,398
    you didn't make BUILTI_SDCARD one of your selections? and BUILTIN_SDCARD is 254 which is a negative number for 8 bit int. here's my version of your sketch
    Code:
    #include <SPI.h>
    #include <SD.h>
    
    #define DEBUG 1
    
    // Teensy Audio Shield
    #define SDCARD_AUDIO_CS_PIN    10
    #define SDCARD_AUDIO_MOSI_PIN  7
    #define SDCARD_AUDIO_SCK_PIN   14
    #ifndef TEENSY4
    // Teensy 3.5 & 3.6 SD card
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  11
    #define SDCARD_SCK_PIN   13
    #else
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  11
    #define SDCARD_SCK_PIN   13
    #endif
    
    int16_t check_sd_cards(void)
    {
      int16_t ret = -1;
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
    
    #ifdef TEENSY4
    #ifdef DEBUG
      Serial.println(F("Checking audio-board SD (T_4.0)"));
    #endif
      if (SD.begin(SDCARD_CS_PIN))
      {
    #ifdef DEBUG
        Serial.println(F("Using SD-card on audio-board (T_4.0)"));
    #endif
        ret = SDCARD_CS_PIN;
      }
    #else // not TEENSY4
    #ifdef DEBUG
      Serial.println(F("Checking on-board SD (T_3.6)"));
    #endif
    
      if (SD.begin(SDCARD_CS_PIN))
      {
    #ifdef DEBUG
        Serial.print(F("Using SD-card on Teensy (T_3.6): "));
        Serial.println(SDCARD_CS_PIN, DEC);
    #endif
        ret = SDCARD_CS_PIN;
      }
      else
      {
        // SD Card from audio-board
    #ifdef DEBUG
        Serial.println(F("Checking audio-board SD (T_3.6)"));
    #endif
        SPI.setMOSI(SDCARD_AUDIO_MOSI_PIN);
        SPI.setSCK(SDCARD_AUDIO_SCK_PIN);
        if (SD.begin(SDCARD_AUDIO_CS_PIN))
        {
    #ifdef DEBUG
          Serial.println(F("Using SD-card on audio-board (T_3.6):"));
          Serial.println(SDCARD_AUDIO_CS_PIN, DEC);
    #endif
          ret = SDCARD_AUDIO_CS_PIN;
        }
      }
    #endif
    #ifdef DEBUG
      if (ret < 0)
        Serial.println(F("Cannot find SD card!"));
    #endif
    
      return (ret);
    }
    
    void setup() {
      while(!Serial);
      delay(500); // needed for serial console
    
      int16_t sd = check_sd_cards();
    
      Serial.println(sd, DEC);
    }
    
    void loop() {
      ;
    }

  3. #3

    Quote Originally Posted by manitou View Post
    you didn't make BUILTI_SDCARD one of your selections? and BUILTIN_SDCARD is 254 which is a negative number for 8 bit int. here's my version of your sketch
    Ahhhh! I thought I am getting a pin number when using BUILTIN_SDCARD and havn't checked this before. Many thanks!!!!

    Regards, Holger

Posting Permissions

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