SD Card on SPI1 - T3.5

Status
Not open for further replies.

kammateo

Member
I have got SPI0 working without problems when I can read and write on the SD Card. Now I want to use SP1 and I can't initialize the SD Card.
I'm using the alternate pins on the T3.5.

What am I missing? Do you have any ideas?

Thank you! :)

Code:
#include <SPI.h>
#include "SdFat.h"
SdFat SD;

#define SD_CARD_CS_PIN       31
#define SD_CARD_MOSI_PIN    0
#define SD_CARD_SCK_PIN     32
#define SD_CARD_MISO_PIN    1

File myFile;

void setup() {
   Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  pinMode(SD_CARD_CS_PIN, OUTPUT);

  SPI1.setMOSI(SD_CARD_MOSI_PIN);
  SPI1.setMISO(SD_CARD_MISO_PIN);
  SPI1.setSCK(SD_CARD_SCK_PIN);
  SPI1.begin();

  Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CARD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}
 
I remember there was an issue with SPI1 on the T3.5 which is slightly different from the other hardware SPIs and a patch should be available in td 1.37 beta which is to be published today or tomorrow.
 
the teensy SD (SPI) library is hardwired for SPI0. you would need to modify the library to support SPI1
https://github.com/PaulStoffregen/SD
Ooops, i didn't see that you were using SdFat,but it is hardwired for SPI0 as well.
https://github.com/greiman/SdFat

of course, the T3.5 has a builtin microSD card that does not use SPI.

Manitou thank you for your reply. Is it possible to use SPI0 with an external MicroSD and SPI1 with a Display TFT ?
I'm trying to make it possible but I can't.
Thank you
 
Simple answer is yes you can get something the display to work on SPI1, however currently it will take some work.

That is all of these libraries are hard coded to work with SPI object... and Not with SPI1... Hopefully soon will be able to make standard versions that work on mutliple SPI busses.

I have a version that I believe is working, but it is using my SPIN library to allow multiple different SPI busses.
My version of Paul's T3 code with this is up at: https://github.com/KurtE/ST7735_t3/tree/SPIN-Version

Also up on my github is the SPIN library: https://github.com/KurtE/SPIN
 
I believe is working

Thank you so much KurtE. I tried and it works. I can use the TFT on SPI1 and the external SD on SPI.
This is final result and may help someone:

Code:
#include <Adafruit_GFX.h>    // Core graphics library
#include <ST7735_t3.h>       // Hardware-specific library
#include <SPI.h>

#include "SdFat.h"
SdFat SD;

#define SD_CS_PIN 10
File myFile;

#define TFT_SCLK  32  // SCLK can also use pin 14
#define TFT_MOSI  0   // MOSI can also use pin 7
#define TFT_CS    6   // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define TFT_DC    2   //  but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define TFT_RST   8   // RST can use any pin
#define SD_CS     4   // CS for SD card, can use any pin

ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

void setup() {

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


  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3,4 .");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  pinMode(SD_CS, INPUT_PULLUP);  // don't touch the SD card
  Serial.print("Initializing TFT....");

  // Use this initializer if you're using a 1.8" TFT
  tft.initR(INITR_BLACKTAB);

  Serial.println("TFT:   Init Succes!");

  uint16_t time = millis();
  tft.fillScreen(ST7735_BLACK);

  // large block of text
  tft.fillScreen(ST7735_BLACK);
  testdrawtext("Hi World. ", ST7735_WHITE);
  delay(1000);

}

void loop() {
  // put your main code here, to run repeatedly:

}

void testdrawtext(const char *text, uint16_t color) {
  tft.setCursor(0, 0);
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}
 
hello, checking in here.
Sorry for reviving, just thinking about the same thing :
the libraries were hard-coded to SPI0 and need work to use in SPI1

in my case I probably will be fine to use SD card with SPI1 and nRF24 with SPI. (teensy LC is it, otherwise I use SDIO too)
both can begin() and work individually, even if hard-ware present. but another can't begin() after the other.
I think they should? They have Chip Select pins.
Don't know why :(.

SD module with pin(CS10,11,12,13) and RF24 module with pin(ce,csn - 8,9 11,12,13)
 
Last edited:
Status
Not open for further replies.
Back
Top