How to read ESP32 SPI output timing half clock period earlier

i have a few dirrent sd cards. some models are working with half clock period earlier.

### Current Working Normal SD Card Timing Output

### Not Working SD Card half clock period earlier

The response timing for ACMD41 , which is used to detect the completion status of initialization, value 0x00 is for completion.
From the timing capture by logic analyzer, it is seen that response data comes half clock period Working SD Card, so it is highly suggested that sampling response data
Not Working with half clock period earlier . otherwise, it is possible to get a wrong
data of 0x01 instead of 0x00 .

How I can fix with Not Working SD card SPI read write half clock period earlier ? can anyone to help with this ?


### Current Working Normal SD Card Response Timing

### Not Working Normal SD Card Response Timing

These example are all the response timing for CMD8 , which is used to send SD card interface condition , value 0x08 is the correct response value .
From the timing capture by logic analyzer, it is seen that response data comes half clock period
earlier for Notworking SD Card, so it is highly suggested that sampling response data
for Notworking SD Card with half clock period earlier . otherwise, it is possible to get a wrong
data of 0x10 instead of 0x08 .

Welcome!
You need to look into SPI modes which refer to the different configurations of clock polarity (CPOL) and clock phase (CPHA) used in the Serial Peripheral Interface protocol to control data transmission timing. There are four modes: Mode 0, Mode 1, Mode 2, and Mode 3, each defining how data is sampled and shifted relative to the clock signal. Check the following link:
https://www.analog.com/en/resources/analog-dialogue/articles/introduction-to-spi-interface.html
Hopefully the following answeres your question. Please note I do NOT know how to program your processor, somebody else will have to help.

SPI Mode CPOL CPHA Clock Polarity in Idle State Clock Phase Used to Sample and/or Shift the Data
0 0 0 Logic low Data sampled on rising edge and shifted out on the falling edge
1 0 1 Logic low Data sampled on the falling edge and shifted out on the rising edge
2 1 0 Logic high Data sampled on the falling edge and shifted out on the rising edge
3 1 1 Logic high Data sampled on the rising edge and shifted out on the falling edge
3 Likes

thanks for your update. i have use that 4 SPI Mode with ESP32 SPI Settings. but no good with SD.h library.

#include "FS.h"
#include "SD.h"
#include "SPI.h"

#define SD_CS 5 // Your CS pin

SPIClass spi = SPIClass(HSPI); // Use VSPI or HSPI

void setup() {
  Serial.begin(115200);

  // Start SPI with custom settings
  spi.begin(18, 19, 23, SD_CS); // SCK, MISO, MOSI, CS

  // Adjust SPI settings for SD
  spi.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  // SPI_MODE0 (default): CPOL=0, CPHA=0
  // Try SPI_MODE3 if timing issues

  // Now initialize SD
  if (!SD.begin(SD_CS, spi)) {
    Serial.println("SD Card Mount Failed");
    return;
  }

  Serial.println("SD Card initialized.");

  File file = SD.open("/test.txt", FILE_WRITE);
  if (file) {
    file.println("Testing SPI timing adjustments.");
    file.close();
    Serial.println("File written.");
  } else {
    Serial.println("Failed to open file for writing.");
  }
}

void loop() {
  // nothing
}

if anyone have any idea please let me know