0

Why does using Serial before enabling TinyUSB usb_msc preven
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Why does using Serial before enabling TinyUSB usb_msc preven

by keylevel on Mon May 03, 2021 6:46 pm

Looking at the code within setup() for msc_ramdisk.ino:

Code: Select all | TOGGLE FULL SIZE
// the setup function runs once when you press reset or power the board
void setup()
{
  // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
  usb_msc.setID("Adafruit", "Mass Storage", "1.0");
 
  // Set disk size
  usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);

  // Set callback
  usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);

  // Set Lun ready (RAM disk is always ready)
  usb_msc.setUnitReady(true);
 
  usb_msc.begin();

  Serial.begin(115200);
  while ( !Serial ) delay(10);   // wait for native usb

  Serial.println("Adafruit TinyUSB Mass Storage RAM Disk example");
}
Why does the MSC device not work (as in, it does not show on the host PC) if the serial commands are moved to the start of the function?

Code: Select all | TOGGLE FULL SIZE
// the setup function runs once when you press reset or power the board
void setup()
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // wait for native usb

  Serial.println("Adafruit TinyUSB Mass Storage RAM Disk example");

  // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
  usb_msc.setID("Adafruit", "Mass Storage", "1.0");
 
  // Set disk size
  usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);

  // Set callback
  usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);

  // Set Lun ready (RAM disk is always ready)
  usb_msc.setUnitReady(true);
 
  usb_msc.begin();
}
I want to be able to do this so that I can use Serial before the MSC device is initialised.

I suspect this is a startup sequencing issue, but I'm not sure where to look to try and work out what's going on. Any pointers would be appreciated.

keylevel
 
Posts: 2
Joined: Mon May 03, 2021 6:40 pm

Re: Why does using Serial before enabling TinyUSB usb_msc pr

by blnkjns on Tue May 04, 2021 1:22 am

Why is having serial active first important?

blnkjns
 
Posts: 615
Joined: Fri Oct 02, 2020 3:33 am

Re: Why does using Serial before enabling TinyUSB usb_msc pr

by keylevel on Tue May 04, 2021 4:07 am

blnkjns wrote:Why is having serial active first important?


Because it is being used to allow a FLASH image that will be used as the USB disk to be checked, verified and repaired.

keylevel
 
Posts: 2
Joined: Mon May 03, 2021 6:40 pm

Re: Why does using Serial before enabling TinyUSB usb_msc pr

by hathach on Thu May 20, 2021 2:22 pm

it is very simple device is recognized by host via a process called enumeration. All USB class driver except Serial is added when its begin() is called. By the time you wait and have serial connected. The enumeration is already complete, and it does not include the MSC that calls begin() afterwards. You force host to re-enumeration by call USBDevice.detach() then USBDevice.attach() which is the same as pulling out and re-plug device (except the power is not cut off).

hathach
 
Posts: 1147
Joined: Tue Apr 23, 2013 1:02 am

Please be positive and constructive with your questions and comments.