SD append to file fails to write when called from inside a function

ienaxxx
Posts: 1
Joined: Mon Dec 04, 2023 6:21 am

SD append to file fails to write when called from inside a function

Postby ienaxxx » Mon Dec 04, 2023 9:36 pm

Hello everyone, I'm new to this forum.
long story short, when i call the SD Functions from inside setup, it works like a charm, but when calling from inside a function, called from another function, it doesn't. It can't open the file for appending (nor for writing).
The https://github.com/espressif/arduino-e ... D_Test.ino sketch works perfectly.

I use Arduino IDE to program My ESP32 NodeMCU-32s. Trying to reorganize some code here, I created a function "exec", that calls other functions when a command is received via Serial. This function will call the function that matches with the received command to behave accordingly. I wrote the functions in a secondary file and let the main program handle the

here the main file:

Code: Select all

#include <EEPROM.h>
#include <FS.h>
#include <SD.h>

#define CCBUFFERSIZE 64
#define RECORDINGBUFFERSIZE 4096   // Buffer for recording the frames
#define EPROMSIZE 512              // Size of EEPROM in your Arduino chip. For ESP32 it is Flash simulated so very slow
#define BUF_LENGTH 128             // Buffer for the incoming command.

// defining PINs set for ESP32 WROOM module

byte sck = 18;     //  GPIO 18
byte miso = 19;    //  GPIO 19
byte mosi = 23;    //  GPIO 23
byte ss = 5;       //  GPIO 5
int gdo0 = 4;      //  GPIO 2
int gdo2 = 2;      //  GPIO 4

byte SDchipSelectPin = 17;
File myFile;


void setup() {
    // First of all, let' bring the MicroSD Card CS PIN HIGH, to disable it.
    pinMode(SDchipSelectPin, OUTPUT);
    digitalWrite(SDchipSelectPin, 255);

    pinMode(ss, OUTPUT);
     // initialize USB Serial Port CDC
     Serial.begin(115200);

    //Init EEPROM - for ESP32 based boards only
     EEPROM.begin(EPROMSIZE);     

     // initialize CC1101 module with preffered parameters
     cc1101initialize();

      if (ELECHOUSE_cc1101.getCC1101()) {  // Check the CC1101 Spi connection.
      Serial.println(F("cc1101 initialized. Connection OK\n\r"));
      } else {
      Serial.println(F("cc1101 connection error! check the wiring.\n\r"));
      };

      // setup variables
     bigrecordingbufferpos = 0;

     
      delay(500);
      if(!SD.begin(SDchipSelectPin)){
          Serial.print(F("Card Mount Failed. CS PIN IS "));
          Serial.print(SDchipSelectPin);
       } else {
        initCard();
       }
      
       myFile = SD.open("/foo.txt", FILE_APPEND);
       if(!myFile){
         Serial.println(F("failed to open file"));
       }
       myFile.print("qweuiyqwe87q98we7h1i23hqwlkjeuqj2e");
      myFile.print("\n");
       myFile.close();
  
}
void loop() {
  int i = 0;
  while (Serial.available()) {
   handleCLIMode();
  }; // end while Serial Available

  /* Process RF received packets  (if received) */
  ccProcessReceivedPackets();   

}  // end of main LOOP

and here the relevant part of the other file

Code: Select all


void ccSave2SD(){
  Serial.println(F("Saving to file, please wait..."));
  
  // spiEnableCC1101(false);
  // spiEnableSD(true);
  delay(500);
  // take 32 bytes at a time, convert To HEX and print it inline in the file.
  SD.begin(SDchipSelectPin);
  File myFile=SD.open("/foo.txt", FILE_APPEND);
  if(!myFile){
    Serial.println(F("unable to open file"));
    return;
  }
  myFile.print("signal:");
   myFile.print("\n");// at the end go for it
   myFile.close();

  delay(300);
  // spiEnableSD(false);
  // spiEnableCC1101(true);
}

static void exec(char *cmdline)
{ 
        
    char *command = strsep(&cmdline, " ");
    int setting, setting2, len;
    byte j, k;
    float settingf1;
    float settingf2;
    if (strcmp_P(command, PSTR("help")) == 0) {
 	Serial.println(F("help function"));
 	// ... many more cases here.
  } else if (strcmp_P(command, PSTR("save2sd")) == 0) {
      //start saving recording buffer content into EEPROM non-volatile memory 
      ccSave2SD();
      Serial.print(F("\r\nSaving complete.\r\n\r\n"));
    } else {
        Serial.print(F("Error: Unknown command: "));
        Serial.println(command);
    }

The problem is: each time the program is executed the file gets "qweuiyqwe87q98we7h1i23hqwlkjeuqj2e" appended to it, but when it comes to the function part it keeps returning "unable to open file" and nothing else is written to the file.

I dont' know why...

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: SD append to file fails to write when called from inside a function

Postby MicroController » Sun Dec 10, 2023 12:53 am

Code: Select all

SD.begin(SDchipSelectPin);
should only be called once, e.g. in setup().

Who is online

Users browsing this forum: No registered users and 142 guests