Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help to create a new file in Sd2Card. #32

Closed
schirrel opened this issue May 12, 2016 · 2 comments
Closed

Help to create a new file in Sd2Card. #32

schirrel opened this issue May 12, 2016 · 2 comments

Comments

@schirrel
Copy link

I used Marlin FIrmware to 3d printer, and i want to save a file on its sd and it use sdfat.
The card fortmat is Sd2Card.
I tried to follow and exemple that is there:

void CardReader::write_new_file(char *buf, char* filename) {
  SdFile myNewFile;
//   if (!myNewFile.open(root, filename, O_RDWR)) {
//    SERIAL_ECHO_START;
//    SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
//    SERIAL_ECHOLN(filename);
//  }
  if (!myNewFile.open("teste.txt", O_CREAT | O_EXCL | O_WRITE)) {
    SERIAL_ECHO_START;
    SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
    SERIAL_ECHOLN(filename);
  }
  char* begin = buf;
  char* npos = 0;
  char* end = buf + strlen(buf) - 1;

  myNewFile.writeError = false;
  if ((npos = strchr(buf, 'N')) != NULL) {
    begin = strchr(npos, ' ') + 1;
    end = strchr(npos, '*') - 1;
  }
  end[1] = '\r';
  end[2] = '\n';
  end[3] = '\0';
  myNewFile.write(begin);
  if (myNewFile.writeError) {
    SERIAL_ERROR_START;
    SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  }
}

And it didn't work.
So i want to know the correct way to write a file at Sd2Card because all the exemples i found was about sdfat card format.
Thanks

@pavel-perina
Copy link

pavel-perina commented Jun 16, 2016

Same here, I've attached almost minimal sketch to reproduce bug. Might be in my code, perhaps I forgot to initialize something. file.open either resets arduino or it halts and initialization often takes 20-25ms (i'm used to 100ms) ... perhaps it depends if card was initalized already or disconnected from power before test). Anyways, file.open never returns.
I will try to copy sdout stream from getline example, at least example works.

Failing code: sketch_jun17a.zip
Warning: code sets pin 4 to output and high ... just in case you have something there

PS: ofstream fails to open file and ofstream.good() is false, I have certainly some silly error in my code, but at least it does not crash.

@greiman
Copy link
Owner

greiman commented Jun 17, 2016

pavel-perina,

Change this:
if (sd.cardBegin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {

To this:
if (sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {

And this will be the result:

Boot up ...
INFO: sd.cardBegin() took 18ms
YYYY-MM.csv
File opened
Line printed
File written
OK

@greiman greiman closed this as completed Jun 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants