Go Down

Topic: Arduino Zero Boot time (Read 1 time) previous topic - next topic

jsantosrico

Hi,

I'm trying to speed up the arduino zero boot times. I'm using a custom board based on the SAMD21, and using the Arduino IDE for development. I normally upload my code via USB, but I also have an Atmel-ICE programmer.

So far, with a simple blink sketch (no serial or anything in it), I get a boot time of ~1.7s from power on to first light out.

Code: [Select]
int laserPin = 8;
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(laserPin, OUTPUT);
  digitalWrite(laserPin, HIGH);   // turn the LED on (HIGH is the voltage level)
}

// the loop function runs over and over again forever
void loop() {
  for (int aux = 0; aux < 10; aux++){
    digitalWrite(laserPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(500);                       // wait for a second
    digitalWrite(laserPin, LOW);    // turn the LED off by making the voltage LOW
    delay(500);
  }
  for (int aux = 0; aux < 10; aux++){
    digitalWrite(laserPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(50);                       // wait for a second
    digitalWrite(laserPin, LOW);    // turn the LED off by making the voltage LOW
    delay(50);
  }
// wait for a second
}


I checked on an oscilloscope, power on the microcontroller is instant, and although there might be some propagation delay (I use a driver to power a laser) it is in the microseconds range, not 1.7s. Again, I checked on an oscilloscope, and the delay is from power on to first activity on the pins of the SAMD

I think I read that removing the bootloader I might achieve faster (instant??) boot. I followed the advice of the great AloyseTech here (https://forum.arduino.cc/index.php?topic=452013.0) to remove the bootloader of my boards, and program them straight from the programmer, and I have the same boot times if I program it from the IDE using the no bootloader option, if I export the binary from the IDE and flash it with Atmel Studio and if I flash the bootloader, and export the binary and I flash it from Atmel studio without erasing the chip and starting in address 0x2000.

So for me, I have the same boot times with and without the bootloader and I was wondering if it's just the time it takes the M0 to boot. Does anyone have any idea about this?

dlabun

With no bootloader on the chip it should start running instantly. Are you using the same crystal and capacitors as the Arduino Zero?

AloyseTech

The bootloader if compiled with the double tap functionality should take at least 500ms. If you don't need the double-tap-on-reset-to-stay-in-bootloader feature, you can remove it. You'll have to look for the remaining 1.2 seconds though.

And of course, as dlabun said, if you can always use the ICE, you could remove the bootloader completely to get almost instant start.

jsantosrico

Well, then I don't get it. It might be that I didn't add as many decoupling caps as in the Zero



I have a 10uF in the 3.3V (although is not very close to VDDA :S), plus the 1uF in the VDDA/GNDA pair, and one 100nF in one of the VDDIO pins (I didn't know that I needed one on each "pair". The crystal is this one (Crystal), and I added the standard 22p as it appears (as an indication) in the SAMD21 datasheet. Sure, it's quite lacking compared to the Xplained and Zero boards, but Adafruit's Feather has even less and it seems to be working good.

Do you think that this might be the issue? Is there a way to check that the crystal is working as expected?

AloyseTech

The 1.7sec needed for booting are not an issue. I may have been a bit unclear sorry.
The boot time is around the same for my board. What I as saying is that there are 500ms taken for sure by the bootloader to check if a double tap occurred (see here). The remaining 1.2 sec might be a lot of USB stuff. Have you measured the boot time with or without USB connection? Is there a difference?

jsantosrico

Well, there is no USB connections on the board, or any USB-dependant code, it's just a blink sketch. I guess that that doesn't mean that the USB activity that you mention doesn't happen, but I can't see how to make it "not happen".

What puzzles me even more is that I'm generating the binary file for a "with bootloader" board and a "without bootloader" board, and flashing it with atmel studio (offsetting it to 0x2000 for the bootloader version), and both take the same amount of time, although when I plug a development board that has a USB socket, the one  with bootloader appears as a com port in windows, and the one without doesn't (so I think that means I managed to get rid of the bootloader).

Can you guys, with your original arduino zero boards, get rid of the bootloader, and start a blink sketch instantly from power off?

dlabun

I actually don't use the Arduino Zero, but rather the Sparkfun SAMD21 dev board. Without the boot loader my sketches do launch instantly, with it they launch in less than a second. I'd venture to guess you have a timing / crystal issue.

jsantosrico

Hi Dlabun,

I'm not saying I don't have a HW issue somewhere, but the thing is: I CAN start it instantly, if I use Atmel Studio for the development, and don't use the Arduino libraries. I attached the Atmel Studio project in a previous message, if you want to check it. If I use the Arduino IDE, then 1.7 seconds is the fastest I can make it. Regarding adding or removing the bootloader, I think I'm doing correctly, but I might be doing it wrong and not removing it. In any case, I see no differences by adding it or removing it. Sadly, I don't have an official board at hand to check. What puzzles me is that I can start it instantly with a certain code, but not with the IDE. Is not a deal breaker, but I would like to know why. I also understand that my board is custom, and hence what applies to the zero doesn't have to apply to my board.

In the next revision, I will do some more tests to get the HW in a "better" state, closer to what the Xplore and Zero boards are.

Is there a way to test if the crystal is not working as intended? In some μC there is an option to output the clocks in an IO pin. Does the SAM D21 has this option?

It would be great if someone with a Adafruit Feather board can tell us what their times are too, because they have a setup that is closer to what I have.

AloyseTech

#8
Feb 24, 2017, 03:05 pm Last Edit: Feb 24, 2017, 03:05 pm by AloyseTech
Try to comment the following line in the main.cpp of the SAMD core file :
Code: [Select]
USBDevice.init();
USBDevice.attach();

(and keep USBCON defined)

dlabun

I do have a Feather M0 Datalogger, but unfortunately it has the Adafruit boot loader still on it.

There's a way to get the SAMD21 to output its clock signals but I haven't been able to make it work. Below is a chunk of code I use to initiate a pulse output on pin 12... you should see a 400Hz pulse with this code if your clocks are working right.

Code: [Select]
REG_GCLK_GENDIV = GCLK_GENDIV_DIV(3) |          // Divide the 48MHz clock source by divisor 3: 48MHz/3=16MHz
                    GCLK_GENDIV_ID(4);            // Select Generic Clock (GCLK) 4
  while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization

  REG_GCLK_GENCTRL = GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                     GCLK_GENCTRL_GENEN |         // Enable GCLK4
                     GCLK_GENCTRL_SRC_DFLL48M |   // Set the 48MHz clock source
                     GCLK_GENCTRL_ID(4);          // Select GCLK4
  while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization

  // Enable the port multiplexer for the 1 PWM channel: timer TCC0 output
  const uint8_t CHANNELS = 1;
  const uint8_t pwmPins[] = { 12 };
  for (uint8_t i = 0; i < CHANNELS; i++)
  {
     PORT->Group[g_APinDescription[pwmPins[i]].ulPort].PINCFG[g_APinDescription[pwmPins[i]].ulPin].bit.PMUXEN = 1;
  }
  // Connect the TCC0 timer to the port outputs - port pins are paired odd PMUO and even PMUXE
  // F & E specify the timers: TCC0, TCC1 and TCC2
 
  PORT->Group[g_APinDescription[12].ulPort].PMUX[g_APinDescription[12].ulPin >> 1].reg = PORT_PMUX_PMUXO_F;

  // Feed GCLK4 to TCC0 and TCC1
  /*REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN |         // Enable GCLK4 to TCC0 and TCC1
                     GCLK_CLKCTRL_GEN_GCLK4 |     // Select GCLK4
                     GCLK_CLKCTRL_ID_TCC0_TCC1;   // Feed GCLK4 to TCC0 and TCC1*/
  GCLK->CLKCTRL.reg = 0x441A;                     // Added for compilation problem with Arduino M0
  while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization

  // Dual slope PWM operation: timers countinuously count up to PER register value then down 0
  REG_TCC0_WAVE |= TCC_WAVE_POL(0xF) |         // Reverse the output polarity on all TCC0 outputs
                    TCC_WAVE_WAVEGEN_DSBOTTOM;    // Setup dual slope PWM on TCC0
  while (TCC0->SYNCBUSY.bit.WAVE);               // Wait for synchronization

  // Each timer counts up to a maximum or TOP value set by the PER register,
  // this determines the frequency of the PWM operation:
  // 20000 = 50Hz, 10000 = 100Hz, 2500  = 400Hz, 2702 = 370Hz



   REG_TCC0_PER = 2500;     
   
  while(TCC0->SYNCBUSY.bit.PER);

  // The CCBx register value corresponds to the pulsewidth in microseconds (us)
  REG_TCC0_CCB3 = 33000;       // TCC0 CCB3 - center the servo on D7
  while(TCC0->SYNCBUSY.bit.CCB3);

  // Divide the 16MHz signal by 8 giving 2MHz (0.5us) TCC0 timer tick and enable the outputs
  REG_TCC0_CTRLA |= TCC_CTRLA_PRESCALER_DIV8 |    // Divide GCLK4 by 8
                    TCC_CTRLA_ENABLE;             // Enable the TCC0 output
  while (TCC0->SYNCBUSY.bit.ENABLE);              // Wait for synchronization
}

AloyseTech

You can also use the Arduino PWM : if clocked right, the frequency should be 732.4Hz when using analogWrite(pin,value)

dlabun

Oh.. Duh, Forgot about the built in PWM feature... Either way, test a pulsing output to see how accurate it is.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy