0

I can use a RingBuf from the SdFat library only inside the setup() function and I don't understand why. Can RingBuf and File32 be defined as global and configured only once during the setup? I'm working on Teensy 4.1.

For instance, this code works

#include "sdios.h"
#include "SdFat.h"
#include "RingBuf.h"

#define SD_CONFIG  SdioConfig(FIFO_SDIO)        // Use Teensy SDIO

File32  stateFile;
RingBuf<File32, 5> outRingBuff;

// SETUP function
void setup() {
  SdFat32 sdCard;
  bool retValBool;
  
  Serial.begin(9600);                 // Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored

  Serial.println("Initialization SD card START");

  // SD Initialization
  if (!sdCard.begin(SD_CONFIG)) {
    sdCard.initErrorHalt(&Serial);
    Serial.println("ERROR SD Initialization failed!");
  } else {
    Serial.println("Initialization SD card DONE");

    // Output file opening
    retValBool = stateFile.open("aa.csv", O_RDWR | O_CREAT | O_TRUNC);

    // Output file opening
    if (retValBool) {
      outRingBuff.begin(&stateFile);
      
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');

      size_t numBytes = outRingBuff.writeOut(4);

      Serial.println(numBytes);

      outRingBuff.sync();
      stateFile.sync();
      stateFile.truncate();
    }
  }
}

// LOOP function
void loop() {
      Serial.print("loop");

      delay(800);
}

But this code does not work, without retrieving any kind of compiler errors, warnings etc

#include "sdios.h"
#include "SdFat.h"
#include "RingBuf.h"

#define SD_CONFIG  SdioConfig(FIFO_SDIO)        // Use Teensy SDIO

File32  stateFile;
RingBuf<File32, 5> outRingBuff;

// SETUP function
void setup() {
  SdFat32 sdCard;
  bool retValBool;
  
  Serial.begin(9600);                 // Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored

  Serial.println("Initialization SD card START");

  // SD Initialization
  if (!sdCard.begin(SD_CONFIG)) {
    sdCard.initErrorHalt(&Serial);
    Serial.println("ERROR SD Initialization failed!");
  } else {
    Serial.println("Initialization SD card DONE");

    // Output file opening
    retValBool = stateFile.open("aa.csv", O_RDWR | O_CREAT | O_TRUNC);

    // Output file opening
    if (retValBool) {
      outRingBuff.begin(&stateFile);
      
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');

      size_t numBytes = outRingBuff.writeOut(4);

      Serial.println(numBytes);

      outRingBuff.sync();
      stateFile.sync();
      stateFile.truncate();
    }
  }
}

// LOOP function
void loop() {
      Serial.print("loop");
      
      outRingBuff.write('A');
      outRingBuff.write('A');
      outRingBuff.write('A');
      outRingBuff.write('A');

      size_t numBytes = outRingBuff.writeOut(4);

      Serial.println(numBytes);
      
      outRingBuff.sync();
      stateFile.sync();
      stateFile.truncate();
      
      delay(800);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
JJAlberto
  • 23
  • 5
  • Exactly what compiler errors or warnings are you getting, and what linea of your code do they point out? – lurker May 07 '21 at 02:40
  • No errors or warnings found. However, I've just discovered that -fpermissive flag is used by default in Arduino IDE. Could it be the reason why It does not raise any error? – JJAlberto May 07 '21 at 08:39
  • 1
    You said "this code doesn't work". It's a little difficult guessing what that means. And, perhaps the flag may cause the lack of error. – lurker May 07 '21 at 17:41
  • You're right. The code freezes, without doing anything, even the simple println `Serial.println("Initialization SD card START");` in the setup. And then, when I try to upload a new software on the microprocessor, I have to restart the board because it is not accessible. – JJAlberto May 10 '21 at 12:26

1 Answers1

0

I have found the problem. The SdFat32 object is defined inside the setup() function so it is inaccessible inside the loop() function. Likely, it is not directly called neither by the RingBuf or the File32 object so it does not retrieve any error, however it is not possible to save data on SD while the SdFat32 object is not accessible.

In my opinion this behaviour should be fixed, I think an error or warning should be raised.

JJAlberto
  • 23
  • 5
  • The code you showed in your original question has `loop` accessing external variables `outRingBuff` and `stateFile`, both of which are globally defined. The definition of `SdFat32` is global (defined by the header), so you must be talking about the instance of `SdFat32` you have declared in `setup()`, namely, `sdCard`? In `setup()` after your `sdCard.begin()` call you make no further reference to `sdCard`, either in `setup()` or `loop()`. If you were intending to use the SD Card, perhaps that's the issue. All I have to go on is the code you posted. If it's changed, then... – lurker May 07 '21 at 17:47