arduino / ArduinoCore-mbed

330 stars 195 forks source link

RP2040 with SD error #606

Open sabas1080 opened 1 year ago

sabas1080 commented 1 year ago

Hi I am trying to use a microSD with the RP2040, but when executing the following example I get the blinking pattern means that the RTOS has crashed

Checking the SD Card SUCCESS!
SD size: 3904897024
SD read size: 512
SD program size: 512
SD erase size: 512
Mounting...
#define SD_MISO      (digitalPinToPinName(PIN_SPI_MISO))
#define SD_MOSI     (digitalPinToPinName(PIN_SPI_MOSI))
#define SD_CLK      (digitalPinToPinName(PIN_SPI_SCK))
#define SD_CS       (digitalPinToPinName(PIN_SPI_SS))

#include <FATFileSystem.h>
#include "COMPONENT_SD/include/SD/SDBlockDevice.h"
#include <mbed.h>

using namespace mbed;
using namespace events;
using namespace rtos;
using namespace std::chrono_literals;

//#include "COMPONENT_SPI/include/SPI/SPIFBlockDevice.h"

void setup() {
    Serial.begin(9600);
    Serial.print("Example SD Card");
    while(!Serial);
    SDBlockDevice bd(SD_MOSI, SD_MISO, SD_CLK, SD_CS);
    FATFileSystem fat("fat");
    Serial.print("Checking the SD Card");
    int err = bd.init();
    if (err != 0) {
        Serial.print(" FAIL!");
        Serial.print("Please, check your SD Card.");
        while(true);
    }
    Serial.println(" SUCCESS!");
    Serial.print("SD size: ");
    Serial.println(bd.size());
    Serial.print("SD read size: ");
    Serial.println(bd.get_read_size());
    Serial.print("SD program size: ");
    Serial.println(bd.get_program_size());
    Serial.print("SD erase size: ");
    Serial.println(bd.get_erase_size());

    Serial.print("Mounting... ");
    err = fat.mount(&bd);
    Serial.println(err ? "Fail :(" : "OK");
    if (err) {
        //Error();
        // Format and remount
        Serial.println("No filesystem found, formatting...");
        err = fat.reformat(&bd);
        if (err) {
            //Error();
            while (1)
                ;
        }
    }

    Serial.println("Hello, FAT!");

}

void loop() {
  // put your main code here, to run repeatedly:

}

Thanks

hpssjellis commented 1 year ago

@sabas1080 I can't really help with your microSd card, but here is some arduino MBED code that does work but with the PortentaH7 with vision shield that has an sd card. I hope parts of it can be helpful. The library is here


/*
  Portenta - TestSDCARD

  The sketch shows how to mount an SDCARD and list its content.
  then add a file.

  Note: SD Card seems finisky, takes a few times to load the filesystem

  The circuit:
   - Portenta H7 + Vision Shield
   or
   - Portenta H7 + Portenta Breakout

  This example code is in the public domain.
*/
#include "SDMMCBlockDevice.h"
#include "FATFileSystem.h"

SDMMCBlockDevice block_device;
mbed::FATFileSystem fs("fs");

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);

  delay(5000); // time to connect serial
  Serial.println("Connect serial if needed");
  delay(5000);
  //while (!Serial);  // blocking call

  Serial.println("Mounting SDCARD...");
  int err =  fs.mount(&block_device);
  if (err) {
     Serial.println("No SD Card filesystem found, please check SD Card on computer and manually format if needed.");
     // int err = fs.reformat(&block_device);  // seriously don't want to format your good data
  }

}

void loop() {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // flips LED on and off

  // Make a folder if needed
  mkdir("fs/myFolder2",0777);                     // 0777 full access permissions linux style 

  char myFileName[] = "fs/myFolder2/test2.txt";   // "fs/" needs to be there, think fileSystem

  FILE *myFile = fopen(myFileName, "w");          // "a" for append (add to file), "w" write, "r" read ??

     Serial.println(myFileName);
     fprintf(myFile,"Test how cool this is \r\n");
     fprintf(myFile,"Also this line \r\n");

  fclose(myFile);

  Serial.println("------------------------- Done Writing file --------------------------------");
  delay(10);

  unsigned char c; 
  FILE *fp = fopen(myFileName, "r");              // "r" read only
     while (!feof(fp)){                           // while not end of file
        c=fgetc(fp);                              // get a character/byte from the file
        //printf("Read from file %02x\n\r",c);    // and show it in hex format
        Serial.print((char)c);                    // show it as a text character
     }
  fclose(fp); 
  Serial.println("------------------------- Done Showing file --------------------------------");

  delay(10000);   // wait a bit

}