MTP Responder Contribution

@mjs513
OK, for multiple cards you have to do two things in main program (*.ino)
- Making sure SPI port definitions are correct (the values in MTP_test are for Teensy 4.1, (added a note))
- changing the constants cs and sd_str
It should work, as I'm doing test right now
BTW, I added LittleFS_RAM on my device list and it seems to work

Edit: Just checked it with only a single SPI disk, and it worked
Edit: It is useful also to check the SPI speed

First this is the configure I am using:
Code:
  // edit SPI to reflect your configuration
  #define SD_MOSI 11
  #define SD_MISO 12
  #define SD_SCK  13

//  const char *sd_str[]={"sdio","sd1","sd2","sd3","sd4","sd5","sd6"}; // WMXZ example
//  const int cs[] = {BUILTIN_SDCARD,34,33,35,36,37,38}; // WMXZ example

//  const char *sd_str[]={"sdio","sd6"}; // WMXZ testing
//  const int cs[] = {BUILTIN_SDCARD,38}; // WMXZ testing
  const char *sd_str[]={"sdio", "sd1"};      // edit to reflect your configuration
  const int cs[] = {BUILTIN_SDCARD, 2};  // edit to reflect your configuration
  const int nsd = sizeof(cs)/sizeof(int);
no other changes to mtp-test.

Ok - got it working by changing the clock to 16Mhz from 33Mhz. Everything else was fine.

I do have a question. Does each of the Sd cards have its own MTPIndex file or do you just use 1 one drive 0?
 
I do have a question. Does each of the Sd cards have its own MTPIndex file or do you just use 1 one drive 0?

I decided to have one Index file in drive 0. and use the storage indicator of MTP to switch between drives.
Not sure if this is the best solution, so time will see.

Re SPI speed: 33 MHz was the max speed my hardware was working with 32GB disks. So that may needs to be adjusted.
Another reason to keep configuration outside library.
 
I decided to have one Index file in drive 0. and use the storage indicator of MTP to switch between drives.
Not sure if this is the best solution, so time will see.

Re SPI speed: 33 MHz was the max speed my hardware was working with 32GB disks. So that may needs to be adjusted.
Another reason to keep configuration outside library.

I love you moved the configuration outside the library makes it really easy to make adjustments. I just tested with a 512mb card and a samsung evo 32gb card. Only seemed to work at 16Mhz for both cards. Maybe add a comment in the example as a not to adjust clock.
 
I changed SPI click speed to 16 MHz as this seems good for testing and moved it as define outside configuration function
 
I changed SPI click speed to 16 MHz as this seems good for testing and moved it as define outside configuration function

Cool. Now I have another problem that I discovered while playing with MTP for spiflash. Just verified that it exists with SD Cards as well. So here goes, using the config:
Code:
  const char *sd_str[]={"sdio", "sd1"};      // edit to reflect your configuration
  const int cs[] = {BUILTIN_SDCARD, 4};  // edit to reflect your configuration
assuming SDIO would be storage 0 (just checking).

