Pico 2 Core use SD Card on 1 Core doesn't work

Hi everyone, I am trying to use SD card library here : khoih-progSDLibrary. I am trying to use Nonblockingwrite so I can write adxl345 value and Mic data simultaneously. ADXL345 at 50hz and Microphone produce 128 sample at 63 ms. I am trying to use microphone at core 1, adxl345 and sd card core 2. Here is my codes:

#include <Wire.h>  // Wire library - used for I2C communication
#define ADXL345_DATAX0      0x32
#define ADXL345_DEVICE (0x53)
#define ADXL345_TO_READ (6)
#define ADXL345_ERROR    0
#define ADXL345_READ_ERROR  1
#if !defined(ARDUINO_ARCH_RP2040)
  #error For RP2040 only
#endif
#if defined(ARDUINO_ARCH_MBED)
  #define PIN_SD_MOSI       PIN_SPI_MOSI
  #define PIN_SD_MISO       PIN_SPI_MISO
  #define PIN_SD_SCK        PIN_SPI_SCK
  #define PIN_SD_SS         PIN_SPI_SS
#else
  #define PIN_SD_MOSI       PIN_SPI0_MOSI
  #define PIN_SD_MISO       PIN_SPI0_MISO
  #define PIN_SD_SCK        PIN_SPI0_SCK
  #define PIN_SD_SS         PIN_SPI0_SS
#endif
#define _RP2040_SD_LOGLEVEL_       4
#include <SPI.h>
#include <RP2040_SD.h>
#define analog_pin 26
const int samples= 128;
String httpRequestData = "";
unsigned long previousMillis = 0 ;
unsigned long prevMillis = 0;
unsigned long prevFlushMillis = 0 ;
unsigned long microSeconds;
unsigned int samplingPeriod;
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
byte _buff[6] ;   //  6 Bytes Buffer
bool status;
byte error_code;
String data="";
String fullData="";
// file name to use for writing
const char filename[] = "demo5.txt";

// File object to represent file
File txtFile;
// string to buffer output
String buffer;
String secDataBuffer;

void readFromI2C(byte address, int num, byte _buff[]) {
  Wire.beginTransmission(ADXL345_DEVICE);
  Wire.write(address);
  Wire.endTransmission();

  //  Wire.beginTransmission(ADXL345_DEVICE);
  // Wire.reqeustFrom contains the beginTransmission and endTransmission in it.
  Wire.requestFrom(ADXL345_DEVICE, num);  // Request 6 Bytes

  int i = 0;
  while (Wire.available())
  {
    _buff[i] = Wire.read();       // Receive Byte
    i++;
  }
  if (i != num) {
    status = ADXL345_ERROR;
    error_code = ADXL345_READ_ERROR;
  }
  //  Wire.endTransmission();
}

void readFrom(byte address, int num, byte _buff[]) {
  readFromI2C(address, num, _buff); // If I2C Communication
}

void readAccel(int *x, int *y, int *z) {
  readFrom(ADXL345_DATAX0, ADXL345_TO_READ, _buff); // Read Accel Data from ADXL345
  // Each Axis @ All g Ranges: 10 Bit Resolution (2 Bytes)
  *x = (int16_t)((((int)_buff[1]) << 8) | _buff[0]);
  *y = (int16_t)((((int)_buff[3]) << 8) | _buff[2]);
  *z = (int16_t)((((int)_buff[5]) << 8) | _buff[4]);
}
int x, y, z;
float X_out, Y_out, Z_out;
void setup() {
  Serial.begin(115200);
  while (!Serial);
  delay(1000);
  samplingPeriod = round(1000000*(1.0/2048)); //Period in microseconds           
}

