SD Card not working on DUE with Elekfreak Mega TFT shield V2.2

Hi, I have a problem with my TFT shield, On the arduino mega, the SD card works fine, but on the due, it wont work...

Thanks for you help :slight_smile:

Please tell me you didn't just put the Mega shield onto the DUE????.......

Regards,

Graham

It's called a mega shiel,d but it's compatible with the DUE :slight_smile:

And, the only thing, i changed, was putting it on the DUE...

Is this the item to which you refer?

Regards,

Graham

Yes :slight_smile:

Then it is NOT compatible with the DUE!!

The DUE uses the small 6 pin connector in the middle of the board for the SPI pins, as you can see, that board has no such connector. Does your display work OK while using the shield on the DUE? Including touch? If so there is a possibility we could get your SD card working in Software SPI mode....

Regards,

Graham

Yes, the display works perfectly with it (It's the display I use for my benchmarks , a 7" 800 x 480 lcd)

Do you already have the SdFat library installed? If not get it from here GitHub - greiman/SdFat: Arduino FAT16/FAT32 exFAT Library.

I will get back to you on configuration....

Regards,

Graham

Ok, I've just installed it

OK..

Open SdFatConfig.h

Change lines 81 - 94 to this :-

#define SD_SPI_CONFIGURATION 2
//------------------------------------------------------------------------------
/**
 * If SD_SPI_CONFIGURATION is defined to be two, these definitions
 * will define the pins used for software SPI.
 *
 * The default definition allows Uno shields to be used on other boards.
 */
/** Software SPI Master Out Slave In pin */
uint8_t const SOFT_SPI_MOSI_PIN = 51;
/** Software SPI Master In Slave Out pin */
uint8_t const SOFT_SPI_MISO_PIN = 50;
/** Software SPI Clock pin */
uint8_t const SOFT_SPI_SCK_PIN = 52;

And your CS pin will be 53.

Hope this helps.

Regards,

Graham

Thanks a lot !!!!!!!! :slight_smile:

It works perfectly !

If you want to use it to draw raw images from sd card, you will also want to take a look at UTFT_SdRaw.... You can install it in library manager. Sadly, although it might work, it is NOT working perfectly!! Perfectly would be with Hardware SPI.... On a proper DUE TFT shield, you can draw 800x480 images in about 450ms !! I would be interested to know what sort of speed you will get on software SPI...... :frowning:

Regards,

Graham

Ok, I'm testing it, and I'll tell you the results in a few minutes !

So

SDRAW demo without overclock: 48.02 seconds

DSRAW demo with overclock (114MHz): 38.27 seconds !!!!!

supply the sketch you are using........

G

The demo 800:

// UTFT_SdRaw_800x480_Demo
// Copyright (C)2015 Graham Lawrence (GHLawrence2000). All Rights reserved.
// web: https://github.com/ghlawrence2000/UTFT_SdRaw
//
// This program is a demo of how to use the functions provided by UTFT_SdRaw.
//
// This program requires the UTFT, UTouch, UTFT_Buttons and SdFat libraries.
//
#include <SPI.h>
// SdFat lib from here :-
// https://github.com/greiman/SdFat/archive/master.zip
#include <SdFat.h>
#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_Buttons.h>
#include <UTFT_SdRaw.h>
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

#define SD_CHIP_SELECT  53  // SD chip select pin
// file system object
SdFat sd;
// print stream
ArduinoOutStream cout(Serial);

int wait = 2, pressed_button;

// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!

UTFT myGLCD(SSD1963_800ALT, 38,39,40,41);
//UTFT myGLCD(CTE50, 25, 26, 27, 28);
//UTFT myGLCD(CTE70, 25, 26, 27, 28);


// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due (JP10):  6, 5,32, 3, 2
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch  myTouch( 6, 5, 4, 3, 2);
//UTouch  myTouch( 6, 5, 4, 3, 2);

UTFT_SdRaw myFiles(&myGLCD);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);

