Forum Rule: Always post complete source code & details to reproduce any issue!
Page 19 of 22 FirstFirst ... 9 17 18 19 20 21 ... LastLast
Results 451 to 475 of 548

Thread: USBHost_t36 USB Mass Storage Driver Experiments

  1. #451
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    Question:
    To set up a volume say for an exFat partion I do a:
    expartVol.begin(sd.card(), true, 1);

    for an SD Card. Now, if I am using a usb drive (non sd). What do I do for a blockdevice or pointer to the device. There is no equivalent .card() for a thumb drive?

  2. #452
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Note: the get volume label code was not working for me as probably timing. My USB is connected through HUB and so takes a bit of time before all of the stuff is setup. So I have it check to make sure the drive is ready...

    Also put in loop so you can try it again...
    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
    
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
    
      if (drvType == msDrive) {
        mscDrive = &msDrive1;
        if (!volName.begin(mscDrive)) {
          return false;
        }
        if (!root.openRoot(&volName)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if (drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
    
      root.read(buf, 32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2 * i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType) {
      uint8_t buf[512];
    
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(msc1.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
      //--------------------------------------------------
      Serial.printf("Initialize USB drive...");
    
    }
    
    void loop(void) {
      myusb.Task();
      if (!msDrive1) {
        Serial.println("Waiting up to 5 seconds for USB drive");
        elapsedMillis em = 0;
        while (!msDrive1 && (em < 5000) )  myusb.Task();
      }
      if (msDrive1) {
        if (!msc1.begin(&msDrive1)) {
          msc1.errorPrint(&Serial);
          Serial.println("initialization failed.\n");
        } else {
          Serial.println("USB drive 1 is present.\n");
        }
    
        if (msc1.fatType() == 32) {
          Serial.printf("Fat Type: Fat32\n");
          if (!getFat32VolumeLabel(msDrive))
            Serial.printf("Failed to get volume label\n");
        } else {
          Serial.printf("Fat Type: ExFat\n");
          if (!getExFatVolumeLabel(msDrive))
            Serial.printf("Failed to get volume label\n");
        }
        msc1.ls(LS_SIZE | LS_DATE | LS_R);
      }
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
        Serial.println("SD card is present.\n");
      }
    
      if (sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if (!getFat32VolumeLabel(sdDrive))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if (!getExFatVolumeLabel(sdDrive)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
      Serial.println("Press any key to run again");
      while (Serial.read() == -1);
      while (Serial.read() != -1);
    
    }
    Next up look at partition. I probably lost yesterdays debug code as I installed the new beta. But I know the code is calling into SDFat to the init/begin functions and some of them are already setup to take
    a partition number, and we passed in 1...

  3. #453
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Quote Originally Posted by mjs513 View Post
    Question:
    To set up a volume say for an exFat partion I do a:
    expartVol.begin(sd.card(), true, 1);

    for an SD Card. Now, if I am using a usb drive (non sd). What do I do for a blockdevice or pointer to the device. There is no equivalent .card() for a thumb drive?
    Take this with a grain of salt, but what I am planning to look at includes:

    In the sketches we have: msController msDrive1(myusb);

    Each one of these is for a physical USB drive, I believe it claims the device at an interface level, but I am pretty sure that all of the partitions are one one of these.
    And each one of these uses another class: UsbFs msc1;

    And the call: if (!msc1.begin(&msDrive1)) {
    Starts up the one logical drive using the msDrive1...

    And the begin code will sooner or later funnel down to call in the SDFat project:
    bool ExFatPartition::init(BlockDevice* dev, uint8_t part);
    Or the Fat version:
    bool FatPartition::init(BlockDevice* dev, uint8_t part)

    Need to walk back to where this one gets called... probably from the begin method which is called by the ...

  4. #454
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    Quote Originally Posted by KurtE View Post
    Take this with a grain of salt, but what I am planning to look at includes:

    In the sketches we have: msController msDrive1(myusb);

    Each one of these is for a physical USB drive, I believe it claims the device at an interface level, but I am pretty sure that all of the partitions are one one of these.
    And each one of these uses another class: UsbFs msc1;

    And the call: if (!msc1.begin(&msDrive1)) {
    Starts up the one logical drive using the msDrive1...

    And the begin code will sooner or later funnel down to call in the SDFat project:
    bool ExFatPartition::init(BlockDevice* dev, uint8_t part);
    Or the Fat version:
    bool FatPartition::init(BlockDevice* dev, uint8_t part)

    Need to walk back to where this one gets called... probably from the begin method which is called by the ...
    Thanks Kurt - right now by brain is mush working on multiple things at a time Need a break before getting back into it.

  5. #455
    Senior Member wwatson's Avatar
    Join Date
    Aug 2017
    Posts
    426
    Quote Originally Posted by mjs513 View Post
    Question:
    To set up a volume say for an exFat partion I do a:
    expartVol.begin(sd.card(), true, 1);

    for an SD Card. Now, if I am using a usb drive (non sd). What do I do for a blockdevice or pointer to the device. There is no equivalent .card() for a thumb drive?
    You would use this:
    Code:
    UsbFs msc1;
    expartVol.begin(msc1.usbDrive(), true, 1);
    .usbDrive() is the equivilent of .card().

    The procedure would be calling sd.cardBegin() followed by sd.volumeBegin(). sd.volumeBegin() is what actually mounts the file system.

  6. #456
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    Finally built here ... swapped PJRC git USBHost into sketchbook/libraries ... still fail

    Deleted that to use the td1.54b7 installed copy and IT BUILDS! { Maybe re-using the sketchbook folder left some debris? }

    Have gotten a few of the other samples to build as well.

    ... now to figure out the hardware and setup to work with given sketches ...

  7. #457
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Glad you have some building and ready to play

    Did not get as much done yesterday, but will now try to see if I can get this MSC volume code and the like to read from all 3 partitions on my USB stick...

  8. #458
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    @all...

    I am winding my way through the different layers and trying to figure out how to get USB Drive partitions to work.

    It might be nice to be able to do something like: today the VojumeName.ino does: if (!volName.begin(mscDrive)) {
    To either do the begin like: begin(mscDrive, true, 1) or make the default not needed: begin(mscDrive, 2)
    To specify which partition to do the work with...

    I know at the lowest level we do pass in currently hard code 1 for partition table. If I look at the code and debug, I show:
    At a lowest level: ExFatPartition::init(20001b74, 1)
    Code:
    bool FsVolume::begin(BlockDevice* blockDev) {
      Serial.printf("FsVolume::begin(%x)\n", (uint32_t)blockDev);
    Which is called from FsVolume::begin(20001b74)
    Code:
    bool FsVolume::begin(BlockDevice* blockDev) {
      Serial.printf("FsVolume::begin(%x)\n", (uint32_t)blockDev);
      m_blockDev = blockDev;
      m_fVol = nullptr;
      m_xVol = new (m_volMem) ExFatVolume;
      if (m_xVol && m_xVol->begin(m_blockDev, false)) {
        goto done;
      }
      m_xVol = nullptr;
      m_fVol = new (m_volMem) FatVolume;
      if (m_fVol && m_fVol->begin(m_blockDev, false)) {
        goto done;
      }
      m_cwv = nullptr;
      m_fVol = nullptr;
      return false;
    
     done:
      m_cwv = this;
      return true;
    }
    Where we don't have place to pass in partition:

    Which is called by code in the UsbMscFat USBFat code
    Code:
     bool mscBegin(msController *pDrive) {
        return usbDriveBegin(pDrive) && Vol::begin(m_USBmscDrive);
    Which is called by the begin code just above it:
    Code:
    bool begin(msController *pdrv) {
    	return mscBegin(pdrv);
    And it is that begin we are calling in the sketch.

    So the question is do we try to add the partition information to these layers, which go across multiple libraries? Question I guess is what does SD Library do now with SD cards to begin the volume stuff for other partitions?
    More to look at!

  9. #459
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    @KurtE - @all

    Finished my diversions this morning and guess I am going to start playing with partitions again. This stuff is giving me a headache to be honest - a lot of pieces to the puzzle.

    @defragster
    Glad you got it working so you can join the fun

  10. #460
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    @KurtE
    Got it partially working
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    exFAT: 1,0,0x20,0x21,0x0,0x7,0xFE,0xFF,0xFF,2048,204800000
    FAT32: 2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,204802048,29634560
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: ExFat
    Volume Name: New Volume
    Fat Type: Fat32
    Volume Name: 3��м
    The gibberish is because I don't know what to do with line for FAT32:
    Code:
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
      
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if(drvType == msDrive) {
        msc1.usbDrive()->readSector(dataStart[part],buf);
      }
    Really needs to point to the vol but doesn;t seem to work

    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
      
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
      
    ExFatVolume expartVol;
    
      if(drvType == msDrive) {
      mscDrive = &msDrive1;
      if (!volName.begin(&msDrive1)) {
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if(drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }  
    
      root.read(buf,32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2*i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
      
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if(drvType == msDrive) {
        msc1.usbDrive()->readSector(dataStart[part],buf);
      }
    
      if(drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(),buf);
      }  
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      return true;
    }
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
    //    if ((pt->boot != 0 && pt->boot != 0X80) ||
    //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
    //      valid = false;
    //    }
        if((pt->type) == 12) Serial.print("FAT32: ");
        if((pt->type) ==  7) Serial.print("exFAT: ");
        partitionTable[ip-1] = pt->type;
        dataStart[ip-1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip));Serial.print( ',');
        Serial.print(int(pt->boot), HEX);Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x");Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x");Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x");Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC);Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
       while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
      //--------------------------------------------------
      Serial.printf("Initialize USB drive...");
    
      if (!msc1.begin(&msDrive1)) {
      msc1.errorPrint(&Serial);
        Serial.println("initialization failed.\n");
      } else {
         Serial.println("USB drive 1 is present.\n");
      }
    
      mbrDmp();
    /*
      if(msc1.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if(!getFat32VolumeLabel(msDrive))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFat\n");
      if(!getExFatVolumeLabel(msDrive,1))
        Serial.printf("Failed to get volume label\n");
      }
      msc1.ls(LS_SIZE | LS_DATE | LS_R);
    */
    
      for(uint8_t i = 1; i < 5; i++){
        if(partitionTable[i-1] == 7){
          Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,i))
            Serial.printf("Failed to get volume label\n");
        } else if(partitionTable[i-1] == 12){
          Serial.printf("Fat Type: Fat32\n");
          if(!getFat32VolumeLabel(msDrive, i))
            Serial.printf("Failed to get volume label\n");
        } else {
          Serial.println("No or Not Supported Partition");
        }
      }
    
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
         Serial.println("SD card is present.\n");
      }
    
      if(sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if(!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if(!getExFatVolumeLabel(sdDrive,1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
    }
    
    void loop(void) {
    }
    EDIT as a side not may not be using the right data start position?

  11. #461
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    Out of curiosity I did add a print directory (LS) for the fat just to see if it work:
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    exFAT: 1,0,0x20,0x21,0x0,0x7,0xFE,0xFF,0xFF,2048,204800000
    FAT32: 2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,204802048,29634560
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: ExFat
    Volume Name: New Volume
    Fat Type: Fat32
    Volume Name: ....
    2020-12-27 14:47    5725283 VSLAM_and_Navigation_System_of_Unmanned_Ground_Vehicle_Based_on_RGB-D_Camera.pdf
    No or Not Supported Partition
    No or Not Supported Partition
    so switching partitions does work just don't know about that one command for reading sector

  12. #462
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    @KurtE
    Fixed this now works for USB devices:
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    exFAT: 1,0,0x20,0x21,0x0,0x7,0xFE,0xFF,0xFF,2048,204800000
    FAT32: 2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,204802048,29634560
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: ExFat
    Volume Name: New Volume
    Fat Type: Fat32
    Volume Name: NEW VOLUME 
    2020-12-27 14:47    5725283 VSLAM_and_Navigation_System_of_Unmanned_Ground_Vehicle_Based_on_RGB-D_Camera.pdf
    No or Not Supported Partition
    No or Not Supported Partition
    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
      
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
      
    ExFatVolume expartVol;
    
      if(drvType == msDrive) {
      mscDrive = &msDrive1;
      if (!volName.begin(&msDrive1)) {
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if(drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }  
    
      root.read(buf,32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2*i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
      
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if(drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(),buf);
      }
    
      if(drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(),buf);
      }  
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
    //    if ((pt->boot != 0 && pt->boot != 0X80) ||
    //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
    //      valid = false;
    //    }
        if((pt->type) == 12) Serial.print("FAT32: ");
        if((pt->type) ==  7) Serial.print("exFAT: ");
        partitionTable[ip-1] = pt->type;
        dataStart[ip-1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip));Serial.print( ',');
        Serial.print(int(pt->boot), HEX);Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x");Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x");Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x");Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC);Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
       while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
      //--------------------------------------------------
      Serial.printf("Initialize USB drive...");
    
      if (!msc1.begin(&msDrive1)) {
      msc1.errorPrint(&Serial);
        Serial.println("initialization failed.\n");
      } else {
         Serial.println("USB drive 1 is present.\n");
      }
    
      mbrDmp();
    /*
      if(msc1.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if(!getFat32VolumeLabel(msDrive))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFat\n");
      if(!getExFatVolumeLabel(msDrive,1))
        Serial.printf("Failed to get volume label\n");
      }
      msc1.ls(LS_SIZE | LS_DATE | LS_R);
    */
    
      for(uint8_t i = 1; i < 5; i++){
        if(partitionTable[i-1] == 7){
          Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,i))
            Serial.printf("Failed to get volume label\n");
        } else if(partitionTable[i-1] == 12){
          Serial.printf("Fat Type: Fat32\n");
          if(!getFat32VolumeLabel(msDrive, i))
            Serial.printf("Failed to get volume label\n");
        } else {
          Serial.println("No or Not Supported Partition");
        }
      }
    
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
         Serial.println("SD card is present.\n");
      }
    
      if(sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if(!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if(!getExFatVolumeLabel(sdDrive,1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
    }
    
    void loop(void) {
    }
    Tested with several USB drives (SSD, SD CARD in USB, 32Gb thumb and 2 gb thumb drive).

  13. #463
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Hi @mjs513,

    I slightly hacked up your sketch, by moving a lot of it from setup() to loop() with a wait at the end of loop() for user to enter character.

    That way you can hopefully rerun.

    I also have it wait up to 5 seconds for the USB drive to come available... That is I was seeing failures on the previous version, where my USBStick is plugged into a HUB and had not fully initialized before we tried to use the drive.

    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
    
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
    
      ExFatVolume expartVol;
    
      if (drvType == msDrive) {
        mscDrive = &msDrive1;
        if (!volName.begin(&msDrive1)) {
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if (drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
    
      root.read(buf, 32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2 * i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
        //    if ((pt->boot != 0 && pt->boot != 0X80) ||
        //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
        //      valid = false;
        //    }
        if ((pt->type) == 12) Serial.print("FAT32: ");
        if ((pt->type) ==  7) Serial.print("exFAT: ");
        partitionTable[ip - 1] = pt->type;
        dataStart[ip - 1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip)); Serial.print( ',');
        Serial.print(int(pt->boot), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x"); Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC); Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
    
    }
    
    void loop(void) {
      //--------------------------------------------------
      myusb.Task();
      if (!msDrive1) {
        Serial.println("Waiting up to 5 seconds for USB drive");
        elapsedMillis em = 0;
        while (!msDrive1 && (em < 5000) )  myusb.Task();
      }
      if (msDrive1) {
        Serial.printf("Initialize USB drive...");
    
        if (!msc1.begin(&msDrive1)) {
          msc1.errorPrint(&Serial);
          Serial.println("initialization failed.\n");
        } else {
          Serial.println("USB drive 1 is present.\n");
        }
    
        mbrDmp();
        /*
          if(msc1.fatType() == 32) {
            Serial.printf("Fat Type: Fat32\n");
            if(!getFat32VolumeLabel(msDrive))
              Serial.printf("Failed to get volume label\n");
          } else {
            Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,1))
            Serial.printf("Failed to get volume label\n");
          }
          msc1.ls(LS_SIZE | LS_DATE | LS_R);
        */
    
        for (uint8_t i = 1; i < 5; i++) {
          if (partitionTable[i - 1] == 7) {
            Serial.printf("Fat Type: ExFat\n");
            if (!getExFatVolumeLabel(msDrive, i))
              Serial.printf("Failed to get volume label\n");
          } else if (partitionTable[i - 1] == 12) {
            Serial.printf("Fat Type: Fat32\n");
            if (!getFat32VolumeLabel(msDrive, i))
              Serial.printf("Failed to get volume label\n");
          } else {
            Serial.println("No or Not Supported Partition");
          }
        }
      }
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
        Serial.println("SD card is present.\n");
      }
    
      if (sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if (!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if (!getExFatVolumeLabel(sdDrive, 1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
      Serial.println("Press any key to run again");
      while (Serial.read() == -1);
      while (Serial.read() != -1);
    
    
    }
    Warning I have debug code in place right now... So my debug output is a little hard to read:
    Code:
    Waiting up to 5 seconds for USB drive
       connected 1
       initialized 0
    Initialize USB drive...mscIint()
    msReset()
    msGetMaxLun()
    WaitMediaReady()
    msTestReady()
    msGetCSW()
    msDeviceInquiry()
    msDoCommand():
    msGetCSW()
    msProcessError()
    msReadDeviceCapacity()
    msDoCommand():
    msGetCSW()
    msProcessError()
    checkConnectedInitialized()
    FsVolume::begin(20001c60)
    ExFatPartition::init(20001c60, 1)
    checkConnectedInitialized()
    <<< msReadBlocks(0 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    checkConnectedInitialized()
    <<< msReadBlocks(800 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    FatPartition::init(20001c60, 1)
    checkConnectedInitialized()
    <<< msReadBlocks(0 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    checkConnectedInitialized()
    <<< msReadBlocks(800 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    USB drive 1 is present.
    
    checkConnectedInitialized()
    <<< msReadBlocks(0 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
    2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
    exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    No or Not Supported Partition
    No or Not Supported Partition
    Fat Type: ExFat
    mscIint()
    msReset()
    msGetMaxLun()
    WaitMediaReady()
    msTestReady()
    msGetCSW()
    msDeviceInquiry()
    msDoCommand():
    msGetCSW()
    msProcessError()
    msReadDeviceCapacity()
    msDoCommand():
    msGetCSW()
    msProcessError()
    checkConnectedInitialized()
    ExFatPartition::init(2006fb0c, 1)
    checkConnectedInitialized()
    <<< msReadBlocks(0 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    checkConnectedInitialized()
    <<< msReadBlocks(800 1 200)
    msDoCommand():
    msGetCSW()
    msProcessError()
    Failed to get volume label
    No or Not Supported Partition
    
    Initialize SD card...FsVolume::begin(200034f0)
    ExFatPartition::init(200034f0, 1)
    FatPartition::init(200034f0, 1)
    SD card is present.
    
    Fat Type: ExFAT
    ExFatPartition::init(2006ff9c, 1)
    Failed to get volume label
    done...
    Press any key to run again
    Now to play some, I don't think it is supporting the FAT16 or ...

  14. #464
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    @all - Wondering in my above Partition table dump you see:
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
    2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
    exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    No or Not Supported Partition
    No or Not Supported Partition
    Fat Type: ExFat
    The three partition types:
    Only one was understood 7 for ExFat

    The first partition: I formatted for Fat32: Turns out there are at least two different Fat32s.
    0xb - FAT32 with CHS addressing - The one that was formatted on it...
    0xc - FAT32 with LBA addressing - The one you are looking for

    Then the second partition: Fat16 with LBA... Not sure if we support?
    There appear to be a few different Fat16, like 4 and 6...

  15. #465
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    Quote Originally Posted by KurtE View Post
    @all - Wondering in my above Partition table dump you see:
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
    2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
    exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    No or Not Supported Partition
    No or Not Supported Partition
    Fat Type: ExFat
    The three partition types:
    Only one was understood 7 for ExFat

    The first partition: I formatted for Fat32: Turns out there are at least two different Fat32s.
    0xb - FAT32 with CHS addressing - The one that was formatted on it...
    0xc - FAT32 with LBA addressing - The one you are looking for

    Then the second partition: Fat16 with LBA... Not sure if we support?
    There appear to be a few different Fat16, like 4 and 6...
    Just gave it a try and works for me. We all are really hacking away - but its working at least so making progress.

    Not sure we are supporting FAT16 - or should we?

    As for different types of FAT32 codes that easy to fix if we know what they are - looks like for you its 0x0B and 0x0E. I just used what my FAT32 was - which was 0x0C

  16. #466
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    Some hacking here - esp with my odd powered hub that has internal device #1 of a 120GB HDD - then as coded it doesn't look beyond msDrive1.

    Also odd about the hub as before - once enabled the Hub/HDD backfeeds power to the Teensy - even the USB power button won't turn off its port light until the Hub/HDD is unplugged.

    Also the first 64GB Flash on hand was MSFT formatted with a first Part of 32GB - plugged into computer and created 2nd Part and Fat32 formatted that. But need to swap hub to see that?

    Hacking indeed ... was on the @mjs513 code - made a UsbFs msc2; and a ptr* in funcs? - but was getting same Part table data displayed when iterated twice - then replaced with KurtE p#463 code and wondering about best start with msDrive[] arrays and for UsbFs msc1 ??? - and passing drive index ???

    shows this now ignoring the USB Flash plugged in:
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    exFAT: 1,0,0x20,0x21,0x0,0x7,0xFE,0xFF,0xFF,2048,234436608
    2,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: ExFat
    Volume Name: 120GB_Ext
    No or Not Supported Partition
    No or Not Supported Partition
    No or Not Supported Partition
    
    Initialize SD card...SD card is present.
    
    Fat Type: Fat32
    Volume Name: B

  17. #467
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    Using unpowered Hub and the 64GB Flash with 32 GB recovery Part - the 2nd part shows using KurtE code:

    Notes on output below:
    > there are about 300-400 pages of files displayed in TyComm on the recovery drive - does a FULL DIR on mounting takes a full 16 seconds to SPEW all the file names
    > no problem with any of the long file names
    > CANNOT cut and paste to anything except WORD as there are NULLS after each char in "Info" :: Volume Name: B I n f o
    >> {edit} - was wrong code spot - this fix allows copy paste::
    Code:
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        if ( buf[i] > 0 && buf[i] < 127 )
          Serial.write(buf[i]);
      }
    Name:  BadINFO.png
Views: 122
Size:  5.2 KB
    Code:
    Waiting up to 5 seconds for USB drive
    Initialize USB drive...USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 1,80,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,67108864
    FAT32: 2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,67110912,54128640
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: Fat32
    Volume Name: B Info
    2017-10-06 02:44          0 efi/
      2017-10-06 02:44          0 boot/
        2017-09-18 16:30    1224608 bootx64.efi
      2017-10-06 02:44          0 microsoft/
        2017-10-06 02:44          0 boot/
          2017-03-18 13:59      16384 BCD
          2017-10-06 02:44          0 fonts/
            2017-03-18 13:57    3695719 chs_boot.ttf
    ...
      2018-04-26 19:30          0 ICON/
        2018-04-26 19:30      34145 remove.ico
      2018-04-26 19:30          0 Metadata/
        2018-04-26 19:30      59471 767402d9-4a9b-4b86-a7cc-87ee0b9dda64.devicemetadata-ms
        2018-04-26 19:30     132642 7da5fe49-047b-4050-af5d-4db9c0b308b1.devicemetadata-ms
        2018-04-26 19:30      59446 90faa471-b2ba-4151-a2ae-42a27fa17081.devicemetadata-ms
        2018-04-26 19:30      59468 bbf1d96f-8f14-4421-afd3-00c29d8c5ae0.devicemetadata-ms
        2018-04-26 19:30      59445 c4d649ac-a413-49dd-845f-0c1ca1416b1a.devicemetadata-ms
        2018-04-26 19:30     137533 cf65cfb5-3970-491b-bb2b-15660bf3abab.devicemetadata-ms
        2018-04-26 19:30      59075 d22853f4-2a28-43da-9432-cba889d49fa8.devicemetadata-ms
        2018-04-26 19:30      75178 fc02c085-991f-446a-a8c4-c4945bd9f129.devicemetadata-ms
        2018-04-26 19:30     280279 ffd10825-4187-4972-bc05-15501fc41dd7.devicemetadata-ms
      2017-11-06 16:59          0 PATCH/
      2018-04-26 19:30          0 README/
        2018-04-26 19:30      81920 AdvProperties.html
      2018-04-26 19:30          0 TOOL/
        2018-04-26 19:30     256352 RTInstaller32.dat
        2018-04-26 19:30     287584 RTInstaller64.dat
      2018-04-26 19:30          0 WIN10/
        2018-04-26 19:30          0 32/
          2018-04-26 19:30      19307 rtux86w10.cat
          2018-04-26 19:30     451154 rtux86w10.INF
          2018-04-26 19:30     344544 rtux86w10.sys
        2018-04-26 19:30          0 64/
          2018-04-26 19:30      19340 rtux64w10.cat
          2018-04-26 19:30     451154 rtux64w10.INF
          2018-04-26 19:30     429024 rtux64w10.sys
    done...
    Press any key to run again
    Last edited by defragster; 02-24-2021 at 06:42 PM. Reason: Wrong V Name Spot

  18. #468
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    T_4.1 with KurtE code ( and the p#467 Volume Name edit ) - on unpowered hub just the two partition 64GB Flash works!
    >> NOTE: putting this flash in Win machine mounts both partitions/drive letters also.
    Pulled SD card to see what it does for Partition #2 on the 64GB Flash and it was found and directory of single file there is shown:
    Code:
    ...
        2018-04-26 19:30          0 64/
          2018-04-26 19:30      19340 rtux64w10.cat
          2018-04-26 19:30     451154 rtux64w10.INF
          2018-04-26 19:30     429024 rtux64w10.sys
    Fat Type: Fat32
    Volume Name: 64G_PT#2   
    2021-02-24 09:01          0 FPartTwo.txt
    No or Not Supported Partition
    No or Not Supported Partition
    
    Initialize SD card...initialization failed.
    
    Fat Type: ExFAT
    Failed to get volume label
    done...
    Press any key to run again
    Only about 200 files on the Flash Part #1 - the others were printing from the SD card - so was not seeing Flash Part#2 directory print.

  19. #469
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Note: I have been playing some with adding Fat16 maybe stuff and not finding the VolID...

    Some output currentlly:
    Code:
    Waiting up to 5 seconds for USB drive
    Initialize USB drive...FsVolume::begin(20001b28)
    ExFatPartition::init(20001b28, 1)
    FatPartition::init(20001b28, 1)
    USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
    FAT16: 2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
    exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: Fat32
    FatPartition::init(20001b28, 1)
    Volume Name: FAT32      
    2010-03-22 07:11    3343737 DSC03357.JPG
    2021-01-01 00:00          0 example.txt
    Fat Type: Fat16
    FatPartition::init(20001b28, 2)
    2E 20 20 20 20 20 20 20 20 20 20 10 00 60 AE 6D 56 52 56 52 00 00 AF 6D 56 52 02 00 00 00 00 00 :.          ..`.mVRVR...mVR......
    2E 2E 20 20 20 20 20 20 20 20 20 10 00 60 AE 6D 56 52 56 52 00 00 AF 6D 56 52 00 00 00 00 00 00 :..         ..`.mVRVR...mVR......
    42 74 00 00 00 FF FF FF FF FF FF 0F 00 CE FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF FF FF FF :Bt..............................
    01 57 00 50 00 53 00 65 00 74 00 0F 00 CE 74 00 69 00 6E 00 67 00 73 00 2E 00 00 00 64 00 61 00 :.W.P.S.e.t....t.i.n.g.s.....d.a.
    57 50 53 45 54 54 7E 31 44 41 54 20 00 64 AE 6D 56 52 58 52 00 00 AF 6D 56 52 03 00 0C 00 00 00 :WPSETT~1DAT .d.mVRXR...mVR......
    42 47 00 75 00 69 00 64 00 00 00 0F 00 FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF FF FF FF :BG.u.i.d........................
    01 49 00 6E 00 64 00 65 00 78 00 0F 00 FF 65 00 72 00 56 00 6F 00 6C 00 75 00 00 00 6D 00 65 00 :.I.n.d.e.x....e.r.V.o.l.u...m.e.
    49 4E 44 45 58 45 7E 31 20 20 20 20 00 7B 11 5D 58 52 58 52 00 00 12 5D 58 52 19 00 4C 00 00 00 :INDEXE~1    .{.]XRXR...]XR..L...
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 :................................
    Volume Name: .          
    2021-01-13 17:56     340510 T4-Cardlike.jpg
    Fat Type: ExFat
    ExFatPartition::init(2006fb0c, 1)
    Failed to get volume label
    No or Not Supported Partition
    
    Initialize SD card...FsVolume::begin(200033b0)
    ExFatPartition::init(200033b0, 1)
    FatPartition::init(200033b0, 1)
    SD card is present.
    
    Fat Type: ExFAT
    ExFatPartition::init(2006ff9c, 1)
    Failed to get volume label
    done...
    Press any key to run again
    Note: Added quick and dirty hex dump to see what the data read in was...
    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
    
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
    
      ExFatVolume expartVol;
    
      if (drvType == msDrive) {
        mscDrive = &msDrive1;
        if (!volName.begin(&msDrive1)) {
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if (drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
    
      root.read(buf, 32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2 * i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    // Get Fat16 volume name.
    bool getFat16VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      print_hexbytes(buf, 512);
    
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
        //    if ((pt->boot != 0 && pt->boot != 0X80) ||
        //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
        //      valid = false;
        //    }
        switch (pt->type) {
          case 4:
          case 6:
          case 0xe:
            Serial.print("FAT16: ");
            break;
          case 11:
          case 12:
            Serial.print("FAT32: ");
            break;
          case 7:
            Serial.print("exFAT: ");
            break;
        }
        partitionTable[ip - 1] = pt->type;
        dataStart[ip - 1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip)); Serial.print( ',');
        Serial.print(int(pt->boot), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x"); Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC); Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
    
    }
    
    void loop(void) {
      //--------------------------------------------------
      myusb.Task();
      if (!msDrive1) {
        Serial.println("Waiting up to 5 seconds for USB drive");
        elapsedMillis em = 0;
        while (!msDrive1 && (em < 5000) )  myusb.Task();
      }
      if (msDrive1) {
        Serial.printf("Initialize USB drive...");
    
        if (!msc1.begin(&msDrive1)) {
          msc1.errorPrint(&Serial);
          Serial.println("initialization failed.\n");
        } else {
          Serial.println("USB drive 1 is present.\n");
        }
    
        mbrDmp();
        /*
          if(msc1.fatType() == 32) {
            Serial.printf("Fat Type: Fat32\n");
            if(!getFat32VolumeLabel(msDrive))
              Serial.printf("Failed to get volume label\n");
          } else {
            Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,1))
            Serial.printf("Failed to get volume label\n");
          }
          msc1.ls(LS_SIZE | LS_DATE | LS_R);
        */
    
        for (uint8_t i = 1; i < 5; i++) {
          switch (partitionTable[i - 1]) {
            case 11:
            case 12:
              Serial.printf("Fat Type: Fat32\n");
              if (!getFat32VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 4:
            case 6:
            case 0xe:
              Serial.printf("Fat Type: Fat16\n");
              if (!getFat16VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 7:
              Serial.printf("Fat Type: ExFat\n");
              if (!getExFatVolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            default:
              Serial.println("No or Not Supported Partition");
          }
        }
      }
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
        Serial.println("SD card is present.\n");
      }
    
      if (sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if (!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if (!getExFatVolumeLabel(sdDrive, 1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
      Serial.println("Press any key to run again");
      while (Serial.read() == -1);
      while (Serial.read() != -1);
    }
    
    void print_hexbytes(const void *ptr, int len)
    {
      if (ptr == NULL || len <= 0) return;
      const uint8_t *p = (const uint8_t *)ptr;
      while (len) {
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%02X ", p[i]);
        }
        Serial.print(":");
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%c", ((p[i] >= ' ') && (p[i] <= '~')) ? p[i] : '.');
        }
        Serial.println();
        p += 32;
        len -= 32;
      }
    }

  20. #470
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    ... using the p#469 code { I added : >>>pt->type: 12 }
    My favorite/most used Flash is a FAT32 16GB with a physical write protect switch.
    Attachment 23836
    That is how Windows sees it, MSFT formatted it when they put on a Win 10 Install image on it

    If the drive is plugged on power up it fails to find?
    <edit>:
    Actually it seems to hang forever ????

    If I power Teensy with the drive removed - wait for first timeout - then plug the drive and send USB input it rescans and finds the drive.
    But it does not find the "Volume Name: B Info" that should be :: TLABBLU20H2
    Code:
    Waiting up to 5 seconds for USB drive
    
    Initialize SD card...initialization failed.
    
    Fat Type: ExFAT
    Failed to get volume label
    done...
    Press any key to run again
    Initialize USB drive...USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 	>>>pt->type: 12
    1,80,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,30963712
    	>>>pt->type: 0
    2,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    	>>>pt->type: 0
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    	>>>pt->type: 0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    Fat Type: Fat32
    Volume Name: B Info
    2020-05-10 23:45        128 autorun.inf
    2020-05-30 16:22          0 boot/
      2020-05-10 23:45      16384 bcd
      2020-05-10 23:45    3170304 boot.sdi
      2020-05-10 23:45       1024 bootfix.bin
      2020-05-10 23:45     110392 bootsect.exe
    ...
    Also how there are two copies of this to fix:
    Code:
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        if ( buf[i] > 0 && buf[i] < 127 )
          Serial.write(buf[i]);
      }
    Last edited by defragster; 02-25-2021 at 12:19 AM.

  21. #471
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Hi @defragster and others...

    Thought I would show so more stuff that is not working
    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
    
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
    
      ExFatVolume expartVol;
    
      if (drvType == msDrive) {
        mscDrive = &msDrive1;
        if (!volName.begin(&msDrive1)) {
          Serial.println("EXFat volName.begin failed");
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if (drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
    
      root.read(buf, 32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2 * i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    // Get Fat16 volume name.
    bool getFat16VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
      MbrSector_t mbr;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        // lets read in the Master boot record...
        msc1.usbDrive()->readSector(0, (uint8_t*)&mbr);
        MbrPart_t *pt = &mbr.part[part - 1];
        uint32_t starting_sector = getLe32(pt->relativeSectors);
        msc1.usbDrive()->readSector(starting_sector, buf);
        Serial.printf("\nFAT first sector:(%x)\n", starting_sector);
        print_hexbytes(buf, 512);
        pbs_t* pbs = (pbs_t*)&buf;
        BpbFat16_t *bpt16 = reinterpret_cast<BpbFat16_t*>(pbs->bpb);
    
        Serial.print(F("Volume Name: "));
        for (size_t i = 0; i < 11; i++) {
          Serial.write(bpt16->volumeLabel[i]);
        }
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
        print_hexbytes(buf, 512);
      
        Serial.print(F("Volume Name: "));
        for (size_t i = 0; i < 11; i++) {
          Serial.write(buf[i]);
        }
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
        //    if ((pt->boot != 0 && pt->boot != 0X80) ||
        //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
        //      valid = false;
        //    }
        switch (pt->type) {
          case 4:
          case 6:
          case 0xe:
            Serial.print("FAT16: ");
            break;
          case 11:
          case 12:
            Serial.print("FAT32: ");
            break;
          case 7:
            Serial.print("exFAT: ");
            break;
        }
        partitionTable[ip - 1] = pt->type;
        dataStart[ip - 1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip)); Serial.print( ',');
        Serial.print(int(pt->boot), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x"); Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC); Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
    
    }
    
    void loop(void) {
      //--------------------------------------------------
      myusb.Task();
      if (!msDrive1) {
        Serial.println("Waiting up to 5 seconds for USB drive");
        elapsedMillis em = 0;
        while (!msDrive1 && (em < 5000) )  myusb.Task();
      }
      if (msDrive1) {
        Serial.printf("Initialize USB drive...");
    
        if (!msc1.begin(&msDrive1)) {
          msc1.errorPrint(&Serial);
          Serial.println("initialization failed.\n");
        } else {
          Serial.println("USB drive 1 is present.\n");
        }
    
        mbrDmp();
        /*
          if(msc1.fatType() == 32) {
            Serial.printf("Fat Type: Fat32\n");
            if(!getFat32VolumeLabel(msDrive))
              Serial.printf("Failed to get volume label\n");
          } else {
            Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,1))
            Serial.printf("Failed to get volume label\n");
          }
          msc1.ls(LS_SIZE | LS_DATE | LS_R);
        */
    
        for (uint8_t i = 1; i < 5; i++) {
          switch (partitionTable[i - 1]) {
            case 11:
            case 12:
              Serial.printf("\nFat Type: Fat32\n");
              if (!getFat32VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 4:
            case 6:
            case 0xe:
              Serial.printf("\nFat Type: Fat16\n");
              if (!getFat16VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 7:
              Serial.printf("\nFat Type: ExFat\n");
              if (!getExFatVolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            default:
              Serial.println("No or Not Supported Partition");
          }
        }
      }
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
        Serial.println("SD card is present.\n");
      }
    
      if (sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if (!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if (!getExFatVolumeLabel(sdDrive, 1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
      Serial.println("Press any key to run again");
      while (Serial.read() == -1);
      while (Serial.read() != -1);
    }
    
    void print_hexbytes(const void *ptr, int len)
    {
      if (ptr == NULL || len <= 0) return;
      const uint8_t *p = (const uint8_t *)ptr;
      while (len) {
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%02X ", p[i]);
        }
        Serial.print(":");
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%c", ((p[i] >= ' ') && (p[i] <= '~')) ? p[i] : '.');
        }
        Serial.println();
        p += 32;
        len -= 32;
      }
    }
    Code:
    Waiting up to 5 seconds for USB drive
    Initialize USB drive...FsVolume::begin(20001b70)
    ExFatPartition::init(20001b70, 1)
    FatPartition::init(20001b70, 1)
    USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
    FAT16: 2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
    exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    
    Fat Type: Fat32
    FatPartition::init(20001b70, 1)
    Volume Name: VOLFAT32   
    2010-03-22 07:11    3343737 DSC03357.JPG
    2021-01-01 00:00          0 example.txt
    2021-02-24 16:35          0 OnFat32.txt
    
    Fat Type: Fat16
    FatPartition::init(20001b70, 2)
    
    FAT first sector:(7d0800)
    EB 3C 90 4D 53 44 4F 53 35 2E 30 00 02 20 08 00 02 00 02 00 00 F8 00 01 3F 00 FF 00 00 08 7D 00 :.<.MSDOS5.0.. ..........?.....}.
    00 00 20 00 80 00 29 19 4C 09 02 4E 4F 20 4E 41 4D 45 20 20 20 20 46 41 54 31 36 20 20 20 33 C9 :.. ...).L..NO NAME    FAT16   3.
    8E D1 BC F0 7B 8E D9 B8 00 20 8E C0 FC BD 00 7C 38 4E 24 7D 24 8B C1 99 E8 3C 01 72 1C 83 EB 3A :....{.... .....|8N$}$....<.r...:
    66 A1 1C 7C 26 66 3B 07 26 8A 57 FC 75 06 80 CA 02 88 56 02 80 C3 10 73 EB 33 C9 8A 46 10 98 F7 :f..|&f;.&.W.u.....V....s.3..F...
    66 16 03 46 1C 13 56 1E 03 46 0E 13 D1 8B 76 11 60 89 46 FC 89 56 FE B8 20 00 F7 E6 8B 5E 0B 03 :f..F..V..F....v.`.F..V.. ....^..
    C3 48 F7 F3 01 46 FC 11 4E FE 61 BF 00 00 E8 E6 00 72 39 26 38 2D 74 17 60 B1 0B BE A1 7D F3 A6 :.H...F..N.a......r9&8-t.`....}..
    61 74 32 4E 74 09 83 C7 20 3B FB 72 E6 EB DC A0 FB 7D B4 7D 8B F0 AC 98 40 74 0C 48 74 13 B4 0E :at2Nt... ;.r.....}.}....@t.Ht...
    BB 07 00 CD 10 EB EF A0 FD 7D EB E6 A0 FC 7D EB E1 CD 16 CD 19 26 8B 55 1A 52 B0 01 BB 00 00 E8 :.........}....}......&.U.R......
    3B 00 72 E8 5B 8A 56 24 BE 0B 7C 8B FC C7 46 F0 3D 7D C7 46 F4 29 7D 8C D9 89 4E F2 89 4E F6 C6 :;.r.[.V$..|...F.=}.F.)}...N..N..
    06 96 7D CB EA 03 00 00 20 0F B6 C8 66 8B 46 F8 66 03 46 1C 66 8B D0 66 C1 EA 10 EB 5E 0F B6 C8 :..}..... ...f.F.f.F.f..f....^...
    4A 4A 8A 46 0D 32 E4 F7 E2 03 46 FC 13 56 FE EB 4A 52 50 06 53 6A 01 6A 10 91 8B 46 18 96 92 33 :JJ.F.2....F..V..JRP.Sj.j...F...3
    D2 F7 F6 91 F7 F6 42 87 CA F7 76 1A 8A F2 8A E8 C0 CC 02 0A CC B8 01 02 80 7E 02 0E 75 04 B4 42 :......B...v..............~..u..B
    8B F4 8A 56 24 CD 13 61 61 72 0B 40 75 01 42 03 5E 0B 49 75 06 F8 C3 41 BB 00 00 60 66 6A 00 EB :...V$..aar.@u.B.^.Iu...A...`fj..
    B0 42 4F 4F 54 4D 47 52 20 20 20 20 0D 0A 52 65 6D 6F 76 65 20 64 69 73 6B 73 20 6F 72 20 6F 74 :.BOOTMGR    ..Remove disks or ot
    68 65 72 20 6D 65 64 69 61 2E FF 0D 0A 44 69 73 6B 20 65 72 72 6F 72 FF 0D 0A 50 72 65 73 73 20 :her media....Disk error...Press 
    61 6E 79 20 6B 65 79 20 74 6F 20 72 65 73 74 61 72 74 0D 0A 00 00 00 00 00 00 00 AC CB D8 55 AA :any key to restart............U.
    Volume Name: NO NAME    
    2021-01-13 17:56     340510 T4-Cardlike.jpg
    2021-02-24 16:34          0 OnFat16.txt
    
    Fat Type: ExFat
    ExFatPartition::init(2006fb0c, 1)
    EXFat volName.begin failed
    Failed to get volume label
    No or Not Supported Partition
    
    Initialize SD card...FsVolume::begin(200033f0)
    ExFatPartition::init(200033f0, 1)
    FatPartition::init(200033f0, 1)
    SD card is present.
    
    Fat Type: ExFAT
    ExFatPartition::init(2006ff9c, 1)
    Failed to get volume label
    done...
    Press any key to run again
    For the fun of it, I tried to pull the volume label for Fat16 out of the Master boot record, but as you see it says the name is: NO NAME...

    And and it is not liking my exfat...

    Still investigating

  22. #472
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Have Fat16 working I believe at least for USB, have not tried with SD yet...
    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
    
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
    
      ExFatVolume expartVol;
    
      if (drvType == msDrive) {
        mscDrive = &msDrive1;
        if (!volName.begin(&msDrive1)) {
          Serial.println("EXFat volName.begin failed");
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if (drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
    
      root.read(buf, 32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2 * i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    // Get Fat16 volume name.
    bool getFat16VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
      MbrSector_t mbr;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.rootDirStart(), buf);
    //    print_hexbytes(buf, 512);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
        print_hexbytes(buf, 512);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
        //    if ((pt->boot != 0 && pt->boot != 0X80) ||
        //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
        //      valid = false;
        //    }
        switch (pt->type) {
          case 4:
          case 6:
          case 0xe:
            Serial.print("FAT16: ");
            break;
          case 11:
          case 12:
            Serial.print("FAT32: ");
            break;
          case 7:
            Serial.print("exFAT: ");
            break;
        }
        partitionTable[ip - 1] = pt->type;
        dataStart[ip - 1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip)); Serial.print( ',');
        Serial.print(int(pt->boot), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x"); Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC); Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
    
    }
    
    void loop(void) {
      //--------------------------------------------------
      myusb.Task();
      if (!msDrive1) {
        Serial.println("Waiting up to 5 seconds for USB drive");
        elapsedMillis em = 0;
        while (!msDrive1 && (em < 5000) )  myusb.Task();
      }
      if (msDrive1) {
        Serial.printf("Initialize USB drive...");
    
        if (!msc1.begin(&msDrive1)) {
          msc1.errorPrint(&Serial);
          Serial.println("initialization failed.\n");
        } else {
          Serial.println("USB drive 1 is present.\n");
        }
    
        mbrDmp();
        /*
          if(msc1.fatType() == 32) {
            Serial.printf("Fat Type: Fat32\n");
            if(!getFat32VolumeLabel(msDrive))
              Serial.printf("Failed to get volume label\n");
          } else {
            Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,1))
            Serial.printf("Failed to get volume label\n");
          }
          msc1.ls(LS_SIZE | LS_DATE | LS_R);
        */
    
        for (uint8_t i = 1; i < 5; i++) {
          switch (partitionTable[i - 1]) {
            case 11:
            case 12:
              Serial.printf("\nFat Type: Fat32\n");
              if (!getFat32VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 4:
            case 6:
            case 0xe:
              Serial.printf("\nFat Type: Fat16\n");
              if (!getFat16VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 7:
              Serial.printf("\nFat Type: ExFat\n");
              if (!getExFatVolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            default:
              Serial.println("No or Not Supported Partition");
          }
        }
      }
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
        Serial.println("SD card is present.\n");
      }
    
      if (sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if (!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if (!getExFatVolumeLabel(sdDrive, 1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
      Serial.println("Press any key to run again");
      while (Serial.read() == -1);
      while (Serial.read() != -1);
    }
    
    void print_hexbytes(const void *ptr, int len)
    {
      if (ptr == NULL || len <= 0) return;
      const uint8_t *p = (const uint8_t *)ptr;
      while (len) {
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%02X ", p[i]);
        }
        Serial.print(":");
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%c", ((p[i] >= ' ') && (p[i] <= '~')) ? p[i] : '.');
        }
        Serial.println();
        p += 32;
        len -= 32;
      }
    }
    Code:
    Waiting up to 5 seconds for USB drive
    Initialize USB drive...FsVolume::begin(20001b58)
    ExFatPartition::init(20001b58, 1)
    FatPartition::init(20001b58, 1)
    USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 1,0,0x4,0x1,0x4,0xB,0xFE,0xC2,0xFF,2048,8192000
    FAT16: 2,0,0xE,0x51,0xFE,0xE,0x98,0x98,0x80,8194048,2097152
    exFAT: 3,0,0x98,0x99,0x80,0x7,0xEE,0xDC,0xD2,10291200,5435392
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    
    Fat Type: Fat32
    FatPartition::init(20001b58, 1)
    Volume Name: VOLFAT32   
    2010-03-22 07:11    3343737 DSC03357.JPG
    2021-01-01 00:00          0 example.txt
    2021-02-24 16:35          0 OnFat32.txt
    
    Fat Type: Fat16
    FatPartition::init(20001b58, 2)
    Volume Name: VOLFAT16   
    2021-01-13 17:56     340510 T4-Cardlike.jpg
    2021-02-24 16:34          0 OnFat16.txt
    
    Fat Type: ExFat
    ExFatPartition::init(2006fb0c, 1)
    EXFat volName.begin failed
    Failed to get volume label
    No or Not Supported Partition
    
    Initialize SD card...FsVolume::begin(200033f0)
    ExFatPartition::init(200033f0, 1)
    FatPartition::init(200033f0, 1)
    SD card is present.
    
    Fat Type: ExFAT
    ExFatPartition::init(2006ff9c, 1)
    Failed to get volume label
    done...
    Press any key to run again

  23. #473
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    6,972
    @KurtE
    Fantastic. I was playing with and trying to make it work like FAT by opening the root directory but failed miserably. Where did you find the rootDirStart?:
    Code:
    msc1.usbDrive()->readSector(partVol.rootDirStart(), buf);
    Code:
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT16: 1,80,0x1,0x1,0x0,0xE,0xFE,0x3F,0x79,63,1974208
    2,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    
    Fat Type: Fat16
    Volume Name: NEW VOLUME 
    2020-11-17 13:56     220021 T4.1-Cardlike0.jpg
    No or Not Supported Partition
    No or Not Supported Partition
    No or Not Supported Partition
    My guess is that it would work the same same way except you would do
    Code:
    sd.card()->readSector(sd.rootDirStart(), buf);
    it seems pretty consistent.

    PS: when I saw what you did it made me smile and seem obvious

  24. #474
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    Just back ... 'not Working' is the norm ... didn't read enough of the thread
    > this with minimal USB 2.0 'hub' - not powered

    Updated above post #470 ::
    That is how Windows sees it, MSFT formatted it when they put on a Win 10 Install image on it

    If the drive is plugged on power up it fails to find?
    Actually it seems to hang forever ????

    Start up and the 'Write Protected' by switch drive has a light that blinks access ... then nothing but this:
    Code:
    Waiting up to 5 seconds for USB drive
    Initialize USB drive...
    If the 'Write Protect' switch is flipped "OFF" - making the drive "vulnerable" : it starts fine? Same as if powered with NO drive then plug drive and enter USB to ReScan - and it finds and mounts properly.

    Second ODD drive - the two part 64GB 'Recovery Drive' starts like this from Teensy power on:
    Code:
    Waiting up to 5 seconds for USB drive
    
    Initialize SD card...initialization failed.
    
    Fat Type: ExFAT
    Failed to get volume label
    done...
    Press any key to run again
    Then 'Press any key' and the drive appears:
    Code:
    Initialize USB drive...USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 1,80,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,67108864
    FAT32: 2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,67110912,54128640
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
    
    Fat Type: Fat32
    Volume Name: B
    NOTE: cut paste error Volume Name' print still printing BAD CHARS ... Add in two places here to the code from p#471:
    << EDITED CODE per post #475 >> >> With ISR CODE :: to Enable "#define SHOW_CLOCK_CARAT 1" to Activate: cmsReport=0; to DeActivate: cmsReport=-1;
    Code:
    //  VolumeName.ino
    //  An example of how to retrieve Fat32 and ExFat volume names using SdFat.
    //  Works with SD cards and USB mass storage drives.
    
    #include "Arduino.h"
    #include "mscFS.h"
    
    // Setup USBHost_t36 and as many HUB ports as needed.
    USBHost myusb;
    USBHub hub1(myusb);
    USBHub hub2(myusb);
    USBHub hub3(myusb);
    USBHub hub4(myusb);
    
    IntervalTimer clocked100ms;
    volatile int32_t cmsReport = -1;
    
    // Setup MSC for the number of USB Drives you are using. (Two for this example)
    // Mutiple  USB drives can be used. Hot plugging is supported. There is a slight
    // delay after a USB MSC device is plugged in. This is waiting for initialization
    // but after it is initialized ther should be no delay.
    msController msDrive1(myusb);
    msController msDrive2(myusb);
    
    #define sdDrive 1
    #define msDrive 2
    
    #define SD_CONFIG SdioConfig(FIFO_SDIO)
    
    // set up variables using the mscFS utility library functions:
    UsbFs msc1;
    SdFs sd;
    //FatVolume partVol;
    
    //create holding array for partions
    uint8_t partitionTable[4];
    int32_t dataStart[4];
    
    // Get ExFat volume name.
    bool getExFatVolumeLabel(uint8_t  drvType, uint8_t part) {
      uint8_t buf[32];
      UsbExFat volName;
      SdExFat volName1;
      ExFatFile root;
      msController *mscDrive;
      DirLabel_t *dir;
    
      ExFatVolume expartVol;
    
      if (drvType == msDrive) {
        mscDrive = &msDrive1;
        if (!volName.begin(&msDrive1)) {
          Serial.println("EXFat volName.begin failed");
          return false;
        }
        expartVol.begin(msc1.usbDrive(), true, part);
        expartVol.chvol();
        if (!root.openRoot(&expartVol)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
      if (drvType == sdDrive) {
        if (!volName1.begin(SD_CONFIG)) {
          return false;
        }
        if (!root.openRoot(&volName1)) {
          Serial.println("openRoot failed");
          return false;
        }
      }
    
      root.read(buf, 32);
      dir = reinterpret_cast<DirLabel_t*>(buf);
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < dir->labelLength; i++) {
        Serial.write(dir->unicode[2 * i]);
      }
      Serial.println();
      return true;
    }
    
    // Get Fat32 volume name.
    bool getFat32VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        msc1.usbDrive()->readSector(partVol.dataStartSector(), buf);
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
      }
      Serial.print(F("Volume Name: "));
      for (size_t i = 0; i < 11; i++) {
        if ( buf[i] > 0 && buf[i] < 127 )
          Serial.write(buf[i]);
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    // Get Fat16 .
    bool getFat16VolumeLabel(uint8_t  drvType, uint8_t part) {
      FatVolume partVol;
      MbrSector_t mbr;
    
      uint8_t buf[512];
    
      partVol.begin(msc1.usbDrive(), true, part);
      partVol.chvol();
      if (drvType == msDrive) {
        // lets read in the Master boot record...
        msc1.usbDrive()->readSector(0, (uint8_t*)&mbr);
        MbrPart_t *pt = &mbr.part[part - 1];
        uint32_t starting_sector = getLe32(pt->relativeSectors);
        msc1.usbDrive()->readSector(starting_sector, buf);
        Serial.printf("\nFAT first sector:(%x)\n", starting_sector);
        print_hexbytes(buf, 512);
        pbs_t* pbs = (pbs_t*)&buf;
        BpbFat16_t *bpt16 = reinterpret_cast<BpbFat16_t*>(pbs->bpb);
    
        Serial.print(F("Volume Name: "));
        for (size_t i = 0; i < 11; i++) {
          Serial.write(bpt16->volumeLabel[i]);
        }
      }
    
      if (drvType == sdDrive) {
        sd.card()->readSector(sd.dataStartSector(), buf);
        print_hexbytes(buf, 512);
      
        Serial.print(F("Volume Name: "));
        for (size_t i = 0; i < 11; i++) {
          if ( buf[i] > 0 && buf[i] < 127 )
            Serial.write(buf[i]);
        }
      }
      Serial.println();
      partVol.ls(LS_SIZE | LS_DATE | LS_R);
    
      return true;
    }
    
    
    
    bool mbrDmp() {
      MbrSector_t mbr;
      bool valid = true;
      if (!msc1.usbDrive()->readSector(0, (uint8_t*)&mbr)) {
        Serial.print("\nread MBR failed.\n");
        //errorPrint();
        return false;
      }
      Serial.print("\nmsc1 Partition Table\n");
      Serial.print("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
      for (uint8_t ip = 1; ip < 5; ip++) {
        MbrPart_t *pt = &mbr.part[ip - 1];
        //    if ((pt->boot != 0 && pt->boot != 0X80) ||
        //        getLe32(pt->relativeSectors) > sdCardCapacity(&m_csd)) {
        //      valid = false;
        //    }
        switch (pt->type) {
          case 4:
          case 6:
          case 0xe:
            Serial.print("FAT16: ");
            break;
          case 11:
          case 12:
            Serial.print("FAT32: ");
            break;
          case 7:
            Serial.print("exFAT: ");
            break;
        }
        partitionTable[ip - 1] = pt->type;
        dataStart[ip - 1] = getLe32(pt->relativeSectors);
        Serial.print( int(ip)); Serial.print( ',');
        Serial.print(int(pt->boot), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->beginCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print("0x"); Serial.print(int(pt->type), HEX); Serial.print( ',');
        for (int i = 0; i < 3; i++ ) {
          Serial.print("0x"); Serial.print(int(pt->endCHS[i]), HEX); Serial.print( ',');
        }
        Serial.print(getLe32(pt->relativeSectors), DEC); Serial.print(',');
        Serial.println(getLe32(pt->totalSectors));
      }
      return true;
    }
    
    void setup()
    {
    #if 0 // easy test to check HardFault Detection response
      int *pp=0;
      *pp=5;
    #endif
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        SysCall::yield(); // wait for serial port to connect.
      }
    
      // Start USBHost_t36, HUB(s) and USB devices.
      myusb.begin();
    
    //#define SHOW_CLOCK_CARAT 1
    #ifdef SHOW_CLOCK_CARAT
      clocked100ms.begin(clock_isr, 100000);
    #endif
    
    }
    
    void clock_isr() {
        if (cmsReport >= 0 ) {
          if (cmsReport > 0 ) {
          if (cmsReport <10 ) 
            Serial.print( "^");
          else if ( !(cmsReport%10) ) 
            Serial.print( "~");
          }
          cmsReport++;
        }
    }
    
    
    
    void loop(void) {
      //--------------------------------------------------
      cmsReport=0;
      myusb.Task();
      if (!msDrive1) {
        Serial.println("Waiting up to 5 seconds for USB drive");
        elapsedMillis em = 0;
        while (!msDrive1 && (em < 5000) )  myusb.Task();
      }
      if (msDrive1) {
        Serial.printf("Initialize USB drive...");
    
        if (!msc1.begin(&msDrive1)) {
          msc1.errorPrint(&Serial);
          Serial.println("initialization failed.\n");
        } else {
          Serial.println("USB drive 1 is present.\n");
        }
        cmsReport=-1;
    
        mbrDmp();
        /*
          if(msc1.fatType() == 32) {
            Serial.printf("Fat Type: Fat32\n");
            if(!getFat32VolumeLabel(msDrive))
              Serial.printf("Failed to get volume label\n");
          } else {
            Serial.printf("Fat Type: ExFat\n");
          if(!getExFatVolumeLabel(msDrive,1))
            Serial.printf("Failed to get volume label\n");
          }
          msc1.ls(LS_SIZE | LS_DATE | LS_R);
        */
    
        for (uint8_t i = 1; i < 5; i++) {
          switch (partitionTable[i - 1]) {
            case 11:
            case 12:
              Serial.printf("\nFat Type: Fat32\n");
              if (!getFat32VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 4:
            case 6:
            case 0xe:
              Serial.printf("\nFat Type: Fat16\n");
              if (!getFat16VolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            case 7:
              Serial.printf("\nFat Type: ExFat\n");
              if (!getExFatVolumeLabel(msDrive, i))
                Serial.printf("Failed to get volume label\n");
              break;
            default:
              Serial.println("No or Not Supported Partition");
          }
        }
      }
      cmsReport=-1;
      //--------------------------------------------------
      Serial.printf("\nInitialize SD card...");
    
      if (!sd.begin(SD_CONFIG)) {
        Serial.println("initialization failed.\n");
      } else {
        Serial.println("SD card is present.\n");
      }
    
      if (sd.fatType() == 32) {
        Serial.printf("Fat Type: Fat32\n");
        if (!getFat32VolumeLabel(sdDrive, 1))
          Serial.printf("Failed to get volume label\n");
      } else {
        Serial.printf("Fat Type: ExFAT\n");
        if (!getExFatVolumeLabel(sdDrive, 1)) {
          Serial.printf("Failed to get volume label\n");
        }
      }
      //sd.ls(LS_SIZE | LS_DATE | LS_R);
    
      Serial.println("done...");
    
      Serial.println("Press any key to run again");
      while (Serial.read() == -1);
      while (Serial.read() != -1);
    }
    
    void print_hexbytes(const void *ptr, int len)
    {
      if (ptr == NULL || len <= 0) return;
      const uint8_t *p = (const uint8_t *)ptr;
      while (len) {
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%02X ", p[i]);
        }
        Serial.print(":");
        for (uint8_t i = 0; i < 32; i++) {
          if (i > len) break;
          Serial.printf("%c", ((p[i] >= ' ') && (p[i] <= '~')) ? p[i] : '.');
        }
        Serial.println();
        p += 32;
        len -= 32;
      }
    }
    Last edited by defragster; 02-25-2021 at 01:30 AM.

  25. #475
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,936
    Just made this ehci.cpp change to my system - No change to (1) on the startup Hang with write protected drive:
    Code:
    	USBHS_USBCMD = USBHS_USBCMD_ITC(1) | USBHS_USBCMD_RS |
    This system has HardFault detection enabled - code is Not faulting on the hang!
    >> Will update p#474 post with this ISR CODE :: to Enable "#define SHOW_CLOCK_CARAT 1" to Activate: cmsReport=0; to DeActivate: cmsReport=-1;

    Turned on a Timer _isr() to print the '^' every 100ms {without .flush} for first 10 then only prints 1 each second and I see this - never exiting the calls made in loop until restarted:
    Code:
    ^Waiting up to 5 seconds for USB drive
    ^^Initialize USB drive...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Waiting up to 5 seconds for USB drive
    ^^Initialize USB drive...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Then Teensy power on without write protected Flash inserted until after initial scan, then pressed a key and the drive is found:
    Code:
    Waiting up to 5 seconds for USB drive
    ^^^^^^^^^^^^^
    Initialize SD card...initialization failed.
    
    Fat Type: ExFAT
    Failed to get volume label
    done...
    Press any key to run again
    Initialize USB drive...^^^^^^^^^USB drive 1 is present.
    
    
    msc1 Partition Table
    That looks like this powering the Teensy with the Other 2 partition 64GB drive in place:
    Code:
    Waiting up to 5 seconds for USB drive
    ^^^^^^^^^^^^^Initialize USB drive...USB drive 1 is present.
    
    
    msc1 Partition Table
    part,boot,bgnCHS[3],type,endCHS[3],start,length
    FAT32: 1,80,0x20,0x21,0x0,0xC,0xFE,0xFF,0xFF,2048,67108864
    FAT32: 2,0,0xFE,0xFF,0xFF,0xC,0xFE,0xFF,0xFF,67110912,54128640
    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •