Hi everybody,
I need (for a first time) to use SD card shield on my ADK board.
In theory I know how to do it:
do USB host stuff -> deselect USB host -> select SD -> do SD stuff -> deselect SD -> select USB host (in loop)
Problem is that I cant make it working. I am not even sure what exactly is USB host (MAX3421E) chip CS pin on ADK. Official page on Arduino states "PH7 (SS)" and that its not broke out on board. In USB host library (UsbCore.h) I found this:
/* shield pins. First parameter - SS pin, second parameter - INT pin */
#elif defined(BOARD_MEGA_ADK)
typedef MAX3421e<P53, P54> MAX3421E; // Arduino Mega ADK
So there it lists that CS pin is pin 53.
I even looked in datasheet of ADK - into buffer logical translator input there are both inputs from PB0 (pin 53) and PH7 (internal pin).
I dont understand what is reason for PH7, so assuming the CS is really pin 53.
I have tested my SD functions without USB and they run fine. Same with USB functions without SD stuff. That means there is no wiring problem at least.
But when compiling both, SD is "working" in a strange manner.
This is my program (my final intention is to write data only during interrupt, but this also not working...):
/* Simplified Logitech Extreme 3D Pro Joystick Report Parser */
#include "TimerOne.h"
#include <usbhid.h>
#include <hiduniversal.h>
#include <SPI.h>
#include <SD.h>
#include <TinyGPS.h>
#include "le3dp_rptparser.h"
USB Usb;
HIDUniversal Hid(&Usb);
JoystickEvents JoyEvents;
JoystickReportParser Joy(&JoyEvents);
TinyGPS gps;
byte data[4096];
byte battery[100];
const int chipSelect = 4;
unsigned long time;
int tick = 0;
bool clear=false;
unsigned long chars;
unsigned short sentences, failed;
int USB_CS = 53;
int state;
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
void setup()
{
Serial.begin( 230400 );
//pinMode(USB_CS, OUTPUT);
Serial.println(F("========================================="));
Serial.println(F("Starting up... "));
Serial.println(F("========================================="));
// Serial1.begin( 4800 );
digitalWrite(53,HIGH);//USB HOST off
digitalWrite(chipSelect,HIGH);//SD off
delay(100);
digitalWrite(chipSelect,LOW);//SD on
Serial.print(F("Initializing SD card..."));
// see if the card is present and can be initialized:
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed.");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
Serial.println(F("Card initialized."));
// print the type of card
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
File dataFile = SD.open("spectrum_datalog.txt", FILE_WRITE);
// if the file is available, write to it
if (dataFile) {
//dataFile.println(dataString);
for (int i=0;i<4096;i++){
dataFile.print(data[i]);
dataFile.print("\n");
}
dataFile.print("*************\n");
dataFile.close();
}
else {
Serial.println("error opening datalog.txt");
}
Serial.println("Written");
delay(10000);
if (Usb.Init() == -1)
Serial.println(F("OSC did not start."));
delay( 200 );
if (!Hid.SetReportParser(0, &Joy))
ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 );
delay(5000);
Timer1.initialize(); // initialize timer1, and set a 5 second period
Timer1.attachInterrupt(getMeasurement,5000000); // attaches callback() as a timer overflow interrupt - max 8388480 (8,3 sec)
pinMode(13,OUTPUT);
Serial.println(F("Init done."));
Serial.println(F("========================================="));
}
void loop()
{
Usb.Task();
}
And program outputs:
=========================================
Starting up...
=========================================
Initializing SD card...Wiring is correct and a card is present.
Card initialized.
Card type: SDHC
error opening datalog.txt
Written
As you can see - SD card is detected correctly and correctly identified as SDHC (Kingston 4GB). But every attempt to write fails...
Could someone tell me what I am doing wrong?