On initially opening the sd cards I see the following which I verified is correct (had to make sure I didn't do something);
Dir512mb.PNG

Now if I copy 2 files to "sd1" I see:
Capture2.PNG

Ok so far so good. Now if I turn power on and off and look at the directories again:
Capture3.PNG

Notice that the 2 files migrated to "sd1". Think there may be a problem with the files are identified as being stored.

EDIT: if i try to delete those files from sd1 after i copied there it won't let me
 
Notice that the 2 files migrated to "sd1". Think there may be a problem with the files are identified as being stored.

EDIT: if i try to delete those files from sd1 after i copied there it won't let me

OK, I think what is going on, will check
 
@WMXZ
Just saw what you did with LittleFS RAM. Cool way of doing it. Looks like will be easier than my approach and easier to maintain. Will have to play with it after you next update then add in SPIFlash and QSPI. Right now I have as separate so I can get multiple instances of RAM and SPIFlash. This is getting fun.

EDIT: couldn't resist so I tried your RAM disk using Paul's latest changes from this morning or was it last night but getting a bunch of errors; My setup is;
Code:
#include "Arduino.h"
#include "LittleFS.h"
#if __has_include("LittleFS.h")
  #define DO_LITTLEFS 1    // set to zero if not wanted // needs LittleFS installed as library
#else
  #define DO_LITTLEFS 0
#endif
everything else is the same.

but getting these errors;
Code:
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:32:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
F:\arduino-1.8.13-beta4\hardware\teensy\avr\libraries\SD\src/SD.h:159:49: error: 'FILE_READ' was not declared in this scope
  File open(const char *filepath, uint8_t mode = FILE_READ) {
                                                 ^
F:\arduino-1.8.13-beta4\hardware\teensy\avr\libraries\SD\src/SD.h: In member function 'virtual File SDClass::open(const char*, uint8_t)':
F:\arduino-1.8.13-beta4\hardware\teensy\avr\libraries\SD\src/SD.h:161:15: error: 'FILE_WRITE' was not declared in this scope
   if (mode == FILE_WRITE) flags = O_RDWR | O_CREAT | O_AT_END;
               ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:41:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h: At global scope:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:29:7: error: redefinition of 'class LittleFSFile'
 class LittleFSFile : public File
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:2:0:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:29:7: error: previous definition of 'class LittleFSFile'
 class LittleFSFile : public File
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:41:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:193:7: error: redefinition of 'class LittleFS'
 class LittleFS : public FS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:2:0:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:193:7: error: previous definition of 'class LittleFS'
 class LittleFS : public FS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:41:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:285:7: error: redefinition of 'class LittleFS_RAM'
 class LittleFS_RAM : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:2:0:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:285:7: error: previous definition of 'class LittleFS_RAM'
 class LittleFS_RAM : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:41:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:353:7: error: redefinition of 'class LittleFS_SPIFlash'
 class LittleFS_SPIFlash : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:2:0:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:353:7: error: previous definition of 'class LittleFS_SPIFlash'
 class LittleFS_SPIFlash : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:41:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:391:7: error: redefinition of 'class LittleFS_QSPIFlash'
 class LittleFS_QSPIFlash : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:2:0:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:391:7: error: previous definition of 'class LittleFS_QSPIFlash'
 class LittleFS_QSPIFlash : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:41:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:434:7: error: redefinition of 'class LittleFS_Program'
 class LittleFS_Program : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:2:0:
D:\Users\Merli\Documents\Arduino\libraries\LittleFS-main\src/LittleFS.h:434:7: error: previous definition of 'class LittleFS_Program'
 class LittleFS_Program : public LittleFS
       ^
In file included from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/MTP.h:36:0,
                 from D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\examples\mtp-test\mtp-test.ino:9:
D:\Users\Merli\Documents\Arduino\libraries\MTP_t4-master\src/Storage.h:195:52: error: 'FILE_READ' was not declared in this scope
   void OpenFileByIndex(uint32_t i, uint32_t mode = FILE_READ) ;
just thought you would like to know
 
Last edited:
@mjs513
Solved problem with copying from PC to teensy
Issue was ambiguity of root index (MTP considers root always -1 independent of storage) I took care of this for all but sendObject. corrected now.

Re LittleFS, I only have a T4.1 with 16 MB RAM here and no QSPI flash or SPIFlash, so I cannot test.
But the RAM version was easy (apart from issues with multiple includes of LittleFS.h) leading to somewhat awkward code, but I'm learning.

Edit: Saw, you had the same Include problems
Edit: did you fore a recompile of the whole libraries (I'm working with VisualCode-Makefile)
Edit: comment the #include "littleFS.h" in mtp-test.ino
 
@mjs513
Solved problem with copying from PC to teensy
Issue was ambiguity of root index (MTP considers root always -1 independent of storage) I took care of this for all but sendObject. corrected now.

Re LittleFS, I only have a T4.1 with 16 MB RAM here and no QSPI flash or SPIFlash, so I cannot test.
But the RAM version was easy (apart from issues with multiple includes of LittleFS.h) leading to somewhat awkward code, but I'm learning.

Edit: Saw, you had the same Include problems

Thanks will check the changes out - I can play with SPI and QSPI thats not a problem but I can't seem to get it work with your example - see previous post. One thing i noticed is with begin, you use begin(8000000) but the alternate form is to begin(buf, sizeof(buf)) so you can do something like DMAMEM char buf[40000] which uses DMAMEM for the RAM disk :) I will play some more later - getting tired :)
 
See my edits on previous post
Yes, I know alternative begin (you have it in your code also)

Missed the edits - must have crossed posted. Tried it with the include "LittleFS.h" commented out as well but then only the SD Card shows - never hits the RAM.

Saw, you had the same Include problems
Yeah was a pain to fix. Did it to ways - one was the way I did it in the master branch and the second with having separate MTP/Storage libraries set up for each of the storage areas but then ran into issues with trying to get them to play nice together

Will take a look at what you have set up.

--------------------------------------------------------

Back to SD Cards - now it seems if I use sdio and sd1 I can delete files from sdio but not from sd1 - keep getting asked to skip the files.


EDIT: Just took a quick look and this never worked for me "#if __has_include("LittleFS.h")" :) struggled with that for awhile before I tossed it. I will play with it.
 
Back to SD Cards - now it seems if I use sdio and sd1 I can delete files from sdio but not from sd1 - keep getting asked to skip the files.

Solved and updated
Was delete had storage still hard coded

Next issue?
 
maybe because I'm using makefiles?

Next Issue
Unknown at this point - that is all I got up to.

Don't think its a makefile issue - I'm using the Arduino IDE and have the same problem. Wound up doing something like this in MTP.h:
Code:
#ifdef use_spi_disk
	#define USE_SPI
#elif defined(use_qspi_disk)
	#define USE_QSPI
#else
	#define USE_RAM
#endif
then do the USE_RAM etc in MTP.h or storage.h/cpp

Check what I did in the WIP branch as well. I created sdp, sdx, and sdr[x]'s so I could have multiple instances. Right now you can really on have 1 instance of each storage area.
 
@mjs513
Now I cleaned up the littleFS issue.
AFAIK, the issue is that littleFS can only be included once in project, but Storage and main need the class definitions.
I also wanted to avoid config files.

Program is now compiling and running correctly with makefile and from Arduino IDE

What I suggest is
to edit Storage.h once to tell the system that we have LittleFS on system and possible may wanted to use it "#define HAVE_LITTLEFS 1"
If one has LittleFS installed one can include the LittleFS.h file

A second issue is, to allow configuration in main program
For this it is best to declare (right term?) the different classes on main file
Code:
// classes need to be declared here (in storage.h there are declared external)
SDClass sdx[nsd];
#if HAVE_LITTLEFS==1
  LittleFS_RAM ramfs; // needs to be declared if LittleFS is used in storage.h
#endif
and have in Storage.h the classes be declared external

This complication is necessary if one wanted to configure in main file how many SDClasses one wanted to use (as done in mtp-test.ino)
When compiling the MTP library, the number of SDClasses is not known, so an extern declaration is used there
For single classes, this is not necessary, but I kept the idea also in this case.

It should then be easy to extend the FS to other implementations: QSPI etc
 
@WMXZ

Just woke up and having my first cup of coffee. Downloaded the updated version of MTP and ran MTP_test. Seems like it is working. Nice job on the fix.

Fully understand about the complications with this - been playing with it for the last week or so!

Now let the funs begin and to see about expanding it.
 
Just read it after your post. Was doing some playing with current version of MTPResponder to add in multiple RAM areas and multiple SPIFlash devices. Making some progress. But had to do a slight modification to your setStorageNumbers but seems to work havent done full testing:)
Capture4.PNG

Now got sd card + RAM + SPIFlash cards working :)
 
