Trying to use 2 sd card modules with Raspberry Pi Pico

Hi,

I'm trying to use two sd card modules on a PCB connected to a raspberry pi pico. I've got this code for writing and initialising both sd cards for writing. However, it seems like only one of the chip select pins are working. I have verified that the chip select of the sd cards are connected to CSN_SD2 and SPI1_CSN hardware wise.

The intent was to open 1 file each on each sd card and then each one would receive the sensor data, however I found that the SD card with the CS as CSN_SD2 opens, and two files end up being written on it with the same data - so I think there's something wrong with how I'm switching them but I'm not sure how. We even tried swapping the values of SPI1_CSN and CSN_SD2 and it swapped which SD was being written to, which I'm not really sure about why.

Has anyone ran into the same issue and can give guidance? Or spot something wrong with my setup? Any help would be appreciated :slight_smile:

Code is below: (i have omitted the actual sensor data as to not crowd the code snippet)

initialising:

void initSD() {

  SPI1.setRX(SPI1_SO);
  SPI1.setTX(SPI1_SI);
  SPI1.setSCK(SPI1_SCK);

  // Initialise GPIO pins
  gpio_init(SPI1_CSN);
  gpio_init(CSN_SD2);

  bool success1 = SD.begin(SPI1_CSN, SPI1);
  bool success2 = SD.begin(CSN_SD2, SPI1);

  Serial.print("SD Init ------ ");

  if (success1 & success2) {
    Serial.println("Card initialisation success");
  } else {
    if (!success1) Serial.println("Card 1 initialisation failed, or not present");
    if (!success2) Serial.println("Card 2 initialisation failed, or not present");
    successful_setup = false;
    return;
  }

  // Set SD2 high/inactive, SD1 low/active
  gpio_put(CSN_SD2, 1);
  gpio_put(SPI1_CSN, 0);

  // Open a new file
  sdFile1name = createNewFileName();
  File sdFile1 = SD.open(sdFile1name, FILE_WRITE);

  Serial.print("SD Write ------ ");

  if (sdFile1) {
    // print to file
    Serial.println(String("Successfull with name ") + sdFile1name);
  } else {
    Serial.println("Failed to open file 1 for writing.");
    successful_setup = false;
  }

  sdFile1.flush();
  sdFile1.close();

  // Swap active/inactive lanes
  gpio_put(SPI1_CSN, 1);
  gpio_put(CSN_SD2, 0);

  // Open 2nd file
  sdFile2name = createNewFileName();
  File sdFile2 = SD.open(sdFile2name, FILE_WRITE);

  if (sdFile2) {
    // print to file
  } else {
    Serial.println("Failed to open file 2 for writing.");
    successful_setup = false;
  }

  sdFile2.flush();
  sdFile2.close();

  // Set both high/inactive
  gpio_put(CSN_SD2, 1);
}

toggling and writing while code is running:

void storeDataOnSD(DateTime now) {
  // Set CSN low/active
  gpio_put(SPI1_CSN, 0);

  File sdFile1 = SD.open(sdFile1name, FILE_WRITE);

  if (!sdFile1) {
    Serial.println("sdFile1 not open");
    return;
  } else {
    Serial.println("sdFile1 open");
  }
  digitalWrite(SD_1_LED, HIGH);

  // write sensor code to SD card

  num_SD1_entries++;

  digitalWrite(SD_1_LED, LOW);
  sdFile1.flush();
  sdFile1.close();

  // Set CSN low/active for CSN2 and high/inactive for CSN1
  gpio_put(SPI1_CSN, 1);
  gpio_put(CSN_SD2, 0);

  File sdFile2 = SD.open(sdFile2name, FILE_WRITE);

  if (!sdFile2) {
    Serial.println("sdFile2 not open");
    return;
  } else {
    Serial.println("sdFile2 open");
  }
  digitalWrite(SD_2_LED, HIGH);

  // write sensor code to sd card

  num_SD2_entries++;
  digitalWrite(SD_2_LED, LOW);
  sdFile2.flush();
  sdFile2.close();

  // Set both to high/inactive
  gpio_put(CSN_SD2, 1);
}

Thanks in advance!

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