diff --git a/boards.txt b/boards.txt index 411bda3bd3a9999fd57b831c4480a4535f23c633..70eaa9ae90a07dc8fbdf7977ce77f92c9952db61 100644 --- a/boards.txt +++ b/boards.txt @@ -28,8 +28,8 @@ arduino_zero_native.pid.0=0x004d arduino_zero_native.upload.tool=bossac arduino_zero_native.upload.protocol=sam-ba arduino_zero_native.upload.maximum_size=262144 -arduino_zero_native.upload.use_1200bps_touch=false -arduino_zero_native.upload.wait_for_upload_port=false +arduino_zero_native.upload.use_1200bps_touch=true +arduino_zero_native.upload.wait_for_upload_port=true arduino_zero_native.upload.native_usb=true arduino_zero_native.build.mcu=cortex-m0plus arduino_zero_native.build.f_cpu=48000000L diff --git a/cores/arduino/Reset.cpp b/cores/arduino/Reset.cpp index afb30d2f57ce2f22e5a1b0ec0274fb568349d037..679e0341ee20768bf202b9582a4932fbdff1638d 100644 --- a/cores/arduino/Reset.cpp +++ b/cores/arduino/Reset.cpp @@ -23,12 +23,28 @@ extern "C" { #endif +#define NVM_MEMORY ((volatile uint16_t *)0x000000) +#define APP_START 0x00002004 + +static inline bool nvmReady(void) { + return NVMCTRL->INTFLAG.reg & NVMCTRL_INTFLAG_READY; +} + __attribute__ ((long_call, section (".ramfunc"))) static void banzai() { // Disable all interrupts __disable_irq(); - // Reset the device + // Erase application + while (!nvmReady()) + ; + NVMCTRL->STATUS.reg |= NVMCTRL_STATUS_MASK; + NVMCTRL->ADDR.reg = (uintptr_t)&NVM_MEMORY[APP_START / 4]; + NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMD_ER | NVMCTRL_CTRLA_CMDEX_KEY; + while (!nvmReady()) + ; + + // Reset the device NVIC_SystemReset() ; while (true); diff --git a/cores/arduino/USB/CDC.cpp b/cores/arduino/USB/CDC.cpp index f113bc16d51bf87c7bf981e1e0d991384063cb7c..40fadaa2f004632771d523c61e670e18b93fd61d 100644 --- a/cores/arduino/USB/CDC.cpp +++ b/cores/arduino/USB/CDC.cpp @@ -119,21 +119,25 @@ bool WEAK CDC_Setup(Setup& setup) if (CDC_SET_LINE_CODING == r) { USBD_RecvControl((void*)&_usbLineInfo,7); - return false; } if (CDC_SET_CONTROL_LINE_STATE == r) { _usbLineInfo.lineState = setup.wValueL; + } + + if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r) + { // auto-reset into the bootloader is triggered when the port, already - // open at 1200 bps, is closed. - if (1200 == _usbLineInfo.dwDTERate) + // open at 1200 bps, is closed. We check DTR state to determine if host + // port is open (bit 0 of lineState). + if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) + { + initiateReset(250); + } + else { - // We check DTR state to determine if host port is open (bit 0 of lineState). - if ((_usbLineInfo.lineState & 0x01) == 0) - initiateReset(250); - else - cancelReset(); + cancelReset(); } return false; } diff --git a/cores/arduino/delay.c b/cores/arduino/delay.c index 2548f91edc1b659f61c0764edc9fbf243f263920..b9126c42e2c230397d1482fab8022b1d90e293de 100644 --- a/cores/arduino/delay.c +++ b/cores/arduino/delay.c @@ -58,10 +58,13 @@ void delay( uint32_t ms ) } while ( _ulTickCount - start <= ms ) ; } +#include "Reset.h" // for tickReset() + void SysTick_Handler( void ) { // Increment tick count each ms _ulTickCount++ ; + tickReset(); } #ifdef __cplusplus