void setup()
{
  setCPUClock(114); //Set to 84 for no overclock
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for DUE & Leonardo only
  }
  Serial.println(F("Initialising SD card..."));
  bool mysd = 0;
  // see if the card is present and can be initialized:
  while (!mysd)
  {
    pinMode(53, OUTPUT);
    if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
      Serial.println(F("Card failed, or not present"));
      Serial.println(F("Retrying...."));
    }
    else
    {
      mysd = 1;
      Serial.println(F("Card initialised."));
    }
  }
  Serial.println(F("Initialising LCD."));
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(SmallFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myButtons.setTextFont(BigFont);
  int butskip = myButtons.addButton( 85,  219 , 70,  20, "Skip");
  Serial.println(F("LCD initialised."));
  // ////////////////////////////////////////////////////////////////////////////////
  //             Wait for GPS screen  or skip button
  ///////////////////////////////////////////////////////////////////////////////////
  Serial.println(F("Initialising GPS."));
  long mytime = millis();
  myFiles.load(0, 0, 240, 240, "wait4gps.RAW", 1 , 1);
  long mytime1 = millis() - mytime;
  cout << F("Time to draw 240x240 raw ") << mytime1 << F(" ms") << endl;
  myButtons.drawButton(butskip);
  while (wait == 2) {
    pressed_button = myButtons.checkButtons();
    if (pressed_button == butskip)
    {
      cout << F("Skip button detected (No GPS :( )\n");
      wait = true;
    }
  }
  char fname440[] = "SO11/SO100100.RAW";
  myGLCD.clrScr();
  mytime = millis();
  myFiles.load(20, 20, 440, 440, fname440, 1);
  mytime1 = millis() - mytime;
  cout << F("Time to draw 440x440 raw (no extra buffer) (non inverted colour) ") << mytime1 << F(" ms") << endl;
  delay(5000);
  myGLCD.clrScr();
  mytime = millis();
  myFiles.load(20, 20, 440, 440, fname440, 32, 0);
  mytime1 = millis() - mytime;
  cout << F("Time to draw 440x440 raw (32*buffer) (non inverted colour) ") << mytime1 << F(" ms") << endl;
  delay(5000);
  mytime = millis();
  myFiles.load(20, 20, 440, 440, fname440, 32, 1);
  mytime1 = millis() - mytime;
  cout << F("Time to draw 440x440 raw (32*buffer) (inverted colour) ") << mytime1 << F(" ms") << endl;
  delay(5000);
  myGLCD.clrScr();
  mytime = millis();
  myFiles.pan(20, 20, 440, 440, 0, 0, 4040, 4040, "SK45/SK45.RAW", 1);
  mytime1 = millis() - mytime;
  cout << F("Time to draw 440x440 portion from 4040x4040 raw image (inverted colour) ") << mytime1 << F(" ms") << endl;
  delay(5000);
  myGLCD.clrScr();
  int xad, yad;
  int xd = 0, yd = 0;
  int tmpx = 0, tmpy = 0;
  mytime = millis();
  for (int z = 0; z < 209; z++)
  {
    tmpy = (z / 19);
    tmpx = z - (tmpy * 19);
    xd = tmpx * 42;
    yd = tmpy * 42;
    tmpy = (z / 22);
    tmpx = z - (tmpy * 22);
    yad = (tmpy * 69) + 10;
    xad = (tmpx * 108) + 44;
    myFiles.pan(xd, yd, 42, 42, xad, yad, 2374, 696, "icons.raw");
  }
  mytime1 = millis();
  cout << F("time to draw 210 42x42 icons (ms) ") << (mytime1 - mytime) << endl;
  delay(5000);
  cout << F("Thanks for watching!") << endl;
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myGLCD.setColor(VGA_GREEN);
  myGLCD.print("Thanks for watching!", CENTER, 231);
}

void loop()
{
}

void setCPUClock(int frq){
  int mult = (frq / 6) - 1;
  EFC0->EEFC_FMR = EEFC_FMR_FWS(4);
  EFC1->EEFC_FMR = EEFC_FMR_FWS(4);
  PMC->CKGR_PLLAR = (CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(mult) | CKGR_PLLAR_PLLACOUNT(0x3fUL) | CKGR_PLLAR_DIVA(1UL));
  while (!(PMC->PMC_SR & PMC_SR_LOCKA)) {}
  PMC->PMC_MCKR = ( PMC_MCKR_PRES_CLK_2 | PMC_MCKR_CSS_PLLA_CLK);
  while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {}
  SystemCoreClockUpdate();
}

We are comparing apples and oranges, so let me give you an example of a benchmark I wrote for SdRaw, it is basically, the demo supplied with SdRaw, but with all delays and touch removed, timings are printed for each item drawn, an additional 800x480 image has been added, these timings are using the standard version of UTFT and custom version of SdRaw.

Hardware SPI :-

Time to draw 240x240 raw 246 ms
Time to draw 440x440 raw (no extra buffer) (non inverted colour) 459 ms
Time to draw 440x440 raw (32*buffer) (non inverted colour) 255 ms
Time to draw 440x440 raw (32*buffer) (inverted colour) 254 ms
Time to draw 440x440 portion from 4040x4040 raw image (inverted colour) 625 ms
time to draw 210 42x42 icons (ms) 4495
Time to draw 800x480 raw (16*buffer) (non inverted colour) 496 ms
Minimum runtime 7356ms
Thanks for watching!

