SdFat and CAN bus compatibility error

Hi all,

I am working on a project where I need to to log data from a motorcycle.
I am using an Arduino Due along with an CAN-bus shield (https://www.sparkfun.com/products/10039), a mux shield for extra analog inputs (Mux Shield II | Mayhew Labs), an Adafruit 10DOF breakout board and an EM-506 GPS.
Using the SdFat library's lowlatency logger I managed to log everything except the CAN bus at 2kHz, but as soon as I add the code to readout the CAN bus I get an error 0x15 from the sd card.

The CAN bus shield uses an MPC2515 chip with is connected to the arduino via SPI, as is the sd card so I'm guessing the problem comes from there.
The code I use to readout the CAN bus uses the Canduino library and only reading out the CAN bus works perfectly so there is no problem for the Arduino to connect with the MPC2515.

I tried messing around with the settings of the SdFat library but so far it didn't help a bit. I tried reducing the sample rate but no luck either.

The lowlatency code is very long so I'm not posting everything here. Only the part for acquiring the data and the setup are changed.

As soon as a "CAN."-function is called in the acquireData-function an error pops up.

// Acquire a data record.
void acquireData(data_t* data) {
  data->time = micros();
  sensors_event_t event;
  
  // Acquire accelerometer data
  accel.getEvent(&event);
  data->accelx = event.acceleration.x;
  data->accely = event.acceleration.y;
  data->accelz = event.acceleration.z;
  
  // Acquire magnetometer data
  mag.getEvent(&event);
  data->magx = event.magnetic.x;
  data->magy = event.magnetic.y;
  data->magz = event.magnetic.z;
  
  for (int i=0; i<16; i++)
  {
    //Analog read on all 16 inputs on IO1, IO2, and IO3
    data->IO1AnalogVals[i] = muxShield.analogReadMS(1,i);
    //data->IO2AnalogVals[i] = muxShield.analogReadMS(2,i);
    //data->IO3AnalogVals[i] = muxShield.analogReadMS(3,i);
  }
  
  //Acquire GPS input
  data->gps_char = Serial1.read();
  
  data->frame_data[0] = 0x00;
  data->frame_data[1] = 0x00;
  data->frame_data[2] = 0x00;
  data->frame_data[3] = 0x00;
  data->frame_data[4] = 0x00;
  data->frame_data[5] = 0x00;
  data->frame_data[6] = 0x00;
  data->frame_data[7] = 0x00;
  
  data->frame_id = 0x0000;
  
  length = 0;
  
  rx_status = CAN.readStatus();
    
  if (rx_status & 0x80 == 0x80) {
    CAN.readDATA_ff_0(&length,data->frame_data,&data->frame_id);    
  }
  else if (rx_status & 0x40 == 0x40) { 
    CAN.readDATA_ff_1(&length,data->frame_data,&data->frame_id);
  }
}
//Setup
void setup(void) {  
  if (ERROR_LED_PIN >= 0) {
    pinMode(ERROR_LED_PIN, OUTPUT);
  }
  Serial.begin(9600);
  
  Serial.print(F("FreeRam: "));
  Serial.println(FreeRam());
  Serial.print(F("Records/block: "));
  Serial.println(DATA_DIM);
  if (sizeof(block_t) != 512) error("Invalid block size");
  // initialize file system.
  if (!sd.begin(SD_CS_PIN, SPI_FULL_SPEED)) {
    sd.initErrorPrint();
    fatalBlink();
  }
  
  /* Initialise the sensors */
  initSensors();
  
  //Set I/O 1, I/O 2, and I/O 3 as analog inputs
  muxShield.setMode(1,ANALOG_IN);
  muxShield.setMode(2,ANALOG_IN);
  muxShield.setMode(3,ANALOG_IN);
  
  //Start GPS
  Serial1.begin(GPSBaud);
  
  // initialize CAN bus class
  // this class initializes SPI communications with MCP2515
  CAN.begin();
  CAN.baudConfig(BUS_SPEED);
  CAN.setMode(NORMAL);  // set to "NORMAL" for standard com
  
  //Wait 5 seconds so that I can still upload even
  //if the previous iteration spams the serial port
  delay(5000);
}

This is what gets printed to the serial monitor.

FreeRam: 92187
Records/block: 6

type:
c - convert file to CSV
d - dump data to Serial
e - overrun error details
r - record data
Creating new file
Erasing all data
Logging - type any character to stop
error: write data failed
SD errorCode: 0X15,0XC0

Any help is much appreciated! :slight_smile:

What do you mean, "an error pops up" ?

By that, I mean that as soon as I send "r" in the serial monitor it shows:

Creating new file
Erasing all data
Logging - type any character to stop

And imidiately after that I get the error code

error: write data failed
SD errorCode: 0X15,0XC0