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

Thread: SD Card on SPI1 - T3.5

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    18

    SD Card on SPI1 - T3.5

    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
    }

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,452
    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.

  3. #3
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,074
    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.
    Last edited by manitou; 05-31-2017 at 08:27 PM.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,296
    Why use slow SPI1 when you have fast SDIO to access the card?

  5. #5
    Junior Member
    Join Date
    Dec 2016
    Posts
    18
    Quote Originally Posted by PaulStoffregen View Post
    Why use slow SPI1 when you have fast SDIO to access the card?
    Actually I will use SDI0 to save data at 300Hz and I want to use SPI1 with a display TFT ST7735. Is it possible to do it?
    Thank you Paul!

  6. #6
    Junior Member
    Join Date
    Dec 2016
    Posts
    18
    Quote Originally Posted by manitou View Post
    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

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,030
    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

  8. #8
    Junior Member
    Join Date
    Dec 2016
    Posts
    18
    Quote Originally Posted by KurtE View Post
    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);
    }

  9. #9
    Senior Member
    Join Date
    Sep 2015
    Location
    Taiwan, Asai. (Traditional Chinese)
    Posts
    154
    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 by Po Ting; 04-29-2019 at 04:51 PM. Reason: LC

Posting Permissions

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