OK, I will get to work on this...probably this weekend, as the week turns out to be a bit more busy.
I used the sdFat library (see here http://code.google.com/p/beta-lib/downloads/list" onclick="window.open(this.href);return false; ... it is sdFatBeta)
For inexplicable reasons, the SD library (that is part of the Arduino libraries) does not show up in my IDE.
Anyway, the code is ugly at the moment, needs to be polished a bit.
What it (see below) does is run the ADC in AD0 and AD1 each millisecond and print the data into an ASCII buffer. Every second the buffer is written to the SD card.
After 20 s, the file is closed. The LED is blinking once a second while logging. If you press reset, it starts the acquisition of another 20 s file.
This is just a proof of concept, so for the real thing we would write binary files, append the file, do some error handling etc.
Not sure how this should be handled..What do you want to log?
ADC data (current, voltage, temperature), CAN messages?. If it isnt too much for the due, we can add Ethernet (there are $9 modules on amazon that run fine with the old arduinos), so any data-intensive stuff I would move off the Due asap. Definitely will run into to trouble if we log at 1 kHZ....
So maybe just the circular buffer? Basically dump its contents to the SD once its full?
Yes, the code I posted should work for CAN interrupt message handling. Note that you have to modify the library file as well (see my earlier post on this), as they define their own message handlers.
The ADC logging code. SS on the DUE seems to digital pin 10 (that worked for me). The rest goes through the 6 pin header near the uP.
Code:
#include <DueTimer.h>
#include <SdFat.h>
#include <SdFatUtil.h> // define FreeRam()
#define BUFFER_SIZE 1000
#define SD_CHIP_SELECT SS // SD chip select pin
#define USE_DS1307 0 // set nonzero to use DS1307 RTC
#define LED_PIN 13
unsigned long delta_t=0;
int n=0,nsec=20;
uint16_t b0[BUFFER_SIZE];
uint16_t b1[BUFFER_SIZE];
// file system object
SdFat sd;
// text file for logging
ofstream logfile;
// buffer to format data - makes it eaiser to echo to Serial
char buf[12000];
boolean LED_VAL=HIGH;
void AD_Handler()
{
unsigned long t1=micros();
while((ADC->ADC_ISR & 0x80)==0);
while((ADC->ADC_ISR & 0x80)==0);// wait for two conversions
b0[n]=ADC->ADC_CDR[7];// read data on A0 pin
b1[n]=ADC->ADC_CDR[6];// read data on A1 pin
delta_t+=micros()-t1; // benchmark
n++;
if(n>BUFFER_SIZE-1)
{
if(nsec>0){
LED_VAL=!LED_VAL;
digitalWrite(LED_PIN, LED_VAL);
//SerialUSB.write((uint8_t *)b0,2*BUFFER_SIZE);
//SerialUSB.write((uint8_t *)b1,2*BUFFER_SIZE);
obufstream bout(buf, sizeof(buf));
for(int i=0;i<BUFFER_SIZE;i++)
{
bout << b0[i] << ',' << b1[i] << endl;
}
logfile << buf << flush;
}
else
{
digitalWrite(LED_PIN, LOW);
nsec=1;
logfile.close();
}
nsec--;
n=0;
delta_t=0;
}
}
void setup(){
//SerialUSB.begin(115200);
//REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000; //makes the ADC run fast
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LED_VAL); // turn the LED on (HIGH is the voltage level)
if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) sd.initErrorHalt();
Timer3.attachInterrupt(AD_Handler);
analogReadResolution(12); // set to 12 bits
int t=analogRead(0);
ADC->ADC_MR |= 0x80; // these lines set free running mode on adc 7 and adc 6 (pin A0 and A1 - see Due Pinout Diagram thread)
ADC->ADC_CR=2;
ADC->ADC_CHER=0xC0; // this is (1<<7) | (1<<6) for adc 7 and adc 6
char name[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (sd.exists(name)) continue;
logfile.open(name);
break;
}
Timer3.start(1000); // Calls every 1 ms
}
void loop()
{
}