From 417be96f5270c0e32c20eb1f5bf102bfaa7aa3ae Mon Sep 17 00:00:00 2001
From: Michael DM Dryden <mdryden@chem.utoronto.ca>
Date: Mon, 12 Jun 2017 20:43:33 -0400
Subject: [PATCH] Add automatic software reboot when USB port closed.

---
 src/leds.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/leds.c b/src/leds.c
index 163ad70..ab94e1e 100644
--- a/src/leds.c
+++ b/src/leds.c
@@ -9,11 +9,22 @@
 #include "leds.h"
 #include "config/conf_board.h"
 #include <ioport.h>
+#include <util/atomic.h>
 
 void my_callback_cdc_set_rts(uint8_t port, uint8_t b_enable){
+    static uint8_t connected = 0;
+    
     #if BOARD_VER_MAJOR >= 1 && BOARD_VER_MINOR >= 2 && BOARD_VER_MICRO >=3
         ioport_set_pin_level(LED2, b_enable);
     #endif
+    
+    if (connected && !b_enable){
+        ATOMIC_BLOCK(ATOMIC_RESTORESTATE){
+            CCP = 0xD8;                        // Configuration change protection: allow protected IO regiser write
+            RST.CTRL = RST_SWRST_bm;           // Request software reset by writing to protected IO register
+        }
+    }
+    connected = b_enable;
 }
 
 
-- 
GitLab