From 703c8ac0efc91242085918f3cca3cec955dc5257 Mon Sep 17 00:00:00 2001
From: ejtagle <ejtagle@hotmail.com>
Date: Sun, 11 Mar 2018 15:17:55 -0300
Subject: [PATCH] Added Arduino-compatible kludge to the (ATMEL supplied) SAM3X
 USB stack we are now using so the Native port is also usable as a programming
 port on Arduino DUE and derived designs

---
 Marlin/src/HAL/HAL_DUE/usb/usb_task.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/Marlin/src/HAL/HAL_DUE/usb/usb_task.c b/Marlin/src/HAL/HAL_DUE/usb/usb_task.c
index c6f14081f9..bd004c9ca4 100644
--- a/Marlin/src/HAL/HAL_DUE/usb/usb_task.c
+++ b/Marlin/src/HAL/HAL_DUE/usb/usb_task.c
@@ -48,6 +48,7 @@
 #include "conf_usb.h"
 #include "udc.h"
 #include <Arduino.h>
+#include <Reset.h>
 
 static volatile bool main_b_msc_enable = false;
 static volatile bool main_b_cdc_enable = false;
@@ -80,11 +81,24 @@ void usb_task_cdc_rx_notify(const uint8_t port) { }
  *
  * \param cfg      line configuration
  */
-void usb_task_cdc_config(const uint8_t port, usb_cdc_line_coding_t *cfg) { }
+static uint16_t dwDTERate = 0;
+void usb_task_cdc_config(const uint8_t port, usb_cdc_line_coding_t *cfg) {
+    // Store last DTE rate
+    dwDTERate = cfg->dwDTERate;
+}
 
 void usb_task_cdc_set_dtr(const uint8_t port, const bool b_enable) {
-  if (b_enable) {
-  } else {
+  //  Implement Arduino-Compatible kludge to enter programming mode from
+  // the native port:
+  //  "Auto-reset into the bootloader is triggered when the port, already
+  // open at 1200 bps, is closed."
+    
+  if (1200 == dwDTERate) {
+    // We check DTR state to determine if host port is open (bit 0 of lineState).
+    if (!b_enable)
+      initiateReset(250);
+    else
+      cancelReset();
   }
 }