Why does using Serial before enabling TinyUSB usb_msc preven

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
keylevel
 
Posts: 2
Joined: Mon May 03, 2021 6:40 pm

Why does using Serial before enabling TinyUSB usb_msc preven

Post by keylevel »

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

Code: Select all

// 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

// 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.

User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Re: Why does using Serial before enabling TinyUSB usb_msc pr

Post by blnkjns »

Why is having serial active first important?

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

Re: Why does using Serial before enabling TinyUSB usb_msc pr

Post by keylevel »

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.

User avatar
hathach
 
Posts: 1271
Joined: Tue Apr 23, 2013 1:02 am

Re: Why does using Serial before enabling TinyUSB usb_msc pr

Post by hathach »

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).

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”