I updated the MTP code to accept any FS that is derived from FS and File classes.
Have a look into mtp_test.ino example to see how it works.
Needs latest cores (inclusive print64)
Tested only with T4.1 (T3.6 TBD)
 
I updated the MTP code to accept any FS that is derived from FS and File classes.
Have a look into mtp_test.ino example to see how it works.
Needs latest cores (inclusive print64)
Tested only with T4.1 (T3.6 TBD)

Just took a look. Alot cleaner implementation. Will play with it later and try to incorporate my mods for SPI and QSPI.
 
@WMXZ
Can you take a look at what I did to address the issue with having multiple RAM storage areas and SPI devices, i.e, mod to setStorageNumbers to include type of storage:
Code:
  void  MTPStorage_SD::setStorageNumbers(const char **str, const int *csx, const int *stypex, int num) 
  { sd_str = str; 
    num_storage=num;
    setCs(csx);
	setType(stypex);
  }
  uint32_t MTPStorage_SD::getNumStorage() 
  { if(num_storage) return num_storage; else return 1;
  }
  const char * MTPStorage_SD::getStorageName(uint32_t storage) 
  { if(sd_str) return sd_str[storage-1]; else return "SD_DISK";
  }
Code:
	bool setType(const int *stypex) { typeStore = stypex; return true; }
The MTP_test example shows how to do this.

EDIT: If you are agreeable I will go ahead and update your latest library changes.
 
@mjs513
Following Paul's advice I changed the filesystem registration
It should be no problem to register any type of filesystems, as long they are derived from FS.
I will update the example next

I was busy overnight the chase the reason why my new implementation crashed, after I copied latest FS.h, Print.cpp/h, SD lib, and LittleFS lib from Paul's github
I turned out that Paul changed the position() and size() from 32bit to 64bit, which screwed up my 32bit implementation
Will next migrate to 64bit (even if I never would write a file larger than 4GB, but one never should say never)
 
I turned out that Paul changed the position() and size() from 32bit to 64bit, which screwed up my 32bit implementation
Will next migrate to 64bit (even if I never would write a file larger than 4GB, but one never should say never)

OK, it seems that I found the corrupting line
github updated Basic tests done with T4.1 and T3.6
 
Back
Top