arduino-libraries / SD

SD Library for Arduino
http://arduino.cc/
GNU General Public License v3.0
179 stars 155 forks source link

ESP32-CAM can't upload new IDE sketch after an sd initialization.. SPI&SD keep MISO level HIGH #111

Closed qbox4u closed 2 years ago

qbox4u commented 2 years ago

The ESP32-CAM has a SD-micro slot on the main board. and is mounted on a ESP32-CAM-MB board for providing serial communication. (eg https://universal-solder.ca/wp-content/uploads/2021/03/5083_b61d93cb-2cb4-4503-b537-ca77679cc7ad0.jpg)

The SD is blocking a new reload of a Sketch after the SD-micro has been initialized.

After the following commands the board can't reload a new IDE sketch

  SPI.begin(14, 2, 15, 13);
  if (!SD.begin(13) ) {
    Serial.println("initialization failed!");
    while (1);
  }

Pressing the reset-button of the ESP32-CAM board does not solve the issue as the SD-micro card still blocks a new sketch upload. The only way to reload a new sketch is to remove the SD-micro (basically a power down) after which the MISO signal of the SD card is disabled (high impedance again).....


/*
 *  Device: Ai-Thinker ESP32-CAM
 *  
 *  Schematic SD connections
 * 1  DATA2      HS2DATA2  GPIO12   x
 * 2  CD/DATA3   HS2DATA3  GPIO13   CS    CS
 * 3  CMD        HS2CMD    GPIO15   DI    MOSI
 * 4  3.3v
 * 5  CLK        HS@CLK    GPIO14   SCLK  SCLK
 * 6  GND
 * 7  DATA0      HS2DATA0  GPIO02   DO    MISO
 * 8  DATA1                         x
 * 
 * HS2DATA1I.begin(14, 2, 15, 13);
 * Reboot code
 *  x1 GPIO  5 
 *  x2 GPIO 15 MTDO
 *  x4 GPIO  4
 *  x8 GPIO  2
 *  1x GPIO  0
 *  2x GPIO 12 MTDI
 * 
 * Problem: Can't upload Arduino IDE sketch after First run of initialization of the SD card
*/
#include <SPI.h>
#include <SD.h>
File root;
#define ONBOARD_LED 33    // main board red LED
#define FLASHLIGHT_LED 4  // Main board White LED

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }
  pinMode(ONBOARD_LED, OUTPUT);     // THE LED GOES OFF AFTER WRITING TO HIGH
  pinMode(FLASHLIGHT_LED, OUTPUT);  // THE LED GOES OFF AFTER WRITING TO LOW
  SetupBySPI_SD();
}

void loop() {
  // nothing happens after setup finishes.
  digitalWrite(ONBOARD_LED, !digitalRead(ONBOARD_LED));  // turn the LED off by making the voltage LOW
  delay(100);
}

void SetupBySPI_SD() {
  //https://openlabpro.com/guide/interfacing-microcontrollers-with-sd-card/
  Serial.print("Initializing SD card...");
  // Master Out, Slave In (MOSI) - which is the data going OUT (send) from the master esp32-cam  to the sd-slave
  // Master In, Slave Out (MISO) - which is the data going OUT (send) from the sd-slave into the master
  // SPI.begin(SCK,MISO,MOSI,CS);  ==> (sclk,d0,di,cs)
  SPI.begin(14, 2, 15, 13);
  if (!SD.begin(13)) {
    Serial.println("initialization failed!");
    while (1)
      ;
  }
  Serial.println("initialization by SPI.begin(14, 2, 15, 13) done.");
  root = SD.open("/");
  printDirectory(root, 0);
  Serial.println("done!");
  Serial.println("SPI Terminating.");

  // Lets try to disable the SD again by stopping the SD & SPI
  SD.end();
  SPI.end();

  // Disable the SD by an HIGH CS
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.print("PIN13 : ");
  Serial.println(digitalRead(13));

  // This goes wrong !!!! Signal stays HIGH !!!!!!!
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  //This means that MISO of the SD-micro is not released
  //Lets make it easier to check with my scope.
  //The output will be pulled down at the moment MISO is released from the SD-micro
  pinMode(2, INPUT_PULLDOWN);
  Serial.print("PIN2 : ");
  Serial.println(digitalRead(2));

  // we need a number (>4) of extra clock pulses after CS was disabled before MISO is released !!!!!
  pinMode(14, OUTPUT);
  digitalWrite(14, LOW);
  Serial.print("PIN14 : ");
  Serial.println(digitalRead(14));
  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);
  pinMode(14, OUTPUT);
  digitalWrite(14, LOW);
  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);
  pinMode(14, OUTPUT);
  digitalWrite(14, LOW);
  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);
  pinMode(14, OUTPUT);
  digitalWrite(14, LOW);
  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);
  pinMode(14, INPUT_PULLUP);

  // If the SD and SPI application would run correct, the signal should go LOW !!!!!!!
  // If the SD-micro is not detached correctly, MISO will stay HIGH!!!
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);  //Check if you can set 0. If s0, leave the signal as input
  pinMode(2, INPUT_PULLDOWN);
  Serial.print("PIN2 : ");
  Serial.println(digitalRead(2));
  pinMode(15, OUTPUT);
  digitalWrite(15, LOW);
  Serial.print("PIN15 : ");
  Serial.println(digitalRead(15));

  // Easy method to reboot again
  // ESP.restart();
}

void printDirectory(File dir, int numTabs) {
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) {
      // no more files
      Serial.println("Close Print directory");
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}
per1234 commented 2 years ago

Hi @qbox4u. Thanks for your report.

The 3rd party ESP32 boards platform comes with its own bundled version of the SD and SPI libraries, which have been modified specifically for use with the ESP32 microcontroller. That library code is hosted in the repository of that boards platform:

https://github.com/espressif/arduino-esp32/tree/master/libraries/SD

This repository is for the official Arduino SD library, which is not used when compiling for an ESP32 board. So I think there is a good chance your issue is not relevant to the code hosted in this repository and so I will close it as off topic.

If you finally do determine that the issue is present in the code of this repository in addition to that of the ESP32-specific variant of the library, and also affects the boards this library is used by, please comment here and I'll reopen.

If you need assistance with your project, feel free to post on the Arduino Forum. I'm sure we'll be able to help you out over there:

https://forum.arduino.cc/