diff --git a/app/src/processing/app/debug/BasicUploader.java b/app/src/processing/app/debug/BasicUploader.java index 27288b238..9db3ffa1e 100644 --- a/app/src/processing/app/debug/BasicUploader.java +++ b/app/src/processing/app/debug/BasicUploader.java @@ -91,7 +91,7 @@ public class BasicUploader extends Uploader { uploadPort = waitForUploadPort(uploadPort, before); } else { - Thread.sleep(2500); + Thread.sleep(1000); } } catch (SerialException e) { throw new RunnerException(e.getMessage()); @@ -136,8 +136,13 @@ public class BasicUploader extends Uploader { long timeout = System.currentTimeMillis() + 2000; while (timeout > System.currentTimeMillis()) { List portList = Serial.list(); - if (portList.contains(Preferences.get("serial.port"))) + String uploadPort = Preferences.get("serial.port"); + if (portList.contains(Preferences.get("serial.port"))) { + // Remove the magic baud rate (1200bps) to avoid + // future unwanted board resets + Serial.touchPort(uploadPort, 9600); break; + } Thread.sleep(100); } } diff --git a/hardware/arduino/sam/cores/arduino/Reset.cpp b/hardware/arduino/sam/cores/arduino/Reset.cpp new file mode 100644 index 000000000..bae2a9227 --- /dev/null +++ b/hardware/arduino/sam/cores/arduino/Reset.cpp @@ -0,0 +1,70 @@ +/* + Copyright (c) 2012 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include +#include "Reset.h" + +__attribute__ ((long_call, section (".ramfunc"))) +void banzai() { + // Disable all interrupts + __disable_irq(); + + // Set bootflag to run SAM-BA bootloader at restart + const int EEFC_FCMD_CGPB = 0x0C; + const int EEFC_KEY = 0x5A; + while (EFC0->EEFC_FSR & EEFC_FSR_FRDY == 0); + EFC0->EEFC_FCR = + EEFC_FCR_FCMD(EEFC_FCMD_CGPB) | + EEFC_FCR_FARG(1) | + EEFC_FCR_FKEY(EEFC_KEY); + while (EFC0->EEFC_FSR & EEFC_FSR_FRDY == 0); + + // From here flash memory is no more available. + + // Memory swap needs some time to stabilize + for (uint32_t i=0; i<1000000; i++) + // force compiler to not optimize this + __asm__ __volatile__(""); + + // BANZAIIIIIII!!! + const int RSTC_KEY = 0xA5; + RSTC->RSTC_CR = + RSTC_CR_KEY(RSTC_KEY) | + RSTC_CR_PROCRST | + RSTC_CR_PERRST; + + while (true); +} + +void ResetClass::initiate(int _ticks) { + ticks = _ticks; +} + +void ResetClass::cancel() { + ticks = -1; +} + +void ResetClass::tick() { + if (ticks == -1) + return; + ticks--; + if (ticks == 0) + banzai(); +} + +int ResetClass::ticks = -1; diff --git a/hardware/arduino/sam/cores/arduino/Reset.h b/hardware/arduino/sam/cores/arduino/Reset.h new file mode 100644 index 000000000..8d0e363cb --- /dev/null +++ b/hardware/arduino/sam/cores/arduino/Reset.h @@ -0,0 +1,33 @@ +/* + Copyright (c) 2012 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef RESET_H +#define RESET_H + +class ResetClass { +public: + static void initiate(int _ticks); + static void cancel(); + static void tick(); +private: + static int ticks; +}; + +extern ResetClass Reset; + +#endif diff --git a/hardware/arduino/sam/cores/arduino/USB/CDC.cpp b/hardware/arduino/sam/cores/arduino/USB/CDC.cpp index 3c4f9feaf..5515881c2 100644 --- a/hardware/arduino/sam/cores/arduino/USB/CDC.cpp +++ b/hardware/arduino/sam/cores/arduino/USB/CDC.cpp @@ -16,6 +16,7 @@ #include "Arduino.h" #include "USBAPI.h" +#include "Reset.h" #ifdef CDC_ENABLED @@ -73,38 +74,6 @@ int WEAK CDC_GetInterface(uint8_t* interfaceNum) return USBD_SendControl(0,&_cdcInterface,sizeof(_cdcInterface)); } -__attribute__ ((long_call, section (".ramfunc"))) -void banzai() { - // Disable all interrupts - __disable_irq(); - - // Set bootflag to run SAM-BA bootloader at restart - const int EEFC_FCMD_CGPB = 0x0C; - const int EEFC_KEY = 0x5A; - while (EFC0->EEFC_FSR & EEFC_FSR_FRDY == 0); - EFC0->EEFC_FCR = - EEFC_FCR_FCMD(EEFC_FCMD_CGPB) | - EEFC_FCR_FARG(1) | - EEFC_FCR_FKEY(EEFC_KEY); - while (EFC0->EEFC_FSR & EEFC_FSR_FRDY == 0); - - // From here flash memory is no more available. - - // Memory swap needs some time to stabilize - volatile uint32_t i; - for (i=0; i<1000000; i++); - - // BANZAIIIIIII!!! - const int RSTC_KEY = 0xA5; - RSTC->RSTC_CR = - RSTC_CR_KEY(RSTC_KEY) | - RSTC_CR_PROCRST | - RSTC_CR_PERRST | - RSTC_CR_EXTRST; - - while (true); -} - bool WEAK CDC_Setup(Setup& setup) { uint8_t r = setup.bRequest; @@ -136,7 +105,9 @@ bool WEAK CDC_Setup(Setup& setup) { // We check DTR state to determine if host port is open (bit 0 of lineState). if ((_usbLineInfo.lineState & 0x01) == 0) - banzai(); + Reset.initiate(240); + else + Reset.cancel(); } return true; } diff --git a/hardware/arduino/sam/cores/arduino/USB/USBCore.cpp b/hardware/arduino/sam/cores/arduino/USB/USBCore.cpp index 61113c20b..e4ecaa9ed 100644 --- a/hardware/arduino/sam/cores/arduino/USB/USBCore.cpp +++ b/hardware/arduino/sam/cores/arduino/USB/USBCore.cpp @@ -16,6 +16,7 @@ #include "Arduino.h" #include "USBAPI.h" +#include "Reset.h" #include //#define TRACE_CORE(x) x @@ -368,6 +369,8 @@ static bool USBD_SendDescriptor(Setup& setup) // Endpoint 0 interrupt static void USB_ISR(void) { + Reset.tick(); + // End of Reset if (Is_udd_reset()) {