ADXL345 not working with RamDisk

My goal is to store accelerometer data (count,x,y,z) every 5 ms to SD memory. It takes a bit longer than 5ms to read the ADXL and store the data to SD, therefore I want to store the data first to RAM and then when the measurement is completed, store to SD. It appears I’m not reading the accelerometer when the accelerometer read is after the ram.begin code. I get x,y,z readings of zero.

//Add the SPI library so we can communicate with the ADXL345 sensor + SD Card + File time stamp
#include <SPI.h>

#include <SdFat.h>
#include <SdFatUtil.h>
#include <M23LCV1024.h>
#include <RamDisk.h>

#define FILENAME "TEST.CSV"
const uint8_t RAM_CS_PIN = 9;
const uint8_t SD_CS_PIN = SS;

SdFat sd;
SdBaseFile sdFile;
M23LCV1024 ram;
RamVolume vol;
RamFile ramFile;
char buf[40];

/*  ADXL Connections
SCL => pin 13
SDA => pin 11
SDO => pin 12
CS => pin 8
*/

const int CS=8;               // Assign ADXL to pin 8     


// some of the registers available on the ADXL345.

char BW_RATE = 0x2C;     //Bandwidth Register
char POWER_CTL = 0x2D;	 //Power Control Register
char DATA_FORMAT = 0x31;  // g range setting 
char DATAX0 = 0x32;	//X-Axis Data 0
char DATAX1 = 0x33;	//X-Axis Data 1
char DATAY0 = 0x34;	//Y-Axis Data 0
char DATAY1 = 0x35;	//Y-Axis Data 1
char DATAZ0 = 0x36;	//Z-Axis Data 0
char DATAZ1 = 0x37;	//Z-Axis Data 1

char values[10];    //This buffer will hold values read from the ADXL345 registers.

int x,y,z;         //These variables will be used to hold the x,y and z axis accelerometer values.



void setup(){  
  
  SPI.begin();                     //Initiate an SPI communication instance.
  SPI.setDataMode(SPI_MODE3);      //Configure the SPI connection for the ADXL345.
  
  
  pinMode(CS, OUTPUT);                //Set up the Chip Select pin to be an output from the Arduino.  
  digitalWrite(CS, HIGH);             //Set Chip Select pin to high.
  writeRegister(DATA_FORMAT, 0x01);   //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeRegister(POWER_CTL, 0x08);     //Put the ADXL345 into Measure Mode by writing 0x08 to the POWER_CTL register.  
  writeRegister(BW_RATE, 0x0D);        //Set bandwidth to: E= 800 Hz, D=400, C=200, B=100, A=50


Serial.begin(9600);
  


  ram.begin(RAM_CS_PIN);
  Serial.print(F("FreeRam: "));
  Serial.println(FreeRam());
  if (!sd.begin(SD_CS_PIN)) sd.errorHalt();
  // Initialize RAM 
  ram.begin(RAM_CS_PIN);

  Serial.print(F("Format (Y/N): "));
  while (!Serial.available());
  char c = toupper(Serial.read());
  Serial.println(c);

  if (c == 'Y') {
    // totalBlocks: 256
    // dirBlocks: 4  (64 entries)
    // clusterSizeBlocks: 1 (one 512 byte block per cluster)
    if (!vol.format(&ram, 256, 4, 1)) {
      Serial.println(F("format fail"));
      return;
    }
  } else if (c != 'N') {
    Serial.println(F("Invalid entry"));
    return;
  }
  if (!vol.init(&ram)) {
    Serial.println(F("init fail"));
    return;
  }
  // Remove old version.
  vol.remove(FILENAME);
  if (!ramFile.open(FILENAME, O_CREAT | O_RDWR)) {
    Serial.println(F("open fail"));
    return;
  }
  Serial.println(F("Writing ramFile"));
  
   for (int j = 0; j < 10; j++) {
     readRegister(DATAX0, 6, values);

    //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
    //The X value is stored in values[0] and values[1].
    x = ((int)values[1]<<8)|(int)values[0];
    //The Y value is stored in values[2] and values[3].
    y = ((int)values[3]<<8)|(int)values[2];
    //The Z value is stored in values[4] and values[5].
    z = ((int)values[5]<<8)|(int)values[4];
    
    Serial.print(j);
    Serial.print(",");
    Serial.print(x);
    Serial.print(",");
    Serial.print(y);
    Serial.print(",");
    Serial.println(z); 
    
    ramFile.print(j);
    ramFile.write(",");
    ramFile.print(x);
    ramFile.write(",");
    ramFile.print(y);
    ramFile.write(",");
    ramFile.println(z);  
   }
 
  // like closing and opening file (need to update dir for ls).
  ramFile.sync();
  ramFile.rewind();
  
  vol.ls(&Serial, LS_DATE | LS_SIZE);

  if (!sdFile.open(FILENAME, O_CREAT | O_RDWR | O_TRUNC)) {
    Serial.println(F("sdFile.open failed"));
    return;
  }
  Serial.println(F("Copying ramFile to sdFile"));
  int n;
  while ((n = ramFile.read(buf, sizeof(buf))) > 0) {
    if (sdFile.write(buf, n) != n) {
      Serial.println(F("sdFile.write failed"));
      return;
    }
  }
  ramFile.close();
  sdFile.close();
  sd.ls(&Serial, LS_DATE | LS_SIZE);

  Serial.println(F("Done"));
}
  
  


void loop(){
  
}
  


//This function will write a value to a register on the ADXL345.
//Parameters:
//  char registerAddress - The register to write a value to
//  char value - The value to be written to the specified register.

void writeRegister(char registerAddress, char value){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress);
  //Transfer the desired register value over SPI.
  SPI.transfer(value);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(CS, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values){
  //Since we're performing a read operation, the most significant bit of the register address should be set.
  char address = 0x80 | registerAddress;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
  
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the starting register address that needs to be read.
  SPI.transfer(address);
  //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00);
  }
  
  digitalWrite(CS, HIGH);   //Set the Chips Select pin high to end the SPI packet.
}

This could be the problem:

  SPI.setDataMode(SPI_MODE3);      //Configure the SPI connection for the ADXL345.

The SdFat and M23LCV1024 library set SPI mode and speed each time they access the SPI bus. They both use mode zero.

Try setting the mode before each ADXL345 SPI access. The SPI.setDataMode() call is very fast.

void SPIClass::setDataMode(uint8_t mode)
{
  SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
}

Thanks! That works.

Gerry