void setup1() {
  Serial.begin(115200);
  while (!Serial);
  delay(1000);
#if defined(ARDUINO_ARCH_MBED)
  Serial.print("Starting SD Card NonBlockingWrite on MBED ");
#else
  Serial.print("Starting SD Card NonBlockingWrite on ");
#endif
  Serial.println(BOARD_NAME);
  Serial.println(RP2040_SD_VERSION);
  Serial.print("Initializing SD card with SS = ");
  Serial.println(PIN_SD_SS);
  Serial.print("SCK = ");
  Serial.println(PIN_SD_SCK);
  Serial.print("MOSI = ");
  Serial.println(PIN_SD_MOSI);
  Serial.print("MISO = ");
  Serial.println(PIN_SD_MISO);
  if (!SD.begin(PIN_SD_SS))
  {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");
  // reserve 1kB for String used as a buffer
  buffer.reserve(1024);
  secDataBuffer.reserve(1024);
  // init the SD card
  if (!SD.begin())
  {
    Serial.println("Card failed, or not present");

    // don't do anything more:
    while (1);
  }
  // try to open the file for writing
  txtFile = SD.open(filename, FILE_WRITE);
  if (!txtFile)
  {
    Serial.print("error opening ");
    Serial.println(filename);
    while (1);
  }
  Wire.setSDA(0);
  Wire.setSCL(1);
  Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345_DEVICE); // Start communicating with the device
  Wire.write(0x31);
  Wire.write(0x0B);
  Wire.endTransmission();

  Wire.beginTransmission(ADXL345_DEVICE);
  Wire.write(0x20); // Z-axis offset register
  Wire.write(-7);
  Wire.endTransmission();

  Wire.beginTransmission(ADXL345_DEVICE);
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(0x08); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
  Wire.endTransmission();

  Wire.beginTransmission(ADXL345_DEVICE);
  Wire.write(0x2C);
  // Enable measurement
  Wire.write(0x09); //For low power 000x x pin set to 1  /1001 determine Hz
  Wire.endTransmission();
  delay(10);

}
void loop() {
 unsigned long currentMicMillis = millis();
  if(currentMicMillis - previousMillis > 1)
  {
    previousMillis = currentMicMillis;
    fullData = "{micData: " ;
    for(int i=0;i<samples;i++)
    {
      microSeconds = micros();    //Returns the number of microseconds since the Arduino board began running the current script. 
      data=analogRead(analog_pin);
      fullData = fullData + data + ",";
      while(micros() < (microSeconds + samplingPeriod))
          {
            //do nothing 
          }
    }
   fullData = + "}";
   buffer += fullData;
   buffer += "\r\n";
   fullData = "";
  }
//  Serial.println(millis() - currentMicMillis);
}

void loop1 () {
  unsigned long accMillis = millis();
  if (accMillis - prevMillis > 10)
  {
    // check if it's been over 100 ms since the last line added
    Wire.beginTransmission(ADXL345);
    Wire.write(0x30); // Access the INT_SOURCE register (0x30)
    Wire.endTransmission(false);
    Wire.requestFrom(ADXL345, 1, true); // Read 1 byte
    uint8_t intSource = Wire.read();
    if (intSource >= 130)
    {
      readAccel(&x, &y, &z);
      X_out = x / 256.f;
      Y_out = y / 256.f;
      Z_out = z / 256.f;
      httpRequestData = "{\"time\":" + String( millis()) + "," + "\"x\":" + String(X_out) + "," + "\"y\":" + String(Y_out) + " ," + "\"z\":" + String(Z_out) + "}" + ",";
      prevMillis = accMillis;
      secDataBuffer += httpRequestData;
      secDataBuffer += "\r\n";
    }
  }

  unsigned long flushMillis = millis();
  unsigned int chunkSize = txtFile.availableForWrite();
  if (chunkSize && buffer.length() >= chunkSize)
  {
    buffer += secDataBuffer;
//    Serial.println(buffer);
    txtFile.write(buffer.c_str(), chunkSize);
    buffer.remove(0, chunkSize);
    secDataBuffer.remove(0,chunkSize);
    if(flushMillis - prevFlushMillis >= 60000)
    {
     txtFile.flush();
     Serial.println("enters here");
     prevFlushMillis = flushMillis ;
    }
  }
}

SD card is not writing after a bit times (I have seen max 6 minute). I try to use with and without flush() it doesn't change anything. Any help ?

I detect the problem, after some times SD card initialization fails. I don't know why is this happening.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.