Software SPI :-

Time to draw 240x240 raw 483 ms
Time to draw 440x440 raw (no extra buffer) (non inverted colour) 1228 ms
Time to draw 440x440 raw (32*buffer) (non inverted colour) 1015 ms
Time to draw 440x440 raw (32*buffer) (inverted colour) 1018 ms
Time to draw 440x440 portion from 4040x4040 raw image (inverted colour) 1808 ms
time to draw 210 42x42 icons (ms) 15274
Time to draw 800x480 raw (16*buffer) (non inverted colour) 1993 ms
Minimum runtime 23339ms
Thanks for watching!

This clearly shows the effect of hardware over software SPI!!

Regards,

Graham

I will now try your overclock method....

here is the code for overclock:

void setCPUClock(int frq){  //setCPUClock(Frequency_in_MHz)
  int mult = (frq / 6) - 1;
  EFC0->EEFC_FMR = EEFC_FMR_FWS(4);
  EFC1->EEFC_FMR = EEFC_FMR_FWS(4);
  PMC->CKGR_PLLAR = (CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(mult) | CKGR_PLLAR_PLLACOUNT(0x3fUL) | CKGR_PLLAR_DIVA(1UL));
  while (!(PMC->PMC_SR & PMC_SR_LOCKA)) {}
  PMC->PMC_MCKR = ( PMC_MCKR_PRES_CLK_2 | PMC_MCKR_CSS_PLLA_CLK);
  while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {}
  SystemCoreClockUpdate();
}

void setCPUMult(int mult){  // setCPUMult(Multiplier);
  EFC0->EEFC_FMR = EEFC_FMR_FWS(4);
  EFC1->EEFC_FMR = EEFC_FMR_FWS(4);
  PMC->CKGR_PLLAR = (CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(mult) | CKGR_PLLAR_PLLACOUNT(0x3fUL) | CKGR_PLLAR_DIVA(1UL));
  while (!(PMC->PMC_SR & PMC_SR_LOCKA)) {}
  PMC->PMC_MCKR = ( PMC_MCKR_PRES_CLK_2 | PMC_MCKR_CSS_PLLA_CLK);
  while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {}
  SystemCoreClockUpdate();
}

:slight_smile:

mmmmmm exactly like before when I tried playing with it.... SLOWER....

Hardware SPI :- (101)

Time to draw 240x240 raw 255 ms
Time to draw 440x440 raw (no extra buffer) (non inverted colour) 489 ms
Time to draw 440x440 raw (32*buffer) (non inverted colour) 257 ms
Time to draw 440x440 raw (32*buffer) (inverted colour) 257 ms
Time to draw 440x440 portion from 4040x4040 raw image (inverted colour) 673 ms
time to draw 210 42x42 icons (ms) 4883
Time to draw 800x480 raw (16*buffer) (non inverted colour) 497 ms
Minimum runtime 7837ms
Thanks for watching!

Now this is curious!! I ran the overclock on software SPI first, @114...... This is the results. But because Hardware SPI fails to initialise the card at SPI_FULL_SPEED with 114 over clock, I found the maximum overclock that worked at SPI_FULL_SPEED to be 101, so I re-ran the software SPI at 101..... and it is faster then 114!!

Software SPI :- (114)

Time to draw 240x240 raw 505 ms
Time to draw 440x440 raw (no extra buffer) (non inverted colour) 1302 ms
Time to draw 440x440 raw (32*buffer) (non inverted colour) 1020 ms
Time to draw 440x440 raw (32*buffer) (inverted colour) 1022 ms
Time to draw 440x440 portion from 4040x4040 raw image (inverted colour) 1929 ms
time to draw 210 42x42 icons (ms) 16230
Time to draw 800x480 raw (16*buffer) (non inverted colour) 2001 ms
Minimum runtime 24527ms
Thanks for watching!

Software SPI :- (101)

Time to draw 240x240 raw 492 ms
Time to draw 440x440 raw (no extra buffer) (non inverted colour) 1258 ms
Time to draw 440x440 raw (32*buffer) (non inverted colour) 1017 ms
Time to draw 440x440 raw (32*buffer) (inverted colour) 1019 ms
Time to draw 440x440 portion from 4040x4040 raw image (inverted colour) 1857 ms
time to draw 210 42x42 icons (ms) 15661
Time to draw 800x480 raw (16*buffer) (non inverted colour) 1996 ms
Minimum runtime 23818ms
Thanks for watching!

Regards,

Graham