timings M0 running slower if plugged to USB wall charger

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

timings M0 running slower if plugged to USB wall charger

Post by jteo »

Hi!

I have an ItsyBitsy M0 Express and a simple code that toggles a PIN (the code follows).

With this code, I observe with the oscilloscope correct timings for the PIN toggling if the ItsyBitsy is connected to my laptop via USB. If instead, I connect the ItsyBitsy via USB to a USB wall charger, the timings are slowed down by 2% more or less.

Where could this come from?

Code: Select all

#include <Arduino.h>

const int testPin = 2;

void setup()
{
  pinMode(testPin, OUTPUT);
  digitalWrite(testPin, LOW);
}

void loop()
{
  digitalWrite(testPin, HIGH);
  delay(500);
  digitalWrite(testPin, LOW);
  delay(500);
}
Thanks!

User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

Re: timings M0 running slower if plugged to USB wall charger

Post by jteo »

If I use to attach to my laptop a USB cable without data lines, only with power lines, I also see the slowing down of the timings.

User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

Re: timings M0 running slower if plugged to USB wall charger

Post by jteo »

New test... if I comment out in main.cpp

Code: Select all

  USBDevice.init();
  USBDevice.attach();
the M0 starts running slower.

Any idea where it could come from?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: timings M0 running slower if plugged to USB wall charger

Post by adafruit_support_bill »

The loop in main.cpp checks for serial communication activity between iterations of loop(). With nothing connected it is probably timing out waiting for a response.

Code: Select all

  for (;;)
  {
    loop();
    if (serialEventRun) serialEventRun();
  }
In any case, the delay() function is not a particularly reliable way to time loops since it does not account for execution time of the rest of the code in loop() or in the for loop that calls it from main().

User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

Re: timings M0 running slower if plugged to USB wall charger

Post by jteo »

Thanks for the reply.

However, why even with a while(1) in the loop function I see the same behavior?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: timings M0 running slower if plugged to USB wall charger

Post by adafruit_support_bill »

A while(1) where in your loop? Please post the code you are using.

User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

Re: timings M0 running slower if plugged to USB wall charger

Post by jteo »

Here the sketch I am using, which is still reproducing the same timing issue, even though the code should stay within the loop function and not reach the check for serial communication activity as done in main.cpp.

Code: Select all

#include <Arduino.h>

const int testPin = 2;

void setup()
{
  pinMode(testPin, OUTPUT);
  digitalWrite(testPin, LOW);
}

void loop()
{ 
    while(1) {
      delay(500);
      digitalWrite(testPin, !digitalRead(testPin));
    }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: timings M0 running slower if plugged to USB wall charger

Post by adafruit_support_bill »

delay() for the SAMD family processors is not a tight-loop. It includes a yield() to allow background processing to occur.

Code: Select all

void delay( unsigned long ms )
{
  if (ms == 0)
  {
    return;
  }

  uint32_t start = micros();

  while (ms > 0)
  {
    yield();
    while (ms > 0 && (micros() - start) >= 1000)
    {
      ms--;
      start += 1000;
    }
  }
}

User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

Re: timings M0 running slower if plugged to USB wall charger

Post by jteo »

Many thanks for the information and hints.

I did some additional test replacing the delay() with a continuous loop using just millis() to check when enough time was passed (no yield()); still the same behavior (correct timings when connected with the laptop, i.e., 1 Hz; slower timings when connected to the wall charger, i.e., 0.968 Hz).

While searching around, this document came up: https://blog.thea.codes/understanding-t ... 21-clocks/
Could my issue be related to the "USB clock recovery mode" mentioned in the document?

Thanks again a lot for your support!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: timings M0 running slower if plugged to USB wall charger

Post by adafruit_support_bill »

That does sound consistent with what you are seeing. The Itsy Bitsy M0 does not have an external crystal. So it would be logical to run in that mode. With no USB signal as a reference, the clock would run open loop and could end-up either faster or slower than the USB-derived frequency.
* If there isn't a USB connection then things work just as they did in open loop mode. The DFLL outputs a clock close to 48 MHz and does not have a reference clock.
* When there is a USB connection then things work similar to the way they did in closed loop mode. The DFLL follows the 1 kHz USB start of frame message and outputs a much more accurate 48 MHz clock

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: timings M0 running slower if plugged to USB wall charger

Post by westfw »

Yeah, when connected to USB, the clock will be synchronized to the USB timing of the host, which should be very accurate.

When not connected to USB, the clocks will be based in an internal oscillator with a somewhat limited accuracy; about 2% (see datasheet: 37.13.3 Digital Frequency Locked Loop (DFLL48M) Characteristics and 37.13.6 8MHz RC Oscillator (OSC8M) Characteristics)

There are some calibration values and opportunities to have misconfigured the clock so that it is consistently off instead of randomly off, but it would take careful analysis of the startup code by someone with more expertise and/or patience that I have :-(

User avatar
jteo
 
Posts: 10
Joined: Tue Aug 24, 2021 5:07 am

Re: timings M0 running slower if plugged to USB wall charger

Post by jteo »

Thanks for the replies!

I managed to find a fix for it. In the code the follows, in the loop() I read the values of the coarse and fine calibration. If the Itsy Bitsy M0 is connected to the USB and running in closed-loop, then you can read the values as calibrated online.

Code: Select all

#include <Arduino.h>

const int testPin = 10;

void setup()
{
  pinMode(testPin, OUTPUT);
  digitalWrite(testPin, LOW);
    
  Serial.begin(9600);
}

void loop()
{  
  Serial.println(SYSCTRL->DFLLCTRL.reg);
  Serial.println(SYSCTRL->DFLLVAL.bit.COARSE);
  Serial.println(SYSCTRL->DFLLVAL.bit.FINE);
  Serial.println("---");
  delay(500);
  digitalWrite(testPin, !digitalRead(testPin));
}
With those values, it is possible to calibrate the use of the open-loop mode accordingly in the setup(). For my device, it looks then something like:

Code: Select all

#include <Arduino.h>

const int testPin = 10;

void setup()
{
  pinMode(testPin, OUTPUT);
  digitalWrite(testPin, LOW);

  SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE;
  while(!SYSCTRL->PCLKSR.bit.DFLLRDY);

  SYSCTRL->DFLLVAL.bit.FINE = 640;
    
  Serial.begin(9600);
}

void loop()
{  
  Serial.println(SYSCTRL->DFLLCTRL.reg);
  Serial.println(SYSCTRL->DFLLVAL.bit.COARSE);
  Serial.println(SYSCTRL->DFLLVAL.bit.FINE);
  Serial.println("---");
  delay(500);
  digitalWrite(testPin, !digitalRead(testPin));
}
I hope it is also useful to somebody else.

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: timings M0 running slower if plugged to USB wall charger

Post by westfw »

If the Itsy Bitsy M0 is connected to the USB and running in closed-loop, then you can read the values
That is a very clever idea!

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

Return to “Itsy Bitsy Boards”