tomicdesu
Member
I'm trying to set up my board that has two SD cards using SPI1 (due to conflicts with pins 10_, 11_, 12_) (*) and also the built-in SDIO card, for a total of three attached SDcards. I need three drives, but there will be no overlapping IO. I'm emulating a CP/M-80 machine and need the separate physical drives. I will use the ExFAT because I need to use position() to address "blocks" in a file.
The built-in SDcard is working fine. I'm thoroughly confused by the SPI usage and configuration for the other two; they are on pins 1, 26, 27, with card selects 36, 37. begin() always returns false.
I'm using the Arduino IDE. The only SD library installed is SdFat.h.
I'm looking at EXAMPLE program "TwoCards". I've stripped down the example code somewhat, for clarity. There's two SdExFat objects, but I'm baffled how I tell those objects to use SPI1. Do I explicity setup an SPI object, and pass a reference to it? I can't find anything pertinent to using other than default SPI object and pins other than TwoCards.
SdFat's SdSpi* driver code is not easy to read.
Any suggestions on how to proceed?
(*) The conflict with 10, 11, 12 are that I'm using DMA driven VGA that drives these pins viao FlexIO.
The built-in SDcard is working fine. I'm thoroughly confused by the SPI usage and configuration for the other two; they are on pins 1, 26, 27, with card selects 36, 37. begin() always returns false.
I'm using the Arduino IDE. The only SD library installed is SdFat.h.
I'm looking at EXAMPLE program "TwoCards". I've stripped down the example code somewhat, for clarity. There's two SdExFat objects, but I'm baffled how I tell those objects to use SPI1. Do I explicity setup an SPI object, and pass a reference to it? I can't find anything pertinent to using other than default SPI object and pins other than TwoCards.
SdFat's SdSpi* driver code is not easy to read.
Any suggestions on how to proceed?
Code:
/*
* Example use of two SD cards.
*/
#include <SPI.h>
#include "sdios.h"
#include "SdFat.h"
//#include <SdFatUtil.h>
SdExFat sd1;
const uint8_t SD1_CS = 36; // chip select for sd1
SdExFat sd2;
const uint8_t SD2_CS = 37; // chip select for sd2
const uint8_t BUF_DIM = 100;
uint8_t buf[BUF_DIM];
const uint32_t FILE_SIZE = 1000000;
const uint16_t NWRITE = FILE_SIZE/BUF_DIM;
//------------------------------------------------------------------------------
// print error msg, any SD error codes, and halt.
// store messages in flash
#define errorExit(msg) errorHalt(F(msg))
#define initError(msg) initErrorHalt(F(msg))
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
while (!Serial) {} // wait for Leonardo
// fill buffer with known data
for (unsigned i = 0; i < sizeof(buf); i++) {
buf[i] = i;
}
Serial.println(F("type any character to start"));
while (Serial.read() <= 0) {}
// disable sd2 while initializing sd1
pinMode(SD1_CS, OUTPUT);
digitalWrite(SD1_CS, HIGH);
pinMode(SD2_CS, OUTPUT);
digitalWrite(SD2_CS, HIGH);
SPI.setMOSI (26);
SPI.setSCK (27);
SPI.setMISO (1);
// initialize the first card
if (!sd1.begin (SD1_CS)) {
sd1.initError("sd1:");
}
// create Dir1 on sd1 if it does not exist
if (!sd1.exists("/Dir1")) {
if (!sd1.mkdir("/Dir1")) {
sd1.errorExit("sd1.mkdir");
}
}
// initialize the second card
if (!sd2.begin(SD2_CS)) {
sd2.initError("sd2:");
}
// create Dir2 on sd2 if it does not exist
if (!sd2.exists("/Dir2")) {
if (!sd2.mkdir("/Dir2")) {
sd2.errorExit("sd2.mkdir");
}
}
// list root directory on both cards
Serial.println(F("------sd1 root-------"));
sd1.ls();
Serial.println(F("------sd2 root-------"));
sd2.ls();
// make /Dir1 the default directory for sd1
if (!sd1.chdir("/Dir1")) {
sd1.errorExit("sd1.chdir");
}
// make /Dir2 the default directory for sd2
if (!sd2.chdir("/Dir2")) {
sd2.errorExit("sd2.chdir");
}
// list current directory on both cards
Serial.println(F("------sd1 Dir1-------"));
sd1.ls();
Serial.println(F("------sd2 Dir2-------"));
sd2.ls();
Serial.println(F("---------------------"));
// remove rename.bin from /Dir2 directory of sd2
if (sd2.exists("rename.bin")) {
if (!sd2.remove("rename.bin")) {
sd2.errorExit("remove rename.bin");
}
}
// set the current working directory for open() to sd1
sd1.chvol();
// create or open /Dir1/test.bin and truncate it to zero length
SdFile file1;
if (!file1.open("test.bin", O_RDWR | O_CREAT | O_TRUNC)) {
sd1.errorExit("file1");
}
Serial.println(F("Writing test.bin to sd1"));
// write data to /Dir1/test.bin on sd1
for (int i = 0; i < NWRITE; i++) {
if (file1.write(buf, sizeof(buf)) != sizeof(buf)) {
sd1.errorExit("sd1.write");
}
}
// set the current working directory for open() to sd2
sd2.chvol();
// create or open /Dir2/copy.bin and truncate it to zero length
SdFile file2;
if (!file2.open("copy.bin", O_WRITE | O_CREAT | O_TRUNC)) {
sd2.errorExit("file2");
}
Serial.println(F("Copying test.bin to copy.bin"));
// copy file1 to file2
file1.rewind();
uint32_t t = millis();
while (1) {
int n = file1.read(buf, sizeof(buf));
if (n < 0) {
sd1.errorExit("read1");
}
if (n == 0) {
break;
}
if (file2.write(buf, n) != n) {
sd2.errorExit("write2");
}
}
t = millis() - t;
Serial.print(F("File size: "));
Serial.println(file2.fileSize());
Serial.print(F("Copy time: "));
Serial.print(t);
Serial.println(F(" millis"));
// close test.bin
file1.close();
file2.close();
// list current directory on both cards
Serial.println(F("------sd1 -------"));
sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
Serial.println(F("------sd2 -------"));
sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
Serial.println(F("---------------------"));
Serial.println(F("Renaming copy.bin"));
// rename the copy
if (!sd2.rename("copy.bin", "rename.bin")) {
sd2.errorExit("sd2.rename");
}
// list current directory on both cards
Serial.println(F("------sd1 -------"));
sd1.ls("/", LS_R | LS_DATE | LS_SIZE);
Serial.println(F("------sd2 -------"));
sd2.ls("/", LS_R | LS_DATE | LS_SIZE);
Serial.println(F("---------------------"));
Serial.println(F("Done"));
}
//------------------------------------------------------------------------------
void loop() {}
(*) The conflict with 10, 11, 12 are that I'm using DMA driven VGA that drives these pins viao FlexIO.