SdFat error 2 (SDIO read CID)

Hi. First of all, mandatory I'm new to the forum, so sorry if I'm doing something wrong or not providing enough info.

I'm trying to read a BMP file to display it in a 2.4" TFT screen, using a Seeeduino XIAO and SdFat. At this moment though, I cannot read a single byte from said file.

The complete hardware list is:

  • Seeeduino XIAO.
  • Micro SDHC card (16 GB, FAT32, formatted as the sticky post in this forum says). I tried two different copies of this card, and a 64 GB Samsung EVO Plus formatted exFat, without any changes.
  • Generic micro SD card reader (I bought it in eBay, it is the most generic 5V SPI one you can find).
  • 240x320 2.4" TFT display, with ST7789 IC (bought it from AliExpress). It shouldn't affect anything regarding the card, but I'll put it here just in case. In the link it says that the screen uses the ILI9341, but that's just not true.

I'm using the Adafruit ST7889 "shieldtest" example, which I modified to work with 8 bpp images. Here it is without the screen interfacing part:

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <SdFat.h>

#define SD_CS 2

SdFat sd;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    yield();
  }

  if (!sd.begin(SD_CS)) {
    Serial.println(F("SD begin() failed"));
    while (1); // Fatal error, do not continue
  }

  if (sd.exists("/24231.bmp")) {
    Serial.println(F("Found it"));
  }
  if (!sd.exists("/24231.bmp")) {
    Serial.println(F("Couldn't find it"));
    while (1); // Fatal error, do not continue
  }

  bmpDraw();
}

#define BUFFPIXEL 20
#define TABLE_OFFSET 54

void bmpDraw() {
  SdFile bmpFile;
  //uint32_t bmpImageOffset;
  //uint32_t numberOfColors;
  uint8_t sdbuffer[240];
  uint8_t buffidx = sizeof(sdbuffer);
  uint8_t r, g, b;

  if (!bmpFile.open("/24231.bmp")) {
    Serial.println(F("Error opening"));
    return;
  }


  Serial.println(bmpFile.getError(), HEX); // this returns 0
  Serial.println(bmpFile.read()); // this returns -1
  Serial.println(bmpFile.getError(), HEX); // this returns 2
  /*bmpFile.seek(10);
    bmpFile.read(&bmpImageOffset, 4);
    Serial.println(bmpImageOffset);
    bmpFile.seek(46);
    bmpFile.read(&numberOfColors, 4);
    Serial.println(numberOfColors);*/

  bmpFile.seek(TABLE_OFFSET);

  Serial.println(bmpFile.read()); // this returns -1

  bmpFile.seek(TABLE_OFFSET);
  uint8_t colorTable[1024];
  uint16_t tableidx;
  Serial.println(bmpFile.read(colorTable, sizeof(colorTable))); // this returns -1

  for (int i = 0; i < 5; i++) {
    // I'm printing the first 5 elements of the color table to check if they are correct
    Serial.println(colorTable[i]); // this returns 37, 203, 96, 176, 45
  }

  bmpFile.seek(1078); // to the start of the pixel array

  for (int row = 0; row < 240; row++) {
    for (int col = 0; col < 240; col++) {
      if (buffidx >= sizeof(sdbuffer)) { // check if the buffer has been completely read
        bmpFile.read(sdbuffer, sizeof(sdbuffer)); // reading pixels to the buffer
        buffidx = 0;
      }

      tableidx = sdbuffer[buffidx]; // each pixel from the array is an index to the color table
      tableidx *= 4; // multiplying it so the index is pointing to the start of the pixel data
      // in 8 bit bmp files, the colors in the table are stored b, g, r, 0
      b = colorTable[tableidx++];
      g = colorTable[tableidx++];
      r = colorTable[tableidx++];

      if (row == 1 && col < 20) { // checking the 20 first read pixels
        /* This returns (I'm only putting the first 5):
             Entry 172: 0, 0, 0
             Entry 131: 0, 12, 196
             Entry 216: 0, 7, 60
             Entry 98: 145, 118, 121
             Entry 161: 0, 100, 79 */
        Serial.print("Entry ");
        Serial.print(sdbuffer[buffidx]);
        Serial.print(": ");
        Serial.print(r);
        Serial.print(", ");
        Serial.print(g);
        Serial.print(", ");
        Serial.println(b);
      }

      buffidx++;
    }
  }

  bmpFile.close();

  Serial.println(F("All finished here"));
}

void loop() {}

I put comments in the code documenting the output of most of the prints, and I can provide the image file and the Python script I used to check if it was correct on demand (I cannot attach them directly as I'm too new in the forum).

I also ran the SdFat examples "bench", "SdInfo" and "QuickStart", and I used the "SdFormatter" example to test if it would change anything. Every example worked flawlessly, and the "bench" wrote and read from the cards without an issue.

At last, I also run the "SdErrorCodes" examples, and it says that error number 2 is "SDIO read CID". I tried to look the meaning of this, but all I've learned is that SDIO is some standard and the CID is the identifier of the card, so I don't really know why it can't read my file.

Thanks in advance, any help is welcome.

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