Cubecell AB02 - Read/Write on SD card with SPI1 bus

Hi everyone,

obviously, I am not the only one to post this kind of subject, but I’ve tested everything I’ve found on this forum and I still struggle to make an SD card reader work…

It works with the “File info”, as described on this post.

I also managed to make it work on the SPI1 bus, with “file info” exemple again.

But when I tried the “read/write” example, it soedn’t works… Actually, it works sometimes, only once and then I have to plug it again. my code is below :slight_smile:. I tried different speeds of SPI bus.
I also tried to remove the “read” part. Still the same : Sometimes it Works, sometimes not.

Here is an example of my serial monitor, when I unplug the re-plug my AB02.

I precise that my SD card reader is an Adafruit, It works perfectly with other arduino boards. The SD card is 8Go and there is no problem as only

Do you have any idea ?

my code :

#include <SPI.h>
#include <SD.h>   //

// --- Objets SdFat bas niveau ---

Sd2Card  card;
SdVolume volume;
SdFile   root;
SdFile   file;

// --- SPI1 pins ---

#define SD_CS   GPIO4
#define SCK1    GPIO3
#define MISO1   GPIO2
#define MOSI1   GPIO1

void setup() {

  Serial.begin(9600);
  while (!Serial);
  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);

  // Init SPI (SPI1)
  SPI.begin(SCK1, MISO1, MOSI1, SD_CS);
  SPI.setFrequency(250000);   // 250 kHz

  Serial.println("card.init...");
  if (!card.init(SPI_QUARTER_SPEED, SD_CS)) {
    Serial.println("card.init FAIL");
    while (1);
  }

  Serial.println("card.init OK");

  if (!volume.init(card)) {
    Serial.println("volume.init FAIL");
    while (1);
  }

  Serial.println("volume.init OK");
  root.openRoot(volume);

  // --- WRITE ---

  if (!file.open(root, "TEST2.TXT", O_CREAT | O_WRITE | O_TRUNC)) {
    Serial.println("open WRITE FAIL");
    while (1);
  }

  file.println("testing 1, 2, 3.");
  file.sync();
  file.close();
  Serial.println("write OK");

  // --- READ ---

  if (!file.open(root, "TEST2.TXT", O_READ)) {
    Serial.println("open READ FAIL");
    while (1);
  }

  Serial.println("content:");
  int c;
  while ((c = file.read()) >= 0) {
    Serial.write(c);
  }

  file.close();
  root.close();
  Serial.println("\nDONE");
}

void loop() {
}

Have you seen this thread on working with an SD card reader with the CubeCell platform? Your problem sounds exactly like what is described there.

Thank you @UniquePete, I’ll give it a try this afternoon. However, it looks like the problem your a citing is more about “how to make the SPI1 work” : it looks like I have no problem with that as it already worked once. So I assume I have no pin problem, no SPI start problem.

In the thread you are citing, did you manage to write/read on your SD card ?

The short answer is yes, I have a CubeCell reading and writing reliably to an SD card.

There are two issues discussed in the referenced thread. One is the issue relating to the use of SPI1, but the other is the very issue you describe: unreliable writing to the card—sometimes it works, sometimes it doesn’t. My investigations pointed to [SPI library] code that appeared not to be properly tuned to the [faster] ASR6502 processor. The fix is effectively to increase the timeout that dictates whether or not an action on the SPI bus has been successful. The CubeCell [V2] was timing out too soon, most of the time. Occasionally, the action completed before the timeout period, as originally set, expired, but most often it didn’t.

The fix described in that thread, however, should be in the current CubeCell support code… but you can easily check that, and make the necessary change if required, by following the advice offered in the associated discussion:

@UniquePete, you are a hero !!

Thank you very much, I went to C:Users\<me>\AppData\Local\Arduino15\packages\CubeCell\hardware\CubeCell\1.5.0\cores\asr650x\SPI.cpp

to change the timeout to 4000

Thank you so much !
If you give my your adress in MP I’ll send you a gift !

We’re all here to help. The knowledge that we helped solve your problem is gift enough.