Teensy LittleFS and SD file names.

sbfreddie

Well-known member
I am hoping one you C gurus can help me with this issue:
When trying to open a new file for write in LittleFS it seems to want a const char for a file name. However I would like to append the date for each new file I create each day a midnight like this: datafile-mm/dd/year.
I have tried a variety of methods to do this with little success.
I tried creating a String and then adding all the characters into it and then using file.open of LittleFS, but it did not like that.
So I tried to convert the String with strcpy( FileNameChar, FileNameString.c_str() ); it did not like that ether.
I though I saw a forum post about this awhile back but could not find it.
Anyone with any Ideas?

Regards,
Ed
 
It works fine with Null terminated C strings.
This from example: ...\hardware\teensy\avr\libraries\LittleFS\examples\Test_Integrity\PROG\functions.ino
"char szPath[150];" is built with sprintf() - other logic removed - but this shows working steps in some fashion:
Code:
...
uint32_t fileCycle(const char *dir) {
	static char szFile[] = "_file.txt";
	char szPath[150];
	int ii;
	lCnt++;
	byte nNum = lCnt % MAXNUM;
	char chNow = 'A' + lCnt % MAXNUM;
	lfs_ssize_t resW = 1;
	uint32_t timeMeAll = micros();

	if ( dir[1] == 0 )	// catch root
		sprintf( szPath, "/%c%s", chNow, szFile );
	else
		sprintf( szPath, "%s/%c%s", dir, chNow, szFile );
...
	Serial.printf( ":: %s ", szPath );
...
		file3 = myfs.open(szPath, FILE_WRITE);
...
 
Now I have another problem with LittleFS, it creates the FS but I cannot get it to create a root directory.
I ran the ExtRam test from Pauls Web page and it passed all tests.
Attached is the test program that is a small part of the large it came out of.View attachment LittleFS Example.txt

Could somebody point me to my mistakes?

Regards,
Ed
 
quick look - side note:
Code:
//    Serial.print(CrashReport);

    long unsigned debug_start = millis ();
    while(!Serial && ((millis () - debug_start) <= 5000UL))
        {
            yield();
        }

[B]    Serial.print(CrashReport);[/B] // to be sure print isn't missed this should be AFTER a wait when needed

Create Root directory? The dir 'root' exists?

Was that meaning a directory on the root?

p#2 shows making a file on the root - versus in a directory on the root

From that same p#2 linked file is this:
Code:
#define NUMDIRS 2  // Number of Directories to use 0 is Rootonly

void makeRootDirs() {
	char szDir[16];
	for ( uint32_t ii = 1; ii <= NUMDIRS; ii++ ) {
		sprintf( szDir, "/%lu_dir", ii );
		myfs.mkdir( szDir );
	}
}
 
Defragster:
After taking care of the Root issue, I now have another issue.
I can successfully create a subdirectory called "EnvironmentalData" with no problem, it even shows up on my Mac using MTP.
How ever I seem to be having difficulty creating a file inside of the "EnvironmentalData" directory. What method do I use to move into the Subdirectory? I have tried opening it but it does not seem to like this method.
( File subdir = sdSDIo_Open("EnvironmentalData") ) always yields fail when I check for its presents. I have tried putting a forward slash (/) before the subdirectory name and after the subdirectory name, and even before and after the subdirectory name, but none of these methods works.
How do I move into a sub directory to create and/or write into the file inside the subdirectory?

Thanks,
Ed
 
Try something like:

File subdir = sdSDIo_Open("/EnvironmentalData/mydata.txt", FILE_WRITE_BEGIN);

If that doesn't solve your problem, try showing us a small but complete program so we can see and reproduce the problem. We're able to help much faster and much better when we're able to see the problem. If it does turn out to be something difficult, being able to actually run it on a Teensy is really needed to get to the bottom of whatever is really wrong.
 
Paul & et all:
This program is originally 6580 lines of code, however I have trimmed it down to the bare basics, and still sort of doing the same thing as the original program.
It is supposed to create/initialize a RAMDisk drive and a SDCard, and then log data from all the sensors the original source has into the RAMDisk for 24 hrs. I have shortened it to 2 minutes for debugging.
After the 2minutes (or 24hrs) it copies the RAMDisk data file to the SDCard into a subdirectory named EnvironmentalData and then changes the name of the original RAMDisk file clears it of data and starts logging again.

Attached is the Arduino folder with the code.


View attachment LittleFS_SDCard_Test.ino


Regards,
Ed
 
@sbfreddie - Been working with the sketch in post #10. First off is this what you are trying to accomplish:
Code:
************************************************
destinationSDCardDir = EnvironmentalData/EdsEnvSysData-3-3-2023.csv
DestinationFile = EdsEnvSysData-3-3-2023.csv
************************************************
This is the total file path for the Destination File: EnvironmentalData/EdsEnvSysData-3-3-2023.csv
This is the total size of the file path for the Destination File: 60
sdCardDestFileNamePresent = 0
************************************************
Creating the destination file for Write on the SD Card...
Successfully created the destination file...
EnvironmentalData/EdsEnvSysData-3-3-2023.csv Started with 0 bytes

Destination file on sd card is open and ready for transfer...
Checking for the existance of the source file, is it open??...
The source file is not open, Open it first...
Source file in the PSRAM Ram Disk is ready for transfer...
Source File Size is: 1128 Bytes in size.

STARTING the Copy from LittleFS to SD Card Transfer...
*.*.*.Closing the source and destination files...
[COLOR="#FF0000"]2023-03-19 11:17          0 EnvironmentalData/
  2023-03-19 11:19       1128 EdsEnvSysData-3-1-2023.csv
  2023-03-19 11:22       1128 EdsEnvSysData-3-2-2023.csv
  2023-03-19 11:25       1128 EdsEnvSysData-3-3-2023.csv[/COLOR]
Press anykey to continue

The first issue I noticed was:
Code:
        // Need to open Destination Directory before we can create a file inside it.
//        sdCardDestinationDirectory = sdSDIO.open(destinationSDCardDir, FILE_WRITE);
//        if (!sdCardDestinationDirectory)
//            {
                // The SD Card Destination Directory did not open.
//                #if defined(DEBUG_COPY_RAMDISK_TO_SDCARD_SUB)
//                    Serial.println(F("Error - Opening the Sub Directory Failed...\n"));
//                #endif
//                sdCardDestDirectoryPresent = false;
//
//                return false;
//            }

I commented this out in both places it was found, "setup()" and "copyDataFileFromRAMDiskToSDCard()". This code was failing and is not needed. You do not need to open the directory to use it. You are using a fully qualified path name that includes the directory path for the cvs destination file that is copied to the SDIO card. If you change to the destination directory SdFat will remember it. Also, "sdCardDestFileNamePresent = false;" needed to be added to "copyDataFileFromRAMDiskToSDCard()" so that the first cvs file is not reused.

Hopefully I understand what you are trying to do. The modified sketch from post #10 is posted bellow. Everything I changed is not indented so that it will be obvious. One little function to wait for a key press was added to the end of the sketch as well.

View attachment LittleFS_SDCard_Test.ino

Let me know if this helped:)

