Native USB (SerialUSB) not stable

Does someone has freezing "issues" with the native USB port of the Arduino Zero?

My project communicates with the board thorugh the native USB port, the communication starts and works, however I have strong limitations: - First, when I unplug the Microusb connector, the board freezes - Second, after not many hours the board stops working, it freezes - Third, I have noticed the USB link is working really slow, although I am theoretically using 115200 bauds

Both situation require a full reset. This is happening for example with this simple sketch

float f = 0.1;

////////////////////////////////////////////////////////////////////////////////
void setup() {
  SerialUSB.begin(115200);
  while(!SerialUSB) {}                  // wait for USB available
}
////////////////////////////////////////////////////////////////////////////////
void loop() {
  if (SerialUSB.available())  { 
           char c = SerialUSB.read() ;
           SerialUSB.print(c);
  }


  SerialUSB.println(f);
  f=f+0.2;
  delay(200);
}

I must add that my project is using the Arduino Zero design, but It has the following differences with the Arduino Zero board: I am not implementing

  • the EDBG part
  • Not using the Arduino Bootloader, I load the program directly with an Armel ICE using the SWD port.
  • I am not using an external clock, but the internal one, I had to modify the startup.c program to reconfigure the clock.

any help or advice?, How can I debug the USB communication?

Hi,

I'm pretty sure your issue comes from the internal clock. USB requires tight timing. The parameter passed to the SerialUSB.begin(int) function does nothing : the USB core always tries to go as fast as possible.

Thanks Aloyse, once again your suggestion is very useful and logical.

To use the standard internal clock, instead of an external one, I modified the standard startup.c file the following way:

From the Original Arduino File: https://github.com/arduino/ArduinoCore-samd/blob/master/cores/arduino/startup.c

  /* ----------------------------------------------------------------------------------------------
   * 1) Enable XOSC32K clock (External on-board 32.768Hz oscillator)
   */
  SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP( 0x6u ) | /* cf table 15.10 of product datasheet in chapter 15.8.6 */
                         SYSCTRL_XOSC32K_XTALEN | SYSCTRL_XOSC32K_EN32K ;
  SYSCTRL->XOSC32K.bit.ENABLE = 1 ; /* separate call, as described in chapter 15.6.3 */

changed these two instructions to:

 SYSCTRL->OSC8M.bit.ENABLE = 1 ; /* separate call, as described in chapter 15.6.3 */

It has been working pretty well, except of the USB inestability issue.

I have now started to search for clock instructions in USB Arduino core inside Github folder ArduinoCore-samd/cores/arduino/USB/

See section 32.5.3 of the datasheet :

32.5.3 Clocks ... The USB module requires a GCLK_USB of 48 MHz ± 0.25% clock for low speed and full speed operation. ...

AloyseTech: See section 32.5.3 of the datasheet :

OK, But this is a hardware issue or do I need some special register configuration?

See section 36.11.6 of datasheet :

36.11.6 8MHz RC Oscillator (OSC8M) Characteristics

It says that at best, there is a 0.06MHz uncertainty. Which is 0.75%. At 8MHz. If you do a PLL on a 8MHz clock with 0.75% uncertainty, you get a 48MHz clock with 3% uncertainty. Which is much bigger than the 0.25% tolerance for USB operation. You have to use an external 32kHz crystal.

I'm curious how your sketch is getting past this line without the USB attached:

  while(!SerialUSB) {}                  // wait for USB

That test will return false / block until a virtual COM/TTy port is opened (just attaching the cable is not enough).

GabrielNotman: I'm curious how your sketch is getting past this line without a the USB attached:

  while(!SerialUSB) {}                  // wait for USB

That test will return false / block until a virtual COM/TTy port is opened (just attaching the cable is not enough).

The communication works and the sketch starts working, the microcontroller is frozen as soon as I unplug the usb cable. I can see also that there are many issues related to USB CDC in the Arduino Github.