Author Topic: Problem with Reboot into bootloader  (Read 7523 times)

fh_

  • Newbie
  • *
  • Posts: 5
Problem with Reboot into bootloader
« on: February 09, 2015, 12:58:09 am »
Hi,
I want to use a reboot function to boot into the boadloader and upload new code.
In my Program I use DigiCDC.
However the reboot function from the wiki/tricks does not work for that.
It reboots fine but when I try to flash new code I get always a
"Run error -1 has occured ..."
and I have to reflash after reset.
I testet Bootloader 1.11 & 1.06.

Any Idea?
Thank you in advance.

My Code:
Code: [Select]
#include <DigiCDC.h>

void reboot(void) {
  noInterrupts(); // disable interrupts which could mess with changing prescaler
  CLKPR = 0b10000000; // enable prescaler speed change
  CLKPR = 0; // set prescaler to default (16mhz) mode required by bootloader
  void (*ptrToFunction)(); // allocate a function pointer
  ptrToFunction = 0x0000; // set function pointer to bootloader reset vector
  (*ptrToFunction)(); // jump to reset, which bounces in to bootloader
}

void setup() {               
  SerialUSB.begin();
}

void loop() {
  if (SerialUSB.available()) {
    char input = SerialUSB.read();
    if(input == 'N') {
      SerialUSB.end();
      for(;;);     // Wait for Reset
    } 
    else if(input == 'R') {
      SerialUSB.end();
      delay(3000);
      reboot(); 
    } 
  }
  SerialUSB.delay(50);   
}

/*
USING REBOOT FUNCTION:
OUTPUT  Arduino1.5.8

Der Sketch verwendet 3.652 Bytes (60%) des Programmspeicherplatzes. Das Maximum sind 6.012 Bytes.

Globale Variablen verwenden 214 Bytes des dynamischen Speichers.

Running Digispark Uploader...
Plug in device now... (will timeout in 60 seconds)
> Please plug in the device ...
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.11
> Available space for user applications: 6330 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 99  page size: 64
> Erase function sleep duration: 792ms
parsing: 50% complete
> Erasing the memory ...
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
> Starting to upload ...
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app ...
>> Run error -1 has occured ...
>> Please unplug the device and restart the program.

I have to REFLASH the programm only reset does not help!!!

----------------------------------------------------------
USING HARDWARE RESET:

Der Sketch verwendet 3.652 Bytes (60%) des Programmspeicherplatzes. Das Maximum sind 6.012 Bytes.

Globale Variablen verwenden 214 Bytes des dynamischen Speichers.

Running Digispark Uploader...
Plug in device now... (will timeout in 60 seconds)
> Please plug in the device ...
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.11
> Available space for user applications: 6330 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 99  page size: 64
> Erase function sleep duration: 792ms
parsing: 50% complete
> Erasing the memory ...
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
> Starting to upload ...
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app ...
running: 100% complete
>> Micronucleus done. Thank you!
*/

fh_

  • Newbie
  • *
  • Posts: 5
Re: Problem with Reboot into bootloader
« Reply #1 on: February 09, 2015, 08:50:25 pm »
Anyway, did you already try to replace
delay(3000);  by
SerialUSB.delay(3000);  ?

Thank you for fast answer I tried but
I close  the USB by 'SerialUSB.end()' befor I delay and reboot.

In my opinion the problem is, that the bootloader requires something to be initialized is a way a hard reset makes but the reboot function doesn't made.
But I have no idea what it is.
I had a look in the source of the bootloader, but it was to complex for me.

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: Problem with Reboot into bootloader
« Reply #2 on: February 11, 2015, 01:06:35 am »
I'm thinking @digispark should look at this.  Some symptoms seem like what I've seen on my brickings.

Are you on Win7?  Do you have a Win8 machine to try an upload?

Can you turn on the led in Setup and see if it comes up by any chance after the restart?  After the failure can you restart on Win8 or a USB powered plug instead of a computer?  May not relate - but if the light comes on then the code started and it might yield a clue.

Code: [Select]
void setup() {               
  pinMode(1, OUTPUT); //LED on Model A  or Pro
  digitalWrite(1, HIGH);
  SerialUSB.begin();
}

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Problem with Reboot into bootloader
« Reply #3 on: February 11, 2015, 10:02:56 am »
A hard reset does many things differently then a jump (as you all obviously know) - and I've never used the jump successfully with USB involved, I believe the issue is with clock calibration as Ralf mentions.

I haven't tried using a watchdog reset with USB but that might work better as it is much more like a real reset, there are some threads on here about using the watchdog.

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: Problem with Reboot into bootloader
« Reply #4 on: February 11, 2015, 02:07:56 pm »
For the Pro [oops this is an Original thread] DigiCDC works on Win7 but not Win8 - there is an open thread on this.  It seems the driver does act differently to my situation and Digistump is going to look into that as they are on a different code base to some degree.  How this relates to the Linux driver I haven't ever tried - and don't know how the driver code base was crafted.

The questions about the Win7 .vs. 8 were just to tie in or exclude the behavior I saw that was not wholly dissimilar to what you report.  The common thread I see is that I was making Bricked units - that I could reflash the bootloader too - or in some cases have Win8 just normally upload code that worked, until I plugged into my Win7 that has worked for months until all four on my units needed to be ReFlashed from this brick state.

During upload the 'bootloader software' performs the control and reset - using USB in some fashion

fh_

  • Newbie
  • *
  • Posts: 5
Re: Problem with Reboot into bootloader
« Reply #5 on: February 15, 2015, 11:20:52 am »
Thank you for alle the reply's.
I found a solution which is very simple and works - I use the watchdog:
Code: [Select]
#include <avr/wdt.h>
...
   wdt_enable(0);
   for(;;);
Works fine and is only a few bytes longer than the reboot function from the wiki.

fh

Some comment to the DigiCDC:
Works fine and stable for me under windows 7.
Important: usbser.sys must exist under /win/system32/drivers or you get an yellow question mark in the dervice manager.
(Which is not the case on a fresh windows 7)
You can save some byte if you change all Size and Count values to uint8_t in ringBuffer.h

smarg

  • Newbie
  • *
  • Posts: 1
Re: Problem with Reboot into bootloader
« Reply #6 on: June 12, 2015, 11:37:41 am »
hi, i just registered to say thank you fh_ for your solution! i too tried the reboot method and it did not work....your solution works - its simple and elegant!