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

Status
Not open for further replies.

C0d3man

Well-known member
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
 
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    [B][COLOR="#FF0000"]BUILTIN_SDCARD[/COLOR][/B]
#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

[B][COLOR="#FF0000"]int16_t [/COLOR][/B]check_sd_cards(void)
{
 [B][COLOR="#FF0000"] int16_t [/COLOR][/B]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() {
  [B][COLOR="#FF0000"]while(!Serial);[/COLOR][/B]
  delay(500); // needed for serial console

  [B][COLOR="#FF0000"]int16_t[/COLOR][/B] sd = check_sd_cards();

  Serial.println(sd, DEC);
}

void loop() {
  ;
}
 
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
 
Status
Not open for further replies.
Back
Top