Edit: You will need to press a key to create the next file. Each time you do this the SDIO directory is displayed...
 
Last edited:
Watson:
Thanks for your trouble and advice.
I have rewritten the parts concerning the subdirectory, instead of trying to open a subdirectory (which cannot be done because it always fails) I found the part in the underlying SdFat where I can change directories (chdir).
So I rewrote the parts about changing to the subdirectory with chdir command and all is working well concerning this part of the code.

I am surprised that @Paul did not choose to include the chdir command in his SD shell around SdFat, as every DOS I have worked with for the past 50 years has had some form of change directory command whether it was "cd" or chdir. Even the first version of DOS created by Adam Osborne and published in Byte magazine in the late 70's (and later stolen by Bill Gates lock, stock and barrel) had this command.

Now the only part I am having trouble with is the new files showing up in MTP type disk drive that shows up on my Mac's desktop.

But that is on another thread which you have been a big part of: https://forum.pjrc.com/threads/6813...ion-including-MTP-and-MSC?p=322553#post322553

Thanks again,
Ed
 
Watson:
Thanks for your trouble and advice.
I have rewritten the parts concerning the subdirectory, instead of trying to open a subdirectory (which cannot be done because it always fails) I found the part in the underlying SdFat where I can change directories (chdir).
So I rewrote the parts about changing to the subdirectory with chdir command and all is working well concerning this part of the code.

I am surprised that @Paul did not choose to include the chdir command in his SD shell around SdFat, as every DOS I have worked with for the past 50 years has had some form of change directory command whether it was "cd" or chdir. Even the first version of DOS created by Adam Osborne and published in Byte magazine in the late 70's (and later stolen by Bill Gates lock, stock and barrel) had this command.

Now the only part I am having trouble with is the new files showing up in MTP type disk drive that shows up on my Mac's desktop.

But that is on another thread which you have been a big part of: https://forum.pjrc.com/threads/6813...ion-including-MTP-and-MSC?p=322553#post322553

Thanks again,
Ed

Glad you have it working. I am running under Linux and I also had to un-mount and re-mount the MTP drive to get an updated copy of the directory listings. I seem to remember that @KurtE had a way of refreshing it from the serial console but that's been a while back.

I think it was back in 76 when I first started with computers. The Microace 2KB kit:)
 
Wwatson:

Ahhh yes, I barely remember back in 1967-68 in High School when I wrote my first simple program with punch cards on teletype connected to the off site IBM mainframe downtown. 1970 I had to write my programs in Octal while I was in the Navy.
I did a 4 year stint with Borroughs Corp in the late 70's where I learned to write a little COBOL, also learned to write in Assembly for the 8080. Created my own computer system with a DOS copied from the very expensive (Costs more than my house) Intel Corp. IDE I used to develop software for the 8080 & 8085 in the early 80's.

It was all down hill from then on. I was mostly a hardware designer, but did my coding on the side for fun.

Did not learn C until MacOS 10 first came out around 2000, I used it mostly for writing code with Xcode for the AVR card that I picked up somewhere, I don't remember where.

Now in my old age >70 I do it mostly to help keep the brain working, seems to do the trick.

Regards
Ed
 
Wwatson:

Ahhh yes, I barely remember back in 1967-68 in High School when I wrote my first simple program with punch cards on teletype connected to the off site IBM mainframe downtown. 1970 I had to write my programs in Octal while I was in the Navy.
I did a 4 year stint with Borroughs Corp in the late 70's where I learned to write a little COBOL, also learned to write in Assembly for the 8080. Created my own computer system with a DOS copied from the very expensive (Costs more than my house) Intel Corp. IDE I used to develop software for the 8080 & 8085 in the early 80's.

It was all down hill from then on. I was mostly a hardware designer, but did my coding on the side for fun.

Did not learn C until MacOS 10 first came out around 2000, I used it mostly for writing code with Xcode for the AVR card that I picked up somewhere, I don't remember where.

Now in my old age >70 I do it mostly to help keep the brain working, seems to do the trick.

Regards
Ed

Will be 70 and retired in November:) And you are right, It does keep the brain sharp...
 
Back
Top